/** * Build `/proc/bare_os/host_os.json` text (Holepunch `bare-os`-shaped snapshot). * * Always uses the **`bare-os`** package (ESM on Node and Bare). On Bare, **`bare-module`** * `createRequire` is used when needed; there is no `node:os` / `node:module` path. */ /** * @param {Record} env * @returns {Promise} */ export async function buildBareOsHostProcJsonText(env) { const rawInject = String(env.BARE_OS_HOST_OS_JSON || '').trim() if (rawInject) { try { const parsed = JSON.parse(rawInject) return `${JSON.stringify({ schema: 1, source: 'BARE_OS_HOST_OS_JSON', host: parsed, atMs: Date.now() })}\n` } catch { return `${JSON.stringify({ schema: 1, error: 'invalid BARE_OS_HOST_OS_JSON', atMs: Date.now() })}\n` } } const probe = env.BARE_OS_HOST_BARE_OS_PROC === '1' || env.BARE_OS_HOST_BARE_OS_PROC === 'true' if (!probe) { return `${JSON.stringify({ schema: 1, note: 'Set BARE_OS_HOST_BARE_OS_PROC=1 or inject BARE_OS_HOST_OS_JSON.', atMs: Date.now() })}\n` } try { let os if (typeof Bare !== 'undefined') { const bm = await import('bare-module') const createRequire = bm.createRequire || bm.default?.createRequire if (typeof createRequire !== 'function') { throw new Error('bare-module createRequire unavailable') } const _require = createRequire(import.meta.url) os = _require('bare-os') } else { const m = await import('bare-os') os = m.default ?? m } return `${JSON.stringify(hostPayloadFromBareOsLike(os, 'bare-os'))}\n` } catch { return `${JSON.stringify({ schema: 1, note: 'Host OS probe failed; use BARE_OS_HOST_OS_JSON on this runtime.', atMs: Date.now() })}\n` } } /** * @param {Record} os * @param {string} source */ function hostPayloadFromBareOsLike(os, source) { const snap = { platform: typeof os.platform === 'function' ? os.platform() : null, arch: typeof os.arch === 'function' ? os.arch() : null, hostname: typeof os.hostname === 'function' ? os.hostname() : null, release: typeof os.release === 'function' ? os.release() : null } let ifaces = null try { if (typeof os.networkInterfaces === 'function') { const n = os.networkInterfaces() if (n && typeof n === 'object') { ifaces = Object.keys(n) .slice(0, 32) .map((name) => ({ name, count: Array.isArray(n[name]) ? n[name].length : 0 })) } } } catch { ifaces = null } return { schema: 1, source, host: { ...snap, networkInterfaceNames: ifaces }, atMs: Date.now() } }