1- import fetch from "cross-fetch" ;
21import WebSocket from "isomorphic-ws" ;
32import type { Logger } from "ts-log" ;
43import { dummyLogger } from "ts-log" ;
@@ -20,6 +19,7 @@ import type {
2019import { BINARY_UPDATE_FORMAT_MAGIC_LE , FORMAT_MAGICS_LE } from "./protocol.js" ;
2120import type { WebSocketPoolConfig } from "./socket/websocket-pool.js" ;
2221import { WebSocketPool } from "./socket/websocket-pool.js" ;
22+ import { bufferFromWebsocketData } from "./util/buffer-util.js" ;
2323
2424export type BinaryResponse = {
2525 subscriptionId : number ;
@@ -113,53 +113,56 @@ export class PythLazerClient {
113113 */
114114 addMessageListener ( handler : ( event : JsonOrBinaryResponse ) => void ) {
115115 const wsp = this . getWebSocketPool ( ) ;
116- wsp . addMessageListener ( ( data : WebSocket . Data ) => {
116+ wsp . addMessageListener ( async ( data : WebSocket . Data ) => {
117117 if ( typeof data == "string" ) {
118118 handler ( {
119119 type : "json" ,
120120 value : JSON . parse ( data ) as Response ,
121121 } ) ;
122- } else if ( Buffer . isBuffer ( data ) ) {
123- let pos = 0 ;
124- const magic = data . subarray ( pos , pos + UINT32_NUM_BYTES ) . readUint32LE ( ) ;
125- pos += UINT32_NUM_BYTES ;
126- if ( magic != BINARY_UPDATE_FORMAT_MAGIC_LE ) {
127- throw new Error ( "binary update format magic mismatch" ) ;
128- }
129- // TODO: some uint64 values may not be representable as Number.
130- const subscriptionId = Number (
131- data . subarray ( pos , pos + UINT64_NUM_BYTES ) . readBigInt64BE ( ) ,
132- ) ;
133- pos += UINT64_NUM_BYTES ;
122+ return ;
123+ }
124+ const buffData = await bufferFromWebsocketData ( data ) ;
125+ let pos = 0 ;
126+ const magic = buffData
127+ . subarray ( pos , pos + UINT32_NUM_BYTES )
128+ . readUint32LE ( ) ;
129+ pos += UINT32_NUM_BYTES ;
130+ if ( magic != BINARY_UPDATE_FORMAT_MAGIC_LE ) {
131+ throw new Error ( "binary update format magic mismatch" ) ;
132+ }
133+ // TODO: some uint64 values may not be representable as Number.
134+ const subscriptionId = Number (
135+ buffData . subarray ( pos , pos + UINT64_NUM_BYTES ) . readBigInt64BE ( ) ,
136+ ) ;
137+ pos += UINT64_NUM_BYTES ;
134138
135- const value : BinaryResponse = { subscriptionId } ;
136- while ( pos < data . length ) {
137- const len = data . subarray ( pos , pos + UINT16_NUM_BYTES ) . readUint16BE ( ) ;
138- pos += UINT16_NUM_BYTES ;
139- const magic = data
140- . subarray ( pos , pos + UINT32_NUM_BYTES )
141- . readUint32LE ( ) ;
142- if ( magic == FORMAT_MAGICS_LE . EVM ) {
143- value . evm = data . subarray ( pos , pos + len ) ;
144- } else if ( magic == FORMAT_MAGICS_LE . SOLANA ) {
145- value . solana = data . subarray ( pos , pos + len ) ;
146- } else if ( magic == FORMAT_MAGICS_LE . LE_ECDSA ) {
147- value . leEcdsa = data . subarray ( pos , pos + len ) ;
148- } else if ( magic == FORMAT_MAGICS_LE . LE_UNSIGNED ) {
149- value . leUnsigned = data . subarray ( pos , pos + len ) ;
150- } else if ( magic == FORMAT_MAGICS_LE . JSON ) {
151- value . parsed = JSON . parse (
152- data . subarray ( pos + UINT32_NUM_BYTES , pos + len ) . toString ( ) ,
153- ) as ParsedPayload ;
154- } else {
155- throw new Error ( "unknown magic: " + magic . toString ( ) ) ;
156- }
157- pos += len ;
139+ const value : BinaryResponse = { subscriptionId } ;
140+ while ( pos < buffData . length ) {
141+ const len = buffData
142+ . subarray ( pos , pos + UINT16_NUM_BYTES )
143+ . readUint16BE ( ) ;
144+ pos += UINT16_NUM_BYTES ;
145+ const magic = buffData
146+ . subarray ( pos , pos + UINT32_NUM_BYTES )
147+ . readUint32LE ( ) ;
148+ if ( magic == FORMAT_MAGICS_LE . EVM ) {
149+ value . evm = buffData . subarray ( pos , pos + len ) ;
150+ } else if ( magic == FORMAT_MAGICS_LE . SOLANA ) {
151+ value . solana = buffData . subarray ( pos , pos + len ) ;
152+ } else if ( magic == FORMAT_MAGICS_LE . LE_ECDSA ) {
153+ value . leEcdsa = buffData . subarray ( pos , pos + len ) ;
154+ } else if ( magic == FORMAT_MAGICS_LE . LE_UNSIGNED ) {
155+ value . leUnsigned = buffData . subarray ( pos , pos + len ) ;
156+ } else if ( magic == FORMAT_MAGICS_LE . JSON ) {
157+ value . parsed = JSON . parse (
158+ buffData . subarray ( pos + UINT32_NUM_BYTES , pos + len ) . toString ( ) ,
159+ ) as ParsedPayload ;
160+ } else {
161+ throw new Error ( `unknown magic: ${ magic . toString ( ) } ` ) ;
158162 }
159- handler ( { type : "binary" , value } ) ;
160- } else {
161- throw new TypeError ( "unexpected event data type" ) ;
163+ pos += len ;
162164 }
165+ handler ( { type : "binary" , value } ) ;
163166 } ) ;
164167 }
165168
0 commit comments