@replit/river 0.200.0-rc.2 → 0.200.0-rc.20

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 (88) hide show
  1. package/README.md +30 -29
  2. package/dist/chunk-3HI3IJTL.js +285 -0
  3. package/dist/chunk-3HI3IJTL.js.map +1 -0
  4. package/dist/chunk-5L5RNZXH.js +391 -0
  5. package/dist/chunk-5L5RNZXH.js.map +1 -0
  6. package/dist/{chunk-QMM35C3H.js → chunk-BAGOAJ3K.js} +1 -1
  7. package/dist/chunk-BAGOAJ3K.js.map +1 -0
  8. package/dist/{chunk-S5RL45KH.js → chunk-BYCR4VEM.js} +78 -54
  9. package/dist/chunk-BYCR4VEM.js.map +1 -0
  10. package/dist/chunk-DM5QR4HQ.js +60 -0
  11. package/dist/chunk-DM5QR4HQ.js.map +1 -0
  12. package/dist/chunk-OLWVR5AB.js +860 -0
  13. package/dist/chunk-OLWVR5AB.js.map +1 -0
  14. package/dist/chunk-WKBWCRGN.js +437 -0
  15. package/dist/chunk-WKBWCRGN.js.map +1 -0
  16. package/dist/chunk-YBCQVIPR.js +351 -0
  17. package/dist/chunk-YBCQVIPR.js.map +1 -0
  18. package/dist/client-75090f07.d.ts +49 -0
  19. package/dist/connection-c9f96b64.d.ts +32 -0
  20. package/dist/context-9c907028.d.ts +622 -0
  21. package/dist/logging/index.cjs.map +1 -1
  22. package/dist/logging/index.d.cts +1 -1
  23. package/dist/logging/index.d.ts +1 -1
  24. package/dist/logging/index.js +1 -1
  25. package/dist/{index-10ebd26a.d.ts → message-59fe53e1.d.ts} +34 -31
  26. package/dist/router/index.cjs +771 -1159
  27. package/dist/router/index.cjs.map +1 -1
  28. package/dist/router/index.d.cts +14 -48
  29. package/dist/router/index.d.ts +14 -48
  30. package/dist/router/index.js +1238 -15
  31. package/dist/router/index.js.map +1 -1
  32. package/dist/server-109a29e2.d.ts +69 -0
  33. package/dist/services-aa49a9fb.d.ts +811 -0
  34. package/dist/transport/impls/ws/client.cjs +1293 -1034
  35. package/dist/transport/impls/ws/client.cjs.map +1 -1
  36. package/dist/transport/impls/ws/client.d.cts +7 -5
  37. package/dist/transport/impls/ws/client.d.ts +7 -5
  38. package/dist/transport/impls/ws/client.js +11 -11
  39. package/dist/transport/impls/ws/client.js.map +1 -1
  40. package/dist/transport/impls/ws/server.cjs +1437 -1072
  41. package/dist/transport/impls/ws/server.cjs.map +1 -1
  42. package/dist/transport/impls/ws/server.d.cts +7 -5
  43. package/dist/transport/impls/ws/server.d.ts +7 -5
  44. package/dist/transport/impls/ws/server.js +20 -8
  45. package/dist/transport/impls/ws/server.js.map +1 -1
  46. package/dist/transport/index.cjs +1720 -1400
  47. package/dist/transport/index.cjs.map +1 -1
  48. package/dist/transport/index.d.cts +5 -26
  49. package/dist/transport/index.d.ts +5 -26
  50. package/dist/transport/index.js +11 -11
  51. package/dist/util/testHelpers.cjs +1164 -591
  52. package/dist/util/testHelpers.cjs.map +1 -1
  53. package/dist/util/testHelpers.d.cts +41 -38
  54. package/dist/util/testHelpers.d.ts +41 -38
  55. package/dist/util/testHelpers.js +124 -89
  56. package/dist/util/testHelpers.js.map +1 -1
  57. package/package.json +3 -3
  58. package/dist/chunk-47TFNAY2.js +0 -476
  59. package/dist/chunk-47TFNAY2.js.map +0 -1
  60. package/dist/chunk-4VNY34QG.js +0 -106
  61. package/dist/chunk-4VNY34QG.js.map +0 -1
  62. package/dist/chunk-7CKIN3JT.js +0 -2004
  63. package/dist/chunk-7CKIN3JT.js.map +0 -1
  64. package/dist/chunk-CZP4LK3F.js +0 -335
  65. package/dist/chunk-CZP4LK3F.js.map +0 -1
  66. package/dist/chunk-DJCW3SKT.js +0 -59
  67. package/dist/chunk-DJCW3SKT.js.map +0 -1
  68. package/dist/chunk-NQWDT6GS.js +0 -347
  69. package/dist/chunk-NQWDT6GS.js.map +0 -1
  70. package/dist/chunk-ONUXWVRC.js +0 -492
  71. package/dist/chunk-ONUXWVRC.js.map +0 -1
  72. package/dist/chunk-QMM35C3H.js.map +0 -1
  73. package/dist/chunk-S5RL45KH.js.map +0 -1
  74. package/dist/connection-3f117047.d.ts +0 -17
  75. package/dist/connection-f900e390.d.ts +0 -35
  76. package/dist/services-970f97bb.d.ts +0 -1372
  77. package/dist/transport/impls/uds/client.cjs +0 -1687
  78. package/dist/transport/impls/uds/client.cjs.map +0 -1
  79. package/dist/transport/impls/uds/client.d.cts +0 -17
  80. package/dist/transport/impls/uds/client.d.ts +0 -17
  81. package/dist/transport/impls/uds/client.js +0 -44
  82. package/dist/transport/impls/uds/client.js.map +0 -1
  83. package/dist/transport/impls/uds/server.cjs +0 -1522
  84. package/dist/transport/impls/uds/server.cjs.map +0 -1
  85. package/dist/transport/impls/uds/server.d.cts +0 -19
  86. package/dist/transport/impls/uds/server.d.ts +0 -19
  87. package/dist/transport/impls/uds/server.js +0 -33
  88. package/dist/transport/impls/uds/server.js.map +0 -1
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # River
2
2
 
