dsh-model-proxy-switch 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (220) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +76 -2
  3. package/client.js +473 -0
  4. package/cordis.patch.yml +7 -0
  5. package/icon.svg +13 -0
  6. package/index.js +341 -0
  7. package/locale/en.json +25 -0
  8. package/locale/zh.json +25 -0
  9. package/package.json +55 -4
  10. package/vendor/http-proxy.js +542 -0
  11. package/vendor/node_modules/undici/LICENSE +21 -0
  12. package/vendor/node_modules/undici/README.md +741 -0
  13. package/vendor/node_modules/undici/docs/docs/api/Agent.md +84 -0
  14. package/vendor/node_modules/undici/docs/docs/api/BalancedPool.md +99 -0
  15. package/vendor/node_modules/undici/docs/docs/api/CacheStorage.md +30 -0
  16. package/vendor/node_modules/undici/docs/docs/api/CacheStore.md +164 -0
  17. package/vendor/node_modules/undici/docs/docs/api/Client.md +290 -0
  18. package/vendor/node_modules/undici/docs/docs/api/ClientStats.md +27 -0
  19. package/vendor/node_modules/undici/docs/docs/api/Connector.md +115 -0
  20. package/vendor/node_modules/undici/docs/docs/api/ContentType.md +57 -0
  21. package/vendor/node_modules/undici/docs/docs/api/Cookies.md +128 -0
  22. package/vendor/node_modules/undici/docs/docs/api/Debug.md +62 -0
  23. package/vendor/node_modules/undici/docs/docs/api/DiagnosticsChannel.md +317 -0
  24. package/vendor/node_modules/undici/docs/docs/api/Dispatcher.md +1393 -0
  25. package/vendor/node_modules/undici/docs/docs/api/EnvHttpProxyAgent.md +159 -0
  26. package/vendor/node_modules/undici/docs/docs/api/Errors.md +49 -0
  27. package/vendor/node_modules/undici/docs/docs/api/EventSource.md +45 -0
  28. package/vendor/node_modules/undici/docs/docs/api/Fetch.md +60 -0
  29. package/vendor/node_modules/undici/docs/docs/api/GlobalInstallation.md +139 -0
  30. package/vendor/node_modules/undici/docs/docs/api/H2CClient.md +263 -0
  31. package/vendor/node_modules/undici/docs/docs/api/MockAgent.md +603 -0
  32. package/vendor/node_modules/undici/docs/docs/api/MockCallHistory.md +197 -0
  33. package/vendor/node_modules/undici/docs/docs/api/MockCallHistoryLog.md +43 -0
  34. package/vendor/node_modules/undici/docs/docs/api/MockClient.md +81 -0
  35. package/vendor/node_modules/undici/docs/docs/api/MockErrors.md +12 -0
  36. package/vendor/node_modules/undici/docs/docs/api/MockPool.md +555 -0
  37. package/vendor/node_modules/undici/docs/docs/api/Pool.md +84 -0
  38. package/vendor/node_modules/undici/docs/docs/api/PoolStats.md +35 -0
  39. package/vendor/node_modules/undici/docs/docs/api/ProxyAgent.md +229 -0
  40. package/vendor/node_modules/undici/docs/docs/api/RedirectHandler.md +93 -0
  41. package/vendor/node_modules/undici/docs/docs/api/RetryAgent.md +50 -0
  42. package/vendor/node_modules/undici/docs/docs/api/RetryHandler.md +118 -0
  43. package/vendor/node_modules/undici/docs/docs/api/RoundRobinPool.md +145 -0
  44. package/vendor/node_modules/undici/docs/docs/api/SnapshotAgent.md +616 -0
  45. package/vendor/node_modules/undici/docs/docs/api/Socks5ProxyAgent.md +275 -0
  46. package/vendor/node_modules/undici/docs/docs/api/Util.md +25 -0
  47. package/vendor/node_modules/undici/docs/docs/api/WebSocket.md +141 -0
  48. package/vendor/node_modules/undici/docs/docs/api/api-lifecycle.md +91 -0
  49. package/vendor/node_modules/undici/docs/docs/best-practices/client-certificate.md +64 -0
  50. package/vendor/node_modules/undici/docs/docs/best-practices/crawling.md +58 -0
  51. package/vendor/node_modules/undici/docs/docs/best-practices/mocking-request.md +190 -0
  52. package/vendor/node_modules/undici/docs/docs/best-practices/proxy.md +127 -0
  53. package/vendor/node_modules/undici/docs/docs/best-practices/undici-vs-builtin-fetch.md +224 -0
  54. package/vendor/node_modules/undici/docs/docs/best-practices/writing-tests.md +63 -0
  55. package/vendor/node_modules/undici/index-fetch.js +65 -0
  56. package/vendor/node_modules/undici/index.d.ts +3 -0
  57. package/vendor/node_modules/undici/index.js +234 -0
  58. package/vendor/node_modules/undici/lib/api/abort-signal.js +59 -0
  59. package/vendor/node_modules/undici/lib/api/api-connect.js +110 -0
  60. package/vendor/node_modules/undici/lib/api/api-pipeline.js +252 -0
  61. package/vendor/node_modules/undici/lib/api/api-request.js +214 -0
  62. package/vendor/node_modules/undici/lib/api/api-stream.js +209 -0
  63. package/vendor/node_modules/undici/lib/api/api-upgrade.js +111 -0
  64. package/vendor/node_modules/undici/lib/api/index.js +7 -0
  65. package/vendor/node_modules/undici/lib/api/readable.js +580 -0
  66. package/vendor/node_modules/undici/lib/cache/memory-cache-store.js +279 -0
  67. package/vendor/node_modules/undici/lib/cache/sqlite-cache-store.js +467 -0
  68. package/vendor/node_modules/undici/lib/core/connect.js +159 -0
  69. package/vendor/node_modules/undici/lib/core/constants.js +143 -0
  70. package/vendor/node_modules/undici/lib/core/diagnostics.js +227 -0
  71. package/vendor/node_modules/undici/lib/core/errors.js +477 -0
  72. package/vendor/node_modules/undici/lib/core/request.js +485 -0
  73. package/vendor/node_modules/undici/lib/core/socks5-client.js +422 -0
  74. package/vendor/node_modules/undici/lib/core/socks5-utils.js +212 -0
  75. package/vendor/node_modules/undici/lib/core/symbols.js +76 -0
  76. package/vendor/node_modules/undici/lib/core/tree.js +160 -0
  77. package/vendor/node_modules/undici/lib/core/util.js +992 -0
  78. package/vendor/node_modules/undici/lib/dispatcher/agent.js +162 -0
  79. package/vendor/node_modules/undici/lib/dispatcher/balanced-pool.js +221 -0
  80. package/vendor/node_modules/undici/lib/dispatcher/client-h1.js +1744 -0
  81. package/vendor/node_modules/undici/lib/dispatcher/client-h2.js +1056 -0
  82. package/vendor/node_modules/undici/lib/dispatcher/client.js +669 -0
  83. package/vendor/node_modules/undici/lib/dispatcher/dispatcher-base.js +196 -0
  84. package/vendor/node_modules/undici/lib/dispatcher/dispatcher.js +48 -0
  85. package/vendor/node_modules/undici/lib/dispatcher/env-http-proxy-agent.js +146 -0
  86. package/vendor/node_modules/undici/lib/dispatcher/fixed-queue.js +135 -0
  87. package/vendor/node_modules/undici/lib/dispatcher/h2c-client.js +51 -0
  88. package/vendor/node_modules/undici/lib/dispatcher/pool-base.js +214 -0
  89. package/vendor/node_modules/undici/lib/dispatcher/pool.js +118 -0
  90. package/vendor/node_modules/undici/lib/dispatcher/proxy-agent.js +319 -0
  91. package/vendor/node_modules/undici/lib/dispatcher/retry-agent.js +35 -0
  92. package/vendor/node_modules/undici/lib/dispatcher/round-robin-pool.js +137 -0
  93. package/vendor/node_modules/undici/lib/dispatcher/socks5-proxy-agent.js +260 -0
  94. package/vendor/node_modules/undici/lib/encoding/index.js +33 -0
  95. package/vendor/node_modules/undici/lib/global.js +59 -0
  96. package/vendor/node_modules/undici/lib/handler/cache-handler.js +824 -0
  97. package/vendor/node_modules/undici/lib/handler/cache-revalidation-handler.js +124 -0
  98. package/vendor/node_modules/undici/lib/handler/decorator-handler.js +67 -0
  99. package/vendor/node_modules/undici/lib/handler/deduplication-handler.js +504 -0
  100. package/vendor/node_modules/undici/lib/handler/redirect-handler.js +245 -0
  101. package/vendor/node_modules/undici/lib/handler/retry-handler.js +504 -0
  102. package/vendor/node_modules/undici/lib/handler/unwrap-handler.js +106 -0
  103. package/vendor/node_modules/undici/lib/handler/wrap-handler.js +105 -0
  104. package/vendor/node_modules/undici/lib/interceptor/cache.js +621 -0
  105. package/vendor/node_modules/undici/lib/interceptor/decompress.js +561 -0
  106. package/vendor/node_modules/undici/lib/interceptor/deduplicate.js +117 -0
  107. package/vendor/node_modules/undici/lib/interceptor/dns.js +571 -0
  108. package/vendor/node_modules/undici/lib/interceptor/dump.js +99 -0
  109. package/vendor/node_modules/undici/lib/interceptor/redirect.js +21 -0
  110. package/vendor/node_modules/undici/lib/interceptor/response-error.js +95 -0
  111. package/vendor/node_modules/undici/lib/interceptor/retry.js +19 -0
  112. package/vendor/node_modules/undici/lib/llhttp/.gitkeep +0 -0
  113. package/vendor/node_modules/undici/lib/llhttp/constants.d.ts +195 -0
  114. package/vendor/node_modules/undici/lib/llhttp/constants.js +531 -0
  115. package/vendor/node_modules/undici/lib/llhttp/llhttp-wasm.js +15 -0
  116. package/vendor/node_modules/undici/lib/llhttp/llhttp_simd-wasm.js +15 -0
  117. package/vendor/node_modules/undici/lib/llhttp/utils.d.ts +2 -0
  118. package/vendor/node_modules/undici/lib/llhttp/utils.js +12 -0
  119. package/vendor/node_modules/undici/lib/mock/mock-agent.js +232 -0
  120. package/vendor/node_modules/undici/lib/mock/mock-call-history.js +248 -0
  121. package/vendor/node_modules/undici/lib/mock/mock-client.js +68 -0
  122. package/vendor/node_modules/undici/lib/mock/mock-errors.js +29 -0
  123. package/vendor/node_modules/undici/lib/mock/mock-interceptor.js +209 -0
  124. package/vendor/node_modules/undici/lib/mock/mock-pool.js +68 -0
  125. package/vendor/node_modules/undici/lib/mock/mock-symbols.js +32 -0
  126. package/vendor/node_modules/undici/lib/mock/mock-utils.js +486 -0
  127. package/vendor/node_modules/undici/lib/mock/pending-interceptors-formatter.js +43 -0
  128. package/vendor/node_modules/undici/lib/mock/snapshot-agent.js +353 -0
  129. package/vendor/node_modules/undici/lib/mock/snapshot-recorder.js +588 -0
  130. package/vendor/node_modules/undici/lib/mock/snapshot-utils.js +158 -0
  131. package/vendor/node_modules/undici/lib/util/cache.js +676 -0
  132. package/vendor/node_modules/undici/lib/util/date.js +670 -0
  133. package/vendor/node_modules/undici/lib/util/promise.js +28 -0
  134. package/vendor/node_modules/undici/lib/util/runtime-features.js +124 -0
  135. package/vendor/node_modules/undici/lib/util/stats.js +32 -0
  136. package/vendor/node_modules/undici/lib/util/timers.js +425 -0
  137. package/vendor/node_modules/undici/lib/web/cache/cache.js +864 -0
  138. package/vendor/node_modules/undici/lib/web/cache/cachestorage.js +152 -0
  139. package/vendor/node_modules/undici/lib/web/cache/util.js +45 -0
  140. package/vendor/node_modules/undici/lib/web/cookies/constants.js +12 -0
  141. package/vendor/node_modules/undici/lib/web/cookies/index.js +201 -0
  142. package/vendor/node_modules/undici/lib/web/cookies/parse.js +315 -0
  143. package/vendor/node_modules/undici/lib/web/cookies/util.js +371 -0
  144. package/vendor/node_modules/undici/lib/web/eventsource/eventsource-stream.js +575 -0
  145. package/vendor/node_modules/undici/lib/web/eventsource/eventsource.js +504 -0
  146. package/vendor/node_modules/undici/lib/web/eventsource/util.js +29 -0
  147. package/vendor/node_modules/undici/lib/web/fetch/LICENSE +21 -0
  148. package/vendor/node_modules/undici/lib/web/fetch/body.js +509 -0
  149. package/vendor/node_modules/undici/lib/web/fetch/constants.js +131 -0
  150. package/vendor/node_modules/undici/lib/web/fetch/data-url.js +596 -0
  151. package/vendor/node_modules/undici/lib/web/fetch/formdata-parser.js +586 -0
  152. package/vendor/node_modules/undici/lib/web/fetch/formdata.js +259 -0
  153. package/vendor/node_modules/undici/lib/web/fetch/global.js +40 -0
  154. package/vendor/node_modules/undici/lib/web/fetch/headers.js +719 -0
  155. package/vendor/node_modules/undici/lib/web/fetch/index.js +2413 -0
  156. package/vendor/node_modules/undici/lib/web/fetch/request.js +1115 -0
  157. package/vendor/node_modules/undici/lib/web/fetch/response.js +641 -0
  158. package/vendor/node_modules/undici/lib/web/fetch/util.js +1522 -0
  159. package/vendor/node_modules/undici/lib/web/infra/index.js +243 -0
  160. package/vendor/node_modules/undici/lib/web/subresource-integrity/Readme.md +9 -0
  161. package/vendor/node_modules/undici/lib/web/subresource-integrity/subresource-integrity.js +307 -0
  162. package/vendor/node_modules/undici/lib/web/webidl/index.js +1006 -0
  163. package/vendor/node_modules/undici/lib/web/websocket/connection.js +339 -0
  164. package/vendor/node_modules/undici/lib/web/websocket/constants.js +126 -0
  165. package/vendor/node_modules/undici/lib/web/websocket/events.js +331 -0
  166. package/vendor/node_modules/undici/lib/web/websocket/frame.js +133 -0
  167. package/vendor/node_modules/undici/lib/web/websocket/permessage-deflate.js +105 -0
  168. package/vendor/node_modules/undici/lib/web/websocket/receiver.js +520 -0
  169. package/vendor/node_modules/undici/lib/web/websocket/sender.js +109 -0
  170. package/vendor/node_modules/undici/lib/web/websocket/stream/websocketerror.js +104 -0
  171. package/vendor/node_modules/undici/lib/web/websocket/stream/websocketstream.js +496 -0
  172. package/vendor/node_modules/undici/lib/web/websocket/util.js +347 -0
  173. package/vendor/node_modules/undici/lib/web/websocket/websocket.js +758 -0
  174. package/vendor/node_modules/undici/package.json +152 -0
  175. package/vendor/node_modules/undici/scripts/strip-comments.js +10 -0
  176. package/vendor/node_modules/undici/types/README.md +6 -0
  177. package/vendor/node_modules/undici/types/agent.d.ts +32 -0
  178. package/vendor/node_modules/undici/types/api.d.ts +43 -0
  179. package/vendor/node_modules/undici/types/balanced-pool.d.ts +30 -0
  180. package/vendor/node_modules/undici/types/cache-interceptor.d.ts +179 -0
  181. package/vendor/node_modules/undici/types/cache.d.ts +36 -0
  182. package/vendor/node_modules/undici/types/client-stats.d.ts +15 -0
  183. package/vendor/node_modules/undici/types/client.d.ts +153 -0
  184. package/vendor/node_modules/undici/types/connector.d.ts +36 -0
  185. package/vendor/node_modules/undici/types/content-type.d.ts +21 -0
  186. package/vendor/node_modules/undici/types/cookies.d.ts +30 -0
  187. package/vendor/node_modules/undici/types/diagnostics-channel.d.ts +74 -0
  188. package/vendor/node_modules/undici/types/dispatcher.d.ts +275 -0
  189. package/vendor/node_modules/undici/types/env-http-proxy-agent.d.ts +22 -0
  190. package/vendor/node_modules/undici/types/errors.d.ts +177 -0
  191. package/vendor/node_modules/undici/types/eventsource.d.ts +66 -0
  192. package/vendor/node_modules/undici/types/fetch.d.ts +231 -0
  193. package/vendor/node_modules/undici/types/formdata.d.ts +114 -0
  194. package/vendor/node_modules/undici/types/global-dispatcher.d.ts +9 -0
  195. package/vendor/node_modules/undici/types/global-origin.d.ts +7 -0
  196. package/vendor/node_modules/undici/types/h2c-client.d.ts +73 -0
  197. package/vendor/node_modules/undici/types/handlers.d.ts +14 -0
  198. package/vendor/node_modules/undici/types/header.d.ts +160 -0
  199. package/vendor/node_modules/undici/types/index.d.ts +91 -0
  200. package/vendor/node_modules/undici/types/interceptors.d.ts +82 -0
  201. package/vendor/node_modules/undici/types/mock-agent.d.ts +68 -0
  202. package/vendor/node_modules/undici/types/mock-call-history.d.ts +111 -0
  203. package/vendor/node_modules/undici/types/mock-client.d.ts +27 -0
  204. package/vendor/node_modules/undici/types/mock-errors.d.ts +12 -0
  205. package/vendor/node_modules/undici/types/mock-interceptor.d.ts +94 -0
  206. package/vendor/node_modules/undici/types/mock-pool.d.ts +27 -0
  207. package/vendor/node_modules/undici/types/patch.d.ts +29 -0
  208. package/vendor/node_modules/undici/types/pool-stats.d.ts +19 -0
  209. package/vendor/node_modules/undici/types/pool.d.ts +41 -0
  210. package/vendor/node_modules/undici/types/proxy-agent.d.ts +29 -0
  211. package/vendor/node_modules/undici/types/readable.d.ts +68 -0
  212. package/vendor/node_modules/undici/types/retry-agent.d.ts +8 -0
  213. package/vendor/node_modules/undici/types/retry-handler.d.ts +125 -0
  214. package/vendor/node_modules/undici/types/round-robin-pool.d.ts +41 -0
  215. package/vendor/node_modules/undici/types/snapshot-agent.d.ts +109 -0
  216. package/vendor/node_modules/undici/types/socks5-proxy-agent.d.ts +25 -0
  217. package/vendor/node_modules/undici/types/util.d.ts +18 -0
  218. package/vendor/node_modules/undici/types/utility.d.ts +7 -0
  219. package/vendor/node_modules/undici/types/webidl.d.ts +347 -0
  220. package/vendor/node_modules/undici/types/websocket.d.ts +188 -0
