{"version":3,"file":"middleware.mjs","names":[],"sources":["../../src/api/middleware.ts"],"sourcesContent":["import buildDebug from 'debug';\nimport fs from 'fs';\nimport _ from 'lodash';\nimport path from 'path';\nimport { fileURLToPath } from 'url';\n\nimport type { Config } from '@verdaccio/types';\nimport { isURL } from '@verdaccio/url';\n\nimport { HTTP_STATUS } from '../lib/constants';\nimport type { $RequestExtend, $ResponseExtend } from '../types';\n\nconst debug = buildDebug('verdaccio:middleware:favicon');\n\nexport function serveFavicon(config: Config) {\n  return function (_req: $RequestExtend, res: $ResponseExtend) {\n    try {\n      const logoConf: string = config?.web?.favicon as string;\n      if (logoConf === '') {\n        debug('favicon disabled');\n        res.status(404);\n      } else if (!_.isEmpty(logoConf)) {\n        debug('custom favicon');\n        if (\n          isURL(logoConf, {\n            require_host: true,\n            require_valid_protocol: true,\n          })\n        ) {\n          debug('redirect to %o', logoConf);\n          res.redirect(logoConf);\n          return;\n        } else {\n          const faviconPath = path.normalize(logoConf);\n          debug('serving favicon from %o', faviconPath);\n          fs.access(faviconPath, fs.constants.R_OK, (err) => {\n            if (err) {\n              debug('no read permissions to read: %o, reason:', logoConf, err?.message);\n              return res.status(HTTP_STATUS.NOT_FOUND).end();\n            } else {\n              res.setHeader('content-type', 'image/x-icon');\n              fs.createReadStream(faviconPath).pipe(res);\n              debug('rendered custom ico');\n            }\n          });\n        }\n      } else {\n        res.setHeader('content-type', 'image/x-icon');\n        // __dirname only exists in the CJS build; the ESM build resolves from import.meta.url\n        const moduleDir =\n          typeof __dirname === 'undefined'\n            ? path.dirname(fileURLToPath(import.meta.url))\n            : __dirname;\n        fs.createReadStream(path.join(moduleDir, './web/html/favicon.ico')).pipe(res);\n        debug('rendered ico');\n      }\n    } catch (err: any) {\n      debug('error triggered, favicon not found %s', err?.message);\n      res.status(HTTP_STATUS.NOT_FOUND).end();\n    }\n  };\n}\n"],"mappings":";;;;;;;;AAYA,IAAM,QAAQ,YAAW,8BAA8B;AAEvD,SAAgB,aAAa,QAAgB;CAC3C,OAAO,SAAU,MAAsB,KAAsB;EAC3D,IAAI;GACF,MAAM,WAAmB,QAAQ,KAAK;GACtC,IAAI,aAAa,IAAI;IACnB,MAAM,kBAAkB;IACxB,IAAI,OAAO,GAAG;GAChB,OAAO,IAAI,CAAC,EAAE,QAAQ,QAAQ,GAAG;IAC/B,MAAM,gBAAgB;IACtB,IACE,MAAM,UAAU;KACd,cAAc;KACd,wBAAwB;IAC1B,CAAC,GACD;KACA,MAAM,kBAAkB,QAAQ;KAChC,IAAI,SAAS,QAAQ;KACrB;IACF,OAAO;KACL,MAAM,cAAc,KAAK,UAAU,QAAQ;KAC3C,MAAM,2BAA2B,WAAW;KAC5C,GAAG,OAAO,aAAa,GAAG,UAAU,OAAO,QAAQ;MACjD,IAAI,KAAK;OACP,MAAM,4CAA4C,UAAU,KAAK,OAAO;OACxE,OAAO,IAAI,OAAO,YAAY,SAAS,CAAC,CAAC,IAAI;MAC/C,OAAO;OACL,IAAI,UAAU,gBAAgB,cAAc;OAC5C,GAAG,iBAAiB,WAAW,CAAC,CAAC,KAAK,GAAG;OACzC,MAAM,qBAAqB;MAC7B;KACF,CAAC;IACH;GACF,OAAO;IACL,IAAI,UAAU,gBAAgB,cAAc;IAE5C,MAAM,YACJ,OAAO,cAAc,cACjB,KAAK,QAAQ,cAAc,OAAO,KAAK,GAAG,CAAC,IAC3C;IACN,GAAG,iBAAiB,KAAK,KAAK,WAAW,wBAAwB,CAAC,CAAC,CAAC,KAAK,GAAG;IAC5E,MAAM,cAAc;GACtB;EACF,SAAS,KAAU;GACjB,MAAM,yCAAyC,KAAK,OAAO;GAC3D,IAAI,OAAO,YAAY,SAAS,CAAC,CAAC,IAAI;EACxC;CACF;AACF"}