Skip to content

Commit fcdbf00

Browse files
committed
fs: move constants to internal/fs/utils.js
Refs: nodejs#38004 (comment)
1 parent 5dae7d6 commit fcdbf00

File tree

4 files changed

+43
-26
lines changed

4 files changed

+43
-26
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.
@@ -90,6 +86,10 @@ const { FSReqCallback } = binding;
9086
const { toPathIfFileURL } = require('internal/url');
9187
const internalUtil = require('internal/util');
9288
const {
89+
constants: {
90+
kIoMaxLength,
91+
kMaxUserId,
92+
},
9393
copyObject,
9494
Dirent,
9595
emitRecursiveRmdirWarning,
@@ -136,8 +136,6 @@ const {
136136
validateFunction,
137137
validateInteger,
138138
} = require('internal/validators');
139-
// 2 ** 32 - 1
140-
const kMaxUserId = 4294967295;
141139

142140
let truncateWarn = true;
143141
let fs;

lib/internal/fs/promises.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
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-
const kReadFileBufferLength = 512 * 1024;
8-
const kReadFileUnknownBufferLength = 64 * 1024;
9-
const kWriteFileMaxChunkSize = 512 * 1024;
10-
11-
// 2 ** 32 - 1
12-
const kMaxUserId = 4294967295;
13-
143
const {
154
ArrayPrototypePush,
165
Error,
@@ -48,6 +37,13 @@ const {
4837
const { isArrayBufferView } = require('internal/util/types');
4938
const { rimrafPromises } = require('internal/fs/rimraf');
5039
const {
40+
constants: {
41+
kIoMaxLength,
42+
kMaxUserId,
43+
kReadFileBufferLength,
44+
kReadFileUnknownBufferLength,
45+
kWriteFileMaxChunkSize,
46+
},
5147
copyObject,
5248
emitRecursiveRmdirWarning,
5349
getDirents,

lib/internal/fs/read_file_context.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ const {
66
ReflectApply,
77
} = primordials;
88

9+
const {
10+
constants: {
11+
kReadFileBufferLength,
12+
kReadFileUnknownBufferLength,
13+
}
14+
} = require('internal/fs/utils');
15+
916
const { Buffer } = require('buffer');
1017

1118
const { FSReqCallback, close, read } = internalBinding('fs');
@@ -15,15 +22,6 @@ const {
1522
aggregateTwoErrors,
1623
} = require('internal/errors');
1724

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

lib/internal/fs/utils.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,24 @@ const kMaximumCopyMode = COPYFILE_EXCL |
120120
COPYFILE_FICLONE |
121121
COPYFILE_FICLONE_FORCE;
122122

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

125143
let fs;
@@ -843,6 +861,13 @@ const validatePosition = hideStackFrames((position, name) => {
843861
});
844862

845863
module.exports = {
864+
constants: {
865+
kIoMaxLength,
866+
kMaxUserId,
867+
kReadFileBufferLength,
868+
kReadFileUnknownBufferLength,
869+
kWriteFileMaxChunkSize,
870+
},
846871
assertEncoding,
847872
BigIntStats, // for testing
848873
copyObject,

0 commit comments

Comments
 (0)