crossws 0.0.0 → 0.1.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 (47) hide show
  1. package/LICENSE +46 -0
  2. package/README.md +286 -0
  3. package/adapters/bun.d.ts +1 -0
  4. package/adapters/cloudflare.d.ts +1 -0
  5. package/adapters/deno.d.ts +1 -0
  6. package/adapters/node.d.ts +1 -0
  7. package/dist/adapters/bun.cjs +72 -0
  8. package/dist/adapters/bun.d.cts +13 -0
  9. package/dist/adapters/bun.d.mts +13 -0
  10. package/dist/adapters/bun.d.ts +13 -0
  11. package/dist/adapters/bun.mjs +70 -0
  12. package/dist/adapters/cloudflare.cjs +52 -0
  13. package/dist/adapters/cloudflare.d.cts +12 -0
  14. package/dist/adapters/cloudflare.d.mts +12 -0
  15. package/dist/adapters/cloudflare.d.ts +12 -0
  16. package/dist/adapters/cloudflare.mjs +50 -0
  17. package/dist/adapters/deno.cjs +48 -0
  18. package/dist/adapters/deno.d.cts +10 -0
  19. package/dist/adapters/deno.d.mts +10 -0
  20. package/dist/adapters/deno.d.ts +10 -0
  21. package/dist/adapters/deno.mjs +46 -0
  22. package/dist/adapters/node.cjs +695 -0
  23. package/dist/adapters/node.d.cts +298 -0
  24. package/dist/adapters/node.d.mts +298 -0
  25. package/dist/adapters/node.d.ts +298 -0
  26. package/dist/adapters/node.mjs +687 -0
  27. package/dist/index.cjs +13 -0
  28. package/dist/index.d.cts +67 -0
  29. package/dist/index.d.mts +67 -0
  30. package/dist/index.d.ts +67 -0
  31. package/dist/index.mjs +7 -0
  32. package/dist/shared/crossws.21e14e0d.cjs +59 -0
  33. package/dist/shared/crossws.2ed26345.cjs +3931 -0
  34. package/dist/shared/crossws.9536f626.mjs +54 -0
  35. package/dist/shared/crossws.a5db571c.mjs +3910 -0
  36. package/dist/websocket/index.cjs +5 -0
  37. package/dist/websocket/index.d.cts +10 -0
  38. package/dist/websocket/index.d.mts +10 -0
  39. package/dist/websocket/index.d.ts +10 -0
  40. package/dist/websocket/index.mjs +3 -0
  41. package/dist/websocket/node.cjs +17 -0
  42. package/dist/websocket/node.d.cts +10 -0
  43. package/dist/websocket/node.d.mts +10 -0
  44. package/dist/websocket/node.d.ts +10 -0
  45. package/dist/websocket/node.mjs +15 -0
  46. package/package.json +94 -1
  47. package/websocket.d.ts +1 -0
