mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
lib: lazy-load deps in source_map_cache.js
So that the file can be snapshotted. PR-URL: https://github.com/nodejs/node/pull/45849 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
@@ -24,17 +24,19 @@ let debug = require('internal/util/debuglog').debuglog('source_map', (fn) => {
|
||||
debug = fn;
|
||||
});
|
||||
const { getOptionValue } = require('internal/options');
|
||||
const { IterableWeakMap } = require('internal/util/iterable_weak_map');
|
||||
const {
|
||||
normalizeReferrerURL,
|
||||
} = require('internal/modules/helpers');
|
||||
|
||||
const { validateBoolean } = require('internal/validators');
|
||||
const { setMaybeCacheGeneratedSourceMap } = internalBinding('errors');
|
||||
const { getLazy } = require('internal/util');
|
||||
|
||||
// Since the CJS module cache is mutable, which leads to memory leaks when
|
||||
// modules are deleted, we use a WeakMap so that the source map cache will
|
||||
// be purged automatically:
|
||||
const cjsSourceMapCache = new IterableWeakMap();
|
||||
const getCjsSourceMapCache = getLazy(() => {
|
||||
const { IterableWeakMap } = require('internal/util/iterable_weak_map');
|
||||
return new IterableWeakMap();
|
||||
});
|
||||
|
||||
// The esm cache is not mutable, so we can use a Map without memory concerns:
|
||||
const esmSourceMapCache = new SafeMap();
|
||||
// The generated sources is not mutable, so we can use a Map without memory concerns:
|
||||
@@ -44,6 +46,7 @@ const kSourceMappingURLMagicComment = /\/[*/]#\s+sourceMappingURL=(?<sourceMappi
|
||||
const kSourceURLMagicComment = /\/[*/]#\s+sourceURL=(?<sourceURL>[^\s]+)/g;
|
||||
|
||||
const { fileURLToPath, pathToFileURL, URL } = require('internal/url');
|
||||
|
||||
let SourceMap;
|
||||
|
||||
let sourceMapsEnabled;
|
||||
@@ -114,6 +117,7 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo
|
||||
const sourceMapsEnabled = getSourceMapsEnabled();
|
||||
if (!(process.env.NODE_V8_COVERAGE || sourceMapsEnabled)) return;
|
||||
try {
|
||||
const { normalizeReferrerURL } = require('internal/modules/helpers');
|
||||
filename = normalizeReferrerURL(filename);
|
||||
} catch (err) {
|
||||
// This is most likely an invalid filename in sourceURL of [eval]-wrapper.
|
||||
@@ -137,7 +141,7 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo
|
||||
const data = dataFromUrl(filename, sourceMapURL);
|
||||
const url = data ? null : sourceMapURL;
|
||||
if (cjsModuleInstance) {
|
||||
cjsSourceMapCache.set(cjsModuleInstance, {
|
||||
getCjsSourceMapCache().set(cjsModuleInstance, {
|
||||
filename,
|
||||
lineLengths: lineLengths(content),
|
||||
data,
|
||||
@@ -291,7 +295,7 @@ function sourceMapCacheToObject() {
|
||||
}
|
||||
|
||||
function appendCJSCache(obj) {
|
||||
for (const value of cjsSourceMapCache) {
|
||||
for (const value of getCjsSourceMapCache()) {
|
||||
obj[ObjectGetValueSafe(value, 'filename')] = {
|
||||
lineLengths: ObjectGetValueSafe(value, 'lineLengths'),
|
||||
data: ObjectGetValueSafe(value, 'data'),
|
||||
@@ -309,7 +313,7 @@ function findSourceMap(sourceURL) {
|
||||
}
|
||||
let sourceMap = esmSourceMapCache.get(sourceURL) ?? generatedSourceMapCache.get(sourceURL);
|
||||
if (sourceMap === undefined) {
|
||||
for (const value of cjsSourceMapCache) {
|
||||
for (const value of getCjsSourceMapCache()) {
|
||||
const filename = ObjectGetValueSafe(value, 'filename');
|
||||
const cachedSourceURL = ObjectGetValueSafe(value, 'sourceURL');
|
||||
if (sourceURL === filename || sourceURL === cachedSourceURL) {
|
||||
|
||||
@@ -87,7 +87,6 @@ const expectedModules = new Set([
|
||||
'NativeModule internal/util',
|
||||
'NativeModule internal/util/debuglog',
|
||||
'NativeModule internal/util/inspect',
|
||||
'NativeModule internal/util/iterable_weak_map',
|
||||
'NativeModule internal/util/types',
|
||||
'NativeModule internal/v8/startup_snapshot',
|
||||
'NativeModule internal/validators',
|
||||
|
||||
Reference in New Issue
Block a user