Skip to content

Commit

Permalink
chore: fix for conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed Dec 14, 2023
1 parent 0bc2594 commit 11b0c05
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions packages/compartment-mapper/src/policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,37 +333,47 @@ export const attenuateGlobals = (
freezeGlobalThisUnlessOptedOut();
};

/**
* @param {string} [errorHint]
* @returns {string}
*/
const diagnoseModulePolicy = errorHint => {
if (!errorHint) {
return '';
}
return ` (info: ${errorHint})`;
};

/**
* Options for {@link enforceModulePolicy}
* @typedef EnforceModulePolicyOptions
* @property {boolean} [exit] - Whether it is an exit module
* @property {string} [errorHint] - Error hint message
*/

/**
* Throws if importing of the specifier is not allowed by the policy
*
* @param {string} specifier
* @param {import('./types.js').CompartmentDescriptor} compartmentDescriptor
* @param {object} [info]
* @param {EnforceModulePolicyOptions} [options]
*/
export const enforceModulePolicy = (
specifier,
compartmentDescriptor,
info = {},
{ exit, errorHint } = {},
) => {
const { policy, modules, label } = compartmentDescriptor;
const { policy, modules } = compartmentDescriptor;
if (!policy) {
return;
}

if (!info.exit) {
if (!exit) {
if (!modules[specifier]) {
throw Error(
`Importing ${q(specifier)} in ${q(
label,
)} was not allowed by packages policy ${q(
`Importing ${q(specifier)} was not allowed by policy packages:${q(
policy.packages,
)}${diagnoseModulePolicy(info.errorHint)}`,
)}${diagnoseModulePolicy(errorHint)}`,
);
}
return;
Expand All @@ -373,7 +383,7 @@ export const enforceModulePolicy = (
throw Error(
`Importing ${q(specifier)} was not allowed by policy builtins:${q(
policy.builtins,
)}${diagnoseModulePolicy(info.errorHint)}`,
)}${diagnoseModulePolicy(errorHint)}`,
);
}
};
Expand Down

0 comments on commit 11b0c05

Please sign in to comment.