11const BrowserWebSocket = globalThis . WebSocket || globalThis . MozWebSocket
22const utils = require ( '../utils/utils' )
33const NodeWebSocket = utils . isNode ? require ( 'ws' ) : null
4- const messageParser = require ( './message-parser' )
5- const messageBuilder = require ( './message-builder' )
4+ const Message = require ( './message' )
65const C = require ( '../constants/constants' )
76const pkg = require ( '../../package.json' )
87const xxhash = require ( 'xxhash-wasm' )
@@ -79,15 +78,15 @@ Connection.prototype.authenticate = function (authParams, callback) {
7978}
8079
8180Connection . prototype . sendMsg = function ( topic , action , data ) {
82- return this . send ( messageBuilder . getMsg ( topic , action , data ) )
81+ return this . send ( Message . encode ( topic , action , data ) )
8382}
8483
8584Connection . prototype . sendMsg1 = function ( topic , action , p0 ) {
86- return this . send ( messageBuilder . getMsg1 ( topic , action , p0 ) )
85+ return this . send ( Message . encode ( topic , action , [ p0 ] ) )
8786}
8887
8988Connection . prototype . sendMsg2 = function ( topic , action , p0 , p1 ) {
90- return this . send ( messageBuilder . getMsg2 ( topic , action , p0 , p1 ) )
89+ return this . send ( Message . encode ( topic , action , [ p0 , p1 ] ) )
9190}
9291
9392Connection . prototype . close = function ( ) {
@@ -101,19 +100,22 @@ Connection.prototype.close = function () {
101100}
102101
103102Connection . prototype . _createEndpoint = function ( ) {
104- this . _endpoint = NodeWebSocket
105- ? new NodeWebSocket ( this . _url , {
106- generateMask ( ) { } ,
107- } )
108- : new BrowserWebSocket ( this . _url )
103+ if ( NodeWebSocket ) {
104+ this . _endpoint = new NodeWebSocket ( this . _url , {
105+ generateMask ( ) { } ,
106+ } )
107+ } else {
108+ this . _endpoint = new BrowserWebSocket ( this . _url )
109+ this . _endpoint . binaryType = 'arraybuffer'
110+ }
109111 this . _corked = false
110112
111113 this . _endpoint . onopen = this . _onOpen . bind ( this )
112114 this . _endpoint . onerror = this . _onError . bind ( this )
113115 this . _endpoint . onclose = this . _onClose . bind ( this )
114116 this . _endpoint . onmessage = BrowserWebSocket
115- ? ( { data } ) => this . _onMessage ( typeof data === 'string' ? data : Buffer . from ( data ) . toString ( ) )
116- : ( { data } ) => this . _onMessage ( typeof data === 'string' ? data : data . toString ( ) )
117+ ? ( { data } ) => this . _onMessage ( Buffer . from ( data ) )
118+ : ( { data } ) => this . _onMessage ( data )
117119}
118120
119121Connection . prototype . send = function ( message ) {
@@ -125,7 +127,10 @@ Connection.prototype.send = function (message) {
125127 C . TOPIC . CONNECTION ,
126128 C . EVENT . CONNECTION_ERROR ,
127129 err ,
128- message . split ( C . MESSAGE_PART_SEPERATOR ) . map ( ( x ) => x . slice ( 0 , 256 ) )
130+ message
131+ . toString ( )
132+ . split ( C . MESSAGE_PART_SEPERATOR )
133+ . map ( ( x ) => x . slice ( 0 , 256 ) )
129134 )
130135 return false
131136 }
@@ -172,14 +177,15 @@ Connection.prototype._submit = function (message) {
172177
173178Connection . prototype . _sendAuthParams = function ( ) {
174179 this . _setState ( C . CONNECTION_STATE . AUTHENTICATING )
175- const authMessage = messageBuilder . getMsg ( C . TOPIC . AUTH , C . ACTIONS . REQUEST , [
176- this . _authParams ,
177- pkg . version ,
178- utils . isNode
179- ? `Node/${ process . version } `
180- : globalThis . navigator && globalThis . navigator . userAgent ,
181- ] )
182- this . _submit ( authMessage )
180+ this . _submit (
181+ Message . encode ( C . TOPIC . AUTH , C . ACTIONS . REQUEST , [
182+ this . _authParams ,
183+ pkg . version ,
184+ utils . isNode
185+ ? `Node/${ process . version } `
186+ : globalThis . navigator && globalThis . navigator . userAgent ,
187+ ] )
188+ )
183189}
184190
185191Connection . prototype . _onOpen = function ( ) {
@@ -219,13 +225,11 @@ Connection.prototype._onClose = function () {
219225 }
220226}
221227
222- Connection . prototype . _onMessage = function ( data ) {
223- // Remove MESSAGE_SEPERATOR if exists.
224- if ( data . charCodeAt ( data . length - 1 ) === 30 ) {
225- data = data . slice ( 0 , - 1 )
228+ Connection . prototype . _onMessage = function ( raw ) {
229+ if ( raw . length <= 2 ) {
230+ return
226231 }
227-
228- this . _recvQueue . push ( data )
232+ this . _recvQueue . push ( Message . decode ( raw ) )
229233 if ( ! this . _processingRecv ) {
230234 this . _processingRecv = true
231235 this . _schedule ( this . _recvMessages )
@@ -245,24 +249,14 @@ Connection.prototype._recvMessages = function (deadline) {
245249 return
246250 }
247251
248- if ( message . length <= 2 ) {
249- continue
250- }
251-
252- if ( this . _logger ) {
253- this . _logger . trace ( message , 'receive' )
254- }
255-
256- messageParser . parseMessage ( message , this . _client , this . _message )
252+ this . emit ( 'recv' , message )
257253
258- this . emit ( 'recv' , this . _message )
259-
260- if ( this . _message . topic === C . TOPIC . CONNECTION ) {
261- this . _handleConnectionResponse ( this . _message )
262- } else if ( this . _message . topic === C . TOPIC . AUTH ) {
263- this . _handleAuthResponse ( this . _message )
254+ if ( message . topic === C . TOPIC . CONNECTION ) {
255+ this . _handleConnectionResponse ( message )
256+ } else if ( message . topic === C . TOPIC . AUTH ) {
257+ this . _handleAuthResponse ( message )
264258 } else {
265- this . _client . _$onMessage ( this . _message )
259+ this . _client . _$onMessage ( message )
266260 }
267261 }
268262
@@ -271,17 +265,15 @@ Connection.prototype._recvMessages = function (deadline) {
271265
272266Connection . prototype . _handleConnectionResponse = function ( message ) {
273267 if ( message . action === C . ACTIONS . PING ) {
274- this . _submit ( messageBuilder . getMsg ( C . TOPIC . CONNECTION , C . ACTIONS . PONG ) )
268+ this . _submit ( Message . encode ( C . TOPIC . CONNECTION , C . ACTIONS . PONG ) )
275269 } else if ( message . action === C . ACTIONS . ACK ) {
276270 this . _setState ( C . CONNECTION_STATE . AWAITING_AUTHENTICATION )
277271 if ( this . _authParams ) {
278272 this . _sendAuthParams ( )
279273 }
280274 } else if ( message . action === C . ACTIONS . CHALLENGE ) {
281275 this . _setState ( C . CONNECTION_STATE . CHALLENGING )
282- this . _submit (
283- messageBuilder . getMsg ( C . TOPIC . CONNECTION , C . ACTIONS . CHALLENGE_RESPONSE , [ this . _url ] )
284- )
276+ this . _submit ( Message . encode ( C . TOPIC . CONNECTION , C . ACTIONS . CHALLENGE_RESPONSE , [ this . _url ] ) )
285277 } else if ( message . action === C . ACTIONS . REJECTION ) {
286278 this . _challengeDenied = true
287279 this . close ( )
@@ -316,10 +308,10 @@ Connection.prototype._handleAuthResponse = function (message) {
316308}
317309
318310Connection . prototype . _getAuthData = function ( data ) {
319- if ( data === undefined ) {
311+ if ( ! data ) {
320312 return null
321313 } else {
322- return messageParser . convertTyped ( data , this . _client )
314+ return Message . decodeTyped ( data , this . _client )
323315 }
324316}
325317
0 commit comments