mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-09-01 14:57:14 +08:00
bb12119123
* feat(types): add upvotes, downvotes, and comments to DataItem * refactor: fix TS2339 * refactor: fix TS2322 * refactor: fix TS2322 * refactor: fix TS2345 * refactor: fix TS2339 * refactor: fix TS2322 * refactor: fix TS2345 * refactor: fix TS2531/2532/18048/18047 * refactor: fix TS18046/TS2571 * refactor: fix TS2538 * refactor: fix TS2554/2769/2558 * refactor: fix TS2362/2363 * refactor: fix TS2353/TS2561 * refactor: fix TS2741/2739/2740 * refactor: fix TS2304 TS2307 TS2305 TS2614 TS2552 TS2551 * refactor: fix TS2352 * refactor: fix TS2604 TS17001 * refactor: fix TS2367 TS2366 * refactor: fix TS2493 * refactor: fix TS2454 * refactor: fix TS2790 * refactor: fix TS2488 * refactor: fix TS2677 * refactor: fix TS18048/18049/2532 * refactor: fix TS2322 * refactor: fix TS2344 * refactor: fix TS2339 * refactor: fix TS2339 * refactor: fix TS2365 * refactor: fix TS1117 * refactor: fix TS2561 * refactor: fix TS2555 * refactor: fix TS2503 * refactor: fix TS2339 mobile api no longer work * fix: clean slop * fix: use Context instead of any * fix: fix more typing after merge * fix: add void return for ctx.redirect in handler * chore: add type check to lint and husky * fix: remove ts ignore * chore: add oxlint rule * fix: suppress build artifact * fix: update getArticles to use cache * fix: codeql * fix(route/typeless): use async gunzip * fix: address review * fix: address review * fix: double quotes escape
106 lines
3.3 KiB
TypeScript
106 lines
3.3 KiB
TypeScript
import path from 'node:path';
|
|
|
|
import { serveStatic } from '@hono/node-server/serve-static';
|
|
import { Hono } from 'hono';
|
|
|
|
import { config } from '@/config';
|
|
import type { DevRegistry } from '@/registry-dev';
|
|
import type { NamespacesType, RoutesType } from '@/registry-helpers';
|
|
import { registerApiRoutes, registerRssRoutes } from '@/registry-helpers';
|
|
import healthz from '@/routes/healthz';
|
|
import index from '@/routes/index';
|
|
import metrics from '@/routes/metrics';
|
|
import robotstxt from '@/routes/robots.txt';
|
|
import type { Route } from '@/types';
|
|
import { isWorker } from '@/utils/is-worker';
|
|
|
|
export type { NamespacesType } from '@/registry-helpers';
|
|
export { collectNamespaceRoots, resolveModuleNamespace, sortRoutes } from '@/registry-helpers';
|
|
|
|
const __dirname = import.meta.dirname;
|
|
|
|
function isSafeRoutes(routes: RoutesType): boolean {
|
|
return Object.values(routes).every((route: Route) => !route.features?.nsfw);
|
|
}
|
|
|
|
function safeNamespaces(namespaces: NamespacesType): NamespacesType {
|
|
const safe: NamespacesType = {};
|
|
|
|
for (const [key, value] of Object.entries(namespaces)) {
|
|
if (value.routes === null || value.routes === undefined || isSafeRoutes(value.routes)) {
|
|
safe[key] = value;
|
|
}
|
|
}
|
|
return safe;
|
|
}
|
|
|
|
let namespaces: NamespacesType = {};
|
|
let devRegistry: DevRegistry | undefined;
|
|
|
|
if (config.isPackage) {
|
|
// @ts-ignore build artifact of pnpm build:routes
|
|
namespaces = (await import('../assets/build/routes.js')).default;
|
|
} else {
|
|
switch (process.env.NODE_ENV || process.env.VERCEL_ENV) {
|
|
case 'production':
|
|
// @ts-ignore build artifact of pnpm build:routes
|
|
namespaces = (await import('../assets/build/routes.js')).default;
|
|
break;
|
|
case 'test':
|
|
// @ts-expect-error TS2322 the JSON module's inferred literal type is narrower than NamespacesType
|
|
namespaces = await import('../assets/build/routes.json');
|
|
if (namespaces.default) {
|
|
// @ts-expect-error TS2322 the JSON module's default export does not satisfy NamespacesType
|
|
namespaces = namespaces.default;
|
|
}
|
|
break;
|
|
default: {
|
|
// lazy load dev namespaces
|
|
const { createDevRegistry } = await import('@/registry-dev');
|
|
devRegistry = createDevRegistry({
|
|
routesDirectory: path.join(__dirname, './routes'),
|
|
namespaces,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
if (config.feature.disable_nsfw && !devRegistry) {
|
|
namespaces = safeNamespaces(namespaces);
|
|
}
|
|
|
|
export const ensureAllLoaded: () => Promise<void> = devRegistry?.ensureAllLoaded ?? (() => Promise.resolve());
|
|
|
|
export { namespaces };
|
|
|
|
const app = new Hono();
|
|
|
|
if (!devRegistry) {
|
|
registerRssRoutes(app, namespaces);
|
|
registerApiRoutes(app, namespaces);
|
|
}
|
|
|
|
app.get('/', index);
|
|
app.get('/healthz', healthz);
|
|
app.get('/robots.txt', robotstxt);
|
|
if (config.debugInfo !== 'false') {
|
|
// Only enable tracing in debug mode
|
|
app.get('/metrics', metrics);
|
|
}
|
|
|
|
if (devRegistry) {
|
|
app.use('*', devRegistry.middleware);
|
|
}
|
|
|
|
if (!config.isPackage && !process.env.VERCEL_ENV && !isWorker) {
|
|
app.use(
|
|
'/*',
|
|
serveStatic({
|
|
root: path.join(__dirname, 'assets'),
|
|
rewriteRequestPath: (path) => (path === '/favicon.ico' ? '/favicon.png' : path),
|
|
})
|
|
);
|
|
}
|
|
|
|
export default app;
|