@gjsify/ws 0.3.13 → 0.3.15

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.
@@ -1,16 +1,17 @@
1
- const BINARY_TYPES = ["nodebuffer", "arraybuffer", "fragments"];
1
+ //#region src/constants.ts
2
+ const BINARY_TYPES = [
3
+ "nodebuffer",
4
+ "arraybuffer",
5
+ "fragments"
6
+ ];
2
7
  const EMPTY_BUFFER = new Uint8Array(0);
3
8
  const CONNECTING = 0;
4
9
  const OPEN = 1;
5
10
  const CLOSING = 2;
6
11
  const CLOSED = 3;
7
- const kWebSocket = /* @__PURE__ */ Symbol("ws:kWebSocket");
8
- export {
9
- BINARY_TYPES,
10
- CLOSED,
11
- CLOSING,
12
- CONNECTING,
13
- EMPTY_BUFFER,
14
- OPEN,
15
- kWebSocket
16
- };
12
+ /** Internal marker for native `globalThis.WebSocket` instances handed in via
13
+ * `{ socket }` option (not currently supported but reserved). */
14
+ const kWebSocket = Symbol("ws:kWebSocket");
15
+
16
+ //#endregion
17
+ export { BINARY_TYPES, CLOSED, CLOSING, CONNECTING, EMPTY_BUFFER, OPEN, kWebSocket };
package/lib/esm/index.js CHANGED
@@ -1,15 +1,13 @@
1
1
  import { WebSocket } from "./websocket.js";
2
2
  import { WebSocketServer } from "./websocket-server.js";
3
3
  import { createWebSocketStream } from "./stream.js";
4
+
5
+ //#region src/index.ts
4
6
  WebSocket.WebSocket = WebSocket;
5
7
  WebSocket.WebSocketServer = WebSocketServer;
6
8
  WebSocket.Server = WebSocketServer;
7
9
  WebSocket.createWebSocketStream = createWebSocketStream;
8
- var index_default = WebSocket;
9
- export {
10
- WebSocketServer as Server,
11
- WebSocket,
12
- WebSocketServer,
13
- createWebSocketStream,
14
- index_default as default
15
- };
10
+ var src_default = WebSocket;
11
+
12
+ //#endregion
13
+ export { WebSocketServer as Server, WebSocketServer, WebSocket, createWebSocketStream, src_default as default };
package/lib/esm/stream.js CHANGED
@@ -1,89 +1,91 @@
1
1
  import { Duplex } from "node:stream";
2
+
3
+ //#region src/stream.ts
2
4
  function emitClose(stream) {
3
- stream.emit("close");
5
+ stream.emit("close");
4
6
  }
5
7
  function duplexOnEnd() {
6
- if (!this.destroyed && this._writableState.finished) {
7
- this.destroy();
8
- }
8
+ if (!this.destroyed && this._writableState.finished) {
9
+ this.destroy();
10
+ }
9
11
  }
10
12
  function duplexOnError(err) {
11
- this.removeListener("error", duplexOnError);
12
- this.destroy();
13
- if (this.listenerCount("error") === 0) {
14
- this.emit("error", err);
15
- }
13
+ this.removeListener("error", duplexOnError);
14
+ this.destroy();
15
+ if (this.listenerCount("error") === 0) {
16
+ this.emit("error", err);
17
+ }
16
18
  }
