1- class BadServerDataError extends Error {
2- constructor ( id , message , errorCode , data ) {
3- super ( message ) ;
4- this . id = id ;
5- this . name = this . constructor . name ;
6- this . code = errorCode ;
7- this . data = data ;
8- }
9- }
10- function send ( resource , fetchInit ) {
11- return fetch ( resource instanceof URL ? resource . href : resource , fetchInit ) . then ( ( res ) => {
12- if ( ! res . ok ) {
13- return Promise . reject ( new BadServerDataError ( null , `${ res . status } '${ res . statusText } ' received instead of 200-299 range.` , - 32002 ) ) ;
14- } else if ( res . status === 204 || res . headers . get ( "content-length" ) === "0" ) {
15- return undefined ;
16- } else return res . json ( ) ;
17- } ) . catch ( ( err ) => Promise . reject ( new BadServerDataError ( null , err . message , - 32001 ) )
18- ) ;
19- }
20- function validateRpcBasis ( data1 ) {
21- return data1 ?. jsonrpc === "2.0" && ( typeof data1 . id === "number" || typeof data1 . id === "string" || data1 . id === null ) ;
22- }
23- function validateRpcSuccess ( data1 ) {
24- return "result" in data1 ;
25- }
26- function validateRpcFailure ( data1 ) {
27- return typeof data1 ?. error ?. code === "number" && typeof data1 . error . message === "string" ;
28- }
29- function validateResponse ( data1 ) {
30- if ( validateRpcBasis ( data1 ) ) {
31- if ( validateRpcSuccess ( data1 ) ) return data1 ;
32- else if ( validateRpcFailure ( data1 ) ) {
33- throw new BadServerDataError ( data1 . id , data1 . error . message , data1 . error . code , data1 . error . data ) ;
34- }
35- }
36- throw new BadServerDataError ( null , "Received data is no RPC response object." , - 32003 ) ;
37- }
38- const validateResponse1 = validateResponse ;
39- function processBatchArray ( rpcResponseBatch ) {
40- return rpcResponseBatch . map ( ( rpcResponse ) => validateResponse ( rpcResponse ) . result
41- ) ;
42- }
43- function processBatchObject ( rpcResponseBatch ) {
44- return rpcResponseBatch . reduce ( ( acc , rpcResponse ) => {
45- acc [ rpcResponse . id ] = validateResponse1 ( rpcResponse ) . result ;
46- return acc ;
47- } , {
48- } ) ;
49- }
501function bytesToUuid ( bytes ) {
512 const bits = [
523 ...bytes
@@ -66,23 +17,20 @@ function bytesToUuid(bytes) {
6617 ...bits . slice ( 10 , 16 ) ,
6718 ] . join ( "" ) ;
6819}
69- const UUID_RE = / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - 4 [ 0 - 9 a - f ] { 3 } - [ 8 9 a b ] [ 0 - 9 a - f ] { 3 } - [ 0 - 9 a - f ] { 12 } $ / i;
7020function generate ( ) {
7121 const rnds = crypto . getRandomValues ( new Uint8Array ( 16 ) ) ;
7222 rnds [ 6 ] = rnds [ 6 ] & 15 | 64 ;
7323 rnds [ 8 ] = rnds [ 8 ] & 63 | 128 ;
7424 return bytesToUuid ( rnds ) ;
7525}
76- const generate1 = generate ;
77- const generateV4Uuid = generate1 ;
78- function createRequest ( { method , params , isNotification = false , id : id1 } ) {
26+ function createRequest ( { method , params , isNotification = false , id } ) {
7927 const rpcRequest = {
8028 jsonrpc : "2.0" ,
8129 method
8230 } ;
8331 params && ( rpcRequest . params = params ) ;
84- id1 = isNotification ? undefined : id1 !== undefined ? id1 : generateV4Uuid ( ) ;
85- id1 !== undefined && ( rpcRequest . id = id1 ) ;
32+ id = isNotification ? undefined : id !== undefined ? id : generate ( ) ;
33+ id !== undefined && ( rpcRequest . id = id ) ;
8634 return rpcRequest ;
8735}
8836function createRequestBatch ( batchObj , isNotification = false ) {
@@ -99,6 +47,54 @@ function createRequestBatch(batchObj, isNotification = false) {
9947 } )
10048 ) ;
10149}
50+ class BadServerDataError extends Error {
51+ constructor ( id , message , errorCode , data ) {
52+ super ( message ) ;
53+ this . id = id ;
54+ this . name = this . constructor . name ;
55+ this . code = errorCode ;
56+ this . data = data ;
57+ }
58+ }
59+ function validateRpcBasis ( data1 ) {
60+ return data1 ?. jsonrpc === "2.0" && ( typeof data1 . id === "number" || typeof data1 . id === "string" || data1 . id === null ) ;
61+ }
62+ function validateRpcSuccess ( data1 ) {
63+ return "result" in data1 ;
64+ }
65+ function validateRpcFailure ( data1 ) {
66+ return typeof data1 ?. error ?. code === "number" && typeof data1 . error . message === "string" ;
67+ }
68+ function validateResponse ( data1 ) {
69+ if ( validateRpcBasis ( data1 ) ) {
70+ if ( validateRpcSuccess ( data1 ) ) return data1 ;
71+ else if ( validateRpcFailure ( data1 ) ) {
72+ throw new BadServerDataError ( data1 . id , data1 . error . message , data1 . error . code , data1 . error . data ) ;
73+ }
74+ }
75+ throw new BadServerDataError ( null , "Received data is no RPC response object." , - 32003 ) ;
76+ }
77+ function send ( resource , fetchInit ) {
78+ return fetch ( resource instanceof URL ? resource . href : resource , fetchInit ) . then ( ( res ) => {
79+ if ( ! res . ok ) {
80+ return Promise . reject ( new BadServerDataError ( null , `${ res . status } '${ res . statusText } ' received instead of 200-299 range.` , - 32002 ) ) ;
81+ } else if ( res . status === 204 || res . headers . get ( "content-length" ) === "0" ) {
82+ return undefined ;
83+ } else return res . json ( ) ;
84+ } ) . catch ( ( err ) => Promise . reject ( new BadServerDataError ( null , err . message , - 32001 ) )
85+ ) ;
86+ }
87+ function processBatchArray ( rpcResponseBatch ) {
88+ return rpcResponseBatch . map ( ( rpcResponse ) => validateResponse ( rpcResponse ) . result
89+ ) ;
90+ }
91+ function processBatchObject ( rpcResponseBatch ) {
92+ return rpcResponseBatch . reduce ( ( acc , rpcResponse ) => {
93+ acc [ rpcResponse . id ] = validateResponse ( rpcResponse ) . result ;
94+ return acc ;
95+ } , {
96+ } ) ;
97+ }
10298class Client {
10399 constructor ( resource , options = {
104100 } ) {
@@ -163,15 +159,27 @@ class Client1 {
163159 }
164160 async getPayloadData ( socket ) {
165161 this . payloadData = new Promise ( ( resolve , reject ) => {
166- socket . onmessage = ( event ) => {
167- resolve ( event . data ) ;
162+ socket . onmessage = async ( event ) => {
163+ let msg ;
164+ if ( event . data instanceof Blob ) {
165+ msg = this . getTextDecoder ( ) . decode ( await event . data . arrayBuffer ( ) ) ;
166+ } else if ( event . data instanceof ArrayBuffer ) {
167+ msg = this . getTextDecoder ( ) . decode ( event . data ) ;
168+ } else {
169+ msg = event . data ;
170+ }
171+ resolve ( msg ) ;
168172 } ;
169173 socket . onclose = ( ) => resolve ( null )
170174 ;
171175 } ) ;
172176 await this . payloadData ;
173- if ( socket . readyState > 1 ) return this . payloadData ;
174- return this . getPayloadData ( socket ) ;
177+ if ( socket . readyState < 2 ) {
178+ this . getPayloadData ( socket ) ;
179+ }
180+ }
181+ getTextDecoder ( ) {
182+ return this . textDecoder || ( this . textDecoder = new TextDecoder ( ) ) ;
175183 }
176184 async * iterateOverPayloadData ( rpcRequest , { isOnetime } ) {
177185 while ( this . socket . readyState < 2 ) {
0 commit comments