mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
fs: handle long files reading in fs.promises
PR-URL: https://github.com/nodejs/node/pull/19643 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
parent
85e1819d8b
commit
11819c7773
@ -147,15 +147,17 @@ async function readFileHandle(filehandle, options) {
|
|||||||
|
|
||||||
const chunks = [];
|
const chunks = [];
|
||||||
const chunkSize = Math.min(size, 16384);
|
const chunkSize = Math.min(size, 16384);
|
||||||
const buf = Buffer.alloc(chunkSize);
|
|
||||||
let totalRead = 0;
|
let totalRead = 0;
|
||||||
|
let endOfFile = false;
|
||||||
do {
|
do {
|
||||||
|
const buf = Buffer.alloc(chunkSize);
|
||||||
const { bytesRead, buffer } =
|
const { bytesRead, buffer } =
|
||||||
await read(filehandle, buf, 0, buf.length);
|
await read(filehandle, buf, 0, chunkSize, totalRead);
|
||||||
totalRead = bytesRead;
|
totalRead += bytesRead;
|
||||||
if (totalRead > 0)
|
endOfFile = bytesRead !== chunkSize;
|
||||||
chunks.push(buffer.slice(0, totalRead));
|
if (bytesRead > 0)
|
||||||
} while (totalRead === chunkSize);
|
chunks.push(buffer.slice(0, bytesRead));
|
||||||
|
} while (!endOfFile);
|
||||||
|
|
||||||
const result = Buffer.concat(chunks);
|
const result = Buffer.concat(chunks);
|
||||||
if (options.encoding) {
|
if (options.encoding) {
|
||||||
|
28
test/parallel/test-fs-promises-readfile.js
Normal file
28
test/parallel/test-fs-promises-readfile.js
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const common = require('../common');
|
||||||
|
|
||||||
|
const assert = require('assert');
|
||||||
|
const path = require('path');
|
||||||
|
const { writeFile, readFile } = require('fs/promises');
|
||||||
|
const tmpdir = require('../common/tmpdir');
|
||||||
|
tmpdir.refresh();
|
||||||
|
|
||||||
|
const fn = path.join(tmpdir.path, 'large-file');
|
||||||
|
|
||||||
|
common.crashOnUnhandledRejection();
|
||||||
|
|
||||||
|
// Creating large buffer with random content
|
||||||
|
const buffer = Buffer.from(
|
||||||
|
Array.apply(null, { length: 16834 * 2 })
|
||||||
|
.map(Math.random)
|
||||||
|
.map((number) => (number * (1 << 8)))
|
||||||
|
);
|
||||||
|
|
||||||
|
// Writing buffer to a file then try to read it
|
||||||
|
writeFile(fn, buffer)
|
||||||
|
.then(() => readFile(fn))
|
||||||
|
.then((readBuffer) => {
|
||||||
|
assert.strictEqual(readBuffer.equals(buffer), true);
|
||||||
|
})
|
||||||
|
.then(common.mustCall());
|
Loading…
Reference in New Issue
Block a user