feat: deploy RSSHub to Cloudflare Workers (#20804)

* feat: deploy RSSHub to Cloudflare Workers

- Create Worker entry point (lib/worker.ts) with polyfills
- Implement Worker-specific app configuration (lib/app.worker.tsx)
- Add automatic .worker.ts resolution plugin for cleaner config
- Simplify build configuration with only 3 essential shims
- Enable static asset serving via Cloudflare Static Assets feature
- Support dynamic route loading with proper module aliasing

🤖 Generated with Claude Code

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>

* feat(worker): add puppeteer and request-rewriter support

- Add @cloudflare/puppeteer for Browser Rendering API support
- Create puppeteer.worker.ts with Cloudflare Browser binding
- Add request-rewriter Worker version with static browser headers
- Add vm module shim to node-module.ts for JSDOM compatibility
- Configure __dirname/__filename in tsdown for CommonJS compat
- Update wrangler.toml with BROWSER binding configuration
- Dynamically extract namespaces from foloAnalysisTop100 for routes

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix(cache): restore synchronous initialization for tests

The async initialization caused cache tests to fail because the cache
module wasn't ready when tests ran. Restored synchronous imports while
keeping Worker-specific no-op behavior.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* test(worker): add automated Worker integration tests

- Add miniflare for Worker environment simulation
- Create lib/worker.worker.test.ts with integration tests
- Test basic routes (/test/1, /, unknown routes)
- Test RSS feed routes (hackernews, v2ex)
- Test error handling for puppeteer routes without BROWSER binding
- Add worker-test npm script

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* refactor(worker): use test routes for Worker integration tests

- Replace third-party routes (hackernews, v2ex, weibo) with /test/* routes
- Remove miniflare dependency (wrangler includes it internally)
- Simplify test setup using wrangler's unstable_dev API

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix(test): make header-generator test less flaky

The header-generator library may return different platform values due to
internal randomness. Update test to check for valid format instead of
exact value.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix(test): exclude worker tests from vitest coverage run

Worker tests require the dist-worker bundle to be built first,
which is not part of the regular CI test workflow. These tests
should be run separately using 'pnpm worker-test'.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* test(worker): add automated Worker integration tests

- Add worker-build step to CI workflow before running tests
- Revert exclusion of worker tests from vitest coverage run
- Worker tests now run as part of the regular test suite

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix(worker): fix routes.js alias path in worker build config

Change alias key from absolute path to relative import path to match
how it's imported in lib/registry.ts. This fixes the worker build
failing with "Could not resolve '../assets/build/routes.js'" error.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* ci: trigger CI re-run

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix(ci): build worker routes before worker-build

The worker build requires routes-worker.js which is generated by
running build:routes with WORKER_BUILD=true. This was missing in CI,
causing the worker build to fail with "Could not resolve routes.js".

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix(test): increase timeout for Worker integration tests

Worker tests need more time in CI environments. Increase individual
test timeouts from default 10s to 30s for each test case.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
DIYgod
2026-01-04 09:22:05 +08:00
committed by GitHub
co-authored by Claude Opus 4.5
parent 472082e3b1
commit 1131350b5f
25 changed files with 860 additions and 21 deletions
+35 -17
View File
@@ -9,6 +9,9 @@ import { getCurrentPath } from '../../lib/utils/helpers';
const __dirname = getCurrentPath(import.meta.url);
// Check if building for Worker environment
const isWorkerBuild = process.env.WORKER_BUILD === 'true';
// Ignore Redis and remote config in route generation to avoid side effects.
process.env.REDIS_URL = '';
process.env.CACHE_TYPE = '';
@@ -43,6 +46,11 @@ const foloAnalysisTop100 = Object.entries(foloAnalysisResult)
.toSorted((a, b) => b[1].subscriptionCount - a[1].subscriptionCount)
.slice(0, 150);
// Extract unique namespaces from top 150 routes for Worker build
const workerNamespaces = new Set(foloAnalysisTop100.map(([routePath]) => routePath.split('/')[1]).filter(Boolean));
// Always include test namespace for testing
workerNamespaces.add('test');
const maintainers: Record<string, string[]> = {};
const radar: {
[domain: string]: {
@@ -54,12 +62,16 @@ const radar: {
// Generate route paths type
const allRoutePaths = new Set<string>();
for (const namespace in namespaces) {
let defaultCategory = namespaces[namespace].categories?.[0];
// Filter namespaces for Worker build
const namespacesToProcess = isWorkerBuild ? Object.fromEntries(Object.entries(namespaces).filter(([key]) => workerNamespaces.has(key))) : namespaces;
for (const namespace in namespacesToProcess) {
const namespaceData = namespacesToProcess[namespace];
let defaultCategory = namespaceData.categories?.[0];
if (!defaultCategory) {
for (const path in namespaces[namespace].routes) {
if (namespaces[namespace].routes[path].categories) {
defaultCategory = namespaces[namespace].routes[path].categories[0];
for (const path in namespaceData.routes) {
if (namespaceData.routes[path].categories) {
defaultCategory = namespaceData.routes[path].categories[0];
break;
}
}
@@ -67,11 +79,11 @@ for (const namespace in namespaces) {
if (!defaultCategory) {
defaultCategory = 'other';
}
for (const path in namespaces[namespace].routes) {
for (const path in namespaceData.routes) {
const realPath = `/${namespace}${path}`;
allRoutePaths.add(realPath);
const data = namespaces[namespace].routes[path];
const categories = data.categories || namespaces[namespace].categories || [defaultCategory];
const data = namespaceData.routes[path];
const categories = data.categories || namespaceData.categories || [defaultCategory];
if (foloAnalysisTop100.some(([path]) => path === realPath)) {
categories.push('popular');
}
@@ -88,7 +100,7 @@ for (const namespace in namespaces) {
if (domain) {
if (!radar[domain]) {
radar[domain] = {
_name: namespaces[namespace].name,
_name: namespaceData.name,
};
}
if (!radar[domain][subdomain]) {
@@ -108,8 +120,8 @@ for (const namespace in namespaces) {
}
data.module = `() => import('@/routes/${namespace}/${data.location}')`;
}
for (const path in namespaces[namespace].apiRoutes) {
const data = namespaces[namespace].apiRoutes[path];
for (const path in namespaceData.apiRoutes) {
const data = namespaceData.apiRoutes[path];
data.module = `() => import('@/routes/${namespace}/${data.location}')`;
}
}
@@ -124,9 +136,15 @@ export type RoutePath =
${uniquePaths.map((path) => ` | \`${path}\``).join('\n')};
`;
fs.writeFileSync(path.join(__dirname, '../../assets/build/radar-rules.json'), JSON.stringify(radar, null, 2));
fs.writeFileSync(path.join(__dirname, '../../assets/build/radar-rules.js'), `(${toSource(radar)})`);
fs.writeFileSync(path.join(__dirname, '../../assets/build/maintainers.json'), JSON.stringify(maintainers, null, 2));
fs.writeFileSync(path.join(__dirname, '../../assets/build/routes.json'), JSON.stringify(namespaces, null, 2));
fs.writeFileSync(path.join(__dirname, '../../assets/build/routes.js'), `export default ${JSON.stringify(namespaces, null, 2)}`.replaceAll(/"module": "(.*)"\n/g, `"module": $1\n`));
fs.writeFileSync(path.join(__dirname, '../../assets/build/route-paths.ts'), routePathsType);
// For Worker build, only output routes-worker.js with filtered namespaces
// For regular build, output all files
if (isWorkerBuild) {
fs.writeFileSync(path.join(__dirname, '../../assets/build/routes-worker.js'), `export default ${JSON.stringify(namespacesToProcess, null, 2)}`.replaceAll(/"module": "(.*)"\n/g, `"module": $1\n`));
} else {
fs.writeFileSync(path.join(__dirname, '../../assets/build/radar-rules.json'), JSON.stringify(radar, null, 2));
fs.writeFileSync(path.join(__dirname, '../../assets/build/radar-rules.js'), `(${toSource(radar)})`);
fs.writeFileSync(path.join(__dirname, '../../assets/build/maintainers.json'), JSON.stringify(maintainers, null, 2));
fs.writeFileSync(path.join(__dirname, '../../assets/build/routes.json'), JSON.stringify(namespaces, null, 2));
fs.writeFileSync(path.join(__dirname, '../../assets/build/routes.js'), `export default ${JSON.stringify(namespaces, null, 2)}`.replaceAll(/"module": "(.*)"\n/g, `"module": $1\n`));
fs.writeFileSync(path.join(__dirname, '../../assets/build/route-paths.ts'), routePathsType);
}