bootstrap: lazy load non-essential modules by joyeecheung · Pull Request #45659 · nodejs/node · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions lib/buffer.js
27 changes: 12 additions & 15 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ const {

const pathModule = require('path');
const { isArrayBufferView } = require('internal/util/types');
const nonNativeWatcher = require('internal/fs/recursive_watch');

// We need to get the statValues from the binding at the callsite since
// it's re-initialized after deserialization.
Expand All @@ -84,6 +83,7 @@ const {
custom: kCustomPromisifiedSymbol,
},
SideEffectFreeRegExpPrototypeExec,
defineLazyProperties,
} = require('internal/util');
const {
constants: {
Expand Down Expand Up @@ -119,11 +119,6 @@ const {
validateStringAfterArrayBufferView,
warnOnNonPortableTemplate
} = require('internal/fs/utils');
const {
Dir,
opendir,
opendirSync
} = require('internal/fs/dir');
const {
CHAR_FORWARD_SLASH,
CHAR_BACKWARD_SLASH,
Expand All @@ -140,9 +135,6 @@ const {
validateString,
} = require('internal/validators');

const watchers = require('internal/fs/watchers');
const ReadFileContext = require('internal/fs/read_file_context');

let truncateWarn = true;
let fs;

Expand Down Expand Up @@ -379,6 +371,7 @@ function checkAborted(signal, callback) {
function readFile(path, options, callback) {
callback = maybeCallback(callback || options);
options = getOptions(options, { flag: 'r' });
const ReadFileContext = require('internal/fs/read_file_context');
const context = new ReadFileContext(callback, options.encoding);
context.isUserFd = isFd(path); // File descriptor ownership

Expand Down Expand Up @@ -2298,11 +2291,12 @@ function watch(filename, options, listener) {
if (options.recursive === undefined) options.recursive = false;

let watcher;

const watchers = require('internal/fs/watchers');
// TODO(anonrig): Remove non-native watcher when/if libuv supports recursive.
// As of November 2022, libuv does not support recursive file watch on all platforms,
// e.g. Linux due to the limitations of inotify.
if (options.recursive && !isOSX && !isWindows) {
const nonNativeWatcher = require('internal/fs/recursive_watch');
watcher = new nonNativeWatcher.FSWatcher(options);
watcher[watchers.kFSWatchStart](filename);
} else {
Expand Down Expand Up @@ -2370,7 +2364,7 @@ function watchFile(filename, options, listener) {
validateFunction(listener, 'listener');

stat = statWatchers.get(filename);

const watchers = require('internal/fs/watchers');
if (stat === undefined) {
stat = new watchers.StatWatcher(options.bigint);
stat[watchers.kFSStatWatcherStart](filename,
Expand All @@ -2396,7 +2390,7 @@ function unwatchFile(filename, listener) {
const stat = statWatchers.get(filename);

if (stat === undefined) return;

const watchers = require('internal/fs/watchers');
if (typeof listener === 'function') {
const beforeListenerCount = stat.listenerCount('change');
stat.removeListener('change', listener);
Expand Down Expand Up @@ -3000,8 +2994,6 @@ module.exports = fs = {
mkdtempSync,
open,
openSync,
opendir,
opendirSync,
readdir,
readdirSync,
read,
Expand Down Expand Up @@ -3039,7 +3031,6 @@ module.exports = fs = {
writeSync,
writev,
writevSync,
Dir,
Dirent,
Stats,

Expand Down Expand Up @@ -3085,6 +3076,12 @@ module.exports = fs = {
_toUnixTimestamp: toUnixTimestamp
};

defineLazyProperties(
fs,
'internal/fs/dir',
['Dir', 'opendir', 'opendirSync']
);

ObjectDefineProperties(fs, {
F_OK: { __proto__: null, enumerable: true, value: F_OK || 0 },
R_OK: { __proto__: null, enumerable: true, value: R_OK || 0 },
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ const {

const { exitCodes: { kGenericUserError } } = internalBinding('errors');

const promiseHooks = require('internal/promise_hooks');

const async_wrap = internalBinding('async_wrap');
const { setCallbackTrampoline } = async_wrap;
/* async_hook_fields is a Uint32Array wrapping the uint32_t array of
Expand Down Expand Up @@ -384,6 +382,7 @@ function updatePromiseHookMode() {
initHook = destroyTracking;
}
if (stopPromiseHook) stopPromiseHook();
const promiseHooks = require('internal/promise_hooks');
stopPromiseHook = promiseHooks.createHook({
init: initHook,
before: promiseBeforeHook,
Expand Down
186 changes: 62 additions & 124 deletions lib/internal/bootstrap/browser.js
Loading