@rivetkit/engine-runner 2.1.3 → 2.1.5

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.
package/dist/mod.cjs CHANGED
@@ -11,6 +11,7 @@ function logger() {
11
11
  }
12
12
 
13
13
  // src/utils.ts
14
+ var MAX_PAYLOAD_SIZE = 20 * 1024 * 1024;
14
15
  function unreachable(x) {
15
16
  throw `Unreachable: ${x}`;
16
17
  }
@@ -553,11 +554,17 @@ var WebSocketTunnelAdapter = class {
553
554
  let isBinary = false;
554
555
  let messageData;
555
556
  if (typeof data === "string") {
557
+ const encoder = new TextEncoder();
558
+ if (encoder.encode(data).byteLength > MAX_PAYLOAD_SIZE) {
559
+ throw new Error("WebSocket message too large");
560
+ }
556
561
  messageData = data;
557
562
  } else if (data instanceof ArrayBuffer) {
563
+ if (data.byteLength > MAX_PAYLOAD_SIZE) throw new Error("WebSocket message too large");
558
564
  isBinary = true;
559
565
  messageData = data;
560
566
  } else if (ArrayBuffer.isView(data)) {
567
+ if (data.byteLength > MAX_PAYLOAD_SIZE) throw new Error("WebSocket message too large");
561
568
  isBinary = true;
562
569
  const view = data;
563
570
  const buffer = view.buffer instanceof SharedArrayBuffer ? new Uint8Array(view.buffer, view.byteOffset, view.byteLength).slice().buffer : view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength);
@@ -1255,6 +1262,9 @@ var Tunnel = class {
1255
1262
  return;
1256
1263
  }
1257
1264
  const body = response.body ? await response.arrayBuffer() : null;
1265
+ if (body && body.byteLength > MAX_PAYLOAD_SIZE) {
1266
+ throw new Error("Response body too large");
1267
+ }
1258
1268
  const headers = /* @__PURE__ */ new Map();
1259
1269
  response.headers.forEach((value, key) => {
1260
1270
  headers.set(key, value);
@@ -1402,7 +1412,7 @@ var Tunnel = class {
1402
1412
  index: clientMessageIndex
1403
1413
  });
1404
1414
  if (clientMessageIndex < 0 || clientMessageIndex > 65535)
1405
- throw new Error("invalid websocket ack index");
1415
+ throw new Error("Invalid websocket ack index");
1406
1416
  const actor = this.getRequestActor(gatewayId, requestId);
1407
1417
  if (!actor) {
1408
1418
  (_b = this.log) == null ? void 0 : _b.warn({
@@ -1453,7 +1463,7 @@ function buildRequestForWebSocket(path, headers) {
1453
1463
  Connection: "Upgrade"
1454
1464
  };
1455
1465
  if (!path.startsWith("/")) {
1456
- throw new Error("path must start with leading slash");
1466
+ throw new Error("Path must start with leading slash");
1457
1467
  }
1458
1468
  const request = new Request(`http://actor${path}`, {
1459
1469
  method: "GET",
@@ -1497,7 +1507,7 @@ async function importWebSocket() {
1497
1507
 
1498
1508
  // src/mod.ts
1499
1509
  var KV_EXPIRE = 3e4;
1500
- var PROTOCOL_VERSION = 5;
1510
+ var PROTOCOL_VERSION = 6;
1501
1511
  var EVENT_BACKLOG_WARN_THRESHOLD = 1e4;
1502
1512
  var SIGNAL_HANDLERS = [];
1503
1513
  var RunnerShutdownError = class extends Error {
@@ -1518,8 +1528,9 @@ var Runner = class {
1518
1528
  #shutdown = false;
1519
1529
  #reconnectAttempt = 0;
1520
1530
  #reconnectTimeout;
1531
+ // Protocol metadata
1532
+ #protocolMetadata;
1521
1533
  // Runner lost threshold management
1522
- #runnerLostThreshold;
1523
1534
  #runnerLostTimeout;
1524
1535
  // Event storage for resending
1525
1536
  #eventBacklogWarned = false;
@@ -1988,7 +1999,7 @@ var Runner = class {
1988
1999
  this.#ackInterval = ackLoop;
1989
2000
  });
1990
2001
  ws.addEventListener("message", async (ev) => {
1991
- var _a2, _b2, _c, _d, _e;
2002
+ var _a2, _b2, _c, _d;
1992
2003
  let buf;
1993
2004
  if (ev.data instanceof Blob) {
1994
2005
  buf = new Uint8Array(await ev.data.arrayBuffer());
@@ -2008,14 +2019,14 @@ var Runner = class {
2008
2019
  this.runnerId = init.runnerId;
2009
2020
  this.#stopAllActors();
2010
2021
  }
2011
- this.#runnerLostThreshold = ((_b2 = init.metadata) == null ? void 0 : _b2.runnerLostThreshold) ? Number(init.metadata.runnerLostThreshold) : void 0;
2012
- (_c = this.log) == null ? void 0 : _c.info({
2022
+ this.#protocolMetadata = init.metadata;
2023
+ (_b2 = this.log) == null ? void 0 : _b2.info({
2013
2024
  msg: "received init",
2014
- runnerLostThreshold: this.#runnerLostThreshold
2025
+ protocolMetadata: this.#protocolMetadata
2015
2026
  });
2016
2027
  this.#processUnsentKvRequests();
2017
2028
  this.#resendUnacknowledgedEvents();
2018
- (_d = this.#tunnel) == null ? void 0 : _d.resendBufferedEvents();
2029
+ (_c = this.#tunnel) == null ? void 0 : _c.resendBufferedEvents();
2019
2030
  this.#config.onConnected();
2020
2031
  } else if (message.tag === "ToClientCommands") {
2021
2032
  const commands = message.val;
@@ -2026,7 +2037,7 @@ var Runner = class {
2026
2037
  const kvResponse = message.val;
2027
2038
  this.#handleKvResponse(kvResponse);
2028
2039
  } else if (message.tag === "ToClientTunnelMessage") {
2029
- (_e = this.#tunnel) == null ? void 0 : _e.handleTunnelMessage(message.val).catch((err) => {
2040
+ (_d = this.#tunnel) == null ? void 0 : _d.handleTunnelMessage(message.val).catch((err) => {
2030
2041
  var _a3;
2031
2042
  (_a3 = this.log) == null ? void 0 : _a3.error({
2032
2043
  msg: "error handling tunnel message",
@@ -2045,33 +2056,17 @@ var Runner = class {
2045
2056
  }
2046
2057
  });
2047
2058
  ws.addEventListener("error", (ev) => {
2048
- var _a2, _b2;
2059
+ var _a2;
2049
2060
  (_a2 = this.log) == null ? void 0 : _a2.error({
2050
2061
  msg: `WebSocket error: ${stringifyError(ev.error)}`
2051
2062
  });
2052
2063
  if (!this.#shutdown) {
2053
- if (!this.#runnerLostTimeout && this.#runnerLostThreshold && this.#runnerLostThreshold > 0) {
2054
- (_b2 = this.log) == null ? void 0 : _b2.info({
2055
- msg: "starting runner lost timeout",
2056
- seconds: this.#runnerLostThreshold / 1e3
2057
- });
2058
- this.#runnerLostTimeout = setTimeout(() => {
2059
- var _a3;
2060
- try {
2061
- this.#handleLost();
2062
- } catch (err) {
2063
- (_a3 = this.log) == null ? void 0 : _a3.error({
2064
- msg: "error handling runner lost",
2065
- error: stringifyError(err)
2066
- });
2067
- }
2068
- }, this.#runnerLostThreshold);
2069
- }
2064
+ this.#startRunnerLostTimeout();
2070
2065
  this.#scheduleReconnect();
2071
2066
  }
2072
2067
  });
2073
2068
  ws.addEventListener("close", async (ev) => {
2074
- var _a2, _b2, _c, _d;
2069
+ var _a2, _b2, _c;
2075
2070
  if (!this.#shutdown) {
2076
2071
  const closeError = parseWebSocketCloseReason(ev.reason);
2077
2072
  if ((closeError == null ? void 0 : closeError.group) === "ws" && (closeError == null ? void 0 : closeError.error) === "eviction") {
@@ -2091,30 +2086,34 @@ var Runner = class {
2091
2086
  clearInterval(this.#ackInterval);
2092
2087
  this.#ackInterval = void 0;
2093
2088
  }
2094
- if (!this.#runnerLostTimeout && this.#runnerLostThreshold && this.#runnerLostThreshold > 0) {
2095
- (_c = this.log) == null ? void 0 : _c.info({
2096
- msg: "starting runner lost timeout",
2097
- seconds: this.#runnerLostThreshold / 1e3
2098
- });
2099
- this.#runnerLostTimeout = setTimeout(() => {
2100
- var _a3;
2101
- try {
2102
- this.#handleLost();
2103
- } catch (err) {
2104
- (_a3 = this.log) == null ? void 0 : _a3.error({
2105
- msg: "error handling runner lost",
2106
- error: stringifyError(err)
2107
- });
2108
- }
2109
- }, this.#runnerLostThreshold);
2110
- }
2089
+ this.#startRunnerLostTimeout();
2111
2090
  this.#scheduleReconnect();
2112
2091
  } else {
2113
- (_d = this.log) == null ? void 0 : _d.info("websocket closed");
2092
+ (_c = this.log) == null ? void 0 : _c.info("websocket closed");
2114
2093
  this.#config.onDisconnected(ev.code, ev.reason);
2115
2094
  }
2116
2095
  });
2117
2096
  }
2097
+ #startRunnerLostTimeout() {
2098
+ var _a;
2099
+ if (!this.#runnerLostTimeout && this.#protocolMetadata && this.#protocolMetadata.runnerLostThreshold > 0) {
2100
+ (_a = this.log) == null ? void 0 : _a.info({
2101
+ msg: "starting runner lost timeout",
2102
+ seconds: this.#protocolMetadata.runnerLostThreshold / 1000n
2103
+ });
2104
+ this.#runnerLostTimeout = setTimeout(() => {
2105
+ var _a2;
2106
+ try {
2107
+ this.#handleLost();
2108
+ } catch (err) {
2109
+ (_a2 = this.log) == null ? void 0 : _a2.error({
2110
+ msg: "error handling runner lost",
2111
+ error: stringifyError(err)
2112
+ });
2113
+ }
2114
+ }, Number(this.#protocolMetadata.runnerLostThreshold));
2115
+ }
2116
+ }
2118
2117
  #handleCommands(commands) {
2119
2118
  var _a;
2120
2119
  (_a = this.log) == null ? void 0 : _a.info({
@@ -2803,6 +2802,9 @@ var Runner = class {
2803
2802
  if (toDelete.length > 0) {
2804
2803
  }
2805
2804
  }
2805
+ getProtocolMetadata() {
2806
+ return this.#protocolMetadata;
2807
+ }
2806
2808
  };
2807
2809
 
2808
2810