@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,505 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// transport/impls/ws/client.ts
|
|
21
|
+
var client_exports = {};
|
|
22
|
+
__export(client_exports, {
|
|
23
|
+
WebSocketClientTransport: () => WebSocketClientTransport
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(client_exports);
|
|
26
|
+
|
|
27
|
+
// transport/transport.ts
|
|
28
|
+
var import_value = require("@sinclair/typebox/value");
|
|
29
|
+
|
|
30
|
+
// transport/message.ts
|
|
31
|
+
var import_typebox = require("@sinclair/typebox");
|
|
32
|
+
var import_nanoid = require("nanoid");
|
|
33
|
+
var TransportMessageSchema = (t) => import_typebox.Type.Object({
|
|
34
|
+
id: import_typebox.Type.String(),
|
|
35
|
+
from: import_typebox.Type.String(),
|
|
36
|
+
to: import_typebox.Type.String(),
|
|
37
|
+
serviceName: import_typebox.Type.Optional(import_typebox.Type.Union([import_typebox.Type.String(), import_typebox.Type.Null()])),
|
|
38
|
+
procedureName: import_typebox.Type.Optional(import_typebox.Type.Union([import_typebox.Type.String(), import_typebox.Type.Null()])),
|
|
39
|
+
streamId: import_typebox.Type.String(),
|
|
40
|
+
controlFlags: import_typebox.Type.Integer(),
|
|
41
|
+
payload: t
|
|
42
|
+
});
|
|
43
|
+
var TransportAckSchema = TransportMessageSchema(
|
|
44
|
+
import_typebox.Type.Object({
|
|
45
|
+
ack: import_typebox.Type.String()
|
|
46
|
+
})
|
|
47
|
+
);
|
|
48
|
+
var ControlMessagePayloadSchema = import_typebox.Type.Object({
|
|
49
|
+
type: import_typebox.Type.Literal("CLOSE")
|
|
50
|
+
});
|
|
51
|
+
var OpaqueTransportMessageSchema = TransportMessageSchema(
|
|
52
|
+
import_typebox.Type.Unknown()
|
|
53
|
+
);
|
|
54
|
+
function reply(msg, response) {
|
|
55
|
+
return {
|
|
56
|
+
id: (0, import_nanoid.nanoid)(),
|
|
57
|
+
streamId: msg.streamId,
|
|
58
|
+
controlFlags: 0,
|
|
59
|
+
to: msg.from,
|
|
60
|
+
from: msg.to,
|
|
61
|
+
payload: response
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function isAck(controlFlag) {
|
|
65
|
+
return (controlFlag & 1 /* AckBit */) === 1 /* AckBit */;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// logging/index.ts
|
|
69
|
+
var log;
|
|
70
|
+
|
|
71
|
+
// transport/events.ts
|
|
72
|
+
var EventDispatcher = class {
|
|
73
|
+
eventListeners = {};
|
|
74
|
+
numberOfListeners(eventType) {
|
|
75
|
+
return this.eventListeners[eventType]?.size ?? 0;
|
|
76
|
+
}
|
|
77
|
+
addEventListener(eventType, handler) {
|
|
78
|
+
if (!this.eventListeners[eventType]) {
|
|
79
|
+
this.eventListeners[eventType] = /* @__PURE__ */ new Set();
|
|
80
|
+
}
|
|
81
|
+
this.eventListeners[eventType]?.add(handler);
|
|
82
|
+
}
|
|
83
|
+
removeEventListener(eventType, handler) {
|
|
84
|
+
const handlers = this.eventListeners[eventType];
|
|
85
|
+
if (handlers) {
|
|
86
|
+
this.eventListeners[eventType]?.delete(handler);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
dispatchEvent(eventType, event) {
|
|
90
|
+
const handlers = this.eventListeners[eventType];
|
|
91
|
+
if (handlers) {
|
|
92
|
+
for (const handler of handlers) {
|
|
93
|
+
handler(event);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// transport/transport.ts
|
|
100
|
+
var Connection = class {
|
|
101
|
+
connectedTo;
|
|
102
|
+
transport;
|
|
103
|
+
constructor(transport, connectedTo) {
|
|
104
|
+
this.connectedTo = connectedTo;
|
|
105
|
+
this.transport = transport;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
var Transport = class {
|
|
109
|
+
/**
|
|
110
|
+
* A flag indicating whether the transport has been destroyed.
|
|
111
|
+
* A destroyed transport will not attempt to reconnect and cannot be used again.
|
|
112
|
+
*/
|
|
113
|
+
state;
|
|
114
|
+
/**
|
|
115
|
+
* The {@link Codec} used to encode and decode messages.
|
|
116
|
+
*/
|
|
117
|
+
codec;
|
|
118
|
+
/**
|
|
119
|
+
* The client ID of this transport.
|
|
120
|
+
*/
|
|
121
|
+
clientId;
|
|
122
|
+
/**
|
|
123
|
+
* An array of message IDs that are waiting to be sent over the WebSocket connection.
|
|
124
|
+
* This builds up if the WebSocket is down for a period of time.
|
|
125
|
+
*/
|
|
126
|
+
sendQueue;
|
|
127
|
+
/**
|
|
128
|
+
* The buffer of messages that have been sent but not yet acknowledged.
|
|
129
|
+
*/
|
|
130
|
+
sendBuffer;
|
|
131
|
+
/**
|
|
132
|
+
* The map of {@link Connection}s managed by this transport.
|
|
133
|
+
*/
|
|
134
|
+
connections;
|
|
135
|
+
/**
|
|
136
|
+
* The event dispatcher for handling events of type EventTypes.
|
|
137
|
+
*/
|
|
138
|
+
eventDispatcher;
|
|
139
|
+
/**
|
|
140
|
+
* Creates a new Transport instance.
|
|
141
|
+
* @param codec The codec used to encode and decode messages.
|
|
142
|
+
* @param clientId The client ID of this transport.
|
|
143
|
+
*/
|
|
144
|
+
constructor(codec, clientId) {
|
|
145
|
+
this.eventDispatcher = new EventDispatcher();
|
|
146
|
+
this.sendBuffer = /* @__PURE__ */ new Map();
|
|
147
|
+
this.sendQueue = /* @__PURE__ */ new Map();
|
|
148
|
+
this.connections = /* @__PURE__ */ new Map();
|
|
149
|
+
this.codec = codec;
|
|
150
|
+
this.clientId = clientId;
|
|
151
|
+
this.state = "open";
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* The downstream implementation needs to call this when a new connection is established.
|
|
155
|
+
* @param conn The connection object.
|
|
156
|
+
*/
|
|
157
|
+
onConnect(conn) {
|
|
158
|
+
log?.info(`${this.clientId} -- new connection to ${conn.connectedTo}`);
|
|
159
|
+
this.connections.set(conn.connectedTo, conn);
|
|
160
|
+
this.eventDispatcher.dispatchEvent("connectionStatus", {
|
|
161
|
+
status: "connect",
|
|
162
|
+
conn
|
|
163
|
+
});
|
|
164
|
+
const outstanding = this.sendQueue.get(conn.connectedTo);
|
|
165
|
+
if (!outstanding) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
for (const id of outstanding) {
|
|
169
|
+
const msg = this.sendBuffer.get(id);
|
|
170
|
+
if (!msg) {
|
|
171
|
+
log?.warn(
|
|
172
|
+
`${this.clientId} -- tried to resend a message we received an ack for`
|
|
173
|
+
);
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
this.send(msg);
|
|
177
|
+
}
|
|
178
|
+
this.sendQueue.delete(conn.connectedTo);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* The downstream implementation needs to call this when a connection is closed.
|
|
182
|
+
* @param conn The connection object.
|
|
183
|
+
*/
|
|
184
|
+
onDisconnect(conn) {
|
|
185
|
+
log?.info(`${this.clientId} -- disconnect from ${conn.connectedTo}`);
|
|
186
|
+
conn.close();
|
|
187
|
+
this.connections.delete(conn.connectedTo);
|
|
188
|
+
this.eventDispatcher.dispatchEvent("connectionStatus", {
|
|
189
|
+
status: "disconnect",
|
|
190
|
+
conn
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Handles a message received by this transport. Thin wrapper around {@link handleMsg} and {@link parseMsg}.
|
|
195
|
+
* @param msg The message to handle.
|
|
196
|
+
*/
|
|
197
|
+
onMessage(msg) {
|
|
198
|
+
return this.handleMsg(this.parseMsg(msg));
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Parses a message from a Uint8Array into a {@link OpaqueTransportMessage}.
|
|
202
|
+
* @param msg The message to parse.
|
|
203
|
+
* @returns The parsed message, or null if the message is malformed or invalid.
|
|
204
|
+
*/
|
|
205
|
+
parseMsg(msg) {
|
|
206
|
+
const parsedMsg = this.codec.fromBuffer(msg);
|
|
207
|
+
if (parsedMsg === null) {
|
|
208
|
+
const decodedBuffer = new TextDecoder().decode(msg);
|
|
209
|
+
log?.warn(`${this.clientId} -- received malformed msg: ${decodedBuffer}`);
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
if (import_value.Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
|
|
213
|
+
return {
|
|
214
|
+
...parsedMsg,
|
|
215
|
+
serviceName: parsedMsg.serviceName === null ? void 0 : parsedMsg.serviceName,
|
|
216
|
+
procedureName: parsedMsg.procedureName === null ? void 0 : parsedMsg.procedureName
|
|
217
|
+
};
|
|
218
|
+
} else {
|
|
219
|
+
log?.warn(
|
|
220
|
+
`${this.clientId} -- received invalid msg: ${JSON.stringify(
|
|
221
|
+
parsedMsg
|
|
222
|
+
)}`
|
|
223
|
+
);
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Called when a message is received by this transport.
|
|
229
|
+
* You generally shouldn't need to override this in downstream transport implementations.
|
|
230
|
+
* @param msg The received message.
|
|
231
|
+
*/
|
|
232
|
+
handleMsg(msg) {
|
|
233
|
+
if (!msg) {
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
if (isAck(msg.controlFlags) && import_value.Value.Check(TransportAckSchema, msg)) {
|
|
237
|
+
log?.info(`${this.clientId} -- received ack: ${JSON.stringify(msg)}`);
|
|
238
|
+
if (this.sendBuffer.has(msg.payload.ack)) {
|
|
239
|
+
this.sendBuffer.delete(msg.payload.ack);
|
|
240
|
+
}
|
|
241
|
+
} else {
|
|
242
|
+
log?.info(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
|
|
243
|
+
if (msg.to !== this.clientId) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
this.eventDispatcher.dispatchEvent("message", msg);
|
|
247
|
+
if (!isAck(msg.controlFlags)) {
|
|
248
|
+
const ackMsg = reply(msg, { ack: msg.id });
|
|
249
|
+
ackMsg.controlFlags = 1 /* AckBit */;
|
|
250
|
+
ackMsg.from = this.clientId;
|
|
251
|
+
this.send(ackMsg);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Adds a listener to this transport.
|
|
257
|
+
* @param the type of event to listen for
|
|
258
|
+
* @param handler The message handler to add.
|
|
259
|
+
*/
|
|
260
|
+
addEventListener(type, handler) {
|
|
261
|
+
this.eventDispatcher.addEventListener(type, handler);
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Removes a listener from this transport.
|
|
265
|
+
* @param the type of event to unlisten on
|
|
266
|
+
* @param handler The message handler to remove.
|
|
267
|
+
*/
|
|
268
|
+
removeEventListener(type, handler) {
|
|
269
|
+
this.eventDispatcher.removeEventListener(type, handler);
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Sends a message over this transport, delegating to the appropriate connection to actually
|
|
273
|
+
* send the message.
|
|
274
|
+
* @param msg The message to send.
|
|
275
|
+
* @returns The ID of the sent message.
|
|
276
|
+
*/
|
|
277
|
+
send(msg) {
|
|
278
|
+
if (this.state === "destroyed") {
|
|
279
|
+
const err = "transport is destroyed, cant send";
|
|
280
|
+
log?.error(`${this.clientId} -- ` + err + `: ${JSON.stringify(msg)}`);
|
|
281
|
+
throw new Error(err);
|
|
282
|
+
} else if (this.state === "closed") {
|
|
283
|
+
log?.info(
|
|
284
|
+
`${this.clientId} -- transport closed when sending, discarding : ${JSON.stringify(
|
|
285
|
+
msg
|
|
286
|
+
)}`
|
|
287
|
+
);
|
|
288
|
+
return msg.id;
|
|
289
|
+
}
|
|
290
|
+
let conn = this.connections.get(msg.to);
|
|
291
|
+
if (!isAck(msg.controlFlags)) {
|
|
292
|
+
this.sendBuffer.set(msg.id, msg);
|
|
293
|
+
}
|
|
294
|
+
if (conn) {
|
|
295
|
+
log?.info(`${this.clientId} -- sending ${JSON.stringify(msg)}`);
|
|
296
|
+
const ok = conn.send(this.codec.toBuffer(msg));
|
|
297
|
+
if (ok) {
|
|
298
|
+
return msg.id;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
log?.info(
|
|
302
|
+
`${this.clientId} -- connection to ${msg.to} not ready, attempting reconnect and queuing ${JSON.stringify(msg)}`
|
|
303
|
+
);
|
|
304
|
+
const outstanding = this.sendQueue.get(msg.to) || [];
|
|
305
|
+
outstanding.push(msg.id);
|
|
306
|
+
this.sendQueue.set(msg.to, outstanding);
|
|
307
|
+
this.createNewConnection(msg.to);
|
|
308
|
+
return msg.id;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Default close implementation for transports. You should override this in the downstream
|
|
312
|
+
* implementation if you need to do any additional cleanup and call super.close() at the end.
|
|
313
|
+
* Closes the transport. Any messages sent while the transport is closed will be silently discarded.
|
|
314
|
+
*/
|
|
315
|
+
async close() {
|
|
316
|
+
for (const conn of this.connections.values()) {
|
|
317
|
+
conn.close();
|
|
318
|
+
}
|
|
319
|
+
this.connections.clear();
|
|
320
|
+
this.state = "closed";
|
|
321
|
+
log?.info(`${this.clientId} -- closed transport`);
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Default destroy implementation for transports. You should override this in the downstream
|
|
325
|
+
* implementation if you need to do any additional cleanup and call super.destroy() at the end.
|
|
326
|
+
* Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
|
|
327
|
+
*/
|
|
328
|
+
async destroy() {
|
|
329
|
+
for (const conn of this.connections.values()) {
|
|
330
|
+
conn.close();
|
|
331
|
+
}
|
|
332
|
+
this.connections.clear();
|
|
333
|
+
this.state = "destroyed";
|
|
334
|
+
log?.info(`${this.clientId} -- destroyed transport`);
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
// codec/json.ts
|
|
339
|
+
var encoder = new TextEncoder();
|
|
340
|
+
var decoder = new TextDecoder();
|
|
341
|
+
function uint8ArrayToBase64(uint8Array) {
|
|
342
|
+
let binary = "";
|
|
343
|
+
uint8Array.forEach((byte) => {
|
|
344
|
+
binary += String.fromCharCode(byte);
|
|
345
|
+
});
|
|
346
|
+
return btoa(binary);
|
|
347
|
+
}
|
|
348
|
+
function base64ToUint8Array(base64) {
|
|
349
|
+
const binaryString = atob(base64);
|
|
350
|
+
const uint8Array = new Uint8Array(binaryString.length);
|
|
351
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
352
|
+
uint8Array[i] = binaryString.charCodeAt(i);
|
|
353
|
+
}
|
|
354
|
+
return uint8Array;
|
|
355
|
+
}
|
|
356
|
+
var NaiveJsonCodec = {
|
|
357
|
+
toBuffer: (obj) => {
|
|
358
|
+
return encoder.encode(
|
|
359
|
+
JSON.stringify(obj, function replacer(key) {
|
|
360
|
+
let val = this[key];
|
|
361
|
+
if (val instanceof Uint8Array) {
|
|
362
|
+
return { $t: uint8ArrayToBase64(val) };
|
|
363
|
+
} else {
|
|
364
|
+
return val;
|
|
365
|
+
}
|
|
366
|
+
})
|
|
367
|
+
);
|
|
368
|
+
},
|
|
369
|
+
fromBuffer: (buff) => {
|
|
370
|
+
try {
|
|
371
|
+
return JSON.parse(decoder.decode(buff), function reviver(_key, val) {
|
|
372
|
+
if (val?.$t) {
|
|
373
|
+
return base64ToUint8Array(val.$t);
|
|
374
|
+
} else {
|
|
375
|
+
return val;
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
} catch {
|
|
379
|
+
return null;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
// transport/impls/ws/connection.ts
|
|
385
|
+
var WebSocketConnection = class extends Connection {
|
|
386
|
+
ws;
|
|
387
|
+
constructor(transport, connectedTo, ws) {
|
|
388
|
+
super(transport, connectedTo);
|
|
389
|
+
this.ws = ws;
|
|
390
|
+
ws.binaryType = "arraybuffer";
|
|
391
|
+
this.ws.onmessage = (msg) => transport.onMessage(msg.data);
|
|
392
|
+
}
|
|
393
|
+
send(payload) {
|
|
394
|
+
if (this.ws.readyState === this.ws.OPEN) {
|
|
395
|
+
this.ws.send(payload);
|
|
396
|
+
return true;
|
|
397
|
+
} else {
|
|
398
|
+
return false;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
async close() {
|
|
402
|
+
this.ws.close();
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
// transport/impls/ws/client.ts
|
|
407
|
+
var defaultOptions = {
|
|
408
|
+
retryIntervalMs: 250,
|
|
409
|
+
retryAttemptsMax: 5,
|
|
410
|
+
codec: NaiveJsonCodec
|
|
411
|
+
};
|
|
412
|
+
var WebSocketClientTransport = class extends Transport {
|
|
413
|
+
/**
|
|
414
|
+
* A function that returns a Promise that resolves to a WebSocket instance.
|
|
415
|
+
*/
|
|
416
|
+
wsGetter;
|
|
417
|
+
options;
|
|
418
|
+
serverId;
|
|
419
|
+
reconnectPromises;
|
|
420
|
+
tryReconnecting = true;
|
|
421
|
+
/**
|
|
422
|
+
* Creates a new WebSocketTransport instance.
|
|
423
|
+
* @param wsGetter A function that returns a Promise that resolves to a WebSocket instance.
|
|
424
|
+
* @param clientId The ID of the client using the transport.
|
|
425
|
+
* @param providedOptions An optional object containing configuration options for the transport.
|
|
426
|
+
*/
|
|
427
|
+
constructor(wsGetter, clientId, serverId, providedOptions) {
|
|
428
|
+
const options = { ...defaultOptions, ...providedOptions };
|
|
429
|
+
super(options.codec, clientId);
|
|
430
|
+
this.wsGetter = wsGetter;
|
|
431
|
+
this.serverId = serverId;
|
|
432
|
+
this.options = options;
|
|
433
|
+
this.reconnectPromises = /* @__PURE__ */ new Map();
|
|
434
|
+
this.setupConnectionStatusListeners();
|
|
435
|
+
}
|
|
436
|
+
setupConnectionStatusListeners() {
|
|
437
|
+
this.createNewConnection(this.serverId);
|
|
438
|
+
}
|
|
439
|
+
async createNewConnection(to, attempt = 0) {
|
|
440
|
+
if (this.state === "destroyed") {
|
|
441
|
+
throw new Error("cant reopen a destroyed connection");
|
|
442
|
+
}
|
|
443
|
+
let reconnectPromise = this.reconnectPromises.get(to);
|
|
444
|
+
if (!reconnectPromise) {
|
|
445
|
+
if (!this.tryReconnecting) {
|
|
446
|
+
log?.info(
|
|
447
|
+
`${this.clientId} -- tryReconnecting is false, not attempting reconnect`
|
|
448
|
+
);
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
reconnectPromise = new Promise(async (resolve) => {
|
|
452
|
+
log?.info(`${this.clientId} -- establishing a new websocket to ${to}`);
|
|
453
|
+
const ws = await this.wsGetter(to);
|
|
454
|
+
if (ws.readyState === ws.OPEN) {
|
|
455
|
+
return resolve({ ws });
|
|
456
|
+
}
|
|
457
|
+
if (ws.readyState === ws.CLOSING || ws.readyState === ws.CLOSED) {
|
|
458
|
+
return resolve({ err: "ws is closing or closed" });
|
|
459
|
+
}
|
|
460
|
+
ws.addEventListener("open", function onOpen() {
|
|
461
|
+
ws.removeEventListener("open", onOpen);
|
|
462
|
+
resolve({ ws });
|
|
463
|
+
});
|
|
464
|
+
ws.addEventListener("close", function onClose(evt) {
|
|
465
|
+
ws.removeEventListener("close", onClose);
|
|
466
|
+
resolve({ err: evt.reason });
|
|
467
|
+
});
|
|
468
|
+
});
|
|
469
|
+
this.reconnectPromises.set(to, reconnectPromise);
|
|
470
|
+
}
|
|
471
|
+
const res = await reconnectPromise;
|
|
472
|
+
if ("ws" in res && res.ws.readyState === res.ws.OPEN) {
|
|
473
|
+
if (res.ws === this.connections.get(to)?.ws) {
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
log?.info(`${this.clientId} -- websocket ok`);
|
|
477
|
+
const conn = new WebSocketConnection(this, to, res.ws);
|
|
478
|
+
this.onConnect(conn);
|
|
479
|
+
res.ws.onclose = () => {
|
|
480
|
+
this.reconnectPromises.delete(to);
|
|
481
|
+
this.onDisconnect(conn);
|
|
482
|
+
};
|
|
483
|
+
this.state = "open";
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
this.reconnectPromises.delete(to);
|
|
487
|
+
if (attempt >= this.options.retryAttemptsMax) {
|
|
488
|
+
throw new Error(
|
|
489
|
+
`${this.clientId} -- websocket to ${to} failed after ${attempt} attempts, giving up`
|
|
490
|
+
);
|
|
491
|
+
} else {
|
|
492
|
+
log?.warn(
|
|
493
|
+
`${this.clientId} -- websocket to ${to} failed, trying again in ${this.options.retryIntervalMs * attempt}ms`
|
|
494
|
+
);
|
|
495
|
+
setTimeout(
|
|
496
|
+
() => this.createNewConnection(to, attempt + 1),
|
|
497
|
+
this.options.retryIntervalMs * attempt
|
|
498
|
+
);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
503
|
+
0 && (module.exports = {
|
|
504
|
+
WebSocketClientTransport
|
|
505
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import WebSocket from 'isomorphic-ws';
|
|
2
|
+
import { Transport, TransportClientId } from '../../index.cjs';
|
|
3
|
+
import { C as Codec } from '../../../types-3e5768ec.js';
|
|
4
|
+
import { W as WebSocketConnection } from '../../../connection-f7688cc1.js';
|
|
5
|
+
import '@sinclair/typebox';
|
|
6
|
+
|
|
7
|
+
interface Options {
|
|
8
|
+
retryIntervalMs: number;
|
|
9
|
+
retryAttemptsMax: number;
|
|
10
|
+
codec: Codec;
|
|
11
|
+
}
|
|
12
|
+
type WebSocketResult = {
|
|
13
|
+
ws: WebSocket;
|
|
14
|
+
} | {
|
|
15
|
+
err: string;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* A transport implementation that uses a WebSocket connection with automatic reconnection.
|
|
19
|
+
* @class
|
|
20
|
+
* @extends Transport
|
|
21
|
+
*/
|
|
22
|
+
declare class WebSocketClientTransport extends Transport<WebSocketConnection> {
|
|
23
|
+
/**
|
|
24
|
+
* A function that returns a Promise that resolves to a WebSocket instance.
|
|
25
|
+
*/
|
|
26
|
+
wsGetter: (to: TransportClientId) => Promise<WebSocket>;
|
|
27
|
+
options: Options;
|
|
28
|
+
serverId: TransportClientId;
|
|
29
|
+
reconnectPromises: Map<TransportClientId, Promise<WebSocketResult>>;
|
|
30
|
+
tryReconnecting: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new WebSocketTransport instance.
|
|
33
|
+
* @param wsGetter A function that returns a Promise that resolves to a WebSocket instance.
|
|
34
|
+
* @param clientId The ID of the client using the transport.
|
|
35
|
+
* @param providedOptions An optional object containing configuration options for the transport.
|
|
36
|
+
*/
|
|
37
|
+
constructor(wsGetter: () => Promise<WebSocket>, clientId: TransportClientId, serverId: TransportClientId, providedOptions?: Partial<Options>);
|
|
38
|
+
setupConnectionStatusListeners(): void;
|
|
39
|
+
createNewConnection(to: string, attempt?: number): Promise<void>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { WebSocketClientTransport };
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
/// <reference types="ws" />
|
|
2
1
|
import WebSocket from 'isomorphic-ws';
|
|
3
|
-
import { Transport } from '../../
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
2
|
+
import { Transport, TransportClientId } from '../../index.js';
|
|
3
|
+
import { C as Codec } from '../../../types-3e5768ec.js';
|
|
4
|
+
import { W as WebSocketConnection } from '../../../connection-8e19874c.js';
|
|
5
|
+
import '@sinclair/typebox';
|
|
6
|
+
|
|
7
7
|
interface Options {
|
|
8
8
|
retryIntervalMs: number;
|
|
9
9
|
retryAttemptsMax: number;
|
|
@@ -19,7 +19,7 @@ type WebSocketResult = {
|
|
|
19
19
|
* @class
|
|
20
20
|
* @extends Transport
|
|
21
21
|
*/
|
|
22
|
-
|
|
22
|
+
declare class WebSocketClientTransport extends Transport<WebSocketConnection> {
|
|
23
23
|
/**
|
|
24
24
|
* A function that returns a Promise that resolves to a WebSocket instance.
|
|
25
25
|
*/
|
|
@@ -38,5 +38,5 @@ export declare class WebSocketClientTransport extends Transport<WebSocketConnect
|
|
|
38
38
|
setupConnectionStatusListeners(): void;
|
|
39
39
|
createNewConnection(to: string, attempt?: number): Promise<void>;
|
|
40
40
|
}
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
|
|
42
|
+
export { WebSocketClientTransport };
|
|
@@ -1,101 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import {
|
|
2
|
+
WebSocketClientTransport
|
|
3
|
+
} from "../../../chunk-UU2Z7LDR.js";
|
|
4
|
+
import "../../../chunk-PC65ZFWJ.js";
|
|
5
|
+
import "../../../chunk-R6H2BIMC.js";
|
|
6
|
+
import "../../../chunk-AJQU4AZG.js";
|
|
7
|
+
import "../../../chunk-ZE4MX7DF.js";
|
|
8
|
+
import "../../../chunk-SLUSVGQH.js";
|
|
9
|
+
export {
|
|
10
|
+
WebSocketClientTransport
|
|
9
11
|
};
|
|
10
|
-
/**
|
|
11
|
-
* A transport implementation that uses a WebSocket connection with automatic reconnection.
|
|
12
|
-
* @class
|
|
13
|
-
* @extends Transport
|
|
14
|
-
*/
|
|
15
|
-
export class WebSocketClientTransport extends Transport {
|
|
16
|
-
/**
|
|
17
|
-
* A function that returns a Promise that resolves to a WebSocket instance.
|
|
18
|
-
*/
|
|
19
|
-
wsGetter;
|
|
20
|
-
options;
|
|
21
|
-
serverId;
|
|
22
|
-
reconnectPromises;
|
|
23
|
-
tryReconnecting = true;
|
|
24
|
-
/**
|
|
25
|
-
* Creates a new WebSocketTransport instance.
|
|
26
|
-
* @param wsGetter A function that returns a Promise that resolves to a WebSocket instance.
|
|
27
|
-
* @param clientId The ID of the client using the transport.
|
|
28
|
-
* @param providedOptions An optional object containing configuration options for the transport.
|
|
29
|
-
*/
|
|
30
|
-
constructor(wsGetter, clientId, serverId, providedOptions) {
|
|
31
|
-
const options = { ...defaultOptions, ...providedOptions };
|
|
32
|
-
super(options.codec, clientId);
|
|
33
|
-
this.wsGetter = wsGetter;
|
|
34
|
-
this.serverId = serverId;
|
|
35
|
-
this.options = options;
|
|
36
|
-
this.reconnectPromises = new Map();
|
|
37
|
-
this.setupConnectionStatusListeners();
|
|
38
|
-
}
|
|
39
|
-
setupConnectionStatusListeners() {
|
|
40
|
-
this.createNewConnection(this.serverId);
|
|
41
|
-
}
|
|
42
|
-
async createNewConnection(to, attempt = 0) {
|
|
43
|
-
if (this.state === 'destroyed') {
|
|
44
|
-
throw new Error('cant reopen a destroyed connection');
|
|
45
|
-
}
|
|
46
|
-
let reconnectPromise = this.reconnectPromises.get(to);
|
|
47
|
-
if (!reconnectPromise) {
|
|
48
|
-
if (!this.tryReconnecting) {
|
|
49
|
-
log?.info(`${this.clientId} -- tryReconnecting is false, not attempting reconnect`);
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
reconnectPromise = new Promise(async (resolve) => {
|
|
53
|
-
log?.info(`${this.clientId} -- establishing a new websocket to ${to}`);
|
|
54
|
-
const ws = await this.wsGetter(to);
|
|
55
|
-
if (ws.readyState === ws.OPEN) {
|
|
56
|
-
return resolve({ ws });
|
|
57
|
-
}
|
|
58
|
-
if (ws.readyState === ws.CLOSING || ws.readyState === ws.CLOSED) {
|
|
59
|
-
return resolve({ err: 'ws is closing or closed' });
|
|
60
|
-
}
|
|
61
|
-
ws.addEventListener('open', function onOpen() {
|
|
62
|
-
ws.removeEventListener('open', onOpen);
|
|
63
|
-
resolve({ ws });
|
|
64
|
-
});
|
|
65
|
-
ws.addEventListener('close', function onClose(evt) {
|
|
66
|
-
ws.removeEventListener('close', onClose);
|
|
67
|
-
resolve({ err: evt.reason });
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
this.reconnectPromises.set(to, reconnectPromise);
|
|
71
|
-
}
|
|
72
|
-
const res = await reconnectPromise;
|
|
73
|
-
if ('ws' in res && res.ws.readyState === res.ws.OPEN) {
|
|
74
|
-
if (res.ws === this.connections.get(to)?.ws) {
|
|
75
|
-
// this is our current connection
|
|
76
|
-
// we reach this state when createNewConnection is called multiple times
|
|
77
|
-
// concurrently
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
log?.info(`${this.clientId} -- websocket ok`);
|
|
81
|
-
const conn = new WebSocketConnection(this, to, res.ws);
|
|
82
|
-
this.onConnect(conn);
|
|
83
|
-
res.ws.onclose = () => {
|
|
84
|
-
this.reconnectPromises.delete(to);
|
|
85
|
-
this.onDisconnect(conn);
|
|
86
|
-
};
|
|
87
|
-
this.state = 'open';
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
// otherwise try and reconnect again
|
|
91
|
-
this.reconnectPromises.delete(to);
|
|
92
|
-
if (attempt >= this.options.retryAttemptsMax) {
|
|
93
|
-
throw new Error(`${this.clientId} -- websocket to ${to} failed after ${attempt} attempts, giving up`);
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
// linear backoff
|
|
97
|
-
log?.warn(`${this.clientId} -- websocket to ${to} failed, trying again in ${this.options.retryIntervalMs * attempt}ms`);
|
|
98
|
-
setTimeout(() => this.createNewConnection(to, attempt + 1), this.options.retryIntervalMs * attempt);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|