{"version":3,"file":"RequireInTheMiddleSingleton.cjs","names":["ModuleNameTrie","Hook"],"sources":["../../../src/platform/node/RequireInTheMiddleSingleton.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type { OnRequireFn } from 'require-in-the-middle';\nimport { Hook } from 'require-in-the-middle';\nimport * as path from 'path';\nimport { ModuleNameTrie, ModuleNameSeparator } from './ModuleNameTrie';\n\nexport type Hooked = {\n  moduleName: string;\n  onRequire: OnRequireFn;\n};\n\n/**\n * Whether Mocha is running in this process\n * Inspired by https://github.com/AndreasPizsa/detect-mocha\n *\n * @type {boolean}\n */\nconst isMocha = [\n  'afterEach',\n  'after',\n  'beforeEach',\n  'before',\n  'describe',\n  'it',\n].every(fn => {\n  // @ts-expect-error TS7053: Element implicitly has an 'any' type\n  return typeof global[fn] === 'function';\n});\n\n/**\n * Singleton class for `require-in-the-middle`\n * Allows instrumentation plugins to patch modules with only a single `require` patch\n * WARNING: Because this class will create its own `require-in-the-middle` (RITM) instance,\n * we should minimize the number of new instances of this class.\n * Multiple instances of `@opentelemetry/instrumentation` (e.g. multiple versions) in a single process\n * will result in multiple instances of RITM, which will have an impact\n * on the performance of instrumentation hooks being applied.\n */\nexport class RequireInTheMiddleSingleton {\n  private _moduleNameTrie: ModuleNameTrie = new ModuleNameTrie();\n  private static _instance?: RequireInTheMiddleSingleton;\n\n  private constructor() {\n    this._initialize();\n  }\n\n  private _initialize() {\n    new Hook(\n      // Intercept all `require` calls; we will filter the matching ones below\n      null,\n      { internals: true },\n      (exports, name, basedir) => {\n        // For internal files on Windows, `name` will use backslash as the path separator\n        const normalizedModuleName = normalizePathSeparators(name);\n\n        const matches = this._moduleNameTrie.search(normalizedModuleName, {\n          maintainInsertionOrder: true,\n          // For core modules (e.g. `fs`), do not match on sub-paths (e.g. `fs/promises').\n          // This matches the behavior of `require-in-the-middle`.\n          // `basedir` is always `undefined` for core modules.\n          fullOnly: basedir === undefined,\n        });\n\n        for (const { onRequire } of matches) {\n          exports = onRequire(exports, name, basedir);\n        }\n\n        return exports;\n      }\n    );\n  }\n\n  /**\n   * Register a hook with `require-in-the-middle`\n   *\n   * @param {string} moduleName Module name\n   * @param {OnRequireFn} onRequire Hook function\n   * @returns {Hooked} Registered hook\n   */\n  register(moduleName: string, onRequire: OnRequireFn): Hooked {\n    const hooked = { moduleName, onRequire };\n    this._moduleNameTrie.insert(hooked);\n    return hooked;\n  }\n\n  /**\n   * Get the `RequireInTheMiddleSingleton` singleton\n   *\n   * @returns {RequireInTheMiddleSingleton} Singleton of `RequireInTheMiddleSingleton`\n   */\n  static getInstance(): RequireInTheMiddleSingleton {\n    // Mocha runs all test suites in the same process\n    // This prevents test suites from sharing a singleton\n    if (isMocha) return new RequireInTheMiddleSingleton();\n\n    return (this._instance =\n      this._instance ?? new RequireInTheMiddleSingleton());\n  }\n}\n\n/**\n * Normalize the path separators to forward slash in a module name or path\n *\n * @param {string} moduleNameOrPath Module name or path\n * @returns {string} Normalized module name or path\n */\nfunction normalizePathSeparators(moduleNameOrPath: string): string {\n  return path.sep !== ModuleNameSeparator\n    ? moduleNameOrPath.split(path.sep).join(ModuleNameSeparator)\n    : moduleNameOrPath;\n}\n"],"mappings":";;;;;;;;;;;;AAqBA,MAAM,UAAU;CACd;CACA;CACA;CACA;CACA;CACA;AACF,CAAC,CAAC,OAAM,OAAM;CAEZ,OAAO,OAAO,OAAO,QAAQ;AAC/B,CAAC;;;;;;;;;;AAWD,IAAa,8BAAb,MAAa,4BAA4B;CACvC,kBAA0C,IAAIA,uBAAAA,eAAe;CAC7D,OAAe;CAEf,cAAsB;EACpB,KAAK,YAAY;CACnB;CAEA,cAAsB;EACpB,IAAIC,sBAAAA,KAEF,MACA,EAAE,WAAW,KAAK,IACjB,SAAS,MAAM,YAAY;GAE1B,MAAM,uBAAuB,wBAAwB,IAAI;GAEzD,MAAM,UAAU,KAAK,gBAAgB,OAAO,sBAAsB;IAChE,wBAAwB;IAIxB,UAAU,YAAY,KAAA;GACxB,CAAC;GAED,KAAK,MAAM,EAAE,eAAe,SAC1B,UAAU,UAAU,SAAS,MAAM,OAAO;GAG5C,OAAO;EACT,CACF;CACF;;;;;;;;CASA,SAAS,YAAoB,WAAgC;EAC3D,MAAM,SAAS;GAAE;GAAY;EAAU;EACvC,KAAK,gBAAgB,OAAO,MAAM;EAClC,OAAO;CACT;;;;;;CAOA,OAAO,cAA2C;EAGhD,IAAI,SAAS,OAAO,IAAI,4BAA4B;EAEpD,OAAQ,KAAK,YACX,KAAK,aAAa,IAAI,4BAA4B;CACtD;AACF;;;;;;;AAQA,SAAS,wBAAwB,kBAAkC;CACjE,OAAO,KAAK,QAAA,MACR,iBAAiB,MAAM,KAAK,GAAG,CAAC,CAAC,KAAA,GAAwB,IACzD;AACN"}