Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/internal/vfs/file_handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
7 changes: 5 additions & 2 deletions lib/internal/vfs/providers/ziparchive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-vfs-handle-semantics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}