@replit/river 0.10.0 → 0.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. package/dist/{router/builder.d.ts → builder-3c4485f0.d.ts} +76 -21
  2. package/dist/chunk-AJQU4AZG.js +284 -0
  3. package/dist/chunk-IYRPZPSQ.js +964 -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 +1046 -0
  23. package/dist/router/index.d.cts +114 -0
  24. package/dist/router/index.d.ts +114 -12
  25. package/dist/router/index.js +24 -5
  26. package/dist/transport/impls/ws/client.cjs +505 -0
  27. package/dist/transport/impls/ws/client.d.cts +42 -0
  28. package/dist/transport/impls/ws/client.d.ts +8 -8
  29. package/dist/transport/impls/ws/client.js +10 -100
  30. package/dist/transport/impls/ws/server.cjs +457 -0
  31. package/dist/transport/impls/ws/server.d.cts +21 -0
  32. package/dist/transport/impls/ws/server.d.ts +11 -10
  33. package/dist/transport/impls/ws/server.js +11 -52
  34. package/dist/transport/index.cjs +362 -0
  35. package/dist/transport/{transport.d.ts → index.d.cts} +119 -7
  36. package/dist/transport/index.d.ts +273 -4
  37. package/dist/transport/index.js +20 -2
  38. package/dist/{codec/types.d.ts → types-3e5768ec.d.ts} +3 -2
  39. package/dist/util/testHelpers.cjs +1010 -0
  40. package/dist/util/testHelpers.d.cts +79 -0
  41. package/dist/util/testHelpers.d.ts +22 -19
  42. package/dist/util/testHelpers.js +135 -163
  43. package/package.json +42 -14
  44. package/dist/__tests__/bandwidth.bench.d.ts +0 -2
  45. package/dist/__tests__/bandwidth.bench.d.ts.map +0 -1
  46. package/dist/__tests__/bandwidth.bench.js +0 -90
  47. package/dist/__tests__/cleanup.test.d.ts +0 -2
  48. package/dist/__tests__/cleanup.test.d.ts.map +0 -1
  49. package/dist/__tests__/cleanup.test.js +0 -165
  50. package/dist/__tests__/disconnects.test.d.ts +0 -2
  51. package/dist/__tests__/disconnects.test.d.ts.map +0 -1
  52. package/dist/__tests__/disconnects.test.js +0 -163
  53. package/dist/__tests__/e2e.test.d.ts +0 -2
  54. package/dist/__tests__/e2e.test.d.ts.map +0 -1
  55. package/dist/__tests__/e2e.test.js +0 -317
  56. package/dist/__tests__/fixtures/cleanup.d.ts +0 -12
  57. package/dist/__tests__/fixtures/cleanup.d.ts.map +0 -1
  58. package/dist/__tests__/fixtures/cleanup.js +0 -36
  59. package/dist/__tests__/fixtures/largePayload.json +0 -33
  60. package/dist/__tests__/fixtures/observable.d.ts +0 -26
  61. package/dist/__tests__/fixtures/observable.d.ts.map +0 -1
  62. package/dist/__tests__/fixtures/observable.js +0 -38
  63. package/dist/__tests__/fixtures/observable.test.d.ts +0 -2
  64. package/dist/__tests__/fixtures/observable.test.d.ts.map +0 -1
  65. package/dist/__tests__/fixtures/observable.test.js +0 -39
  66. package/dist/__tests__/fixtures/services.d.ts +0 -288
  67. package/dist/__tests__/fixtures/services.d.ts.map +0 -1
  68. package/dist/__tests__/fixtures/services.js +0 -207
  69. package/dist/__tests__/handler.test.d.ts +0 -2
  70. package/dist/__tests__/handler.test.d.ts.map +0 -1
  71. package/dist/__tests__/handler.test.js +0 -120
  72. package/dist/__tests__/serialize.test.d.ts +0 -2
  73. package/dist/__tests__/serialize.test.d.ts.map +0 -1
  74. package/dist/__tests__/serialize.test.js +0 -208
  75. package/dist/__tests__/typescript-stress.test.d.ts +0 -1583
  76. package/dist/__tests__/typescript-stress.test.d.ts.map +0 -1
  77. package/dist/__tests__/typescript-stress.test.js +0 -123
  78. package/dist/codec/binary.d.ts +0 -7
  79. package/dist/codec/binary.d.ts.map +0 -1
  80. package/dist/codec/binary.js +0 -20
  81. package/dist/codec/codec.test.d.ts +0 -5
  82. package/dist/codec/codec.test.d.ts.map +0 -1
  83. package/dist/codec/codec.test.js +0 -41
  84. package/dist/codec/index.d.ts.map +0 -1
  85. package/dist/codec/json.d.ts +0 -7
  86. package/dist/codec/json.d.ts.map +0 -1
  87. package/dist/codec/json.js +0 -51
  88. package/dist/codec/types.d.ts.map +0 -1
  89. package/dist/codec/types.js +0 -1
  90. package/dist/logging/index.d.ts.map +0 -1
  91. package/dist/router/builder.d.ts.map +0 -1
  92. package/dist/router/builder.js +0 -91
  93. package/dist/router/client.d.ts +0 -72
  94. package/dist/router/client.d.ts.map +0 -1
  95. package/dist/router/client.js +0 -257
  96. package/dist/router/context.d.ts +0 -30
  97. package/dist/router/context.d.ts.map +0 -1
  98. package/dist/router/context.js +0 -1
  99. package/dist/router/defs.d.ts +0 -16
  100. package/dist/router/defs.d.ts.map +0 -1
  101. package/dist/router/defs.js +0 -11
  102. package/dist/router/index.d.ts.map +0 -1
  103. package/dist/router/result.d.ts +0 -26
  104. package/dist/router/result.d.ts.map +0 -1
  105. package/dist/router/result.js +0 -22
  106. package/dist/router/server.d.ts +0 -39
  107. package/dist/router/server.d.ts.map +0 -1
  108. package/dist/router/server.js +0 -260
  109. package/dist/transport/events.d.ts +0 -19
  110. package/dist/transport/events.d.ts.map +0 -1
  111. package/dist/transport/events.js +0 -26
  112. package/dist/transport/impls/stdio/stdio.d.ts +0 -33
  113. package/dist/transport/impls/stdio/stdio.d.ts.map +0 -1
  114. package/dist/transport/impls/stdio/stdio.js +0 -75
  115. package/dist/transport/impls/stdio/stdio.test.d.ts +0 -2
  116. package/dist/transport/impls/stdio/stdio.test.d.ts.map +0 -1
  117. package/dist/transport/impls/stdio/stdio.test.js +0 -24
  118. package/dist/transport/impls/ws/client.d.ts.map +0 -1
  119. package/dist/transport/impls/ws/connection.d.ts +0 -11
  120. package/dist/transport/impls/ws/connection.d.ts.map +0 -1
  121. package/dist/transport/impls/ws/connection.js +0 -23
  122. package/dist/transport/impls/ws/server.d.ts.map +0 -1
  123. package/dist/transport/impls/ws/ws.test.d.ts +0 -2
  124. package/dist/transport/impls/ws/ws.test.d.ts.map +0 -1
  125. package/dist/transport/impls/ws/ws.test.js +0 -185
  126. package/dist/transport/index.d.ts.map +0 -1
  127. package/dist/transport/message.d.ts +0 -142
  128. package/dist/transport/message.d.ts.map +0 -1
  129. package/dist/transport/message.js +0 -113
  130. package/dist/transport/message.test.d.ts +0 -2
  131. package/dist/transport/message.test.d.ts.map +0 -1
  132. package/dist/transport/message.test.js +0 -52
  133. package/dist/transport/transport.d.ts.map +0 -1
  134. package/dist/transport/transport.js +0 -281
  135. package/dist/util/testHelpers.d.ts.map +0 -1
