@replit/river 0.6.4 → 0.7.1
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 +4 -4
- package/dist/__tests__/e2e.test.js +7 -8
- package/dist/__tests__/typescript-stress.test.d.ts +4 -2
- package/dist/__tests__/typescript-stress.test.d.ts.map +1 -1
- package/dist/__tests__/typescript-stress.test.js +3 -1
- package/dist/router/client.d.ts +3 -2
- package/dist/router/client.d.ts.map +1 -1
- package/dist/router/client.js +5 -5
- package/dist/router/server.d.ts +2 -2
- package/dist/router/server.d.ts.map +1 -1
- package/dist/router/server.js +10 -10
- package/dist/testUtils.d.ts +6 -5
- package/dist/testUtils.d.ts.map +1 -1
- package/dist/testUtils.js +7 -16
- package/dist/transport/impls/{stdio.d.ts → stdio/stdio.d.ts} +17 -20
- package/dist/transport/impls/stdio/stdio.d.ts.map +1 -0
- package/dist/transport/impls/stdio/stdio.js +80 -0
- package/dist/transport/impls/stdio/stdio.test.d.ts.map +1 -0
- package/dist/transport/impls/{stdio.test.js → stdio/stdio.test.js} +5 -5
- package/dist/transport/impls/ws/client.d.ts +45 -0
- package/dist/transport/impls/ws/client.d.ts.map +1 -0
- package/dist/transport/impls/ws/client.js +102 -0
- package/dist/transport/impls/ws/connection.d.ts +11 -0
- package/dist/transport/impls/ws/connection.d.ts.map +1 -0
- package/dist/transport/impls/ws/connection.js +22 -0
- package/dist/transport/impls/ws/server.d.ts +19 -0
- package/dist/transport/impls/ws/server.d.ts.map +1 -0
- package/dist/transport/impls/ws/server.js +53 -0
- package/dist/transport/impls/ws/ws.test.d.ts.map +1 -0
- package/dist/transport/impls/{ws.test.js → ws/ws.test.js} +38 -5
- package/dist/transport/index.d.ts +3 -3
- package/dist/transport/index.d.ts.map +1 -1
- package/dist/transport/index.js +6 -3
- package/dist/transport/message.d.ts +4 -1
- package/dist/transport/message.d.ts.map +1 -1
- package/dist/transport/message.js +3 -0
- package/dist/transport/transport.d.ts +132 -0
- package/dist/transport/transport.d.ts.map +1 -0
- package/dist/transport/transport.js +241 -0
- package/package.json +4 -3
- package/dist/__tests__/integration.test.d.ts +0 -50
- package/dist/__tests__/integration.test.js +0 -193
- package/dist/transport/impls/stdio.d.ts.map +0 -1
- package/dist/transport/impls/stdio.js +0 -56
- package/dist/transport/impls/stdio.test.d.ts.map +0 -1
- package/dist/transport/impls/ws.d.ts +0 -71
- package/dist/transport/impls/ws.d.ts.map +0 -1
- package/dist/transport/impls/ws.js +0 -156
- package/dist/transport/impls/ws.test.d.ts.map +0 -1
- package/dist/transport/types.d.ts +0 -50
- package/dist/transport/types.d.ts.map +0 -1
- package/dist/transport/types.js +0 -95
- /package/dist/transport/impls/{stdio.test.d.ts → stdio/stdio.test.d.ts} +0 -0
- /package/dist/transport/impls/{ws.test.d.ts → ws/ws.test.d.ts} +0 -0
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { Value } from '@sinclair/typebox/value';
|
|
2
|
+
import { OpaqueTransportMessageSchema, TransportAckSchema, isAck, reply, } from './message';
|
|
3
|
+
import { log } from '../logging';
|
|
4
|
+
/**
|
|
5
|
+
* Abstract base for a connection between two nodes in a River network.
|
|
6
|
+
* A connection is responsible for sending and receiving messages on a 1:1
|
|
7
|
+
* basis between nodes.
|
|
8
|
+
* Connections can be reused across different transports.
|
|
9
|
+
* @abstract
|
|
10
|
+
*/
|
|
11
|
+
export class Connection {
|
|
12
|
+
connectedTo;
|
|
13
|
+
transport;
|
|
14
|
+
constructor(transport, connectedTo) {
|
|
15
|
+
this.connectedTo = connectedTo;
|
|
16
|
+
this.transport = transport;
|
|
17
|
+
}
|
|
18
|
+
onMessage(msg) {
|
|
19
|
+
return this.transport.onMessage(msg);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Abstract base for a transport layer for communication between nodes in a River network.
|
|
24
|
+
* A transport is responsible for handling the 1:n connection logic between nodes and
|
|
25
|
+
* delegating sending/receiving to connections.
|
|
26
|
+
* Any River transport methods need to implement this interface.
|
|
27
|
+
* @abstract
|
|
28
|
+
*/
|
|
29
|
+
export class Transport {
|
|
30
|
+
/**
|
|
31
|
+
* A flag indicating whether the transport has been destroyed.
|
|
32
|
+
* A destroyed transport will not attempt to reconnect and cannot be used again.
|
|
33
|
+
*/
|
|
34
|
+
state;
|
|
35
|
+
/**
|
|
36
|
+
* The {@link Codec} used to encode and decode messages.
|
|
37
|
+
*/
|
|
38
|
+
codec;
|
|
39
|
+
/**
|
|
40
|
+
* The client ID of this transport.
|
|
41
|
+
*/
|
|
42
|
+
clientId;
|
|
43
|
+
/**
|
|
44
|
+
* The set of message handlers registered with this transport.
|
|
45
|
+
*/
|
|
46
|
+
messageHandlers;
|
|
47
|
+
/**
|
|
48
|
+
* An array of message IDs that are waiting to be sent over the WebSocket connection.
|
|
49
|
+
* This builds up if the WebSocket is down for a period of time.
|
|
50
|
+
*/
|
|
51
|
+
sendQueue;
|
|
52
|
+
/**
|
|
53
|
+
* The buffer of messages that have been sent but not yet acknowledged.
|
|
54
|
+
*/
|
|
55
|
+
sendBuffer;
|
|
56
|
+
/**
|
|
57
|
+
* The map of {@link Connection}s managed by this transport.
|
|
58
|
+
*/
|
|
59
|
+
connections;
|
|
60
|
+
/**
|
|
61
|
+
* Creates a new Transport instance.
|
|
62
|
+
* @param codec The codec used to encode and decode messages.
|
|
63
|
+
* @param clientId The client ID of this transport.
|
|
64
|
+
*/
|
|
65
|
+
constructor(codec, clientId) {
|
|
66
|
+
this.messageHandlers = new Set();
|
|
67
|
+
this.sendBuffer = new Map();
|
|
68
|
+
this.sendQueue = new Map();
|
|
69
|
+
this.connections = new Map();
|
|
70
|
+
this.codec = codec;
|
|
71
|
+
this.clientId = clientId;
|
|
72
|
+
this.state = 'open';
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The downstream implementation needs to call this when a new connection is established.
|
|
76
|
+
* @param conn The connection object.
|
|
77
|
+
*/
|
|
78
|
+
onConnect(conn) {
|
|
79
|
+
log?.info(`${this.clientId} -- new connection to ${conn.connectedTo}`);
|
|
80
|
+
this.connections.set(conn.connectedTo, conn);
|
|
81
|
+
// send outstanding
|
|
82
|
+
const outstanding = this.sendQueue.get(conn.connectedTo);
|
|
83
|
+
if (!outstanding) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
for (const id of outstanding) {
|
|
87
|
+
const msg = this.sendBuffer.get(id);
|
|
88
|
+
if (!msg) {
|
|
89
|
+
log?.warn(`${this.clientId} -- tried to resend a message we received an ack for`);
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
this.send(msg);
|
|
93
|
+
}
|
|
94
|
+
this.sendQueue.set(conn.connectedTo, []);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* The downstream implementation needs to call this when a connection is closed.
|
|
98
|
+
* @param conn The connection object.
|
|
99
|
+
*/
|
|
100
|
+
onDisconnect(conn) {
|
|
101
|
+
log?.info(`${this.clientId} -- disconnect from ${conn.connectedTo}`);
|
|
102
|
+
conn.close();
|
|
103
|
+
this.connections.delete(conn.connectedTo);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Handles a message received by this transport. Thin wrapper around {@link handleMsg} and {@link parseMsg}.
|
|
107
|
+
* @param msg The message to handle.
|
|
108
|
+
*/
|
|
109
|
+
onMessage(msg) {
|
|
110
|
+
return this.handleMsg(this.parseMsg(msg));
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Parses a message from a Uint8Array into a {@link OpaqueTransportMessage}.
|
|
114
|
+
* @param msg The message to parse.
|
|
115
|
+
* @returns The parsed message, or null if the message is malformed or invalid.
|
|
116
|
+
*/
|
|
117
|
+
parseMsg(msg) {
|
|
118
|
+
const parsedMsg = this.codec.fromBuffer(msg);
|
|
119
|
+
if (parsedMsg === null) {
|
|
120
|
+
const decodedBuffer = new TextDecoder().decode(msg);
|
|
121
|
+
log?.warn(`${this.clientId} -- received malformed msg: ${decodedBuffer}`);
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
if (Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
|
|
125
|
+
return parsedMsg;
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
log?.warn(`${this.clientId} -- received invalid msg: ${JSON.stringify(msg)}`);
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Called when a message is received by this transport.
|
|
134
|
+
* You generally shouldn't need to override this in downstream transport implementations.
|
|
135
|
+
* @param msg The received message.
|
|
136
|
+
*/
|
|
137
|
+
handleMsg(msg) {
|
|
138
|
+
if (!msg) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (isAck(msg.controlFlags) && Value.Check(TransportAckSchema, msg)) {
|
|
142
|
+
// process ack
|
|
143
|
+
log?.info(`${this.clientId} -- received ack: ${JSON.stringify(msg)}`);
|
|
144
|
+
if (this.sendBuffer.has(msg.payload.ack)) {
|
|
145
|
+
this.sendBuffer.delete(msg.payload.ack);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
// regular river message
|
|
150
|
+
log?.info(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
|
|
151
|
+
if (msg.to !== this.clientId) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
for (const handler of this.messageHandlers) {
|
|
155
|
+
handler(msg);
|
|
156
|
+
}
|
|
157
|
+
if (!isAck(msg.controlFlags)) {
|
|
158
|
+
const ackMsg = reply(msg, { ack: msg.id });
|
|
159
|
+
ackMsg.controlFlags = 1 /* ControlFlags.AckBit */;
|
|
160
|
+
ackMsg.from = this.clientId;
|
|
161
|
+
this.send(ackMsg);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Adds a message listener to this transport.
|
|
167
|
+
* @param handler The message handler to add.
|
|
168
|
+
*/
|
|
169
|
+
addMessageListener(handler) {
|
|
170
|
+
this.messageHandlers.add(handler);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Removes a message listener from this transport.
|
|
174
|
+
* @param handler The message handler to remove.
|
|
175
|
+
*/
|
|
176
|
+
removeMessageListener(handler) {
|
|
177
|
+
this.messageHandlers.delete(handler);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Sends a message over this transport, delegating to the appropriate connection to actually
|
|
181
|
+
* send the message.
|
|
182
|
+
* @param msg The message to send.
|
|
183
|
+
* @returns The ID of the sent message.
|
|
184
|
+
*/
|
|
185
|
+
send(msg) {
|
|
186
|
+
if (this.state === 'destroyed') {
|
|
187
|
+
const err = 'transport is destroyed, cant send';
|
|
188
|
+
log?.error(`${this.clientId} -- ` + err + `: ${JSON.stringify(msg)}`);
|
|
189
|
+
throw new Error(err);
|
|
190
|
+
}
|
|
191
|
+
else if (this.state === 'closed') {
|
|
192
|
+
log?.info(`${this.clientId} -- transport closed when sending, discarding : ${JSON.stringify(msg)}`);
|
|
193
|
+
return msg.id;
|
|
194
|
+
}
|
|
195
|
+
let conn = this.connections.get(msg.to);
|
|
196
|
+
// we only use sendBuffer to track messages that we expect an ack from,
|
|
197
|
+
// messages with the ack flag are not responded to
|
|
198
|
+
if (!isAck(msg.controlFlags)) {
|
|
199
|
+
this.sendBuffer.set(msg.id, msg);
|
|
200
|
+
}
|
|
201
|
+
if (conn) {
|
|
202
|
+
log?.info(`${this.clientId} -- sending ${JSON.stringify(msg)}`);
|
|
203
|
+
const ok = conn.send(this.codec.toBuffer(msg));
|
|
204
|
+
if (ok) {
|
|
205
|
+
return msg.id;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
log?.info(`${this.clientId} -- connection to ${msg.to} not ready, attempting reconnect and queuing ${JSON.stringify(msg)}`);
|
|
209
|
+
const outstanding = this.sendQueue.get(msg.to) || [];
|
|
210
|
+
outstanding.push(msg.id);
|
|
211
|
+
this.sendQueue.set(msg.to, outstanding);
|
|
212
|
+
this.createNewConnection(msg.to);
|
|
213
|
+
return msg.id;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Default close implementation for transports. You should override this in the downstream
|
|
217
|
+
* implementation if you need to do any additional cleanup and call super.close() at the end.
|
|
218
|
+
* Closes the transport. Any messages sent while the transport is closed will be silently discarded.
|
|
219
|
+
*/
|
|
220
|
+
async close() {
|
|
221
|
+
for (const conn of this.connections.values()) {
|
|
222
|
+
conn.close();
|
|
223
|
+
}
|
|
224
|
+
this.connections.clear();
|
|
225
|
+
this.state = 'closed';
|
|
226
|
+
log?.info(`${this.clientId} -- closed transport`);
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Default destroy implementation for transports. You should override this in the downstream
|
|
230
|
+
* implementation if you need to do any additional cleanup and call super.destroy() at the end.
|
|
231
|
+
* Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
|
|
232
|
+
*/
|
|
233
|
+
async destroy() {
|
|
234
|
+
for (const conn of this.connections.values()) {
|
|
235
|
+
conn.close();
|
|
236
|
+
}
|
|
237
|
+
this.connections.clear();
|
|
238
|
+
this.state = 'destroyed';
|
|
239
|
+
log?.info(`${this.clientId} -- destroyed transport`);
|
|
240
|
+
}
|
|
241
|
+
}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@replit/river",
|
|
3
3
|
"sideEffects": false,
|
|
4
4
|
"description": "It's like tRPC but... with JSON Schema Support, duplex streaming and support for service multiplexing. Transport agnostic!",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "0.7.1",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": "./dist/router/index.js",
|
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
"./codec": "./dist/codec/index.js",
|
|
11
11
|
"./test-util": "./dist/testUtils.js",
|
|
12
12
|
"./transport": "./dist/transport/index.js",
|
|
13
|
-
"./transport/ws": "./dist/transport/impls/ws.js",
|
|
13
|
+
"./transport/ws/client": "./dist/transport/impls/ws/client.js",
|
|
14
|
+
"./transport/ws/server": "./dist/transport/impls/ws/server.js",
|
|
14
15
|
"./transport/stdio": "./dist/transport/impls/stdio.js"
|
|
15
16
|
},
|
|
16
17
|
"files": [
|
|
@@ -34,7 +35,7 @@
|
|
|
34
35
|
"scripts": {
|
|
35
36
|
"check": "tsc --noEmit && npx prettier . --check",
|
|
36
37
|
"format": "npx prettier . --write",
|
|
37
|
-
"build": "tsc",
|
|
38
|
+
"build": "rm -rf ./dist && tsc",
|
|
38
39
|
"prepack": "npm run build",
|
|
39
40
|
"release": "npm publish --access public",
|
|
40
41
|
"test": "vitest",
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
export declare const EchoRequest: import("@sinclair/typebox").TObject<{
|
|
2
|
-
msg: import("@sinclair/typebox").TString;
|
|
3
|
-
ignore: import("@sinclair/typebox").TBoolean;
|
|
4
|
-
}>;
|
|
5
|
-
export declare const EchoResponse: import("@sinclair/typebox").TObject<{
|
|
6
|
-
response: import("@sinclair/typebox").TString;
|
|
7
|
-
}>;
|
|
8
|
-
export declare const TestServiceConstructor: () => {
|
|
9
|
-
name: "test";
|
|
10
|
-
state: {
|
|
11
|
-
count: number;
|
|
12
|
-
};
|
|
13
|
-
procedures: {
|
|
14
|
-
add: {
|
|
15
|
-
input: import("@sinclair/typebox").TObject<{
|
|
16
|
-
n: import("@sinclair/typebox").TNumber;
|
|
17
|
-
}>;
|
|
18
|
-
output: import("@sinclair/typebox").TObject<{
|
|
19
|
-
result: import("@sinclair/typebox").TNumber;
|
|
20
|
-
}>;
|
|
21
|
-
handler: (context: import("../router").ServiceContextWithState<{
|
|
22
|
-
count: number;
|
|
23
|
-
}>, input: import("../transport/message").TransportMessage<{
|
|
24
|
-
n: number;
|
|
25
|
-
}>) => Promise<import("../transport/message").TransportMessage<{
|
|
26
|
-
result: number;
|
|
27
|
-
}>>;
|
|
28
|
-
type: "rpc";
|
|
29
|
-
};
|
|
30
|
-
} & {
|
|
31
|
-
echo: {
|
|
32
|
-
input: import("@sinclair/typebox").TObject<{
|
|
33
|
-
msg: import("@sinclair/typebox").TString;
|
|
34
|
-
ignore: import("@sinclair/typebox").TBoolean;
|
|
35
|
-
}>;
|
|
36
|
-
output: import("@sinclair/typebox").TObject<{
|
|
37
|
-
response: import("@sinclair/typebox").TString;
|
|
38
|
-
}>;
|
|
39
|
-
handler: (context: import("../router").ServiceContextWithState<{
|
|
40
|
-
count: number;
|
|
41
|
-
}>, input: AsyncIterable<import("../transport/message").TransportMessage<{
|
|
42
|
-
msg: string;
|
|
43
|
-
ignore: boolean;
|
|
44
|
-
}>>, output: import("it-pushable").Pushable<import("../transport/message").TransportMessage<{
|
|
45
|
-
response: string;
|
|
46
|
-
}>, void, unknown>) => Promise<void>;
|
|
47
|
-
type: "stream";
|
|
48
|
-
};
|
|
49
|
-
};
|
|
50
|
-
};
|
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
import http from 'http';
|
|
2
|
-
import { Type } from '@sinclair/typebox';
|
|
3
|
-
import { ServiceBuilder, serializeService } from '../router/builder';
|
|
4
|
-
import { reply } from '../transport/message';
|
|
5
|
-
import { afterAll, describe, expect, test } from 'vitest';
|
|
6
|
-
import { createWebSocketServer, createWsTransports, onServerReady, asClientRpc, asClientStream, } from '../testUtils';
|
|
7
|
-
import { createServer } from '../router/server';
|
|
8
|
-
import { createClient } from '../router/client';
|
|
9
|
-
export const EchoRequest = Type.Object({
|
|
10
|
-
msg: Type.String(),
|
|
11
|
-
ignore: Type.Boolean(),
|
|
12
|
-
});
|
|
13
|
-
export const EchoResponse = Type.Object({ response: Type.String() });
|
|
14
|
-
export const TestServiceConstructor = () => ServiceBuilder.create('test')
|
|
15
|
-
.initialState({
|
|
16
|
-
count: 0,
|
|
17
|
-
})
|
|
18
|
-
.defineProcedure('add', {
|
|
19
|
-
type: 'rpc',
|
|
20
|
-
input: Type.Object({ n: Type.Number() }),
|
|
21
|
-
output: Type.Object({ result: Type.Number() }),
|
|
22
|
-
async handler(ctx, msg) {
|
|
23
|
-
const { n } = msg.payload;
|
|
24
|
-
ctx.state.count += n;
|
|
25
|
-
return reply(msg, { result: ctx.state.count });
|
|
26
|
-
},
|
|
27
|
-
})
|
|
28
|
-
.defineProcedure('echo', {
|
|
29
|
-
type: 'stream',
|
|
30
|
-
input: EchoRequest,
|
|
31
|
-
output: EchoResponse,
|
|
32
|
-
async handler(_ctx, msgStream, returnStream) {
|
|
33
|
-
for await (const msg of msgStream) {
|
|
34
|
-
const req = msg.payload;
|
|
35
|
-
if (!req.ignore) {
|
|
36
|
-
returnStream.push(reply(msg, { response: req.msg }));
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
})
|
|
41
|
-
.finalize();
|
|
42
|
-
const OrderingServiceConstructor = () => ServiceBuilder.create('test')
|
|
43
|
-
.initialState({
|
|
44
|
-
msgs: [],
|
|
45
|
-
})
|
|
46
|
-
.defineProcedure('add', {
|
|
47
|
-
type: 'rpc',
|
|
48
|
-
input: Type.Object({ n: Type.Number() }),
|
|
49
|
-
output: Type.Object({ ok: Type.Boolean() }),
|
|
50
|
-
async handler(ctx, msg) {
|
|
51
|
-
const { n } = msg.payload;
|
|
52
|
-
ctx.state.msgs.push(n);
|
|
53
|
-
return reply(msg, { ok: true });
|
|
54
|
-
},
|
|
55
|
-
})
|
|
56
|
-
.defineProcedure('getAll', {
|
|
57
|
-
type: 'rpc',
|
|
58
|
-
input: Type.Object({}),
|
|
59
|
-
output: Type.Object({ msgs: Type.Array(Type.Number()) }),
|
|
60
|
-
async handler(ctx, msg) {
|
|
61
|
-
return reply(msg, { msgs: ctx.state.msgs });
|
|
62
|
-
},
|
|
63
|
-
})
|
|
64
|
-
.finalize();
|
|
65
|
-
test('serialize service to jsonschema', () => {
|
|
66
|
-
const service = TestServiceConstructor();
|
|
67
|
-
expect(serializeService(service)).toStrictEqual({
|
|
68
|
-
name: 'test',
|
|
69
|
-
state: { count: 0 },
|
|
70
|
-
procedures: {
|
|
71
|
-
add: {
|
|
72
|
-
input: {
|
|
73
|
-
properties: {
|
|
74
|
-
n: { type: 'number' },
|
|
75
|
-
},
|
|
76
|
-
required: ['n'],
|
|
77
|
-
type: 'object',
|
|
78
|
-
},
|
|
79
|
-
output: {
|
|
80
|
-
properties: {
|
|
81
|
-
result: { type: 'number' },
|
|
82
|
-
},
|
|
83
|
-
required: ['result'],
|
|
84
|
-
type: 'object',
|
|
85
|
-
},
|
|
86
|
-
type: 'rpc',
|
|
87
|
-
},
|
|
88
|
-
echo: {
|
|
89
|
-
input: {
|
|
90
|
-
properties: {
|
|
91
|
-
msg: { type: 'string' },
|
|
92
|
-
ignore: { type: 'boolean' },
|
|
93
|
-
},
|
|
94
|
-
required: ['msg', 'ignore'],
|
|
95
|
-
type: 'object',
|
|
96
|
-
},
|
|
97
|
-
output: {
|
|
98
|
-
properties: {
|
|
99
|
-
response: { type: 'string' },
|
|
100
|
-
},
|
|
101
|
-
required: ['response'],
|
|
102
|
-
type: 'object',
|
|
103
|
-
},
|
|
104
|
-
type: 'stream',
|
|
105
|
-
},
|
|
106
|
-
},
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
describe('server-side test', () => {
|
|
110
|
-
const service = TestServiceConstructor();
|
|
111
|
-
const initialState = { count: 0 };
|
|
112
|
-
test('rpc basic', async () => {
|
|
113
|
-
const add = asClientRpc(initialState, service.procedures.add);
|
|
114
|
-
await expect(add({ n: 3 })).resolves.toStrictEqual({ result: 3 });
|
|
115
|
-
});
|
|
116
|
-
test('rpc initial state', async () => {
|
|
117
|
-
const add = asClientRpc({ count: 5 }, service.procedures.add);
|
|
118
|
-
await expect(add({ n: 6 })).resolves.toStrictEqual({ result: 11 });
|
|
119
|
-
});
|
|
120
|
-
test('stream basic', async () => {
|
|
121
|
-
const [input, output] = asClientStream(initialState, service.procedures.echo);
|
|
122
|
-
input.push({ msg: 'abc', ignore: false });
|
|
123
|
-
input.push({ msg: 'def', ignore: true });
|
|
124
|
-
input.push({ msg: 'ghi', ignore: false });
|
|
125
|
-
input.end();
|
|
126
|
-
await expect(output.next().then((res) => res.value)).resolves.toStrictEqual({
|
|
127
|
-
response: 'abc',
|
|
128
|
-
});
|
|
129
|
-
await expect(output.next().then((res) => res.value)).resolves.toStrictEqual({
|
|
130
|
-
response: 'ghi',
|
|
131
|
-
});
|
|
132
|
-
expect(output.readableLength).toBe(0);
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
describe('client <-> server integration test', async () => {
|
|
136
|
-
const server = http.createServer();
|
|
137
|
-
const port = await onServerReady(server);
|
|
138
|
-
const webSocketServer = await createWebSocketServer(server);
|
|
139
|
-
afterAll(() => {
|
|
140
|
-
webSocketServer.clients.forEach((socket) => {
|
|
141
|
-
socket.close();
|
|
142
|
-
});
|
|
143
|
-
server.close();
|
|
144
|
-
});
|
|
145
|
-
test('rpc', async () => {
|
|
146
|
-
const [clientTransport, serverTransport] = createWsTransports(port, webSocketServer);
|
|
147
|
-
const serviceDefs = { test: TestServiceConstructor() };
|
|
148
|
-
const server = await createServer(serverTransport, serviceDefs);
|
|
149
|
-
const client = createClient(clientTransport);
|
|
150
|
-
await expect(client.test.add({ n: 3 })).resolves.toStrictEqual({
|
|
151
|
-
result: 3,
|
|
152
|
-
});
|
|
153
|
-
});
|
|
154
|
-
test('stream', async () => {
|
|
155
|
-
const [clientTransport, serverTransport] = createWsTransports(port, webSocketServer);
|
|
156
|
-
const serviceDefs = { test: TestServiceConstructor() };
|
|
157
|
-
const server = await createServer(serverTransport, serviceDefs);
|
|
158
|
-
const client = createClient(clientTransport);
|
|
159
|
-
const [input, output, close] = await client.test.echo();
|
|
160
|
-
input.push({ msg: 'abc', ignore: false });
|
|
161
|
-
input.push({ msg: 'def', ignore: true });
|
|
162
|
-
input.push({ msg: 'ghi', ignore: false });
|
|
163
|
-
input.end();
|
|
164
|
-
await expect(output.next().then((res) => res.value)).resolves.toStrictEqual({
|
|
165
|
-
response: 'abc',
|
|
166
|
-
});
|
|
167
|
-
await expect(output.next().then((res) => res.value)).resolves.toStrictEqual({
|
|
168
|
-
response: 'ghi',
|
|
169
|
-
});
|
|
170
|
-
close();
|
|
171
|
-
});
|
|
172
|
-
test('message order is preserved in the face of disconnects', async () => {
|
|
173
|
-
const [clientTransport, serverTransport] = createWsTransports(port, webSocketServer);
|
|
174
|
-
const serviceDefs = { test: OrderingServiceConstructor() };
|
|
175
|
-
const server = await createServer(serverTransport, serviceDefs);
|
|
176
|
-
const client = createClient(clientTransport);
|
|
177
|
-
const expected = [];
|
|
178
|
-
for (let i = 0; i < 50; i++) {
|
|
179
|
-
expected.push(i);
|
|
180
|
-
if (i == 10) {
|
|
181
|
-
clientTransport.ws?.close();
|
|
182
|
-
}
|
|
183
|
-
if (i == 42) {
|
|
184
|
-
clientTransport.ws?.terminate();
|
|
185
|
-
}
|
|
186
|
-
await client.test.add({
|
|
187
|
-
n: i,
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
const res = await client.test.getAll({});
|
|
191
|
-
return expect(res.msgs).toStrictEqual(expected);
|
|
192
|
-
});
|
|
193
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"stdio.d.ts","sourceRoot":"","sources":["../../../transport/impls/stdio.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAEpC,OAAO,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AACvE,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAGrC,UAAU,OAAO;IACf,KAAK,EAAE,KAAK,CAAC;CACd;AAQD;;;GAGG;AACH,qBAAa,cAAe,SAAQ,SAAS;IAC3C;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC,cAAc,CAAC;IAC7B;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC,cAAc,CAAC;IAE9B;;;;;OAKG;gBAED,QAAQ,EAAE,iBAAiB,EAC3B,KAAK,GAAE,MAAM,CAAC,cAA8B,EAC5C,MAAM,GAAE,MAAM,CAAC,cAA+B,EAC9C,eAAe,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC;IAcpC;;;;OAIG;IACH,IAAI,CAAC,GAAG,EAAE,sBAAsB,GAAG,MAAM;IAWzC;;OAEG;IACG,KAAK;CACZ"}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { NaiveJsonCodec } from '../../codec/json';
|
|
2
|
-
import { Transport } from '../types';
|
|
3
|
-
import readline from 'readline';
|
|
4
|
-
const defaultOptions = {
|
|
5
|
-
codec: NaiveJsonCodec,
|
|
6
|
-
};
|
|
7
|
-
const newlineBuff = new TextEncoder().encode('\n');
|
|
8
|
-
/**
|
|
9
|
-
* A transport implementation that uses standard input and output streams.
|
|
10
|
-
* @extends Transport
|
|
11
|
-
*/
|
|
12
|
-
export class StdioTransport extends Transport {
|
|
13
|
-
/**
|
|
14
|
-
* The readable stream to use as input.
|
|
15
|
-
*/
|
|
16
|
-
input;
|
|
17
|
-
/**
|
|
18
|
-
* The writable stream to use as output.
|
|
19
|
-
*/
|
|
20
|
-
output;
|
|
21
|
-
/**
|
|
22
|
-
* Constructs a new StdioTransport instance.
|
|
23
|
-
* @param clientId - The ID of the client associated with this transport.
|
|
24
|
-
* @param input - The readable stream to use as input. Defaults to process.stdin.
|
|
25
|
-
* @param output - The writable stream to use as output. Defaults to process.stdout.
|
|
26
|
-
*/
|
|
27
|
-
constructor(clientId, input = process.stdin, output = process.stdout, providedOptions) {
|
|
28
|
-
const options = { ...defaultOptions, ...providedOptions };
|
|
29
|
-
super(options.codec, clientId);
|
|
30
|
-
this.input = input;
|
|
31
|
-
this.output = output;
|
|
32
|
-
const rl = readline.createInterface({
|
|
33
|
-
input: this.input,
|
|
34
|
-
});
|
|
35
|
-
const encoder = new TextEncoder();
|
|
36
|
-
rl.on('line', (msg) => this.onMessage(encoder.encode(msg)));
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Sends a message over the transport.
|
|
40
|
-
* @param msg - The message to send.
|
|
41
|
-
* @returns The ID of the sent message.
|
|
42
|
-
*/
|
|
43
|
-
send(msg) {
|
|
44
|
-
const id = msg.id;
|
|
45
|
-
const payload = this.codec.toBuffer(msg);
|
|
46
|
-
const out = new Uint8Array(payload.length + newlineBuff.length);
|
|
47
|
-
out.set(payload, 0);
|
|
48
|
-
out.set(newlineBuff, payload.length);
|
|
49
|
-
this.output.write(out);
|
|
50
|
-
return id;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Closes the transport.
|
|
54
|
-
*/
|
|
55
|
-
async close() { }
|
|
56
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"stdio.test.d.ts","sourceRoot":"","sources":["../../../transport/impls/stdio.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/// <reference types="ws" />
|
|
2
|
-
import WebSocket from 'isomorphic-ws';
|
|
3
|
-
import { Transport } from '../types';
|
|
4
|
-
import { MessageId, OpaqueTransportMessage, TransportClientId } from '../message';
|
|
5
|
-
import { type Codec } from '../../codec';
|
|
6
|
-
interface Options {
|
|
7
|
-
retryIntervalMs: number;
|
|
8
|
-
codec: Codec;
|
|
9
|
-
binaryType: 'arraybuffer' | 'blob';
|
|
10
|
-
}
|
|
11
|
-
type WebSocketResult = {
|
|
12
|
-
ws: WebSocket;
|
|
13
|
-
} | {
|
|
14
|
-
err: string;
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* A transport implementation that uses a WebSocket connection with automatic reconnection.
|
|
18
|
-
* @class
|
|
19
|
-
* @extends Transport
|
|
20
|
-
*/
|
|
21
|
-
export declare class WebSocketTransport extends Transport {
|
|
22
|
-
/**
|
|
23
|
-
* A function that returns a Promise that resolves to a WebSocket instance.
|
|
24
|
-
*/
|
|
25
|
-
wsGetter: () => Promise<WebSocket>;
|
|
26
|
-
ws?: WebSocket;
|
|
27
|
-
options: Options;
|
|
28
|
-
/**
|
|
29
|
-
* A flag indicating whether the transport has been destroyed.
|
|
30
|
-
* A destroyed transport will not attempt to reconnect and cannot be used again.
|
|
31
|
-
*/
|
|
32
|
-
state: 'open' | 'closed' | 'destroyed';
|
|
33
|
-
/**
|
|
34
|
-
* An ongoing reconnect attempt if it exists. When the attempt finishes, it contains a
|
|
35
|
-
* {@link WebSocketResult} object when a connection is established or an error occurs.
|
|
36
|
-
*/
|
|
37
|
-
reconnectPromise?: Promise<WebSocketResult>;
|
|
38
|
-
/**
|
|
39
|
-
* An array of message IDs that are waiting to be sent over the WebSocket connection.
|
|
40
|
-
* This builds up if the WebSocket is down for a period of time.
|
|
41
|
-
*/
|
|
42
|
-
sendQueue: Array<MessageId>;
|
|
43
|
-
/**
|
|
44
|
-
* Creates a new WebSocketTransport instance.
|
|
45
|
-
* @param wsGetter A function that returns a Promise that resolves to a WebSocket instance.
|
|
46
|
-
* @param clientId The ID of the client using the transport.
|
|
47
|
-
* @param providedOptions An optional object containing configuration options for the transport.
|
|
48
|
-
*/
|
|
49
|
-
constructor(wsGetter: () => Promise<WebSocket>, clientId: TransportClientId, providedOptions?: Partial<Options>);
|
|
50
|
-
/**
|
|
51
|
-
* Begins a new attempt to establish a WebSocket connection.
|
|
52
|
-
*/
|
|
53
|
-
private tryConnect;
|
|
54
|
-
/**
|
|
55
|
-
* Sends a message over the WebSocket connection. If the WebSocket connection is
|
|
56
|
-
* not healthy, it will queue until the connection is successful.
|
|
57
|
-
* @param msg The message to send.
|
|
58
|
-
* @returns The ID of the sent message.
|
|
59
|
-
*/
|
|
60
|
-
send(msg: OpaqueTransportMessage): MessageId;
|
|
61
|
-
/**
|
|
62
|
-
* Closes the WebSocket transport. Any messages sent while the transport is closed will be silently discarded.
|
|
63
|
-
*/
|
|
64
|
-
close(): Promise<void | undefined>;
|
|
65
|
-
/**
|
|
66
|
-
* Destroys the WebSocket transport. Any messages sent while the transport is closed will throw an error.
|
|
67
|
-
*/
|
|
68
|
-
destroy(): Promise<void | undefined>;
|
|
69
|
-
}
|
|
70
|
-
export {};
|
|
71
|
-
//# sourceMappingURL=ws.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ws.d.ts","sourceRoot":"","sources":["../../../transport/impls/ws.ts"],"names":[],"mappings":";AAAA,OAAO,SAAS,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAErC,OAAO,EACL,SAAS,EACT,sBAAsB,EACtB,iBAAiB,EAClB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,aAAa,CAAC;AAEzC,UAAU,OAAO;IACf,eAAe,EAAE,MAAM,CAAC;IACxB,KAAK,EAAE,KAAK,CAAC;IACb,UAAU,EAAE,aAAa,GAAG,MAAM,CAAC;CACpC;AAQD,KAAK,eAAe,GAAG;IAAE,EAAE,EAAE,SAAS,CAAA;CAAE,GAAG;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC;AAE3D;;;;GAIG;AACH,qBAAa,kBAAmB,SAAQ,SAAS;IAC/C;;OAEG;IACH,QAAQ,EAAE,MAAM,OAAO,CAAC,SAAS,CAAC,CAAC;IACnC,EAAE,CAAC,EAAE,SAAS,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;IAEjB;;;OAGG;IACH,KAAK,EAAE,MAAM,GAAG,QAAQ,GAAG,WAAW,CAAC;IAEvC;;;OAGG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC,eAAe,CAAC,CAAC;IAE5C;;;OAGG;IACH,SAAS,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC;IAE5B;;;;;OAKG;gBAED,QAAQ,EAAE,MAAM,OAAO,CAAC,SAAS,CAAC,EAClC,QAAQ,EAAE,iBAAiB,EAC3B,eAAe,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC;IAWpC;;OAEG;YACW,UAAU;IAuExB;;;;;OAKG;IACH,IAAI,CAAC,GAAG,EAAE,sBAAsB,GAAG,SAAS;IA4B5C;;OAEG;IACG,KAAK;IAMX;;OAEG;IACG,OAAO;CAKd"}
|