@replit/river 0.19.2 → 0.20.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 (72) hide show
  1. package/dist/{chunk-D5PVGZPQ.js → chunk-5WFL722S.js} +15 -1
  2. package/dist/chunk-5WFL722S.js.map +1 -0
  3. package/dist/{chunk-NBE3D667.js → chunk-DT5JS6TM.js} +1 -0
  4. package/dist/chunk-DT5JS6TM.js.map +1 -0
  5. package/dist/{chunk-ZWPEZS27.js → chunk-HGBAUTIW.js} +2 -1
  6. package/dist/chunk-HGBAUTIW.js.map +1 -0
  7. package/dist/{chunk-YFPVQTWL.js → chunk-NYOK5VKK.js} +385 -246
  8. package/dist/chunk-NYOK5VKK.js.map +1 -0
  9. package/dist/{chunk-GZ7HCLLM.js → chunk-XAG3SC7R.js} +1 -0
  10. package/dist/chunk-XAG3SC7R.js.map +1 -0
  11. package/dist/{chunk-SR4DBLJ6.js → chunk-YB5LRYL6.js} +518 -260
  12. package/dist/chunk-YB5LRYL6.js.map +1 -0
  13. package/dist/{chunk-JH275HID.js → chunk-ZIWOHAQM.js} +2 -1
  14. package/dist/chunk-ZIWOHAQM.js.map +1 -0
  15. package/dist/codec/index.cjs +1 -0
  16. package/dist/codec/index.cjs.map +1 -0
  17. package/dist/codec/index.js +2 -1
  18. package/dist/codec/index.js.map +1 -0
  19. package/dist/{connection-cfec12e6.d.ts → connection-d49d5d56.d.ts} +1 -1
  20. package/dist/{connection-aa0ea000.d.ts → connection-dba95bc8.d.ts} +1 -1
  21. package/dist/{index-e2513701.d.ts → index-3ac92295.d.ts} +12 -0
  22. package/dist/logging/index.cjs +1 -0
  23. package/dist/logging/index.cjs.map +1 -0
  24. package/dist/logging/index.d.cts +1 -1
  25. package/dist/logging/index.d.ts +1 -1
  26. package/dist/logging/index.js +2 -1
  27. package/dist/logging/index.js.map +1 -0
  28. package/dist/router/index.cjs +515 -257
  29. package/dist/router/index.cjs.map +1 -0
  30. package/dist/router/index.d.cts +4 -4
  31. package/dist/router/index.d.ts +4 -4
  32. package/dist/router/index.js +4 -3
  33. package/dist/router/index.js.map +1 -0
  34. package/dist/{services-5fc5712d.d.ts → services-8496d6e8.d.ts} +1 -1
  35. package/dist/{services-4bba42d8.d.ts → services-abc077db.d.ts} +1 -1
  36. package/dist/transport/impls/uds/client.cjs +151 -74
  37. package/dist/transport/impls/uds/client.cjs.map +1 -0
  38. package/dist/transport/impls/uds/client.d.cts +2 -2
  39. package/dist/transport/impls/uds/client.d.ts +2 -2
  40. package/dist/transport/impls/uds/client.js +6 -5
  41. package/dist/transport/impls/uds/client.js.map +1 -0
  42. package/dist/transport/impls/uds/server.cjs +250 -169
  43. package/dist/transport/impls/uds/server.cjs.map +1 -0
  44. package/dist/transport/impls/uds/server.d.cts +2 -2
  45. package/dist/transport/impls/uds/server.d.ts +2 -2
  46. package/dist/transport/impls/uds/server.js +6 -5
  47. package/dist/transport/impls/uds/server.js.map +1 -0
  48. package/dist/transport/impls/ws/client.cjs +151 -74
  49. package/dist/transport/impls/ws/client.cjs.map +1 -0
  50. package/dist/transport/impls/ws/client.d.cts +2 -2
  51. package/dist/transport/impls/ws/client.d.ts +2 -2
  52. package/dist/transport/impls/ws/client.js +6 -5
  53. package/dist/transport/impls/ws/client.js.map +1 -0
  54. package/dist/transport/impls/ws/server.cjs +250 -169
  55. package/dist/transport/impls/ws/server.cjs.map +1 -0
  56. package/dist/transport/impls/ws/server.d.cts +2 -2
  57. package/dist/transport/impls/ws/server.d.ts +2 -2
  58. package/dist/transport/impls/ws/server.js +6 -5
  59. package/dist/transport/impls/ws/server.js.map +1 -0
  60. package/dist/transport/index.cjs +388 -243
  61. package/dist/transport/index.cjs.map +1 -0
  62. package/dist/transport/index.d.cts +3 -2
  63. package/dist/transport/index.d.ts +3 -2
  64. package/dist/transport/index.js +5 -4
  65. package/dist/transport/index.js.map +1 -0
  66. package/dist/util/testHelpers.cjs +2 -0
  67. package/dist/util/testHelpers.cjs.map +1 -0
  68. package/dist/util/testHelpers.d.cts +2 -2
  69. package/dist/util/testHelpers.d.ts +2 -2
  70. package/dist/util/testHelpers.js +6 -5
  71. package/dist/util/testHelpers.js.map +1 -0
  72. package/package.json +2 -1
