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

72 lines
1.9 KiB
JavaScript

/**
* Canonical booter boot-step identifiers emitted through {@link emitBooterBootStep}
* (lifecycle / diagnostics). Keep in sync with `emitBooterBootPhase` call sites in `index.js`.
*/
export const BARE_OS_BOOTER_BOOT_STEPS = Object.freeze([
'vfs',
'ctx',
'repl',
'initd',
'kernel_invoke'
])
/**
* @param {string} step
* @returns {boolean}
*/
export function isKnownBareOsBooterBootStep(step) {
return BARE_OS_BOOTER_BOOT_STEPS.includes(String(step))
}
/**
* @param {{
* bootReadyStateRef: { booterPhases: string[], booterStages: string[] },
* bootStartedMs: number,
* bootHrtimeNowNs?: (() => bigint) | null,
* bootEventSubs: Iterable<(ev: Record<string, unknown>) => unknown>,
* diagnosticsSubs: Iterable<(ev: Record<string, unknown>) => unknown>,
* sessionId: string,
* lifecycleSchemaVersion: string | number
* }} deps
*/
export function createBooterBootEmitter(deps) {
const {
bootReadyStateRef,
bootStartedMs,
bootHrtimeNowNs = null,
bootEventSubs,
diagnosticsSubs,
sessionId,
lifecycleSchemaVersion
} = deps
function emitBooterBootStep(step) {
const p = String(step)
const arr = bootReadyStateRef.booterPhases
const arrSt = bootReadyStateRef.booterStages
if (!arr.includes(p)) arr.push(p)
if (!arrSt.includes(p)) arrSt.push(p)
const ev = {
type: 'boot',
phase: 'booter:' + p,
ms: Date.now() - bootStartedMs,
ts: Date.now(),
sessionId,
lifecycleSchemaVersion,
...(bootHrtimeNowNs ? { monotonicNs: String(bootHrtimeNowNs()) } : {})
}
for (const fn of bootEventSubs) {
Promise.resolve(fn(ev)).catch(() => {})
}
for (const fn of diagnosticsSubs) {
Promise.resolve(fn({ ...ev, source: 'booter' })).catch(() => {})
}
}
function emitBooterBootPhase(phase) {
return emitBooterBootStep(phase)
}
return { emitBooterBootStep, emitBooterBootPhase }
}