@@ -1,19 +1,73 @@
1
- import { TObject, Static, TUnion } from '@sinclair/typebox';
2
- import type { Pushable } from 'it-pushable';
3
- import { ServiceContextWithState } from './context';
4
- import { Result, RiverError, RiverUncaughtSchema } from './result';
1
+ import * as _sinclair_typebox from '@sinclair/typebox';
2
+ import { TObject, TLiteralString, TString, TSchema, TUnion, TNever, Static } from '@sinclair/typebox';
3
+ import { Pushable } from 'it-pushable';
4
+
5
+ /**
6
+ * The context for services/procedures. This is used only on
7
+ * the server.
8
+ *
9
+ * An important detail is that the state prop is always on
10
+ * this interface and it shouldn't be changed, removed, or
11
+ * extended. This prop is for the state of a service.
12
+ *
13
+ * You should use declaration merging to extend this interface
14
+ * with whatever you need. For example, if you need to access
15
+ * a database, you could do:
16
+ *
17
+ * ```ts
18
+ * declare module '@replit/river' {
19
+ * interface ServiceContext {
20
+ * db: Database;
21
+ * }
22
+ * }
23
+ * ```
24
+ */
25
+ interface ServiceContext {
26
+ state: object | unknown;
27
+ }
28
+ /**
29
+ * The {@link ServiceContext} with state. This is what is passed to procedures.
30
+ */
31
+ type ServiceContextWithState<State extends object | unknown> = ServiceContext & {
32
+ state: State;
33
+ };
34
+
35
+ type RiverErrorSchema = TObject<{
36
+ code: TLiteralString;
37
+ message: TLiteralString | TString;
38
+ }> | TObject<{
39
+ code: TLiteralString;
40
+ message: TLiteralString | TString;
41
+ extras: TSchema;
42
+ }>;
43
+ type RiverError = TUnion<RiverErrorSchema[]> | RiverErrorSchema | TNever;
44
+ declare const UNCAUGHT_ERROR = "UNCAUGHT_ERROR";
45
+ declare const RiverUncaughtSchema: TObject<{
46
+ code: TUnion<[_sinclair_typebox.TLiteral<"UNCAUGHT_ERROR">, _sinclair_typebox.TLiteral<"UNEXPECTED_DISCONNECT">]>;
47
+ message: TString;
48
+ }>;
49
+ type Result<T, E> = {
50
+ ok: true;
51
+ payload: T;
52
+ } | {
53
+ ok: false;
54
+ payload: E;
55
+ };
56
+ declare function Ok<T, E>(payload: T): Result<T, E>;
57
+ declare function Err<T, E>(error: E): Result<T, E>;
58
+
5
59
  /**
6
60
  * The valid {@link Procedure} types. The `stream` and `upload` types can optionally have a
7
61
  * different type for the very first initialization message. The suffixless types correspond to
8
62
  * gRPC's four combinations of stream / non-stream in each direction.
9
63
  */
