deepv-code 1.0.182 → 1.0.185

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