1- import { isUint8Array } from './parser/utils' ;
1+ import { isAnyArrayBuffer , isUint8Array } from './parser/utils' ;
22import type { EJSONOptions } from './extended_json' ;
33import { BSONError } from './error' ;
44import { BSON_BINARY_SUBTYPE_UUID_NEW } from './constants' ;
@@ -65,27 +65,19 @@ export class Binary extends BSONValue {
6565
6666 /**
6767 * 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- *
7468 * @param buffer - a buffer object containing the binary data.
7569 * @param subType - the option binary type.
7670 */
77- constructor ( buffer ?: string | BinarySequence , subType ?: number ) {
71+ constructor ( buffer ?: BinarySequence , subType ?: number ) {
7872 super ( ) ;
7973 if (
8074 ! ( buffer == null ) &&
81- ! ( typeof buffer === 'string' ) &&
75+ typeof buffer === 'string' &&
8276 ! ArrayBuffer . isView ( buffer ) &&
83- ! ( buffer instanceof ArrayBuffer ) &&
77+ ! isAnyArrayBuffer ( buffer ) &&
8478 ! Array . isArray ( buffer )
8579 ) {
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[]' ) ;
8981 }
9082
9183 this . sub_type = subType ?? Binary . BSON_BINARY_SUBTYPE_DEFAULT ;
@@ -95,17 +87,9 @@ export class Binary extends BSONValue {
9587 this . buffer = ByteUtils . allocate ( Binary . BUFFER_SIZE ) ;
9688 this . position = 0 ;
9789 } 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 ) ;
10993 this . position = this . buffer . byteLength ;
11094 }
11195 }
@@ -147,12 +131,12 @@ export class Binary extends BSONValue {
147131 }
148132
149133 /**
150- * Writes a buffer or string to the binary.
134+ * Writes a buffer to the binary.
151135 *
152136 * @param sequence - a string or buffer to be written to the Binary BSON object.
153137 * @param offset - specify the binary of where to write the content.
154138 */
155- write ( sequence : string | BinarySequence , offset : number ) : void {
139+ write ( sequence : BinarySequence , offset : number ) : void {
156140 offset = typeof offset === 'number' ? offset : this . position ;
157141
158142 // If the buffer is to small let's extend the buffer
@@ -169,10 +153,7 @@ export class Binary extends BSONValue {
169153 this . position =
170154 offset + sequence . byteLength > this . position ? offset + sequence . length : this . position ;
171155 } 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' ) ;
176157 }
177158 }
178159
@@ -189,26 +170,12 @@ export class Binary extends BSONValue {
189170 return this . buffer . slice ( position , position + length ) ;
190171 }
191172
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 {
201175 // 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 ) ;
212179 }
213180
214181 /** the length of the binary sequence */
0 commit comments