|
| 1 | +import { type Code } from '../../code'; |
| 2 | +import { type BSONElement, getSize, parseToElements as p } from './parse_to_elements'; |
| 3 | + |
| 4 | +/** @internal TODO */ |
| 5 | +const DEFAULT_REVIVER = () => null; |
| 6 | + |
| 7 | +/** @internal */ |
| 8 | +function parseToElements(...args: Parameters<typeof p>): BSONElement[] { |
| 9 | + const res = p(...args); |
| 10 | + return Array.isArray(res) ? res : [...res]; |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * @internal |
| 15 | + * BSONElement offsets |
| 16 | + */ |
| 17 | +const enum e { |
| 18 | + type = 0, |
| 19 | + nameOffset = 1, |
| 20 | + nameLength = 2, |
| 21 | + offset = 3, |
| 22 | + length = 4 |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * @internal |
| 27 | + * Embedded bson types |
| 28 | + */ |
| 29 | +const enum t { |
| 30 | + object = 3, |
| 31 | + array = 4, |
| 32 | + javascriptWithScope = 15 |
| 33 | +} |
| 34 | + |
| 35 | +/** @internal */ |
| 36 | +type ParseContext = { |
| 37 | + elementOffset: number; |
| 38 | + elements: BSONElement[]; |
| 39 | + container: Container; |
| 40 | + previous: ParseContext | null; |
| 41 | +}; |
| 42 | + |
| 43 | +/** |
| 44 | + * @experimental |
| 45 | + * @public |
| 46 | + * A union of the possible containers for BSON elements. |
| 47 | + * |
| 48 | + * Depending on kind, a reviver can accurately assign a value to a name on the container. |
| 49 | + */ |
| 50 | +export type Container = |
| 51 | + | { |
| 52 | + dest: Record<string, unknown>; |
| 53 | + kind: 'object'; |
| 54 | + } |
| 55 | + | { |
| 56 | + dest: Map<string, unknown>; |
| 57 | + kind: 'map'; |
| 58 | + } |
| 59 | + | { |
| 60 | + dest: Array<unknown>; |
| 61 | + kind: 'array'; |
| 62 | + } |
| 63 | + | { |
| 64 | + dest: Code; |
| 65 | + kind: 'code'; |
| 66 | + } |
| 67 | + | { |
| 68 | + kind: 'custom'; |
| 69 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 70 | + [key: string]: any; |
| 71 | + }; |
| 72 | + |
| 73 | +/** |
| 74 | + * @experimental |
| 75 | + * @public |
| 76 | + */ |
| 77 | +export type BSONReviver = ( |
| 78 | + bytes: Uint8Array, |
| 79 | + container: Container, |
| 80 | + element: BSONElement |
| 81 | +) => Container | null; |
| 82 | + |
| 83 | +/** |
| 84 | + * @experimental |
| 85 | + * @public |
| 86 | + */ |
| 87 | +export function parseToStructure<TRoot = Record<string, unknown>>( |
| 88 | + bytes: Uint8Array, |
| 89 | + startOffset?: number, |
| 90 | + root?: Container, |
| 91 | + reviver?: BSONReviver |
| 92 | +): TRoot { |
| 93 | + root ??= { |
| 94 | + kind: 'object', |
| 95 | + dest: Object.create(null) |
| 96 | + }; |
| 97 | + |
| 98 | + reviver ??= DEFAULT_REVIVER; |
| 99 | + |
| 100 | + let ctx: ParseContext | null = { |
| 101 | + elementOffset: 0, |
| 102 | + elements: parseToElements(bytes, startOffset), |
| 103 | + container: root, |
| 104 | + previous: null |
| 105 | + }; |
| 106 | + |
| 107 | + embedded: while (ctx !== null) { |
| 108 | + for ( |
| 109 | + let it: BSONElement | undefined = ctx.elements[ctx.elementOffset++]; |
| 110 | + it != null; |
| 111 | + it = ctx.elements[ctx.elementOffset++] |
| 112 | + ) { |
| 113 | + const maybeNewContainer = reviver(bytes, ctx.container, it); |
| 114 | + const isEmbeddedType = |
| 115 | + it[e.type] === t.object || it[e.type] === t.array || it[e.type] === t.javascriptWithScope; |
| 116 | + const iterateEmbedded = maybeNewContainer != null && isEmbeddedType; |
| 117 | + |
| 118 | + if (iterateEmbedded) { |
| 119 | + const docOffset: number = |
| 120 | + it[e.type] !== t.javascriptWithScope |
| 121 | + ? it[e.offset] |
| 122 | + : it[e.offset] + getSize(bytes, it[e.offset] + 4) + 4 + 4; // value offset + codeSize + value int + code int |
| 123 | + |
| 124 | + ctx = { |
| 125 | + elementOffset: 0, |
| 126 | + elements: parseToElements(bytes, docOffset), |
| 127 | + container: maybeNewContainer, |
| 128 | + previous: ctx |
| 129 | + }; |
| 130 | + |
| 131 | + continue embedded; |
| 132 | + } |
| 133 | + } |
| 134 | + ctx = ctx.previous; |
| 135 | + } |
| 136 | + |
| 137 | + return root.dest as unknown as TRoot; |
| 138 | +} |
0 commit comments