-
Notifications
You must be signed in to change notification settings - Fork 258
feat(NODE-5958): add BSON iterating API #656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
32d1204
feat(NODE-5958): add BSON iterating API
nbbeeken 7275458
docs: fix comment format
nbbeeken d4c557d
fix: typescript for root
nbbeeken cd3dd41
fix: enum location and parseToElementToArray
nbbeeken 39f1633
test: name
nbbeeken eeae36c
cleanups, support null to default
nbbeeken e3c5996
Merge branch 'main' into NODE-5958-parseToStructure
aditi-khare-mongoDB da05d20
address comments
nbbeeken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import { type Code } from '../../code'; | ||
import { type BSONElement, getSize, parseToElements } from './parse_to_elements'; | ||
|
||
/** @internal */ | ||
const DEFAULT_REVIVER: BSONReviver = ( | ||
_bytes: Uint8Array, | ||
_container: Container, | ||
_element: BSONElement | ||
) => null; | ||
|
||
/** @internal */ | ||
function parseToElementsToArray(bytes: Uint8Array, offset?: number | null): BSONElement[] { | ||
const res = parseToElements(bytes, offset); | ||
return Array.isArray(res) ? res : [...res]; | ||
} | ||
|
||
/** @internal */ | ||
type ParseContext = { | ||
elementOffset: number; | ||
elements: BSONElement[]; | ||
container: Container; | ||
previous: ParseContext | null; | ||
}; | ||
|
||
/** | ||
* @experimental | ||
* @public | ||
* A union of the possible containers for BSON elements. | ||
* | ||
* Depending on kind, a reviver can accurately assign a value to a name on the container. | ||
*/ | ||
export type Container = | ||
| { | ||
dest: Record<string, unknown>; | ||
kind: 'object'; | ||
} | ||
| { | ||
dest: Map<string, unknown>; | ||
kind: 'map'; | ||
} | ||
| { | ||
dest: Array<unknown>; | ||
kind: 'array'; | ||
} | ||
| { | ||
dest: Code; | ||
kind: 'code'; | ||
} | ||
| { | ||
kind: 'custom'; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
dest: any; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
[key: string]: any; | ||
}; | ||
|
||
/** | ||
* @experimental | ||
* @public | ||
*/ | ||
export type BSONReviver = ( | ||
bytes: Uint8Array, | ||
container: Container, | ||
element: BSONElement | ||
) => Container | null; | ||
|
||
/** | ||
* @experimental | ||
* @public | ||
*/ | ||
export function parseToStructure< | ||
TRoot extends Container = { | ||
dest: Record<string, unknown>; | ||
kind: 'object'; | ||
} | ||
>( | ||
bytes: Uint8Array, | ||
startOffset?: number | null, | ||
pRoot?: TRoot | null, | ||
pReviver?: BSONReviver | null | ||
baileympearson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
): TRoot extends undefined ? Record<string, unknown> : TRoot['dest'] { | ||
const root = pRoot ?? { | ||
kind: 'object', | ||
dest: Object.create(null) as Record<string, unknown> | ||
}; | ||
|
||
const reviver = pReviver ?? DEFAULT_REVIVER; | ||
|
||
let ctx: ParseContext | null = { | ||
elementOffset: 0, | ||
elements: parseToElementsToArray(bytes, startOffset), | ||
container: root, | ||
previous: null | ||
}; | ||
|
||
/** BSONElement offsets: type indicator and value offset */ | ||
const enum BSONElementOffset { | ||
type = 0, | ||
offset = 3 | ||
} | ||
|
||
/** BSON Embedded types */ | ||
const enum BSONElementType { | ||
object = 3, | ||
array = 4, | ||
javascriptWithScope = 15 | ||
} | ||
|
||
embedded: while (ctx !== null) { | ||
for ( | ||
let bsonElement: BSONElement | undefined = ctx.elements[ctx.elementOffset++]; | ||
bsonElement != null; | ||
bsonElement = ctx.elements[ctx.elementOffset++] | ||
) { | ||
const type = bsonElement[BSONElementOffset.type]; | ||
const offset = bsonElement[BSONElementOffset.offset]; | ||
|
||
const container = reviver(bytes, ctx.container, bsonElement); | ||
const isEmbeddedType = | ||
type === BSONElementType.object || | ||
type === BSONElementType.array || | ||
type === BSONElementType.javascriptWithScope; | ||
|
||
if (container != null && isEmbeddedType) { | ||
const docOffset: number = | ||
type !== BSONElementType.javascriptWithScope | ||
? offset | ||
: // value offset + codeSize + value int + code int | ||
offset + getSize(bytes, offset + 4) + 4 + 4; | ||
|
||
ctx = { | ||
elementOffset: 0, | ||
elements: parseToElementsToArray(bytes, docOffset), | ||
container, | ||
previous: ctx | ||
}; | ||
|
||
continue embedded; | ||
} | ||
} | ||
ctx = ctx.previous; | ||
} | ||
|
||
return root.dest; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.