This commit is contained in:
Raven Scott
2026-04-22 18:57:40 -04:00
parent 93198d4fa6
commit c4e60fd83e
3 changed files with 91 additions and 3 deletions
@@ -149,6 +149,48 @@ export function bareHolesailManagedNormalizeEntry(raw) {
return { ok: true, entry }
}
/**
* Strip Bare-managed fields (e.g. `enabled`) and pass only upstream holesail constructor options.
* @param {Record<string, unknown>} entry normalized row from {@link bareHolesailManagedNormalizeEntry}
* @returns {Record<string, unknown>}
*/
export function bareHolesailManagedEntryToOpts(entry) {
const e = /** @type {Record<string, unknown>} */ (entry)
/** @type {Record<string, unknown>} */
const opts = {
server: !!e.server,
client: !!e.client
}
if (e.port != null && e.port !== '') opts.port = e.port
const host = String(e.host ?? '').trim()
if (host) opts.host = host
const key = String(e.key ?? '').trim()
if (key) opts.key = key
if (e.secure !== undefined && e.secure !== null && String(e.secure) !== '')
opts.secure = bareHolesailEnvTruthy(e.secure)
if (e.udp !== undefined && e.udp !== null && String(e.udp) !== '')
opts.udp = bareHolesailEnvTruthy(e.udp)
if (e.log !== undefined && e.log !== null && e.log !== '') {
if (typeof e.log === 'number') opts.log = e.log
else if (/^\d+$/.test(String(e.log)))
opts.log = Number.parseInt(String(e.log), 10)
else opts.log = bareHolesailEnvTruthy(e.log)
}
return opts
}
/**
* Compare persisted shareable URL / key string to live `hs.info.url` (case-insensitive trim).
* @param {string} persisted
* @param {string} live
*/
export function bareHolesailManagedPersistedUrlMatchesLive(persisted, live) {
const p = String(persisted ?? '').trim().toLowerCase()
const l = String(live ?? '').trim().toLowerCase()
if (!p || !l) return false
return p === l
}
/**
* @param {unknown} parsed
*/
@@ -333,13 +375,23 @@ export async function bareHolesailManagedSyncPersistedServerKey(ctx, env, id) {
* @param {Record<string, unknown>} entry
*/
async function startManagedInstance(ctx, env, id, entry) {
if (managedInstances.has(id)) return
const norm = bareHolesailManagedNormalizeEntry(entry)
if (!norm.ok) throw new Error(norm.err)
if (!norm.entry.enabled) return
if (managedInstances.has(id)) {
const persisted = String(norm.entry.key ?? '').trim()
if (norm.entry.server && persisted) {
const live = bareHolesailManagedRuntimeUrl(id).trim()
if (bareHolesailManagedPersistedUrlMatchesLive(persisted, live)) return
await stopManagedInstance(ctx, id)
} else {
return
}
}
const Holesail = await loadHolesailConstructor(ctx)
const hs = new Holesail(norm.entry)
const hs = new Holesail(bareHolesailManagedEntryToOpts(norm.entry))
await hs.ready()
managedInstances.set(id, { hs, entry: norm.entry })
if (norm.entry.server) {
+2 -1
View File
@@ -176,7 +176,8 @@ export async function runHolesailCli(ctx, argv) {
const norm = bareHolesailManagedNormalizeEntry(raw)
const en = norm.ok ? String(!!norm.entry.enabled) : '?'
const live = bareHolesailManagedRuntimeRunning(id)
const url = live ? bareHolesailManagedRuntimeUrl(id) : ''
const persistedUrl = norm.ok ? String(norm.entry.key ?? '').trim() : ''
const url = persistedUrl || (live ? bareHolesailManagedRuntimeUrl(id) : '')
const mode =
norm.ok && norm.entry.server ? 'server' : norm.ok && norm.entry.client ? 'client' : '?'
const err = norm.ok ? '' : ` INVALID: ${norm.err}`
@@ -1,8 +1,10 @@
import test from 'brittle'
import {
BARE_HOLESAIL_STATE_USER,
bareHolesailManagedEntryToOpts,
bareHolesailManagedEnvForConnectionId,
bareHolesailManagedNormalizeEntry,
bareHolesailManagedPersistedUrlMatchesLive,
bareHolesailManagedStatePath,
bareHolesailManagedValidateId
} from './lib/bare-holesail-managed.js'
@@ -62,6 +64,39 @@ test('bareHolesailManagedNormalizeEntry client needs key', (t) => {
t.absent(r.ok)
})
test('bareHolesailManagedEntryToOpts strips enabled and passes holesail fields', (t) => {
const r = bareHolesailManagedNormalizeEntry({
server: true,
port: 8088,
host: '127.0.0.1',
key: 'hs://0000abc',
secure: true,
udp: true,
log: 0,
enabled: false
})
t.ok(r.ok)
const o = bareHolesailManagedEntryToOpts(r.entry)
t.is(o.server, true)
t.is(o.client, false)
t.is(o.port, 8088)
t.is(o.host, '127.0.0.1')
t.is(o.key, 'hs://0000abc')
t.is(o.secure, true)
t.is(o.udp, true)
t.is(o.log, 0)
t.absent(Object.prototype.hasOwnProperty.call(o, 'enabled'))
})
test('bareHolesailManagedPersistedUrlMatchesLive', (t) => {
const u = 'hs://0000aaaabbbbccccddddeeeeffffgggg'
t.ok(bareHolesailManagedPersistedUrlMatchesLive(u, u))
t.ok(bareHolesailManagedPersistedUrlMatchesLive(` ${u} `, u.toUpperCase()))
t.absent(bareHolesailManagedPersistedUrlMatchesLive(u, 'hs://0000other'))
t.absent(bareHolesailManagedPersistedUrlMatchesLive(u, ''))
t.absent(bareHolesailManagedPersistedUrlMatchesLive('', u))
})
test('holesailCliParseAdd', (t) => {
const r = holesailCliParseAdd(['t', '--server', '--port', '9000'])
t.ok(r.ok)