~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/atlibs-fedproxy

fedproxy and ingress modules for atlibs: SSH public keys on your PDS, did:key ingress; pages and web tiles

0
0
main
55 lines · 2.7 KB Raw History
import { IngressCubit } from './ingress-cubit.js'
import { createPluginContext } from 'atlibs/plugin-context/index.js'
import { loadModule } from 'atlibs/run-module/index.js'

// Wiring only. By default the page runs the `cargo wasm` build through the modules/ symlinks;
// ?module=<handle>/<name> (or at://...) runs a published module record instead (its did:key is then keyed by
// the record's at:// URI).
// ?maxRequests=&lifetimeMs=&quietMs= fill the form, and &serve starts serving on load (a headless
// tab: each log line also goes to the console): at once for the local module, after a confirm for a record.
const QUERY = new URLSearchParams(location.search);
const MODULE = QUERY.get('module');
const FIELDS = ['maxRequests', 'lifetimeMs', 'quietMs'];
const WASM = new URL('./modules/ingress.wasm', import.meta.url);

const ctx = createPluginContext({ id: 'ingress', settingsSchema: { didkeys: { type: 'secret' } } });

function localModule() {
	return Promise.all([
		fetch(WASM).then((r) => r.arrayBuffer()),
		fetch(new URL('./modules/ingress.json', import.meta.url)).then((r) => r.json()),
	]).then(([wasm, manifest]) => ({ module: new Uint8Array(wasm), calls: manifest.calls, didkey: manifest.didkey }));
}

const cubit = new IngressCubit({ ctx, module: MODULE ? loadModule(ctx, MODULE) : localModule(), id: MODULE ?? WASM.href });
const $ = (id) => document.getElementById(id);

function render(state) {
	$('did').innerText = state.did ?? '...';
	$('url').innerText = state.url ?? '...';
	if (state.url) $('url').href = state.url;
	$('status').innerText = state.status;
	$('served').innerText = state.served;
	$('drops').innerText = state.drops;
	$('error').innerText = state.error ?? '';
	$('start').disabled = state.status !== 'idle' && state.status !== 'done' && state.status !== 'error';
	$('stop').disabled = state.status !== 'running';
	$('log').replaceChildren(...state.log.map((line) => Object.assign(document.createElement('li'), { innerText: line })));
}

document.addEventListener('DOMContentLoaded', async () => {
	cubit.subscribe(render);
	$('serve-form').onsubmit = (e) => {
		e.preventDefault();
		const input = Object.fromEntries([...new FormData(e.target)].filter(([, v]) => String(v).trim() !== ''));
		cubit.start(input);
	};
	$('stop').onclick = () => cubit.stop();
	for (const field of FIELDS) if (QUERY.has(field)) $('serve-form')[field].value = QUERY.get(field);
	await cubit.init();
	if (QUERY.has('serve') && (await cubit.mayAutoServe((message) => window.confirm(message)))) {
		cubit.subscribe((state, previous) => state.log[0] !== previous?.log[0] && state.log[0] && console.info(`ingress ${state.log[0]}`));
		console.info(`ingress ${cubit.state.did} ${cubit.state.url}`);
		$('serve-form').requestSubmit();
	}
});