-
Notifications
You must be signed in to change notification settings - Fork 258
feat(NODE-5957): add BSON indexing API #654
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
6 commits
Select commit
Hold shift + click to select a range
05ad18c
feat(NODE-5957): add parse to elements API
nbbeeken a078182
test: comments
nbbeeken f3a01d6
fix: comments
nbbeeken 2fe5a49
add type value variables
nbbeeken e29e790
set preserveConstEnums: false
nbbeeken 71b6a71
finish using enum
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { type BSONError, BSONOffsetError } from '../../error'; | ||
import { type BSONElement, parseToElements } from './parse_to_elements'; | ||
/** | ||
* @experimental | ||
* @public | ||
* | ||
* A new set of BSON APIs that are currently experimental and not intended for production use. | ||
*/ | ||
export type OnDemand = { | ||
BSONOffsetError: { | ||
new (message: string, offset: number): BSONOffsetError; | ||
isBSONError(value: unknown): value is BSONError; | ||
}; | ||
parseToElements: (this: void, bytes: Uint8Array, startOffset?: number) => Iterable<BSONElement>; | ||
}; | ||
|
||
/** | ||
* @experimental | ||
* @public | ||
*/ | ||
const onDemand: OnDemand = Object.create(null); | ||
|
||
onDemand.parseToElements = parseToElements; | ||
onDemand.BSONOffsetError = BSONOffsetError; | ||
|
||
Object.freeze(onDemand); | ||
|
||
export { onDemand }; |
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,174 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */ | ||
import { BSONOffsetError } from '../../error'; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @remarks | ||
* - This enum is const so the code we produce will inline the numbers | ||
* - `minKey` is set to 255 so unsigned comparisons succeed | ||
* - Modify with caution, double check the bundle contains literals | ||
*/ | ||
const enum t { | ||
double = 1, | ||
string = 2, | ||
object = 3, | ||
array = 4, | ||
binData = 5, | ||
undefined = 6, | ||
objectId = 7, | ||
bool = 8, | ||
date = 9, | ||
null = 10, | ||
regex = 11, | ||
dbPointer = 12, | ||
javascript = 13, | ||
symbol = 14, | ||
javascriptWithScope = 15, | ||
int = 16, | ||
timestamp = 17, | ||
long = 18, | ||
decimal = 19, | ||
minKey = 255, | ||
maxKey = 127 | ||
} | ||
|
||
/** | ||
* @public | ||
* @experimental | ||
*/ | ||
export type BSONElement = [ | ||
baileympearson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type: number, | ||
nameOffset: number, | ||
nameLength: number, | ||
offset: number, | ||
length: number | ||
]; | ||
|
||
/** Parses a int32 little-endian at offset, throws if it is negative */ | ||
function getSize(source: Uint8Array, offset: number): number { | ||
if (source[offset + 3] > 127) { | ||
throw new BSONOffsetError('BSON size cannot be negative', offset); | ||
} | ||
return ( | ||
source[offset] | | ||
(source[offset + 1] << 8) | | ||
(source[offset + 2] << 16) | | ||
(source[offset + 3] << 24) | ||
); | ||
} | ||
|
||
/** | ||
* Searches for null terminator of a BSON element's value (Never the document null terminator) | ||
* **Does not** bounds check since this should **ONLY** be used within parseToElements which has asserted that `bytes` ends with a `0x00`. | ||
* So this will at most iterate to the document's terminator and error if that is the offset reached. | ||
*/ | ||
function findNull(bytes: Uint8Array, offset: number): number { | ||
let nullTerminatorOffset = offset; | ||
|
||
for (; bytes[nullTerminatorOffset] !== 0x00; nullTerminatorOffset++); | ||
|
||
if (nullTerminatorOffset === bytes.length - 1) { | ||
baileympearson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// We reached the null terminator of the document, not a value's | ||
throw new BSONOffsetError('Null terminator not found', offset); | ||
} | ||
|
||
return nullTerminatorOffset; | ||
} | ||
|
||
/** | ||
* @public | ||
* @experimental | ||
*/ | ||
export function parseToElements(bytes: Uint8Array, startOffset = 0): Iterable<BSONElement> { | ||
nbbeeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (bytes.length < 5) { | ||
throw new BSONOffsetError( | ||
`Input must be at least 5 bytes, got ${bytes.length} bytes`, | ||
startOffset | ||
); | ||
} | ||
|
||
const documentSize = getSize(bytes, startOffset); | ||
|
||
if (documentSize > bytes.length - startOffset) { | ||
throw new BSONOffsetError( | ||
`Parsed documentSize (${documentSize} bytes) does not match input length (${bytes.length} bytes)`, | ||
startOffset | ||
); | ||
} | ||
|
||
if (bytes[startOffset + documentSize - 1] !== 0x00) { | ||
throw new BSONOffsetError('BSON documents must end in 0x00', startOffset + documentSize); | ||
} | ||
|
||
const elements: BSONElement[] = []; | ||
let offset = startOffset + 4; | ||
|
||
while (offset <= documentSize + startOffset) { | ||
const type = bytes[offset]; | ||
offset += 1; | ||
|
||
if (type === 0) { | ||
if (offset - startOffset !== documentSize) { | ||
throw new BSONOffsetError(`Invalid 0x00 type byte`, offset); | ||
} | ||
break; | ||
} | ||
|
||
const nameOffset = offset; | ||
const nameLength = findNull(bytes, offset) - nameOffset; | ||
offset += nameLength + 1; | ||
|
||
let length: number; | ||
|
||
if (type === t.double || type === t.long || type === t.date || type === t.timestamp) { | ||
length = 8; | ||
} else if (type === t.int) { | ||
length = 4; | ||
} else if (type === t.objectId) { | ||
length = 12; | ||
} else if (type === t.decimal) { | ||
length = 16; | ||
} else if (type === t.bool) { | ||
length = 1; | ||
} else if (type === t.null || type === t.undefined || type === t.maxKey || type === t.minKey) { | ||
length = 0; | ||
} | ||
// Needs a size calculation | ||
else if (type === t.regex) { | ||
length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset; | ||
} else if (type === t.object || type === t.array || type === t.javascriptWithScope) { | ||
length = getSize(bytes, offset); | ||
} else if ( | ||
type === t.string || | ||
type === t.binData || | ||
type === t.dbPointer || | ||
type === t.javascript || | ||
type === t.symbol | ||
) { | ||
length = getSize(bytes, offset) + 4; | ||
if (type === t.binData) { | ||
// binary subtype | ||
length += 1; | ||
} | ||
if (type === t.dbPointer) { | ||
// dbPointer's objectId | ||
length += 12; | ||
} | ||
} else { | ||
throw new BSONOffsetError( | ||
`Invalid 0x${type.toString(16).padStart(2, '0')} type byte`, | ||
offset | ||
); | ||
} | ||
|
||
if (length > documentSize) { | ||
throw new BSONOffsetError('value reports length larger than document', offset); | ||
} | ||
|
||
elements.push([type, nameOffset, nameLength, offset, length]); | ||
offset += length; | ||
} | ||
|
||
return elements; | ||
} |
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 |
---|---|---|
|
@@ -18,6 +18,7 @@ const EXPECTED_EXPORTS = [ | |
'DBRef', | ||
'Binary', | ||
'ObjectId', | ||
'onDemand', | ||
'UUID', | ||
'Long', | ||
'Timestamp', | ||
|
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.