10
- export type ValidProcType = 'rpc' | 'upload' | 'subscription' | 'stream';
64
+ type ValidProcType = 'rpc' | 'upload' | 'subscription' | 'stream';
11
65
  /**
12
66
  * A generic procedure listing where the keys are the names of the procedures
13
67
  * and the values are the {@link Procedure} definitions. This is not meant to
14
68
  * be constructed directly, use the {@link ServiceBuilder} class instead.
15
69
  */
16
- export type ProcListing = Record<string, AnyProcedure>;
70
+ type ProcListing = Record<string, AnyProcedure>;
17
71
  /**
18
72
  * Represents a service with a name, state, and procedures.
19
73
  * This is not meant to be constructed directly, use the {@link ServiceBuilder} class instead.
@@ -21,30 +75,30 @@ export type ProcListing = Record<string, AnyProcedure>;
21
75
  * @template State The type of the service state.
22
76
  * @template Procs The type of the service procedures.
23
77
  */
24
- export interface Service<Name extends string, State extends object, Procs extends ProcListing> {
78
+ interface Service<Name extends string, State extends object, Procs extends ProcListing> {
25
79
  name: Name;
26
80
  state: State;
27
81
  procedures: Procs;
28
82
  }
29
- export type AnyService = Service<string, object, any>;
83
+ type AnyService = Service<string, object, any>;
30
84
  /**
31
85
  * Serializes a service object into its corresponding JSON Schema Draft 7 type.
32
86
  * @param {AnyService} s - The service object to serialize.
33
87
  * @returns A plain object representing the serialized service.
34
88
  */
35
- export declare function serializeService(s: AnyService): object;
89
+ declare function serializeService(s: AnyService): object;
36
90
  /**
37
91
  * Helper to get the type definition for a specific handler of a procedure in a service.
38
92
  * @template S - The service.
39
93
  * @template ProcName - The name of the procedure.
40
94
  */
41
- export type ProcHandler<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['handler'];
95
+ type ProcHandler<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['handler'];
42
96
  /**
43
97
  * Helper to get whether the type definition for the procedure contains an init type.
44
98
  * @template S - The service.
45
99
  * @template ProcName - The name of the procedure.
46
100
  */
47
- export type ProcHasInit<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName] extends {
101
+ type ProcHasInit<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName] extends {
48
102
  init: any;
49
103
  } ? true : false;
