From a4d6f78619ca63b512c1bfe134b90d3ff4696584 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sat, 3 Apr 2021 21:14:32 +0530 Subject: [PATCH] fs: move constants to internal/fs/utils.js Refs: https://github.com/nodejs/node/pull/38004#discussion_r604951958 PR-URL: https://github.com/nodejs/node/pull/38061 Backport-PR-URL: https://github.com/nodejs/node/pull/39838 Reviewed-By: Antoine du Hamel Reviewed-By: Nitzan Uziely Reviewed-By: James M Snell --- lib/fs.js | 10 ++++------ lib/internal/fs/promises.js | 16 ++++++---------- lib/internal/fs/read_file_context.js | 16 +++++++--------- lib/internal/fs/utils.js | 21 +++++++++++++++++++++ 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 6c402d1e6b7335..836550e7c283cb 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -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. @@ -79,6 +75,10 @@ const { FSReqCallback, statValues } = binding; const { toPathIfFileURL } = require('internal/url'); const internalUtil = require('internal/util'); const { + constants: { + kIoMaxLength, + kMaxUserId, + }, copyObject, Dirent, getDirents, @@ -121,8 +121,6 @@ const { validateInteger, validateInt32 } = require('internal/validators'); -// 2 ** 32 - 1 -const kMaxUserId = 4294967295; let truncateWarn = true; let fs; diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 767195cc2ec738..c82f8270dd9066 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -1,17 +1,7 @@ '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; - -// Note: This is different from kReadFileBufferLength used for non-promisified -// fs.readFile. -const kReadFileMaxChunkSize = 2 ** 14; const kWriteFileMaxChunkSize = 2 ** 14; -// 2 ** 32 - 1 -const kMaxUserId = 4294967295; - const { Error, MathMax, @@ -44,6 +34,12 @@ const { const { isArrayBufferView } = require('internal/util/types'); const { rimrafPromises } = require('internal/fs/rimraf'); const { + constants: { + kIoMaxLength, + kMaxUserId, + kReadFileBufferLength, + kReadFileUnknownBufferLength, + }, copyObject, getDirents, getOptions, diff --git a/lib/internal/fs/read_file_context.js b/lib/internal/fs/read_file_context.js index faca0e0c331e39..4976a7a1deb8d8 100644 --- a/lib/internal/fs/read_file_context.js +++ b/lib/internal/fs/read_file_context.js @@ -4,6 +4,13 @@ const { MathMin, } = primordials; +const { + constants: { + kReadFileBufferLength, + kReadFileUnknownBufferLength, + } +} = require('internal/fs/utils'); + const { Buffer } = require('buffer'); const { FSReqCallback, close, read } = internalBinding('fs'); @@ -18,15 +25,6 @@ const lazyDOMException = hideStackFrames((message, name) => { return new DOMException(message, name); }); -// 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; diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index d3f4c621e4dd56..1d4edc0e011e4c 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -113,6 +113,21 @@ 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 kMaxUserId = 2 ** 32 - 1; + const isWindows = process.platform === 'win32'; let fs; @@ -815,6 +830,12 @@ const validatePosition = hideStackFrames((position, name) => { }); module.exports = { + constants: { + kIoMaxLength, + kMaxUserId, + kReadFileBufferLength, + kReadFileUnknownBufferLength, + }, assertEncoding, BigIntStats, // for testing copyObject,