Skip to content

Commit

Permalink
fix types + handle undefined id
Browse files Browse the repository at this point in the history
  • Loading branch information
shanejonas committed May 17, 2021
1 parent be7a364 commit 3d554c7
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
6 changes: 0 additions & 6 deletions packages/controllers/src/idCounter.ts

This file was deleted.

7 changes: 3 additions & 4 deletions packages/controllers/src/plugins/PluginController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import EventEmitter from '@metamask/safe-event-emitter';
import { ethErrors, serializeError } from 'eth-rpc-errors';
import { IOcapLdCapability } from 'rpc-cap/dist/src/@types/ocap-ld';
import { IRequestedPermissions } from 'rpc-cap/dist/src/@types';
import { JsonRpcResponse } from 'json-rpc-engine';
import { nanoid } from 'nanoid';
import {
WorkerController,
SetupWorkerConnection,
} from '../workers/WorkerController';
import { getId } from '../idCounter';
import { INLINE_PLUGINS } from './inlinePlugins';

export const PLUGIN_PREFIX = 'wallet_plugin_';
Expand Down Expand Up @@ -40,7 +39,7 @@ export interface Plugin extends SerializablePlugin {
export type PluginRpcHook = (
origin: string,
request: Record<string, unknown>,
) => Promise<JsonRpcResponse<unknown>>;
) => Promise<unknown>;

export type ProcessPluginReturnType =
| SerializablePlugin
Expand Down Expand Up @@ -744,7 +743,7 @@ export class PluginController extends EventEmitter {
request: Record<string, unknown>,
) => {
return await this.workerController.command(workerId, {
id: getId(),
id: nanoid(),
jsonrpc: '2.0',
method: 'pluginRpc',
params: {
Expand Down
19 changes: 11 additions & 8 deletions packages/controllers/src/workers/WorkerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import { PluginData } from '@mm-snap/types';
import {
JsonRpcEngine,
JsonRpcRequest,
JsonRpcResponse,
PendingJsonRpcResponse,
} from 'json-rpc-engine';

import { getId } from '../idCounter';

export type SetupWorkerConnection = (metadata: any, stream: Duplex) => void;

interface PluginWorkerMetadata {
Expand Down Expand Up @@ -82,7 +80,10 @@ export class WorkerController extends SafeEventEmitter {
this.store.updateState({ workers: newWorkerState });
}

async command(workerId: string, message: JsonRpcRequest<any>): Promise<unknown> {
async command(
workerId: string,
message: JsonRpcRequest<unknown>,
): Promise<unknown> {
if (typeof message !== 'object') {
throw new Error('Must send object.');
}
Expand All @@ -93,7 +94,9 @@ export class WorkerController extends SafeEventEmitter {
}

console.log('Parent: Sending Command', message);
const response: any = await workerObj.rpcEngine.handle(message);
const response: PendingJsonRpcResponse<unknown> = await workerObj.rpcEngine.handle(
message,
);
if (response.error) {
throw new Error(response.error.message);
}
Expand Down Expand Up @@ -134,7 +137,7 @@ export class WorkerController extends SafeEventEmitter {
async startPlugin(
workerId: string,
pluginData: PluginData,
): Promise<JsonRpcResponse<any>> {
): Promise<unknown> {
const _workerId: string = workerId || this.workers.keys().next()?.value();
if (!_workerId) {
throw new Error('No workers available.');
Expand All @@ -146,7 +149,7 @@ export class WorkerController extends SafeEventEmitter {
jsonrpc: '2.0',
method: 'installPlugin',
params: pluginData,
id: getId(),
id: nanoid(),
});
}

Expand Down Expand Up @@ -211,7 +214,7 @@ export class WorkerController extends SafeEventEmitter {
await this.command(workerId, {
jsonrpc: '2.0',
method: 'ping',
id: getId(),
id: nanoid(),
});
return workerId;
}
Expand Down
1 change: 1 addition & 0 deletions packages/workers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@types/pump": "^1.1.0",
"@types/readable-stream": "^2.3.9",
"browserify": "16.2.3",
"json-rpc-engine": "^6.1.0",
"pump": "^3.0.0",
"ses": "^0.11.0",
"tinyify": "^3.0.0",
Expand Down
11 changes: 6 additions & 5 deletions packages/workers/src/PluginWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ObjectMultiplex from '@metamask/object-multiplex';
import pump from 'pump';
import { WorkerPostMessageStream } from '@metamask/post-message-stream';
import { PluginData, PluginProvider } from '@mm-snap/types';
import type { JsonRpcId, JsonRpcRequest } from 'json-rpc-engine';
import { STREAM_NAMES } from './enums';

// eslint-disable-next-line import/no-unassigned-import
Expand Down Expand Up @@ -75,15 +76,15 @@ lockdown({
this.rpcStream = mux.createStream(STREAM_NAMES.JSON_RPC) as any;
}

private async onCommandRequest(message: any) {
private async onCommandRequest(message: JsonRpcRequest<unknown>) {
if (!message || typeof message !== 'object' || Array.isArray(message)) {
console.error('Command stream received non-object message.');
return;
}

const { id, method, params } = message;

if (!id && typeof id !== 'string' && typeof id !== 'number') {
if (typeof id !== 'string' && typeof id !== 'number') {
console.error(`Command stream received invalid id "${id}".`);
return;
}
Expand Down Expand Up @@ -112,7 +113,7 @@ lockdown({
}
}

private respond(id: string, responseObj: Record<string, unknown>) {
private respond(id: JsonRpcId, responseObj: Record<string, unknown>) {
this.commandStream.write({
...responseObj,
id,
Expand All @@ -121,7 +122,7 @@ lockdown({
}

private async handlePluginRpc(
id: string,
id: JsonRpcId,
{ origin, request, target }: PluginRpcRequest,
) {
const handler = this.pluginRpcHandlers.get(target);
Expand All @@ -142,7 +143,7 @@ lockdown({
}

private installPlugin(
id: string,
id: JsonRpcId,
{ pluginName, sourceCode }: Partial<PluginData> = {},
) {
if (!isTruthyString(pluginName) || !isTruthyString(sourceCode)) {
Expand Down

0 comments on commit 3d554c7

Please sign in to comment.