MCPcopy Create free account
hub / github.com/cloudflare/computer / WorkspaceFilesystemStub

Class WorkspaceFilesystemStub

packages/computer/src/stub.ts:90–278  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

88// WriteFileContent union includes ReadableStream<Uint8Array> for
89// the same reason.
90export class WorkspaceFilesystemStub extends RpcTarget {
91 readonly #ws: Workspace;
92
93 constructor(ws: Workspace) {
94 super();
95 this.#ws = ws;
96 trackStub(this);
97 }
98
99 [Symbol.dispose](): void {
100 untrackStub(this);
101 }
102
103 // --- Reads -------------------------------------------------------
104
105 readFile(path: string): Promise<ReadableStream<Uint8Array>>;
106 readFile(path: string, encoding: "utf8"): Promise<string>;
107 readFile(path: string, options: ReadFileOptions): Promise<string | ReadableStream<Uint8Array>>;
108 readFile(
109 path: string,
110 optionsOrEncoding?: "utf8" | ReadFileOptions,
111 ): Promise<string | ReadableStream<Uint8Array>> {
112 return withSpan(this.#ws.observer, "workspace.fs.readFile", { "workspace.fs.path": path }, () =>
113 this.#ws.fs.readFile(path, optionsOrEncoding as ReadFileOptions),
114 );
115 }
116
117 exists(path: string): Promise<boolean> {
118 return withSpan(
119 this.#ws.observer,
120 "workspace.fs.exists",
121 { "workspace.fs.path": path },
122 async () => (await this.statOrNull(path)) !== null,
123 );
124 }
125
126 stat(path: string): Promise<WorkspaceStatResult> {
127 return withSpan(this.#ws.observer, "workspace.fs.stat", { "workspace.fs.path": path }, () =>
128 this.#ws.fs.stat(path),
129 );
130 }
131
132 async statOrNull(path: string): Promise<WorkspaceStatResult | null> {
133 return withSpan(
134 this.#ws.observer,
135 "workspace.fs.statOrNull",
136 { "workspace.fs.path": path },
137 async () => {
138 try {
139 return await this.#ws.fs.stat(path);
140 } catch (error) {
141 if ((error as { code?: string }).code === "ENOENT") return null;
142 throw error;
143 }
144 },
145 );
146 }
147

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected