@replit/river 0.6.3 → 0.7.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.
Files changed (46) hide show
  1. package/dist/__tests__/e2e.test.js +7 -8
  2. package/dist/__tests__/typescript-stress.test.d.ts +4 -2
  3. package/dist/__tests__/typescript-stress.test.d.ts.map +1 -1
  4. package/dist/__tests__/typescript-stress.test.js +3 -1
  5. package/dist/router/client.d.ts +3 -2
  6. package/dist/router/client.d.ts.map +1 -1
  7. package/dist/router/client.js +5 -5
  8. package/dist/router/server.d.ts +2 -2
  9. package/dist/router/server.d.ts.map +1 -1
  10. package/dist/router/server.js +10 -10
  11. package/dist/testUtils.d.ts +6 -5
  12. package/dist/testUtils.d.ts.map +1 -1
  13. package/dist/testUtils.js +7 -16
  14. package/dist/transport/impls/stdio/stdio.d.ts +37 -0
  15. package/dist/transport/impls/stdio/stdio.d.ts.map +1 -0
  16. package/dist/transport/impls/stdio/stdio.js +80 -0
  17. package/dist/transport/impls/stdio/stdio.test.d.ts +2 -0
  18. package/dist/transport/impls/stdio/stdio.test.d.ts.map +1 -0
  19. package/dist/transport/impls/stdio/stdio.test.js +20 -0
  20. package/dist/transport/impls/ws/client.d.ts +45 -0
  21. package/dist/transport/impls/ws/client.d.ts.map +1 -0
  22. package/dist/transport/impls/ws/client.js +102 -0
  23. package/dist/transport/impls/ws/connection.d.ts +11 -0
  24. package/dist/transport/impls/ws/connection.d.ts.map +1 -0
  25. package/dist/transport/impls/ws/connection.js +22 -0
  26. package/dist/transport/impls/ws/server.d.ts +19 -0
  27. package/dist/transport/impls/ws/server.d.ts.map +1 -0
  28. package/dist/transport/impls/ws/server.js +53 -0
  29. package/dist/transport/impls/ws/ws.test.d.ts +2 -0
  30. package/dist/transport/impls/ws/ws.test.d.ts.map +1 -0
  31. package/dist/transport/impls/ws/ws.test.js +97 -0
  32. package/dist/transport/impls/ws.d.ts +6 -2
  33. package/dist/transport/impls/ws.d.ts.map +1 -1
  34. package/dist/transport/impls/ws.js +22 -7
  35. package/dist/transport/impls/ws.test.js +2 -2
  36. package/dist/transport/index.d.ts +3 -3
  37. package/dist/transport/index.d.ts.map +1 -1
  38. package/dist/transport/index.js +6 -3
  39. package/dist/transport/message.d.ts +4 -1
  40. package/dist/transport/message.d.ts.map +1 -1
  41. package/dist/transport/message.js +3 -0
  42. package/dist/transport/transport.d.ts +132 -0
  43. package/dist/transport/transport.d.ts.map +1 -0
  44. package/dist/transport/transport.js +241 -0
  45. package/dist/transport/types.d.ts.map +1 -1
  46. package/package.json +3 -2
@@ -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
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../transport/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAEvC,OAAO,EAEL,SAAS,EACT,sBAAsB,EAGtB,iBAAiB,EAGlB,MAAM,WAAW,CAAC;AAGnB;;;;GAIG;AACH,8BAAsB,SAAS;IAC7B;;OAEG;IACH,KAAK,EAAE,KAAK,CAAC;IAEb;;OAEG;IACH,QAAQ,EAAE,iBAAiB,CAAC;IAE5B;;OAEG;IACH,QAAQ,EAAE,GAAG,CAAC,CAAC,GAAG,EAAE,sBAAsB,KAAK,IAAI,CAAC,CAAC;IAGrD;;OAEG;IACH,UAAU,EAAE,GAAG,CAAC,SAAS,EAAE,sBAAsB,CAAC,CAAC;IAEnD;;;;OAIG;gBACS,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,iBAAiB;IAOrD;;;;OAIG;IACH,SAAS,CAAC,GAAG,EAAE,UAAU;IAmDzB;;;OAGG;IACH,kBAAkB,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,sBAAsB,KAAK,IAAI,GAAG,IAAI;IAIxE;;;OAGG;IACH,qBAAqB,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,sBAAsB,KAAK,IAAI,GAAG,IAAI;IAI3E,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,sBAAsB,GAAG,SAAS;IACrD,QAAQ,CAAC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAChC"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../transport/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAEvC,OAAO,EAEL,SAAS,EACT,sBAAsB,EAGtB,iBAAiB,EAGlB,MAAM,WAAW,CAAC;AAGnB;;;;GAIG;AACH,8BAAsB,SAAS;IAC7B;;OAEG;IACH,KAAK,EAAE,KAAK,CAAC;IAEb;;OAEG;IACH,QAAQ,EAAE,iBAAiB,CAAC;IAE5B;;OAEG;IACH,QAAQ,EAAE,GAAG,CAAC,CAAC,GAAG,EAAE,sBAAsB,KAAK,IAAI,CAAC,CAAC;IAGrD;;OAEG;IACH,UAAU,EAAE,GAAG,CAAC,SAAS,EAAE,sBAAsB,CAAC,CAAC;IAEnD;;;;OAIG;gBACS,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,iBAAiB;IAOrD;;;;OAIG;IACH,SAAS,CAAC,GAAG,EAAE,UAAU;IAoDzB;;;OAGG;IACH,kBAAkB,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,sBAAsB,KAAK,IAAI,GAAG,IAAI;IAIxE;;;OAGG;IACH,qBAAqB,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,sBAAsB,KAAK,IAAI,GAAG,IAAI;IAI3E,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,sBAAsB,GAAG,SAAS;IACrD,QAAQ,CAAC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAChC"}
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.6.3",
5
+ "version": "0.7.0",
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": [