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

Method readFileSync

packages/dofs/src/provider.ts:354–398  ·  view source on GitHub ↗
(
    path: string,
    options?: BufferEncoding | { encoding?: BufferEncoding | null } | null,
  )

Source from the content-addressed store, hash-verified

352 }
353
354 readFileSync(
355 path: string,
356 options?: BufferEncoding | { encoding?: BufferEncoding | null } | null,
357 ): Buffer | string {
358 const encoding = typeof options === "string" ? options : options?.encoding;
359 const { path: canonical } = canonicalizePath(path);
360 const pending = getPendingWriteBufferByPath(this.db, canonical);
361 if (pending !== undefined) {
362 const snapshot = Buffer.alloc(pending.size);
363 snapshot.set(pending.buf.subarray(0, pending.size));
364 return encoding ? snapshot.toString(encoding) : snapshot;
365 }
366 const node = resolveInode(this.db, path);
367 if (node === null) {
368 throw createWorkspaceError("ENOENT", `no such file: ${path}`, path);
369 }
370 if (node.type !== "file") {
371 throw createWorkspaceError("EISDIR", `path is a directory: ${path}`, path);
372 }
373 // While a buffer is open for this inode it owns the latest
374 // bytes; serve from it instead of the chunk store.
375 const buffered = getWriteBuffer(this.db, node.inode);
376 if (buffered?.dirty) {
377 const snapshot = Buffer.alloc(buffered.size);
378 snapshot.set(buffered.buf.subarray(0, buffered.size));
379 return encoding ? snapshot.toString(encoding) : snapshot;
380 }
381 const chunks = this.db.all<{ hash: Uint8Array; size: number }>(
382 "SELECT hash, size FROM vfs_chunks WHERE inode = ? ORDER BY idx",
383 node.inode,
384 );
385 let total = 0;
386 for (const c of chunks) total += c.size;
387 const out = Buffer.alloc(total);
388 let offset = 0;
389 for (const chunk of chunks) {
390 const bytes = getBlobBytes(this.db, chunk.hash);
391 if (bytes === undefined) {
392 throw createWorkspaceError("EIO", `missing blob bytes for ${path}`, path);
393 }
394 out.set(bytes, offset);
395 offset += bytes.byteLength;
396 }
397 return encoding ? out.toString(encoding) : out;
398 }
399
400 writeFile(
401 path: string,

Callers 15

readFileMethod · 0.95
mainFunction · 0.95
provider.test.tsFile · 0.80
assertRenamedFunction · 0.80
assertUnchangedFunction · 0.80
fs-ops.bench.tsFile · 0.80
materialiseVfsToDiskFunction · 0.80
syncVfsPathToDiskFunction · 0.80
safeVfsReadFunction · 0.80
shim.test.tsFile · 0.80
vfs.test.tsFile · 0.80

Calls 9

canonicalizePathFunction · 0.85
resolveInodeFunction · 0.85
getWriteBufferFunction · 0.85
getBlobBytesFunction · 0.85
allMethod · 0.80
createWorkspaceErrorFunction · 0.70
setMethod · 0.65
toStringMethod · 0.65

Tested by 2

assertRenamedFunction · 0.64
assertUnchangedFunction · 0.64