Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions engine/sdks/typescript/runner/src/tunnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { MessageId, RequestId } from "@rivetkit/engine-runner-protocol";
import type { Logger } from "pino";
import { stringify as uuidstringify, v4 as uuidv4 } from "uuid";
import { logger } from "./log";

Check warning on line 5 in engine/sdks/typescript/runner/src/tunnel.ts

View workflow job for this annotation

GitHub Actions / quality

lint/correctness/noUnusedImports

This import is unused.

Check warning on line 5 in engine/sdks/typescript/runner/src/tunnel.ts

View workflow job for this annotation

GitHub Actions / quality

lint/correctness/noUnusedImports

This import is unused.
import type { ActorInstance, Runner } from "./mod";
import {
stringifyToClientTunnelMessageKind,
Expand All @@ -13,7 +13,7 @@

const GC_INTERVAL = 60000; // 60 seconds
const MESSAGE_ACK_TIMEOUT = 5000; // 5 seconds
const WEBSOCKET_STATE_PERSIST_TIMEOUT = 30000; // 30 seconds

Check warning on line 16 in engine/sdks/typescript/runner/src/tunnel.ts

View workflow job for this annotation

GitHub Actions / quality

lint/correctness/noUnusedVariables

This variable WEBSOCKET_STATE_PERSIST_TIMEOUT is unused.

Check warning on line 16 in engine/sdks/typescript/runner/src/tunnel.ts

View workflow job for this annotation

GitHub Actions / quality

lint/correctness/noUnusedVariables

This variable WEBSOCKET_STATE_PERSIST_TIMEOUT is unused.

interface PendingRequest {
resolve: (response: Response) => void;
Expand Down Expand Up @@ -573,6 +573,24 @@
return;
}

// Close existing WebSocket if one already exists for this request ID.
// There should always be a close message sent before another open
// message for the same message ID.
//
// This should never occur if all is functioning correctly, but this
// prevents any edge case that would result in duplicate WebSockets for
// the same request.
const existingAdapter = this.#actorWebSockets.get(requestIdStr);
if (existingAdapter) {
this.log?.warn({
msg: "closing existing websocket for duplicate open event for the same request id",
requestId: requestIdStr,
});
// Close without sending a message through the tunnel since the server
// already knows about the new connection
existingAdapter.__closeWithoutCallback(1000, "ws.duplicate_open");
}

// Track this WebSocket for the actor
if (actor) {
actor.webSockets.add(requestIdStr);
Expand Down Expand Up @@ -635,8 +653,8 @@
headerInit[k] = v;
}
}
headerInit["Upgrade"] = "websocket";

Check notice on line 656 in engine/sdks/typescript/runner/src/tunnel.ts

View workflow job for this annotation

GitHub Actions / quality

lint/complexity/useLiteralKeys

The computed expression can be simplified without the use of a string literal.

Check notice on line 656 in engine/sdks/typescript/runner/src/tunnel.ts

View workflow job for this annotation

GitHub Actions / quality

lint/complexity/useLiteralKeys

The computed expression can be simplified without the use of a string literal.
headerInit["Connection"] = "Upgrade";

Check notice on line 657 in engine/sdks/typescript/runner/src/tunnel.ts

View workflow job for this annotation

GitHub Actions / quality

lint/complexity/useLiteralKeys

The computed expression can be simplified without the use of a string literal.

Check notice on line 657 in engine/sdks/typescript/runner/src/tunnel.ts

View workflow job for this annotation

GitHub Actions / quality

lint/complexity/useLiteralKeys

The computed expression can be simplified without the use of a string literal.

const request = new Request(`http://localhost${open.path}`, {
method: "GET",
Expand Down
Loading