@@ -0,0 +1,695 @@
1
+ 'use strict';
2
+
3
+ require('stream');
4
+ const websocket = require('../shared/crossws.2ed26345.cjs');
5
+ const require$$0 = require('events');
6
+ const require$$2 = require('http');
7
+ const require$$1 = require('crypto');
8
+ const peer = require('../shared/crossws.21e14e0d.cjs');
9
+ require('https');
10
+ require('net');
11
+ require('tls');
12
+ require('url');
13
+ require('zlib');
14
+ require('buffer');
15
+
16
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
17
+
18
+ const require$$0__default = /*#__PURE__*/_interopDefaultCompat(require$$0);
19
+ const require$$2__default = /*#__PURE__*/_interopDefaultCompat(require$$2);
20
+ const require$$1__default = /*#__PURE__*/_interopDefaultCompat(require$$1);
21
+
22
+ const { tokenChars } = websocket.validationExports;
23
+
24
+ /**
25
+ * Parses the `Sec-WebSocket-Protocol` header into a set of subprotocol names.
26
+ *
27
+ * @param {String} header The field value of the header
28
+ * @return {Set} The subprotocol names
29
+ * @public
30
+ */
31
+ function parse(header) {
32
+ const protocols = new Set();
33
+ let start = -1;
34
+ let end = -1;
35
+ let i = 0;
36
+
37
+ for (i; i < header.length; i++) {
38
+ const code = header.charCodeAt(i);
39
+
40
+ if (end === -1 && tokenChars[code] === 1) {
41
+ if (start === -1) start = i;
42
+ } else if (
43
+ i !== 0 &&
44
+ (code === 0x20 /* ' ' */ || code === 0x09) /* '\t' */
45
+ ) {
46
+ if (end === -1 && start !== -1) end = i;
47
+ } else if (code === 0x2c /* ',' */) {
48
+ if (start === -1) {
49
+ throw new SyntaxError(`Unexpected character at index ${i}`);
50
+ }
51
+
52
+ if (end === -1) end = i;
53
+
54
+ const protocol = header.slice(start, end);
55
+
56
+ if (protocols.has(protocol)) {
57
+ throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
58
+ }
59
+
60
+ protocols.add(protocol);
61
+ start = end = -1;
62
+ } else {
63
+ throw new SyntaxError(`Unexpected character at index ${i}`);
64
+ }
65
+ }
66
+
67
+ if (start === -1 || end !== -1) {
68
+ throw new SyntaxError('Unexpected end of input');
69
+ }
70
+
71
+ const protocol = header.slice(start, i);
72
+
73
+ if (protocols.has(protocol)) {
74
+ throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
75
+ }
76
+
77
+ protocols.add(protocol);
78
+ return protocols;
79
+ }
80
+
81
+ var subprotocol$1 = { parse };
82
+
83
+ /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Duplex$" }] */
84
+
85
+ const EventEmitter = require$$0__default;
86
+ const http = require$$2__default;
87
+ const { createHash } = require$$1__default;
88
+
89
+ const extension = websocket.extension;
90
+ const PerMessageDeflate = websocket.permessageDeflate;
91
+ const subprotocol = subprotocol$1;
92
+ const WebSocket = websocket.websocket;
93
+ const { GUID, kWebSocket } = websocket.constants;
94
+
95
+ const keyRegex = /^[+/0-9A-Za-z]{22}==$/;
96
+
97
+ const RUNNING = 0;
98
+ const CLOSING = 1;
99
+ const CLOSED = 2;
100
+
101
+ /**
102
+ * Class representing a WebSocket server.
103
+ *
104
+ * @extends EventEmitter
105
+ */
106
+ class WebSocketServer extends EventEmitter {
107
+ /**
108
+ * Create a `WebSocketServer` instance.
109
+ *
110
+ * @param {Object} options Configuration options
111
+ * @param {Boolean} [options.allowSynchronousEvents=false] Specifies whether
112
+ * any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
113
+ * multiple times in the same tick
114
+ * @param {Boolean} [options.autoPong=true] Specifies whether or not to
115
+ * automatically send a pong in response to a ping
116
+ * @param {Number} [options.backlog=511] The maximum length of the queue of
117
+ * pending connections
118
+ * @param {Boolean} [options.clientTracking=true] Specifies whether or not to
119
+ * track clients
120
+ * @param {Function} [options.handleProtocols] A hook to handle protocols
121
+ * @param {String} [options.host] The hostname where to bind the server
122
+ * @param {Number} [options.maxPayload=104857600] The maximum allowed message
123
+ * size
124
+ * @param {Boolean} [options.noServer=false] Enable no server mode
125
+ * @param {String} [options.path] Accept only connections matching this path
126
+ * @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
127
+ * permessage-deflate
128
+ * @param {Number} [options.port] The port where to bind the server
129
+ * @param {(http.Server|https.Server)} [options.server] A pre-created HTTP/S
130
+ * server to use
131
+ * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
132
+ * not to skip UTF-8 validation for text and close messages
133
+ * @param {Function} [options.verifyClient] A hook to reject connections
134
+ * @param {Function} [options.WebSocket=WebSocket] Specifies the `WebSocket`
135
+ * class to use. It must be the `WebSocket` class or class that extends it
136
+ * @param {Function} [callback] A listener for the `listening` event
137
+ */
138
+ constructor(options, callback) {
139
+ super();
140
+
141
+ options = {
142
+ allowSynchronousEvents: false,
143
+ autoPong: true,
144
+ maxPayload: 100 * 1024 * 1024,
145
+ skipUTF8Validation: false,
146
+ perMessageDeflate: false,
147
+ handleProtocols: null,
148
+ clientTracking: true,
149
+ verifyClient: null,
150
+ noServer: false,
151
+ backlog: null, // use default (511 as implemented in net.js)
152
+ server: null,
153
+ host: null,
154
+ path: null,
155
+ port: null,
156
+ WebSocket,
157
+ ...options
158
+ };
159
+
160
+ if (
161
+ (options.port == null && !options.server && !options.noServer) ||
162
+ (options.port != null && (options.server || options.noServer)) ||
163
+ (options.server && options.noServer)
164
+ ) {
165
+ throw new TypeError(
166
+ 'One and only one of the "port", "server", or "noServer" options ' +
167
+ 'must be specified'
168
+ );
169
+ }
170
+
171
+ if (options.port != null) {
172
+ this._server = http.createServer((req, res) => {
173
+ const body = http.STATUS_CODES[426];
174
+
175
+ res.writeHead(426, {
176
+ 'Content-Length': body.length,
177
+ 'Content-Type': 'text/plain'
178
+ });
179
+ res.end(body);
180
+ });
181
+ this._server.listen(
182
+ options.port,
183
+ options.host,
184
+ options.backlog,
185
+ callback
186
+ );
187
+ } else if (options.server) {
188
+ this._server = options.server;
189
+ }
190
+
191
+ if (this._server) {
192
+ const emitConnection = this.emit.bind(this, 'connection');
193
+
194
+ this._removeListeners = addListeners(this._server, {
195
+ listening: this.emit.bind(this, 'listening'),
196
+ error: this.emit.bind(this, 'error'),
197
+ upgrade: (req, socket, head) => {
198
+ this.handleUpgrade(req, socket, head, emitConnection);
199
+ }
200
+ });
201
+ }
202
+
203
+ if (options.perMessageDeflate === true) options.perMessageDeflate = {};
204
+ if (options.clientTracking) {
205
+ this.clients = new Set();
206
+ this._shouldEmitClose = false;
207
+ }
208
+
209
+ this.options = options;
210
+ this._state = RUNNING;
211
+ }
212
+
213
+ /**
214
+ * Returns the bound address, the address family name, and port of the server
215
+ * as reported by the operating system if listening on an IP socket.
216
+ * If the server is listening on a pipe or UNIX domain socket, the name is
217
+ * returned as a string.
218
+ *
219
+ * @return {(Object|String|null)} The address of the server
220
+ * @public
221
+ */
222
+ address() {
223
+ if (this.options.noServer) {
224
+ throw new Error('The server is operating in "noServer" mode');
225
+ }
226
+
227
+ if (!this._server) return null;
228
+ return this._server.address();
229
+ }
230
+
231
+ /**
232
+ * Stop the server from accepting new connections and emit the `'close'` event
233
+ * when all existing connections are closed.
234
+ *
235
+ * @param {Function} [cb] A one-time listener for the `'close'` event
236
+ * @public
237
+ */
238
+ close(cb) {
239
+ if (this._state === CLOSED) {
240
+ if (cb) {
241
+ this.once('close', () => {
242
+ cb(new Error('The server is not running'));
243
+ });
244
+ }
245
+
246
+ process.nextTick(emitClose, this);
247
+ return;
248
+ }
249
+
250
+ if (cb) this.once('close', cb);
251
+
252
+ if (this._state === CLOSING) return;
253
+ this._state = CLOSING;
254
+
255
+ if (this.options.noServer || this.options.server) {
256
+ if (this._server) {
257
+ this._removeListeners();
258
+ this._removeListeners = this._server = null;
259
+ }
260
+
261
+ if (this.clients) {
262
+ if (!this.clients.size) {
263
+ process.nextTick(emitClose, this);
264
+ } else {
265
+ this._shouldEmitClose = true;
266
+ }
267
+ } else {
268
+ process.nextTick(emitClose, this);
269
+ }
270
+ } else {
271
+ const server = this._server;
272
+
273
+ this._removeListeners();
274
+ this._removeListeners = this._server = null;
275
+
276
+ //
277
+ // The HTTP/S server was created internally. Close it, and rely on its
278
+ // `'close'` event.
279
+ //
280
+ server.close(() => {
281
+ emitClose(this);
282
+ });
283
+ }
284
+ }
285
+
286
+ /**
287
+ * See if a given request should be handled by this server instance.
288
+ *
289
+ * @param {http.IncomingMessage} req Request object to inspect
290
+ * @return {Boolean} `true` if the request is valid, else `false`
291
+ * @public
292
+ */
293
+ shouldHandle(req) {
294
+ if (this.options.path) {
295
+ const index = req.url.indexOf('?');
296
+ const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
297
+
298
+ if (pathname !== this.options.path) return false;
299
+ }
300
+
301
+ return true;
302
+ }
303
+
304
+ /**
305
+ * Handle a HTTP Upgrade request.
306
+ *
307
+ * @param {http.IncomingMessage} req The request object
308
+ * @param {Duplex} socket The network socket between the server and client
309
+ * @param {Buffer} head The first packet of the upgraded stream
310
+ * @param {Function} cb Callback
311
+ * @public
312
+ */
313
+ handleUpgrade(req, socket, head, cb) {
314
+ socket.on('error', socketOnError);
315
+
316
+ const key = req.headers['sec-websocket-key'];
317
+ const version = +req.headers['sec-websocket-version'];
318
+
319
+ if (req.method !== 'GET') {
320
+ const message = 'Invalid HTTP method';
321
+ abortHandshakeOrEmitwsClientError(this, req, socket, 405, message);
322
+ return;
323
+ }
324
+
325
+ if (req.headers.upgrade.toLowerCase() !== 'websocket') {
326
+ const message = 'Invalid Upgrade header';
327
+ abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
328
+ return;
329
+ }
330
+
331
+ if (!key || !keyRegex.test(key)) {
332
+ const message = 'Missing or invalid Sec-WebSocket-Key header';
333
+ abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
334
+ return;
335
+ }
336
+
337
+ if (version !== 8 && version !== 13) {
338
+ const message = 'Missing or invalid Sec-WebSocket-Version header';
339
+ abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
340
+ return;
341
+ }
342
+
343
+ if (!this.shouldHandle(req)) {
344
+ abortHandshake(socket, 400);
345
+ return;
346
+ }
347
+
348
+ const secWebSocketProtocol = req.headers['sec-websocket-protocol'];
349
+ let protocols = new Set();
350
+
351
+ if (secWebSocketProtocol !== undefined) {
352
+ try {
353
+ protocols = subprotocol.parse(secWebSocketProtocol);
354
+ } catch (err) {
355
+ const message = 'Invalid Sec-WebSocket-Protocol header';
356
+ abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
357
+ return;
358
+ }
359
+ }
360
+
361
+ const secWebSocketExtensions = req.headers['sec-websocket-extensions'];
362
+ const extensions = {};
363
+
364
+ if (
365
+ this.options.perMessageDeflate &&
366
+ secWebSocketExtensions !== undefined
367
+ ) {
368
+ const perMessageDeflate = new PerMessageDeflate(
369
+ this.options.perMessageDeflate,
370
+ true,
371
+ this.options.maxPayload
372
+ );
373
+
374
+ try {
375
+ const offers = extension.parse(secWebSocketExtensions);
376
+
377
+ if (offers[PerMessageDeflate.extensionName]) {
378
+ perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
379
+ extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
380
+ }
381
+ } catch (err) {
382
+ const message =
383
+ 'Invalid or unacceptable Sec-WebSocket-Extensions header';
384
+ abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
385
+ return;
386
+ }
387
+ }
388
+
389
+ //
390
+ // Optionally call external client verification handler.
391
+ //
392
+ if (this.options.verifyClient) {
393
+ const info = {
394
+ origin:
395
+ req.headers[`${version === 8 ? 'sec-websocket-origin' : 'origin'}`],
396
+ secure: !!(req.socket.authorized || req.socket.encrypted),
397
+ req
398
+ };
399
+
400
+ if (this.options.verifyClient.length === 2) {
401
+ this.options.verifyClient(info, (verified, code, message, headers) => {
402
+ if (!verified) {
403
+ return abortHandshake(socket, code || 401, message, headers);
404
+ }
405
+
406
+ this.completeUpgrade(
407
+ extensions,
408
+ key,
409
+ protocols,
410
+ req,
411
+ socket,
412
+ head,
413
+ cb
414
+ );
415
+ });
416
+ return;
417
+ }
418
+
419
+ if (!this.options.verifyClient(info)) return abortHandshake(socket, 401);
420
+ }
421
+
422
+ this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
423
+ }
424
+
425
+ /**
426
+ * Upgrade the connection to WebSocket.
427
+ *
428
+ * @param {Object} extensions The accepted extensions
429
+ * @param {String} key The value of the `Sec-WebSocket-Key` header
430
+ * @param {Set} protocols The subprotocols
431
+ * @param {http.IncomingMessage} req The request object
432
+ * @param {Duplex} socket The network socket between the server and client
433
+ * @param {Buffer} head The first packet of the upgraded stream
434
+ * @param {Function} cb Callback
435
+ * @throws {Error} If called more than once with the same socket
436
+ * @private
437
+ */
438
+ completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
439
+ //
440
+ // Destroy the socket if the client has already sent a FIN packet.
441
+ //
442
+ if (!socket.readable || !socket.writable) return socket.destroy();
443
+
444
+ if (socket[kWebSocket]) {
445
+ throw new Error(
446
+ 'server.handleUpgrade() was called more than once with the same ' +
447
+ 'socket, possibly due to a misconfiguration'
448
+ );
449
+ }
450
+
451
+ if (this._state > RUNNING) return abortHandshake(socket, 503);
452
+
453
+ const digest = createHash('sha1')
454
+ .update(key + GUID)
455
+ .digest('base64');
456
+
457
+ const headers = [
458
+ 'HTTP/1.1 101 Switching Protocols',
459
+ 'Upgrade: websocket',
460
+ 'Connection: Upgrade',
461
+ `Sec-WebSocket-Accept: ${digest}`
462
+ ];
463
+
464
+ const ws = new this.options.WebSocket(null, undefined, this.options);
465
+
466
+ if (protocols.size) {
467
+ //
468
+ // Optionally call external protocol selection handler.
469
+ //
470
+ const protocol = this.options.handleProtocols
471
+ ? this.options.handleProtocols(protocols, req)
472
+ : protocols.values().next().value;
473
+
474
+ if (protocol) {
475
+ headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
476
+ ws._protocol = protocol;
477
+ }
478
+ }
479
+
480
+ if (extensions[PerMessageDeflate.extensionName]) {
481
+ const params = extensions[PerMessageDeflate.extensionName].params;
482
+ const value = extension.format({
483
+ [PerMessageDeflate.extensionName]: [params]
484
+ });
485
+ headers.push(`Sec-WebSocket-Extensions: ${value}`);
486
+ ws._extensions = extensions;
487
+ }
488
+
489
+ //
490
+ // Allow external modification/inspection of handshake headers.
491
+ //
492
+ this.emit('headers', headers, req);
493
+
494
+ socket.write(headers.concat('\r\n').join('\r\n'));
495
+ socket.removeListener('error', socketOnError);
496
+
497
+ ws.setSocket(socket, head, {
498
+ allowSynchronousEvents: this.options.allowSynchronousEvents,
499
+ maxPayload: this.options.maxPayload,
500
+ skipUTF8Validation: this.options.skipUTF8Validation
501
+ });
502
+
503
+ if (this.clients) {
504
+ this.clients.add(ws);
505
+ ws.on('close', () => {
506
+ this.clients.delete(ws);
507
+
508
+ if (this._shouldEmitClose && !this.clients.size) {
509
+ process.nextTick(emitClose, this);
510
+ }
511
+ });
512
+ }
513
+
514
+ cb(ws, req);
515
+ }
516
+ }
517
+
518
+ var websocketServer = WebSocketServer;
519
+
520
+ /**
521
+ * Add event listeners on an `EventEmitter` using a map of <event, listener>
522
+ * pairs.
523
+ *
524
+ * @param {EventEmitter} server The event emitter
525
+ * @param {Object.<String, Function>} map The listeners to add
526
+ * @return {Function} A function that will remove the added listeners when
527
+ * called
528
+ * @private
529
+ */
530
+ function addListeners(server, map) {
531
+ for (const event of Object.keys(map)) server.on(event, map[event]);
532
+
533
+ return function removeListeners() {
534
+ for (const event of Object.keys(map)) {
535
+ server.removeListener(event, map[event]);
536
+ }
537
+ };
538
+ }
539
+
540
+ /**
541
+ * Emit a `'close'` event on an `EventEmitter`.
542
+ *
543
+ * @param {EventEmitter} server The event emitter
544
+ * @private
545
+ */
546
+ function emitClose(server) {
547
+ server._state = CLOSED;
548
+ server.emit('close');
549
+ }
550
+
551
+ /**
552
+ * Handle socket errors.
553
+ *
554
+ * @private
555
+ */
556
+ function socketOnError() {
557
+ this.destroy();
558
+ }
559
+
560
+ /**
561
+ * Close the connection when preconditions are not fulfilled.
562
+ *
563
+ * @param {Duplex} socket The socket of the upgrade request
564
+ * @param {Number} code The HTTP response status code
565
+ * @param {String} [message] The HTTP response body
566
+ * @param {Object} [headers] Additional HTTP response headers
567
+ * @private
568
+ */
569
+ function abortHandshake(socket, code, message, headers) {
570
+ //
571
+ // The socket is writable unless the user destroyed or ended it before calling
572
+ // `server.handleUpgrade()` or in the `verifyClient` function, which is a user
573
+ // error. Handling this does not make much sense as the worst that can happen
574
+ // is that some of the data written by the user might be discarded due to the
575
+ // call to `socket.end()` below, which triggers an `'error'` event that in
576
+ // turn causes the socket to be destroyed.
577
+ //
578
+ message = message || http.STATUS_CODES[code];
579
+ headers = {
580
+ Connection: 'close',
581
+ 'Content-Type': 'text/html',
582
+ 'Content-Length': Buffer.byteLength(message),
583
+ ...headers
584
+ };
585
+
586
+ socket.once('finish', socket.destroy);
587
+
588
+ socket.end(
589
+ `HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r\n` +
590
+ Object.keys(headers)
591
+ .map((h) => `${h}: ${headers[h]}`)
592
+ .join('\r\n') +
593
+ '\r\n\r\n' +
594
+ message
595
+ );
596
+ }
597
+
598
+ /**
599
+ * Emit a `'wsClientError'` event on a `WebSocketServer` if there is at least
600
+ * one listener for it, otherwise call `abortHandshake()`.
601
+ *
602
+ * @param {WebSocketServer} server The WebSocket server
603
+ * @param {http.IncomingMessage} req The request object
604
+ * @param {Duplex} socket The socket of the upgrade request
605
+ * @param {Number} code The HTTP response status code
606
+ * @param {String} message The HTTP response body
607
+ * @private
608
+ */
609
+ function abortHandshakeOrEmitwsClientError(server, req, socket, code, message) {
610
+ if (server.listenerCount('wsClientError')) {
611
+ const err = new Error(message);
612
+ Error.captureStackTrace(err, abortHandshakeOrEmitwsClientError);
613
+
614
+ server.emit('wsClientError', err, socket, req);
615
+ } else {
616
+ abortHandshake(socket, code, message);
617
+ }
618
+ }
619
+
620
+ const _WebSocketServer = /*@__PURE__*/websocket.getDefaultExportFromCjs(websocketServer);
621
+
622
+ const node = peer.defineWebSocketAdapter(
623
+ (hooks, opts = {}) => {
624
+ const wss = opts.wss || new _WebSocketServer({
625
+ noServer: true,
626
+ ...opts.serverOptions
627
+ });
628
+ wss.on("connection", (ws, req) => {
629
+ const peer$1 = new NodeWebSocketPeer({ node: { ws, req, server: wss } });
630
+ hooks.open?.(peer$1);
631
+ ws.on("message", (data, isBinary) => {
632
+ hooks["node:message"]?.(peer$1, data, isBinary);
633
+ if (Array.isArray(data)) {
634
+ data = Buffer.concat(data);
635
+ }
636
+ hooks.message?.(peer$1, new peer.WebSocketMessage(data, isBinary));
637
+ });
638
+ ws.on("error", (error) => {
639
+ hooks["node:error"]?.(peer$1, error);
640
+ hooks.error?.(peer$1, new peer.WebSocketError(error));
641
+ });
642
+ ws.on("close", (code, reason) => {
643
+ hooks["node:close"]?.(peer$1, code, reason);
644
+ hooks.close?.(peer$1, {
645
+ code,
646
+ reason: reason?.toString()
647
+ });
648
+ });
649
+ ws.on("open", () => {
650
+ hooks["node:open"]?.(peer$1);
651
+ });
652
+ ws.on("ping", (data) => {
653
+ hooks["node:ping"]?.(peer$1, data);
654
+ });
655
+ ws.on("pong", (data) => {
656
+ hooks["node:pong"]?.(peer$1, data);
657
+ });
658
+ ws.on(
659
+ "unexpected-response",
660
+ (req2, res) => {
661
+ hooks["node:unexpected-response"]?.(peer$1, req2, res);
662
+ }
663
+ );
664
+ ws.on("upgrade", (req2) => {
665
+ hooks["node:upgrade"]?.(peer$1, req2);
666
+ });
667
+ });
668
+ return {
669
+ handleUpgrade: (req, socket, head) => {
670
+ wss.handleUpgrade(req, socket, head, (ws) => {
671
+ wss.emit("connection", ws, req);
672
+ });
673
+ }
674
+ };
675
+ }
676
+ );
677
+ class NodeWebSocketPeer extends peer.WebSocketPeerBase {
678
+ get id() {
679
+ const socket = this.ctx.node.req.socket;
680
+ if (!socket) {
681
+ return void 0;
682
+ }
683
+ const addr = socket.remoteFamily === "IPv6" ? `[${socket.remoteAddress}]` : socket.remoteAddress;
684
+ return `${addr}:${socket.remotePort}`;
685
+ }
686
+ get readyState() {
687
+ return this.ctx.node.ws.readyState;
688
+ }
689
+ send(message, compress) {
690
+ this.ctx.node.ws.send(message, { compress });
691
+ return 0;
692
+ }
693
+ }
694
+
695
+ module.exports = node;