CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
deps.ts288 linesDownload Raw Back to src
1import { type Stream } from './cmap/connect';2import { MongoMissingDependencyError } from './error';3import type { Callback } from './utils';4 5function makeErrorModule(error: any) {6  const props = error ? { kModuleError: error } : {};7  return new Proxy(props, {8    get: (_: any, key: any) => {9      if (key === 'kModuleError') {10        return error;11      }12      throw error;13    },14    set: () => {15      throw error;16    }17  });18}19 20export type Kerberos = typeof import('kerberos') | { kModuleError: MongoMissingDependencyError };21 22export function getKerberos(): Kerberos {23  let kerberos: Kerberos;24  try {25    // Ensure you always wrap an optional require in the try block NODE-319926    // eslint-disable-next-line @typescript-eslint/no-require-imports27    kerberos = require('kerberos');28  } catch (error) {29    kerberos = makeErrorModule(30      new MongoMissingDependencyError(31        'Optional module `kerberos` not found. Please install it to enable kerberos authentication',32        { cause: error, dependencyName: 'kerberos' }33      )34    );35  }36  return kerberos;37}38 39export interface KerberosClient {40  step(challenge: string): Promise<string>;41  step(challenge: string, callback: Callback<string>): void;42  wrap(challenge: string, options: { user: string }): Promise<string>;43  wrap(challenge: string, options: { user: string }, callback: Callback<string>): void;44  unwrap(challenge: string): Promise<string>;45  unwrap(challenge: string, callback: Callback<string>): void;46}47 48type ZStandardLib = {49  /**50   * Compress using zstd.51   * @param buf - Buffer to be compressed.52   */53  compress(buf: Buffer, level?: number): Promise<Buffer>;54 55  /**56   * Decompress using zstd.57   */58  decompress(buf: Buffer): Promise<Buffer>;59};60 61export type ZStandard = ZStandardLib | { kModuleError: MongoMissingDependencyError };62 63export function getZstdLibrary(): ZStandardLib | { kModuleError: MongoMissingDependencyError } {64  let ZStandard: ZStandardLib | { kModuleError: MongoMissingDependencyError };65  try {66    // eslint-disable-next-line @typescript-eslint/no-require-imports67    ZStandard = require('@mongodb-js/zstd');68  } catch (error) {69    ZStandard = makeErrorModule(70      new MongoMissingDependencyError(71        'Optional module `@mongodb-js/zstd` not found. Please install it to enable zstd compression',72        { cause: error, dependencyName: 'zstd' }73      )74    );75  }76 77  return ZStandard;78}79 80/**81 * @public82 * Copy of the AwsCredentialIdentityProvider interface from [`smithy/types`](https://socket.dev/npm/package/\@smithy/types/files/1.1.1/dist-types/identity/awsCredentialIdentity.d.ts),83 * the return type of the aws-sdk's `fromNodeProviderChain().provider()`.84 */85export interface AWSCredentials {86  accessKeyId: string;87  secretAccessKey: string;88  sessionToken?: string;89  expiration?: Date;90}91 92type CredentialProvider = {93  fromNodeProviderChain(94    this: void,95    options: { clientConfig: { region: string } }96  ): () => Promise<AWSCredentials>;97  fromNodeProviderChain(this: void): () => Promise<AWSCredentials>;98};99 100export function getAwsCredentialProvider():101  | CredentialProvider102  | { kModuleError: MongoMissingDependencyError } {103  try {104    // Ensure you always wrap an optional require in the try block NODE-3199105    // eslint-disable-next-line @typescript-eslint/no-require-imports106    const credentialProvider = require('@aws-sdk/credential-providers');107    return credentialProvider;108  } catch (error) {109    return makeErrorModule(110      new MongoMissingDependencyError(111        'Optional module `@aws-sdk/credential-providers` not found.' +112          ' Please install it to enable getting aws credentials via the official sdk.',113        { cause: error, dependencyName: '@aws-sdk/credential-providers' }114      )115    );116  }117}118 119/** @internal */120export type GcpMetadata =121  | typeof import('gcp-metadata')122  | { kModuleError: MongoMissingDependencyError };123 124export function getGcpMetadata(): GcpMetadata {125  try {126    // Ensure you always wrap an optional require in the try block NODE-3199127    // eslint-disable-next-line @typescript-eslint/no-require-imports128    const credentialProvider = require('gcp-metadata');129    return credentialProvider;130  } catch (error) {131    return makeErrorModule(132      new MongoMissingDependencyError(133        'Optional module `gcp-metadata` not found.' +134          ' Please install it to enable getting gcp credentials via the official sdk.',135        { cause: error, dependencyName: 'gcp-metadata' }136      )137    );138  }139}140 141/** @internal */142export type SnappyLib = {143  /**144   * In order to support both we must check the return value of the function145   * @param buf - Buffer to be compressed146   */147  compress(buf: Buffer): Promise<Buffer>;148 149  /**150   * In order to support both we must check the return value of the function151   * @param buf - Buffer to be compressed152   */153  uncompress(buf: Buffer, opt: { asBuffer: true }): Promise<Buffer>;154};155 156export function getSnappy(): SnappyLib | { kModuleError: MongoMissingDependencyError } {157  try {158    // Ensure you always wrap an optional require in the try block NODE-3199159    // eslint-disable-next-line @typescript-eslint/no-require-imports160    const value = require('snappy');161    return value;162  } catch (error) {163    const kModuleError = new MongoMissingDependencyError(164      'Optional module `snappy` not found. Please install it to enable snappy compression',165      { cause: error, dependencyName: 'snappy' }166    );167    return { kModuleError };168  }169}170 171export type SocksLib = {172  SocksClient: {173    createConnection(options: {174      command: 'connect';175      destination: { host: string; port: number };176      proxy: {177        /** host and port are ignored because we pass existing_socket */178        host: 'iLoveJavaScript';179        port: 0;180        type: 5;181        userId?: string;182        password?: string;183      };184      timeout?: number;185      /** We always create our own socket, and pass it to this API for proxy negotiation */186      existing_socket: Stream;187    }): Promise<{ socket: Stream }>;188  };189};190 191export function getSocks(): SocksLib | { kModuleError: MongoMissingDependencyError } {192  try {193    // Ensure you always wrap an optional require in the try block NODE-3199194    // eslint-disable-next-line @typescript-eslint/no-require-imports195    const value = require('socks');196    return value;197  } catch (error) {198    const kModuleError = new MongoMissingDependencyError(199      'Optional module `socks` not found. Please install it to connections over a SOCKS5 proxy',200      { cause: error, dependencyName: 'socks' }201    );202    return { kModuleError };203  }204}205 206interface AWS4 {207  /**208   * Created these inline types to better assert future usage of this API209   * @param options - options for request210   * @param credentials - AWS credential details, sessionToken should be omitted entirely if its false-y211   */212  sign(213    this: void,214    options: {215      path: '/';216      body: string;217      host: string;218      method: 'POST';219      headers: {220        'Content-Type': 'application/x-www-form-urlencoded';221        'Content-Length': number;222        'X-MongoDB-Server-Nonce': string;223        'X-MongoDB-GS2-CB-Flag': 'n';224      };225      service: string;226      region: string;227    },228    credentials:229      | {230          accessKeyId: string;231          secretAccessKey: string;232          sessionToken: string;233        }234      | {235          accessKeyId: string;236          secretAccessKey: string;237        }238      | undefined239  ): {240    headers: {241      Authorization: string;242      'X-Amz-Date': string;243    };244  };245}246 247export const aws4: AWS4 | { kModuleError: MongoMissingDependencyError } = loadAws4();248 249function loadAws4() {250  let aws4: AWS4 | { kModuleError: MongoMissingDependencyError };251  try {252    // eslint-disable-next-line @typescript-eslint/no-require-imports253    aws4 = require('aws4');254  } catch (error) {255    aws4 = makeErrorModule(256      new MongoMissingDependencyError(257        'Optional module `aws4` not found. Please install it to enable AWS authentication',258        { cause: error, dependencyName: 'aws4' }259      )260    );261  }262 263  return aws4;264}265 266/** A utility function to get the instance of mongodb-client-encryption, if it exists. */267export function getMongoDBClientEncryption():268  | typeof import('mongodb-client-encryption')269  | { kModuleError: MongoMissingDependencyError } {270  let mongodbClientEncryption = null;271 272  try {273    // NOTE(NODE-3199): Ensure you always wrap an optional require literally in the try block274    // Cannot be moved to helper utility function, bundlers search and replace the actual require call275    // in a way that makes this line throw at bundle time, not runtime, catching here will make bundling succeed276    // eslint-disable-next-line @typescript-eslint/no-require-imports277    mongodbClientEncryption = require('mongodb-client-encryption');278  } catch (error) {279    const kModuleError = new MongoMissingDependencyError(280      'Optional module `mongodb-client-encryption` not found. Please install it to use auto encryption or ClientEncryption.',281      { cause: error, dependencyName: 'mongodb-client-encryption' }282    );283    return { kModuleError };284  }285 286  return mongodbClientEncryption;287}288