@replit/river 0.17.4 → 0.18.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 (45) hide show
  1. package/README.md +4 -3
  2. package/dist/{chunk-7WY3Z5ZN.js → chunk-CLY7AQ25.js} +169 -95
  3. package/dist/{chunk-4C2OXQJB.js → chunk-TIFNW5GQ.js} +62 -65
  4. package/dist/{chunk-F3LFO3GU.js → chunk-UEKU6XRG.js} +1 -1
  5. package/dist/chunk-YITXOAPA.js +72 -0
  6. package/dist/{chunk-Q7OSVPZ5.js → chunk-ZPPKYJI7.js} +1 -1
  7. package/dist/{connection-bdbd20da.d.ts → connection-32bf6608.d.ts} +1 -1
  8. package/dist/{connection-c4a17403.d.ts → connection-df5f32ee.d.ts} +1 -1
  9. package/dist/{index-9e300e8a.d.ts → index-314e676a.d.ts} +4 -86
  10. package/dist/index-6118cd48.d.ts +117 -0
  11. package/dist/logging/index.cjs +63 -27
  12. package/dist/logging/index.d.cts +2 -34
  13. package/dist/logging/index.d.ts +2 -34
  14. package/dist/logging/index.js +7 -7
  15. package/dist/{procedures-1c0d2eee.d.ts → procedures-74a10937.d.ts} +4 -3
  16. package/dist/router/index.cjs +63 -66
  17. package/dist/router/index.d.cts +43 -42
  18. package/dist/router/index.d.ts +43 -42
  19. package/dist/router/index.js +2 -2
  20. package/dist/transport/impls/uds/client.cjs +152 -84
  21. package/dist/transport/impls/uds/client.d.cts +3 -2
  22. package/dist/transport/impls/uds/client.d.ts +3 -2
  23. package/dist/transport/impls/uds/client.js +7 -4
  24. package/dist/transport/impls/uds/server.cjs +116 -65
  25. package/dist/transport/impls/uds/server.d.cts +3 -2
  26. package/dist/transport/impls/uds/server.d.ts +3 -2
  27. package/dist/transport/impls/uds/server.js +3 -3
  28. package/dist/transport/impls/ws/client.cjs +156 -87
  29. package/dist/transport/impls/ws/client.d.cts +3 -2
  30. package/dist/transport/impls/ws/client.d.ts +3 -2
  31. package/dist/transport/impls/ws/client.js +11 -7
  32. package/dist/transport/impls/ws/server.cjs +116 -65
  33. package/dist/transport/impls/ws/server.d.cts +4 -3
  34. package/dist/transport/impls/ws/server.d.ts +4 -3
  35. package/dist/transport/impls/ws/server.js +3 -3
  36. package/dist/transport/index.cjs +170 -96
  37. package/dist/transport/index.d.cts +2 -1
  38. package/dist/transport/index.d.ts +2 -1
  39. package/dist/transport/index.js +2 -2
  40. package/dist/util/testHelpers.cjs +48 -17
  41. package/dist/util/testHelpers.d.cts +4 -3
  42. package/dist/util/testHelpers.d.ts +4 -3
  43. package/dist/util/testHelpers.js +3 -3
  44. package/package.json +1 -1
  45. package/dist/chunk-H4BYJELI.js +0 -37
@@ -52,8 +52,8 @@ var import_ws = require("ws");
52
52
  // transport/transport.ts
53
53
  var import_value = require("@sinclair/typebox/value");
54
54
 
55
- // logging/index.ts
56
- var log;
55
+ // logging/log.ts
56
+ var log = void 0;
57
57
 
58
58
  // transport/session.ts
59
59
  var import_nanoid = require("nanoid");
@@ -115,6 +115,14 @@ var Session = class {
115
115
  options.heartbeatIntervalMs
116
116
  );
117
117
  }
