{"version":3,"file":"loader.mjs","sources":["../src/libc.ts","../src/utils/logger.ts","../src/manifest.ts","../src/loader.ts"],"sourcesContent":["import fs from \"fs\"\n\nexport function detectLibc(os: typeof process.platform) {\n  if (os === \"linux\") {\n    if (fs.existsSync(\"/etc/alpine-release\")) {\n      return \"musl\"\n    }\n    return \"glibc\"\n  } else if (os === \"darwin\") {\n    return \"libc\"\n  } else if (os === \"win32\") {\n    return \"msvc\"\n  }\n  return \"unknown\"\n}\n","/* eslint-disable class-methods-use-this */\n\nclass Logger {\n  private level: number = 2\n\n  /**\n   * Set the log level\n   * @param level - The log level\n   *\n   * trace - 4\n   * debug - 3\n   * info - 2\n   * warn - 1\n   * error - 0\n   * off - -1\n   *\n   * @default \"info\"\n   */\n  setLevel(level: \"trace\" | \"debug\" | \"info\" | \"warn\" | \"error\" | \"off\" = \"info\") {\n    this.level =\n      level === \"trace\"\n        ? 4\n        : level === \"debug\"\n          ? 3\n          : level === \"info\"\n            ? 2\n            : level === \"warn\"\n              ? 1\n              : level === \"error\"\n                ? 0\n                : -1\n  }\n\n  error(...args: unknown[]) {\n    if (this.level >= 0) {\n      console.error(\"\\x1b[31m[ERROR cmake-ts]\\x1b[0m\", ...args)\n    }\n  }\n\n  warn(...args: unknown[]) {\n    if (this.level >= 1) {\n      console.warn(\"\\x1b[33m[WARN cmake-ts]\\x1b[0m\", ...args)\n    }\n  }\n\n  info(...args: unknown[]) {\n    if (this.level >= 2) {\n      console.info(\"\\x1b[32m[INFO cmake-ts]\\x1b[0m\", ...args)\n    }\n  }\n\n  log(...args: unknown[]) {\n    return this.info(...args)\n  }\n\n  debug(...args: unknown[]) {\n    if (this.level >= 3) {\n      console.debug(\"\\x1b[34m[DEBUG cmake-ts]\\x1b[0m\", ...args)\n    }\n  }\n\n  trace(...args: unknown[]) {\n    if (this.level >= 4) {\n      console.trace(\"\\x1b[34m[TRACE cmake-ts]\\x1b[0m\", ...args)\n    }\n  }\n}\n\nexport const logger = new Logger()\n\n/**\n * Get the error string.\n *\n * @param error - The error.\n * @returns The error string.\n */\nexport function errorString(error: unknown) {\n  return error instanceof Error && error.stack !== undefined ? error.stack : String(error)\n}\n","import fs from \"fs\"\nimport path from \"path\"\nimport type { BuildConfiguration } from \"./config-types.d\"\nimport { detectLibc } from \"./libc.js\"\nimport { errorString, logger } from \"./utils/logger.js\"\n\n/**\n * A class that represents the manifest file.\n */\n\nexport class Manifest {\n  private buildDir: string\n  private manifest: Record<string, string>\n\n  /**\n   * Create a new manifest from the build directory.\n   *\n   * @param buildDir - The directory containing the build artifacts. It should contain a `manifest.json` file.\n   */\n  constructor(buildDir: string) {\n    this.buildDir = buildDir\n    const manifestPath = path.resolve(buildDir, \"manifest.json\")\n    if (!fs.existsSync(manifestPath)) {\n      throw new Error(`Manifest file not found at ${manifestPath}`)\n    }\n    try {\n      logger.debug(`Reading and parsing manifest file at ${manifestPath}`)\n      const manifestContent = fs.readFileSync(manifestPath, \"utf-8\")\n      this.manifest = JSON.parse(manifestContent) as Record<string, string>\n    } catch (err) {\n      throw new Error(`Failed to read and parse the manifest file at ${manifestPath}: ${errorString(err)}`)\n    }\n  }\n\n  /**\n   * Find the compatible configs for the current runtime.\n   *\n   * @param platform - The platform of the current runtime.\n   * @returns The compatible configs.\n   */\n  findCompatibleConfigs(platform: Platform) {\n    // get the configs from the manifest\n    const configKeys = this.getConfigKeys()\n\n    // find the compatible addons (config -> addon path)\n    const compatibleAddons: [BuildConfiguration, string][] = []\n    for (const configKey of configKeys) {\n      try {\n        // parse the config key\n        const config = this.getConfig(configKey)\n\n        // check if the config is compatible with the current runtime\n        if (config.os !== platform.os || config.arch !== platform.arch || config.libc !== platform.libc) {\n          logger.debug(`Config ${configKey} is not compatible with the current runtime. Skipping...`)\n          continue\n        }\n\n        // get the relative path to the addon\n        const addonRelativePath = this.getAddonPath(configKey)\n\n        // add the addon to the list of compatible addons\n        compatibleAddons.push([config, path.resolve(this.buildDir, addonRelativePath)])\n      } catch (err) {\n        logger.warn(`Failed to parse config ${configKey}: ${errorString(err)}`)\n      }\n    }\n\n    if (compatibleAddons.length === 0) {\n      throw new Error(\n        `No compatible zeromq.js addon found for ${platform.os} ${platform.arch} ${platform.libc}. The candidates were:\\n${configKeys.join(\n          \"\\n\",\n        )}`,\n      )\n    }\n\n    // sort the compatible addons by the ABI in descending order\n    compatibleAddons.sort(([c1, _p1], [c2, _p2]) => {\n      return (c2.abi ?? 0) - (c1.abi ?? 0)\n    })\n\n    return compatibleAddons\n  }\n\n  /**\n   * Get the config keys from the manifest in the string format.\n   *\n   * @returns The config keys in the string format.\n   */\n  getConfigKeys() {\n    return Object.keys(this.manifest)\n  }\n\n  /**\n   * Get the config from the manifest.\n   *\n   * @param configKey - The key of the config.\n   * @returns The config.\n   */\n  // eslint-disable-next-line class-methods-use-this\n  getConfig(configKey: string) {\n    return JSON.parse(configKey) as BuildConfiguration\n  }\n\n  /**\n   * Get the addon path from the manifest.\n   *\n   * @param configKey - The key of the config.\n   * @returns The addon path.\n   */\n  getAddonPath(configKey: string) {\n    return this.manifest[configKey]\n  }\n}\n/**\n * Get the platform of the current runtime.\n *\n * @returns The platform of the current runtime.\n */\nexport function getPlatform(): Platform {\n  return {\n    os: process.platform,\n    arch: process.arch,\n    libc: detectLibc(process.platform),\n  }\n}\n/**\n * The platform of the current runtime.\n */\n\nexport type Platform = {\n  os: string\n  arch: string\n  libc: string\n}\n","import { createRequire } from \"module\"\nimport { Manifest, getPlatform } from \"./manifest.js\"\nimport { errorString, logger } from \"./utils/logger.js\"\n\n/**\n * Find the addon for the current runtime.\n *\n * @param buildDir - The directory containing the build artifacts. It should contain a `manifest.json` file.\n * @returns The addon for the current runtime.\n */\nexport function loadAddon<Addon>(buildDir: string): Addon | undefined {\n  let addon: undefined | Addon = undefined\n  try {\n    // detect the platform of the current runtime\n    const platform = getPlatform()\n\n    // read the manifest file\n    const manifest = new Manifest(buildDir)\n\n    // find the compatible configs for the current runtime\n    const compatibleAddons = manifest.findCompatibleConfigs(platform)\n\n    // require function polyfill for ESM or CommonJS\n    const requireFn = typeof require === \"function\" ? require : createRequire(import.meta.url)\n\n    // try loading each available addon in order\n    for (const [_config, addonPath] of compatibleAddons) {\n      try {\n        logger.debug(`Loading addon at ${addonPath}`)\n        addon = requireFn(addonPath)\n        break\n      } catch (err) {\n        logger.warn(`Failed to load addon at ${addonPath}: ${errorString(err)}\\nTrying others...`)\n      }\n    }\n  } catch (err) {\n    throw new Error(`Failed to load zeromq.js addon.node: ${errorString(err)}`)\n  }\n\n  if (addon === undefined) {\n    throw new Error(\"No compatible zeromq.js addon found\")\n  }\n\n  return addon\n}\n"],"names":["detectLibc","os","fs","Logger","level","args","logger","errorString","error","Manifest","buildDir","manifestPath","path","manifestContent","err","platform","configKeys","compatibleAddons","configKey","config","addonRelativePath","c1","_p1","c2","_p2","getPlatform","loadAddon","addon","requireFn","createRequire","_config","addonPath"],"mappings":"6EAEO,SAASA,EAAWC,EAA6B,CACtD,OAAIA,IAAO,QACLC,EAAG,WAAW,qBAAqB,EAC9B,OAEF,QACED,IAAO,SACT,OACEA,IAAO,QACT,OAEF,SACT,CCZA,MAAME,CAAO,CACH,MAAgB,EAexB,SAASC,EAA+D,OAAQ,CAC9E,KAAK,MACHA,IAAU,QACN,EACAA,IAAU,QACR,EACAA,IAAU,OACR,EACAA,IAAU,OACR,EACAA,IAAU,QACR,EACA,EAAA,CAGhB,SAASC,EAAiB,CACpB,KAAK,OAAS,GACR,QAAA,MAAM,kCAAmC,GAAGA,CAAI,CAC1D,CAGF,QAAQA,EAAiB,CACnB,KAAK,OAAS,GACR,QAAA,KAAK,iCAAkC,GAAGA,CAAI,CACxD,CAGF,QAAQA,EAAiB,CACnB,KAAK,OAAS,GACR,QAAA,KAAK,iCAAkC,GAAGA,CAAI,CACxD,CAGF,OAAOA,EAAiB,CACf,OAAA,KAAK,KAAK,GAAGA,CAAI,CAAA,CAG1B,SAASA,EAAiB,CACpB,KAAK,OAAS,GACR,QAAA,MAAM,kCAAmC,GAAGA,C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