@@ -26,6 +26,7 @@ module.exports = __toCommonJS(server_exports);
26
26
 
27
27
  // transport/transport.ts
28
28
  var import_value = require("@sinclair/typebox/value");
29
+ var import_api2 = require("@opentelemetry/api");
29
30
 
30
31
  // transport/message.ts
31
32
  var import_typebox = require("@sinclair/typebox");
@@ -40,6 +41,12 @@ var TransportMessageSchema = (t) => import_typebox.Type.Object({
40
41
  procedureName: import_typebox.Type.Optional(import_typebox.Type.String()),
41
42
  streamId: import_typebox.Type.String(),
42
43
  controlFlags: import_typebox.Type.Integer(),
44
+ tracing: import_typebox.Type.Optional(
45
+ import_typebox.Type.Object({
46
+ traceparent: import_typebox.Type.String(),
47
+ tracestate: import_typebox.Type.String()
48
+ })
49
+ ),
43
50
  payload: t
44
51
  });
45
52
  var ControlMessageAckSchema = import_typebox.Type.Object({
@@ -371,6 +378,11 @@ var Session = class {
371
378
  }
372
379
  };
373
380
 
381
+ // tracing/index.ts
382
+ var import_api = require("@opentelemetry/api");
383
+ var tracer = import_api.trace.getTracer("river");
384
+ var tracing_default = tracer;
385
+
374
386
  // util/stringify.ts
375
387
  function coerceErrorString(err) {
376
388
  if (err instanceof Error) {
@@ -743,78 +755,100 @@ var ServerTransport = class extends Transport {
743
755
  });
744
756
  }
745
757
  handleConnection(conn) {
746
- if (this.state !== "open")
747
- return;
748
- log?.info(`new incoming connection`, {
749
- clientId: this.clientId,
750
- connId: conn.debugId
751
- });
752
- let session = void 0;
753
- const client = () => session?.to ?? "unknown";
754
- const handshakeTimeout = setTimeout(() => {
755
- if (!session) {
756
- log?.warn(
757
- `connection to ${client()} timed out waiting for handshake, closing`,
758
- {
759
- clientId: this.clientId,
760
- connectedTo: client(),
761
- connId: conn.debugId
762
- }
763
- );
764
- conn.close();
765
- }
766
- }, this.options.sessionDisconnectGraceMs);
767
- const buffer = [];
768
- let receivedHandshakeMessage = false;
769
- const handshakeHandler = (data) => {
770
- if (receivedHandshakeMessage) {
771
- buffer.push(data);
772
- return;
773
- }
774
- receivedHandshakeMessage = true;
775
- clearTimeout(handshakeTimeout);
776
- void this.receiveHandshakeRequestMessage(data, conn).then(
777
- (maybeSession) => {
778
- if (!maybeSession) {
758
+ tracing_default.startActiveSpan(
759
+ "handleConnection",
760
+ {
761
+ attributes: {
762
+ component: "river",
763
+ "span.kind": "server"
764
+ },
765
+ kind: import_api2.SpanKind.SERVER
766
+ },
767
+ (span) => {
768
+ if (this.state !== "open")
769
+ return;
770
+ log?.info(`new incoming connection`, {
771
+ clientId: this.clientId,
772
+ connId: conn.debugId
773
+ });
774
+ let session = void 0;
775
+ const client = () => session?.to ?? "unknown";
776
+ const handshakeTimeout = setTimeout(() => {
777
+ if (!session) {
778
+ log?.warn(
779
+ `connection to ${client()} timed out waiting for handshake, closing`,
780
+ {
781
+ clientId: this.clientId,
782
+ connectedTo: client(),
783
+ connId: conn.debugId
784
+ }
785
+ );
786
+ span.setStatus({ code: import_api2.SpanStatusCode.ERROR });
787
+ span.end();
779
788
  conn.close();
789
+ }
790
+ }, this.options.sessionDisconnectGraceMs);
791
+ const buffer = [];
792
+ let receivedHandshakeMessage = false;
793
+ const handshakeHandler = (data) => {
794
+ if (receivedHandshakeMessage) {
795
+ buffer.push(data);
780
796
  return;
781
797
  }
782
- session = maybeSession;
783
- const dataHandler = (data2) => {
784
- const parsed = this.parseMsg(data2);
785
- if (!parsed) {
786
- conn.close();
787
- return;
798
+ receivedHandshakeMessage = true;
799
+ clearTimeout(handshakeTimeout);
800
+ void this.receiveHandshakeRequestMessage(data, conn).then(
801
+ (maybeSession) => {
802
+ if (!maybeSession) {
803
+ span.setStatus({ code: import_api2.SpanStatusCode.ERROR });
804
+ span.end();
805
+ conn.close();
806
+ return;
807
+ }
808
+ session = maybeSession;
809
+ const dataHandler = (data2) => {
810
+ const parsed = this.parseMsg(data2);
811
+ if (!parsed) {
812
+ conn.close();
813
+ return;
814
+ }
815
+ this.handleMsg(parsed);
816
+ };
817
+ conn.removeDataListener(handshakeHandler);
818
+ conn.addDataListener(dataHandler);
819
+ for (const data2 of buffer) {
820
+ dataHandler(data2);
821
+ }
822
+ buffer.length = 0;
788
823
  }
789
- this.handleMsg(parsed);
790
- };
791
- conn.removeDataListener(handshakeHandler);
792
- conn.addDataListener(dataHandler);
793
- for (const data2 of buffer) {
794
- dataHandler(data2);
824
+ );
825
+ };
826
+ conn.addDataListener(handshakeHandler);
827
+ conn.addCloseListener(() => {
828
+ if (session) {
829
+ log?.info(`connection to ${client()} disconnected`, {
830
+ clientId: this.clientId,
831
+ connId: conn.debugId
832
+ });
833
+ this.onDisconnect(conn, session);
795
834
  }
796
- buffer.length = 0;
797
- }
798
- );
799
- };
800
- conn.addDataListener(handshakeHandler);
801
- conn.addCloseListener(() => {
802
- if (!session)
803
- return;
804
- log?.info(`connection to ${client()} disconnected`, {
805
- clientId: this.clientId,
806
- connId: conn.debugId
807
- });
808
- this.onDisconnect(conn, session);
809
- });
810
- conn.addErrorListener((err) => {
811
- if (!session)
812
- return;
813
- log?.warn(
814
- `connection to ${client()} got an error: ${coerceErrorString(err)}`,
815
- { clientId: this.clientId, connId: conn.debugId }
816
- );
817
- });
835
+ span.setStatus({ code: import_api2.SpanStatusCode.OK });
836
+ span.end();
837
+ });
838
+ conn.addErrorListener((err) => {
839
+ if (session) {
840
+ log?.warn(
841
+ `connection to ${client()} got an error: ${coerceErrorString(
842
+ err
843
+ )}`,
844
+ { clientId: this.clientId, connId: conn.debugId }
845
+ );
846
+ }
847
+ span.setStatus({ code: import_api2.SpanStatusCode.ERROR });
848
+ span.end();
849
+ });
850
+ }
851
+ );
818
852
  }
819
853
  async receiveHandshakeRequestMessage(data, conn) {
820
854
  const parsed = this.parseMsg(data);
@@ -825,117 +859,163 @@ var ServerTransport = class extends Transport {
825
859
  );
826
860
  return false;
827
861
  }
828
- if (!import_value.Value.Check(ControlMessageHandshakeRequestSchema, parsed.payload)) {
829
- const reason = "received invalid handshake msg";
830
- const responseMsg2 = handshakeResponseMessage(this.clientId, parsed.from, {
831
- ok: false,
832
- reason
833
- });
834
- conn.send(this.codec.toBuffer(responseMsg2));
835
- const logData = typeof parsed.payload === "object" ? { ...parsed, payload: { ...parsed.payload, metadata: "redacted" } } : { ...parsed };
836
- log?.warn(`${reason}: ${JSON.stringify(logData)}`, {
837
- clientId: this.clientId,
838
- connId: conn.debugId
839
- });
840
- this.protocolError(
841
- ProtocolError.HandshakeFailed,
842
- "invalid handshake request"
843
- );
844
- return false;
845
- }
846
- const gotVersion = parsed.payload.protocolVersion;
847
- if (gotVersion !== PROTOCOL_VERSION) {
848
- const reason = `incorrect version (got: ${gotVersion} wanted ${PROTOCOL_VERSION})`;
849
- const responseMsg2 = handshakeResponseMessage(this.clientId, parsed.from, {
850
- ok: false,
851
- reason
852
- });
853
- conn.send(this.codec.toBuffer(responseMsg2));
854
- log?.warn(
855
- `received handshake msg with incompatible protocol version (got: ${gotVersion}, expected: ${PROTOCOL_VERSION})`,
856
- { clientId: this.clientId, connId: conn.debugId }
857
- );
858
- this.protocolError(ProtocolError.HandshakeFailed, reason);
859
- return false;
862
+ let activeContext = import_api2.context.active();
863
+ if (parsed.tracing) {
864
+ activeContext = import_api2.propagation.extract(activeContext, parsed.tracing);
860
865
  }
861
- const { session, isReconnect } = this.getOrCreateSession(
862
- parsed.from,
863
- conn,
864
- parsed.payload.sessionId
865
- );
866
- let handshakeMetadata;
867
- if (this.options.handshake) {
868
- if (!import_value.Value.Check(
869
- this.options.handshake.requestSchema,
870
- parsed.payload.metadata
871
- )) {
872
- const reason = "received malformed handshake metadata";
873
- const responseMsg2 = handshakeResponseMessage(
874
- this.clientId,
866
+ return tracing_default.startActiveSpan(
867
+ "receiveHandshakeRequestMessage",
868
+ {
869
+ attributes: {
870
+ component: "river",
871
+ "span.kind": "server"
872
+ },
873
+ kind: import_api2.SpanKind.SERVER
874
+ },
875
+ activeContext,
876
+ async (span) => {
877
+ if (!import_value.Value.Check(ControlMessageHandshakeRequestSchema, parsed.payload)) {
878
+ const reason = "received invalid handshake msg";
879
+ const responseMsg2 = handshakeResponseMessage(
880
+ this.clientId,
881
+ parsed.from,
882
+ {
883
+ ok: false,
884
+ reason
885
+ }
886
+ );
887
+ conn.send(this.codec.toBuffer(responseMsg2));
888
+ const logData = typeof parsed.payload === "object" ? {
889
+ ...parsed,
890
+ payload: { ...parsed.payload, metadata: "redacted" }
891
+ } : { ...parsed };
892
+ log?.warn(`${reason}: ${JSON.stringify(logData)}`, {
893
+ clientId: this.clientId,
894
+ connId: conn.debugId
895
+ });
896
+ this.protocolError(
897
+ ProtocolError.HandshakeFailed,
898
+ "invalid handshake request"
899
+ );
900
+ span.setStatus({ code: import_api2.SpanStatusCode.ERROR });
901
+ span.end();
902
+ return false;
903
+ }
904
+ const gotVersion = parsed.payload.protocolVersion;
905
+ if (gotVersion !== PROTOCOL_VERSION) {
906
+ const reason = `incorrect version (got: ${gotVersion} wanted ${PROTOCOL_VERSION})`;
907
+ const responseMsg2 = handshakeResponseMessage(
908
+ this.clientId,
909
+ parsed.from,
910
+ {
911
+ ok: false,
912
+ reason
913
+ }
914
+ );
915
+ conn.send(this.codec.toBuffer(responseMsg2));
916
+ log?.warn(
917
+ `received handshake msg with incompatible protocol version (got: ${gotVersion}, expected: ${PROTOCOL_VERSION})`,
918
+ { clientId: this.clientId, connId: conn.debugId }
919
+ );
920
+ this.protocolError(ProtocolError.HandshakeFailed, reason);
921
+ span.setStatus({ code: import_api2.SpanStatusCode.ERROR });
922
+ span.end();
923
+ return false;
924
+ }
925
+ const { session, isReconnect } = this.getOrCreateSession(
875
926
  parsed.from,
876
- { ok: false, reason }
927
+ conn,
928
+ parsed.payload.sessionId
877
929
  );
878
- conn.send(this.codec.toBuffer(responseMsg2));
879
- log?.warn(`received malformed handshake metadata from ${parsed.from}`, {
880
- clientId: this.clientId,
881
- connId: conn.debugId
882
- });
883
- this.protocolError(ProtocolError.HandshakeFailed, reason);
884
- this.deleteSession(session);
885
- return false;
886
- }
887
- const parsedMetadata = await this.options.handshake.parse(
888
- parsed.payload.metadata,
889
- session,
890
- isReconnect
891
- );
892
- if (parsedMetadata === false) {
893
- const reason = "rejected by server";
894
- const responseMsg2 = handshakeResponseMessage(
895
- this.clientId,
896
- parsed.from,
897
- { ok: false, reason }
930
+ let handshakeMetadata;
931
+ if (this.options.handshake) {
932
+ if (!import_value.Value.Check(
933
+ this.options.handshake.requestSchema,
934
+ parsed.payload.metadata
935
+ )) {
936
+ const reason = "received malformed handshake metadata";
937
+ const responseMsg2 = handshakeResponseMessage(
938
+ this.clientId,
939
+ parsed.from,
940
+ { ok: false, reason }
941
+ );
942
+ conn.send(this.codec.toBuffer(responseMsg2));
943
+ log?.warn(
944
+ `received malformed handshake metadata from ${parsed.from}`,
945
+ {
946
+ clientId: this.clientId,
947
+ connId: conn.debugId
948
+ }
949
+ );
950
+ this.protocolError(ProtocolError.HandshakeFailed, reason);
951
+ this.deleteSession(session);
952
+ span.setStatus({ code: import_api2.SpanStatusCode.ERROR });
953
+ span.end();
954
+ return false;
955
+ }
956
+ const parsedMetadata = await this.options.handshake.parse(
957
+ parsed.payload.metadata,
958
+ session,
959
+ isReconnect
960
+ );
961
+ if (parsedMetadata === false) {
962
+ const reason = "rejected by server";
963
+ const responseMsg2 = handshakeResponseMessage(
964
+ this.clientId,
965
+ parsed.from,
966
+ { ok: false, reason }
967
+ );
968
+ conn.send(this.codec.toBuffer(responseMsg2));
969
+ log?.warn(`rejected handshake from ${parsed.from}`, {
970
+ clientId: this.clientId,
971
+ connId: conn.debugId
972
+ });
973
+ this.protocolError(ProtocolError.HandshakeFailed, reason);
974
+ this.deleteSession(session);
975
+ span.setStatus({ code: import_api2.SpanStatusCode.ERROR });
976
+ span.end();
977
+ return false;
978
+ }
979
+ if (!import_value.Value.Check(this.options.handshake.parsedSchema, parsedMetadata)) {
980
+ const reason = "failed to parse handshake metadata";
981
+ const responseMsg2 = handshakeResponseMessage(
982
+ this.clientId,
983
+ parsed.from,
984
+ { ok: false, reason }
985
+ );
986
+ conn.send(this.codec.toBuffer(responseMsg2));
987
+ log?.error(`failed to parse handshake metadata`, {
988
+ clientId: this.clientId,
989
+ connId: conn.debugId
990
+ });
991
+ this.protocolError(ProtocolError.HandshakeFailed, reason);
992
+ this.deleteSession(session);
993
+ span.setStatus({ code: import_api2.SpanStatusCode.ERROR });
994
+ span.end();
995
+ return false;
996
+ }
997
+ handshakeMetadata = parsedMetadata;
998
+ }
999
+ handshakeMetadata ??= {};
1000
+ session.metadata = handshakeMetadata;
1001
+ log?.debug(
1002
+ `handshake from ${parsed.from} ok, responding with handshake success`,
1003
+ { clientId: this.clientId, connId: conn.debugId }
898
1004
  );
899
- conn.send(this.codec.toBuffer(responseMsg2));
900
- log?.warn(`rejected handshake from ${parsed.from}`, {
901
- clientId: this.clientId,
902
- connId: conn.debugId
903
- });
904
- this.protocolError(ProtocolError.HandshakeFailed, reason);
905
- this.deleteSession(session);
906
- return false;
907
- }
908
- if (!import_value.Value.Check(this.options.handshake.parsedSchema, parsedMetadata)) {
909
- const reason = "failed to parse handshake metadata";
910
- const responseMsg2 = handshakeResponseMessage(
1005
+ const responseMsg = handshakeResponseMessage(
911
1006
  this.clientId,
912
1007
  parsed.from,
913
- { ok: false, reason }
1008
+ {
1009
+ ok: true,
1010
+ sessionId: session.id
1011
+ }
914
1012
  );
915
- conn.send(this.codec.toBuffer(responseMsg2));
916
- log?.error(`failed to parse handshake metadata`, {
917
- clientId: this.clientId,
918
- connId: conn.debugId
919
- });
920
- this.protocolError(ProtocolError.HandshakeFailed, reason);
921
- this.deleteSession(session);
922
- return false;
1013
+ conn.send(this.codec.toBuffer(responseMsg));
1014
+ this.onConnect(conn, parsed.from, session, isReconnect);
1015
+ span.end();
1016
+ return session;
923
1017
  }
924
- handshakeMetadata = parsedMetadata;
925
- }
926
- handshakeMetadata ??= {};
927
- session.metadata = handshakeMetadata;
928
- log?.debug(
929
- `handshake from ${parsed.from} ok, responding with handshake success`,
930
- { clientId: this.clientId, connId: conn.debugId }
931
1018
  );
932
- const responseMsg = handshakeResponseMessage(this.clientId, parsed.from, {
933
- ok: true,
934
- sessionId: session.id
935
- });
936
- conn.send(this.codec.toBuffer(responseMsg));
937
- this.onConnect(conn, parsed.from, session, isReconnect);
938
- return session;
939
1019
  }
940
1020
  };
941
1021
 
@@ -993,3 +1073,4 @@ var WebSocketServerTransport = class extends ServerTransport {
993
1073
  0 && (module.exports = {
994
1074
  WebSocketServerTransport
995
1075
  });
1076
+ //# sourceMappingURL=server.cjs.map