{"version":3,"file":"runtime-features.mjs","sources":["../../../lib/util/runtime-features.js"],"sourcesContent":["'use strict'\n\n/** @typedef {`node:${string}`} NodeModuleName */\n\n/** @type {Record<NodeModuleName, () => any>} */\nconst lazyLoaders = {\n  __proto__: null,\n  'node:crypto': () => require('node:crypto'),\n}\n\n/**\n * @param {NodeModuleName} moduleName\n * @returns {boolean}\n */\nfunction detectRuntimeFeatureByNodeModule (moduleName) {\n  try {\n    lazyLoaders[moduleName]()\n    return true\n  } catch (err) {\n    if (err.code !== 'ERR_UNKNOWN_BUILTIN_MODULE' && err.code !== 'ERR_NO_CRYPTO') {\n      throw err\n    }\n    return false\n  }\n}\n\nconst runtimeFeaturesAsNodeModule = /** @type {const} */ (['crypto', 'sqlite'])\n/** @typedef {typeof runtimeFeaturesAsNodeModule[number]} RuntimeFeatureByNodeModule */\n/** @typedef {RuntimeFeatureByNodeModule} Feature */\n\n/**\n * @param {Feature} feature\n * @returns {boolean}\n */\nfunction detectRuntimeFeature (feature) {\n  if (runtimeFeaturesAsNodeModule.includes(/** @type {RuntimeFeatureByNodeModule} */ (feature))) {\n    return detectRuntimeFeatureByNodeModule(`node:${feature}`)\n  }\n  throw new TypeError(`unknown feature: ${feature}`)\n}\n\n/**\n * @class\n * @name RuntimeFeatures\n */\nclass RuntimeFeatures {\n  /** @type {Map<Feature, boolean>} */\n  #map = new Map()\n\n  /**\n   * Clears all cached feature detections.\n   */\n  clear () {\n    this.#map.clear()\n  }\n\n  /**\n   * @param {Feature} feature\n   * @returns {boolean}\n   */\n  has (feature) {\n    return (\n      this.#map.get(feature) ?? this.#detectRuntimeFeature(feature)\n    )\n  }\n\n  /**\n   * @param {Feature} feature\n   * @param {boolean} value\n   */\n  set (feature, value) {\n    if (runtimeFeaturesAsNodeModule.includes(feature) === false) {\n      throw new TypeError(`unknown feature: ${feature}`)\n    }\n    this.#map.set(feature, value)\n  }\n\n  /**\n   * @param {Feature} feature\n   * @returns {boolean}\n   */\n  #detectRuntimeFeature (feature) {\n    const result = detectRuntimeFeature(feature)\n    this.#map.set(feature, result)\n    return result\n  }\n}\n\nconst instance = new RuntimeFeatures()\n\nmodule.exports.runtimeFeatures = instance\nmodule.exports.default = instance\n"],"names":["lazyLoaders","__proto__","require$$0","runtimeFeaturesAsNodeModule","instance","map","Map","clear","this","has","feature","get","detectRuntimeFeature","set","value","includes","TypeError","result","moduleName","err","code","detectRuntimeFeatureByNodeModule","runtimeFeatures","default"],"mappings":";;;AAKA,MAAMA,YAAc,CAClBC,UAAW,KACX,cAAe,IAAMC,QAmBjBC,gDAA2B,CAA0B,SAAU,UA8D/DC,SAAW;;;;;AA3CjB;;AAEEC,KAAO,IAAIC;;;OAKX,KAAAC,GACEC,MAAKH,IAAKE,OACd;;;;MAME,GAAAE,CAAKC,SACH,OACEF,MAAKH,IAAKM,IAAID,UAAYF,MAAKI,qBAAsBF,QAE3D;;;;MAME,GAAAG,CAAKH,QAASI,OACZ,IAAsD,IAAlDX,4BAA4BY,SAASL,SACvC,MAAM,IAAIM,UAAU,oBAAoBN,WAE1CF,MAAKH,IAAKQ,IAAIH,QAASI,MAC3B;;;;MAME,qBAAAF,CAAuBF,SACrB,MAAMO;;;;;;;AAhDV,SAA+BP,SAC7B,GAAIP,4BAA4BY,kDAAQ;;;;;AACtC,OAtBJ,SAA2CG,YACzC,IAEE,OADAlB,YAAYkB,eACL,CACX,CAAI,MAAOC,KACP,GAAiB,+BAAbA,IAAIC,MAAsD,kBAAbD,IAAIC,KACnD,MAAMD,IAER,OAAO,CACX,CACA,CAYWE,CAAiC,QAAQX,WAElD,MAAM,IAAIM,UAAU,oBAAoBN,UAC1C,CA2CmBE,CAAqBF,SAEpC,OADAF,MAAKH,IAAKQ,IAAIH,QAASO,QAChBA,MACX,UAKAK,gBAAAA,gBAAiClB,SACjCkB,gBAAAC,QAAyBnB"}