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 ) {
@@ -172,14 +174,15 @@ Connection.prototype._submit = function (message) {
172174
173175Connection . prototype . _sendAuthParams = function ( ) {
174176 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 )
177+ this . _submit (
178+ Message . encode ( C . TOPIC . AUTH , C . ACTIONS . REQUEST , [
179+ this . _authParams ,
180+ pkg . version ,
181+ utils . isNode
182+ ? `Node/${ process . version } `
183+ : globalThis . navigator && globalThis . navigator . userAgent ,
184+ ] )
185+ )
183186}
184187
185188Connection . prototype . _onOpen = function ( ) {
@@ -220,11 +223,6 @@ Connection.prototype._onClose = function () {
220223}
221224
222225Connection . prototype . _onMessage = function ( data ) {
223- // Remove MESSAGE_SEPERATOR if exists.
224- if ( data . charCodeAt ( data . length - 1 ) === 30 ) {
225- data = data . slice ( 0 , - 1 )
226- }
227-
228226 this . _recvQueue . push ( data )
229227 if ( ! this . _processingRecv ) {
230228 this . _processingRecv = true
@@ -239,30 +237,26 @@ Connection.prototype._recvMessages = function (deadline) {
239237 deadline ? deadline . didTimeout || deadline . timeRemaining ( ) : n < this . _batchSize ;
240238 ++ n
241239 ) {
242- const message = this . _recvQueue . shift ( )
243- if ( ! message ) {
240+ const raw = this . _recvQueue . shift ( )
241+ if ( ! raw ) {
244242 this . _processingRecv = false
245243 return
246244 }
247245
248- if ( message . length <= 2 ) {
246+ if ( raw . length <= 2 ) {
249247 continue
250248 }
251249
252- if ( this . _logger ) {
253- this . _logger . trace ( message , 'receive' )
254- }
255-
256- messageParser . parseMessage ( message , this . _client , this . _message )
250+ const message = Message . decode ( raw )
257251
258- this . emit ( 'recv' , this . _message )
252+ this . emit ( 'recv' , message )
259253
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 ( )
@@ -319,7 +311,7 @@ Connection.prototype._getAuthData = function (data) {
319311 if ( data === undefined ) {
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