Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/kernel/src/command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Primitive } from '@endo/captp';
import { hasProperty, isObject } from '@metamask/utils';
import { isPrimitive, isTypedArray, isTypedObject } from '@ocap/utils';

export enum CommandMethod {
CapTpCall = 'callCapTp',
Expand All @@ -16,6 +17,12 @@ export type CommandParams =
| CommandParams[]
| { [key: string]: CommandParams };

const isCommandParams = (value: unknown): value is CommandParams =>
isPrimitive(value) ||
value instanceof Promise ||
isTypedArray(value, isCommandParams) ||
isTypedObject(value, isCommandParams);

export type CapTpPayload = {
method: string;
params: CommandParams[];
Expand Down Expand Up @@ -48,7 +55,8 @@ const isCommandLike = (
} =>
isObject(value) &&
Object.values(CommandMethod).includes(value.method as CommandMethod) &&
hasProperty(value, 'params');
hasProperty(value, 'params') &&
isCommandParams(value.params);

export type Command =
| CommandLike<CommandMethod.Ping, null>
Expand Down
4 changes: 4 additions & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
"test:verbose": "yarn test --reporter verbose",
"test:watch": "vitest --config vitest.config.ts"
},
"dependencies": {
"@endo/captp": "^4.2.2",
"@metamask/utils": "^9.1.0"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.15.3",
"@metamask/auto-changelog": "^3.4.4",
Expand Down
9 changes: 8 additions & 1 deletion packages/utils/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import * as indexModule from './index.js';
describe('index', () => {
it('has the expected exports', () => {
expect(Object.keys(indexModule)).toStrictEqual(
expect.arrayContaining(['makeCounter', 'makeLogger', 'stringify']),
expect.arrayContaining([
'makeCounter',
'makeLogger',
'stringify',
'isPrimitive',
'isTypedArray',
'isTypedObject',
]),
);
});
});
2 changes: 2 additions & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export type { Logger } from './logger.js';
export { makeLogger } from './logger.js';
export { makeCounter } from './counter.js';
export { stringify } from './stringify.js';
export type { TypeGuard } from './types.js';
export { isPrimitive, isTypedArray, isTypedObject } from './types.js';
87 changes: 87 additions & 0 deletions packages/utils/src/types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { isObject } from '@metamask/utils';
import { describe, it, expect } from 'vitest';

import { isPrimitive, isTypedArray, isTypedObject } from './types.js';

const isNumber = (value: unknown): value is number => typeof value === 'number';
const alwaysFalse = (): boolean => false;
const alwaysTrue = (): boolean => true;

describe('isPrimitive', () => {
it.each`
value
${''}
${'foo'}
${0}
${6.28}
${BigInt('9999999999999999')}
${Symbol('meaning')}
${false}
${null}
${undefined}
`('returns true for primitive $value', ({ value }) => {
expect(isPrimitive(value)).toBe(true);
});

it.each`
value
${[]}
${{}}
${{ foo: 'bar' }}
${new MessageChannel()}
${alwaysTrue}
${function foo() {
return 'bar';
}}
`('returns false for invalid values: $value', ({ value }) => {
expect(isPrimitive(value)).toBe(false);
});
});

describe('isTypedArray', () => {
it.each`
value | guard
${[]} | ${alwaysFalse}
${[0, 2, 4.5]} | ${isNumber}
${[0, 'foo']} | ${isPrimitive}
${[{}, { foo: 'bar' }]} | ${isObject}
${[[]]} | ${Array.isArray}
`('returns true for homogeneously typed array $value', ({ value, guard }) => {
expect(isTypedArray(value, guard)).toBe(true);
});

it.each`
value | guard
${[null]} | ${alwaysFalse}
${0} | ${isNumber}
${null} | ${alwaysTrue}
${[0, 'foo']} | ${isNumber}
${[0, [1]]} | ${isNumber}
${[{}, 1]} | ${isObject}
`('returns false for invalid values: $value', ({ value, guard }) => {
expect(isTypedArray(value, guard)).toBe(false);
});
});

describe('isTypedObject', () => {
it.each`
value | guard
${{}} | ${alwaysFalse}
${{ foo: 0, bar: 2 }} | ${isNumber}
${{ foo: {}, bar: { foo: 0 } }} | ${isObject}
`(
'returns true for homogeneously typed object $value',
({ value, guard }) => {
expect(isTypedObject(value, guard)).toBe(true);
},
);

it.each`
value | guard
${{ foo: 'bar' }} | ${alwaysFalse}
${null} | ${alwaysTrue}
${[{}, { foo: 'bar ' }]} | ${isObject}
`('returns false for invalid values: $value', ({ value, guard }) => {
expect(isTypedObject(value, guard)).toBe(false);
});
});
28 changes: 28 additions & 0 deletions packages/utils/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { Primitive } from '@endo/captp';
import { isObject } from '@metamask/utils';

export type TypeGuard<Type> = (value: unknown) => value is Type;

const primitives = [
'string',
'number',
'bigint',
'boolean',
'symbol',
'null',
'undefined',
];
export const isPrimitive = (value: unknown): value is Primitive =>
value === null || primitives.includes(typeof value);

export const isTypedArray = <ElementType>(
value: unknown,
isElement: TypeGuard<ElementType>,
): value is ElementType[] =>
Array.isArray(value) && !value.some((ele) => !isElement(ele));

export const isTypedObject = <ValueType>(
value: unknown,
isValue: TypeGuard<ValueType>,
): value is { [Key in keyof object]: ValueType } =>
isObject(value) && !Object.values(value).some((val) => !isValue(val));
2 changes: 2 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1607,10 +1607,12 @@ __metadata:
resolution: "@ocap/utils@workspace:packages/utils"
dependencies:
"@arethetypeswrong/cli": "npm:^0.15.3"
"@endo/captp": "npm:^4.2.2"
"@metamask/auto-changelog": "npm:^3.4.4"
"@metamask/eslint-config": "npm:^13.0.0"
"@metamask/eslint-config-nodejs": "npm:^13.0.0"
"@metamask/eslint-config-typescript": "npm:^13.0.0"
"@metamask/utils": "npm:^9.1.0"
"@ts-bridge/cli": "npm:^0.5.1"
"@ts-bridge/shims": "npm:^0.1.1"
"@typescript-eslint/eslint-plugin": "npm:^8.1.0"
Expand Down