Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ const currentNode = computed(() => {

const isViewingFile = computed<boolean>(() => currentNode.value?.type === 'file')

// Estimate binary file based on mime type
// Estimate binary file based on MIME type and the detected source language
const isBinaryFile = computed<boolean>(() => {
if (!isViewingFile.value) return false

const contentType = fileContent.value?.contentType
if (!contentType) return false
return isBinaryContentType(contentType)
return isBinaryContentType(contentType, fileContent.value?.language)
})

const isFileTooLarge = computed<boolean>(() => {
Expand Down
8 changes: 7 additions & 1 deletion app/utils/file-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ const BINARY_MIME_PREFIXES = new Set([
'application/octet-stream',
])

export function isBinaryContentType(contentType: string): boolean {
export function isBinaryContentType(contentType: string, language?: string): boolean {
// CDNs use octet-stream for source formats they don't recognize. The server
// already identifies their language from the path; 'text' is its unknown fallback.
if (contentType.startsWith('application/octet-stream') && language && language !== 'text') {
return false
}

for (const prefix of BINARY_MIME_PREFIXES) {
if (contentType.startsWith(prefix)) {
return true
Expand Down
2 changes: 1 addition & 1 deletion server/api/registry/file/[...pkg].get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ERROR_PACKAGE_VERSION_AND_FILE_FAILED,
} from '#shared/utils/constants'

const CACHE_VERSION = 3
const CACHE_VERSION = 4

// Maximum file size to fetch and highlight (500KB)
const MAX_FILE_SIZE = 500 * 1024
Expand Down
1 change: 1 addition & 0 deletions server/utils/code-highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const FILENAME_MAP: Record<string, string> = {
'package-lock.json': 'json',
'pnpm-lock.yaml': 'yaml',
'yarn.lock': 'yaml',
'bun.lock': 'jsonc',
'Makefile': 'bash',
'Dockerfile': 'bash',
'LICENSE': 'text',
Expand Down
30 changes: 30 additions & 0 deletions test/unit/file-types.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { describe, expect, it } from 'vitest'
import { isBinaryContentType } from '~/utils/file-types'

describe('isBinaryContentType', () => {
it.each(['svelte', 'astro', 'typescript', 'jsonc'])(
'previews recognized %s source served as octet-stream',
language => {
expect(isBinaryContentType('application/octet-stream', language)).toBe(false)
expect(isBinaryContentType('application/octet-stream; charset=utf-8', language)).toBe(false)
},
)

it.each([undefined, '', 'text'])('keeps unknown octet-stream files binary (%s)', language => {
expect(isBinaryContentType('application/octet-stream', language)).toBe(true)
})

it.each(['image/svg+xml', 'application/wasm', 'application/zip', 'font/woff2'])(
'keeps explicit binary MIME type %s binary even with a recognized language',
contentType => {
expect(isBinaryContentType(contentType, 'xml')).toBe(true)
},
)

it.each(['text/plain', 'text/javascript', 'application/json'])(
'previews text MIME type %s without a recognized language',
contentType => {
expect(isBinaryContentType(contentType)).toBe(false)
},
)
})
5 changes: 5 additions & 0 deletions test/unit/server/utils/code-highlight.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ describe('linkifyModuleSpecifiers', () => {
})

describe('getLanguageFromPath', () => {
it('recognizes text bun lockfiles without treating binary lockfiles as text', () => {
expect(getLanguageFromPath('nested/bun.lock')).toBe('jsonc')
expect(getLanguageFromPath('nested/bun.lockb')).toBe('text')
})

it('prefers well-known filenames over extension heuristics', () => {
expect(getLanguageFromPath('foo/README.md')).toBe('markdown')
expect(getLanguageFromPath('nested/tsconfig.json')).toBe('jsonc')
Expand Down
Loading