@replit/river 0.9.3 → 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 (129) hide show
  1. package/dist/{router/builder.d.ts → builder-3c4485f0.d.ts} +82 -28
  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 -10
  25. package/dist/router/index.js +24 -4
  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 +9 -12
  29. package/dist/transport/impls/ws/client.js +10 -101
  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 +14 -10
  33. package/dist/transport/impls/ws/server.js +11 -47
  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 -13
  37. package/dist/transport/index.js +20 -23
  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 +34 -108
  42. package/dist/util/testHelpers.js +137 -320
  43. package/package.json +52 -23
  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__/e2e.test.d.ts +0 -2
  48. package/dist/__tests__/e2e.test.d.ts.map +0 -1
  49. package/dist/__tests__/e2e.test.js +0 -316
  50. package/dist/__tests__/fixtures/cleanup.d.ts +0 -12
  51. package/dist/__tests__/fixtures/cleanup.d.ts.map +0 -1
  52. package/dist/__tests__/fixtures/cleanup.js +0 -39
  53. package/dist/__tests__/fixtures/largePayload.json +0 -33
  54. package/dist/__tests__/fixtures/observable.d.ts +0 -26
  55. package/dist/__tests__/fixtures/observable.d.ts.map +0 -1
  56. package/dist/__tests__/fixtures/observable.js +0 -38
  57. package/dist/__tests__/fixtures/observable.test.d.ts +0 -2
  58. package/dist/__tests__/fixtures/observable.test.d.ts.map +0 -1
  59. package/dist/__tests__/fixtures/observable.test.js +0 -39
  60. package/dist/__tests__/fixtures/services.d.ts +0 -288
  61. package/dist/__tests__/fixtures/services.d.ts.map +0 -1
  62. package/dist/__tests__/fixtures/services.js +0 -224
  63. package/dist/__tests__/handler.test.d.ts +0 -2
  64. package/dist/__tests__/handler.test.d.ts.map +0 -1
  65. package/dist/__tests__/handler.test.js +0 -121
  66. package/dist/__tests__/invariants.test.d.ts +0 -2
  67. package/dist/__tests__/invariants.test.d.ts.map +0 -1
  68. package/dist/__tests__/invariants.test.js +0 -138
  69. package/dist/__tests__/serialize.test.d.ts +0 -2
  70. package/dist/__tests__/serialize.test.d.ts.map +0 -1
  71. package/dist/__tests__/serialize.test.js +0 -208
  72. package/dist/__tests__/typescript-stress.test.d.ts +0 -1583
  73. package/dist/__tests__/typescript-stress.test.d.ts.map +0 -1
  74. package/dist/__tests__/typescript-stress.test.js +0 -123
  75. package/dist/codec/binary.d.ts +0 -7
  76. package/dist/codec/binary.d.ts.map +0 -1
  77. package/dist/codec/binary.js +0 -20
  78. package/dist/codec/codec.test.d.ts +0 -5
  79. package/dist/codec/codec.test.d.ts.map +0 -1
  80. package/dist/codec/codec.test.js +0 -41
  81. package/dist/codec/index.d.ts.map +0 -1
  82. package/dist/codec/json.d.ts +0 -7
  83. package/dist/codec/json.d.ts.map +0 -1
  84. package/dist/codec/json.js +0 -51
  85. package/dist/codec/types.d.ts.map +0 -1
  86. package/dist/codec/types.js +0 -1
  87. package/dist/logging/index.d.ts.map +0 -1
  88. package/dist/router/builder.d.ts.map +0 -1
  89. package/dist/router/builder.js +0 -91
  90. package/dist/router/client.d.ts +0 -68
  91. package/dist/router/client.d.ts.map +0 -1
  92. package/dist/router/client.js +0 -159
  93. package/dist/router/context.d.ts +0 -30
  94. package/dist/router/context.d.ts.map +0 -1
  95. package/dist/router/context.js +0 -1
  96. package/dist/router/index.d.ts.map +0 -1
  97. package/dist/router/result.d.ts +0 -25
  98. package/dist/router/result.d.ts.map +0 -1
  99. package/dist/router/result.js +0 -18
  100. package/dist/router/server.d.ts +0 -39
  101. package/dist/router/server.d.ts.map +0 -1
  102. package/dist/router/server.js +0 -217
  103. package/dist/transport/events.d.ts +0 -19
  104. package/dist/transport/events.d.ts.map +0 -1
  105. package/dist/transport/events.js +0 -26
  106. package/dist/transport/impls/stdio/stdio.d.ts +0 -33
  107. package/dist/transport/impls/stdio/stdio.d.ts.map +0 -1
  108. package/dist/transport/impls/stdio/stdio.js +0 -75
  109. package/dist/transport/impls/stdio/stdio.test.d.ts +0 -2
  110. package/dist/transport/impls/stdio/stdio.test.d.ts.map +0 -1
  111. package/dist/transport/impls/stdio/stdio.test.js +0 -25
  112. package/dist/transport/impls/ws/client.d.ts.map +0 -1
  113. package/dist/transport/impls/ws/connection.d.ts +0 -11
  114. package/dist/transport/impls/ws/connection.d.ts.map +0 -1
  115. package/dist/transport/impls/ws/connection.js +0 -23
  116. package/dist/transport/impls/ws/server.d.ts.map +0 -1
  117. package/dist/transport/impls/ws/ws.test.d.ts +0 -2
  118. package/dist/transport/impls/ws/ws.test.d.ts.map +0 -1
  119. package/dist/transport/impls/ws/ws.test.js +0 -117
  120. package/dist/transport/index.d.ts.map +0 -1
  121. package/dist/transport/message.d.ts +0 -143
  122. package/dist/transport/message.d.ts.map +0 -1
  123. package/dist/transport/message.js +0 -113
  124. package/dist/transport/message.test.d.ts +0 -2
  125. package/dist/transport/message.test.d.ts.map +0 -1
  126. package/dist/transport/message.test.js +0 -52
  127. package/dist/transport/transport.d.ts.map +0 -1
  128. package/dist/transport/transport.js +0 -281
  129. package/dist/util/testHelpers.d.ts.map +0 -1
