Files
RSSHub/lib/registry.ts
T
dependabot[bot] 5a824d3367 chore(deps-dev): bump eslint-plugin-unicorn from 58.0.0 to 59.0.0 (#18957)
* chore(deps-dev): bump eslint-plugin-unicorn from 58.0.0 to 59.0.0

Bumps [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) from 58.0.0 to 59.0.0.
- [Release notes](https://github.com/sindresorhus/eslint-plugin-unicorn/releases)
- [Commits](https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v58.0.0...v59.0.0)

---
updated-dependencies:
- dependency-name: eslint-plugin-unicorn
  dependency-version: 59.0.0
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* fix: no-unnecessary-array-flat-depth

* fix(eslint): unicorn/prefer-import-meta-properties

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-28 21:50:53 +08:00

162 lines
4.8 KiB
TypeScript

import type { Namespace, Route } from '@/types';
import { directoryImport } from 'directory-import';
import { Hono, type Handler } from 'hono';
import path from 'node:path';
import { serveStatic } from '@hono/node-server/serve-static';
import { config } from '@/config';
import index from '@/routes/index';
import healthz from '@/routes/healthz';
import robotstxt from '@/routes/robots.txt';
import metrics from '@/routes/metrics';
const __dirname = import.meta.dirname;
let modules: Record<string, { route: Route } | { namespace: Namespace }> = {};
let namespaces: Record<
string,
Namespace & {
routes: Record<
string,
Route & {
location: string;
}
>;
}
> = {};
switch (process.env.NODE_ENV) {
case 'production':
namespaces = (await import('../assets/build/routes.js')).default;
break;
case 'test':
// @ts-expect-error
namespaces = await import('../assets/build/routes.json');
break;
default:
modules = directoryImport({
targetDirectoryPath: path.join(__dirname, './routes'),
importPattern: /\.ts$/,
}) as typeof modules;
}
if (Object.keys(modules).length) {
for (const module in modules) {
const content = modules[module] as
| {
route: Route;
}
| {
namespace: Namespace;
};
const namespace = module.split(/[/\\]/)[1];
if ('namespace' in content) {
namespaces[namespace] = Object.assign(
{
routes: {},
},
namespaces[namespace],
content.namespace
);
} else if ('route' in content) {
if (!namespaces[namespace]) {
namespaces[namespace] = {
name: namespace,
routes: {},
};
}
if (Array.isArray(content.route.path)) {
for (const path of content.route.path) {
namespaces[namespace].routes[path] = {
...content.route,
location: module.split(/[/\\]/).slice(2).join('/'),
};
}
} else {
namespaces[namespace].routes[content.route.path] = {
...content.route,
location: module.split(/[/\\]/).slice(2).join('/'),
};
}
}
}
}
export { namespaces };
const app = new Hono();
const sortRoutes = (
routes: Record<
string,
Route & {
location: string;
module?: () => Promise<{ route: Route }>;
}
>
) =>
Object.entries(routes).sort(([pathA], [pathB]) => {
const segmentsA = pathA.split('/');
const segmentsB = pathB.split('/');
const lenA = segmentsA.length;
const lenB = segmentsB.length;
const minLen = Math.min(lenA, lenB);
for (let i = 0; i < minLen; i++) {
const segmentA = segmentsA[i];
const segmentB = segmentsB[i];
// Literal segments have priority over parameter segments
if (segmentA.startsWith(':') !== segmentB.startsWith(':')) {
return segmentA.startsWith(':') ? 1 : -1;
}
}
return 0;
});
for (const namespace in namespaces) {
const subApp = app.basePath(`/${namespace}`);
const namespaceData = namespaces[namespace];
if (!namespaceData || !namespaceData.routes) {
continue;
}
const sortedRoutes = sortRoutes(namespaceData.routes);
for (const [path, routeData] of sortedRoutes) {
const wrappedHandler: Handler = async (ctx) => {
if (!ctx.get('data')) {
if (typeof routeData.handler !== 'function') {
if (process.env.NODE_ENV === 'test') {
const { route } = await import(`./routes/${namespace}/${routeData.location}`);
routeData.handler = route.handler;
} else if (routeData.module) {
const { route } = await routeData.module();
routeData.handler = route.handler;
}
}
ctx.set('data', await routeData.handler(ctx));
}
};
subApp.get(path, wrappedHandler);
}
}
app.get('/', index);
app.get('/healthz', healthz);
app.get('/robots.txt', robotstxt);
if (config.debugInfo) {
// Only enable tracing in debug mode
app.get('/metrics', metrics);
}
app.use(
'/*',
serveStatic({
root: './lib/assets',
rewriteRequestPath: (path) => (path === '/favicon.ico' ? '/favicon.png' : path),
})
);
export default app;