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

135 lines
4.1 KiB
JavaScript

/**
* Booter /proc snapshot helpers used while constructing executeKernel ctx.
*/
import {
bareOsProcBlindPeerRelayHintsExposed,
buildBareOsBlindRelaySwarmRuntime
} from './bare-os-proc-blind-peer-relay-gate.js'
import { bareOsChatMuxEnabled } from './bare-os-chat-service.js'
import { bareOsMeshdropMuxEnabled } from './bare-os-meshdrop-service.js'
/**
* @param {{
* interactiveCtxRef: { ctx: Record<string, unknown> | null },
* disk: Record<string, unknown>,
* shellEnv: Record<string, string | undefined>,
* hostEnv?: Record<string, string | undefined> | null
* }} deps
*/
export function createBooterProcHelpers(deps) {
const { interactiveCtxRef, disk, shellEnv, hostEnv = null } = deps
function bareOsCollectLogicalFdRows() {
const c = interactiveCtxRef.ctx
const o = c?.bareOsLogicalFds
if (!o || typeof o !== 'object') return []
return Object.keys(o)
.filter((k) => /^[0-9]+$/.test(k) && k !== '0' && k !== '1' && k !== '2')
.sort((a, b) => Number(a) - Number(b))
.map((fdNum) => ({
fd: Number(fdNum),
target: String(/** @type {Record<string, string>} */ (o)[fdNum] ?? '')
}))
}
function bareOsLiveShellJobsAndIpcStats() {
const live = interactiveCtxRef.ctx
const shellJobs =
live &&
live.shellBackgroundJobs &&
typeof live.shellBackgroundJobs === 'object' &&
Array.isArray(live.shellBackgroundJobs.list)
? live.shellBackgroundJobs.list
: []
let ipcStats
try {
ipcStats =
live && live.bareOsIpc && typeof live.bareOsIpc.stats === 'function'
? live.bareOsIpc.stats()
: undefined
} catch {
ipcStats = undefined
}
return { shellJobs, ipcStats }
}
function bareOsBlindRelayRuntimeHintsForProc() {
if (!bareOsProcBlindPeerRelayHintsExposed(shellEnv)) return undefined
const peers = disk.peers ? [...disk.peers] : []
return {
swarm: buildBareOsBlindRelaySwarmRuntime(shellEnv, peers, {
hasSystemDrive: !!disk.drive,
hasPersonalDrive: !!disk.personalDrive,
auxiliaryDriveCount: Array.isArray(disk.auxiliaryDrives)
? disk.auxiliaryDrives.length
: 0
})
}
}
/**
* Same object as `/proc/bare_os/chat.json` (single source for VFS + ctx hook).
* @returns {Record<string, unknown>}
*/
function bareOsChatProcSnapshotRecord() {
const svc = disk.bareOsChatService
if (!svc) {
return {
schema: 1,
note: 'Swarm chat is off when BARE_OS_PROTOMUX_CHAT_CHANNEL=0/false/off (stock default is on).'
}
}
return {
schema: 1,
atMs: Date.now(),
protocol: svc.PROTOCOL_CHAT_CHANNEL_NAME,
metrics: svc.snapshotMetrics(),
protomuxChatRxTotal:
typeof disk.protomuxChatChannelRxTotal === 'number'
? disk.protomuxChatChannelRxTotal
: 0,
swarmPeers:
disk.peers && typeof disk.peers.size === 'number' ? disk.peers.size : 0,
muxEnabled:
bareOsChatMuxEnabled(shellEnv) || bareOsChatMuxEnabled(hostEnv || {})
}
}
/**
* Same object as `/proc/bare_os/meshdrop.json`.
* @returns {Record<string, unknown>}
*/
function bareOsMeshdropProcSnapshotRecord() {
const svc = disk.bareOsMeshdropService
if (!svc) {
return {
schema: 1,
note: 'Meshdrop channel is off when BARE_OS_PROTOMUX_MESHDROP_CHANNEL=0/false/off (stock default is on).'
}
}
return {
schema: 1,
atMs: Date.now(),
protocol: svc.PROTOCOL_MESHDROP_CHANNEL_NAME,
metrics: svc.snapshotMetrics(),
protomuxMeshdropRxTotal:
typeof disk.protomuxMeshdropChannelRxTotal === 'number'
? disk.protomuxMeshdropChannelRxTotal
: 0,
swarmPeers:
disk.peers && typeof disk.peers.size === 'number' ? disk.peers.size : 0,
muxEnabled:
bareOsMeshdropMuxEnabled(shellEnv) ||
bareOsMeshdropMuxEnabled(hostEnv || {})
}
}
return {
bareOsCollectLogicalFdRows,
bareOsLiveShellJobsAndIpcStats,
bareOsBlindRelayRuntimeHintsForProc,
bareOsChatProcSnapshotRecord,
bareOsMeshdropProcSnapshotRecord
}
}