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

Class WorkspaceRuntimeCapability

packages/computer/src/runtime/capability.ts:7–239  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5} from "./types.js";
6
7export class WorkspaceRuntimeCapability {
8 readonly #fs: WorkspaceRuntimeFilesystem;
9 readonly #root: string;
10 readonly #access: WorkspaceRuntimeAccess;
11 readonly #maxReadBytes: number;
12 readonly #maxDirectoryEntries: number;
13
14 constructor(
15 fs: WorkspaceRuntimeFilesystem,
16 root: string,
17 access: WorkspaceRuntimeAccess,
18 maxReadBytes = 1024 * 1024,
19 maxDirectoryEntries = 1024,
20 ) {
21 this.#fs = fs;
22 this.#root = normalizeRoot(root);
23 this.#access = access;
24 this.#maxReadBytes = maxReadBytes;
25 this.#maxDirectoryEntries = maxDirectoryEntries;
26 }
27
28 get access(): WorkspaceRuntimeAccess {
29 return this.#access;
30 }
31
32 async readFile(path: string) {
33 const resolved = await this.#resolveSafe(path);
34 await this.#assertReadableSize(resolved);
35 return this.#fs.readFile(resolved, "utf8");
36 }
37
38 async readFileBytes(path: string) {
39 const resolved = await this.#resolveSafe(path);
40 await this.#assertReadableSize(resolved);
41 const stream = await this.#fs.readFile(resolved);
42 const reader = stream.getReader();
43 const chunks: Uint8Array[] = [];
44 let size = 0;
45 try {
46 while (true) {
47 const { done, value } = await reader.read();
48 if (done) break;
49 size += value.byteLength;
50 if (size > this.#maxReadBytes) {
51 await reader.cancel("Workspace runtime file read limit exceeded");
52 throw new Error(`Workspace runtime file read exceeds ${this.#maxReadBytes} bytes.`);
53 }
54 chunks.push(value);
55 }
56 } finally {
57 reader.releaseLock();
58 }
59 const bytes = new Uint8Array(size);
60 let offset = 0;
61 for (const chunk of chunks) {
62 bytes.set(chunk, offset);
63 offset += chunk.byteLength;
64 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected