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

module: add releaseLoadedModule #50618

Closed
wants to merge 5 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
12 changes: 12 additions & 0 deletions doc/api/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ changes:
Register a module that exports [hooks][] that customize Node.js module
resolution and loading behavior. See [Customization hooks][].

### `module.releaseResolvedModule(resolvedURL)`

<!-- YAML
added: x.x.x
-->

> Stability: 1 - Experimental

* `resolvedURL` {string} The fully-resolved URL of a module.

Clears the given module from the ESM resolve and load caches.

### `module.syncBuiltinESMExports()`

<!-- YAML
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/modules/esm/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {

// The HTML spec has an implied default type of `'javascript'`.
const kImplicitAssertType = 'javascript';
const kAnyAssertType = '';

/**
* Define a map of module formats to import attributes types (the value of
Expand Down Expand Up @@ -112,5 +113,6 @@ function handleInvalidType(url, type) {

module.exports = {
kImplicitAssertType,
kAnyAssertType,
validateAttributes,
};
19 changes: 19 additions & 0 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
ERR_REQUIRE_ESM,
ERR_UNKNOWN_MODULE_FORMAT,
} = require('internal/errors').codes;
const { kAnyAssertType } = require('internal/modules/esm/assert');
const { getOptionValue } = require('internal/options');
const { pathToFileURL, isURL } = require('internal/url');
const { emitExperimentalWarning } = require('internal/util');
Expand Down Expand Up @@ -223,6 +224,14 @@ class ModuleLoader {
};
}

/**
* Evict saved result of `resolve` and `load` for the given resolved URL.
*/
evict(resolvedURL) {
this.#resolveCache.clearResolvedURL(resolvedURL);
this.loadCache.delete(resolvedURL, kAnyAssertType);
}

/**
* Get a (possibly still pending) module job from the cache,
* or create one and return its Promise.
Expand Down Expand Up @@ -618,8 +627,18 @@ function register(specifier, parentURL = undefined, options) {
);
}

/**
* Release saved results of `resolve` and `load` for the given resolved module
* URL.
*/
function releaseResolvedModule(resolvedURL) {
const moduleLoader = require('internal/process/esm_loader').esmLoader;
return moduleLoader.evict(resolvedURL);
}

module.exports = {
createModuleLoader,
getHooksProxy,
register,
releaseResolvedModule,
};
41 changes: 40 additions & 1 deletion lib/internal/modules/esm/module_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const {
ObjectKeys,
SafeMap,
} = primordials;
const { kImplicitAssertType } = require('internal/modules/esm/assert');
const {
kImplicitAssertType,
kAnyAssertType,
} = require('internal/modules/esm/assert');
let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
debug = fn;
});
Expand Down Expand Up @@ -59,6 +62,27 @@ class ResolveCache extends SafeMap {
return internalCache;
}

/**
* @param {string} resolvedURL
*/
clearResolvedURL(resolvedURL) {
for (const entry of this.entries()) {
const internalCache = entry[1];
let isEmpty = true;
for (const serializedKey of ObjectKeys(internalCache)) {
if (internalCache[serializedKey].url === resolvedURL) {
delete internalCache[serializedKey];
} else {
isEmpty = false;
}
}
if (isEmpty) {
const parentURL = entry[0];
this.delete(parentURL);
}
}
}

/**
* @param {string} serializedKey
* @param {string} parentURL
Expand Down Expand Up @@ -88,6 +112,21 @@ class ResolveCache extends SafeMap {
*/
class LoadCache extends SafeMap {
constructor(i) { super(i); } // eslint-disable-line no-useless-constructor
delete(url, type = kImplicitAssertType) {
validateString(url, 'url');
validateString(type, 'type');
const cachedJobsForUrl = super.get(url);
if (cachedJobsForUrl) {
if (type === kAnyAssertType) {
super.delete(url);
return true;
}
const has = type in cachedJobsForUrl;
delete cachedJobsForUrl[type];
return has;
}
return false;
}
get(url, type = kImplicitAssertType) {
validateString(url, 'url');
validateString(type, 'type');
Expand Down
3 changes: 2 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

const { findSourceMap } = require('internal/source_map/source_map_cache');
const { Module } = require('internal/modules/cjs/loader');
const { register } = require('internal/modules/esm/loader');
const { releaseResolvedModule, register } = require('internal/modules/esm/loader');
const { SourceMap } = require('internal/source_map/source_map');

Module.findSourceMap = findSourceMap;
Module.register = register;
Module.releaseResolvedModule = releaseResolvedModule;
Module.SourceMap = SourceMap;
module.exports = Module;
17 changes: 17 additions & 0 deletions test/es-module/test-esm-evict.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import '../common/index.mjs';
import { strictEqual } from 'node:assert';
import { releaseResolvedModule } from 'node:module';

const specifier = 'data:application/javascript,export default globalThis.value;';

globalThis.value = 1;
const instance1 = await import(specifier);
strictEqual(instance1.default, 1);
globalThis.value = 2;
const instance2 = await import(specifier);
strictEqual(instance2.default, 1);

releaseResolvedModule(specifier);
const instance3 = await import(specifier);
strictEqual(instance3.default, 2);
delete globalThis.value;
Loading