crossws 0.3.3 → 0.3.5
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/README.md +7 -7
- package/dist/adapters/bun.d.mts +7 -6
- package/dist/adapters/bun.d.ts +7 -6
- package/dist/adapters/bun.mjs +43 -43
- package/dist/adapters/cloudflare-durable.d.mts +30 -8
- package/dist/adapters/cloudflare-durable.d.ts +30 -8
- package/dist/adapters/cloudflare-durable.mjs +18 -11
- package/dist/adapters/cloudflare.d.mts +5 -4
- package/dist/adapters/cloudflare.d.ts +5 -4
- package/dist/adapters/cloudflare.mjs +52 -54
- package/dist/adapters/deno.d.mts +5 -4
- package/dist/adapters/deno.d.ts +5 -4
- package/dist/adapters/deno.mjs +42 -44
- package/dist/adapters/node.d.mts +13 -14
- package/dist/adapters/node.d.ts +13 -14
- package/dist/adapters/node.mjs +67 -62
- package/dist/adapters/sse.d.mts +5 -4
- package/dist/adapters/sse.d.ts +5 -4
- package/dist/adapters/sse.mjs +6 -6
- package/dist/adapters/uws.d.mts +6 -4
- package/dist/adapters/uws.d.ts +6 -4
- package/dist/adapters/uws.mjs +84 -81
- package/dist/index.d.mts +13 -4
- package/dist/index.d.ts +13 -4
- package/dist/index.mjs +1 -1
- package/dist/shared/{crossws.ChIJSJVK.d.mts → crossws.BQXMA5bH.d.mts} +1 -1
- package/dist/shared/{crossws.ChIJSJVK.d.ts → crossws.BQXMA5bH.d.ts} +1 -1
- package/dist/shared/{crossws.DelSCW9g.mjs → crossws.CipVM6lf.mjs} +197 -7
- package/dist/shared/{crossws.CB4awDDj.mjs → crossws.D9ehKjSh.mjs} +19 -6
- package/dist/shared/{crossws.DLRVRjZs.mjs → crossws.DfCzGthR.mjs} +12 -5
- 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 +3 -3
- package/dist/websocket/sse.d.mts +3 -2
- package/dist/websocket/sse.d.ts +3 -2
- package/dist/websocket/sse.mjs +1 -1
- package/package.json +32 -24
package/dist/adapters/node.d.mts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
import { AdapterInstance, AdapterOptions
|
|
2
|
-
import { IncomingMessage as
|
|
3
|
-
import {
|
|
1
|
+
import { Adapter, AdapterInstance, AdapterOptions } from '../index.mjs';
|
|
2
|
+
import { Agent, ClientRequestArgs, IncomingMessage, ClientRequest, Server as Server$1, OutgoingHttpHeaders } from 'node:http';
|
|
3
|
+
import { DuplexOptions, Duplex } from 'node:stream';
|
|
4
4
|
import { EventEmitter } from 'events';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import
|
|
10
|
-
import { ZlibOptions } from 'zlib';
|
|
11
|
-
import '../shared/crossws.ChIJSJVK.mjs';
|
|
5
|
+
import { Server as Server$2 } from 'node:https';
|
|
6
|
+
import { SecureContextOptions } from 'node:tls';
|
|
7
|
+
import { URL } from 'node:url';
|
|
8
|
+
import { ZlibOptions } from 'node:zlib';
|
|
9
|
+
import '../shared/crossws.BQXMA5bH.mjs';
|
|
12
10
|
|
|
13
11
|
type BufferLike = string | Buffer | DataView | number | ArrayBufferView | Uint8Array | ArrayBuffer | SharedArrayBuffer | readonly any[] | readonly number[] | {
|
|
14
12
|
valueOf(): ArrayBuffer;
|
|
@@ -288,13 +286,14 @@ type WebSocketServer = Server;
|
|
|
288
286
|
declare function createWebSocketStream(websocket: WebSocket, options?: DuplexOptions): Duplex;
|
|
289
287
|
|
|
290
288
|
interface NodeAdapter extends AdapterInstance {
|
|
291
|
-
handleUpgrade(req: IncomingMessage
|
|
292
|
-
closeAll: (code?: number, data?: string | Buffer) => void;
|
|
289
|
+
handleUpgrade(req: IncomingMessage, socket: Duplex, head: Buffer): Promise<void>;
|
|
290
|
+
closeAll: (code?: number, data?: string | Buffer, force?: boolean) => void;
|
|
293
291
|
}
|
|
294
292
|
interface NodeOptions extends AdapterOptions {
|
|
295
293
|
wss?: WebSocketServer;
|
|
296
294
|
serverOptions?: ServerOptions;
|
|
297
295
|
}
|
|
298
|
-
declare const
|
|
296
|
+
declare const nodeAdapter: Adapter<NodeAdapter, NodeOptions>;
|
|
299
297
|
|
|
300
|
-
export {
|
|
298
|
+
export { nodeAdapter as default };
|
|
299
|
+
export type { NodeAdapter, NodeOptions };
|
package/dist/adapters/node.d.ts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
import { AdapterInstance, AdapterOptions
|
|
2
|
-
import { IncomingMessage as
|
|
3
|
-
import {
|
|
1
|
+
import { Adapter, AdapterInstance, AdapterOptions } from '../index.js';
|
|
2
|
+
import { Agent, ClientRequestArgs, IncomingMessage, ClientRequest, Server as Server$1, OutgoingHttpHeaders } from 'node:http';
|
|
3
|
+
import { DuplexOptions, Duplex } from 'node:stream';
|
|
4
4
|
import { EventEmitter } from 'events';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import
|
|
10
|
-
import { ZlibOptions } from 'zlib';
|
|
11
|
-
import '../shared/crossws.ChIJSJVK.js';
|
|
5
|
+
import { Server as Server$2 } from 'node:https';
|
|
6
|
+
import { SecureContextOptions } from 'node:tls';
|
|
7
|
+
import { URL } from 'node:url';
|
|
8
|
+
import { ZlibOptions } from 'node:zlib';
|
|
9
|
+
import '../shared/crossws.BQXMA5bH.js';
|
|
12
10
|
|
|
13
11
|
type BufferLike = string | Buffer | DataView | number | ArrayBufferView | Uint8Array | ArrayBuffer | SharedArrayBuffer | readonly any[] | readonly number[] | {
|
|
14
12
|
valueOf(): ArrayBuffer;
|
|
@@ -288,13 +286,14 @@ type WebSocketServer = Server;
|
|
|
288
286
|
declare function createWebSocketStream(websocket: WebSocket, options?: DuplexOptions): Duplex;
|
|
289
287
|
|
|
290
288
|
interface NodeAdapter extends AdapterInstance {
|
|
291
|
-
handleUpgrade(req: IncomingMessage
|
|
292
|
-
closeAll: (code?: number, data?: string | Buffer) => void;
|
|
289
|
+
handleUpgrade(req: IncomingMessage, socket: Duplex, head: Buffer): Promise<void>;
|
|
290
|
+
closeAll: (code?: number, data?: string | Buffer, force?: boolean) => void;
|
|
293
291
|
}
|
|
294
292
|
interface NodeOptions extends AdapterOptions {
|
|
295
293
|
wss?: WebSocketServer;
|
|
296
294
|
serverOptions?: ServerOptions;
|
|
297
295
|
}
|
|
298
|
-
declare const
|
|
296
|
+
declare const nodeAdapter: Adapter<NodeAdapter, NodeOptions>;
|
|
299
297
|
|
|
300
|
-
export {
|
|
298
|
+
export { nodeAdapter as default };
|
|
299
|
+
export type { NodeAdapter, NodeOptions };
|
package/dist/adapters/node.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
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.DfCzGthR.mjs';
|
|
2
|
+
import { A as AdapterHookable, a as adapterUtils } from '../shared/crossws.D9ehKjSh.mjs';
|
|
3
3
|
import { W as WSError } from '../shared/crossws.By9qWDAI.mjs';
|
|
4
|
-
import '
|
|
5
|
-
import { _ as _WebSocketServer } from '../shared/crossws.DelSCW9g.mjs';
|
|
4
|
+
import { _ as _WebSocketServer } from '../shared/crossws.CipVM6lf.mjs';
|
|
6
5
|
import 'uncrypto';
|
|
6
|
+
import 'stream';
|
|
7
7
|
import 'events';
|
|
8
8
|
import 'http';
|
|
9
9
|
import 'crypto';
|
|
@@ -14,68 +14,70 @@ 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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
|
|
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);
|
|
62
|
+
}
|
|
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, force) => {
|
|
71
|
+
for (const client of wss.clients) {
|
|
72
|
+
if (force) {
|
|
73
|
+
client.terminate();
|
|
74
|
+
} else {
|
|
73
75
|
client.close(code, data);
|
|
74
76
|
}
|
|
75
77
|
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
};
|
|
79
81
|
class NodePeer extends Peer {
|
|
80
82
|
get remoteAddress() {
|
|
81
83
|
return this._internal.nodeReq.socket?.remoteAddress;
|
|
@@ -85,7 +87,7 @@ class NodePeer extends Peer {
|
|
|
85
87
|
}
|
|
86
88
|
send(data, options) {
|
|
87
89
|
const dataBuff = toBufferLike(data);
|
|
88
|
-
const isBinary = typeof
|
|
90
|
+
const isBinary = typeof dataBuff !== "string";
|
|
89
91
|
this._internal.ws.send(dataBuff, {
|
|
90
92
|
compress: options?.compress,
|
|
91
93
|
binary: isBinary,
|
|
@@ -151,8 +153,11 @@ async function sendResponse(socket, res) {
|
|
|
151
153
|
}
|
|
152
154
|
}
|
|
153
155
|
return new Promise((resolve) => {
|
|
154
|
-
socket.end(
|
|
156
|
+
socket.end(() => {
|
|
157
|
+
socket.destroy();
|
|
158
|
+
resolve();
|
|
159
|
+
});
|
|
155
160
|
});
|
|
156
161
|
}
|
|
157
162
|
|
|
158
|
-
export {
|
|
163
|
+
export { nodeAdapter as default };
|
package/dist/adapters/sse.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { AdapterInstance, AdapterOptions
|
|
2
|
-
import '../shared/crossws.
|
|
1
|
+
import { Adapter, AdapterInstance, AdapterOptions } from '../index.mjs';
|
|
2
|
+
import '../shared/crossws.BQXMA5bH.mjs';
|
|
3
3
|
|
|
4
4
|
interface SSEAdapter extends AdapterInstance {
|
|
5
5
|
fetch(req: Request): Promise<Response>;
|
|
@@ -7,6 +7,7 @@ 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 {
|
|
12
|
+
export { sseAdapter as default };
|
|
13
|
+
export type { SSEAdapter, SSEOptions };
|
package/dist/adapters/sse.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { AdapterInstance, AdapterOptions
|
|
2
|
-
import '../shared/crossws.
|
|
1
|
+
import { Adapter, AdapterInstance, AdapterOptions } from '../index.js';
|
|
2
|
+
import '../shared/crossws.BQXMA5bH.js';
|
|
3
3
|
|
|
4
4
|
interface SSEAdapter extends AdapterInstance {
|
|
5
5
|
fetch(req: Request): Promise<Response>;
|
|
@@ -7,6 +7,7 @@ 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 {
|
|
12
|
+
export { sseAdapter as default };
|
|
13
|
+
export type { SSEAdapter, SSEOptions };
|
package/dist/adapters/sse.mjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
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.DfCzGthR.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
|
-
const peersMap = opts.bidir ? /* @__PURE__ */ new Map() :
|
|
8
|
+
const peersMap = opts.bidir ? /* @__PURE__ */ new Map() : void 0;
|
|
9
9
|
return {
|
|
10
10
|
...adapterUtils(peers),
|
|
11
11
|
fetch: async (request) => {
|
|
@@ -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
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { W as WebSocket } from '../shared/crossws.
|
|
1
|
+
import { Adapter, AdapterInstance, Peer, AdapterOptions } from '../index.mjs';
|
|
2
|
+
import { W as WebSocket } from '../shared/crossws.BQXMA5bH.mjs';
|
|
3
3
|
import uws from 'uWebSockets.js';
|
|
4
4
|
|
|
5
5
|
type UserData = {
|
|
@@ -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,5 @@ declare class UwsWebSocketProxy implements Partial<WebSocket> {
|
|
|
54
55
|
get extensions(): string;
|
|
55
56
|
}
|
|
56
57
|
|
|
57
|
-
export {
|
|
58
|
+
export { uwsAdapter as default };
|
|
59
|
+
export type { UWSAdapter, UWSOptions };
|
package/dist/adapters/uws.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { W as WebSocket } from '../shared/crossws.
|
|
1
|
+
import { Adapter, AdapterInstance, Peer, AdapterOptions } from '../index.js';
|
|
2
|
+
import { W as WebSocket } from '../shared/crossws.BQXMA5bH.js';
|
|
3
3
|
import uws from 'uWebSockets.js';
|
|
4
4
|
|
|
5
5
|
type UserData = {
|
|
@@ -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,5 @@ declare class UwsWebSocketProxy implements Partial<WebSocket> {
|
|
|
54
55
|
get extensions(): string;
|
|
55
56
|
}
|
|
56
57
|
|
|
57
|
-
export {
|
|
58
|
+
export { uwsAdapter as default };
|
|
59
|
+
export type { UWSAdapter, UWSOptions };
|
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.DfCzGthR.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,4 +1,6 @@
|
|
|
1
|
-
import { W as WebSocket } from './shared/crossws.
|
|
1
|
+
import { W as WebSocket } from './shared/crossws.BQXMA5bH.mjs';
|
|
2
|
+
|
|
3
|
+
declare const kNodeInspect: unique symbol;
|
|
2
4
|
|
|
3
5
|
interface AdapterInternal {
|
|
4
6
|
ws: unknown;
|
|
@@ -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;
|
|
@@ -145,4 +153,5 @@ interface Hooks {
|
|
|
145
153
|
error: (peer: Peer, error: WSError) => MaybePromise<void>;
|
|
146
154
|
}
|
|
147
155
|
|
|
148
|
-
export {
|
|
156
|
+
export { Message, Peer, WSError, defineHooks, defineWebSocketAdapter };
|
|
157
|
+
export type { Adapter, AdapterInstance, AdapterInternal, AdapterOptions, Hooks, ResolveHooks };
|