@@ -27,27 +27,19 @@ class Binary {
27
27
! ( buffer instanceof Uint8Array ) &&
28
28
! Array . isArray ( buffer )
29
29
) {
30
- throw new Error ( 'only String, Buffer, Uint8Array or Array accepted' ) ;
30
+ throw new TypeError ( 'only String, Buffer, Uint8Array or Array accepted' ) ;
31
31
}
32
32
33
- if ( buffer instanceof Number ) {
34
- this . sub_type = buffer ;
35
- this . position = 0 ;
36
- } else {
37
- this . sub_type = subType == null ? BSON_BINARY_SUBTYPE_DEFAULT : subType ;
38
- this . position = 0 ;
39
- }
33
+ this . sub_type = subType == null ? BSON_BINARY_SUBTYPE_DEFAULT : subType ;
34
+ this . position = 0 ;
40
35
41
36
if ( buffer != null && ! ( buffer instanceof Number ) ) {
42
37
// Only accept Buffer, Uint8Array or Arrays
43
38
if ( typeof buffer === 'string' ) {
44
39
// Different ways of writing the length of the string for the different types
45
40
if ( typeof Buffer !== 'undefined' ) {
46
41
this . buffer = new Buffer ( buffer ) ;
47
- } else if (
48
- typeof Uint8Array !== 'undefined' ||
49
- Object . prototype . toString . call ( buffer ) === '[object Array]'
50
- ) {
42
+ } else if ( typeof Uint8Array !== 'undefined' || Array . isArray ( buffer ) ) {
51
43
this . buffer = writeStringToArray ( buffer ) ;
52
44
} else {
53
45
throw new TypeError ( 'only String, Buffer, Uint8Array or Array accepted' ) ;
@@ -64,8 +56,6 @@ class Binary {
64
56
} else {
65
57
this . buffer = new Array ( Binary . BUFFER_SIZE ) ;
66
58
}
67
- // Set position to start of buffer
68
- this . position = 0 ;
69
59
}
70
60
}
71
61
@@ -105,7 +95,7 @@ class Binary {
105
95
} else {
106
96
let buffer = null ;
107
97
// Create a new buffer (typed or normal array)
108
- if ( Object . prototype . toString . call ( this . buffer ) === '[object Uint8Array]' ) {
98
+ if ( isUint8Array ( this . buffer ) ) {
109
99
buffer = new Uint8Array ( new ArrayBuffer ( Binary . BUFFER_SIZE + this . buffer . length ) ) ;
110
100
} else {
111
101
buffer = new Array ( Binary . BUFFER_SIZE + this . buffer . length ) ;
@@ -142,7 +132,7 @@ class Binary {
142
132
if ( typeof Buffer !== 'undefined' && Buffer . isBuffer ( this . buffer ) ) {
143
133
buffer = new Buffer ( this . buffer . length + string . length ) ;
144
134
this . buffer . copy ( buffer , 0 , 0 , this . buffer . length ) ;
145
- } else if ( Object . prototype . toString . call ( this . buffer ) === '[object Uint8Array]' ) {
135
+ } else if ( isUint8Array ( this . buffer ) ) {
146
136
// Create a new buffer
147
137
buffer = new Uint8Array ( new ArrayBuffer ( this . buffer . length + string . length ) ) ;
148
138
// Copy the content
@@ -169,10 +159,7 @@ class Binary {
169
159
this . position =
170
160
offset + string . length > this . position ? offset + string . length : this . position ;
171
161
// offset = string.length;
172
- } else if (
173
- Object . prototype . toString . call ( string ) === '[object Uint8Array]' ||
174
- ( Object . prototype . toString . call ( string ) === '[object Array]' && typeof string !== 'string' )
175
- ) {
162
+ } else if ( isUint8Array ( string ) || ( Array . isArray ( string ) && typeof string !== 'string' ) ) {
176
163
for ( let i = 0 ; i < string . length ; i ++ ) {
177
164
this . buffer [ offset ++ ] = string [ i ] ;
178
165
}
@@ -246,10 +233,9 @@ class Binary {
246
233
return this . buffer . slice ( 0 , this . position ) ;
247
234
} else {
248
235
// Create a new buffer to copy content to
249
- const newBuffer =
250
- Object . prototype . toString . call ( this . buffer ) === '[object Uint8Array]'
251
- ? new Uint8Array ( new ArrayBuffer ( this . position ) )
252
- : new Array ( this . position ) ;
236
+ const newBuffer = isUint8Array ( this . buffer )
237
+ ? new Uint8Array ( new ArrayBuffer ( this . position ) )
238
+ : new Array ( this . position ) ;
253
239
254
240
// Copy content
255
241
for ( let i = 0 ; i < this . position ; i ++ ) {
@@ -296,6 +282,10 @@ class Binary {
296
282
*/
297
283
const BSON_BINARY_SUBTYPE_DEFAULT = 0 ;
298
284
285
+ function isUint8Array ( obj ) {
286
+ return Object . prototype . toString . call ( obj ) === '[object Uint8Array]' ;
287
+ }
288
+
299
289
/**
300
290
* @ignore
301
291
*/
0 commit comments