Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: support fs_event_wrap in the snapshot and load more modules eagerly #38737

Closed
wants to merge 3 commits into from
Closed
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
11 changes: 3 additions & 8 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,14 @@ const {
validateInteger,
} = require('internal/validators');

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

let truncateWarn = true;
let fs;

// Lazy loaded
let promises = null;
let watchers;
let ReadFileContext;
let ReadStream;
let WriteStream;
let rimraf;
Expand Down Expand Up @@ -369,8 +370,6 @@ function checkAborted(signal, callback) {
function readFile(path, options, callback) {
callback = maybeCallback(callback || options);
options = getOptions(options, { flag: 'r' });
if (!ReadFileContext)
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 @@ -2234,8 +2233,6 @@ function watch(filename, options, listener) {
if (options.recursive === undefined) options.recursive = false;
if (options.recursive && !(isOSX || isWindows))
throw new ERR_FEATURE_UNAVAILABLE_ON_PLATFORM('watch recursively');
if (!watchers)
watchers = require('internal/fs/watchers');
const watcher = new watchers.FSWatcher();
watcher[watchers.kFSWatchStart](filename,
options.persistent,
Expand Down Expand Up @@ -2301,8 +2298,6 @@ function watchFile(filename, options, listener) {
stat = statWatchers.get(filename);

if (stat === undefined) {
if (!watchers)
watchers = require('internal/fs/watchers');
stat = new watchers.StatWatcher(options.bigint);
stat[watchers.kFSStatWatcherStart](filename,
options.persistent, options.interval);
Expand Down
5 changes: 1 addition & 4 deletions lib/internal/crypto/cipher.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ const LazyTransform = require('internal/streams/lazy_transform');

const { normalizeEncoding } = require('internal/util');

// Lazy loaded for startup performance.
let StringDecoder;
const { StringDecoder } = require('string_decoder');

function rsaFunctionFor(method, defaultPadding, keyType) {
return (options, buffer) => {
Expand Down Expand Up @@ -93,8 +92,6 @@ const privateDecrypt = rsaFunctionFor(_privateDecrypt, RSA_PKCS1_OAEP_PADDING,

function getDecoder(decoder, encoding) {
encoding = normalizeEncoding(encoding);
if (StringDecoder === undefined)
StringDecoder = require('string_decoder').StringDecoder;
decoder = decoder || new StringDecoder(encoding);
assert(decoder.encoding === encoding, 'Cannot change encoding');
return decoder;
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
SymbolAsyncIterator,
} = primordials;

let eos;
const eos = require('internal/streams/end-of-stream');

const { once } = require('internal/util');
const destroyImpl = require('internal/streams/destroy');
Expand Down Expand Up @@ -39,7 +39,6 @@ function destroyer(stream, reading, writing, callback) {
finished = true;
});

if (eos === undefined) eos = require('internal/streams/end-of-stream');
eos(stream, { readable: reading, writable: writing }, (err) => {
finished = !err;

Expand Down
12 changes: 2 additions & 10 deletions lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ const {

const kPaused = Symbol('kPaused');

// Lazy loaded to improve the startup performance.
let StringDecoder;
let from;
const { StringDecoder } = require('string_decoder');
const from = require('internal/streams/from');

ObjectSetPrototypeOf(Readable.prototype, Stream.prototype);
ObjectSetPrototypeOf(Readable, Stream);
Expand Down Expand Up @@ -170,8 +169,6 @@ function ReadableState(options, stream, isDuplex) {
this.decoder = null;
this.encoding = null;
if (options && options.encoding) {
if (!StringDecoder)
StringDecoder = require('string_decoder').StringDecoder;
this.decoder = new StringDecoder(options.encoding);
this.encoding = options.encoding;
}
Expand Down Expand Up @@ -333,8 +330,6 @@ Readable.prototype.isPaused = function() {

// Backwards compatibility.
Readable.prototype.setEncoding = function(enc) {
if (!StringDecoder)
StringDecoder = require('string_decoder').StringDecoder;
const decoder = new StringDecoder(enc);
this._readableState.decoder = decoder;
// If setEncoding(null), decoder.encoding equals utf8.
Expand Down Expand Up @@ -1340,8 +1335,5 @@ function endWritableNT(state, stream) {
}

Readable.from = function(iterable, opts) {
if (from === undefined) {
from = require('internal/streams/from');
}
return from(Readable, iterable, opts);
};
14 changes: 4 additions & 10 deletions lib/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ const pipeline = require('internal/streams/pipeline');
const eos = require('internal/streams/end-of-stream');
const internalBuffer = require('internal/buffer');

// Lazy loaded
let promises = null;
const promises = require('stream/promises');

const Stream = module.exports = require('internal/streams/legacy').Stream;
Stream.Readable = require('internal/streams/readable');
Expand All @@ -47,30 +46,25 @@ const { addAbortSignal } = require('internal/streams/add-abort-signal');
Stream.addAbortSignal = addAbortSignal;
Stream.finished = eos;

function lazyLoadPromises() {
if (promises === null) promises = require('stream/promises');
return promises;
}

ObjectDefineProperty(Stream, 'promises', {
configurable: true,
enumerable: true,
get() {
return lazyLoadPromises();
return promises;
}
});

ObjectDefineProperty(pipeline, customPromisify, {
enumerable: true,
get() {
return lazyLoadPromises().pipeline;
return promises.pipeline;
}
});

ObjectDefineProperty(eos, customPromisify, {
enumerable: true,
get() {
return lazyLoadPromises().finished;
return promises.finished;
}
});

Expand Down
6 changes: 2 additions & 4 deletions lib/stream/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ const {
isStream,
} = require('internal/streams/utils');

let pl;
let eos;
const pl = require('internal/streams/pipeline');
const eos = require('internal/streams/end-of-stream');

function pipeline(...streams) {
if (!pl) pl = require('internal/streams/pipeline');
return new Promise((resolve, reject) => {
let signal;
const lastArg = streams[streams.length - 1];
Expand All @@ -47,7 +46,6 @@ function pipeline(...streams) {
}

function finished(stream, opts) {
if (!eos) eos = require('internal/streams/end-of-stream');
return new Promise((resolve, reject) => {
eos(stream, opts, (err) => {
if (err) {
Expand Down
13 changes: 11 additions & 2 deletions src/fs_event_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

#include "async_wrap-inl.h"
#include "env-inl.h"
#include "node.h"
#include "handle_wrap.h"
#include "node.h"
#include "node_external_reference.h"
#include "string_bytes.h"


namespace node {

using v8::Context;
Expand All @@ -52,6 +52,7 @@ class FSEventWrap: public HandleWrap {
Local<Value> unused,
Local<Context> context,
void* priv);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
static void New(const FunctionCallbackInfo<Value>& args);
static void Start(const FunctionCallbackInfo<Value>& args);
static void GetInitialized(const FunctionCallbackInfo<Value>& args);
Expand Down Expand Up @@ -117,6 +118,12 @@ void FSEventWrap::Initialize(Local<Object> target,
env->SetConstructorFunction(target, "FSEvent", t);
}

void FSEventWrap::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(New);
registry->Register(Start);
registry->Register(GetInitialized);
}

void FSEventWrap::New(const FunctionCallbackInfo<Value>& args) {
CHECK(args.IsConstructCall());
Expand Down Expand Up @@ -229,3 +236,5 @@ void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename,
} // namespace node

NODE_MODULE_CONTEXT_AWARE_INTERNAL(fs_event_wrap, node::FSEventWrap::Initialize)
NODE_MODULE_EXTERNAL_REFERENCE(fs_event_wrap,
node::FSEventWrap::RegisterExternalReferences)
1 change: 1 addition & 0 deletions src/node_external_reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class ExternalReferenceRegistry {
V(errors) \
V(fs) \
V(fs_dir) \
V(fs_event_wrap) \
V(handle_wrap) \
V(heap_utils) \
V(messaging) \
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-bootstrap-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const expectedModules = new Set([
'NativeModule internal/fs/dir',
'NativeModule internal/fs/utils',
'NativeModule internal/fs/promises',
'NativeModule internal/fs/read_file_context',
'NativeModule internal/fs/rimraf',
'NativeModule internal/fs/watchers',
'NativeModule internal/heap_utils',
Expand Down Expand Up @@ -92,6 +93,7 @@ const expectedModules = new Set([
'NativeModule internal/streams/destroy',
'NativeModule internal/streams/duplex',
'NativeModule internal/streams/end-of-stream',
'NativeModule internal/streams/from',
'NativeModule internal/streams/legacy',
'NativeModule internal/streams/passthrough',
'NativeModule internal/streams/pipeline',
Expand All @@ -115,6 +117,8 @@ const expectedModules = new Set([
'NativeModule async_hooks',
'NativeModule path',
'NativeModule stream',
'NativeModule stream/promises',
'NativeModule string_decoder',
'NativeModule timers',
'NativeModule url',
'NativeModule util',
Expand Down