-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
d6b15f8
commit 6460989
Showing
12 changed files
with
213 additions
and
119 deletions.
There are no files selected for viewing
This file contains 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 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 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,35 @@ | ||
import { BSONError } from './error'; | ||
|
||
type TextDecoder = { | ||
readonly encoding: string; | ||
readonly fatal: boolean; | ||
readonly ignoreBOM: boolean; | ||
decode(input?: Uint8Array): string; | ||
}; | ||
type TextDecoderConstructor = { | ||
new (label: 'utf8', options: { fatal: boolean; ignoreBOM?: boolean }): TextDecoder; | ||
}; | ||
|
||
// parse utf8 globals | ||
declare const TextDecoder: TextDecoderConstructor; | ||
let TextDecoderFatal: TextDecoder; | ||
let TextDecoderNonFatal: TextDecoder; | ||
|
||
/** | ||
* Determines if the passed in bytes are valid utf8 | ||
* @param bytes - An array of 8-bit bytes. Must be indexable and have length property | ||
* @param start - The index to start validating | ||
* @param end - The index to end validating | ||
*/ | ||
export function parseUtf8(buffer: Uint8Array, start: number, end: number, fatal: boolean): string { | ||
if (fatal) { | ||
TextDecoderFatal ??= new TextDecoder('utf8', { fatal: true }); | ||
try { | ||
return TextDecoderFatal.decode(buffer.subarray(start, end)); | ||
} catch (cause) { | ||
throw new BSONError('Invalid UTF-8 string in BSON document'); | ||
} | ||
} | ||
TextDecoderNonFatal ??= new TextDecoder('utf8', { fatal: false }); | ||
return TextDecoderNonFatal.decode(buffer.subarray(start, end)); | ||
} |
This file contains 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 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 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 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 was deleted.
Oops, something went wrong.
This file contains 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
Oops, something went wrong.