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,18 @@ Connection.prototype._recvMessages = function (deadline) {
245249 return
246250 }
247251
248- if ( message . length <= 2 ) {
249- continue
250- }
252+ this . emit ( 'recv' , message )
251253
252254 try {
253- messageParser . parseMessage ( message , this . _client , this . _message )
254-
255- this . emit ( 'recv' , this . _message )
256-
257- if ( this . _message . topic === C . TOPIC . CONNECTION ) {
258- this . _handleConnectionResponse ( this . _message )
259- } else if ( this . _message . topic === C . TOPIC . AUTH ) {
260- this . _handleAuthResponse ( this . _message )
255+ if ( message . topic === C . TOPIC . CONNECTION ) {
256+ this . _handleConnectionResponse ( message )
257+ } else if ( message . topic === C . TOPIC . AUTH ) {
258+ this . _handleAuthResponse ( message )
261259 } else {
262- this . _client . _$onMessage ( this . _message )
260+ this . _client . _$onMessage ( message )
263261 }
264262 } catch ( err ) {
265- this . _onError ( err )
263+ this . _client . _onError ( err )
266264 }
267265 }
268266
@@ -271,17 +269,15 @@ Connection.prototype._recvMessages = function (deadline) {
271269
272270Connection . prototype . _handleConnectionResponse = function ( message ) {
273271 if ( message . action === C . ACTIONS . PING ) {
274- this . _submit ( messageBuilder . getMsg ( C . TOPIC . CONNECTION , C . ACTIONS . PONG ) )
272+ this . _submit ( Message . encode ( C . TOPIC . CONNECTION , C . ACTIONS . PONG ) )
275273 } else if ( message . action === C . ACTIONS . ACK ) {
276274 this . _setState ( C . CONNECTION_STATE . AWAITING_AUTHENTICATION )
277275 if ( this . _authParams ) {
278276 this . _sendAuthParams ( )
279277 }
280278 } else if ( message . action === C . ACTIONS . CHALLENGE ) {
281279 this . _setState ( C . CONNECTION_STATE . CHALLENGING )
282- this . _submit (
283- messageBuilder . getMsg ( C . TOPIC . CONNECTION , C . ACTIONS . CHALLENGE_RESPONSE , [ this . _url ] )
284- )
280+ this . _submit ( Message . encode ( C . TOPIC . CONNECTION , C . ACTIONS . CHALLENGE_RESPONSE , [ this . _url ] ) )
285281 } else if ( message . action === C . ACTIONS . REJECTION ) {
286282 this . _challengeDenied = true
287283 this . close ( )
@@ -316,10 +312,10 @@ Connection.prototype._handleAuthResponse = function (message) {
316312}
317313
318314Connection . prototype . _getAuthData = function ( data ) {
319- if ( data === undefined ) {
315+ if ( ! data ) {
320316 return null
321317 } else {
322- return messageParser . convertTyped ( data , this . _client )
318+ return Message . decodeTyped ( data , this . _client )
323319 }
324320}
325321
0 commit comments