11import type { CapData } from '@endo/marshal' ;
22import { serializeError } from '@metamask/rpc-errors' ;
3- import type { JsonRpcRequest , JsonRpcResponse } from '@metamask/utils' ;
3+ import { hasProperty } from '@metamask/utils' ;
4+ import type { JsonRpcResponse } from '@metamask/utils' ;
45import {
56 StreamReadError ,
67 VatAlreadyExistsError ,
@@ -11,6 +12,7 @@ import { RpcService } from '@ocap/rpc-methods';
1112import type { ExtractParams , ExtractResult } from '@ocap/rpc-methods' ;
1213import type { KernelDatabase } from '@ocap/store' ;
1314import type { DuplexStream } from '@ocap/streams' ;
15+ import type { JsonRpcCall } from '@ocap/utils' ;
1416
1517import { KernelQueue } from './KernelQueue.ts' ;
1618import { KernelRouter } from './KernelRouter.ts' ;
@@ -33,7 +35,7 @@ import { VatHandle } from './VatHandle.ts';
3335
3436export class Kernel {
3537 /** Command channel from the controlling console/browser extension/test driver */
36- readonly #commandStream: DuplexStream < JsonRpcRequest , JsonRpcResponse > ;
38+ readonly #commandStream: DuplexStream < JsonRpcCall , JsonRpcResponse > ;
3739
3840 readonly #rpcService: RpcService < typeof kernelHandlers > ;
3941
@@ -70,7 +72,7 @@ export class Kernel {
7072 */
7173 // eslint-disable-next-line no-restricted-syntax
7274 private constructor (
73- commandStream : DuplexStream < JsonRpcRequest , JsonRpcResponse > ,
75+ commandStream : DuplexStream < JsonRpcCall , JsonRpcResponse > ,
7476 vatWorkerService : VatWorkerManager ,
7577 kernelDatabase : KernelDatabase ,
7678 options : {
@@ -108,7 +110,7 @@ export class Kernel {
108110 * @returns A promise for the new kernel instance.
109111 */
110112 static async make (
111- commandStream : DuplexStream < JsonRpcRequest , JsonRpcResponse > ,
113+ commandStream : DuplexStream < JsonRpcCall , JsonRpcResponse > ,
112114 vatWorkerService : VatWorkerManager ,
113115 kernelDatabase : KernelDatabase ,
114116 options : {
@@ -155,25 +157,29 @@ export class Kernel {
155157 *
156158 * @param message - The message to handle.
157159 */
158- async #handleCommandMessage( message : JsonRpcRequest ) : Promise < void > {
160+ async #handleCommandMessage( message : JsonRpcCall ) : Promise < void > {
159161 try {
160162 this . #rpcService. assertHasMethod ( message . method ) ;
161163 const result = await this . #rpcService. execute (
162164 message . method ,
163165 message . params ,
164166 ) ;
165- await this . #commandStream. write ( {
166- id : message . id ,
167- jsonrpc : '2.0' ,
168- result,
169- } ) ;
167+ if ( hasProperty ( message , 'id' ) && typeof message . id === 'string' ) {
168+ await this . #commandStream. write ( {
169+ id : message . id ,
170+ jsonrpc : '2.0' ,
171+ result,
172+ } ) ;
173+ }
170174 } catch ( error ) {
171175 this . #logger. error ( 'Error executing command' , error ) ;
172- await this . #commandStream. write ( {
173- id : message . id ,
174- jsonrpc : '2.0' ,
175- error : serializeError ( error ) ,
176- } ) ;
176+ if ( hasProperty ( message , 'id' ) && typeof message . id === 'string' ) {
177+ await this . #commandStream. write ( {
178+ id : message . id ,
179+ jsonrpc : '2.0' ,
180+ error : serializeError ( error ) ,
181+ } ) ;
182+ }
177183 }
178184 }
179185
0 commit comments