Skip to content

Commit

Permalink
feat(daemon): add incarnateLeastAuthority
Browse files Browse the repository at this point in the history
  • Loading branch information
kumavis committed Feb 18, 2024
1 parent e82a7b7 commit 073d46d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
45 changes: 33 additions & 12 deletions packages/daemon/src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ import { assertPetName } from './pet-name.js';
import { makeContextMaker } from './context.js';
import { parseFormulaIdentifier } from './formula-identifier.js';

/** @type {import('./types.js').EndoGuest} */
const leastAuthority = Far('EndoGuest', {
async request() {
throw new Error('declined');
},
});

const delay = async (ms, cancelled) => {
// Do not attempt to set up a timer if already cancelled.
await Promise.race([cancelled, undefined]);
Expand Down Expand Up @@ -428,7 +421,13 @@ const makeDaemonCore = async (
provideValueForFormulaIdentifier(formula.host)
);
},
leastAuthority: async () => leastAuthority,
leastAuthority: () => {
// Behold, recursion:
return /** @type {Promise<import('./types.js').EndoGuest>} */ (
// eslint-disable-next-line no-use-before-define
provideValueForFormulaIdentifier(formula.leastAuthority)
);
},
webPageJs: () => {
if (formula.webPageJs === undefined) {
throw new Error('No web-page-js formula provided.');
Expand Down Expand Up @@ -456,6 +455,14 @@ const makeDaemonCore = async (
external: endoBootstrap,
internal: undefined,
};
} else if (formula.type === 'least-authority') {
/** @type {import('./types.js').EndoGuest} */
const leastAuthority = Far('EndoGuest', {
async request() {
throw new Error('declined');
},
});
return { external: leastAuthority, internal: undefined };
} else {
throw new TypeError(`Invalid formula: ${q(formula)}`);
}
Expand Down Expand Up @@ -492,8 +499,6 @@ const makeDaemonCore = async (
assertPetName,
);
return { external, internal: undefined };
} else if (formulaType === 'least-authority') {
return { external: leastAuthority, internal: undefined };
} else if (
[
'endo',
Expand All @@ -502,6 +507,7 @@ const makeDaemonCore = async (
'make-bundle',
'host',
'guest',
'least-authority',
'web-bundle',
'web-page-js',
'handle',
Expand Down Expand Up @@ -676,6 +682,20 @@ const makeDaemonCore = async (
makeMailbox,
});

/**
* @returns {Promise<{ formulaIdentifier: string, value: import('./types').EndoGuest }>}
*/
const incarnateLeastAuthority = async () => {
const formulaNumber = await randomHex512();
/** @type {import('./types.js').LeastAuthorityFormula} */
const formula = {
type: 'least-authority',
};
return /** @type {Promise<{ formulaIdentifier: string, value: import('./types').EndoGuest }>} */ (
provideValueForNumberedFormula(formula.type, formulaNumber, formula)
);
};

/**
* @param {string} targetFormulaIdentifier
* @returns {Promise<{ formulaIdentifier: string, value: import('./types').Handle }>}
Expand Down Expand Up @@ -769,10 +789,11 @@ const makeDaemonCore = async (
const incarnateEndoBootstrap = async specifiedFormulaNumber => {
const formulaNumber = specifiedFormulaNumber || (await randomHex512());
const endoFormulaIdentifier = `endo:${formulaNumber}`;

const leastAuthorityFormulaIdentifier = `least-authority:${await randomHex512()}`;
const defaultHostWorkerFormulaIdentifier = `worker:${await randomHex512()}`;

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

// Ensure the default host is incarnated and persisted.
const { formulaIdentifier: defaultHostFormulaIdentifier } =
await incarnateHost(
Expand Down
5 changes: 5 additions & 0 deletions packages/daemon/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ type GuestFormula = {
host: string;
};

type LeastAuthorityFormula = {
type: 'least-authority';
};

type EvalFormula = {
type: 'eval';
worker: string;
Expand Down Expand Up @@ -133,6 +137,7 @@ export type Formula =
| EndoFormula
| HostFormula
| GuestFormula
| LeastAuthorityFormula
| EvalFormula
| LookupFormula
| MakeUnconfinedFormula
Expand Down

0 comments on commit 073d46d

Please sign in to comment.