Skip to content

feat(reader-core): add parser timeouts for EPUB content parsing#842

Merged
d-oit merged 9 commits into
mainfrom
feat/parser-timeouts
Jul 23, 2026
Merged

feat(reader-core): add parser timeouts for EPUB content parsing#842
d-oit merged 9 commits into
mainfrom
feat/parser-timeouts

Conversation

@d-oit

@d-oit d-oit commented Jul 23, 2026

Copy link
Copy Markdown
Owner

Summary

  • Adds withTimeout/checkDeadline/createDeadline utilities in @do-epub-studio/shared for parser timeout enforcement
  • Integrates timeouts across all EPUB parsing entry points (archive validation, DOMPurify, OPF/XML, accessibility, reanchor)
  • Forwards AbortSignal through epub-loader for proper cancellation
  • Delegates worker-side validation to shared validateArchive (removes 78 lines of duplicated code)
  • Adds 26 new tests across 3 test files

Motivation

Satisfies TIER-1 AGENTS.md requirement: "MUST apply parser timeouts to EPUB content parsing to prevent infinite loops on malformed input."

Default Timeout Budget

Operation Default Rationale
Archive validation 10s CPU-bound, normal <1s
DOMPurify sanitize 5s Per-chapter, normal <100ms
OPF/XML regex parsing 2s Regex-only, normal <50ms
Accessibility parsing 2s Regex-only, normal <50ms
Reanchor (all chapters) 15s I/O-bound, normal <2s
Total load pipeline 30s Existing worker-side timeout

Files Changed

  • packages/shared/src/timeout.tsnew — core timeout utilities
  • packages/shared/src/errors.ts — added TimeoutError class
  • packages/shared/src/index.ts — barrel export
  • packages/shared/src/__tests__/timeout.test.tsnew — 10 tests
  • packages/reader-core/src/archive-validator.ts — timeout wrapper
  • packages/reader-core/src/sanitizer.ts — deadline checks between DOMPurify passes
  • packages/reader-core/src/fixed-layout.ts — deadline checks in OPF parsing
  • packages/reader-core/src/epub-accessibility.ts — deadline checks in accessibility parsing
  • packages/reader-core/src/epub-loader.ts — top-level withTimeout + signal forwarding
  • packages/reader-core/src/reanchor.ts — deadline checks in reanchor loops
  • packages/reader-core/src/epub-parser.worker.ts — delegated to shared validateArchive
  • packages/reader-core/src/epub-parser-worker.ts — timeout options for fallback path
  • packages/reader-core/src/__tests__/archive-validator-timeout.test.tsnew — 6 tests
  • packages/reader-core/src/__tests__/sanitizer-timeout.test.tsnew — 5 tests

Test Results

All 1,840 tests pass across 7 packages. Quality gate (lint + typecheck) passes.

Design Decisions

  • Synchronous checkDeadline() for sync code paths — DOMPurify and regex loops must remain synchronous for epubjs hook compatibility; uses performance.now() deadline checks every N iterations
  • Async withTimeout() for async code paths — Uses AbortController/AbortSignal pattern for archive validation, reanchor, and top-level load pipeline
  • All new parameters are optional — Full backward compatibility with existing callers

Implements TIER-1 requirement: parser timeouts to prevent infinite
loops on malformed EPUB input. Adds withTimeout/checkDeadline/
createDeadline utilities in shared package, then integrates them
across all EPUB parsing entry points:
- Archive validation (10s)
- DOMPurify sanitization (5s)
- OPF/XML regex parsing (2s)
- Accessibility metadata parsing (2s)
- Reanchor text matching (15s)
- Total load pipeline (30s)

Also forwards AbortSignal through epub-loader for proper
cancellation, and delegates worker-side validation to shared
validateArchive (removing 78 lines of duplicated code).

26 new tests across 3 test files. All 1840 tests pass.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 23, 2026

Copy link
Copy Markdown

Deploying do-epub-studio with  Cloudflare Pages  Cloudflare Pages

Latest commit: f6657ca
Status: ✅  Deploy successful!
Preview URL: https://d4854f64.do-epub-studio.pages.dev
Branch Preview URL: https://feat-parser-timeouts.do-epub-studio.pages.dev

View logs

@github-actions

github-actions Bot commented Jul 23, 2026

Copy link
Copy Markdown

🚀 Performance Report

⚡ Startup & Interaction

Metric Value (ms) Limit (ms) Trend Status
First Contentful Paint 272.00 1500 -
Chapter Switch Latency 0.00 300 -
Offline Rehydrate Time 210.40 800 -
DOM Interactive 37.50 - - -
Load Event End 190.80 - - -

🛠️ CI & Workflow

Metric Value Limit Status
Total CI Duration 5.77 min 15.00 min
Pnpm Cache Hit Hit -

@codacy-production

codacy-production Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 69 complexity · -14 duplication

Metric Results
Complexity 69
Duplication -14

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Jul 23, 2026
@d-oit
d-oit merged commit f493d5c into main Jul 23, 2026
29 checks passed
@d-oit
d-oit deleted the feat/parser-timeouts branch July 23, 2026 16:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:reader area:shared documentation Improvements or additions to documentation security tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant