@zukhruf/rpc 0.0.0-stage → 0.8.180

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 (87) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +202 -2
  3. package/dist/call/client.d.ts +34 -0
  4. package/dist/call/client.d.ts.map +1 -0
  5. package/dist/call/client.js +114 -0
  6. package/dist/call/client.js.map +1 -0
  7. package/dist/call/methods.d.ts +20 -0
  8. package/dist/call/methods.d.ts.map +1 -0
  9. package/dist/call/methods.js +2 -0
  10. package/dist/call/methods.js.map +1 -0
  11. package/dist/call/pending-calls.d.ts +66 -0
  12. package/dist/call/pending-calls.d.ts.map +1 -0
  13. package/dist/call/pending-calls.js +105 -0
  14. package/dist/call/pending-calls.js.map +1 -0
  15. package/dist/call/rpc-message.d.ts +30 -0
  16. package/dist/call/rpc-message.d.ts.map +1 -0
  17. package/dist/call/rpc-message.js +17 -0
  18. package/dist/call/rpc-message.js.map +1 -0
  19. package/dist/call/server.d.ts +24 -0
  20. package/dist/call/server.d.ts.map +1 -0
  21. package/dist/call/server.js +67 -0
  22. package/dist/call/server.js.map +1 -0
  23. package/dist/channel/child-process-connection.d.ts +17 -0
  24. package/dist/channel/child-process-connection.d.ts.map +1 -0
  25. package/dist/channel/child-process-connection.js +39 -0
  26. package/dist/channel/child-process-connection.js.map +1 -0
  27. package/dist/channel/envelope.d.ts +18 -0
  28. package/dist/channel/envelope.d.ts.map +1 -0
  29. package/dist/channel/envelope.js +18 -0
  30. package/dist/channel/envelope.js.map +1 -0
  31. package/dist/channel/parent-port-connection.d.ts +20 -0
  32. package/dist/channel/parent-port-connection.d.ts.map +1 -0
  33. package/dist/channel/parent-port-connection.js +30 -0
  34. package/dist/channel/parent-port-connection.js.map +1 -0
  35. package/dist/channel/process-channel-connection.d.ts +19 -0
  36. package/dist/channel/process-channel-connection.d.ts.map +1 -0
  37. package/dist/channel/process-channel-connection.js +31 -0
  38. package/dist/channel/process-channel-connection.js.map +1 -0
  39. package/dist/channel/shared-channel-connection.d.ts +29 -0
  40. package/dist/channel/shared-channel-connection.d.ts.map +1 -0
  41. package/dist/channel/shared-channel-connection.js +43 -0
  42. package/dist/channel/shared-channel-connection.js.map +1 -0
  43. package/dist/channel/worker-connection.d.ts +18 -0
  44. package/dist/channel/worker-connection.d.ts.map +1 -0
  45. package/dist/channel/worker-connection.js +31 -0
  46. package/dist/channel/worker-connection.js.map +1 -0
  47. package/dist/connection/connection-closed-error.d.ts +5 -0
  48. package/dist/connection/connection-closed-error.d.ts.map +1 -0
  49. package/dist/connection/connection-closed-error.js +8 -0
  50. package/dist/connection/connection-closed-error.js.map +1 -0
  51. package/dist/connection/connection-supervisor.d.ts +40 -0
  52. package/dist/connection/connection-supervisor.d.ts.map +1 -0
  53. package/dist/connection/connection-supervisor.js +202 -0
  54. package/dist/connection/connection-supervisor.js.map +1 -0
  55. package/dist/connection/connection.d.ts +27 -0
  56. package/dist/connection/connection.d.ts.map +1 -0
  57. package/dist/connection/connection.js +2 -0
  58. package/dist/connection/connection.js.map +1 -0
  59. package/dist/connection/connector.d.ts +11 -0
  60. package/dist/connection/connector.d.ts.map +1 -0
  61. package/dist/connection/connector.js +2 -0
  62. package/dist/connection/connector.js.map +1 -0
  63. package/dist/error/connection-lost-error.d.ts +10 -0
  64. package/dist/error/connection-lost-error.d.ts.map +1 -0
  65. package/dist/error/connection-lost-error.js +13 -0
  66. package/dist/error/connection-lost-error.js.map +1 -0
  67. package/dist/error/error-codec.d.ts +37 -0
  68. package/dist/error/error-codec.d.ts.map +1 -0
  69. package/dist/error/error-codec.js +120 -0
  70. package/dist/error/error-codec.js.map +1 -0
  71. package/dist/error/server-error.d.ts +9 -0
  72. package/dist/error/server-error.d.ts.map +1 -0
  73. package/dist/error/server-error.js +12 -0
  74. package/dist/error/server-error.js.map +1 -0
  75. package/dist/error/server-warning.d.ts +9 -0
  76. package/dist/error/server-warning.d.ts.map +1 -0
  77. package/dist/error/server-warning.js +11 -0
  78. package/dist/error/server-warning.js.map +1 -0
  79. package/dist/index.d.ts +19 -0
  80. package/dist/index.d.ts.map +1 -0
  81. package/dist/index.js +19 -0
  82. package/dist/index.js.map +1 -0
  83. package/dist/shared/is-record.d.ts +3 -0
  84. package/dist/shared/is-record.d.ts.map +1 -0
  85. package/dist/shared/is-record.js +3 -0
  86. package/dist/shared/is-record.js.map +1 -0
  87. package/package.json +48 -4
