Skip to content

Commit 741f0c9

Browse files
committed
computer: unify backend connect on a single host-injected signature
The shell and module backend interfaces took different connect signatures: the shell kind a bare connect(), the module kind a connect(host) receiving the storage-layer handles the Workspace owns. The split was historical, not necessary; the host bag exists only because the Workspace builds db and fs after the caller constructs the backends. Give WorkspaceBackend.connect the same host parameter and pass the bag on every connect. Shell and container backends ignore it and reach the host through their own transport; the module backend uses it as before. Method-parameter bivariance lets the existing zero-argument implementations satisfy the wider signature unchanged. Collapse the duplicate WorkspaceModuleBackendHost onto the canonical WorkspaceBackendHost so there is one definition.
1 parent 4e782d0 commit 741f0c9

3 files changed

Lines changed: 29 additions & 11 deletions

File tree

packages/computer/src/backend.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@
2020

2121
import type { WorkspaceRPC } from "@cloudflare/computer-rpc";
2222

23+
// The handles the Workspace owns and injects into a backend when
24+
// it connects. The Workspace constructor builds `db`/`fs` itself,
25+
// after the caller has already constructed the backends and passed
26+
// them in, so a backend cannot capture them at its own
27+
// construction time. `connect(host)` is how the Workspace hands
28+
// over the storage-layer handles once they exist. Shell and
29+
// container backends ignore the bag (they reach the host through
30+
// their own transport); the in-process module backend uses it.
31+
export interface WorkspaceBackendHost {
32+
readonly db: import("@cloudflare/dofs").Database;
33+
readonly waitUntil?: (promise: Promise<unknown>) => void;
34+
readonly fs: import("@cloudflare/dofs").WorkspaceFilesystem;
35+
readonly git: import("./git/index.js").GitClient;
36+
readonly artifacts: import("./artifacts/index.js").ArtifactClient;
37+
}
38+
2339
export interface WorkspaceBackend {
2440
// User-supplied selector. Passed in `ExecOptions.backend` and
2541
// `Workspace.push(id)` / `Workspace.pull(id)` to address this
@@ -48,8 +64,10 @@ export interface WorkspaceBackend {
4864
// Materialise a connection. Called lazily on first use, once
4965
// per backend per workspace lifetime. The Workspace caches the
5066
// resulting handle by `id`; subsequent exec / push / pull
51-
// calls reuse it.
52-
connect(): Promise<BackendHandle>;
67+
// calls reuse it. The Workspace always passes its host bag;
68+
// backends that reach the host through their own transport
69+
// ignore it.
70+
connect(host: WorkspaceBackendHost): Promise<BackendHandle>;
5371
}
5472

5573
export interface BackendHandle {

packages/computer/src/runtime/types.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,7 @@ export interface WorkspaceModuleBackendHandle {
160160
close(): Promise<void>;
161161
}
162162

163-
export interface WorkspaceModuleBackendHost {
164-
readonly db: import("@cloudflare/dofs").Database;
165-
/** Attach detached backend work to the host event lifetime. */
166-
readonly waitUntil?: (promise: Promise<unknown>) => void;
167-
readonly fs: import("@cloudflare/dofs").WorkspaceFilesystem;
168-
readonly git: import("../git/index.js").GitClient;
169-
readonly artifacts: import("../artifacts/index.js").ArtifactClient;
170-
}
163+
export type WorkspaceModuleBackendHost = import("../backend.js").WorkspaceBackendHost;
171164

172165
export interface WorkspaceModuleBackend {
173166
readonly protocol: "module";

packages/computer/src/workspace.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,14 @@ export class Workspace {
830830
this.#observer,
831831
"workspace.connect",
832832
{ "workspace.backend.id": id, "workspace.backend.type": backend.type },
833-
() => backend.connect(),
833+
() =>
834+
backend.connect({
835+
db: this.#db,
836+
waitUntil: this.#waitUntil,
837+
fs: this.#fs,
838+
git: this.git,
839+
artifacts: this.#artifacts,
840+
}),
834841
);
835842
if (generation !== this.#connectionGeneration) {
836843
await handle.close().catch(() => undefined);

0 commit comments

Comments
 (0)