constants.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. 'use strict'
  2. const { MessageChannel, receiveMessageOnPort } = require('worker_threads')
  3. const corsSafeListedMethods = ['GET', 'HEAD', 'POST']
  4. const nullBodyStatus = [101, 204, 205, 304]
  5. const redirectStatus = [301, 302, 303, 307, 308]
  6. // https://fetch.spec.whatwg.org/#block-bad-port
  7. const badPorts = [
  8. '1', '7', '9', '11', '13', '15', '17', '19', '20', '21', '22', '23', '25', '37', '42', '43', '53', '69', '77', '79',
  9. '87', '95', '101', '102', '103', '104', '109', '110', '111', '113', '115', '117', '119', '123', '135', '137',
  10. '139', '143', '161', '179', '389', '427', '465', '512', '513', '514', '515', '526', '530', '531', '532',
  11. '540', '548', '554', '556', '563', '587', '601', '636', '989', '990', '993', '995', '1719', '1720', '1723',
  12. '2049', '3659', '4045', '5060', '5061', '6000', '6566', '6665', '6666', '6667', '6668', '6669', '6697',
  13. '10080'
  14. ]
  15. // https://w3c.github.io/webappsec-referrer-policy/#referrer-policies
  16. const referrerPolicy = [
  17. '',
  18. 'no-referrer',
  19. 'no-referrer-when-downgrade',
  20. 'same-origin',
  21. 'origin',
  22. 'strict-origin',
  23. 'origin-when-cross-origin',
  24. 'strict-origin-when-cross-origin',
  25. 'unsafe-url'
  26. ]
  27. const requestRedirect = ['follow', 'manual', 'error']
  28. const safeMethods = ['GET', 'HEAD', 'OPTIONS', 'TRACE']
  29. const requestMode = ['navigate', 'same-origin', 'no-cors', 'cors']
  30. const requestCredentials = ['omit', 'same-origin', 'include']
  31. const requestCache = [
  32. 'default',
  33. 'no-store',
  34. 'reload',
  35. 'no-cache',
  36. 'force-cache',
  37. 'only-if-cached'
  38. ]
  39. // https://fetch.spec.whatwg.org/#request-body-header-name
  40. const requestBodyHeader = [
  41. 'content-encoding',
  42. 'content-language',
  43. 'content-location',
  44. 'content-type',
  45. // See https://github.com/nodejs/undici/issues/2021
  46. // 'Content-Length' is a forbidden header name, which is typically
  47. // removed in the Headers implementation. However, undici doesn't
  48. // filter out headers, so we add it here.
  49. 'content-length'
  50. ]
  51. // https://fetch.spec.whatwg.org/#enumdef-requestduplex
  52. const requestDuplex = [
  53. 'half'
  54. ]
  55. // http://fetch.spec.whatwg.org/#forbidden-method
  56. const forbiddenMethods = ['CONNECT', 'TRACE', 'TRACK']
  57. const subresource = [
  58. 'audio',
  59. 'audioworklet',
  60. 'font',
  61. 'image',
  62. 'manifest',
  63. 'paintworklet',
  64. 'script',
  65. 'style',
  66. 'track',
  67. 'video',
  68. 'xslt',
  69. ''
  70. ]
  71. /** @type {globalThis['DOMException']} */
  72. const DOMException = globalThis.DOMException ?? (() => {
  73. // DOMException was only made a global in Node v17.0.0,
  74. // but fetch supports >= v16.8.
  75. try {
  76. atob('~')
  77. } catch (err) {
  78. return Object.getPrototypeOf(err).constructor
  79. }
  80. })()
  81. let channel
  82. /** @type {globalThis['structuredClone']} */
  83. const structuredClone =
  84. globalThis.structuredClone ??
  85. // https://github.com/nodejs/node/blob/b27ae24dcc4251bad726d9d84baf678d1f707fed/lib/internal/structured_clone.js
  86. // structuredClone was added in v17.0.0, but fetch supports v16.8
  87. function structuredClone (value, options = undefined) {
  88. if (arguments.length === 0) {
  89. throw new TypeError('missing argument')
  90. }
  91. if (!channel) {
  92. channel = new MessageChannel()
  93. }
  94. channel.port1.unref()
  95. channel.port2.unref()
  96. channel.port1.postMessage(value, options?.transfer)
  97. return receiveMessageOnPort(channel.port2).message
  98. }
  99. module.exports = {
  100. DOMException,
  101. structuredClone,
  102. subresource,
  103. forbiddenMethods,
  104. requestBodyHeader,
  105. referrerPolicy,
  106. requestRedirect,
  107. requestMode,
  108. requestCredentials,
  109. requestCache,
  110. redirectStatus,
  111. corsSafeListedMethods,
  112. nullBodyStatus,
  113. safeMethods,
  114. badPorts,
  115. requestDuplex
  116. }