apiController.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. // Don't worry if there's an error
  85. .fail(function(err) {
  86. debug('An error occured while creating the screenshot\'s thumbnail. Ignoring and continuing...');
  87. });
  88. }
  89. // Let's continue
  90. screenshotPromise
  91. // Save results
  92. .then(function() {
  93. // Remove uneeded temp screenshot path
  94. delete data.params.options.screenshot;
  95. // Empty javascriptExecutionTree if not needed
  96. if (!run.params.jsTimeline) {
  97. data.javascriptExecutionTree = {};
  98. data.scrollExecutionTree = {};
  99. }
  100. // Remove tools results if not needed
  101. return resultsDatastore.saveResult(data);
  102. })
  103. // Mark as the run as complete and send the response if the request is still waiting
  104. .then(function() {
  105. debug('Result saved in datastore');
  106. runsDatastore.markAsComplete(run.runId);
  107. if (run.params.waitForResponse) {
  108. // If the user only wants a portion of the result (partialResult option)
  109. switch(run.params.partialResult) {
  110. case 'generalScores':
  111. res.redirect(302, '/api/results/' + run.runId + '/generalScores');
  112. break;
  113. case 'rules':
  114. res.redirect(302, '/api/results/' + run.runId + '/rules');
  115. break;
  116. case 'javascriptExecutionTree':
  117. res.redirect(302, '/api/results/' + run.runId + '/javascriptExecutionTree');
  118. break;
  119. case 'phantomas':
  120. res.redirect(302, '/api/results/' + run.runId + '/toolsResults/phantomas');
  121. break;
  122. default:
  123. res.redirect(302, '/api/results/' + run.runId);
  124. }
  125. }
  126. })
  127. .fail(function(err) {
  128. console.error('Test failed for URL: %s', run.params.url);
  129. console.error(err.toString());
  130. runsDatastore.markAsFailed(run.runId, err.toString());
  131. res.status(500).send('An error occured');
  132. });
  133. })
  134. .fail(function(err) {
  135. console.error('Test failed for URL: %s', run.params.url);
  136. console.error(err.toString());
  137. runsDatastore.markAsFailed(run.runId, err.toString());
  138. res.status(400).send('Bad request');
  139. })
  140. .finally(function() {
  141. queue.remove(run.runId);
  142. });
  143. // The user doesn't want to wait for the response, sending the run ID only
  144. if (!run.params.waitForResponse) {
  145. console.log('Sending response without waiting.');
  146. res.setHeader('Content-Type', 'application/json');
  147. res.send(JSON.stringify({runId: run.runId}));
  148. }
  149. });
  150. // Retrive one run by id
  151. app.get('/api/runs/:id', function(req, res) {
  152. var runId = req.params.id;
  153. var run = runsDatastore.get(runId);
  154. if (run) {
  155. res.setHeader('Content-Type', 'application/json');
  156. res.send(JSON.stringify(run, null, 2));
  157. } else {
  158. res.status(404).send('Not found');
  159. }
  160. });
  161. // Retrieve the list of all runs
  162. /*app.get('/api/runs', function(req, res) {
  163. // NOT YET
  164. });*/
  165. // Delete one run by id
  166. /*app.delete('/api/runs/:id', function(req, res) {
  167. deleteRun()
  168. });*/
  169. // Delete all
  170. /*app.delete('/api/runs', function(req, res) {
  171. purgeRuns()
  172. });
  173. // List all
  174. app.get('/api/runs', function(req, res) {
  175. listRuns()
  176. });
  177. // Exists
  178. app.head('/api/runs/:id', function(req, res) {
  179. existsX();
  180. // Returns 200 if the result exists or 404 if not
  181. });
  182. */
  183. // Retrive one result by id
  184. app.get('/api/results/:id', function(req, res) {
  185. getPartialResults(req.params.id, res, function(data) {
  186. return data;
  187. });
  188. });
  189. // Retrieve one result and return only the generalScores part of the response
  190. app.get('/api/results/:id/generalScores', function(req, res) {
  191. getPartialResults(req.params.id, res, function(data) {
  192. return data.scoreProfiles.generic;
  193. });
  194. });
  195. app.get('/api/results/:id/generalScores/:scoreProfile', function(req, res) {
  196. getPartialResults(req.params.id, res, function(data) {
  197. return data.scoreProfiles[req.params.scoreProfile];
  198. });
  199. });
  200. app.get('/api/results/:id/rules', function(req, res) {
  201. getPartialResults(req.params.id, res, function(data) {
  202. return data.rules;
  203. });
  204. });
  205. app.get('/api/results/:id/javascriptExecutionTree', function(req, res) {
  206. getPartialResults(req.params.id, res, function(data) {
  207. return data.javascriptExecutionTree;
  208. });
  209. });
  210. app.get('/api/results/:id/toolsResults/phantomas', function(req, res) {
  211. getPartialResults(req.params.id, res, function(data) {
  212. return data.toolsResults.phantomas;
  213. });
  214. });
  215. function getPartialResults(runId, res, partialGetterFn) {
  216. resultsDatastore.getResult(runId)
  217. .then(function(data) {
  218. var results = partialGetterFn(data);
  219. if (typeof results === 'undefined') {
  220. res.status(404).send('Not found');
  221. return;
  222. }
  223. res.setHeader('Content-Type', 'application/json');
  224. res.send(JSON.stringify(results, null, 2));
  225. }).fail(function() {
  226. res.status(404).send('Not found');
  227. });
  228. }
  229. // Retrive one result by id
  230. app.get('/api/results/:id/screenshot.jpg', function(req, res) {
  231. var runId = req.params.id;
  232. resultsDatastore.getScreenshot(runId)
  233. .then(function(screenshotBuffer) {
  234. res.setHeader('Content-Type', 'image/jpeg');
  235. res.send(screenshotBuffer);
  236. }).fail(function() {
  237. res.status(404).send('Not found');
  238. });
  239. });
  240. };
  241. module.exports = ApiController;