Skip to content

Commit

Permalink
fs: move constants to internal/fs/utils.js
Browse files Browse the repository at this point in the history
Refs: #38004 (comment)

PR-URL: #38061
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Nitzan Uziely <linkgoron@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
RaisinTen authored and jasnell committed Apr 6, 2021
1 parent e155b1f commit 24fd791
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 26 deletions.
10 changes: 4 additions & 6 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@

'use strict';

// Most platforms don't allow reads or writes >= 2 GB.
// See https://github.com/libuv/libuv/pull/1501.
const kIoMaxLength = 2 ** 31 - 1;

// When using FSReqCallback, make sure to create the object only *after* all
// parameter validation has happened, so that the objects are not kept in memory
// in case they are created but never used due to an exception.
Expand Down Expand Up @@ -90,6 +86,10 @@ const { FSReqCallback } = binding;
const { toPathIfFileURL } = require('internal/url');
const internalUtil = require('internal/util');
const {
constants: {
kIoMaxLength,
kMaxUserId,
},
copyObject,
Dirent,
emitRecursiveRmdirWarning,
Expand Down Expand Up @@ -136,8 +136,6 @@ const {
validateFunction,
validateInteger,
} = require('internal/validators');
// 2 ** 32 - 1
const kMaxUserId = 4294967295;

let truncateWarn = true;
let fs;
Expand Down
18 changes: 7 additions & 11 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
'use strict';

// Most platforms don't allow reads or writes >= 2 GB.
// See https://github.com/libuv/libuv/pull/1501.
const kIoMaxLength = 2 ** 31 - 1;

const kReadFileBufferLength = 512 * 1024;
const kReadFileUnknownBufferLength = 64 * 1024;
const kWriteFileMaxChunkSize = 512 * 1024;

// 2 ** 32 - 1
const kMaxUserId = 4294967295;

const {
ArrayPrototypePush,
Error,
Expand Down Expand Up @@ -48,6 +37,13 @@ const {
const { isArrayBufferView } = require('internal/util/types');
const { rimrafPromises } = require('internal/fs/rimraf');
const {
constants: {
kIoMaxLength,
kMaxUserId,
kReadFileBufferLength,
kReadFileUnknownBufferLength,
kWriteFileMaxChunkSize,
},
copyObject,
emitRecursiveRmdirWarning,
getDirents,
Expand Down
16 changes: 7 additions & 9 deletions lib/internal/fs/read_file_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ const {
ReflectApply,
} = primordials;

const {
constants: {
kReadFileBufferLength,
kReadFileUnknownBufferLength,
}
} = require('internal/fs/utils');

const { Buffer } = require('buffer');

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

// Use 64kb in case the file type is not a regular file and thus do not know the
// actual file size. Increasing the value further results in more frequent over
// allocation for small files and consumes CPU time and memory that should be
// used else wise.
// Use up to 512kb per read otherwise to partition reading big files to prevent
// blocking other threads in case the available threads are all in use.
const kReadFileUnknownBufferLength = 64 * 1024;
const kReadFileBufferLength = 512 * 1024;

function readFileAfterRead(err, bytesRead) {
const context = this.context;

Expand Down
24 changes: 24 additions & 0 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,23 @@ const kMaximumCopyMode = COPYFILE_EXCL |
COPYFILE_FICLONE |
COPYFILE_FICLONE_FORCE;

// Most platforms don't allow reads or writes >= 2 GB.
// See https://github.com/libuv/libuv/pull/1501.
const kIoMaxLength = 2 ** 31 - 1;

// Use 64kb in case the file type is not a regular file and thus do not know the
// actual file size. Increasing the value further results in more frequent over
// allocation for small files and consumes CPU time and memory that should be
// used else wise.
// Use up to 512kb per read otherwise to partition reading big files to prevent
// blocking other threads in case the available threads are all in use.
const kReadFileUnknownBufferLength = 64 * 1024;
const kReadFileBufferLength = 512 * 1024;

const kWriteFileMaxChunkSize = 512 * 1024;

const kMaxUserId = 2 ** 32 - 1;

const isWindows = process.platform === 'win32';

let fs;
Expand Down Expand Up @@ -843,6 +860,13 @@ const validatePosition = hideStackFrames((position, name) => {
});

module.exports = {
constants: {
kIoMaxLength,
kMaxUserId,
kReadFileBufferLength,
kReadFileUnknownBufferLength,
kWriteFileMaxChunkSize,
},
assertEncoding,
BigIntStats, // for testing
copyObject,
Expand Down

0 comments on commit 24fd791

Please sign in to comment.