17
19
  function createWebSocketStream(ws, options = {}) {
18
- let terminateOnDestroy = true;
19
- const duplex = new Duplex({
20
- ...options,
21
- autoDestroy: false,
22
- emitClose: false,
23
- objectMode: false,
24
- writableObjectMode: false
25
- });
26
- ws.on("message", (msg, isBinary) => {
27
- let data;
28
- if (isBinary || duplex.readableObjectMode) {
29
- data = msg;
30
- } else {
31
- data = typeof msg === "string" ? Buffer.from(msg) : msg;
32
- }
33
- if (!duplex.push(data) && typeof ws.pause === "function") ws.pause();
34
- });
35
- ws.once("error", (err) => {
36
- if (duplex.destroyed) return;
37
- terminateOnDestroy = false;
38
- duplex.destroy(err);
39
- });
40
- ws.once("close", () => {
41
- if (duplex.destroyed) return;
42
- duplex.push(null);
43
- });
44
- duplex._destroy = function(err, callback) {
45
- if (ws.readyState === ws.CLOSED) {
46
- callback(err);
47
- process.nextTick(emitClose, duplex);
48
- return;
49
- }
50
- let called = false;
51
- ws.once("error", (e) => {
52
- called = true;
53
- callback(e);
54
- });
55
- ws.once("close", () => {
56
- if (!called) callback(err);
57
- process.nextTick(emitClose, duplex);
58
- });
59
- if (terminateOnDestroy) ws.terminate();
60
- };
61
- duplex._final = function(callback) {
62
- if (ws.readyState === ws.CONNECTING) {
63
- ws.once("open", () => duplex._final(callback));
64
- return;
65
- }
66
- if (ws.readyState === ws.CLOSED || ws.readyState === ws.CLOSING) {
67
- callback();
68
- return;
69
- }
70
- ws.once("close", callback);
71
- ws.close();
72
- };
73
- duplex._read = function() {
74
- if (typeof ws.resume === "function") ws.resume();
75
- };
76
- duplex._write = function(chunk, _encoding, callback) {
77
- if (ws.readyState === ws.CONNECTING) {
78
- ws.once("open", () => duplex._write(chunk, _encoding, callback));
79
- return;
80
- }
81
- ws.send(chunk, callback);
82
- };
83
- duplex.on("end", duplexOnEnd);
84
- duplex.on("error", duplexOnError);
85
- return duplex;
20
+ let terminateOnDestroy = true;
21
+ const duplex = new Duplex({
22
+ ...options,
23
+ autoDestroy: false,
24
+ emitClose: false,
25
+ objectMode: false,
26
+ writableObjectMode: false
27
+ });
28
+ ws.on("message", (msg, isBinary) => {
29
+ let data;
30
+ if (isBinary || duplex.readableObjectMode) {
31
+ data = msg;
32
+ } else {
33
+ data = typeof msg === "string" ? Buffer.from(msg) : msg;
34
+ }
35
+ if (!duplex.push(data) && typeof ws.pause === "function") ws.pause();
36
+ });
37
+ ws.once("error", (err) => {
38
+ if (duplex.destroyed) return;
39
+ terminateOnDestroy = false;
40
+ duplex.destroy(err);
41
+ });
42
+ ws.once("close", () => {
43
+ if (duplex.destroyed) return;
44
+ duplex.push(null);
45
+ });
46
+ duplex._destroy = function(err, callback) {
47
+ if (ws.readyState === ws.CLOSED) {
48
+ callback(err);
49
+ process.nextTick(emitClose, duplex);
50
+ return;
51
+ }
52
+ let called = false;
53
+ ws.once("error", (e) => {
54
+ called = true;
55
+ callback(e);
56
+ });
57
+ ws.once("close", () => {
58
+ if (!called) callback(err);
59
+ process.nextTick(emitClose, duplex);
60
+ });
61
+ if (terminateOnDestroy) ws.terminate();
62
+ };
63
+ duplex._final = function(callback) {
64
+ if (ws.readyState === ws.CONNECTING) {
65
+ ws.once("open", () => duplex._final(callback));
66
+ return;
67
+ }
68
+ if (ws.readyState === ws.CLOSED || ws.readyState === ws.CLOSING) {
69
+ callback();
70
+ return;
71
+ }
72
+ ws.once("close", callback);
73
+ ws.close();
74
+ };
75
+ duplex._read = function() {
76
+ if (typeof ws.resume === "function") ws.resume();
77
+ };
78
+ duplex._write = function(chunk, _encoding, callback) {
79
+ if (ws.readyState === ws.CONNECTING) {
80
+ ws.once("open", () => duplex._write(chunk, _encoding, callback));
81
+ return;
82
+ }
83
+ ws.send(chunk, callback);
84
+ };
85
+ duplex.on("end", duplexOnEnd);
86
+ duplex.on("error", duplexOnError);
87
+ return duplex;
86
88
  }
87
- export {
88
- createWebSocketStream
89
- };
89
+
90
+ //#endregion
91
+ export { createWebSocketStream };