@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.
- package/lib/esm/constants.js +12 -11
- package/lib/esm/index.js +6 -8
- package/lib/esm/stream.js +82 -80
- package/lib/esm/websocket-server.js +393 -399
- package/lib/esm/websocket.js +273 -246
- package/package.json +11 -11
package/lib/esm/constants.js
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
5
|
+
stream.emit("close");
|
|
4
6
|
}
|
|
5
7
|
function duplexOnEnd() {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
if (!this.destroyed && this._writableState.finished) {
|
|
9
|
+
this.destroy();
|
|
10
|
+
}
|
|
9
11
|
}
|
|
10
12
|
function duplexOnError(err) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
};
|
|
89
|
+
|
|
90
|
+
//#endregion
|
|
91
|
+
export { createWebSocketStream };
|