Skip to content

Commit

Permalink
feat(daemon): add incarnateWorker
Browse files Browse the repository at this point in the history
  • Loading branch information
kumavis committed Feb 19, 2024
1 parent bbcdc11 commit d110daf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
39 changes: 30 additions & 9 deletions packages/daemon/src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,17 +373,13 @@ const makeDaemonCore = async (
context,
);
} else if (formula.type === 'guest') {
const storeFormulaNumber = derive(formulaNumber, 'pet-store');
const storeFormulaIdentifier = `pet-store:${storeFormulaNumber}`;
const workerFormulaNumber = derive(formulaNumber, 'worker');
const workerFormulaIdentifier = `worker:${workerFormulaNumber}`;
// Behold, recursion:
// eslint-disable-next-line no-use-before-define
return makeIdentifiedGuestController(
formulaIdentifier,
formula.host,
storeFormulaIdentifier,
workerFormulaIdentifier,
formula.petStore,
formula.worker,
context,
);
} else if (formula.type === 'web-bundle') {
Expand Down Expand Up @@ -705,6 +701,20 @@ const makeDaemonCore = async (
);
};

/**
* @returns {Promise<{ formulaIdentifier: string, value: import('./types').EndoWorker }>}
*/
const incarnateWorker = async () => {
const formulaNumber = await randomHex512();
/** @type {import('./types.js').WorkerFormula} */
const formula = {
type: 'worker',
};
return /** @type {Promise<{ formulaIdentifier: string, value: import('./types').EndoWorker }>} */ (
provideValueForNumberedFormula(formula.type, formulaNumber, formula)
);
};

/**
* @param {string} endoFormulaIdentifier
* @param {string} leastAuthorityFormulaIdentifier
Expand All @@ -717,8 +727,11 @@ const makeDaemonCore = async (
specifiedWorkerFormulaIdentifier,
) => {
const formulaNumber = await randomHex512();
const workerFormulaIdentifier =
specifiedWorkerFormulaIdentifier || `worker:${await randomHex512()}`;
let workerFormulaIdentifier = specifiedWorkerFormulaIdentifier;
if (workerFormulaIdentifier === undefined) {
({ formulaIdentifier: workerFormulaIdentifier } =
await incarnateWorker());
}
const inspectorFormulaNumber = derive(formulaNumber, 'pet-inspector');
const inspectorFormulaIdentifier = `pet-inspector:${inspectorFormulaNumber}`;
// Note the pet store formula number derivation path:
Expand All @@ -745,10 +758,16 @@ const makeDaemonCore = async (
*/
const incarnateGuest = async hostHandleFormulaIdentifier => {
const formulaNumber = await randomHex512();
const storeFormulaNumber = derive(formulaNumber, 'pet-store');
const storeFormulaIdentifier = `pet-store:${storeFormulaNumber}`;
const { formulaIdentifier: workerFormulaIdentifier } =
await incarnateWorker();
/** @type {import('./types.js').GuestFormula} */
const formula = {
type: 'guest',
host: hostHandleFormulaIdentifier,
petStore: storeFormulaIdentifier,
worker: workerFormulaIdentifier,
};
return /** @type {Promise<{ formulaIdentifier: string, value: import('./types').EndoGuest }>} */ (
provideValueForNumberedFormula(formula.type, formulaNumber, formula)
Expand Down Expand Up @@ -890,8 +909,9 @@ const makeDaemonCore = async (
const incarnateEndoBootstrap = async specifiedFormulaNumber => {
const formulaNumber = specifiedFormulaNumber || (await randomHex512());
const endoFormulaIdentifier = `endo:${formulaNumber}`;
const defaultHostWorkerFormulaIdentifier = `worker:${await randomHex512()}`;

const { formulaIdentifier: defaultHostWorkerFormulaIdentifier } =
await incarnateWorker();
const { formulaIdentifier: leastAuthorityFormulaIdentifier } =
await incarnateLeastAuthority();

Expand Down Expand Up @@ -927,6 +947,7 @@ const makeDaemonCore = async (
const makeIdentifiedHost = makeHostMaker({
provideValueForFormulaIdentifier,
provideControllerForFormulaIdentifier,
incarnateWorker,
incarnateHost,
incarnateGuest,
incarnateEval,
Expand Down
10 changes: 6 additions & 4 deletions packages/daemon/src/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { quote: q } = assert;
export const makeHostMaker = ({
provideValueForFormulaIdentifier,
provideControllerForFormulaIdentifier,
incarnateWorker,
incarnateHost,
incarnateGuest,
incarnateEval,
Expand Down Expand Up @@ -207,14 +208,15 @@ export const makeHostMaker = ({
if (workerName === 'MAIN') {
return mainWorkerFormulaIdentifier;
} else if (workerName === 'NEW') {
const workerId512 = await randomHex512();
return `worker:${workerId512}`;
const { formulaIdentifier: workerFormulaIdentifier } =
await incarnateWorker();
return workerFormulaIdentifier;
}
assertPetName(workerName);
let workerFormulaIdentifier = identifyLocal(workerName);
if (workerFormulaIdentifier === undefined) {
const workerId512 = await randomHex512();
workerFormulaIdentifier = `worker:${workerId512}`;
({ formulaIdentifier: workerFormulaIdentifier } =
await incarnateWorker());
assertPetName(workerName);
await petStore.write(workerName, workerFormulaIdentifier);
}
Expand Down
7 changes: 7 additions & 0 deletions packages/daemon/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ type EndoFormula = {
webPageJs?: string;
};

type WorkerFormula = {
type: 'worker';
};

type HostFormula = {
type: 'host';
worker: string;
Expand All @@ -76,6 +80,8 @@ type HostFormula = {
type GuestFormula = {
type: 'guest';
host: string;
petStore: string;
worker: string;
};

type LeastAuthorityFormula = {
Expand Down Expand Up @@ -140,6 +146,7 @@ type HandleFormula = {

export type Formula =
| EndoFormula
| WorkerFormula
| HostFormula
| GuestFormula
| LeastAuthorityFormula
Expand Down

0 comments on commit d110daf

Please sign in to comment.