@@ -0,0 +1,243 @@
1
+ 'use strict'
2
+
3
+ const assert = require('node:assert')
4
+ const { utf8DecodeBytes } = require('../../encoding')
5
+
6
+ /**
7
+ * @param {(char: string) => boolean} condition
8
+ * @param {string} input
9
+ * @param {{ position: number }} position
10
+ * @returns {string}
11
+ *
12
+ * @see https://infra.spec.whatwg.org/#collect-a-sequence-of-code-points
13
+ */
14
+ function collectASequenceOfCodePoints (condition, input, position) {
15
+ // 1. Let result be the empty string.
16
+ let result = ''
17
+
18
+ // 2. While position doesn’t point past the end of input and the
19
+ // code point at position within input meets the condition condition:
20
+ while (position.position < input.length && condition(input[position.position])) {
21
+ // 1. Append that code point to the end of result.
22
+ result += input[position.position]
23
+
24
+ // 2. Advance position by 1.
25
+ position.position++
26
+ }
27
+
28
+ // 3. Return result.
29
+ return result
30
+ }
31
+
32
+ /**
33
+ * A faster collectASequenceOfCodePoints that only works when comparing a single character.
34
+ * @param {string} char
35
+ * @param {string} input
36
+ * @param {{ position: number }} position
37
+ * @returns {string}
38
+ *
39
+ * @see https://infra.spec.whatwg.org/#collect-a-sequence-of-code-points
40
+ */
41
+ function collectASequenceOfCodePointsFast (char, input, position) {
42
+ const idx = input.indexOf(char, position.position)
43
+ const start = position.position
44
+
45
+ if (idx === -1) {
46
+ position.position = input.length
47
+ return input.slice(start)
48
+ }
49
+
50
+ position.position = idx
51
+ return input.slice(start, position.position)
52
+ }
53
+
54
+ const ASCII_WHITESPACE_REPLACE_REGEX = /[\u0009\u000A\u000C\u000D\u0020]/g // eslint-disable-line no-control-regex
55
+ const nonASCIIRegex = /[^\x00-\x7F]/ // eslint-disable-line no-control-regex
56
+
57
+ /**
58
+ * @param {string} str
59
+ * @returns {string}
60
+ *
61
+ * @see https://infra.spec.whatwg.org/#ascii-lowercase
62
+ */
63
+ function asciiLowercase (str) {
64
+ return nonASCIIRegex.test(str)
65
+ ? str.replace(/[A-Z]+/g, (upper) => upper.toLowerCase())
66
+ : str.toLowerCase()
67
+ }
68
+
69
+ /**
70
+ * @param {string} data
71
+ * @returns {Uint8Array | 'failure'}
72
+ *
73
+ * @see https://infra.spec.whatwg.org/#forgiving-base64-decode
74
+ */
75
+ function forgivingBase64 (data) {
76
+ // 1. Remove all ASCII whitespace from data.
77
+ data = data.replace(ASCII_WHITESPACE_REPLACE_REGEX, '')
78
+
79
+ let dataLength = data.length
80
+ // 2. If data’s code point length divides by 4 leaving
81
+ // no remainder, then:
82
+ if (dataLength % 4 === 0) {
83
+ // 1. If data ends with one or two U+003D (=) code points,
84
+ // then remove them from data.
85
+ if (data.charCodeAt(dataLength - 1) === 0x003D) {
86
+ --dataLength
87
+ if (data.charCodeAt(dataLength - 1) === 0x003D) {
88
+ --dataLength
89
+ }
90
+ }
91
+ }
92
+
93
+ // 3. If data’s code point length divides by 4 leaving
94
+ // a remainder of 1, then return failure.
95
+ if (dataLength % 4 === 1) {
96
+ return 'failure'
97
+ }
98
+
99
+ // 4. If data contains a code point that is not one of
100
+ // U+002B (+)
101
+ // U+002F (/)
102
+ // ASCII alphanumeric
103
+ // then return failure.
104
+ if (/[^+/0-9A-Za-z]/.test(data.length === dataLength ? data : data.substring(0, dataLength))) {
105
+ return 'failure'
106
+ }
107
+
108
+ const buffer = Buffer.from(data, 'base64')
109
+ return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength)
110
+ }
111
+
112
+ /**
113
+ * @param {number} char
114
+ * @returns {boolean}
115
+ *
116
+ * @see https://infra.spec.whatwg.org/#ascii-whitespace
117
+ */
118
+ function isASCIIWhitespace (char) {
119
+ return (
120
+ char === 0x09 || // \t
121
+ char === 0x0a || // \n
122
+ char === 0x0c || // \f
123
+ char === 0x0d || // \r
124
+ char === 0x20 // space
125
+ )
126
+ }
127
+
128
+ /**
129
+ * @param {Uint8Array} input
130
+ * @returns {string}
131
+ *
132
+ * @see https://infra.spec.whatwg.org/#isomorphic-decode
133
+ */
134
+ function isomorphicDecode (input) {
135
+ // 1. To isomorphic decode a byte sequence input, return a string whose code point
136
+ // length is equal to input’s length and whose code points have the same values
137
+ // as the values of input’s bytes, in the same order.
138
+ const length = input.length
139
+ if ((2 << 15) - 1 > length) {
140
+ return String.fromCharCode.apply(null, input)
141
+ }
142
+ let result = ''
143
+ let i = 0
144
+ let addition = (2 << 15) - 1
145
+ while (i < length) {
146
+ if (i + addition > length) {
147
+ addition = length - i
148
+ }
149
+ result += String.fromCharCode.apply(null, input.subarray(i, i += addition))
150
+ }
151
+ return result
152
+ }
153
+
154
+ const invalidIsomorphicEncodeValueRegex = /[^\x00-\xFF]/ // eslint-disable-line no-control-regex
155
+
156
+ /**
157
+ * @param {string} input
158
+ * @returns {string}
159
+ *
160
+ * @see https://infra.spec.whatwg.org/#isomorphic-encode
161
+ */
162
+ function isomorphicEncode (input) {
163
+ // 1. Assert: input contains no code points greater than U+00FF.
164
+ assert(!invalidIsomorphicEncodeValueRegex.test(input))
165
+
166
+ // 2. Return a byte sequence whose length is equal to input’s code
167
+ // point length and whose bytes have the same values as the
168
+ // values of input’s code points, in the same order
169
+ return input
170
+ }
171
+
172
+ /**
173
+ * @see https://infra.spec.whatwg.org/#parse-json-bytes-to-a-javascript-value
174
+ * @param {Uint8Array} bytes
175
+ */
176
+ function parseJSONFromBytes (bytes) {
177
+ return JSON.parse(utf8DecodeBytes(bytes))
178
+ }
179
+
180
+ /**
181
+ * @param {string} str
182
+ * @param {boolean} [leading=true]
183
+ * @param {boolean} [trailing=true]
184
+ * @returns {string}
185
+ *
186
+ * @see https://infra.spec.whatwg.org/#strip-leading-and-trailing-ascii-whitespace
187
+ */
188
+ function removeASCIIWhitespace (str, leading = true, trailing = true) {
189
+ return removeChars(str, leading, trailing, isASCIIWhitespace)
190
+ }
191
+
192
+ /**
193
+ * @param {string} str
194
+ * @param {boolean} leading
195
+ * @param {boolean} trailing
196
+ * @param {(charCode: number) => boolean} predicate
197
+ * @returns {string}
198
+ */
199
+ function removeChars (str, leading, trailing, predicate) {
200
+ let lead = 0
201
+ let trail = str.length - 1
202
+
203
+ if (leading) {
204
+ while (lead < str.length && predicate(str.charCodeAt(lead))) lead++
205
+ }
206
+
207
+ if (trailing) {
208
+ while (trail > 0 && predicate(str.charCodeAt(trail))) trail--
209
+ }
210
+
211
+ return lead === 0 && trail === str.length - 1 ? str : str.slice(lead, trail + 1)
212
+ }
213
+
214
+ // https://infra.spec.whatwg.org/#serialize-a-javascript-value-to-a-json-string
215
+ function serializeJavascriptValueToJSONString (value) {
216
+ // 1. Let result be ? Call(%JSON.stringify%, undefined, « value »).
217
+ const result = JSON.stringify(value)
218
+
219
+ // 2. If result is undefined, then throw a TypeError.
220
+ if (result === undefined) {
221
+ throw new TypeError('Value is not JSON serializable')
222
+ }
223
+
224
+ // 3. Assert: result is a string.
225
+ assert(typeof result === 'string')
226
+
227
+ // 4. Return result.
228
+ return result
229
+ }
230
+
231
+ module.exports = {
232
+ asciiLowercase,
233
+ collectASequenceOfCodePoints,
234
+ collectASequenceOfCodePointsFast,
235
+ forgivingBase64,
236
+ isASCIIWhitespace,
237
+ isomorphicDecode,
238
+ isomorphicEncode,
239
+ parseJSONFromBytes,
240
+ removeASCIIWhitespace,
241
+ removeChars,
242
+ serializeJavascriptValueToJSONString
243
+ }
@@ -0,0 +1,9 @@
1
+ # Subresource Integrity
2
+
3
+ based on Editor’s Draft, 12 June 2025
4
+
5
+ This module provides support for Subresource Integrity (SRI) in the context of web fetch operations. SRI is a security feature that allows clients to verify that fetched resources are delivered without unexpected manipulation.
6
+
7
+ ## Links
8
+
9
+ - [Subresource Integrity](https://w3c.github.io/webappsec-subresource-integrity/)
@@ -0,0 +1,307 @@
1
+ 'use strict'
2
+
3
+ const assert = require('node:assert')
4
+ const { runtimeFeatures } = require('../../util/runtime-features.js')
5
+ const { asciiLowercase } = require('../infra/index.js')
6
+
7
+ /**
8
+ * @typedef {object} Metadata
9
+ * @property {SRIHashAlgorithm} alg - The algorithm used for the hash.
10
+ * @property {string} val - The base64-encoded hash value.
11
+ */
12
+
13
+ /**
14
+ * @typedef {Metadata[]} MetadataList
15
+ */
16
+
17
+ /**
18
+ * @typedef {('sha256' | 'sha384' | 'sha512')} SRIHashAlgorithm
19
+ */
20
+
21
+ /**
22
+ * @type {Map<SRIHashAlgorithm, number>}
23
+ *
24
+ * The valid SRI hash algorithm token set is the ordered set « "sha256",
25
+ * "sha384", "sha512" » (corresponding to SHA-256, SHA-384, and SHA-512
26
+ * respectively). The ordering of this set is meaningful, with stronger
27
+ * algorithms appearing later in the set.
28
+ *
29
+ * @see https://w3c.github.io/webappsec-subresource-integrity/#valid-sri-hash-algorithm-token-set
30
+ */
31
+ const validSRIHashAlgorithmTokenSet = new Map([['sha256', 0], ['sha384', 1], ['sha512', 2]])
32
+
33
+ // https://nodejs.org/api/crypto.html#determining-if-crypto-support-is-unavailable
34
+ /** @type {import('node:crypto')} */
35
+ let crypto
36
+
37
+ if (runtimeFeatures.has('crypto')) {
38
+ crypto = require('node:crypto')
39
+ const cryptoHashes = crypto.getHashes()
40
+
41
+ // If no hashes are available, we cannot support SRI.
42
+ if (cryptoHashes.length === 0) {
43
+ validSRIHashAlgorithmTokenSet.clear()
44
+ }
45
+
46
+ for (const algorithm of validSRIHashAlgorithmTokenSet.keys()) {
47
+ // If the algorithm is not supported, remove it from the list.
48
+ if (cryptoHashes.includes(algorithm) === false) {
49
+ validSRIHashAlgorithmTokenSet.delete(algorithm)
50
+ }
51
+ }
52
+ } else {
53
+ // If crypto is not available, we cannot support SRI.
54
+ validSRIHashAlgorithmTokenSet.clear()
55
+ }
56
+
57
+ /**
58
+ * @typedef GetSRIHashAlgorithmIndex
59
+ * @type {(algorithm: SRIHashAlgorithm) => number}
60
+ * @param {SRIHashAlgorithm} algorithm
61
+ * @returns {number} The index of the algorithm in the valid SRI hash algorithm
62
+ * token set.
63
+ */
64
+
65
+ const getSRIHashAlgorithmIndex = /** @type {GetSRIHashAlgorithmIndex} */ (Map.prototype.get.bind(
66
+ validSRIHashAlgorithmTokenSet))
67
+
68
+ /**
69
+ * @typedef IsValidSRIHashAlgorithm
70
+ * @see https://w3c.github.io/webappsec-subresource-integrity/#valid-sri-hash-algorithm-token
71
+ * @type {(algorithm: string) => algorithm is SRIHashAlgorithm}
72
+ * @param {*} algorithm
73
+ * @returns {algorithm is SRIHashAlgorithm}
74
+ */
75
+ const isValidSRIHashAlgorithm = (algorithm) => validSRIHashAlgorithmTokenSet.has(asciiLowercase(algorithm))
76
+
77
+ /**
78
+ * @param {Uint8Array} bytes
79
+ * @param {string} metadataList
80
+ * @returns {boolean}
81
+ *
82
+ * @see https://w3c.github.io/webappsec-subresource-integrity/#does-response-match-metadatalist
83
+ */
84
+ const bytesMatch = runtimeFeatures.has('crypto') === false || validSRIHashAlgorithmTokenSet.size === 0
85
+ // If node is not built with OpenSSL support, we cannot check
86
+ // a request's integrity, so allow it by default (the spec will
87
+ // allow requests if an invalid hash is given, as precedence).
88
+ ? () => true
89
+ : (bytes, metadataList) => {
90
+ // 1. Let parsedMetadata be the result of parsing metadataList.
91
+ const parsedMetadata = parseMetadata(metadataList)
92
+
93
+ // 2. If parsedMetadata is empty set, return true.
94
+ if (parsedMetadata.length === 0) {
95
+ return true
96
+ }
97
+
98
+ // 3. Let metadata be the result of getting the strongest
99
+ // metadata from parsedMetadata.
100
+ const metadata = getStrongestMetadata(parsedMetadata)
101
+
102
+ // 4. For each item in metadata:
103
+ for (const item of metadata) {
104
+ // 1. Let algorithm be the item["alg"].
105
+ const algorithm = item.alg
106
+
107
+ // 2. Let expectedValue be the item["val"].
108
+ const expectedValue = item.val
109
+
110
+ // See https://github.com/web-platform-tests/wpt/commit/e4c5cc7a5e48093220528dfdd1c4012dc3837a0e
111
+ // "be liberal with padding". This is annoying, and it's not even in the spec.
112
+
113
+ // 3. Let actualValue be the result of applying algorithm to bytes .
114
+ const actualValue = applyAlgorithmToBytes(algorithm, bytes)
115
+
116
+ // 4. If actualValue is a case-sensitive match for expectedValue,
117
+ // return true.
118
+ if (caseSensitiveMatch(actualValue, expectedValue)) {
119
+ return true
120
+ }
121
+ }
122
+
123
+ // 5. Return false.
124
+ return false
125
+ }
126
+
127
+ /**
128
+ * @param {MetadataList} metadataList
129
+ * @returns {MetadataList} The strongest hash algorithm from the metadata list.
130
+ */
131
+ function getStrongestMetadata (metadataList) {
132
+ // 1. Let result be the empty set and strongest be the empty string.
133
+ const result = []
134
+ /** @type {Metadata|null} */
135
+ let strongest = null
136
+
137
+ // 2. For each item in set:
138
+ for (const item of metadataList) {
139
+ // 1. Assert: item["alg"] is a valid SRI hash algorithm token.
140
+ assert(isValidSRIHashAlgorithm(item.alg), 'Invalid SRI hash algorithm token')
141
+
142
+ // 2. If result is the empty set, then:
143
+ if (result.length === 0) {
144
+ // 1. Append item to result.
145
+ result.push(item)
146
+
147
+ // 2. Set strongest to item.
148
+ strongest = item
149
+
150
+ // 3. Continue.
151
+ continue
152
+ }
153
+
154
+ // 3. Let currentAlgorithm be strongest["alg"], and currentAlgorithmIndex be
155
+ // the index of currentAlgorithm in the valid SRI hash algorithm token set.
156
+ const currentAlgorithm = /** @type {Metadata} */ (strongest).alg
157
+ const currentAlgorithmIndex = getSRIHashAlgorithmIndex(currentAlgorithm)
158
+
159
+ // 4. Let newAlgorithm be the item["alg"], and newAlgorithmIndex be the
160
+ // index of newAlgorithm in the valid SRI hash algorithm token set.
161
+ const newAlgorithm = item.alg
162
+ const newAlgorithmIndex = getSRIHashAlgorithmIndex(newAlgorithm)
163
+
164
+ // 5. If newAlgorithmIndex is less than currentAlgorithmIndex, then continue.
165
+ if (newAlgorithmIndex < currentAlgorithmIndex) {
166
+ continue
167
+
168
+ // 6. Otherwise, if newAlgorithmIndex is greater than
169
+ // currentAlgorithmIndex:
170
+ } else if (newAlgorithmIndex > currentAlgorithmIndex) {
171
+ // 1. Set strongest to item.
172
+ strongest = item
173
+
174
+ // 2. Set result to « item ».
175
+ result[0] = item
176
+ result.length = 1
177
+
178
+ // 7. Otherwise, newAlgorithmIndex and currentAlgorithmIndex are the same
179
+ // value. Append item to result.
180
+ } else {
181
+ result.push(item)
182
+ }
183
+ }
184
+
185
+ // 3. Return result.
186
+ return result
187
+ }
188
+
189
+ /**
190
+ * @param {string} metadata
191
+ * @returns {MetadataList}
192
+ *
193
+ * @see https://w3c.github.io/webappsec-subresource-integrity/#parse-metadata
194
+ */
195
+ function parseMetadata (metadata) {
196
+ // 1. Let result be the empty set.
197
+ /** @type {MetadataList} */
198
+ const result = []
199
+
200
+ // 2. For each item returned by splitting metadata on spaces:
201
+ for (const item of metadata.split(' ')) {
202
+ // 1. Let expression-and-options be the result of splitting item on U+003F (?).
203
+ const expressionAndOptions = item.split('?', 1)
204
+
205
+ // 2. Let algorithm-expression be expression-and-options[0].
206
+ const algorithmExpression = expressionAndOptions[0]
207
+
208
+ // 3. Let base64-value be the empty string.
209
+ let base64Value = ''
210
+
211
+ // 4. Let algorithm-and-value be the result of splitting algorithm-expression on U+002D (-).
212
+ const algorithmAndValue = [algorithmExpression.slice(0, 6), algorithmExpression.slice(7)]
213
+
214
+ // 5. Let algorithm be algorithm-and-value[0].
215
+ const algorithm = algorithmAndValue[0]
216
+
217
+ // 6. If algorithm is not a valid SRI hash algorithm token, then continue.
218
+ if (!isValidSRIHashAlgorithm(algorithm)) {
219
+ continue
220
+ }
221
+
222
+ // 7. If algorithm-and-value[1] exists, set base64-value to
223
+ // algorithm-and-value[1].
224
+ if (algorithmAndValue[1]) {
225
+ base64Value = algorithmAndValue[1]
226
+ }
227
+
228
+ // 8. Let metadata be the ordered map
229
+ // «["alg" → algorithm, "val" → base64-value]».
230
+ const metadata = {
231
+ // Possible spec bug?
232
+ alg: asciiLowercase(algorithm),
233
+ val: base64Value
234
+ }
235
+
236
+ // 9. Append metadata to result.
237
+ result.push(metadata)
238
+ }
239
+
240
+ // 3. Return result.
241
+ return result
242
+ }
243
+
244
+ /**
245
+ * Applies the specified hash algorithm to the given bytes
246
+ *
247
+ * @typedef {(algorithm: SRIHashAlgorithm, bytes: Uint8Array) => string} ApplyAlgorithmToBytes
248
+ * @param {SRIHashAlgorithm} algorithm
249
+ * @param {Uint8Array} bytes
250
+ * @returns {string}
251
+ */
252
+ const applyAlgorithmToBytes = (algorithm, bytes) => {
253
+ return crypto.hash(algorithm, bytes, 'base64')
254
+ }
255
+
256
+ /**
257
+ * Compares two base64 strings, allowing for base64url
258
+ * in the second string.
259
+ *
260
+ * @param {string} actualValue base64 encoded string
261
+ * @param {string} expectedValue base64 or base64url encoded string
262
+ * @returns {boolean}
263
+ */
264
+ function caseSensitiveMatch (actualValue, expectedValue) {
265
+ // Ignore padding characters from the end of the strings by
266
+ // decreasing the length by 1 or 2 if the last characters are `=`.
267
+ let actualValueLength = actualValue.length
268
+ if (actualValueLength !== 0 && actualValue[actualValueLength - 1] === '=') {
269
+ actualValueLength -= 1
270
+ }
271
+ if (actualValueLength !== 0 && actualValue[actualValueLength - 1] === '=') {
272
+ actualValueLength -= 1
273
+ }
274
+ let expectedValueLength = expectedValue.length
275
+ if (expectedValueLength !== 0 && expectedValue[expectedValueLength - 1] === '=') {
276
+ expectedValueLength -= 1
277
+ }
278
+ if (expectedValueLength !== 0 && expectedValue[expectedValueLength - 1] === '=') {
279
+ expectedValueLength -= 1
280
+ }
281
+
282
+ if (actualValueLength !== expectedValueLength) {
283
+ return false
284
+ }
285
+
286
+ for (let i = 0; i < actualValueLength; ++i) {
287
+ if (
288
+ actualValue[i] === expectedValue[i] ||
289
+ (actualValue[i] === '+' && expectedValue[i] === '-') ||
290
+ (actualValue[i] === '/' && expectedValue[i] === '_')
291
+ ) {
292
+ continue
293
+ }
294
+ return false
295
+ }
296
+
297
+ return true
298
+ }
299
+
300
+ module.exports = {
301
+ applyAlgorithmToBytes,
302
+ bytesMatch,
303
+ caseSensitiveMatch,
304
+ isValidSRIHashAlgorithm,
305
+ getStrongestMetadata,
306
+ parseMetadata
307
+ }