@@ -29,15 +29,17 @@ export default abstract class Server {
2929 request : string | Request ,
3030 stringify : boolean = true
3131 ) : Promise < string | Response > {
32- const response = new Response ( - 1 )
32+ let id = - 1
33+ let result
34+ let error
3335
3436 // Extract a parameter by name from Object or by index from Array
3537 function param (
3638 request : Request ,
3739 index : number ,
3840 name : string ,
3941 required : boolean = true
40- ) : unknown {
42+ ) : any {
4143 if ( request . params === undefined )
4244 throw new Error ( - 32600 , 'Invalid request: missing "params" property' )
4345 const value = Array . isArray ( request . params )
@@ -59,12 +61,11 @@ export default abstract class Server {
5961 }
6062
6163 // Response id is same as the request id
62- response . id = request . id
64+ id = request . id
6365
6466 if ( request . method === undefined )
6567 throw new Error ( - 32600 , 'Invalid request: missing "method" property' )
6668
67- let result
6869 switch ( request . method ) {
6970 case 'capabilities' :
7071 result = await this . executor . capabilities ( )
@@ -93,16 +94,16 @@ export default abstract class Server {
9394 default :
9495 throw new Error ( - 32601 , `Method not found: "${ request . method } "` )
9596 }
96- response . result = result
9797 } catch ( exc ) {
98- response . error =
98+ error =
9999 exc instanceof Error
100100 ? exc
101101 : new Error ( - 32603 , `Internal error: ${ exc . message } ` , {
102102 trace : exc . stack
103103 } )
104104 }
105105
106+ const response = new Response ( id , result , error )
106107 return stringify ? JSON . stringify ( response ) : response
107108 }
108109
0 commit comments