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,315 @@
1
+ 'use strict'
2
+
3
+ const { collectASequenceOfCodePointsFast } = require('../infra')
4
+ const { maxNameValuePairSize, maxAttributeValueSize } = require('./constants')
5
+ const { isCTLExcludingHtab } = require('./util')
6
+ const assert = require('node:assert')
7
+
8
+ /**
9
+ * @description Parses the field-value attributes of a set-cookie header string.
10
+ * @see https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#section-5.4
11
+ * @param {string} header
12
+ * @returns {import('./index').Cookie|null} if the header is invalid, null will be returned
13
+ */
14
+ function parseSetCookie (header) {
15
+ // 1. If the set-cookie-string contains a %x00-08 / %x0A-1F / %x7F
16
+ // character (CTL characters excluding HTAB): Abort these steps and
17
+ // ignore the set-cookie-string entirely.
18
+ if (isCTLExcludingHtab(header)) {
19
+ return null
20
+ }
21
+
22
+ let nameValuePair = ''
23
+ let unparsedAttributes = ''
24
+ let name = ''
25
+ let value = ''
26
+
27
+ // 2. If the set-cookie-string contains a %x3B (";") character:
28
+ if (header.includes(';')) {
29
+ // 1. The name-value-pair string consists of the characters up to,
30
+ // but not including, the first %x3B (";"), and the unparsed-
31
+ // attributes consist of the remainder of the set-cookie-string
32
+ // (including the %x3B (";") in question).
33
+ const position = { position: 0 }
34
+
35
+ nameValuePair = collectASequenceOfCodePointsFast(';', header, position)
36
+ unparsedAttributes = header.slice(position.position)
37
+ } else {
38
+ // Otherwise:
39
+
40
+ // 1. The name-value-pair string consists of all the characters
41
+ // contained in the set-cookie-string, and the unparsed-
42
+ // attributes is the empty string.
43
+ nameValuePair = header
44
+ }
45
+
46
+ // 3. If the name-value-pair string lacks a %x3D ("=") character, then
47
+ // the name string is empty, and the value string is the value of
48
+ // name-value-pair.
49
+ if (!nameValuePair.includes('=')) {
50
+ value = nameValuePair
51
+ } else {
52
+ // Otherwise, the name string consists of the characters up to, but
53
+ // not including, the first %x3D ("=") character, and the (possibly
54
+ // empty) value string consists of the characters after the first
55
+ // %x3D ("=") character.
56
+ const position = { position: 0 }
57
+ name = collectASequenceOfCodePointsFast(
58
+ '=',
59
+ nameValuePair,
60
+ position
61
+ )
62
+ value = nameValuePair.slice(position.position + 1)
63
+ }
64
+
65
+ // 4. Remove any leading or trailing WSP characters from the name
66
+ // string and the value string.
67
+ name = name.trim()
68
+ value = value.trim()
69
+
70
+ // 5. If the sum of the lengths of the name string and the value string
71
+ // is more than 4096 octets, abort these steps and ignore the set-
72
+ // cookie-string entirely.
73
+ if (name.length + value.length > maxNameValuePairSize) {
74
+ return null
75
+ }
76
+
77
+ // 6. The cookie-name is the name string, and the cookie-value is the
78
+ // value string.
79
+ // https://datatracker.ietf.org/doc/html/rfc6265
80
+ // To maximize compatibility with user agents, servers that wish to
81
+ // store arbitrary data in a cookie-value SHOULD encode that data, for
82
+ // example, using Base64 [RFC4648].
83
+ return {
84
+ name, value, ...parseUnparsedAttributes(unparsedAttributes)
85
+ }
86
+ }
87
+
88
+ /**
89
+ * Parses the remaining attributes of a set-cookie header
90
+ * @see https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#section-5.4
91
+ * @param {string} unparsedAttributes
92
+ * @param {Object.<string, unknown>} [cookieAttributeList={}]
93
+ */
94
+ function parseUnparsedAttributes (unparsedAttributes, cookieAttributeList = {}) {
95
+ while (true) {
96
+ // 1. If the unparsed-attributes string is empty, skip the rest of
97
+ // these steps.
98
+ if (unparsedAttributes.length === 0) {
99
+ return cookieAttributeList
100
+ }
101
+
102
+ // 2. Discard the first character of the unparsed-attributes (which
103
+ // will be a %x3B (";") character).
104
+ assert(unparsedAttributes[0] === ';')
105
+ unparsedAttributes = unparsedAttributes.slice(1)
106
+
107
+ let cookieAv = ''
108
+
109
+ // 3. If the remaining unparsed-attributes contains a %x3B (";")
110
+ // character:
111
+ if (unparsedAttributes.includes(';')) {
112
+ // 1. Consume the characters of the unparsed-attributes up to, but
113
+ // not including, the first %x3B (";") character.
114
+ cookieAv = collectASequenceOfCodePointsFast(
115
+ ';',
116
+ unparsedAttributes,
117
+ { position: 0 }
118
+ )
119
+ unparsedAttributes = unparsedAttributes.slice(cookieAv.length)
120
+ } else {
121
+ // Otherwise:
122
+
123
+ // 1. Consume the remainder of the unparsed-attributes.
124
+ cookieAv = unparsedAttributes
125
+ unparsedAttributes = ''
126
+ }
127
+
128
+ // Let the cookie-av string be the characters consumed in this step.
129
+
130
+ let attributeName = ''
131
+ let attributeValue = ''
132
+
133
+ // 4. If the cookie-av string contains a %x3D ("=") character:
134
+ if (cookieAv.includes('=')) {
135
+ // 1. The (possibly empty) attribute-name string consists of the
136
+ // characters up to, but not including, the first %x3D ("=")
137
+ // character, and the (possibly empty) attribute-value string
138
+ // consists of the characters after the first %x3D ("=")
139
+ // character.
140
+ const position = { position: 0 }
141
+
142
+ attributeName = collectASequenceOfCodePointsFast(
143
+ '=',
144
+ cookieAv,
145
+ position
146
+ )
147
+ attributeValue = cookieAv.slice(position.position + 1)
148
+ } else {
149
+ // Otherwise:
150
+
151
+ // 1. The attribute-name string consists of the entire cookie-av
152
+ // string, and the attribute-value string is empty.
153
+ attributeName = cookieAv
154
+ }
155
+
156
+ // 5. Remove any leading or trailing WSP characters from the attribute-
157
+ // name string and the attribute-value string.
158
+ attributeName = attributeName.trim()
159
+ attributeValue = attributeValue.trim()
160
+
161
+ // 6. If the attribute-value is longer than 1024 octets, ignore the
162
+ // cookie-av string and return to Step 1 of this algorithm.
163
+ if (attributeValue.length > maxAttributeValueSize) {
164
+ continue
165
+ }
166
+
167
+ // 7. Process the attribute-name and attribute-value according to the
168
+ // requirements in the following subsections. (Notice that
169
+ // attributes with unrecognized attribute-names are ignored.)
170
+ const attributeNameLowercase = attributeName.toLowerCase()
171
+
172
+ // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#section-5.4.1
173
+ // If the attribute-name case-insensitively matches the string
174
+ // "Expires", the user agent MUST process the cookie-av as follows.
175
+ if (attributeNameLowercase === 'expires') {
176
+ // 1. Let the expiry-time be the result of parsing the attribute-value
177
+ // as cookie-date (see Section 5.1.1).
178
+ const expiryTime = new Date(attributeValue)
179
+
180
+ // 2. If the attribute-value failed to parse as a cookie date, ignore
181
+ // the cookie-av.
182
+
183
+ cookieAttributeList.expires = expiryTime
184
+ } else if (attributeNameLowercase === 'max-age') {
185
+ // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#section-5.4.2
186
+ // If the attribute-name case-insensitively matches the string "Max-
187
+ // Age", the user agent MUST process the cookie-av as follows.
188
+
189
+ // 1. If the first character of the attribute-value is not a DIGIT or a
190
+ // "-" character, ignore the cookie-av.
191
+ const charCode = attributeValue.charCodeAt(0)
192
+
193
+ if ((charCode < 48 || charCode > 57) && attributeValue[0] !== '-') {
194
+ continue
195
+ }
196
+
197
+ // 2. If the remainder of attribute-value contains a non-DIGIT
198
+ // character, ignore the cookie-av.
199
+ if (!/^\d+$/.test(attributeValue)) {
200
+ continue
201
+ }
202
+
203
+ // 3. Let delta-seconds be the attribute-value converted to an integer.
204
+ const deltaSeconds = Number(attributeValue)
205
+
206
+ // 4. Let cookie-age-limit be the maximum age of the cookie (which
207
+ // SHOULD be 400 days or less, see Section 4.1.2.2).
208
+
209
+ // 5. Set delta-seconds to the smaller of its present value and cookie-
210
+ // age-limit.
211
+ // deltaSeconds = Math.min(deltaSeconds * 1000, maxExpiresMs)
212
+
213
+ // 6. If delta-seconds is less than or equal to zero (0), let expiry-
214
+ // time be the earliest representable date and time. Otherwise, let
215
+ // the expiry-time be the current date and time plus delta-seconds
216
+ // seconds.
217
+ // const expiryTime = deltaSeconds <= 0 ? Date.now() : Date.now() + deltaSeconds
218
+
219
+ // 7. Append an attribute to the cookie-attribute-list with an
220
+ // attribute-name of Max-Age and an attribute-value of expiry-time.
221
+ cookieAttributeList.maxAge = deltaSeconds
222
+ } else if (attributeNameLowercase === 'domain') {
223
+ // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#section-5.4.3
224
+ // If the attribute-name case-insensitively matches the string "Domain",
225
+ // the user agent MUST process the cookie-av as follows.
226
+
227
+ // 1. Let cookie-domain be the attribute-value.
228
+ let cookieDomain = attributeValue
229
+
230
+ // 2. If cookie-domain starts with %x2E ("."), let cookie-domain be
231
+ // cookie-domain without its leading %x2E (".").
232
+ if (cookieDomain[0] === '.') {
233
+ cookieDomain = cookieDomain.slice(1)
234
+ }
235
+
236
+ // 3. Convert the cookie-domain to lower case.
237
+ cookieDomain = cookieDomain.toLowerCase()
238
+
239
+ // 4. Append an attribute to the cookie-attribute-list with an
240
+ // attribute-name of Domain and an attribute-value of cookie-domain.
241
+ cookieAttributeList.domain = cookieDomain
242
+ } else if (attributeNameLowercase === 'path') {
243
+ // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#section-5.4.4
244
+ // If the attribute-name case-insensitively matches the string "Path",
245
+ // the user agent MUST process the cookie-av as follows.
246
+
247
+ // 1. If the attribute-value is empty or if the first character of the
248
+ // attribute-value is not %x2F ("/"):
249
+ let cookiePath = ''
250
+ if (attributeValue.length === 0 || attributeValue[0] !== '/') {
251
+ // 1. Let cookie-path be the default-path.
252
+ cookiePath = '/'
253
+ } else {
254
+ // Otherwise:
255
+
256
+ // 1. Let cookie-path be the attribute-value.
257
+ cookiePath = attributeValue
258
+ }
259
+
260
+ // 2. Append an attribute to the cookie-attribute-list with an
261
+ // attribute-name of Path and an attribute-value of cookie-path.
262
+ cookieAttributeList.path = cookiePath
263
+ } else if (attributeNameLowercase === 'secure') {
264
+ // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#section-5.4.5
265
+ // If the attribute-name case-insensitively matches the string "Secure",
266
+ // the user agent MUST append an attribute to the cookie-attribute-list
267
+ // with an attribute-name of Secure and an empty attribute-value.
268
+
269
+ cookieAttributeList.secure = true
270
+ } else if (attributeNameLowercase === 'httponly') {
271
+ // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#section-5.4.6
272
+ // If the attribute-name case-insensitively matches the string
273
+ // "HttpOnly", the user agent MUST append an attribute to the cookie-
274
+ // attribute-list with an attribute-name of HttpOnly and an empty
275
+ // attribute-value.
276
+
277
+ cookieAttributeList.httpOnly = true
278
+ } else if (attributeNameLowercase === 'samesite') {
279
+ // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#section-5.4.7
280
+ // If the attribute-name case-insensitively matches the string
281
+ // "SameSite", the user agent MUST process the cookie-av as follows:
282
+
283
+ const attributeValueLowercase = attributeValue.toLowerCase()
284
+
285
+ // 1. If cookie-av's attribute-value is a case-insensitive match for
286
+ // "None", append an attribute to the cookie-attribute-list with an
287
+ // attribute-name of "SameSite" and an attribute-value of "None".
288
+ if (attributeValueLowercase === 'none') {
289
+ cookieAttributeList.sameSite = 'None'
290
+ } else if (attributeValueLowercase === 'strict') {
291
+ // 2. If cookie-av's attribute-value is a case-insensitive match for
292
+ // "Strict", append an attribute to the cookie-attribute-list with
293
+ // an attribute-name of "SameSite" and an attribute-value of
294
+ // "Strict".
295
+ cookieAttributeList.sameSite = 'Strict'
296
+ } else if (attributeValueLowercase === 'lax') {
297
+ // 3. If cookie-av's attribute-value is a case-insensitive match for
298
+ // "Lax", append an attribute to the cookie-attribute-list with an
299
+ // attribute-name of "SameSite" and an attribute-value of "Lax".
300
+ cookieAttributeList.sameSite = 'Lax'
301
+ }
302
+ } else {
303
+ cookieAttributeList.unparsed ??= []
304
+
305
+ cookieAttributeList.unparsed.push(`${attributeName}=${attributeValue}`)
306
+ }
307
+
308
+ // 8. Return to Step 1 of this algorithm.
309
+ }
310
+ }
311
+
312
+ module.exports = {
313
+ parseSetCookie,
314
+ parseUnparsedAttributes
315
+ }
@@ -0,0 +1,371 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * @param {string} value
5
+ * @returns {boolean}
6
+ */
7
+ function isCTLExcludingHtab (value) {
8
+ for (let i = 0; i < value.length; ++i) {
9
+ const code = value.charCodeAt(i)
10
+
11
+ if (
12
+ (code >= 0x00 && code <= 0x08) ||
13
+ (code >= 0x0A && code <= 0x1F) ||
14
+ code === 0x7F
15
+ ) {
16
+ return true
17
+ }
18
+ }
19
+ return false
20
+ }
21
+
22
+ /**
23
+ CHAR = <any US-ASCII character (octets 0 - 127)>
24
+ token = 1*<any CHAR except CTLs or separators>
25
+ separators = "(" | ")" | "<" | ">" | "@"
26
+ | "," | ";" | ":" | "\" | <">
27
+ | "/" | "[" | "]" | "?" | "="
28
+ | "{" | "}" | SP | HT
29
+ * @param {string} name
30
+ */
31
+ function validateCookieName (name) {
32
+ for (let i = 0; i < name.length; ++i) {
33
+ const code = name.charCodeAt(i)
34
+
35
+ if (
36
+ code < 0x21 || // exclude CTLs (0-31), SP and HT
37
+ code > 0x7E || // exclude non-ascii and DEL
38
+ code === 0x22 || // "
39
+ code === 0x28 || // (
40
+ code === 0x29 || // )
41
+ code === 0x3C || // <
42
+ code === 0x3E || // >
43
+ code === 0x40 || // @
44
+ code === 0x2C || // ,
45
+ code === 0x3B || // ;
46
+ code === 0x3A || // :
47
+ code === 0x5C || // \
48
+ code === 0x2F || // /
49
+ code === 0x5B || // [
50
+ code === 0x5D || // ]
51
+ code === 0x3F || // ?
52
+ code === 0x3D || // =
53
+ code === 0x7B || // {
54
+ code === 0x7D // }
55
+ ) {
56
+ throw new Error('Invalid cookie name')
57
+ }
58
+ }
59
+ }
60
+
61
+ /**
62
+ cookie-value = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE )
63
+ cookie-octet = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E
64
+ ; US-ASCII characters excluding CTLs,
65
+ ; whitespace DQUOTE, comma, semicolon,
66
+ ; and backslash
67
+ * @param {string} value
68
+ */
69
+ function validateCookieValue (value) {
70
+ let len = value.length
71
+ let i = 0
72
+
73
+ // if the value is wrapped in DQUOTE
74
+ if (value[0] === '"') {
75
+ if (len === 1 || value[len - 1] !== '"') {
76
+ throw new Error('Invalid cookie value')
77
+ }
78
+ --len
79
+ ++i
80
+ }
81
+
82
+ while (i < len) {
83
+ const code = value.charCodeAt(i++)
84
+
85
+ if (
86
+ code < 0x21 || // exclude CTLs (0-31)
87
+ code > 0x7E || // non-ascii and DEL (127)
88
+ code === 0x22 || // "
89
+ code === 0x2C || // ,
90
+ code === 0x3B || // ;
91
+ code === 0x5C // \
92
+ ) {
93
+ throw new Error('Invalid cookie value')
94
+ }
95
+ }
96
+ }
97
+
98
+ /**
99
+ * path-value = <any CHAR except CTLs or ";">
100
+ * @param {string} path
101
+ */
102
+ function validateCookiePath (path) {
103
+ for (let i = 0; i < path.length; ++i) {
104
+ const code = path.charCodeAt(i)
105
+
106
+ if (
107
+ code < 0x20 || // exclude CTLs (0-31)
108
+ code > 0x7E || // exclude DEL and non-ascii
109
+ code === 0x3B // ;
110
+ ) {
111
+ throw new Error('Invalid cookie path')
112
+ }
113
+ }
114
+ }
115
+
116
+ /**
117
+ * <let-dig> ::= <letter> | <digit>
118
+ *
119
+ * <letter> ::= any one of the 52 alphabetic characters A through Z in
120
+ * upper case and a through z in lower case
121
+ *
122
+ * <digit> ::= any one of the ten digits 0 through 9r
123
+ *
124
+ * @see https://www.rfc-editor.org/rfc/rfc1034#section-3.5
125
+ * @param {number} code
126
+ */
127
+ function isLetterOrDigit (code) {
128
+ return (
129
+ (code >= 0x30 && code <= 0x39) || // 0-9
130
+ (code >= 0x41 && code <= 0x5A) || // A-Z
131
+ (code >= 0x61 && code <= 0x7A) // a-z
132
+ )
133
+ }
134
+
135
+ /**
136
+ * Validates a cookie domain against the "preferred name syntax".
137
+ *
138
+ * <domain> ::= <subdomain> | " "
139
+ * <subdomain> ::= <label> | <subdomain> "." <label>
140
+ * <label> ::= <let-dig> [ [ <ldh-str> ] <let-dig> ]
141
+ * <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
142
+ * <let-dig-hyp> ::= <let-dig> | "-"
143
+ *
144
+ * @see https://www.rfc-editor.org/rfc/rfc1034#section-3.5
145
+ * @see https://www.rfc-editor.org/rfc/rfc1123#section-2.1
146
+ * @see https://www.rfc-editor.org/rfc/rfc1035#section-2.3.4
147
+ * @param {string} domain
148
+ */
149
+ function validateCookieDomain (domain) {
150
+ // <domain> ::= <subdomain> | " "
151
+ if (domain === ' ') {
152
+ return
153
+ }
154
+
155
+ if (domain.length > 255) {
156
+ throw new Error('Invalid cookie domain')
157
+ }
158
+
159
+ let labelLength = 0
160
+
161
+ for (let i = 0; i < domain.length; ++i) {
162
+ const code = domain.charCodeAt(i)
163
+
164
+ if (code === 0x2E) {
165
+ if (labelLength === 0) {
166
+ throw new Error('Invalid cookie domain')
167
+ }
168
+
169
+ if (domain.charCodeAt(i - 1) === 0x2D) { // "-"
170
+ throw new Error('Invalid cookie domain')
171
+ }
172
+
173
+ labelLength = 0
174
+ continue
175
+ }
176
+
177
+ if (labelLength === 0 && !isLetterOrDigit(code)) {
178
+ throw new Error('Invalid cookie domain')
179
+ }
180
+
181
+ if (!isLetterOrDigit(code) && code !== 0x2D) { // "-"
182
+ throw new Error('Invalid cookie domain')
183
+ }
184
+
185
+ if (++labelLength > 63) {
186
+ throw new Error('Invalid cookie domain')
187
+ }
188
+ }
189
+
190
+ if (labelLength === 0 || domain.charCodeAt(domain.length - 1) === 0x2D) { // "-"
191
+ throw new Error('Invalid cookie domain')
192
+ }
193
+ }
194
+
195
+ const IMFDays = [
196
+ 'Sun', 'Mon', 'Tue', 'Wed',
197
+ 'Thu', 'Fri', 'Sat'
198
+ ]
199
+
200
+ const IMFMonths = [
201
+ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
202
+ 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
203
+ ]
204
+
205
+ const IMFPaddedNumbers = Array(61).fill(0).map((_, i) => i.toString().padStart(2, '0'))
206
+
207
+ /**
208
+ * @see https://www.rfc-editor.org/rfc/rfc7231#section-7.1.1.1
209
+ * @param {number|Date} date
210
+ IMF-fixdate = day-name "," SP date1 SP time-of-day SP GMT
211
+ ; fixed length/zone/capitalization subset of the format
212
+ ; see Section 3.3 of [RFC5322]
213
+
214
+ day-name = %x4D.6F.6E ; "Mon", case-sensitive
215
+ / %x54.75.65 ; "Tue", case-sensitive
216
+ / %x57.65.64 ; "Wed", case-sensitive
217
+ / %x54.68.75 ; "Thu", case-sensitive
218
+ / %x46.72.69 ; "Fri", case-sensitive
219
+ / %x53.61.74 ; "Sat", case-sensitive
220
+ / %x53.75.6E ; "Sun", case-sensitive
221
+ date1 = day SP month SP year
222
+ ; e.g., 02 Jun 1982
223
+
224
+ day = 2DIGIT
225
+ month = %x4A.61.6E ; "Jan", case-sensitive
226
+ / %x46.65.62 ; "Feb", case-sensitive
227
+ / %x4D.61.72 ; "Mar", case-sensitive
228
+ / %x41.70.72 ; "Apr", case-sensitive
229
+ / %x4D.61.79 ; "May", case-sensitive
230
+ / %x4A.75.6E ; "Jun", case-sensitive
231
+ / %x4A.75.6C ; "Jul", case-sensitive
232
+ / %x41.75.67 ; "Aug", case-sensitive
233
+ / %x53.65.70 ; "Sep", case-sensitive
234
+ / %x4F.63.74 ; "Oct", case-sensitive
235
+ / %x4E.6F.76 ; "Nov", case-sensitive
236
+ / %x44.65.63 ; "Dec", case-sensitive
237
+ year = 4DIGIT
238
+
239
+ GMT = %x47.4D.54 ; "GMT", case-sensitive
240
+
241
+ time-of-day = hour ":" minute ":" second
242
+ ; 00:00:00 - 23:59:60 (leap second)
243
+
244
+ hour = 2DIGIT
245
+ minute = 2DIGIT
246
+ second = 2DIGIT
247
+ */
248
+ function toIMFDate (date) {
249
+ if (typeof date === 'number') {
250
+ date = new Date(date)
251
+ }
252
+
253
+ return `${IMFDays[date.getUTCDay()]}, ${IMFPaddedNumbers[date.getUTCDate()]} ${IMFMonths[date.getUTCMonth()]} ${date.getUTCFullYear()} ${IMFPaddedNumbers[date.getUTCHours()]}:${IMFPaddedNumbers[date.getUTCMinutes()]}:${IMFPaddedNumbers[date.getUTCSeconds()]} GMT`
254
+ }
255
+
256
+ /**
257
+ max-age-av = "Max-Age=" non-zero-digit *DIGIT
258
+ ; In practice, both expires-av and max-age-av
259
+ ; are limited to dates representable by the
260
+ ; user agent.
261
+ * @param {number} maxAge
262
+ */
263
+ function validateCookieMaxAge (maxAge) {
264
+ if (maxAge < 0) {
265
+ throw new Error('Invalid cookie max-age')
266
+ }
267
+ }
268
+
269
+ /**
270
+ * @see https://www.rfc-editor.org/rfc/rfc6265#section-4.1.1
271
+ * @param {import('./index').Cookie} cookie
272
+ */
273
+ function stringify (cookie) {
274
+ if (cookie.name.length === 0) {
275
+ return null
276
+ }
277
+
278
+ validateCookieName(cookie.name)
279
+ validateCookieValue(cookie.value)
280
+
281
+ const out = [`${cookie.name}=${cookie.value}`]
282
+
283
+ // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-cookie-prefixes-00#section-3.1
284
+ // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-cookie-prefixes-00#section-3.2
285
+ if (cookie.name.startsWith('__Secure-')) {
286
+ cookie.secure = true
287
+ }
288
+
289
+ if (cookie.name.startsWith('__Host-')) {
290
+ cookie.secure = true
291
+ cookie.domain = null
292
+ cookie.path = '/'
293
+ }
294
+
295
+ if (cookie.secure) {
296
+ out.push('Secure')
297
+ }
298
+
299
+ if (cookie.httpOnly) {
300
+ out.push('HttpOnly')
301
+ }
302
+
303
+ if (typeof cookie.maxAge === 'number') {
304
+ validateCookieMaxAge(cookie.maxAge)
305
+ out.push(`Max-Age=${cookie.maxAge}`)
306
+ }
307
+
308
+ if (cookie.domain) {
309
+ validateCookieDomain(cookie.domain)
310
+ out.push(`Domain=${cookie.domain}`)
311
+ }
312
+
313
+ if (cookie.path) {
314
+ validateCookiePath(cookie.path)
315
+ out.push(`Path=${cookie.path}`)
316
+ }
317
+
318
+ // A numeric 0 is the Unix epoch, not an absent value -- the same reason the
319
+ // Max-Age check above tests the type rather than truthiness.
320
+ //
321
+ // expires is Date | number, so the "Invalid Date" check only guards the Date
322
+ // form. A number that is not a valid time value (NaN, +/-Infinity, or outside
323
+ // the Date range) would otherwise reach toIMFDate, which splices undefined and
324
+ // NaN into the header value instead of failing.
325
+ if (cookie.expires != null) {
326
+ const expiresMs = cookie.expires instanceof Date
327
+ ? cookie.expires.getTime()
328
+ : cookie.expires
329
+
330
+ // The largest time value a Date can represent, in milliseconds, i.e.
331
+ // 100,000,000 days either side of the epoch (ECMA-262 Date Time Values).
332
+ // Anything within it is safe for toIMFDate to format; anything beyond it
333
+ // produces an invalid Date and must be ignored instead.
334
+ const maxTimeValue = 8640000000000000
335
+
336
+ if (typeof expiresMs === 'number' && Number.isFinite(expiresMs) && Math.abs(expiresMs) <= maxTimeValue) {
337
+ out.push(`Expires=${toIMFDate(cookie.expires)}`)
338
+ }
339
+ }
340
+
341
+ if (cookie.sameSite) {
342
+ out.push(`SameSite=${cookie.sameSite}`)
343
+ }
344
+
345
+ for (const part of cookie.unparsed) {
346
+ if (!part.includes('=')) {
347
+ throw new Error('Invalid unparsed')
348
+ }
349
+
350
+ const [key, ...value] = part.split('=')
351
+
352
+ const trimmedKey = key.trim()
353
+ const joinedValue = value.join('=')
354
+
355
+ validateCookieName(trimmedKey)
356
+ validateCookieValue(joinedValue)
357
+
358
+ out.push(`${trimmedKey}=${joinedValue}`)
359
+ }
360
+
361
+ return out.join('; ')
362
+ }
363
+
364
+ module.exports = {
365
+ isCTLExcludingHtab,
366
+ validateCookieName,
367
+ validateCookiePath,
368
+ validateCookieValue,
369
+ toIMFDate,
370
+ stringify
371
+ }