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 ) {
@@ -125,7 +127,10 @@ Connection.prototype.send = function (message) {
125
127
C . TOPIC . CONNECTION ,
126
128
C . EVENT . CONNECTION_ERROR ,
127
129
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 ) )
129
134
)
130
135
return false
131
136
}
@@ -172,14 +177,15 @@ Connection.prototype._submit = function (message) {
172
177
173
178
Connection . prototype . _sendAuthParams = function ( ) {
174
179
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
+ )
183
189
}
184
190
185
191
Connection . prototype . _onOpen = function ( ) {
@@ -219,13 +225,11 @@ Connection.prototype._onClose = function () {
219
225
}
220
226
}
221
227
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
226
231
}
227
-
228
- this . _recvQueue . push ( data )
232
+ this . _recvQueue . push ( Message . decode ( raw ) )
229
233
if ( ! this . _processingRecv ) {
230
234
this . _processingRecv = true
231
235
this . _schedule ( this . _recvMessages )
@@ -245,24 +249,14 @@ Connection.prototype._recvMessages = function (deadline) {
245
249
return
246
250
}
247
251
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 )
257
253
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 )
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 ( )
@@ -316,10 +308,10 @@ Connection.prototype._handleAuthResponse = function (message) {
316
308
}
317
309
318
310
Connection . prototype . _getAuthData = function ( data ) {
319
- if ( data === undefined ) {
311
+ if ( ! data ) {
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