@@ -9,7 +9,7 @@ var utf8 = require('./utf8');
9
9
/**
10
10
* Current protocol version.
11
11
*/
12
- exports . protocol = 3 ;
12
+ export const protocol = 3 ;
13
13
14
14
const hasBinary = ( packets ) => {
15
15
for ( const packet of packets ) {
@@ -24,7 +24,7 @@ const hasBinary = (packets) => {
24
24
* Packet types.
25
25
*/
26
26
27
- var packets = exports . packets = {
27
+ export const packets = {
28
28
open : 0 // non-ws
29
29
, close : 1 // non-ws
30
30
, ping : 2
@@ -60,7 +60,7 @@ const EMPTY_BUFFER = Buffer.concat([]);
60
60
* @api private
61
61
*/
62
62
63
- exports . encodePacket = function ( packet , supportsBinary , utf8encode , callback ) {
63
+ export function encodePacket ( packet , supportsBinary , utf8encode , callback ) {
64
64
if ( typeof supportsBinary === 'function' ) {
65
65
callback = supportsBinary ;
66
66
supportsBinary = null ;
@@ -94,7 +94,7 @@ exports.encodePacket = function (packet, supportsBinary, utf8encode, callback) {
94
94
95
95
function encodeBuffer ( packet , supportsBinary , callback ) {
96
96
if ( ! supportsBinary ) {
97
- return exports . encodeBase64Packet ( packet , callback ) ;
97
+ return encodeBase64Packet ( packet , callback ) ;
98
98
}
99
99
100
100
var data = packet . data ;
@@ -110,7 +110,7 @@ function encodeBuffer(packet, supportsBinary, callback) {
110
110
* @return {String } base64 encoded message
111
111
*/
112
112
113
- exports . encodeBase64Packet = function ( packet , callback ) {
113
+ export function encodeBase64Packet ( packet , callback ) {
114
114
var data = Buffer . isBuffer ( packet . data ) ? packet . data : arrayBufferToBuffer ( packet . data ) ;
115
115
var message = 'b' + packets [ packet . type ] ;
116
116
message += data . toString ( 'base64' ) ;
@@ -124,7 +124,7 @@ exports.encodeBase64Packet = function(packet, callback){
124
124
* @api private
125
125
*/
126
126
127
- exports . decodePacket = function ( data , binaryType , utf8decode ) {
127
+ export function decodePacket ( data , binaryType , utf8decode ) {
128
128
if ( data === undefined ) {
129
129
return err ;
130
130
}
@@ -137,7 +137,7 @@ exports.decodePacket = function (data, binaryType, utf8decode) {
137
137
type = data . charAt ( 0 ) ;
138
138
139
139
if ( type === 'b' ) {
140
- return exports . decodeBase64Packet ( data . substr ( 1 ) , binaryType ) ;
140
+ return decodeBase64Packet ( data . substr ( 1 ) , binaryType ) ;
141
141
}
142
142
143
143
if ( utf8decode ) {
@@ -189,14 +189,15 @@ function tryDecode(data) {
189
189
* @return {Object } with `type` and `data` (if any)
190
190
*/
191
191
192
- exports . decodeBase64Packet = function ( msg , binaryType ) {
192
+ export function decodeBase64Packet ( msg , binaryType ) {
193
193
var type = packetslist [ msg . charAt ( 0 ) ] ;
194
194
var data = Buffer . from ( msg . substr ( 1 ) , 'base64' ) ;
195
195
if ( binaryType === 'arraybuffer' ) {
196
196
var abv = new Uint8Array ( data . length ) ;
197
197
for ( var i = 0 ; i < abv . length ; i ++ ) {
198
198
abv [ i ] = data [ i ] ;
199
199
}
200
+ // @ts -ignore
200
201
data = abv . buffer ;
201
202
}
202
203
return { type : type , data : data } ;
@@ -218,22 +219,22 @@ exports.decodeBase64Packet = function(msg, binaryType) {
218
219
* @api private
219
220
*/
220
221
221
- exports . encodePayload = function ( packets , supportsBinary , callback ) {
222
+ export function encodePayload ( packets , supportsBinary , callback ) {
222
223
if ( typeof supportsBinary === 'function' ) {
223
224
callback = supportsBinary ;
224
225
supportsBinary = null ;
225
226
}
226
227
227
228
if ( supportsBinary && hasBinary ( packets ) ) {
228
- return exports . encodePayloadAsBinary ( packets , callback ) ;
229
+ return encodePayloadAsBinary ( packets , callback ) ;
229
230
}
230
231
231
232
if ( ! packets . length ) {
232
233
return callback ( '0:' ) ;
233
234
}
234
235
235
236
function encodeOne ( packet , doneCallback ) {
236
- exports . encodePacket ( packet , supportsBinary , false , function ( message ) {
237
+ encodePacket ( packet , supportsBinary , false , function ( message ) {
237
238
doneCallback ( null , setLengthHeader ( message ) ) ;
238
239
} ) ;
239
240
}
@@ -273,9 +274,9 @@ function map(ary, each, done) {
273
274
* @api public
274
275
*/
275
276
276
- exports . decodePayload = function ( data , binaryType , callback ) {
277
+ export function decodePayload ( data , binaryType , callback ) {
277
278
if ( typeof data !== 'string' ) {
278
- return exports . decodePayloadAsBinary ( data , binaryType , callback ) ;
279
+ return decodePayloadAsBinary ( data , binaryType , callback ) ;
279
280
}
280
281
281
282
if ( typeof binaryType === 'function' ) {
@@ -298,6 +299,7 @@ exports.decodePayload = function (data, binaryType, callback) {
298
299
continue ;
299
300
}
300
301
302
+ // @ts -ignore
301
303
if ( length === '' || ( length != ( n = Number ( length ) ) ) ) {
302
304
// parser error - ignoring payload
303
305
return callback ( err , 0 , 1 ) ;
@@ -311,7 +313,7 @@ exports.decodePayload = function (data, binaryType, callback) {
311
313
}
312
314
313
315
if ( msg . length ) {
314
- packet = exports . decodePacket ( msg , binaryType , false ) ;
316
+ packet = decodePacket ( msg , binaryType , false ) ;
315
317
316
318
if ( err . type === packet . type && err . data === packet . data ) {
317
319
// parser error in individual packet - ignoring payload
@@ -393,7 +395,7 @@ function arrayBufferToBuffer(data) {
393
395
* @api private
394
396
*/
395
397
396
- exports . encodePayloadAsBinary = function ( packets , callback ) {
398
+ export function encodePayloadAsBinary ( packets , callback ) {
397
399
if ( ! packets . length ) {
398
400
return callback ( EMPTY_BUFFER ) ;
399
401
}
@@ -430,7 +432,7 @@ function encodeOneBinaryPacket(p, doneCallback) {
430
432
doneCallback ( null , Buffer . concat ( [ sizeBuffer , packet ] ) ) ;
431
433
}
432
434
433
- exports . encodePacket ( p , true , true , onBinaryPacketEncode ) ;
435
+ encodePacket ( p , true , true , onBinaryPacketEncode ) ;
434
436
435
437
}
436
438
@@ -444,7 +446,7 @@ function encodeOneBinaryPacket(p, doneCallback) {
444
446
* @api public
445
447
*/
446
448
447
- exports . decodePayloadAsBinary = function ( data , binaryType , callback ) {
449
+ export function decodePayloadAsBinary ( data , binaryType , callback ) {
448
450
if ( typeof binaryType === 'function' ) {
449
451
callback = binaryType ;
450
452
binaryType = null ;
@@ -478,6 +480,6 @@ exports.decodePayloadAsBinary = function (data, binaryType, callback) {
478
480
var total = buffers . length ;
479
481
for ( i = 0 ; i < total ; i ++ ) {
480
482
var buffer = buffers [ i ] ;
481
- callback ( exports . decodePacket ( buffer , binaryType , true ) , i , total ) ;
483
+ callback ( decodePacket ( buffer , binaryType , true ) , i , total ) ;
482
484
}
483
485
} ;
0 commit comments