fs: add writev() promises version by cjihrig · Pull Request #29186 · nodejs/node · GitHub
Skip to content
Merged
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
26 changes: 26 additions & 0 deletions doc/api/fs.md
27 changes: 4 additions & 23 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const {
stringToFlags,
stringToSymlinkType,
toUnixTimestamp,
validateBufferArray,
validateOffsetLengthRead,
validateOffsetLengthWrite,
validatePath,
Expand Down Expand Up @@ -142,19 +143,6 @@ function maybeCallback(cb) {
throw new ERR_INVALID_CALLBACK(cb);
}

function isBuffersArray(value) {
if (!Array.isArray(value))
return false;

for (var i = 0; i < value.length; i += 1) {
if (!isArrayBufferView(value[i])) {
return false;
}
}

return true;
}

// Ensure that callbacks run in the global context. Only use this function
// for callbacks that are passed to the binding layer, callbacks that are
// invoked from JS already run in the proper scope.
Expand Down Expand Up @@ -626,10 +614,7 @@ function writev(fd, buffers, position, callback) {
}

validateInt32(fd, 'fd', 0);

if (!isBuffersArray(buffers)) {
throw new ERR_INVALID_ARG_TYPE('buffers', 'ArrayBufferView[]', buffers);
}
validateBufferArray(buffers);

const req = new FSReqCallback();
req.oncomplete = wrapper;
Expand All @@ -647,15 +632,11 @@ Object.defineProperty(writev, internalUtil.customPromisifyArgs, {
enumerable: false
});

// fs.writevSync(fd, buffers[, position]);
function writevSync(fd, buffers, position) {

validateInt32(fd, 'fd', 0);
const ctx = {};
validateBufferArray(buffers);

if (!isBuffersArray(buffers)) {
throw new ERR_INVALID_ARG_TYPE('buffers', 'ArrayBufferView[]', buffers);
}
const ctx = {};

if (typeof position !== 'number')
position = null;
Expand Down
17 changes: 17 additions & 0 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const {
stringToFlags,
stringToSymlinkType,
toUnixTimestamp,
validateBufferArray,
validateOffsetLengthRead,
validateOffsetLengthWrite,
warnOnNonPortableTemplate
Expand Down Expand Up @@ -104,6 +105,10 @@ class FileHandle {
return write(this, buffer, offset, length, position);
}

writev(buffers, position) {
return writev(this, buffers, position);
}

writeFile(data, options) {
return writeFile(this, data, options);
}
Expand Down Expand Up @@ -263,6 +268,18 @@ async function write(handle, buffer, offset, length, position) {
return { bytesWritten, buffer };
}

async function writev(handle, buffers, position) {
validateFileHandle(handle);
validateBufferArray(buffers);

if (typeof position !== 'number')
position = null;

const bytesWritten = (await binding.writeBuffers(handle.fd, buffers, position,
kUsePromises)) || 0;
return { bytesWritten, buffers };
}

async function rename(oldPath, newPath) {
oldPath = getValidatedPath(oldPath, 'oldPath');
newPath = getValidatedPath(newPath, 'newPath');
Expand Down
14 changes: 14 additions & 0 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
hideStackFrames
} = require('internal/errors');
const {
isArrayBufferView,
isUint8Array,
isDate,
isBigUint64Array
Expand Down Expand Up @@ -500,6 +501,18 @@ const getValidatedPath = hideStackFrames((fileURLOrPath, propName = 'path') => {
return path;
});

const validateBufferArray = hideStackFrames((buffers, propName = 'buffers') => {
if (!Array.isArray(buffers))
throw new ERR_INVALID_ARG_TYPE(propName, 'ArrayBufferView[]', buffers);

for (let i = 0; i < buffers.length; i++) {
if (!isArrayBufferView(buffers[i]))
throw new ERR_INVALID_ARG_TYPE(propName, 'ArrayBufferView[]', buffers);
}

return buffers;
});

let nonPortableTemplateWarn = true;

function warnOnNonPortableTemplate(template) {
Expand Down Expand Up @@ -528,6 +541,7 @@ module.exports = {
stringToSymlinkType,
Stats,
toUnixTimestamp,
validateBufferArray,
validateOffsetLengthRead,
validateOffsetLengthWrite,
validatePath,
Expand Down
51 changes: 51 additions & 0 deletions test/parallel/test-fs-writev-promises.js