apiController.js 9.9 KB

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