@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.
Files changed (135) hide show
  1. package/dist/{router/builder.d.ts → builder-3c4485f0.d.ts} +76 -21
  2. package/dist/chunk-7WJ6YLE5.js +683 -0
  3. package/dist/chunk-AJQU4AZG.js +284 -0
  4. package/dist/chunk-ORAG7IAU.js +0 -0
  5. package/dist/chunk-PC65ZFWJ.js +29 -0
  6. package/dist/chunk-R6H2BIMC.js +49 -0
  7. package/dist/chunk-RGMHF6PF.js +65 -0
  8. package/dist/chunk-SLUSVGQH.js +30 -0
  9. package/dist/chunk-UU2Z7LDR.js +113 -0
  10. package/dist/chunk-WVT5QXMZ.js +20 -0
  11. package/dist/chunk-ZE4MX7DF.js +75 -0
  12. package/dist/codec/index.cjs +94 -0
  13. package/dist/codec/index.d.cts +15 -0
  14. package/dist/codec/index.d.ts +15 -4
  15. package/dist/codec/index.js +10 -2
  16. package/dist/connection-8e19874c.d.ts +11 -0
  17. package/dist/connection-f7688cc1.d.ts +11 -0
  18. package/dist/logging/index.cjs +56 -0
  19. package/dist/logging/index.d.cts +28 -0
  20. package/dist/logging/index.d.ts +6 -6
  21. package/dist/logging/index.js +9 -40
  22. package/dist/router/index.cjs +770 -0
  23. package/dist/router/index.d.cts +114 -0
  24. package/dist/router/index.d.ts +114 -12
  25. package/dist/router/index.js +24 -5
  26. package/dist/transport/impls/ws/client.cjs +505 -0
  27. package/dist/transport/impls/ws/client.d.cts +42 -0
  28. package/dist/transport/impls/ws/client.d.ts +8 -8
  29. package/dist/transport/impls/ws/client.js +10 -100
  30. package/dist/transport/impls/ws/server.cjs +457 -0
  31. package/dist/transport/impls/ws/server.d.cts +21 -0
  32. package/dist/transport/impls/ws/server.d.ts +11 -10
  33. package/dist/transport/impls/ws/server.js +11 -52
  34. package/dist/transport/index.cjs +362 -0
  35. package/dist/transport/{transport.d.ts → index.d.cts} +119 -7
  36. package/dist/transport/index.d.ts +273 -4
  37. package/dist/transport/index.js +20 -2
  38. package/dist/{codec/types.d.ts → types-3e5768ec.d.ts} +3 -2
  39. package/dist/util/testHelpers.cjs +731 -0
  40. package/dist/util/testHelpers.d.cts +79 -0
  41. package/dist/util/testHelpers.d.ts +22 -19
  42. package/dist/util/testHelpers.js +135 -163
  43. package/package.json +41 -13
  44. package/dist/__tests__/bandwidth.bench.d.ts +0 -2
  45. package/dist/__tests__/bandwidth.bench.d.ts.map +0 -1
  46. package/dist/__tests__/bandwidth.bench.js +0 -90
  47. package/dist/__tests__/cleanup.test.d.ts +0 -2
  48. package/dist/__tests__/cleanup.test.d.ts.map +0 -1
  49. package/dist/__tests__/cleanup.test.js +0 -165
  50. package/dist/__tests__/disconnects.test.d.ts +0 -2
  51. package/dist/__tests__/disconnects.test.d.ts.map +0 -1
  52. package/dist/__tests__/disconnects.test.js +0 -163
  53. package/dist/__tests__/e2e.test.d.ts +0 -2
  54. package/dist/__tests__/e2e.test.d.ts.map +0 -1
  55. package/dist/__tests__/e2e.test.js +0 -317
  56. package/dist/__tests__/fixtures/cleanup.d.ts +0 -12
  57. package/dist/__tests__/fixtures/cleanup.d.ts.map +0 -1
  58. package/dist/__tests__/fixtures/cleanup.js +0 -36
  59. package/dist/__tests__/fixtures/largePayload.json +0 -33
  60. package/dist/__tests__/fixtures/observable.d.ts +0 -26
  61. package/dist/__tests__/fixtures/observable.d.ts.map +0 -1
  62. package/dist/__tests__/fixtures/observable.js +0 -38
  63. package/dist/__tests__/fixtures/observable.test.d.ts +0 -2
  64. package/dist/__tests__/fixtures/observable.test.d.ts.map +0 -1
  65. package/dist/__tests__/fixtures/observable.test.js +0 -39
  66. package/dist/__tests__/fixtures/services.d.ts +0 -288
  67. package/dist/__tests__/fixtures/services.d.ts.map +0 -1
  68. package/dist/__tests__/fixtures/services.js +0 -207
  69. package/dist/__tests__/handler.test.d.ts +0 -2
  70. package/dist/__tests__/handler.test.d.ts.map +0 -1
  71. package/dist/__tests__/handler.test.js +0 -120
  72. package/dist/__tests__/serialize.test.d.ts +0 -2
  73. package/dist/__tests__/serialize.test.d.ts.map +0 -1
  74. package/dist/__tests__/serialize.test.js +0 -208
  75. package/dist/__tests__/typescript-stress.test.d.ts +0 -1583
  76. package/dist/__tests__/typescript-stress.test.d.ts.map +0 -1
  77. package/dist/__tests__/typescript-stress.test.js +0 -123
  78. package/dist/codec/binary.d.ts +0 -7
  79. package/dist/codec/binary.d.ts.map +0 -1
  80. package/dist/codec/binary.js +0 -20
  81. package/dist/codec/codec.test.d.ts +0 -5
  82. package/dist/codec/codec.test.d.ts.map +0 -1
  83. package/dist/codec/codec.test.js +0 -41
  84. package/dist/codec/index.d.ts.map +0 -1
  85. package/dist/codec/json.d.ts +0 -7
  86. package/dist/codec/json.d.ts.map +0 -1
  87. package/dist/codec/json.js +0 -51
  88. package/dist/codec/types.d.ts.map +0 -1
  89. package/dist/codec/types.js +0 -1
  90. package/dist/logging/index.d.ts.map +0 -1
  91. package/dist/router/builder.d.ts.map +0 -1
  92. package/dist/router/builder.js +0 -91
  93. package/dist/router/client.d.ts +0 -72
  94. package/dist/router/client.d.ts.map +0 -1
  95. package/dist/router/client.js +0 -257
  96. package/dist/router/context.d.ts +0 -30
  97. package/dist/router/context.d.ts.map +0 -1
  98. package/dist/router/context.js +0 -1
  99. package/dist/router/defs.d.ts +0 -16
  100. package/dist/router/defs.d.ts.map +0 -1
  101. package/dist/router/defs.js +0 -11
  102. package/dist/router/index.d.ts.map +0 -1
  103. package/dist/router/result.d.ts +0 -26
  104. package/dist/router/result.d.ts.map +0 -1
  105. package/dist/router/result.js +0 -22
  106. package/dist/router/server.d.ts +0 -39
  107. package/dist/router/server.d.ts.map +0 -1
  108. package/dist/router/server.js +0 -260
  109. package/dist/transport/events.d.ts +0 -19
  110. package/dist/transport/events.d.ts.map +0 -1
  111. package/dist/transport/events.js +0 -26
  112. package/dist/transport/impls/stdio/stdio.d.ts +0 -33
  113. package/dist/transport/impls/stdio/stdio.d.ts.map +0 -1
  114. package/dist/transport/impls/stdio/stdio.js +0 -75
  115. package/dist/transport/impls/stdio/stdio.test.d.ts +0 -2
  116. package/dist/transport/impls/stdio/stdio.test.d.ts.map +0 -1
  117. package/dist/transport/impls/stdio/stdio.test.js +0 -24
  118. package/dist/transport/impls/ws/client.d.ts.map +0 -1
  119. package/dist/transport/impls/ws/connection.d.ts +0 -11
  120. package/dist/transport/impls/ws/connection.d.ts.map +0 -1
  121. package/dist/transport/impls/ws/connection.js +0 -23
  122. package/dist/transport/impls/ws/server.d.ts.map +0 -1
  123. package/dist/transport/impls/ws/ws.test.d.ts +0 -2
  124. package/dist/transport/impls/ws/ws.test.d.ts.map +0 -1
  125. package/dist/transport/impls/ws/ws.test.js +0 -185
  126. package/dist/transport/index.d.ts.map +0 -1
  127. package/dist/transport/message.d.ts +0 -142
  128. package/dist/transport/message.d.ts.map +0 -1
  129. package/dist/transport/message.js +0 -113
  130. package/dist/transport/message.test.d.ts +0 -2
  131. package/dist/transport/message.test.d.ts.map +0 -1
  132. package/dist/transport/message.test.js +0 -52
  133. package/dist/transport/transport.d.ts.map +0 -1
  134. package/dist/transport/transport.js +0 -281
  135. package/dist/util/testHelpers.d.ts.map +0 -1
