Skip to content

Commit

Permalink
feat(daemon): Rename Locator to Config, improve type exports (merge #…
Browse files Browse the repository at this point in the history
…2212)

Renames the daemon's `Locator`, used to specify its file and storage
paths, to `Config` to get rid of the name collision with endo locator
strings. Also adds return values to functions in exported types, and
removes redundant definition of `Config` type.
  • Loading branch information
rekmarks authored Apr 15, 2024
2 parents 13c52f1 + 89d4a2c commit b71d7d1
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 191 deletions.
File renamed without changes.
10 changes: 5 additions & 5 deletions packages/cli/src/endo.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,39 +464,39 @@ export const main = async rawArgs => {
.command('state')
.description('prints the state directory path')
.action(async _cmd => {
const { statePath } = await import('./locator.js');
const { statePath } = await import('./config.js');
process.stdout.write(`${statePath}\n`);
});

where
.command('run')
.description('prints the daemon PID file path')
.action(async _cmd => {
const { ephemeralStatePath } = await import('./locator.js');
const { ephemeralStatePath } = await import('./config.js');
process.stdout.write(`${ephemeralStatePath}\n`);
});

where
.command('sock')
.description('prints the UNIX domain socket or Windows named pipe path')
.action(async _cmd => {
const { sockPath } = await import('./locator.js');
const { sockPath } = await import('./config.js');
process.stdout.write(`${sockPath}\n`);
});

where
.command('log')
.description('prints the log file path')
.action(async _cmd => {
const { logPath } = await import('./locator.js');
const { logPath } = await import('./config.js');
process.stdout.write(`${logPath}\n`);
});

where
.command('cache')
.description('prints the cache directory path')
.action(async _cmd => {
const { cachePath } = await import('./locator.js');
const { cachePath } = await import('./config.js');
process.stdout.write(`${cachePath}\n`);
});

Expand Down
48 changes: 24 additions & 24 deletions packages/daemon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const info = {
temp,
};

const defaultLocator = {
const defaultConfig = {
statePath: whereEndoState(process.platform, process.env, info),
ephemeralStatePath: whereEndoEphemeralState(
process.platform,
Expand All @@ -56,11 +56,11 @@ const endoDaemonPath = url.fileURLToPath(
new URL('src/daemon-node.js', import.meta.url),
);

export const terminate = async (locator = defaultLocator) => {
export const terminate = async (config = defaultConfig) => {
const { resolve: cancel, promise: cancelled } = makePromiseKit();
const { getBootstrap, closed } = await makeEndoClient(
'harbinger',
locator.sockPath,
config.sockPath,
cancelled,
);
const bootstrap = getBootstrap();
Expand All @@ -72,22 +72,22 @@ export const terminate = async (locator = defaultLocator) => {
await closed.catch(() => {});
};

export const start = async (locator = defaultLocator) => {
await fs.promises.mkdir(locator.statePath, {
export const start = async (config = defaultConfig) => {
await fs.promises.mkdir(config.statePath, {
recursive: true,
});
const logPath = path.join(locator.statePath, 'endo.log');
const logPath = path.join(config.statePath, 'endo.log');
const output = fs.openSync(logPath, 'a');

const env = { ...process.env };

const child = popen.fork(
endoDaemonPath,
[
locator.sockPath,
locator.statePath,
locator.ephemeralStatePath,
locator.cachePath,
config.sockPath,
config.statePath,
config.ephemeralStatePath,
config.cachePath,
],
{
detached: true,
Expand Down Expand Up @@ -143,31 +143,31 @@ const enoentOk = error => {
throw error;
};

export const clean = async (locator = defaultLocator) => {
export const clean = async (config = defaultConfig) => {
if (process.platform !== 'win32') {
await removePath(locator.sockPath).catch(enoentOk);
await removePath(config.sockPath).catch(enoentOk);
}
};

export const stop = async (locator = defaultLocator) => {
await terminate(locator).catch(() => {});
await clean(locator);
export const stop = async (config = defaultConfig) => {
await terminate(config).catch(() => {});
await clean(config);
};

export const restart = async (locator = defaultLocator) => {
await stop(locator);
return start(locator);
export const restart = async (config = defaultConfig) => {
await stop(config);
return start(config);
};

export const purge = async (locator = defaultLocator) => {
await terminate(locator).catch(() => {});
export const purge = async (config = defaultConfig) => {
await terminate(config).catch(() => {});

const cleanedUp = clean(locator);
const removedState = removePath(locator.statePath).catch(enoentOk);
const removedEphemeralState = removePath(locator.ephemeralStatePath).catch(
const cleanedUp = clean(config);
const removedState = removePath(config.statePath).catch(enoentOk);
const removedEphemeralState = removePath(config.ephemeralStatePath).catch(
enoentOk,
);
const removedCache = removePath(locator.cachePath).catch(enoentOk);
const removedCache = removePath(config.cachePath).catch(enoentOk);
await Promise.all([
cleanedUp,
removedState,
Expand Down
35 changes: 20 additions & 15 deletions packages/daemon/src/daemon-node-powers.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,26 +290,24 @@ export const makeCryptoPowers = crypto => {
/**
* @param {import('./types.js').FilePowers} filePowers
* @param {import('./types.js').CryptoPowers} cryptoPowers
* @param {import('./types.js').Locator} locator
* @param {boolean} [includeWebPageBundler]
* @param {import('./types.js').Config} config
* @returns {import('./types.js').DaemonicPersistencePowers}
*/
export const makeDaemonicPersistencePowers = (
filePowers,
cryptoPowers,
locator,
includeWebPageBundler = true,
config,
) => {
const initializePersistence = async () => {
const { statePath, ephemeralStatePath, cachePath } = locator;
const { statePath, ephemeralStatePath, cachePath } = config;
const statePathP = filePowers.makePath(statePath);
const ephemeralStatePathP = filePowers.makePath(ephemeralStatePath);
const cachePathP = filePowers.makePath(cachePath);
await Promise.all([statePathP, cachePathP, ephemeralStatePathP]);
};

const provideRootNonce = async () => {
const noncePath = filePowers.joinPath(locator.statePath, 'nonce');
const noncePath = filePowers.joinPath(config.statePath, 'nonce');
let nonce = await filePowers.maybeReadFileText(noncePath);
const isNewlyCreated = nonce === undefined;
if (nonce === undefined) {
Expand All @@ -320,7 +318,7 @@ export const makeDaemonicPersistencePowers = (
};

const makeContentSha512Store = () => {
const { statePath } = locator;
const { statePath } = config;
const storageDirectoryPath = filePowers.joinPath(statePath, 'store-sha512');

return harden({
Expand Down Expand Up @@ -382,7 +380,7 @@ export const makeDaemonicPersistencePowers = (
* @param {string} formulaNumber
*/
const makeFormulaPath = formulaNumber => {
const { statePath } = locator;
const { statePath } = config;
if (formulaNumber.length < 3) {
throw new TypeError(`Invalid formula number ${q(formulaNumber)}`);
}
Expand Down Expand Up @@ -433,8 +431,15 @@ export const makeDaemonicPersistencePowers = (
});
};

/**
* @param {import('./types.js').Config} config
* @param {import('url').fileURLToPath} fileURLToPath
* @param {import('./types.js').FilePowers} filePowers
* @param {typeof import('fs')} fs
* @param {typeof import('child_process')} popen
*/
export const makeDaemonicControlPowers = (
locator,
config,
fileURLToPath,
filePowers,
fs,
Expand All @@ -450,7 +455,7 @@ export const makeDaemonicControlPowers = (
* @param {Promise<never>} cancelled
*/
const makeWorker = async (workerId, daemonWorkerFacet, cancelled) => {
const { cachePath, statePath, ephemeralStatePath, sockPath } = locator;
const { cachePath, statePath, ephemeralStatePath, sockPath } = config;

const workerCachePath = filePowers.joinPath(cachePath, 'worker', workerId);
const workerStatePath = filePowers.joinPath(statePath, 'worker', workerId);
Expand Down Expand Up @@ -544,7 +549,7 @@ export const makeDaemonicControlPowers = (

/**
* @param {object} opts
* @param {import('./types.js').Locator} opts.locator
* @param {import('./types.js').Config} opts.config
* @param {typeof import('fs')} opts.fs
* @param {typeof import('child_process')} opts.popen
* @param {typeof import('url')} opts.url
Expand All @@ -553,7 +558,7 @@ export const makeDaemonicControlPowers = (
* @returns {import('./types.js').DaemonicPowers}
*/
export const makeDaemonicPowers = ({
locator,
config,
fs,
popen,
url,
Expand All @@ -562,14 +567,14 @@ export const makeDaemonicPowers = ({
}) => {
const { fileURLToPath } = url;

const petStorePowers = makePetStoreMaker(filePowers, locator);
const petStorePowers = makePetStoreMaker(filePowers, config);
const daemonicPersistencePowers = makeDaemonicPersistencePowers(
filePowers,
cryptoPowers,
locator,
config,
);
const daemonicControlPowers = makeDaemonicControlPowers(
locator,
config,
fileURLToPath,
filePowers,
fs,
Expand Down
6 changes: 3 additions & 3 deletions packages/daemon/src/daemon-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ if (process.argv.length < 5) {
const [sockPath, statePath, ephemeralStatePath, cachePath] =
process.argv.slice(2);

/** @type {import('../types.js').Locator} */
const locator = {
/** @type {import('./types.js').Config} */
const config = {
sockPath,
statePath,
ephemeralStatePath,
Expand All @@ -48,7 +48,7 @@ const networkPowers = makeNetworkPowers({ net });
const filePowers = makeFilePowers({ fs, path });
const cryptoPowers = makeCryptoPowers(crypto);
const powers = makeDaemonicPowers({
locator,
config,
fs,
popen,
url,
Expand Down
6 changes: 3 additions & 3 deletions packages/daemon/src/pet-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const { quote: q } = assert;

/**
* @param {import('./types.js').FilePowers} filePowers
* @param {import('./types.js').Locator} locator
* @param {import('./types.js').Config} config
*/
export const makePetStoreMaker = (filePowers, locator) => {
export const makePetStoreMaker = (filePowers, config) => {
/**
* @param {string} petNameDirectoryPath
* @param {(name: string) => void} assertValidName
Expand Down Expand Up @@ -211,7 +211,7 @@ export const makePetStoreMaker = (filePowers, locator) => {
const prefix = formulaNumber.slice(0, 2);
const suffix = formulaNumber.slice(2);
const petNameDirectoryPath = filePowers.joinPath(
locator.statePath,
config.statePath,
formulaType,
prefix,
suffix,
Expand Down
2 changes: 1 addition & 1 deletion packages/daemon/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type SomehowAsyncIterable<T> =
| Iterable<T>
| { next: () => IteratorResult<T> };

export type Locator = {
export type Config = {
statePath: string;
ephemeralStatePath: string;
cachePath: string;
Expand Down
6 changes: 3 additions & 3 deletions packages/daemon/src/worker-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ if (process.argv.length < 7) {
const [workerUuid, sockPath, statePath, ephemeralStatePath, cachePath] =
process.argv.slice(2);

/** @type {import('./types.js').Locator} */
const locator = {
/** @type {import('./types.js').Config} */
const config = {
sockPath,
statePath,
ephemeralStatePath,
Expand All @@ -44,7 +44,7 @@ process.once('SIGINT', () => cancel(new Error('SIGINT')));

// @ts-ignore Yes, we can assign to exitCode, typedoc.
process.exitCode = 1;
main(powers, locator, workerUuid, process.pid, cancel, cancelled).then(
main(powers, config, workerUuid, process.pid, cancel, cancelled).then(
() => {
process.exitCode = 0;
},
Expand Down
6 changes: 3 additions & 3 deletions packages/daemon/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ export const makeWorkerFacet = ({ cancel }) => {

/**
* @param {import('./types.js').MignonicPowers} powers
* @param {import('./types.js').Locator} locator
* @param {string} uuid
* @param {import('./types.js').Config} _config
* @param {string} _uuid
* @param {number | undefined} pid
* @param {(error: Error) => void} cancel
* @param {Promise<never>} cancelled
*/
export const main = async (powers, locator, uuid, pid, cancel, cancelled) => {
export const main = async (powers, _config, _uuid, pid, cancel, cancelled) => {
console.error(`Endo worker started on pid ${pid}`);
cancelled.catch(() => {
console.error(`Endo worker exiting on pid ${pid}`);
Expand Down
Loading

0 comments on commit b71d7d1

Please sign in to comment.