@replit/river 0.10.4 → 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 (39) hide show
  1. package/dist/{chunk-UU2Z7LDR.js → chunk-IEU7OE5W.js} +2 -5
  2. package/dist/{chunk-FWPZDOFL.js → chunk-IYRPZPSQ.js} +3 -0
  3. package/dist/{chunk-RGMHF6PF.js → chunk-PNZXYQME.js} +6 -8
  4. package/dist/{chunk-PC65ZFWJ.js → chunk-SZTOUKL7.js} +1 -1
  5. package/dist/chunk-TZSX5KM2.js +80 -0
  6. package/dist/{chunk-AJQU4AZG.js → chunk-V2YJRBRX.js} +1 -0
  7. package/dist/connection-2529fc14.d.ts +10 -0
  8. package/dist/connection-316d6e3a.d.ts +10 -0
  9. package/dist/router/index.cjs +3 -0
  10. package/dist/router/index.js +1 -1
  11. package/dist/transport/impls/stdio/stdio.cjs +78 -35
  12. package/dist/transport/impls/stdio/stdio.d.cts +4 -11
  13. package/dist/transport/impls/stdio/stdio.d.ts +4 -11
  14. package/dist/transport/impls/stdio/stdio.js +21 -36
  15. package/dist/transport/impls/unixsocket/client.cjs +509 -0
  16. package/dist/transport/impls/unixsocket/client.d.cts +16 -0
  17. package/dist/transport/impls/unixsocket/client.d.ts +16 -0
  18. package/dist/transport/impls/unixsocket/client.js +67 -0
  19. package/dist/transport/impls/unixsocket/server.cjs +513 -0
  20. package/dist/transport/impls/unixsocket/server.d.cts +18 -0
  21. package/dist/transport/impls/unixsocket/server.d.ts +18 -0
  22. package/dist/transport/impls/unixsocket/server.js +73 -0
  23. package/dist/transport/impls/ws/client.cjs +1 -3
  24. package/dist/transport/impls/ws/client.d.cts +0 -1
  25. package/dist/transport/impls/ws/client.d.ts +0 -1
  26. package/dist/transport/impls/ws/client.js +3 -3
  27. package/dist/transport/impls/ws/server.cjs +5 -6
  28. package/dist/transport/impls/ws/server.d.cts +0 -2
  29. package/dist/transport/impls/ws/server.d.ts +0 -2
  30. package/dist/transport/impls/ws/server.js +3 -3
  31. package/dist/transport/index.cjs +1 -0
  32. package/dist/transport/index.d.cts +6 -7
  33. package/dist/transport/index.d.ts +6 -7
  34. package/dist/transport/index.js +1 -1
  35. package/dist/util/testHelpers.cjs +24 -12
  36. package/dist/util/testHelpers.d.cts +6 -3
  37. package/dist/util/testHelpers.d.ts +6 -3
  38. package/dist/util/testHelpers.js +19 -8
  39. package/package.json +23 -14
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  WebSocketConnection
3
- } from "./chunk-PC65ZFWJ.js";
3
+ } from "./chunk-SZTOUKL7.js";
4
4
  import {
5
5
  NaiveJsonCodec
6
6
  } from "./chunk-R6H2BIMC.js";
7
7
  import {
8
8
  Transport
9
- } from "./chunk-AJQU4AZG.js";
9
+ } from "./chunk-V2YJRBRX.js";
10
10
  import {
11
11
  log
12
12
  } from "./chunk-SLUSVGQH.js";
@@ -39,9 +39,6 @@ var WebSocketClientTransport = class extends Transport {
39
39
  this.serverId = serverId;
40
40
  this.options = options;
41
41
  this.reconnectPromises = /* @__PURE__ */ new Map();
42
- this.setupConnectionStatusListeners();
43
- }
44
- setupConnectionStatusListeners() {
45
42
  this.createNewConnection(this.serverId);
46
43
  }
47
44
  async createNewConnection(to, attempt = 0) {
@@ -373,6 +373,9 @@ function _pushable(getNext, options) {
373
373
  },
374
374
  get readableLength() {
375
375
  return _pushable2.readableLength;
376
+ },
377
+ onEmpty: (opts) => {
378
+ return _pushable2.onEmpty(opts);
376
379
  }
377
380
  };
378
381
  return pushable2;
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  WebSocketConnection
3
- } from "./chunk-PC65ZFWJ.js";
3
+ } from "./chunk-SZTOUKL7.js";
4
4
  import {
5
5
  NaiveJsonCodec
6
6
  } from "./chunk-R6H2BIMC.js";