@@ -0,0 +1,731 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // util/testHelpers.ts
31
+ var testHelpers_exports = {};
32
+ __export(testHelpers_exports, {
33
+ asClientRpc: () => asClientRpc,
34
+ asClientStream: () => asClientStream,
35
+ asClientSubscription: () => asClientSubscription,
36
+ asClientUpload: () => asClientUpload,
37
+ createDummyTransportMessage: () => createDummyTransportMessage,
38
+ createLocalWebSocketClient: () => createLocalWebSocketClient,
39
+ createWebSocketServer: () => createWebSocketServer,
40
+ createWsTransports: () => createWsTransports,
41
+ iterNext: () => iterNext,
42
+ onServerReady: () => onServerReady,
43
+ payloadToTransportMessage: () => payloadToTransportMessage,
44
+ waitForMessage: () => waitForMessage
45
+ });
46
+ module.exports = __toCommonJS(testHelpers_exports);
47
+ var import_isomorphic_ws = __toESM(require("isomorphic-ws"), 1);
48
+ var import_ws = require("ws");
49
+
50
+ // transport/transport.ts
51
+ var import_value = require("@sinclair/typebox/value");
52
+
53
+ // transport/message.ts
54
+ var import_typebox = require("@sinclair/typebox");
55
+ var import_nanoid = require("nanoid");
56
+ var TransportMessageSchema = (t) => import_typebox.Type.Object({
57
+ id: import_typebox.Type.String(),
58
+ from: import_typebox.Type.String(),
59
+ to: import_typebox.Type.String(),
60
+ serviceName: import_typebox.Type.Optional(import_typebox.Type.Union([import_typebox.Type.String(), import_typebox.Type.Null()])),
61
+ procedureName: import_typebox.Type.Optional(import_typebox.Type.Union([import_typebox.Type.String(), import_typebox.Type.Null()])),
62
+ streamId: import_typebox.Type.String(),
63
+ controlFlags: import_typebox.Type.Integer(),
64
+ payload: t
65
+ });
66
+ var TransportAckSchema = TransportMessageSchema(
67
+ import_typebox.Type.Object({
68
+ ack: import_typebox.Type.String()
69
+ })
70
+ );
71
+ var ControlMessagePayloadSchema = import_typebox.Type.Object({
72
+ type: import_typebox.Type.Literal("CLOSE")
73
+ });
74
+ var OpaqueTransportMessageSchema = TransportMessageSchema(
75
+ import_typebox.Type.Unknown()
76
+ );
77
+ function msg(from, to, streamId, payload, serviceName, procedureName) {
78
+ return {
79
+ id: (0, import_nanoid.nanoid)(),
80
+ to,
81
+ from,
82
+ serviceName,
83
+ procedureName,
84
+ streamId,
85
+ controlFlags: 0,
86
+ payload
87
+ };
88
+ }
89
+ function reply(msg2, response) {
90
+ return {
91
+ id: (0, import_nanoid.nanoid)(),
92
+ streamId: msg2.streamId,
93
+ controlFlags: 0,
94
+ to: msg2.from,
95
+ from: msg2.to,
96
+ payload: response
97
+ };
98
+ }
99
+ function isAck(controlFlag) {
100
+ return (controlFlag & 1 /* AckBit */) === 1 /* AckBit */;
101
+ }
102
+
103
+ // logging/index.ts
104
+ var log;
105
+
106
+ // transport/events.ts
107
+ var EventDispatcher = class {
108
+ eventListeners = {};
109
+ numberOfListeners(eventType) {
110
+ return this.eventListeners[eventType]?.size ?? 0;
111
+ }
112
+ addEventListener(eventType, handler) {
113
+ if (!this.eventListeners[eventType]) {
114
+ this.eventListeners[eventType] = /* @__PURE__ */ new Set();
115
+ }
116
+ this.eventListeners[eventType]?.add(handler);
117
+ }
118
+ removeEventListener(eventType, handler) {
119
+ const handlers = this.eventListeners[eventType];
120
+ if (handlers) {
121
+ this.eventListeners[eventType]?.delete(handler);
122
+ }
123
+ }
124
+ dispatchEvent(eventType, event) {
125
+ const handlers = this.eventListeners[eventType];
126
+ if (handlers) {
127
+ for (const handler of handlers) {
128
+ handler(event);
129
+ }
130
+ }
131
+ }
132
+ };
133
+
134
+ // transport/transport.ts
135
+ var Connection = class {
136
+ connectedTo;
137
+ transport;
138
+ constructor(transport, connectedTo) {
139
+ this.connectedTo = connectedTo;
140
+ this.transport = transport;
141
+ }
142
+ };
143
+ var Transport = class {
144
+ /**
145
+ * A flag indicating whether the transport has been destroyed.
146
+ * A destroyed transport will not attempt to reconnect and cannot be used again.
147
+ */
148
+ state;
149
+ /**
150
+ * The {@link Codec} used to encode and decode messages.
151
+ */
152
+ codec;
153
+ /**
154
+ * The client ID of this transport.
155
+ */
156
+ clientId;
157
+ /**
158
+ * An array of message IDs that are waiting to be sent over the WebSocket connection.
159
+ * This builds up if the WebSocket is down for a period of time.
160
+ */
161
+ sendQueue;
162
+ /**
163
+ * The buffer of messages that have been sent but not yet acknowledged.
164
+ */
165
+ sendBuffer;
166
+ /**
167
+ * The map of {@link Connection}s managed by this transport.
168
+ */
169
+ connections;
170
+ /**
171
+ * The event dispatcher for handling events of type EventTypes.
172
+ */
173
+ eventDispatcher;
174
+ /**
175
+ * Creates a new Transport instance.
176
+ * @param codec The codec used to encode and decode messages.
177
+ * @param clientId The client ID of this transport.
178
+ */
179
+ constructor(codec, clientId) {
180
+ this.eventDispatcher = new EventDispatcher();
181
+ this.sendBuffer = /* @__PURE__ */ new Map();
182
+ this.sendQueue = /* @__PURE__ */ new Map();
183
+ this.connections = /* @__PURE__ */ new Map();
184
+ this.codec = codec;
185
+ this.clientId = clientId;
186
+ this.state = "open";
187
+ }
188
+ /**
189
+ * The downstream implementation needs to call this when a new connection is established.
190
+ * @param conn The connection object.
191
+ */
192
+ onConnect(conn) {
193
+ log?.info(`${this.clientId} -- new connection to ${conn.connectedTo}`);
194
+ this.connections.set(conn.connectedTo, conn);
195
+ this.eventDispatcher.dispatchEvent("connectionStatus", {
196
+ status: "connect",
197
+ conn
198
+ });
199
+ const outstanding = this.sendQueue.get(conn.connectedTo);
200
+ if (!outstanding) {
201
+ return;
202
+ }
203
+ for (const id of outstanding) {
204
+ const msg2 = this.sendBuffer.get(id);
205
+ if (!msg2) {
206
+ log?.warn(
207
+ `${this.clientId} -- tried to resend a message we received an ack for`
208
+ );
209
+ continue;
210
+ }
211
+ this.send(msg2);
212
+ }
213
+ this.sendQueue.delete(conn.connectedTo);
214
+ }
215
+ /**
216
+ * The downstream implementation needs to call this when a connection is closed.
217
+ * @param conn The connection object.
218
+ */
219
+ onDisconnect(conn) {
220
+ log?.info(`${this.clientId} -- disconnect from ${conn.connectedTo}`);
221
+ conn.close();
222
+ this.connections.delete(conn.connectedTo);
223
+ this.eventDispatcher.dispatchEvent("connectionStatus", {
224
+ status: "disconnect",
225
+ conn
226
+ });
227
+ }
228
+ /**
229
+ * Handles a message received by this transport. Thin wrapper around {@link handleMsg} and {@link parseMsg}.
230
+ * @param msg The message to handle.
231
+ */
232
+ onMessage(msg2) {
233
+ return this.handleMsg(this.parseMsg(msg2));
234
+ }
235
+ /**
236
+ * Parses a message from a Uint8Array into a {@link OpaqueTransportMessage}.
237
+ * @param msg The message to parse.
238
+ * @returns The parsed message, or null if the message is malformed or invalid.
239
+ */
240
+ parseMsg(msg2) {
241
+ const parsedMsg = this.codec.fromBuffer(msg2);
242
+ if (parsedMsg === null) {
243
+ const decodedBuffer = new TextDecoder().decode(msg2);
244
+ log?.warn(`${this.clientId} -- received malformed msg: ${decodedBuffer}`);
245
+ return null;
246
+ }
247
+ if (import_value.Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
248
+ return {
249
+ ...parsedMsg,
250
+ serviceName: parsedMsg.serviceName === null ? void 0 : parsedMsg.serviceName,
251
+ procedureName: parsedMsg.procedureName === null ? void 0 : parsedMsg.procedureName
252
+ };
253
+ } else {
254
+ log?.warn(
255
+ `${this.clientId} -- received invalid msg: ${JSON.stringify(
256
+ parsedMsg
257
+ )}`
258
+ );
259
+ return null;
260
+ }
261
+ }
262
+ /**
263
+ * Called when a message is received by this transport.
264
+ * You generally shouldn't need to override this in downstream transport implementations.
265
+ * @param msg The received message.
266
+ */
267
+ handleMsg(msg2) {
268
+ if (!msg2) {
269
+ return;
270
+ }
271
+ if (isAck(msg2.controlFlags) && import_value.Value.Check(TransportAckSchema, msg2)) {
272
+ log?.info(`${this.clientId} -- received ack: ${JSON.stringify(msg2)}`);
273
+ if (this.sendBuffer.has(msg2.payload.ack)) {
274
+ this.sendBuffer.delete(msg2.payload.ack);
275
+ }
276
+ } else {
277
+ log?.info(`${this.clientId} -- received msg: ${JSON.stringify(msg2)}`);
278
+ if (msg2.to !== this.clientId) {
279
+ return;
280
+ }
281
+ this.eventDispatcher.dispatchEvent("message", msg2);
282
+ if (!isAck(msg2.controlFlags)) {
283
+ const ackMsg = reply(msg2, { ack: msg2.id });
284
+ ackMsg.controlFlags = 1 /* AckBit */;
285
+ ackMsg.from = this.clientId;
286
+ this.send(ackMsg);
287
+ }
288
+ }
289
+ }
290
+ /**
291
+ * Adds a listener to this transport.
292
+ * @param the type of event to listen for
293
+ * @param handler The message handler to add.
294
+ */
295
+ addEventListener(type, handler) {
296
+ this.eventDispatcher.addEventListener(type, handler);
297
+ }
298
+ /**
299
+ * Removes a listener from this transport.
300
+ * @param the type of event to unlisten on
301
+ * @param handler The message handler to remove.
302
+ */
303
+ removeEventListener(type, handler) {
304
+ this.eventDispatcher.removeEventListener(type, handler);
305
+ }
306
+ /**
307
+ * Sends a message over this transport, delegating to the appropriate connection to actually
308
+ * send the message.
309
+ * @param msg The message to send.
310
+ * @returns The ID of the sent message.
311
+ */
312
+ send(msg2) {
313
+ if (this.state === "destroyed") {
314
+ const err = "transport is destroyed, cant send";
315
+ log?.error(`${this.clientId} -- ` + err + `: ${JSON.stringify(msg2)}`);
316
+ throw new Error(err);
317
+ } else if (this.state === "closed") {
318
+ log?.info(
319
+ `${this.clientId} -- transport closed when sending, discarding : ${JSON.stringify(
320
+ msg2
321
+ )}`
322
+ );
323
+ return msg2.id;
324
+ }
325
+ let conn = this.connections.get(msg2.to);
326
+ if (!isAck(msg2.controlFlags)) {
327
+ this.sendBuffer.set(msg2.id, msg2);
328
+ }
329
+ if (conn) {
330
+ log?.info(`${this.clientId} -- sending ${JSON.stringify(msg2)}`);
331
+ const ok = conn.send(this.codec.toBuffer(msg2));
332
+ if (ok) {
333
+ return msg2.id;
334
+ }
335
+ }
336
+ log?.info(
337
+ `${this.clientId} -- connection to ${msg2.to} not ready, attempting reconnect and queuing ${JSON.stringify(msg2)}`
338
+ );
339
+ const outstanding = this.sendQueue.get(msg2.to) || [];
340
+ outstanding.push(msg2.id);
341
+ this.sendQueue.set(msg2.to, outstanding);
342
+ this.createNewConnection(msg2.to);
343
+ return msg2.id;
344
+ }
345
+ /**
346
+ * Default close implementation for transports. You should override this in the downstream
347
+ * implementation if you need to do any additional cleanup and call super.close() at the end.
348
+ * Closes the transport. Any messages sent while the transport is closed will be silently discarded.
349
+ */
350
+ async close() {
351
+ for (const conn of this.connections.values()) {
352
+ conn.close();
353
+ }
354
+ this.connections.clear();
355
+ this.state = "closed";
356
+ log?.info(`${this.clientId} -- closed transport`);
357
+ }
358
+ /**
359
+ * Default destroy implementation for transports. You should override this in the downstream
360
+ * implementation if you need to do any additional cleanup and call super.destroy() at the end.
361
+ * Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
362
+ */
363
+ async destroy() {
364
+ for (const conn of this.connections.values()) {
365
+ conn.close();
366
+ }
367
+ this.connections.clear();
368
+ this.state = "destroyed";
369
+ log?.info(`${this.clientId} -- destroyed transport`);
370
+ }
371
+ };
372
+
373
+ // codec/json.ts
374
+ var encoder = new TextEncoder();
375
+ var decoder = new TextDecoder();
376
+ function uint8ArrayToBase64(uint8Array) {
377
+ let binary = "";
378
+ uint8Array.forEach((byte) => {
379
+ binary += String.fromCharCode(byte);
380
+ });
381
+ return btoa(binary);
382
+ }
383
+ function base64ToUint8Array(base64) {
384
+ const binaryString = atob(base64);
385
+ const uint8Array = new Uint8Array(binaryString.length);
386
+ for (let i = 0; i < binaryString.length; i++) {
387
+ uint8Array[i] = binaryString.charCodeAt(i);
388
+ }
389
+ return uint8Array;
390
+ }
391
+ var NaiveJsonCodec = {
392
+ toBuffer: (obj) => {
393
+ return encoder.encode(
394
+ JSON.stringify(obj, function replacer(key) {
395
+ let val = this[key];
396
+ if (val instanceof Uint8Array) {
397
+ return { $t: uint8ArrayToBase64(val) };
398
+ } else {
399
+ return val;
400
+ }
401
+ })
402
+ );
403
+ },
404
+ fromBuffer: (buff) => {
405
+ try {
406
+ return JSON.parse(decoder.decode(buff), function reviver(_key, val) {
407
+ if (val?.$t) {
408
+ return base64ToUint8Array(val.$t);
409
+ } else {
410
+ return val;
411
+ }
412
+ });
413
+ } catch {
414
+ return null;
415
+ }
416
+ }
417
+ };
418
+
419
+ // transport/impls/ws/connection.ts
420
+ var WebSocketConnection = class extends Connection {
421
+ ws;
422
+ constructor(transport, connectedTo, ws) {
423
+ super(transport, connectedTo);
424
+ this.ws = ws;
425
+ ws.binaryType = "arraybuffer";
426
+ this.ws.onmessage = (msg2) => transport.onMessage(msg2.data);
427
+ }
428
+ send(payload) {
429
+ if (this.ws.readyState === this.ws.OPEN) {
430
+ this.ws.send(payload);
431
+ return true;
432
+ } else {
433
+ return false;
434
+ }
435
+ }
436
+ async close() {
437
+ this.ws.close();
438
+ }
439
+ };
440
+
441
+ // transport/impls/ws/client.ts
442
+ var defaultOptions = {
443
+ retryIntervalMs: 250,
444
+ retryAttemptsMax: 5,
445
+ codec: NaiveJsonCodec
446
+ };
447
+ var WebSocketClientTransport = class extends Transport {
448
+ /**
449
+ * A function that returns a Promise that resolves to a WebSocket instance.
450
+ */
451
+ wsGetter;
452
+ options;
453
+ serverId;
454
+ reconnectPromises;
455
+ tryReconnecting = true;
456
+ /**
457
+ * Creates a new WebSocketTransport instance.
458
+ * @param wsGetter A function that returns a Promise that resolves to a WebSocket instance.
459
+ * @param clientId The ID of the client using the transport.
460
+ * @param providedOptions An optional object containing configuration options for the transport.
461
+ */
462
+ constructor(wsGetter, clientId, serverId, providedOptions) {
463
+ const options = { ...defaultOptions, ...providedOptions };
464
+ super(options.codec, clientId);
465
+ this.wsGetter = wsGetter;
466
+ this.serverId = serverId;
467
+ this.options = options;
468
+ this.reconnectPromises = /* @__PURE__ */ new Map();
469
+ this.setupConnectionStatusListeners();
470
+ }
471
+ setupConnectionStatusListeners() {
472
+ this.createNewConnection(this.serverId);
473
+ }
474
+ async createNewConnection(to, attempt = 0) {
475
+ if (this.state === "destroyed") {
476
+ throw new Error("cant reopen a destroyed connection");
477
+ }
478
+ let reconnectPromise = this.reconnectPromises.get(to);
479
+ if (!reconnectPromise) {
480
+ if (!this.tryReconnecting) {
481
+ log?.info(
482
+ `${this.clientId} -- tryReconnecting is false, not attempting reconnect`
483
+ );
484
+ return;
485
+ }
486
+ reconnectPromise = new Promise(async (resolve) => {
487
+ log?.info(`${this.clientId} -- establishing a new websocket to ${to}`);
488
+ const ws = await this.wsGetter(to);
489
+ if (ws.readyState === ws.OPEN) {
490
+ return resolve({ ws });
491
+ }
492
+ if (ws.readyState === ws.CLOSING || ws.readyState === ws.CLOSED) {
493
+ return resolve({ err: "ws is closing or closed" });
494
+ }
495
+ ws.addEventListener("open", function onOpen() {
496
+ ws.removeEventListener("open", onOpen);
497
+ resolve({ ws });
498
+ });
499
+ ws.addEventListener("close", function onClose(evt) {
500
+ ws.removeEventListener("close", onClose);
501
+ resolve({ err: evt.reason });
502
+ });
503
+ });
504
+ this.reconnectPromises.set(to, reconnectPromise);
505
+ }
506
+ const res = await reconnectPromise;
507
+ if ("ws" in res && res.ws.readyState === res.ws.OPEN) {
508
+ if (res.ws === this.connections.get(to)?.ws) {
509
+ return;
510
+ }
511
+ log?.info(`${this.clientId} -- websocket ok`);
512
+ const conn = new WebSocketConnection(this, to, res.ws);
513
+ this.onConnect(conn);
514
+ res.ws.onclose = () => {
515
+ this.reconnectPromises.delete(to);
516
+ this.onDisconnect(conn);
517
+ };
518
+ this.state = "open";
519
+ return;
520
+ }
521
+ this.reconnectPromises.delete(to);
522
+ if (attempt >= this.options.retryAttemptsMax) {
523
+ throw new Error(
524
+ `${this.clientId} -- websocket to ${to} failed after ${attempt} attempts, giving up`
525
+ );
526
+ } else {
527
+ log?.warn(
528
+ `${this.clientId} -- websocket to ${to} failed, trying again in ${this.options.retryIntervalMs * attempt}ms`
529
+ );
530
+ setTimeout(
531
+ () => this.createNewConnection(to, attempt + 1),
532
+ this.options.retryIntervalMs * attempt
533
+ );
534
+ }
535
+ }
536
+ };
537
+
538
+ // util/testHelpers.ts
539
+ var import_it_pushable = require("it-pushable");
540
+
541
+ // transport/impls/ws/server.ts
542
+ var defaultOptions2 = {
543
+ codec: NaiveJsonCodec
544
+ };
545
+ var WebSocketServerTransport = class extends Transport {
546
+ wss;
547
+ clientId;
548
+ constructor(wss, clientId, providedOptions) {
549
+ const options = { ...defaultOptions2, ...providedOptions };
550
+ super(options.codec, clientId);
551
+ this.wss = wss;
552
+ this.clientId = clientId;
553
+ this.setupConnectionStatusListeners();
554
+ }
555
+ connectionHandler = (ws) => {
556
+ let conn = void 0;
557
+ ws.onmessage = (msg2) => {
558
+ const parsedMsg = this.parseMsg(msg2.data);
559
+ if (parsedMsg && !conn) {
560
+ conn = new WebSocketConnection(this, parsedMsg.from, ws);
561
+ this.onConnect(conn);
562
+ this.handleMsg(parsedMsg);
563
+ }
564
+ };
565
+ ws.onclose = () => {
566
+ if (conn) {
567
+ this.onDisconnect(conn);
568
+ }
569
+ };
570
+ ws.onerror = (msg2) => {
571
+ log?.warn(
572
+ `${this.clientId} -- ws error from client ${conn?.connectedTo ?? "unknown"}: ${msg2}`
573
+ );
574
+ };
575
+ };
576
+ setupConnectionStatusListeners() {
577
+ this.wss.on("connection", this.connectionHandler);
578
+ }
579
+ async createNewConnection(to) {
580
+ const err = `${this.clientId} -- failed to send msg to ${to}, client probably dropped`;
581
+ log?.warn(err);
582
+ return;
583
+ }
584
+ async close() {
585
+ super.close();
586
+ this.wss.off("connection", this.connectionHandler);
587
+ }
588
+ };
589
+
590
+ // router/result.ts
591
+ var import_typebox2 = require("@sinclair/typebox");
592
+ var UNCAUGHT_ERROR = "UNCAUGHT_ERROR";
593
+ var UNEXPECTED_DISCONNECT = "UNEXPECTED_DISCONNECT";
594
+ var RiverUncaughtSchema = import_typebox2.Type.Object({
595
+ code: import_typebox2.Type.Union([
596
+ import_typebox2.Type.Literal(UNCAUGHT_ERROR),
597
+ import_typebox2.Type.Literal(UNEXPECTED_DISCONNECT)
598
+ ]),
599
+ message: import_typebox2.Type.String()
600
+ });
601
+
602
+ // util/testHelpers.ts
603
+ async function createWebSocketServer(server) {
604
+ return new import_ws.WebSocketServer({ server });
605
+ }
606
+ async function onServerReady(server) {
607
+ return new Promise((resolve, reject) => {
608
+ server.listen(() => {
609
+ const addr = server.address();
610
+ if (typeof addr === "object" && addr) {
611
+ resolve(addr.port);
612
+ } else {
613
+ reject(new Error("couldn't find a port to allocate"));
614
+ }
615
+ });
616
+ });
617
+ }
618
+ async function createLocalWebSocketClient(port) {
619
+ const sock = new import_isomorphic_ws.default(`ws://localhost:${port}`);
620
+ sock.binaryType = "arraybuffer";
621
+ return sock;
622
+ }
623
+ function createWsTransports(port, wss, codec) {
624
+ const options = codec ? { codec } : void 0;
625
+ return [
626
+ new WebSocketClientTransport(
627
+ () => createLocalWebSocketClient(port),
628
+ "client",
629
+ "SERVER",
630
+ options
631
+ ),
632
+ new WebSocketServerTransport(wss, "SERVER", options)
633
+ ];
634
+ }
635
+ function payloadToTransportMessage(payload, streamId, from = "client", to = "SERVER") {
636
+ return msg(from, to, streamId ?? "stream", payload, "service", "procedure");
637
+ }
638
+ function createDummyTransportMessage() {
639
+ return payloadToTransportMessage({
640
+ msg: "cool",
641
+ test: Math.random()
642
+ });
643
+ }
644
+ async function iterNext(iter) {
645
+ return await iter.next().then((res) => res.value);
646
+ }
647
+ async function waitForMessage(t, filter, rejectMismatch) {
648
+ return new Promise((resolve, reject) => {
649
+ function cleanup() {
650
+ t.removeEventListener("message", onMessage);
651
+ }
652
+ function onMessage(msg2) {
653
+ if (!filter || filter?.(msg2)) {
654
+ cleanup();
655
+ resolve(msg2.payload);
656
+ } else if (rejectMismatch) {
657
+ reject(new Error("message didnt match the filter"));
658
+ }
659
+ }
660
+ t.addEventListener("message", onMessage);
661
+ });
662
+ }
663
+ function catchProcError(err) {
664
+ const errorMsg = err instanceof Error ? err.message : `[coerced to error] ${err}`;
665
+ return {
666
+ ok: false,
667
+ payload: {
668
+ code: UNCAUGHT_ERROR,
669
+ message: errorMsg
670
+ }
671
+ };
672
+ }
673
+ function asClientRpc(state, proc, extendedContext) {
674
+ return async (msg2) => {
675
+ return await proc.handler({ ...extendedContext, state }, msg2).catch(catchProcError);
676
+ };
677
+ }
678
+ function asClientStream(state, proc, init, extendedContext) {
679
+ const input = (0, import_it_pushable.pushable)({ objectMode: true });
680
+ const output = (0, import_it_pushable.pushable)({
681
+ objectMode: true
682
+ });
683
+ (async () => {
684
+ if (init) {
685
+ const _proc = proc;
686
+ await _proc.handler({ ...extendedContext, state }, init, input, output).catch((err) => output.push(catchProcError(err)));
687
+ } else {
688
+ const _proc = proc;
689
+ await _proc.handler({ ...extendedContext, state }, input, output).catch((err) => output.push(catchProcError(err)));
690
+ }
691
+ })();
692
+ return [input, output];
693
+ }
694
+ function asClientSubscription(state, proc, extendedContext) {
695
+ const output = (0, import_it_pushable.pushable)({
696
+ objectMode: true
697
+ });
698
+ return async (msg2) => {
699
+ (async () => {
700
+ return await proc.handler({ ...extendedContext, state }, msg2, output).catch((err) => output.push(catchProcError(err)));
701
+ })();
702
+ return output;
703
+ };
704
+ }
705
+ async function asClientUpload(state, proc, init, extendedContext) {
706
+ const input = (0, import_it_pushable.pushable)({ objectMode: true });
707
+ if (init) {
708
+ const _proc = proc;
709
+ const result = _proc.handler({ ...extendedContext, state }, init, input).catch(catchProcError);
710
+ return [input, result];
711
+ } else {
712
+ const _proc = proc;
713
+ const result = _proc.handler({ ...extendedContext, state }, input).catch(catchProcError);
714
+ return [input, result];
715
+ }
716
+ }
717
+ // Annotate the CommonJS export names for ESM import in node:
718
+ 0 && (module.exports = {
719
+ asClientRpc,
720
+ asClientStream,
721
+ asClientSubscription,
722
+ asClientUpload,
723
+ createDummyTransportMessage,
724
+ createLocalWebSocketClient,
725
+ createWebSocketServer,
726
+ createWsTransports,
727
+ iterNext,
728
+ onServerReady,
729
+ payloadToTransportMessage,
730
+ waitForMessage
731
+ });