{"version":3,"file":"storage.mjs","names":["buildDebug","BaseStorage"],"sources":["../../../src/lib/storage.ts"],"sourcesContent":["import buildDebug from 'debug';\nimport { head, isNil } from 'lodash-es';\nimport assert from 'node:assert';\n\nimport { PLUGIN_CATEGORY, PLUGIN_PREFIX, errorUtils } from '@verdaccio/core';\nimport { asyncLoadPlugin } from '@verdaccio/loaders';\nimport LocalDatabaseModule from '@verdaccio/local-storage';\nimport { Storage as BaseStorage } from '@verdaccio/store';\nimport { Config, Logger } from '@verdaccio/types';\n\nimport { isLegacyStoragePlugin, wrapLegacyStoragePlugin } from './legacy-storage-adapter';\n\nconst debug = buildDebug('verdaccio:storage');\n\n// CJS/ESM interop for the default storage plugin\nconst LocalDatabase = (LocalDatabaseModule as any).default || LocalDatabaseModule;\n\n// `storageSanityCheck` only verifies `getPackageStorage` exists, which both the\n// legacy and promise contracts satisfy.\nconst storageSanityCheck = (plugin: any): boolean =>\n  typeof plugin?.getPackageStorage !== 'undefined';\n\n/**\n * Drop-in replacement for `@verdaccio/store`'s internal `LocalStorage` that\n * loads the configured storage plugin exactly like upstream, but wraps a\n * legacy callback-based plugin in a promisifying adapter so it satisfies the\n * promise contract the rest of `@verdaccio/store` relies on.\n *\n * `@verdaccio/store` only ever calls `init()`, `getSecret()` and\n * `getStoragePlugin()` on this object, so that is the entire surface we mirror.\n */\nclass CompatLocalStorage {\n  public storagePlugin: any = null;\n  private readonly config: Config;\n  private readonly logger: Logger;\n\n  public constructor(config: Config, logger: Logger) {\n    this.config = config;\n    this.logger = logger.child({ sub: 'fs' });\n  }\n\n  public async init(): Promise<void> {\n    if (this.storagePlugin === null) {\n      const loaded = await this.loadStorage();\n      this.storagePlugin = isLegacyStoragePlugin(loaded)\n        ? wrapLegacyStoragePlugin(loaded, this.logger)\n        : loaded;\n      debug('storage plugin init');\n      await this.storagePlugin.init();\n      debug('storage plugin initialized');\n    } else {\n      this.logger.warn('storage plugin has been already initialized');\n    }\n  }\n\n  public getStoragePlugin(): any {\n    if (this.storagePlugin === null) {\n      throw errorUtils.getInternalError('storage plugin is not initialized');\n    }\n    return this.storagePlugin;\n  }\n\n  public async getSecret(config: Config): Promise<void> {\n    const secretKey = await this.storagePlugin.getSecret();\n    return this.storagePlugin.setSecret(config.checkSecretKey(secretKey));\n  }\n\n  private async loadStorage(): Promise<any> {\n    const Storage = await this.loadStorePlugin();\n    if (isNil(Storage)) {\n      assert(this.config.storage, 'CONFIG: storage path not defined');\n      debug('no custom storage found, loading default storage @verdaccio/local-storage');\n      const localStorage = new LocalDatabase(this.config, this.logger);\n      this.logger.info(\n        { name: '@verdaccio/local-storage', pluginCategory: PLUGIN_CATEGORY.STORAGE },\n        'plugin @{name} successfully loaded (@{pluginCategory})'\n      );\n      return localStorage;\n    }\n    return Storage;\n  }\n\n  private async loadStorePlugin(): Promise<any> {\n    const plugins = await asyncLoadPlugin(\n      this.config.store,\n      { config: this.config, logger: this.logger },\n      storageSanityCheck,\n      // legacyMergeConfigs: pass the full config as the plugin's first constructor\n      // arg, which legacy `(config, logger)` storage plugins require (matches the\n      // pre-migration 7.x behaviour; @verdaccio/store uses false and so cannot\n      // drive legacy plugins).\n      true,\n      this.config.server?.pluginPrefix ?? PLUGIN_PREFIX,\n      PLUGIN_CATEGORY.STORAGE\n    );\n    if (plugins.length > 1) {\n      this.logger.warn(\n        'more than one storage plugins has been detected, multiple storage are not supported, one will be selected automatically'\n      );\n    }\n    return head(plugins);\n  }\n}\n\n/**\n * `@verdaccio/store` Storage, with one change: it loads the storage plugin\n * through {@link CompatLocalStorage} so legacy callback plugins keep working.\n *\n * The base `init()` only creates its internal `LocalStorage` when\n * `this.localStorage` is still null; by pre-populating it we reuse the entire\n * rest of the upstream Storage implementation (including filter loading via\n * `super.init`) unchanged.\n */\nclass Storage extends BaseStorage {\n  public async init(config: Config): Promise<void> {\n    if (this.localStorage === null) {\n      const localStorage = new CompatLocalStorage(this.config, this.logger);\n      await localStorage.init();\n      await localStorage.getSecret(config);\n      // structurally compatible with the (non-exported) upstream LocalStorage\n      this.localStorage = localStorage as any;\n      debug('compat local storage initialized');\n    }\n    // localStorage is now non-null, so super.init only loads filter plugins\n    await super.init(config);\n  }\n}\n\nexport default Storage;\nexport { Storage };\n"],"mappings":";;;;;;;;;AAYA,IAAM,QAAQA,YAAW,mBAAmB;AAG5C,IAAM,gBAAiB,oBAA4B,WAAW;AAI9D,IAAM,sBAAsB,WAC1B,OAAO,QAAQ,sBAAsB;;;;;;;;;;AAWvC,IAAM,qBAAN,MAAyB;CACvB,gBAA4B;CAC5B;CACA;CAEA,YAAmB,QAAgB,QAAgB;EACjD,KAAK,SAAS;EACd,KAAK,SAAS,OAAO,MAAM,EAAE,KAAK,KAAK,CAAC;CAC1C;CAEA,MAAa,OAAsB;EACjC,IAAI,KAAK,kBAAkB,MAAM;GAC/B,MAAM,SAAS,MAAM,KAAK,YAAY;GACtC,KAAK,gBAAgB,sBAAsB,MAAM,IAC7C,wBAAwB,QAAQ,KAAK,MAAM,IAC3C;GACJ,MAAM,qBAAqB;GAC3B,MAAM,KAAK,cAAc,KAAK;GAC9B,MAAM,4BAA4B;EACpC,OACE,KAAK,OAAO,KAAK,6CAA6C;CAElE;CAEA,mBAA+B;EAC7B,IAAI,KAAK,kBAAkB,MACzB,MAAM,WAAW,iBAAiB,mCAAmC;EAEvE,OAAO,KAAK;CACd;CAEA,MAAa,UAAU,QAA+B;EACpD,MAAM,YAAY,MAAM,KAAK,cAAc,UAAU;EACrD,OAAO,KAAK,cAAc,UAAU,OAAO,eAAe,SAAS,CAAC;CACtE;CAEA,MAAc,cAA4B;EACxC,MAAM,UAAU,MAAM,KAAK,gBAAgB;EAC3C,IAAI,MAAM,OAAO,GAAG;GAClB,OAAO,KAAK,OAAO,SAAS,kCAAkC;GAC9D,MAAM,2EAA2E;GACjF,MAAM,eAAe,IAAI,cAAc,KAAK,QAAQ,KAAK,MAAM;GAC/D,KAAK,OAAO,KACV;IAAE,MAAM;IAA4B,gBAAgB,gBAAgB;GAAQ,GA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