Skip to content

Commit 24fd791

Browse files
RaisinTenjasnell
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 e155b1f commit 24fd791

File tree

4 files changed

+42
-26
lines changed

4 files changed

+42
-26
lines changed

Diff for: lib/fs.js

+4-6
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;

Diff for: lib/internal/fs/promises.js

+7-11
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,

Diff for: lib/internal/fs/read_file_context.js

+7-9
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

Diff for: lib/internal/fs/utils.js

+24
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,23 @@ 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+
const kMaxUserId = 2 ** 32 - 1;
139+
123140
const isWindows = process.platform === 'win32';
124141

125142
let fs;
@@ -843,6 +860,13 @@ const validatePosition = hideStackFrames((position, name) => {
843860
});
844861

845862
module.exports = {
863+
constants: {
864+
kIoMaxLength,
865+
kMaxUserId,
866+
kReadFileBufferLength,
867+
kReadFileUnknownBufferLength,
868+
kWriteFileMaxChunkSize,
869+
},
846870
assertEncoding,
847871
BigIntStats, // for testing
848872
copyObject,

0 commit comments

Comments
 (0)