Skip to content

Commit

Permalink
update docs and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SRHerzog committed Feb 9, 2023
1 parent 0c2d047 commit 00952f7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
14 changes: 8 additions & 6 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,8 @@ changes:
strings anymore.
-->
* `data` {string|Buffer|TypedArray|DataView|ArrayBuffer|AsyncIterable|Iterable|Stream}
* `data` {string|Buffer|TypedArray|DataView|ArrayBuffer|AsyncIterable|
Iterable|Stream}
* `options` {Object|string}
* `encoding` {string|null} The expected character encoding when `data` is a
string. **Default:** `'utf8'`
Expand Down Expand Up @@ -799,8 +800,8 @@ Write an array of {ArrayBufferView}s or {ArrayBuffer}s to the file.
The promise is resolved with an object containing a two properties:
* `bytesWritten` {integer} the number of bytes written
* `buffers` {Buffer\[]|TypedArray\[]|DataView\[]|ArrayBuffer\[]} a reference to the `buffers`
input.
* `buffers` {Buffer\[]|TypedArray\[]|DataView\[]|ArrayBuffer\[]} a reference
to the `buffers` input.
It is unsafe to call `writev()` multiple times on the same file without waiting
for the promise to be resolved (or rejected).
Expand Down Expand Up @@ -1693,7 +1694,8 @@ changes:
-->
* `file` {string|Buffer|URL|FileHandle} filename or `FileHandle`
* `data` {string|Buffer|TypedArray|DataView|ArrayBuffer|AsyncIterable|Iterable|Stream}
* `data` {string|Buffer|TypedArray|DataView|ArrayBuffer|AsyncIterable
|Iterable|Stream}
* `options` {Object|string}
* `encoding` {string|null} **Default:** `'utf8'`
* `mode` {integer} **Default:** `0o666`
Expand Down Expand Up @@ -4898,8 +4900,8 @@ changes:
* `bytesWritten` {integer}
* `buffers` {ArrayBufferView\[]}

Write an array of `ArrayBufferView`s or `ArrayBuffer`s to the file specified by `fd` using
`writev()`.
Write an array of `ArrayBufferView`s or `ArrayBuffer`s to the file specified
by `fd` using `writev()`.

`position` is the offset from the beginning of the file where this data
should be written. If `typeof position !== 'number'`, the data will be written
Expand Down
1 change: 1 addition & 0 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {
StringPrototypeIncludes,
Symbol,
TypedArrayPrototypeIncludes,
Uint8Array,
} = primordials;

const { Buffer } = require('buffer');
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-fs-writev-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ const getFileName = (i) => path.join(tmpdir.path, `writev_sync_${i}.txt`);
assert(Buffer.concat(bufferArr).equals(fs.readFileSync(filename)));
}

// fs.writevSync with array of ArrayBuffers without position
{
const filename = getFileName(2);
const fd = fs.openSync(filename, 'w');

const buffer = Buffer.from(expected);
const arrayBuffer = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
const arrayBufferArr = [arrayBuffer, arrayBuffer, arrayBuffer];
const expectedLength = arrayBufferArr.length * arrayBuffer.byteLength;

let written = fs.writevSync(fd, [Buffer.from('')]);
assert.strictEqual(written, 0);

written = fs.writevSync(fd, arrayBufferArr);
assert.strictEqual(written, expectedLength);

fs.closeSync(fd);
const expectedResult = Buffer.concat(arrayBufferArr.map((buf) => new Uint8Array(buf)));
assert(expectedResult.equals(fs.readFileSync(filename)));
}

// fs.writevSync with empty array of buffers
{
const filename = getFileName(3);
Expand Down
11 changes: 1 addition & 10 deletions test/parallel/test-fs-writev.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,10 @@ const getFileName = (i) => path.join(tmpdir.path, `writev_${i}.txt`);
assert.deepStrictEqual(arrayBufferArr, buffers);

const expectedLength = arrayBufferArr.length * arrayBuffer.byteLength;
console.log({ arrLength: arrayBufferArr.length, byteLength: arrayBuffer.byteLength })
assert.deepStrictEqual(written, expectedLength);
fs.closeSync(fd);
const expectedResult = Buffer.concat(arrayBufferArr.map((buf) => new Uint8Array(buf)));
const gotResult = fs.readFileSync(filename);
for (let i = 0; i < expectedResult.byteLength; i++) {
if (expectedResult[i] !== gotResult[i]) {
console.log({ i, expected: expectedResult[i], got: gotResult[i], expectedLength })
break;
}
}
// console.log({ expectedResult, _____gotResult, equal: expectedResult.compare(gotResult) });
assert(expectedResult.equals(gotResult));
assert(expectedResult.equals(fs.readFileSync(filename)));
});

fs.writev(fd, arrayBufferArr, done);
Expand Down

0 comments on commit 00952f7

Please sign in to comment.