From af24e101fe2907e5f429bdad3e80d06fa9d9f1c5 Mon Sep 17 00:00:00 2001 From: Raunak Burrows Date: Thu, 3 Dec 2026 22:41:23 +0000 Subject: [PATCH] vfs: accept any ArrayBufferView when writing 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 --- lib/internal/vfs/file_handle.js | 8 +++++--- lib/internal/vfs/providers/ziparchive.js | 7 +++++-- test/parallel/test-vfs-handle-semantics.js | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/internal/vfs/file_handle.js b/lib/internal/vfs/file_handle.js index 9055804dabcd..75a8a3f9bd47 100644 --- a/lib/internal/vfs/file_handle.js +++ b/lib/internal/vfs/file_handle.js @@ -383,7 +383,7 @@ class VirtualFileHandle { this.#checkClosed('appendFile'); const buffer = typeof data === 'string' ? Buffer.from(data, options?.encoding) : data; - await this.write(buffer, 0, buffer.length, null); + await this.write(buffer, 0, buffer.byteLength, null); } readableWebStream() { @@ -610,7 +610,9 @@ class MemoryFileHandle extends VirtualFileHandle { const writePos = this.#isAppend() ? this.#size : (useCurrentPosition ? this.position : Number(position)); - const data = buffer.subarray(offset, offset + length); + // `buffer` can be any ArrayBufferView, and offset and length count bytes. + const data = Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength) + .subarray(offset, offset + length); // Expand buffer if needed (geometric doubling for amortized O(1) appends) const neededSize = writePos + length; @@ -705,7 +707,7 @@ class MemoryFileHandle extends VirtualFileHandle { */ writeFileSync(data, options) { const buffer = typeof data === 'string' ? Buffer.from(data, options?.encoding) : data; - this.writeSync(buffer, 0, buffer.length, null); + this.writeSync(buffer, 0, buffer.byteLength, null); } /** diff --git a/lib/internal/vfs/providers/ziparchive.js b/lib/internal/vfs/providers/ziparchive.js index 442de67b8b21..7324edd14dcf 100644 --- a/lib/internal/vfs/providers/ziparchive.js +++ b/lib/internal/vfs/providers/ziparchive.js @@ -197,7 +197,9 @@ class ZipFileHandle extends VirtualFileHandle { const useCurrent = isCurrentPosition(position); const pos = this[kAccess].append ? this.#size : (useCurrent ? this.position : Number(position)); this.#ensureCapacity(pos + length); - buffer.copy(this.#buffer, pos, offset, offset + length); + // `buffer` can be any ArrayBufferView, and offset and length count bytes. + Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength) + .copy(this.#buffer, pos, offset, offset + length); if (pos + length > this.#size) this.#size = pos + length; this.#dirty = true; if (useCurrent) this.position = pos + length; @@ -229,7 +231,8 @@ class ZipFileHandle extends VirtualFileHandle { // place and keeps any tail. This is what makes `appendFile()` (built on // this by VirtualProvider's defaults) actually append. #doWriteFile(data, options) { - const content = typeof data === 'string' ? Buffer.from(data, options?.encoding) : Buffer.from(data); + const content = typeof data === 'string' ? Buffer.from(data, options?.encoding) : + Buffer.from(data.buffer, data.byteOffset, data.byteLength); this.#doWrite(content, 0, content.length, null); } async writeFile(data, options) { diff --git a/test/parallel/test-vfs-handle-semantics.js b/test/parallel/test-vfs-handle-semantics.js index 4243b665abb3..f69ad74e8cec 100644 --- a/test/parallel/test-vfs-handle-semantics.js +++ b/test/parallel/test-vfs-handle-semantics.js @@ -112,4 +112,23 @@ for (const { 0: name, 1: fileWith } of Object.entries(providers)) { fs.writeFileSync(file, 'HEY', { flag: 'r+' }); assert.strictEqual(fs.readFileSync(file, 'utf8'), 'HEYlo world'); }); + + test(`${name}: writes accept any ArrayBufferView, not only a Buffer`, async () => { + const file = fileWith(''); + fs.writeFileSync(file, new TextEncoder().encode('ab')); + await fs.promises.appendFile(file, new Uint8Array([0x63])); + fs.appendFileSync(file, new DataView(new Uint8Array([0x64]).buffer)); + const fd = fs.openSync(file, 'a'); + try { + // Offset and length count bytes, whatever the element size. + const u16 = new Uint16Array(new Uint8Array([0x65, 0x66, 0x67, 0x68]).buffer); + assert.strictEqual(fs.writeSync(fd, u16, 2, 2), 2); + } finally { + fs.closeSync(fd); + } + const handle = await fs.promises.open(file, 'a'); + await handle.appendFile(new Uint16Array(new Uint8Array([0x69, 0x6a]).buffer)); + await handle.close(); + assert.strictEqual(fs.readFileSync(file, 'latin1'), 'abcdghij'); + }); }