Files
bare-operating-system/packages/bare-os-booter/lib/proc/bare-os-proc-blind-peer-relay-gate.js
T
2026-08-18 18:11:28 -04:00

93 lines
3.4 KiB
JavaScript

import { bareOsSwarmLifecycleSnapshot } from './bare-os-swarm-lifecycle.js'
/**
* Gate blind-peer / relay operator JSON under `/proc/bare_os` (non-secret sketches only).
* Default off so images do not advertise topology unless the operator opts in.
* @param {Record<string, string | undefined>} env
*/
export function bareOsProcBlindPeerRelayHintsExposed(env) {
const v = String(env?.BARE_OS_PROC_BLIND_PEER_RELAY_HINTS ?? '')
.trim()
.toLowerCase()
return v === '1' || v === 'true' || v === 'yes'
}
/**
* Default blind-relay / pairing / geo proc JSON when the operator has not opted in:
* schema 1, no swarm aggregates, topology redacted.
* @param {number} atMs
*/
export function bareOsProcBlindPeerRelayHintsMinimal(atMs) {
return {
schema: 1,
exposed: false,
operatorRedacted: true,
note:
'Set BARE_OS_PROC_BLIND_PEER_RELAY_HINTS=1 to expose blind relay / pairing / geo operator JSON (still non-secret env-injected sketches).',
atMs
}
}
/**
* Non-secret swarm-derived hints for blind-relay / pairing / geo proc JSON when hints are exposed.
* No peer keys, addresses, or topic material — only counts and lifecycle classification.
* @param {Record<string, string | undefined>} env
* @param {Array<{ id?: unknown }>} peers
* @param {{ hasSystemDrive?: boolean; hasPersonalDrive?: boolean; auxiliaryDriveCount?: number } | null} [diskHints]
*/
export function buildBareOsBlindRelaySwarmRuntime(env, peers, diskHints) {
const peerCount = Array.isArray(peers) ? peers.length : 0
const lifecycle = bareOsSwarmLifecycleSnapshot(env, peerCount)
/** @type {'isolated' | 'single_peer' | 'multi_peer'} */
let relayGeoTier = 'isolated'
if (peerCount === 1) relayGeoTier = 'single_peer'
else if (peerCount > 1) relayGeoTier = 'multi_peer'
const dh = diskHints && typeof diskHints === 'object' ? diskHints : null
const bpRaw = String(env?.BARE_OS_SWARM_PROTOMUX_BACKPRESSURE_COUNT ?? '')
.trim()
const bpN = Math.floor(Number(bpRaw))
const protomuxBackpressure =
Number.isFinite(bpN) && bpN >= 0
? {
schema: 1,
emitCountEstimate: Math.min(1e12, bpN),
note:
'Operator-injected aggregate only (e.g. from protomux drain/cork metrics); no wire payloads.'
}
: {
schema: 1,
emitCountEstimate: null,
note:
'Set BARE_OS_SWARM_PROTOMUX_BACKPRESSURE_COUNT to surface non-secret backpressure aggregate.'
}
return {
schema: 3,
peerCount,
relayGeoTier,
pairingSurfaceReady: peerCount > 0,
lifecycle,
protomuxBackpressure,
blindPeerStack: {
schema: 1,
blindPeering: true,
blindPeerRouter: true,
note:
'Aligns with Holepunch **blind-peering** / **blind-peer-router** repos: P2P-first pairing and relay hints only; no centralized broker.'
},
replicationSurfaces: {
schema: 1,
systemDriveOpen: dh ? !!dh.hasSystemDrive : null,
personalDriveOpen: dh ? !!dh.hasPersonalDrive : null,
auxiliaryDriveCount:
dh && typeof dh.auxiliaryDriveCount === 'number'
? dh.auxiliaryDriveCount
: null,
note: 'Boolean open flags and auxiliary count only; no drive keys.'
},
note:
'Schema 3 adds blindPeerStack pointer. Aggregates peer count, swarm lifecycle, and replication surface booleans; merge with env-injected operator JSON. No DHT keys or peer identities.',
atMs: Date.now()
}
}