Skip to content

fix(export): clamp oversized Lightning media probe ranges#522

Merged
webadderall merged 2 commits into
mainfrom
fix/lightning-media-server-range-probe
May 17, 2026
Merged

fix(export): clamp oversized Lightning media probe ranges#522
webadderall merged 2 commits into
mainfrom
fix/lightning-media-server-range-probe

Conversation

@webadderall
Copy link
Copy Markdown
Collaborator

@webadderall webadderall commented May 17, 2026

Summary

  • clamp oversized HTTP byte-range end offsets to EOF in the local /video media server
  • keep valid suffix ranges working while still rejecting requests that start beyond EOF
  • add focused tests covering the large-file probe behavior

Root Cause

Lightning metadata probing goes through the local loopback media server. For large files, the demuxer can request a byte range whose end overshoots the file size. Our server treated that as a hard 416 Range Not Satisfiable instead of clamping the end to EOF, which could cause get_media_info failed: Failed after 3 attempts before export even started.

Impact

This fixes the media-server bug at the source instead of adding another exporter fallback. Large local recordings should now survive oversized probe ranges during Lightning startup.

Testing

  • npx vitest run electron/mediaServer.test.ts

Summary by CodeRabbit

  • Bug Fixes
    • More robust handling of HTTP byte-range requests for media streaming and resumable downloads.
    • Returns correct HTTP error for invalid ranges and for empty files.
    • Supports suffix-range requests and various partial-content formats.
    • Clamps ranges that extend beyond file boundaries to the actual file end to improve playback reliability.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 17, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 89071dc9-6779-4cf0-b87c-b81833487e68

📥 Commits

Reviewing files that changed from the base of the PR and between 7b137e0 and 9876b8c.

📒 Files selected for processing (2)
  • electron/mediaServer.test.ts
  • electron/mediaServer.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • electron/mediaServer.test.ts
  • electron/mediaServer.ts

📝 Walkthrough

Walkthrough

Extracts HTTP byte-range parsing into exported resolveHttpByteRange (handles suffix and open-ended ranges, clamps end, validates against file size) and updates media /video handling to use it, adding explicit 416 handling for zero-size files and delegating error responses to the helper.

Changes

HTTP Range Request Parsing

Layer / File(s) Summary
HTTP byte-range parsing helper and tests
electron/mediaServer.ts, electron/mediaServer.test.ts
Adds exported resolveHttpByteRange that normalizes bytes=<start>-<end> and bytes=-N, clamps end to fileSize - 1, validates against fileSize, and returns { start, end } or null. Tests validate malformed/multi-range rejection, EOF clamping, out-of-bounds start, and suffix-range translation.
Media request handler integration
electron/mediaServer.ts
Replaces inline Range parsing in handleMediaRequest with resolveHttpByteRange, returns 416 with Content-Range: bytes */0 for zero-size files, and uses the helper result to compute streaming boundaries or return 416 on failure.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

Checked

Poem

🐰 I nibbled bytes with careful paws,
Split ranges clean without a pause,
Clamped the end and checked the start,
Tests-hop proving every part,
Now streams hop out, precise applause.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: clamping oversized HTTP byte-range end offsets in the Lightning media server, which aligns with the primary objective of the pull request.
Description check ✅ Passed The pull request description is thorough and complete, providing a clear summary of changes, root cause analysis, impact statement, and testing instructions that cover the essential information needed for review.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 fix/lightning-media-server-range-probe

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
electron/mediaServer.test.ts (1)

74-98: ⚡ Quick win

Add a regression case for malformed/multi-range headers.

Please add explicit toBeNull() cases for values like bytes=0-1,2-3 and bytes=0-1foo so parser strictness is locked by tests.

🧪 Suggested test addition
 describe("resolveHttpByteRange", () => {
+	it("rejects malformed and multi-range headers", async () => {
+		const { resolveHttpByteRange } = await import("./mediaServer");
+		expect(resolveHttpByteRange("bytes=0-1,2-3", 100)).toBeNull();
+		expect(resolveHttpByteRange("bytes=0-1foo", 100)).toBeNull();
+	});
+
 	it("clamps oversized explicit end offsets to EOF", async () => {
 		const { resolveHttpByteRange } = await import("./mediaServer");
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@electron/mediaServer.test.ts` around lines 74 - 98, Add regression tests for
malformed or multi-range HTTP Range headers by asserting resolveHttpByteRange
returns null for those inputs: call resolveHttpByteRange with "bytes=0-1,2-3"
and with "bytes=0-1foo" (and the same EOF values used elsewhere) and add
expect(...).toBeNull() assertions; locate where resolveHttpByteRange is imported
in the existing tests (the describe block in mediaServer.test.ts) and add these
two cases alongside the other it(...) cases so parser strictness is locked by
tests.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@electron/mediaServer.ts`:
- Around line 15-16: The range parsing currently uses an unanchored regex
(rangeHeader.match) which allows malformed or multi-range headers to partially
match; replace the regex with an anchored pattern (for example
/^\s*bytes=(\d*)-(\d*)\s*$/) and keep the existing check that both capture
groups are not empty so headers like "bytes=0-1,2-3" or "bytes=0-1foo" are
rejected rather than partially accepted; update the code around the match
variable and the rangeHeader.match call accordingly so only a full, single-range
header passes.

---

Nitpick comments:
In `@electron/mediaServer.test.ts`:
- Around line 74-98: Add regression tests for malformed or multi-range HTTP
Range headers by asserting resolveHttpByteRange returns null for those inputs:
call resolveHttpByteRange with "bytes=0-1,2-3" and with "bytes=0-1foo" (and the
same EOF values used elsewhere) and add expect(...).toBeNull() assertions;
locate where resolveHttpByteRange is imported in the existing tests (the
describe block in mediaServer.test.ts) and add these two cases alongside the
other it(...) cases so parser strictness is locked by tests.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: cca9eb93-f741-4442-9a5f-80f6fbbea064

📥 Commits

Reviewing files that changed from the base of the PR and between 56a6f5f and 7b137e0.

📒 Files selected for processing (2)
  • electron/mediaServer.test.ts
  • electron/mediaServer.ts

Comment thread electron/mediaServer.ts Outdated
@webadderall webadderall merged commit d51c9d9 into main May 17, 2026
3 checks passed
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.

1 participant