1
0

binary.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. import { isUint8Array } from './parser/utils';
  2. import type { EJSONOptions } from './extended_json';
  3. import { BSONError } from './error';
  4. import { BSON_BINARY_SUBTYPE_UUID_NEW } from './constants';
  5. import { ByteUtils } from './utils/byte_utils';
  6. import { BSONValue } from './bson_value';
  7. /** @public */
  8. export type BinarySequence = Uint8Array | number[];
  9. /** @public */
  10. export interface BinaryExtendedLegacy {
  11. $type: string;
  12. $binary: string;
  13. }
  14. /** @public */
  15. export interface BinaryExtended {
  16. $binary: {
  17. subType: string;
  18. base64: string;
  19. };
  20. }
  21. /**
  22. * A class representation of the BSON Binary type.
  23. * @public
  24. * @category BSONType
  25. */
  26. export class Binary extends BSONValue {
  27. get _bsontype(): 'Binary' {
  28. return 'Binary';
  29. }
  30. /**
  31. * Binary default subtype
  32. * @internal
  33. */
  34. private static readonly BSON_BINARY_SUBTYPE_DEFAULT = 0;
  35. /** Initial buffer default size */
  36. static readonly BUFFER_SIZE = 256;
  37. /** Default BSON type */
  38. static readonly SUBTYPE_DEFAULT = 0;
  39. /** Function BSON type */
  40. static readonly SUBTYPE_FUNCTION = 1;
  41. /** Byte Array BSON type */
  42. static readonly SUBTYPE_BYTE_ARRAY = 2;
  43. /** Deprecated UUID BSON type @deprecated Please use SUBTYPE_UUID */
  44. static readonly SUBTYPE_UUID_OLD = 3;
  45. /** UUID BSON type */
  46. static readonly SUBTYPE_UUID = 4;
  47. /** MD5 BSON type */
  48. static readonly SUBTYPE_MD5 = 5;
  49. /** Encrypted BSON type */
  50. static readonly SUBTYPE_ENCRYPTED = 6;
  51. /** Column BSON type */
  52. static readonly SUBTYPE_COLUMN = 7;
  53. /** User BSON type */
  54. static readonly SUBTYPE_USER_DEFINED = 128;
  55. buffer!: Uint8Array;
  56. sub_type!: number;
  57. position!: number;
  58. /**
  59. * Create a new Binary instance.
  60. *
  61. * This constructor can accept a string as its first argument. In this case,
  62. * this string will be encoded using ISO-8859-1, **not** using UTF-8.
  63. * This is almost certainly not what you want. Use `new Binary(Buffer.from(string))`
  64. * instead to convert the string to a Buffer using UTF-8 first.
  65. *
  66. * @param buffer - a buffer object containing the binary data.
  67. * @param subType - the option binary type.
  68. */
  69. constructor(buffer?: string | BinarySequence, subType?: number) {
  70. super();
  71. if (
  72. !(buffer == null) &&
  73. !(typeof buffer === 'string') &&
  74. !ArrayBuffer.isView(buffer) &&
  75. !(buffer instanceof ArrayBuffer) &&
  76. !Array.isArray(buffer)
  77. ) {
  78. throw new BSONError(
  79. 'Binary can only be constructed from string, Buffer, TypedArray, or Array<number>'
  80. );
  81. }
  82. this.sub_type = subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT;
  83. if (buffer == null) {
  84. // create an empty binary buffer
  85. this.buffer = ByteUtils.allocate(Binary.BUFFER_SIZE);
  86. this.position = 0;
  87. } else {
  88. if (typeof buffer === 'string') {
  89. // string
  90. this.buffer = ByteUtils.fromISO88591(buffer);
  91. } else if (Array.isArray(buffer)) {
  92. // number[]
  93. this.buffer = ByteUtils.fromNumberArray(buffer);
  94. } else {
  95. // Buffer | TypedArray | ArrayBuffer
  96. this.buffer = ByteUtils.toLocalBufferType(buffer);
  97. }
  98. this.position = this.buffer.byteLength;
  99. }
  100. }
  101. /**
  102. * Updates this binary with byte_value.
  103. *
  104. * @param byteValue - a single byte we wish to write.
  105. */
  106. put(byteValue: string | number | Uint8Array | number[]): void {
  107. // If it's a string and a has more than one character throw an error
  108. if (typeof byteValue === 'string' && byteValue.length !== 1) {
  109. throw new BSONError('only accepts single character String');
  110. } else if (typeof byteValue !== 'number' && byteValue.length !== 1)
  111. throw new BSONError('only accepts single character Uint8Array or Array');
  112. // Decode the byte value once
  113. let decodedByte: number;
  114. if (typeof byteValue === 'string') {
  115. decodedByte = byteValue.charCodeAt(0);
  116. } else if (typeof byteValue === 'number') {
  117. decodedByte = byteValue;
  118. } else {
  119. decodedByte = byteValue[0];
  120. }
  121. if (decodedByte < 0 || decodedByte > 255) {
  122. throw new BSONError('only accepts number in a valid unsigned byte range 0-255');
  123. }
  124. if (this.buffer.byteLength > this.position) {
  125. this.buffer[this.position++] = decodedByte;
  126. } else {
  127. const newSpace = ByteUtils.allocate(Binary.BUFFER_SIZE + this.buffer.length);
  128. newSpace.set(this.buffer, 0);
  129. this.buffer = newSpace;
  130. this.buffer[this.position++] = decodedByte;
  131. }
  132. }
  133. /**
  134. * Writes a buffer or string to the binary.
  135. *
  136. * @param sequence - a string or buffer to be written to the Binary BSON object.
  137. * @param offset - specify the binary of where to write the content.
  138. */
  139. write(sequence: string | BinarySequence, offset: number): void {
  140. offset = typeof offset === 'number' ? offset : this.position;
  141. // If the buffer is to small let's extend the buffer
  142. if (this.buffer.byteLength < offset + sequence.length) {
  143. const newSpace = ByteUtils.allocate(this.buffer.byteLength + sequence.length);
  144. newSpace.set(this.buffer, 0);
  145. // Assign the new buffer
  146. this.buffer = newSpace;
  147. }
  148. if (ArrayBuffer.isView(sequence)) {
  149. this.buffer.set(ByteUtils.toLocalBufferType(sequence), offset);
  150. this.position =
  151. offset + sequence.byteLength > this.position ? offset + sequence.length : this.position;
  152. } else if (typeof sequence === 'string') {
  153. const bytes = ByteUtils.fromISO88591(sequence);
  154. this.buffer.set(bytes, offset);
  155. this.position =
  156. offset + sequence.length > this.position ? offset + sequence.length : this.position;
  157. }
  158. }
  159. /**
  160. * Reads **length** bytes starting at **position**.
  161. *
  162. * @param position - read from the given position in the Binary.
  163. * @param length - the number of bytes to read.
  164. */
  165. read(position: number, length: number): BinarySequence {
  166. length = length && length > 0 ? length : this.position;
  167. // Let's return the data based on the type we have
  168. return this.buffer.slice(position, position + length);
  169. }
  170. /**
  171. * Returns the value of this binary as a string.
  172. * @param asRaw - Will skip converting to a string
  173. * @remarks
  174. * This is handy when calling this function conditionally for some key value pairs and not others
  175. */
  176. value(asRaw?: boolean): string | BinarySequence {
  177. asRaw = !!asRaw;
  178. // Optimize to serialize for the situation where the data == size of buffer
  179. if (asRaw && this.buffer.length === this.position) {
  180. return this.buffer;
  181. }
  182. // If it's a node.js buffer object
  183. if (asRaw) {
  184. return this.buffer.slice(0, this.position);
  185. }
  186. // TODO(NODE-4361): remove binary string support, value(true) should be the default / only option here.
  187. return ByteUtils.toISO88591(this.buffer.subarray(0, this.position));
  188. }
  189. /** the length of the binary sequence */
  190. length(): number {
  191. return this.position;
  192. }
  193. toJSON(): string {
  194. return ByteUtils.toBase64(this.buffer);
  195. }
  196. toString(encoding?: 'hex' | 'base64' | 'utf8' | 'utf-8'): string {
  197. if (encoding === 'hex') return ByteUtils.toHex(this.buffer);
  198. if (encoding === 'base64') return ByteUtils.toBase64(this.buffer);
  199. if (encoding === 'utf8' || encoding === 'utf-8')
  200. return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength);
  201. return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength);
  202. }
  203. /** @internal */
  204. toExtendedJSON(options?: EJSONOptions): BinaryExtendedLegacy | BinaryExtended {
  205. options = options || {};
  206. const base64String = ByteUtils.toBase64(this.buffer);
  207. const subType = Number(this.sub_type).toString(16);
  208. if (options.legacy) {
  209. return {
  210. $binary: base64String,
  211. $type: subType.length === 1 ? '0' + subType : subType
  212. };
  213. }
  214. return {
  215. $binary: {
  216. base64: base64String,
  217. subType: subType.length === 1 ? '0' + subType : subType
  218. }
  219. };
  220. }
  221. toUUID(): UUID {
  222. if (this.sub_type === Binary.SUBTYPE_UUID) {
  223. return new UUID(this.buffer.slice(0, this.position));
  224. }
  225. throw new BSONError(
  226. `Binary sub_type "${this.sub_type}" is not supported for converting to UUID. Only "${Binary.SUBTYPE_UUID}" is currently supported.`
  227. );
  228. }
  229. /** Creates an Binary instance from a hex digit string */
  230. static createFromHexString(hex: string, subType?: number): Binary {
  231. return new Binary(ByteUtils.fromHex(hex), subType);
  232. }
  233. /** Creates an Binary instance from a base64 string */
  234. static createFromBase64(base64: string, subType?: number): Binary {
  235. return new Binary(ByteUtils.fromBase64(base64), subType);
  236. }
  237. /** @internal */
  238. static fromExtendedJSON(
  239. doc: BinaryExtendedLegacy | BinaryExtended | UUIDExtended,
  240. options?: EJSONOptions
  241. ): Binary {
  242. options = options || {};
  243. let data: Uint8Array | undefined;
  244. let type;
  245. if ('$binary' in doc) {
  246. if (options.legacy && typeof doc.$binary === 'string' && '$type' in doc) {
  247. type = doc.$type ? parseInt(doc.$type, 16) : 0;
  248. data = ByteUtils.fromBase64(doc.$binary);
  249. } else {
  250. if (typeof doc.$binary !== 'string') {
  251. type = doc.$binary.subType ? parseInt(doc.$binary.subType, 16) : 0;
  252. data = ByteUtils.fromBase64(doc.$binary.base64);
  253. }
  254. }
  255. } else if ('$uuid' in doc) {
  256. type = 4;
  257. data = UUID.bytesFromString(doc.$uuid);
  258. }
  259. if (!data) {
  260. throw new BSONError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
  261. }
  262. return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
  263. }
  264. /** @internal */
  265. [Symbol.for('nodejs.util.inspect.custom')](): string {
  266. return this.inspect();
  267. }
  268. inspect(): string {
  269. const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
  270. return `Binary.createFromBase64("${base64}", ${this.sub_type})`;
  271. }
  272. }
  273. /** @public */
  274. export type UUIDExtended = {
  275. $uuid: string;
  276. };
  277. const UUID_BYTE_LENGTH = 16;
  278. const UUID_WITHOUT_DASHES = /^[0-9A-F]{32}$/i;
  279. const UUID_WITH_DASHES = /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i;
  280. /**
  281. * A class representation of the BSON UUID type.
  282. * @public
  283. */
  284. export class UUID extends Binary {
  285. /** @deprecated Hex string is no longer cached, this control will be removed in a future major release */
  286. static cacheHexString = false;
  287. /**
  288. * Create a UUID type
  289. *
  290. * When the argument to the constructor is omitted a random v4 UUID will be generated.
  291. *
  292. * @param input - Can be a 32 or 36 character hex string (dashes excluded/included) or a 16 byte binary Buffer.
  293. */
  294. constructor(input?: string | Uint8Array | UUID) {
  295. let bytes: Uint8Array;
  296. if (input == null) {
  297. bytes = UUID.generate();
  298. } else if (input instanceof UUID) {
  299. bytes = ByteUtils.toLocalBufferType(new Uint8Array(input.buffer));
  300. } else if (ArrayBuffer.isView(input) && input.byteLength === UUID_BYTE_LENGTH) {
  301. bytes = ByteUtils.toLocalBufferType(input);
  302. } else if (typeof input === 'string') {
  303. bytes = UUID.bytesFromString(input);
  304. } else {
  305. throw new BSONError(
  306. 'Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).'
  307. );
  308. }
  309. super(bytes, BSON_BINARY_SUBTYPE_UUID_NEW);
  310. }
  311. /**
  312. * The UUID bytes
  313. * @readonly
  314. */
  315. get id(): Uint8Array {
  316. return this.buffer;
  317. }
  318. set id(value: Uint8Array) {
  319. this.buffer = value;
  320. }
  321. /**
  322. * Returns the UUID id as a 32 or 36 character hex string representation, excluding/including dashes (defaults to 36 character dash separated)
  323. * @param includeDashes - should the string exclude dash-separators.
  324. */
  325. toHexString(includeDashes = true): string {
  326. if (includeDashes) {
  327. return [
  328. ByteUtils.toHex(this.buffer.subarray(0, 4)),
  329. ByteUtils.toHex(this.buffer.subarray(4, 6)),
  330. ByteUtils.toHex(this.buffer.subarray(6, 8)),
  331. ByteUtils.toHex(this.buffer.subarray(8, 10)),
  332. ByteUtils.toHex(this.buffer.subarray(10, 16))
  333. ].join('-');
  334. }
  335. return ByteUtils.toHex(this.buffer);
  336. }
  337. /**
  338. * Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified.
  339. */
  340. toString(encoding?: 'hex' | 'base64'): string {
  341. if (encoding === 'hex') return ByteUtils.toHex(this.id);
  342. if (encoding === 'base64') return ByteUtils.toBase64(this.id);
  343. return this.toHexString();
  344. }
  345. /**
  346. * Converts the id into its JSON string representation.
  347. * A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  348. */
  349. toJSON(): string {
  350. return this.toHexString();
  351. }
  352. /**
  353. * Compares the equality of this UUID with `otherID`.
  354. *
  355. * @param otherId - UUID instance to compare against.
  356. */
  357. equals(otherId: string | Uint8Array | UUID): boolean {
  358. if (!otherId) {
  359. return false;
  360. }
  361. if (otherId instanceof UUID) {
  362. return ByteUtils.equals(otherId.id, this.id);
  363. }
  364. try {
  365. return ByteUtils.equals(new UUID(otherId).id, this.id);
  366. } catch {
  367. return false;
  368. }
  369. }
  370. /**
  371. * Creates a Binary instance from the current UUID.
  372. */
  373. toBinary(): Binary {
  374. return new Binary(this.id, Binary.SUBTYPE_UUID);
  375. }
  376. /**
  377. * Generates a populated buffer containing a v4 uuid
  378. */
  379. static generate(): Uint8Array {
  380. const bytes = ByteUtils.randomBytes(UUID_BYTE_LENGTH);
  381. // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
  382. // Kindly borrowed from https://github.com/uuidjs/uuid/blob/master/src/v4.js
  383. bytes[6] = (bytes[6] & 0x0f) | 0x40;
  384. bytes[8] = (bytes[8] & 0x3f) | 0x80;
  385. return bytes;
  386. }
  387. /**
  388. * Checks if a value is a valid bson UUID
  389. * @param input - UUID, string or Buffer to validate.
  390. */
  391. static isValid(input: string | Uint8Array | UUID | Binary): boolean {
  392. if (!input) {
  393. return false;
  394. }
  395. if (typeof input === 'string') {
  396. return UUID.isValidUUIDString(input);
  397. }
  398. if (isUint8Array(input)) {
  399. return input.byteLength === UUID_BYTE_LENGTH;
  400. }
  401. return (
  402. input._bsontype === 'Binary' &&
  403. input.sub_type === this.SUBTYPE_UUID &&
  404. input.buffer.byteLength === 16
  405. );
  406. }
  407. /**
  408. * Creates an UUID from a hex string representation of an UUID.
  409. * @param hexString - 32 or 36 character hex string (dashes excluded/included).
  410. */
  411. static override createFromHexString(hexString: string): UUID {
  412. const buffer = UUID.bytesFromString(hexString);
  413. return new UUID(buffer);
  414. }
  415. /** Creates an UUID from a base64 string representation of an UUID. */
  416. static override createFromBase64(base64: string): UUID {
  417. return new UUID(ByteUtils.fromBase64(base64));
  418. }
  419. /** @internal */
  420. static bytesFromString(representation: string) {
  421. if (!UUID.isValidUUIDString(representation)) {
  422. throw new BSONError(
  423. 'UUID string representation must be 32 hex digits or canonical hyphenated representation'
  424. );
  425. }
  426. return ByteUtils.fromHex(representation.replace(/-/g, ''));
  427. }
  428. /**
  429. * @internal
  430. *
  431. * Validates a string to be a hex digit sequence with or without dashes.
  432. * The canonical hyphenated representation of a uuid is hex in 8-4-4-4-12 groups.
  433. */
  434. static isValidUUIDString(representation: string) {
  435. return UUID_WITHOUT_DASHES.test(representation) || UUID_WITH_DASHES.test(representation);
  436. }
  437. /**
  438. * Converts to a string representation of this Id.
  439. *
  440. * @returns return the 36 character hex string representation.
  441. * @internal
  442. */
  443. [Symbol.for('nodejs.util.inspect.custom')](): string {
  444. return this.inspect();
  445. }
  446. inspect(): string {
  447. return `new UUID("${this.toHexString()}")`;
  448. }
  449. }