@@ -0,0 +1,17 @@
1
+ import { isEncodedReason } from '../error/error-codec.js';
2
+ import { isRecord } from '../shared/is-record.js';
3
+ /** A received request or notification, checked before the server reads it. */
4
+ export function isRpcCall(message) {
5
+ return (isRecord(message) &&
6
+ typeof message.method === 'string' &&
7
+ Array.isArray(message.params) &&
8
+ (message.id === undefined || typeof message.id === 'string'));
9
+ }
10
+ /** A received response, checked before the client reads it. A JSON line drops a `result` that is `undefined`. */
11
+ export function isRpcResponse(message) {
12
+ return (isRecord(message) &&
13
+ typeof message.id === 'string' &&
14
+ !Object.hasOwn(message, 'method') &&
15
+ (!Object.hasOwn(message, 'error') || isEncodedReason(message.error)));
16
+ }
17
+ //# sourceMappingURL=rpc-message.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rpc-message.js","sourceRoot":"","sources":["../../src/call/rpc-message.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC9E,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAyBlD,8EAA8E;AAC9E,MAAM,UAAU,SAAS,CACvB,OAAgB;IAEhB,OAAO,CACL,QAAQ,CAAC,OAAO,CAAC;QACjB,OAAO,OAAO,CAAC,MAAM,KAAK,QAAQ;QAClC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC;QAC7B,CAAC,OAAO,CAAC,EAAE,KAAK,SAAS,IAAI,OAAO,OAAO,CAAC,EAAE,KAAK,QAAQ,CAAC,CAC7D,CAAC;AACJ,CAAC;AAED,iHAAiH;AACjH,MAAM,UAAU,aAAa,CAAC,OAAgB;IAC5C,OAAO,CACL,QAAQ,CAAC,OAAO,CAAC;QACjB,OAAO,OAAO,CAAC,EAAE,KAAK,QAAQ;QAC9B,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,QAAQ,CAAC;QACjC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,IAAI,eAAe,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CACrE,CAAC;AACJ,CAAC"}
@@ -0,0 +1,24 @@
1
+ import type { Connection } from '../connection/connection.ts';
2
+ import type { ErrorCodec } from '../error/error-codec.ts';
3
+ import type { Methods, ParamChecks } from './methods.ts';
4
+ import { type RpcMessage } from './rpc-message.ts';
5
+ export interface ServerOptions<Local extends Methods<Local>> {
6
+ /** The methods of `target` that a peer may call, each with the check of its parameters. Any other name, also one that `target` has, is refused. */
7
+ methods: ParamChecks<Local>;
8
+ /** Encodes what a method throws, so the caller gets its class and fields. */
9
+ errors: ErrorCodec;
10
+ }
11
+ /**
12
+ * Answers the calls that peers make of `target`. It answers each request with
13
+ * the method's value or the error it threw, and runs each notification
14
+ * without an answer. A message that is not a call is not the server's, so it
15
+ * is ignored. Serving does not keep the process alive: the owner of the
16
+ * connection refs it.
17
+ */
18
+ export declare class Server<Local extends Methods<Local>> {
19
+ #private;
20
+ constructor(target: Local, { methods, errors }: ServerOptions<Local>);
21
+ /** Answers the calls that arrive on `connection` until it closes. */
22
+ serve(connection: Connection<RpcMessage>): void;
23
+ }
24
+ //# sourceMappingURL=server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/call/server.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAE1D,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AACzD,OAAO,EACL,KAAK,UAAU,EAIhB,MAAM,kBAAkB,CAAC;AAE1B,MAAM,WAAW,aAAa,CAAC,KAAK,SAAS,OAAO,CAAC,KAAK,CAAC;IACzD,mJAAmJ;IACnJ,OAAO,EAAE,WAAW,CAAC,KAAK,CAAC,CAAC;IAC5B,6EAA6E;IAC7E,MAAM,EAAE,UAAU,CAAC;CACpB;AAED;;;;;;GAMG;AACH,qBAAa,MAAM,CAAC,KAAK,SAAS,OAAO,CAAC,KAAK,CAAC;;IAQ9C,YAAY,MAAM,EAAE,KAAK,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,aAAa,CAAC,KAAK,CAAC,EAInE;IAED,qEAAqE;IACrE,KAAK,CAAC,UAAU,EAAE,UAAU,CAAC,UAAU,CAAC,QAQvC;CAyCF"}
@@ -0,0 +1,67 @@
1
+ import { ConnectionClosedError } from '../connection/connection-closed-error.js';
2
+ import { ServerWarning } from '../error/server-warning.js';
3
+ import { isRpcCall, } from './rpc-message.js';
4
+ /**
5
+ * Answers the calls that peers make of `target`. It answers each request with
6
+ * the method's value or the error it threw, and runs each notification
7
+ * without an answer. A message that is not a call is not the server's, so it
8
+ * is ignored. Serving does not keep the process alive: the owner of the
9
+ * connection refs it.
10
+ */
11
+ export class Server {
12
+ #target;
13
+ #methods;
14
+ #errors;
15
+ constructor(target, { methods, errors }) {
16
+ this.#target = target;
17
+ this.#methods = new Map(Object.entries(methods));
18
+ this.#errors = errors;
19
+ }
20
+ /** Answers the calls that arrive on `connection` until it closes. */
21
+ serve(connection) {
22
+ const onMessage = (message) => {
23
+ if (!isRpcCall(message))
24
+ return;
25
+ if (message.id === undefined)
26
+ void this.#run(message);
27
+ else
28
+ void this.#answer(connection, message);
29
+ };
30
+ connection.on('message', onMessage);
31
+ connection.once('close', () => connection.off('message', onMessage));
32
+ }
33
+ async #answer(connection, request) {
34
+ let response;
35
+ try {
36
+ response = { id: request.id, result: await this.#invoke(request) };
37
+ }
38
+ catch (error) {
39
+ response = { id: request.id, error: this.#errors.encode(error) };
40
+ }
41
+ await connection.send(response).catch((error) => {
42
+ // The caller is gone; nobody waits for the answer.
43
+ if (!(error instanceof ConnectionClosedError))
44
+ throw error;
45
+ });
46
+ }
47
+ async #run(notification) {
48
+ try {
49
+ await this.#invoke(notification);
50
+ }
51
+ catch (error) {
52
+ process.emitWarning(new ServerWarning(error, { method: notification.method }));
53
+ }
54
+ }
55
+ async #invoke({ method, params, }) {
56
+ const check = this.#methods.get(method);
57
+ const run = check && Reflect.get(this.#target, method);
58
+ if (!check || typeof run !== 'function') {
59
+ throw new TypeError(`The server has no method "${method}".`);
60
+ }
61
+ if (!check(params)) {
62
+ throw new TypeError(`The parameters of "${method}" do not pass the server's check.`);
63
+ }
64
+ return Reflect.apply(run, this.#target, params);
65
+ }
66
+ }
67
+ //# sourceMappingURL=server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/call/server.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,17 @@
1
+ import type { ChildProcess } from 'node:child_process';
2
+ import type { ChannelOptions } from './envelope.ts';
3
+ import { SharedChannelConnection } from './shared-channel-connection.ts';
4
+ /**
5
+ * The parent's end of the IPC channel to one child. The kernel closes the
6
+ * channel when the child dies, so `disconnect` reports even a SIGKILL.
7
+ */
8
+ export declare class ChildProcessConnection<Outgoing> extends SharedChannelConnection<Outgoing> {
9
+ #private;
10
+ constructor(child: ChildProcess, options: ChannelOptions);
11
+ send(message: Outgoing): Promise<void>;
12
+ /** The running child already keeps the parent alive. */
13
+ ref(): void;
14
+ unref(): void;
15
+ close(): Promise<void>;
16
+ }
17
+ //# sourceMappingURL=child-process-connection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"child-process-connection.d.ts","sourceRoot":"","sources":["../../src/channel/child-process-connection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAgB,MAAM,oBAAoB,CAAC;AAIrE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAKzE;;;GAGG;AACH,qBAAa,sBAAsB,CACjC,QAAQ,CACR,SAAQ,uBAAuB,CAAC,QAAQ,CAAC;;IAIzC,YAAY,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,cAAc,EAIvD;IAEK,IAAI,CAAC,OAAO,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAc3C;IAED,wDAAwD;IACxD,GAAG,SAAK;IAER,KAAK,SAAK;IAEJ,KAAK,kBAEV;CACF"}
@@ -0,0 +1,39 @@
1
+ import { promisify } from 'node:util';
2
+ import { ConnectionClosedError } from '../connection/connection-closed-error.js';
3
+ import { SharedChannelConnection } from './shared-channel-connection.js';
4
+ /** A send to a child that died fails with EPIPE, and one after the channel closed with ERR_IPC_CHANNEL_CLOSED. */
5
+ const closedCodes = new Set(['EPIPE', 'ERR_IPC_CHANNEL_CLOSED']);
6
+ /**
7
+ * The parent's end of the IPC channel to one child. The kernel closes the
8
+ * channel when the child dies, so `disconnect` reports even a SIGKILL.
9
+ */
10
+ export class ChildProcessConnection extends SharedChannelConnection {
11
+ /** A send after the channel closed reports the error through its callback. */
12
+ #send;
13
+ constructor(child, options) {
14
+ super(child, ['disconnect'], options);
15
+ this.#send = promisify(child.send).bind(child);
16
+ this.listen();
17
+ }
18
+ async send(message) {
19
+ try {
20
+ await this.#send(this.envelope.wrap(message));
21
+ }
22
+ catch (error) {
23
+ if (Error.isError(error) &&
24
+ 'code' in error &&
25
+ typeof error.code === 'string' &&
26
+ closedCodes.has(error.code)) {
27
+ throw new ConnectionClosedError({ cause: error });
28
+ }
29
+ throw error;
30
+ }
31
+ }
32
+ /** The running child already keeps the parent alive. */
33
+ ref() { }
34
+ unref() { }
35
+ async close() {
36
+ this.stopListening();
37
+ }
38
+ }
39
+ //# sourceMappingURL=child-process-connection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"child-process-connection.js","sourceRoot":"","sources":["../../src/channel/child-process-connection.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,OAAO,EAAE,qBAAqB,EAAE,MAAM,0CAA0C,CAAC;AAEjF,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAEzE,kHAAkH;AAClH,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,CAAC,OAAO,EAAE,wBAAwB,CAAC,CAAC,CAAC;AAEjE;;;GAGG;AACH,MAAM,OAAO,sBAEX,SAAQ,uBAAiC;IACzC,8EAA8E;IACrE,KAAK,CAA4C;IAE1D,YAAY,KAAmB,EAAE,OAAuB;QACtD,KAAK,CAAC,KAAK,EAAE,CAAC,YAAY,CAAC,EAAE,OAAO,CAAC,CAAC;QACtC,IAAI,CAAC,KAAK,GAAG,SAAS,CAAqB,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACnE,IAAI,CAAC,MAAM,EAAE,CAAC;IAChB,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,OAAiB;QAC1B,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;QAChD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IACE,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;gBACpB,MAAM,IAAI,KAAK;gBACf,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ;gBAC9B,WAAW,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,EAC3B,CAAC;gBACD,MAAM,IAAI,qBAAqB,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;YACpD,CAAC;YACD,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;IAED,wDAAwD;IACxD,GAAG,KAAI,CAAC;IAER,KAAK,KAAI,CAAC;IAEV,KAAK,CAAC,KAAK;QACT,IAAI,CAAC,aAAa,EAAE,CAAC;IACvB,CAAC;CACF"}
@@ -0,0 +1,18 @@
1
+ /** How a channel that the application uses too tells its own messages apart. */
2
+ export interface ChannelOptions {
3
+ /**
4
+ * The key of the envelope that each message of this connection travels in:
5
+ * `{ [tag]: message }`. A message in no envelope, or in another tag's, is
6
+ * the application's or another protocol's, and the connection drops it.
7
+ */
8
+ tag: string;
9
+ }
10
+ /** The envelope of one tag. */
11
+ export declare class Envelope {
12
+ #private;
13
+ constructor(tag: string);
14
+ wrap(message: unknown): Record<string, unknown>;
15
+ /** The message inside this tag's envelope, unchecked; `undefined` for any other value. */
16
+ unwrap(envelope: unknown): unknown;
17
+ }
18
+ //# sourceMappingURL=envelope.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"envelope.d.ts","sourceRoot":"","sources":["../../src/channel/envelope.ts"],"names":[],"mappings":"AAEA,gFAAgF;AAChF,MAAM,WAAW,cAAc;IAC7B;;;;OAIG;IACH,GAAG,EAAE,MAAM,CAAC;CACb;AAED,+BAA+B;AAC/B,qBAAa,QAAQ;;IAGnB,YAAY,GAAG,EAAE,MAAM,EAEtB;IAED,IAAI,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAE9C;IAED,0FAA0F;IAC1F,MAAM,CAAC,QAAQ,EAAE,OAAO,GAAG,OAAO,CAIjC;CACF"}
@@ -0,0 +1,18 @@
1
+ import { isRecord } from '../shared/is-record.js';
2
+ /** The envelope of one tag. */
3
+ export class Envelope {
4
+ #tag;
5
+ constructor(tag) {
6
+ this.#tag = tag;
7
+ }
8
+ wrap(message) {
9
+ return { [this.#tag]: message };
10
+ }
11
+ /** The message inside this tag's envelope, unchecked; `undefined` for any other value. */
12
+ unwrap(envelope) {
13
+ return isRecord(envelope) && Object.hasOwn(envelope, this.#tag)
14
+ ? envelope[this.#tag]
15
+ : undefined;
16
+ }
17
+ }
18
+ //# sourceMappingURL=envelope.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"envelope.js","sourceRoot":"","sources":["../../src/channel/envelope.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAYlD,+BAA+B;AAC/B,MAAM,OAAO,QAAQ;IACV,IAAI,CAAS;IAEtB,YAAY,GAAW;QACrB,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;IAClB,CAAC;IAED,IAAI,CAAC,OAAgB;QACnB,OAAO,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,CAAC;IAClC,CAAC;IAED,0FAA0F;IAC1F,MAAM,CAAC,QAAiB;QACtB,OAAO,QAAQ,CAAC,QAAQ,CAAC,IAAI,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,IAAI,CAAC;YAC7D,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC;YACrB,CAAC,CAAC,SAAS,CAAC;IAChB,CAAC;CACF"}
@@ -0,0 +1,20 @@
1
+ import type { MessagePort } from 'node:worker_threads';
2
+ import type { ChannelOptions } from './envelope.ts';
3
+ import { SharedChannelConnection } from './shared-channel-connection.ts';
4
+ /**
5
+ * A worker thread's end of the message port to the thread that started it.
6
+ * A `message` listener keeps the worker alive, so this adapter listens only
7
+ * while referenced; messages that arrive meanwhile wait in the port. The
8
+ * starting thread cannot stop without its workers stopping too, so `close` is
9
+ * never reported.
10
+ */
11
+ export declare class ParentPortConnection<Outgoing> extends SharedChannelConnection<Outgoing> {
12
+ #private;
13
+ constructor(port: MessagePort, options: ChannelOptions);
14
+ send(message: Outgoing): Promise<void>;
15
+ /** Removes the listener before it adds it, so a second `ref` does not listen twice. */
16
+ ref(): void;
17
+ unref(): void;
18
+ close(): Promise<void>;
19
+ }
20
+ //# sourceMappingURL=parent-port-connection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parent-port-connection.d.ts","sourceRoot":"","sources":["../../src/channel/parent-port-connection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAEvD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAEzE;;;;;;GAMG;AACH,qBAAa,oBAAoB,CAC/B,QAAQ,CACR,SAAQ,uBAAuB,CAAC,QAAQ,CAAC;;IAGzC,YAAY,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,cAAc,EAGrD;IAEK,IAAI,CAAC,OAAO,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAE3C;IAED,uFAAuF;IACvF,GAAG,SAGF;IAED,KAAK,SAEJ;IAEK,KAAK,kBAEV;CACF"}
@@ -0,0 +1,30 @@
1
+ import { SharedChannelConnection } from './shared-channel-connection.js';
2
+ /**
3
+ * A worker thread's end of the message port to the thread that started it.
4
+ * A `message` listener keeps the worker alive, so this adapter listens only
5
+ * while referenced; messages that arrive meanwhile wait in the port. The
6
+ * starting thread cannot stop without its workers stopping too, so `close` is
7
+ * never reported.
8
+ */
9
+ export class ParentPortConnection extends SharedChannelConnection {
10
+ #port;
11
+ constructor(port, options) {
12
+ super(port, [], options);
13
+ this.#port = port;
14
+ }
15
+ async send(message) {
16
+ this.#port.postMessage(this.envelope.wrap(message));
17
+ }
18
+ /** Removes the listener before it adds it, so a second `ref` does not listen twice. */
19
+ ref() {
20
+ this.stopListening();
21
+ this.listen();
22
+ }
23
+ unref() {
24
+ this.stopListening();
25
+ }
26
+ async close() {
27
+ this.unref();
28
+ }
29
+ }
30
+ //# sourceMappingURL=parent-port-connection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parent-port-connection.js","sourceRoot":"","sources":["../../src/channel/parent-port-connection.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAEzE;;;;;;GAMG;AACH,MAAM,OAAO,oBAEX,SAAQ,uBAAiC;IAChC,KAAK,CAAc;IAE5B,YAAY,IAAiB,EAAE,OAAuB;QACpD,KAAK,CAAC,IAAI,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACpB,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,OAAiB;QAC1B,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;IACtD,CAAC;IAED,uFAAuF;IACvF,GAAG;QACD,IAAI,CAAC,aAAa,EAAE,CAAC;QACrB,IAAI,CAAC,MAAM,EAAE,CAAC;IAChB,CAAC;IAED,KAAK;QACH,IAAI,CAAC,aAAa,EAAE,CAAC;IACvB,CAAC;IAED,KAAK,CAAC,KAAK;QACT,IAAI,CAAC,KAAK,EAAE,CAAC;IACf,CAAC;CACF"}
@@ -0,0 +1,19 @@
1
+ import type { ChannelOptions } from './envelope.ts';
2
+ import { SharedChannelConnection } from './shared-channel-connection.ts';
3
+ /**
4
+ * The child's end of the IPC channel to its parent. Node counts `message` and
5
+ * `disconnect` listeners to decide whether the channel keeps the process alive,
6
+ * so this adapter listens only while referenced. That leaves the application's
7
+ * own listeners in charge of the process lifetime. Messages that arrive while
8
+ * not listening are buffered by Node and delivered on the next `ref`.
9
+ */
10
+ export declare class ProcessChannelConnection<Outgoing> extends SharedChannelConnection<Outgoing> {
11
+ #private;
12
+ constructor(send: (envelope: unknown) => Promise<void>, options: ChannelOptions);
13
+ send(message: Outgoing): Promise<void>;
14
+ /** Removes the listeners before it adds them, so a second `ref` does not listen twice. */
15
+ ref(): void;
16
+ unref(): void;
17
+ close(): Promise<void>;
18
+ }
19
+ //# sourceMappingURL=process-channel-connection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"process-channel-connection.d.ts","sourceRoot":"","sources":["../../src/channel/process-channel-connection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAEzE;;;;;;GAMG;AACH,qBAAa,wBAAwB,CACnC,QAAQ,CACR,SAAQ,uBAAuB,CAAC,QAAQ,CAAC;;IAIzC,YACE,IAAI,EAAE,CAAC,QAAQ,EAAE,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,EAC1C,OAAO,EAAE,cAAc,EAIxB;IAED,IAAI,CAAC,OAAO,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAErC;IAED,0FAA0F;IAC1F,GAAG,SAGF;IAED,KAAK,SAEJ;IAEK,KAAK,kBAEV;CACF"}
@@ -0,0 +1,31 @@
1
+ import { SharedChannelConnection } from './shared-channel-connection.js';
2
+ /**
3
+ * The child's end of the IPC channel to its parent. Node counts `message` and
4
+ * `disconnect` listeners to decide whether the channel keeps the process alive,
5
+ * so this adapter listens only while referenced. That leaves the application's
6
+ * own listeners in charge of the process lifetime. Messages that arrive while
7
+ * not listening are buffered by Node and delivered on the next `ref`.
8
+ */
9
+ export class ProcessChannelConnection extends SharedChannelConnection {
10
+ /** `process.send`, which reports a send after the channel closed through its callback. */
11
+ #send;
12
+ constructor(send, options) {
13
+ super(process, ['disconnect'], options);
14
+ this.#send = send;
15
+ }
16
+ send(message) {
17
+ return this.#send(this.envelope.wrap(message));
18
+ }
19
+ /** Removes the listeners before it adds them, so a second `ref` does not listen twice. */
20
+ ref() {
21
+ this.stopListening();
22
+ this.listen();
23
+ }
24
+ unref() {
25
+ this.stopListening();
26
+ }
27
+ async close() {
28
+ this.unref();
29
+ }
30
+ }
31
+ //# sourceMappingURL=process-channel-connection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"process-channel-connection.js","sourceRoot":"","sources":["../../src/channel/process-channel-connection.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAEzE;;;;;;GAMG;AACH,MAAM,OAAO,wBAEX,SAAQ,uBAAiC;IACzC,0FAA0F;IACjF,KAAK,CAAuC;IAErD,YACE,IAA0C,EAC1C,OAAuB;QAEvB,KAAK,CAAC,OAAO,EAAE,CAAC,YAAY,CAAC,EAAE,OAAO,CAAC,CAAC;QACxC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACpB,CAAC;IAED,IAAI,CAAC,OAAiB;QACpB,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;IACjD,CAAC;IAED,0FAA0F;IAC1F,GAAG;QACD,IAAI,CAAC,aAAa,EAAE,CAAC;QACrB,IAAI,CAAC,MAAM,EAAE,CAAC;IAChB,CAAC;IAED,KAAK;QACH,IAAI,CAAC,aAAa,EAAE,CAAC;IACvB,CAAC;IAED,KAAK,CAAC,KAAK;QACT,IAAI,CAAC,KAAK,EAAE,CAAC;IACf,CAAC;CACF"}
@@ -0,0 +1,29 @@
1
+ import { EventEmitter } from 'node:events';
2
+ import type { Connection, ConnectionEvents } from '../connection/connection.ts';
3
+ import { type ChannelOptions, Envelope } from './envelope.ts';
4
+ /** The end of a message channel in Node: a worker, a child process, the process itself, or a worker port. */
5
+ interface ChannelEnd {
6
+ on(event: string, listener: (envelope: unknown) => void): unknown;
7
+ once(event: string, listener: () => void): unknown;
8
+ off(event: string, listener: (envelope: unknown) => void): unknown;
9
+ }
10
+ /**
11
+ * A connection over a message channel that the application uses too: an IPC
12
+ * channel or a worker port. The messages of the connection travel in the
13
+ * envelope of its tag, so the application's own messages, which have none,
14
+ * are dropped (mutex ADR 0017). Each subclass decides when it listens,
15
+ * because the listeners can keep the process alive.
16
+ */
17
+ export declare abstract class SharedChannelConnection<Outgoing> extends EventEmitter<ConnectionEvents> implements Connection<Outgoing> {
18
+ #private;
19
+ protected readonly envelope: Envelope;
20
+ protected constructor(channel: ChannelEnd, goneEvents: readonly string[], { tag }: ChannelOptions);
21
+ abstract send(message: Outgoing): Promise<void>;
22
+ abstract ref(): void;
23
+ abstract unref(): void;
24
+ abstract close(): Promise<void>;
25
+ protected listen(): void;
26
+ protected stopListening(): void;
27
+ }
28
+ export {};
29
+ //# sourceMappingURL=shared-channel-connection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared-channel-connection.d.ts","sourceRoot":"","sources":["../../src/channel/shared-channel-connection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,OAAO,KAAK,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAChF,OAAO,EAAE,KAAK,cAAc,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9D,6GAA6G;AAC7G,UAAU,UAAU;IAClB,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,QAAQ,EAAE,OAAO,KAAK,IAAI,GAAG,OAAO,CAAC;IAClE,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC;IACnD,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,QAAQ,EAAE,OAAO,KAAK,IAAI,GAAG,OAAO,CAAC;CACpE;AAED;;;;;;GAMG;AACH,8BAAsB,uBAAuB,CAAC,QAAQ,CACpD,SAAQ,YAAY,CAAC,gBAAgB,CACrC,YAAW,UAAU,CAAC,QAAQ,CAAC;;IAE/B,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAetC,SAAS,aACP,OAAO,EAAE,UAAU,EACnB,UAAU,EAAE,SAAS,MAAM,EAAE,EAC7B,EAAE,GAAG,EAAE,EAAE,cAAc,EAMxB;IAED,QAAQ,CAAC,IAAI,CAAC,OAAO,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEhD,QAAQ,CAAC,GAAG,IAAI,IAAI,CAAC;IAErB,QAAQ,CAAC,KAAK,IAAI,IAAI,CAAC;IAEvB,QAAQ,CAAC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEhC,SAAS,CAAC,MAAM,SAKf;IAED,SAAS,CAAC,aAAa,SAKtB;CACF"}
@@ -0,0 +1,43 @@
1
+ import { EventEmitter } from 'node:events';
2
+ import { Envelope } from './envelope.js';
3
+ /**
4
+ * A connection over a message channel that the application uses too: an IPC
5
+ * channel or a worker port. The messages of the connection travel in the
6
+ * envelope of its tag, so the application's own messages, which have none,
7
+ * are dropped (mutex ADR 0017). Each subclass decides when it listens,
8
+ * because the listeners can keep the process alive.
9
+ */
10
+ export class SharedChannelConnection extends EventEmitter {
11
+ envelope;
12
+ #channel;
13
+ /** The events of the channel that tell that the peer is gone. */
14
+ #goneEvents;
15
+ #onMessage = (envelope) => {
16
+ const message = this.envelope.unwrap(envelope);
17
+ if (message !== undefined)
18
+ this.emit('message', message);
19
+ };
20
+ #onGone = () => {
21
+ this.stopListening();
22
+ this.emit('close');
23
+ };
24
+ constructor(channel, goneEvents, { tag }) {
25
+ super();
26
+ this.#channel = channel;
27
+ this.#goneEvents = goneEvents;
28
+ this.envelope = new Envelope(tag);
29
+ }
30
+ listen() {
31
+ this.#channel.on('message', this.#onMessage);
32
+ for (const event of this.#goneEvents) {
33
+ this.#channel.once(event, this.#onGone);
34
+ }
35
+ }
36
+ stopListening() {
37
+ this.#channel.off('message', this.#onMessage);
38
+ for (const event of this.#goneEvents) {
39
+ this.#channel.off(event, this.#onGone);
40
+ }
41
+ }
42
+ }
43
+ //# sourceMappingURL=shared-channel-connection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared-channel-connection.js","sourceRoot":"","sources":["../../src/channel/shared-channel-connection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG3C,OAAO,EAAuB,QAAQ,EAAE,MAAM,eAAe,CAAC;AAS9D;;;;;;GAMG;AACH,MAAM,OAAgB,uBACpB,SAAQ,YAA8B;IAGnB,QAAQ,CAAW;IAC7B,QAAQ,CAAa;IAC9B,iEAAiE;IACxD,WAAW,CAAoB;IAE/B,UAAU,GAAG,CAAC,QAAiB,EAAE,EAAE;QAC1C,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAC/C,IAAI,OAAO,KAAK,SAAS;YAAE,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAC3D,CAAC,CAAC;IAEO,OAAO,GAAG,GAAG,EAAE;QACtB,IAAI,CAAC,aAAa,EAAE,CAAC;QACrB,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACrB,CAAC,CAAC;IAEF,YACE,OAAmB,EACnB,UAA6B,EAC7B,EAAE,GAAG,EAAkB;QAEvB,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC;QAC9B,IAAI,CAAC,QAAQ,GAAG,IAAI,QAAQ,CAAC,GAAG,CAAC,CAAC;IACpC,CAAC;IAUS,MAAM;QACd,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,SAAS,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;QAC7C,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;YACrC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QAC1C,CAAC;IACH,CAAC;IAES,aAAa;QACrB,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;QAC9C,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;YACrC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QACzC,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,18 @@
1
+ import type { Worker } from 'node:worker_threads';
2
+ import type { ChannelOptions } from './envelope.ts';
3
+ import { SharedChannelConnection } from './shared-channel-connection.ts';
4
+ /**
5
+ * The starting thread's end of the message port to one worker thread.
6
+ * `Worker` emits `exit` however the thread stops, so a terminated or crashed
7
+ * thread is always noticed.
8
+ */
9
+ export declare class WorkerConnection<Outgoing> extends SharedChannelConnection<Outgoing> {
10
+ #private;
11
+ constructor(worker: Worker, options: ChannelOptions);
12
+ send(message: Outgoing): Promise<void>;
13
+ /** The running worker already keeps its starting thread alive. */
14
+ ref(): void;
15
+ unref(): void;
16
+ close(): Promise<void>;
17
+ }
18
+ //# sourceMappingURL=worker-connection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker-connection.d.ts","sourceRoot":"","sources":["../../src/channel/worker-connection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAGlD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAEzE;;;;GAIG;AACH,qBAAa,gBAAgB,CAC3B,QAAQ,CACR,SAAQ,uBAAuB,CAAC,QAAQ,CAAC;;IAGzC,YAAY,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,EAIlD;IAEK,IAAI,CAAC,OAAO,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAQ3C;IAED,kEAAkE;IAClE,GAAG,SAAK;IAER,KAAK,SAAK;IAEJ,KAAK,kBAEV;CACF"}
@@ -0,0 +1,31 @@
1
+ import { ConnectionClosedError } from '../connection/connection-closed-error.js';
2
+ import { SharedChannelConnection } from './shared-channel-connection.js';
3
+ /**
4
+ * The starting thread's end of the message port to one worker thread.
5
+ * `Worker` emits `exit` however the thread stops, so a terminated or crashed
6
+ * thread is always noticed.
7
+ */
8
+ export class WorkerConnection extends SharedChannelConnection {
9
+ #worker;
10
+ constructor(worker, options) {
11
+ super(worker, ['exit'], options);
12
+ this.#worker = worker;
13
+ this.listen();
14
+ }
15
+ async send(message) {
16
+ // A stopped worker drops messages without an error; its thread id is -1 from then on.
17
+ if (this.#worker.threadId === -1) {
18
+ throw new ConnectionClosedError({
19
+ cause: new Error('The worker thread has exited.'),
20
+ });
21
+ }
22
+ this.#worker.postMessage(this.envelope.wrap(message));
23
+ }
24
+ /** The running worker already keeps its starting thread alive. */
25
+ ref() { }
26
+ unref() { }
27
+ async close() {
28
+ this.stopListening();
29
+ }
30
+ }
31
+ //# sourceMappingURL=worker-connection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker-connection.js","sourceRoot":"","sources":["../../src/channel/worker-connection.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,0CAA0C,CAAC;AAEjF,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAEzE;;;;GAIG;AACH,MAAM,OAAO,gBAEX,SAAQ,uBAAiC;IAChC,OAAO,CAAS;IAEzB,YAAY,MAAc,EAAE,OAAuB;QACjD,KAAK,CAAC,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,CAAC;QACjC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,IAAI,CAAC,MAAM,EAAE,CAAC;IAChB,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,OAAiB;QAC1B,sFAAsF;QACtF,IAAI,IAAI,CAAC,OAAO,CAAC,QAAQ,KAAK,CAAC,CAAC,EAAE,CAAC;YACjC,MAAM,IAAI,qBAAqB,CAAC;gBAC9B,KAAK,EAAE,IAAI,KAAK,CAAC,+BAA+B,CAAC;aAClD,CAAC,CAAC;QACL,CAAC;QACD,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;IACxD,CAAC;IAED,kEAAkE;IAClE,GAAG,KAAI,CAAC;IAER,KAAK,KAAI,CAAC;IAEV,KAAK,CAAC,KAAK;QACT,IAAI,CAAC,aAAa,EAAE,CAAC;IACvB,CAAC;CACF"}
@@ -0,0 +1,5 @@
1
+ /** A send that cannot reach the peer, because the connection is closed. The `cause` is the error of the transport. */
2
+ export declare class ConnectionClosedError extends Error {
3
+ constructor(options?: ErrorOptions);
4
+ }
5
+ //# sourceMappingURL=connection-closed-error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connection-closed-error.d.ts","sourceRoot":"","sources":["../../src/connection/connection-closed-error.ts"],"names":[],"mappings":"AAAA,sHAAsH;AACtH,qBAAa,qBAAsB,SAAQ,KAAK;IAC9C,YAAY,OAAO,CAAC,EAAE,YAAY,EAGjC;CACF"}
@@ -0,0 +1,8 @@
1
+ /** A send that cannot reach the peer, because the connection is closed. The `cause` is the error of the transport. */
2
+ export class ConnectionClosedError extends Error {
3
+ constructor(options) {
4
+ super('The connection is closed.', options);
5
+ this.name = 'ConnectionClosedError';
6
+ }
7
+ }
8
+ //# sourceMappingURL=connection-closed-error.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connection-closed-error.js","sourceRoot":"","sources":["../../src/connection/connection-closed-error.ts"],"names":[],"mappings":"AAAA,sHAAsH;AACtH,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IAC9C,YAAY,OAAsB;QAChC,KAAK,CAAC,2BAA2B,EAAE,OAAO,CAAC,CAAC;QAC5C,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;IACtC,CAAC;CACF"}
@@ -0,0 +1,40 @@
1
+ import { EventEmitter } from 'node:events';
2
+ import type { Connector } from './connector.ts';
3
+ export type SupervisorStatus = 'idle' | 'connecting' | 'connected' | 'unavailable' | 'closed';
4
+ export interface SupervisorEvents {
5
+ /** A connection is open: the first one, or the replacement for a lost one. */
6
+ connected: [];
7
+ message: [message: unknown];
8
+ /** The open connection is gone and a replacement is on its way. Nothing sent on it will be answered. */
9
+ disconnected: [];
10
+ /** The connector can never connect again. */
11
+ unavailable: [];
12
+ /** One attempt to connect failed; the next `open` tries again. */
13
+ failed: [error: unknown];
14
+ }
15
+ /**
16
+ * Keeps one connection open at a time. It connects on `open`, replaces a
17
+ * connection that is lost, and stops for good when its connector has no peer
18
+ * left. It never reads the messages it carries: what a replacement must be
19
+ * told again is for its listener to decide.
20
+ */
21
+ export declare class ConnectionSupervisor<Outgoing> extends EventEmitter<SupervisorEvents> {
22
+ #private;
23
+ constructor(connector: Connector<Outgoing>);
24
+ get status(): SupervisorStatus;
25
+ /** Starts connecting, unless a connection is open, on its way, or never coming. */
26
+ open(): void;
27
+ /** Whether `message` was handed to an open connection. A failed delivery is reported as a loss. */
28
+ send(message: Outgoing): boolean;
29
+ /**
30
+ * Keeps the process alive through the open connection and each one that
31
+ * replaces it, until `unref`. A new supervisor keeps the process alive, as a
32
+ * new socket does.
33
+ */
34
+ ref(): void;
35
+ /** Lets the process exit while the open connection, or one that replaces it, waits. */
36
+ unref(): void;
37
+ /** Closes for good, after any connect still on its way has stopped. */
38
+ close(): Promise<void>;
39
+ }
40
+ //# sourceMappingURL=connection-supervisor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connection-supervisor.d.ts","sourceRoot":"","sources":["../../src/connection/connection-supervisor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAK3C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD,MAAM,MAAM,gBAAgB,GAC1B,MAAM,GAAG,YAAY,GAAG,WAAW,GAAG,aAAa,GAAG,QAAQ,CAAC;AAEjE,MAAM,WAAW,gBAAgB;IAC/B,8EAA8E;IAC9E,SAAS,EAAE,EAAE,CAAC;IACd,OAAO,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC5B,wGAAwG;IACxG,YAAY,EAAE,EAAE,CAAC;IACjB,6CAA6C;IAC7C,WAAW,EAAE,EAAE,CAAC;IAChB,kEAAkE;IAClE,MAAM,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;CAC1B;AAED;;;;;GAKG;AACH,qBAAa,oBAAoB,CAC/B,QAAQ,CACR,SAAQ,YAAY,CAAC,gBAAgB,CAAC;;IAGtC,YAAY,SAAS,EAAE,SAAS,CAAC,QAAQ,CAAC,EAGzC;IAED,IAAI,MAAM,IAAI,gBAAgB,CAE7B;IAED,mFAAmF;IACnF,IAAI,SAEH;IAED,mGAAmG;IACnG,IAAI,CAAC,OAAO,EAAE,QAAQ,GAAG,OAAO,CAE/B;IAED;;;;OAIG;IACH,GAAG,SAEF;IAED,uFAAuF;IACvF,KAAK,SAEJ;IAED,uEAAuE;IACvE,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAErB;CACF"}