@replit/river 0.10.0 → 0.10.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/dist/{router/builder.d.ts → builder-3c4485f0.d.ts} +76 -21
- package/dist/chunk-7WJ6YLE5.js +683 -0
- package/dist/chunk-AJQU4AZG.js +284 -0
- package/dist/chunk-ORAG7IAU.js +0 -0
- package/dist/chunk-PC65ZFWJ.js +29 -0
- package/dist/chunk-R6H2BIMC.js +49 -0
- package/dist/chunk-RGMHF6PF.js +65 -0
- package/dist/chunk-SLUSVGQH.js +30 -0
- package/dist/chunk-UU2Z7LDR.js +113 -0
- package/dist/chunk-WVT5QXMZ.js +20 -0
- package/dist/chunk-ZE4MX7DF.js +75 -0
- package/dist/codec/index.cjs +94 -0
- package/dist/codec/index.d.cts +15 -0
- package/dist/codec/index.d.ts +15 -4
- package/dist/codec/index.js +10 -2
- package/dist/connection-8e19874c.d.ts +11 -0
- package/dist/connection-f7688cc1.d.ts +11 -0
- package/dist/logging/index.cjs +56 -0
- package/dist/logging/index.d.cts +28 -0
- package/dist/logging/index.d.ts +6 -6
- package/dist/logging/index.js +9 -40
- package/dist/router/index.cjs +770 -0
- package/dist/router/index.d.cts +114 -0
- package/dist/router/index.d.ts +114 -12
- package/dist/router/index.js +24 -5
- package/dist/transport/impls/ws/client.cjs +505 -0
- package/dist/transport/impls/ws/client.d.cts +42 -0
- package/dist/transport/impls/ws/client.d.ts +8 -8
- package/dist/transport/impls/ws/client.js +10 -100
- package/dist/transport/impls/ws/server.cjs +457 -0
- package/dist/transport/impls/ws/server.d.cts +21 -0
- package/dist/transport/impls/ws/server.d.ts +11 -10
- package/dist/transport/impls/ws/server.js +11 -52
- package/dist/transport/index.cjs +362 -0
- package/dist/transport/{transport.d.ts → index.d.cts} +119 -7
- package/dist/transport/index.d.ts +273 -4
- package/dist/transport/index.js +20 -2
- package/dist/{codec/types.d.ts → types-3e5768ec.d.ts} +3 -2
- package/dist/util/testHelpers.cjs +731 -0
- package/dist/util/testHelpers.d.cts +79 -0
- package/dist/util/testHelpers.d.ts +22 -19
- package/dist/util/testHelpers.js +135 -163
- package/package.json +41 -13
- package/dist/__tests__/bandwidth.bench.d.ts +0 -2
- package/dist/__tests__/bandwidth.bench.d.ts.map +0 -1
- package/dist/__tests__/bandwidth.bench.js +0 -90
- package/dist/__tests__/cleanup.test.d.ts +0 -2
- package/dist/__tests__/cleanup.test.d.ts.map +0 -1
- package/dist/__tests__/cleanup.test.js +0 -165
- package/dist/__tests__/disconnects.test.d.ts +0 -2
- package/dist/__tests__/disconnects.test.d.ts.map +0 -1
- package/dist/__tests__/disconnects.test.js +0 -163
- package/dist/__tests__/e2e.test.d.ts +0 -2
- package/dist/__tests__/e2e.test.d.ts.map +0 -1
- package/dist/__tests__/e2e.test.js +0 -317
- package/dist/__tests__/fixtures/cleanup.d.ts +0 -12
- package/dist/__tests__/fixtures/cleanup.d.ts.map +0 -1
- package/dist/__tests__/fixtures/cleanup.js +0 -36
- package/dist/__tests__/fixtures/largePayload.json +0 -33
- package/dist/__tests__/fixtures/observable.d.ts +0 -26
- package/dist/__tests__/fixtures/observable.d.ts.map +0 -1
- package/dist/__tests__/fixtures/observable.js +0 -38
- package/dist/__tests__/fixtures/observable.test.d.ts +0 -2
- package/dist/__tests__/fixtures/observable.test.d.ts.map +0 -1
- package/dist/__tests__/fixtures/observable.test.js +0 -39
- package/dist/__tests__/fixtures/services.d.ts +0 -288
- package/dist/__tests__/fixtures/services.d.ts.map +0 -1
- package/dist/__tests__/fixtures/services.js +0 -207
- package/dist/__tests__/handler.test.d.ts +0 -2
- package/dist/__tests__/handler.test.d.ts.map +0 -1
- package/dist/__tests__/handler.test.js +0 -120
- package/dist/__tests__/serialize.test.d.ts +0 -2
- package/dist/__tests__/serialize.test.d.ts.map +0 -1
- package/dist/__tests__/serialize.test.js +0 -208
- package/dist/__tests__/typescript-stress.test.d.ts +0 -1583
- package/dist/__tests__/typescript-stress.test.d.ts.map +0 -1
- package/dist/__tests__/typescript-stress.test.js +0 -123
- package/dist/codec/binary.d.ts +0 -7
- package/dist/codec/binary.d.ts.map +0 -1
- package/dist/codec/binary.js +0 -20
- package/dist/codec/codec.test.d.ts +0 -5
- package/dist/codec/codec.test.d.ts.map +0 -1
- package/dist/codec/codec.test.js +0 -41
- package/dist/codec/index.d.ts.map +0 -1
- package/dist/codec/json.d.ts +0 -7
- package/dist/codec/json.d.ts.map +0 -1
- package/dist/codec/json.js +0 -51
- package/dist/codec/types.d.ts.map +0 -1
- package/dist/codec/types.js +0 -1
- package/dist/logging/index.d.ts.map +0 -1
- package/dist/router/builder.d.ts.map +0 -1
- package/dist/router/builder.js +0 -91
- package/dist/router/client.d.ts +0 -72
- package/dist/router/client.d.ts.map +0 -1
- package/dist/router/client.js +0 -257
- package/dist/router/context.d.ts +0 -30
- package/dist/router/context.d.ts.map +0 -1
- package/dist/router/context.js +0 -1
- package/dist/router/defs.d.ts +0 -16
- package/dist/router/defs.d.ts.map +0 -1
- package/dist/router/defs.js +0 -11
- package/dist/router/index.d.ts.map +0 -1
- package/dist/router/result.d.ts +0 -26
- package/dist/router/result.d.ts.map +0 -1
- package/dist/router/result.js +0 -22
- package/dist/router/server.d.ts +0 -39
- package/dist/router/server.d.ts.map +0 -1
- package/dist/router/server.js +0 -260
- package/dist/transport/events.d.ts +0 -19
- package/dist/transport/events.d.ts.map +0 -1
- package/dist/transport/events.js +0 -26
- package/dist/transport/impls/stdio/stdio.d.ts +0 -33
- package/dist/transport/impls/stdio/stdio.d.ts.map +0 -1
- package/dist/transport/impls/stdio/stdio.js +0 -75
- package/dist/transport/impls/stdio/stdio.test.d.ts +0 -2
- package/dist/transport/impls/stdio/stdio.test.d.ts.map +0 -1
- package/dist/transport/impls/stdio/stdio.test.js +0 -24
- package/dist/transport/impls/ws/client.d.ts.map +0 -1
- package/dist/transport/impls/ws/connection.d.ts +0 -11
- package/dist/transport/impls/ws/connection.d.ts.map +0 -1
- package/dist/transport/impls/ws/connection.js +0 -23
- package/dist/transport/impls/ws/server.d.ts.map +0 -1
- package/dist/transport/impls/ws/ws.test.d.ts +0 -2
- package/dist/transport/impls/ws/ws.test.d.ts.map +0 -1
- package/dist/transport/impls/ws/ws.test.js +0 -185
- package/dist/transport/index.d.ts.map +0 -1
- package/dist/transport/message.d.ts +0 -142
- package/dist/transport/message.d.ts.map +0 -1
- package/dist/transport/message.js +0 -113
- package/dist/transport/message.test.d.ts +0 -2
- package/dist/transport/message.test.d.ts.map +0 -1
- package/dist/transport/message.test.js +0 -52
- package/dist/transport/transport.d.ts.map +0 -1
- package/dist/transport/transport.js +0 -281
- package/dist/util/testHelpers.d.ts.map +0 -1
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import {
|
|
2
|
+
OpaqueTransportMessageSchema,
|
|
3
|
+
TransportAckSchema,
|
|
4
|
+
isAck,
|
|
5
|
+
reply
|
|
6
|
+
} from "./chunk-ZE4MX7DF.js";
|
|
7
|
+
import {
|
|
8
|
+
log
|
|
9
|
+
} from "./chunk-SLUSVGQH.js";
|
|
10
|
+
|
|
11
|
+
// transport/transport.ts
|
|
12
|
+
import { Value } from "@sinclair/typebox/value";
|
|
13
|
+
|
|
14
|
+
// transport/events.ts
|
|
15
|
+
var EventDispatcher = class {
|
|
16
|
+
eventListeners = {};
|
|
17
|
+
numberOfListeners(eventType) {
|
|
18
|
+
return this.eventListeners[eventType]?.size ?? 0;
|
|
19
|
+
}
|
|
20
|
+
addEventListener(eventType, handler) {
|
|
21
|
+
if (!this.eventListeners[eventType]) {
|
|
22
|
+
this.eventListeners[eventType] = /* @__PURE__ */ new Set();
|
|
23
|
+
}
|
|
24
|
+
this.eventListeners[eventType]?.add(handler);
|
|
25
|
+
}
|
|
26
|
+
removeEventListener(eventType, handler) {
|
|
27
|
+
const handlers = this.eventListeners[eventType];
|
|
28
|
+
if (handlers) {
|
|
29
|
+
this.eventListeners[eventType]?.delete(handler);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
dispatchEvent(eventType, event) {
|
|
33
|
+
const handlers = this.eventListeners[eventType];
|
|
34
|
+
if (handlers) {
|
|
35
|
+
for (const handler of handlers) {
|
|
36
|
+
handler(event);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// transport/transport.ts
|
|
43
|
+
var Connection = class {
|
|
44
|
+
connectedTo;
|
|
45
|
+
transport;
|
|
46
|
+
constructor(transport, connectedTo) {
|
|
47
|
+
this.connectedTo = connectedTo;
|
|
48
|
+
this.transport = transport;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
var Transport = class {
|
|
52
|
+
/**
|
|
53
|
+
* A flag indicating whether the transport has been destroyed.
|
|
54
|
+
* A destroyed transport will not attempt to reconnect and cannot be used again.
|
|
55
|
+
*/
|
|
56
|
+
state;
|
|
57
|
+
/**
|
|
58
|
+
* The {@link Codec} used to encode and decode messages.
|
|
59
|
+
*/
|
|
60
|
+
codec;
|
|
61
|
+
/**
|
|
62
|
+
* The client ID of this transport.
|
|
63
|
+
*/
|
|
64
|
+
clientId;
|
|
65
|
+
/**
|
|
66
|
+
* An array of message IDs that are waiting to be sent over the WebSocket connection.
|
|
67
|
+
* This builds up if the WebSocket is down for a period of time.
|
|
68
|
+
*/
|
|
69
|
+
sendQueue;
|
|
70
|
+
/**
|
|
71
|
+
* The buffer of messages that have been sent but not yet acknowledged.
|
|
72
|
+
*/
|
|
73
|
+
sendBuffer;
|
|
74
|
+
/**
|
|
75
|
+
* The map of {@link Connection}s managed by this transport.
|
|
76
|
+
*/
|
|
77
|
+
connections;
|
|
78
|
+
/**
|
|
79
|
+
* The event dispatcher for handling events of type EventTypes.
|
|
80
|
+
*/
|
|
81
|
+
eventDispatcher;
|
|
82
|
+
/**
|
|
83
|
+
* Creates a new Transport instance.
|
|
84
|
+
* @param codec The codec used to encode and decode messages.
|
|
85
|
+
* @param clientId The client ID of this transport.
|
|
86
|
+
*/
|
|
87
|
+
constructor(codec, clientId) {
|
|
88
|
+
this.eventDispatcher = new EventDispatcher();
|
|
89
|
+
this.sendBuffer = /* @__PURE__ */ new Map();
|
|
90
|
+
this.sendQueue = /* @__PURE__ */ new Map();
|
|
91
|
+
this.connections = /* @__PURE__ */ new Map();
|
|
92
|
+
this.codec = codec;
|
|
93
|
+
this.clientId = clientId;
|
|
94
|
+
this.state = "open";
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* The downstream implementation needs to call this when a new connection is established.
|
|
98
|
+
* @param conn The connection object.
|
|
99
|
+
*/
|
|
100
|
+
onConnect(conn) {
|
|
101
|
+
log?.info(`${this.clientId} -- new connection to ${conn.connectedTo}`);
|
|
102
|
+
this.connections.set(conn.connectedTo, conn);
|
|
103
|
+
this.eventDispatcher.dispatchEvent("connectionStatus", {
|
|
104
|
+
status: "connect",
|
|
105
|
+
conn
|
|
106
|
+
});
|
|
107
|
+
const outstanding = this.sendQueue.get(conn.connectedTo);
|
|
108
|
+
if (!outstanding) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
for (const id of outstanding) {
|
|
112
|
+
const msg = this.sendBuffer.get(id);
|
|
113
|
+
if (!msg) {
|
|
114
|
+
log?.warn(
|
|
115
|
+
`${this.clientId} -- tried to resend a message we received an ack for`
|
|
116
|
+
);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
this.send(msg);
|
|
120
|
+
}
|
|
121
|
+
this.sendQueue.delete(conn.connectedTo);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* The downstream implementation needs to call this when a connection is closed.
|
|
125
|
+
* @param conn The connection object.
|
|
126
|
+
*/
|
|
127
|
+
onDisconnect(conn) {
|
|
128
|
+
log?.info(`${this.clientId} -- disconnect from ${conn.connectedTo}`);
|
|
129
|
+
conn.close();
|
|
130
|
+
this.connections.delete(conn.connectedTo);
|
|
131
|
+
this.eventDispatcher.dispatchEvent("connectionStatus", {
|
|
132
|
+
status: "disconnect",
|
|
133
|
+
conn
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Handles a message received by this transport. Thin wrapper around {@link handleMsg} and {@link parseMsg}.
|
|
138
|
+
* @param msg The message to handle.
|
|
139
|
+
*/
|
|
140
|
+
onMessage(msg) {
|
|
141
|
+
return this.handleMsg(this.parseMsg(msg));
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Parses a message from a Uint8Array into a {@link OpaqueTransportMessage}.
|
|
145
|
+
* @param msg The message to parse.
|
|
146
|
+
* @returns The parsed message, or null if the message is malformed or invalid.
|
|
147
|
+
*/
|
|
148
|
+
parseMsg(msg) {
|
|
149
|
+
const parsedMsg = this.codec.fromBuffer(msg);
|
|
150
|
+
if (parsedMsg === null) {
|
|
151
|
+
const decodedBuffer = new TextDecoder().decode(msg);
|
|
152
|
+
log?.warn(`${this.clientId} -- received malformed msg: ${decodedBuffer}`);
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
if (Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
|
|
156
|
+
return {
|
|
157
|
+
...parsedMsg,
|
|
158
|
+
serviceName: parsedMsg.serviceName === null ? void 0 : parsedMsg.serviceName,
|
|
159
|
+
procedureName: parsedMsg.procedureName === null ? void 0 : parsedMsg.procedureName
|
|
160
|
+
};
|
|
161
|
+
} else {
|
|
162
|
+
log?.warn(
|
|
163
|
+
`${this.clientId} -- received invalid msg: ${JSON.stringify(
|
|
164
|
+
parsedMsg
|
|
165
|
+
)}`
|
|
166
|
+
);
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Called when a message is received by this transport.
|
|
172
|
+
* You generally shouldn't need to override this in downstream transport implementations.
|
|
173
|
+
* @param msg The received message.
|
|
174
|
+
*/
|
|
175
|
+
handleMsg(msg) {
|
|
176
|
+
if (!msg) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (isAck(msg.controlFlags) && Value.Check(TransportAckSchema, msg)) {
|
|
180
|
+
log?.info(`${this.clientId} -- received ack: ${JSON.stringify(msg)}`);
|
|
181
|
+
if (this.sendBuffer.has(msg.payload.ack)) {
|
|
182
|
+
this.sendBuffer.delete(msg.payload.ack);
|
|
183
|
+
}
|
|
184
|
+
} else {
|
|
185
|
+
log?.info(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
|
|
186
|
+
if (msg.to !== this.clientId) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
this.eventDispatcher.dispatchEvent("message", msg);
|
|
190
|
+
if (!isAck(msg.controlFlags)) {
|
|
191
|
+
const ackMsg = reply(msg, { ack: msg.id });
|
|
192
|
+
ackMsg.controlFlags = 1 /* AckBit */;
|
|
193
|
+
ackMsg.from = this.clientId;
|
|
194
|
+
this.send(ackMsg);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Adds a listener to this transport.
|
|
200
|
+
* @param the type of event to listen for
|
|
201
|
+
* @param handler The message handler to add.
|
|
202
|
+
*/
|
|
203
|
+
addEventListener(type, handler) {
|
|
204
|
+
this.eventDispatcher.addEventListener(type, handler);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Removes a listener from this transport.
|
|
208
|
+
* @param the type of event to unlisten on
|
|
209
|
+
* @param handler The message handler to remove.
|
|
210
|
+
*/
|
|
211
|
+
removeEventListener(type, handler) {
|
|
212
|
+
this.eventDispatcher.removeEventListener(type, handler);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Sends a message over this transport, delegating to the appropriate connection to actually
|
|
216
|
+
* send the message.
|
|
217
|
+
* @param msg The message to send.
|
|
218
|
+
* @returns The ID of the sent message.
|
|
219
|
+
*/
|
|
220
|
+
send(msg) {
|
|
221
|
+
if (this.state === "destroyed") {
|
|
222
|
+
const err = "transport is destroyed, cant send";
|
|
223
|
+
log?.error(`${this.clientId} -- ` + err + `: ${JSON.stringify(msg)}`);
|
|
224
|
+
throw new Error(err);
|
|
225
|
+
} else if (this.state === "closed") {
|
|
226
|
+
log?.info(
|
|
227
|
+
`${this.clientId} -- transport closed when sending, discarding : ${JSON.stringify(
|
|
228
|
+
msg
|
|
229
|
+
)}`
|
|
230
|
+
);
|
|
231
|
+
return msg.id;
|
|
232
|
+
}
|
|
233
|
+
let conn = this.connections.get(msg.to);
|
|
234
|
+
if (!isAck(msg.controlFlags)) {
|
|
235
|
+
this.sendBuffer.set(msg.id, msg);
|
|
236
|
+
}
|
|
237
|
+
if (conn) {
|
|
238
|
+
log?.info(`${this.clientId} -- sending ${JSON.stringify(msg)}`);
|
|
239
|
+
const ok = conn.send(this.codec.toBuffer(msg));
|
|
240
|
+
if (ok) {
|
|
241
|
+
return msg.id;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
log?.info(
|
|
245
|
+
`${this.clientId} -- connection to ${msg.to} not ready, attempting reconnect and queuing ${JSON.stringify(msg)}`
|
|
246
|
+
);
|
|
247
|
+
const outstanding = this.sendQueue.get(msg.to) || [];
|
|
248
|
+
outstanding.push(msg.id);
|
|
249
|
+
this.sendQueue.set(msg.to, outstanding);
|
|
250
|
+
this.createNewConnection(msg.to);
|
|
251
|
+
return msg.id;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Default close implementation for transports. You should override this in the downstream
|
|
255
|
+
* implementation if you need to do any additional cleanup and call super.close() at the end.
|
|
256
|
+
* Closes the transport. Any messages sent while the transport is closed will be silently discarded.
|
|
257
|
+
*/
|
|
258
|
+
async close() {
|
|
259
|
+
for (const conn of this.connections.values()) {
|
|
260
|
+
conn.close();
|
|
261
|
+
}
|
|
262
|
+
this.connections.clear();
|
|
263
|
+
this.state = "closed";
|
|
264
|
+
log?.info(`${this.clientId} -- closed transport`);
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Default destroy implementation for transports. You should override this in the downstream
|
|
268
|
+
* implementation if you need to do any additional cleanup and call super.destroy() at the end.
|
|
269
|
+
* Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
|
|
270
|
+
*/
|
|
271
|
+
async destroy() {
|
|
272
|
+
for (const conn of this.connections.values()) {
|
|
273
|
+
conn.close();
|
|
274
|
+
}
|
|
275
|
+
this.connections.clear();
|
|
276
|
+
this.state = "destroyed";
|
|
277
|
+
log?.info(`${this.clientId} -- destroyed transport`);
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
export {
|
|
282
|
+
Connection,
|
|
283
|
+
Transport
|
|
284
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Connection
|
|
3
|
+
} from "./chunk-AJQU4AZG.js";
|
|
4
|
+
|
|
5
|
+
// transport/impls/ws/connection.ts
|
|
6
|
+
var WebSocketConnection = class extends Connection {
|
|
7
|
+
ws;
|
|
8
|
+
constructor(transport, connectedTo, ws) {
|
|
9
|
+
super(transport, connectedTo);
|
|
10
|
+
this.ws = ws;
|
|
11
|
+
ws.binaryType = "arraybuffer";
|
|
12
|
+
this.ws.onmessage = (msg) => transport.onMessage(msg.data);
|
|
13
|
+
}
|
|
14
|
+
send(payload) {
|
|
15
|
+
if (this.ws.readyState === this.ws.OPEN) {
|
|
16
|
+
this.ws.send(payload);
|
|
17
|
+
return true;
|
|
18
|
+
} else {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async close() {
|
|
23
|
+
this.ws.close();
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export {
|
|
28
|
+
WebSocketConnection
|
|
29
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// codec/json.ts
|
|
2
|
+
var encoder = new TextEncoder();
|
|
3
|
+
var decoder = new TextDecoder();
|
|
4
|
+
function uint8ArrayToBase64(uint8Array) {
|
|
5
|
+
let binary = "";
|
|
6
|
+
uint8Array.forEach((byte) => {
|
|
7
|
+
binary += String.fromCharCode(byte);
|
|
8
|
+
});
|
|
9
|
+
return btoa(binary);
|
|
10
|
+
}
|
|
11
|
+
function base64ToUint8Array(base64) {
|
|
12
|
+
const binaryString = atob(base64);
|
|
13
|
+
const uint8Array = new Uint8Array(binaryString.length);
|
|
14
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
15
|
+
uint8Array[i] = binaryString.charCodeAt(i);
|
|
16
|
+
}
|
|
17
|
+
return uint8Array;
|
|
18
|
+
}
|
|
19
|
+
var NaiveJsonCodec = {
|
|
20
|
+
toBuffer: (obj) => {
|
|
21
|
+
return encoder.encode(
|
|
22
|
+
JSON.stringify(obj, function replacer(key) {
|
|
23
|
+
let val = this[key];
|
|
24
|
+
if (val instanceof Uint8Array) {
|
|
25
|
+
return { $t: uint8ArrayToBase64(val) };
|
|
26
|
+
} else {
|
|
27
|
+
return val;
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
);
|
|
31
|
+
},
|
|
32
|
+
fromBuffer: (buff) => {
|
|
33
|
+
try {
|
|
34
|
+
return JSON.parse(decoder.decode(buff), function reviver(_key, val) {
|
|
35
|
+
if (val?.$t) {
|
|
36
|
+
return base64ToUint8Array(val.$t);
|
|
37
|
+
} else {
|
|
38
|
+
return val;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
} catch {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export {
|
|
48
|
+
NaiveJsonCodec
|
|
49
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {
|
|
2
|
+
WebSocketConnection
|
|
3
|
+
} from "./chunk-PC65ZFWJ.js";
|
|
4
|
+
import {
|
|
5
|
+
NaiveJsonCodec
|
|
6
|
+
} from "./chunk-R6H2BIMC.js";
|
|
7
|
+
import {
|
|
8
|
+
Transport
|
|
9
|
+
} from "./chunk-AJQU4AZG.js";
|
|
10
|
+
import {
|
|
11
|
+
log
|
|
12
|
+
} from "./chunk-SLUSVGQH.js";
|
|
13
|
+
|
|
14
|
+
// transport/impls/ws/server.ts
|
|
15
|
+
var defaultOptions = {
|
|
16
|
+
codec: NaiveJsonCodec
|
|
17
|
+
};
|
|
18
|
+
var WebSocketServerTransport = class extends Transport {
|
|
19
|
+
wss;
|
|
20
|
+
clientId;
|
|
21
|
+
constructor(wss, clientId, providedOptions) {
|
|
22
|
+
const options = { ...defaultOptions, ...providedOptions };
|
|
23
|
+
super(options.codec, clientId);
|
|
24
|
+
this.wss = wss;
|
|
25
|
+
this.clientId = clientId;
|
|
26
|
+
this.setupConnectionStatusListeners();
|
|
27
|
+
}
|
|
28
|
+
connectionHandler = (ws) => {
|
|
29
|
+
let conn = void 0;
|
|
30
|
+
ws.onmessage = (msg) => {
|
|
31
|
+
const parsedMsg = this.parseMsg(msg.data);
|
|
32
|
+
if (parsedMsg && !conn) {
|
|
33
|
+
conn = new WebSocketConnection(this, parsedMsg.from, ws);
|
|
34
|
+
this.onConnect(conn);
|
|
35
|
+
this.handleMsg(parsedMsg);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
ws.onclose = () => {
|
|
39
|
+
if (conn) {
|
|
40
|
+
this.onDisconnect(conn);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
ws.onerror = (msg) => {
|
|
44
|
+
log?.warn(
|
|
45
|
+
`${this.clientId} -- ws error from client ${conn?.connectedTo ?? "unknown"}: ${msg}`
|
|
46
|
+
);
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
setupConnectionStatusListeners() {
|
|
50
|
+
this.wss.on("connection", this.connectionHandler);
|
|
51
|
+
}
|
|
52
|
+
async createNewConnection(to) {
|
|
53
|
+
const err = `${this.clientId} -- failed to send msg to ${to}, client probably dropped`;
|
|
54
|
+
log?.warn(err);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
async close() {
|
|
58
|
+
super.close();
|
|
59
|
+
this.wss.off("connection", this.connectionHandler);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export {
|
|
64
|
+
WebSocketServerTransport
|
|
65
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// logging/index.ts
|
|
2
|
+
var LoggingLevels = {
|
|
3
|
+
info: 0,
|
|
4
|
+
warn: 1,
|
|
5
|
+
error: 2
|
|
6
|
+
};
|
|
7
|
+
var log;
|
|
8
|
+
var defaultLoggingLevel = "warn";
|
|
9
|
+
function bindLogger(write, color) {
|
|
10
|
+
const info = color ? "\x1B[37minfo\x1B[0m" : "info";
|
|
11
|
+
const warn = color ? "\x1B[33mwarn\x1B[0m" : "warn";
|
|
12
|
+
const error = color ? "\x1B[31merr\x1B[0m" : "err";
|
|
13
|
+
log = {
|
|
14
|
+
info: (msg) => log && LoggingLevels[log.minLevel] <= 0 && write(`[river:${info}] ${msg}`),
|
|
15
|
+
warn: (msg) => log && LoggingLevels[log.minLevel] <= 1 && write(`[river:${warn}] ${msg}`),
|
|
16
|
+
error: (msg) => log && LoggingLevels[log.minLevel] <= 2 && write(`[river:${error}] ${msg}`),
|
|
17
|
+
minLevel: log?.minLevel ?? defaultLoggingLevel
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function setLevel(level) {
|
|
21
|
+
if (log) {
|
|
22
|
+
log.minLevel = level;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export {
|
|
27
|
+
log,
|
|
28
|
+
bindLogger,
|
|
29
|
+
setLevel
|
|
30
|
+
};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {
|
|
2
|
+
WebSocketConnection
|
|
3
|
+
} from "./chunk-PC65ZFWJ.js";
|
|
4
|
+
import {
|
|
5
|
+
NaiveJsonCodec
|
|
6
|
+
} from "./chunk-R6H2BIMC.js";
|
|
7
|
+
import {
|
|
8
|
+
Transport
|
|
9
|
+
} from "./chunk-AJQU4AZG.js";
|
|
10
|
+
import {
|
|
11
|
+
log
|
|
12
|
+
} from "./chunk-SLUSVGQH.js";
|
|
13
|
+
|
|
14
|
+
// transport/impls/ws/client.ts
|
|
15
|
+
var defaultOptions = {
|
|
16
|
+
retryIntervalMs: 250,
|
|
17
|
+
retryAttemptsMax: 5,
|
|
18
|
+
codec: NaiveJsonCodec
|
|
19
|
+
};
|
|
20
|
+
var WebSocketClientTransport = class extends Transport {
|
|
21
|
+
/**
|
|
22
|
+
* A function that returns a Promise that resolves to a WebSocket instance.
|
|
23
|
+
*/
|
|
24
|
+
wsGetter;
|
|
25
|
+
options;
|
|
26
|
+
serverId;
|
|
27
|
+
reconnectPromises;
|
|
28
|
+
tryReconnecting = true;
|
|
29
|
+
/**
|
|
30
|
+
* Creates a new WebSocketTransport instance.
|
|
31
|
+
* @param wsGetter A function that returns a Promise that resolves to a WebSocket instance.
|
|
32
|
+
* @param clientId The ID of the client using the transport.
|
|
33
|
+
* @param providedOptions An optional object containing configuration options for the transport.
|
|
34
|
+
*/
|
|
35
|
+
constructor(wsGetter, clientId, serverId, providedOptions) {
|
|
36
|
+
const options = { ...defaultOptions, ...providedOptions };
|
|
37
|
+
super(options.codec, clientId);
|
|
38
|
+
this.wsGetter = wsGetter;
|
|
39
|
+
this.serverId = serverId;
|
|
40
|
+
this.options = options;
|
|
41
|
+
this.reconnectPromises = /* @__PURE__ */ new Map();
|
|
42
|
+
this.setupConnectionStatusListeners();
|
|
43
|
+
}
|
|
44
|
+
setupConnectionStatusListeners() {
|
|
45
|
+
this.createNewConnection(this.serverId);
|
|
46
|
+
}
|
|
47
|
+
async createNewConnection(to, attempt = 0) {
|
|
48
|
+
if (this.state === "destroyed") {
|
|
49
|
+
throw new Error("cant reopen a destroyed connection");
|
|
50
|
+
}
|
|
51
|
+
let reconnectPromise = this.reconnectPromises.get(to);
|
|
52
|
+
if (!reconnectPromise) {
|
|
53
|
+
if (!this.tryReconnecting) {
|
|
54
|
+
log?.info(
|
|
55
|
+
`${this.clientId} -- tryReconnecting is false, not attempting reconnect`
|
|
56
|
+
);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
reconnectPromise = new Promise(async (resolve) => {
|
|
60
|
+
log?.info(`${this.clientId} -- establishing a new websocket to ${to}`);
|
|
61
|
+
const ws = await this.wsGetter(to);
|
|
62
|
+
if (ws.readyState === ws.OPEN) {
|
|
63
|
+
return resolve({ ws });
|
|
64
|
+
}
|
|
65
|
+
if (ws.readyState === ws.CLOSING || ws.readyState === ws.CLOSED) {
|
|
66
|
+
return resolve({ err: "ws is closing or closed" });
|
|
67
|
+
}
|
|
68
|
+
ws.addEventListener("open", function onOpen() {
|
|
69
|
+
ws.removeEventListener("open", onOpen);
|
|
70
|
+
resolve({ ws });
|
|
71
|
+
});
|
|
72
|
+
ws.addEventListener("close", function onClose(evt) {
|
|
73
|
+
ws.removeEventListener("close", onClose);
|
|
74
|
+
resolve({ err: evt.reason });
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
this.reconnectPromises.set(to, reconnectPromise);
|
|
78
|
+
}
|
|
79
|
+
const res = await reconnectPromise;
|
|
80
|
+
if ("ws" in res && res.ws.readyState === res.ws.OPEN) {
|
|
81
|
+
if (res.ws === this.connections.get(to)?.ws) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
log?.info(`${this.clientId} -- websocket ok`);
|
|
85
|
+
const conn = new WebSocketConnection(this, to, res.ws);
|
|
86
|
+
this.onConnect(conn);
|
|
87
|
+
res.ws.onclose = () => {
|
|
88
|
+
this.reconnectPromises.delete(to);
|
|
89
|
+
this.onDisconnect(conn);
|
|
90
|
+
};
|
|
91
|
+
this.state = "open";
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
this.reconnectPromises.delete(to);
|
|
95
|
+
if (attempt >= this.options.retryAttemptsMax) {
|
|
96
|
+
throw new Error(
|
|
97
|
+
`${this.clientId} -- websocket to ${to} failed after ${attempt} attempts, giving up`
|
|
98
|
+
);
|
|
99
|
+
} else {
|
|
100
|
+
log?.warn(
|
|
101
|
+
`${this.clientId} -- websocket to ${to} failed, trying again in ${this.options.retryIntervalMs * attempt}ms`
|
|
102
|
+
);
|
|
103
|
+
setTimeout(
|
|
104
|
+
() => this.createNewConnection(to, attempt + 1),
|
|
105
|
+
this.options.retryIntervalMs * attempt
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export {
|
|
112
|
+
WebSocketClientTransport
|
|
113
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// codec/binary.ts
|
|
2
|
+
import { decode, encode } from "@msgpack/msgpack";
|
|
3
|
+
var BinaryCodec = {
|
|
4
|
+
toBuffer: encode,
|
|
5
|
+
fromBuffer: (buff) => {
|
|
6
|
+
try {
|
|
7
|
+
const res = decode(buff);
|
|
8
|
+
if (typeof res !== "object") {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
return res;
|
|
12
|
+
} catch {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export {
|
|
19
|
+
BinaryCodec
|
|
20
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// transport/message.ts
|
|
2
|
+
import { Type } from "@sinclair/typebox";
|
|
3
|
+
import { nanoid } from "nanoid";
|
|
4
|
+
var TransportMessageSchema = (t) => Type.Object({
|
|
5
|
+
id: Type.String(),
|
|
6
|
+
from: Type.String(),
|
|
7
|
+
to: Type.String(),
|
|
8
|
+
serviceName: Type.Optional(Type.Union([Type.String(), Type.Null()])),
|
|
9
|
+
procedureName: Type.Optional(Type.Union([Type.String(), Type.Null()])),
|
|
10
|
+
streamId: Type.String(),
|
|
11
|
+
controlFlags: Type.Integer(),
|
|
12
|
+
payload: t
|
|
13
|
+
});
|
|
14
|
+
var TransportAckSchema = TransportMessageSchema(
|
|
15
|
+
Type.Object({
|
|
16
|
+
ack: Type.String()
|
|
17
|
+
})
|
|
18
|
+
);
|
|
19
|
+
var ControlMessagePayloadSchema = Type.Object({
|
|
20
|
+
type: Type.Literal("CLOSE")
|
|
21
|
+
});
|
|
22
|
+
var OpaqueTransportMessageSchema = TransportMessageSchema(
|
|
23
|
+
Type.Unknown()
|
|
24
|
+
);
|
|
25
|
+
function msg(from, to, streamId, payload, serviceName, procedureName) {
|
|
26
|
+
return {
|
|
27
|
+
id: nanoid(),
|
|
28
|
+
to,
|
|
29
|
+
from,
|
|
30
|
+
serviceName,
|
|
31
|
+
procedureName,
|
|
32
|
+
streamId,
|
|
33
|
+
controlFlags: 0,
|
|
34
|
+
payload
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function reply(msg2, response) {
|
|
38
|
+
return {
|
|
39
|
+
id: nanoid(),
|
|
40
|
+
streamId: msg2.streamId,
|
|
41
|
+
controlFlags: 0,
|
|
42
|
+
to: msg2.from,
|
|
43
|
+
from: msg2.to,
|
|
44
|
+
payload: response
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function closeStream(from, to, stream) {
|
|
48
|
+
const closeMessage = msg(from, to, stream, {
|
|
49
|
+
type: "CLOSE"
|
|
50
|
+
});
|
|
51
|
+
closeMessage.controlFlags |= 4 /* StreamClosedBit */;
|
|
52
|
+
return closeMessage;
|
|
53
|
+
}
|
|
54
|
+
function isAck(controlFlag) {
|
|
55
|
+
return (controlFlag & 1 /* AckBit */) === 1 /* AckBit */;
|
|
56
|
+
}
|
|
57
|
+
function isStreamOpen(controlFlag) {
|
|
58
|
+
return (controlFlag & 2 /* StreamOpenBit */) === 2 /* StreamOpenBit */;
|
|
59
|
+
}
|
|
60
|
+
function isStreamClose(controlFlag) {
|
|
61
|
+
return (controlFlag & 4 /* StreamClosedBit */) === 4 /* StreamClosedBit */;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export {
|
|
65
|
+
TransportMessageSchema,
|
|
66
|
+
TransportAckSchema,
|
|
67
|
+
ControlMessagePayloadSchema,
|
|
68
|
+
OpaqueTransportMessageSchema,
|
|
69
|
+
msg,
|
|
70
|
+
reply,
|
|
71
|
+
closeStream,
|
|
72
|
+
isAck,
|
|
73
|
+
isStreamOpen,
|
|
74
|
+
isStreamClose
|
|
75
|
+
};
|