@rivetkit/engine-runner 25.7.2-rc.1 → 25.7.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rivetkit/engine-runner",
3
- "version": "25.7.2-rc.1",
3
+ "version": "25.7.3",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  "import": {
@@ -16,16 +16,16 @@
16
16
  "uuid": "^12.0.0",
17
17
  "pino": "^9.9.5",
18
18
  "ws": "^8.18.3",
19
- "@rivetkit/engine-runner-protocol": "25.7.2-rc.1"
19
+ "@rivetkit/engine-runner-protocol": "25.7.3"
20
20
  },
21
21
  "devDependencies": {
22
- "@types/node": "^22.13.9",
22
+ "@types/node": "^22.18.1",
23
23
  "@types/ws": "^8.18.1",
24
- "tinybench": "^5.0.0",
24
+ "tinybench": "^5.0.1",
25
25
  "tsup": "^8.5.0",
26
- "tsx": "^4.19.3",
27
- "typescript": "^5.3.3",
28
- "vitest": "^1.6.0"
26
+ "tsx": "^4.20.5",
27
+ "typescript": "^5.9.2",
28
+ "vitest": "^1.6.1"
29
29
  },
30
30
  "scripts": {
31
31
  "build": "tsup src/mod.ts",
package/src/mod.ts CHANGED
@@ -8,6 +8,7 @@ import type { Logger } from "pino";
8
8
  import { setLogger, logger } from "./log.js";
9
9
 
10
10
  const KV_EXPIRE: number = 30_000;
11
+ const PROTOCOL_VERSION: number = 1;
11
12
 
12
13
  export interface ActorInstance {
13
14
  actorId: string;
@@ -28,6 +29,7 @@ export interface RunnerConfig {
28
29
  logger?: Logger;
29
30
  version: number;
30
31
  endpoint: string;
32
+ token?: string;
31
33
  pegboardEndpoint?: string;
32
34
  pegboardRelayEndpoint?: string;
33
35
  namespace: string;
@@ -393,7 +395,7 @@ export class Runner {
393
395
  const wsEndpoint = endpoint
394
396
  .replace("http://", "ws://")
395
397
  .replace("https://", "wss://");
396
- return `${wsEndpoint}?protocol_version=1&namespace=${encodeURIComponent(this.#config.namespace)}&runner_key=${encodeURIComponent(this.#config.runnerKey)}`;
398
+ return `${wsEndpoint}?protocol_version=${PROTOCOL_VERSION}&namespace=${encodeURIComponent(this.#config.namespace)}&runner_key=${encodeURIComponent(this.#config.runnerKey)}`;
397
399
  }
398
400
 
399
401
  get pegboardTunnelUrl() {
@@ -404,17 +406,16 @@ export class Runner {
404
406
  const wsEndpoint = endpoint
405
407
  .replace("http://", "ws://")
406
408
  .replace("https://", "wss://");
407
- return `${wsEndpoint}?protocol_version=1&namespace=${encodeURIComponent(this.#config.namespace)}&runner_name=${encodeURIComponent(this.#config.runnerName)}&runner_key=${encodeURIComponent(this.#config.runnerKey)}`;
409
+ return `${wsEndpoint}?protocol_version=${PROTOCOL_VERSION}&namespace=${encodeURIComponent(this.#config.namespace)}&runner_name=${encodeURIComponent(this.#config.runnerName)}&runner_key=${encodeURIComponent(this.#config.runnerKey)}`;
408
410
  }
409
411
 
410
412
  // MARK: Runner protocol
411
413
  async #openPegboardWebSocket() {
414
+ const protocols = ["rivet", `rivet_target.runner`];
415
+ if (this.config.token) protocols.push(`rivet_token.${this.config.token}`);
416
+
412
417
  const WS = await importWebSocket();
413
- const ws = new WS(this.pegboardUrl, {
414
- headers: {
415
- "x-rivet-target": "runner",
416
- },
417
- }) as any as WebSocket;
418
+ const ws = new WS(this.pegboardUrl, protocols) as any as WebSocket;
418
419
  this.#pegboardWebSocket = ws;
419
420
 
420
421
  ws.addEventListener("open", () => {
@@ -538,14 +539,35 @@ export class Runner {
538
539
  } else if (message.tag === "ToClientTunnelMessage") {
539
540
  this.#tunnel?.handleTunnelMessage(message.val);
540
541
  } else if (message.tag === "ToClientClose") {
541
- // TODO: Close ws
542
+ this.#tunnel.shutdown();
543
+ ws.close(1000, "manual closure");
542
544
  } else {
543
545
  unreachable(message);
544
546
  }
545
547
  });
546
548
 
547
549
  ws.addEventListener("error", (ev) => {
548
- logger()?.error("WebSocket error:", ev.error);
550
+ logger()?.error(`WebSocket error: ${ev.error}`);
551
+
552
+ if (!this.#shutdown) {
553
+ // Start runner lost timeout if we have a threshold and are not shutting down
554
+ if (
555
+ !this.#runnerLostTimeout &&
556
+ this.#runnerLostThreshold &&
557
+ this.#runnerLostThreshold > 0
558
+ ) {
559
+ logger()?.info({
560
+ msg: "starting runner lost timeout",
561
+ seconds: this.#runnerLostThreshold / 1000,
562
+ });
563
+ this.#runnerLostTimeout = setTimeout(() => {
564
+ this.#stopAllActors();
565
+ }, this.#runnerLostThreshold);
566
+ }
567
+
568
+ // Attempt to reconnect if not stopped
569
+ this.#scheduleReconnect();
570
+ }
549
571
  });
550
572
 
551
573
  ws.addEventListener("close", (ev) => {
@@ -572,6 +594,7 @@ export class Runner {
572
594
  if (!this.#shutdown) {
573
595
  // Start runner lost timeout if we have a threshold and are not shutting down
574
596
  if (
597
+ !this.#runnerLostTimeout &&
575
598
  this.#runnerLostThreshold &&
576
599
  this.#runnerLostThreshold > 0
577
600
  ) {
@@ -1242,9 +1265,27 @@ export class Runner {
1242
1265
  }
1243
1266
  }
1244
1267
 
1268
+ getServerlessInitPacket(): string | undefined {
1269
+ if (!this.runnerId) return undefined;
1270
+
1271
+ let data = protocol.encodeToServerlessServer({
1272
+ tag: "ToServerlessServerInit",
1273
+ val: {
1274
+ runnerId: this.runnerId,
1275
+ }
1276
+ });
1277
+
1278
+ // Embed version
1279
+ const buffer = Buffer.alloc(data.length + 2);
1280
+ buffer.writeUInt16LE(PROTOCOL_VERSION, 0);
1281
+ Buffer.from(data).copy(buffer, 2);
1282
+
1283
+ return buffer.toString('base64');
1284
+ }
1285
+
1245
1286
  #scheduleReconnect() {
1246
1287
  if (this.#shutdown) {
1247
- //logger()?.log("Runner is shut down, not attempting reconnect");
1288
+ logger()?.debug("Runner is shut down, not attempting reconnect");
1248
1289
  return;
1249
1290
  }
1250
1291
 
@@ -1255,16 +1296,16 @@ export class Runner {
1255
1296
  jitter: true,
1256
1297
  });
1257
1298
 
1258
- //logger()?.log(
1259
- // `Scheduling reconnect attempt ${this.#reconnectAttempt + 1} in ${delay}ms`,
1260
- //);
1299
+ logger()?.debug(
1300
+ `Scheduling reconnect attempt ${this.#reconnectAttempt + 1} in ${delay}ms`,
1301
+ );
1261
1302
 
1262
1303
  this.#reconnectTimeout = setTimeout(async () => {
1263
1304
  if (!this.#shutdown) {
1264
1305
  this.#reconnectAttempt++;
1265
- //logger()?.log(
1266
- // `Attempting to reconnect (attempt ${this.#reconnectAttempt})...`,
1267
- //);
1306
+ logger()?.debug(
1307
+ `Attempting to reconnect (attempt ${this.#reconnectAttempt})...`,
1308
+ );
1268
1309
  await this.#openPegboardWebSocket();
1269
1310
  }
1270
1311
  }, delay);
@@ -1,23 +0,0 @@
1
-
2
- 
3
- > @rivetkit/engine-runner@25.7.1 build /Users/nathan/engine/sdks/typescript/runner
4
- > tsup src/mod.ts
5
-
6
- CLI Building entry: src/mod.ts
7
- CLI Using tsconfig: tsconfig.json
8
- CLI tsup v8.5.0
9
- CLI Using tsup config: /Users/nathan/engine/sdks/typescript/runner/tsup.config.ts
10
- CLI Target: node16
11
- CLI Cleaning output folder
12
- CJS Build start
13
- ESM Build start
14
- ESM dist/mod.js 53.27 KB
15
- ESM dist/mod.js.map 103.94 KB
16
- ESM ⚡️ Build success in 113ms
17
- CJS dist/mod.cjs 54.16 KB
18
- CJS dist/mod.cjs.map 88.80 KB
19
- CJS ⚡️ Build success in 115ms
20
- DTS Build start
21
- DTS ⚡️ Build success in 781ms
22
- DTS dist/mod.d.cts 2.71 KB
23
- DTS dist/mod.d.ts 2.71 KB
@@ -1,5 +0,0 @@
1
-
2
- 
3
- > @rivetkit/engine-runner@25.6.1 check-types /Users/nathan/engine/sdks/typescript/runner
4
- > tsc --noEmit
5
-