@replit/river 0.21.1 → 0.23.0

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 (63) hide show
  1. package/README.md +1 -1
  2. package/dist/{chunk-FDLAPYCK.js → chunk-DZOATC6M.js} +2 -2
  3. package/dist/{chunk-JMXO5L2X.js → chunk-MJUFKPBT.js} +354 -398
  4. package/dist/chunk-MJUFKPBT.js.map +1 -0
  5. package/dist/{chunk-5WFL722S.js → chunk-PCKHBAVP.js} +94 -3
  6. package/dist/chunk-PCKHBAVP.js.map +1 -0
  7. package/dist/{chunk-NCXUFDVL.js → chunk-VOJVLWVX.js} +360 -516
  8. package/dist/chunk-VOJVLWVX.js.map +1 -0
  9. package/dist/chunk-ZF2UFTNN.js +60 -0
  10. package/dist/chunk-ZF2UFTNN.js.map +1 -0
  11. package/dist/{connection-76c5ed01.d.ts → connection-5685d817.d.ts} +6 -4
  12. package/dist/{connection-975b25c9.d.ts → connection-7582fb92.d.ts} +1 -1
  13. package/dist/{index-dfad460e.d.ts → index-a6fe0edd.d.ts} +55 -59
  14. package/dist/logging/index.d.cts +2 -1
  15. package/dist/logging/index.d.ts +2 -1
  16. package/dist/router/index.cjs +405 -502
  17. package/dist/router/index.cjs.map +1 -1
  18. package/dist/router/index.d.cts +12 -6
  19. package/dist/router/index.d.ts +12 -6
  20. package/dist/router/index.js +4 -3
  21. package/dist/{services-9c496c6e.d.ts → services-be91b485.d.ts} +21 -52
  22. package/dist/{services-7b716dcf.d.ts → services-eb9326a1.d.ts} +21 -52
  23. package/dist/transport/impls/uds/client.cjs +197 -155
  24. package/dist/transport/impls/uds/client.cjs.map +1 -1
  25. package/dist/transport/impls/uds/client.d.cts +3 -2
  26. package/dist/transport/impls/uds/client.d.ts +3 -2
  27. package/dist/transport/impls/uds/client.js +3 -3
  28. package/dist/transport/impls/uds/server.cjs +280 -266
  29. package/dist/transport/impls/uds/server.cjs.map +1 -1
  30. package/dist/transport/impls/uds/server.d.cts +3 -2
  31. package/dist/transport/impls/uds/server.d.ts +3 -2
  32. package/dist/transport/impls/uds/server.js +3 -3
  33. package/dist/transport/impls/ws/client.cjs +251 -214
  34. package/dist/transport/impls/ws/client.cjs.map +1 -1
  35. package/dist/transport/impls/ws/client.d.cts +6 -6
  36. package/dist/transport/impls/ws/client.d.ts +6 -6
  37. package/dist/transport/impls/ws/client.js +33 -48
  38. package/dist/transport/impls/ws/client.js.map +1 -1
  39. package/dist/transport/impls/ws/server.cjs +302 -280
  40. package/dist/transport/impls/ws/server.cjs.map +1 -1
  41. package/dist/transport/impls/ws/server.d.cts +5 -4
  42. package/dist/transport/impls/ws/server.d.ts +5 -4
  43. package/dist/transport/impls/ws/server.js +3 -3
  44. package/dist/transport/impls/ws/server.js.map +1 -1
  45. package/dist/transport/index.cjs +400 -396
  46. package/dist/transport/index.cjs.map +1 -1
  47. package/dist/transport/index.d.cts +25 -14
  48. package/dist/transport/index.d.ts +25 -14
  49. package/dist/transport/index.js +2 -2
  50. package/dist/util/testHelpers.cjs +59 -14
  51. package/dist/util/testHelpers.cjs.map +1 -1
  52. package/dist/util/testHelpers.d.cts +14 -5
  53. package/dist/util/testHelpers.d.ts +14 -5
  54. package/dist/util/testHelpers.js +12 -5
  55. package/dist/util/testHelpers.js.map +1 -1
  56. package/dist/wslike-e0b32dd5.d.ts +40 -0
  57. package/package.json +4 -5
  58. package/dist/chunk-3Y7AB5EB.js +0 -42
  59. package/dist/chunk-3Y7AB5EB.js.map +0 -1
  60. package/dist/chunk-5WFL722S.js.map +0 -1
  61. package/dist/chunk-JMXO5L2X.js.map +0 -1
  62. package/dist/chunk-NCXUFDVL.js.map +0 -1
  63. /package/dist/{chunk-FDLAPYCK.js.map → chunk-DZOATC6M.js.map} +0 -0