7
7
  import {
8
8
  Transport
9
- } from "./chunk-AJQU4AZG.js";
9
+ } from "./chunk-V2YJRBRX.js";
10
10
  import {
11
11
  log
12
12
  } from "./chunk-SLUSVGQH.js";
@@ -17,13 +17,14 @@ var defaultOptions = {
17
17
  };
18
18
  var WebSocketServerTransport = class extends Transport {
19
19
  wss;
20
- clientId;
21
20
  constructor(wss, clientId, providedOptions) {
22
21
  const options = { ...defaultOptions, ...providedOptions };
23
22
  super(options.codec, clientId);
24
23
  this.wss = wss;
25
- this.clientId = clientId;
26
- this.setupConnectionStatusListeners();
24
+ wss.on("listening", () => {
25
+ log?.info(`${this.clientId} -- server is listening`);
26
+ });
27
+ this.wss.on("connection", this.connectionHandler);
27
28
  }
28
29
  connectionHandler = (ws) => {
29
30
  let conn = void 0;
@@ -46,9 +47,6 @@ var WebSocketServerTransport = class extends Transport {
46
47
  );
47
48
  };
48
49
  };
49
- setupConnectionStatusListeners() {
50
- this.wss.on("connection", this.connectionHandler);
51
- }
52
50
  async createNewConnection(to) {
53
51
  const err = `${this.clientId} -- failed to send msg to ${to}, client probably dropped`;
54
52
  log?.warn(err);
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Connection
3
- } from "./chunk-AJQU4AZG.js";
3
+ } from "./chunk-V2YJRBRX.js";
4
4
 
5
5
  // transport/impls/ws/connection.ts
6
6
  var WebSocketConnection = class extends Connection {
@@ -0,0 +1,80 @@
1
+ import {
2
+ Connection
3
+ } from "./chunk-V2YJRBRX.js";
4
+
5
+ // transport/transforms/delim.ts
6
+ import { Transform } from "node:stream";
7
+ var DelimiterParser = class extends Transform {
8
+ delimiter;
9
+ buffer;
10
+ maxBufferSizeBytes;
11
+ constructor({ delimiter, maxBufferSizeBytes, ...options }) {
12
+ super(options);
13
+ this.maxBufferSizeBytes = maxBufferSizeBytes;
14
+ this.delimiter = Buffer.from(delimiter);
15
+ this.buffer = Buffer.alloc(0);
16
+ }
17
+ // tldr; backpressure will be automatically applied for transform streams
18
+ // but it relies on both the input/output streams connected on either end having
19
+ // implemented backpressure properly
20
+ // see: https://nodejs.org/en/guides/backpressuring-in-streams#lifecycle-of-pipe
21
+ _transform(chunk, _encoding, cb) {
22
+ let data = Buffer.concat([this.buffer, chunk]);
23
+ let position;
24
+ while ((position = data.indexOf(this.delimiter)) !== -1) {
25
+ this.push(data.subarray(0, position));
26
+ data = data.subarray(position + this.delimiter.length);
27
+ }
28
+ if (data.byteLength > this.maxBufferSizeBytes) {
29
+ const err = new Error(
30
+ `buffer overflow: ${data.byteLength}B > ${this.maxBufferSizeBytes}B`
31
+ );
32
+ this.emit("error", err);
33
+ return cb(err);
34
+ }
35
+ this.buffer = data;
36
+ cb();
37
+ }
38
+ _flush(cb) {
39
+ if (this.buffer.length) {
40
+ this.push(this.buffer);
41
+ }
42
+ this.buffer = Buffer.alloc(0);
43
+ cb();
44
+ }
45
+ _destroy(error, callback) {
46
+ this.buffer = Buffer.alloc(0);
47
+ super._destroy(error, callback);
48
+ }
49
+ };
50
+ var defaultDelimiter = Buffer.from("\n");
51
+ function createDelimitedStream(options) {
52
+ return new DelimiterParser({
53
+ delimiter: options?.delimiter ?? defaultDelimiter,
54
+ maxBufferSizeBytes: options?.maxBufferSizeBytes ?? 16 * 1024 * 1024
55
+ // 16MB
56
+ });
57
+ }
58
+
59
+ // transport/impls/stdio/connection.ts
60
+ var StreamConnection = class extends Connection {
61
+ output;
62
+ constructor(transport, connectedTo, output) {
63
+ super(transport, connectedTo);
64
+ this.output = output;
65
+ }
66
+ send(payload) {
67
+ if (!this.output.writable) {
68
+ return false;
69
+ }
70
+ return this.output.write(Buffer.concat([payload, defaultDelimiter]));
71
+ }
72
+ async close() {
73
+ this.output.end();
74
+ }
75
+ };
76
+
77
+ export {
78
+ createDelimitedStream,
79
+ StreamConnection
80
+ };
@@ -81,6 +81,7 @@ var Transport = class {
81
81
  eventDispatcher;
82
82
  /**
83
83
  * Creates a new Transport instance.
84
+ * This should also set up {@link onConnect}, and {@link onDisconnect} listeners.
84
85
  * @param codec The codec used to encode and decode messages.
85
86
  * @param clientId The client ID of this transport.
86
87
  */
@@ -0,0 +1,10 @@
1
+ import { Connection, Transport, TransportClientId } from './transport/index.cjs';
2
+
3
+ declare class StreamConnection extends Connection {
4
+ output: NodeJS.WritableStream;
5
+ constructor(transport: Transport<StreamConnection>, connectedTo: TransportClientId, output: NodeJS.WritableStream);
6
+ send(payload: Uint8Array): boolean;
7
+ close(): Promise<void>;
8
+ }
9
+
10
+ export { StreamConnection as S };
@@ -0,0 +1,10 @@
1
+ import { Connection, Transport, TransportClientId } from './transport/index.js';
2
+
3
+ declare class StreamConnection extends Connection {
4
+ output: NodeJS.WritableStream;
5
+ constructor(transport: Transport<StreamConnection>, connectedTo: TransportClientId, output: NodeJS.WritableStream);
6
+ send(payload: Uint8Array): boolean;
7
+ close(): Promise<void>;
8
+ }
9
+
10
+ export { StreamConnection as S };
@@ -395,6 +395,9 @@ function _pushable(getNext, options) {
395
395
  },
396
396
  get readableLength() {
397
397
  return _pushable2.readableLength;
398
+ },
399
+ onEmpty: (opts) => {
400
+ return _pushable2.onEmpty(opts);
398
401
  }
399
402
  };
400
403
  return pushable2;
@@ -8,7 +8,7 @@ import {
8
8
  createClient,
9
9
  createServer,
10
10
  serializeService
11
- } from "../chunk-FWPZDOFL.js";
11
+ } from "../chunk-IYRPZPSQ.js";
12
12
  import "../chunk-ZE4MX7DF.js";
