Skip to content

Commit

Permalink
fix(cli): Fix result name parsing in request command
Browse files Browse the repository at this point in the history
  • Loading branch information
rekmarks committed Jul 17, 2024
1 parent 26b188f commit 67a22bd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/cli/src/commands/eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os from 'os';
import { E } from '@endo/far';
import { withEndoAgent } from '../context.js';
import { parsePetNamePath } from '../pet-name.js';
import { parsePetNamePath, parseOptionalPetNamePath } from '../pet-name.js';

export const evalCommand = async ({
source,
Expand Down Expand Up @@ -35,7 +35,7 @@ export const evalCommand = async ({
source,
codeNames,
petNames,
resultName === undefined ? undefined : parsePetNamePath(resultName),
parseOptionalPetNamePath(resultName),
);
console.log(result);
});
5 changes: 2 additions & 3 deletions packages/cli/src/commands/make.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import bundleSource from '@endo/bundle-source';
import { makeReaderRef } from '@endo/daemon';
import { E } from '@endo/far';
import { withEndoAgent } from '../context.js';
import { parsePetNamePath } from '../pet-name.js';
import { parseOptionalPetNamePath } from '../pet-name.js';
import { randomHex16 } from '../random.js';

const textEncoder = new TextEncoder();
Expand Down Expand Up @@ -40,8 +40,7 @@ export const makeCommand = async ({
return;
}

assert(resultName === undefined || typeof resultName === 'string');
const resultPath = resultName && parsePetNamePath(resultName);
const resultPath = parseOptionalPetNamePath(resultName);

/** @type {import('@endo/eventual-send').FarRef<import('@endo/stream').Reader<string>> | undefined} */
let bundleReaderRef;
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os from 'os';
import { E } from '@endo/far';
import { withEndoAgent } from '../context.js';
import { parsePetNamePath } from '../pet-name.js';
import { parseOptionalPetNamePath } from '../pet-name.js';

export const request = async ({
description,
Expand All @@ -14,7 +14,7 @@ export const request = async ({
const result = await E(agent).request(
toName,
description,
parsePetNamePath(resultName),
parseOptionalPetNamePath(resultName),
);
console.log(result);
});
Expand Down
23 changes: 22 additions & 1 deletion packages/cli/src/pet-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { q } from '@endo/errors';

/**
* Splits a dot-delimited pet name path into an array of pet names.
* Throws if any of the path segments are empty.
* Throws if the path is not a string or if any of the path segments are empty.
*
* @param {string} petNamePath - A dot-delimited pet name path.
* @returns {string[]} - The pet name path, as an array of pet names.
*/
export const parsePetNamePath = petNamePath => {
assert(typeof petNamePath === 'string');

const petNames = petNamePath.split('.');
for (const petName of petNames) {
if (petName === '') {
Expand All @@ -18,3 +20,22 @@ export const parsePetNamePath = petNamePath => {
}
return petNames;
};

/**
* Like {@link parsePetNamePath}, but immediately returns `undefined` values.
*
* @param {string | undefined} optionalPetNamePath - A dot-delimited pet name path,
* or `undefined`.
* @returns {string[] | undefined} - The pet name path as an array of pet names, or
* `undefined`.
*/
export const parseOptionalPetNamePath = optionalPetNamePath => {
assert(
optionalPetNamePath === undefined ||
typeof optionalPetNamePath === 'string',
);

return optionalPetNamePath === undefined
? undefined
: parsePetNamePath(optionalPetNamePath);
};

0 comments on commit 67a22bd

Please sign in to comment.