Skip to content

Commit 07d5dd2

Browse files
RaisinTenMoritzLoewenstein
authored andcommitted
fs: move constants to internal/fs/utils.js
Refs: #38004 (comment) PR-URL: #38061 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Nitzan Uziely <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 327838d commit 07d5dd2

File tree

4 files changed

+38
-25
lines changed

4 files changed

+38
-25
lines changed

lib/fs.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424

2525
'use strict';
2626

27-
// Most platforms don't allow reads or writes >= 2 GB.
28-
// See https://github.com/libuv/libuv/pull/1501.
29-
const kIoMaxLength = 2 ** 31 - 1;
30-
3127
// When using FSReqCallback, make sure to create the object only *after* all
3228
// parameter validation has happened, so that the objects are not kept in memory
3329
// in case they are created but never used due to an exception.
@@ -79,6 +75,10 @@ const { FSReqCallback, statValues } = binding;
7975
const { toPathIfFileURL } = require('internal/url');
8076
const internalUtil = require('internal/util');
8177
const {
78+
constants: {
79+
kIoMaxLength,
80+
kMaxUserId,
81+
},
8282
copyObject,
8383
Dirent,
8484
getDirents,
@@ -121,8 +121,6 @@ const {
121121
validateInteger,
122122
validateInt32
123123
} = require('internal/validators');
124-
// 2 ** 32 - 1
125-
const kMaxUserId = 4294967295;
126124

127125
let truncateWarn = true;
128126
let fs;

lib/internal/fs/promises.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
'use strict';
22

3-
// Most platforms don't allow reads or writes >= 2 GB.
4-
// See https://github.com/libuv/libuv/pull/1501.
5-
const kIoMaxLength = 2 ** 31 - 1;
6-
7-
// Note: This is different from kReadFileBufferLength used for non-promisified
8-
// fs.readFile.
9-
const kReadFileMaxChunkSize = 2 ** 14;
103
const kWriteFileMaxChunkSize = 2 ** 14;
114

12-
// 2 ** 32 - 1
13-
const kMaxUserId = 4294967295;
14-
155
const {
166
Error,
177
MathMax,
@@ -44,6 +34,12 @@ const {
4434
const { isArrayBufferView } = require('internal/util/types');
4535
const { rimrafPromises } = require('internal/fs/rimraf');
4636
const {
37+
constants: {
38+
kIoMaxLength,
39+
kMaxUserId,
40+
kReadFileBufferLength,
41+
kReadFileUnknownBufferLength,
42+
},
4743
copyObject,
4844
getDirents,
4945
getOptions,

lib/internal/fs/read_file_context.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ const {
44
MathMin,
55
} = primordials;
66

7+
const {
8+
constants: {
9+
kReadFileBufferLength,
10+
kReadFileUnknownBufferLength,
11+
}
12+
} = require('internal/fs/utils');
13+
714
const { Buffer } = require('buffer');
815

916
const { FSReqCallback, close, read } = internalBinding('fs');
@@ -18,15 +25,6 @@ const lazyDOMException = hideStackFrames((message, name) => {
1825
return new DOMException(message, name);
1926
});
2027

21-
// Use 64kb in case the file type is not a regular file and thus do not know the
22-
// actual file size. Increasing the value further results in more frequent over
23-
// allocation for small files and consumes CPU time and memory that should be
24-
// used else wise.
25-
// Use up to 512kb per read otherwise to partition reading big files to prevent
26-
// blocking other threads in case the available threads are all in use.
27-
const kReadFileUnknownBufferLength = 64 * 1024;
28-
const kReadFileBufferLength = 512 * 1024;
29-
3028
function readFileAfterRead(err, bytesRead) {
3129
const context = this.context;
3230

lib/internal/fs/utils.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,21 @@ const kMaximumCopyMode = COPYFILE_EXCL |
113113
COPYFILE_FICLONE |
114114
COPYFILE_FICLONE_FORCE;
115115

116+
// Most platforms don't allow reads or writes >= 2 GB.
117+
// See https://github.com/libuv/libuv/pull/1501.
118+
const kIoMaxLength = 2 ** 31 - 1;
119+
120+
// Use 64kb in case the file type is not a regular file and thus do not know the
121+
// actual file size. Increasing the value further results in more frequent over
122+
// allocation for small files and consumes CPU time and memory that should be
123+
// used else wise.
124+
// Use up to 512kb per read otherwise to partition reading big files to prevent
125+
// blocking other threads in case the available threads are all in use.
126+
const kReadFileUnknownBufferLength = 64 * 1024;
127+
const kReadFileBufferLength = 512 * 1024;
128+
129+
const kMaxUserId = 2 ** 32 - 1;
130+
116131
const isWindows = process.platform === 'win32';
117132

118133
let fs;
@@ -815,6 +830,12 @@ const validatePosition = hideStackFrames((position, name) => {
815830
});
816831

817832
module.exports = {
833+
constants: {
834+
kIoMaxLength,
835+
kMaxUserId,
836+
kReadFileBufferLength,
837+
kReadFileUnknownBufferLength,
838+
},
818839
assertEncoding,
819840
BigIntStats, // for testing
820841
copyObject,

0 commit comments

Comments
 (0)