| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -538,6 +538,7 @@ async function readFileHandle(filehandle, options) { | |||
| 538 | 538 | throw new ERR_FS_FILE_TOO_LARGE(size); | |
| 539 | 539 | ||
| 540 | 540 | let totalRead = 0; | |
| 541 | + const noSize = size === 0; | ||
| 541 | 542 | let buffer = Buffer.allocUnsafeSlow(length); | |
| 542 | 543 | let result = ''; | |
| 543 | 544 | let offset = 0; | |
@@ -560,7 +561,7 @@ async function readFileHandle(filehandle, options) { | |||
| 560 | 561 | ||
| 561 | 562 | if (bytesRead === 0 || | |
| 562 | 563 | totalRead === size || | |
| 563 | - (bytesRead !== buffer.length && !chunkedRead)) { | ||
| 564 | + (bytesRead !== buffer.length && !chunkedRead && !noSize)) { | ||
| 564 | 565 | const singleRead = bytesRead === totalRead; | |
| 565 | 566 | ||
| 566 | 567 | const bytesToCheck = chunkedRead ? totalRead : bytesRead; | |
@@ -570,7 +571,7 @@ async function readFileHandle(filehandle, options) { | |||
| 570 | 571 | } | |
| 571 | 572 | ||
| 572 | 573 | if (!encoding) { | |
| 573 | - if (size === 0 && !singleRead) { | ||
| 574 | + if (noSize && !singleRead) { | ||
| 574 | 575 | ArrayPrototypePush(buffers, buffer); | |
| 575 | 576 | return Buffer.concat(buffers, totalRead); | |
| 576 | 577 | } | |
@@ -583,15 +584,17 @@ async function readFileHandle(filehandle, options) { | |||
| 583 | 584 | result += decoder.end(buffer); | |
| 584 | 585 | return result; | |
| 585 | 586 | } | |
| 586 | - | ||
| 587 | + const readBuffer = bytesRead !== buffer.length ? | ||
| 588 | + buffer.subarray(0, bytesRead) : | ||
| 589 | + buffer; | ||
| 587 | 590 | if (encoding) { | |
| 588 | - result += decoder.write(buffer); | ||
| 591 | + result += decoder.write(readBuffer); | ||
| 589 | 592 | } else if (size !== 0) { | |
| 590 | 593 | offset = totalRead; | |
| 591 | 594 | } else { | |
| 592 | 595 | buffers ??= []; | |
| 593 | 596 | // Unknown file size requires chunks. | |
| 594 | - ArrayPrototypePush(buffers, buffer); | ||
| 597 | + ArrayPrototypePush(buffers, readBuffer); | ||
| 595 | 598 | buffer = Buffer.allocUnsafeSlow(kReadFileUnknownBufferLength); | |
| 596 | 599 | } | |
| 597 | 600 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,46 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + | ||
| 4 | + if (common.isWindows || common.isAIX || common.isIBMi) | ||
| 5 | + common.skip(`No /dev/stdin on ${process.platform}.`); | ||
| 6 | + | ||
| 7 | + const assert = require('assert'); | ||
| 8 | + const fs = require('fs/promises'); | ||
| 9 | + const childType = ['child-encoding', 'child-non-encoding']; | ||
| 10 | + | ||
| 11 | + if (process.argv[2] === childType[0]) { | ||
| 12 | + fs.readFile('/dev/stdin', 'utf8').then((data) => { | ||
| 13 | + process.stdout.write(data); | ||
| 14 | + }); | ||
| 15 | + return; | ||
| 16 | + } else if (process.argv[2] === childType[1]) { | ||
| 17 | + fs.readFile('/dev/stdin').then((data) => { | ||
| 18 | + process.stdout.write(data); | ||
| 19 | + }); | ||
| 20 | + return; | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + const data1 = 'Hello'; | ||
| 24 | + const data2 = 'World'; | ||
| 25 | + const expected = `${data1}\n${data2}\n`; | ||
| 26 | + | ||
| 27 | + const exec = require('child_process').exec; | ||
| 28 | + const f = JSON.stringify(__filename); | ||
| 29 | + const node = JSON.stringify(process.execPath); | ||
| 30 | + | ||
| 31 | + function test(child) { | ||
| 32 | + const cmd = `(echo ${data1}; sleep 0.5; echo ${data2}) | ${node} ${f} ${child}`; | ||
| 33 | + exec(cmd, common.mustSucceed((stdout, stderr) => { | ||
| 34 | + assert.strictEqual( | ||
| 35 | + stdout, | ||
| 36 | + expected, | ||
| 37 | + `expected to read(${child === childType[0] ? 'with' : 'without'} encoding): '${expected}' but got: '${stdout}'`); | ||
| 38 | + assert.strictEqual( | ||
| 39 | + stderr, | ||
| 40 | + '', | ||
| 41 | + `expected not to read anything from stderr but got: '${stderr}'`); | ||
| 42 | + })); | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + test(childType[0]); | ||
| 46 | + test(childType[1]); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments