@replit/river 0.10.3 → 0.10.5

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 (36) hide show
  1. package/dist/{chunk-UU2Z7LDR.js → chunk-IEU7OE5W.js} +2 -5
  2. package/dist/{chunk-RGMHF6PF.js → chunk-PNZXYQME.js} +6 -8
  3. package/dist/{chunk-PC65ZFWJ.js → chunk-SZTOUKL7.js} +1 -1
  4. package/dist/chunk-TZSX5KM2.js +80 -0
  5. package/dist/{chunk-AJQU4AZG.js → chunk-V2YJRBRX.js} +1 -0
  6. package/dist/connection-2529fc14.d.ts +10 -0
  7. package/dist/connection-316d6e3a.d.ts +10 -0
  8. package/dist/transport/impls/stdio/stdio.cjs +511 -0
  9. package/dist/transport/impls/stdio/stdio.d.cts +26 -0
  10. package/dist/transport/impls/stdio/stdio.d.ts +26 -0
  11. package/dist/transport/impls/stdio/stdio.js +70 -0
  12. package/dist/transport/impls/unixsocket/client.cjs +509 -0
  13. package/dist/transport/impls/unixsocket/client.d.cts +16 -0
  14. package/dist/transport/impls/unixsocket/client.d.ts +16 -0
  15. package/dist/transport/impls/unixsocket/client.js +67 -0
  16. package/dist/transport/impls/unixsocket/server.cjs +513 -0
  17. package/dist/transport/impls/unixsocket/server.d.cts +18 -0
  18. package/dist/transport/impls/unixsocket/server.d.ts +18 -0
  19. package/dist/transport/impls/unixsocket/server.js +73 -0
  20. package/dist/transport/impls/ws/client.cjs +1 -3
  21. package/dist/transport/impls/ws/client.d.cts +0 -1
  22. package/dist/transport/impls/ws/client.d.ts +0 -1
  23. package/dist/transport/impls/ws/client.js +3 -3
  24. package/dist/transport/impls/ws/server.cjs +5 -6
  25. package/dist/transport/impls/ws/server.d.cts +0 -2
  26. package/dist/transport/impls/ws/server.d.ts +0 -2
  27. package/dist/transport/impls/ws/server.js +3 -3
  28. package/dist/transport/index.cjs +1 -0
  29. package/dist/transport/index.d.cts +6 -7
  30. package/dist/transport/index.d.ts +6 -7
  31. package/dist/transport/index.js +1 -1
  32. package/dist/util/testHelpers.cjs +21 -12
  33. package/dist/util/testHelpers.d.cts +6 -3
  34. package/dist/util/testHelpers.d.ts +6 -3
  35. package/dist/util/testHelpers.js +18 -7
  36. package/package.json +12 -4
