Skip to content

Commit

Permalink
feat(compartment-mapper): Collect unretained module descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Oct 24, 2024
1 parent b6547ff commit d160c67
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 12 deletions.
5 changes: 5 additions & 0 deletions packages/compartment-mapper/NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
User-visible changes to `@endo/compartment-mapper`:

# Next version

- Omits unused module descriptors from `compartment-map.json` in archived
applications, potentially reducing file sizes.

# v1.3.0 (2024-10-10)

- Adds support for dynamic requires in CommonJS modules. This requires specific
Expand Down
23 changes: 13 additions & 10 deletions packages/compartment-mapper/src/archive-lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,25 @@ const translateCompartmentMap = (compartments, sources, compartmentRenames) => {
const result = create(null);
for (const compartmentName of keys(compartmentRenames)) {
const compartment = compartments[compartmentName];
const { name, label, retained, policy } = compartment;
if (retained) {
const { name, label, retained: compartmentRetained, policy } = compartment;
if (compartmentRetained) {
// rename module compartments
/** @type {Record<string, ModuleDescriptor>} */
const modules = create(null);
const compartmentModules = compartment.modules;
if (compartment.modules) {
for (const name of keys(compartmentModules).sort()) {
const module = compartmentModules[name];
if (module.compartment !== undefined) {
modules[name] = {
...module,
compartment: compartmentRenames[module.compartment],
};
} else {
modules[name] = module;
const { retained: moduleRetained, ...retainedModule } =
compartmentModules[name];
if (moduleRetained) {
if (retainedModule.compartment !== undefined) {
modules[name] = {
...retainedModule,
compartment: compartmentRenames[retainedModule.compartment],
};
} else {
modules[name] = retainedModule;
}
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion packages/compartment-mapper/src/compartment-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const assertConditions = (conditions, url) => {
* @param {string} url
*/
const assertCompartmentModule = (allegedModule, path, url) => {
const { compartment, module, ...extra } = allegedModule;
const { compartment, module, retained, ...extra } = allegedModule;
assertEmptyObject(
extra,
`${path} must not have extra properties, got ${q({
Expand All @@ -125,6 +125,13 @@ const assertCompartmentModule = (allegedModule, path, url) => {
'string',
`${path}.module must be a string, got ${q(module)} in ${q(url)}`,
);
if (retained !== undefined) {
assert.typeof(
retained,
'boolean',
`${path}.retained must be a boolean, got ${q(retained)} in ${q(url)}`,
);
}
};

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/compartment-mapper/src/import-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ function* chooseModuleDescriptor(
for (const candidateSpecifier of candidates) {
const candidateModuleDescriptor = moduleDescriptors[candidateSpecifier];
if (candidateModuleDescriptor !== undefined) {
candidateModuleDescriptor.retained = true;
const { compartment: candidateCompartmentName = packageLocation } =
candidateModuleDescriptor;
const candidateCompartment = compartments[candidateCompartmentName];
Expand Down Expand Up @@ -339,6 +340,7 @@ function* chooseModuleDescriptor(
// module specifier than the requested one.
if (candidateSpecifier !== moduleSpecifier) {
moduleDescriptors[moduleSpecifier] = {
retained: true,
module: candidateSpecifier,
compartment: packageLocation,
};
Expand Down
3 changes: 3 additions & 0 deletions packages/compartment-mapper/src/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ const makeModuleMapHook = (

const moduleDescriptor = moduleDescriptors[moduleSpecifier];
if (moduleDescriptor !== undefined) {
moduleDescriptor.retained = true;

// "foreignCompartmentName" refers to the compartment which
// may differ from the current compartment
const {
Expand Down Expand Up @@ -193,6 +195,7 @@ const makeModuleMapHook = (
// a moduleMapHook when we assemble compartments from the resulting
// archive.
moduleDescriptors[moduleSpecifier] = {
retained: true,
compartment: foreignCompartmentName,
module: foreignModuleSpecifier,
};
Expand Down
3 changes: 2 additions & 1 deletion packages/compartment-mapper/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ export {};
* `package.json`, there is a corresponding module descriptor.
*
* @typedef {object} ModuleDescriptor
* @property {string=} [compartment]
* @property {string} [compartment]
* @property {string} [module]
* @property {string} [location]
* @property {Language} [parser]
* @property {string} [sha512] in base 16, hex
* @property {string} [exit]
* @property {string} [deferredError]
* @property {boolean} [retained]
*/

/**
Expand Down

0 comments on commit d160c67

Please sign in to comment.