fix(stats): fallback to node server when Bun.serve unavailable

This commit is contained in:
2026-03-29 00:27:53 -07:00
parent 9dae4af773
commit 4ff8529744

View File

@@ -1,4 +1,5 @@
import { Hono } from 'hono'; import { Hono } from 'hono';
import { serve } from '@hono/node-server';
import type { ImmersionTrackerService } from './immersion-tracker-service.js'; import type { ImmersionTrackerService } from './immersion-tracker-service.js';
import { basename, extname, resolve, sep } from 'node:path'; import { basename, extname, resolve, sep } from 'node:path';
import { readFileSync, existsSync, statSync } from 'node:fs'; import { readFileSync, existsSync, statSync } from 'node:fs';
@@ -1006,27 +1007,35 @@ export function startStatsServer(config: StatsServerConfig): { close: () => void
resolveAnkiNoteId: config.resolveAnkiNoteId, resolveAnkiNoteId: config.resolveAnkiNoteId,
}); });
const bunServe = ( const bunRuntime = globalThis as typeof globalThis & {
globalThis as typeof globalThis & { Bun?: {
Bun: { serve?: (options: {
serve: (options: { fetch: (typeof app)['fetch'];
fetch: (typeof app)['fetch']; port: number;
port: number; hostname: string;
hostname: string; }) => { stop: () => void };
}) => { stop: () => void }; };
}; };
}
).Bun.serve;
const server = bunServe({ const server = bunRuntime.Bun?.serve
fetch: app.fetch, ? bunRuntime.Bun.serve({
port: config.port, fetch: app.fetch,
hostname: '127.0.0.1', port: config.port,
}); hostname: '127.0.0.1',
})
: serve({
fetch: app.fetch,
port: config.port,
hostname: '127.0.0.1',
});
return { return {
close: () => { close: () => {
server.stop(); if ('stop' in server) {
server.stop();
} else {
server.close();
}
}, },
}; };
} }