@nocobase/plugin-idp-oauth 2.1.0-alpha.17 → 2.1.0-alpha.18

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 (214) hide show
  1. package/build.config.ts +1 -1
  2. package/dist/externalVersion.js +4 -4
  3. package/dist/node_modules/light-my-request/package.json +1 -1
  4. package/dist/node_modules/undici/LICENSE +21 -0
  5. package/dist/node_modules/undici/README.md +741 -0
  6. package/dist/node_modules/undici/docs/docs/api/Agent.md +84 -0
  7. package/dist/node_modules/undici/docs/docs/api/BalancedPool.md +99 -0
  8. package/dist/node_modules/undici/docs/docs/api/CacheStorage.md +30 -0
  9. package/dist/node_modules/undici/docs/docs/api/CacheStore.md +164 -0
  10. package/dist/node_modules/undici/docs/docs/api/Client.md +285 -0
  11. package/dist/node_modules/undici/docs/docs/api/ClientStats.md +27 -0
  12. package/dist/node_modules/undici/docs/docs/api/Connector.md +115 -0
  13. package/dist/node_modules/undici/docs/docs/api/ContentType.md +57 -0
  14. package/dist/node_modules/undici/docs/docs/api/Cookies.md +101 -0
  15. package/dist/node_modules/undici/docs/docs/api/Debug.md +62 -0
  16. package/dist/node_modules/undici/docs/docs/api/DiagnosticsChannel.md +315 -0
  17. package/dist/node_modules/undici/docs/docs/api/Dispatcher.md +1392 -0
  18. package/dist/node_modules/undici/docs/docs/api/EnvHttpProxyAgent.md +159 -0
  19. package/dist/node_modules/undici/docs/docs/api/Errors.md +49 -0
  20. package/dist/node_modules/undici/docs/docs/api/EventSource.md +45 -0
  21. package/dist/node_modules/undici/docs/docs/api/Fetch.md +60 -0
  22. package/dist/node_modules/undici/docs/docs/api/GlobalInstallation.md +139 -0
  23. package/dist/node_modules/undici/docs/docs/api/H2CClient.md +263 -0
  24. package/dist/node_modules/undici/docs/docs/api/MockAgent.md +603 -0
  25. package/dist/node_modules/undici/docs/docs/api/MockCallHistory.md +197 -0
  26. package/dist/node_modules/undici/docs/docs/api/MockCallHistoryLog.md +43 -0
  27. package/dist/node_modules/undici/docs/docs/api/MockClient.md +81 -0
  28. package/dist/node_modules/undici/docs/docs/api/MockErrors.md +12 -0
  29. package/dist/node_modules/undici/docs/docs/api/MockPool.md +555 -0
  30. package/dist/node_modules/undici/docs/docs/api/Pool.md +84 -0
  31. package/dist/node_modules/undici/docs/docs/api/PoolStats.md +35 -0
  32. package/dist/node_modules/undici/docs/docs/api/ProxyAgent.md +229 -0
  33. package/dist/node_modules/undici/docs/docs/api/RedirectHandler.md +93 -0
  34. package/dist/node_modules/undici/docs/docs/api/RetryAgent.md +50 -0
  35. package/dist/node_modules/undici/docs/docs/api/RetryHandler.md +118 -0
  36. package/dist/node_modules/undici/docs/docs/api/RoundRobinPool.md +145 -0
  37. package/dist/node_modules/undici/docs/docs/api/SnapshotAgent.md +616 -0
  38. package/dist/node_modules/undici/docs/docs/api/Socks5ProxyAgent.md +274 -0
  39. package/dist/node_modules/undici/docs/docs/api/Util.md +25 -0
  40. package/dist/node_modules/undici/docs/docs/api/WebSocket.md +141 -0
  41. package/dist/node_modules/undici/docs/docs/api/api-lifecycle.md +91 -0
  42. package/dist/node_modules/undici/docs/docs/best-practices/client-certificate.md +64 -0
  43. package/dist/node_modules/undici/docs/docs/best-practices/crawling.md +58 -0
  44. package/dist/node_modules/undici/docs/docs/best-practices/mocking-request.md +190 -0
  45. package/dist/node_modules/undici/docs/docs/best-practices/proxy.md +127 -0
  46. package/dist/node_modules/undici/docs/docs/best-practices/undici-vs-builtin-fetch.md +224 -0
  47. package/dist/node_modules/undici/docs/docs/best-practices/writing-tests.md +63 -0
  48. package/dist/node_modules/undici/index-fetch.js +65 -0
  49. package/dist/node_modules/undici/index.d.ts +3 -0
  50. package/dist/node_modules/undici/index.js +234 -0
  51. package/dist/node_modules/undici/lib/api/abort-signal.js +59 -0
  52. package/dist/node_modules/undici/lib/api/api-connect.js +110 -0
  53. package/dist/node_modules/undici/lib/api/api-pipeline.js +252 -0
  54. package/dist/node_modules/undici/lib/api/api-request.js +214 -0
  55. package/dist/node_modules/undici/lib/api/api-stream.js +209 -0
  56. package/dist/node_modules/undici/lib/api/api-upgrade.js +111 -0
  57. package/dist/node_modules/undici/lib/api/index.js +7 -0
  58. package/dist/node_modules/undici/lib/api/readable.js +580 -0
  59. package/dist/node_modules/undici/lib/cache/memory-cache-store.js +234 -0
  60. package/dist/node_modules/undici/lib/cache/sqlite-cache-store.js +461 -0
  61. package/dist/node_modules/undici/lib/core/connect.js +137 -0
  62. package/dist/node_modules/undici/lib/core/constants.js +143 -0
  63. package/dist/node_modules/undici/lib/core/diagnostics.js +227 -0
  64. package/dist/node_modules/undici/lib/core/errors.js +477 -0
  65. package/dist/node_modules/undici/lib/core/request.js +438 -0
  66. package/dist/node_modules/undici/lib/core/socks5-client.js +407 -0
  67. package/dist/node_modules/undici/lib/core/socks5-utils.js +203 -0
  68. package/dist/node_modules/undici/lib/core/symbols.js +75 -0
  69. package/dist/node_modules/undici/lib/core/tree.js +160 -0
  70. package/dist/node_modules/undici/lib/core/util.js +992 -0
  71. package/dist/node_modules/undici/lib/dispatcher/agent.js +158 -0
  72. package/dist/node_modules/undici/lib/dispatcher/balanced-pool.js +219 -0
  73. package/dist/node_modules/undici/lib/dispatcher/client-h1.js +1610 -0
  74. package/dist/node_modules/undici/lib/dispatcher/client-h2.js +995 -0
  75. package/dist/node_modules/undici/lib/dispatcher/client.js +659 -0
  76. package/dist/node_modules/undici/lib/dispatcher/dispatcher-base.js +165 -0
  77. package/dist/node_modules/undici/lib/dispatcher/dispatcher.js +48 -0
  78. package/dist/node_modules/undici/lib/dispatcher/env-http-proxy-agent.js +146 -0
  79. package/dist/node_modules/undici/lib/dispatcher/fixed-queue.js +135 -0
  80. package/dist/node_modules/undici/lib/dispatcher/h2c-client.js +51 -0
  81. package/dist/node_modules/undici/lib/dispatcher/pool-base.js +214 -0
  82. package/dist/node_modules/undici/lib/dispatcher/pool.js +118 -0
  83. package/dist/node_modules/undici/lib/dispatcher/proxy-agent.js +318 -0
  84. package/dist/node_modules/undici/lib/dispatcher/retry-agent.js +35 -0
  85. package/dist/node_modules/undici/lib/dispatcher/round-robin-pool.js +137 -0
  86. package/dist/node_modules/undici/lib/dispatcher/socks5-proxy-agent.js +249 -0
  87. package/dist/node_modules/undici/lib/encoding/index.js +33 -0
  88. package/dist/node_modules/undici/lib/global.js +50 -0
  89. package/dist/node_modules/undici/lib/handler/cache-handler.js +578 -0
  90. package/dist/node_modules/undici/lib/handler/cache-revalidation-handler.js +124 -0
  91. package/dist/node_modules/undici/lib/handler/decorator-handler.js +67 -0
  92. package/dist/node_modules/undici/lib/handler/deduplication-handler.js +460 -0
  93. package/dist/node_modules/undici/lib/handler/redirect-handler.js +238 -0
  94. package/dist/node_modules/undici/lib/handler/retry-handler.js +394 -0
  95. package/dist/node_modules/undici/lib/handler/unwrap-handler.js +100 -0
  96. package/dist/node_modules/undici/lib/handler/wrap-handler.js +105 -0
  97. package/dist/node_modules/undici/lib/interceptor/cache.js +495 -0
  98. package/dist/node_modules/undici/lib/interceptor/decompress.js +259 -0
  99. package/dist/node_modules/undici/lib/interceptor/deduplicate.js +117 -0
  100. package/dist/node_modules/undici/lib/interceptor/dns.js +571 -0
  101. package/dist/node_modules/undici/lib/interceptor/dump.js +112 -0
  102. package/dist/node_modules/undici/lib/interceptor/redirect.js +21 -0
  103. package/dist/node_modules/undici/lib/interceptor/response-error.js +95 -0
  104. package/dist/node_modules/undici/lib/interceptor/retry.js +19 -0
  105. package/dist/node_modules/undici/lib/llhttp/.gitkeep +0 -0
  106. package/dist/node_modules/undici/lib/llhttp/constants.d.ts +195 -0
  107. package/dist/node_modules/undici/lib/llhttp/constants.js +531 -0
  108. package/dist/node_modules/undici/lib/llhttp/llhttp-wasm.js +15 -0
  109. package/dist/node_modules/undici/lib/llhttp/llhttp_simd-wasm.js +15 -0
  110. package/dist/node_modules/undici/lib/llhttp/utils.d.ts +2 -0
  111. package/dist/node_modules/undici/lib/llhttp/utils.js +12 -0
  112. package/dist/node_modules/undici/lib/mock/mock-agent.js +232 -0
  113. package/dist/node_modules/undici/lib/mock/mock-call-history.js +248 -0
  114. package/dist/node_modules/undici/lib/mock/mock-client.js +68 -0
  115. package/dist/node_modules/undici/lib/mock/mock-errors.js +29 -0
  116. package/dist/node_modules/undici/lib/mock/mock-interceptor.js +209 -0
  117. package/dist/node_modules/undici/lib/mock/mock-pool.js +68 -0
  118. package/dist/node_modules/undici/lib/mock/mock-symbols.js +32 -0
  119. package/dist/node_modules/undici/lib/mock/mock-utils.js +486 -0
  120. package/dist/node_modules/undici/lib/mock/pending-interceptors-formatter.js +43 -0
  121. package/dist/node_modules/undici/lib/mock/snapshot-agent.js +353 -0
  122. package/dist/node_modules/undici/lib/mock/snapshot-recorder.js +588 -0
  123. package/dist/node_modules/undici/lib/mock/snapshot-utils.js +158 -0
  124. package/dist/node_modules/undici/lib/util/cache.js +407 -0
  125. package/dist/node_modules/undici/lib/util/date.js +653 -0
  126. package/dist/node_modules/undici/lib/util/promise.js +28 -0
  127. package/dist/node_modules/undici/lib/util/runtime-features.js +124 -0
  128. package/dist/node_modules/undici/lib/util/stats.js +32 -0
  129. package/dist/node_modules/undici/lib/util/timers.js +425 -0
  130. package/dist/node_modules/undici/lib/web/cache/cache.js +864 -0
  131. package/dist/node_modules/undici/lib/web/cache/cachestorage.js +152 -0
  132. package/dist/node_modules/undici/lib/web/cache/util.js +45 -0
  133. package/dist/node_modules/undici/lib/web/cookies/constants.js +12 -0
  134. package/dist/node_modules/undici/lib/web/cookies/index.js +199 -0
  135. package/dist/node_modules/undici/lib/web/cookies/parse.js +322 -0
  136. package/dist/node_modules/undici/lib/web/cookies/util.js +282 -0
  137. package/dist/node_modules/undici/lib/web/eventsource/eventsource-stream.js +399 -0
  138. package/dist/node_modules/undici/lib/web/eventsource/eventsource.js +501 -0
  139. package/dist/node_modules/undici/lib/web/eventsource/util.js +29 -0
  140. package/dist/node_modules/undici/lib/web/fetch/LICENSE +21 -0
  141. package/dist/node_modules/undici/lib/web/fetch/body.js +509 -0
  142. package/dist/node_modules/undici/lib/web/fetch/constants.js +131 -0
  143. package/dist/node_modules/undici/lib/web/fetch/data-url.js +596 -0
  144. package/dist/node_modules/undici/lib/web/fetch/formdata-parser.js +575 -0
  145. package/dist/node_modules/undici/lib/web/fetch/formdata.js +259 -0
  146. package/dist/node_modules/undici/lib/web/fetch/global.js +40 -0
  147. package/dist/node_modules/undici/lib/web/fetch/headers.js +719 -0
  148. package/dist/node_modules/undici/lib/web/fetch/index.js +2397 -0
  149. package/dist/node_modules/undici/lib/web/fetch/request.js +1115 -0
  150. package/dist/node_modules/undici/lib/web/fetch/response.js +641 -0
  151. package/dist/node_modules/undici/lib/web/fetch/util.js +1520 -0
  152. package/dist/node_modules/undici/lib/web/infra/index.js +229 -0
  153. package/dist/node_modules/undici/lib/web/subresource-integrity/Readme.md +9 -0
  154. package/dist/node_modules/undici/lib/web/subresource-integrity/subresource-integrity.js +307 -0
  155. package/dist/node_modules/undici/lib/web/webidl/index.js +1006 -0
  156. package/dist/node_modules/undici/lib/web/websocket/connection.js +329 -0
  157. package/dist/node_modules/undici/lib/web/websocket/constants.js +126 -0
  158. package/dist/node_modules/undici/lib/web/websocket/events.js +331 -0
  159. package/dist/node_modules/undici/lib/web/websocket/frame.js +133 -0
  160. package/dist/node_modules/undici/lib/web/websocket/permessage-deflate.js +118 -0
  161. package/dist/node_modules/undici/lib/web/websocket/receiver.js +450 -0
  162. package/dist/node_modules/undici/lib/web/websocket/sender.js +109 -0
  163. package/dist/node_modules/undici/lib/web/websocket/stream/websocketerror.js +104 -0
  164. package/dist/node_modules/undici/lib/web/websocket/stream/websocketstream.js +497 -0
  165. package/dist/node_modules/undici/lib/web/websocket/util.js +347 -0
  166. package/dist/node_modules/undici/lib/web/websocket/websocket.js +751 -0
  167. package/dist/node_modules/undici/package.json +152 -0
  168. package/dist/node_modules/undici/scripts/strip-comments.js +10 -0
  169. package/dist/node_modules/undici/types/README.md +6 -0
  170. package/dist/node_modules/undici/types/agent.d.ts +32 -0
  171. package/dist/node_modules/undici/types/api.d.ts +43 -0
  172. package/dist/node_modules/undici/types/balanced-pool.d.ts +30 -0
  173. package/dist/node_modules/undici/types/cache-interceptor.d.ts +179 -0
  174. package/dist/node_modules/undici/types/cache.d.ts +36 -0
  175. package/dist/node_modules/undici/types/client-stats.d.ts +15 -0
  176. package/dist/node_modules/undici/types/client.d.ts +123 -0
  177. package/dist/node_modules/undici/types/connector.d.ts +36 -0
  178. package/dist/node_modules/undici/types/content-type.d.ts +21 -0
  179. package/dist/node_modules/undici/types/cookies.d.ts +30 -0
  180. package/dist/node_modules/undici/types/diagnostics-channel.d.ts +74 -0
  181. package/dist/node_modules/undici/types/dispatcher.d.ts +273 -0
  182. package/dist/node_modules/undici/types/env-http-proxy-agent.d.ts +22 -0
  183. package/dist/node_modules/undici/types/errors.d.ts +177 -0
  184. package/dist/node_modules/undici/types/eventsource.d.ts +66 -0
  185. package/dist/node_modules/undici/types/fetch.d.ts +231 -0
  186. package/dist/node_modules/undici/types/formdata.d.ts +114 -0
  187. package/dist/node_modules/undici/types/global-dispatcher.d.ts +9 -0
  188. package/dist/node_modules/undici/types/global-origin.d.ts +7 -0
  189. package/dist/node_modules/undici/types/h2c-client.d.ts +73 -0
  190. package/dist/node_modules/undici/types/handlers.d.ts +14 -0
  191. package/dist/node_modules/undici/types/header.d.ts +160 -0
  192. package/dist/node_modules/undici/types/index.d.ts +91 -0
  193. package/dist/node_modules/undici/types/interceptors.d.ts +80 -0
  194. package/dist/node_modules/undici/types/mock-agent.d.ts +68 -0
  195. package/dist/node_modules/undici/types/mock-call-history.d.ts +111 -0
  196. package/dist/node_modules/undici/types/mock-client.d.ts +27 -0
  197. package/dist/node_modules/undici/types/mock-errors.d.ts +12 -0
  198. package/dist/node_modules/undici/types/mock-interceptor.d.ts +94 -0
  199. package/dist/node_modules/undici/types/mock-pool.d.ts +27 -0
  200. package/dist/node_modules/undici/types/patch.d.ts +29 -0
  201. package/dist/node_modules/undici/types/pool-stats.d.ts +19 -0
  202. package/dist/node_modules/undici/types/pool.d.ts +41 -0
  203. package/dist/node_modules/undici/types/proxy-agent.d.ts +29 -0
  204. package/dist/node_modules/undici/types/readable.d.ts +68 -0
  205. package/dist/node_modules/undici/types/retry-agent.d.ts +8 -0
  206. package/dist/node_modules/undici/types/retry-handler.d.ts +125 -0
  207. package/dist/node_modules/undici/types/round-robin-pool.d.ts +41 -0
  208. package/dist/node_modules/undici/types/snapshot-agent.d.ts +109 -0
  209. package/dist/node_modules/undici/types/socks5-proxy-agent.d.ts +25 -0
  210. package/dist/node_modules/undici/types/util.d.ts +18 -0
  211. package/dist/node_modules/undici/types/utility.d.ts +7 -0
  212. package/dist/node_modules/undici/types/webidl.d.ts +347 -0
  213. package/dist/node_modules/undici/types/websocket.d.ts +188 -0
  214. package/package.json +2 -2
