diff --git a/packages/daemon/src/daemon.js b/packages/daemon/src/daemon.js index 96bdd71e18..334710f031 100644 --- a/packages/daemon/src/daemon.js +++ b/packages/daemon/src/daemon.js @@ -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') { @@ -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 @@ -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: @@ -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) @@ -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(); @@ -927,6 +947,7 @@ const makeDaemonCore = async ( const makeIdentifiedHost = makeHostMaker({ provideValueForFormulaIdentifier, provideControllerForFormulaIdentifier, + incarnateWorker, incarnateHost, incarnateGuest, incarnateEval, diff --git a/packages/daemon/src/host.js b/packages/daemon/src/host.js index e72b58107e..00d6a61f27 100644 --- a/packages/daemon/src/host.js +++ b/packages/daemon/src/host.js @@ -8,6 +8,7 @@ const { quote: q } = assert; export const makeHostMaker = ({ provideValueForFormulaIdentifier, provideControllerForFormulaIdentifier, + incarnateWorker, incarnateHost, incarnateGuest, incarnateEval, @@ -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); } diff --git a/packages/daemon/src/types.d.ts b/packages/daemon/src/types.d.ts index 1f632b7974..8c452d42d9 100644 --- a/packages/daemon/src/types.d.ts +++ b/packages/daemon/src/types.d.ts @@ -64,6 +64,10 @@ type EndoFormula = { webPageJs?: string; }; +type WorkerFormula = { + type: 'worker'; +}; + type HostFormula = { type: 'host'; worker: string; @@ -76,6 +80,8 @@ type HostFormula = { type GuestFormula = { type: 'guest'; host: string; + petStore: string; + worker: string; }; type LeastAuthorityFormula = { @@ -140,6 +146,7 @@ type HandleFormula = { export type Formula = | EndoFormula + | WorkerFormula | HostFormula | GuestFormula | LeastAuthorityFormula