@@ -0,0 +1,67 @@
1
+ import {
2
+ StreamConnection,
3
+ createDelimitedStream
4
+ } from "../../../chunk-TZSX5KM2.js";
5
+ import "../../../chunk-ORAG7IAU.js";
6
+ import "../../../chunk-WVT5QXMZ.js";
7
+ import {
8
+ NaiveJsonCodec
9
+ } from "../../../chunk-R6H2BIMC.js";
10
+ import {
11
+ Transport
12
+ } from "../../../chunk-V2YJRBRX.js";
13
+ import "../../../chunk-ZE4MX7DF.js";
14
+ import {
15
+ log
16
+ } from "../../../chunk-SLUSVGQH.js";
17
+
18
+ // transport/impls/unixsocket/client.ts
19
+ import { createConnection } from "node:net";
20
+ var defaultOptions = {
21
+ codec: NaiveJsonCodec
22
+ };
23
+ var UnixDomainSocketClientTransport = class extends Transport {
24
+ path;
25
+ serverId;
26
+ constructor(socketPath, clientId, serverId, providedOptions) {
27
+ const options = { ...defaultOptions, ...providedOptions };
28
+ super(options.codec, clientId);
29
+ this.path = socketPath;
30
+ this.serverId = serverId;
31
+ }
32
+ // lazily create connection on first send to avoid cases
33
+ // where we create client and server transports at the same time
34
+ // but the server hasn't created the socket file yet
35
+ async createNewConnection(to) {
36
+ const oldConnection = this.connections.get(to);
37
+ if (oldConnection) {
38
+ oldConnection.close();
39
+ }
40
+ const sock = createConnection(this.path);
41
+ const conn = new StreamConnection(this, to, sock);
42
+ const delimStream = createDelimitedStream();
43
+ sock.pipe(delimStream).on("data", (data) => {
44
+ const parsedMsg = this.parseMsg(data);
45
+ if (parsedMsg) {
46
+ this.handleMsg(parsedMsg);
47
+ }
48
+ });
49
+ const cleanup = () => {
50
+ delimStream.destroy();
51
+ if (conn) {
52
+ this.onDisconnect(conn);
53
+ }
54
+ };
55
+ sock.on("close", cleanup);
56
+ sock.on("error", (err) => {
57
+ log?.warn(
58
+ `${this.clientId} -- socket error in connection to ${conn?.connectedTo ?? "unknown"}: ${err}`
59
+ );
60
+ cleanup();
61
+ });
62
+ this.onConnect(conn);
63
+ }
64
+ };
65
+ export {
66
+ UnixDomainSocketClientTransport
67
+ };
@@ -0,0 +1,513 @@
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/unixsocket/server.ts
21
+ var server_exports = {};
22
+ __export(server_exports, {
23
+ UnixDomainSocketServerTransport: () => UnixDomainSocketServerTransport
24
+ });
25
+ module.exports = __toCommonJS(server_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
+ * This should also set up {@link onConnect}, and {@link onDisconnect} listeners.
142
+ * @param codec The codec used to encode and decode messages.
143
+ * @param clientId The client ID of this transport.
144
+ */
145
+ constructor(codec, clientId) {
146
+ this.eventDispatcher = new EventDispatcher();
147
+ this.sendBuffer = /* @__PURE__ */ new Map();
148
+ this.sendQueue = /* @__PURE__ */ new Map();
149
+ this.connections = /* @__PURE__ */ new Map();
150
+ this.codec = codec;
151
+ this.clientId = clientId;
152
+ this.state = "open";
153
+ }
154
+ /**
155
+ * The downstream implementation needs to call this when a new connection is established.
156
+ * @param conn The connection object.
157
+ */
158
+ onConnect(conn) {
159
+ log?.info(`${this.clientId} -- new connection to ${conn.connectedTo}`);
160
+ this.connections.set(conn.connectedTo, conn);
161
+ this.eventDispatcher.dispatchEvent("connectionStatus", {
162
+ status: "connect",
163
+ conn
164
+ });
165
+ const outstanding = this.sendQueue.get(conn.connectedTo);
166
+ if (!outstanding) {
167
+ return;
168
+ }
169
+ for (const id of outstanding) {
170
+ const msg = this.sendBuffer.get(id);
171
+ if (!msg) {
172
+ log?.warn(
173
+ `${this.clientId} -- tried to resend a message we received an ack for`
174
+ );
175
+ continue;
176
+ }
177
+ this.send(msg);
178
+ }
179
+ this.sendQueue.delete(conn.connectedTo);
180
+ }
181
+ /**
182
+ * The downstream implementation needs to call this when a connection is closed.
183
+ * @param conn The connection object.
184
+ */
185
+ onDisconnect(conn) {
186
+ log?.info(`${this.clientId} -- disconnect from ${conn.connectedTo}`);
187
+ conn.close();
188
+ this.connections.delete(conn.connectedTo);
189
+ this.eventDispatcher.dispatchEvent("connectionStatus", {
190
+ status: "disconnect",
191
+ conn
192
+ });
193
+ }
194
+ /**
195
+ * Handles a message received by this transport. Thin wrapper around {@link handleMsg} and {@link parseMsg}.
196
+ * @param msg The message to handle.
197
+ */
198
+ onMessage(msg) {
199
+ return this.handleMsg(this.parseMsg(msg));
200
+ }
201
+ /**
202
+ * Parses a message from a Uint8Array into a {@link OpaqueTransportMessage}.
203
+ * @param msg The message to parse.
204
+ * @returns The parsed message, or null if the message is malformed or invalid.
205
+ */
206
+ parseMsg(msg) {
207
+ const parsedMsg = this.codec.fromBuffer(msg);
208
+ if (parsedMsg === null) {
209
+ const decodedBuffer = new TextDecoder().decode(msg);
210
+ log?.warn(`${this.clientId} -- received malformed msg: ${decodedBuffer}`);
211
+ return null;
212
+ }
213
+ if (import_value.Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
214
+ return {
215
+ ...parsedMsg,
216
+ serviceName: parsedMsg.serviceName === null ? void 0 : parsedMsg.serviceName,
217
+ procedureName: parsedMsg.procedureName === null ? void 0 : parsedMsg.procedureName
218
+ };
219
+ } else {
220
+ log?.warn(
221
+ `${this.clientId} -- received invalid msg: ${JSON.stringify(
222
+ parsedMsg
223
+ )}`
224
+ );
225
+ return null;
226
+ }
227
+ }
228
+ /**
229
+ * Called when a message is received by this transport.
230
+ * You generally shouldn't need to override this in downstream transport implementations.
231
+ * @param msg The received message.
232
+ */
233
+ handleMsg(msg) {
234
+ if (!msg) {
235
+ return;
236
+ }
237
+ if (isAck(msg.controlFlags) && import_value.Value.Check(TransportAckSchema, msg)) {
238
+ log?.info(`${this.clientId} -- received ack: ${JSON.stringify(msg)}`);
239
+ if (this.sendBuffer.has(msg.payload.ack)) {
240
+ this.sendBuffer.delete(msg.payload.ack);
241
+ }
242
+ } else {
243
+ log?.info(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
244
+ if (msg.to !== this.clientId) {
245
+ return;
246
+ }
247
+ this.eventDispatcher.dispatchEvent("message", msg);
248
+ if (!isAck(msg.controlFlags)) {
249
+ const ackMsg = reply(msg, { ack: msg.id });
250
+ ackMsg.controlFlags = 1 /* AckBit */;
251
+ ackMsg.from = this.clientId;
252
+ this.send(ackMsg);
253
+ }
254
+ }
255
+ }
256
+ /**
257
+ * Adds a listener to this transport.
258
+ * @param the type of event to listen for
259
+ * @param handler The message handler to add.
260
+ */
261
+ addEventListener(type, handler) {
262
+ this.eventDispatcher.addEventListener(type, handler);
263
+ }
264
+ /**
265
+ * Removes a listener from this transport.
266
+ * @param the type of event to unlisten on
267
+ * @param handler The message handler to remove.
268
+ */
269
+ removeEventListener(type, handler) {
270
+ this.eventDispatcher.removeEventListener(type, handler);
271
+ }
272
+ /**
273
+ * Sends a message over this transport, delegating to the appropriate connection to actually
274
+ * send the message.
275
+ * @param msg The message to send.
276
+ * @returns The ID of the sent message.
277
+ */
278
+ send(msg) {
279
+ if (this.state === "destroyed") {
280
+ const err = "transport is destroyed, cant send";
281
+ log?.error(`${this.clientId} -- ` + err + `: ${JSON.stringify(msg)}`);
282
+ throw new Error(err);
283
+ } else if (this.state === "closed") {
284
+ log?.info(
285
+ `${this.clientId} -- transport closed when sending, discarding : ${JSON.stringify(
286
+ msg
287
+ )}`
288
+ );
289
+ return msg.id;
290
+ }
291
+ let conn = this.connections.get(msg.to);
292
+ if (!isAck(msg.controlFlags)) {
293
+ this.sendBuffer.set(msg.id, msg);
294
+ }
295
+ if (conn) {
296
+ log?.info(`${this.clientId} -- sending ${JSON.stringify(msg)}`);
297
+ const ok = conn.send(this.codec.toBuffer(msg));
298
+ if (ok) {
299
+ return msg.id;
300
+ }
301
+ }
302
+ log?.info(
303
+ `${this.clientId} -- connection to ${msg.to} not ready, attempting reconnect and queuing ${JSON.stringify(msg)}`
304
+ );
305
+ const outstanding = this.sendQueue.get(msg.to) || [];
306
+ outstanding.push(msg.id);
307
+ this.sendQueue.set(msg.to, outstanding);
308
+ this.createNewConnection(msg.to);
309
+ return msg.id;
310
+ }
311
+ /**
312
+ * Default close implementation for transports. You should override this in the downstream
313
+ * implementation if you need to do any additional cleanup and call super.close() at the end.
314
+ * Closes the transport. Any messages sent while the transport is closed will be silently discarded.
315
+ */
316
+ async close() {
317
+ for (const conn of this.connections.values()) {
318
+ conn.close();
319
+ }
320
+ this.connections.clear();
321
+ this.state = "closed";
322
+ log?.info(`${this.clientId} -- closed transport`);
323
+ }
324
+ /**
325
+ * Default destroy implementation for transports. You should override this in the downstream
326
+ * implementation if you need to do any additional cleanup and call super.destroy() at the end.
327
+ * Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
328
+ */
329
+ async destroy() {
330
+ for (const conn of this.connections.values()) {
331
+ conn.close();
332
+ }
333
+ this.connections.clear();
334
+ this.state = "destroyed";
335
+ log?.info(`${this.clientId} -- destroyed transport`);
336
+ }
337
+ };
338
+
339
+ // codec/json.ts
340
+ var encoder = new TextEncoder();
341
+ var decoder = new TextDecoder();
342
+ function uint8ArrayToBase64(uint8Array) {
343
+ let binary = "";
344
+ uint8Array.forEach((byte) => {
345
+ binary += String.fromCharCode(byte);
346
+ });
347
+ return btoa(binary);
348
+ }
349
+ function base64ToUint8Array(base64) {
350
+ const binaryString = atob(base64);
351
+ const uint8Array = new Uint8Array(binaryString.length);
352
+ for (let i = 0; i < binaryString.length; i++) {
353
+ uint8Array[i] = binaryString.charCodeAt(i);
354
+ }
355
+ return uint8Array;
356
+ }
357
+ var NaiveJsonCodec = {
358
+ toBuffer: (obj) => {
359
+ return encoder.encode(
360
+ JSON.stringify(obj, function replacer(key) {
361
+ let val = this[key];
362
+ if (val instanceof Uint8Array) {
363
+ return { $t: uint8ArrayToBase64(val) };
364
+ } else {
365
+ return val;
366
+ }
367
+ })
368
+ );
369
+ },
370
+ fromBuffer: (buff) => {
371
+ try {
372
+ return JSON.parse(decoder.decode(buff), function reviver(_key, val) {
373
+ if (val?.$t) {
374
+ return base64ToUint8Array(val.$t);
375
+ } else {
376
+ return val;
377
+ }
378
+ });
379
+ } catch {
380
+ return null;
381
+ }
382
+ }
383
+ };
384
+
385
+ // transport/transforms/delim.ts
386
+ var import_node_stream = require("stream");
387
+ var DelimiterParser = class extends import_node_stream.Transform {
388
+ delimiter;
389
+ buffer;
390
+ maxBufferSizeBytes;
391
+ constructor({ delimiter, maxBufferSizeBytes, ...options }) {
392
+ super(options);
393
+ this.maxBufferSizeBytes = maxBufferSizeBytes;
394
+ this.delimiter = Buffer.from(delimiter);
395
+ this.buffer = Buffer.alloc(0);
396
+ }
397
+ // tldr; backpressure will be automatically applied for transform streams
398
+ // but it relies on both the input/output streams connected on either end having
399
+ // implemented backpressure properly
400
+ // see: https://nodejs.org/en/guides/backpressuring-in-streams#lifecycle-of-pipe
401
+ _transform(chunk, _encoding, cb) {
402
+ let data = Buffer.concat([this.buffer, chunk]);
403
+ let position;
404
+ while ((position = data.indexOf(this.delimiter)) !== -1) {
405
+ this.push(data.subarray(0, position));
406
+ data = data.subarray(position + this.delimiter.length);
407
+ }
408
+ if (data.byteLength > this.maxBufferSizeBytes) {
409
+ const err = new Error(
410
+ `buffer overflow: ${data.byteLength}B > ${this.maxBufferSizeBytes}B`
411
+ );
412
+ this.emit("error", err);
413
+ return cb(err);
414
+ }
415
+ this.buffer = data;
416
+ cb();
417
+ }
418
+ _flush(cb) {
419
+ if (this.buffer.length) {
420
+ this.push(this.buffer);
421
+ }
422
+ this.buffer = Buffer.alloc(0);
423
+ cb();
424
+ }
425
+ _destroy(error, callback) {
426
+ this.buffer = Buffer.alloc(0);
427
+ super._destroy(error, callback);
428
+ }
429
+ };
430
+ var defaultDelimiter = Buffer.from("\n");
431
+ function createDelimitedStream(options) {
432
+ return new DelimiterParser({
433
+ delimiter: options?.delimiter ?? defaultDelimiter,
434
+ maxBufferSizeBytes: options?.maxBufferSizeBytes ?? 16 * 1024 * 1024
435
+ // 16MB
436
+ });
437
+ }
438
+
439
+ // transport/impls/stdio/connection.ts
440
+ var StreamConnection = class extends Connection {
441
+ output;
442
+ constructor(transport, connectedTo, output) {
443
+ super(transport, connectedTo);
444
+ this.output = output;
445
+ }
446
+ send(payload) {
447
+ if (!this.output.writable) {
448
+ return false;
449
+ }
450
+ return this.output.write(Buffer.concat([payload, defaultDelimiter]));
451
+ }
452
+ async close() {
453
+ this.output.end();
454
+ }
455
+ };
456
+
457
+ // transport/impls/unixsocket/server.ts
458
+ var defaultOptions = {
459
+ codec: NaiveJsonCodec
460
+ };
461
+ var UnixDomainSocketServerTransport = class extends Transport {
462
+ server;
463
+ constructor(server, clientId, providedOptions) {
464
+ const options = { ...defaultOptions, ...providedOptions };
465
+ super(options.codec, clientId);
466
+ this.server = server;
467
+ server.addListener("connection", this.connectionListener);
468
+ server.on("listening", () => {
469
+ log?.info(`${this.clientId} -- server is listening`);
470
+ });
471
+ }
472
+ connectionListener = (sock) => {
473
+ let conn = void 0;
474
+ const delimStream = createDelimitedStream();
475
+ sock.pipe(delimStream).on("data", (data) => {
476
+ const parsedMsg = this.parseMsg(data);
477
+ if (!parsedMsg) {
478
+ return;
479
+ }
480
+ if (!conn) {
481
+ conn = new StreamConnection(this, parsedMsg.from, sock);
482
+ this.onConnect(conn);
483
+ }
484
+ this.handleMsg(parsedMsg);
485
+ });
486
+ const cleanup = () => {
487
+ delimStream.destroy();
488
+ if (conn) {
489
+ this.onDisconnect(conn);
490
+ }
491
+ };
492
+ sock.on("close", cleanup);
493
+ sock.on("error", (err) => {
494
+ log?.warn(
495
+ `${this.clientId} -- socket error in connection to ${conn?.connectedTo ?? "unknown"}: ${err}`
496
+ );
497
+ cleanup();
498
+ });
499
+ };
500
+ async createNewConnection(to) {
501
+ const err = `${this.clientId} -- failed to send msg to ${to}, client probably dropped`;
502
+ log?.warn(err);
503
+ return;
504
+ }
505
+ async close() {
506
+ this.server.removeListener("connection", this.connectionListener);
507
+ super.close();
508
+ }
509
+ };
510
+ // Annotate the CommonJS export names for ESM import in node:
511
+ 0 && (module.exports = {
512
+ UnixDomainSocketServerTransport
513
+ });
@@ -0,0 +1,18 @@
1
+ import { Transport, TransportClientId } from '../../index.cjs';
2
+ import { C as Codec } from '../../../types-3e5768ec.js';
3
+ import { Server, Socket } from 'node:net';
4
+ import { S as StreamConnection } from '../../../connection-2529fc14.js';
5
+ import '@sinclair/typebox';
6
+
7
+ interface Options {
8
+ codec: Codec;
9
+ }
10
+ declare class UnixDomainSocketServerTransport extends Transport<StreamConnection> {
11
+ server: Server;
12
+ constructor(server: Server, clientId: TransportClientId, providedOptions?: Partial<Options>);
13
+ connectionListener: (sock: Socket) => void;
14
+ createNewConnection(to: string): Promise<void>;
15
+ close(): Promise<void>;
16
+ }
17
+
18
+ export { UnixDomainSocketServerTransport };
@@ -0,0 +1,18 @@
1
+ import { Transport, TransportClientId } from '../../index.js';
2
+ import { C as Codec } from '../../../types-3e5768ec.js';
3
+ import { Server, Socket } from 'node:net';
4
+ import { S as StreamConnection } from '../../../connection-316d6e3a.js';
5
+ import '@sinclair/typebox';
6
+
7
+ interface Options {
8
+ codec: Codec;
9
+ }
10
+ declare class UnixDomainSocketServerTransport extends Transport<StreamConnection> {
11
+ server: Server;
12
+ constructor(server: Server, clientId: TransportClientId, providedOptions?: Partial<Options>);
13
+ connectionListener: (sock: Socket) => void;
14
+ createNewConnection(to: string): Promise<void>;
15
+ close(): Promise<void>;
16
+ }
17
+
18
+ export { UnixDomainSocketServerTransport };
@@ -0,0 +1,73 @@
1
+ import {
2
+ StreamConnection,
3
+ createDelimitedStream
4
+ } from "../../../chunk-TZSX5KM2.js";
5
+ import "../../../chunk-ORAG7IAU.js";
6
+ import "../../../chunk-WVT5QXMZ.js";
7
+ import {
8
+ NaiveJsonCodec
9
+ } from "../../../chunk-R6H2BIMC.js";
10
+ import {
11
+ Transport
12
+ } from "../../../chunk-V2YJRBRX.js";
13
+ import "../../../chunk-ZE4MX7DF.js";
14
+ import {
15
+ log
16
+ } from "../../../chunk-SLUSVGQH.js";
17
+
18
+ // transport/impls/unixsocket/server.ts
19
+ var defaultOptions = {
20
+ codec: NaiveJsonCodec
21
+ };
22
+ var UnixDomainSocketServerTransport = class extends Transport {
23
+ server;
24
+ constructor(server, clientId, providedOptions) {
25
+ const options = { ...defaultOptions, ...providedOptions };
26
+ super(options.codec, clientId);
27
+ this.server = server;
28
+ server.addListener("connection", this.connectionListener);
29
+ server.on("listening", () => {
30
+ log?.info(`${this.clientId} -- server is listening`);
31
+ });
32
+ }
33
+ connectionListener = (sock) => {
34
+ let conn = void 0;
35
+ const delimStream = createDelimitedStream();
36
+ sock.pipe(delimStream).on("data", (data) => {
37
+ const parsedMsg = this.parseMsg(data);
38
+ if (!parsedMsg) {
39
+ return;
40
+ }
41
+ if (!conn) {
42
+ conn = new StreamConnection(this, parsedMsg.from, sock);
43
+ this.onConnect(conn);
44
+ }
45
+ this.handleMsg(parsedMsg);
46
+ });
47
+ const cleanup = () => {
48
+ delimStream.destroy();
49
+ if (conn) {
50
+ this.onDisconnect(conn);
51
+ }
52
+ };
53
+ sock.on("close", cleanup);
54
+ sock.on("error", (err) => {
55
+ log?.warn(
56
+ `${this.clientId} -- socket error in connection to ${conn?.connectedTo ?? "unknown"}: ${err}`
57
+ );
58
+ cleanup();
59
+ });
60
+ };
61
+ async createNewConnection(to) {
62
+ const err = `${this.clientId} -- failed to send msg to ${to}, client probably dropped`;
63
+ log?.warn(err);
64
+ return;
65
+ }
66
+ async close() {
67
+ this.server.removeListener("connection", this.connectionListener);
68
+ super.close();
69
+ }
70
+ };
71
+ export {
72
+ UnixDomainSocketServerTransport
73
+ };
@@ -138,6 +138,7 @@ var Transport = class {
138
138
  eventDispatcher;
139
139
  /**
140
140
  * Creates a new Transport instance.
141
+ * This should also set up {@link onConnect}, and {@link onDisconnect} listeners.
141
142
  * @param codec The codec used to encode and decode messages.
142
143
  * @param clientId The client ID of this transport.
143
144
  */
@@ -431,9 +432,6 @@ var WebSocketClientTransport = class extends Transport {
431
432
  this.serverId = serverId;
432
433
  this.options = options;
433
434
  this.reconnectPromises = /* @__PURE__ */ new Map();
434
- this.setupConnectionStatusListeners();
435
- }
436
- setupConnectionStatusListeners() {
437
435
  this.createNewConnection(this.serverId);
438
436
  }
439
437
  async createNewConnection(to, attempt = 0) {