Skip to content

refactor(worker): extract videos routes into videos.ts#39

Merged
aloewright merged 1 commit into
mainfrom
refactor/extract-videos-routes
Apr 28, 2026
Merged

refactor(worker): extract videos routes into videos.ts#39
aloewright merged 1 commit into
mainfrom
refactor/extract-videos-routes

Conversation

@aloewright
Copy link
Copy Markdown
Owner

@aloewright aloewright commented Apr 28, 2026

Summary

  • src/workers/index.ts had grown to 542 lines (over the 500-line guideline). The trending/list/get/upload/delete handlers move to a new src/workers/videos.ts, mirroring the existing modular pattern (channels.ts, comments.ts, dmca.ts, moderation.ts, ...).
  • Pure extraction, no behavioural change. The upload-meta JSON parser is now a named zod schema (uploadMetaPersistedSchema) instead of an inline object, but the validation rules are identical.
  • After the move: index.ts is 142 lines and only wires sub-apps + middleware + the queue/scheduled handlers; videos.ts is 413 lines and owns all /api/videos/* routes.

Test plan

  • npm run lint — 0 warnings, 0 errors
  • npm run type-check — clean
  • npm test — 17 files / 129 tests passing
  • npm run build — clean
  • CI green on the PR

https://claude.ai/code/session_01YJvfCwicKZAbyhnj6W6GfY


Generated by Claude Code

Summary by CodeRabbit

  • New Features
    • Video upload functionality now supports large files through multi-part chunked uploads with resume capability
    • Secure video deletion with ownership verification for content management
    • Video discovery through browsing and trending endpoints
    • Automated view counting with user/session deduplication for engagement tracking
    • Integrated analytics event collection

index.ts crossed the 500-line guideline (542) so the trending/list/get/
upload/delete handlers move to a dedicated videos.ts module, mirroring
the existing pattern (channels.ts, comments.ts, dmca.ts, etc.).

Pure extraction — no behavioural changes. index.ts is now 142 lines and
just wires sub-apps; videos.ts owns all /api/videos/* routes plus the
upload-meta zod schema that was previously inline.

https://claude.ai/code/session_01YJvfCwicKZAbyhnj6W6GfY
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 28, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 337b8666-5a5a-4c83-9d2e-c3b312bb4c65

📥 Commits

Reviewing files that changed from the base of the PR and between 0ec6076 and 87f8d61.

📒 Files selected for processing (2)
  • src/workers/index.ts
  • src/workers/videos.ts

Disabled knowledge base sources:

  • Linear integration is disabled

You can enable these sources in your CodeRabbit configuration.


Walkthrough

This change refactors the video API implementation by extracting inlined route handlers from the main worker into a dedicated videoRoutes module. The main worker now delegates all video endpoints (list, trending, details, upload, delete) through route registration, reducing the worker file from ~405 lines while centralizing video-specific logic into a new separate module.

Changes

Cohort / File(s) Summary
Route Extraction & Simplification
src/workers/index.ts
Removes inlined video endpoint implementations (trending, list, detail, upload, delete), associated zod validation, analytics logic, and video-related bindings. Delegates to videoRoutes via single route registration. Simplifies environment bindings by extending VideoRoutesEnv.
Video Routes Implementation
src/workers/videos.ts
New module implementing all video endpoints: GET /api/videos (list), GET /api/videos/trending (cached aggregation), GET /api/videos/:id (with DMCA/hidden rules), view counting with deduplication, POST /api/videos/upload (multipart chunked uploads to R2 with session persistence), and DELETE /api/videos/:id (soft-delete with cache invalidation).

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/extract-videos-routes

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@aloewright aloewright merged commit 8c1a4af into main Apr 28, 2026
1 of 2 checks passed
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the codebase by modularizing video-related logic, moving routes, schemas, and types from src/workers/index.ts into a new dedicated file, src/workers/videos.ts. This change improves the maintainability and organization of the main worker entry point. Review feedback highlights opportunities to further improve the code by centralizing duplicated type and interface definitions (such as SessionUser and AnalyticsEngineDataset) and using safeParse for session metadata to prevent unhandled exceptions during JSON parsing.

Comment thread src/workers/videos.ts
Comment on lines +13 to +15
interface AnalyticsEngineDataset {
writeDataPoint(point: { blobs?: string[]; doubles?: number[]; indexes?: string[] }): void;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The AnalyticsEngineDataset interface is duplicated here and in src/workers/analytics.ts. To improve maintainability and follow the DRY (Don't Repeat Yourself) principle, consider moving shared interfaces and types to a common file (e.g., src/workers/types.ts) or exporting them from their primary source.

Comment thread src/workers/videos.ts
ANALYTICS?: AnalyticsEngineDataset;
}

type SessionUser = { id: string; email: string; name: string } | null;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The SessionUser type is duplicated across several files (index.ts, analytics.ts, and now videos.ts) with slight variations. This can lead to type mismatches and maintenance overhead. It is recommended to centralize this type definition in a shared location.

Comment thread src/workers/videos.ts
return c.json({ error: 'Missing upload session. Start with chunkIndex=0.' }, 400);
}

const uploadMeta = uploadMetaPersistedSchema.parse(JSON.parse(uploadMetaJson));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using .parse() directly on JSON.parse() results can throw unhandled exceptions if the stored session metadata is corrupted or has an unexpected schema. For consistency with how other inputs are handled in this file (e.g., query parameters and form data), consider using safeParse and providing a structured error response.

Suggested change
const uploadMeta = uploadMetaPersistedSchema.parse(JSON.parse(uploadMetaJson));
const uploadMetaParsed = uploadMetaPersistedSchema.safeParse(JSON.parse(uploadMetaJson));
if (!uploadMetaParsed.success) {
return c.json({ error: 'Invalid upload session metadata' }, 400);
}
const uploadMeta = uploadMetaParsed.data;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants