apiController.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. var debug = require('debug')('ylt:server');
  2. var Q = require('q');
  3. var ylt = require('../../index');
  4. var ScreenshotHandler = require('../../screenshotHandler');
  5. var RunsQueue = require('../datastores/runsQueue');
  6. var RunsDatastore = require('../datastores/runsDatastore');
  7. var ResultsDatastore = require('../datastores/resultsDatastore');
  8. var ApiController = function(app) {
  9. 'use strict';
  10. var queue = new RunsQueue();
  11. var runsDatastore = new RunsDatastore();
  12. var resultsDatastore = new ResultsDatastore();
  13. // Create a new run
  14. app.post('/api/runs', function(req, res) {
  15. // Add http to the test URL
  16. if (req.body.url && req.body.url.toLowerCase().indexOf('http://') !== 0 && req.body.url.toLowerCase().indexOf('https://') !== 0) {
  17. req.body.url = 'http://' + req.body.url;
  18. }
  19. // Grab the test parameters and generate a random run ID
  20. var run = {
  21. runId: (Date.now()*1000 + Math.round(Math.random()*1000)).toString(36),
  22. params: {
  23. url: req.body.url,
  24. waitForResponse: req.body.waitForResponse !== false && req.body.waitForResponse !== 'false' && req.body.waitForResponse !== 0,
  25. partialResult: req.body.partialResult || null,
  26. screenshot: req.body.screenshot || false,
  27. jsTimeline: req.body.jsTimeline || false,
  28. device: req.body.device || 'desktop',
  29. waitForSelector: req.body.waitForSelector || null,
  30. cookie: req.body.cookie || null,
  31. authUser: req.body.authUser || null,
  32. authPass: req.body.authPass || null
  33. }
  34. };
  35. // Create a temporary folder to save the screenshot
  36. var screenshot;
  37. if (run.params.screenshot) {
  38. screenshot = ScreenshotHandler.getScreenshotTempFile();
  39. }
  40. // Add test to the testQueue
  41. debug('Adding test %s to the queue', run.runId);
  42. var queuePromise = queue.push(run.runId);
  43. // Save the run to the datastore
  44. runsDatastore.add(run, queuePromise.startingPosition);
  45. // Listening for position updates
  46. queuePromise.progress(function(position) {
  47. runsDatastore.updatePosition(run.runId, position);
  48. });
  49. // Let's start the run
  50. queuePromise.then(function() {
  51. runsDatastore.updatePosition(run.runId, 0);
  52. debug('Launching test %s on %s', run.runId, run.params.url);
  53. var runOptions = {
  54. screenshot: run.params.screenshot ? screenshot.getTmpFilePath() : false,
  55. jsDeepAnalysis: run.params.jsTimeline,
  56. device: run.params.device,
  57. waitForSelector: run.params.waitForSelector,
  58. cookie: run.params.cookie,
  59. authUser: run.params.authUser,
  60. authPass: run.params.authPass
  61. };
  62. return ylt(run.params.url, runOptions);
  63. })
  64. // Phantomas completed, let's save the screenshot if any
  65. .then(function(data) {
  66. debug('Success');
  67. data.runId = run.runId;
  68. // Some conditional steps are made if there is a screenshot
  69. var screenshotPromise = Q.resolve();
  70. if (run.params.screenshot) {
  71. // Replace the empty promise created earlier with Q.resolve()
  72. screenshotPromise = screenshot.toThumbnail(400)
  73. // Read screenshot
  74. .then(function(screenshotBuffer) {
  75. if (screenshotBuffer) {
  76. debug('Image optimized');
  77. data.screenshotBuffer = screenshotBuffer;
  78. // Official path to get the image
  79. data.screenshotUrl = '/api/results/' + data.runId + '/screenshot.jpg';
  80. }
  81. })
  82. // Delete screenshot temporary file
  83. .then(screenshot.deleteTmpFile);
  84. }
  85. // Let's continue
  86. screenshotPromise
  87. // Save results
  88. .then(function() {
  89. // Remove uneeded temp screenshot path
  90. delete data.params.options.screenshot;
  91. // Empty javascriptExecutionTree if not needed
  92. if (!run.params.jsTimeline) {
  93. data.javascriptExecutionTree = {};
  94. data.scrollExecutionTree = {};
  95. }
  96. // Remove tools results if not needed
  97. return resultsDatastore.saveResult(data);
  98. })
  99. // Mark as the run as complete and send the response if the request is still waiting
  100. .then(function() {
  101. debug('Result saved in datastore');
  102. runsDatastore.markAsComplete(run.runId);
  103. if (run.params.waitForResponse) {
  104. // If the user only wants a portion of the result (partialResult option)
  105. switch(run.params.partialResult) {
  106. case 'generalScores':
  107. res.redirect(302, '/api/results/' + run.runId + '/generalScores');
  108. break;
  109. case 'rules':
  110. res.redirect(302, '/api/results/' + run.runId + '/rules');
  111. break;
  112. case 'javascriptExecutionTree':
  113. res.redirect(302, '/api/results/' + run.runId + '/javascriptExecutionTree');
  114. break;
  115. case 'phantomas':
  116. res.redirect(302, '/api/results/' + run.runId + '/toolsResults/phantomas');
  117. break;
  118. default:
  119. res.redirect(302, '/api/results/' + run.runId);
  120. }
  121. }
  122. })
  123. .fail(function(err) {
  124. console.error('Test failed for URL: %s', run.params.url);
  125. console.error(err.toString());
  126. runsDatastore.markAsFailed(run.runId, err.toString());
  127. res.status(500).send('An error occured');
  128. });
  129. })
  130. .fail(function(err) {
  131. console.error('Test failed for URL: %s', run.params.url);
  132. console.error(err.toString());
  133. runsDatastore.markAsFailed(run.runId, err.toString());
  134. res.status(400).send('Bad request');
  135. })
  136. .finally(function() {
  137. queue.remove(run.runId);
  138. });
  139. // The user doesn't want to wait for the response, sending the run ID only
  140. if (!run.params.waitForResponse) {
  141. console.log('Sending response without waiting.');
  142. res.setHeader('Content-Type', 'application/json');
  143. res.send(JSON.stringify({runId: run.runId}));
  144. }
  145. });
  146. // Retrive one run by id
  147. app.get('/api/runs/:id', function(req, res) {
  148. var runId = req.params.id;
  149. var run = runsDatastore.get(runId);
  150. if (run) {
  151. res.setHeader('Content-Type', 'application/json');
  152. res.send(JSON.stringify(run, null, 2));
  153. } else {
  154. res.status(404).send('Not found');
  155. }
  156. });
  157. // Retrieve the list of all runs
  158. /*app.get('/api/runs', function(req, res) {
  159. // NOT YET
  160. });*/
  161. // Delete one run by id
  162. /*app.delete('/api/runs/:id', function(req, res) {
  163. deleteRun()
  164. });*/
  165. // Delete all
  166. /*app.delete('/api/runs', function(req, res) {
  167. purgeRuns()
  168. });
  169. // List all
  170. app.get('/api/runs', function(req, res) {
  171. listRuns()
  172. });
  173. // Exists
  174. app.head('/api/runs/:id', function(req, res) {
  175. existsX();
  176. // Returns 200 if the result exists or 404 if not
  177. });
  178. */
  179. // Retrive one result by id
  180. app.get('/api/results/:id', function(req, res) {
  181. getPartialResults(req.params.id, res, function(data) {
  182. return data;
  183. });
  184. });
  185. // Retrieve one result and return only the generalScores part of the response
  186. app.get('/api/results/:id/generalScores', function(req, res) {
  187. getPartialResults(req.params.id, res, function(data) {
  188. return data.scoreProfiles.generic;
  189. });
  190. });
  191. app.get('/api/results/:id/generalScores/:scoreProfile', function(req, res) {
  192. getPartialResults(req.params.id, res, function(data) {
  193. return data.scoreProfiles[req.params.scoreProfile];
  194. });
  195. });
  196. app.get('/api/results/:id/rules', function(req, res) {
  197. getPartialResults(req.params.id, res, function(data) {
  198. return data.rules;
  199. });
  200. });
  201. app.get('/api/results/:id/javascriptExecutionTree', function(req, res) {
  202. getPartialResults(req.params.id, res, function(data) {
  203. return data.javascriptExecutionTree;
  204. });
  205. });
  206. app.get('/api/results/:id/toolsResults/phantomas', function(req, res) {
  207. getPartialResults(req.params.id, res, function(data) {
  208. return data.toolsResults.phantomas;
  209. });
  210. });
  211. function getPartialResults(runId, res, partialGetterFn) {
  212. resultsDatastore.getResult(runId)
  213. .then(function(data) {
  214. var results = partialGetterFn(data);
  215. if (typeof results === 'undefined') {
  216. res.status(404).send('Not found');
  217. return;
  218. }
  219. res.setHeader('Content-Type', 'application/json');
  220. res.send(JSON.stringify(results, null, 2));
  221. }).fail(function() {
  222. res.status(404).send('Not found');
  223. });
  224. }
  225. // Retrive one result by id
  226. app.get('/api/results/:id/screenshot.jpg', function(req, res) {
  227. var runId = req.params.id;
  228. resultsDatastore.getScreenshot(runId)
  229. .then(function(screenshotBuffer) {
  230. res.setHeader('Content-Type', 'image/jpeg');
  231. res.send(screenshotBuffer);
  232. }).fail(function() {
  233. res.status(404).send('Not found');
  234. });
  235. });
  236. };
  237. module.exports = ApiController;