Skip to content
Open
Show file tree
Hide file tree
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
22 changes: 20 additions & 2 deletions packages/computer/src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@

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

// The handles the Workspace owns and injects into a backend when
// it connects. The Workspace constructor builds `db`/`fs` itself,
// after the caller has already constructed the backends and passed
// them in, so a backend cannot capture them at its own
// construction time. `connect(host)` is how the Workspace hands
// over the storage-layer handles once they exist. Shell and
// container backends ignore the bag (they reach the host through
// their own transport); the in-process module backend uses it.
export interface WorkspaceBackendHost {
readonly db: import("@cloudflare/dofs").Database;
readonly waitUntil?: (promise: Promise<unknown>) => void;
readonly fs: import("@cloudflare/dofs").WorkspaceFilesystem;
readonly git: import("./git/index.js").GitClient;
readonly artifacts: import("./artifacts/index.js").ArtifactClient;
}

export interface WorkspaceBackend {
// User-supplied selector. Passed in `ExecOptions.backend` and
// `Workspace.push(id)` / `Workspace.pull(id)` to address this
Expand Down Expand Up @@ -48,8 +64,10 @@ export interface WorkspaceBackend {
// Materialise a connection. Called lazily on first use, once
// per backend per workspace lifetime. The Workspace caches the
// resulting handle by `id`; subsequent exec / push / pull
// calls reuse it.
connect(): Promise<BackendHandle>;
// calls reuse it. The Workspace always passes its host bag;
// backends that reach the host through their own transport
// ignore it.
connect(host: WorkspaceBackendHost): Promise<BackendHandle>;
}

export interface BackendHandle {
Expand Down
9 changes: 1 addition & 8 deletions packages/computer/src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,7 @@ export interface WorkspaceModuleBackendHandle {
close(): Promise<void>;
}

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

export interface WorkspaceModuleBackend {
readonly protocol: "module";
Expand Down
9 changes: 8 additions & 1 deletion packages/computer/src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,14 @@ export class Workspace {
this.#observer,
"workspace.connect",
{ "workspace.backend.id": id, "workspace.backend.type": backend.type },
() => backend.connect(),
() =>
backend.connect({
db: this.#db,
waitUntil: this.#waitUntil,
fs: this.#fs,
git: this.git,
artifacts: this.#artifacts,
}),
);
if (generation !== this.#connectionGeneration) {
await handle.close().catch(() => undefined);
Expand Down