Skip to content

vfs: accept any ArrayBufferView when writing - #68003

Open
burrows99 wants to merge 1 commit into
nodejs:mainfrom
burrows99:vfs-typed-array-write
Open

burrows99 wants to merge 1 commit into
nodejs:mainfrom
burrows99:vfs-typed-array-write

Conversation

@burrows99

Copy link
Copy Markdown

writing a Uint8Array (or any ArrayBufferView that isn't a Buffer) to a file in a memory or zip vfs mount throws TypeError: data.copy is not a function. so fs.writeFileSync(file, new TextEncoder().encode('hi')), fs.promises.writeFile, fs.appendFileSync and fs.writeSync with a typed array all fail inside a mount, while they work on a real file.

import vfs from 'node:vfs';
import fs from 'node:fs';

const dir = vfs.create().mount();
fs.writeFileSync(`${dir}/note.txt`, new TextEncoder().encode('hello'));
// main: TypeError: data.copy is not a function

the memory and zip file handles call Buffer methods (copy) on whatever they're given. this views the data's bytes as a Buffer before copying, and uses byteLength instead of length, so offset/length count bytes for Uint16Array & co. like they do in node:fs. before this, the zip handle's writeFileSync wrote one byte per element of a Uint16Array, and filehandle.appendFile() wrote only the first half of one.

evidence.mp4

tests: added a case to test/parallel/test-vfs-handle-semantics.js (runs on the memory and zip providers): writeFileSync with a Uint8Array, fs.promises.appendFile with a Uint8Array, appendFileSync with a DataView, writeSync with a Uint16Array + byte offset/length, filehandle.appendFile with a Uint16Array. both cases fail on main and pass here. python3 tools/test.py 'parallel/test-vfs-*' parallel/test-fs-write-file-sync parallel/test-fs-append-file-sync parallel/test-fs-write-sync: 131/131 pass. eslint clean on the touched files.

ai disclosure: i used an AI coding agent to find this, write the fix and the test. i read the diff, checked it against node:fs's handling of ArrayBufferView writes, and ran the test and the repro above on a local build before opening this.

Writing a Uint8Array, or any ArrayBufferView that is not a Buffer, to
a file in a memory or ZIP mount threw "data.copy is not a function".
This hit fs.writeFileSync(), fs.appendFileSync(),
fs.promises.writeFile() and fs.writeSync() alike, so writing the
output of TextEncoder failed, while the same calls work on a real file.

The memory and ZIP file handles called Buffer methods on the data they
were given. View the data's bytes as a Buffer before copying it, and
count lengths in bytes, so that offset and length mean bytes for
multi-byte views such as Uint16Array too, as they do in node:fs.

Assisted-by: a closed-source coding agent
Signed-off-by: Raunak Burrows <raunakburrows@gmail.com>
@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. vfs Issues and PRs related to the virtual filesystem subsystem. labels Dec 3, 2026
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

Caution

AgentScan found account activity patterns that may be consistent with automation. This is a heuristic, not proof that this pull request was opened by an agent or violates policy. AI-assisted contributions are permitted, but automated tooling must not open pull requests without advance approval, and contributors must personally understand, test, verify, and take responsibility for every submitted change. See the AgentScan analysis, AI use policy, and automation policy for additional context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agentscan:automated-account needs-ci PRs that need a full CI run. vfs Issues and PRs related to the virtual filesystem subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants