@agoric/swingset-vat 0.32.3-dev-ca248f1.0.ca248f1 → 0.32.3-dev-0dccf64.0.0dccf64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agoric/swingset-vat",
3
- "version": "0.32.3-dev-ca248f1.0.ca248f1",
3
+ "version": "0.32.3-dev-0dccf64.0.0dccf64",
4
4
  "description": "Vat/Container Launcher",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -22,7 +22,7 @@
22
22
  "lint:eslint": "yarn run -T eslint ."
23
23
  },
24
24
  "devDependencies": {
25
- "@agoric/xsnap": "0.14.3-dev-ca248f1.0.ca248f1",
25
+ "@agoric/xsnap": "0.14.3-dev-0dccf64.0.0dccf64",
26
26
  "@types/better-sqlite3": "^7.6.13",
27
27
  "@types/microtime": "^2.1.0",
28
28
  "@types/tmp": "^0.2.0",
@@ -30,15 +30,15 @@
30
30
  "ava": "^6.4.1"
31
31
  },
32
32
  "dependencies": {
33
- "@agoric/internal": "0.3.3-dev-ca248f1.0.ca248f1",
34
- "@agoric/kmarshal": "0.1.1-dev-ca248f1.0.ca248f1",
35
- "@agoric/store": "0.9.3-dev-ca248f1.0.ca248f1",
36
- "@agoric/swing-store": "0.9.2-dev-ca248f1.0.ca248f1",
37
- "@agoric/swingset-liveslots": "0.10.3-dev-ca248f1.0.ca248f1",
38
- "@agoric/swingset-xsnap-supervisor": "0.10.3-dev-ca248f1.0.ca248f1",
39
- "@agoric/time": "0.3.3-dev-ca248f1.0.ca248f1",
40
- "@agoric/vat-data": "0.5.3-dev-ca248f1.0.ca248f1",
41
- "@agoric/xsnap-lockdown": "0.14.1-dev-ca248f1.0.ca248f1",
33
+ "@agoric/internal": "0.3.3-dev-0dccf64.0.0dccf64",
34
+ "@agoric/kmarshal": "0.1.1-dev-0dccf64.0.0dccf64",
35
+ "@agoric/store": "0.9.3-dev-0dccf64.0.0dccf64",
36
+ "@agoric/swing-store": "0.9.2-dev-0dccf64.0.0dccf64",
37
+ "@agoric/swingset-liveslots": "0.10.3-dev-0dccf64.0.0dccf64",
38
+ "@agoric/swingset-xsnap-supervisor": "0.10.3-dev-0dccf64.0.0dccf64",
39
+ "@agoric/time": "0.3.3-dev-0dccf64.0.0dccf64",
40
+ "@agoric/vat-data": "0.5.3-dev-0dccf64.0.0dccf64",
41
+ "@agoric/xsnap-lockdown": "0.14.1-dev-0dccf64.0.0dccf64",
42
42
  "@endo/base64": "^1.0.12",
43
43
  "@endo/bundle-source": "^4.1.2",
44
44
  "@endo/captp": "^4.4.8",
@@ -105,5 +105,5 @@
105
105
  "typeCoverage": {
106
106
  "atLeast": 76.72
107
107
  },
108
- "gitHead": "ca248f1c1f1d3ade1c8b2fd248ef297e5f6a8d7b"
108
+ "gitHead": "0dccf64926ac58da475a8eb599300841f55ee84f"
109
109
  }
@@ -1,6 +1,11 @@
1
- import { getLockdownBundleSHA256, getLockdownBundle } from '@agoric/xsnap-lockdown';
2
- import { getSupervisorBundleSHA256, getSupervisorBundle } from '@agoric/swingset-xsnap-supervisor';
3
-
1
+ import {
2
+ getLockdownBundleSHA256,
3
+ getLockdownBundle,
4
+ } from '@agoric/xsnap-lockdown';
5
+ import {
6
+ getSupervisorBundleSHA256,
7
+ getSupervisorBundle,
8
+ } from '@agoric/swingset-xsnap-supervisor';
4
9
 
