@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,362 @@
|
|
|
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/index.ts
|
|
21
|
+
var transport_exports = {};
|
|
22
|
+
__export(transport_exports, {
|
|
23
|
+
Connection: () => Connection,
|
|
24
|
+
OpaqueTransportMessageSchema: () => OpaqueTransportMessageSchema,
|
|
25
|
+
Transport: () => Transport,
|
|
26
|
+
TransportMessageSchema: () => TransportMessageSchema,
|
|
27
|
+
msg: () => msg,
|
|
28
|
+
reply: () => reply
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(transport_exports);
|
|
31
|
+
|
|
32
|
+
// transport/transport.ts
|
|
33
|
+
var import_value = require("@sinclair/typebox/value");
|
|
34
|
+
|
|
35
|
+
// transport/message.ts
|
|
36
|
+
var import_typebox = require("@sinclair/typebox");
|
|
37
|
+
var import_nanoid = require("nanoid");
|
|
38
|
+
var TransportMessageSchema = (t) => import_typebox.Type.Object({
|
|
39
|
+
id: import_typebox.Type.String(),
|
|
40
|
+
from: import_typebox.Type.String(),
|
|
41
|
+
to: import_typebox.Type.String(),
|
|
42
|
+
serviceName: import_typebox.Type.Optional(import_typebox.Type.Union([import_typebox.Type.String(), import_typebox.Type.Null()])),
|
|
43
|
+
procedureName: import_typebox.Type.Optional(import_typebox.Type.Union([import_typebox.Type.String(), import_typebox.Type.Null()])),
|
|
44
|
+
streamId: import_typebox.Type.String(),
|
|
45
|
+
controlFlags: import_typebox.Type.Integer(),
|
|
46
|
+
payload: t
|
|
47
|
+
});
|
|
48
|
+
var TransportAckSchema = TransportMessageSchema(
|
|
49
|
+
import_typebox.Type.Object({
|
|
50
|
+
ack: import_typebox.Type.String()
|
|
51
|
+
})
|
|
52
|
+
);
|
|
53
|
+
var ControlMessagePayloadSchema = import_typebox.Type.Object({
|
|
54
|
+
type: import_typebox.Type.Literal("CLOSE")
|
|
55
|
+
});
|
|
56
|
+
var OpaqueTransportMessageSchema = TransportMessageSchema(
|
|
57
|
+
import_typebox.Type.Unknown()
|
|
58
|
+
);
|
|
59
|
+
function msg(from, to, streamId, payload, serviceName, procedureName) {
|
|
60
|
+
return {
|
|
61
|
+
id: (0, import_nanoid.nanoid)(),
|
|
62
|
+
to,
|
|
63
|
+
from,
|
|
64
|
+
serviceName,
|
|
65
|
+
procedureName,
|
|
66
|
+
streamId,
|
|
67
|
+
controlFlags: 0,
|
|
68
|
+
payload
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function reply(msg2, response) {
|
|
72
|
+
return {
|
|
73
|
+
id: (0, import_nanoid.nanoid)(),
|
|
74
|
+
streamId: msg2.streamId,
|
|
75
|
+
controlFlags: 0,
|
|
76
|
+
to: msg2.from,
|
|
77
|
+
from: msg2.to,
|
|
78
|
+
payload: response
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function isAck(controlFlag) {
|
|
82
|
+
return (controlFlag & 1 /* AckBit */) === 1 /* AckBit */;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// logging/index.ts
|
|
86
|
+
var log;
|
|
87
|
+
|
|
88
|
+
// transport/events.ts
|
|
89
|
+
var EventDispatcher = class {
|
|
90
|
+
eventListeners = {};
|
|
91
|
+
numberOfListeners(eventType) {
|
|
92
|
+
return this.eventListeners[eventType]?.size ?? 0;
|
|
93
|
+
}
|
|
94
|
+
addEventListener(eventType, handler) {
|
|
95
|
+
if (!this.eventListeners[eventType]) {
|
|
96
|
+
this.eventListeners[eventType] = /* @__PURE__ */ new Set();
|
|
97
|
+
}
|
|
98
|
+
this.eventListeners[eventType]?.add(handler);
|
|
99
|
+
}
|
|
100
|
+
removeEventListener(eventType, handler) {
|
|
101
|
+
const handlers = this.eventListeners[eventType];
|
|
102
|
+
if (handlers) {
|
|
103
|
+
this.eventListeners[eventType]?.delete(handler);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
dispatchEvent(eventType, event) {
|
|
107
|
+
const handlers = this.eventListeners[eventType];
|
|
108
|
+
if (handlers) {
|
|
109
|
+
for (const handler of handlers) {
|
|
110
|
+
handler(event);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// transport/transport.ts
|
|
117
|
+
var Connection = class {
|
|
118
|
+
connectedTo;
|
|
119
|
+
transport;
|
|
120
|
+
constructor(transport, connectedTo) {
|
|
121
|
+
this.connectedTo = connectedTo;
|
|
122
|
+
this.transport = transport;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
var Transport = class {
|
|
126
|
+
/**
|
|
127
|
+
* A flag indicating whether the transport has been destroyed.
|
|
128
|
+
* A destroyed transport will not attempt to reconnect and cannot be used again.
|
|
129
|
+
*/
|
|
130
|
+
state;
|
|
131
|
+
/**
|
|
132
|
+
* The {@link Codec} used to encode and decode messages.
|
|
133
|
+
*/
|
|
134
|
+
codec;
|
|
135
|
+
/**
|
|
136
|
+
* The client ID of this transport.
|
|
137
|
+
*/
|
|
138
|
+
clientId;
|
|
139
|
+
/**
|
|
140
|
+
* An array of message IDs that are waiting to be sent over the WebSocket connection.
|
|
141
|
+
* This builds up if the WebSocket is down for a period of time.
|
|
142
|
+
*/
|
|
143
|
+
sendQueue;
|
|
144
|
+
/**
|
|
145
|
+
* The buffer of messages that have been sent but not yet acknowledged.
|
|
146
|
+
*/
|
|
147
|
+
sendBuffer;
|
|
148
|
+
/**
|
|
149
|
+
* The map of {@link Connection}s managed by this transport.
|
|
150
|
+
*/
|
|
151
|
+
connections;
|
|
152
|
+
/**
|
|
153
|
+
* The event dispatcher for handling events of type EventTypes.
|
|
154
|
+
*/
|
|
155
|
+
eventDispatcher;
|
|
156
|
+
/**
|
|
157
|
+
* Creates a new Transport instance.
|
|
158
|
+
* @param codec The codec used to encode and decode messages.
|
|
159
|
+
* @param clientId The client ID of this transport.
|
|
160
|
+
*/
|
|
161
|
+
constructor(codec, clientId) {
|
|
162
|
+
this.eventDispatcher = new EventDispatcher();
|
|
163
|
+
this.sendBuffer = /* @__PURE__ */ new Map();
|
|
164
|
+
this.sendQueue = /* @__PURE__ */ new Map();
|
|
165
|
+
this.connections = /* @__PURE__ */ new Map();
|
|
166
|
+
this.codec = codec;
|
|
167
|
+
this.clientId = clientId;
|
|
168
|
+
this.state = "open";
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* The downstream implementation needs to call this when a new connection is established.
|
|
172
|
+
* @param conn The connection object.
|
|
173
|
+
*/
|
|
174
|
+
onConnect(conn) {
|
|
175
|
+
log?.info(`${this.clientId} -- new connection to ${conn.connectedTo}`);
|
|
176
|
+
this.connections.set(conn.connectedTo, conn);
|
|
177
|
+
this.eventDispatcher.dispatchEvent("connectionStatus", {
|
|
178
|
+
status: "connect",
|
|
179
|
+
conn
|
|
180
|
+
});
|
|
181
|
+
const outstanding = this.sendQueue.get(conn.connectedTo);
|
|
182
|
+
if (!outstanding) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
for (const id of outstanding) {
|
|
186
|
+
const msg2 = this.sendBuffer.get(id);
|
|
187
|
+
if (!msg2) {
|
|
188
|
+
log?.warn(
|
|
189
|
+
`${this.clientId} -- tried to resend a message we received an ack for`
|
|
190
|
+
);
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
this.send(msg2);
|
|
194
|
+
}
|
|
195
|
+
this.sendQueue.delete(conn.connectedTo);
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* The downstream implementation needs to call this when a connection is closed.
|
|
199
|
+
* @param conn The connection object.
|
|
200
|
+
*/
|
|
201
|
+
onDisconnect(conn) {
|
|
202
|
+
log?.info(`${this.clientId} -- disconnect from ${conn.connectedTo}`);
|
|
203
|
+
conn.close();
|
|
204
|
+
this.connections.delete(conn.connectedTo);
|
|
205
|
+
this.eventDispatcher.dispatchEvent("connectionStatus", {
|
|
206
|
+
status: "disconnect",
|
|
207
|
+
conn
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Handles a message received by this transport. Thin wrapper around {@link handleMsg} and {@link parseMsg}.
|
|
212
|
+
* @param msg The message to handle.
|
|
213
|
+
*/
|
|
214
|
+
onMessage(msg2) {
|
|
215
|
+
return this.handleMsg(this.parseMsg(msg2));
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Parses a message from a Uint8Array into a {@link OpaqueTransportMessage}.
|
|
219
|
+
* @param msg The message to parse.
|
|
220
|
+
* @returns The parsed message, or null if the message is malformed or invalid.
|
|
221
|
+
*/
|
|
222
|
+
parseMsg(msg2) {
|
|
223
|
+
const parsedMsg = this.codec.fromBuffer(msg2);
|
|
224
|
+
if (parsedMsg === null) {
|
|
225
|
+
const decodedBuffer = new TextDecoder().decode(msg2);
|
|
226
|
+
log?.warn(`${this.clientId} -- received malformed msg: ${decodedBuffer}`);
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
if (import_value.Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
|
|
230
|
+
return {
|
|
231
|
+
...parsedMsg,
|
|
232
|
+
serviceName: parsedMsg.serviceName === null ? void 0 : parsedMsg.serviceName,
|
|
233
|
+
procedureName: parsedMsg.procedureName === null ? void 0 : parsedMsg.procedureName
|
|
234
|
+
};
|
|
235
|
+
} else {
|
|
236
|
+
log?.warn(
|
|
237
|
+
`${this.clientId} -- received invalid msg: ${JSON.stringify(
|
|
238
|
+
parsedMsg
|
|
239
|
+
)}`
|
|
240
|
+
);
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Called when a message is received by this transport.
|
|
246
|
+
* You generally shouldn't need to override this in downstream transport implementations.
|
|
247
|
+
* @param msg The received message.
|
|
248
|
+
*/
|
|
249
|
+
handleMsg(msg2) {
|
|
250
|
+
if (!msg2) {
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
if (isAck(msg2.controlFlags) && import_value.Value.Check(TransportAckSchema, msg2)) {
|
|
254
|
+
log?.info(`${this.clientId} -- received ack: ${JSON.stringify(msg2)}`);
|
|
255
|
+
if (this.sendBuffer.has(msg2.payload.ack)) {
|
|
256
|
+
this.sendBuffer.delete(msg2.payload.ack);
|
|
257
|
+
}
|
|
258
|
+
} else {
|
|
259
|
+
log?.info(`${this.clientId} -- received msg: ${JSON.stringify(msg2)}`);
|
|
260
|
+
if (msg2.to !== this.clientId) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
this.eventDispatcher.dispatchEvent("message", msg2);
|
|
264
|
+
if (!isAck(msg2.controlFlags)) {
|
|
265
|
+
const ackMsg = reply(msg2, { ack: msg2.id });
|
|
266
|
+
ackMsg.controlFlags = 1 /* AckBit */;
|
|
267
|
+
ackMsg.from = this.clientId;
|
|
268
|
+
this.send(ackMsg);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Adds a listener to this transport.
|
|
274
|
+
* @param the type of event to listen for
|
|
275
|
+
* @param handler The message handler to add.
|
|
276
|
+
*/
|
|
277
|
+
addEventListener(type, handler) {
|
|
278
|
+
this.eventDispatcher.addEventListener(type, handler);
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Removes a listener from this transport.
|
|
282
|
+
* @param the type of event to unlisten on
|
|
283
|
+
* @param handler The message handler to remove.
|
|
284
|
+
*/
|
|
285
|
+
removeEventListener(type, handler) {
|
|
286
|
+
this.eventDispatcher.removeEventListener(type, handler);
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Sends a message over this transport, delegating to the appropriate connection to actually
|
|
290
|
+
* send the message.
|
|
291
|
+
* @param msg The message to send.
|
|
292
|
+
* @returns The ID of the sent message.
|
|
293
|
+
*/
|
|
294
|
+
send(msg2) {
|
|
295
|
+
if (this.state === "destroyed") {
|
|
296
|
+
const err = "transport is destroyed, cant send";
|
|
297
|
+
log?.error(`${this.clientId} -- ` + err + `: ${JSON.stringify(msg2)}`);
|
|
298
|
+
throw new Error(err);
|
|
299
|
+
} else if (this.state === "closed") {
|
|
300
|
+
log?.info(
|
|
301
|
+
`${this.clientId} -- transport closed when sending, discarding : ${JSON.stringify(
|
|
302
|
+
msg2
|
|
303
|
+
)}`
|
|
304
|
+
);
|
|
305
|
+
return msg2.id;
|
|
306
|
+
}
|
|
307
|
+
let conn = this.connections.get(msg2.to);
|
|
308
|
+
if (!isAck(msg2.controlFlags)) {
|
|
309
|
+
this.sendBuffer.set(msg2.id, msg2);
|
|
310
|
+
}
|
|
311
|
+
if (conn) {
|
|
312
|
+
log?.info(`${this.clientId} -- sending ${JSON.stringify(msg2)}`);
|
|
313
|
+
const ok = conn.send(this.codec.toBuffer(msg2));
|
|
314
|
+
if (ok) {
|
|
315
|
+
return msg2.id;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
log?.info(
|
|
319
|
+
`${this.clientId} -- connection to ${msg2.to} not ready, attempting reconnect and queuing ${JSON.stringify(msg2)}`
|
|
320
|
+
);
|
|
321
|
+
const outstanding = this.sendQueue.get(msg2.to) || [];
|
|
322
|
+
outstanding.push(msg2.id);
|
|
323
|
+
this.sendQueue.set(msg2.to, outstanding);
|
|
324
|
+
this.createNewConnection(msg2.to);
|
|
325
|
+
return msg2.id;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Default close implementation for transports. You should override this in the downstream
|
|
329
|
+
* implementation if you need to do any additional cleanup and call super.close() at the end.
|
|
330
|
+
* Closes the transport. Any messages sent while the transport is closed will be silently discarded.
|
|
331
|
+
*/
|
|
332
|
+
async close() {
|
|
333
|
+
for (const conn of this.connections.values()) {
|
|
334
|
+
conn.close();
|
|
335
|
+
}
|
|
336
|
+
this.connections.clear();
|
|
337
|
+
this.state = "closed";
|
|
338
|
+
log?.info(`${this.clientId} -- closed transport`);
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Default destroy implementation for transports. You should override this in the downstream
|
|
342
|
+
* implementation if you need to do any additional cleanup and call super.destroy() at the end.
|
|
343
|
+
* Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
|
|
344
|
+
*/
|
|
345
|
+
async destroy() {
|
|
346
|
+
for (const conn of this.connections.values()) {
|
|
347
|
+
conn.close();
|
|
348
|
+
}
|
|
349
|
+
this.connections.clear();
|
|
350
|
+
this.state = "destroyed";
|
|
351
|
+
log?.info(`${this.clientId} -- destroyed transport`);
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
355
|
+
0 && (module.exports = {
|
|
356
|
+
Connection,
|
|
357
|
+
OpaqueTransportMessageSchema,
|
|
358
|
+
Transport,
|
|
359
|
+
TransportMessageSchema,
|
|
360
|
+
msg,
|
|
361
|
+
reply
|
|
362
|
+
});
|
|
@@ -1,6 +1,117 @@
|
|
|
1
|
-
import { Codec } from '../
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
1
|
+
import { C as Codec } from '../types-3e5768ec.js';
|
|
2
|
+
import * as _sinclair_typebox from '@sinclair/typebox';
|
|
3
|
+
import { TSchema } from '@sinclair/typebox';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generic Typebox schema for a transport message.
|
|
7
|
+
* @template T The type of the payload.
|
|
8
|
+
* @param {T} t The payload schema.
|
|
9
|
+
* @returns The transport message schema.
|
|
10
|
+
*/
|
|
11
|
+
declare const TransportMessageSchema: <T extends TSchema>(t: T) => _sinclair_typebox.TObject<{
|
|
12
|
+
id: _sinclair_typebox.TString;
|
|
13
|
+
from: _sinclair_typebox.TString;
|
|
14
|
+
to: _sinclair_typebox.TString;
|
|
15
|
+
serviceName: _sinclair_typebox.TOptional<_sinclair_typebox.TUnion<[_sinclair_typebox.TString, _sinclair_typebox.TNull]>>;
|
|
16
|
+
procedureName: _sinclair_typebox.TOptional<_sinclair_typebox.TUnion<[_sinclair_typebox.TString, _sinclair_typebox.TNull]>>;
|
|
17
|
+
streamId: _sinclair_typebox.TString;
|
|
18
|
+
controlFlags: _sinclair_typebox.TInteger;
|
|
19
|
+
payload: T;
|
|
20
|
+
}>;
|
|
21
|
+
/**
|
|
22
|
+
* Defines the schema for an opaque transport message that is agnostic to any
|
|
23
|
+
* procedure/service.
|
|
24
|
+
* @returns The transport message schema.
|
|
25
|
+
*/
|
|
26
|
+
declare const OpaqueTransportMessageSchema: _sinclair_typebox.TObject<{
|
|
27
|
+
id: _sinclair_typebox.TString;
|
|
28
|
+
from: _sinclair_typebox.TString;
|
|
29
|
+
to: _sinclair_typebox.TString;
|
|
30
|
+
serviceName: _sinclair_typebox.TOptional<_sinclair_typebox.TUnion<[_sinclair_typebox.TString, _sinclair_typebox.TNull]>>;
|
|
31
|
+
procedureName: _sinclair_typebox.TOptional<_sinclair_typebox.TUnion<[_sinclair_typebox.TString, _sinclair_typebox.TNull]>>;
|
|
32
|
+
streamId: _sinclair_typebox.TString;
|
|
33
|
+
controlFlags: _sinclair_typebox.TInteger;
|
|
34
|
+
payload: _sinclair_typebox.TUnknown;
|
|
35
|
+
}>;
|
|
36
|
+
/**
|
|
37
|
+
* Represents a transport message. This is the same type as {@link TransportMessageSchema} but
|
|
38
|
+
* we can't statically infer generics from generic Typebox schemas so we have to define it again here.
|
|
39
|
+
*
|
|
40
|
+
* TypeScript can't enforce types when a bitmask is involved, so these are the semantics of
|
|
41
|
+
* `controlFlags`:
|
|
42
|
+
* * If `controlFlags & StreamOpenBit == StreamOpenBit`, `streamId` must be set to a unique value
|
|
43
|
+
* (suggestion: use `nanoid`).
|
|
44
|
+
* * If `controlFlags & StreamOpenBit == StreamOpenBit`, `serviceName` and `procedureName` must be set.
|
|
45
|
+
* * If `controlFlags & StreamClosedBit == StreamClosedBit` and the kind is `stream` or `subscription`,
|
|
46
|
+
* `payload` should be discarded (usually contains a control message).
|
|
47
|
+
* @template Payload The type of the payload.
|
|
48
|
+
*/
|
|
49
|
+
type TransportMessage<Payload extends Record<string, unknown> | unknown = Record<string, unknown>> = {
|
|
50
|
+
id: string;
|
|
51
|
+
from: string;
|
|
52
|
+
to: string;
|
|
53
|
+
serviceName?: string;
|
|
54
|
+
procedureName?: string;
|
|
55
|
+
streamId: string;
|
|
56
|
+
controlFlags: number;
|
|
57
|
+
payload: Payload;
|
|
58
|
+
};
|
|
59
|
+
type MessageId = string;
|
|
60
|
+
/**
|
|
61
|
+
* A type alias for a transport message with an opaque payload.
|
|
62
|
+
* @template T - The type of the opaque payload.
|
|
63
|
+
*/
|
|
64
|
+
type OpaqueTransportMessage = TransportMessage<unknown>;
|
|
65
|
+
type TransportClientId = string;
|
|
66
|
+
/**
|
|
67
|
+
* Creates a transport message with the given parameters. You shouldn't need to call this manually unless
|
|
68
|
+
* you're writing a test.
|
|
69
|
+
* @param from The sender of the message.
|
|
70
|
+
* @param to The intended recipient of the message.
|
|
71
|
+
* @param service The name of the service the message is intended for.
|
|
72
|
+
* @param proc The name of the procedure the message is intended for.
|
|
73
|
+
* @param stream The ID of the stream the message is intended for.
|
|
74
|
+
* @param payload The payload of the message.
|
|
75
|
+
* @returns A TransportMessage object with the given parameters.
|
|
76
|
+
*/
|
|
77
|
+
declare function msg<Payload extends object>(from: string, to: string, streamId: string, payload: Payload, serviceName?: string, procedureName?: string): TransportMessage<Payload>;
|
|
78
|
+
/**
|
|
79
|
+
* Creates a new transport message as a response to the given message.
|
|
80
|
+
* @param msg The original message to respond to.
|
|
81
|
+
* @param response The payload of the response message.
|
|
82
|
+
* @returns A new transport message with appropriate to, from, and payload fields
|
|
83
|
+
*/
|
|
84
|
+
declare function reply<Payload extends object>(msg: OpaqueTransportMessage, response: Payload): TransportMessage<Payload>;
|
|
85
|
+
/**
|
|
86
|
+
* Checks if the given control flag (usually found in msg.controlFlag) is a stream open message.
|
|
87
|
+
* @param controlFlag - The control flag to check.
|
|
88
|
+
* @returns True if the control flag contains the StreamOpenBit, false otherwise.
|
|
89
|
+
*/
|
|
90
|
+
declare function isStreamOpen(controlFlag: number): boolean;
|
|
91
|
+
/**
|
|
92
|
+
* Checks if the given control flag (usually found in msg.controlFlag) is a stream close message.
|
|
93
|
+
* @param controlFlag - The control flag to check.
|
|
94
|
+
* @returns True if the control flag contains the StreamCloseBit, false otherwise.
|
|
95
|
+
*/
|
|
96
|
+
declare function isStreamClose(controlFlag: number): boolean;
|
|
97
|
+
|
|
98
|
+
interface EventMap {
|
|
99
|
+
message: OpaqueTransportMessage;
|
|
100
|
+
connectionStatus: {
|
|
101
|
+
status: 'connect' | 'disconnect';
|
|
102
|
+
conn: Connection;
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
type EventTypes = keyof EventMap;
|
|
106
|
+
type EventHandler<K extends EventTypes> = (event: EventMap[K]) => void;
|
|
107
|
+
declare class EventDispatcher<T extends EventTypes> {
|
|
108
|
+
private eventListeners;
|
|
109
|
+
numberOfListeners<K extends T>(eventType: K): number;
|
|
110
|
+
addEventListener<K extends T>(eventType: K, handler: EventHandler<K>): void;
|
|
111
|
+
removeEventListener<K extends T>(eventType: K, handler: EventHandler<K>): void;
|
|
112
|
+
dispatchEvent<K extends T>(eventType: K, event: EventMap[K]): void;
|
|
113
|
+
}
|
|
114
|
+
|
|
4
115
|
/**
|
|
5
116
|
* A 1:1 connection between two transports. Once this is created,
|
|
6
117
|
* the {@link Connection} is expected to take over responsibility for
|
|
@@ -12,14 +123,14 @@ import { EventDispatcher, EventHandler, EventTypes } from './events';
|
|
|
12
123
|
* connection in the {@link connections} map via `msg.to` and calls {@link send}(bytes)
|
|
13
124
|
* so the connection can send it.
|
|
14
125
|
*/
|
|
15
|
-
|
|
126
|
+
declare abstract class Connection {
|
|
16
127
|
connectedTo: TransportClientId;
|
|
17
128
|
transport: Transport<Connection>;
|
|
18
129
|
constructor(transport: Transport<Connection>, connectedTo: TransportClientId);
|
|
19
130
|
abstract send(msg: Uint8Array): boolean;
|
|
20
131
|
abstract close(): void;
|
|
21
132
|
}
|
|
22
|
-
|
|
133
|
+
type TransportStatus = 'open' | 'closed' | 'destroyed';
|
|
23
134
|
/**
|
|
24
135
|
* Transports manage the lifecycle (creation/deletion) of connections. Its responsibilities include:
|
|
25
136
|
*
|
|
@@ -50,7 +161,7 @@ export type TransportStatus = 'open' | 'closed' | 'destroyed';
|
|
|
50
161
|
* ```
|
|
51
162
|
* @abstract
|
|
52
163
|
*/
|
|
53
|
-
|
|
164
|
+
declare abstract class Transport<ConnType extends Connection> {
|
|
54
165
|
/**
|
|
55
166
|
* A flag indicating whether the transport has been destroyed.
|
|
56
167
|
* A destroyed transport will not attempt to reconnect and cannot be used again.
|
|
@@ -158,4 +269,5 @@ export declare abstract class Transport<ConnType extends Connection> {
|
|
|
158
269
|
*/
|
|
159
270
|
destroy(): Promise<void>;
|
|
160
271
|
}
|
|
161
|
-
|
|
272
|
+
|
|
273
|
+
export { Connection, MessageId, OpaqueTransportMessage, OpaqueTransportMessageSchema, Transport, TransportClientId, TransportMessage, TransportMessageSchema, isStreamClose, isStreamOpen, msg, reply };
|