|
| 1 | +import { BSONOffsetError } from '../../error'; |
| 2 | +import { NumberUtils } from '../../utils/number_utils'; |
| 3 | + |
| 4 | +/** |
| 5 | + * @public |
| 6 | + * @experimental |
| 7 | + */ |
| 8 | +export type BSONElement = [ |
| 9 | + type: number, |
| 10 | + nameOffset: number, |
| 11 | + nameLength: number, |
| 12 | + offset: number, |
| 13 | + length: number |
| 14 | +]; |
| 15 | + |
| 16 | +/** |
| 17 | + * Searches for null terminator. |
| 18 | + * **Does not** bounds check since this should **ONLY** be used within parseToElements which has asserted that `bytes` ends with a `0x00`. |
| 19 | + * So this will at most iterate to the document's terminator and error if that is the offset reached. |
| 20 | + */ |
| 21 | +function findNull(bytes: Uint8Array, offset: number): number { |
| 22 | + let nullTerminatorOffset = offset; |
| 23 | + |
| 24 | + for (; bytes[nullTerminatorOffset] !== 0x00; nullTerminatorOffset++); |
| 25 | + |
| 26 | + if (nullTerminatorOffset === bytes.length - 1) { |
| 27 | + throw new BSONOffsetError('Null terminator not found', offset); |
| 28 | + } |
| 29 | + |
| 30 | + return nullTerminatorOffset; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * @public |
| 35 | + * @experimental |
| 36 | + */ |
| 37 | +export function parseToElements(bytes: Uint8Array, startOffset = 0): Iterable<BSONElement> { |
| 38 | + if (bytes.length < 5) { |
| 39 | + throw new BSONOffsetError( |
| 40 | + `Input must be at least 5 bytes, got ${bytes.length} bytes`, |
| 41 | + startOffset |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + const documentSize = NumberUtils.getSize(bytes, startOffset); |
| 46 | + |
| 47 | + if (documentSize > bytes.length - startOffset) { |
| 48 | + throw new BSONOffsetError( |
| 49 | + `Parsed documentSize (${documentSize} bytes) does not match input length (${bytes.length} bytes)`, |
| 50 | + startOffset |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + if (bytes[startOffset + documentSize - 1] !== 0x00) { |
| 55 | + throw new BSONOffsetError('BSON documents must end in 0x00', startOffset + documentSize); |
| 56 | + } |
| 57 | + |
| 58 | + const elements: BSONElement[] = []; |
| 59 | + let offset = startOffset + 4; |
| 60 | + |
| 61 | + while (offset <= documentSize + startOffset) { |
| 62 | + const type = bytes[offset]; |
| 63 | + offset += 1; |
| 64 | + |
| 65 | + if (type === 0) { |
| 66 | + if (offset - startOffset !== documentSize) { |
| 67 | + throw new BSONOffsetError(`Invalid 0x00 type byte`, offset); |
| 68 | + } |
| 69 | + break; |
| 70 | + } |
| 71 | + |
| 72 | + const nameOffset = offset; |
| 73 | + const nameLength = findNull(bytes, offset) - nameOffset; |
| 74 | + offset += nameLength + 1; |
| 75 | + |
| 76 | + let length: number; |
| 77 | + |
| 78 | + if (type === 1 || type === 18 || type === 9 || type === 17) { |
| 79 | + // double, long, date, timestamp |
| 80 | + length = 8; |
| 81 | + } else if (type === 16) { |
| 82 | + // int |
| 83 | + length = 4; |
| 84 | + } else if (type === 7) { |
| 85 | + // objectId |
| 86 | + length = 12; |
| 87 | + } else if (type === 19) { |
| 88 | + // decimal128 |
| 89 | + length = 16; |
| 90 | + } else if (type === 8) { |
| 91 | + // boolean |
| 92 | + length = 1; |
| 93 | + } else if (type === 10 || type === 6 || type === 127 || type === 255) { |
| 94 | + // null, undefined, maxKey, minKey |
| 95 | + length = 0; |
| 96 | + } |
| 97 | + // Needs a size calculation |
| 98 | + else if (type === 11) { |
| 99 | + // regex |
| 100 | + length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset; |
| 101 | + } else if (type === 3 || type === 4 || type === 15) { |
| 102 | + // object, array, code_w_scope |
| 103 | + length = NumberUtils.getSize(bytes, offset); |
| 104 | + } else if (type === 2 || type === 5 || type === 12 || type === 13 || type === 14) { |
| 105 | + // string, binary, dbpointer, code, symbol |
| 106 | + length = NumberUtils.getSize(bytes, offset) + 4; |
| 107 | + if (type === 5) { |
| 108 | + // binary subtype |
| 109 | + length += 1; |
| 110 | + } |
| 111 | + if (type === 12) { |
| 112 | + // dbPointer's objectId |
| 113 | + length += 12; |
| 114 | + } |
| 115 | + } else { |
| 116 | + throw new BSONOffsetError( |
| 117 | + `Invalid 0x${type.toString(16).padStart(2, '0')} type byte`, |
| 118 | + offset |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + if (length > documentSize) { |
| 123 | + throw new BSONOffsetError('value reports length larger than document', offset); |
| 124 | + } |
| 125 | + |
| 126 | + elements.push([type, nameOffset, nameLength, offset, length]); |
| 127 | + offset += length; |
| 128 | + } |
| 129 | + |
| 130 | + return elements; |
| 131 | +} |
0 commit comments