50
104
  /**
@@ -52,32 +106,32 @@ export type ProcHasInit<S extends AnyService, ProcName extends keyof S['procedur
52
106
  * @template S - The service.
53
107
  * @template ProcName - The name of the procedure.
54
108
  */
55
- export type ProcInit<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['init'];
109
+ type ProcInit<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['init'];
56
110
  /**
57
111
  * Helper to get the type definition for the procedure input of a service.
58
112
  * @template S - The service.
59
113
  * @template ProcName - The name of the procedure.
60
114
  */
61
- export type ProcInput<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['input'];
115
+ type ProcInput<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['input'];
62
116
  /**
63
117
  * Helper to get the type definition for the procedure output of a service.
64
118
  * @template S - The service.
65
119
  * @template ProcName - The name of the procedure.
66
120
  */
67
- export type ProcOutput<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['output'];
121
+ type ProcOutput<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['output'];
68
122
  /**
69
123
  * Helper to get the type definition for the procedure errors of a service.
70
124
  * @template S - The service.
71
125
  * @template ProcName - The name of the procedure.
72
126
  */
73
- export type ProcErrors<S extends AnyService, ProcName extends keyof S['procedures']> = TUnion<[S['procedures'][ProcName]['errors'], typeof RiverUncaughtSchema]>;
127
+ type ProcErrors<S extends AnyService, ProcName extends keyof S['procedures']> = TUnion<[S['procedures'][ProcName]['errors'], typeof RiverUncaughtSchema]>;
74
128
  /**
75
129
  * Helper to get the type of procedure in a service.
76
130
  * @template S - The service.
77
131
  * @template ProcName - The name of the procedure.
78
132
  */
79
- export type ProcType<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['type'];
80
- export type PayloadType = TObject | TUnion<TObject[]>;
133
+ type ProcType<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['type'];
134
+ type PayloadType = TObject | TUnion<TObject[]>;
81
135
  /**
82
136
  * Defines a Procedure type that can be either an RPC or a stream procedure.
83
137
  * @template State - The TypeBox schema of the state object.
@@ -86,7 +140,7 @@ export type PayloadType = TObject | TUnion<TObject[]>;
86
140
  * @template O - The TypeBox schema of the output object.
87
141
  * @template Init - The TypeBox schema of the input initialization object.
88
142
  */