13
13
  import "../chunk-SLUSVGQH.js";
14
14
  export {
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,20 +15,11 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
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
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
 
30
20
  // transport/impls/stdio/stdio.ts
31
21
  var stdio_exports = {};
32
22
  __export(stdio_exports, {
33
- StdioConnection: () => StdioConnection,
34
23
  StdioTransport: () => StdioTransport
35
24
  });
36
25
  module.exports = __toCommonJS(stdio_exports);
@@ -84,6 +73,60 @@ var NaiveJsonCodec = {
84
73
  // logging/index.ts
85
74
  var log;
86
75
 
76
+ // transport/transforms/delim.ts
77
+ var import_node_stream = require("stream");
78
+ var DelimiterParser = class extends import_node_stream.Transform {
79
+ delimiter;
80
+ buffer;
81
+ maxBufferSizeBytes;
82
+ constructor({ delimiter, maxBufferSizeBytes, ...options }) {
83
+ super(options);
84
+ this.maxBufferSizeBytes = maxBufferSizeBytes;
85
+ this.delimiter = Buffer.from(delimiter);
86
+ this.buffer = Buffer.alloc(0);
87
+ }
88
+ // tldr; backpressure will be automatically applied for transform streams
89
+ // but it relies on both the input/output streams connected on either end having
90
+ // implemented backpressure properly
91
+ // see: https://nodejs.org/en/guides/backpressuring-in-streams#lifecycle-of-pipe
92
+ _transform(chunk, _encoding, cb) {
93
+ let data = Buffer.concat([this.buffer, chunk]);
94
+ let position;
95
+ while ((position = data.indexOf(this.delimiter)) !== -1) {
96
+ this.push(data.subarray(0, position));
97
+ data = data.subarray(position + this.delimiter.length);
98
+ }
99
+ if (data.byteLength > this.maxBufferSizeBytes) {
100
+ const err = new Error(
101
+ `buffer overflow: ${data.byteLength}B > ${this.maxBufferSizeBytes}B`
102
+ );
103
+ this.emit("error", err);
104
+ return cb(err);
105
+ }
106
+ this.buffer = data;
107
+ cb();
108
+ }
109
+ _flush(cb) {
110
+ if (this.buffer.length) {
111
+ this.push(this.buffer);
112
+ }
113
+ this.buffer = Buffer.alloc(0);
114
+ cb();
115
+ }
116
+ _destroy(error, callback) {
117
+ this.buffer = Buffer.alloc(0);
118
+ super._destroy(error, callback);
119
+ }
120
+ };
121
+ var defaultDelimiter = Buffer.from("\n");
122
+ function createDelimitedStream(options) {
123
+ return new DelimiterParser({
124
+ delimiter: options?.delimiter ?? defaultDelimiter,
125
+ maxBufferSizeBytes: options?.maxBufferSizeBytes ?? 16 * 1024 * 1024
126
+ // 16MB
127
+ });
128
+ }
129
+
87
130
  // transport/transport.ts
88
131
  var import_value = require("@sinclair/typebox/value");
89
132
 
@@ -195,6 +238,7 @@ var Transport = class {
195
238
  eventDispatcher;
196
239
  /**
197
240
  * Creates a new Transport instance.
241
+ * This should also set up {@link onConnect}, and {@link onDisconnect} listeners.
198
242
  * @param codec The codec used to encode and decode messages.
199
243
  * @param clientId The client ID of this transport.
200
244
  */
@@ -392,30 +436,29 @@ var Transport = class {
392
436
  }
393
437
  };
394
438
 
395
- // transport/impls/stdio/stdio.ts
396
- var import_readline = __toESM(require("readline"), 1);
397
- var newlineBuff = new TextEncoder().encode("\n");
398
- var StdioConnection = class extends Connection {
439
+ // transport/impls/stdio/connection.ts
440
+ var StreamConnection = class extends Connection {
399
441
  output;
400
442
  constructor(transport, connectedTo, output) {
401
443
  super(transport, connectedTo);
402
444
  this.output = output;
403
445
  }
404
446
  send(payload) {
405
- const out = new Uint8Array(payload.length + newlineBuff.length);
406
- out.set(payload, 0);
407
- out.set(newlineBuff, payload.length);
408
- return this.output.write(out);
447
+ if (!this.output.writable) {
448
+ return false;
449
+ }
450
+ return this.output.write(Buffer.concat([payload, defaultDelimiter]));
409
451
  }
410
452
  async close() {
411
453
  this.output.end();
412
454
  }
413
455
  };
456
+
457
+ // transport/impls/stdio/stdio.ts
414
458
  var defaultOptions = {
415
459
  codec: NaiveJsonCodec
416
460
  };
417
461
  var StdioTransport = class extends Transport {
418
- clientId;
419
462
  input = process.stdin;
420
463
  output = process.stdout;
421
464
  /**
@@ -427,29 +470,30 @@ var StdioTransport = class extends Transport {
427
470
  constructor(clientId, input = process.stdin, output = process.stdout, providedOptions) {
428
471
  const options = { ...defaultOptions, ...providedOptions };
429
472
  super(options.codec, clientId);
430
- this.clientId = clientId;
431
- this.input = input;
473
+ const delimStream = createDelimitedStream();
474
+ this.input = input.pipe(delimStream);
432
475
  this.output = output;
433
- this.setupConnectionStatusListeners();
434
- }
435
- setupConnectionStatusListeners() {
436
476
  let conn = void 0;
437
- const rl = import_readline.default.createInterface({
438
- input: this.input
439
- });
440
- const encoder2 = new TextEncoder();
441
- rl.on("line", (msg) => {
442
- const parsedMsg = this.parseMsg(encoder2.encode(msg));
477
+ this.input.on("data", (msg) => {
478
+ const parsedMsg = this.parseMsg(msg);
443
479
  if (parsedMsg && !this.connections.has(parsedMsg.from)) {
444
- conn = new StdioConnection(this, parsedMsg.from, this.output);
480
+ conn = new StreamConnection(this, parsedMsg.from, this.output);
445
481
  this.onConnect(conn);
446
482
  }
447
483
  this.handleMsg(parsedMsg);
448
484
  });
449
- rl.on("close", () => {
485
+ const cleanup = () => {
486
+ delimStream.destroy();
450
487
  if (conn) {
451
488
  this.onDisconnect(conn);
452
489
  }
490
+ };
491
+ this.input.on("close", cleanup);
492
+ this.input.on("error", (err) => {
493
+ log?.warn(
494
+ `${this.clientId} -- stdio error in connection to ${conn?.connectedTo ?? "unknown"}: ${err}`
495
+ );
496
+ cleanup();
453
497
  });
454
498
  }
455
499
  async createNewConnection(to) {
@@ -457,12 +501,11 @@ var StdioTransport = class extends Transport {
457
501
  throw new Error("cant reopen a destroyed connection");
458
502
  }
459
503
  log?.info(`${this.clientId} -- establishing a new stream to ${to}`);
460
- const conn = new StdioConnection(this, to, this.output);
504
+ const conn = new StreamConnection(this, to, this.output);
461
505
  this.onConnect(conn);
462
506
  }
463
507
  };
464
508
  // Annotate the CommonJS export names for ESM import in node:
465
509
  0 && (module.exports = {
466
- StdioConnection,
467
510
  StdioTransport
468
511
  });
@@ -1,13 +1,8 @@
1
1
  import { C as Codec } from '../../../types-3e5768ec.js';
2
- import { Connection, Transport, TransportClientId } from '../../index.cjs';
2
+ import { Transport, TransportClientId } from '../../index.cjs';
3
+ import { S as StreamConnection } from '../../../connection-2529fc14.js';
3
4
  import '@sinclair/typebox';
4
5
 
5
- declare class StdioConnection extends Connection {
6
- output: NodeJS.WritableStream;
7
- constructor(transport: Transport<StdioConnection>, connectedTo: TransportClientId, output: NodeJS.WritableStream);
8
- send(payload: Uint8Array): boolean;
9
- close(): Promise<void>;
10
- }
11
6
  interface Options {
12
7
  codec: Codec;
13
8
  }
@@ -15,8 +10,7 @@ interface Options {
15
10
  * A transport implementation that uses standard input and output streams.
16
11
  * @extends Transport
17
12
  */
18
- declare class StdioTransport extends Transport<StdioConnection> {
19
- clientId: TransportClientId;
13
+ declare class StdioTransport extends Transport<StreamConnection> {
20
14
  input: NodeJS.ReadableStream;
21
15
  output: NodeJS.WritableStream;
22
16
  /**
@@ -26,8 +20,7 @@ declare class StdioTransport extends Transport<StdioConnection> {
26
20
  * @param output - The writable stream to use as output. Defaults to process.stdout.
27
21
  */
28
22
  constructor(clientId: TransportClientId, input?: NodeJS.ReadableStream, output?: NodeJS.WritableStream, providedOptions?: Partial<Options>);
29
- setupConnectionStatusListeners(): void;
30
23
  createNewConnection(to: TransportClientId): Promise<void>;
31
24
  }
32
25
 
33
- export { StdioConnection, StdioTransport };
26
+ export { StdioTransport };
@@ -1,13 +1,8 @@
1
1
  import { C as Codec } from '../../../types-3e5768ec.js';
2
- import { Connection, Transport, TransportClientId } from '../../index.js';
2
+ import { Transport, TransportClientId } from '../../index.js';
3
+ import { S as StreamConnection } from '../../../connection-316d6e3a.js';
3
4
  import '@sinclair/typebox';
4
5
 
5
- declare class StdioConnection extends Connection {
6
- output: NodeJS.WritableStream;
7
- constructor(transport: Transport<StdioConnection>, connectedTo: TransportClientId, output: NodeJS.WritableStream);
8
- send(payload: Uint8Array): boolean;
9
- close(): Promise<void>;
10
- }
11
6
  interface Options {
12
7
  codec: Codec;
13
8
  }
@@ -15,8 +10,7 @@ interface Options {
15
10
  * A transport implementation that uses standard input and output streams.
16
11
  * @extends Transport
17
12
  */
18
- declare class StdioTransport extends Transport<StdioConnection> {
19
- clientId: TransportClientId;
13
+ declare class StdioTransport extends Transport<StreamConnection> {
20
14
  input: NodeJS.ReadableStream;
21
15
  output: NodeJS.WritableStream;
22
16
  /**
@@ -26,8 +20,7 @@ declare class StdioTransport extends Transport<StdioConnection> {
26
20
  * @param output - The writable stream to use as output. Defaults to process.stdout.
27
21
  */
28
22
  constructor(clientId: TransportClientId, input?: NodeJS.ReadableStream, output?: NodeJS.WritableStream, providedOptions?: Partial<Options>);
29
- setupConnectionStatusListeners(): void;
30
23
  createNewConnection(to: TransportClientId): Promise<void>;
31
24
  }
32
25
 
33
- export { StdioConnection, StdioTransport };
26
+ export { StdioTransport };
@@ -1,39 +1,24 @@
1
+ import {
2
+ StreamConnection,
3
+ createDelimitedStream
4
+ } from "../../../chunk-TZSX5KM2.js";
5
+ import "../../../chunk-ORAG7IAU.js";
1
6
  import {
2
7
  NaiveJsonCodec
3
8
  } from "../../../chunk-R6H2BIMC.js";
4
9
  import {
5
- Connection,
6
10
  Transport
7
- } from "../../../chunk-AJQU4AZG.js";
11
+ } from "../../../chunk-V2YJRBRX.js";
8
12
  import "../../../chunk-ZE4MX7DF.js";
9
13
  import {
10
14
  log
11
15
  } from "../../../chunk-SLUSVGQH.js";
12
16
 
13
17
  // transport/impls/stdio/stdio.ts
14
- import readline from "readline";
15
- var newlineBuff = new TextEncoder().encode("\n");
16
- var StdioConnection = class extends Connection {
17
- output;
18
- constructor(transport, connectedTo, output) {
19
- super(transport, connectedTo);
20
- this.output = output;
21
- }
22
- send(payload) {
23
- const out = new Uint8Array(payload.length + newlineBuff.length);
24
- out.set(payload, 0);
25
- out.set(newlineBuff, payload.length);
26
- return this.output.write(out);
27
- }
28
- async close() {
29
- this.output.end();
30
- }
31
- };
32
18
  var defaultOptions = {
33
19
  codec: NaiveJsonCodec
34
20
  };
35
21
  var StdioTransport = class extends Transport {
36
- clientId;
37
22
  input = process.stdin;
38
23
  output = process.stdout;
39
24
  /**
@@ -45,29 +30,30 @@ var StdioTransport = class extends Transport {
45
30
  constructor(clientId, input = process.stdin, output = process.stdout, providedOptions) {
46
31
  const options = { ...defaultOptions, ...providedOptions };
47
32
  super(options.codec, clientId);
48
- this.clientId = clientId;
49
- this.input = input;
33
+ const delimStream = createDelimitedStream();
34
+ this.input = input.pipe(delimStream);
50
35
  this.output = output;
51
- this.setupConnectionStatusListeners();
52
- }
53
- setupConnectionStatusListeners() {
54
36
  let conn = void 0;
55
- const rl = readline.createInterface({
56
- input: this.input
57
- });
58
- const encoder = new TextEncoder();
59
- rl.on("line", (msg) => {
60
- const parsedMsg = this.parseMsg(encoder.encode(msg));
37
+ this.input.on("data", (msg) => {
38
+ const parsedMsg = this.parseMsg(msg);
61
39
  if (parsedMsg && !this.connections.has(parsedMsg.from)) {
62
- conn = new StdioConnection(this, parsedMsg.from, this.output);
40
+ conn = new StreamConnection(this, parsedMsg.from, this.output);
63
41
  this.onConnect(conn);
64
42
  }
65
43
  this.handleMsg(parsedMsg);
66
44
  });
67
- rl.on("close", () => {
45
+ const cleanup = () => {
46
+ delimStream.destroy();
68
47
  if (conn) {
69
48
  this.onDisconnect(conn);
70
49
  }
50
+ };
51
+ this.input.on("close", cleanup);
52
+ this.input.on("error", (err) => {
53
+ log?.warn(
54
+ `${this.clientId} -- stdio error in connection to ${conn?.connectedTo ?? "unknown"}: ${err}`
55
+ );
56
+ cleanup();
71
57
  });
72
58
  }
73
59
  async createNewConnection(to) {
@@ -75,11 +61,10 @@ var StdioTransport = class extends Transport {
75
61
  throw new Error("cant reopen a destroyed connection");
76
62
  }
77
63
  log?.info(`${this.clientId} -- establishing a new stream to ${to}`);
78
- const conn = new StdioConnection(this, to, this.output);
64
+ const conn = new StreamConnection(this, to, this.output);
79
65
  this.onConnect(conn);
80
66
  }
81
67
  };
82
68
  export {
83
- StdioConnection,
84
69
  StdioTransport
85
70
  };