| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 474fbb5 commit 6bbe285
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -265,18 +265,20 @@ added: v10.0.0 | |||
| 265 | 265 | added: v10.0.0 | |
| 266 | 266 | --> | |
| 267 | 267 | ||
| 268 | - * `buffer` {Buffer|Uint8Array} A buffer that will be filled with the file | ||
| 269 | - data read. | ||
| 268 | + * `buffer` {Buffer|TypedArray|DataView} A buffer that will be filled with the | ||
| 269 | + file data read. | ||
| 270 | 270 | * `offset` {integer} The location in the buffer at which to start filling. | |
| 271 | 271 | **Default:** `0` | |
| 272 | - * `length` {integer} The number of bytes to read. **Default:** `buffer.length` | ||
| 272 | + * `length` {integer} The number of bytes to read. **Default:** | ||
| 273 | + `buffer.byteLength` | ||
| 273 | 274 | * `position` {integer} The location where to begin reading data from the | |
| 274 | 275 | file. If `null`, data will be read from the current file position, and | |
| 275 | 276 | the position will be updated. If `position` is an integer, the current | |
| 276 | 277 | file position will remain unchanged. | |
| 277 | 278 | * Returns: {Promise} Fulfills upon success with an object with two properties: | |
| 278 | 279 | * `bytesRead` {integer} The number of bytes read | |
| 279 | - * `buffer` {Buffer|Uint8Array} A reference to the passed in `buffer` argument. | ||
| 280 | + * `buffer` {Buffer|TypedArray|DataView} A reference to the passed in `buffer` | ||
| 281 | + argument. | ||
| 280 | 282 | ||
| 281 | 283 | Reads data from the file and stores that in the given buffer. | |
| 282 | 284 | ||
@@ -290,19 +292,20 @@ added: | |||
| 290 | 292 | - v12.17.0 | |
| 291 | 293 | --> | |
| 292 | 294 | * `options` {Object} | |
| 293 | - * `buffer` {Buffer|Uint8Array} A buffer that will be filled with the file | ||
| 294 | - data read. **Default:** `Buffer.alloc(16384)` | ||
| 295 | + * `buffer` {Buffer|TypedArray|DataView} A buffer that will be filled with the | ||
| 296 | + file data read. **Default:** `Buffer.alloc(16384)` | ||
| 295 | 297 | * `offset` {integer} The location in the buffer at which to start filling. | |
| 296 | 298 | **Default:** `0` | |
| 297 | - * `length` {integer} The number of bytes to read. **Default:** `buffer.length` | ||
| 299 | + * `length` {integer} The number of bytes to read. **Default:** | ||
| 300 | + `buffer.byteLength` | ||
| 298 | 301 | * `position` {integer} The location where to begin reading data from the | |
| 299 | 302 | file. If `null`, data will be read from the current file position, and | |
| 300 | 303 | the position will be updated. If `position` is an integer, the current | |
| 301 | 304 | file position will remain unchanged. **Default:**: `null` | |
| 302 | 305 | * Returns: {Promise} Fulfills upon success with an object with two properties: | |
| 303 | 306 | * `bytesRead` {integer} The number of bytes read | |
| 304 | - * `buffer` {Buffer|Uint8Array} A reference to the passed in `buffer` | ||
| 305 | - argument. | ||
| 307 | + * `buffer` {Buffer|TypedArray|DataView} A reference to the passed in `buffer` | ||
| 308 | + argument. | ||
| 306 | 309 | ||
| 307 | 310 | Reads data from the file and stores that in the given buffer. | |
| 308 | 311 | ||
@@ -434,10 +437,11 @@ changes: | |||
| 434 | 437 | buffers anymore. | |
| 435 | 438 | --> | |
| 436 | 439 | ||
| 437 | - * `buffer` {Buffer|Uint8Array|string|Object} | ||
| 440 | + * `buffer` {Buffer|TypedArray|DataView|string|Object} | ||
| 438 | 441 | * `offset` {integer} The start position from within `buffer` where the data | |
| 439 | - to write begins. | ||
| 440 | - * `length` {integer} The number of bytes from `buffer` to write. | ||
| 442 | + to write begins. **Default:** `0` | ||
| 443 | + * `length` {integer} The number of bytes from `buffer` to write. **Default:** | ||
| 444 | + `buffer.byteLength` | ||
| 441 | 445 | * `position` {integer} The offset from the beginning of the file where the | |
| 442 | 446 | data from `buffer` should be written. If `position` is not a `number`, | |
| 443 | 447 | the data will be written at the current position. See the POSIX pwrite(2) | |
@@ -449,8 +453,8 @@ Write `buffer` to the file. | |||
| 449 | 453 | The promise is resolved with an object containing two properties: | |
| 450 | 454 | ||
| 451 | 455 | * `bytesWritten` {integer} the number of bytes written | |
| 452 | - * `buffer` {Buffer|Uint8Array|string|Object} a reference to the `buffer` | ||
| 453 | - written. | ||
| 456 | + * `buffer` {Buffer|TypedArray|DataView|string|Object} a reference to the | ||
| 457 | + `buffer` written. | ||
| 454 | 458 | ||
| 455 | 459 | It is unsafe to use `filehandle.write()` multiple times on the same file | |
| 456 | 460 | without waiting for the promise to be resolved (or rejected). For this | |
@@ -512,7 +516,7 @@ changes: | |||
| 512 | 516 | strings anymore. | |
| 513 | 517 | --> | |
| 514 | 518 | ||
| 515 | - * `data` {string|Buffer|Uint8Array|Object} | ||
| 519 | + * `data` {string|Buffer|TypedArray|DataView|Object} | ||
| 516 | 520 | * `options` {Object|string} | |
| 517 | 521 | * `encoding` {string|null} The expected character encoding when `data` is a | |
| 518 | 522 | string. **Default:** `'utf8'` | |
@@ -1270,7 +1274,7 @@ changes: | |||
| 1270 | 1274 | --> | |
| 1271 | 1275 | ||
| 1272 | 1276 | * `file` {string|Buffer|URL|FileHandle} filename or `FileHandle` | |
| 1273 | - * `data` {string|Buffer|Uint8Array|Object|AsyncIterable|Iterable | ||
| 1277 | + * `data` {string|Buffer|TypedArray|DataView|Object|AsyncIterable|Iterable | ||
| 1274 | 1278 | |Stream} | |
| 1275 | 1279 | * `options` {Object|string} | |
| 1276 | 1280 | * `encoding` {string|null} **Default:** `'utf8'` | |
@@ -2722,9 +2726,11 @@ changes: | |||
| 2722 | 2726 | ||
| 2723 | 2727 | * `fd` {integer} | |
| 2724 | 2728 | * `buffer` {Buffer|TypedArray|DataView} The buffer that the data will be | |
| 2725 | - written to. | ||
| 2726 | - * `offset` {integer} The position in `buffer` to write the data to. | ||
| 2727 | - * `length` {integer} The number of bytes to read. | ||
| 2729 | + written to. **Default:** `Buffer.alloc(16384)` | ||
| 2730 | + * `offset` {integer} The position in `buffer` to write the data to. **Default:** | ||
| 2731 | + `0` | ||
| 2732 | + * `length` {integer} The number of bytes to read. **Default:** | ||
| 2733 | + `buffer.byteLength` | ||
| 2728 | 2734 | * `position` {integer|bigint} Specifies where to begin reading from in the | |
| 2729 | 2735 | file. If `position` is `null` or `-1 `, data will be read from the current | |
| 2730 | 2736 | file position, and the file position will be updated. If `position` is an | |
@@ -2761,7 +2767,7 @@ changes: | |||
| 2761 | 2767 | * `options` {Object} | |
| 2762 | 2768 | * `buffer` {Buffer|TypedArray|DataView} **Default:** `Buffer.alloc(16384)` | |
| 2763 | 2769 | * `offset` {integer} **Default:** `0` | |
| 2764 | - * `length` {integer} **Default:** `buffer.length` | ||
| 2770 | + * `length` {integer} **Default:** `buffer.byteLength` | ||
| 2765 | 2771 | * `position` {integer|bigint} **Default:** `null` | |
| 2766 | 2772 | * `callback` {Function} | |
| 2767 | 2773 | * `err` {Error} | |
@@ -4689,7 +4695,7 @@ changes: | |||
| 4689 | 4695 | * `buffer` {Buffer|TypedArray|DataView} | |
| 4690 | 4696 | * `options` {Object} | |
| 4691 | 4697 | * `offset` {integer} **Default:** `0` | |
| 4692 | - * `length` {integer} **Default:** `buffer.length` | ||
| 4698 | + * `length` {integer} **Default:** `buffer.byteLength` | ||
| 4693 | 4699 | * `position` {integer|bigint} **Default:** `null` | |
| 4694 | 4700 | * Returns: {number} | |
| 4695 | 4701 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -532,7 +532,7 @@ function read(fd, buffer, offset, length, position, callback) { | |||
| 532 | 532 | ({ | |
| 533 | 533 | buffer = Buffer.alloc(16384), | |
| 534 | 534 | offset = 0, | |
| 535 | - length = buffer.length, | ||
| 535 | + length = buffer.byteLength, | ||
| 536 | 536 | position | |
| 537 | 537 | } = options); | |
| 538 | 538 | } | |
@@ -587,15 +587,15 @@ ObjectDefineProperty(read, internalUtil.customPromisifyArgs, | |||
| 587 | 587 | function readSync(fd, buffer, offset, length, position) { | |
| 588 | 588 | fd = getValidatedFd(fd); | |
| 589 | 589 | ||
| 590 | + validateBuffer(buffer); | ||
| 591 | + | ||
| 590 | 592 | if (arguments.length <= 3) { | |
| 591 | 593 | // Assume fs.read(fd, buffer, options) | |
| 592 | 594 | const options = offset || {}; | |
| 593 | 595 | ||
| 594 | - ({ offset = 0, length = buffer.length, position } = options); | ||
| 596 | + ({ offset = 0, length = buffer.byteLength, position } = options); | ||
| 595 | 597 | } | |
| 596 | 598 | ||
| 597 | - validateBuffer(buffer); | ||
| 598 | - | ||
| 599 | 599 | if (offset == null) { | |
| 600 | 600 | offset = 0; | |
| 601 | 601 | } else { | |
@@ -682,7 +682,7 @@ function write(fd, buffer, offset, length, position, callback) { | |||
| 682 | 682 | validateInteger(offset, 'offset'); | |
| 683 | 683 | } | |
| 684 | 684 | if (typeof length !== 'number') | |
| 685 | - length = buffer.length - offset; | ||
| 685 | + length = buffer.byteLength - offset; | ||
| 686 | 686 | if (typeof position !== 'number') | |
| 687 | 687 | position = null; | |
| 688 | 688 | validateOffsetLengthWrite(offset, length, buffer.byteLength); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -280,19 +280,19 @@ async function writeFileHandle(filehandle, data, signal, encoding) { | |||
| 280 | 280 | checkAborted(signal); | |
| 281 | 281 | await write( | |
| 282 | 282 | filehandle, buf, undefined, | |
| 283 | - isArrayBufferView(buf) ? buf.length : encoding); | ||
| 283 | + isArrayBufferView(buf) ? buf.byteLength : encoding); | ||
| 284 | 284 | checkAborted(signal); | |
| 285 | 285 | } | |
| 286 | 286 | return; | |
| 287 | 287 | } | |
| 288 | 288 | data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength); | |
| 289 | - let remaining = data.length; | ||
| 289 | + let remaining = data.byteLength; | ||
| 290 | 290 | if (remaining === 0) return; | |
| 291 | 291 | do { | |
| 292 | 292 | checkAborted(signal); | |
| 293 | 293 | const { bytesWritten } = | |
| 294 | 294 | await write(filehandle, data, 0, | |
| 295 | - MathMin(kWriteFileMaxChunkSize, data.length)); | ||
| 295 | + MathMin(kWriteFileMaxChunkSize, data.byteLength)); | ||
| 296 | 296 | remaining -= bytesWritten; | |
| 297 | 297 | data = new Uint8Array( | |
| 298 | 298 | data.buffer, | |
@@ -404,7 +404,7 @@ async function read(handle, bufferOrOptions, offset, length, position) { | |||
| 404 | 404 | buffer = Buffer.alloc(16384); | |
| 405 | 405 | } | |
| 406 | 406 | offset = bufferOrOptions.offset || 0; | |
| 407 | - length = buffer.length; | ||
| 407 | + length = buffer.byteLength; | ||
| 408 | 408 | position = bufferOrOptions.position || null; | |
| 409 | 409 | } | |
| 410 | 410 | ||
@@ -419,12 +419,12 @@ async function read(handle, bufferOrOptions, offset, length, position) { | |||
| 419 | 419 | if (length === 0) | |
| 420 | 420 | return { bytesRead: length, buffer }; | |
| 421 | 421 | ||
| 422 | - if (buffer.length === 0) { | ||
| 422 | + if (buffer.byteLength === 0) { | ||
| 423 | 423 | throw new ERR_INVALID_ARG_VALUE('buffer', buffer, | |
| 424 | 424 | 'is empty and cannot be written'); | |
| 425 | 425 | } | |
| 426 | 426 | ||
| 427 | - validateOffsetLengthRead(offset, length, buffer.length); | ||
| 427 | + validateOffsetLengthRead(offset, length, buffer.byteLength); | ||
| 428 | 428 | ||
| 429 | 429 | if (!NumberIsSafeInteger(position)) | |
| 430 | 430 | position = -1; | |
@@ -447,7 +447,7 @@ async function readv(handle, buffers, position) { | |||
| 447 | 447 | } | |
| 448 | 448 | ||
| 449 | 449 | async function write(handle, buffer, offset, length, position) { | |
| 450 | - if (buffer?.length === 0) | ||
| 450 | + if (buffer?.byteLength === 0) | ||
| 451 | 451 | return { bytesWritten: 0, buffer }; | |
| 452 | 452 | ||
| 453 | 453 | if (isArrayBufferView(buffer)) { | |
@@ -457,7 +457,7 @@ async function write(handle, buffer, offset, length, position) { | |||
| 457 | 457 | validateInteger(offset, 'offset'); | |
| 458 | 458 | } | |
| 459 | 459 | if (typeof length !== 'number') | |
| 460 | - length = buffer.length - offset; | ||
| 460 | + length = buffer.byteLength - offset; | ||
| 461 | 461 | if (typeof position !== 'number') | |
| 462 | 462 | position = null; | |
| 463 | 463 | validateOffsetLengthWrite(offset, length, buffer.byteLength); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -146,3 +146,20 @@ tmpdir.refresh(); | |||
| 146 | 146 | fs.closeSync(fd); | |
| 147 | 147 | })); | |
| 148 | 148 | } | |
| 149 | + | ||
| 150 | + // fs.write with a DataView, without the offset and length parameters: | ||
| 151 | + { | ||
| 152 | + const filename = path.join(tmpdir.path, 'write8.txt'); | ||
| 153 | + fs.open(filename, 'w', 0o644, common.mustSucceed((fd) => { | ||
| 154 | + const cb = common.mustSucceed((written) => { | ||
| 155 | + assert.strictEqual(written, expected.length); | ||
| 156 | + fs.closeSync(fd); | ||
| 157 | + | ||
| 158 | + const found = fs.readFileSync(filename, 'utf8'); | ||
| 159 | + assert.strictEqual(found, expected.toString()); | ||
| 160 | + }); | ||
| 161 | + | ||
| 162 | + const uint8 = Uint8Array.from(expected); | ||
| 163 | + fs.write(fd, new DataView(uint8.buffer), cb); | ||
| 164 | + })); | ||
| 165 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments