Attempt at HTTP Server
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* Idempotent managed Holesail entry for the stock www static server port.
|
||||
*/
|
||||
import { bareHolesailEnvTruthy } from './bare-holesail-env.js'
|
||||
import {
|
||||
bareHolesailManagedReadState,
|
||||
bareHolesailManagedServiceIsRunning,
|
||||
bareHolesailManagedStartOne,
|
||||
bareHolesailManagedWriteState
|
||||
} from './bare-holesail-managed.js'
|
||||
|
||||
export const BARE_OS_WWW_HOLESAIL_CONNECTION_ID_PREFIX = 'bare-www-'
|
||||
|
||||
/**
|
||||
* @param {Record<string, string | undefined>} env
|
||||
* @param {number} port
|
||||
*/
|
||||
export function bareOsWwwHolesailConnectionId(port) {
|
||||
return `${BARE_OS_WWW_HOLESAIL_CONNECTION_ID_PREFIX}${port}`
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Record<string, string | undefined>} env
|
||||
*/
|
||||
export function bareOsWwwHolesailAutoEnabled(env) {
|
||||
const v = env.BARE_OS_WWW_HOLESAIL
|
||||
if (v === '0' || v === 'false') return false
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist a managed server tunnel for `port` and start it if bare-holesail is already active.
|
||||
* @param {Record<string, unknown>} ctx
|
||||
* @param {Record<string, string | undefined>} env
|
||||
* @param {number} port
|
||||
*/
|
||||
export async function ensureBareOsWwwHolesailTunnel(ctx, env, port) {
|
||||
if (!bareOsWwwHolesailAutoEnabled(env)) return
|
||||
const p =
|
||||
Number.isFinite(port) && port > 0 && port < 65536
|
||||
? Math.floor(port)
|
||||
: 8088
|
||||
const id = bareOsWwwHolesailConnectionId(p)
|
||||
const vfs = ctx.vfs
|
||||
if (!vfs || typeof vfs.readFile !== 'function' || typeof vfs.writeFile !== 'function') {
|
||||
return
|
||||
}
|
||||
if (!ctx.b4a || typeof ctx.b4a.from !== 'function') return
|
||||
|
||||
const { path, state } = await bareHolesailManagedReadState(ctx, env)
|
||||
/** @type {Record<string, unknown>} */
|
||||
const want = { server: true, port: p, enabled: true }
|
||||
const existing = state.connections[id]
|
||||
if (existing && typeof existing === 'object') {
|
||||
const ex = /** @type {Record<string, unknown>} */ (existing)
|
||||
const curPort = Number.parseInt(String(ex.port ?? ''), 10)
|
||||
const server = bareHolesailEnvTruthy(ex.server)
|
||||
const enabled =
|
||||
ex.enabled === undefined || ex.enabled === null || String(ex.enabled) === ''
|
||||
? true
|
||||
: bareHolesailEnvTruthy(ex.enabled)
|
||||
if (server && curPort === p && enabled) {
|
||||
if (bareHolesailManagedServiceIsRunning()) {
|
||||
try {
|
||||
await bareHolesailManagedStartOne(ctx, env, id)
|
||||
} catch {
|
||||
/* may already run */
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
state.connections[id] = want
|
||||
await bareHolesailManagedWriteState(ctx, env, state)
|
||||
void path
|
||||
if (bareHolesailManagedServiceIsRunning()) {
|
||||
try {
|
||||
await bareHolesailManagedStartOne(ctx, env, id)
|
||||
} catch (e) {
|
||||
try {
|
||||
ctx.console?.error?.(
|
||||
`[bare-os-www] holesail start ${id}: ${(e && /** @type {{ message?: string }} */ (e).message) || String(e)}`
|
||||
)
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user