3
- ⚠️ Not production ready, while Replit is using parts of river in production, we are still going through rapid breaking changes. First production ready version will be 1.x.x ⚠️
3
+ ⚠️ Not production ready, while Replit is using parts of River in production, we are still going through rapid breaking changes. First production ready version will be `1.x.x` ⚠️
4
4
 
5
5
  River allows multiple clients to connect to and make remote procedure calls to a remote server as if they were local procedures.
6
6
 
@@ -65,11 +65,11 @@ Before proceeding, ensure you have TypeScript 5 installed and configured appropr
65
65
 
66
66
  - Router: a collection of services, namespaced by service name.
67
67
  - Service: a collection of procedures with a shared state.
68
- - Procedure: a single procedure. A procedure declares its type, an input message type, an output message type, optionally an error type, and the associated handler. Valid types are:
69
- - `rpc` whose handler has a signature of `Input -> Result<Output, Error>`.
70
- - `upload` whose handler has a signature of `AsyncIterableIterator<Input> -> Result<Output, Error>`.
71
- - `subscription` whose handler has a signature of `Input -> Pushable<Result<Output, Error>>`.
72
- - `stream` whose handler has a signature of `AsyncIterableIterator<Input> -> Pushable<Result<Output, Error>>`.
68
+ - Procedure: a single procedure. A procedure declares its type, a request data type, a response data type, optionally a response error type, and the associated handler. Valid types are:
69
+ - `rpc`, single request, single response
70
+ - `upload`, multiple requests, single response
71
+ - `subscription`, single request, multiple responses
72
+ - `stream`, multiple requests, multiple response
73
73
  - Transport: manages the lifecycle (creation/deletion) of connections and multiplexing read/writes from clients. Both the client and the server must be passed in a subclass of `Transport` to work.
74
74
  - Connection: the actual raw underlying transport connection
75
75
  - Session: a higher-level abstraction that operates over the span of potentially multiple transport-level connections
@@ -80,7 +80,7 @@ Before proceeding, ensure you have TypeScript 5 installed and configured appropr
80
80
  First, we create a service using `ServiceSchema`:
81
81
 
82
82
  ```ts
83
- import { ServicaSchema, Procedure, Ok } from '@replit/river';
83
+ import { ServiceSchema, Procedure, Ok } from '@replit/river';
84
84
  import { Type } from '@sinclair/typebox';
85
85
 
86
86
  export const ExampleService = ServiceSchema.define(
@@ -92,11 +92,11 @@ export const ExampleService = ServiceSchema.define(
92
92
  // procedures
93
93
  {
94
94
  add: Procedure.rpc({
95
- input: Type.Object({ n: Type.Number() }),
96
- output: Type.Object({ result: Type.Number() }),
97
- errors: Type.Never(),
95
+ requestInit: Type.Object({ n: Type.Number() }),
96
+ responseData: Type.Object({ result: Type.Number() }),
97
+ requestErrors: Type.Never(),
98
98
  // note that a handler is unique per user RPC
99
- async handler(ctx, { n }) {
99
+ async handler({ ctx, reqInit: { n } }) {
100
100
  // access and mutate shared state
101
101
  ctx.state.count += n;
102
102
  return Ok({ result: ctx.state.count });
@@ -134,17 +134,17 @@ In another file for the client (to create a separate entrypoint),
134
134
  ```ts
