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:
Antoine du HAMEL 2018-03-28 19:15:52 +02:00 committed by Timothy Gu
parent 85e1819d8b
commit 11819c7773
No known key found for this signature in database
GPG Key ID: 7FE6B095B582B0D4
2 changed files with 36 additions and 6 deletions

View File

@ -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) {

View 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());