@@ -83,11 +83,11 @@ exports.encodePacket = function (packet, supportsBinary, utf8encode, callback) {
83
83
*/
84
84
85
85
function encodeBuffer ( packet , supportsBinary , callback ) {
86
- var data = packet . data ;
87
86
if ( ! supportsBinary ) {
88
87
return exports . encodeBase64Packet ( packet , callback ) ;
89
88
}
90
89
90
+ var data = packet . data ;
91
91
var typeBuffer = new Buffer ( 1 ) ;
92
92
typeBuffer [ 0 ] = packets [ packet . type ] ;
93
93
return callback ( Buffer . concat ( [ typeBuffer , data ] ) ) ;
@@ -121,14 +121,18 @@ exports.decodePacket = function (data, binaryType, utf8decode) {
121
121
if ( data === undefined ) {
122
122
return err ;
123
123
}
124
+
125
+ var type ;
126
+
124
127
// String data
125
128
if ( typeof data === 'string' ) {
126
- if ( data . charAt ( 0 ) === 'b' ) {
129
+
130
+ type = data . charAt ( 0 ) ;
131
+
132
+ if ( type === 'b' ) {
127
133
return exports . decodeBase64Packet ( data . substr ( 1 ) , binaryType ) ;
128
134
}
129
135
130
- var type = data . charAt ( 0 ) ;
131
-
132
136
if ( utf8decode ) {
133
137
data = tryDecode ( data ) ;
134
138
if ( data === false ) {
@@ -151,14 +155,14 @@ exports.decodePacket = function (data, binaryType, utf8decode) {
151
155
if ( binaryType === 'arraybuffer' ) {
152
156
// wrap Buffer/ArrayBuffer data into an Uint8Array
153
157
var intArray = new Uint8Array ( data ) ;
154
- var type = intArray [ 0 ] ;
158
+ type = intArray [ 0 ] ;
155
159
return { type : packetslist [ type ] , data : intArray . buffer . slice ( 1 ) } ;
156
160
}
157
161
158
162
if ( data instanceof ArrayBuffer ) {
159
163
data = arrayBufferToBuffer ( data ) ;
160
164
}
161
- var type = data [ 0 ] ;
165
+ type = data [ 0 ] ;
162
166
return { type : packetslist [ type ] , data : data . slice ( 1 ) } ;
163
167
} ;
164
168
@@ -221,10 +225,6 @@ exports.encodePayload = function (packets, supportsBinary, callback) {
221
225
return callback ( '0:' ) ;
222
226
}
223
227
224
- function setLengthHeader ( message ) {
225
- return message . length + ':' + message ;
226
- }
227
-
228
228
function encodeOne ( packet , doneCallback ) {
229
229
exports . encodePacket ( packet , supportsBinary , false , function ( message ) {
230
230
doneCallback ( null , setLengthHeader ( message ) ) ;
@@ -236,6 +236,10 @@ exports.encodePayload = function (packets, supportsBinary, callback) {
236
236
} ) ;
237
237
} ;
238
238
239
+ function setLengthHeader ( message ) {
240
+ return message . length + ':' + message ;
241
+ }
242
+
239
243
/**
240
244
* Async array map using after
241
245
*/
@@ -244,15 +248,11 @@ function map(ary, each, done) {
244
248
var result = new Array ( ary . length ) ;
245
249
var next = after ( ary . length , done ) ;
246
250
247
- var eachWithIndex = function ( i , el , cb ) {
248
- each ( el , function ( error , msg ) {
251
+ for ( var i = 0 ; i < ary . length ; i ++ ) {
252
+ each ( ary [ i ] , function ( error , msg ) {
249
253
result [ i ] = msg ;
250
- cb ( error , result ) ;
254
+ next ( error , result ) ;
251
255
} ) ;
252
- } ;
253
-
254
- for ( var i = 0 ; i < ary . length ; i ++ ) {
255
- eachWithIndex ( i , ary [ i ] , next ) ;
256
256
}
257
257
}
258
258
@@ -274,13 +274,12 @@ exports.decodePayload = function (data, binaryType, callback) {
274
274
binaryType = null ;
275
275
}
276
276
277
- var packet ;
278
277
if ( data === '' ) {
279
278
// parser error - ignoring payload
280
279
return callback ( err , 0 , 1 ) ;
281
280
}
282
281
283
- var length = '' , n , msg ;
282
+ var length = '' , n , msg , packet ;
284
283
285
284
for ( var i = 0 , l = data . length ; i < l ; i ++ ) {
286
285
var chr = data . charAt ( i ) ;
@@ -310,8 +309,8 @@ exports.decodePayload = function (data, binaryType, callback) {
310
309
return callback ( err , 0 , 1 ) ;
311
310
}
312
311
313
- var ret = callback ( packet , i + n , l ) ;
314
- if ( false === ret ) return ;
312
+ var more = callback ( packet , i + n , l ) ;
313
+ if ( false === more ) return ;
315
314
}
316
315
317
316
// advance cursor
@@ -395,35 +394,42 @@ exports.encodePayloadAsBinary = function (packets, callback) {
395
394
return callback ( new Buffer ( 0 ) ) ;
396
395
}
397
396
398
- function encodeOne ( p , doneCallback ) {
399
- exports . encodePacket ( p , true , true , function ( packet ) {
400
-
401
- if ( typeof packet === 'string' ) {
402
- var encodingLength = '' + packet . length ;
403
- var sizeBuffer = new Buffer ( encodingLength . length + 2 ) ;
404
- sizeBuffer [ 0 ] = 0 ; // is a string (not true binary = 0)
405
- for ( var i = 0 ; i < encodingLength . length ; i ++ ) {
406
- sizeBuffer [ i + 1 ] = parseInt ( encodingLength [ i ] , 10 ) ;
407
- }
408
- sizeBuffer [ sizeBuffer . length - 1 ] = 255 ;
409
- return doneCallback ( null , Buffer . concat ( [ sizeBuffer , stringToBuffer ( packet ) ] ) ) ;
410
- }
397
+ map ( packets , encodeOneBinaryPacket , function ( err , results ) {
398
+ return callback ( Buffer . concat ( results ) ) ;
399
+ } ) ;
400
+ } ;
411
401
412
- var encodingLength = '' + packet . length ;
413
- var sizeBuffer = new Buffer ( encodingLength . length + 2 ) ;
414
- sizeBuffer [ 0 ] = 1 ; // is binary (true binary = 1)
402
+ function encodeOneBinaryPacket ( p , doneCallback ) {
403
+
404
+ function onBinaryPacketEncode ( packet ) {
405
+
406
+ var encodingLength = '' + packet . length ;
407
+ var sizeBuffer ;
408
+
409
+ if ( typeof packet === 'string' ) {
410
+ sizeBuffer = new Buffer ( encodingLength . length + 2 ) ;
411
+ sizeBuffer [ 0 ] = 0 ; // is a string (not true binary = 0)
415
412
for ( var i = 0 ; i < encodingLength . length ; i ++ ) {
416
413
sizeBuffer [ i + 1 ] = parseInt ( encodingLength [ i ] , 10 ) ;
417
414
}
418
415
sizeBuffer [ sizeBuffer . length - 1 ] = 255 ;
419
- doneCallback ( null , Buffer . concat ( [ sizeBuffer , packet ] ) ) ;
420
- } ) ;
416
+ return doneCallback ( null , Buffer . concat ( [ sizeBuffer , stringToBuffer ( packet ) ] ) ) ;
417
+ }
418
+
419
+ sizeBuffer = new Buffer ( encodingLength . length + 2 ) ;
420
+ sizeBuffer [ 0 ] = 1 ; // is binary (true binary = 1)
421
+ for ( var i = 0 ; i < encodingLength . length ; i ++ ) {
422
+ sizeBuffer [ i + 1 ] = parseInt ( encodingLength [ i ] , 10 ) ;
423
+ }
424
+ sizeBuffer [ sizeBuffer . length - 1 ] = 255 ;
425
+
426
+ doneCallback ( null , Buffer . concat ( [ sizeBuffer , packet ] ) ) ;
421
427
}
422
428
423
- map ( packets , encodeOne , function ( err , results ) {
424
- return callback ( Buffer . concat ( results ) ) ;
425
- } ) ;
426
- } ;
429
+ exports . encodePacket ( p , true , true , onBinaryPacketEncode ) ;
430
+
431
+ }
432
+
427
433
428
434
/*
429
435
* Decodes data when a payload is maybe expected. Strings are decoded by
@@ -442,11 +448,12 @@ exports.decodePayloadAsBinary = function (data, binaryType, callback) {
442
448
443
449
var bufferTail = data ;
444
450
var buffers = [ ] ;
451
+ var i ;
445
452
446
453
while ( bufferTail . length > 0 ) {
447
454
var strLen = '' ;
448
455
var isString = bufferTail [ 0 ] === 0 ;
449
- for ( var i = 1 ; ; i ++ ) {
456
+ for ( i = 1 ; ; i ++ ) {
450
457
if ( bufferTail [ i ] === 255 ) break ;
451
458
// 310 = char length of Number.MAX_VALUE
452
459
if ( strLen . length > 310 ) {
@@ -465,7 +472,8 @@ exports.decodePayloadAsBinary = function (data, binaryType, callback) {
465
472
}
466
473
467
474
var total = buffers . length ;
468
- buffers . forEach ( function ( buffer , i ) {
475
+ for ( i = 0 ; i < total ; i ++ ) {
476
+ var buffer = buffers [ i ] ;
469
477
callback ( exports . decodePacket ( buffer , binaryType , true ) , i , total ) ;
470
- } ) ;
478
+ }
471
479
} ;
0 commit comments