mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-03-27 06:12:05 -07:00
refactor: use bun serve for stats server
This commit is contained in:
@@ -3,7 +3,7 @@ import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { createStatsApp } from '../stats-server.js';
|
||||
import { createStatsApp, startStatsServer } from '../stats-server.js';
|
||||
import type { ImmersionTrackerService } from '../immersion-tracker-service.js';
|
||||
|
||||
const SESSION_SUMMARIES = [
|
||||
@@ -1110,4 +1110,54 @@ describe('stats server API routes', () => {
|
||||
assert.equal(res.headers.get('content-type'), 'image/jpeg');
|
||||
assert.equal(ensureCalls, 1);
|
||||
});
|
||||
|
||||
it('starts the stats server with Bun.serve', () => {
|
||||
type BunRuntime = {
|
||||
Bun: {
|
||||
serve: (options: { fetch: unknown; port: number; hostname: string }) => {
|
||||
stop: () => void;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
const bun = globalThis as typeof globalThis & BunRuntime;
|
||||
const originalServe = bun.Bun.serve;
|
||||
let servedWith: { fetch: unknown; port: number; hostname: string } | null = null;
|
||||
let stopCalls = 0;
|
||||
|
||||
bun.Bun.serve = (options: { fetch: unknown; port: number; hostname: string }) => {
|
||||
servedWith = options;
|
||||
return {
|
||||
stop: () => {
|
||||
stopCalls += 1;
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
try {
|
||||
const server = startStatsServer({
|
||||
port: 3210,
|
||||
staticDir: fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-stats-server-start-')),
|
||||
tracker: createMockTracker(),
|
||||
});
|
||||
|
||||
if (servedWith === null) {
|
||||
throw new Error('expected Bun.serve to be called');
|
||||
}
|
||||
|
||||
const servedOptions = servedWith as {
|
||||
fetch: unknown;
|
||||
port: number;
|
||||
hostname: string;
|
||||
};
|
||||
assert.equal(servedOptions.port, 3210);
|
||||
assert.equal(servedOptions.hostname, '127.0.0.1');
|
||||
assert.equal(typeof servedOptions.fetch, 'function');
|
||||
|
||||
server.close();
|
||||
assert.equal(stopCalls, 1);
|
||||
} finally {
|
||||
bun.Bun.serve = originalServe;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Hono } from 'hono';
|
||||
import { serve } from '@hono/node-server';
|
||||
import type { ImmersionTrackerService } from './immersion-tracker-service.js';
|
||||
import { basename, extname, resolve, sep } from 'node:path';
|
||||
import { readFileSync, existsSync, statSync } from 'node:fs';
|
||||
@@ -156,6 +155,22 @@ export interface StatsServerConfig {
|
||||
resolveAnkiNoteId?: (noteId: number) => number;
|
||||
}
|
||||
|
||||
type StatsServerHandle = {
|
||||
stop: () => void;
|
||||
};
|
||||
|
||||
type StatsApp = ReturnType<typeof createStatsApp>;
|
||||
|
||||
type BunRuntime = {
|
||||
Bun: {
|
||||
serve: (options: {
|
||||
fetch: StatsApp['fetch'];
|
||||
port: number;
|
||||
hostname: string;
|
||||
}) => StatsServerHandle;
|
||||
};
|
||||
};
|
||||
|
||||
const STATS_STATIC_CONTENT_TYPES: Record<string, string> = {
|
||||
'.css': 'text/css; charset=utf-8',
|
||||
'.gif': 'image/gif',
|
||||
@@ -1001,7 +1016,7 @@ export function startStatsServer(config: StatsServerConfig): { close: () => void
|
||||
resolveAnkiNoteId: config.resolveAnkiNoteId,
|
||||
});
|
||||
|
||||
const server = serve({
|
||||
const server = (globalThis as typeof globalThis & BunRuntime).Bun.serve({
|
||||
fetch: app.fetch,
|
||||
port: config.port,
|
||||
hostname: '127.0.0.1',
|
||||
@@ -1009,7 +1024,7 @@ export function startStatsServer(config: StatsServerConfig): { close: () => void
|
||||
|
||||
return {
|
||||
close: () => {
|
||||
server.close();
|
||||
server.stop();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user