Files
bare-operating-system/packages/bare-os-booter/scripts/holesail-managed-seed-state.mjs
T
2026-08-18 18:11:34 -04:00

105 lines
3.0 KiB
JavaScript

#!/usr/bin/env bare
/**
* Exercise managed holesail seed persistence without the full OS.
*
* Run from package root:
* bare scripts/holesail-managed-seed-state.mjs
*
* Expect: after ensure, state includes a 64-hex `seed` OR a migrated z32 `seed`
* from an `hs://…` URL; ctor opts prefer `seed` over duplicate `hs://` `key`.
*/
import b4a from 'b4a'
import {
BARE_HOLESAIL_STATE_USER,
bareHolesailManagedEnsureServerSeedPersisted,
bareHolesailManagedNewHolesailOpts,
bareHolesailManagedReadState,
bareHolesailManagedSeedLooksValid
} from '../lib/p2p/bare-holesail-managed.js'
/**
* @param {typeof import('b4a').default} b4aMod
* @returns {{ readFile: Function, writeFile: Function, mkdir: Function }}
*/
function memoryFs(b4aMod, initialPath, initialText) {
/** @type {Map<string, string>} */
const files = new Map([[initialPath, initialText]])
return {
/**
* @param {string} p
* @param {string | Uint8Array} data
*/
async writeFile(p, data) {
const s =
typeof data === 'string'
? data
: b4aMod.toString(
data instanceof Uint8Array ? data : new Uint8Array(data),
'utf8'
)
files.set(p, s)
},
/** @param {string} p */
async readFile(p) {
const b = files.get(p)
if (b === undefined) throw Object.assign(new Error('ENOENT'), { code: 'ENOENT' })
return b4aMod.from(b)
},
mkdir: async () => {}
}
}
const hsOnly = {
version: 1,
connections: {
'bare-www-test': {
server: true,
port: 8088,
host: '127.0.0.1',
enabled: true,
key: 'hs://0000xkzftmerxjhuxdztn1zxoxb98xop65rthdf9s96yxxu6zyt55odo'
}
}
}
const vfs = memoryFs(b4a, BARE_HOLESAIL_STATE_USER, JSON.stringify(hsOnly, null, 2) + '\n')
/** @type {Record<string, unknown>} */
const ctx = {
b4a,
vfs,
console
}
const env = /** @type {Record<string, string | undefined>} */ ({ HOME: '/home/u' })
await bareHolesailManagedEnsureServerSeedPersisted(ctx, env, 'bare-www-test')
const { state } = await bareHolesailManagedReadState(ctx, env)
const row = /** @type {Record<string, unknown> | undefined} */ (
state.connections['bare-www-test']
)
if (!row) {
console.error(
'[holesail-managed-seed-state] FAIL: missing connection after ensure; keys=%s',
JSON.stringify(Object.keys(state.connections))
)
process.exit(1)
}
const opts = bareHolesailManagedNewHolesailOpts(row, env)
console.log('[holesail-managed-seed-state] persisted row:', JSON.stringify(row, null, 2))
console.log('[holesail-managed-seed-state] ctor opts.key:', opts.key)
const seedStr = row.seed != null ? String(row.seed).trim() : ''
if (!seedStr) {
console.error('FAIL: seed missing after bareHolesailManagedEnsureServerSeedPersisted')
process.exitCode = 1
} else if (!bareHolesailManagedSeedLooksValid(seedStr)) {
console.log(
'[holesail-managed-seed-state] OK (migrated non-hex persistent seed — z32 suffix)'
)
} else {
console.log('[holesail-managed-seed-state] OK (64-hex seed present)')
}