Skip to content

Commit

Permalink
refactor(daemon): Extract caplet incarnation prelude to function
Browse files Browse the repository at this point in the history
  • Loading branch information
rekmarks committed Mar 9, 2024
1 parent 520b2ae commit eb82858
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 42 deletions.
4 changes: 3 additions & 1 deletion packages/cli/src/pet-name.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { quote: q } = assert;

/**
* Splits a dot-delimited pet name path into an array of pet names.
* Throws if any of the path segments are empty.
Expand All @@ -10,7 +12,7 @@ export const parsePetNamePath = petNamePath => {
for (const petName of petNames) {
if (petName === '') {
throw new Error(
`Pet name path "${petNamePath}" contains an empty segment.`,
`Pet name path ${q(petNamePath)} contains an empty segment.`,
);
}
}
Expand Down
93 changes: 53 additions & 40 deletions packages/daemon/src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,41 @@ const makeDaemonCore = async (
return guestIncarnation.formulaIdentifier;
};

/**
* Helper for `incarnateUnconfined` and `incarnateBundle`.
* @param {'make-bundle' | 'make-unconfined'} formulaType
* @param {string} hostFormulaIdentifier
* @param {import('./types.js').DeferredTasks<import('./types.js').MakeCapletDeferredTaskParams>} deferredTasks
* @param {string} [specifiedWorkerFormulaIdentifier]
* @param {string} [specifiedPowersFormulaIdentifier]
*/
const incarnateCapletDependencies = async (
formulaType,
hostFormulaIdentifier,
deferredTasks,
specifiedWorkerFormulaIdentifier,
specifiedPowersFormulaIdentifier,
) => {
const ownFormulaNumber = await randomHex512();
const identifiers = harden({
powersFormulaIdentifier: await providePowersFormulaIdentifier(
hostFormulaIdentifier,
specifiedPowersFormulaIdentifier,
),
capletFormulaIdentifier: serializeFormulaIdentifier({
type: formulaType,
number: ownFormulaNumber,
node: ownNodeIdentifier,
}),
capletFormulaNumber: ownFormulaNumber,
workerFormulaIdentifier: await provideWorkerFormulaIdentifier(
specifiedWorkerFormulaIdentifier,
),
});
await deferredTasks.execute(identifiers);
return identifiers;
};

/** @type {import('./types.js').DaemonCore['incarnateUnconfined']} */
const incarnateUnconfined = async (
hostFormulaIdentifier,
Expand All @@ -1226,26 +1261,15 @@ const makeDaemonCore = async (
powersFormulaIdentifier,
capletFormulaNumber,
workerFormulaIdentifier,
} = await formulaGraphMutex.enqueue(async () => {
const ownFormulaNumber = await randomHex512();
const identifiers = harden({
powersFormulaIdentifier: await providePowersFormulaIdentifier(
hostFormulaIdentifier,
specifiedPowersFormulaIdentifier,
),
capletFormulaIdentifier: serializeFormulaIdentifier({
type: 'make-unconfined',
number: ownFormulaNumber,
node: ownNodeIdentifier,
}),
capletFormulaNumber: ownFormulaNumber,
workerFormulaIdentifier: await provideWorkerFormulaIdentifier(
specifiedWorkerFormulaIdentifier,
),
});
await deferredTasks.execute(identifiers);
return identifiers;
});
} = await formulaGraphMutex.enqueue(() =>
incarnateCapletDependencies(
'make-unconfined',
hostFormulaIdentifier,
deferredTasks,
specifiedWorkerFormulaIdentifier,
specifiedPowersFormulaIdentifier,
),
);

/** @type {import('./types.js').MakeUnconfinedFormula} */
const formula = {
Expand Down Expand Up @@ -1273,26 +1297,15 @@ const makeDaemonCore = async (
powersFormulaIdentifier,
capletFormulaNumber,
workerFormulaIdentifier,
} = await formulaGraphMutex.enqueue(async () => {
const ownFormulaNumber = await randomHex512();
const identifiers = harden({
powersFormulaIdentifier: await providePowersFormulaIdentifier(
hostFormulaIdentifier,
specifiedPowersFormulaIdentifier,
),
capletFormulaIdentifier: serializeFormulaIdentifier({
type: 'make-bundle',
number: ownFormulaNumber,
node: ownNodeIdentifier,
}),
capletFormulaNumber: ownFormulaNumber,
workerFormulaIdentifier: await provideWorkerFormulaIdentifier(
specifiedWorkerFormulaIdentifier,
),
});
await deferredTasks.execute(identifiers);
return identifiers;
});
} = await formulaGraphMutex.enqueue(() =>
incarnateCapletDependencies(
'make-bundle',
hostFormulaIdentifier,
deferredTasks,
specifiedWorkerFormulaIdentifier,
specifiedPowersFormulaIdentifier,
),
);

/** @type {import('./types.js').MakeBundleFormula} */
const formula = {
Expand Down
2 changes: 1 addition & 1 deletion packages/daemon/src/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ export const makeHostMaker = ({
) => {
const bundleFormulaIdentifier = petStore.identifyLocal(bundleName);
if (bundleFormulaIdentifier === undefined) {
throw new TypeError(`Unknown pet name for bundle: ${bundleName}`);
throw new TypeError(`Unknown pet name for bundle: ${q(bundleName)}`);
}

const { tasks, workerFormulaIdentifier, powersFormulaIdentifier } =
Expand Down

0 comments on commit eb82858

Please sign in to comment.