@celljs/rpc 4.33.0 → 4.35.0
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 +6 -0
- package/README.zh-cn.md +6 -0
- package/esm/common/protocol/json-rpc.d.ts +2 -0
- package/esm/common/protocol/json-rpc.js +2 -0
- package/esm/common/protocol/json-rpc.js.map +1 -1
- package/esm/common/transport/index.d.ts +1 -0
- package/esm/common/transport/index.js +1 -0
- package/esm/common/transport/index.js.map +1 -1
- package/esm/common/transport/rpc-websocket-client.d.ts +139 -0
- package/esm/common/transport/rpc-websocket-client.js +404 -0
- package/esm/common/transport/rpc-websocket-client.js.map +1 -0
- package/esm/common/transport/websocket-transport.d.ts +10 -19
- package/esm/common/transport/websocket-transport.js +50 -121
- package/esm/common/transport/websocket-transport.js.map +1 -1
- package/esm/node/connection/index.d.ts +1 -0
- package/esm/node/connection/index.js +1 -0
- package/esm/node/connection/index.js.map +1 -1
- package/esm/node/connection/rpc-push-channel.d.ts +43 -0
- package/esm/node/connection/rpc-push-channel.js +83 -0
- package/esm/node/connection/rpc-push-channel.js.map +1 -0
- package/esm/node/websocket/index.d.ts +1 -0
- package/esm/node/websocket/index.js +1 -0
- package/esm/node/websocket/index.js.map +1 -1
- package/esm/node/websocket/rpc-websocket-server.d.ts +1 -2
- package/esm/node/websocket/rpc-websocket-server.js +10 -19
- package/esm/node/websocket/rpc-websocket-server.js.map +1 -1
- package/esm/node/websocket/websocket-upgrades.d.ts +46 -0
- package/esm/node/websocket/websocket-upgrades.js +76 -0
- package/esm/node/websocket/websocket-upgrades.js.map +1 -0
- package/lib/common/protocol/json-rpc.d.ts +2 -0
- package/lib/common/protocol/json-rpc.d.ts.map +1 -1
- package/lib/common/protocol/json-rpc.js +2 -0
- package/lib/common/protocol/json-rpc.js.map +1 -1
- package/lib/common/transport/index.d.ts +1 -0
- package/lib/common/transport/index.d.ts.map +1 -1
- package/lib/common/transport/index.js +1 -0
- package/lib/common/transport/index.js.map +1 -1
- package/lib/common/transport/rpc-websocket-client.d.ts +140 -0
- package/lib/common/transport/rpc-websocket-client.d.ts.map +1 -0
- package/lib/common/transport/rpc-websocket-client.js +408 -0
- package/lib/common/transport/rpc-websocket-client.js.map +1 -0
- package/lib/common/transport/rpc-websocket-client.spec.d.ts +2 -0
- package/lib/common/transport/rpc-websocket-client.spec.d.ts.map +1 -0
- package/lib/common/transport/rpc-websocket-client.spec.js +162 -0
- package/lib/common/transport/rpc-websocket-client.spec.js.map +1 -0
- package/lib/common/transport/websocket-transport.d.ts +10 -19
- package/lib/common/transport/websocket-transport.d.ts.map +1 -1
- package/lib/common/transport/websocket-transport.js +50 -121
- package/lib/common/transport/websocket-transport.js.map +1 -1
- package/lib/node/connection/index.d.ts +1 -0
- package/lib/node/connection/index.d.ts.map +1 -1
- package/lib/node/connection/index.js +1 -0
- package/lib/node/connection/index.js.map +1 -1
- package/lib/node/connection/rpc-push-channel.d.ts +44 -0
- package/lib/node/connection/rpc-push-channel.d.ts.map +1 -0
- package/lib/node/connection/rpc-push-channel.js +87 -0
- package/lib/node/connection/rpc-push-channel.js.map +1 -0
- package/lib/node/connection/rpc-push-channel.spec.d.ts +2 -0
- package/lib/node/connection/rpc-push-channel.spec.d.ts.map +1 -0
- package/lib/node/connection/rpc-push-channel.spec.js +59 -0
- package/lib/node/connection/rpc-push-channel.spec.js.map +1 -0
- package/lib/node/websocket/index.d.ts +1 -0
- package/lib/node/websocket/index.d.ts.map +1 -1
- package/lib/node/websocket/index.js +1 -0
- package/lib/node/websocket/index.js.map +1 -1
- package/lib/node/websocket/rpc-websocket-server.d.ts +1 -2
- package/lib/node/websocket/rpc-websocket-server.d.ts.map +1 -1
- package/lib/node/websocket/rpc-websocket-server.js +10 -19
- package/lib/node/websocket/rpc-websocket-server.js.map +1 -1
- package/lib/node/websocket/websocket-upgrades.d.ts +47 -0
- package/lib/node/websocket/websocket-upgrades.d.ts.map +1 -0
- package/lib/node/websocket/websocket-upgrades.js +80 -0
- package/lib/node/websocket/websocket-upgrades.js.map +1 -0
- package/lib/node/websocket/websocket-upgrades.spec.d.ts +2 -0
- package/lib/node/websocket/websocket-upgrades.spec.d.ts.map +1 -0
- package/lib/node/websocket/websocket-upgrades.spec.js +64 -0
- package/lib/node/websocket/websocket-upgrades.spec.js.map +1 -0
- package/package.json +4 -4
- package/src/common/protocol/json-rpc.ts +2 -0
- package/src/common/transport/index.ts +1 -0
- package/src/common/transport/rpc-websocket-client.spec.ts +164 -0
- package/src/common/transport/rpc-websocket-client.ts +476 -0
- package/src/common/transport/websocket-transport.ts +51 -126
- package/src/node/connection/index.ts +3 -1
- package/src/node/connection/rpc-push-channel.spec.ts +61 -0
- package/src/node/connection/rpc-push-channel.ts +98 -0
- package/src/node/websocket/index.ts +1 -0
- package/src/node/websocket/rpc-websocket-server.ts +5 -16
- package/src/node/websocket/websocket-upgrades.spec.ts +64 -0
- package/src/node/websocket/websocket-upgrades.ts +95 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { JsonRpc } from '../protocol/index.js';
|
|
2
|
+
import { RpcWebSocketClient } from './rpc-websocket-client.js';
|
|
2
3
|
import { RpcTransport } from './transport-protocol.js';
|
|
3
4
|
export interface WebSocketTransportOptions {
|
|
4
5
|
/** `ws://` or `wss://` URL of the endpoint (`/<rpc.path>/ws` of the server). */
|
|
@@ -16,39 +17,29 @@ export interface WebSocketTransportOptions {
|
|
|
16
17
|
/** Milliseconds to wait for a connection to open before using the fallback. Default 5000. */
|
|
17
18
|
connectTimeout?: number;
|
|
18
19
|
}
|
|
19
|
-
interface Pending {
|
|
20
|
-
resolve(response: JsonRpc.Response): void;
|
|
21
|
-
timer: ReturnType<typeof setTimeout>;
|
|
22
|
-
}
|
|
23
20
|
/**
|
|
24
21
|
* JSON-RPC over one WebSocket connection for all services (methods are `<service>/<method>`), which
|
|
25
|
-
* the server can call back over (`@RpcClient`). While no connection can
|
|
26
|
-
* this runtime, a server or platform without WebSocket support) calls go
|
|
27
|
-
* A connection that closes answers its pending calls with
|
|
28
|
-
* not sent again, since they may have run.
|
|
22
|
+
* the server can call back over (`@RpcClient`): an {@link RpcWebSocketClient}. While no connection can
|
|
23
|
+
* be opened (no `WebSocket` in this runtime, a server or platform without WebSocket support) calls go
|
|
24
|
+
* to the fallback transport. A connection that closes answers its pending calls with
|
|
25
|
+
* {@link JsonRpc.CONNECTION_CLOSED}: they are not sent again, since they may have run.
|
|
29
26
|
*/
|
|
30
27
|
export declare class WebSocketTransport implements RpcTransport {
|
|
31
28
|
protected readonly options: WebSocketTransportOptions;
|
|
32
29
|
static readonly MAX_RECONNECT_DELAY = 30000;
|
|
33
|
-
protected
|
|
34
|
-
protected
|
|
35
|
-
protected readonly pending: Map<JsonRpc.Id, Pending>;
|
|
36
|
-
protected seq: number;
|
|
30
|
+
protected client?: RpcWebSocketClient;
|
|
31
|
+
protected resolving?: Promise<RpcWebSocketClient>;
|
|
37
32
|
protected retryAt: number;
|
|
38
33
|
protected delay: number;
|
|
39
|
-
protected reconnectTimer?: ReturnType<typeof setTimeout>;
|
|
40
34
|
protected closed: boolean;
|
|
35
|
+
protected retryTimer?: ReturnType<typeof setTimeout>;
|
|
41
36
|
constructor(options: WebSocketTransportOptions);
|
|
42
37
|
/** Whether calls go over a WebSocket connection now. */
|
|
43
38
|
get connected(): boolean;
|
|
44
39
|
call(service: string, method: string, params: unknown[]): Promise<JsonRpc.Response>;
|
|
45
40
|
/** Opens the connection (calls open it when needed); undefined while none can be made. */
|
|
46
|
-
connect(): Promise<
|
|
41
|
+
connect(): Promise<RpcWebSocketClient | undefined>;
|
|
47
42
|
/** Closes the connection for good: later calls use the fallback. */
|
|
48
43
|
close(): void;
|
|
49
|
-
protected
|
|
50
|
-
protected scheduleReconnect(): void;
|
|
51
|
-
protected failPending(): void;
|
|
52
|
-
protected receive(socket: WebSocket, data: unknown): void;
|
|
44
|
+
protected create(): Promise<RpcWebSocketClient>;
|
|
53
45
|
}
|
|
54
|
-
export {};
|
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import { JsonRpc } from '../protocol/index.js';
|
|
2
|
+
import { RpcWebSocketClient } from './rpc-websocket-client.js';
|
|
2
3
|
/**
|
|
3
4
|
* JSON-RPC over one WebSocket connection for all services (methods are `<service>/<method>`), which
|
|
4
|
-
* the server can call back over (`@RpcClient`). While no connection can
|
|
5
|
-
* this runtime, a server or platform without WebSocket support) calls go
|
|
6
|
-
* A connection that closes answers its pending calls with
|
|
7
|
-
* not sent again, since they may have run.
|
|
5
|
+
* the server can call back over (`@RpcClient`): an {@link RpcWebSocketClient}. While no connection can
|
|
6
|
+
* be opened (no `WebSocket` in this runtime, a server or platform without WebSocket support) calls go
|
|
7
|
+
* to the fallback transport. A connection that closes answers its pending calls with
|
|
8
|
+
* {@link JsonRpc.CONNECTION_CLOSED}: they are not sent again, since they may have run.
|
|
8
9
|
*/
|
|
9
10
|
export class WebSocketTransport {
|
|
10
11
|
constructor(options) {
|
|
11
12
|
var _a;
|
|
12
13
|
this.options = options;
|
|
13
|
-
this.pending = new Map();
|
|
14
|
-
this.seq = 0;
|
|
15
14
|
this.retryAt = 0;
|
|
16
15
|
this.closed = false;
|
|
17
16
|
this.delay = (_a = options.reconnectDelay) !== null && _a !== void 0 ? _a : 1000;
|
|
@@ -19,141 +18,71 @@ export class WebSocketTransport {
|
|
|
19
18
|
/** Whether calls go over a WebSocket connection now. */
|
|
20
19
|
get connected() {
|
|
21
20
|
var _a;
|
|
22
|
-
|
|
21
|
+
var _b;
|
|
22
|
+
return (_b = (_a = this.client) === null || _a === void 0 ? void 0 : _a.connected) !== null && _b !== void 0 ? _b : false;
|
|
23
23
|
}
|
|
24
24
|
async call(service, method, params) {
|
|
25
|
-
const
|
|
26
|
-
if (!
|
|
25
|
+
const client = await this.connect();
|
|
26
|
+
if (!client) {
|
|
27
27
|
return this.options.fallback.call(service, method, params);
|
|
28
28
|
}
|
|
29
|
-
|
|
30
|
-
return new Promise(resolve => {
|
|
31
|
-
var _a;
|
|
32
|
-
const timeout = (_a = this.options.callTimeout) !== null && _a !== void 0 ? _a : 60000;
|
|
33
|
-
const timer = setTimeout(() => {
|
|
34
|
-
this.pending.delete(id);
|
|
35
|
-
resolve(JsonRpc.error(id, JsonRpc.TIMEOUT, `No response to ${service}.${method} within ${timeout} ms.`));
|
|
36
|
-
}, timeout);
|
|
37
|
-
this.pending.set(id, { resolve, timer });
|
|
38
|
-
socket.send(JSON.stringify(JsonRpc.request(id, `${service}/${method}`, params)));
|
|
39
|
-
});
|
|
29
|
+
return client.call(`${service}/${method}`, params);
|
|
40
30
|
}
|
|
41
31
|
/** Opens the connection (calls open it when needed); undefined while none can be made. */
|
|
42
|
-
connect() {
|
|
32
|
+
async connect() {
|
|
33
|
+
var _a, _b;
|
|
43
34
|
if (this.closed || typeof WebSocket === 'undefined') {
|
|
44
|
-
return
|
|
45
|
-
}
|
|
46
|
-
if (this.socket && this.socket.readyState === 1) {
|
|
47
|
-
return Promise.resolve(this.socket);
|
|
35
|
+
return undefined;
|
|
48
36
|
}
|
|
49
|
-
|
|
50
|
-
|
|
37
|
+
const client = await ((_a = this.resolving) !== null && _a !== void 0 ? _a : (this.resolving = this.create()));
|
|
38
|
+
if (client.connected) {
|
|
39
|
+
return client;
|
|
51
40
|
}
|
|
52
41
|
if (Date.now() < this.retryAt) {
|
|
53
|
-
return
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
await client.connect();
|
|
46
|
+
this.delay = (_b = this.options.reconnectDelay) !== null && _b !== void 0 ? _b : 1000;
|
|
47
|
+
return client;
|
|
48
|
+
}
|
|
49
|
+
catch (_c) {
|
|
50
|
+
// No WebSocket endpoint (yet): HTTP for a while, then try again; on its own when the server pushes.
|
|
51
|
+
this.retryAt = Date.now() + this.delay;
|
|
52
|
+
this.delay = Math.min(this.delay * 2, WebSocketTransport.MAX_RECONNECT_DELAY);
|
|
53
|
+
if (this.options.keepAlive) {
|
|
54
|
+
clearTimeout(this.retryTimer);
|
|
55
|
+
this.retryTimer = setTimeout(() => void this.connect(), this.retryAt - Date.now());
|
|
56
|
+
}
|
|
57
|
+
return undefined;
|
|
54
58
|
}
|
|
55
|
-
this.opening = this.open().finally(() => {
|
|
56
|
-
this.opening = undefined;
|
|
57
|
-
});
|
|
58
|
-
return this.opening;
|
|
59
59
|
}
|
|
60
60
|
/** Closes the connection for good: later calls use the fallback. */
|
|
61
61
|
close() {
|
|
62
62
|
var _a;
|
|
63
63
|
this.closed = true;
|
|
64
|
-
clearTimeout(this.
|
|
65
|
-
(_a = this.
|
|
64
|
+
clearTimeout(this.retryTimer);
|
|
65
|
+
(_a = this.client) === null || _a === void 0 ? void 0 : _a.close();
|
|
66
66
|
}
|
|
67
|
-
async
|
|
67
|
+
async create() {
|
|
68
|
+
var _a, _b, _c;
|
|
68
69
|
const url = await this.options.url();
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
//
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
failed = true;
|
|
81
|
-
this.retryAt = Date.now() + this.delay;
|
|
82
|
-
this.delay = Math.min(this.delay * 2, WebSocketTransport.MAX_RECONNECT_DELAY);
|
|
83
|
-
resolve(undefined);
|
|
84
|
-
this.scheduleReconnect();
|
|
85
|
-
};
|
|
86
|
-
// A server that never answers the upgrade (Bun < 1.4 behind node:http) must not hold the calls;
|
|
87
|
-
// nor a client whose close() of a connecting socket does not fire close (the WebSocket of Node.js 22).
|
|
88
|
-
const timer = setTimeout(() => {
|
|
89
|
-
socket.close();
|
|
90
|
-
fail();
|
|
91
|
-
}, (_a = this.options.connectTimeout) !== null && _a !== void 0 ? _a : 5000);
|
|
92
|
-
socket.onopen = () => {
|
|
93
|
-
var _a;
|
|
94
|
-
if (failed) {
|
|
95
|
-
socket.close(1000);
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
clearTimeout(timer);
|
|
99
|
-
opened = true;
|
|
100
|
-
this.socket = socket;
|
|
101
|
-
this.delay = (_a = this.options.reconnectDelay) !== null && _a !== void 0 ? _a : 1000;
|
|
102
|
-
resolve(socket);
|
|
103
|
-
};
|
|
104
|
-
socket.onmessage = event => this.receive(socket, event.data);
|
|
105
|
-
socket.onerror = () => undefined;
|
|
106
|
-
socket.onclose = () => {
|
|
107
|
-
if (!opened) {
|
|
108
|
-
fail();
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
this.socket = undefined;
|
|
112
|
-
this.failPending();
|
|
113
|
-
this.scheduleReconnect();
|
|
114
|
-
};
|
|
70
|
+
this.client = new RpcWebSocketClient({
|
|
71
|
+
createSocket: () => new WebSocket(url),
|
|
72
|
+
reconnect: !!this.options.keepAlive,
|
|
73
|
+
reconnectDelayMs: (_a = this.options.reconnectDelay) !== null && _a !== void 0 ? _a : 1000,
|
|
74
|
+
maxReconnectDelayMs: WebSocketTransport.MAX_RECONNECT_DELAY,
|
|
75
|
+
// A server that never answers the upgrade (Bun < 1.4 behind node:http) must not hold the calls.
|
|
76
|
+
openTimeoutMs: (_b = this.options.connectTimeout) !== null && _b !== void 0 ? _b : 5000,
|
|
77
|
+
waitForOpenMs: 0,
|
|
78
|
+
stallMs: 0,
|
|
79
|
+
callTimeoutMs: (_c = this.options.callTimeout) !== null && _c !== void 0 ? _c : 60000,
|
|
80
|
+
handle: request => { var _a, _b; var _c, _d; return (_c = (_b = (_a = this.options).handle) === null || _b === void 0 ? void 0 : _b.call(_a, request)) !== null && _c !== void 0 ? _c : Promise.resolve(JsonRpc.error((_d = request.id) !== null && _d !== void 0 ? _d : null, JsonRpc.METHOD_NOT_FOUND, `No @RpcClient for ${request.method}.`)); },
|
|
115
81
|
});
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
if (!this.options.keepAlive || this.closed) {
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
clearTimeout(this.reconnectTimer);
|
|
122
|
-
this.reconnectTimer = setTimeout(() => void this.connect(), Math.max(this.retryAt - Date.now(), this.delay));
|
|
123
|
-
}
|
|
124
|
-
failPending() {
|
|
125
|
-
for (const [id, pending] of this.pending) {
|
|
126
|
-
clearTimeout(pending.timer);
|
|
127
|
-
pending.resolve(JsonRpc.error(id, JsonRpc.CONNECTION_CLOSED, 'The WebSocket connection closed before the response arrived; the call may or may not have run.'));
|
|
128
|
-
}
|
|
129
|
-
this.pending.clear();
|
|
130
|
-
}
|
|
131
|
-
receive(socket, data) {
|
|
132
|
-
var _a;
|
|
133
|
-
let message;
|
|
134
|
-
try {
|
|
135
|
-
message = JSON.parse(String(data));
|
|
136
|
-
}
|
|
137
|
-
catch (_b) {
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
if (JsonRpc.isResponse(message)) {
|
|
141
|
-
const pending = message.id === null ? undefined : this.pending.get(message.id);
|
|
142
|
-
if (pending) {
|
|
143
|
-
clearTimeout(pending.timer);
|
|
144
|
-
this.pending.delete(message.id);
|
|
145
|
-
pending.resolve(message);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
else if (JsonRpc.isRequest(message)) {
|
|
149
|
-
const request = message;
|
|
150
|
-
const handle = (_a = this.options.handle) !== null && _a !== void 0 ? _a : (async (r) => { var _a; return JsonRpc.error((_a = r.id) !== null && _a !== void 0 ? _a : null, JsonRpc.METHOD_NOT_FOUND, `No @RpcClient for ${r.method}.`); });
|
|
151
|
-
void handle(request).then(response => {
|
|
152
|
-
if (request.id !== undefined && socket.readyState === 1) {
|
|
153
|
-
socket.send(JSON.stringify(response));
|
|
154
|
-
}
|
|
155
|
-
});
|
|
82
|
+
if (this.closed) {
|
|
83
|
+
this.client.close();
|
|
156
84
|
}
|
|
85
|
+
return this.client;
|
|
157
86
|
}
|
|
158
87
|
}
|
|
159
88
|
WebSocketTransport.MAX_RECONNECT_DELAY = 30000;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"websocket-transport.js","sourceRoot":"","sources":["../../../src/common/transport/websocket-transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"websocket-transport.js","sourceRoot":"","sources":["../../../src/common/transport/websocket-transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAoB5D;;;;;;GAMG;AACH,MAAM,OAAO,kBAAkB;IAW3B,YAA+B,OAAkC;;QAAlC,YAAO,GAAP,OAAO,CAA2B;QALvD,YAAO,GAAG,CAAC,CAAC;QAEZ,WAAM,GAAG,KAAK,CAAC;QAIrB,IAAI,CAAC,KAAK,SAAG,OAAO,CAAC,cAAc,mCAAI,IAAI,CAAC;IAChD,CAAC;IAED,wDAAwD;IACxD,IAAI,SAAS;;;QACT,aAAO,MAAA,IAAI,CAAC,MAAM,0CAAE,SAAS,mCAAI,KAAK,CAAC;IAC3C,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,OAAe,EAAE,MAAc,EAAE,MAAiB;QACzD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;QACpC,IAAI,CAAC,MAAM,EAAE,CAAC;YACV,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;QAC/D,CAAC;QACD,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,OAAO,IAAI,MAAM,EAAE,EAAE,MAAM,CAAC,CAAC;IACvD,CAAC;IAED,0FAA0F;IAC1F,KAAK,CAAC,OAAO;;QACT,IAAI,IAAI,CAAC,MAAM,IAAI,OAAO,SAAS,KAAK,WAAW,EAAE,CAAC;YAClD,OAAO,SAAS,CAAC;QACrB,CAAC;QACD,MAAM,MAAM,GAAG,MAAM,OAAC,IAAI,CAAC,SAAS,oCAAd,IAAI,CAAC,SAAS,G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CAAC;QACrC,IAAI,CAAC,MAAM,GAAG,IAAI,kBAAkB,CAAC;YACjC,YAAY,EAAE,GAAG,EAAE,CAAC,IAAI,SAAS,CAAC,GAAG,CAAC;YACtC,SAAS,EAAE,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,SAAS;YACnC,gBAAgB,QAAE,IAAI,CAAC,OAAO,CAAC,cAAc,mCAAI,IAAI;YACrD,mBAAmB,EAAE,kBAAkB,CAAC,mBAAmB;YAC3D,gGAAgG;YAChG,aAAa,QAAE,IAAI,CAAC,OAAO,CAAC,cAAc,mCAAI,IAAI;YAClD,aAAa,EAAE,CAAC;YAChB,OAAO,EAAE,CAAC;YACV,aAAa,QAAE,IAAI,CAAC,OAAO,CAAC,WAAW,mCAAI,KAAK;YAChD,MAAM,EAAE,OAAO,CAAC,EAAE,wCACd,MAAA,MAAA,IAAI,CAAC,OAAO,EAAC,MAAM,mDAAG,OAAO,CAAC,mCAC9B,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,OAAC,OAAO,CAAC,EAAE,mCAAI,IAAI,EAAE,OAAO,CAAC,gBAAgB,EAAE,qBAAqB,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,GAAA;SAC3H,CAAC,CAAC;QACH,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YACd,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QACxB,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,CAAC;IACvB,CAAC;CACJ;AAlFmB,sCAAmB,GAAG,KAAK,AAAR,CAAS"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/connection/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,eAAe,CAAC;AAC9B,cAAc,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/connection/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,eAAe,CAAC;AAC9B,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export interface RpcPushChannelOptions<T> {
|
|
2
|
+
/** Sends a batch to the client; the next waits until it settles (the client answered, or the call failed). */
|
|
3
|
+
send(batch: T[]): Promise<unknown>;
|
|
4
|
+
/** Whether the connection is closed: nothing is sent then. */
|
|
5
|
+
closed(): boolean;
|
|
6
|
+
/** Called once a client falls more than `maxBacklogBytes` behind; its queue is dropped. */
|
|
7
|
+
overflow(): void;
|
|
8
|
+
/** Bytes a client may fall behind. */
|
|
9
|
+
maxBacklogBytes: number;
|
|
10
|
+
/** Items of this kind leave at most once per `intervalMs`, the first of a while at once; others leave at once. */
|
|
11
|
+
streamed?(item: T): boolean;
|
|
12
|
+
/** Default 30. */
|
|
13
|
+
intervalMs?: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* What the server pushes to one client (`@RpcClient`), one call at a time: the next batch leaves when the client has
|
|
17
|
+
* answered the last, so that a slow client costs a bounded queue (`maxBacklogBytes`, then `overflow`) and nothing
|
|
18
|
+
* while nothing flows. Items that come by the hundred (`streamed`: the deltas of a reply) leave at most once an
|
|
19
|
+
* interval, the first of them at once; any other leaves at once, with what is queued, so that nothing a user waits
|
|
20
|
+
* for is held back. Items of the same turn of the event loop leave together.
|
|
21
|
+
*/
|
|
22
|
+
export declare class RpcPushChannel<T> {
|
|
23
|
+
protected readonly options: RpcPushChannelOptions<T>;
|
|
24
|
+
protected queue: T[];
|
|
25
|
+
protected queuedBytes: number;
|
|
26
|
+
protected timer?: ReturnType<typeof setTimeout>;
|
|
27
|
+
protected sending: boolean;
|
|
28
|
+
protected lastFlushAt: number;
|
|
29
|
+
/** When the timer fires, on the clock of `performance.now()`. */
|
|
30
|
+
protected dueAt: number;
|
|
31
|
+
/** The queue holds an item that should not wait. */
|
|
32
|
+
protected urgent: boolean;
|
|
33
|
+
/** The queue holds a streamed item. */
|
|
34
|
+
protected streamed: boolean;
|
|
35
|
+
constructor(options: RpcPushChannelOptions<T>);
|
|
36
|
+
/** Queues an item of `bytes` (its size as sent, about). */
|
|
37
|
+
push(item: T, bytes: number): void;
|
|
38
|
+
/** Drops what is queued. */
|
|
39
|
+
clear(): void;
|
|
40
|
+
/** A timer, not a direct send: the items of the same turn of the event loop leave together. */
|
|
41
|
+
protected schedule(): void;
|
|
42
|
+
protected flush(): void;
|
|
43
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the server pushes to one client (`@RpcClient`), one call at a time: the next batch leaves when the client has
|
|
3
|
+
* answered the last, so that a slow client costs a bounded queue (`maxBacklogBytes`, then `overflow`) and nothing
|
|
4
|
+
* while nothing flows. Items that come by the hundred (`streamed`: the deltas of a reply) leave at most once an
|
|
5
|
+
* interval, the first of them at once; any other leaves at once, with what is queued, so that nothing a user waits
|
|
6
|
+
* for is held back. Items of the same turn of the event loop leave together.
|
|
7
|
+
*/
|
|
8
|
+
export class RpcPushChannel {
|
|
9
|
+
constructor(options) {
|
|
10
|
+
this.options = options;
|
|
11
|
+
this.queue = [];
|
|
12
|
+
this.queuedBytes = 0;
|
|
13
|
+
this.sending = false;
|
|
14
|
+
this.lastFlushAt = Number.NEGATIVE_INFINITY;
|
|
15
|
+
/** When the timer fires, on the clock of `performance.now()`. */
|
|
16
|
+
this.dueAt = 0;
|
|
17
|
+
/** The queue holds an item that should not wait. */
|
|
18
|
+
this.urgent = false;
|
|
19
|
+
/** The queue holds a streamed item. */
|
|
20
|
+
this.streamed = false;
|
|
21
|
+
}
|
|
22
|
+
/** Queues an item of `bytes` (its size as sent, about). */
|
|
23
|
+
push(item, bytes) {
|
|
24
|
+
var _a, _b;
|
|
25
|
+
var _c;
|
|
26
|
+
this.queue.push(item);
|
|
27
|
+
this.queuedBytes += bytes;
|
|
28
|
+
if (this.queuedBytes > this.options.maxBacklogBytes) {
|
|
29
|
+
this.clear();
|
|
30
|
+
this.options.overflow();
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const streamed = (_c = (_b = (_a = this.options).streamed) === null || _b === void 0 ? void 0 : _b.call(_a, item)) !== null && _c !== void 0 ? _c : false;
|
|
34
|
+
this.streamed || (this.streamed = streamed);
|
|
35
|
+
this.urgent || (this.urgent = !streamed);
|
|
36
|
+
this.schedule();
|
|
37
|
+
}
|
|
38
|
+
/** Drops what is queued. */
|
|
39
|
+
clear() {
|
|
40
|
+
clearTimeout(this.timer);
|
|
41
|
+
this.timer = undefined;
|
|
42
|
+
this.queue = [];
|
|
43
|
+
this.queuedBytes = 0;
|
|
44
|
+
this.urgent = false;
|
|
45
|
+
this.streamed = false;
|
|
46
|
+
}
|
|
47
|
+
/** A timer, not a direct send: the items of the same turn of the event loop leave together. */
|
|
48
|
+
schedule() {
|
|
49
|
+
var _a;
|
|
50
|
+
const due = this.urgent ? 0 : this.lastFlushAt + ((_a = this.options.intervalMs) !== null && _a !== void 0 ? _a : 30);
|
|
51
|
+
if (this.timer !== undefined && this.dueAt <= due) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
clearTimeout(this.timer);
|
|
55
|
+
this.dueAt = due;
|
|
56
|
+
this.timer = setTimeout(() => this.flush(), Math.max(0, due - performance.now()));
|
|
57
|
+
}
|
|
58
|
+
flush() {
|
|
59
|
+
this.timer = undefined;
|
|
60
|
+
if (this.sending || !this.queue.length || this.options.closed()) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const batch = this.queue;
|
|
64
|
+
this.queue = [];
|
|
65
|
+
this.queuedBytes = 0;
|
|
66
|
+
if (this.streamed) {
|
|
67
|
+
this.lastFlushAt = performance.now();
|
|
68
|
+
}
|
|
69
|
+
this.urgent = false;
|
|
70
|
+
this.streamed = false;
|
|
71
|
+
this.sending = true;
|
|
72
|
+
this.options
|
|
73
|
+
.send(batch)
|
|
74
|
+
.catch(() => undefined)
|
|
75
|
+
.finally(() => {
|
|
76
|
+
this.sending = false;
|
|
77
|
+
if (this.queue.length) {
|
|
78
|
+
this.schedule();
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=rpc-push-channel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rpc-push-channel.js","sourceRoot":"","sources":["../../../src/node/connection/rpc-push-channel.ts"],"names":[],"mappings":"AAeA;;;;;;GAMG;AACH,MAAM,OAAO,cAAc;IAavB,YAA+B,OAAiC;QAAjC,YAAO,GAAP,OAAO,CAA0B;QAZtD,UAAK,GAAQ,EAAE,CAAC;QAChB,gBAAW,GAAG,CAAC,CAAC;QAEhB,YAAO,GAAG,KAAK,CAAC;QAChB,gBAAW,GAAG,MAAM,CAAC,iBAAiB,CAAC;QACjD,iEAAiE;QACvD,UAAK,GAAG,CAAC,CAAC;QACpB,oDAAoD;QAC1C,WAAM,GAAG,KAAK,CAAC;QACzB,uCAAuC;QAC7B,aAAQ,GAAG,KAAK,CAAC;IAEwC,CAAC;IAEpE,2DAA2D;IAC3D,IAAI,CAAC,IAAO,EAAE,KAAa;;;QACvB,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACtB,IAAI,CAAC,WAAW,IAAI,KAAK,CAAC;QAC1B,IAAI,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,OAAO,CAAC,eAAe,EAAE,CAAC;YAClD,IAAI,CAAC,KAAK,EAAE,CAAC;YACb,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;YACxB,OAAO;QACX,CAAC;QACD,MAAM,QAAQ,SAAG,MAAA,MAAA,IAAI,CAAC,OAAO,EAAC,QAAQ,mDAAG,IAAI,CAAC,mCAAI,KAAK,CAAC;QACxD,IAAI,CAAC,QAAQ,KAAb,IAAI,CAAC,QAAQ,GAAK,QAAQ,E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KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;QAChB,IAAI,CAAC,WAAW,GAAG,CAAC,CAAC;QACrB,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAChB,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;QACzC,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC;QACtB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,IAAI,CAAC,OAAO;aACP,IAAI,CAAC,KAAK,CAAC;aACX,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC;aACtB,OAAO,CAAC,GAAG,EAAE;YACV,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;YACrB,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;gBACpB,IAAI,CAAC,QAAQ,EAAE,CAAC;YACpB,CAAC;QACL,CAAC,CAAC,CAAC;IACX,CAAC;CACJ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/websocket/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,wBAAwB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/websocket/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,wBAAwB,CAAC"}
|
|
@@ -32,10 +32,9 @@ export declare class RpcWebSocketServer implements ServerAware, ApplicationLifec
|
|
|
32
32
|
protected readonly connections: RpcConnectionManager;
|
|
33
33
|
protected readonly guards: RpcWebSocketGuard[];
|
|
34
34
|
protected readonly logger: Logger;
|
|
35
|
-
protected server?: Server;
|
|
36
35
|
protected path: string;
|
|
37
36
|
protected base: string;
|
|
38
|
-
protected
|
|
37
|
+
protected unroute?: () => void;
|
|
39
38
|
setServer(server: Server): Promise<void>;
|
|
40
39
|
onStop(): void;
|
|
41
40
|
protected upgrade(request: IncomingMessage, socket: Duplex, head: Buffer): void;
|
|
@@ -15,6 +15,7 @@ import { RPC_CONNECTION_KEY, RpcConnectionManager, RpcWebSocketGuard } from '../
|
|
|
15
15
|
import { RpcConnectionImpl } from '../connection/rpc-connection.js';
|
|
16
16
|
import { WebSocketConnection } from './websocket-connection.js';
|
|
17
17
|
import { WebSocketExchange } from './websocket-exchange.js';
|
|
18
|
+
import { WebSocketUpgrades } from './websocket-upgrades.js';
|
|
18
19
|
/**
|
|
19
20
|
* With `rpc.transport: websocket`, accepts WebSocket connections at `/<rpc.path>/ws` of the HTTP
|
|
20
21
|
* server. Each call over a connection is dispatched like a POST to `/<rpc.path>/<service>` with the
|
|
@@ -34,26 +35,16 @@ let RpcWebSocketServer = class RpcWebSocketServer {
|
|
|
34
35
|
}
|
|
35
36
|
this.base = (await this.pathResolver.resolve(this.rpcPath)).replace(/\/$/, '');
|
|
36
37
|
this.path = `${this.base}/${RPC_WEBSOCKET_PATH}`;
|
|
37
|
-
this.
|
|
38
|
-
this.server = server;
|
|
39
|
-
server.on('upgrade', this.listener);
|
|
38
|
+
this.unroute = WebSocketUpgrades.of(server).route(this.path, (request, socket, head) => this.upgrade(request, socket, head));
|
|
40
39
|
}
|
|
41
40
|
onStop() {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
41
|
+
var _a;
|
|
42
|
+
(_a = this.unroute) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
45
43
|
this.connections.closeAll();
|
|
46
44
|
}
|
|
47
45
|
upgrade(request, socket, head) {
|
|
48
46
|
var _a, _b, _c, _d;
|
|
49
|
-
var _e, _f, _g, _h
|
|
50
|
-
if (((_e = request.url) !== null && _e !== void 0 ? _e : '').split('?')[0] !== this.path) {
|
|
51
|
-
// Another listener (the HMR socket of cell dev) may want it; nobody else would answer.
|
|
52
|
-
if (this.server.listenerCount('upgrade') === 1) {
|
|
53
|
-
socket.destroy();
|
|
54
|
-
}
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
47
|
+
var _e, _f, _g, _h;
|
|
57
48
|
const checked = WebSocketConnection.check(request);
|
|
58
49
|
if ('status' in checked) {
|
|
59
50
|
WebSocketConnection.refuse(socket, checked.status, checked.message);
|
|
@@ -64,16 +55,16 @@ let RpcWebSocketServer = class RpcWebSocketServer {
|
|
|
64
55
|
WebSocketConnection.refuse(socket, 403, 'Origin not allowed; add it to rpc.websocket.allowedOrigins.');
|
|
65
56
|
return;
|
|
66
57
|
}
|
|
67
|
-
if (!((
|
|
58
|
+
if (!((_e = this.guards) !== null && _e !== void 0 ? _e : []).every(guard => guard.allows(request))) {
|
|
68
59
|
WebSocketConnection.refuse(socket, 401, 'Not authorized.');
|
|
69
60
|
return;
|
|
70
61
|
}
|
|
71
|
-
const socketConnection = WebSocketConnection.open(socket, checked.key, head
|
|
72
|
-
maxPayload: (
|
|
73
|
-
heartbeat: (
|
|
62
|
+
const socketConnection = WebSocketConnection.open(socket, checked.key, head, {
|
|
63
|
+
maxPayload: (_f = (_a = this.options) === null || _a === void 0 ? void 0 : _a.maxPayload) !== null && _f !== void 0 ? _f : 1048576,
|
|
64
|
+
heartbeat: (_g = (_b = this.options) === null || _b === void 0 ? void 0 : _b.heartbeat) !== null && _g !== void 0 ? _g : 30000,
|
|
74
65
|
deflate: checked.deflate && ((_c = this.options) === null || _c === void 0 ? void 0 : _c.deflate) !== false,
|
|
75
66
|
});
|
|
76
|
-
const connection = new RpcConnectionImpl(socketConnection, request, (c, r) => this.dispatch(c, r), (
|
|
67
|
+
const connection = new RpcConnectionImpl(socketConnection, request, (c, r) => this.dispatch(c, r), (_h = (_d = this.options) === null || _d === void 0 ? void 0 : _d.callTimeout) !== null && _h !== void 0 ? _h : 30000);
|
|
77
68
|
this.connections.add(connection);
|
|
78
69
|
}
|
|
79
70
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rpc-websocket-server.js","sourceRoot":"","sources":["../../../src/node/websocket/rpc-websocket-server.ts"],"names":[],"mappings":";;;;;;;;;AAEA,OAAO,EAAe,oBAAoB,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAChH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACxE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,kBAAkB,EAAE,aAAa,EAAoB,MAAM,cAAc,CAAC;AACtG,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAChH,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAC7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAezD;;;;GAIG;AAEI,IAAM,kBAAkB,GAAxB,MAAM,kBAAkB;IAAxB;QAsBgB,WAAM,GAAwB,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"rpc-websocket-server.js","sourceRoot":"","sources":["../../../src/node/websocket/rpc-websocket-server.ts"],"names":[],"mappings":";;;;;;;;;AAEA,OAAO,EAAe,oBAAoB,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAChH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,sBAAsB,CA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,CAAC;CACJ,CAAA;AAhIsB;IADlB,KAAK,CAAC,eAAe,CAAC;;qDACyB;AAG7B;IADlB,KAAK,CAAC,eAAe,CAAC;;mDAC0B;AAG9B;IADlB,KAAK,CAAC,QAAQ,CAAC;;mDACmB;AAGhB;IADlB,SAAS,CAAC,YAAY,CAAC;;wDACsB;AAG3B;IADlB,SAAS,CAAC,UAAU,CAAC;;sDAC6B;AAGhC;IADlB,SAAS,CAAC,oBAAoB,CAAC;;uDACqB;AAIlC;IAFlB,SAAS,CAAC,iBAAiB,CAAC;IAC5B,QAAQ,EAAE;;kDACyC;AAGjC;IADlB,SAAS,CAAC,MAAM,CAAC;;kDACgB;AAzBzB,kBAAkB;IAD9B,SAAS,CAAC,CAAC,WAAW,EAAE,oBAAoB,CAAC,CAAC;GAClC,kBAAkB,CAmI9B"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { IncomingMessage } from 'node:http';
|
|
2
|
+
import type { Duplex } from 'node:stream';
|
|
3
|
+
/** What answers the upgrade requests of one route: `head` are the bytes already read after the request. */
|
|
4
|
+
export type WebSocketUpgradeHandler = (request: IncomingMessage, socket: Duplex, head: Buffer, url: URL) => void;
|
|
5
|
+
/** The part of a Node.js HTTP server the routes need. */
|
|
6
|
+
export interface WebSocketUpgradeServer {
|
|
7
|
+
on(event: 'upgrade', listener: (request: IncomingMessage, socket: Duplex, head: Buffer) => void): unknown;
|
|
8
|
+
off(event: 'upgrade', listener: (request: IncomingMessage, socket: Duplex, head: Buffer) => void): unknown;
|
|
9
|
+
listenerCount(event: 'upgrade'): number;
|
|
10
|
+
}
|
|
11
|
+
interface Route {
|
|
12
|
+
path: string;
|
|
13
|
+
/** The path and all below it (`/r/` takes `/r/abc`), not only the path itself. */
|
|
14
|
+
prefix: boolean;
|
|
15
|
+
handler: WebSocketUpgradeHandler;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The upgrade requests of one HTTP server, routed by path: one `upgrade` listener for all that answer WebSocket
|
|
19
|
+
* connections on it (the RPC socket of Cell, those of an application), so that none has to guess whether another
|
|
20
|
+
* will answer a request it does not take. A request no route takes is refused with 404, unless the server has
|
|
21
|
+
* listeners of its own besides (the HMR socket of a dev server), which may take it.
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* const dispose = WebSocketUpgrades.of(server).route('/api/exec', (request, socket, head) => exec.accept(request, socket, head));
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export declare class WebSocketUpgrades {
|
|
28
|
+
protected readonly server: WebSocketUpgradeServer;
|
|
29
|
+
protected static readonly servers: WeakMap<WebSocketUpgradeServer, WebSocketUpgrades>;
|
|
30
|
+
protected readonly routes: Route[];
|
|
31
|
+
protected readonly listener: (request: IncomingMessage, socket: Duplex, head: Buffer) => void;
|
|
32
|
+
/** The routes of `server`: the same for everyone that asks, listening while it has a route. */
|
|
33
|
+
static of(server: WebSocketUpgradeServer): WebSocketUpgrades;
|
|
34
|
+
protected constructor(server: WebSocketUpgradeServer);
|
|
35
|
+
/**
|
|
36
|
+
* Routes the upgrade requests of `path` (and, with `prefix`, of the paths below it) to `handler`; a path taken
|
|
37
|
+
* already throws. Returns what removes the route.
|
|
38
|
+
*/
|
|
39
|
+
route(path: string, handler: WebSocketUpgradeHandler, options?: {
|
|
40
|
+
prefix?: boolean;
|
|
41
|
+
}): () => void;
|
|
42
|
+
/** The route of a path, or undefined. */
|
|
43
|
+
match(path: string): WebSocketUpgradeHandler | undefined;
|
|
44
|
+
protected dispatch(request: IncomingMessage, socket: Duplex, head: Buffer): void;
|
|
45
|
+
}
|
|
46
|
+
export {};
|