89
- export type Procedure<State extends object | unknown, Ty extends ValidProcType, I extends PayloadType, O extends PayloadType, E extends RiverError, Init extends PayloadType | null = null> = Ty extends 'rpc' ? Init extends null ? {
143
+ type Procedure<State extends object | unknown, Ty extends ValidProcType, I extends PayloadType, O extends PayloadType, E extends RiverError, Init extends PayloadType | null = null> = Ty extends 'rpc' ? Init extends null ? {
90
144
  input: I;
91
145
  output: O;
92
146
  errors: E;
@@ -125,13 +179,13 @@ export type Procedure<State extends object | unknown, Ty extends ValidProcType,
125
179
  handler: (context: ServiceContextWithState<State>, input: AsyncIterableIterator<Static<I>>, output: Pushable<Result<Static<O>, Static<E>>>) => Promise<void>;
126
180
  type: Ty;
127
181
  } : never;
128
- export type AnyProcedure = Procedure<object, ValidProcType, PayloadType, PayloadType, RiverError, PayloadType | null>;
182
+ type AnyProcedure = Procedure<object, ValidProcType, PayloadType, PayloadType, RiverError, PayloadType | null>;
129
183
  /**
130
184
  * A builder class for creating River Services.
131
185
  * You must call the finalize method to get the finalized schema for use in a service router.
132
186
  * @template T The type of the service.
133
187
  */
134
- export declare class ServiceBuilder<T extends Service<string, object, ProcListing>> {
188
+ declare class ServiceBuilder<T extends Service<string, object, ProcListing>> {
135
189
  private readonly schema;
136
190
  private constructor();
137
191
  /**
@@ -174,4 +228,5 @@ export declare class ServiceBuilder<T extends Service<string, object, ProcListin
174
228
  procedures: {};
175
229
  }>;
176
230
  }
177
- //# sourceMappingURL=builder.d.ts.map
231
+
232
+ export { AnyService as A, Err as E, Ok as O, PayloadType as P, RiverError as R, ServiceContext as S, UNCAUGHT_ERROR as U, ValidProcType as V, Procedure as a, Result as b, RiverUncaughtSchema as c, AnyProcedure as d, ProcType as e, ProcInput as f, ProcOutput as g, ProcErrors as h, ProcHasInit as i, ProcInit as j, ServiceBuilder as k, ProcListing as l, Service as m, ProcHandler as n, ServiceContextWithState as o, RiverErrorSchema as p, serializeService as s };
@@ -0,0 +1,284 @@
1
+ import {
2
+ OpaqueTransportMessageSchema,
3
+ TransportAckSchema,
4
+ isAck,
5
+ reply
6
+ } from "./chunk-ZE4MX7DF.js";
7
+ import {
8
+ log
9
+ } from "./chunk-SLUSVGQH.js";
10
+
11
+ // transport/transport.ts
12
+ import { Value } from "@sinclair/typebox/value";
13
+
14
+ // transport/events.ts
15
+ var EventDispatcher = class {
16
+ eventListeners = {};
17
+ numberOfListeners(eventType) {
18
+ return this.eventListeners[eventType]?.size ?? 0;
19
+ }
20
+ addEventListener(eventType, handler) {
21
+ if (!this.eventListeners[eventType]) {
22
+ this.eventListeners[eventType] = /* @__PURE__ */ new Set();
23
+ }
24
+ this.eventListeners[eventType]?.add(handler);
25
+ }
26
+ removeEventListener(eventType, handler) {
27
+ const handlers = this.eventListeners[eventType];
28
+ if (handlers) {
29
+ this.eventListeners[eventType]?.delete(handler);
30
+ }
31
+ }
32
+ dispatchEvent(eventType, event) {
33
+ const handlers = this.eventListeners[eventType];
34
+ if (handlers) {
35
+ for (const handler of handlers) {
36
+ handler(event);
37
+ }
38
+ }
39
+ }
40
+ };
41
+
42
+ // transport/transport.ts
43
+ var Connection = class {
44
+ connectedTo;
45
+ transport;
46
+ constructor(transport, connectedTo) {
47
+ this.connectedTo = connectedTo;
48
+ this.transport = transport;
49
+ }
50
+ };
51
+ var Transport = class {
52
+ /**
53
+ * A flag indicating whether the transport has been destroyed.
54
+ * A destroyed transport will not attempt to reconnect and cannot be used again.
55
+ */
56
+ state;
57
+ /**
58
+ * The {@link Codec} used to encode and decode messages.
59
+ */
60
+ codec;
61
+ /**
62
+ * The client ID of this transport.
63
+ */
64
+ clientId;
65
+ /**
66
+ * An array of message IDs that are waiting to be sent over the WebSocket connection.
67
+ * This builds up if the WebSocket is down for a period of time.
68
+ */
69
+ sendQueue;
70
+ /**
71
+ * The buffer of messages that have been sent but not yet acknowledged.
72
+ */
73
+ sendBuffer;
74
+ /**
75
+ * The map of {@link Connection}s managed by this transport.
76
+ */
77
+ connections;
78
+ /**
79
+ * The event dispatcher for handling events of type EventTypes.
80
+ */
81
+ eventDispatcher;
82
+ /**
83
+ * Creates a new Transport instance.
84
+ * @param codec The codec used to encode and decode messages.
85
+ * @param clientId The client ID of this transport.
86
+ */
87
+ constructor(codec, clientId) {
88
+ this.eventDispatcher = new EventDispatcher();
89
+ this.sendBuffer = /* @__PURE__ */ new Map();
90
+ this.sendQueue = /* @__PURE__ */ new Map();
91
+ this.connections = /* @__PURE__ */ new Map();
92
+ this.codec = codec;
93
+ this.clientId = clientId;
94
+ this.state = "open";
95
+ }
96
+ /**
97
+ * The downstream implementation needs to call this when a new connection is established.
98
+ * @param conn The connection object.
99
+ */
100
+ onConnect(conn) {
101
+ log?.info(`${this.clientId} -- new connection to ${conn.connectedTo}`);
102
+ this.connections.set(conn.connectedTo, conn);
103
+ this.eventDispatcher.dispatchEvent("connectionStatus", {
104
+ status: "connect",
105
+ conn
106
+ });
107
+ const outstanding = this.sendQueue.get(conn.connectedTo);
108
+ if (!outstanding) {
109
+ return;
110
+ }
111
+ for (const id of outstanding) {
112
+ const msg = this.sendBuffer.get(id);
113
+ if (!msg) {
114
+ log?.warn(
115
+ `${this.clientId} -- tried to resend a message we received an ack for`
116
+ );
117
+ continue;
118
+ }
119
+ this.send(msg);
120
+ }
121
+ this.sendQueue.delete(conn.connectedTo);
122
+ }
123
+ /**
124
+ * The downstream implementation needs to call this when a connection is closed.
125
+ * @param conn The connection object.
126
+ */
127
+ onDisconnect(conn) {
128
+ log?.info(`${this.clientId} -- disconnect from ${conn.connectedTo}`);
129
+ conn.close();
130
+ this.connections.delete(conn.connectedTo);
131
+ this.eventDispatcher.dispatchEvent("connectionStatus", {
132
+ status: "disconnect",
133
+ conn
134
+ });
135
+ }
136
+ /**
137
+ * Handles a message received by this transport. Thin wrapper around {@link handleMsg} and {@link parseMsg}.
138
+ * @param msg The message to handle.
139
+ */
140
+ onMessage(msg) {
141
+ return this.handleMsg(this.parseMsg(msg));
142
+ }
143
+ /**
144
+ * Parses a message from a Uint8Array into a {@link OpaqueTransportMessage}.
145
+ * @param msg The message to parse.
146
+ * @returns The parsed message, or null if the message is malformed or invalid.
147
+ */
148
+ parseMsg(msg) {
149
+ const parsedMsg = this.codec.fromBuffer(msg);
150
+ if (parsedMsg === null) {
151
+ const decodedBuffer = new TextDecoder().decode(msg);
152
+ log?.warn(`${this.clientId} -- received malformed msg: ${decodedBuffer}`);
153
+ return null;
154
+ }
155
+ if (Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
156
+ return {
157
+ ...parsedMsg,
158
+ serviceName: parsedMsg.serviceName === null ? void 0 : parsedMsg.serviceName,
159
+ procedureName: parsedMsg.procedureName === null ? void 0 : parsedMsg.procedureName
160
+ };
161
+ } else {
162
+ log?.warn(
163
+ `${this.clientId} -- received invalid msg: ${JSON.stringify(
164
+ parsedMsg
165
+ )}`
166
+ );
167
+ return null;
168
+ }
169
+ }
170
+ /**
171
+ * Called when a message is received by this transport.
172
+ * You generally shouldn't need to override this in downstream transport implementations.
173
+ * @param msg The received message.
174
+ */
175
+ handleMsg(msg) {
176
+ if (!msg) {
177
+ return;
178
+ }
179
+ if (isAck(msg.controlFlags) && Value.Check(TransportAckSchema, msg)) {
180
+ log?.info(`${this.clientId} -- received ack: ${JSON.stringify(msg)}`);
181
+ if (this.sendBuffer.has(msg.payload.ack)) {
182
+ this.sendBuffer.delete(msg.payload.ack);
183
+ }
184
+ } else {
185
+ log?.info(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
186
+ if (msg.to !== this.clientId) {
187
+ return;
188
+ }
189
+ this.eventDispatcher.dispatchEvent("message", msg);
190
+ if (!isAck(msg.controlFlags)) {
191
+ const ackMsg = reply(msg, { ack: msg.id });
192
+ ackMsg.controlFlags = 1 /* AckBit */;
193
+ ackMsg.from = this.clientId;
194
+ this.send(ackMsg);
195
+ }
196
+ }
197
+ }
198
+ /**
199
+ * Adds a listener to this transport.
200
+ * @param the type of event to listen for
201
+ * @param handler The message handler to add.
202
+ */
203
+ addEventListener(type, handler) {
204
+ this.eventDispatcher.addEventListener(type, handler);
205
+ }
206
+ /**
207
+ * Removes a listener from this transport.
208
+ * @param the type of event to unlisten on
209
+ * @param handler The message handler to remove.
210
+ */
211
+ removeEventListener(type, handler) {
212
+ this.eventDispatcher.removeEventListener(type, handler);
213
+ }
214
+ /**
215
+ * Sends a message over this transport, delegating to the appropriate connection to actually
216
+ * send the message.
217
+ * @param msg The message to send.
218
+ * @returns The ID of the sent message.
219
+ */
220
+ send(msg) {
221
+ if (this.state === "destroyed") {
222
+ const err = "transport is destroyed, cant send";
223
+ log?.error(`${this.clientId} -- ` + err + `: ${JSON.stringify(msg)}`);
224
+ throw new Error(err);
225
+ } else if (this.state === "closed") {
226
+ log?.info(
227
+ `${this.clientId} -- transport closed when sending, discarding : ${JSON.stringify(
228
+ msg
229
+ )}`
230
+ );
231
+ return msg.id;
232
+ }
233
+ let conn = this.connections.get(msg.to);
234
+ if (!isAck(msg.controlFlags)) {
235
+ this.sendBuffer.set(msg.id, msg);
236
+ }
237
+ if (conn) {
238
+ log?.info(`${this.clientId} -- sending ${JSON.stringify(msg)}`);
239
+ const ok = conn.send(this.codec.toBuffer(msg));
240
+ if (ok) {
241
+ return msg.id;
242
+ }
243
+ }
244
+ log?.info(
245
+ `${this.clientId} -- connection to ${msg.to} not ready, attempting reconnect and queuing ${JSON.stringify(msg)}`
246
+ );
247
+ const outstanding = this.sendQueue.get(msg.to) || [];
248
+ outstanding.push(msg.id);
249
+ this.sendQueue.set(msg.to, outstanding);
250
+ this.createNewConnection(msg.to);
251
+ return msg.id;
252
+ }
253
+ /**
254
+ * Default close implementation for transports. You should override this in the downstream
255
+ * implementation if you need to do any additional cleanup and call super.close() at the end.
256
+ * Closes the transport. Any messages sent while the transport is closed will be silently discarded.
257
+ */
258
+ async close() {
259
+ for (const conn of this.connections.values()) {
260
+ conn.close();
261
+ }
262
+ this.connections.clear();
263
+ this.state = "closed";
264
+ log?.info(`${this.clientId} -- closed transport`);
265
+ }
266
+ /**
267
+ * Default destroy implementation for transports. You should override this in the downstream
268
+ * implementation if you need to do any additional cleanup and call super.destroy() at the end.
269
+ * Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
270
+ */
271
+ async destroy() {
272
+ for (const conn of this.connections.values()) {
273
+ conn.close();
274
+ }
275
+ this.connections.clear();
276
+ this.state = "destroyed";
277
+ log?.info(`${this.clientId} -- destroyed transport`);
278
+ }
279
+ };
280
+
281
+ export {
282
+ Connection,
283
+ Transport
284
+ };