118
+ get loggingMetadata() {
119
+ return {
120
+ clientId: this.from,
121
+ connectedTo: this.to,
122
+ sessionId: this.id,
123
+ connId: this.connection?.debugId
124
+ };
125
+ }
118
126
  /**
119
127
  * Sends a message over the session's connection.
120
128
  * If the connection is not ready or the message fails to send, the message can be buffered for retry unless skipped.
@@ -125,26 +133,37 @@ var Session = class {
125
133
  */
126
134
  send(msg) {
127
135
  const fullMsg = this.constructMsg(msg);
128
- log?.debug(`${this.from} -- sending ${JSON.stringify(fullMsg)}`);
136
+ log?.debug(`sending msg`, {
137
+ ...this.loggingMetadata,
138
+ fullTransportMessage: fullMsg
139
+ });
129
140
  if (this.connection) {
130
141
  const ok = this.connection.send(this.codec.toBuffer(fullMsg));
131
142
  if (ok)
132
143
  return fullMsg.id;
133
144
  log?.info(
134
- `${this.from} -- failed to send ${fullMsg.id} (seq: ${fullMsg.seq}) to ${fullMsg.to}, connection (id: ${this.connection.debugId}) is probably dead`
145
+ `failed to send msg to ${fullMsg.to}, connection is probably dead`,
146
+ {
147
+ ...this.loggingMetadata,
148
+ fullTransportMessage: fullMsg
149
+ }
135
150
  );
136
151
  } else {
137
152
  log?.info(
138
- `${this.from} -- failed to send ${fullMsg.id} (seq: ${fullMsg.seq}) to ${fullMsg.to}, connection not ready yet`
153
+ `failed to send msg to ${fullMsg.to}, connection not ready yet`,
154
+ { ...this.loggingMetadata, fullTransportMessage: fullMsg }
139
155
  );
140
156
  }
141
157
  return fullMsg.id;
142
158
  }
