Skip to content

Commit

Permalink
fix(daemon): Strengthen type checks (merge #1832)
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal authored Oct 17, 2023
2 parents 302679f + 2f8aa51 commit 58e8d06
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 13 deletions.
11 changes: 11 additions & 0 deletions packages/daemon/src/daemon-node-powers.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ export const makeNetworkPowers = ({ http, ws, net }) => {
return readFrom;
};

/**
* @param {object} args
* @param {number} args.port
* @param {string} [args.host]
* @param {Promise<never>} args.cancelled
*/
const servePort = async ({ port, host = '0.0.0.0', cancelled }) =>
serveListener(
server =>
Expand All @@ -232,6 +238,11 @@ export const makeNetworkPowers = ({ http, ws, net }) => {
cancelled,
);

/**
* @param {object} args
* @param {string} args.path
* @param {Promise<never>} args.cancelled
*/
const servePath = async ({ path, cancelled }) =>
serveListener(
server =>
Expand Down
4 changes: 3 additions & 1 deletion packages/daemon/src/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ export const makeMailboxMaker = ({
}
return undefined;
}
throw Error(`panic: Unknown message type ${type}`);
throw new Error(
`panic: Unknown message type ${/** @type {any} */ (message).type}`,
);
};

const listMessages = async () =>
Expand Down
2 changes: 2 additions & 0 deletions packages/daemon/src/pet-name.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

const { quote: q } = assert;

const validNamePattern = /^[a-z][a-z0-9-]{0,127}$/;
Expand Down
4 changes: 3 additions & 1 deletion packages/daemon/src/pubsub.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

import { makePromiseKit } from '@endo/promise-kit';
import { makeStream } from '@endo/stream';

Expand All @@ -9,7 +11,7 @@ const freeze = /** @type {<T>(v: T | Readonly<T>) => T} */ (Object.freeze);
/**
* @template TValue TValue
* @param {TValue} value
* @returns {import('./types.js').AsyncQueue<TValue, unknown>}
* @returns {import('@endo/stream').AsyncQueue<TValue, unknown>}
*/
export const makeNullQueue = value =>
harden({
Expand Down
2 changes: 2 additions & 0 deletions packages/daemon/src/serve-private-path.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

import { makeNetstringCapTP } from './connection.js';

const { quote: q } = assert;
Expand Down
2 changes: 2 additions & 0 deletions packages/daemon/src/serve-private-port-http.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

import { E } from '@endo/far';
import { mapReader, mapWriter } from '@endo/stream';
import {
Expand Down
9 changes: 5 additions & 4 deletions packages/daemon/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ export interface Topic<
}

export interface PetStore {
lookup(petName: string): string | undefined;
reverseLookup(formulaIdentifier: string): Array<string>;
list(): Array<string>;
follow(): Promise<FarRef<Stream<unknown>>>;
write(petName: string, formulaIdentifier: string): Promise<void>;
remove(petName: string);
rename(fromPetName: string, toPetName: string);
lookup(petName: string): string | undefined;
reverseLookup(formulaIdentifier: string): Array<string>;
follow(): Promise<FarRef<String<{ add: string } | { remove: string }>>>;
}

export type RequestFn = (
Expand Down Expand Up @@ -184,9 +184,10 @@ export interface EndoGuest {
export interface EndoHost {
listMessages(): Promise<Array<Message>>;
followMessages(): ERef<AsyncIterable<Message>>;
lookup(petName: string): Promise<unknown>;
resolve(requestNumber: number, petName: string);
reject(requestNumber: number, message: string);
lookup(ref: object): Promise<Array<string>>;
reverseLookup(ref: object): Promise<Array<string>>;
remove(petName: string);
rename(fromPetName: string, toPetName: string);
list(): Array<string>; // pet names
Expand Down
2 changes: 2 additions & 0 deletions packages/daemon/src/web-page-bundler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

// This is a built-in unsafe plugin for lazily constructing the web-page.js
// bundle for booting up web caplets.
// The hard-coded 'web-page-js' formula is a hard-coded 'import-unsafe' formula
Expand Down
3 changes: 2 additions & 1 deletion packages/daemon/src/web-page.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
/* global window, document */

import '@endo/init/debug.js';
Expand All @@ -21,7 +22,7 @@ const endowments = Object.freeze({
console,
});

const url = new URL('/', window.location);
const url = new URL('/', `${window.location}`);
url.protocol = 'ws';

const bootstrap = Far('WebFacet', {
Expand Down
3 changes: 2 additions & 1 deletion packages/daemon/src/worker-node.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
/* global process */

// Establish a perimeter:
Expand All @@ -24,7 +25,7 @@ if (process.argv.length < 7) {
const [workerUuid, sockPath, statePath, ephemeralStatePath, cachePath] =
process.argv.slice(2);

/** @type {import('../index.js').Locator} */
/** @type {import('./types.js').Locator} */
const locator = {
sockPath,
statePath,
Expand Down
4 changes: 2 additions & 2 deletions packages/stream/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ harden(makeQueue);
* @template TWrite
* @template TReadReturn
* @template TWriteReturn
* @param {import('./types.js').AsyncQueue<IteratorResult<TRead, TReadReturn>>} acks
* @param {import('./types.js').AsyncQueue<IteratorResult<TWrite, TWriteReturn>>} data
* @param {import('./types.js').AsyncSpring<IteratorResult<TRead, TReadReturn>>} acks
* @param {import('./types.js').AsyncSink<IteratorResult<TWrite, TWriteReturn>>} data
*/
export const makeStream = (acks, data) => {
const stream = harden({
Expand Down
13 changes: 10 additions & 3 deletions packages/stream/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
export interface AsyncQueue<TValue> {
export interface AsyncSink<TValue> {
put(value: TValue | Promise<TValue>): void;
}

export interface AsyncSpring<TValue> {
get(): Promise<TValue>;
}

export interface AsyncQueue<TSpringValue, TSinkValue = TSpringValue>
extends AsyncSpring<TSpringValue>,
AsyncSink<TSinkValue> {}

// Stream is nearly identical to AsyncGenerator and AsyncGenerator should
// probably be identical to this definition of Stream.
// Stream does not make the mistake of conflating the read and write return
Expand Down Expand Up @@ -40,8 +47,8 @@ export declare function makeStream<
TReadReturn = undefined,
TWriteReturn = undefined,
>(
acks: AsyncQueue<IteratorResult<TRead, TReadReturn>>,
data: AsyncQueue<IteratorResult<TWrite, TWriteReturn>>,
acks: AsyncSpring<IteratorResult<TRead, TReadReturn>>,
data: AsyncSink<IteratorResult<TWrite, TWriteReturn>>,
): Stream<TRead, TWrite, TReadReturn, TWriteReturn>;

export declare function makePipe<
Expand Down

0 comments on commit 58e8d06

Please sign in to comment.