@@ -1,7 +1,8 @@
1
1
  import { C as Codec } from '../types-3e5768ec.js';
2
- import { O as OpaqueTransportMessage, C as Connection, a as Session, T as TransportClientId, P as PartialTransportMessage, S as SessionOptions, f as ClientHandshakeOptions, g as ServerHandshakeOptions } from '../index-dfad460e.js';
3
- export { H as HandshakeRequestMetadata, d as OpaqueTransportMessageSchema, b as ParsedHandshakeMetadata, e as TransportMessage, c as TransportMessageSchema, h as isStreamClose, i as isStreamOpen } from '../index-dfad460e.js';
4
- import '@sinclair/typebox';
2
+ import { O as OpaqueTransportMessage, C as Connection, b as Session, T as TransportClientId, l as PropagationContext, P as PartialTransportMessage, f as ClientHandshakeOptions, c as ServerHandshakeOptions, m as ParsedMetadata, S as SessionOptions } from '../index-a6fe0edd.js';
3
+ export { h as OpaqueTransportMessageSchema, i as TransportMessage, g as TransportMessageSchema, k as isStreamClose, j as isStreamOpen } from '../index-a6fe0edd.js';
4
+ import { TSchema } from '@sinclair/typebox';
5
+ import '@opentelemetry/api';
5
6
 
6
7
  type ConnectionStatus = 'connect' | 'disconnect';
