File tree 2 files changed +26
-2
lines changed
2 files changed +26
-2
lines changed Original file line number Diff line number Diff line change 1
1
2
2
module . exports = isBuf ;
3
3
4
+ var withNativeBuffer = typeof global . Buffer === 'function' && typeof global . Buffer . isBuffer === 'function' ;
5
+ var withNativeArrayBuffer = typeof global . ArrayBuffer === 'function' ;
6
+
7
+ var isView = ( function ( ) {
8
+ if ( typeof global . ArrayBuffer . isView === 'function' ) {
9
+ return global . ArrayBuffer . isView ;
10
+ } else {
11
+ return function ( obj ) { return obj . buffer instanceof global . ArrayBuffer ; } ;
12
+ }
13
+ } ) ( ) ;
14
+
4
15
/**
5
16
* Returns true if obj is a buffer or an arraybuffer.
6
17
*
7
18
* @api private
8
19
*/
9
20
10
21
function isBuf ( obj ) {
11
- return ( global . Buffer && global . Buffer . isBuffer ( obj ) ) ||
12
- ( global . ArrayBuffer && ( obj instanceof ArrayBuffer || ArrayBuffer . isView ( obj ) ) ) ;
22
+ return ( withNativeBuffer && global . Buffer . isBuffer ( obj ) ) ||
23
+ ( withNativeArrayBuffer && ( obj instanceof global . ArrayBuffer || isView ( obj ) ) ) ;
13
24
}
Original file line number Diff line number Diff line change @@ -14,6 +14,19 @@ describe('parser', function() {
14
14
helpers . test_bin ( packet ) ;
15
15
} ) ;
16
16
17
+ it ( 'encodes a TypedArray' , function ( ) {
18
+ var array = new Uint8Array ( 5 ) ;
19
+ for ( var i = 0 ; i < array . length ; i ++ ) array [ i ] = i ;
20
+
21
+ var packet = {
22
+ type : parser . BINARY_EVENT ,
23
+ data : [ 'a' , array ] ,
24
+ id : 0 ,
25
+ nsp : '/'
26
+ } ;
27
+ helpers . test_bin ( packet ) ;
28
+ } ) ;
29
+
17
30
it ( 'encodes ArrayBuffers deep in JSON' , function ( ) {
18
31
var packet = {
19
32
type : parser . BINARY_EVENT ,
You can’t perform that action at this time.
0 commit comments