Skip to content

Commit e3579a0

Browse files
aduh95jasnell
authored andcommitted
fs: handle long files reading in fs.promises
PR-URL: #19643 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 445a89f commit e3579a0

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

lib/fs/promises.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,17 @@ async function readFileHandle(filehandle, options) {
147147

148148
const chunks = [];
149149
const chunkSize = Math.min(size, 16384);
150-
const buf = Buffer.alloc(chunkSize);
151150
let totalRead = 0;
151+
let endOfFile = false;
152152
do {
153+
const buf = Buffer.alloc(chunkSize);
153154
const { bytesRead, buffer } =
154-
await read(filehandle, buf, 0, buf.length);
155-
totalRead = bytesRead;
156-
if (totalRead > 0)
157-
chunks.push(buffer.slice(0, totalRead));
158-
} while (totalRead === chunkSize);
155+
await read(filehandle, buf, 0, chunkSize, totalRead);
156+
totalRead += bytesRead;
157+
endOfFile = bytesRead !== chunkSize;
158+
if (bytesRead > 0)
159+
chunks.push(buffer.slice(0, bytesRead));
160+
} while (!endOfFile);
159161

160162
const result = Buffer.concat(chunks);
161163
if (options.encoding) {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
const assert = require('assert');
6+
const path = require('path');
7+
const { writeFile, readFile } = require('fs/promises');
8+
const tmpdir = require('../common/tmpdir');
9+
tmpdir.refresh();
10+
11+
const fn = path.join(tmpdir.path, 'large-file');
12+
13+
common.crashOnUnhandledRejection();
14+
15+
// Creating large buffer with random content
16+
const buffer = Buffer.from(
17+
Array.apply(null, { length: 16834 * 2 })
18+
.map(Math.random)
19+
.map((number) => (number * (1 << 8)))
20+
);
21+
22+
// Writing buffer to a file then try to read it
23+
writeFile(fn, buffer)
24+
.then(() => readFile(fn))
25+
.then((readBuffer) => {
26+
assert.strictEqual(readBuffer.equals(buffer), true);
27+
})
28+
.then(common.mustCall());

0 commit comments

Comments
 (0)