@tinycloud/cli 0.7.0-beta.22 → 0.7.0-beta.23

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.
@@ -5,111 +5,18 @@ import {
5
5
  __toESM
6
6
  } from "./chunk-PLDDJCW6.js";
7
7
 
8
- // ../../node_modules/ws/lib/stream.js
9
- var require_stream = __commonJS({
10
- "../../node_modules/ws/lib/stream.js"(exports, module) {
11
- "use strict";
12
- var { Duplex } = __require("stream");
13
- function emitClose(stream) {
14
- stream.emit("close");
15
- }
16
- function duplexOnEnd() {
17
- if (!this.destroyed && this._writableState.finished) {
18
- this.destroy();
19
- }
20
- }
21
- function duplexOnError(err) {
22
- this.removeListener("error", duplexOnError);
23
- this.destroy();
24
- if (this.listenerCount("error") === 0) {
25
- this.emit("error", err);
26
- }
27
- }
28
- function createWebSocketStream2(ws, options) {
29
- let terminateOnDestroy = true;
30
- const duplex = new Duplex({
31
- ...options,
32
- autoDestroy: false,
33
- emitClose: false,
34
- objectMode: false,
35
- writableObjectMode: false
36
- });
37
- ws.on("message", function message(msg, isBinary) {
38
- const data = !isBinary && duplex._readableState.objectMode ? msg.toString() : msg;
39
- if (!duplex.push(data)) ws.pause();
40
- });
41
- ws.once("error", function error(err) {
42
- if (duplex.destroyed) return;
43
- terminateOnDestroy = false;
44
- duplex.destroy(err);
45
- });
46
- ws.once("close", function close() {
47
- if (duplex.destroyed) return;
48
- duplex.push(null);
49
- });
50
- duplex._destroy = function(err, callback) {
51
- if (ws.readyState === ws.CLOSED) {
52
- callback(err);
53
- process.nextTick(emitClose, duplex);
54
- return;
55
- }
56
- let called = false;
57
- ws.once("error", function error(err2) {
58
- called = true;
59
- callback(err2);
60
- });
61
- ws.once("close", function close() {
62
- if (!called) callback(err);
63
- process.nextTick(emitClose, duplex);
64
- });
65
- if (terminateOnDestroy) ws.terminate();
66
- };
67
- duplex._final = function(callback) {
68
- if (ws.readyState === ws.CONNECTING) {
69
- ws.once("open", function open() {
70
- duplex._final(callback);
71
- });
72
- return;
73
- }
74
- if (ws._socket === null) return;
75
- if (ws._socket._writableState.finished) {
76
- callback();
77
- if (duplex._readableState.endEmitted) duplex.destroy();
78
- } else {
79
- ws._socket.once("finish", function finish() {
80
- callback();
81
- });
82
- ws.close();
83
- }
84
- };
85
- duplex._read = function() {
86
- if (ws.isPaused) ws.resume();
87
- };
88
- duplex._write = function(chunk, encoding, callback) {
89
- if (ws.readyState === ws.CONNECTING) {
90
- ws.once("open", function open() {
91
- duplex._write(chunk, encoding, callback);
92
- });
93
- return;
94
- }
95
- ws.send(chunk, callback);
96
- };
97
- duplex.on("end", duplexOnEnd);
98
- duplex.on("error", duplexOnError);
99
- return duplex;
100
- }
101
- module.exports = createWebSocketStream2;
102
- }
103
- });
104
-
105
8
  // ../../node_modules/ws/lib/constants.js
