pages.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const routes = require('../../app/routes');
  2. const storage = require('../storage');
  3. const state = require('../state');
  4. function stripEvents(str) {
  5. // For CSP we need to remove all the event handler placeholders.
  6. // It's ok, app.js will add them when it attaches to the DOM.
  7. return str.replace(/\son\w+=""/g, '');
  8. }
  9. module.exports = {
  10. index: async function(req, res) {
  11. const appState = await state(req);
  12. res.send(stripEvents(routes().toString('/blank', appState)));
  13. },
  14. blank: async function(req, res) {
  15. const appState = await state(req);
  16. res.send(stripEvents(routes().toString('/blank', appState)));
  17. },
  18. download: async function(req, res, next) {
  19. const id = req.params.id;
  20. const appState = await state(req);
  21. try {
  22. const { nonce, pwd } = await storage.metadata(id);
  23. res.set('WWW-Authenticate', `send-v1 ${nonce}`);
  24. res.send(
  25. stripEvents(
  26. routes().toString(
  27. `/download/${id}`,
  28. Object.assign(appState, {
  29. downloadMetadata: { nonce, pwd }
  30. })
  31. )
  32. )
  33. );
  34. } catch (e) {
  35. next();
  36. }
  37. },
  38. unsupported: async function(req, res) {
  39. const appState = await state(req);
  40. res.send(
  41. stripEvents(
  42. routes().toString(`/unsupported/${req.params.reason}`, appState)
  43. )
  44. );
  45. },
  46. legal: async function(req, res) {
  47. const appState = await state(req);
  48. res.send(stripEvents(routes().toString('/legal', appState)));
  49. },
  50. notfound: async function(req, res) {
  51. const appState = await state(req);
  52. res.status(404).send(stripEvents(routes().toString('/404', appState)));
  53. }
  54. };