| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 812f0e9 commit b5c30e2
25 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,9 +13,6 @@ const { | |||
| 13 | 13 | Boolean, | |
| 14 | 14 | ErrorCaptureStackTrace, | |
| 15 | 15 | FunctionPrototypeBind, | |
| 16 | - MathFloor, | ||
| 17 | - Number, | ||
| 18 | - NumberPrototypeToFixed, | ||
| 19 | 16 | ObjectDefineProperties, | |
| 20 | 17 | ObjectDefineProperty, | |
| 21 | 18 | ObjectKeys, | |
@@ -30,10 +27,8 @@ const { | |||
| 30 | 27 | SafeSet, | |
| 31 | 28 | SafeWeakMap, | |
| 32 | 29 | StringPrototypeIncludes, | |
| 33 | - StringPrototypePadStart, | ||
| 34 | 30 | StringPrototypeRepeat, | |
| 35 | 31 | StringPrototypeSlice, | |
| 36 | - StringPrototypeSplit, | ||
| 37 | 32 | Symbol, | |
| 38 | 33 | SymbolHasInstance, | |
| 39 | 34 | SymbolToStringTag, | |
@@ -63,19 +58,14 @@ const { | |||
| 63 | 58 | isTypedArray, isSet, isMap, isSetIterator, isMapIterator, | |
| 64 | 59 | } = require('internal/util/types'); | |
| 65 | 60 | const { | |
| 66 | - CHAR_LOWERCASE_B: kTraceBegin, | ||
| 67 | - CHAR_LOWERCASE_E: kTraceEnd, | ||
| 68 | - CHAR_LOWERCASE_N: kTraceInstant, | ||
| 69 | 61 | CHAR_UPPERCASE_C: kTraceCount, | |
| 70 | 62 | } = require('internal/constants'); | |
| 71 | 63 | const { styleText } = require('util'); | |
| 72 | 64 | const kCounts = Symbol('counts'); | |
| 65 | + const { time, timeLog, timeEnd, kNone } = require('internal/util/debuglog'); | ||
| 73 | 66 | ||
| 74 | 67 | const kTraceConsoleCategory = 'node,node.console'; | |
| 75 | 68 | ||
| 76 | - const kSecond = 1000; | ||
| 77 | - const kMinute = 60 * kSecond; | ||
| 78 | - const kHour = 60 * kMinute; | ||
| 79 | 69 | const kMaxGroupIndentation = 1000; | |
| 80 | 70 | ||
| 81 | 71 | // Lazy loaded for startup performance. | |
@@ -101,6 +91,7 @@ const kBindStreamsEager = Symbol('kBindStreamsEager'); | |||
| 101 | 91 | const kBindStreamsLazy = Symbol('kBindStreamsLazy'); | |
| 102 | 92 | const kUseStdout = Symbol('kUseStdout'); | |
| 103 | 93 | const kUseStderr = Symbol('kUseStderr'); | |
| 94 | + const kInternalTimeLogImpl = Symbol('kInternalTimeLogImpl'); | ||
| 104 | 95 | ||
| 105 | 96 | const optionsMap = new SafeWeakMap(); | |
| 106 | 97 | function Console(options /* or: stdout, stderr, ignoreErrors = true */) { | |
@@ -381,6 +372,14 @@ function createWriteErrorHandler(instance, streamSymbol) { | |||
| 381 | 372 | }; | |
| 382 | 373 | } | |
| 383 | 374 | ||
| 375 | + function timeLogImpl(label, formatted, args) { | ||
| 376 | + if (args === undefined) { | ||
| 377 | + this.log('%s: %s', label, formatted); | ||
| 378 | + } else { | ||
| 379 | + this.log('%s: %s', label, formatted, ...new SafeArrayIterator(args)); | ||
| 380 | + } | ||
| 381 | + } | ||
| 382 | + | ||
| 384 | 383 | const consoleMethods = { | |
| 385 | 384 | log(...args) { | |
| 386 | 385 | this[kWriteToConsole](kUseStdout, this[kFormatForStdout](args)); | |
@@ -404,31 +403,21 @@ const consoleMethods = { | |||
| 404 | 403 | }, | |
| 405 | 404 | ||
| 406 | 405 | time(label = 'default') { | |
| 407 | - // Coerces everything other than Symbol to a string | ||
| 408 | - label = `${label}`; | ||
| 409 | - if (this._times.has(label)) { | ||
| 410 | - process.emitWarning(`Label '${label}' already exists for console.time()`); | ||
| 411 | - return; | ||
| 412 | - } | ||
| 413 | - trace(kTraceBegin, kTraceConsoleCategory, `time::${label}`, 0); | ||
| 414 | - this._times.set(label, process.hrtime()); | ||
| 406 | + time(this._times, kTraceConsoleCategory, 'console.time()', kNone, label, `time::${label}`); | ||
| 415 | 407 | }, | |
| 416 | 408 | ||
| 417 | 409 | timeEnd(label = 'default') { | |
| 418 | - // Coerces everything other than Symbol to a string | ||
| 419 | - label = `${label}`; | ||
| 420 | - const found = timeLogImpl(this, 'timeEnd', label); | ||
| 421 | - trace(kTraceEnd, kTraceConsoleCategory, `time::${label}`, 0); | ||
| 422 | - if (found) { | ||
| 423 | - this._times.delete(label); | ||
| 424 | - } | ||
| 410 | + if (this[kInternalTimeLogImpl] === undefined) | ||
| 411 | + this[kInternalTimeLogImpl] = FunctionPrototypeBind(timeLogImpl, this); | ||
| 412 | + | ||
| 413 | + timeEnd(this._times, kTraceConsoleCategory, 'console.timeEnd()', kNone, this[kInternalTimeLogImpl], label, `time::${label}`); | ||
| 425 | 414 | }, | |
| 426 | 415 | ||
| 427 | 416 | timeLog(label = 'default', ...data) { | |
| 428 | - // Coerces everything other than Symbol to a string | ||
| 429 | - label = `${label}`; | ||
| 430 | - timeLogImpl(this, 'timeLog', label, data); | ||
| 431 | - trace(kTraceInstant, kTraceConsoleCategory, `time::${label}`, 0); | ||
| 417 | + if (this[kInternalTimeLogImpl] === undefined) | ||
| 418 | + this[kInternalTimeLogImpl] = FunctionPrototypeBind(timeLogImpl, this); | ||
| 419 | + | ||
| 420 | + timeLog(this._times, kTraceConsoleCategory, 'console.timeLog()', kNone, this[kInternalTimeLogImpl], label, `time::${label}`, data); | ||
| 432 | 421 | }, | |
| 433 | 422 | ||
| 434 | 423 | trace: function trace(...args) { | |
@@ -627,63 +616,6 @@ const consoleMethods = { | |||
| 627 | 616 | }, | |
| 628 | 617 | }; | |
| 629 | 618 | ||
| 630 | - // Returns true if label was found | ||
| 631 | - function timeLogImpl(self, name, label, data) { | ||
| 632 | - const time = self._times.get(label); | ||
| 633 | - if (time === undefined) { | ||
| 634 | - process.emitWarning(`No such label '${label}' for console.${name}()`); | ||
| 635 | - return false; | ||
| 636 | - } | ||
| 637 | - const duration = process.hrtime(time); | ||
| 638 | - const ms = duration[0] * 1000 + duration[1] / 1e6; | ||
| 639 | - | ||
| 640 | - const formatted = formatTime(ms); | ||
| 641 | - | ||
| 642 | - if (data === undefined) { | ||
| 643 | - self.log('%s: %s', label, formatted); | ||
| 644 | - } else { | ||
| 645 | - self.log('%s: %s', label, formatted, ...new SafeArrayIterator(data)); | ||
| 646 | - } | ||
| 647 | - return true; | ||
| 648 | - } | ||
| 649 | - | ||
| 650 | - function pad(value) { | ||
| 651 | - return StringPrototypePadStart(`${value}`, 2, '0'); | ||
| 652 | - } | ||
| 653 | - | ||
| 654 | - function formatTime(ms) { | ||
| 655 | - let hours = 0; | ||
| 656 | - let minutes = 0; | ||
| 657 | - let seconds = 0; | ||
| 658 | - | ||
| 659 | - if (ms >= kSecond) { | ||
| 660 | - if (ms >= kMinute) { | ||
| 661 | - if (ms >= kHour) { | ||
| 662 | - hours = MathFloor(ms / kHour); | ||
| 663 | - ms = ms % kHour; | ||
| 664 | - } | ||
| 665 | - minutes = MathFloor(ms / kMinute); | ||
| 666 | - ms = ms % kMinute; | ||
| 667 | - } | ||
| 668 | - seconds = ms / kSecond; | ||
| 669 | - } | ||
| 670 | - | ||
| 671 | - if (hours !== 0 || minutes !== 0) { | ||
| 672 | - ({ 0: seconds, 1: ms } = StringPrototypeSplit( | ||
| 673 | - NumberPrototypeToFixed(seconds, 3), | ||
| 674 | - '.', | ||
| 675 | - )); | ||
| 676 | - const res = hours !== 0 ? `${hours}:${pad(minutes)}` : minutes; | ||
| 677 | - return `${res}:${pad(seconds)}.${ms} (${hours !== 0 ? 'h:m' : ''}m:ss.mmm)`; | ||
| 678 | - } | ||
| 679 | - | ||
| 680 | - if (seconds !== 0) { | ||
| 681 | - return `${NumberPrototypeToFixed(seconds, 3)}s`; | ||
| 682 | - } | ||
| 683 | - | ||
| 684 | - return `${Number(NumberPrototypeToFixed(ms, 3))}ms`; | ||
| 685 | - } | ||
| 686 | - | ||
| 687 | 619 | const keyKey = 'Key'; | |
| 688 | 620 | const valuesKey = 'Values'; | |
| 689 | 621 | const indexKey = '(index)'; | |
@@ -743,5 +675,4 @@ module.exports = { | |||
| 743 | 675 | kBindStreamsLazy, | |
| 744 | 676 | kBindProperties, | |
| 745 | 677 | initializeGlobalConsole, | |
| 746 | - formatTime, // exported for tests | ||
| 747 | 678 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,8 +24,7 @@ markBootstrapComplete(); | |||
| 24 | 24 | ||
| 25 | 25 | if (process.env.NODE_REPL_EXTERNAL_MODULE) { | |
| 26 | 26 | require('internal/modules/cjs/loader') | |
| 27 | - .Module | ||
| 28 | - ._load(process.env.NODE_REPL_EXTERNAL_MODULE, undefined, true); | ||
| 27 | + .wrapModuleLoad(process.env.NODE_REPL_EXTERNAL_MODULE, undefined, true); | ||
| 29 | 28 | } else { | |
| 30 | 29 | // --input-type flag not supported in REPL | |
| 31 | 30 | if (getOptionValue('--input-type')) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -108,6 +108,7 @@ module.exports = { | |||
| 108 | 108 | initializeCJS, | |
| 109 | 109 | Module, | |
| 110 | 110 | wrapSafe, | |
| 111 | + wrapModuleLoad, | ||
| 111 | 112 | kIsMainSymbol, | |
| 112 | 113 | kIsCachedByESMLoader, | |
| 113 | 114 | kRequiredModuleSymbol, | |
@@ -181,6 +182,13 @@ const { | |||
| 181 | 182 | isProxy, | |
| 182 | 183 | } = require('internal/util/types'); | |
| 183 | 184 | ||
| 185 | + const { debuglog, debugWithTimer } = require('internal/util/debuglog'); | ||
| 186 | + | ||
| 187 | + let { startTimer, endTimer } = debugWithTimer('module_timer', (start, end) => { | ||
| 188 | + startTimer = start; | ||
| 189 | + endTimer = end; | ||
| 190 | + }); | ||
| 191 | + | ||
| 184 | 192 | const isWindows = process.platform === 'win32'; | |
| 185 | 193 | ||
| 186 | 194 | const relativeResolveCache = { __proto__: null }; | |
@@ -189,6 +197,24 @@ let requireDepth = 0; | |||
| 189 | 197 | let isPreloading = false; | |
| 190 | 198 | let statCache = null; | |
| 191 | 199 | ||
| 200 | + /** | ||
| 201 | + * Internal method to add tracing capabilities for Module._load. | ||
| 202 | + * | ||
| 203 | + * See more {@link Module._load} | ||
| 204 | + */ | ||
| 205 | + function wrapModuleLoad(request, parent, isMain) { | ||
| 206 | + const logLabel = `[${parent?.id || ''}] [${request}]`; | ||
| 207 | + const traceLabel = `require('${request}')`; | ||
| 208 | + | ||
| 209 | + startTimer(logLabel, traceLabel); | ||
| 210 | + | ||
| 211 | + try { | ||
| 212 | + return Module._load(request, parent, isMain); | ||
| 213 | + } finally { | ||
| 214 | + endTimer(logLabel, traceLabel); | ||
| 215 | + } | ||
| 216 | + } | ||
| 217 | + | ||
| 192 | 218 | /** | |
| 193 | 219 | * Get a path's properties, using an in-memory cache to minimize lookups. | |
| 194 | 220 | * @param {string} filename Absolute path to the file | |
@@ -354,7 +380,7 @@ function setModuleParent(value) { | |||
| 354 | 380 | this[kModuleParent] = value; | |
| 355 | 381 | } | |
| 356 | 382 | ||
| 357 | - let debug = require('internal/util/debuglog').debuglog('module', (fn) => { | ||
| 383 | + let debug = debuglog('module', (fn) => { | ||
| 358 | 384 | debug = fn; | |
| 359 | 385 | }); | |
| 360 | 386 | ||
@@ -971,7 +997,7 @@ function getExportsForCircularRequire(module) { | |||
| 971 | 997 | * 3. Otherwise, create a new module for the file and save it to the cache. | |
| 972 | 998 | * Then have it load the file contents before returning its exports object. | |
| 973 | 999 | * @param {string} request Specifier of module to load via `require` | |
| 974 | - * @param {string} parent Absolute path of the module importing the child | ||
| 1000 | + * @param {Module} parent Absolute path of the module importing the child | ||
| 975 | 1001 | * @param {boolean} isMain Whether the module is the main entry point | |
| 976 | 1002 | */ | |
| 977 | 1003 | Module._load = function(request, parent, isMain) { | |
@@ -1268,7 +1294,7 @@ Module.prototype.require = function(id) { | |||
| 1268 | 1294 | } | |
| 1269 | 1295 | requireDepth++; | |
| 1270 | 1296 | try { | |
| 1271 | - return Module._load(id, this, /* isMain */ false); | ||
| 1297 | + return wrapModuleLoad(id, this, /* isMain */ false); | ||
| 1272 | 1298 | } finally { | |
| 1273 | 1299 | requireDepth--; | |
| 1274 | 1300 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -43,6 +43,7 @@ const { | |||
| 43 | 43 | const { | |
| 44 | 44 | kIsCachedByESMLoader, | |
| 45 | 45 | Module: CJSModule, | |
| 46 | + wrapModuleLoad, | ||
| 46 | 47 | kModuleSource, | |
| 47 | 48 | kModuleExport, | |
| 48 | 49 | kModuleExportNames, | |
@@ -197,7 +198,7 @@ function loadCJSModule(module, source, url, filename, isMain) { | |||
| 197 | 198 | importAttributes = { __proto__: null, type: 'json' }; | |
| 198 | 199 | break; | |
| 199 | 200 | case '.node': | |
| 200 | - return CJSModule._load(specifier, module); | ||
| 201 | + return wrapModuleLoad(specifier, module); | ||
| 201 | 202 | default: | |
| 202 | 203 | // fall through | |
| 203 | 204 | } | |
@@ -289,7 +290,7 @@ translators.set('commonjs-sync', function requireCommonJS(url, source, isMain) { | |||
| 289 | 290 | return createCJSModuleWrap(url, source, isMain, (module, source, url, filename, isMain) => { | |
| 290 | 291 | assert(module === CJSModule._cache[filename]); | |
| 291 | 292 | assert(!isMain); | |
| 292 | - CJSModule._load(filename, null, isMain); | ||
| 293 | + wrapModuleLoad(filename, null, isMain); | ||
| 293 | 294 | }); | |
| 294 | 295 | }); | |
| 295 | 296 | ||
@@ -314,7 +315,7 @@ translators.set('commonjs', async function commonjsStrategy(url, source, | |||
| 314 | 315 | // obtained by calling the monkey-patchable CJS loader. | |
| 315 | 316 | const cjsLoader = source == null ? (module, source, url, filename, isMain) => { | |
| 316 | 317 | assert(module === CJSModule._cache[filename]); | |
| 317 | - CJSModule._load(filename, undefined, isMain); | ||
| 318 | + wrapModuleLoad(filename, undefined, isMain); | ||
| 318 | 319 | } : loadCJSModule; | |
| 319 | 320 | ||
| 320 | 321 | try { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -154,8 +154,8 @@ function executeUserEntryPoint(main = process.argv[1]) { | |||
| 154 | 154 | // try to run the entry point via the CommonJS loader; and if that fails under certain conditions, retry as ESM. | |
| 155 | 155 | if (!useESMLoader) { | |
| 156 | 156 | const cjsLoader = require('internal/modules/cjs/loader'); | |
| 157 | - const { Module } = cjsLoader; | ||
| 158 | - Module._load(main, null, true); | ||
| 157 | + const { wrapModuleLoad } = cjsLoader; | ||
| 158 | + wrapModuleLoad(main, null, true); | ||
| 159 | 159 | } else { | |
| 160 | 160 | const mainPath = resolvedMain || main; | |
| 161 | 161 | if (mainURL === undefined) { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments