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,19 +1007,23 @@ 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
? bunRuntime.Bun.serve({
fetch: app.fetch,
port: config.port,
hostname: '127.0.0.1',
})
: serve({
fetch: app.fetch, fetch: app.fetch,
port: config.port, port: config.port,
hostname: '127.0.0.1', hostname: '127.0.0.1',
@@ -1026,7 +1031,11 @@ export function startStatsServer(config: StatsServerConfig): { close: () => void
return { return {
close: () => { close: () => {
if ('stop' in server) {
server.stop(); server.stop();
} else {
server.close();
}
}, },
}; };
} }