143
159
  sendHeartbeat() {
144
- if (this.heartbeatMisses >= this.options.heartbeatsUntilDead) {
160
+ const misses = this.heartbeatMisses;
161
+ const missDuration = misses * this.options.heartbeatIntervalMs;
162
+ if (misses > this.options.heartbeatsUntilDead) {
145
163
  if (this.connection) {
146
164
  log?.info(
147
- `${this.from} -- closing connection (id: ${this.connection.debugId}) to ${this.to} due to inactivity`
165
+ `closing connection to ${this.to} due to inactivity (missed ${misses} heartbeats which is ${missDuration}ms)`,
166
+ this.loggingMetadata
148
167
  );
149
168
  this.closeStaleConnection();
150
169
  }
@@ -166,26 +185,36 @@ var Session = class {
166
185
  }
167
186
  sendBufferedMessages() {
168
187
  if (!this.connection) {
169
- const msg = `${this.from} -- tried sending buffered messages without a connection (if you hit this code path something is seriously wrong)`;
170
- log?.error(msg);
188
+ const msg = `tried sending buffered messages without a connection (if you hit this code path something is seriously wrong)`;
189
+ log?.error(msg, this.loggingMetadata);
171
190
  throw new Error(msg);
172
191
  }
173
192
  log?.info(
174
- `${this.from} -- resending ${this.sendBuffer.length} buffered messages`
193
+ `resending ${this.sendBuffer.length} buffered messages`,
194
+ this.loggingMetadata
175
195
  );
176
196
  for (const msg of this.sendBuffer) {
177
- log?.debug(`${this.from} -- resending ${msg.id} (seq: ${msg.seq})`);
197
+ log?.debug(`resending msg`, {
198
+ ...this.loggingMetadata,
199
+ fullTransportMessage: msg
200
+ });
178
201
  const ok = this.connection.send(this.codec.toBuffer(msg));
179
202
  if (!ok) {
180
- const msg2 = `${this.from} -- failed to send buffered message to ${this.to} in session (id: ${this.id}) (if you hit this code path something is seriously wrong)`;
181
- log?.error(msg2);
182
- throw new Error(msg2);
203
+ const errMsg = `failed to send buffered message to ${this.to} (if you hit this code path something is seriously wrong)`;
204
+ log?.error(errMsg, {
205
+ ...this.loggingMetadata,
206
+ fullTransportMessage: msg
207
+ });
208
+ throw new Error(errMsg);
183
209
  }
184
210
  }
185
211
  }
186
212
  updateBookkeeping(ack, seq) {
187
213
  if (seq + 1 < this.ack) {
188
- log?.error(`${this.from} -- received stale seq ${seq} + 1 < ${this.ack}`);
214
+ log?.error(
215
+ `received stale seq ${seq} + 1 < ${this.ack}`,
216
+ this.loggingMetadata
217
+ );
189
218
  return;
190
219
  }
191
220
  this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq >= ack);
@@ -195,7 +224,8 @@ var Session = class {
195
224
  if (this.connection === void 0 || this.connection === conn)
196
225
  return;
197
226
  log?.info(
198
- `${this.from} -- closing old inner connection (id: ${this.connection.debugId}) from session (id: ${this.id}) to ${this.to}`
227
+ `closing old inner connection from session to ${this.to}`,
228
+ this.loggingMetadata
199
229
  );
200
230
  this.connection.close();
201
231
  this.connection = void 0;
@@ -207,7 +237,8 @@ var Session = class {
207
237
  }
208
238
  beginGrace(cb) {
209
239
  log?.info(
210
- `${this.from} -- starting ${this.options.sessionDisconnectGraceMs}ms grace period until session (id: ${this.id}) to ${this.to} is closed`
240
+ `starting ${this.options.sessionDisconnectGraceMs}ms grace period until session to ${this.to} is closed`,
241
+ this.loggingMetadata
211
242
  );
212
243
  this.disconnectionGrace = setTimeout(() => {
213
244
  this.close();
@@ -1,7 +1,8 @@
1
1
  import { Static } from '@sinclair/typebox';
2
- import { P as PayloadType, R as RiverError, a as Procedure, S as ServiceContext, b as Result, c as RiverUncaughtSchema, d as ProcedureResult } from '../procedures-1c0d2eee.js';
3
- import { P as PartialTransportMessage, T as Transport, C as Connection, O as OpaqueTransportMessage, S as SessionOptions, a as Session } from '../index-9e300e8a.js';
2
+ import { P as PayloadType, R as RiverError, a as Procedure, S as ServiceContext, b as Result, c as RiverUncaughtSchema, d as ProcedureResult } from '../procedures-74a10937.js';
3
+ import { T as Transport, C as Connection, S as SessionOptions, a as Session } from '../index-314e676a.js';
4
4
  import * as it_pushable from 'it-pushable';
5
+ import { P as PartialTransportMessage, O as OpaqueTransportMessage } from '../index-6118cd48.js';
5
6
  import WebSocket from 'isomorphic-ws';
6
7
  import http from 'node:http';
7
8
  import net from 'node:net';
@@ -36,7 +37,7 @@ declare function createLocalWebSocketClient(port: number): WebSocket;
36
37
  * @returns A promise that resolves to the next value from the iterator.
37
38
  */
38
39
  declare function iterNext<T>(iter: AsyncIterableIterator<T>): Promise<T>;
39
- declare function payloadToTransportMessage<Payload extends Record<string, unknown>>(payload: Payload): PartialTransportMessage<Payload>;
40
+ declare function payloadToTransportMessage<Payload>(payload: Payload): PartialTransportMessage<Payload>;
40
41
  declare function createDummyTransportMessage(): PartialTransportMessage<{
41
42
  msg: string;
42
43
  test: number;
@@ -1,7 +1,8 @@
1
1
  import { Static } from '@sinclair/typebox';
2
- import { P as PayloadType, R as RiverError, a as Procedure, S as ServiceContext, b as Result, c as RiverUncaughtSchema, d as ProcedureResult } from '../procedures-1c0d2eee.js';
3
- import { P as PartialTransportMessage, T as Transport, C as Connection, O as OpaqueTransportMessage, S as SessionOptions, a as Session } from '../index-9e300e8a.js';
2
+ import { P as PayloadType, R as RiverError, a as Procedure, S as ServiceContext, b as Result, c as RiverUncaughtSchema, d as ProcedureResult } from '../procedures-74a10937.js';
3
+ import { T as Transport, C as Connection, S as SessionOptions, a as Session } from '../index-314e676a.js';
4
4
  import * as it_pushable from 'it-pushable';
5
+ import { P as PartialTransportMessage, O as OpaqueTransportMessage } from '../index-6118cd48.js';
5
6
  import WebSocket from 'isomorphic-ws';
6
7
  import http from 'node:http';
7
8
  import net from 'node:net';
@@ -36,7 +37,7 @@ declare function createLocalWebSocketClient(port: number): WebSocket;
36
37
  * @returns A promise that resolves to the next value from the iterator.
37
38
  */
38
39
  declare function iterNext<T>(iter: AsyncIterableIterator<T>): Promise<T>;
39
- declare function payloadToTransportMessage<Payload extends Record<string, unknown>>(payload: Payload): PartialTransportMessage<Payload>;
40
+ declare function payloadToTransportMessage<Payload>(payload: Payload): PartialTransportMessage<Payload>;
40
41
  declare function createDummyTransportMessage(): PartialTransportMessage<{
41
42
  msg: string;
42
43
  test: number;
@@ -1,16 +1,16 @@
1
1
  import {
2
2
  UNCAUGHT_ERROR,
3
3
  pushable
4
- } from "../chunk-4C2OXQJB.js";
4
+ } from "../chunk-TIFNW5GQ.js";
5
5
  import "../chunk-RPIDSIQG.js";
6
6
  import {
7
7
  Session,
8
8
  defaultTransportOptions
9
- } from "../chunk-7WY3Z5ZN.js";
9
+ } from "../chunk-CLY7AQ25.js";
10
10
  import {
11
11
  coerceErrorString
12
12
  } from "../chunk-VH3NGOXQ.js";
13
- import "../chunk-H4BYJELI.js";
13
+ import "../chunk-YITXOAPA.js";
14
14
  import "../chunk-GZ7HCLLM.js";
15
15
 
16
16
  // util/testHelpers.ts
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@replit/river",
3
3
  "description": "It's like tRPC but... with JSON Schema Support, duplex streaming and support for service multiplexing. Transport agnostic!",
4
- "version": "0.17.4",
4
+ "version": "0.18.0",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  ".": {
@@ -1,37 +0,0 @@
1
- // logging/index.ts
2
- var LoggingLevels = {
3
- debug: -1,
4
- info: 0,
5
- warn: 1,
6
- error: 2
7
- };
8
- var log;
9
- var defaultLoggingLevel = "info";
10
- function bindLogger(write, color) {
11
- const debug = color ? "\x1B[37mdebug\x1B[0m" : "debug";
12
- const info = color ? "\x1B[37minfo\x1B[0m" : "info";
13
- const warn = color ? "\x1B[33mwarn\x1B[0m" : "warn";
14
- const error = color ? "\x1B[31merr\x1B[0m" : "err";
15
- log = {
16
- debug: (msg) => log && LoggingLevels[log.minLevel] <= -1 && write(`[river:${debug}] ${msg}`),
17
- info: (msg) => log && LoggingLevels[log.minLevel] <= 0 && write(`[river:${info}] ${msg}`),
18
- warn: (msg) => log && LoggingLevels[log.minLevel] <= 1 && write(`[river:${warn}] ${msg}`),
19
- error: (msg) => log && LoggingLevels[log.minLevel] <= 2 && write(`[river:${error}] ${msg}`),
20
- minLevel: log?.minLevel ?? defaultLoggingLevel
21
- };
22
- }
23
- function unbindLogger() {
24
- log = void 0;
25
- }
26
- function setLevel(level) {
27
- if (log) {
28
- log.minLevel = level;
29
- }
30
- }
31
-
32
- export {
33
- log,
34
- bindLogger,
35
- unbindLogger,
36
- setLevel
37
- };