106
9
  var require_constants = __commonJS({
107
10
  "../../node_modules/ws/lib/constants.js"(exports, module) {
108
11
  "use strict";
12
+ var BINARY_TYPES = ["nodebuffer", "arraybuffer", "fragments"];
13
+ var hasBlob = typeof Blob !== "undefined";
14
+ if (hasBlob) BINARY_TYPES.push("blob");
109
15
  module.exports = {
110
- BINARY_TYPES: ["nodebuffer", "arraybuffer", "fragments"],
16
+ BINARY_TYPES,
111
17
  EMPTY_BUFFER: Buffer.alloc(0),
112
18
  GUID: "258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
19
+ hasBlob,
113
20
  kForOnEventAttribute: /* @__PURE__ */ Symbol("kIsForOnEventAttribute"),
114
21
  kListener: /* @__PURE__ */ Symbol("kListener"),
115
22
  kStatusCode: /* @__PURE__ */ Symbol("status-code"),
@@ -832,6 +739,10 @@ var require_permessage_deflate = __commonJS({
832
739
  }
833
740
  function inflateOnError(err) {
834
741
  this[kPerMessageDeflate]._inflate = null;
742
+ if (this[kError]) {
743
+ this[kCallback](this[kError]);
744
+ return;
745
+ }
835
746
  err[kStatusCode] = 1007;
836
747
  this[kCallback](err);
837
748
  }
@@ -892,6 +803,7 @@ var require_validation = __commonJS({
892
803
  "../../node_modules/ws/lib/validation.js"(exports, module) {
893
804
  "use strict";
894
805
  var { isUtf8 } = __require("buffer");
806
+ var { hasBlob } = require_constants();
895
807
  var tokenChars = [
896
808
  0,
897
809
  0,
@@ -1062,7 +974,11 @@ var require_validation = __commonJS({
1062
974
  }
1063
975
  return true;
1064
976
  }
977
+ function isBlob(value) {
978
+ return hasBlob && typeof value === "object" && typeof value.arrayBuffer === "function" && typeof value.type === "string" && typeof value.stream === "function" && (value[Symbol.toStringTag] === "Blob" || value[Symbol.toStringTag] === "File");
979
+ }
1065
980
  module.exports = {
981
+ isBlob,
1066
982
  isValidStatusCode,
1067
983
  isValidUTF8: _isValidUTF8,
1068
984
  tokenChars
@@ -1544,6 +1460,8 @@ var require_receiver = __commonJS({
1544
1460
  data = concat(fragments, messageLength);
1545
1461
  } else if (this._binaryType === "arraybuffer") {
1546
1462
  data = toArrayBuffer(concat(fragments, messageLength));
1463
+ } else if (this._binaryType === "blob") {
1464
+ data = new Blob(fragments);
1547
1465
  } else {
1548
1466
  data = fragments;
1549
1467
  }
@@ -1680,14 +1598,17 @@ var require_sender = __commonJS({
1680
1598
  var { Duplex } = __require("stream");
1681
1599
  var { randomFillSync } = __require("crypto");
1682
1600
  var PerMessageDeflate = require_permessage_deflate();
1683
- var { EMPTY_BUFFER } = require_constants();
1684
- var { isValidStatusCode } = require_validation();
1601
+ var { EMPTY_BUFFER, kWebSocket, NOOP } = require_constants();
1602
+ var { isBlob, isValidStatusCode } = require_validation();
1685
1603
  var { mask: applyMask, toBuffer } = require_buffer_util();
1686
1604
  var kByteLength = /* @__PURE__ */ Symbol("kByteLength");
1687
1605
  var maskBuffer = Buffer.alloc(4);
1688
1606
  var RANDOM_POOL_SIZE = 8 * 1024;
1689
1607
  var randomPool;
1690
1608
  var randomPoolPointer = RANDOM_POOL_SIZE;
1609
+ var DEFAULT = 0;
1610
+ var DEFLATING = 1;
1611
+ var GET_BLOB_DATA = 2;
1691
1612
  var Sender2 = class _Sender {
1692
1613
  /**
1693
1614
  * Creates a Sender instance.
@@ -1707,8 +1628,10 @@ var require_sender = __commonJS({
1707
1628
  this._firstFragment = true;
1708
1629
  this._compress = false;
1709
1630
  this._bufferedBytes = 0;
1710
- this._deflating = false;
1711
1631
  this._queue = [];
1632
+ this._state = DEFAULT;
1633
+ this.onerror = NOOP;
1634
+ this[kWebSocket] = void 0;
1712
1635
  }
1713
1636
  /**
1714
1637
  * Frames a piece of data according to the HyBi WebSocket protocol.
@@ -1841,7 +1764,7 @@ var require_sender = __commonJS({
1841
1764
  readOnly: false,
1842
1765
  rsv1: false
1843
1766
  };
1844
- if (this._deflating) {
1767
+ if (this._state !== DEFAULT) {
1845
1768
  this.enqueue([this.dispatch, buf, false, options, cb]);
1846
1769
  } else {
1847
1770
  this.sendFrame(_Sender.frame(buf, options), cb);
@@ -1861,6 +1784,9 @@ var require_sender = __commonJS({
1861
1784
  if (typeof data === "string") {
1862
1785
  byteLength = Buffer.byteLength(data);
1863
1786
  readOnly = false;
1787
+ } else if (isBlob(data)) {
1788
+ byteLength = data.size;
1789
+ readOnly = false;
1864
1790
  } else {
1865
1791
  data = toBuffer(data);
1866
1792
  byteLength = data.length;
@@ -1879,7 +1805,13 @@ var require_sender = __commonJS({
1879
1805
  readOnly,
1880
1806
  rsv1: false
1881
1807
  };
1882
- if (this._deflating) {
1808
+ if (isBlob(data)) {
1809
+ if (this._state !== DEFAULT) {
1810
+ this.enqueue([this.getBlobData, data, false, options, cb]);
1811
+ } else {
1812
+ this.getBlobData(data, false, options, cb);
1813
+ }
1814
+ } else if (this._state !== DEFAULT) {
1883
1815
  this.enqueue([this.dispatch, data, false, options, cb]);
1884
1816
  } else {
1885
1817
  this.sendFrame(_Sender.frame(data, options), cb);
@@ -1899,6 +1831,9 @@ var require_sender = __commonJS({
1899
1831
  if (typeof data === "string") {
1900
1832
  byteLength = Buffer.byteLength(data);
1901
1833
  readOnly = false;
1834
+ } else if (isBlob(data)) {
1835
+ byteLength = data.size;
1836
+ readOnly = false;
1902
1837
  } else {
1903
1838
  data = toBuffer(data);
1904
1839
  byteLength = data.length;
@@ -1917,7 +1852,13 @@ var require_sender = __commonJS({
1917
1852
  readOnly,
1918
1853
  rsv1: false
1919
1854
  };
1920
- if (this._deflating) {
1855
+ if (isBlob(data)) {
1856
+ if (this._state !== DEFAULT) {
1857
+ this.enqueue([this.getBlobData, data, false, options, cb]);
1858
+ } else {
1859
+ this.getBlobData(data, false, options, cb);
1860
+ }
1861
+ } else if (this._state !== DEFAULT) {
1921
1862
  this.enqueue([this.dispatch, data, false, options, cb]);
1922
1863
  } else {
1923
1864
  this.sendFrame(_Sender.frame(data, options), cb);
@@ -1948,6 +1889,9 @@ var require_sender = __commonJS({
1948
1889
  if (typeof data === "string") {
1949
1890
  byteLength = Buffer.byteLength(data);
1950
1891
  readOnly = false;
1892
+ } else if (isBlob(data)) {
1893
+ byteLength = data.size;
1894
+ readOnly = false;
1951
1895
  } else {
1952
1896
  data = toBuffer(data);
1953
1897
  byteLength = data.length;
@@ -1964,38 +1908,75 @@ var require_sender = __commonJS({
1964
1908
  opcode = 0;
1965
1909
  }
1966
1910
  if (options.fin) this._firstFragment = true;
1967
- if (perMessageDeflate) {
1968
- const opts = {
1969
- [kByteLength]: byteLength,
1970
- fin: options.fin,
1971
- generateMask: this._generateMask,
1972
- mask: options.mask,
1973
- maskBuffer: this._maskBuffer,
1974
- opcode,
1975
- readOnly,
1976
- rsv1
1977
- };
1978
- if (this._deflating) {
1979
- this.enqueue([this.dispatch, data, this._compress, opts, cb]);
1911
+ const opts = {
1912
+ [kByteLength]: byteLength,
1913
+ fin: options.fin,
1914
+ generateMask: this._generateMask,
1915
+ mask: options.mask,
1916
+ maskBuffer: this._maskBuffer,
1917
+ opcode,
1918
+ readOnly,
1919
+ rsv1
1920
+ };
1921
+ if (isBlob(data)) {
1922
+ if (this._state !== DEFAULT) {
1923
+ this.enqueue([this.getBlobData, data, this._compress, opts, cb]);
1980
1924
  } else {
1981
- this.dispatch(data, this._compress, opts, cb);
1925
+ this.getBlobData(data, this._compress, opts, cb);
1982
1926
  }
1927
+ } else if (this._state !== DEFAULT) {
1928
+ this.enqueue([this.dispatch, data, this._compress, opts, cb]);
1983
1929
  } else {
1984
- this.sendFrame(
1985
- _Sender.frame(data, {
1986
- [kByteLength]: byteLength,
1987
- fin: options.fin,
1988
- generateMask: this._generateMask,
1989
- mask: options.mask,
1990
- maskBuffer: this._maskBuffer,
1991
- opcode,
1992
- readOnly,
1993
- rsv1: false
1994
- }),
1995
- cb
1996
- );
1930
+ this.dispatch(data, this._compress, opts, cb);
1997
1931
  }
1998
1932
  }
1933
+ /**
1934
+ * Gets the contents of a blob as binary data.
1935
+ *
1936
+ * @param {Blob} blob The blob
1937
+ * @param {Boolean} [compress=false] Specifies whether or not to compress
1938
+ * the data
1939
+ * @param {Object} options Options object
1940
+ * @param {Boolean} [options.fin=false] Specifies whether or not to set the
1941
+ * FIN bit
1942
+ * @param {Function} [options.generateMask] The function used to generate the
1943
+ * masking key
1944
+ * @param {Boolean} [options.mask=false] Specifies whether or not to mask
1945
+ * `data`
1946
+ * @param {Buffer} [options.maskBuffer] The buffer used to store the masking
1947
+ * key
1948
+ * @param {Number} options.opcode The opcode
1949
+ * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
1950
+ * modified
1951
+ * @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
1952
+ * RSV1 bit
1953
+ * @param {Function} [cb] Callback
1954
+ * @private
1955
+ */
1956
+ getBlobData(blob, compress, options, cb) {
1957
+ this._bufferedBytes += options[kByteLength];
1958
+ this._state = GET_BLOB_DATA;
1959
+ blob.arrayBuffer().then((arrayBuffer) => {
1960
+ if (this._socket.destroyed) {
1961
+ const err = new Error(
1962
+ "The socket was closed while the blob was being read"
1963
+ );
1964
+ process.nextTick(callCallbacks, this, err, cb);
1965
+ return;
1966
+ }
1967
+ this._bufferedBytes -= options[kByteLength];
1968
+ const data = toBuffer(arrayBuffer);
1969
+ if (!compress) {
1970
+ this._state = DEFAULT;
1971
+ this.sendFrame(_Sender.frame(data, options), cb);
1972
+ this.dequeue();
1973
+ } else {
1974
+ this.dispatch(data, compress, options, cb);
1975
+ }
1976
+ }).catch((err) => {
1977
+ process.nextTick(onError, this, err, cb);
1978
+ });
1979
+ }
1999
1980
  /**
2000
1981
  * Dispatches a message.
2001
1982
  *
@@ -2026,22 +2007,17 @@ var require_sender = __commonJS({
2026
2007
  }
2027
2008
  const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
2028
2009
  this._bufferedBytes += options[kByteLength];
2029
- this._deflating = true;
2010
+ this._state = DEFLATING;
2030
2011
  perMessageDeflate.compress(data, options.fin, (_, buf) => {
2031
2012
  if (this._socket.destroyed) {
2032
2013
  const err = new Error(
2033
2014
  "The socket was closed while data was being compressed"
2034
2015
  );
2035
- if (typeof cb === "function") cb(err);
2036
- for (let i = 0; i < this._queue.length; i++) {
2037
- const params = this._queue[i];
2038
- const callback = params[params.length - 1];
2039
- if (typeof callback === "function") callback(err);
2040
- }
2016
+ callCallbacks(this, err, cb);
2041
2017
  return;
2042
2018
  }
2043
2019
  this._bufferedBytes -= options[kByteLength];
2044
- this._deflating = false;
2020
+ this._state = DEFAULT;
2045
2021
  options.readOnly = false;
2046
2022
  this.sendFrame(_Sender.frame(buf, options), cb);
2047
2023
  this.dequeue();
@@ -2053,7 +2029,7 @@ var require_sender = __commonJS({
2053
2029
  * @private
2054
2030
  */
2055
2031
  dequeue() {
2056
- while (!this._deflating && this._queue.length) {
2032
+ while (this._state === DEFAULT && this._queue.length) {
2057
2033
  const params = this._queue.shift();
2058
2034
  this._bufferedBytes -= params[3][kByteLength];
2059
2035
  Reflect.apply(params[0], this, params.slice(1));
@@ -2072,7 +2048,7 @@ var require_sender = __commonJS({
2072
2048
  /**
2073
2049
  * Sends a frame.
2074
2050
  *
2075
- * @param {Buffer[]} list The frame to send
2051
+ * @param {(Buffer | String)[]} list The frame to send
2076
2052
  * @param {Function} [cb] Callback
2077
2053
  * @private
2078
2054
  */
@@ -2088,6 +2064,18 @@ var require_sender = __commonJS({
2088
2064
  }
2089
2065
  };
2090
2066
  module.exports = Sender2;
2067
+ function callCallbacks(sender, err, cb) {
2068
+ if (typeof cb === "function") cb(err);
2069
+ for (let i = 0; i < sender._queue.length; i++) {
2070
+ const params = sender._queue[i];
2071
+ const callback = params[params.length - 1];
2072
+ if (typeof callback === "function") callback(err);
2073
+ }
2074
+ }
2075
+ function onError(sender, err, cb) {
2076
+ callCallbacks(sender, err, cb);
2077
+ sender.onerror(err);
2078
+ }
2091
2079
  }
2092
2080
  });
2093
2081
 
@@ -2488,6 +2476,7 @@ var require_websocket = __commonJS({
2488
2476
  var PerMessageDeflate = require_permessage_deflate();
2489
2477
  var Receiver2 = require_receiver();
2490
2478
  var Sender2 = require_sender();
2479
+ var { isBlob } = require_validation();
2491
2480
  var {
2492
2481
  BINARY_TYPES,
2493
2482
  EMPTY_BUFFER,
@@ -2524,6 +2513,7 @@ var require_websocket = __commonJS({
2524
2513
  this._closeFrameSent = false;
2525
2514
  this._closeMessage = EMPTY_BUFFER;
2526
2515
  this._closeTimer = null;
2516
+ this._errorEmitted = false;
2527
2517
  this._extensions = {};
2528
2518
  this._paused = false;
2529
2519
  this._protocol = "";
@@ -2552,9 +2542,8 @@ var require_websocket = __commonJS({
2552
2542
  }
2553
2543
  }
2554
2544
  /**
2555
- * This deviates from the WHATWG interface since ws doesn't support the
2556
- * required default "blob" type (instead we define a custom "nodebuffer"
2557
- * type).
2545
+ * For historical reasons, the custom "nodebuffer" type is used by the default
2546
+ * instead of "blob".
2558
2547
  *
2559
2548
  * @type {String}
2560
2549
  */
@@ -2656,10 +2645,12 @@ var require_websocket = __commonJS({
2656
2645
  maxPayload: options.maxPayload,
2657
2646
  skipUTF8Validation: options.skipUTF8Validation
2658
2647
  });
2659
- this._sender = new Sender2(socket, this._extensions, options.generateMask);
2648
+ const sender = new Sender2(socket, this._extensions, options.generateMask);
2660
2649
  this._receiver = receiver;
2650
+ this._sender = sender;
2661
2651
  this._socket = socket;
2662
2652
  receiver[kWebSocket] = this;
2653
+ sender[kWebSocket] = this;
2663
2654
  socket[kWebSocket] = this;
2664
2655
  receiver.on("conclude", receiverOnConclude);
2665
2656
  receiver.on("drain", receiverOnDrain);
@@ -2667,6 +2658,7 @@ var require_websocket = __commonJS({
2667
2658
  receiver.on("message", receiverOnMessage);
2668
2659
  receiver.on("ping", receiverOnPing);
2669
2660
  receiver.on("pong", receiverOnPong);
2661
+ sender.onerror = senderOnError;
2670
2662
  if (socket.setTimeout) socket.setTimeout(0);
2671
2663
  if (socket.setNoDelay) socket.setNoDelay();
2672
2664
  if (head.length > 0) socket.unshift(head);
@@ -2736,10 +2728,7 @@ var require_websocket = __commonJS({
2736
2728
  this._socket.end();
2737
2729
  }
2738
2730
  });
2739
- this._closeTimer = setTimeout(
2740
- this._socket.destroy.bind(this._socket),
2741
- closeTimeout
2742
- );
2731
+ setCloseTimer(this);
2743
2732
  }
2744
2733
  /**
2745
2734
  * Pause the socket.
@@ -2992,7 +2981,7 @@ var require_websocket = __commonJS({
2992
2981
  const isIpcUrl = parsedUrl.protocol === "ws+unix:";
2993
2982
  let invalidUrlMessage;
2994
2983
  if (parsedUrl.protocol !== "ws:" && !isSecure && !isIpcUrl) {
2995
- invalidUrlMessage = `The URL's protocol must be one of "ws:", "wss:", "http:", "https", or "ws+unix:"`;
2984
+ invalidUrlMessage = `The URL's protocol must be one of "ws:", "wss:", "http:", "https:", or "ws+unix:"`;
2996
2985
  } else if (isIpcUrl && !parsedUrl.pathname) {
2997
2986
  invalidUrlMessage = "The URL's pathname is empty";
2998
2987
  } else if (parsedUrl.hash) {
@@ -3204,6 +3193,7 @@ var require_websocket = __commonJS({
3204
3193
  }
3205
3194
  function emitErrorAndClose(websocket, err) {
3206
3195
  websocket._readyState = WebSocket4.CLOSING;
3196
+ websocket._errorEmitted = true;
3207
3197
  websocket.emit("error", err);
3208
3198
  websocket.emitClose();
3209
3199
  }
@@ -3237,7 +3227,7 @@ var require_websocket = __commonJS({
3237
3227
  }
3238
3228
  function sendAfterClose(websocket, data, cb) {
3239
3229
  if (data) {
3240
- const length = toBuffer(data).length;
3230
+ const length = isBlob(data) ? data.size : toBuffer(data).length;
3241
3231
  if (websocket._socket) websocket._sender._bufferedBytes += length;
3242
3232
  else websocket._bufferedAmount += length;
3243
3233
  }
@@ -3270,7 +3260,10 @@ var require_websocket = __commonJS({
3270
3260
  process.nextTick(resume, websocket._socket);
3271
3261
  websocket.close(err[kStatusCode]);
3272
3262
  }
3273
- websocket.emit("error", err);
3263
+ if (!websocket._errorEmitted) {
3264
+ websocket._errorEmitted = true;
3265
+ websocket.emit("error", err);
3266
+ }
3274
3267
  }
3275
3268
  function receiverOnFinish() {
3276
3269
  this[kWebSocket].emitClose();
@@ -3289,6 +3282,25 @@ var require_websocket = __commonJS({
3289
3282
  function resume(stream) {
3290
3283
  stream.resume();
3291
3284
  }
3285
+ function senderOnError(err) {
3286
+ const websocket = this[kWebSocket];
3287
+ if (websocket.readyState === WebSocket4.CLOSED) return;
3288
+ if (websocket.readyState === WebSocket4.OPEN) {
3289
+ websocket._readyState = WebSocket4.CLOSING;
3290
+ setCloseTimer(websocket);
3291
+ }
3292
+ this._socket.end();
3293
+ if (!websocket._errorEmitted) {
3294
+ websocket._errorEmitted = true;
3295
+ websocket.emit("error", err);
3296
+ }
3297
+ }
3298
+ function setCloseTimer(websocket) {
3299
+ websocket._closeTimer = setTimeout(
3300
+ websocket._socket.destroy.bind(websocket._socket),
3301
+ closeTimeout
3302
+ );
3303
+ }
3292
3304
  function socketOnClose() {
3293
3305
  const websocket = this[kWebSocket];
3294
3306
  this.removeListener("close", socketOnClose);
@@ -3332,6 +3344,104 @@ var require_websocket = __commonJS({
3332
3344
  }
3333
3345
  });
3334
3346
 
3347
+ // ../../node_modules/ws/lib/stream.js
3348
+ var require_stream = __commonJS({
3349
+ "../../node_modules/ws/lib/stream.js"(exports, module) {
3350
+ "use strict";
3351
+ var WebSocket4 = require_websocket();
3352
+ var { Duplex } = __require("stream");
3353
+ function emitClose(stream) {
3354
+ stream.emit("close");
3355
+ }
3356
+ function duplexOnEnd() {
3357
+ if (!this.destroyed && this._writableState.finished) {
3358
+ this.destroy();
3359
+ }
3360
+ }
3361
+ function duplexOnError(err) {
3362
+ this.removeListener("error", duplexOnError);
3363
+ this.destroy();
3364
+ if (this.listenerCount("error") === 0) {
3365
+ this.emit("error", err);
3366
+ }
3367
+ }
3368
+ function createWebSocketStream2(ws, options) {
3369
+ let terminateOnDestroy = true;
3370
+ const duplex = new Duplex({
3371
+ ...options,
3372
+ autoDestroy: false,
3373
+ emitClose: false,
3374
+ objectMode: false,
3375
+ writableObjectMode: false
3376
+ });
3377
+ ws.on("message", function message(msg, isBinary) {
3378
+ const data = !isBinary && duplex._readableState.objectMode ? msg.toString() : msg;
3379
+ if (!duplex.push(data)) ws.pause();
3380
+ });
3381
+ ws.once("error", function error(err) {
3382
+ if (duplex.destroyed) return;
3383
+ terminateOnDestroy = false;
3384
+ duplex.destroy(err);
3385
+ });
3386
+ ws.once("close", function close() {
3387
+ if (duplex.destroyed) return;
3388
+ duplex.push(null);
3389
+ });
3390
+ duplex._destroy = function(err, callback) {
3391
+ if (ws.readyState === ws.CLOSED) {
3392
+ callback(err);
3393
+ process.nextTick(emitClose, duplex);
3394
+ return;
3395
+ }
3396
+ let called = false;
3397
+ ws.once("error", function error(err2) {
3398
+ called = true;
3399
+ callback(err2);
3400
+ });
3401
+ ws.once("close", function close() {
3402
+ if (!called) callback(err);
3403
+ process.nextTick(emitClose, duplex);
3404
+ });
3405
+ if (terminateOnDestroy) ws.terminate();
3406
+ };
3407
+ duplex._final = function(callback) {
3408
+ if (ws.readyState === ws.CONNECTING) {
3409
+ ws.once("open", function open() {
3410
+ duplex._final(callback);
3411
+ });
3412
+ return;
3413
+ }
3414
+ if (ws._socket === null) return;
3415
+ if (ws._socket._writableState.finished) {
3416
+ callback();
3417
+ if (duplex._readableState.endEmitted) duplex.destroy();
3418
+ } else {
3419
+ ws._socket.once("finish", function finish() {
3420
+ callback();
3421
+ });
3422
+ ws.close();
3423
+ }
3424
+ };
3425
+ duplex._read = function() {
3426
+ if (ws.isPaused) ws.resume();
3427
+ };
3428
+ duplex._write = function(chunk, encoding, callback) {
3429
+ if (ws.readyState === ws.CONNECTING) {
3430
+ ws.once("open", function open() {
3431
+ duplex._write(chunk, encoding, callback);
3432
+ });
3433
+ return;
3434
+ }
3435
+ ws.send(chunk, callback);
3436
+ };
3437
+ duplex.on("end", duplexOnEnd);
3438
+ duplex.on("error", duplexOnError);
3439
+ return duplex;
3440
+ }
3441
+ module.exports = createWebSocketStream2;
3442
+ }
3443
+ });
3444
+
3335
3445
  // ../../node_modules/ws/lib/subprotocol.js
3336
3446
  var require_subprotocol = __commonJS({
3337
3447
  "../../node_modules/ws/lib/subprotocol.js"(exports, module) {
@@ -3591,9 +3701,11 @@ var require_websocket_server = __commonJS({
3591
3701
  abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
3592
3702
  return;
3593
3703
  }
3594
- if (version !== 8 && version !== 13) {
3704
+ if (version !== 13 && version !== 8) {
3595
3705
  const message = "Missing or invalid Sec-WebSocket-Version header";
3596
- abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
3706
+ abortHandshakeOrEmitwsClientError(this, req, socket, 400, message, {
3707
+ "Sec-WebSocket-Version": "13, 8"
3708
+ });
3597
3709
  return;
3598
3710
  }
3599
3711
  if (!this.shouldHandle(req)) {
@@ -3752,13 +3864,13 @@ var require_websocket_server = __commonJS({
3752
3864
  ` + Object.keys(headers).map((h) => `${h}: ${headers[h]}`).join("\r\n") + "\r\n\r\n" + message
3753
3865
  );
3754
3866
  }
3755
- function abortHandshakeOrEmitwsClientError(server, req, socket, code, message) {
3867
+ function abortHandshakeOrEmitwsClientError(server, req, socket, code, message, headers) {
3756
3868
  if (server.listenerCount("wsClientError")) {
3757
3869
  const err = new Error(message);
3758
3870
  Error.captureStackTrace(err, abortHandshakeOrEmitwsClientError);
3759
3871
  server.emit("wsClientError", err, socket, req);
3760
3872
  } else {
3761
- abortHandshake(socket, code, message);
3873
+ abortHandshake(socket, code, message, headers);
3762
3874
  }
3763
3875
  }
3764
3876
  }
@@ -3807,4 +3919,4 @@ var WebSocket3 = (() => {
3807
3919
  export {
3808
3920
  WebSocket3 as WebSocket
3809
3921
  };
3810
- //# sourceMappingURL=_esm-3NTT6V2X.js.map
3922
+ //# sourceMappingURL=_esm-6RCT7X34.js.map