Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: fix fs.promises.writeFile with typed arrays #35376

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 10 additions & 3 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ const kReadFileMaxChunkSize = 2 ** 14;
const kWriteFileMaxChunkSize = 2 ** 14;

const {
Error,
MathMax,
MathMin,
NumberIsSafeInteger,
Symbol,
Error,
Promise,
Symbol,
Uint8Array,
} = primordials;

const {
Expand Down Expand Up @@ -237,14 +238,20 @@ async function fsCall(fn, handle, ...args) {
}

async function writeFileHandle(filehandle, data) {
// `data` could be any kind of typed array.
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
let remaining = data.length;
if (remaining === 0) return;
do {
const { bytesWritten } =
await write(filehandle, data, 0,
MathMin(kWriteFileMaxChunkSize, data.length));
remaining -= bytesWritten;
data = data.slice(bytesWritten);
data = new Uint8Array(
data.buffer,
data.byteOffset + bytesWritten,
data.byteLength - bytesWritten
);
} while (remaining > 0);
}

Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-fs-promises-writefile-typedarray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

const common = require('../common');
const fs = require('fs');
const fsPromises = fs.promises;
const path = require('path');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;

tmpdir.refresh();

const dest = path.resolve(tmpDir, 'tmp.txt');
// Use a file size larger than `kReadFileMaxChunkSize`.
const buffer = Buffer.from('012'.repeat(2 ** 14));

(async () => {
for (const Constructor of [Uint8Array, Uint16Array, Uint32Array]) {
const array = new Constructor(buffer.buffer);
await fsPromises.writeFile(dest, array);
const data = await fsPromises.readFile(dest);
assert.deepStrictEqual(data, buffer);
}
})().then(common.mustCall());