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
13 changes: 12 additions & 1 deletion packages/extension/src/kernel-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ async function main(): Promise<void> {
postMessage({ method, params });
};

/**
* Cast an unknown problem to an Error object.
*
* @param problem - Whatever was caught.
* @returns The problem if it is an Error, or a new Error with the problem as the cause.
*/
const asError = (problem: unknown): Error =>
problem instanceof Error
? problem
: new Error('Unknown', { cause: problem });

// Handle messages from the console service worker
globalThis.onmessage = async (event: MessageEvent<unknown>) => {
if (!isCommand(event.data)) {
Expand Down Expand Up @@ -213,7 +224,7 @@ async function main(): Promise<void> {
const result = kvGet(params);
reply(CommandMethod.KVGet, result);
} catch (problem) {
reply(CommandMethod.KVGet, problem as string); // cast is a lie, it really is an Error
reply(CommandMethod.KVGet, asError(problem));
}
break;
}
Expand Down
27 changes: 19 additions & 8 deletions packages/kernel/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ type CommandLike<Method extends CommandMethod, Data extends CommandParams> = {
params: Data;
};

type CommandReplyLike<
Method extends CommandMethod,
Data extends CommandParams,
ErrorType extends Error = never,
> = {
method: Method;
params: Data | ErrorType;
};

const isCommandLike = (
value: unknown,
): value is {
Expand Down Expand Up @@ -67,22 +76,24 @@ export type CommandFunction<Return = void | Promise<void>> = {
};

export type CommandReply =
| CommandLike<CommandMethod.Ping, 'pong'>
| CommandLike<CommandMethod.Evaluate, string>
| CommandLike<CommandMethod.CapTpInit, string>
| CommandLike<CommandMethod.CapTpCall, string>
| CommandLike<CommandMethod.KVGet, string>
| CommandLike<CommandMethod.KVSet, string>;
| CommandReplyLike<CommandMethod.Ping, 'pong'>
| CommandReplyLike<CommandMethod.Evaluate, string>
| CommandReplyLike<CommandMethod.CapTpInit, string>
| CommandReplyLike<CommandMethod.CapTpCall, string>
| CommandReplyLike<CommandMethod.KVGet, string, Error>
| CommandReplyLike<CommandMethod.KVSet, string>;

export const isCommandReply = (value: unknown): value is CommandReply =>
isCommandLike(value) && typeof value.params === 'string';
isCommandLike(value) &&
(typeof value.params === 'string' || value.params instanceof Error);

type UnionMinus<Union, Minus> = Union extends Minus ? never : Union;

export type CommandReplyFunction<Return = void> = {
(method: CommandMethod.Ping, params: 'pong'): Return;
(method: CommandMethod.KVGet, params: string | Error): Return;
(
method: UnionMinus<CommandMethod, CommandMethod.Ping>,
method: UnionMinus<CommandMethod, CommandMethod.Ping | CommandMethod.KVGet>,
params: string,
): Return;
};
Expand Down