Skip to content

Commit 9ea02ac

Browse files
committed
add large file support to callback-based readFile implementation
1 parent fab698d commit 9ea02ac

File tree

2 files changed

+13
-27
lines changed

2 files changed

+13
-27
lines changed

lib/fs.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ const {
7272
aggregateTwoErrors,
7373
codes: {
7474
ERR_ACCESS_DENIED,
75-
ERR_FS_FILE_TOO_LARGE,
7675
ERR_INVALID_ARG_VALUE,
7776
},
7877
} = require('internal/errors');
@@ -97,7 +96,6 @@ const {
9796
} = require('internal/util');
9897
const {
9998
constants: {
100-
kIoMaxLength,
10199
kMaxUserId,
102100
},
103101
copyObject,
@@ -318,11 +316,6 @@ function readFileAfterStat(err, stats) {
318316
// stringify operations vs multiple C++/JS boundary crossings).
319317
const size = context.size = isFileType(stats, S_IFREG) ? stats[8] : 0;
320318

321-
if (size > kIoMaxLength) {
322-
err = new ERR_FS_FILE_TOO_LARGE(size);
323-
return context.close(err);
324-
}
325-
326319
try {
327320
if (size === 0) {
328321
// TODO(BridgeAR): If an encoding is set, use the StringDecoder to concat
@@ -399,9 +392,6 @@ function tryCreateBuffer(size, fd, isUserFd) {
399392
let threw = true;
400393
let buffer;
401394
try {
402-
if (size > kIoMaxLength) {
403-
throw new ERR_FS_FILE_TOO_LARGE(size);
404-
}
405395
buffer = Buffer.allocUnsafe(size);
406396
threw = false;
407397
} finally {

test/parallel/test-fs-readfile.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const common = require('../common');
66

77
const tmpdir = require('../../test/common/tmpdir');
88
const assert = require('assert');
9+
const path = require('node:path');
910
const fs = require('fs');
1011

1112
const prefix = `.removeme-fs-readfile-${process.pid}`;
@@ -52,26 +53,21 @@ for (const e of fileInfo) {
5253
}));
5354
}
5455

55-
// readFile() and readFileSync() should fail if the file is too big.
56+
// Test to verify that readFile() and readFileSync() can handle large files
5657
{
57-
const kIoMaxLength = 2 ** 31 - 1;
58+
const kLargeFileSize = 3 * 1024 * 1024 * 1024; // 3 GiB
5859

59-
if (!tmpdir.hasEnoughSpace(kIoMaxLength)) {
60-
// truncateSync() will fail with ENOSPC if there is not enough space.
61-
common.printSkipMessage(`Not enough space in ${tmpdir.path}`);
62-
} else {
63-
const file = tmpdir.resolve(`${prefix}-too-large.txt`);
64-
fs.writeFileSync(file, Buffer.from('0'));
65-
fs.truncateSync(file, kIoMaxLength + 1);
60+
const file = path.join(tmpdir.path, 'temp-large-file.txt');
61+
fs.writeFileSync(file, Buffer.alloc(1024));
62+
fs.truncateSync(file, kLargeFileSize);
6663

67-
fs.readFile(file, common.expectsError({
68-
code: 'ERR_FS_FILE_TOO_LARGE',
69-
name: 'RangeError',
70-
}));
71-
assert.throws(() => {
72-
fs.readFileSync(file);
73-
}, { code: 'ERR_FS_FILE_TOO_LARGE', name: 'RangeError' });
74-
}
64+
fs.readFile(file, (err, data) => {
65+
if (err) {
66+
console.error('Error reading file:', err);
67+
} else {
68+
console.log('File read successfully:', data.length);
69+
}
70+
});
7571
}
7672

7773
{

0 commit comments

Comments
 (0)