1
1
const BrowserWebSocket = globalThis . WebSocket || globalThis . MozWebSocket
2
2
const utils = require ( '../utils/utils' )
3
3
const NodeWebSocket = utils . isNode ? require ( 'ws' ) : null
4
- const messageParser = require ( './message-parser' )
5
- const messageBuilder = require ( './message-builder' )
4
+ const Message = require ( './message' )
6
5
const C = require ( '../constants/constants' )
7
6
const pkg = require ( '../../package.json' )
8
7
const xxhash = require ( 'xxhash-wasm' )
@@ -79,15 +78,15 @@ Connection.prototype.authenticate = function (authParams, callback) {
79
78
}
80
79
81
80
Connection . prototype . sendMsg = function ( topic , action , data ) {
82
- return this . send ( messageBuilder . getMsg ( topic , action , data ) )
81
+ return this . send ( Message . encode ( topic , action , data ) )
83
82
}
84
83
85
84
Connection . prototype . sendMsg1 = function ( topic , action , p0 ) {
86
- return this . send ( messageBuilder . getMsg1 ( topic , action , p0 ) )
85
+ return this . send ( Message . encode ( topic , action , [ p0 ] ) )
87
86
}
88
87
89
88
Connection . 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 ] ) )
91
90
}
92
91
93
92
Connection . prototype . close = function ( ) {
@@ -101,19 +100,22 @@ Connection.prototype.close = function () {
101
100
}
102
101
103
102
Connection . 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
+ }
109
111
this . _corked = false
110
112
111
113
this . _endpoint . onopen = this . _onOpen . bind ( this )
112
114
this . _endpoint . onerror = this . _onError . bind ( this )
113
115
this . _endpoint . onclose = this . _onClose . bind ( this )
114
116
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 )
117
119
}
118
120
119
121
Connection . prototype . send = function ( message ) {
@@ -172,14 +174,15 @@ Connection.prototype._submit = function (message) {
172
174
173
175
Connection . prototype . _sendAuthParams = function ( ) {
174
176
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
+ )
183
186
}
184
187
185
188
Connection . prototype . _onOpen = function ( ) {
@@ -220,11 +223,6 @@ Connection.prototype._onClose = function () {
220
223
}
221
224
222
225
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 )
226
- }
227
-
228
226
this . _recvQueue . push ( data )
229
227
if ( ! this . _processingRecv ) {
230
228
this . _processingRecv = true
@@ -239,30 +237,26 @@ Connection.prototype._recvMessages = function (deadline) {
239
237
deadline ? deadline . didTimeout || deadline . timeRemaining ( ) : n < this . _batchSize ;
240
238
++ n
241
239
) {
242
- const message = this . _recvQueue . shift ( )
243
- if ( ! message ) {
240
+ const raw = this . _recvQueue . shift ( )
241
+ if ( ! raw ) {
244
242
this . _processingRecv = false
245
243
return
246
244
}
247
245
248
- if ( message . length <= 2 ) {
246
+ if ( raw . length <= 2 ) {
249
247
continue
250
248
}
251
249
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 )
257
251
258
- this . emit ( 'recv' , this . _message )
252
+ this . emit ( 'recv' , message )
259
253
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 )
264
258
} else {
265
- this . _client . _$onMessage ( this . _message )
259
+ this . _client . _$onMessage ( message )
266
260
}
267
261
}
268
262
@@ -271,17 +265,15 @@ Connection.prototype._recvMessages = function (deadline) {
271
265
272
266
Connection . prototype . _handleConnectionResponse = function ( message ) {
273
267
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 ) )
275
269
} else if ( message . action === C . ACTIONS . ACK ) {
276
270
this . _setState ( C . CONNECTION_STATE . AWAITING_AUTHENTICATION )
277
271
if ( this . _authParams ) {
278
272
this . _sendAuthParams ( )
279
273
}
280
274
} else if ( message . action === C . ACTIONS . CHALLENGE ) {
281
275
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 ] ) )
285
277
} else if ( message . action === C . ACTIONS . REJECTION ) {
286
278
this . _challengeDenied = true
287
279
this . close ( )
@@ -319,7 +311,7 @@ Connection.prototype._getAuthData = function (data) {
319
311
if ( data === undefined ) {
320
312
return null
321
313
} else {
322
- return messageParser . convertTyped ( data , this . _client )
314
+ return Message . decodeTyped ( data , this . _client )
323
315
}
324
316
}
325
317
0 commit comments