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

Method writeSync

packages/dofs/src/provider.ts:597–630  ·  view source on GitHub ↗
(
    fd: number,
    buffer: Buffer | Uint8Array,
    offset: number = 0,
    length: number = buffer.byteLength - offset,
    position: number | null = null,
  )

Source from the content-addressed store, hash-verified

595 }
596
597 writeSync(
598 fd: number,
599 buffer: Buffer | Uint8Array,
600 offset: number = 0,
601 length: number = buffer.byteLength - offset,
602 position: number | null = null,
603 ): number {
604 const state = this.#fdOrThrow(fd);
605 if (!state.writable) {
606 throw createWorkspaceError("EBADF", `fd ${fd} is not writable`);
607 }
608 // Append needs the current EOF, so stat only then. A non-append
609 // write of >0 bytes doesn't need it: writeRangeSyncImpl resolves the
610 // path and raises ENOENT/EISDIR. A zero-length write short-circuits
611 // before that resolve, so keep an explicit existence check for it.
612 let startAt: number;
613 if (state.append) {
614 startAt = this.statSync(state.path).size;
615 } else {
616 if (length === 0) {
617 this.statSync(state.path);
618 }
619 startAt = position ?? state.position;
620 }
621 const view =
622 buffer instanceof Buffer
623 ? new Uint8Array(buffer.buffer, buffer.byteOffset + offset, length)
624 : new Uint8Array(buffer.buffer, buffer.byteOffset + offset, length);
625 writeRangeSyncImpl(this.db, state.path, view, startAt, {}, this.now);
626 if (position === null || position === undefined) {
627 state.position = startAt + length;
628 }
629 return length;
630 }
631
632 fstatSync(fd: number, _options?: { bigint?: boolean }): VirtualStatsLike {
633 const state = this.#fdOrThrow(fd);

Callers 3

wire.test.tsFile · 0.80

Calls 3

#fdOrThrowMethod · 0.95
statSyncMethod · 0.95
createWorkspaceErrorFunction · 0.70

Tested by

no test coverage detected