|
7 | 7 | ArrayPrototypeSplice, |
8 | 8 | JSONParse, |
9 | 9 | JSONStringify, |
| 10 | + ObjectKeys, |
10 | 11 | PromiseResolve, |
11 | 12 | String, |
12 | 13 | StringPrototypeEndsWith, |
| 14 | + StringPrototypeIncludes, |
13 | 15 | StringPrototypeStartsWith, |
14 | 16 | } = primordials; |
15 | 17 |
|
@@ -101,34 +103,101 @@ function deregisterVFS(vfs) { |
101 | 103 | if (index === -1) return; |
102 | 104 | ArrayPrototypeSplice(activeVFSList, index, 1); |
103 | 105 | debug('deregister layer=%d active=%d', vfs.layerId, activeVFSList.length); |
104 | | - // Loader/path caches are shared across all VFSes and we can't tell |
105 | | - // which entries belonged to the one going away, so flush them on |
106 | | - // every unmount. The cost is bounded; correctness wins. |
107 | | - clearLoaderCaches(); |
| 106 | + // Scope-purge: only drop loader-cache entries that belong to the VFS |
| 107 | + // that is going away. Other VFSes and real-fs imports are untouched. |
| 108 | + purgeLoaderCachesForVFS(vfs); |
108 | 109 | if (activeVFSList.length === 0) { |
109 | 110 | uninstallHooks(); |
110 | 111 | } |
111 | 112 | } |
112 | 113 |
|
113 | 114 | /** |
114 | | - * Clear every JS-reachable loader cache that could hold a VFS-resolved |
115 | | - * entry. Called from deregisterVFS only when the last VFS unmounts. |
| 115 | + * Returns true if `cjsFilename` is a path that `vfs` claims via |
| 116 | + * `shouldHandle()`. Used to filter CJS cache entries on deregister. |
| 117 | + * @param {object} vfs |
| 118 | + * @param {string} cjsFilename |
| 119 | + * @returns {boolean} |
116 | 120 | */ |
117 | | -function clearLoaderCaches() { |
| 121 | +function isOwnedByVFS(vfs, cjsFilename) { |
| 122 | + if (typeof cjsFilename !== 'string') return false; |
| 123 | + try { |
| 124 | + return vfs.shouldHandle(resolve(cjsFilename)); |
| 125 | + } catch { |
| 126 | + return false; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * Returns true if `url` carries the `vfs-layer=<layerId>` tag emitted by |
| 132 | + * esm/resolve.js for the given VFS layer. Used to filter ESM cache |
| 133 | + * entries on deregister. |
| 134 | + * @param {string} url |
| 135 | + * @param {number} layerId |
| 136 | + * @returns {boolean} |
| 137 | + */ |
| 138 | +function urlBelongsToLayer(url, layerId) { |
| 139 | + if (typeof url !== 'string') return false; |
| 140 | + return StringPrototypeIncludes(url, `vfs-layer=${layerId}`); |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * Drop the cache entries owned by `vfs` from the JS-reachable loader |
| 145 | + * caches. Real-fs entries and other-VFS entries are left in place. |
| 146 | + * @param {object} vfs The VFS being deregistered |
| 147 | + */ |
| 148 | +function purgeLoaderCachesForVFS(vfs) { |
| 149 | + const layerId = vfs.layerId; |
| 150 | + |
| 151 | + // CJS module cache, keyed by absolute filename. |
118 | 152 | const cjsLoader = require('internal/modules/cjs/loader'); |
119 | | - cjsLoader.Module._pathCache = { __proto__: null }; |
120 | | - cjsLoader.clearStatCache(); |
| 153 | + const moduleCache = cjsLoader.Module._cache; |
| 154 | + for (const filename of ObjectKeys(moduleCache)) { |
| 155 | + if (isOwnedByVFS(vfs, filename)) { |
| 156 | + delete moduleCache[filename]; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + // CJS path cache: keyed by request + parent paths string. The |
| 161 | + // cached value is the resolved filename, so filter on that. |
| 162 | + const pathCache = cjsLoader.Module._pathCache; |
| 163 | + for (const key of ObjectKeys(pathCache)) { |
| 164 | + if (isOwnedByVFS(vfs, pathCache[key])) { |
| 165 | + delete pathCache[key]; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + // CJS stat cache: keys are absolute filenames. |
| 170 | + cjsLoader.clearStatCacheForVFS(vfs); |
| 171 | + |
| 172 | + // Realpath cache used by helpers.toRealPath: keys are absolute paths. |
121 | 173 | const helpers = require('internal/modules/helpers'); |
122 | | - helpers.clearRealpathCache(); |
123 | | - const { clearPackageJSONCache } = require('internal/modules/package_json_reader'); |
124 | | - clearPackageJSONCache(); |
125 | | - // The ESM cascaded loader's loadCache is intentionally NOT cleared here: |
126 | | - // clearing it mid-flight (while another import() is awaiting nested |
127 | | - // resolution) would let a second ModuleJob be created for the same URL, |
128 | | - // breaking module identity. The trade-off is that an ESM module already |
129 | | - // loaded from a VFS path remains cached after unmount and across a |
130 | | - // re-mount with different content, consistent with how ESM caches |
131 | | - // modules everywhere else in Node.js. |
| 174 | + helpers.purgeRealpathCacheForVFS(vfs); |
| 175 | + |
| 176 | + // package.json caches: keyed by absolute path. |
| 177 | + const pkgReader = require('internal/modules/package_json_reader'); |
| 178 | + pkgReader.purgePackageJSONCacheForVFS(vfs); |
| 179 | + |
| 180 | + // ESM cascaded loader: scope-purge by URL tag. |
| 181 | + const esmLoader = require('internal/modules/esm/loader'); |
| 182 | + if (esmLoader.isCascadedLoaderInitialized()) { |
| 183 | + const loader = esmLoader.getOrInitializeCascadedLoader(); |
| 184 | + const loadCache = loader.loadCache; |
| 185 | + // LoadCache extends SafeMap (url -> { [type]: job }). We iterate |
| 186 | + // keys() and delete entries whose URL carries this layer's tag. |
| 187 | + if (loadCache && typeof loadCache.delete === 'function') { |
| 188 | + for (const url of loadCache.keys()) { |
| 189 | + if (urlBelongsToLayer(url, layerId)) { |
| 190 | + // Drop every type variant for this URL. |
| 191 | + const variants = loadCache.get(url); |
| 192 | + if (variants) { |
| 193 | + for (const type of ObjectKeys(variants)) { |
| 194 | + loadCache.delete(url, type); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + } |
132 | 201 | } |
133 | 202 |
|
134 | 203 | /** |
@@ -934,6 +1003,14 @@ function installModuleLoaderOverrides() { |
934 | 1003 | } |
935 | 1004 | return 0; // EXTENSIONLESS_FORMAT_JAVASCRIPT |
936 | 1005 | }, |
| 1006 | + getLayerForPath(filename) { |
| 1007 | + const normalized = resolve(filename); |
| 1008 | + for (let i = 0; i < activeVFSList.length; i++) { |
| 1009 | + const vfs = activeVFSList[i]; |
| 1010 | + if (vfs.shouldHandle(normalized)) return vfs.layerId; |
| 1011 | + } |
| 1012 | + return undefined; |
| 1013 | + }, |
937 | 1014 | }); |
938 | 1015 |
|
939 | 1016 | setLoaderPackageOverrides({ |
|
0 commit comments