@@ -0,0 +1,751 @@
1
+ 'use strict'
2
+
3
+ const { isArrayBuffer } = require('node:util/types')
4
+ const { webidl } = require('../webidl')
5
+ const { URLSerializer } = require('../fetch/data-url')
6
+ const { environmentSettingsObject } = require('../fetch/util')
7
+ const { staticPropertyDescriptors, states, sentCloseFrameState, sendHints, opcodes } = require('./constants')
8
+ const {
9
+ isConnecting,
10
+ isEstablished,
11
+ isClosing,
12
+ isClosed,
13
+ isValidSubprotocol,
14
+ fireEvent,
15
+ utf8Decode,
16
+ toArrayBuffer,
17
+ getURLRecord
18
+ } = require('./util')
19
+ const { establishWebSocketConnection, closeWebSocketConnection, failWebsocketConnection } = require('./connection')
20
+ const { ByteParser } = require('./receiver')
21
+ const { kEnumerableProperty } = require('../../core/util')
22
+ const { getGlobalDispatcher } = require('../../global')
23
+ const { ErrorEvent, CloseEvent, createFastMessageEvent } = require('./events')
24
+ const { SendQueue } = require('./sender')
25
+ const { WebsocketFrameSend } = require('./frame')
26
+ const { channels } = require('../../core/diagnostics')
27
+
28
+ function getSocketAddress (socket) {
29
+ if (typeof socket?.address === 'function') {
30
+ return socket.address()
31
+ }
32
+
33
+ if (typeof socket?.session?.socket?.address === 'function') {
34
+ return socket.session.socket.address()
35
+ }
36
+
37
+ return null
38
+ }
39
+
40
+ /**
41
+ * @typedef {object} Handler
42
+ * @property {(response: any, extensions?: string[]) => void} onConnectionEstablished
43
+ * @property {(opcode: number, data: Buffer) => void} onMessage
44
+ * @property {(error: Error) => void} onParserError
45
+ * @property {() => void} onParserDrain
46
+ * @property {(chunk: Buffer) => void} onSocketData
47
+ * @property {(err: Error) => void} onSocketError
48
+ * @property {() => void} onSocketClose
49
+ * @property {(body: Buffer) => void} onPing
50
+ * @property {(body: Buffer) => void} onPong
51
+ *
52
+ * @property {number} readyState
53
+ * @property {import('stream').Duplex} socket
54
+ * @property {Set<number>} closeState
55
+ * @property {import('../fetch/index').Fetch} controller
56
+ * @property {boolean} [wasEverConnected=false]
57
+ */
58
+
59
+ // https://websockets.spec.whatwg.org/#interface-definition
60
+ class WebSocket extends EventTarget {
61
+ #events = {
62
+ open: null,
63
+ error: null,
64
+ close: null,
65
+ message: null
66
+ }
67
+
68
+ #bufferedAmount = 0
69
+ #protocol = ''
70
+ #extensions = ''
71
+
72
+ /** @type {SendQueue} */
73
+ #sendQueue
74
+
75
+ /** @type {Handler} */
76
+ #handler = {
77
+ onConnectionEstablished: (response, extensions) => this.#onConnectionEstablished(response, extensions),
78
+ onMessage: (opcode, data) => this.#onMessage(opcode, data),
79
+ onParserError: (err) => failWebsocketConnection(this.#handler, null, err.message),
80
+ onParserDrain: () => this.#onParserDrain(),
81
+ onSocketData: (chunk) => {
82
+ if (!this.#parser.write(chunk)) {
83
+ this.#handler.socket.pause()
84
+ }
85
+ },
86
+ onSocketError: (err) => {
87
+ this.#handler.readyState = states.CLOSING
88
+
89
+ if (channels.socketError.hasSubscribers) {
90
+ channels.socketError.publish(err)
91
+ }
92
+
93
+ this.#handler.socket.destroy()
94
+ },
95
+ onSocketClose: () => this.#onSocketClose(),
96
+ onPing: (body) => {
97
+ if (channels.ping.hasSubscribers) {
98
+ channels.ping.publish({
99
+ payload: body,
100
+ websocket: this
101
+ })
102
+ }
103
+ },
104
+ onPong: (body) => {
105
+ if (channels.pong.hasSubscribers) {
106
+ channels.pong.publish({
107
+ payload: body,
108
+ websocket: this
109
+ })
110
+ }
111
+ },
112
+
113
+ readyState: states.CONNECTING,
114
+ socket: null,
115
+ closeState: new Set(),
116
+ controller: null,
117
+ wasEverConnected: false
118
+ }
119
+
120
+ #url
121
+ #binaryType
122
+ /** @type {import('./receiver').ByteParser} */
123
+ #parser
124
+
125
+ /**
126
+ * @param {string} url
127
+ * @param {string|string[]} protocols
128
+ */
129
+ constructor (url, protocols = []) {
130
+ super()
131
+
132
+ webidl.util.markAsUncloneable(this)
133
+
134
+ const prefix = 'WebSocket constructor'
135
+ webidl.argumentLengthCheck(arguments, 1, prefix)
136
+
137
+ const options = webidl.converters['DOMString or sequence<DOMString> or WebSocketInit'](protocols, prefix, 'options')
138
+
139
+ url = webidl.converters.USVString(url)
140
+ protocols = options.protocols
141
+
142
+ // 1. Let baseURL be this's relevant settings object's API base URL.
143
+ const baseURL = environmentSettingsObject.settingsObject.baseUrl
144
+
145
+ // 2. Let urlRecord be the result of getting a URL record given url and baseURL.
146
+ const urlRecord = getURLRecord(url, baseURL)
147
+
148
+ // 3. If protocols is a string, set protocols to a sequence consisting
149
+ // of just that string.
150
+ if (typeof protocols === 'string') {
151
+ protocols = [protocols]
152
+ }
153
+
154
+ // 4. If any of the values in protocols occur more than once or otherwise
155
+ // fail to match the requirements for elements that comprise the value
156
+ // of `Sec-WebSocket-Protocol` fields as defined by The WebSocket
157
+ // protocol, then throw a "SyntaxError" DOMException.
158
+ if (protocols.length !== new Set(protocols.map(p => p.toLowerCase())).size) {
159
+ throw new DOMException('Invalid Sec-WebSocket-Protocol value', 'SyntaxError')
160
+ }
161
+
162
+ if (protocols.length > 0 && !protocols.every(p => isValidSubprotocol(p))) {
163
+ throw new DOMException('Invalid Sec-WebSocket-Protocol value', 'SyntaxError')
164
+ }
165
+
166
+ // 5. Set this's url to urlRecord.
167
+ this.#url = new URL(urlRecord.href)
168
+
169
+ // 6. Let client be this's relevant settings object.
170
+ const client = environmentSettingsObject.settingsObject
171
+
172
+ // 7. Run this step in parallel:
173
+ // 7.1. Establish a WebSocket connection given urlRecord, protocols,
174
+ // and client.
175
+ this.#handler.controller = establishWebSocketConnection(
176
+ urlRecord,
177
+ protocols,
178
+ client,
179
+ this.#handler,
180
+ options
181
+ )
182
+
183
+ // Each WebSocket object has an associated ready state, which is a
184
+ // number representing the state of the connection. Initially it must
185
+ // be CONNECTING (0).
186
+ this.#handler.readyState = WebSocket.CONNECTING
187
+
188
+ // The extensions attribute must initially return the empty string.
189
+
190
+ // The protocol attribute must initially return the empty string.
191
+
192
+ // Each WebSocket object has an associated binary type, which is a
193
+ // BinaryType. Initially it must be "blob".
194
+ this.#binaryType = 'blob'
195
+ }
196
+
197
+ /**
198
+ * @see https://websockets.spec.whatwg.org/#dom-websocket-close
199
+ * @param {number|undefined} code
200
+ * @param {string|undefined} reason
201
+ */
202
+ close (code = undefined, reason = undefined) {
203
+ webidl.brandCheck(this, WebSocket)
204
+
205
+ const prefix = 'WebSocket.close'
206
+
207
+ if (code !== undefined) {
208
+ code = webidl.converters['unsigned short'](code, prefix, 'code', webidl.attributes.Clamp)
209
+ }
210
+
211
+ if (reason !== undefined) {
212
+ reason = webidl.converters.USVString(reason)
213
+ }
214
+
215
+ // 1. If code is the special value "missing", then set code to null.
216
+ code ??= null
217
+
218
+ // 2. If reason is the special value "missing", then set reason to the empty string.
219
+ reason ??= ''
220
+
221
+ // 3. Close the WebSocket with this, code, and reason.
222
+ closeWebSocketConnection(this.#handler, code, reason, true)
223
+ }
224
+
225
+ /**
226
+ * @see https://websockets.spec.whatwg.org/#dom-websocket-send
227
+ * @param {NodeJS.TypedArray|ArrayBuffer|Blob|string} data
228
+ */
229
+ send (data) {
230
+ webidl.brandCheck(this, WebSocket)
231
+
232
+ const prefix = 'WebSocket.send'
233
+ webidl.argumentLengthCheck(arguments, 1, prefix)
234
+
235
+ data = webidl.converters.WebSocketSendData(data, prefix, 'data')
236
+
237
+ // 1. If this's ready state is CONNECTING, then throw an
238
+ // "InvalidStateError" DOMException.
239
+ if (isConnecting(this.#handler.readyState)) {
240
+ throw new DOMException('Sent before connected.', 'InvalidStateError')
241
+ }
242
+
243
+ // 2. Run the appropriate set of steps from the following list:
244
+ // https://datatracker.ietf.org/doc/html/rfc6455#section-6.1
245
+ // https://datatracker.ietf.org/doc/html/rfc6455#section-5.2
246
+
247
+ if (!isEstablished(this.#handler.readyState) || isClosing(this.#handler.readyState)) {
248
+ return
249
+ }
250
+
251
+ // If data is a string
252
+ if (typeof data === 'string') {
253
+ // If the WebSocket connection is established and the WebSocket
254
+ // closing handshake has not yet started, then the user agent
255
+ // must send a WebSocket Message comprised of the data argument
256
+ // using a text frame opcode; if the data cannot be sent, e.g.
257
+ // because it would need to be buffered but the buffer is full,
258
+ // the user agent must flag the WebSocket as full and then close
259
+ // the WebSocket connection. Any invocation of this method with a
260
+ // string argument that does not throw an exception must increase
261
+ // the bufferedAmount attribute by the number of bytes needed to
262
+ // express the argument as UTF-8.
263
+
264
+ const buffer = Buffer.from(data)
265
+
266
+ this.#bufferedAmount += buffer.byteLength
267
+ this.#sendQueue.add(buffer, () => {
268
+ this.#bufferedAmount -= buffer.byteLength
269
+ }, sendHints.text)
270
+ } else if (isArrayBuffer(data)) {
271
+ // If the WebSocket connection is established, and the WebSocket
272
+ // closing handshake has not yet started, then the user agent must
273
+ // send a WebSocket Message comprised of data using a binary frame
274
+ // opcode; if the data cannot be sent, e.g. because it would need
275
+ // to be buffered but the buffer is full, the user agent must flag
276
+ // the WebSocket as full and then close the WebSocket connection.
277
+ // The data to be sent is the data stored in the buffer described
278
+ // by the ArrayBuffer object. Any invocation of this method with an
279
+ // ArrayBuffer argument that does not throw an exception must
280
+ // increase the bufferedAmount attribute by the length of the
281
+ // ArrayBuffer in bytes.
282
+
283
+ this.#bufferedAmount += data.byteLength
284
+ this.#sendQueue.add(data, () => {
285
+ this.#bufferedAmount -= data.byteLength
286
+ }, sendHints.arrayBuffer)
287
+ } else if (ArrayBuffer.isView(data)) {
288
+ // If the WebSocket connection is established, and the WebSocket
289
+ // closing handshake has not yet started, then the user agent must
290
+ // send a WebSocket Message comprised of data using a binary frame
291
+ // opcode; if the data cannot be sent, e.g. because it would need to
292
+ // be buffered but the buffer is full, the user agent must flag the
293
+ // WebSocket as full and then close the WebSocket connection. The
294
+ // data to be sent is the data stored in the section of the buffer
295
+ // described by the ArrayBuffer object that data references. Any
296
+ // invocation of this method with this kind of argument that does
297
+ // not throw an exception must increase the bufferedAmount attribute
298
+ // by the length of data’s buffer in bytes.
299
+
300
+ this.#bufferedAmount += data.byteLength
301
+ this.#sendQueue.add(data, () => {
302
+ this.#bufferedAmount -= data.byteLength
303
+ }, sendHints.typedArray)
304
+ } else if (webidl.is.Blob(data)) {
305
+ // If the WebSocket connection is established, and the WebSocket
306
+ // closing handshake has not yet started, then the user agent must
307
+ // send a WebSocket Message comprised of data using a binary frame
308
+ // opcode; if the data cannot be sent, e.g. because it would need to
309
+ // be buffered but the buffer is full, the user agent must flag the
310
+ // WebSocket as full and then close the WebSocket connection. The data
311
+ // to be sent is the raw data represented by the Blob object. Any
312
+ // invocation of this method with a Blob argument that does not throw
313
+ // an exception must increase the bufferedAmount attribute by the size
314
+ // of the Blob object’s raw data, in bytes.
315
+
316
+ this.#bufferedAmount += data.size
317
+ this.#sendQueue.add(data, () => {
318
+ this.#bufferedAmount -= data.size
319
+ }, sendHints.blob)
320
+ }
321
+ }
322
+
323
+ get readyState () {
324
+ webidl.brandCheck(this, WebSocket)
325
+
326
+ // The readyState getter steps are to return this's ready state.
327
+ return this.#handler.readyState
328
+ }
329
+
330
+ get bufferedAmount () {
331
+ webidl.brandCheck(this, WebSocket)
332
+
333
+ return this.#bufferedAmount
334
+ }
335
+
336
+ get url () {
337
+ webidl.brandCheck(this, WebSocket)
338
+
339
+ // The url getter steps are to return this's url, serialized.
340
+ return URLSerializer(this.#url)
341
+ }
342
+
343
+ get extensions () {
344
+ webidl.brandCheck(this, WebSocket)
345
+
346
+ return this.#extensions
347
+ }
348
+
349
+ get protocol () {
350
+ webidl.brandCheck(this, WebSocket)
351
+
352
+ return this.#protocol
353
+ }
354
+
355
+ get onopen () {
356
+ webidl.brandCheck(this, WebSocket)
357
+
358
+ return this.#events.open
359
+ }
360
+
361
+ set onopen (fn) {
362
+ webidl.brandCheck(this, WebSocket)
363
+
364
+ if (this.#events.open) {
365
+ this.removeEventListener('open', this.#events.open)
366
+ }
367
+
368
+ const listener = webidl.converters.EventHandlerNonNull(fn)
369
+
370
+ if (listener !== null) {
371
+ this.addEventListener('open', listener)
372
+ this.#events.open = fn
373
+ } else {
374
+ this.#events.open = null
375
+ }
376
+ }
377
+
378
+ get onerror () {
379
+ webidl.brandCheck(this, WebSocket)
380
+
381
+ return this.#events.error
382
+ }
383
+
384
+ set onerror (fn) {
385
+ webidl.brandCheck(this, WebSocket)
386
+
387
+ if (this.#events.error) {
388
+ this.removeEventListener('error', this.#events.error)
389
+ }
390
+
391
+ const listener = webidl.converters.EventHandlerNonNull(fn)
392
+
393
+ if (listener !== null) {
394
+ this.addEventListener('error', listener)
395
+ this.#events.error = fn
396
+ } else {
397
+ this.#events.error = null
398
+ }
399
+ }
400
+
401
+ get onclose () {
402
+ webidl.brandCheck(this, WebSocket)
403
+
404
+ return this.#events.close
405
+ }
406
+
407
+ set onclose (fn) {
408
+ webidl.brandCheck(this, WebSocket)
409
+
410
+ if (this.#events.close) {
411
+ this.removeEventListener('close', this.#events.close)
412
+ }
413
+
414
+ const listener = webidl.converters.EventHandlerNonNull(fn)
415
+
416
+ if (listener !== null) {
417
+ this.addEventListener('close', listener)
418
+ this.#events.close = fn
419
+ } else {
420
+ this.#events.close = null
421
+ }
422
+ }
423
+
424
+ get onmessage () {
425
+ webidl.brandCheck(this, WebSocket)
426
+
427
+ return this.#events.message
428
+ }
429
+
430
+ set onmessage (fn) {
431
+ webidl.brandCheck(this, WebSocket)
432
+
433
+ if (this.#events.message) {
434
+ this.removeEventListener('message', this.#events.message)
435
+ }
436
+
437
+ const listener = webidl.converters.EventHandlerNonNull(fn)
438
+
439
+ if (listener !== null) {
440
+ this.addEventListener('message', listener)
441
+ this.#events.message = fn
442
+ } else {
443
+ this.#events.message = null
444
+ }
445
+ }
446
+
447
+ get binaryType () {
448
+ webidl.brandCheck(this, WebSocket)
449
+
450
+ return this.#binaryType
451
+ }
452
+
453
+ set binaryType (type) {
454
+ webidl.brandCheck(this, WebSocket)
455
+
456
+ if (type !== 'blob' && type !== 'arraybuffer') {
457
+ this.#binaryType = 'blob'
458
+ } else {
459
+ this.#binaryType = type
460
+ }
461
+ }
462
+
463
+ /**
464
+ * @see https://websockets.spec.whatwg.org/#feedback-from-the-protocol
465
+ */
466
+ #onConnectionEstablished (response, parsedExtensions) {
467
+ // processResponse is called when the "response's header list has been received and initialized."
468
+ // once this happens, the connection is open
469
+ this.#handler.socket = response.socket
470
+
471
+ const parser = new ByteParser(this.#handler, parsedExtensions)
472
+ parser.on('drain', () => this.#handler.onParserDrain())
473
+ parser.on('error', (err) => this.#handler.onParserError(err))
474
+
475
+ this.#parser = parser
476
+ this.#sendQueue = new SendQueue(response.socket)
477
+
478
+ // 1. Change the ready state to OPEN (1).
479
+ this.#handler.readyState = states.OPEN
480
+
481
+ // 2. Change the extensions attribute’s value to the extensions in use, if
482
+ // it is not the null value.
483
+ // https://datatracker.ietf.org/doc/html/rfc6455#section-9.1
484
+ const extensions = response.headersList.get('sec-websocket-extensions')
485
+
486
+ if (extensions !== null) {
487
+ this.#extensions = extensions
488
+ }
489
+
490
+ // 3. Change the protocol attribute’s value to the subprotocol in use, if
491
+ // it is not the null value.
492
+ // https://datatracker.ietf.org/doc/html/rfc6455#section-1.9
493
+ const protocol = response.headersList.get('sec-websocket-protocol')
494
+
495
+ if (protocol !== null) {
496
+ this.#protocol = protocol
497
+ }
498
+
499
+ // 4. Fire an event named open at the WebSocket object.
500
+ fireEvent('open', this)
501
+
502
+ if (channels.open.hasSubscribers) {
503
+ // Convert headers to a plain object for the event
504
+ const headers = response.headersList.entries
505
+ channels.open.publish({
506
+ address: getSocketAddress(response.socket),
507
+ protocol: this.#protocol,
508
+ extensions: this.#extensions,
509
+ websocket: this,
510
+ handshakeResponse: {
511
+ status: response.status,
512
+ statusText: response.statusText,
513
+ headers
514
+ }
515
+ })
516
+ }
517
+ }
518
+
519
+ #onMessage (type, data) {
520
+ // 1. If ready state is not OPEN (1), then return.
521
+ if (this.#handler.readyState !== states.OPEN) {
522
+ return
523
+ }
524
+
525
+ // 2. Let dataForEvent be determined by switching on type and binary type:
526
+ let dataForEvent
527
+
528
+ if (type === opcodes.TEXT) {
529
+ // -> type indicates that the data is Text
530
+ // a new DOMString containing data
531
+ try {
532
+ dataForEvent = utf8Decode(data)
533
+ } catch {
534
+ failWebsocketConnection(this.#handler, 1007, 'Received invalid UTF-8 in text frame.')
535
+ return
536
+ }
537
+ } else if (type === opcodes.BINARY) {
538
+ if (this.#binaryType === 'blob') {
539
+ // -> type indicates that the data is Binary and binary type is "blob"
540
+ // a new Blob object, created in the relevant Realm of the WebSocket
541
+ // object, that represents data as its raw data
542
+ dataForEvent = new Blob([data])
543
+ } else {
544
+ // -> type indicates that the data is Binary and binary type is "arraybuffer"
545
+ // a new ArrayBuffer object, created in the relevant Realm of the
546
+ // WebSocket object, whose contents are data
547
+ dataForEvent = toArrayBuffer(data)
548
+ }
549
+ }
550
+
551
+ // 3. Fire an event named message at the WebSocket object, using MessageEvent,
552
+ // with the origin attribute initialized to the serialization of the WebSocket
553
+ // object’s url's origin, and the data attribute initialized to dataForEvent.
554
+ fireEvent('message', this, createFastMessageEvent, {
555
+ origin: this.#url.origin,
556
+ data: dataForEvent
557
+ })
558
+ }
559
+
560
+ #onParserDrain () {
561
+ this.#handler.socket.resume()
562
+ }
563
+
564
+ /**
565
+ * @see https://websockets.spec.whatwg.org/#feedback-from-the-protocol
566
+ * @see https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.4
567
+ */
568
+ #onSocketClose () {
569
+ // If the TCP connection was closed after the
570
+ // WebSocket closing handshake was completed, the WebSocket connection
571
+ // is said to have been closed _cleanly_.
572
+ const wasClean =
573
+ this.#handler.closeState.has(sentCloseFrameState.SENT) &&
574
+ this.#handler.closeState.has(sentCloseFrameState.RECEIVED)
575
+
576
+ let code = 1005
577
+ let reason = ''
578
+
579
+ const result = this.#parser?.closingInfo
580
+
581
+ if (result && !result.error) {
582
+ code = result.code ?? 1005
583
+ reason = result.reason
584
+ }
585
+
586
+ // 1. Change the ready state to CLOSED (3).
587
+ this.#handler.readyState = states.CLOSED
588
+
589
+ // 2. If the user agent was required to fail the WebSocket
590
+ // connection, or if the WebSocket connection was closed
591
+ // after being flagged as full, fire an event named error
592
+ // at the WebSocket object.
593
+ if (!this.#handler.closeState.has(sentCloseFrameState.RECEIVED)) {
594
+ // If _The WebSocket
595
+ // Connection is Closed_ and no Close control frame was received by the
596
+ // endpoint (such as could occur if the underlying transport connection
597
+ // is lost), _The WebSocket Connection Close Code_ is considered to be
598
+ // 1006.
599
+ code = 1006
600
+
601
+ fireEvent('error', this, (type, init) => new ErrorEvent(type, init), {
602
+ error: new TypeError(reason)
603
+ })
604
+ }
605
+
606
+ // 3. Fire an event named close at the WebSocket object,
607
+ // using CloseEvent, with the wasClean attribute
608
+ // initialized to true if the connection closed cleanly
609
+ // and false otherwise, the code attribute initialized to
610
+ // the WebSocket connection close code, and the reason
611
+ // attribute initialized to the result of applying UTF-8
612
+ // decode without BOM to the WebSocket connection close
613
+ // reason.
614
+ // TODO: process.nextTick
615
+ fireEvent('close', this, (type, init) => new CloseEvent(type, init), {
616
+ wasClean, code, reason
617
+ })
618
+
619
+ if (channels.close.hasSubscribers) {
620
+ channels.close.publish({
621
+ websocket: this,
622
+ code,
623
+ reason
624
+ })
625
+ }
626
+ }
627
+
628
+ /**
629
+ * @param {WebSocket} ws
630
+ * @param {Buffer|undefined} buffer
631
+ */
632
+ static ping (ws, buffer) {
633
+ if (Buffer.isBuffer(buffer)) {
634
+ if (buffer.length > 125) {
635
+ throw new TypeError('A PING frame cannot have a body larger than 125 bytes.')
636
+ }
637
+ } else if (buffer !== undefined) {
638
+ throw new TypeError('Expected buffer payload')
639
+ }
640
+
641
+ // An endpoint MAY send a Ping frame any time after the connection is
642
+ // established and before the connection is closed.
643
+ const readyState = ws.#handler.readyState
644
+
645
+ if (isEstablished(readyState) && !isClosing(readyState) && !isClosed(readyState)) {
646
+ const frame = new WebsocketFrameSend(buffer)
647
+ ws.#handler.socket.write(frame.createFrame(opcodes.PING))
648
+ }
649
+ }
650
+ }
651
+
652
+ const { ping } = WebSocket
653
+ Reflect.deleteProperty(WebSocket, 'ping')
654
+
655
+ // https://websockets.spec.whatwg.org/#dom-websocket-connecting
656
+ WebSocket.CONNECTING = WebSocket.prototype.CONNECTING = states.CONNECTING
657
+ // https://websockets.spec.whatwg.org/#dom-websocket-open
658
+ WebSocket.OPEN = WebSocket.prototype.OPEN = states.OPEN
659
+ // https://websockets.spec.whatwg.org/#dom-websocket-closing
660
+ WebSocket.CLOSING = WebSocket.prototype.CLOSING = states.CLOSING
661
+ // https://websockets.spec.whatwg.org/#dom-websocket-closed
662
+ WebSocket.CLOSED = WebSocket.prototype.CLOSED = states.CLOSED
663
+
664
+ Object.defineProperties(WebSocket.prototype, {
665
+ CONNECTING: staticPropertyDescriptors,
666
+ OPEN: staticPropertyDescriptors,
667
+ CLOSING: staticPropertyDescriptors,
668
+ CLOSED: staticPropertyDescriptors,
669
+ url: kEnumerableProperty,
670
+ readyState: kEnumerableProperty,
671
+ bufferedAmount: kEnumerableProperty,
672
+ onopen: kEnumerableProperty,
673
+ onerror: kEnumerableProperty,
674
+ onclose: kEnumerableProperty,
675
+ close: kEnumerableProperty,
676
+ onmessage: kEnumerableProperty,
677
+ binaryType: kEnumerableProperty,
678
+ send: kEnumerableProperty,
679
+ extensions: kEnumerableProperty,
680
+ protocol: kEnumerableProperty,
681
+ [Symbol.toStringTag]: {
682
+ value: 'WebSocket',
683
+ writable: false,
684
+ enumerable: false,
685
+ configurable: true
686
+ }
687
+ })
688
+
689
+ Object.defineProperties(WebSocket, {
690
+ CONNECTING: staticPropertyDescriptors,
691
+ OPEN: staticPropertyDescriptors,
692
+ CLOSING: staticPropertyDescriptors,
693
+ CLOSED: staticPropertyDescriptors
694
+ })
695
+
696
+ webidl.converters['sequence<DOMString>'] = webidl.sequenceConverter(
697
+ webidl.converters.DOMString
698
+ )
699
+
700
+ webidl.converters['DOMString or sequence<DOMString>'] = function (V, prefix, argument) {
701
+ if (webidl.util.Type(V) === webidl.util.Types.OBJECT && Symbol.iterator in V) {
702
+ return webidl.converters['sequence<DOMString>'](V)
703
+ }
704
+
705
+ return webidl.converters.DOMString(V, prefix, argument)
706
+ }
707
+
708
+ // This implements the proposal made in https://github.com/whatwg/websockets/issues/42
709
+ webidl.converters.WebSocketInit = webidl.dictionaryConverter([
710
+ {
711
+ key: 'protocols',
712
+ converter: webidl.converters['DOMString or sequence<DOMString>'],
713
+ defaultValue: () => []
714
+ },
715
+ {
716
+ key: 'dispatcher',
717
+ converter: webidl.converters.any,
718
+ defaultValue: () => getGlobalDispatcher()
719
+ },
720
+ {
721
+ key: 'headers',
722
+ converter: webidl.nullableConverter(webidl.converters.HeadersInit)
723
+ }
724
+ ])
725
+
726
+ webidl.converters['DOMString or sequence<DOMString> or WebSocketInit'] = function (V) {
727
+ if (webidl.util.Type(V) === webidl.util.Types.OBJECT && !(Symbol.iterator in V)) {
728
+ return webidl.converters.WebSocketInit(V)
729
+ }
730
+
731
+ return { protocols: webidl.converters['DOMString or sequence<DOMString>'](V) }
732
+ }
733
+
734
+ webidl.converters.WebSocketSendData = function (V) {
735
+ if (webidl.util.Type(V) === webidl.util.Types.OBJECT) {
736
+ if (webidl.is.Blob(V)) {
737
+ return V
738
+ }
739
+
740
+ if (webidl.is.BufferSource(V)) {
741
+ return V
742
+ }
743
+ }
744
+
745
+ return webidl.converters.USVString(V)
746
+ }
747
+
748
+ module.exports = {
749
+ WebSocket,
750
+ ping
751
+ }