binary.ts
8.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import { Buffer } from 'buffer';
import { ensureBuffer } from './ensure_buffer';
import { uuidHexStringToBuffer } from './uuid_utils';
import { UUID, UUIDExtended } from './uuid';
import type { EJSONOptions } from './extended_json';
import { BSONError, BSONTypeError } from './error';
/** @public */
export type BinarySequence = Uint8Array | Buffer | number[];
/** @public */
export interface BinaryExtendedLegacy {
$type: string;
$binary: string;
}
/** @public */
export interface BinaryExtended {
$binary: {
subType: string;
base64: string;
};
}
/**
* A class representation of the BSON Binary type.
* @public
* @category BSONType
*/
export class Binary {
_bsontype!: 'Binary';
/**
* Binary default subtype
* @internal
*/
private static readonly BSON_BINARY_SUBTYPE_DEFAULT = 0;
/** Initial buffer default size */
static readonly BUFFER_SIZE = 256;
/** Default BSON type */
static readonly SUBTYPE_DEFAULT = 0;
/** Function BSON type */
static readonly SUBTYPE_FUNCTION = 1;
/** Byte Array BSON type */
static readonly SUBTYPE_BYTE_ARRAY = 2;
/** Deprecated UUID BSON type @deprecated Please use SUBTYPE_UUID */
static readonly SUBTYPE_UUID_OLD = 3;
/** UUID BSON type */
static readonly SUBTYPE_UUID = 4;
/** MD5 BSON type */
static readonly SUBTYPE_MD5 = 5;
/** Encrypted BSON type */
static readonly SUBTYPE_ENCRYPTED = 6;
/** Column BSON type */
static readonly SUBTYPE_COLUMN = 7;
/** User BSON type */
static readonly SUBTYPE_USER_DEFINED = 128;
buffer!: Buffer;
sub_type!: number;
position!: number;
/**
* @param buffer - a buffer object containing the binary data.
* @param subType - the option binary type.
*/
constructor(buffer?: string | BinarySequence, subType?: number) {
if (!(this instanceof Binary)) return new Binary(buffer, subType);
if (
!(buffer == null) &&
!(typeof buffer === 'string') &&
!ArrayBuffer.isView(buffer) &&
!(buffer instanceof ArrayBuffer) &&
!Array.isArray(buffer)
) {
throw new BSONTypeError(
'Binary can only be constructed from string, Buffer, TypedArray, or Array<number>'
);
}
this.sub_type = subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT;
if (buffer == null) {
// create an empty binary buffer
this.buffer = Buffer.alloc(Binary.BUFFER_SIZE);
this.position = 0;
} else {
if (typeof buffer === 'string') {
// string
this.buffer = Buffer.from(buffer, 'binary');
} else if (Array.isArray(buffer)) {
// number[]
this.buffer = Buffer.from(buffer);
} else {
// Buffer | TypedArray | ArrayBuffer
this.buffer = ensureBuffer(buffer);
}
this.position = this.buffer.byteLength;
}
}
/**
* Updates this binary with byte_value.
*
* @param byteValue - a single byte we wish to write.
*/
put(byteValue: string | number | Uint8Array | Buffer | number[]): void {
// If it's a string and a has more than one character throw an error
if (typeof byteValue === 'string' && byteValue.length !== 1) {
throw new BSONTypeError('only accepts single character String');
} else if (typeof byteValue !== 'number' && byteValue.length !== 1)
throw new BSONTypeError('only accepts single character Uint8Array or Array');
// Decode the byte value once
let decodedByte: number;
if (typeof byteValue === 'string') {
decodedByte = byteValue.charCodeAt(0);
} else if (typeof byteValue === 'number') {
decodedByte = byteValue;
} else {
decodedByte = byteValue[0];
}
if (decodedByte < 0 || decodedByte > 255) {
throw new BSONTypeError('only accepts number in a valid unsigned byte range 0-255');
}
if (this.buffer.length > this.position) {
this.buffer[this.position++] = decodedByte;
} else {
const buffer = Buffer.alloc(Binary.BUFFER_SIZE + this.buffer.length);
// Combine the two buffers together
this.buffer.copy(buffer, 0, 0, this.buffer.length);
this.buffer = buffer;
this.buffer[this.position++] = decodedByte;
}
}
/**
* Writes a buffer or string to the binary.
*
* @param sequence - a string or buffer to be written to the Binary BSON object.
* @param offset - specify the binary of where to write the content.
*/
write(sequence: string | BinarySequence, offset: number): void {
offset = typeof offset === 'number' ? offset : this.position;
// If the buffer is to small let's extend the buffer
if (this.buffer.length < offset + sequence.length) {
const buffer = Buffer.alloc(this.buffer.length + sequence.length);
this.buffer.copy(buffer, 0, 0, this.buffer.length);
// Assign the new buffer
this.buffer = buffer;
}
if (ArrayBuffer.isView(sequence)) {
this.buffer.set(ensureBuffer(sequence), offset);
this.position =
offset + sequence.byteLength > this.position ? offset + sequence.length : this.position;
} else if (typeof sequence === 'string') {
this.buffer.write(sequence, offset, sequence.length, 'binary');
this.position =
offset + sequence.length > this.position ? offset + sequence.length : this.position;
}
}
/**
* Reads **length** bytes starting at **position**.
*
* @param position - read from the given position in the Binary.
* @param length - the number of bytes to read.
*/
read(position: number, length: number): BinarySequence {
length = length && length > 0 ? length : this.position;
// Let's return the data based on the type we have
return this.buffer.slice(position, position + length);
}
/**
* Returns the value of this binary as a string.
* @param asRaw - Will skip converting to a string
* @remarks
* This is handy when calling this function conditionally for some key value pairs and not others
*/
value(asRaw?: boolean): string | BinarySequence {
asRaw = !!asRaw;
// Optimize to serialize for the situation where the data == size of buffer
if (asRaw && this.buffer.length === this.position) {
return this.buffer;
}
// If it's a node.js buffer object
if (asRaw) {
return this.buffer.slice(0, this.position);
}
return this.buffer.toString('binary', 0, this.position);
}
/** the length of the binary sequence */
length(): number {
return this.position;
}
toJSON(): string {
return this.buffer.toString('base64');
}
toString(format?: string): string {
return this.buffer.toString(format);
}
/** @internal */
toExtendedJSON(options?: EJSONOptions): BinaryExtendedLegacy | BinaryExtended {
options = options || {};
const base64String = this.buffer.toString('base64');
const subType = Number(this.sub_type).toString(16);
if (options.legacy) {
return {
$binary: base64String,
$type: subType.length === 1 ? '0' + subType : subType
};
}
return {
$binary: {
base64: base64String,
subType: subType.length === 1 ? '0' + subType : subType
}
};
}
toUUID(): UUID {
if (this.sub_type === Binary.SUBTYPE_UUID) {
return new UUID(this.buffer.slice(0, this.position));
}
throw new BSONError(
`Binary sub_type "${this.sub_type}" is not supported for converting to UUID. Only "${Binary.SUBTYPE_UUID}" is currently supported.`
);
}
/** @internal */
static fromExtendedJSON(
doc: BinaryExtendedLegacy | BinaryExtended | UUIDExtended,
options?: EJSONOptions
): Binary {
options = options || {};
let data: Buffer | undefined;
let type;
if ('$binary' in doc) {
if (options.legacy && typeof doc.$binary === 'string' && '$type' in doc) {
type = doc.$type ? parseInt(doc.$type, 16) : 0;
data = Buffer.from(doc.$binary, 'base64');
} else {
if (typeof doc.$binary !== 'string') {
type = doc.$binary.subType ? parseInt(doc.$binary.subType, 16) : 0;
data = Buffer.from(doc.$binary.base64, 'base64');
}
}
} else if ('$uuid' in doc) {
type = 4;
data = uuidHexStringToBuffer(doc.$uuid);
}
if (!data) {
throw new BSONTypeError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
}
return new Binary(data, type);
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
const asBuffer = this.value(true);
return `new Binary(Buffer.from("${asBuffer.toString('hex')}", "hex"), ${this.sub_type})`;
}
}
Object.defineProperty(Binary.prototype, '_bsontype', { value: 'Binary' });