|
4 | 4 | ArrayPrototypeFindIndex,
|
5 | 5 | ArrayPrototypePush,
|
6 | 6 | ArrayPrototypeSplice,
|
| 7 | + ObjectAssign, |
7 | 8 | ObjectFreeze,
|
8 | 9 | StringPrototypeStartsWith,
|
9 | 10 | Symbol,
|
@@ -162,19 +163,31 @@ function convertURLToCJSFilename(url) {
|
162 | 163 | * @param {'load'|'resolve'} name Name of the hook in ModuleHooks.
|
163 | 164 | * @param {Function} defaultStep The default step in the chain.
|
164 | 165 | * @param {Function} validate A function that validates and sanitize the result returned by the chain.
|
165 |
| - * @returns {Function} |
166 | 166 | */
|
167 |
| -function buildHooks(hooks, name, defaultStep, validate) { |
| 167 | +function buildHooks(hooks, name, defaultStep, validate, mergedContext) { |
168 | 168 | let lastRunIndex = hooks.length;
|
169 |
| - function wrapHook(index, userHook, next) { |
170 |
| - return function wrappedHook(...args) { |
| 169 | + /** |
| 170 | + * Helper function to wrap around invocation of user hook or the default step |
| 171 | + * in order to fill in missing arguments or check returned results. |
| 172 | + * Due to the merging of the context, this must be a closure. |
| 173 | + * @param {number} index Index in the chain. Default step is 0, last added hook is 1, |
| 174 | + * and so on. |
| 175 | + * @param {Function} userHookOrDefault Either the user hook or the default step to invoke. |
| 176 | + * @param {Function|undefined} next The next wrapped step. If this is the default step, it's undefined. |
| 177 | + * @returns {Function} Wrapped hook or default step. |
| 178 | + */ |
| 179 | + function wrapHook(index, userHookOrDefault, next) { |
| 180 | + return function nextStep(arg0, context) { |
171 | 181 | lastRunIndex = index;
|
172 |
| - const hookResult = userHook(...args, next); |
| 182 | + if (context && context !== mergedContext) { |
| 183 | + ObjectAssign(mergedContext, context); |
| 184 | + } |
| 185 | + const hookResult = userHookOrDefault(arg0, mergedContext, next); |
173 | 186 | if (lastRunIndex > 0 && lastRunIndex === index && !hookResult.shortCircuit) {
|
174 | 187 | throw new ERR_INVALID_RETURN_PROPERTY_VALUE('true', name, 'shortCircuit',
|
175 | 188 | hookResult.shortCircuit);
|
176 | 189 | }
|
177 |
| - return validate(...args, hookResult); |
| 190 | + return validate(arg0, mergedContext, hookResult); |
178 | 191 | };
|
179 | 192 | }
|
180 | 193 | const chain = [wrapHook(0, defaultStep)];
|
@@ -330,7 +343,7 @@ function loadWithHooks(url, originalFormat, importAttributes, conditions, defaul
|
330 | 343 | return defaultLoad(url, context);
|
331 | 344 | }
|
332 | 345 |
|
333 |
| - const runner = buildHooks(loadHooks, 'load', defaultLoad, validateLoad); |
| 346 | + const runner = buildHooks(loadHooks, 'load', defaultLoad, validateLoad, context); |
334 | 347 |
|
335 | 348 | const result = runner(url, context);
|
336 | 349 | const { source, format } = result;
|
@@ -373,7 +386,7 @@ function resolveWithHooks(specifier, parentURL, importAttributes, conditions, de
|
373 | 386 | return defaultResolve(specifier, context);
|
374 | 387 | }
|
375 | 388 |
|
376 |
| - const runner = buildHooks(resolveHooks, 'resolve', defaultResolve, validateResolve); |
| 389 | + const runner = buildHooks(resolveHooks, 'resolve', defaultResolve, validateResolve, context); |
377 | 390 |
|
378 | 391 | return runner(specifier, context);
|
379 | 392 | }
|
|
0 commit comments