From 24fd791184ad0ba27f92ac49b820aec78e491296 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 Reviewed-By: Antoine du Hamel Reviewed-By: Nitzan Uziely Reviewed-By: James M Snell --- lib/fs.js | 10 ++++------ lib/internal/fs/promises.js | 18 +++++++----------- lib/internal/fs/read_file_context.js | 16 +++++++--------- lib/internal/fs/utils.js | 24 ++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 26 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index d396d3a80c99c0..6bf068416f1daa 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. @@ -90,6 +86,10 @@ const { FSReqCallback } = binding; const { toPathIfFileURL } = require('internal/url'); const internalUtil = require('internal/util'); const { + constants: { + kIoMaxLength, + kMaxUserId, + }, copyObject, Dirent, emitRecursiveRmdirWarning, @@ -136,8 +136,6 @@ const { validateFunction, validateInteger, } = 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 6daa61e5658067..791bf57a7d70b4 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -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, @@ -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, diff --git a/lib/internal/fs/read_file_context.js b/lib/internal/fs/read_file_context.js index 4c50044368c7ec..afa4b7852f6790 100644 --- a/lib/internal/fs/read_file_context.js +++ b/lib/internal/fs/read_file_context.js @@ -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'); @@ -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; diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index b3efe293ccef99..06cc5e26dd6b9f 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -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; @@ -843,6 +860,13 @@ const validatePosition = hideStackFrames((position, name) => { }); module.exports = { + constants: { + kIoMaxLength, + kMaxUserId, + kReadFileBufferLength, + kReadFileUnknownBufferLength, + kWriteFileMaxChunkSize, + }, assertEncoding, BigIntStats, // for testing copyObject,