5
10
  export const makeXsnapBundleData = harden(() => {
6
11
  return harden({
@@ -56,4 +61,3 @@ export const makeWorkerBundleHandler = harden((bundleStore, bundleData) => {
56
61
  },
57
62
  });
58
63
  });
59
-
@@ -0,0 +1,10 @@
1
+ /**
2
+ * @typedef {(bundleSpecPath: string) => string} ReadText
3
+ */
4
+
5
+ /**
6
+ * @param {ReadText} readText
7
+ * @param {string} bundleSpecPath
8
+ */
9
+ export const parseBundleSpec = (readText, bundleSpecPath) =>
10
+ JSON.parse(readText(bundleSpecPath));
@@ -25,9 +25,10 @@ import { kslot, krefOf } from '@agoric/kmarshal';
25
25
  import { insistStorageAPI } from '../lib/storageAPI.js';
26
26
  import { insistCapData } from '../lib/capdata.js';
27
27
  import {
28
+ buildSwingsetKernelConfig,
28
29
  buildKernelBundle,
30
+ initializeSwingsetKernel,
29
31
  swingsetIsInitialized,
30
- initializeSwingset,
31
32
  } from './initializeSwingset.js';
32
33
  import {
33
34
  makeWorkerBundleHandler,
@@ -683,13 +684,17 @@ export async function buildVatController(
683
684
  let bootstrapResult;
684
685
  await null;
685
686
  if (!swingsetIsInitialized(kernelStorage)) {
686
- bootstrapResult = await initializeSwingset(
687
+ const kernelConfig = await buildSwingsetKernelConfig(
687
688
  config,
688
689
  bootstrapArgs,
689
- kernelStorage,
690
690
  initializationOptions,
691
691
  runtimeOptions,
692
692
  );
693
+ bootstrapResult = await initializeSwingsetKernel(
694
+ kernelConfig,
695
+ kernelStorage,
696
+ runtimeOptions,
697
+ );
693
698
  }
694
699
  const controller = await makeSwingsetController(
695
700
  kernelStorage,
@@ -1,33 +1,37 @@
1
1
  /* eslint-env node */
2
- import fs from 'fs';
3
- import path from 'path';
4
2
 
5
- import { assert, bare, Fail } from '@endo/errors';
6
3
  import { deepCopyJsonable, makeTracer } from '@agoric/internal';
7
4
  import { mustMatch } from '@agoric/store';
8
5
  import bundleSource from '@endo/bundle-source';
9
- import { resolve as importMetaResolve } from 'import-meta-resolve';
10
- import { ManagerType } from '../typeGuards.js';
11
- import { provideBundleCache } from '../../tools/bundleTool.js';
6
+ import { assert, Fail } from '@endo/errors';
12
7
  import { kdebugEnable } from '../lib/kdebug.js';
13
8
  import { insistStorageAPI } from '../lib/storageAPI.js';
14
- import { initializeKernel } from './initializeKernel.js';
9
+ import { ManagerType } from '../typeGuards.js';
15
10
  import {
16
11
  makeWorkerBundleHandler,
17
12
  makeXsnapBundleData,
18
13
  } from './bundle-handler.js';
14
+ import { initializeKernel } from './initializeKernel.js';
19
15
 
20
16
  /**
21
- * @import {BundleFormat} from '../types-external.js';
22
17
  * @import {SwingSetConfig} from '../types-external.js';
23
18
  * @import {SwingSetKernelConfig} from '../types-external.js';
24
- * @import {SwingSetConfigDescriptor} from '../types-external.js';
25
19
  * @import {SwingSetConfigProperties} from '../types-external.js';
26
20
  * @import {Bundle} from '../types-external.js';
27
21
  * @import {SwingStoreKernelStorage} from '../types-external.js';
28
22
  * @import {EndoZipBase64Bundle} from '../types-external.js';
29
23
  * @import {BundleHandler} from './bundle-handler.js';
30
24
  */
25
+ /**
26
+ * @typedef {(path: string) => Promise<EndoZipBase64Bundle>} BundleFromPathPower
27
+ */
28
+ /**
29
+ * @typedef {(sourceSpec: string, options: {
30
+ * dev?: boolean,
31
+ * format?: string,
32
+ * byteLimit?: number,
33
+ * }) => Promise<EndoZipBase64Bundle>} BundleFromSourceSpecPower
34
+ */
31
35
 
32
36
  const trace = makeTracer('IniSwi', false);
33
37
 
@@ -98,184 +102,8 @@ export async function buildKernelBundles() {
98
102
  return harden({ kernel: kernelBundle, ...vdBundles });
99
103
  }
100
104
 
101
- /**
102
- * Scan a directory for files defining the vats to bootstrap for a swingset, and
103
- * produce a swingset config object for what was found there. Looks for files
104
- * with names of the pattern `vat-NAME.js` as well as a file named
105
- * 'bootstrap.js'.
106
- *
107
- * @param {string} basedir The directory to scan
108
- * @param {object} [options]
109
- * @param {boolean} [options.includeDevDependencies] whether to include devDependencies
110
- * @param {BundleFormat} [options.bundleFormat] the bundle format to use
111
- * @returns {SwingSetConfig} a swingset config object: {
112
- * bootstrap: "bootstrap",
113
- * vats: {
114
- * NAME: {
115
- * sourceSpec: PATHSTRING
116
- * }
117
- * }
118
- * }
119
- *
120
- * Where NAME is the name of the vat; `sourceSpec` contains the path to the vat with that name. Note that
121
- * the `bootstrap` property names the vat that should be used as the bootstrap vat. Although a swingset
122
- * configuration can designate any vat as its bootstrap vat, `loadBasedir` will always look for a file named
123
- * 'bootstrap.js' and use that (note that if there is no 'bootstrap.js', there will be no bootstrap vat).
124
- *
125
- * Swingsets defined by scanning a directory in this manner define no devices.
126
- */
127
- export function loadBasedir(basedir, options = {}) {
128
- const { includeDevDependencies = false, bundleFormat = undefined } = options;
129
- /** @type { SwingSetConfigDescriptor } */
130
- const vats = {};
131
- const rVatName = /^vat-(.*)\.js$/s;
132
- const files = fs.readdirSync(basedir, { withFileTypes: true });
133
- const vatFiles = files.flatMap(dirent => {
134
- const file = dirent.name;
135
- const m = rVatName.exec(file);
136
- return m && dirent.isFile() ? [{ file, label: m[1] }] : [];
137
- });
138
- vatFiles.sort((a, b) => (a.label < b.label ? -1 : a.label > b.label ? 1 : 0));
139
- for (const { file, label } of vatFiles) {
140
- const vatSourcePath = path.resolve(basedir, file);
141
- vats[label] = { sourceSpec: vatSourcePath, parameters: {} };
142
- }
143
- /** @type {string | void} */
144
- let bootstrapPath = path.resolve(basedir, 'bootstrap.js');
145
- try {
146
- fs.statSync(bootstrapPath);
147
- } catch (e) {
148
- // TODO this will catch the case of the file not existing but doesn't check
149
- // that it's a plain file and not a directory or something else unreadable.
150
- // Consider putting in a more sophisticated check if this whole directory
151
- // scanning thing is something we decide we want to have long term.
152
- bootstrapPath = undefined;
153
- }
154
- const config = { vats, includeDevDependencies, format: bundleFormat };
155
- if (bootstrapPath) {
156
- vats.bootstrap = {
157
- sourceSpec: bootstrapPath,
158
- parameters: {},
159
- };
160
- config.bootstrap = 'bootstrap';
161
- }
162
- return config;
163
- }
164
-
165
- /**
166
- * Resolve a pathname found in a config descriptor. First try to resolve it as
167
- * a module path, and then if that doesn't work try to resolve it as an
168
- * ordinary path relative to the directory in which the config file was found.
169
- *
170
- * @param {string} referrer URL of file or directory containing the config file
171
- * @param {string} specPath Path found in a `sourceSpec` or `bundleSpec` property
172
- *
173
- * @returns {Promise<string>} the absolute path corresponding to `specPath` if it can be
174
- * determined.
175
- */
176
- async function resolveSpecFromConfig(referrer, specPath) {
177
- await null;
178
- try {
179
- return new URL(await importMetaResolve(specPath, referrer)).pathname;
180
- } catch (e) {
181
- if (e.code !== 'MODULE_NOT_FOUND' && e.code !== 'ERR_MODULE_NOT_FOUND') {
182
- throw e;
183
- }
184
- }
185
- return new URL(specPath, referrer).pathname;
186
- }
187
-
188
- /**
189
- * Convert each entry in a config descriptor group (`vats`/`bundles`/etc.) to
190
- * normal form: resolve each pathname to a context-insensitive absolute path and
191
- * run any other appropriate fixup.
192
- *
193
- * @param {SwingSetConfig} config
194
- * @param {'vats' | 'bundles' | 'devices'} groupName
195
- * @param {string | undefined} configPath of the containing config file
196
- * @param {string} referrer URL
197
- * @param {(entry: SwingSetConfigProperties, name?: string) => void} [fixupEntry]
198
- * A function to call on each entry to e.g. add defaults for missing fields
199
- * such as vat `parameters`.
200
- */
201
- async function normalizeConfigDescriptor(
202
- config,
203
- groupName,
204
- configPath,
205
- referrer,
206
- fixupEntry,
207
- ) {
208
- const normalizeSpec = async (entry, specKey, name) => {
209
- const sourcePath = await resolveSpecFromConfig(referrer, entry[specKey]);
210
- fs.existsSync(sourcePath) ||
211
- Fail`${sourcePath} for ${bare(groupName)}[${name}].${bare(specKey)} in ${configPath} config file does not exist`;
212
- entry[specKey] = sourcePath;
213
- };
214
-
215
- const jobs = [];
216
- const desc = config[groupName];
217
- if (desc) {
218
- for (const [name, entry] of Object.entries(desc)) {
219
- fixupEntry?.(entry, name);
220
- if ('sourceSpec' in entry) {
221
- jobs.push(normalizeSpec(entry, 'sourceSpec', name));
222
- }
223
- if ('bundleSpec' in entry) {
224
- jobs.push(normalizeSpec(entry, 'bundleSpec', name));
225
- }
226
- }
227
- }
228
- return Promise.all(jobs);
229
- }
230
-
231
- /**
232
- * @param {SwingSetConfig} config
233
- * @param {string} [configPath]
234
- * @returns {Promise<void>}
235
- * @throws {Error} if the config is invalid
236
- */
237
- export async function normalizeConfig(config, configPath) {
238
- const base = `file://${process.cwd()}/`;
239
- const referrer = configPath
240
- ? new URL(configPath, base).href
241
- : new URL(base).href;
242
- const fixupVat = vat => (vat.parameters ||= {});
243
- await Promise.all([
244
- normalizeConfigDescriptor(config, 'vats', configPath, referrer, fixupVat),
245
- normalizeConfigDescriptor(config, 'bundles', configPath, referrer),
246
- // TODO: represent devices
247
- // normalizeConfigDescriptor(config, 'devices', configPath, referrer),
248
- ]);
249
- config.bootstrap ||
250
- Fail`no designated bootstrap vat in ${configPath} config file`;
251
- (config.vats && config.vats[/** @type {string} */ (config.bootstrap)]) ||
252
- Fail`bootstrap vat ${config.bootstrap} not found in ${configPath} config file`;
253
- }
254
-
255
- /**
256
- * Read and normalize a swingset config file.
257
- *
258
- * @param {string} configPath
259
- * @returns {Promise<SwingSetConfig | null>} the normalized config,
260
- * or null if the file did not exist
261
- */
262
- export async function loadSwingsetConfigFile(configPath) {
263
- await null;
264
- try {
265
- const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
266
- await normalizeConfig(config, configPath);
267
- return config;
268
- } catch (e) {
269
- console.error(`failed to load ${configPath}`);
270
- if (e.code === 'ENOENT') {
271
- return null;
272
- } else {
273
- throw e;
274
- }
275
- }
276
- }
277
-
278
105
  export function swingsetIsInitialized(kernelStorage) {
106
+ insistStorageAPI(kernelStorage.kvStore);
279
107
  return !!(
280
108
  kernelStorage.kvStore.get('version') ||
281
109
  kernelStorage.kvStore.get('initialized')
@@ -304,34 +132,32 @@ function sortObjectProperties(obj, firsts = []) {
304
132
  * addTimer?: boolean,
305
133
  * }} InitializationOptions
306
134
  */
135
+ /**
136
+ * @typedef {{
137
+ * env?: Record<string, string | undefined>,
138
+ * verbose?: boolean,
139
+ * bundleFromPath?: BundleFromPathPower,
140
+ * bundleFromSourceSpec?: BundleFromSourceSpecPower,
141
+ * bundleHandler?: BundleHandler,
142
+ * }} InitializeSwingsetRuntimeOptions
143
+ */
307
144
 
308
145
  /**
146
+ * Build the kernel-facing config object with deterministic bundle IDs.
147
+ *
309
148
  * @param {SwingSetConfig} config
310
149
  * @param {unknown} bootstrapArgs
311
- * @param {SwingStoreKernelStorage} kernelStorage
312
150
  * @param {InitializationOptions} initializationOptions
313
- * @param {{ env?: Record<string, string | undefined >,
314
- * bundleHandler?: BundleHandler,
315
- * }} runtimeOptions
316
- * @returns {Promise<string | undefined>} KPID of the bootstrap message result promise
151
+ * @param {InitializeSwingsetRuntimeOptions} runtimeOptions
152
+ * @returns {Promise<SwingSetKernelConfig>}
317
153
  */
318
- export async function initializeSwingset(
154
+ export async function buildSwingsetKernelConfig(
319
155
  config,
320
156
  bootstrapArgs,
321
- kernelStorage,
322
157
  initializationOptions = {},
323
158
  runtimeOptions = {},
324
159
  ) {
325
- const kvStore = kernelStorage.kvStore;
326
- insistStorageAPI(kvStore);
327
- !swingsetIsInitialized(kernelStorage) ||
328
- Fail`kernel store already initialized`;
329
- const {
330
- bundleHandler = makeWorkerBundleHandler(
331
- kernelStorage.bundleStore,
332
- makeXsnapBundleData(),
333
- ),
334
- } = runtimeOptions;
160
+ const { bundleFromPath, bundleFromSourceSpec } = runtimeOptions;
335
161
 
336
162
  // copy config so we can safely mess with it even if it's shared or hardened
337
163
  config = deepCopyJsonable(config);
@@ -379,7 +205,6 @@ export async function initializeSwingset(
379
205
  initializationOptions.kernelBundles || buildVatAndDeviceBundles();
380
206
  const kernelBundles = await obtainKernelBundles();
381
207
  const {
382
- verbose,
383
208
  addVatAdmin = true,
384
209
  addComms = true,
385
210
  addVattp = true,
@@ -441,21 +266,6 @@ export async function initializeSwingset(
441
266
  };
442
267
  }
443
268
 
444
- const bundleCache = await (config.bundleCachePath
445
- ? provideBundleCache(
446
- config.bundleCachePath,
447
- {
448
- dev: config.includeDevDependencies,
449
- format: config.bundleFormat,
450
- // Disable bundle size limits for kernel-generated bundles which may
451
- // be large, but do not travel through RPC and we still want to be
452
- // legible.
453
- byteLimit: Infinity,
454
- },
455
- s => import(s),
456
- )
457
- : null);
458
-
459
269
  /**
460
270
  * The host application gives us
461
271
  * config.[vats|devices].NAME.[bundle|bundleSpec|sourceSpec|bundleName] .
@@ -485,18 +295,21 @@ export async function initializeSwingset(
485
295
  if ('bundle' in desc) {
486
296
  return desc.bundle;
487
297
  } else if ('bundleSpec' in desc) {
488
- return JSON.parse(fs.readFileSync(desc.bundleSpec).toString());
489
- } else if ('sourceSpec' in desc) {
490
- if (bundleCache) {
491
- return bundleCache.load(desc.sourceSpec);
298
+ if (!bundleFromPath) {
299
+ throw Fail`runtimeOptions.bundleFromPath is required for bundleSpec`;
492
300
  }
493
- return bundleSource(desc.sourceSpec, {
301
+ return bundleFromPath(desc.bundleSpec);
302
+ } else if ('sourceSpec' in desc) {
303
+ const options = {
494
304
  dev: config.includeDevDependencies,
495
305
  format: config.bundleFormat,
496
306
  // Disable bundle size limits for kernel-generated bundles which may be
497
307
  // large, but do not travel through RPC and we still want to be legible.
498
308
  byteLimit: Infinity,
499
- });
309
+ };
310
+ return bundleFromSourceSpec
311
+ ? bundleFromSourceSpec(desc.sourceSpec, options)
312
+ : bundleSource(desc.sourceSpec, options);
500
313
  } else if ('bundleName' in desc) {
501
314
  if (!nameToBundle) {
502
315
  throw Fail`cannot use .bundleName in config.bundles`;
@@ -612,13 +425,64 @@ export async function initializeSwingset(
612
425
  kconfig.namedBundleIDs[name] = nameToBundle[name].id;
613
426
  }
614
427
  delete kconfig.bundles;
428
+ return kconfig;
429
+ }
615
430
 
616
- if (verbose) {
617
- kdebugEnable(true);
618
- }
431
+ /** @type {(kernelStorage: SwingStoreKernelStorage) => void} */
432
+ const assertUninitialized = kernelStorage => {
433
+ !swingsetIsInitialized(kernelStorage) ||
434
+ Fail`kernel store already initialized`;
435
+ };
436
+
437
+ /**
438
+ * Initialize kernel state from a pre-built kernel config.
439
+ *
440
+ * @param {SwingSetKernelConfig} kernelConfig
441
+ * @param {SwingStoreKernelStorage} kernelStorage
442
+ * @param {InitializeSwingsetRuntimeOptions} runtimeOptions
443
+ * @returns {Promise<string | undefined>} KPID of the bootstrap message result promise
444
+ */
445
+ export async function initializeSwingsetKernel(
446
+ kernelConfig,
447
+ kernelStorage,
448
+ runtimeOptions = {},
449
+ ) {
450
+ assertUninitialized(kernelStorage);
451
+
452
+ const {
453
+ bundleHandler = makeWorkerBundleHandler(
454
+ kernelStorage.bundleStore,
455
+ makeXsnapBundleData(),
456
+ ),
457
+ } = runtimeOptions;
619
458
 
620
- const kopts = { bundleHandler };
621
- // returns the kpid of the bootstrap() result
622
- const bootKpid = await initializeKernel(kconfig, kernelStorage, kopts);
623
- return bootKpid;
459
+ return initializeKernel(kernelConfig, kernelStorage, { bundleHandler });
460
+ }
461
+
462
+ /**
463
+ * @deprecated For tests use initializeTestSwingset()
464
+ *
465
+ * @param {SwingSetConfig} config
466
+ * @param {unknown} bootstrapArgs
467
+ * @param {SwingStoreKernelStorage} kernelStorage
468
+ * @param {InitializationOptions} initializationOptions
469
+ * @param {InitializeSwingsetRuntimeOptions} runtimeOptions
470
+ * @returns {Promise<string | undefined>} KPID of the bootstrap message result promise
471
+ */
472
+ export async function initializeSwingset(
473
+ config,
474
+ bootstrapArgs,
475
+ kernelStorage,
476
+ initializationOptions = {},
477
+ runtimeOptions = {},
478
+ ) {
479
+ assertUninitialized(kernelStorage);
480
+ const kernelConfig = await buildSwingsetKernelConfig(
481
+ config,
482
+ bootstrapArgs,
483
+ initializationOptions,
484
+ runtimeOptions,
485
+ );
486
+ if (runtimeOptions.verbose) kdebugEnable(true);
487
+ return initializeSwingsetKernel(kernelConfig, kernelStorage, runtimeOptions);
624
488
  }
@@ -0,0 +1,17 @@
1
+ /* eslint-env node */
2
+ import fs from 'fs';
3
+ import path from 'path';
4
+ import { resolve as importMetaResolve } from 'import-meta-resolve';
5
+ import { makeSwingsetConfigIO } from './swingset-config.js';
6
+
7
+ const { loadBasedir, normalizeConfig, loadSwingsetConfigFile } =
8
+ makeSwingsetConfigIO({
9
+ readdirSync: fs.readdirSync,
10
+ existsSync: fs.existsSync,
11
+ readFileSync: (filePath, encoding) => fs.readFileSync(filePath, encoding),
12
+ pathResolve: path.resolve,
13
+ cwd: () => process.cwd(),
14
+ importMetaResolve,
15
+ });
16
+
17
+ export { loadBasedir, normalizeConfig, loadSwingsetConfigFile };
@@ -0,0 +1,194 @@
1
+ /* eslint-env node */
2
+
3
+ import { b, Fail } from '@endo/errors';
4
+ import url from 'node:url';
5
+
6
+ /**
7
+ * @import * as nodePath from 'node:path';
8
+ * @import {SwingSetConfig} from '../types-external.js';
9
+ * @import {SwingSetConfigDescriptor} from '../types-external.js';
10
+ * @import {SwingSetConfigProperties} from '../types-external.js';
11
+ */
12
+
13
+ /**
14
+ * @typedef {{
15
+ * readdirSync: (path: string, options: { withFileTypes: true }) => Array<{ name: string, isFile: () => boolean }>,
16
+ * existsSync: (path: string) => boolean,
17
+ * readFileSync: (path: string, encoding: 'utf-8') => string,
18
+ * pathResolve: typeof nodePath.resolve,
19
+ * cwd: () => string,
20
+ * importMetaResolve: (specifier: string, referrer: string) => Promise<string> | string,
21
+ * consoleError?: (...args: unknown[]) => void,
22
+ * }} SwingsetConfigIOPowers
23
+ */
24
+
25
+ /**
26
+ * @param {SwingsetConfigIOPowers} powers
27
+ */
28
+ export const makeSwingsetConfigIO = powers => {
29
+ const {
30
+ readdirSync,
31
+ existsSync,
32
+ readFileSync,
33
+ pathResolve,
34
+ cwd,
35
+ importMetaResolve,
36
+ consoleError = (...args) => console.error(...args),
37
+ } = powers;
38
+
39
+ /**
40
+ * Scan a directory for `vat-${name}.js` files defining the vats to bootstrap
41
+ * for a swingset, as well as a file named "bootstrap.js" which if present
42
+ * will be associated with name "bootstrap" and identified as `bootstrap` in
43
+ * the resulting SwingSetConfig.
44
+ * Each vat record will include `sourceSpec` and have empty `parameters`, and
45
+ * there will be no devices.
46
+ *
47
+ * @param {string} basedir
48
+ * @param {Pick<SwingSetConfig, 'includeDevDependencies' | 'bundleFormat'>} [options]
49
+ * copied into the return value
50
+ * @returns {SwingSetConfig}
51
+ */
52
+ const loadBasedir = (basedir, options = {}) => {
53
+ const { includeDevDependencies = false, bundleFormat = undefined } =
54
+ options;
55
+ /** @type { SwingSetConfigDescriptor } */
56
+ const vats = {};
57
+ const rVatName = /^vat-(.*)\.js$/s;
58
+ const files = readdirSync(basedir, { withFileTypes: true });
59
+ const vatFiles = files.flatMap(dirent => {
60
+ const file = dirent.name;
61
+ const m = rVatName.exec(file);
62
+ return m && dirent.isFile() ? [{ file, label: m[1] }] : [];
63
+ });
64
+ vatFiles.sort((a, b2) =>
65
+ a.label < b2.label ? -1 : a.label > b2.label ? 1 : 0,
66
+ );
67
+ for (const { file, label } of vatFiles) {
68
+ const vatSourcePath = pathResolve(basedir, file);
69
+ vats[label] = { sourceSpec: vatSourcePath, parameters: {} };
70
+ }
71
+ const config = { vats, includeDevDependencies, bundleFormat };
72
+ // If the directory contains a bootstrap.js, try to use it (and fail later
73
+ // if it's unreadable/a directory/etc.).
74
+ const bootstrapPath = pathResolve(basedir, 'bootstrap.js');
75
+ if (existsSync(bootstrapPath)) {
76
+ vats.bootstrap = {
77
+ sourceSpec: bootstrapPath,
78
+ parameters: {},
79
+ };
80
+ config.bootstrap = 'bootstrap';
81
+ }
82
+ return config;
83
+ };
84
+
85
+ /**
86
+ * Resolve a module specifier to an absolute path in a particular context,
87
+ * falling back on simple directory path operations if necessary.
88
+ *
89
+ * @param {string} referrer URL
90
+ * @param {string} specPath
91
+ * @returns {Promise<string>}
92
+ */
93
+ const resolveSpecFromConfig = async (referrer, specPath) => {
94
+ await null;
95
+ try {
96
+ return url.fileURLToPath(await importMetaResolve(specPath, referrer));
97
+ } catch (e) {
98
+ if (e.code !== 'MODULE_NOT_FOUND' && e.code !== 'ERR_MODULE_NOT_FOUND') {
99
+ throw e;
100
+ }
101
+ }
102
+ return url.fileURLToPath(new URL(specPath, referrer));
103
+ };
104
+
105
+ /**
106
+ * Convert each entry in a config descriptor group (`vats`/`bundles`/etc.) to
107
+ * normal form: resolve each pathname to a context-insensitive absolute path
108
+ * and run any other appropriate fixup.
109
+ *
110
+ * @param {SwingSetConfig} config
111
+ * @param {'vats' | 'bundles' | 'devices'} groupName
112
+ * @param {string | undefined} configPath of the containing config file
113
+ * @param {string} referrer URL
114
+ * @param {(entry: SwingSetConfigProperties, name?: string) => void} [fixupEntry]
115
+ * A function to call on each entry to e.g. add defaults for missing fields
116
+ * such as vat `parameters`.
117
+ */
118
+ const normalizeConfigDescriptor = async (
119
+ config,
120
+ groupName,
121
+ configPath,
122
+ referrer,
123
+ fixupEntry,
124
+ ) => {
125
+ const normalizeSpec = async (entry, specKey, name) => {
126
+ const sourcePath = await resolveSpecFromConfig(referrer, entry[specKey]);
127
+ existsSync(sourcePath) ||
128
+ Fail`${sourcePath} for ${b(groupName)}[${name}].${b(specKey)} in ${configPath} config file does not exist`;
129
+ entry[specKey] = sourcePath;
130
+ };
131
+
132
+ const jobs = [];
133
+ const desc = config[groupName];
134
+ if (desc) {
135
+ for (const [name, entry] of Object.entries(desc)) {
136
+ fixupEntry?.(entry, name);
137
+ if ('sourceSpec' in entry) {
138
+ jobs.push(normalizeSpec(entry, 'sourceSpec', name));
139
+ }
140
+ if ('bundleSpec' in entry) {
141
+ jobs.push(normalizeSpec(entry, 'bundleSpec', name));
142
+ }
143
+ }
144
+ }
145
+ return Promise.all(jobs);
146
+ };
147
+
148
+ /**
149
+ * @param {SwingSetConfig} config
150
+ * @param {string} [configPath]
151
+ * @returns {Promise<void>}
152
+ */
153
+ const normalizeConfig = async (config, configPath) => {
154
+ const baseHref = `${url.pathToFileURL(cwd()).href}/`;
155
+ const referrer = configPath ? new URL(configPath, baseHref).href : baseHref;
156
+ const fixupVat = vat => (vat.parameters ||= {});
157
+ await Promise.all([
158
+ normalizeConfigDescriptor(config, 'vats', configPath, referrer, fixupVat),
159
+ normalizeConfigDescriptor(config, 'bundles', configPath, referrer),
160
+ ]);
161
+ config.bootstrap ||
162
+ Fail`no designated bootstrap vat in ${configPath} config file`;
163
+ (config.vats && config.vats[/** @type {string} */ (config.bootstrap)]) ||
164
+ Fail`bootstrap vat ${config.bootstrap} not found in ${configPath} config file`;
165
+ };
166
+
167
+ /**
168
+ * Read and normalize a swingset config file, or return null if the file does
169
+ * not exist.
170
+ *
171
+ * @param {string} configPath
172
+ * @returns {Promise<SwingSetConfig | null>}
173
+ */
174
+ const loadSwingsetConfigFile = async configPath => {
175
+ await null;
176
+ try {
177
+ const config = JSON.parse(readFileSync(configPath, 'utf-8'));
178
+ await normalizeConfig(config, configPath);
179
+ return config;
180
+ } catch (e) {
181
+ consoleError(`failed to load ${configPath}`);
182
+ if (e.code === 'ENOENT') {
183
+ return null;
184
+ }
185
+ throw e;
186
+ }
187
+ };
188
+
189
+ return harden({
190
+ loadBasedir,
191
+ normalizeConfig,
192
+ loadSwingsetConfigFile,
193
+ });
194
+ };
package/src/index.js CHANGED
@@ -4,12 +4,16 @@ export {
4
4
  } from './controller/controller.js';
5
5
  export {
6
6
  swingsetIsInitialized,
7
+ buildSwingsetKernelConfig,
8
+ initializeSwingsetKernel,
7
9
  initializeSwingset,
8
10
  buildKernelBundles,
11
+ } from './controller/initializeSwingset.js';
12
+ export {
9
13
  loadBasedir,
10
14
  loadSwingsetConfigFile,
11
15
  normalizeConfig,
12
- } from './controller/initializeSwingset.js';
16
+ } from './controller/swingset-config-node.js';
13
17
  export { upgradeSwingset } from './controller/upgradeSwingset.js';
14
18
  export {
15
19
  buildMailboxStateMap,
@@ -225,7 +225,8 @@ export {};
225
225
  * @property {boolean} [includeDevDependencies] indicates that
226
226
  * `devDependencies` of the surrounding `package.json` should be accessible to
227
227
  * bundles.
228
- * @property {string} [bundleCachePath] if present, SwingSet will use a bundle cache at this path
228
+ * @property {string} [bundleCachePath] optional cache path used by wrappers
229
+ * that choose to cache `sourceSpec` bundling
229
230
  * @property {SwingSetConfigDescriptor<VatConfigOptions>} vats
230
231
  * @property {SwingSetConfigDescriptor} [bundles]
231
232
  * @property {BundleFormat} [bundleFormat] the bundle source / import bundle
@@ -564,7 +564,6 @@ export function buildRootObject(vatPowers, _vatParameters, baggage) {
564
564
  }
565
565
 
566
566
  // XXX TEMP
567
-
568
567
  function meterCrossedThreshold(_meterID, _remaining) {
569
568
  // const { updater } = meterByID.get(meterID); // XXX RESTORE
570
569
  // updater.updateState(remaining); // XXX RESTORE
@@ -4,7 +4,8 @@ import { test } from './prepare-test-env-ava.js';
4
4
  import { assert } from '@endo/errors';
5
5
  import { makeMarshal } from '@endo/marshal';
6
6
  import { initSwingStore } from '@agoric/swing-store';
7
- import { initializeSwingset, makeSwingsetController } from '../src/index.js';
7
+ import { makeSwingsetController } from '../src/index.js';
8
+ import { initializeTestSwingset as initializeSwingset } from './test-swingset.js';
8
9
 
9
10
  export { test };
10
11
 
@@ -0,0 +1,67 @@
1
+ /* eslint-env node */
2
+ import fs from 'fs';
3
+ import path from 'path';
4
+ import { fileURLToPath } from 'url';
5
+ import { makeReadJsonFile } from '@agoric/internal/src/node/read-json.js';
6
+ import { provideBundleCache } from './bundleTool.js';
7
+ import {
8
+ buildSwingsetKernelConfig,
9
+ initializeSwingsetKernel,
10
+ } from '../src/controller/initializeSwingset.js';
11
+
12
+ /**
13
+ * @import {SwingSetConfig} from '../src/types-external.js';
14
+ * @import {SwingStoreKernelStorage} from '../src/types-external.js';
15
+ * @import {InitializationOptions} from '../src/controller/initializeSwingset.js';
16
+ * @import {InitializeSwingsetRuntimeOptions} from '../src/controller/initializeSwingset.js';
17
+ */
18
+
19
+ const readBundleSpecFile = makeReadJsonFile(fs.promises);
20
+
21
+ const sharedBundleCachePath = fileURLToPath(
22
+ new URL('../../../bundles', import.meta.url),
23
+ );
24
+
25
+ /**
26
+ * Test-only wrapper that supplies ambient-powered bundleSpec loading.
27
+ *
28
+ * @param {SwingSetConfig} config
29
+ * @param {unknown} bootstrapArgs
30
+ * @param {SwingStoreKernelStorage} kernelStorage
31
+ * @param {InitializationOptions} initializationOptions
32
+ * @param {InitializeSwingsetRuntimeOptions} runtimeOptions
33
+ */
34
+ export const initializeTestSwingset = async (
35
+ config,
36
+ bootstrapArgs,
37
+ kernelStorage,
38
+ initializationOptions = {},
39
+ runtimeOptions = {},
40
+ ) => {
41
+ const {
42
+ bundleCachePath = sharedBundleCachePath,
43
+ includeDevDependencies,
44
+ bundleFormat,
45
+ } = config;
46
+ const cache = await provideBundleCache(
47
+ path.resolve(bundleCachePath),
48
+ {
49
+ dev: includeDevDependencies,
50
+ format: bundleFormat,
51
+ byteLimit: Infinity,
52
+ },
53
+ spec => import(spec),
54
+ );
55
+
56
+ const kernelConfig = await buildSwingsetKernelConfig(
57
+ config,
58
+ bootstrapArgs,
59
+ initializationOptions,
60
+ {
61
+ ...runtimeOptions,
62
+ bundleFromPath: runtimeOptions.bundleFromPath || readBundleSpecFile,
63
+ bundleFromSourceSpec: (sourceSpec, _options) => cache.load(sourceSpec),
64
+ },
65
+ );
66
+ return initializeSwingsetKernel(kernelConfig, kernelStorage, runtimeOptions);
67
+ };