initScript.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const html = require('choo/html');
  2. const raw = require('choo/html/raw');
  3. const config = require('./config');
  4. let sentry = '';
  5. if (config.sentry_id) {
  6. //eslint-disable-next-line node/no-missing-require
  7. const version = require('../dist/version.json');
  8. sentry = `
  9. var RAVEN_CONFIG = {
  10. release: '${version.version}',
  11. tags: {
  12. commit: '${version.commit}'
  13. },
  14. dataCallback: function (data) {
  15. var hash = window.location.hash;
  16. if (hash) {
  17. return JSON.parse(JSON.stringify(data).replace(new RegExp(hash.slice(1), 'g'), ''));
  18. }
  19. return data;
  20. }
  21. }
  22. var SENTRY_ID = '${config.sentry_id}';
  23. `;
  24. }
  25. module.exports = function(state) {
  26. const authConfig = state.authConfig
  27. ? `var AUTH_CONFIG = ${JSON.stringify(state.authConfig)};`
  28. : '';
  29. /* eslint-disable no-useless-escape */
  30. const jsconfig = `
  31. var isIE = /trident\\\/7\.|msie/i.test(navigator.userAgent);
  32. var isUnsupportedPage = /\\\/unsupported/.test(location.pathname);
  33. if (isIE && !isUnsupportedPage) {
  34. window.location.assign('/unsupported/ie');
  35. }
  36. if (
  37. // Firefox < 50
  38. /firefox/i.test(navigator.userAgent) &&
  39. parseInt(navigator.userAgent.match(/firefox\\/*([^\\n\\r]*)\./i)[1], 10) < 50
  40. ) {
  41. window.location.assign('/unsupported/outdated');
  42. }
  43. var LIMITS = {
  44. ANON: {
  45. MAX_FILE_SIZE: ${config.anon_max_file_size},
  46. MAX_DOWNLOADS: ${config.anon_max_downloads},
  47. MAX_EXPIRE_SECONDS: ${config.anon_max_expire_seconds},
  48. },
  49. MAX_FILE_SIZE: ${config.max_file_size},
  50. MAX_DOWNLOADS: ${config.max_downloads},
  51. MAX_EXPIRE_SECONDS: ${config.max_expire_seconds},
  52. MAX_FILES_PER_ARCHIVE: ${config.max_files_per_archive},
  53. MAX_ARCHIVES_PER_USER: ${config.max_archives_per_user}
  54. };
  55. var DEFAULTS = {
  56. DOWNLOAD_COUNTS: ${JSON.stringify(config.download_counts)},
  57. EXPIRE_TIMES_SECONDS: ${JSON.stringify(config.expire_times_seconds)},
  58. EXPIRE_SECONDS: ${config.default_expire_seconds}
  59. };
  60. const LOCALE = '${state.locale}';
  61. const downloadMetadata = ${
  62. state.downloadMetadata ? raw(JSON.stringify(state.downloadMetadata)) : '{}'
  63. };
  64. ${authConfig};
  65. ${sentry}
  66. `;
  67. return state.cspNonce
  68. ? html`
  69. <script nonce="${state.cspNonce}">
  70. ${raw(jsconfig)};
  71. </script>
  72. `
  73. : '';
  74. };