CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
parse_utf8.ts36 linesDownload Raw Back to src
1import { BSONError } from './error';2 3type TextDecoder = {4  readonly encoding: string;5  readonly fatal: boolean;6  readonly ignoreBOM: boolean;7  decode(input?: Uint8Array): string;8};9type TextDecoderConstructor = {10  new (label: 'utf8', options: { fatal: boolean; ignoreBOM?: boolean }): TextDecoder;11};12 13// parse utf8 globals14declare const TextDecoder: TextDecoderConstructor;15let TextDecoderFatal: TextDecoder;16let TextDecoderNonFatal: TextDecoder;17 18/**19 * Determines if the passed in bytes are valid utf820 * @param bytes - An array of 8-bit bytes. Must be indexable and have length property21 * @param start - The index to start validating22 * @param end - The index to end validating23 */24export function parseUtf8(buffer: Uint8Array, start: number, end: number, fatal: boolean): string {25  if (fatal) {26    TextDecoderFatal ??= new TextDecoder('utf8', { fatal: true });27    try {28      return TextDecoderFatal.decode(buffer.subarray(start, end));29    } catch (cause) {30      throw new BSONError('Invalid UTF-8 string in BSON document', { cause });31    }32  }33  TextDecoderNonFatal ??= new TextDecoder('utf8', { fatal: false });34  return TextDecoderNonFatal.decode(buffer.subarray(start, end));35}36