Adds the Fingerprint page plugin (packages/fingerprint): it checks whether a model behind the local magpie gateway answers like the model it claims to be.
Reviewed at 531ca11f4 (KaranocaVe). Run at that head merged with main 848497c, through mote-run on the Mac mini with Bun 1.4.2:
- bun run rebuild from the committed bun.lock (frozen, --ignore-scripts): typecheck, 30/30 tests, build; diff -r against the committed dist/ is empty.
- HOME=$(mktemp -d) bun test: 3838 pass, 0 fail. bun scripts/check.mjs: exit 0.
- Offline preload refusing every non-loopback fetch/socket: 30/30, nothing refused.
- Broken by hand, each red: ensureDetector() in bootstrap, dropping ttl: Infinity, validating the job after the download.
Maintainer decision: the reference data comes from two third-party hosts (lm.ikale.io, cdn.jsdmirror.com), only on an explicit Download or a valid job, and both are named on the page, in registry.json and the READMEs. It is data used locally to rank candidates and is sent nowhere; the @latest fallback has no pinned hash, so a bad snapshot would rank wrongly, which the page states.
Not on npm yet (E404): magpie installs it from the dist branch until an owner publishes the first version.
90 lines
2.7 KiB
TypeScript
90 lines
2.7 KiB
TypeScript
import { options, type Options } from './config';
|
|
import { boundedText } from './http';
|
|
import { startServer } from './server';
|
|
import { serviceIdentity } from './service-identity';
|
|
|
|
/** Hosts with identical settings share one listener. An attached host takes over
|
|
* when its owner exits; no detached daemon survives the Magpie hosts. */
|
|
export async function startManagedService(input: Options = {}, pollMs = 1000) {
|
|
const opt = options(input);
|
|
const identity = serviceIdentity(opt);
|
|
let owned: Awaited<ReturnType<typeof startServer>> | undefined;
|
|
let closed = false;
|
|
let checking: Promise<void> | undefined;
|
|
let lastError = '';
|
|
const probe = async () => {
|
|
if (!opt.port) return false;
|
|
let response: Response;
|
|
try {
|
|
response = await fetch(`http://127.0.0.1:${opt.port}/_fingerprint/health`, {
|
|
signal: AbortSignal.timeout(1000),
|
|
redirect: 'error',
|
|
});
|
|
} catch {
|
|
return false;
|
|
}
|
|
try {
|
|
const peer = JSON.parse(await boundedText(response, 4096));
|
|
if (
|
|
response.ok &&
|
|
peer.app === identity.app &&
|
|
peer.protocol === identity.protocol &&
|
|
peer.configuration === identity.configuration
|
|
)
|
|
return true;
|
|
} catch {
|
|
/* An occupied port is never assumed to be our service. */
|
|
}
|
|
throw new Error(
|
|
`Port ${opt.port} belongs to an incompatible Fingerprint service or another application. Use another port, or turn Fingerprint off and on after upgrading.`,
|
|
);
|
|
};
|
|
const acquire = async () => {
|
|
if (closed || owned || (await probe())) return;
|
|
try {
|
|
owned = await startServer(opt);
|
|
} catch (error) {
|
|
// Another host may have won between discovery and bind.
|
|
if ((error as NodeJS.ErrnoException).code === 'EADDRINUSE' && (await probe())) return;
|
|
throw error;
|
|
}
|
|
if (closed) {
|
|
await owned.close();
|
|
owned = undefined;
|
|
}
|
|
};
|
|
const reconcile = () =>
|
|
(checking ??= acquire().finally(() => {
|
|
checking = undefined;
|
|
}));
|
|
await reconcile();
|
|
const timer = setInterval(() => {
|
|
void reconcile()
|
|
.then(() => {
|
|
lastError = '';
|
|
})
|
|
.catch((error) => {
|
|
const message = error instanceof Error ? error.message : String(error);
|
|
if (message !== lastError) console.warn(`Fingerprint service recovery: ${message}`);
|
|
lastError = message;
|
|
});
|
|
}, pollMs);
|
|
timer.unref();
|
|
return {
|
|
get port() {
|
|
return owned?.port ?? opt.port;
|
|
},
|
|
get role() {
|
|
return owned ? ('owner' as const) : ('attached' as const);
|
|
},
|
|
async close() {
|
|
if (closed) return;
|
|
closed = true;
|
|
clearInterval(timer);
|
|
await checking;
|
|
await owned?.close();
|
|
owned = undefined;
|
|
},
|
|
};
|
|
}
|