CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
bson.ts249 linesDownload Raw Back to src
1import { Binary, UUID } from './binary';2import { Code } from './code';3import { DBRef } from './db_ref';4import { Decimal128 } from './decimal128';5import { Double } from './double';6import { Int32 } from './int_32';7import { Long } from './long';8import { MaxKey } from './max_key';9import { MinKey } from './min_key';10import { ObjectId } from './objectid';11import { internalCalculateObjectSize } from './parser/calculate_size';12// Parts of the parser13import { internalDeserialize, type DeserializeOptions } from './parser/deserializer';14import { serializeInto, type SerializeOptions } from './parser/serializer';15import { BSONRegExp } from './regexp';16import { BSONSymbol } from './symbol';17import { Timestamp } from './timestamp';18import { ByteUtils } from './utils/byte_utils';19import { NumberUtils } from './utils/number_utils';20export type { UUIDExtended, BinaryExtended, BinaryExtendedLegacy, BinarySequence } from './binary';21export type { CodeExtended } from './code';22export type { DBRefLike } from './db_ref';23export type { Decimal128Extended } from './decimal128';24export type { DoubleExtended } from './double';25export type { EJSONOptions } from './extended_json';26export type { Int32Extended } from './int_32';27export type { LongExtended } from './long';28export type { MaxKeyExtended } from './max_key';29export type { MinKeyExtended } from './min_key';30export type { ObjectIdExtended, ObjectIdLike } from './objectid';31export type { BSONRegExpExtended, BSONRegExpExtendedLegacy } from './regexp';32export type { BSONSymbolExtended } from './symbol';33export type { LongWithoutOverrides, TimestampExtended, TimestampOverrides } from './timestamp';34export type { LongWithoutOverridesClass } from './timestamp';35export type { SerializeOptions, DeserializeOptions };36 37export {38  Code,39  BSONSymbol,40  DBRef,41  Binary,42  ObjectId,43  UUID,44  Long,45  Timestamp,46  Double,47  Int32,48  MinKey,49  MaxKey,50  BSONRegExp,51  Decimal12852};53export { BSONValue } from './bson_value';54export { BSONError, BSONVersionError, BSONRuntimeError, BSONOffsetError } from './error';55export { BSONType } from './constants';56export { EJSON } from './extended_json';57export { onDemand, type OnDemand } from './parser/on_demand/index';58 59/** @public */60export interface Document {61  // eslint-disable-next-line @typescript-eslint/no-explicit-any62  [key: string]: any;63}64 65/** @internal */66// Default Max Size67const MAXSIZE = 1024 * 1024 * 17;68 69// Current Internal Temporary Serialization Buffer70let buffer = ByteUtils.allocate(MAXSIZE);71 72/**73 * Sets the size of the internal serialization buffer.74 *75 * @param size - The desired size for the internal serialization buffer in bytes76 * @public77 */78export function setInternalBufferSize(size: number): void {79  // Resize the internal serialization buffer if needed80  if (buffer.length < size) {81    buffer = ByteUtils.allocate(size);82  }83}84 85/**86 * Serialize a Javascript object.87 *88 * @param object - the Javascript object to serialize.89 * @returns Buffer object containing the serialized object.90 * @public91 */92export function serialize(object: Document, options: SerializeOptions = {}): Uint8Array {93  // Unpack the options94  const checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;95  const serializeFunctions =96    typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;97  const ignoreUndefined =98    typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;99  const minInternalBufferSize =100    typeof options.minInternalBufferSize === 'number' ? options.minInternalBufferSize : MAXSIZE;101 102  // Resize the internal serialization buffer if needed103  if (buffer.length < minInternalBufferSize) {104    buffer = ByteUtils.allocate(minInternalBufferSize);105  }106 107  // Attempt to serialize108  const serializationIndex = serializeInto(109    buffer,110    object,111    checkKeys,112    0,113    0,114    serializeFunctions,115    ignoreUndefined,116    null117  );118 119  // Create the final buffer120  const finishedBuffer = ByteUtils.allocateUnsafe(serializationIndex);121 122  // Copy into the finished buffer123  finishedBuffer.set(buffer.subarray(0, serializationIndex), 0);124 125  // Return the buffer126  return finishedBuffer;127}128 129/**130 * Serialize a Javascript object using a predefined Buffer and index into the buffer,131 * useful when pre-allocating the space for serialization.132 *133 * @param object - the Javascript object to serialize.134 * @param finalBuffer - the Buffer you pre-allocated to store the serialized BSON object.135 * @returns the index pointing to the last written byte in the buffer.136 * @public137 */138export function serializeWithBufferAndIndex(139  object: Document,140  finalBuffer: Uint8Array,141  options: SerializeOptions = {}142): number {143  // Unpack the options144  const checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;145  const serializeFunctions =146    typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;147  const ignoreUndefined =148    typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;149  const startIndex = typeof options.index === 'number' ? options.index : 0;150 151  // Attempt to serialize152  const serializationIndex = serializeInto(153    buffer,154    object,155    checkKeys,156    0,157    0,158    serializeFunctions,159    ignoreUndefined,160    null161  );162 163  finalBuffer.set(buffer.subarray(0, serializationIndex), startIndex);164 165  // Return the index166  return startIndex + serializationIndex - 1;167}168 169/**170 * Deserialize data as BSON.171 *172 * @param buffer - the buffer containing the serialized set of BSON documents.173 * @returns returns the deserialized Javascript Object.174 * @public175 */176export function deserialize(buffer: Uint8Array, options: DeserializeOptions = {}): Document {177  return internalDeserialize(ByteUtils.toLocalBufferType(buffer), options);178}179 180/** @public */181export type CalculateObjectSizeOptions = Pick<182  SerializeOptions,183  'serializeFunctions' | 'ignoreUndefined'184>;185 186/**187 * Calculate the bson size for a passed in Javascript object.188 *189 * @param object - the Javascript object to calculate the BSON byte size for190 * @returns size of BSON object in bytes191 * @public192 */193export function calculateObjectSize(194  object: Document,195  options: CalculateObjectSizeOptions = {}196): number {197  options = options || {};198 199  const serializeFunctions =200    typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;201  const ignoreUndefined =202    typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;203 204  return internalCalculateObjectSize(object, serializeFunctions, ignoreUndefined);205}206 207/**208 * Deserialize stream data as BSON documents.209 *210 * @param data - the buffer containing the serialized set of BSON documents.211 * @param startIndex - the start index in the data Buffer where the deserialization is to start.212 * @param numberOfDocuments - number of documents to deserialize.213 * @param documents - an array where to store the deserialized documents.214 * @param docStartIndex - the index in the documents array from where to start inserting documents.215 * @param options - additional options used for the deserialization.216 * @returns next index in the buffer after deserialization **x** numbers of documents.217 * @public218 */219export function deserializeStream(220  data: Uint8Array | ArrayBuffer,221  startIndex: number,222  numberOfDocuments: number,223  documents: Document[],224  docStartIndex: number,225  options: DeserializeOptions226): number {227  const internalOptions = Object.assign(228    { allowObjectSmallerThanBufferSize: true, index: 0 },229    options230  );231  const bufferData = ByteUtils.toLocalBufferType(data);232 233  let index = startIndex;234  // Loop over all documents235  for (let i = 0; i < numberOfDocuments; i++) {236    // Find size of the document237    const size = NumberUtils.getInt32LE(bufferData, index);238    // Update options with index239    internalOptions.index = index;240    // Parse the document at this point241    documents[docStartIndex + i] = internalDeserialize(bufferData, internalOptions);242    // Adjust index by the document size243    index = index + size;244  }245 246  // Return object containing end index of parsing and list of documents247  return index;248}249