Skip to content

Commit 9c86393

Browse files
committed
chore(compartment-mapper): post-rebase fixes
1 parent 0744b25 commit 9c86393

File tree

2 files changed

+52
-28
lines changed

2 files changed

+52
-28
lines changed

packages/compartment-mapper/src/import-hook.js

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
* ImportHook,
1616
* ImportNowHook,
1717
* RedirectStaticModuleInterface,
18-
* StaticModuleType
18+
* StaticModuleType,
19+
VirtualModuleSource
1920
* } from 'ses'
2021
* @import {
2122
* CompartmentDescriptor,
@@ -691,19 +692,13 @@ export function makeImportNowHookMaker(
691692

692693
/** @type {ImportNowHook} */
693694
const importNowHook = moduleSpecifier => {
694-
// many dynamically-required specifiers will be absolute paths owing to use of `require.resolve()` and `path.resolve()`
695-
if (isAbsolute(moduleSpecifier)) {
696-
const record = findRedirect({
697-
compartmentDescriptor,
698-
compartmentDescriptors,
699-
compartments,
700-
absoluteModuleSpecifier: moduleSpecifier,
701-
});
702-
if (record) {
703-
return record;
704-
}
705-
// if there is no record found this way, we will search for it instead of considering it to be an exit module
706-
} else if (moduleSpecifier !== '.' && !moduleSpecifier.startsWith('./')) {
695+
/**
696+
* Attempt to load the moduleSpecifier via an {@link ExitModuleImportNowHook}, if one exists.
697+
*
698+
* If it doesn't exist, then throw an exception.
699+
* @returns {VirtualModuleSource}
700+
*/
701+
const tryExitModuleImportNowHook = () => {
707702
if (exitModuleImportNowHook) {
708703
// This hook is responsible for ensuring that the moduleSpecifier
709704
// actually refers to an exit module.
@@ -722,14 +717,38 @@ export function makeImportNowHookMaker(
722717
});
723718
return exitRecord;
724719
}
720+
throw Error(
721+
`Cannot find external module ${q(
722+
moduleSpecifier,
723+
)} in package at ${packageLocation}`,
724+
);
725+
} else {
726+
throw Error(
727+
`Cannot find external module ${q(
728+
moduleSpecifier,
729+
)} from package at ${packageLocation}; try providing an importNowHook`,
730+
);
725731
}
726-
throw Error(
727-
`Cannot find external module ${q(
728-
moduleSpecifier,
729-
)} in package ${packageLocation}`,
730-
);
732+
};
733+
734+
// many dynamically-required specifiers will be absolute paths owing to use of `require.resolve()` and `path.resolve()`
735+
if (isAbsolute(moduleSpecifier)) {
736+
const record = findRedirect({
737+
compartmentDescriptor,
738+
compartmentDescriptors,
739+
compartments,
740+
absoluteModuleSpecifier: moduleSpecifier,
741+
});
742+
if (record) {
743+
return record;
744+
}
745+
} else if (moduleSpecifier !== '.' && !moduleSpecifier.startsWith('./')) {
746+
// could be a builtin, which means we should not bother bouncing on the trampoline to find it.
747+
return tryExitModuleImportNowHook();
731748
}
732749

750+
// we might have an absolute path here, but it might be within the compartment, so
751+
// we will try to find it.
733752
const candidates = nominateCandidates(moduleSpecifier, searchSuffixes);
734753

735754
const record = syncTrampoline(
@@ -759,11 +778,8 @@ export function makeImportNowHookMaker(
759778
return record;
760779
}
761780

762-
throw new Error(
763-
`Could not import module: ${q(
764-
moduleSpecifier,
765-
)}; try providing an importNowHook`,
766-
);
781+
// at this point, we haven't found the module by guessing, so we'll try the importer-of-last-resort
782+
return tryExitModuleImportNowHook();
767783
};
768784

769785
return importNowHook;

packages/compartment-mapper/test/dynamic-require.test.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,12 @@ test('inter-package and exit module dynamic require policy is enforced', async t
407407
},
408408
'hooked>dynamic': {
409409
packages: {
410-
'is-ok': true,
410+
'hooked>dynamic>is-ok': true,
411+
},
412+
},
413+
'hooked>dynamic>is-ok': {
414+
packages: {
415+
'hooked>dynamic>is-ok>is-not-ok': true,
411416
},
412417
},
413418
},
@@ -469,7 +474,12 @@ test('inter-package and exit module dynamic require works ("node:"-namespaced)',
469474
},
470475
'hooked>dynamic': {
471476
packages: {
472-
'is-ok': true,
477+
'hooked>dynamic>is-ok': true,
478+
},
479+
},
480+
'hooked>dynamic>is-ok': {
481+
packages: {
482+
'hooked>dynamic>is-ok>is-not-ok': true,
473483
},
474484
},
475485
},
@@ -619,5 +629,3 @@ test('dynamic require of missing module falls through to importNowHook', async t
619629
},
620630
);
621631
});
622-
623-
// test('dynamic require of external module which imports a third module', async t => {});

0 commit comments

Comments
 (0)