crossws 0.3.2 → 0.3.4
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/dist/adapters/bun.d.mts +4 -4
- package/dist/adapters/bun.d.ts +4 -4
- package/dist/adapters/bun.mjs +43 -43
- package/dist/adapters/cloudflare-durable.d.mts +29 -8
- package/dist/adapters/cloudflare-durable.d.ts +29 -8
- package/dist/adapters/cloudflare-durable.mjs +18 -11
- package/dist/adapters/cloudflare.d.mts +2 -2
- package/dist/adapters/cloudflare.d.ts +2 -2
- package/dist/adapters/cloudflare.mjs +52 -54
- package/dist/adapters/deno.d.mts +2 -2
- package/dist/adapters/deno.d.ts +2 -2
- package/dist/adapters/deno.mjs +42 -44
- package/dist/adapters/node.d.mts +9 -11
- package/dist/adapters/node.d.ts +9 -11
- package/dist/adapters/node.mjs +59 -61
- package/dist/adapters/sse.d.mts +2 -2
- package/dist/adapters/sse.d.ts +2 -2
- package/dist/adapters/sse.mjs +5 -5
- package/dist/adapters/uws.d.mts +3 -2
- package/dist/adapters/uws.d.ts +3 -2
- package/dist/adapters/uws.mjs +84 -81
- package/dist/index.d.mts +10 -2
- package/dist/index.d.ts +10 -2
- package/dist/index.mjs +1 -1
- package/dist/shared/{crossws.DLRVRjZs.mjs → crossws.BS81iGZK.mjs} +9 -2
- package/dist/shared/{crossws.BcEs6ZHK.mjs → crossws.D9ehKjSh.mjs} +25 -8
- package/dist/websocket/native.d.mts +2 -9
- package/dist/websocket/native.d.ts +2 -9
- package/dist/websocket/native.mjs +2 -2
- package/dist/websocket/node.d.mts +2 -9
- package/dist/websocket/node.d.ts +2 -9
- package/dist/websocket/node.mjs +2 -2
- package/package.json +15 -15
package/dist/adapters/node.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { M as Message, P as Peer, t as toBufferLike } from '../shared/crossws.
|
|
2
|
-
import {
|
|
1
|
+
import { M as Message, P as Peer, t as toBufferLike } from '../shared/crossws.BS81iGZK.mjs';
|
|
2
|
+
import { a as adapterUtils, A as AdapterHookable } from '../shared/crossws.D9ehKjSh.mjs';
|
|
3
3
|
import { W as WSError } from '../shared/crossws.By9qWDAI.mjs';
|
|
4
4
|
import 'stream';
|
|
5
5
|
import { _ as _WebSocketServer } from '../shared/crossws.DelSCW9g.mjs';
|
|
@@ -14,68 +14,66 @@ import 'net';
|
|
|
14
14
|
import 'tls';
|
|
15
15
|
import 'url';
|
|
16
16
|
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
const nodeAdapter = (options = {}) => {
|
|
18
|
+
const hooks = new AdapterHookable(options);
|
|
19
|
+
const peers = /* @__PURE__ */ new Set();
|
|
20
|
+
const wss = options.wss || new _WebSocketServer({
|
|
21
|
+
noServer: true,
|
|
22
|
+
...options.serverOptions
|
|
23
|
+
});
|
|
24
|
+
wss.on("connection", (ws, nodeReq) => {
|
|
25
|
+
const request = new NodeReqProxy(nodeReq);
|
|
26
|
+
const peer = new NodePeer({ ws, request, peers, nodeReq });
|
|
27
|
+
peers.add(peer);
|
|
28
|
+
hooks.callHook("open", peer);
|
|
29
|
+
ws.on("message", (data) => {
|
|
30
|
+
if (Array.isArray(data)) {
|
|
31
|
+
data = Buffer.concat(data);
|
|
32
|
+
}
|
|
33
|
+
hooks.callHook("message", peer, new Message(data, peer));
|
|
24
34
|
});
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
hooks.callHook("message", peer, new Message(data, peer));
|
|
35
|
-
});
|
|
36
|
-
ws.on("error", (error) => {
|
|
37
|
-
peers.delete(peer);
|
|
38
|
-
hooks.callHook("error", peer, new WSError(error));
|
|
39
|
-
});
|
|
40
|
-
ws.on("close", (code, reason) => {
|
|
41
|
-
peers.delete(peer);
|
|
42
|
-
hooks.callHook("close", peer, {
|
|
43
|
-
code,
|
|
44
|
-
reason: reason?.toString()
|
|
45
|
-
});
|
|
35
|
+
ws.on("error", (error) => {
|
|
36
|
+
peers.delete(peer);
|
|
37
|
+
hooks.callHook("error", peer, new WSError(error));
|
|
38
|
+
});
|
|
39
|
+
ws.on("close", (code, reason) => {
|
|
40
|
+
peers.delete(peer);
|
|
41
|
+
hooks.callHook("close", peer, {
|
|
42
|
+
code,
|
|
43
|
+
reason: reason?.toString()
|
|
46
44
|
});
|
|
47
45
|
});
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
46
|
+
});
|
|
47
|
+
wss.on("headers", (outgoingHeaders, req) => {
|
|
48
|
+
const upgradeHeaders = req._upgradeHeaders;
|
|
49
|
+
if (upgradeHeaders) {
|
|
50
|
+
for (const [key, value] of new Headers(upgradeHeaders)) {
|
|
51
|
+
outgoingHeaders.push(`${key}: ${value}`);
|
|
54
52
|
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
nodeReq._request = request;
|
|
65
|
-
nodeReq._upgradeHeaders = upgradeHeaders;
|
|
66
|
-
nodeReq._context = context;
|
|
67
|
-
wss.handleUpgrade(nodeReq, socket, head, (ws) => {
|
|
68
|
-
wss.emit("connection", ws, nodeReq);
|
|
69
|
-
});
|
|
70
|
-
},
|
|
71
|
-
closeAll: (code, data) => {
|
|
72
|
-
for (const client of wss.clients) {
|
|
73
|
-
client.close(code, data);
|
|
74
|
-
}
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
return {
|
|
56
|
+
...adapterUtils(peers),
|
|
57
|
+
handleUpgrade: async (nodeReq, socket, head) => {
|
|
58
|
+
const request = new NodeReqProxy(nodeReq);
|
|
59
|
+
const { upgradeHeaders, endResponse, context } = await hooks.upgrade(request);
|
|
60
|
+
if (endResponse) {
|
|
61
|
+
return sendResponse(socket, endResponse);
|
|
75
62
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
63
|
+
nodeReq._request = request;
|
|
64
|
+
nodeReq._upgradeHeaders = upgradeHeaders;
|
|
65
|
+
nodeReq._context = context;
|
|
66
|
+
wss.handleUpgrade(nodeReq, socket, head, (ws) => {
|
|
67
|
+
wss.emit("connection", ws, nodeReq);
|
|
68
|
+
});
|
|
69
|
+
},
|
|
70
|
+
closeAll: (code, data) => {
|
|
71
|
+
for (const client of wss.clients) {
|
|
72
|
+
client.close(code, data);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
};
|
|
79
77
|
class NodePeer extends Peer {
|
|
80
78
|
get remoteAddress() {
|
|
81
79
|
return this._internal.nodeReq.socket?.remoteAddress;
|
|
@@ -85,7 +83,7 @@ class NodePeer extends Peer {
|
|
|
85
83
|
}
|
|
86
84
|
send(data, options) {
|
|
87
85
|
const dataBuff = toBufferLike(data);
|
|
88
|
-
const isBinary = typeof
|
|
86
|
+
const isBinary = typeof dataBuff !== "string";
|
|
89
87
|
this._internal.ws.send(dataBuff, {
|
|
90
88
|
compress: options?.compress,
|
|
91
89
|
binary: isBinary,
|
|
@@ -155,4 +153,4 @@ async function sendResponse(socket, res) {
|
|
|
155
153
|
});
|
|
156
154
|
}
|
|
157
155
|
|
|
158
|
-
export {
|
|
156
|
+
export { nodeAdapter as default };
|
package/dist/adapters/sse.d.mts
CHANGED
|
@@ -7,6 +7,6 @@ interface SSEAdapter extends AdapterInstance {
|
|
|
7
7
|
interface SSEOptions extends AdapterOptions {
|
|
8
8
|
bidir?: boolean;
|
|
9
9
|
}
|
|
10
|
-
declare const
|
|
10
|
+
declare const sseAdapter: Adapter<SSEAdapter, SSEOptions>;
|
|
11
11
|
|
|
12
|
-
export { type SSEAdapter, type SSEOptions,
|
|
12
|
+
export { type SSEAdapter, type SSEOptions, sseAdapter as default };
|
package/dist/adapters/sse.d.ts
CHANGED
|
@@ -7,6 +7,6 @@ interface SSEAdapter extends AdapterInstance {
|
|
|
7
7
|
interface SSEOptions extends AdapterOptions {
|
|
8
8
|
bidir?: boolean;
|
|
9
9
|
}
|
|
10
|
-
declare const
|
|
10
|
+
declare const sseAdapter: Adapter<SSEAdapter, SSEOptions>;
|
|
11
11
|
|
|
12
|
-
export { type SSEAdapter, type SSEOptions,
|
|
12
|
+
export { type SSEAdapter, type SSEOptions, sseAdapter as default };
|
package/dist/adapters/sse.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { M as Message, P as Peer, a as toString } from '../shared/crossws.
|
|
2
|
-
import {
|
|
1
|
+
import { M as Message, P as Peer, a as toString } from '../shared/crossws.BS81iGZK.mjs';
|
|
2
|
+
import { a as adapterUtils, A as AdapterHookable } from '../shared/crossws.D9ehKjSh.mjs';
|
|
3
3
|
import 'uncrypto';
|
|
4
4
|
|
|
5
|
-
const
|
|
5
|
+
const sseAdapter = (opts = {}) => {
|
|
6
6
|
const hooks = new AdapterHookable(opts);
|
|
7
7
|
const peers = /* @__PURE__ */ new Set();
|
|
8
8
|
const peersMap = opts.bidir ? /* @__PURE__ */ new Map() : undefined;
|
|
@@ -63,7 +63,7 @@ const sse = defineWebSocketAdapter((opts = {}) => {
|
|
|
63
63
|
return new Response(peer._sseStream, { headers });
|
|
64
64
|
}
|
|
65
65
|
};
|
|
66
|
-
}
|
|
66
|
+
};
|
|
67
67
|
class SSEPeer extends Peer {
|
|
68
68
|
_sseStream;
|
|
69
69
|
// server -> client
|
|
@@ -118,4 +118,4 @@ class SSEWebSocketStub {
|
|
|
118
118
|
readyState;
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
export {
|
|
121
|
+
export { sseAdapter as default };
|
package/dist/adapters/uws.d.mts
CHANGED
|
@@ -17,7 +17,7 @@ interface UWSAdapter extends AdapterInstance {
|
|
|
17
17
|
interface UWSOptions extends AdapterOptions {
|
|
18
18
|
uws?: Exclude<uws.WebSocketBehavior<any>, "close" | "drain" | "message" | "open" | "ping" | "pong" | "subscription" | "upgrade">;
|
|
19
19
|
}
|
|
20
|
-
declare const
|
|
20
|
+
declare const uwsAdapter: Adapter<UWSAdapter, UWSOptions>;
|
|
21
21
|
|
|
22
22
|
declare class UWSPeer extends Peer<{
|
|
23
23
|
peers: Set<UWSPeer>;
|
|
@@ -32,6 +32,7 @@ declare class UWSPeer extends Peer<{
|
|
|
32
32
|
compress?: boolean;
|
|
33
33
|
}): number;
|
|
34
34
|
subscribe(topic: string): void;
|
|
35
|
+
unsubscribe(topic: string): void;
|
|
35
36
|
publish(topic: string, message: string, options?: {
|
|
36
37
|
compress?: boolean;
|
|
37
38
|
}): number;
|
|
@@ -54,4 +55,4 @@ declare class UwsWebSocketProxy implements Partial<WebSocket> {
|
|
|
54
55
|
get extensions(): string;
|
|
55
56
|
}
|
|
56
57
|
|
|
57
|
-
export { type UWSAdapter, type UWSOptions,
|
|
58
|
+
export { type UWSAdapter, type UWSOptions, uwsAdapter as default };
|
package/dist/adapters/uws.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ interface UWSAdapter extends AdapterInstance {
|
|
|
17
17
|
interface UWSOptions extends AdapterOptions {
|
|
18
18
|
uws?: Exclude<uws.WebSocketBehavior<any>, "close" | "drain" | "message" | "open" | "ping" | "pong" | "subscription" | "upgrade">;
|
|
19
19
|
}
|
|
20
|
-
declare const
|
|
20
|
+
declare const uwsAdapter: Adapter<UWSAdapter, UWSOptions>;
|
|
21
21
|
|
|
22
22
|
declare class UWSPeer extends Peer<{
|
|
23
23
|
peers: Set<UWSPeer>;
|
|
@@ -32,6 +32,7 @@ declare class UWSPeer extends Peer<{
|
|
|
32
32
|
compress?: boolean;
|
|
33
33
|
}): number;
|
|
34
34
|
subscribe(topic: string): void;
|
|
35
|
+
unsubscribe(topic: string): void;
|
|
35
36
|
publish(topic: string, message: string, options?: {
|
|
36
37
|
compress?: boolean;
|
|
37
38
|
}): number;
|
|
@@ -54,4 +55,4 @@ declare class UwsWebSocketProxy implements Partial<WebSocket> {
|
|
|
54
55
|
get extensions(): string;
|
|
55
56
|
}
|
|
56
57
|
|
|
57
|
-
export { type UWSAdapter, type UWSOptions,
|
|
58
|
+
export { type UWSAdapter, type UWSOptions, uwsAdapter as default };
|
package/dist/adapters/uws.mjs
CHANGED
|
@@ -1,91 +1,89 @@
|
|
|
1
|
-
import { M as Message, P as Peer, t as toBufferLike } from '../shared/crossws.
|
|
2
|
-
import {
|
|
1
|
+
import { M as Message, P as Peer, t as toBufferLike } from '../shared/crossws.BS81iGZK.mjs';
|
|
2
|
+
import { a as adapterUtils, A as AdapterHookable } from '../shared/crossws.D9ehKjSh.mjs';
|
|
3
3
|
import 'uncrypto';
|
|
4
4
|
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
res.write(chunk);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
if (!aborted) {
|
|
52
|
-
res.end();
|
|
5
|
+
const uwsAdapter = (options = {}) => {
|
|
6
|
+
const hooks = new AdapterHookable(options);
|
|
7
|
+
const peers = /* @__PURE__ */ new Set();
|
|
8
|
+
return {
|
|
9
|
+
...adapterUtils(peers),
|
|
10
|
+
websocket: {
|
|
11
|
+
...options.uws,
|
|
12
|
+
close(ws, code, message) {
|
|
13
|
+
const peer = getPeer(ws, peers);
|
|
14
|
+
peer._internal.ws.readyState = 2;
|
|
15
|
+
peers.delete(peer);
|
|
16
|
+
hooks.callHook("close", peer, {
|
|
17
|
+
code,
|
|
18
|
+
reason: message?.toString()
|
|
19
|
+
});
|
|
20
|
+
peer._internal.ws.readyState = 3;
|
|
21
|
+
},
|
|
22
|
+
message(ws, message, isBinary) {
|
|
23
|
+
const peer = getPeer(ws, peers);
|
|
24
|
+
hooks.callHook("message", peer, new Message(message, peer));
|
|
25
|
+
},
|
|
26
|
+
open(ws) {
|
|
27
|
+
const peer = getPeer(ws, peers);
|
|
28
|
+
peers.add(peer);
|
|
29
|
+
hooks.callHook("open", peer);
|
|
30
|
+
},
|
|
31
|
+
async upgrade(res, req, uwsContext) {
|
|
32
|
+
let aborted = false;
|
|
33
|
+
res.onAborted(() => {
|
|
34
|
+
aborted = true;
|
|
35
|
+
});
|
|
36
|
+
const { upgradeHeaders, endResponse, context } = await hooks.upgrade(
|
|
37
|
+
new UWSReqProxy(req)
|
|
38
|
+
);
|
|
39
|
+
if (endResponse) {
|
|
40
|
+
res.writeStatus(`${endResponse.status} ${endResponse.statusText}`);
|
|
41
|
+
for (const [key, value] of endResponse.headers) {
|
|
42
|
+
res.writeHeader(key, value);
|
|
43
|
+
}
|
|
44
|
+
if (endResponse.body) {
|
|
45
|
+
for await (const chunk of endResponse.body) {
|
|
46
|
+
if (aborted) break;
|
|
47
|
+
res.write(chunk);
|
|
53
48
|
}
|
|
54
|
-
return;
|
|
55
49
|
}
|
|
56
|
-
if (aborted) {
|
|
57
|
-
|
|
50
|
+
if (!aborted) {
|
|
51
|
+
res.end();
|
|
58
52
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (aborted) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
res.writeStatus("101 Switching Protocols");
|
|
59
|
+
if (upgradeHeaders) {
|
|
60
|
+
const headers = upgradeHeaders instanceof Headers ? upgradeHeaders : new Headers(upgradeHeaders);
|
|
61
|
+
for (const [key, value] of headers) {
|
|
62
|
+
res.writeHeader(key, value);
|
|
65
63
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
extensions,
|
|
76
|
-
context
|
|
77
|
-
},
|
|
78
|
-
key,
|
|
64
|
+
}
|
|
65
|
+
res.cork(() => {
|
|
66
|
+
const key = req.getHeader("sec-websocket-key");
|
|
67
|
+
const protocol = req.getHeader("sec-websocket-protocol");
|
|
68
|
+
const extensions = req.getHeader("sec-websocket-extensions");
|
|
69
|
+
res.upgrade(
|
|
70
|
+
{
|
|
71
|
+
req,
|
|
72
|
+
res,
|
|
79
73
|
protocol,
|
|
80
74
|
extensions,
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
75
|
+
context
|
|
76
|
+
},
|
|
77
|
+
key,
|
|
78
|
+
protocol,
|
|
79
|
+
extensions,
|
|
80
|
+
uwsContext
|
|
81
|
+
);
|
|
82
|
+
});
|
|
85
83
|
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
};
|
|
89
87
|
function getPeer(uws, peers) {
|
|
90
88
|
const uwsData = uws.getUserData();
|
|
91
89
|
if (uwsData.peer) {
|
|
@@ -116,12 +114,17 @@ class UWSPeer extends Peer {
|
|
|
116
114
|
}
|
|
117
115
|
send(data, options) {
|
|
118
116
|
const dataBuff = toBufferLike(data);
|
|
119
|
-
const isBinary = typeof
|
|
117
|
+
const isBinary = typeof dataBuff !== "string";
|
|
120
118
|
return this._internal.uws.send(dataBuff, isBinary, options?.compress);
|
|
121
119
|
}
|
|
122
120
|
subscribe(topic) {
|
|
121
|
+
this._topics.add(topic);
|
|
123
122
|
this._internal.uws.subscribe(topic);
|
|
124
123
|
}
|
|
124
|
+
unsubscribe(topic) {
|
|
125
|
+
this._topics.delete(topic);
|
|
126
|
+
this._internal.uws.unsubscribe(topic);
|
|
127
|
+
}
|
|
125
128
|
publish(topic, message, options) {
|
|
126
129
|
const data = toBufferLike(message);
|
|
127
130
|
const isBinary = typeof data !== "string";
|
|
@@ -177,4 +180,4 @@ class UwsWebSocketProxy {
|
|
|
177
180
|
}
|
|
178
181
|
}
|
|
179
182
|
|
|
180
|
-
export {
|
|
183
|
+
export { uwsAdapter as default };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { W as WebSocket } from './shared/crossws.ChIJSJVK.mjs';
|
|
2
2
|
|
|
3
|
+
declare const kNodeInspect: unique symbol;
|
|
4
|
+
|
|
3
5
|
interface AdapterInternal {
|
|
4
6
|
ws: unknown;
|
|
5
7
|
request: UpgradeRequest;
|
|
@@ -32,6 +34,8 @@ declare abstract class Peer<Internal extends AdapterInternal = AdapterInternal>
|
|
|
32
34
|
get websocket(): Partial<WebSocket>;
|
|
33
35
|
/** All connected peers to the server */
|
|
34
36
|
get peers(): Set<Peer>;
|
|
37
|
+
/** All topics, this peer has been subscribed to. */
|
|
38
|
+
get topics(): Set<string>;
|
|
35
39
|
abstract close(code?: number, reason?: string): void;
|
|
36
40
|
/** Abruptly close the connection */
|
|
37
41
|
terminate(): void;
|
|
@@ -49,7 +53,8 @@ declare abstract class Peer<Internal extends AdapterInternal = AdapterInternal>
|
|
|
49
53
|
}): void;
|
|
50
54
|
toString(): string;
|
|
51
55
|
[Symbol.toPrimitive](): string;
|
|
52
|
-
[Symbol.toStringTag]():
|
|
56
|
+
[Symbol.toStringTag](): "WebSocket";
|
|
57
|
+
[kNodeInspect](): Record<string, unknown>;
|
|
53
58
|
}
|
|
54
59
|
|
|
55
60
|
interface AdapterInstance {
|
|
@@ -85,7 +90,7 @@ declare class Message implements Partial<MessageEvent> {
|
|
|
85
90
|
*
|
|
86
91
|
* If raw data is in any other format or string, it will be automatically converted and encoded.
|
|
87
92
|
*/
|
|
88
|
-
uint8Array(): Uint8Array
|
|
93
|
+
uint8Array(): Uint8Array;
|
|
89
94
|
/**
|
|
90
95
|
* Get data as [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) or [SharedArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer) value.
|
|
91
96
|
*
|
|
@@ -113,6 +118,9 @@ declare class Message implements Partial<MessageEvent> {
|
|
|
113
118
|
get data(): unknown;
|
|
114
119
|
toString(): string;
|
|
115
120
|
[Symbol.toPrimitive](): string;
|
|
121
|
+
[kNodeInspect](): {
|
|
122
|
+
data: unknown;
|
|
123
|
+
};
|
|
116
124
|
}
|
|
117
125
|
|
|
118
126
|
declare function defineHooks<T extends Partial<Hooks> = Partial<Hooks>>(hooks: T): T;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { W as WebSocket } from './shared/crossws.ChIJSJVK.js';
|
|
2
2
|
|
|
3
|
+
declare const kNodeInspect: unique symbol;
|
|
4
|
+
|
|
3
5
|
interface AdapterInternal {
|
|
4
6
|
ws: unknown;
|
|
5
7
|
request: UpgradeRequest;
|
|
@@ -32,6 +34,8 @@ declare abstract class Peer<Internal extends AdapterInternal = AdapterInternal>
|
|
|
32
34
|
get websocket(): Partial<WebSocket>;
|
|
33
35
|
/** All connected peers to the server */
|
|
34
36
|
get peers(): Set<Peer>;
|
|
37
|
+
/** All topics, this peer has been subscribed to. */
|
|
38
|
+
get topics(): Set<string>;
|
|
35
39
|
abstract close(code?: number, reason?: string): void;
|
|
36
40
|
/** Abruptly close the connection */
|
|
37
41
|
terminate(): void;
|
|
@@ -49,7 +53,8 @@ declare abstract class Peer<Internal extends AdapterInternal = AdapterInternal>
|
|
|
49
53
|
}): void;
|
|
50
54
|
toString(): string;
|
|
51
55
|
[Symbol.toPrimitive](): string;
|
|
52
|
-
[Symbol.toStringTag]():
|
|
56
|
+
[Symbol.toStringTag](): "WebSocket";
|
|
57
|
+
[kNodeInspect](): Record<string, unknown>;
|
|
53
58
|
}
|
|
54
59
|
|
|
55
60
|
interface AdapterInstance {
|
|
@@ -85,7 +90,7 @@ declare class Message implements Partial<MessageEvent> {
|
|
|
85
90
|
*
|
|
86
91
|
* If raw data is in any other format or string, it will be automatically converted and encoded.
|
|
87
92
|
*/
|
|
88
|
-
uint8Array(): Uint8Array
|
|
93
|
+
uint8Array(): Uint8Array;
|
|
89
94
|
/**
|
|
90
95
|
* Get data as [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) or [SharedArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer) value.
|
|
91
96
|
*
|
|
@@ -113,6 +118,9 @@ declare class Message implements Partial<MessageEvent> {
|
|
|
113
118
|
get data(): unknown;
|
|
114
119
|
toString(): string;
|
|
115
120
|
[Symbol.toPrimitive](): string;
|
|
121
|
+
[kNodeInspect](): {
|
|
122
|
+
data: unknown;
|
|
123
|
+
};
|
|
116
124
|
}
|
|
117
125
|
|
|
118
126
|
declare function defineHooks<T extends Partial<Hooks> = Partial<Hooks>>(hooks: T): T;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { d as defineHooks, b as defineWebSocketAdapter } from './shared/crossws.D9ehKjSh.mjs';
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { randomUUID } from 'uncrypto';
|
|
2
2
|
|
|
3
|
+
const kNodeInspect = /* @__PURE__ */ Symbol.for(
|
|
4
|
+
"nodejs.util.inspect.custom"
|
|
5
|
+
);
|
|
3
6
|
function toBufferLike(val) {
|
|
4
7
|
if (val === undefined || val === null) {
|
|
5
8
|
return "";
|
|
@@ -206,7 +209,7 @@ class Message {
|
|
|
206
209
|
[Symbol.toPrimitive]() {
|
|
207
210
|
return this.text();
|
|
208
211
|
}
|
|
209
|
-
[
|
|
212
|
+
[kNodeInspect]() {
|
|
210
213
|
return { data: this.rawData };
|
|
211
214
|
}
|
|
212
215
|
}
|
|
@@ -260,6 +263,10 @@ class Peer {
|
|
|
260
263
|
get peers() {
|
|
261
264
|
return this._internal.peers || /* @__PURE__ */ new Set();
|
|
262
265
|
}
|
|
266
|
+
/** All topics, this peer has been subscribed to. */
|
|
267
|
+
get topics() {
|
|
268
|
+
return this._topics;
|
|
269
|
+
}
|
|
263
270
|
/** Abruptly close the connection */
|
|
264
271
|
terminate() {
|
|
265
272
|
this.close();
|
|
@@ -282,7 +289,7 @@ class Peer {
|
|
|
282
289
|
[Symbol.toStringTag]() {
|
|
283
290
|
return "WebSocket";
|
|
284
291
|
}
|
|
285
|
-
[
|
|
292
|
+
[kNodeInspect]() {
|
|
286
293
|
return Object.fromEntries(
|
|
287
294
|
[
|
|
288
295
|
["id", this.id],
|
|
@@ -19,7 +19,14 @@ class AdapterHookable {
|
|
|
19
19
|
);
|
|
20
20
|
}
|
|
21
21
|
async upgrade(request) {
|
|
22
|
-
|
|
22
|
+
let context = request.context;
|
|
23
|
+
if (!context) {
|
|
24
|
+
context = {};
|
|
25
|
+
Object.defineProperty(request, "context", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
value: context
|
|
28
|
+
});
|
|
29
|
+
}
|
|
23
30
|
try {
|
|
24
31
|
const res = await this.callHook(
|
|
25
32
|
"upgrade",
|
|
@@ -38,8 +45,12 @@ class AdapterHookable {
|
|
|
38
45
|
};
|
|
39
46
|
}
|
|
40
47
|
} catch (error) {
|
|
41
|
-
|
|
42
|
-
|
|
48
|
+
const errResponse = error.response || error;
|
|
49
|
+
if (errResponse instanceof Response) {
|
|
50
|
+
return {
|
|
51
|
+
context,
|
|
52
|
+
endResponse: errResponse
|
|
53
|
+
};
|
|
43
54
|
}
|
|
44
55
|
throw error;
|
|
45
56
|
}
|
|
@@ -54,10 +65,16 @@ function adapterUtils(peers) {
|
|
|
54
65
|
return {
|
|
55
66
|
peers,
|
|
56
67
|
publish(topic, message, options) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
68
|
+
let firstPeerWithTopic;
|
|
69
|
+
for (const peer of peers) {
|
|
70
|
+
if (peer.topics.has(topic)) {
|
|
71
|
+
firstPeerWithTopic = peer;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (firstPeerWithTopic) {
|
|
76
|
+
firstPeerWithTopic.send(message, options);
|
|
77
|
+
firstPeerWithTopic.publish(topic, message, options);
|
|
61
78
|
}
|
|
62
79
|
}
|
|
63
80
|
};
|
|
@@ -66,4 +83,4 @@ function defineWebSocketAdapter(factory) {
|
|
|
66
83
|
return factory;
|
|
67
84
|
}
|
|
68
85
|
|
|
69
|
-
export { AdapterHookable as A, adapterUtils as a,
|
|
86
|
+
export { AdapterHookable as A, adapterUtils as a, defineWebSocketAdapter as b, defineHooks as d };
|
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
declare const
|
|
2
|
-
new (url: string | URL, protocols?: string | string[]): WebSocket;
|
|
3
|
-
prototype: WebSocket;
|
|
4
|
-
readonly CONNECTING: 0;
|
|
5
|
-
readonly OPEN: 1;
|
|
6
|
-
readonly CLOSING: 2;
|
|
7
|
-
readonly CLOSED: 3;
|
|
8
|
-
};
|
|
1
|
+
declare const WebSocket: typeof globalThis.WebSocket;
|
|
9
2
|
|
|
10
|
-
export {
|
|
3
|
+
export { WebSocket as default };
|
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
declare const
|
|
2
|
-
new (url: string | URL, protocols?: string | string[]): WebSocket;
|
|
3
|
-
prototype: WebSocket;
|
|
4
|
-
readonly CONNECTING: 0;
|
|
5
|
-
readonly OPEN: 1;
|
|
6
|
-
readonly CLOSING: 2;
|
|
7
|
-
readonly CLOSED: 3;
|
|
8
|
-
};
|
|
1
|
+
declare const WebSocket: typeof globalThis.WebSocket;
|
|
9
2
|
|
|
10
|
-
export {
|
|
3
|
+
export { WebSocket as default };
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
const
|
|
1
|
+
const WebSocket = globalThis.WebSocket;
|
|
2
2
|
|
|
3
|
-
export {
|
|
3
|
+
export { WebSocket as default };
|