Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compartment-mapper/node): support anonymous entrypoints #2664

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/compartment-mapper/src/node-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,11 @@ const gatherDependency = async (
);
};

/**
* The name used by a root entry compartment that has no `name` in its package descriptor.
*/
export const ANONYMOUS_COMPARTMENT = '<ANONYMOUS>';

/**
* graphPackages returns a graph whose keys are nominally URLs, one per
* package, with values that are label: (an informative Compartment name, built
Expand Down Expand Up @@ -621,6 +626,15 @@ const graphPackages = async (
};
}

/**
* If the entry package descriptor has no `name`, we need to put _something_
* in there to pass compartment map validation (go find
* `assertCompartmentMap()`).
*/
if (!packageDescriptor.name) {
packageDescriptor.name = ANONYMOUS_COMPARTMENT;
}

const graph = create(null);
await graphPackage(
packageDescriptor.name,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions packages/compartment-mapper/test/node-modules.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'ses';
import fs from 'node:fs';
import url from 'node:url';
import test from 'ava';
import { ANONYMOUS_COMPARTMENT, mapNodeModules } from '../src/node-modules.js';
import { makeReadPowers } from '../src/node-powers.js';

const { keys, values } = Object;

test('mapNodeModules() should fulfill with a denormalized CompartmentMapDescriptor', async t => {
t.plan(4);

const readPowers = makeReadPowers({ fs, url });
const moduleLocation = `${new URL(
'fixtures-0/node_modules/bundle/main.js',
import.meta.url,
)}`;

const { compartments, entry } = await mapNodeModules(
readPowers,
moduleLocation,
);

t.deepEqual(
values(compartments)
.map(({ name }) => name)
.sort(),
['bundle', 'bundle-dep'],
);

t.true(keys(compartments).every(name => name.startsWith('file://')));

t.is(compartments[entry.compartment].name, 'bundle');

t.deepEqual(keys(compartments[entry.compartment].modules).sort(), [
'.',
'bundle',
'bundle-dep',
]);
});

test(`mapNodeModules() should assign a package name when the entry point's package descriptor lacks a "name" field`, async t => {
t.plan(1);

const readPowers = makeReadPowers({ fs, url });
const moduleLocation = `${new URL(
'fixtures-anonymous/node_modules/unnamed/incognito/index.js',
import.meta.url,
)}`;

const { compartments } = await mapNodeModules(readPowers, moduleLocation);

t.deepEqual(
values(compartments).map(({ name }) => name),
[ANONYMOUS_COMPARTMENT],
);
});
Loading