| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4356836 commit 81f3eb5
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4212,6 +4212,32 @@ If one or more `filehandle.write()` calls are made on a file handle and then a | |||
| 4212 | 4212 | current position till the end of the file. It doesn't always write from the | |
| 4213 | 4213 | beginning of the file. | |
| 4214 | 4214 | ||
| 4215 | + #### filehandle.writev(buffers[, position]) | ||
| 4216 | + <!-- YAML | ||
| 4217 | + added: REPLACEME | ||
| 4218 | + --> | ||
| 4219 | + | ||
| 4220 | + * `buffers` {ArrayBufferView[]} | ||
| 4221 | + * `position` {integer} | ||
| 4222 | + * Returns: {Promise} | ||
| 4223 | + | ||
| 4224 | + Write an array of `ArrayBufferView`s to the file. | ||
| 4225 | + | ||
| 4226 | + The `Promise` is resolved with an object containing a `bytesWritten` property | ||
| 4227 | + identifying the number of bytes written, and a `buffers` property containing | ||
| 4228 | + a reference to the `buffers` input. | ||
| 4229 | + | ||
| 4230 | + `position` is the offset from the beginning of the file where this data | ||
| 4231 | + should be written. If `typeof position !== 'number'`, the data will be written | ||
| 4232 | + at the current position. | ||
| 4233 | + | ||
| 4234 | + It is unsafe to call `writev()` multiple times on the same file without waiting | ||
| 4235 | + for the previous operation to complete. | ||
| 4236 | + | ||
| 4237 | + On Linux, positional writes don't work when the file is opened in append mode. | ||
| 4238 | + The kernel ignores the position argument and always appends the data to | ||
| 4239 | + the end of the file. | ||
| 4240 | + | ||
| 4215 | 4241 | ### fsPromises.access(path[, mode]) | |
| 4216 | 4242 | <!-- YAML | |
| 4217 | 4243 | added: v10.0.0 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,6 +29,7 @@ const { | |||
| 29 | 29 | stringToFlags, | |
| 30 | 30 | stringToSymlinkType, | |
| 31 | 31 | toUnixTimestamp, | |
| 32 | + validateBufferArray, | ||
| 32 | 33 | validateOffsetLengthRead, | |
| 33 | 34 | validateOffsetLengthWrite, | |
| 34 | 35 | warnOnNonPortableTemplate | |
@@ -104,6 +105,10 @@ class FileHandle { | |||
| 104 | 105 | return write(this, buffer, offset, length, position); | |
| 105 | 106 | } | |
| 106 | 107 | ||
| 108 | + writev(buffers, position) { | ||
| 109 | + return writev(this, buffers, position); | ||
| 110 | + } | ||
| 111 | + | ||
| 107 | 112 | writeFile(data, options) { | |
| 108 | 113 | return writeFile(this, data, options); | |
| 109 | 114 | } | |
@@ -255,6 +260,18 @@ async function write(handle, buffer, offset, length, position) { | |||
| 255 | 260 | return { bytesWritten, buffer }; | |
| 256 | 261 | } | |
| 257 | 262 | ||
| 263 | + async function writev(handle, buffers, position) { | ||
| 264 | + validateFileHandle(handle); | ||
| 265 | + validateBufferArray(buffers); | ||
| 266 | + | ||
| 267 | + if (typeof position !== 'number') | ||
| 268 | + position = null; | ||
| 269 | + | ||
| 270 | + const bytesWritten = (await binding.writeBuffers(handle.fd, buffers, position, | ||
| 271 | + kUsePromises)) || 0; | ||
| 272 | + return { bytesWritten, buffers }; | ||
| 273 | + } | ||
| 274 | + | ||
| 258 | 275 | async function rename(oldPath, newPath) { | |
| 259 | 276 | oldPath = getValidatedPath(oldPath, 'oldPath'); | |
| 260 | 277 | newPath = getValidatedPath(newPath, 'newPath'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,6 +15,7 @@ const { | |||
| 15 | 15 | hideStackFrames | |
| 16 | 16 | } = require('internal/errors'); | |
| 17 | 17 | const { | |
| 18 | + isArrayBufferView, | ||
| 18 | 19 | isUint8Array, | |
| 19 | 20 | isDate | |
| 20 | 21 | } = require('internal/util/types'); | |
@@ -439,6 +440,18 @@ const getValidatedPath = hideStackFrames((fileURLOrPath, propName = 'path') => { | |||
| 439 | 440 | return path; | |
| 440 | 441 | }); | |
| 441 | 442 | ||
| 443 | + const validateBufferArray = hideStackFrames((buffers, propName = 'buffers') => { | ||
| 444 | + if (!Array.isArray(buffers)) | ||
| 445 | + throw new ERR_INVALID_ARG_TYPE(propName, 'ArrayBufferView[]', buffers); | ||
| 446 | + | ||
| 447 | + for (let i = 0; i < buffers.length; i++) { | ||
| 448 | + if (!isArrayBufferView(buffers[i])) | ||
| 449 | + throw new ERR_INVALID_ARG_TYPE(propName, 'ArrayBufferView[]', buffers); | ||
| 450 | + } | ||
| 451 | + | ||
| 452 | + return buffers; | ||
| 453 | + }); | ||
| 454 | + | ||
| 442 | 455 | let nonPortableTemplateWarn = true; | |
| 443 | 456 | ||
| 444 | 457 | function warnOnNonPortableTemplate(template) { | |
@@ -466,6 +479,7 @@ module.exports = { | |||
| 466 | 479 | stringToSymlinkType, | |
| 467 | 480 | Stats, | |
| 468 | 481 | toUnixTimestamp, | |
| 482 | + validateBufferArray, | ||
| 469 | 483 | validateOffsetLengthRead, | |
| 470 | 484 | validateOffsetLengthWrite, | |
| 471 | 485 | validatePath, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,51 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + require('../common'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const path = require('path'); | ||
| 6 | + const fs = require('fs').promises; | ||
| 7 | + const tmpdir = require('../common/tmpdir'); | ||
| 8 | + const expected = 'ümlaut. Лорем 運務ホソモ指及 आपको करने विकास 紙読決多密所 أضف'; | ||
| 9 | + let cnt = 0; | ||
| 10 | + | ||
| 11 | + function getFileName() { | ||
| 12 | + return path.join(tmpdir.path, `writev_promises_${++cnt}.txt`); | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + tmpdir.refresh(); | ||
| 16 | + | ||
| 17 | + (async () => { | ||
| 18 | + { | ||
| 19 | + const filename = getFileName(); | ||
| 20 | + const handle = await fs.open(filename, 'w'); | ||
| 21 | + const buffer = Buffer.from(expected); | ||
| 22 | + const bufferArr = [buffer, buffer]; | ||
| 23 | + const expectedLength = bufferArr.length * buffer.byteLength; | ||
| 24 | + let { bytesWritten, buffers } = await handle.writev([Buffer.from('')], | ||
| 25 | + null); | ||
| 26 | + assert.deepStrictEqual(bytesWritten, 0); | ||
| 27 | + assert.deepStrictEqual(buffers, [Buffer.from('')]); | ||
| 28 | + ({ bytesWritten, buffers } = await handle.writev(bufferArr, null)); | ||
| 29 | + assert.deepStrictEqual(bytesWritten, expectedLength); | ||
| 30 | + assert.deepStrictEqual(buffers, bufferArr); | ||
| 31 | + assert(Buffer.concat(bufferArr).equals(await fs.readFile(filename))); | ||
| 32 | + handle.close(); | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + // fs.promises.writev() with an array of buffers without position. | ||
| 36 | + { | ||
| 37 | + const filename = getFileName(); | ||
| 38 | + const handle = await fs.open(filename, 'w'); | ||
| 39 | + const buffer = Buffer.from(expected); | ||
| 40 | + const bufferArr = [buffer, buffer, buffer]; | ||
| 41 | + const expectedLength = bufferArr.length * buffer.byteLength; | ||
| 42 | + let { bytesWritten, buffers } = await handle.writev([Buffer.from('')]); | ||
| 43 | + assert.deepStrictEqual(bytesWritten, 0); | ||
| 44 | + assert.deepStrictEqual(buffers, [Buffer.from('')]); | ||
| 45 | + ({ bytesWritten, buffers } = await handle.writev(bufferArr)); | ||
| 46 | + assert.deepStrictEqual(bytesWritten, expectedLength); | ||
| 47 | + assert.deepStrictEqual(buffers, bufferArr); | ||
| 48 | + assert(Buffer.concat(bufferArr).equals(await fs.readFile(filename))); | ||
| 49 | + handle.close(); | ||
| 50 | + } | ||
| 51 | + })(); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments