@rivetkit/engine-runner 2.1.6 → 2.1.7

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
@@ -461,6 +461,10 @@ function stringifyKvRequestData(data) {
461
461
  const { keys } = data.val;
462
462
  return `KvDeleteRequest{keys: ${keys.length}}`;
463
463
  }
464
+ case "KvDeleteRangeRequest": {
465
+ const { start, end } = data.val;
466
+ return `KvDeleteRangeRequest{start: ${stringifyArrayBuffer(start)}, end: ${stringifyArrayBuffer(end)}}`;
467
+ }
464
468
  case "KvDropRequest":
465
469
  return "KvDropRequest";
466
470
  }
@@ -1506,8 +1510,11 @@ async function importWebSocket() {
1506
1510
  }
1507
1511
 
1508
1512
  // src/mod.ts
1513
+
1514
+
1515
+ var _uuid = require('uuid');
1509
1516
  var KV_EXPIRE = 3e4;
1510
- var PROTOCOL_VERSION = 6;
1517
+ var PROTOCOL_VERSION = 7;
1511
1518
  var EVENT_BACKLOG_WARN_THRESHOLD = 1e4;
1512
1519
  var SIGNAL_HANDLERS = [];
1513
1520
  var RunnerShutdownError = class extends Error {
@@ -1517,6 +1524,7 @@ var RunnerShutdownError = class extends Error {
1517
1524
  };
1518
1525
  var Runner = class {
1519
1526
  #config;
1527
+ #runnerKey = _uuid.v4.call(void 0, );
1520
1528
  get config() {
1521
1529
  return this.#config;
1522
1530
  }
@@ -1913,7 +1921,7 @@ var Runner = class {
1913
1921
  get pegboardUrl() {
1914
1922
  const wsEndpoint = this.pegboardEndpoint.replace("http://", "ws://").replace("https://", "wss://");
1915
1923
  const baseUrl = wsEndpoint.endsWith("/") ? wsEndpoint.slice(0, -1) : wsEndpoint;
1916
- return `${baseUrl}/runners/connect?protocol_version=${PROTOCOL_VERSION}&namespace=${encodeURIComponent(this.#config.namespace)}&runner_key=${encodeURIComponent(this.#config.runnerKey)}`;
1924
+ return `${baseUrl}/runners/connect?protocol_version=${PROTOCOL_VERSION}&namespace=${encodeURIComponent(this.#config.namespace)}&runner_key=${encodeURIComponent(this.#runnerKey)}`;
1917
1925
  }
1918
1926
  // MARK: Runner protocol
1919
1927
  async #openPegboardWebSocket() {
@@ -1934,7 +1942,7 @@ var Runner = class {
1934
1942
  msg: "connecting",
1935
1943
  endpoint: this.pegboardEndpoint,
1936
1944
  namespace: this.#config.namespace,
1937
- runnerKey: this.#config.runnerKey,
1945
+ runnerKey: this.#runnerKey,
1938
1946
  hasToken: !!this.config.token
1939
1947
  });
1940
1948
  ws.addEventListener("open", () => {
@@ -2011,6 +2019,7 @@ var Runner = class {
2011
2019
  } else {
2012
2020
  throw new Error(`expected binary data, got ${typeof ev.data}`);
2013
2021
  }
2022
+ await this.#injectLatency();
2014
2023
  const message = protocol.decodeToClient(buf);
2015
2024
  (_a2 = this.log) == null ? void 0 : _a2.debug({
2016
2025
  msg: "received runner message",
@@ -2570,6 +2579,24 @@ var Runner = class {
2570
2579
  };
2571
2580
  await this.#sendKvRequest(actorId, requestData);
2572
2581
  }
2582
+ async kvDeleteRange(actorId, start, end) {
2583
+ const startKey = start.buffer.slice(
2584
+ start.byteOffset,
2585
+ start.byteOffset + start.byteLength
2586
+ );
2587
+ const endKey = end.buffer.slice(
2588
+ end.byteOffset,
2589
+ end.byteOffset + end.byteLength
2590
+ );
2591
+ const requestData = {
2592
+ tag: "KvDeleteRangeRequest",
2593
+ val: {
2594
+ start: startKey,
2595
+ end: endKey
2596
+ }
2597
+ };
2598
+ await this.#sendKvRequest(actorId, requestData);
2599
+ }
2573
2600
  async kvDrop(actorId) {
2574
2601
  const requestData = {
2575
2602
  tag: "KvDropRequest",
@@ -2655,6 +2682,12 @@ var Runner = class {
2655
2682
  if (processedCount > 0) {
2656
2683
  }
2657
2684
  }
2685
+ /** Resolves after the configured debug latency, or immediately if none. */
2686
+ #injectLatency() {
2687
+ const ms = this.#config.debugLatencyMs;
2688
+ if (!ms) return Promise.resolve();
2689
+ return new Promise((resolve) => setTimeout(resolve, ms));
2690
+ }
2658
2691
  /** Asserts WebSocket exists and is ready. */
2659
2692
  getPegboardWebSocketIfReady() {
2660
2693
  if (!!this.#pegboardWebSocket && this.#pegboardWebSocket.readyState === 1) {
@@ -2664,20 +2697,23 @@ var Runner = class {
2664
2697
  }
2665
2698
  }
2666
2699
  __sendToServer(message) {
2667
- var _a, _b;
2700
+ var _a;
2668
2701
  (_a = this.log) == null ? void 0 : _a.debug({
2669
2702
  msg: "sending runner message",
2670
2703
  data: stringifyToServer(message)
2671
2704
  });
2672
2705
  const encoded = protocol.encodeToServer(message);
2673
- const pegboardWebSocket = this.getPegboardWebSocketIfReady();
2674
- if (pegboardWebSocket) {
2675
- pegboardWebSocket.send(encoded);
2676
- } else {
2677
- (_b = this.log) == null ? void 0 : _b.error({
2678
- msg: "WebSocket not available or not open for sending data"
2679
- });
2680
- }
2706
+ this.#injectLatency().then(() => {
2707
+ var _a2;
2708
+ const pegboardWebSocket = this.getPegboardWebSocketIfReady();
2709
+ if (pegboardWebSocket) {
2710
+ pegboardWebSocket.send(encoded);
2711
+ } else {
2712
+ (_a2 = this.log) == null ? void 0 : _a2.error({
2713
+ msg: "WebSocket not available or not open for sending data"
2714
+ });
2715
+ }
2716
+ });
2681
2717
  }
2682
2718
  sendHibernatableWebSocketMessageAck(gatewayId, requestId, index) {
2683
2719
  if (!this.#tunnel)