CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
bson.ts149 linesDownload Raw Back to src
1/* eslint-disable no-restricted-imports */2import { BSON, type DeserializeOptions, type SerializeOptions } from 'bson';3 4export {5  Binary,6  BSON,7  BSONError,8  BSONRegExp,9  BSONSymbol,10  BSONType,11  calculateObjectSize,12  Code,13  DBRef,14  Decimal128,15  deserialize,16  type DeserializeOptions,17  Document,18  Double,19  EJSON,20  EJSONOptions,21  Int32,22  Long,23  MaxKey,24  MinKey,25  ObjectId,26  type ObjectIdLike,27  serialize,28  Timestamp,29  UUID30} from 'bson';31 32/** @internal */33export type BSONElement = BSON.OnDemand['BSONElement'];34 35export function parseToElementsToArray(bytes: Uint8Array, offset?: number): BSONElement[] {36  const res = BSON.onDemand.parseToElements(bytes, offset);37  return Array.isArray(res) ? res : [...res];38}39 40export const getInt32LE = BSON.onDemand.NumberUtils.getInt32LE;41export const getFloat64LE = BSON.onDemand.NumberUtils.getFloat64LE;42export const getBigInt64LE = BSON.onDemand.NumberUtils.getBigInt64LE;43export const toUTF8 = BSON.onDemand.ByteUtils.toUTF8;44 45/**46 * BSON Serialization options.47 * @public48 */49export interface BSONSerializeOptions50  extends Omit<SerializeOptions, 'index'>,51    Omit<52      DeserializeOptions,53      | 'evalFunctions'54      | 'cacheFunctions'55      | 'cacheFunctionsCrc32'56      | 'allowObjectSmallerThanBufferSize'57      | 'index'58      | 'validation'59    > {60  /**61   * Enabling the raw option will return a [Node.js Buffer](https://nodejs.org/api/buffer.html)62   * which is allocated using [allocUnsafe API](https://nodejs.org/api/buffer.html#static-method-bufferallocunsafesize).63   * See this section from the [Node.js Docs here](https://nodejs.org/api/buffer.html#what-makes-bufferallocunsafe-and-bufferallocunsafeslow-unsafe)64   * for more detail about what "unsafe" refers to in this context.65   * If you need to maintain your own editable clone of the bytes returned for an extended life time of the process, it is recommended you allocate66   * your own buffer and clone the contents:67   *68   * @example69   * ```ts70   * const raw = await collection.findOne({}, { raw: true });71   * const myBuffer = Buffer.alloc(raw.byteLength);72   * myBuffer.set(raw, 0);73   * // Only save and use `myBuffer` beyond this point74   * ```75   *76   * @remarks77   * Please note there is a known limitation where this option cannot be used at the MongoClient level (see [NODE-3946](https://jira.mongodb.org/browse/NODE-3946)).78   * It does correctly work at `Db`, `Collection`, and per operation the same as other BSON options work.79   */80  raw?: boolean;81 82  /** Enable utf8 validation when deserializing BSON documents.  Defaults to true. */83  enableUtf8Validation?: boolean;84}85 86export function pluckBSONSerializeOptions(options: BSONSerializeOptions): BSONSerializeOptions {87  const {88    fieldsAsRaw,89    useBigInt64,90    promoteValues,91    promoteBuffers,92    promoteLongs,93    serializeFunctions,94    ignoreUndefined,95    bsonRegExp,96    raw,97    enableUtf8Validation98  } = options;99  return {100    fieldsAsRaw,101    useBigInt64,102    promoteValues,103    promoteBuffers,104    promoteLongs,105    serializeFunctions,106    ignoreUndefined,107    bsonRegExp,108    raw,109    enableUtf8Validation110  };111}112 113/**114 * Merge the given BSONSerializeOptions, preferring options over the parent's options, and115 * substituting defaults for values not set.116 *117 * @internal118 */119export function resolveBSONOptions(120  options?: BSONSerializeOptions,121  parent?: { bsonOptions?: BSONSerializeOptions }122): BSONSerializeOptions {123  const parentOptions = parent?.bsonOptions;124  return {125    raw: options?.raw ?? parentOptions?.raw ?? false,126    useBigInt64: options?.useBigInt64 ?? parentOptions?.useBigInt64 ?? false,127    promoteLongs: options?.promoteLongs ?? parentOptions?.promoteLongs ?? true,128    promoteValues: options?.promoteValues ?? parentOptions?.promoteValues ?? true,129    promoteBuffers: options?.promoteBuffers ?? parentOptions?.promoteBuffers ?? false,130    ignoreUndefined: options?.ignoreUndefined ?? parentOptions?.ignoreUndefined ?? false,131    bsonRegExp: options?.bsonRegExp ?? parentOptions?.bsonRegExp ?? false,132    serializeFunctions: options?.serializeFunctions ?? parentOptions?.serializeFunctions ?? false,133    fieldsAsRaw: options?.fieldsAsRaw ?? parentOptions?.fieldsAsRaw ?? {},134    enableUtf8Validation:135      options?.enableUtf8Validation ?? parentOptions?.enableUtf8Validation ?? true136  };137}138 139/** @internal */140export function parseUtf8ValidationOption(options?: { enableUtf8Validation?: boolean }): {141  utf8: { writeErrors: false } | false;142} {143  const enableUtf8Validation = options?.enableUtf8Validation;144  if (enableUtf8Validation === false) {145    return { utf8: false };146  }147  return { utf8: { writeErrors: false } };148}149