1
- import { isUint8Array } from './parser/utils' ;
1
+ import { isAnyArrayBuffer , isUint8Array } from './parser/utils' ;
2
2
import type { EJSONOptions } from './extended_json' ;
3
3
import { BSONError } from './error' ;
4
4
import { BSON_BINARY_SUBTYPE_UUID_NEW } from './constants' ;
@@ -65,27 +65,19 @@ export class Binary extends BSONValue {
65
65
66
66
/**
67
67
* Create a new Binary instance.
68
- *
69
- * This constructor can accept a string as its first argument. In this case,
70
- * this string will be encoded using ISO-8859-1, **not** using UTF-8.
71
- * This is almost certainly not what you want. Use `new Binary(Buffer.from(string))`
72
- * instead to convert the string to a Buffer using UTF-8 first.
73
- *
74
68
* @param buffer - a buffer object containing the binary data.
75
69
* @param subType - the option binary type.
76
70
*/
77
- constructor ( buffer ?: string | BinarySequence , subType ?: number ) {
71
+ constructor ( buffer ?: BinarySequence , subType ?: number ) {
78
72
super ( ) ;
79
73
if (
80
74
! ( buffer == null ) &&
81
- ! ( typeof buffer === 'string' ) &&
75
+ typeof buffer === 'string' &&
82
76
! ArrayBuffer . isView ( buffer ) &&
83
- ! ( buffer instanceof ArrayBuffer ) &&
77
+ ! isAnyArrayBuffer ( buffer ) &&
84
78
! Array . isArray ( buffer )
85
79
) {
86
- throw new BSONError (
87
- 'Binary can only be constructed from string, Buffer, TypedArray, or Array<number>'
88
- ) ;
80
+ throw new BSONError ( 'Binary can only be constructed from Uint8Array or number[]' ) ;
89
81
}
90
82
91
83
this . sub_type = subType ?? Binary . BSON_BINARY_SUBTYPE_DEFAULT ;
@@ -95,17 +87,9 @@ export class Binary extends BSONValue {
95
87
this . buffer = ByteUtils . allocate ( Binary . BUFFER_SIZE ) ;
96
88
this . position = 0 ;
97
89
} else {
98
- if ( typeof buffer === 'string' ) {
99
- // string
100
- this . buffer = ByteUtils . fromISO88591 ( buffer ) ;
101
- } else if ( Array . isArray ( buffer ) ) {
102
- // number[]
103
- this . buffer = ByteUtils . fromNumberArray ( buffer ) ;
104
- } else {
105
- // Buffer | TypedArray | ArrayBuffer
106
- this . buffer = ByteUtils . toLocalBufferType ( buffer ) ;
107
- }
108
-
90
+ this . buffer = Array . isArray ( buffer )
91
+ ? ByteUtils . fromNumberArray ( buffer )
92
+ : ByteUtils . toLocalBufferType ( buffer ) ;
109
93
this . position = this . buffer . byteLength ;
110
94
}
111
95
}
@@ -147,12 +131,12 @@ export class Binary extends BSONValue {
147
131
}
148
132
149
133
/**
150
- * Writes a buffer or string to the binary.
134
+ * Writes a buffer to the binary.
151
135
*
152
136
* @param sequence - a string or buffer to be written to the Binary BSON object.
153
137
* @param offset - specify the binary of where to write the content.
154
138
*/
155
- write ( sequence : string | BinarySequence , offset : number ) : void {
139
+ write ( sequence : BinarySequence , offset : number ) : void {
156
140
offset = typeof offset === 'number' ? offset : this . position ;
157
141
158
142
// If the buffer is to small let's extend the buffer
@@ -169,10 +153,7 @@ export class Binary extends BSONValue {
169
153
this . position =
170
154
offset + sequence . byteLength > this . position ? offset + sequence . length : this . position ;
171
155
} else if ( typeof sequence === 'string' ) {
172
- const bytes = ByteUtils . fromISO88591 ( sequence ) ;
173
- this . buffer . set ( bytes , offset ) ;
174
- this . position =
175
- offset + sequence . length > this . position ? offset + sequence . length : this . position ;
156
+ throw new BSONError ( 'input cannot be string' ) ;
176
157
}
177
158
}
178
159
@@ -189,26 +170,12 @@ export class Binary extends BSONValue {
189
170
return this . buffer . slice ( position , position + length ) ;
190
171
}
191
172
192
- /**
193
- * Returns the value of this binary as a string.
194
- * @param asRaw - Will skip converting to a string
195
- * @remarks
196
- * This is handy when calling this function conditionally for some key value pairs and not others
197
- */
198
- value ( asRaw ?: boolean ) : string | BinarySequence {
199
- asRaw = ! ! asRaw ;
200
-
173
+ /** returns a view of the binary value as a Uint8Array */
174
+ value ( ) : Uint8Array {
201
175
// Optimize to serialize for the situation where the data == size of buffer
202
- if ( asRaw && this . buffer . length === this . position ) {
203
- return this . buffer ;
204
- }
205
-
206
- // If it's a node.js buffer object
207
- if ( asRaw ) {
208
- return this . buffer . slice ( 0 , this . position ) ;
209
- }
210
- // TODO(NODE-4361): remove binary string support, value(true) should be the default / only option here.
211
- return ByteUtils . toISO88591 ( this . buffer . subarray ( 0 , this . position ) ) ;
176
+ return this . buffer . length === this . position
177
+ ? this . buffer
178
+ : this . buffer . subarray ( 0 , this . position ) ;
212
179
}
213
180
214
181
/** the length of the binary sequence */
0 commit comments