135
135
  import { WebSocketClientTransport } from '@replit/river/transport/ws/client';
136
136
  import { createClient } from '@replit/river';
137
- import type ServiceSurface from './server';
137
+ import { WebSocket } from 'ws';
138
138
 
139
139
  const transport = new WebSocketClientTransport(
140
140
  async () => new WebSocket('ws://localhost:3000'),
141
141
  'my-client-id',
142
142
  );
143
143
 
144
- const client = createClient<ServiceSurface>(
144
+ const client = createClient(
145
145
  transport,
146
146
  'SERVER', // transport id of the server in the previous step
147
- true, // whether to eagerly connect to the server on creation (optional argument)
147
+ { eagerlyConnect: true }, // whether to eagerly connect to the server on creation (optional argument)
148
148
  );
149
149
 
150
150
  // we get full type safety on `client`
@@ -157,15 +157,6 @@ if (result.ok) {
157
157
  }
158
158
  ```
159
159
 
160
- You can then access the `ParsedMetadata` in your procedure handlers:
161
-
162
- ```ts
163
- async handler(ctx, ...args) {
164
- // this contains the parsed metadata
165
- console.log(ctx.metadata)
166
- }
167
- ```
168
-
169
160
  ### Logging
170
161
 
171
162
  To add logging, you can bind a logging function to a transport.
@@ -192,12 +183,12 @@ River defines two types of reconnects:
192
183
  1. **Transparent reconnects:** These occur when the connection is temporarily lost and reestablished without losing any messages. From the application's perspective, this process is seamless and does not disrupt ongoing operations.
193
184
  2. **Hard reconnect:** This occurs when all server state is lost, requiring the client to reinitialize anything stateful (e.g. subscriptions).
194
185
 
195
- You can listen for transparent reconnects via the `connectionStatus` events, but realistically, no applications should need to listen for this unless it is for debugging purposes. Hard reconnects are signaled via `sessionStatus` events.
186
+ Hard reconnects are signaled via `sessionStatus` events.
196
187
 
197
188
  If your application is stateful on either the server or the client, the service consumer _should_ wrap all the client-side setup with `transport.addEventListener('sessionStatus', (evt) => ...)` to do appropriate setup and teardown.
198
189
 
199
190
  ```ts
200
- transport.addEventListener('connectionStatus', (evt) => {
191
+ transport.addEventListener('sessionStatus', (evt) => {
201
192
  if (evt.status === 'connect') {
202
193
  // do something
203
194
  } else if (evt.status === 'disconnect') {
@@ -205,11 +196,12 @@ transport.addEventListener('connectionStatus', (evt) => {
205
196
  }
206
197
  });
207
198
 
208
- transport.addEventListener('sessionStatus', (evt) => {
209
- if (evt.status === 'connect') {
199
+ // or, listen for specific session states
200
+ transport.addEventListener('sessionTransition', (evt) => {
201
+ if (evt.state === SessionState.Connected) {
202
+ // switch on various transition states
203
+ } else if (evt.state === SessionState.NoConnection) {
210
204
  // do something
211
- } else if (evt.status === 'disconnect') {
212
- // do something else
213
205
  }
214
206
  });
215
207
  ```
@@ -253,6 +245,15 @@ createServer(new MockServerTransport('SERVER'), services, {
253
245
  });
254
246
  ```
255
247
 
248
+ You can then access the `ParsedMetadata` in your procedure handlers:
249
+
250
+ ```ts
251
+ async handler(ctx, ...args) {
252
+ // this contains the parsed metadata
253
+ console.log(ctx.metadata)
254
+ }
255
+ ```
256
+
256
257
  ### Further examples
257
258
 
258
259
  We've also provided an end-to-end testing environment using `Next.js`, and a simple backend connected with the WebSocket transport that you can [play with on Replit](https://replit.com/@jzhao-replit/riverbed).
@@ -0,0 +1,285 @@
1
+ import {
2
+ BaseLogger,
3
+ createLogProxy
4
+ } from "./chunk-BAGOAJ3K.js";
5
+ import {
6
+ SessionStateGraph,
7
+ defaultTransportOptions
8
+ } from "./chunk-OLWVR5AB.js";
9
+ import {
10
+ generateId
11
+ } from "./chunk-BYCR4VEM.js";
12
+
13
+ // transport/events.ts
14
+ var ProtocolError = {
15
+ RetriesExceeded: "conn_retry_exceeded",
16
+ HandshakeFailed: "handshake_failed",
17
+ MessageOrderingViolated: "message_ordering_violated",
18
+ InvalidMessage: "invalid_message"
19
+ };
20
+ var EventDispatcher = class {
21
+ eventListeners = {};
22
+ removeAllListeners() {
23
+ this.eventListeners = {};
24
+ }
25
+ numberOfListeners(eventType) {
26
+ return this.eventListeners[eventType]?.size ?? 0;
27
+ }
28
+ addEventListener(eventType, handler) {
29
+ if (!this.eventListeners[eventType]) {
30
+ this.eventListeners[eventType] = /* @__PURE__ */ new Set();
31
+ }
32
+ this.eventListeners[eventType]?.add(handler);
33
+ }
34
+ removeEventListener(eventType, handler) {
35
+ const handlers = this.eventListeners[eventType];
36
+ if (handlers) {
37
+ this.eventListeners[eventType]?.delete(handler);
38
+ }
39
+ }
40
+ dispatchEvent(eventType, event) {
41
+ const handlers = this.eventListeners[eventType];
42
+ if (handlers) {
43
+ const copy = [...handlers];
44
+ for (const handler of copy) {
45
+ handler(event);
46
+ }
47
+ }
48
+ }
49
+ };
50
+
51
+ // transport/transport.ts
52
+ var Transport = class {
53
+ /**
54
+ * The status of the transport.
55
+ */
56
+ status;
57
+ /**
58
+ * The client ID of this transport.
59
+ */
60
+ clientId;
61
+ /**
62
+ * The event dispatcher for handling events of type EventTypes.
63
+ */
64
+ eventDispatcher;
65
+ /**
66
+ * The options for this transport.
67
+ */
68
+ options;
69
+ log;
70
+ sessions;
71
+ /**
72
+ * Creates a new Transport instance.
73
+ * @param codec The codec used to encode and decode messages.
74
+ * @param clientId The client ID of this transport.
75
+ */
76
+ constructor(clientId, providedOptions) {
77
+ this.options = { ...defaultTransportOptions, ...providedOptions };
78
+ this.eventDispatcher = new EventDispatcher();
79
+ this.clientId = clientId;
80
+ this.status = "open";
81
+ this.sessions = /* @__PURE__ */ new Map();
82
+ }
83
+ bindLogger(fn, level) {
84
+ if (typeof fn === "function") {
85
+ this.log = createLogProxy(new BaseLogger(fn, level));
86
+ return;
87
+ }
88
+ this.log = createLogProxy(fn);
89
+ }
90
+ /**
91
+ * Called when a message is received by this transport.
92
+ * You generally shouldn't need to override this in downstream transport implementations.
93
+ * @param msg The received message.
94
+ */
95
+ handleMsg(msg) {
96
+ if (this.getStatus() !== "open")
97
+ return;
98
+ this.eventDispatcher.dispatchEvent("message", msg);
99
+ }
100
+ /**
101
+ * Adds a listener to this transport.
102
+ * @param the type of event to listen for
103
+ * @param handler The message handler to add.
104
+ */
105
+ addEventListener(type, handler) {
106
+ this.eventDispatcher.addEventListener(type, handler);
107
+ }
108
+ /**
109
+ * Removes a listener from this transport.
110
+ * @param the type of event to un-listen on
111
+ * @param handler The message handler to remove.
112
+ */
113
+ removeEventListener(type, handler) {
114
+ this.eventDispatcher.removeEventListener(type, handler);
115
+ }
116
+ protocolError(message) {
117
+ this.eventDispatcher.dispatchEvent("protocolError", message);
118
+ }
119
+ /**
120
+ * Default close implementation for transports. You should override this in the downstream
121
+ * implementation if you need to do any additional cleanup and call super.close() at the end.
122
+ * Closes the transport. Any messages sent while the transport is closed will be silently discarded.
123
+ */
124
+ close() {
125
+ this.status = "closed";
126
+ for (const session of this.sessions.values()) {
127
+ this.deleteSession(session);
128
+ }
129
+ this.eventDispatcher.dispatchEvent("transportStatus", {
130
+ status: this.status
131
+ });
132
+ this.eventDispatcher.removeAllListeners();
133
+ this.log?.info(`manually closed transport`, { clientId: this.clientId });
134
+ }
135
+ getStatus() {
136
+ return this.status;
137
+ }
138
+ updateSession(session) {
139
+ const activeSession = this.sessions.get(session.to);
140
+ if (activeSession && activeSession.id !== session.id) {
141
+ const msg = `attempt to transition active session for ${session.to} but active session (${activeSession.id}) is different from handle (${session.id})`;
142
+ throw new Error(msg);
143
+ }
144
+ this.sessions.set(session.to, session);
145
+ if (!activeSession) {
146
+ this.eventDispatcher.dispatchEvent("sessionStatus", {
147
+ status: "connect",
148
+ session
149
+ });
150
+ }
151
+ this.eventDispatcher.dispatchEvent("sessionTransition", {
152
+ state: session.state,
153
+ session
154
+ });
155
+ return session;
156
+ }
157
+ // state transitions
158
+ deleteSession(session, options) {
159
+ if (session._isConsumed)
160
+ return;
161
+ const loggingMetadata = session.loggingMetadata;
162
+ if (loggingMetadata.tags && options?.unhealthy) {
163
+ loggingMetadata.tags.push("unhealthy-session");
164
+ }
165
+ session.log?.info(`closing session ${session.id}`, loggingMetadata);
166
+ this.eventDispatcher.dispatchEvent("sessionStatus", {
167
+ status: "disconnect",
168
+ session
169
+ });
170
+ const to = session.to;
171
+ session.close();
172
+ this.sessions.delete(to);
173
+ }
174
+ // common listeners
175
+ onSessionGracePeriodElapsed(session) {
176
+ this.log?.warn(
177
+ `session to ${session.to} grace period elapsed, closing`,
178
+ session.loggingMetadata
179
+ );
180
+ this.deleteSession(session);
181
+ }
182
+ onConnectingFailed(session) {
183
+ const noConnectionSession = SessionStateGraph.transition.ConnectingToNoConnection(session, {
184
+ onSessionGracePeriodElapsed: () => {
185
+ this.onSessionGracePeriodElapsed(noConnectionSession);
186
+ }
187
+ });
188
+ return this.updateSession(noConnectionSession);
189
+ }
190
+ onConnClosed(session) {
191
+ let noConnectionSession;
192
+ if (session.state === "Handshaking" /* Handshaking */) {
193
+ noConnectionSession = SessionStateGraph.transition.HandshakingToNoConnection(session, {
194
+ onSessionGracePeriodElapsed: () => {
195
+ this.onSessionGracePeriodElapsed(noConnectionSession);
196
+ }
197
+ });
198
+ } else {
199
+ noConnectionSession = SessionStateGraph.transition.ConnectedToNoConnection(session, {
200
+ onSessionGracePeriodElapsed: () => {
201
+ this.onSessionGracePeriodElapsed(noConnectionSession);
202
+ }
203
+ });
204
+ }
205
+ return this.updateSession(noConnectionSession);
206
+ }
207
+ };
208
+
209
+ // transport/connection.ts
210
+ var Connection = class {
211
+ id;
212
+ telemetry;
213
+ constructor() {
214
+ this.id = `conn-${generateId()}`;
215
+ }
216
+ get loggingMetadata() {
217
+ const metadata = { connId: this.id };
218
+ const spanContext = this.telemetry?.span.spanContext();
219
+ if (this.telemetry?.span.isRecording() && spanContext) {
220
+ metadata.telemetry = {
221
+ traceId: spanContext.traceId,
222
+ spanId: spanContext.spanId
223
+ };
224
+ }
225
+ return metadata;
226
+ }
227
+ // can't use event emitter because we need this to work in both node + browser
228
+ _dataListeners = /* @__PURE__ */ new Set();
229
+ _closeListeners = /* @__PURE__ */ new Set();
230
+ _errorListeners = /* @__PURE__ */ new Set();
231
+ get dataListeners() {
232
+ return [...this._dataListeners];
233
+ }
234
+ get closeListeners() {
235
+ return [...this._closeListeners];
236
+ }
237
+ get errorListeners() {
238
+ return [...this._errorListeners];
239
+ }
240
+ /**
241
+ * Handle adding a callback for when a message is received.
242
+ * @param msg The message that was received.
243
+ */
244
+ addDataListener(cb) {
245
+ this._dataListeners.add(cb);
246
+ }
247
+ removeDataListener(cb) {
248
+ this._dataListeners.delete(cb);
249
+ }
250
+ /**
251
+ * Handle adding a callback for when the connection is closed.
252
+ * This should also be called if an error happens and after notifying all the error listeners.
253
+ * @param cb The callback to call when the connection is closed.
254
+ */
255
+ addCloseListener(cb) {
256
+ this._closeListeners.add(cb);
257
+ }
258
+ removeCloseListener(cb) {
259
+ this._closeListeners.delete(cb);
260
+ }
261
+ /**
262
+ * Handle adding a callback for when an error is received.
263
+ * This should only be used for this.logging errors, all cleanup
264
+ * should be delegated to addCloseListener.
265
+ *
266
+ * The implementer should take care such that the implemented
267
+ * connection will call both the close and error callbacks
268
+ * on an error.
269
+ *
270
+ * @param cb The callback to call when an error is received.
271
+ */
272
+ addErrorListener(cb) {
273
+ this._errorListeners.add(cb);
274
+ }
275
+ removeErrorListener(cb) {
276
+ this._errorListeners.delete(cb);
277
+ }
278
+ };
279
+
280
+ export {
281
+ ProtocolError,
282
+ Transport,
283
+ Connection
284
+ };
285
+ //# sourceMappingURL=chunk-3HI3IJTL.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../transport/events.ts","../transport/transport.ts","../transport/connection.ts"],"sourcesContent":["import { type Static } from '@sinclair/typebox';\nimport { Connection } from './connection';\nimport { OpaqueTransportMessage, HandshakeErrorResponseCodes } from './message';\nimport { Session, SessionState } from './sessionStateMachine';\nimport { TransportStatus } from './transport';\n\nexport const ProtocolError = {\n RetriesExceeded: 'conn_retry_exceeded',\n HandshakeFailed: 'handshake_failed',\n MessageOrderingViolated: 'message_ordering_violated',\n InvalidMessage: 'invalid_message',\n} as const;\n\nexport type ProtocolErrorType =\n (typeof ProtocolError)[keyof typeof ProtocolError];\n\nexport interface EventMap {\n message: OpaqueTransportMessage;\n sessionStatus: {\n status: 'connect' | 'disconnect';\n session: Session<Connection>;\n };\n sessionTransition:\n | { state: SessionState.Connected }\n | { state: SessionState.Handshaking }\n | { state: SessionState.Connecting }\n | { state: SessionState.BackingOff }\n | { state: SessionState.NoConnection };\n protocolError:\n | {\n type: (typeof ProtocolError)['HandshakeFailed'];\n code: Static<typeof HandshakeErrorResponseCodes>;\n message: string;\n }\n | {\n type: Omit<\n ProtocolErrorType,\n (typeof ProtocolError)['HandshakeFailed']\n >;\n message: string;\n };\n transportStatus: {\n status: TransportStatus;\n };\n}\n\nexport type EventTypes = keyof EventMap;\nexport type EventHandler<K extends EventTypes> = (\n event: EventMap[K],\n) => unknown;\n\nexport class EventDispatcher<T extends EventTypes> {\n private eventListeners: { [K in T]?: Set<EventHandler<K>> } = {};\n\n removeAllListeners() {\n this.eventListeners = {};\n }\n\n numberOfListeners<K extends T>(eventType: K) {\n return this.eventListeners[eventType]?.size ?? 0;\n }\n\n addEventListener<K extends T>(eventType: K, handler: EventHandler<K>) {\n if (!this.eventListeners[eventType]) {\n this.eventListeners[eventType] = new Set();\n }\n\n this.eventListeners[eventType]?.add(handler);\n }\n\n removeEventListener<K extends T>(eventType: K, handler: EventHandler<K>) {\n const handlers = this.eventListeners[eventType];\n if (handlers) {\n this.eventListeners[eventType]?.delete(handler);\n }\n }\n\n dispatchEvent<K extends T>(eventType: K, event: EventMap[K]) {\n const handlers = this.eventListeners[eventType];\n if (handlers) {\n // copying ensures that adding more listeners in a handler doesn't\n // affect the current dispatch.\n const copy = [...handlers];\n for (const handler of copy) {\n handler(event);\n }\n }\n }\n}\n","import {\n OpaqueTransportMessage,\n TransportClientId,\n PartialTransportMessage,\n} from './message';\nimport {\n BaseLogger,\n LogFn,\n Logger,\n LoggingLevel,\n createLogProxy,\n} from '../logging/log';\nimport { EventDispatcher, EventHandler, EventMap, EventTypes } from './events';\nimport {\n ProvidedTransportOptions,\n TransportOptions,\n defaultTransportOptions,\n} from './options';\nimport {\n SessionConnected,\n SessionConnecting,\n SessionHandshaking,\n SessionNoConnection,\n SessionState,\n} from './sessionStateMachine';\nimport { Connection } from './connection';\nimport { Session, SessionStateGraph } from './sessionStateMachine/transitions';\n\n/**\n * Represents the possible states of a transport.\n * @property {'open'} open - The transport is open and operational (note that this doesn't mean it is actively connected)\n * @property {'closed'} closed - The transport is permanently closed and cannot be reopened.\n */\nexport type TransportStatus = 'open' | 'closed';\n\nexport interface DeleteSessionOptions {\n unhealthy: boolean;\n}\n\n/**\n * Transports manage the lifecycle (creation/deletion) of sessions\n *\n * ```plaintext\n * ▲\n * incoming │\n * messages │\n * ▼\n * ┌─────────────┐ 1:N ┌───────────┐ 1:1* ┌────────────┐\n * │ Transport │ ◄─────► │ Session │ ◄─────► │ Connection │\n * └─────────────┘ └───────────┘ └────────────┘\n * ▲ * (may or may not be initialized yet)\n * │\n * ▼\n * ┌───────────┐\n * │ Message │\n * │ Listeners │\n * └───────────┘\n * ```\n * @abstract\n */\nexport abstract class Transport<ConnType extends Connection> {\n /**\n * The status of the transport.\n */\n private status: TransportStatus;\n\n /**\n * The client ID of this transport.\n */\n clientId: TransportClientId;\n\n /**\n * The event dispatcher for handling events of type EventTypes.\n */\n eventDispatcher: EventDispatcher<EventTypes>;\n\n /**\n * The options for this transport.\n */\n protected options: TransportOptions;\n log?: Logger;\n\n sessions: Map<TransportClientId, Session<ConnType>>;\n\n /**\n * Creates a new Transport instance.\n * @param codec The codec used to encode and decode messages.\n * @param clientId The client ID of this transport.\n */\n constructor(\n clientId: TransportClientId,\n providedOptions?: ProvidedTransportOptions,\n ) {\n this.options = { ...defaultTransportOptions, ...providedOptions };\n this.eventDispatcher = new EventDispatcher();\n this.clientId = clientId;\n this.status = 'open';\n this.sessions = new Map();\n }\n\n bindLogger(fn: LogFn | Logger, level?: LoggingLevel) {\n // construct logger from fn\n if (typeof fn === 'function') {\n this.log = createLogProxy(new BaseLogger(fn, level));\n return;\n }\n\n // object case, just assign\n this.log = createLogProxy(fn);\n }\n\n /**\n * Called when a message is received by this transport.\n * You generally shouldn't need to override this in downstream transport implementations.\n * @param msg The received message.\n */\n protected handleMsg(msg: OpaqueTransportMessage) {\n if (this.getStatus() !== 'open') return;\n this.eventDispatcher.dispatchEvent('message', msg);\n }\n\n /**\n * Adds a listener to this transport.\n * @param the type of event to listen for\n * @param handler The message handler to add.\n */\n addEventListener<K extends EventTypes, T extends EventHandler<K>>(\n type: K,\n handler: T,\n ): void {\n this.eventDispatcher.addEventListener(type, handler);\n }\n\n /**\n * Removes a listener from this transport.\n * @param the type of event to un-listen on\n * @param handler The message handler to remove.\n */\n removeEventListener<K extends EventTypes, T extends EventHandler<K>>(\n type: K,\n handler: T,\n ): void {\n this.eventDispatcher.removeEventListener(type, handler);\n }\n\n /**\n * Sends a message over this transport, delegating to the appropriate connection to actually\n * send the message.\n * @param msg The message to send.\n * @returns The ID of the sent message or undefined if it wasn't sent\n */\n abstract send(to: TransportClientId, msg: PartialTransportMessage): string;\n\n protected protocolError(message: EventMap['protocolError']) {\n this.eventDispatcher.dispatchEvent('protocolError', message);\n }\n\n /**\n * Default close implementation for transports. You should override this in the downstream\n * implementation if you need to do any additional cleanup and call super.close() at the end.\n * Closes the transport. Any messages sent while the transport is closed will be silently discarded.\n */\n close() {\n this.status = 'closed';\n\n for (const session of this.sessions.values()) {\n this.deleteSession(session);\n }\n\n this.eventDispatcher.dispatchEvent('transportStatus', {\n status: this.status,\n });\n\n this.eventDispatcher.removeAllListeners();\n\n this.log?.info(`manually closed transport`, { clientId: this.clientId });\n }\n\n getStatus(): TransportStatus {\n return this.status;\n }\n\n protected updateSession<S extends Session<ConnType>>(session: S): S {\n const activeSession = this.sessions.get(session.to);\n if (activeSession && activeSession.id !== session.id) {\n const msg = `attempt to transition active session for ${session.to} but active session (${activeSession.id}) is different from handle (${session.id})`;\n throw new Error(msg);\n }\n\n this.sessions.set(session.to, session);\n\n if (!activeSession) {\n this.eventDispatcher.dispatchEvent('sessionStatus', {\n status: 'connect',\n session: session,\n });\n }\n\n this.eventDispatcher.dispatchEvent('sessionTransition', {\n state: session.state,\n session: session,\n } as EventMap['sessionTransition']);\n\n return session;\n }\n\n // state transitions\n protected deleteSession(\n session: Session<ConnType>,\n options?: DeleteSessionOptions,\n ) {\n // ensure idempotency esp re: dispatching events\n if (session._isConsumed) return;\n\n const loggingMetadata = session.loggingMetadata;\n if (loggingMetadata.tags && options?.unhealthy) {\n loggingMetadata.tags.push('unhealthy-session');\n }\n\n session.log?.info(`closing session ${session.id}`, loggingMetadata);\n this.eventDispatcher.dispatchEvent('sessionStatus', {\n status: 'disconnect',\n session: session,\n });\n\n const to = session.to;\n session.close();\n this.sessions.delete(to);\n }\n\n // common listeners\n protected onSessionGracePeriodElapsed(session: Session<ConnType>) {\n this.log?.warn(\n `session to ${session.to} grace period elapsed, closing`,\n session.loggingMetadata,\n );\n\n this.deleteSession(session);\n }\n\n protected onConnectingFailed(\n session: SessionConnecting<ConnType>,\n ): SessionNoConnection {\n // transition to no connection\n const noConnectionSession =\n SessionStateGraph.transition.ConnectingToNoConnection(session, {\n onSessionGracePeriodElapsed: () => {\n this.onSessionGracePeriodElapsed(noConnectionSession);\n },\n });\n\n return this.updateSession(noConnectionSession);\n }\n\n protected onConnClosed(\n session: SessionHandshaking<ConnType> | SessionConnected<ConnType>,\n ): SessionNoConnection {\n // transition to no connection\n let noConnectionSession: SessionNoConnection;\n if (session.state === SessionState.Handshaking) {\n noConnectionSession =\n SessionStateGraph.transition.HandshakingToNoConnection(session, {\n onSessionGracePeriodElapsed: () => {\n this.onSessionGracePeriodElapsed(noConnectionSession);\n },\n });\n } else {\n noConnectionSession =\n SessionStateGraph.transition.ConnectedToNoConnection(session, {\n onSessionGracePeriodElapsed: () => {\n this.onSessionGracePeriodElapsed(noConnectionSession);\n },\n });\n }\n\n return this.updateSession(noConnectionSession);\n }\n}\n","import { TelemetryInfo } from '../tracing';\nimport { MessageMetadata } from '../logging';\nimport { generateId } from './id';\n\n/**\n * A connection is the actual raw underlying transport connection.\n * It’s responsible for dispatching to/from the actual connection itself\n * This should be instantiated as soon as the client/server has a connection\n * It’s tied to the lifecycle of the underlying transport connection (i.e. if the WS drops, this connection should be deleted)\n */\nexport abstract class Connection {\n id: string;\n telemetry?: TelemetryInfo;\n\n constructor() {\n this.id = `conn-${generateId()}`; // for debugging, no collision safety needed\n }\n\n get loggingMetadata(): MessageMetadata {\n const metadata: MessageMetadata = { connId: this.id };\n const spanContext = this.telemetry?.span.spanContext();\n\n if (this.telemetry?.span.isRecording() && spanContext) {\n metadata.telemetry = {\n traceId: spanContext.traceId,\n spanId: spanContext.spanId,\n };\n }\n\n return metadata;\n }\n\n // can't use event emitter because we need this to work in both node + browser\n private _dataListeners = new Set<(msg: Uint8Array) => void>();\n private _closeListeners = new Set<() => void>();\n private _errorListeners = new Set<(err: Error) => void>();\n\n get dataListeners() {\n return [...this._dataListeners];\n }\n\n get closeListeners() {\n return [...this._closeListeners];\n }\n\n get errorListeners() {\n return [...this._errorListeners];\n }\n\n /**\n * Handle adding a callback for when a message is received.\n * @param msg The message that was received.\n */\n addDataListener(cb: (msg: Uint8Array) => void) {\n this._dataListeners.add(cb);\n }\n\n removeDataListener(cb: (msg: Uint8Array) => void): void {\n this._dataListeners.delete(cb);\n }\n\n /**\n * Handle adding a callback for when the connection is closed.\n * This should also be called if an error happens and after notifying all the error listeners.\n * @param cb The callback to call when the connection is closed.\n */\n addCloseListener(cb: () => void): void {\n this._closeListeners.add(cb);\n }\n\n removeCloseListener(cb: () => void): void {\n this._closeListeners.delete(cb);\n }\n\n /**\n * Handle adding a callback for when an error is received.\n * This should only be used for this.logging errors, all cleanup\n * should be delegated to addCloseListener.\n *\n * The implementer should take care such that the implemented\n * connection will call both the close and error callbacks\n * on an error.\n *\n * @param cb The callback to call when an error is received.\n */\n addErrorListener(cb: (err: Error) => void): void {\n this._errorListeners.add(cb);\n }\n\n removeErrorListener(cb: (err: Error) => void): void {\n this._errorListeners.delete(cb);\n }\n\n /**\n * Sends a message over the connection.\n * @param msg The message to send.\n * @returns true if the message was sent, false otherwise.\n */\n abstract send(msg: Uint8Array): boolean;\n\n /**\n * Closes the connection.\n */\n abstract close(): void;\n}\n"],"mappings":";;;;;;;;;;;;;AAMO,IAAM,gBAAgB;AAAA,EAC3B,iBAAiB;AAAA,EACjB,iBAAiB;AAAA,EACjB,yBAAyB;AAAA,EACzB,gBAAgB;AAClB;AAwCO,IAAM,kBAAN,MAA4C;AAAA,EACzC,iBAAsD,CAAC;AAAA,EAE/D,qBAAqB;AACnB,SAA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