{"version":3,"sources":["../src/index.ts"],"sourcesContent":["// An awesome idea originally by\n// https://github.com/svitejs/vite-plugin-qrcode (MIT)\n\nimport { on } from \"@zerva/core\"\nimport \"@zerva/http\"\nimport type { Server } from \"http\"\nimport { AddressInfo } from \"net\"\nimport os from \"os\"\nimport qr from \"qrcode-terminal\"\nimport { Logger } from \"zeed\"\n\nconst log = Logger(\"zerva:qrcode\")\n\nfunction logQrcode(http: Server) {\n  const networkUrls = getNetworkUrls(http)\n  if (networkUrls.length === 0) {\n    console.info(\"Zerva: No external server IP available.\")\n    return\n  }\n  console.info(\"Zerva: Visit page on mobile using QRCode\")\n  for (const url of networkUrls) {\n    qr.generate(url, { small: true }, (result) => {\n      console.info(`\\n  ${cyan(url)}\\n  ${result.replace(/\\n/g, \"\\n  \")}\\n`)\n    })\n  }\n}\n\nfunction cyan(str: string): string {\n  return `\\x1b[36m${str}\\x1b[0m`\n}\n\n// // Referenced from https://github.com/vitejs/vite/blob/77447496704e61cdb68b5788d8d79f19a2d895f1/packages/vite/src/node/logger.ts#L143\nfunction getNetworkUrls(server: Server): string[] {\n  const address = server?.address()\n  if (!isAddressInfo(address)) return []\n  // const hostname = resolveHostname(server.config.server.host)\n  // if (hostname.host === \"127.0.0.1\") return []\n  const protocol = \"http\"\n  const port = address.port\n\n  return Object.values(os.networkInterfaces())\n    .flatMap((nInterface) => nInterface ?? [])\n    .filter(\n      (detail) =>\n        detail &&\n        detail.address &&\n        // @ts-ignore\n        (detail.family === \"IPv4\" || detail.family === 4) &&\n        !detail.address.includes(\"127.0.0.1\")\n    )\n    .map((detail) => `${protocol}://${detail.address}:${port}`)\n}\n\nfunction isAddressInfo(v: any): v is AddressInfo {\n  return v.address\n}\n\n// // Copied from https://github.com/vitejs/vite/blob/77447496704e61cdb68b5788d8d79f19a2d895f1/packages/vite/src/node/utils.ts#L531\n// function resolveHostname(optionsHost: string | boolean | undefined): Hostname {\n//   let host: string | undefined\n//   if (\n//     optionsHost === undefined ||\n//     optionsHost === false ||\n//     optionsHost === \"localhost\"\n//   ) {\n//     // Use a secure default\n//     host = \"127.0.0.1\"\n//   } else if (optionsHost === true) {\n//     // If passed --host in the CLI without arguments\n//     host = undefined // undefined typically means 0.0.0.0 or :: (listen on all IPs)\n//   } else {\n//     host = optionsHost\n//   }\n\n//   // Set host name to localhost when possible, unless the user explicitly asked for '127.0.0.1'\n//   const name =\n//     (optionsHost !== \"127.0.0.1\" && host === \"127.0.0.1\") ||\n//     host === \"0.0.0.0\" ||\n//     host === \"::\" ||\n//     host === undefined\n//       ? \"localhost\"\n//       : host\n\n//   return { host, name }\n// }\n\n// interface Hostname {\n//   // undefined sets the default behaviour of server.listen\n//   host: string | undefined\n//   // resolve to localhost when possible\n//   name: string\n// }\n\nexport function useQrCode() {\n  on(\"httpRunning\", ({ http }) => {\n    logQrcode(http)\n  })\n}\n"],"mappings":"0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,eAAAE,IAAA,eAAAC,EAAAH,GAGA,IAAAI,EAAmB,uBACnBC,EAAO,uBAGPC,EAAe,mBACfC,EAAe,gCACfC,EAAuB,gBAEjBC,KAAM,UAAO,cAAc,EAEjC,SAASC,EAAUC,EAAc,CAC/B,IAAMC,EAAcC,EAAeF,CAAI,EACvC,GAAIC,EAAY,SAAW,EAAG,CAC5B,QAAQ,KAAK,yCAAyC,EACtD,OAEF,QAAQ,KAAK,0CAA0C,EACvD,QAAWE,KAAOF,EAChB,EAAAG,QAAG,SAASD,EAAK,CAAE,MAAO,EAAK,EAAIE,GAAW,CAC5C,QAAQ,KAAK;AAAA,IAAOC,EAAKH,CAAG;AAAA,IAAQE,EAAO,QAAQ,MAAO;AAAA,GAAM;AAAA,CAAK,CACvE,CAAC,CAEL,CAEA,SAASC,EAAKC,EAAqB,CACjC,MAAO,WAAWA,UACpB,CAGA,SAASL,EAAeM,EAA0B,CAChD,IAAMC,EAAUD,GAAQ,QAAQ,EAChC,GAAI,CAACE,EAAcD,CAAO,EAAG,MAAO,CAAC,EAGrC,IAAME,EAAW,OACXC,EAAOH,EAAQ,KAErB,OAAO,OAAO,OAAO,EAAAI,QAAG,kBAAkB,CAAC,EACxC,QAASC,GAAeA,GAAc,CAAC,CAAC,EACxC,OACEC,GACCA,GACAA,EAAO,UAENA,EAAO,SAAW,QAAUA,EAAO,SAAW,IAC/C,CAACA,EAAO,QAAQ,SAAS,WAAW,CACxC,EACC,IAAKA,GAAW,GAAGJ,OAAcI,EAAO,WAAWH,GAAM,CAC9D,CAEA,SAASF,EAAcM,EAA0B,CAC/C,OAAOA,EAAE,OACX,CAsCO,SAASzB,GAAY,IAC1B,MAAG,cAAe,CAAC,CAAE,KAAAS,CAAK,IAAM,CAC9BD,EAAUC,CAAI,CAChB,CAAC,CACH","names":["src_exports","__export","useQrCode","__toCommonJS","import_core","import_http","import_os","import_qrcode_terminal","import_zeed","log","logQrcode","http","networkUrls","getNetworkUrls","url","qr","result","cyan","str","server","address","isAddressInfo","protocol","port","os","nInterface","detail","v"]}