@@ -0,0 +1,457 @@
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/server.ts
21
+ var server_exports = {};
22
+ __export(server_exports, {
23
+ WebSocketServerTransport: () => WebSocketServerTransport
24
+ });
25
+ module.exports = __toCommonJS(server_exports);
26
+
27
+ // codec/json.ts
28
+ var encoder = new TextEncoder();
29
+ var decoder = new TextDecoder();
30
+ function uint8ArrayToBase64(uint8Array) {
31
+ let binary = "";
32
+ uint8Array.forEach((byte) => {
33
+ binary += String.fromCharCode(byte);
34
+ });
35
+ return btoa(binary);
36
+ }
37
+ function base64ToUint8Array(base64) {
38
+ const binaryString = atob(base64);
39
+ const uint8Array = new Uint8Array(binaryString.length);
40
+ for (let i = 0; i < binaryString.length; i++) {
41
+ uint8Array[i] = binaryString.charCodeAt(i);
42
+ }
43
+ return uint8Array;
44
+ }
45
+ var NaiveJsonCodec = {
46
+ toBuffer: (obj) => {
47
+ return encoder.encode(
48
+ JSON.stringify(obj, function replacer(key) {
49
+ let val = this[key];
50
+ if (val instanceof Uint8Array) {
51
+ return { $t: uint8ArrayToBase64(val) };
52
+ } else {
53
+ return val;
54
+ }
55
+ })
56
+ );
57
+ },
58
+ fromBuffer: (buff) => {
59
+ try {
60
+ return JSON.parse(decoder.decode(buff), function reviver(_key, val) {
61
+ if (val?.$t) {
62
+ return base64ToUint8Array(val.$t);
63
+ } else {
64
+ return val;
65
+ }
66
+ });
67
+ } catch {
68
+ return null;
69
+ }
70
+ }
71
+ };
72
+
73
+ // logging/index.ts
74
+ var log;
75
+
76
+ // transport/transport.ts
77
+ var import_value = require("@sinclair/typebox/value");
78
+
79
+ // transport/message.ts
80
+ var import_typebox = require("@sinclair/typebox");
81
+ var import_nanoid = require("nanoid");
82
+ var TransportMessageSchema = (t) => import_typebox.Type.Object({
83
+ id: import_typebox.Type.String(),
84
+ from: import_typebox.Type.String(),
85
+ to: import_typebox.Type.String(),
86
+ serviceName: import_typebox.Type.Optional(import_typebox.Type.Union([import_typebox.Type.String(), import_typebox.Type.Null()])),
87
+ procedureName: import_typebox.Type.Optional(import_typebox.Type.Union([import_typebox.Type.String(), import_typebox.Type.Null()])),
88
+ streamId: import_typebox.Type.String(),
89
+ controlFlags: import_typebox.Type.Integer(),
90
+ payload: t
91
+ });
92
+ var TransportAckSchema = TransportMessageSchema(
93
+ import_typebox.Type.Object({
94
+ ack: import_typebox.Type.String()
95
+ })
96
+ );
97
+ var ControlMessagePayloadSchema = import_typebox.Type.Object({
98
+ type: import_typebox.Type.Literal("CLOSE")
99
+ });
100
+ var OpaqueTransportMessageSchema = TransportMessageSchema(
101
+ import_typebox.Type.Unknown()
102
+ );
103
+ function reply(msg, response) {
104
+ return {
105
+ id: (0, import_nanoid.nanoid)(),
106
+ streamId: msg.streamId,
107
+ controlFlags: 0,
108
+ to: msg.from,
109
+ from: msg.to,
110
+ payload: response
111
+ };
112
+ }
113
+ function isAck(controlFlag) {
114
+ return (controlFlag & 1 /* AckBit */) === 1 /* AckBit */;
115
+ }
116
+
117
+ // transport/events.ts
118
+ var EventDispatcher = class {
119
+ eventListeners = {};
120
+ numberOfListeners(eventType) {
121
+ return this.eventListeners[eventType]?.size ?? 0;
122
+ }
123
+ addEventListener(eventType, handler) {
124
+ if (!this.eventListeners[eventType]) {
125
+ this.eventListeners[eventType] = /* @__PURE__ */ new Set();
126
+ }
127
+ this.eventListeners[eventType]?.add(handler);
128
+ }
129
+ removeEventListener(eventType, handler) {
130
+ const handlers = this.eventListeners[eventType];
131
+ if (handlers) {
132
+ this.eventListeners[eventType]?.delete(handler);
133
+ }
134
+ }
135
+ dispatchEvent(eventType, event) {
136
+ const handlers = this.eventListeners[eventType];
137
+ if (handlers) {
138
+ for (const handler of handlers) {
139
+ handler(event);
140
+ }
141
+ }
142
+ }
143
+ };
144
+
145
+ // transport/transport.ts
146
+ var Connection = class {
147
+ connectedTo;
148
+ transport;
149
+ constructor(transport, connectedTo) {
150
+ this.connectedTo = connectedTo;
151
+ this.transport = transport;
152
+ }
153
+ };
154
+ var Transport = class {
155
+ /**
156
+ * A flag indicating whether the transport has been destroyed.
157
+ * A destroyed transport will not attempt to reconnect and cannot be used again.
158
+ */
159
+ state;
160
+ /**
161
+ * The {@link Codec} used to encode and decode messages.
162
+ */
163
+ codec;
164
+ /**
165
+ * The client ID of this transport.
166
+ */
167
+ clientId;
168
+ /**
169
+ * An array of message IDs that are waiting to be sent over the WebSocket connection.
170
+ * This builds up if the WebSocket is down for a period of time.
171
+ */
172
+ sendQueue;
173
+ /**
174
+ * The buffer of messages that have been sent but not yet acknowledged.
175
+ */
176
+ sendBuffer;
177
+ /**
178
+ * The map of {@link Connection}s managed by this transport.
179
+ */
180
+ connections;
181
+ /**
182
+ * The event dispatcher for handling events of type EventTypes.
183
+ */
184
+ eventDispatcher;
185
+ /**
186
+ * Creates a new Transport instance.
187
+ * @param codec The codec used to encode and decode messages.
188
+ * @param clientId The client ID of this transport.
189
+ */
190
+ constructor(codec, clientId) {
191
+ this.eventDispatcher = new EventDispatcher();
192
+ this.sendBuffer = /* @__PURE__ */ new Map();
193
+ this.sendQueue = /* @__PURE__ */ new Map();
194
+ this.connections = /* @__PURE__ */ new Map();
195
+ this.codec = codec;
196
+ this.clientId = clientId;
197
+ this.state = "open";
198
+ }
199
+ /**
200
+ * The downstream implementation needs to call this when a new connection is established.
201
+ * @param conn The connection object.
202
+ */
203
+ onConnect(conn) {
204
+ log?.info(`${this.clientId} -- new connection to ${conn.connectedTo}`);
205
+ this.connections.set(conn.connectedTo, conn);
206
+ this.eventDispatcher.dispatchEvent("connectionStatus", {
207
+ status: "connect",
208
+ conn
209
+ });
210
+ const outstanding = this.sendQueue.get(conn.connectedTo);
211
+ if (!outstanding) {
212
+ return;
213
+ }
214
+ for (const id of outstanding) {
215
+ const msg = this.sendBuffer.get(id);
216
+ if (!msg) {
217
+ log?.warn(
218
+ `${this.clientId} -- tried to resend a message we received an ack for`
219
+ );
220
+ continue;
221
+ }
222
+ this.send(msg);
223
+ }
224
+ this.sendQueue.delete(conn.connectedTo);
225
+ }
226
+ /**
227
+ * The downstream implementation needs to call this when a connection is closed.
228
+ * @param conn The connection object.
229
+ */
230
+ onDisconnect(conn) {
231
+ log?.info(`${this.clientId} -- disconnect from ${conn.connectedTo}`);
232
+ conn.close();
233
+ this.connections.delete(conn.connectedTo);
234
+ this.eventDispatcher.dispatchEvent("connectionStatus", {
235
+ status: "disconnect",
236
+ conn
237
+ });
238
+ }
239
+ /**
240
+ * Handles a message received by this transport. Thin wrapper around {@link handleMsg} and {@link parseMsg}.
241
+ * @param msg The message to handle.
242
+ */
243
+ onMessage(msg) {
244
+ return this.handleMsg(this.parseMsg(msg));
245
+ }
246
+ /**
247
+ * Parses a message from a Uint8Array into a {@link OpaqueTransportMessage}.
248
+ * @param msg The message to parse.
249
+ * @returns The parsed message, or null if the message is malformed or invalid.
250
+ */
251
+ parseMsg(msg) {
252
+ const parsedMsg = this.codec.fromBuffer(msg);
253
+ if (parsedMsg === null) {
254
+ const decodedBuffer = new TextDecoder().decode(msg);
255
+ log?.warn(`${this.clientId} -- received malformed msg: ${decodedBuffer}`);
256
+ return null;
257
+ }
258
+ if (import_value.Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
259
+ return {
260
+ ...parsedMsg,
261
+ serviceName: parsedMsg.serviceName === null ? void 0 : parsedMsg.serviceName,
262
+ procedureName: parsedMsg.procedureName === null ? void 0 : parsedMsg.procedureName
263
+ };
264
+ } else {
265
+ log?.warn(
266
+ `${this.clientId} -- received invalid msg: ${JSON.stringify(
267
+ parsedMsg
268
+ )}`
269
+ );
270
+ return null;
271
+ }
272
+ }
273
+ /**
274
+ * Called when a message is received by this transport.
275
+ * You generally shouldn't need to override this in downstream transport implementations.
276
+ * @param msg The received message.
277
+ */
278
+ handleMsg(msg) {
279
+ if (!msg) {
280
+ return;
281
+ }
282
+ if (isAck(msg.controlFlags) && import_value.Value.Check(TransportAckSchema, msg)) {
283
+ log?.info(`${this.clientId} -- received ack: ${JSON.stringify(msg)}`);
284
+ if (this.sendBuffer.has(msg.payload.ack)) {
285
+ this.sendBuffer.delete(msg.payload.ack);
286
+ }
287
+ } else {
288
+ log?.info(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
289
+ if (msg.to !== this.clientId) {
290
+ return;
291
+ }
292
+ this.eventDispatcher.dispatchEvent("message", msg);
293
+ if (!isAck(msg.controlFlags)) {
294
+ const ackMsg = reply(msg, { ack: msg.id });
295
+ ackMsg.controlFlags = 1 /* AckBit */;
296
+ ackMsg.from = this.clientId;
297
+ this.send(ackMsg);
298
+ }
299
+ }
300
+ }
301
+ /**
302
+ * Adds a listener to this transport.
303
+ * @param the type of event to listen for
304
+ * @param handler The message handler to add.
305
+ */
306
+ addEventListener(type, handler) {
307
+ this.eventDispatcher.addEventListener(type, handler);
308
+ }
309
+ /**
310
+ * Removes a listener from this transport.
311
+ * @param the type of event to unlisten on
312
+ * @param handler The message handler to remove.
313
+ */
314
+ removeEventListener(type, handler) {
315
+ this.eventDispatcher.removeEventListener(type, handler);
316
+ }
317
+ /**
318
+ * Sends a message over this transport, delegating to the appropriate connection to actually
319
+ * send the message.
320
+ * @param msg The message to send.
321
+ * @returns The ID of the sent message.
322
+ */
323
+ send(msg) {
324
+ if (this.state === "destroyed") {
325
+ const err = "transport is destroyed, cant send";
326
+ log?.error(`${this.clientId} -- ` + err + `: ${JSON.stringify(msg)}`);
327
+ throw new Error(err);
328
+ } else if (this.state === "closed") {
329
+ log?.info(
330
+ `${this.clientId} -- transport closed when sending, discarding : ${JSON.stringify(
331
+ msg
332
+ )}`
333
+ );
334
+ return msg.id;
335
+ }
336
+ let conn = this.connections.get(msg.to);
337
+ if (!isAck(msg.controlFlags)) {
338
+ this.sendBuffer.set(msg.id, msg);
339
+ }
340
+ if (conn) {
341
+ log?.info(`${this.clientId} -- sending ${JSON.stringify(msg)}`);
342
+ const ok = conn.send(this.codec.toBuffer(msg));
343
+ if (ok) {
344
+ return msg.id;
345
+ }
346
+ }
347
+ log?.info(
348
+ `${this.clientId} -- connection to ${msg.to} not ready, attempting reconnect and queuing ${JSON.stringify(msg)}`
349
+ );
350
+ const outstanding = this.sendQueue.get(msg.to) || [];
351
+ outstanding.push(msg.id);
352
+ this.sendQueue.set(msg.to, outstanding);
353
+ this.createNewConnection(msg.to);
354
+ return msg.id;
355
+ }
356
+ /**
357
+ * Default close implementation for transports. You should override this in the downstream
358
+ * implementation if you need to do any additional cleanup and call super.close() at the end.
359
+ * Closes the transport. Any messages sent while the transport is closed will be silently discarded.
360
+ */
361
+ async close() {
362
+ for (const conn of this.connections.values()) {
363
+ conn.close();
364
+ }
365
+ this.connections.clear();
366
+ this.state = "closed";
367
+ log?.info(`${this.clientId} -- closed transport`);
368
+ }
369
+ /**
370
+ * Default destroy implementation for transports. You should override this in the downstream
371
+ * implementation if you need to do any additional cleanup and call super.destroy() at the end.
372
+ * Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
373
+ */
374
+ async destroy() {
375
+ for (const conn of this.connections.values()) {
376
+ conn.close();
377
+ }
378
+ this.connections.clear();
379
+ this.state = "destroyed";
380
+ log?.info(`${this.clientId} -- destroyed transport`);
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/server.ts
407
+ var defaultOptions = {
408
+ codec: NaiveJsonCodec
409
+ };
410
+ var WebSocketServerTransport = class extends Transport {
411
+ wss;
412
+ clientId;
413
+ constructor(wss, clientId, providedOptions) {
414
+ const options = { ...defaultOptions, ...providedOptions };
415
+ super(options.codec, clientId);
416
+ this.wss = wss;
417
+ this.clientId = clientId;
418
+ this.setupConnectionStatusListeners();
419
+ }
420
+ connectionHandler = (ws) => {
421
+ let conn = void 0;
422
+ ws.onmessage = (msg) => {
423
+ const parsedMsg = this.parseMsg(msg.data);
424
+ if (parsedMsg && !conn) {
425
+ conn = new WebSocketConnection(this, parsedMsg.from, ws);
426
+ this.onConnect(conn);
427
+ this.handleMsg(parsedMsg);
428
+ }
429
+ };
430
+ ws.onclose = () => {
431
+ if (conn) {
432
+ this.onDisconnect(conn);
433
+ }
434
+ };
435
+ ws.onerror = (msg) => {
436
+ log?.warn(
437
+ `${this.clientId} -- ws error from client ${conn?.connectedTo ?? "unknown"}: ${msg}`
438
+ );
439
+ };
440
+ };
441
+ setupConnectionStatusListeners() {
442
+ this.wss.on("connection", this.connectionHandler);
443
+ }
444
+ async createNewConnection(to) {
445
+ const err = `${this.clientId} -- failed to send msg to ${to}, client probably dropped`;
446
+ log?.warn(err);
447
+ return;
448
+ }
449
+ async close() {
450
+ super.close();
451
+ this.wss.off("connection", this.connectionHandler);
452
+ }
453
+ };
454
+ // Annotate the CommonJS export names for ESM import in node:
455
+ 0 && (module.exports = {
456
+ WebSocketServerTransport
457
+ });
@@ -0,0 +1,21 @@
1
+ import { C as Codec } from '../../../types-3e5768ec.js';
2
+ import { Transport, TransportClientId } from '../../index.cjs';
3
+ import { WebSocketServer } from 'ws';
4
+ import { WebSocket } from 'isomorphic-ws';
5
+ import { W as WebSocketConnection } from '../../../connection-f7688cc1.js';
6
+ import '@sinclair/typebox';
7
+
8
+ interface Options {
9
+ codec: Codec;
10
+ }
11
+ declare class WebSocketServerTransport extends Transport<WebSocketConnection> {
12
+ wss: WebSocketServer;
13
+ clientId: TransportClientId;
14
+ constructor(wss: WebSocketServer, clientId: TransportClientId, providedOptions?: Partial<Options>);
15
+ connectionHandler: (ws: WebSocket) => void;
16
+ setupConnectionStatusListeners(): void;
17
+ createNewConnection(to: string): Promise<void>;
18
+ close(): Promise<void>;
19
+ }
20
+
21
+ export { WebSocketServerTransport };
@@ -1,17 +1,21 @@
1
- import { Codec } from '../../../codec';
2
- import { TransportClientId } from '../../message';
3
- import { Transport } from '../../transport';
4
- import { Server } from 'ws';
5
- import { WebSocketConnection } from './connection';
1
+ import { C as Codec } from '../../../types-3e5768ec.js';
2
+ import { Transport, TransportClientId } from '../../index.js';
3
+ import { WebSocketServer } from 'ws';
4
+ import { WebSocket } from 'isomorphic-ws';
5
+ import { W as WebSocketConnection } from '../../../connection-8e19874c.js';
6
+ import '@sinclair/typebox';
7
+
6
8
  interface Options {
7
9
  codec: Codec;
8
10
  }
9
- export declare class WebSocketServerTransport extends Transport<WebSocketConnection> {
10
- wss: Server;
11
+ declare class WebSocketServerTransport extends Transport<WebSocketConnection> {
12
+ wss: WebSocketServer;
11
13
  clientId: TransportClientId;
12
- constructor(wss: Server, clientId: TransportClientId, providedOptions?: Partial<Options>);
14
+ constructor(wss: WebSocketServer, clientId: TransportClientId, providedOptions?: Partial<Options>);
15
+ connectionHandler: (ws: WebSocket) => void;
13
16
  setupConnectionStatusListeners(): void;
14
17
  createNewConnection(to: string): Promise<void>;
18
+ close(): Promise<void>;
15
19
  }
16
- export {};
17
- //# sourceMappingURL=server.d.ts.map
20
+
21
+ export { WebSocketServerTransport };
@@ -1,48 +1,12 @@
1
- import { NaiveJsonCodec } from '../../../codec';
2
- import { log } from '../../../logging';
3
- import { Transport } from '../../transport';
4
- import { WebSocketConnection } from './connection';
5
- const defaultOptions = {
6
- codec: NaiveJsonCodec,
1
+ import {
2
+ WebSocketServerTransport
3
+ } from "../../../chunk-RGMHF6PF.js";
4
+ import "../../../chunk-WVT5QXMZ.js";
5
+ import "../../../chunk-PC65ZFWJ.js";
6
+ import "../../../chunk-R6H2BIMC.js";
7
+ import "../../../chunk-AJQU4AZG.js";
8
+ import "../../../chunk-ZE4MX7DF.js";
9
+ import "../../../chunk-SLUSVGQH.js";
10
+ export {
11
+ WebSocketServerTransport
7
12
  };
8
- export class WebSocketServerTransport extends Transport {
9
- wss;
10
- clientId;
11
- constructor(wss, clientId, providedOptions) {
12
- const options = { ...defaultOptions, ...providedOptions };
13
- super(options.codec, clientId);
14
- this.wss = wss;
15
- this.clientId = clientId;
16
- this.setupConnectionStatusListeners();
17
- }
18
- setupConnectionStatusListeners() {
19
- this.wss.on('connection', (ws) => {
20
- let conn = undefined;
21
- ws.onmessage = (msg) => {
22
- // when we establish WebSocketConnection, ws.onmessage
23
- // gets overriden so this only runs on the first valid message
24
- // the websocket receives
25
- const parsedMsg = this.parseMsg(msg.data);
26
- if (parsedMsg && !conn) {
27
- conn = new WebSocketConnection(this, parsedMsg.from, ws);
28
- this.onConnect(conn);
29
- this.handleMsg(parsedMsg);
30
- }
31
- };
32
- // close is always emitted, even on error, ok to do cleanup here
33
- ws.onclose = () => {
34
- if (conn) {
35
- this.onDisconnect(conn);
36
- }
37
- };
38
- ws.onerror = (msg) => {
39
- log?.warn(`${this.clientId} -- ws error from client ${conn?.connectedTo ?? 'unknown'}: ${msg}`);
40
- };
41
- });
42
- }
43
- async createNewConnection(to) {
44
- const err = `${this.clientId} -- failed to send msg to ${to}, client probably dropped`;
45
- log?.warn(err);
46
- return;
47
- }
48
- }