7
8
  declare const ProtocolError: {
@@ -97,13 +98,9 @@ declare class LeakyBucketRateLimit {
97
98
  type TransportStatus = 'open' | 'closed' | 'destroyed';
98
99
  type TransportOptions = SessionOptions;
99
100
  type ProvidedTransportOptions = Partial<TransportOptions>;
100
- type ClientTransportOptions = TransportOptions & ConnectionRetryOptions & {
101
- handshake?: ClientHandshakeOptions;
102
- };
101
+ type ClientTransportOptions = TransportOptions & ConnectionRetryOptions;
103
102
  type ProvidedClientTransportOptions = Partial<ClientTransportOptions>;
104
- type ServerTransportOptions = TransportOptions & {
105
- handshake?: ServerHandshakeOptions;
106
- };
103
+ type ServerTransportOptions = TransportOptions;
107
104
  type ProvidedServerTransportOptions = Partial<ServerTransportOptions>;
108
105
  /**
109
106
  * Transports manage the lifecycle (creation/deletion) of sessions and connections. Its responsibilities include:
@@ -187,8 +184,8 @@ declare abstract class Transport<ConnType extends Connection> {
187
184
  * @param conn The connection object.
188
185
  */
189
186
  protected onConnect(conn: ConnType, connectedTo: TransportClientId, session: Session<ConnType>, isReconnect: boolean): void;
190
- protected createSession(to: TransportClientId, conn?: ConnType): Session<ConnType>;
191
- protected getOrCreateSession(to: TransportClientId, conn?: ConnType, sessionId?: string): {
187
+ protected createSession(to: TransportClientId, conn?: ConnType, propagationCtx?: PropagationContext): Session<ConnType>;
188
+ protected getOrCreateSession(to: TransportClientId, conn?: ConnType, sessionId?: string, propagationCtx?: PropagationContext): {
192
189
  session: Session<ConnType>;
193
190
  isReconnect: boolean;
194
191
  };
@@ -245,7 +242,7 @@ declare abstract class Transport<ConnType extends Connection> {
245
242
  */
246
243
  destroy(): void;
247
244
  }
248
- declare abstract class ClientTransport<ConnType extends Connection> extends Transport<ConnType> {
245
+ declare abstract class ClientTransport<ConnType extends Connection, HandshakeMetadata extends TSchema = TSchema> extends Transport<ConnType> {
249
246
  /**
250
247
  * The options for this transport.
251
248
  */
@@ -262,7 +259,12 @@ declare abstract class ClientTransport<ConnType extends Connection> extends Tran
262
259
  * tests or a special case where you don't want to reconnect.
263
260
  */
264
261
  reconnectOnConnectionDrop: boolean;
262
+ /**
263
+ * Optional handshake options for this client.
264
+ */
265
+ handshakeExtensions?: ClientHandshakeOptions<HandshakeMetadata>;
265
266
  constructor(clientId: TransportClientId, providedOptions?: ProvidedClientTransportOptions);
267
+ extendHandshake(options: ClientHandshakeOptions<HandshakeMetadata>): void;
266
268
  protected handleConnection(conn: ConnType, to: TransportClientId): void;
267
269
  receiveHandshakeResponseMessage(data: Uint8Array, conn: ConnType): Session<ConnType> | false;
268
270
  /**
@@ -279,18 +281,27 @@ declare abstract class ClientTransport<ConnType extends Connection> extends Tran
279
281
  * @param to The client ID of the node to connect to.
280
282
  */
281
283
  connect(to: TransportClientId): Promise<void>;
282
- private connectAttempt;
283
284
  protected deleteSession(session: Session<ConnType>): void;
284
285
  protected sendHandshake(to: TransportClientId, conn: ConnType): Promise<boolean>;
285
286
  close(): void;
286
287
  }
287
- declare abstract class ServerTransport<ConnType extends Connection> extends Transport<ConnType> {
288
+ declare abstract class ServerTransport<ConnType extends Connection, CustomMetadataSchema extends TSchema = TSchema> extends Transport<ConnType> {
288
289
  /**
289
290
  * The options for this transport.
290
291
  */
291
292
  protected options: ServerTransportOptions;
293
+ /**
294
+ * Optional handshake options for the server.
295
+ */
296
+ handshakeExtensions?: ServerHandshakeOptions<CustomMetadataSchema>;
297
+ /**
298
+ * A map of session handshake data for each session.
299
+ */
300
+ sessionHandshakeMetadata: WeakMap<Session<ConnType>, ParsedMetadata>;
292
301
  constructor(clientId: TransportClientId, providedOptions?: ProvidedServerTransportOptions);
302
+ extendHandshake(options: ServerHandshakeOptions<CustomMetadataSchema>): void;
293
303
  protected handleConnection(conn: ConnType): void;
304
+ private validateHandshakeMetadata;
294
305
  receiveHandshakeRequestMessage(data: Uint8Array, conn: ConnType): Promise<Session<ConnType> | false>;
295
306
  }
296
307
 
@@ -5,11 +5,11 @@ import {
5
5
  ServerTransport,
6
6
  Session,
7
7
  Transport
8
- } from "../chunk-JMXO5L2X.js";
8
+ } from "../chunk-MJUFKPBT.js";
9
9
  import {
10
10
  OpaqueTransportMessageSchema,
11
11
  TransportMessageSchema
12
- } from "../chunk-5WFL722S.js";
12
+ } from "../chunk-PCKHBAVP.js";
13
13
  import "../chunk-OTQNCLFH.js";
14
14
  import "../chunk-3AW3IXVD.js";
15
15
  export {
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // util/testHelpers.ts
@@ -25,6 +35,7 @@ __export(testHelpers_exports, {
25
35
  asClientSubscription: () => asClientSubscription,
26
36
  asClientUpload: () => asClientUpload,
27
37
  createDummyTransportMessage: () => createDummyTransportMessage,
38
+ createLocalWebSocketClient: () => createLocalWebSocketClient,
28
39
  createWebSocketServer: () => createWebSocketServer,
29
40
  getUnixSocketPath: () => getUnixSocketPath,
30
41
  iterNext: () => iterNext,
@@ -35,7 +46,7 @@ __export(testHelpers_exports, {
35
46
  waitForMessage: () => waitForMessage
36
47
  });
37
48
  module.exports = __toCommonJS(testHelpers_exports);
38
- var import_ws = require("ws");
49
+ var import_ws = __toESM(require("ws"), 1);
39
50
 
40
51
  // node_modules/p-defer/index.js
41
52
  function pDefer() {
@@ -340,6 +351,31 @@ function Err(error) {
340
351
  // logging/log.ts
341
352
  var log = void 0;
342
353
 
354
+ // tracing/index.ts
355
+ var import_api = require("@opentelemetry/api");
356
+
357
+ // package.json
358
+ var version = "0.23.0";
359
+
360
+ // tracing/index.ts
361
+ function createSessionTelemetryInfo(session, propagationCtx) {
362
+ const ctx = propagationCtx ? import_api.propagation.extract(import_api.context.active(), propagationCtx) : import_api.context.active();
363
+ const span = tracer.startSpan(
364
+ `session ${session.id}`,
365
+ {
366
+ attributes: {
367
+ component: "river",
368
+ "river.session.id": session.id,
369
+ "river.session.to": session.to,
370
+ "river.session.from": session.from
371
+ }
372
+ },
373
+ ctx
374
+ );
375
+ return { span, ctx };
376
+ }
377
+ var tracer = import_api.trace.getTracer("river", version);
378
+
343
379
  // util/stringify.ts
344
380
  function coerceErrorString(err) {
345
381
  if (err instanceof Error) {
@@ -353,11 +389,13 @@ var import_nanoid2 = require("nanoid");
353
389
 
354
390
  // transport/session.ts
355
391
  var import_nanoid = require("nanoid");
392
+ var import_api2 = require("@opentelemetry/api");
356
393
  var nanoid = (0, import_nanoid.customAlphabet)("1234567890abcdefghijklmnopqrstuvxyz", 6);
357
394
  var unsafeId = () => nanoid();
358
395
  var Session = class {
359
396
  codec;
360
397
  options;
398
+ telemetry;
361
399
  /**
362
400
  * The buffer of messages that have been sent but not yet acknowledged.
363
401
  */
@@ -377,12 +415,6 @@ var Session = class {
377
415
  * for this session.
378
416
  */
379
417
  advertisedSessionId;
380
- /**
381
- * The metadata for this session, as parsed from the handshake.
382
- *
383
- * Will only ever be populated on the server side.
384
- */
385
- metadata;
386
418
  /**
387
419
  * Number of messages we've sent along this session (excluding handshake and acks)
388
420
  */
@@ -404,7 +436,7 @@ var Session = class {
404
436
  * The interval for sending heartbeats.
405
437
  */
406
438
  heartbeat;
407
- constructor(conn, from, to, options) {
439
+ constructor(conn, from, to, options, propagationCtx) {
408
440
  this.id = `session-${nanoid(12)}`;
409
441
  this.options = options;
410
442
  this.from = from;
@@ -416,13 +448,14 @@ var Session = class {
416
448
  () => this.sendHeartbeat(),
417
449
  options.heartbeatIntervalMs
418
450
  );
451
+ this.telemetry = createSessionTelemetryInfo(this, propagationCtx);
419
452
  }
420
453
  get loggingMetadata() {
421
454
  return {
422
455
  clientId: this.from,
423
456
  connectedTo: this.to,
424
457
  sessionId: this.id,
425
- connId: this.connection?.debugId
458
+ connId: this.connection?.id
426
459
  };
427
460
  }
428
461
  /**
@@ -467,6 +500,7 @@ var Session = class {
467
500
  `closing connection to ${this.to} due to inactivity (missed ${misses} heartbeats which is ${missDuration}ms)`,
468
501
  this.loggingMetadata
469
502
  );
503
+ this.telemetry.span.addEvent("closing connection due to inactivity");
470
504
  this.closeStaleConnection();
471
505
  }
472
506
  return;
@@ -488,21 +522,25 @@ var Session = class {
488
522
  sendBufferedMessages(conn) {
489
523
  log?.info(`resending ${this.sendBuffer.length} buffered messages`, {
490
524
  ...this.loggingMetadata,
491
- connId: conn.debugId
525
+ connId: conn.id
492
526
  });
493
527
  for (const msg of this.sendBuffer) {
494
528
  log?.debug(`resending msg`, {
495
529
  ...this.loggingMetadata,
496
530
  fullTransportMessage: msg,
497
- connId: conn.debugId
531
+ connId: conn.id
498
532
  });
499
533
  const ok = conn.send(this.codec.toBuffer(msg));
500
534
  if (!ok) {
501
535
  const errMsg = `failed to send buffered message to ${this.to} (sus, this is a fresh connection)`;
536
+ conn.telemetry?.span.setStatus({
537
+ code: import_api2.SpanStatusCode.ERROR,
538
+ message: errMsg
539
+ });
502
540
  log?.error(errMsg, {
503
541
  ...this.loggingMetadata,
504
542
  fullTransportMessage: msg,
505
- connId: conn.debugId,
543
+ connId: conn.id,
506
544
  tags: ["invariant-violation"]
507
545
  });
508
546
  conn.close();
@@ -587,7 +625,6 @@ var Session = class {
587
625
 
588
626
  // transport/transport.ts
589
627
  var import_value = require("@sinclair/typebox/value");
590
- var import_api = require("@opentelemetry/api");
591
628
 
592
629
  // codec/json.ts
593
630
  var encoder = new TextEncoder();
@@ -642,6 +679,7 @@ var NaiveJsonCodec = {
642
679
  };
643
680
 
644
681
  // transport/transport.ts
682
+ var import_api3 = require("@opentelemetry/api");
645
683
  var defaultTransportOptions = {
646
684
  heartbeatIntervalMs: 1e3,
647
685
  heartbeatsUntilDead: 2,
@@ -664,6 +702,11 @@ var defaultServerTransportOptions = {
664
702
  };
665
703
 
666
704
  // util/testHelpers.ts
705
+ function createLocalWebSocketClient(port) {
706
+ const sock = new import_ws.default(`ws://localhost:${port}`);
707
+ sock.binaryType = "arraybuffer";
708
+ return sock;
709
+ }
667
710
  function createWebSocketServer(server) {
668
711
  return new import_ws.WebSocketServer({ server });
669
712
  }
@@ -737,7 +780,8 @@ function dummyCtx(state, session, extendedContext) {
737
780
  to: session.to,
738
781
  from: session.from,
739
782
  streamId: (0, import_nanoid2.nanoid)(),
740
- session
783
+ session,
784
+ metadata: {}
741
785
  };
742
786
  }
743
787
  function asClientRpc(state, proc, extendedContext, session = dummySession()) {
@@ -794,6 +838,7 @@ var getUnixSocketPath = () => {
794
838
  asClientSubscription,
795
839
  asClientUpload,
796
840
  createDummyTransportMessage,
841
+ createLocalWebSocketClient,
797
842
  createWebSocketServer,
798
843
  getUnixSocketPath,
799
844
  iterNext,