Skip to content

Commit 1c02653

Browse files
committed
feat: support use of module references in swingset config sourceSpecs
1 parent abd35f6 commit 1c02653

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

packages/SwingSet/src/controller.js

+48-16
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,48 @@ export function loadBasedir(basedir) {
125125
return config;
126126
}
127127

128+
/**
129+
* Resolve a pathname found in a config descriptor. First try to resolve it as
130+
* a module path, and then if that doesn't work try to resolve it as an
131+
* ordinary path relative to the directory in which the config file was found.
132+
*
133+
* @param dirname Path to directory containing the config file
134+
* @param specPath Path found in a `sourceSpec` or `bundleSpec` property
135+
*
136+
* @return the absolute path corresponding to `specPath` if it can be
137+
* determined.
138+
*/
139+
function resolveSpecFromConfig(dirname, specPath) {
140+
try {
141+
return require.resolve(specPath);
142+
} catch (e) {
143+
if (e.code === 'MODULE_NOT_FOUND') {
144+
return path.resolve(dirname, specPath);
145+
} else {
146+
throw e;
147+
}
148+
}
149+
}
150+
151+
/**
152+
* For each entry in a config descriptor (i.e, `vats`, `bundles`, etc), convert
153+
* it to normal form: resolve each pathname to a context-insensitive absolute
154+
* path and make sure it has a `parameters` property if it's supposed to.
155+
*
156+
* @param desc The config descriptor to be normalized.
157+
* @param dirname The pathname of the directory in which the config file was found
158+
* @param expectParameters `true` if the entries should have parameters (for
159+
* example, `true` for `vats` but `false` for bundles).
160+
*/
128161
function normalizeConfigDescriptor(desc, dirname, expectParameters) {
129162
if (desc) {
130163
for (const name of Object.keys(desc)) {
131164
const entry = desc[name];
132165
if (entry.sourceSpec) {
133-
entry.sourceSpec = path.resolve(dirname, entry.sourceSpec);
166+
entry.sourceSpec = resolveSpecFromConfig(dirname, entry.sourceSpec);
134167
}
135168
if (entry.bundleSpec) {
136-
entry.bundleSpec = path.resolve(dirname, entry.bundleSpec);
169+
entry.bundleSpec = resolveSpecFromConfig(dirname, entry.bundleSpec);
137170
}
138171
if (expectParameters && !entry.parameters) {
139172
entry.parameters = {};
@@ -142,6 +175,17 @@ function normalizeConfigDescriptor(desc, dirname, expectParameters) {
142175
}
143176
}
144177

178+
/**
179+
* Read and parse a swingset config file and return it in normalized form.
180+
*
181+
* @param configPath Path to the config file to be processed
182+
*
183+
* @return the contained config object, in normalized form, or null if the
184+
* requested config file did not exist.
185+
*
186+
* @throws if the file existed but was inaccessible, malformed, or otherwise
187+
* invalid.
188+
*/
145189
export function loadSwingsetConfigFile(configPath) {
146190
try {
147191
const config = JSON.parse(fs.readFileSync(configPath));
@@ -398,21 +442,9 @@ export async function buildVatController(
398442
if (!desc.bundleName) {
399443
names.push(name);
400444
if (desc.sourceSpec) {
401-
const sourceSpec = desc.sourceSpec;
402-
if (!(sourceSpec[0] === '.' || path.isAbsolute(sourceSpec))) {
403-
throw Error(
404-
'sourceSpec must be relative (./foo) or absolute (/foo) not bare (foo)',
405-
);
406-
}
407-
presumptiveBundles.push(bundleSource(sourceSpec));
445+
presumptiveBundles.push(bundleSource(desc.sourceSpec));
408446
} else if (desc.bundleSpec) {
409-
const bundleSpec = desc.bundleSpec;
410-
if (!(bundleSpec[0] === '.' || path.isAbsolute(bundleSpec))) {
411-
throw Error(
412-
'bundleSpec must be relative (./foo) or absolute (/foo) not bare (foo)',
413-
);
414-
}
415-
presumptiveBundles.push(fs.readFileSync(bundleSpec));
447+
presumptiveBundles.push(fs.readFileSync(desc.bundleSpec));
416448
} else if (desc.bundle) {
417449
presumptiveBundles.push(desc.bundle);
418450
} else {

packages/swingset-runner/demo/exchangeBenchmark/swingset.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"bootstrap": "bootstrap",
33
"bundles": {
44
"zcf": {
5-
"sourceSpec": "../../../zoe/src/contractFacet.js"
5+
"sourceSpec": "@agoric/zoe/src/contractFacet"
66
}
77
},
88
"vats": {

0 commit comments

Comments
 (0)