Skip to content

background: stream detection and MPV mode in modules of their own - #633

Merged
Nawid3333 merged 2 commits into
mainfrom
refactor/bg-stream-detection
Nov 7, 2026
Merged

Nawid3333 merged 2 commits into
mainfrom
refactor/bg-stream-detection

Conversation

@Nawid3333

Copy link
Copy Markdown
Owner

background.mjs was 4,900 lines. Two parts go into modules of their own, without a change in behaviour:

  • StreamDetection.mjs: how the background classifies each response (classifyResponse → ignore / subtitles / source with its mode), as pure functions that take the custom-pattern matcher as a parameter. The listener calls it and does the side effects (VpnMirror.noteSource, handleSubtitles, onSourceRecieved) as before. 20 table cases in tests/unit/StreamDetection.test.mjs.
  • MpvMode.mjs: MPV mode, 48 declarations (1,267 lines) moved as they were. It imports the state it shares with background.mjs from there; it runs nothing at load that reads those bindings (new UrlMatchList(), new MpvBackend(), Promise.resolve() only), so the cycle is safe.

background.mjs: 3,476 lines.

Checks

  • Every top-level declaration of main's background.mjs, compared as source text: 120 unchanged in background.mjs, 48 unchanged in MpvMode.mjs, 2 in StreamDetection.mjs; changed only the five detection helpers that take the matcher now (urlType, modeFromQuery, redirectedType, modeFromRedirectQuery) and recoverFrameSources, which passes it; none gone.
  • A second look at the detection by another model (old listener and helpers against the new module): no input classified differently, no side effect added, dropped or reordered.
  • Unit tests 3,452 passed; tsc and lint clean.
  • e2e here: web 45/45, extension 25/25, classic 10/10, private window 1/1. The real-mpv specs failed on the local machine because mpv did not answer on its pipe within 15 s (the extension forwarded the stream; the host's own answer); CI's Windows runner with its pinned mpv runs them.

🤖 Generated with Claude Code

Nawid3333 and others added 2 commits November 7, 2026 03:47
The first step of splitting background.mjs (4,880 lines). What onHeadersReceived decided
about a response - nothing, a subtitle file, or a stream and its player mode - is now
classifyResponse(details, firstAddress, matcher), moved with its helpers (urlType,
modeFromQuery, isHtmlResponse, isRedirect, redirectedType, modeFromRedirectQuery) and their
comments word for word; the custom source patterns and a redirect's first address are
parameters instead of module state. The listener keeps its side effects and their order.
isSubtitles moved with them (BackgroundUtils.isSubtitles delegates), so the module needs no
chrome API and is tested by itself: StreamDetection.test.mjs, 20 cases, one per behaviour the
comments record (errors, redirects, CORS preflights, HEAD, pages at a stream's address, a
player's text/html manifest, proxies, tracking pixels, redirects to addresses that name
nothing, custom patterns). The 272 background tests pass unchanged. background.mjs: -156 lines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The second step of splitting background.mjs: the 48 top-level declarations of MPV mode -
the hand-off (openInMpv, autoOpenInMpv, sendPlayedToMpv, openMpvWithSources), the toggle
(startMpv, stopMpv, onToggleMpv, leaveMpvWhenOff, inheritMpv), the autoplay watch over the
next episode (mpvWatch, watchMpvFor, its countdown, goToEpisode...), the decoder and error
reports, and tabSources (it sets mpvWatch) - moved with their comments by a script that cuts
each declaration from the syntax tree, exports it, and writes both modules' imports from the
reference graph. Its guards held: no moved variable is assigned outside the move (the
LastManualMpv* the message handler sets stay), and no moved initializer reads background.mjs
while the modules load, so the import cycle (MpvMode uses background.mjs's state and helpers,
which it exports now; background.mjs registers the listeners at its top level, as the event
page needs) is safe. Typedefs imported across. background.mjs: 4,724 -> 3,476 lines.

The 294 background tests pass unchanged (UrlMatchList.test reads both files now).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Nawid3333
Nawid3333 merged commit b2182ce into main Nov 7, 2026
39 of 41 checks passed
@Nawid3333
Nawid3333 deleted the refactor/bg-stream-detection branch November 7, 2026 05:22
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