GeezerVison is showing github.com — bootstrap: lazy load non-essential modules · nodejs/node@6466acb
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/buffer.js +12-12Lines changed: 12 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -85,6 +85,7 @@ const { | 85 | 85 | lazyDOMException, | 86 | 86 | normalizeEncoding, | 87 | 87 | kIsEncodingSymbol, | | 88 | + defineLazyProperties, | 88 | 89 | } = require('internal/util'); | 89 | 90 | const { | 90 | 91 | isAnyArrayBuffer, | @@ -128,15 +129,6 @@ const { | 128 | 129 | createUnsafeBuffer, | 129 | 130 | } = require('internal/buffer'); | 130 | 131 |
| 131 | | -const { | 132 | | - Blob, | 133 | | - resolveObjectURL, | 134 | | -} = require('internal/blob'); | 135 | | - | 136 | | -const { | 137 | | - File, | 138 | | -} = require('internal/file'); | 139 | | - | 140 | 132 | FastBuffer.prototype.constructor = Buffer; | 141 | 133 | Buffer.prototype = FastBuffer.prototype; | 142 | 134 | addBufferPrototypeMethods(Buffer.prototype); | @@ -1382,9 +1374,6 @@ function isAscii(input) { | 1382 | 1374 | } | 1383 | 1375 |
| 1384 | 1376 | module.exports = { | 1385 | | - Blob, | 1386 | | - File, | 1387 | | - resolveObjectURL, | 1388 | 1377 | Buffer, | 1389 | 1378 | SlowBuffer, | 1390 | 1379 | transcode, | @@ -1413,3 +1402,14 @@ ObjectDefineProperties(module.exports, { | 1413 | 1402 | set(val) { INSPECT_MAX_BYTES = val; }, | 1414 | 1403 | }, | 1415 | 1404 | }); | | 1405 | + | | 1406 | +defineLazyProperties( | | 1407 | + module.exports, | | 1408 | + 'internal/blob', | | 1409 | + ['Blob', 'resolveObjectURL'], | | 1410 | +); | | 1411 | +defineLazyProperties( | | 1412 | + module.exports, | | 1413 | + 'internal/file', | | 1414 | + ['File'], | | 1415 | +); |
Collapse file Copy file name to clipboardExpand all lines: lib/fs.js +12-14Lines changed: 12 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -87,6 +87,7 @@ const { | 87 | 87 | custom: kCustomPromisifiedSymbol, | 88 | 88 | }, | 89 | 89 | SideEffectFreeRegExpPrototypeExec, | | 90 | + defineLazyProperties, | 90 | 91 | } = require('internal/util'); | 91 | 92 | const { | 92 | 93 | constants: { | @@ -125,11 +126,6 @@ const { | 125 | 126 | validatePrimitiveStringAfterArrayBufferView, | 126 | 127 | warnOnNonPortableTemplate, | 127 | 128 | } = require('internal/fs/utils'); | 128 | | -const { | 129 | | - Dir, | 130 | | - opendir, | 131 | | - opendirSync, | 132 | | -} = require('internal/fs/dir'); | 133 | 129 | const { | 134 | 130 | CHAR_FORWARD_SLASH, | 135 | 131 | CHAR_BACKWARD_SLASH, | @@ -146,9 +142,6 @@ const { | 146 | 142 | validateString, | 147 | 143 | } = require('internal/validators'); | 148 | 144 |
| 149 | | -const watchers = require('internal/fs/watchers'); | 150 | | -const ReadFileContext = require('internal/fs/read_file_context'); | 151 | | - | 152 | 145 | let truncateWarn = true; | 153 | 146 | let fs; | 154 | 147 |
| @@ -392,6 +385,7 @@ function checkAborted(signal, callback) { | 392 | 385 | function readFile(path, options, callback) { | 393 | 386 | callback = maybeCallback(callback || options); | 394 | 387 | options = getOptions(options, { flag: 'r' }); | | 388 | + const ReadFileContext = require('internal/fs/read_file_context'); | 395 | 389 | const context = new ReadFileContext(callback, options.encoding); | 396 | 390 | context.isUserFd = isFd(path); // File descriptor ownership | 397 | 391 |
| @@ -2422,12 +2416,13 @@ function watch(filename, options, listener) { | 2422 | 2416 | if (options.recursive === undefined) options.recursive = false; | 2423 | 2417 | if (options.recursive && !(isOSX || isWindows)) | 2424 | 2418 | throw new ERR_FEATURE_UNAVAILABLE_ON_PLATFORM('watch recursively'); | | 2419 | + | | 2420 | + const watchers = require('internal/fs/watchers'); | 2425 | 2421 | const watcher = new watchers.FSWatcher(); | 2426 | 2422 | watcher[watchers.kFSWatchStart](filename, | 2427 | 2423 | options.persistent, | 2428 | 2424 | options.recursive, | 2429 | 2425 | options.encoding); | 2430 | | - | 2431 | 2426 | if (listener) { | 2432 | 2427 | watcher.addListener('change', listener); | 2433 | 2428 | } | @@ -2486,7 +2481,7 @@ function watchFile(filename, options, listener) { | 2486 | 2481 | validateFunction(listener, 'listener'); | 2487 | 2482 |
| 2488 | 2483 | stat = statWatchers.get(filename); | 2489 | | - | | 2484 | + const watchers = require('internal/fs/watchers'); | 2490 | 2485 | if (stat === undefined) { | 2491 | 2486 | stat = new watchers.StatWatcher(options.bigint); | 2492 | 2487 | stat[watchers.kFSStatWatcherStart](filename, | @@ -2512,7 +2507,7 @@ function unwatchFile(filename, listener) { | 2512 | 2507 | const stat = statWatchers.get(filename); | 2513 | 2508 |
| 2514 | 2509 | if (stat === undefined) return; | 2515 | | - | | 2510 | + const watchers = require('internal/fs/watchers'); | 2516 | 2511 | if (typeof listener === 'function') { | 2517 | 2512 | const beforeListenerCount = stat.listenerCount('change'); | 2518 | 2513 | stat.removeListener('change', listener); | @@ -3116,8 +3111,6 @@ module.exports = fs = { | 3116 | 3111 | mkdtempSync, | 3117 | 3112 | open, | 3118 | 3113 | openSync, | 3119 | | - opendir, | 3120 | | - opendirSync, | 3121 | 3114 | readdir, | 3122 | 3115 | readdirSync, | 3123 | 3116 | read, | @@ -3157,7 +3150,6 @@ module.exports = fs = { | 3157 | 3150 | writeSync, | 3158 | 3151 | writev, | 3159 | 3152 | writevSync, | 3160 | | - Dir, | 3161 | 3153 | Dirent, | 3162 | 3154 | Stats, | 3163 | 3155 |
| @@ -3203,6 +3195,12 @@ module.exports = fs = { | 3203 | 3195 | _toUnixTimestamp: toUnixTimestamp, | 3204 | 3196 | }; | 3205 | 3197 |
| | 3198 | +defineLazyProperties( | | 3199 | + fs, | | 3200 | + 'internal/fs/dir', | | 3201 | + ['Dir', 'opendir', 'opendirSync'], | | 3202 | +); | | 3203 | + | 3206 | 3204 | ObjectDefineProperties(fs, { | 3207 | 3205 | F_OK: { __proto__: null, enumerable: true, value: F_OK || 0 }, | 3208 | 3206 | R_OK: { __proto__: null, enumerable: true, value: R_OK || 0 }, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/async_hooks.js +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | Symbol, | 9 | 9 | } = primordials; | 10 | 10 |
| 11 | | -const promiseHooks = require('internal/promise_hooks'); | 12 | | - | 13 | 11 | const async_wrap = internalBinding('async_wrap'); | 14 | 12 | const { setCallbackTrampoline } = async_wrap; | 15 | 13 | /* async_hook_fields is a Uint32Array wrapping the uint32_t array of | @@ -382,6 +380,7 @@ function updatePromiseHookMode() { | 382 | 380 | initHook = destroyTracking; | 383 | 381 | } | 384 | 382 | if (stopPromiseHook) stopPromiseHook(); | | 383 | + const promiseHooks = require('internal/promise_hooks'); | 385 | 384 | stopPromiseHook = promiseHooks.createHook({ | 386 | 385 | init: initHook, | 387 | 386 | before: promiseBeforeHook, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/bootstrap/browser.js +62-100Lines changed: 62 additions & 100 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 | defineOperation, | 10 | 10 | exposeInterface, | 11 | 11 | lazyDOMExceptionClass, | | 12 | + defineLazyProperties, | | 13 | + defineReplaceableLazyAttribute, | | 14 | + exposeLazyInterfaces, | 12 | 15 | } = require('internal/util'); | 13 | 16 | const config = internalBinding('config'); | 14 | 17 |
| @@ -28,56 +31,39 @@ exposeGetterAndSetter(globalThis, | 28 | 31 | exposeInterface(globalThis, 'DOMException', value); | 29 | 32 | }); | 30 | 33 |
| 31 | | -const { | 32 | | - TextEncoder, | 33 | | - TextDecoder, | 34 | | -} = require('internal/encoding'); | 35 | | -// https://encoding.spec.whatwg.org/#textencoder | 36 | | -exposeInterface(globalThis, 'TextEncoder', TextEncoder); | 37 | | -// https://encoding.spec.whatwg.org/#textdecoder | 38 | | -exposeInterface(globalThis, 'TextDecoder', TextDecoder); | 39 | | - | 40 | | -const { | 41 | | - AbortController, | 42 | | - AbortSignal, | 43 | | -} = require('internal/abort_controller'); | 44 | | -exposeInterface(globalThis, 'AbortController', AbortController); | 45 | | -exposeInterface(globalThis, 'AbortSignal', AbortSignal); | 46 | | - | 47 | | -const { | 48 | | - EventTarget, | 49 | | - Event, | 50 | | -} = require('internal/event_target'); | 51 | | -exposeInterface(globalThis, 'EventTarget', EventTarget); | 52 | | -exposeInterface(globalThis, 'Event', Event); | 53 | | -const { | 54 | | - MessageChannel, | 55 | | - MessagePort, | 56 | | - MessageEvent, | 57 | | -} = require('internal/worker/io'); | 58 | | -exposeInterface(globalThis, 'MessageChannel', MessageChannel); | 59 | | -exposeInterface(globalThis, 'MessagePort', MessagePort); | 60 | | -exposeInterface(globalThis, 'MessageEvent', MessageEvent); | 61 | | - | 62 | 34 | // https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope | 63 | 35 | const timers = require('timers'); | 64 | 36 | defineOperation(globalThis, 'clearInterval', timers.clearInterval); | 65 | 37 | defineOperation(globalThis, 'clearTimeout', timers.clearTimeout); | 66 | 38 | defineOperation(globalThis, 'setInterval', timers.setInterval); | 67 | 39 | defineOperation(globalThis, 'setTimeout', timers.setTimeout); | 68 | 40 |
| 69 | | -const buffer = require('buffer'); | 70 | | -defineOperation(globalThis, 'atob', buffer.atob); | 71 | | -defineOperation(globalThis, 'btoa', buffer.btoa); | 72 | | - | | 41 | +// Lazy ones. | | 42 | +exposeLazyInterfaces(globalThis, 'internal/abort_controller', [ | | 43 | + 'AbortController', 'AbortSignal', | | 44 | +]); | | 45 | +exposeLazyInterfaces(globalThis, 'internal/event_target', [ | | 46 | + 'EventTarget', 'Event', | | 47 | +]); | | 48 | +exposeLazyInterfaces(globalThis, 'internal/worker/io', [ | | 49 | + 'MessageChannel', 'MessagePort', 'MessageEvent', | | 50 | +]); | | 51 | +defineLazyProperties(globalThis, 'buffer', ['atob', 'btoa']); | 73 | 52 | // https://www.w3.org/TR/FileAPI/#dfn-Blob | 74 | | -exposeInterface(globalThis, 'Blob', buffer.Blob); | 75 | | - | | 53 | +exposeLazyInterfaces(globalThis, 'internal/blob', ['Blob']); | 76 | 54 | // https://www.w3.org/TR/hr-time-2/#the-performance-attribute | 77 | | -const perf_hooks = require('perf_hooks'); | 78 | | -exposeInterface(globalThis, 'Performance', perf_hooks.Performance); | 79 | | -defineReplacableAttribute(globalThis, 'performance', | 80 | | - perf_hooks.performance); | | 55 | + | | 56 | +exposeLazyInterfaces(globalThis, 'perf_hooks', [ | | 57 | + 'Performance', | | 58 | +]); | | 59 | + | | 60 | +defineReplaceableLazyAttribute(globalThis, 'perf_hooks', ['performance']); | | 61 | + | | 62 | +// https://encoding.spec.whatwg.org/#textencoder | | 63 | +// https://encoding.spec.whatwg.org/#textdecoder | | 64 | +exposeLazyInterfaces(globalThis, | | 65 | + 'internal/encoding', | | 66 | + ['TextEncoder', 'TextDecoder']); | 81 | 67 |
| 82 | 68 | function createGlobalConsole() { | 83 | 69 | const consoleFromNode = | @@ -115,67 +101,43 @@ function exposeGetterAndSetter(target, name, getter, setter = undefined) { | 115 | 101 | }); | 116 | 102 | } | 117 | 103 |
| 118 | | -// https://heycam.github.io/webidl/#Replaceable | 119 | | -function defineReplacableAttribute(target, name, value) { | 120 | | - ObjectDefineProperty(target, name, { | 121 | | - __proto__: null, | 122 | | - writable: true, | 123 | | - enumerable: true, | 124 | | - configurable: true, | 125 | | - value, | 126 | | - }); | 127 | | -} | 128 | | - | 129 | 104 | // Web Streams API | 130 | | -const { | 131 | | - TransformStream, | 132 | | - TransformStreamDefaultController, | 133 | | -} = require('internal/webstreams/transformstream'); | 134 | | - | 135 | | -const { | 136 | | - WritableStream, | 137 | | - WritableStreamDefaultController, | 138 | | - WritableStreamDefaultWriter, | 139 | | -} = require('internal/webstreams/writablestream'); | | 105 | +exposeLazyInterfaces( | | 106 | + globalThis, | | 107 | + 'internal/webstreams/transformstream', | | 108 | + ['TransformStream', 'TransformStreamDefaultController']); | 140 | 109 |
| 141 | | -const { | 142 | | - ReadableStream, | 143 | | - ReadableStreamDefaultReader, | 144 | | - ReadableStreamBYOBReader, | 145 | | - ReadableStreamBYOBRequest, | 146 | | - ReadableByteStreamController, | 147 | | - ReadableStreamDefaultController, | 148 | | -} = require('internal/webstreams/readablestream'); | | 110 | +exposeLazyInterfaces( | | 111 | + globalThis, | | 112 | + 'internal/webstreams/writablestream', | | 113 | + ['WritableStream', 'WritableStreamDefaultController', 'WritableStreamDefaultWriter']); | 149 | 114 |
| 150 | | -const { | 151 | | - ByteLengthQueuingStrategy, | 152 | | - CountQueuingStrategy, | 153 | | -} = require('internal/webstreams/queuingstrategies'); | | 115 | +exposeLazyInterfaces( | | 116 | + globalThis, | | 117 | + 'internal/webstreams/readablestream', | | 118 | + [ | | 119 | + 'ReadableStream', 'ReadableStreamDefaultReader', | | 120 | + 'ReadableStreamBYOBReader', 'ReadableStreamBYOBRequest', | | 121 | + 'ReadableByteStreamController', 'ReadableStreamDefaultController', | | 122 | + ]); | | 123 | + | | 124 | +exposeLazyInterfaces( | | 125 | + globalThis, | | 126 | + 'internal/webstreams/queuingstrategies', | | 127 | + [ | | 128 | + 'ByteLengthQueuingStrategy', 'CountQueuingStrategy', | | 129 | + ]); | 154 | 130 |
| 155 | | -const { | 156 | | - TextEncoderStream, | 157 | | - TextDecoderStream, | 158 | | -} = require('internal/webstreams/encoding'); | | 131 | +exposeLazyInterfaces( | | 132 | + globalThis, | | 133 | + 'internal/webstreams/encoding', | | 134 | + [ | | 135 | + 'TextEncoderStream', 'TextDecoderStream', | | 136 | + ]); | 159 | 137 |
| 160 | | -const { | 161 | | - CompressionStream, | 162 | | - DecompressionStream, | 163 | | -} = require('internal/webstreams/compression'); | 164 | | - | 165 | | -exposeInterface(globalThis, 'ReadableStream', ReadableStream); | 166 | | -exposeInterface(globalThis, 'ReadableStreamDefaultReader', ReadableStreamDefaultReader); | 167 | | -exposeInterface(globalThis, 'ReadableStreamBYOBReader', ReadableStreamBYOBReader); | 168 | | -exposeInterface(globalThis, 'ReadableStreamBYOBRequest', ReadableStreamBYOBRequest); | 169 | | -exposeInterface(globalThis, 'ReadableByteStreamController', ReadableByteStreamController); | 170 | | -exposeInterface(globalThis, 'ReadableStreamDefaultController', ReadableStreamDefaultController); | 171 | | -exposeInterface(globalThis, 'TransformStream', TransformStream); | 172 | | -exposeInterface(globalThis, 'TransformStreamDefaultController', TransformStreamDefaultController); | 173 | | -exposeInterface(globalThis, 'WritableStream', WritableStream); | 174 | | -exposeInterface(globalThis, 'WritableStreamDefaultWriter', WritableStreamDefaultWriter); | 175 | | -exposeInterface(globalThis, 'WritableStreamDefaultController', WritableStreamDefaultController); | 176 | | -exposeInterface(globalThis, 'ByteLengthQueuingStrategy', ByteLengthQueuingStrategy); | 177 | | -exposeInterface(globalThis, 'CountQueuingStrategy', CountQueuingStrategy); | 178 | | -exposeInterface(globalThis, 'TextEncoderStream', TextEncoderStream); | 179 | | -exposeInterface(globalThis, 'TextDecoderStream', TextDecoderStream); | 180 | | -exposeInterface(globalThis, 'CompressionStream', CompressionStream); | 181 | | -exposeInterface(globalThis, 'DecompressionStream', DecompressionStream); | | 138 | +exposeLazyInterfaces( | | 139 | + globalThis, | | 140 | + 'internal/webstreams/compression', | | 141 | + [ | | 142 | + 'CompressionStream', 'DecompressionStream', | | 143 | + ]); |
|