Refactor (packages/opencode/src/util/media.ts): Function with many returns (count = 7): sniffAttachmentMime - #169
Open
alexjack58 wants to merge 1 commit into
Conversation
alexjack58
pushed a commit
to alexjack58/opencode
that referenced
this pull request
Sep 8, 2026
`sniffAttachmentMime` was a chain of six `if (...) return` guards plus a fallback return, which Qlty flagged as "Function with many returns (count = 7)". Replace the chain with a declarative `SIGNATURES` table of magic-byte sequences (with offsets, so WebP's RIFF + WEBP check is one entry) and a single `find(...)?.mime ?? fallback` expression. Behavior is unchanged: signatures are checked in the same order, the out-of-range byte comparison still fails closed for short buffers, and the fallback is returned when nothing matches. Add test/util/media.test.ts covering every signature, the WebP two-marker case, truncated/empty inputs, and the mime predicate helpers. Closes CMU-313#166
alexjack58
force-pushed
the
refactor/media-sniff-attachment-mime
branch
from
September 8, 2026 02:21
4679470 to
95789bf
Compare
alexjack58
pushed a commit
to alexjack58/opencode
that referenced
this pull request
Sep 8, 2026
alexjack58
pushed a commit
to alexjack58/opencode
that referenced
this pull request
Sep 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
P1B: Starter Task: Refactoring PR
1. Issue
Link to the associated GitHub issue:
Closes #166 (#166)
Full path to the refactored file:
packages/opencode/src/util/media.tsWhat do you think this file does?
It is a small helper module for classifying attachments by MIME type. It exposes predicates (
isPdfAttachment,isMedia,isImageAttachment) plussniffAttachmentMime, which inspects the leading bytes of a file to detect PNG/JPEG/GIF/BMP/PDF/WebP and falls back to an extension-derived MIME type. Thereadtool uses it to decide whether a file should be returned to the model as an image/PDF or as text.What is the scope of your refactoring within that file?
Only the
sniffAttachmentMimefunction (and the privatestartsWithhelper it used). The three exported predicate functions are untouched.Which Qlty‑reported issue did you address?
Function with many returns (count = 7): sniffAttachmentMimeatpackages/opencode/src/util/media.ts:15. After the change Qlty reports no smells for the file (count 7 → 1 return).2. Refactoring
How did the specific issue you chose impact the codebase’s maintainability?
The function was a chain of six
if (startsWith(...)) return "<mime>"guards followed by a fallback return, with the magic-byte constants embedded inline in the control flow. Adding or reordering a format meant editing branching logic rather than data, and the WebP case had its own ad-hocsubarray(8)handling that made it look structurally different from the others even though it is just a second byte sequence at an offset.What changes did you make to resolve the issue?
I moved the format knowledge into a declarative
SIGNATUREStable where each entry is a MIME type plus one or more{ offset, bytes }magic sequences (WebP is expressed as two sequences at offsets 0 and 8). Two tiny helpers (matchesMagic,matchesSignature) test a buffer against an entry, andsniffAttachmentMimeis now a single expression:SIGNATURES.find(...)?.mime ?? fallback.How do your changes improve maintainability? Did you consider alternatives?
The function now has one return, one place to add a new format (a table row), and no special-casing for WebP. Match order is preserved so behavior is identical, and out-of-range indexing still fails closed for short buffers. I considered keeping the
ifchain but extracting per-format helper functions; that would have reduced complexity less and still left the returns count high, so the table-driven approach was the better fit for this smell.3. Validation
How did you validate that the change is correct?
I added
packages/opencode/test/util/media.test.ts(13 tests) that execute the refactored code directly. It covers every signature in the table (PNG, JPEG, GIF87a/89a, BMP, PDF, WebP), the WebP two-marker rule (RIFF+WEBP matches, RIFF+WAVE and a truncated RIFF header do not), empty and truncated inputs, unknown content falling back to the caller-supplied MIME, trailing bytes after a signature, and the three predicate helpers. Coverage forsrc/util/media.tsis 100% functions / 100% lines from this test alone. These tests are sufficient because the function is pure and its entire behavior is "which signature matches, or fallback"; each table row and each failure mode is exercised.bun lint,bun run typecheck(opencode package), and the fullpackages/opencodebun testsuite also pass locally. Theopencodepackage's tests already run in CI via theopencode#testturbo task, so the new test file executes on this PR without any workflow changes.Attach a screenshot of the test coverage showing the lines were executed by the tests.
Attach a screenshot showing the tests that cover the change passing during CI
All three checks on this PR are green (unit, typecheck, e2e smoke). The
unitjob runs theopencode#testturbo task, which executesbun testfor the wholepackages/opencodepackage including the newtest/util/media.test.ts(3294 tests, 0 failures). Note the package script uses--only-failures, so passing files are not listed individually in the log.Local
bun lintandbun testfor reference. Lint is clean on both changed files; the repo-widebun lintreports 2 errors that are pre-existing on upstreammainin files this PR does not touch (packages/web/src/types/lang-map.d.ts,packages/session-ui/src/v2/components/prompt-input/index.tsx).Local run of the new test file:
Attach a screenshot of
qlty smells --no-snippets <full/path/to/file.ts>showing fewer reported issues after the changes.Before:
After: