Skip to content

Commit e653656

Browse files
committed
ongoing work for hook ownership
1 parent 8a6ea82 commit e653656

3 files changed

Lines changed: 11 additions & 6 deletions

File tree

lib/internal/modules/esm/hooks.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ class Hooks {
169169
addCustomLoader(url, exports, data) {
170170
const alreadyKnown = ArrayPrototypeSome(ArrayPrototypeMap(['initialize', 'resolve', 'load'], (hookName) => {
171171
if (this.#chains[hookName]) {
172-
return ArrayPrototypeFilter(this.#chains[hookName], (el) => el.url === url).length === 1;
172+
return ArrayPrototypeFilter(
173+
this.#chains[hookName], (el) => el.url === url && el.data === data).length === 1;
173174
}
174175
}), (el) => el);
175176

@@ -185,11 +186,11 @@ class Hooks {
185186

186187
if (resolve) {
187188
const next = this.#chains.resolve[this.#chains.resolve.length - 1];
188-
ArrayPrototypePush(this.#chains.resolve, { __proto__: null, fn: resolve, url, next });
189+
ArrayPrototypePush(this.#chains.resolve, { __proto__: null, fn: resolve, url, data, next });
189190
}
190191
if (load) {
191192
const next = this.#chains.load[this.#chains.load.length - 1];
192-
ArrayPrototypePush(this.#chains.load, { __proto__: null, fn: load, url, next });
193+
ArrayPrototypePush(this.#chains.load, { __proto__: null, fn: load, url, data, next });
193194
}
194195
return initialize?.(data);
195196
}
@@ -211,6 +212,7 @@ class Hooks {
211212
originalSpecifier,
212213
parentURL,
213214
importAttributes = { __proto__: null },
215+
threadId
214216
) {
215217
throwIfInvalidParentURL(parentURL);
216218

@@ -219,6 +221,7 @@ class Hooks {
219221
conditions: getDefaultConditions(),
220222
importAttributes,
221223
parentURL,
224+
threadId,
222225
};
223226
const meta = {
224227
chainFinished: null,

lib/internal/modules/esm/loader.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const { ModuleWrap, kEvaluating, kEvaluated } = internalBinding('module_wrap');
4141
const {
4242
urlToFilename,
4343
} = require('internal/modules/helpers');
44+
const { threadId } = require('worker_threads');
4445
let defaultResolve, defaultLoad, defaultLoadSync, importMetaInitializer;
4546

4647
/**
@@ -500,7 +501,7 @@ class ModuleLoader {
500501
*/
501502
resolve(originalSpecifier, parentURL, importAttributes) {
502503
if (this.#customizations) {
503-
return this.#customizations.resolve(originalSpecifier, parentURL, importAttributes);
504+
return this.#customizations.resolve(originalSpecifier, parentURL, importAttributes, threadId);
504505
}
505506
const requestKey = this.#resolveCache.serializeKey(originalSpecifier, importAttributes);
506507
const cachedResult = this.#resolveCache.get(requestKey, parentURL);
@@ -622,7 +623,7 @@ class CustomizedModuleLoader {
622623
* @returns {{ format: string, url: URL['href'] } | undefined}
623624
*/
624625
register(originalSpecifier, parentURL, data, transferList) {
625-
return hooksProxy.makeSyncRequest('register', transferList, originalSpecifier, parentURL, data);
626+
return hooksProxy.makeSyncRequest('register', transferList, originalSpecifier, parentURL, data, threadId);
626627
}
627628

628629
/**
@@ -635,7 +636,7 @@ class CustomizedModuleLoader {
635636
* @returns {{ format: string, url: URL['href'] }}
636637
*/
637638
resolve(originalSpecifier, parentURL, importAttributes) {
638-
return hooksProxy.makeAsyncRequest('resolve', undefined, originalSpecifier, parentURL, importAttributes);
639+
return hooksProxy.makeAsyncRequest('resolve', undefined, originalSpecifier, parentURL, importAttributes, threadId);
639640
}
640641

641642
resolveSync(originalSpecifier, parentURL, importAttributes) {

test/fixtures/es-module-loaders/hooks-input.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export async function resolve(specifier, context, next) {
3737
'conditions',
3838
'importAttributes',
3939
'parentURL',
40+
'threadId',
4041
]);
4142
assert.ok(Array.isArray(context.conditions));
4243
assert.strictEqual(typeof next, 'function');

0 commit comments

Comments
 (0)