From e7577e47ce65ea4f7ce9cecf412c2ea10c98a519 Mon Sep 17 00:00:00 2001 From: Vordgi Date: Sat, 28 Feb 2026 16:10:15 +0000 Subject: [PATCH 1/3] fix: configure client side limit for comparison --- app/components/diff/ViewerPanel.vue | 50 +++++++++++++++++++-------- app/composables/useNumberFormatter.ts | 4 +-- i18n/locales/en.json | 2 ++ i18n/schema.json | 6 ++++ lunaria/files/en-GB.json | 2 ++ lunaria/files/en-US.json | 2 ++ 6 files changed, 49 insertions(+), 17 deletions(-) diff --git a/app/components/diff/ViewerPanel.vue b/app/components/diff/ViewerPanel.vue index 9996f301e3..507668b231 100644 --- a/app/components/diff/ViewerPanel.vue +++ b/app/components/diff/ViewerPanel.vue @@ -2,6 +2,8 @@ import type { FileDiffResponse, FileChange } from '#shared/types' import { onClickOutside } from '@vueuse/core' +const bytesFormatter = useBytesFormatter() + const props = defineProps<{ packageName: string fromVersion: string @@ -20,9 +22,21 @@ onClickOutside(optionsDropdownRef, () => { showOptions.value = false }) -const apiUrl = computed( - () => - `/api/registry/compare-file/${props.packageName}/v/${props.fromVersion}...${props.toVersion}/${props.file.path}`, +// Maximum file size we'll try to load (250KB) - must match server +const MAX_FILE_SIZE = 250 * 1024 +const isFilesTooLarge = computed(() => { + const newSize = props.file?.newSize + const oldSize = props.file?.oldSize + return ( + (newSize !== undefined && newSize > MAX_FILE_SIZE) || + (oldSize !== undefined && oldSize > MAX_FILE_SIZE) + ) +}) + +const apiUrl = computed(() => + isFilesTooLarge.value + ? null + : `/api/registry/compare-file/${props.packageName}/v/${props.fromVersion}...${props.toVersion}/${props.file.path}`, ) const apiQuery = computed(() => ({ @@ -36,7 +50,7 @@ const { data: diff, status, error: loadError, -} = useFetch(apiUrl, { +} = useFetch(() => apiUrl.value!, { query: apiQuery, timeout: 15000, }) @@ -69,13 +83,6 @@ const changeRatioPercent = computed(() => calcPercent(maxChangeRatio.value, 0, 1 const diffDistancePercent = computed(() => calcPercent(maxDiffDistance.value, 1, 60)) const charEditPercent = computed(() => calcPercent(inlineMaxCharEdits.value, 0, 10)) -function formatBytes(bytes: number | undefined): string { - if (bytes === undefined) return '' - if (bytes < 1024) return `${bytes} B` - if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB` - return `${(bytes / (1024 * 1024)).toFixed(1)} MB` -} - // Build code browser URL function getCodeUrl(version: string): string { return `/package-code/${props.packageName}/v/${version}/${props.file.path}` @@ -117,13 +124,14 @@ function getCodeUrl(version: string): string { @@ -295,8 +303,20 @@ function getCodeUrl(version: string): string {
+ +
+
+

{{ $t('compare.file_too_large') }}

+

+ {{ + $t('compare.file_size_warning', { + size: bytesFormatter.format(Math.max(file.newSize ?? 0, file.oldSize ?? 0)), + }) + }} +

+
-
+

Loading diff...

diff --git a/app/composables/useNumberFormatter.ts b/app/composables/useNumberFormatter.ts index a375bc0431..4a13d439b9 100644 --- a/app/composables/useNumberFormatter.ts +++ b/app/composables/useNumberFormatter.ts @@ -16,8 +16,8 @@ export const useBytesFormatter = () => { const decimalNumberFormatter = useNumberFormatter({ maximumFractionDigits: 1, }) - const KB = 1000 - const MB = 1000 * 1000 + const KB = 1024 + const MB = 1024 * 1024 return { format: (bytes: number) => { diff --git a/i18n/locales/en.json b/i18n/locales/en.json index 34d88abaa3..7a6899cc8b 100644 --- a/i18n/locales/en.json +++ b/i18n/locales/en.json @@ -1100,6 +1100,8 @@ "file_changes": "File Changes", "files_count": "{count} files", "lines_hidden": "{count} lines hidden", + "file_too_large": "File too large to compare", + "file_size_warning": "{size} exceeds the 250KB limit for comparison", "compare_versions": "diff", "summary": "Summary", "deps_count": "{count} deps", diff --git a/i18n/schema.json b/i18n/schema.json index 36795db2b2..af8303fe06 100644 --- a/i18n/schema.json +++ b/i18n/schema.json @@ -3304,6 +3304,12 @@ "lines_hidden": { "type": "string" }, + "file_too_large": { + "type": "string" + }, + "file_size_warning": { + "type": "string" + }, "compare_versions": { "type": "string" }, diff --git a/lunaria/files/en-GB.json b/lunaria/files/en-GB.json index 42611bf9e0..73851bc89c 100644 --- a/lunaria/files/en-GB.json +++ b/lunaria/files/en-GB.json @@ -1099,6 +1099,8 @@ "file_changes": "File Changes", "files_count": "{count} files", "lines_hidden": "{count} lines hidden", + "file_too_large": "File too large to compare", + "file_size_warning": "{size} exceeds the 250KB limit for comparison", "compare_versions": "diff", "summary": "Summary", "deps_count": "{count} deps", diff --git a/lunaria/files/en-US.json b/lunaria/files/en-US.json index a63a6cba47..f315bf4c7a 100644 --- a/lunaria/files/en-US.json +++ b/lunaria/files/en-US.json @@ -1099,6 +1099,8 @@ "file_changes": "File Changes", "files_count": "{count} files", "lines_hidden": "{count} lines hidden", + "file_too_large": "File too large to compare", + "file_size_warning": "{size} exceeds the 250KB limit for comparison", "compare_versions": "diff", "summary": "Summary", "deps_count": "{count} deps", From c7c66a6965a5e350b196bb31f95f6176fbc203c1 Mon Sep 17 00:00:00 2001 From: Vordgi Date: Sat, 28 Feb 2026 16:18:55 +0000 Subject: [PATCH 2/3] test: update bytes formatter tests --- .../app/composables/use-number-formatter.spec.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/unit/app/composables/use-number-formatter.spec.ts b/test/unit/app/composables/use-number-formatter.spec.ts index eef5d0f025..92fa46ac86 100644 --- a/test/unit/app/composables/use-number-formatter.spec.ts +++ b/test/unit/app/composables/use-number-formatter.spec.ts @@ -30,17 +30,17 @@ describe('useBytesFormatter', () => { expect(format(999)).toBe('999 B') }) - it('formats kB values using decimal base (1000)', () => { + it('formats kB values using decimal base (1024)', () => { const { format } = useBytesFormatter() - expect(format(1000)).toBe('1 kB') - expect(format(8414)).toBe('8.4 kB') + expect(format(1024)).toBe('1 kB') + expect(format(8704)).toBe('8.5 kB') }) - it('formats MB values using decimal base (1000 * 1000)', () => { + it('formats MB values using decimal base (1024 * 1024)', () => { const { format } = useBytesFormatter() - expect(format(1_000_000)).toBe('1 MB') - expect(format(1_500_000)).toBe('1.5 MB') + expect(format(1_048_576)).toBe('1 MB') + expect(format(1_572_864)).toBe('1.5 MB') }) }) From f8062debe47f202c287426371101a115b7b96c94 Mon Sep 17 00:00:00 2001 From: Vordgi Date: Sat, 28 Feb 2026 16:31:55 +0000 Subject: [PATCH 3/3] test: restore original calculation behavior --- app/composables/useNumberFormatter.ts | 4 ++-- .../app/composables/use-number-formatter.spec.ts | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/composables/useNumberFormatter.ts b/app/composables/useNumberFormatter.ts index 4a13d439b9..a375bc0431 100644 --- a/app/composables/useNumberFormatter.ts +++ b/app/composables/useNumberFormatter.ts @@ -16,8 +16,8 @@ export const useBytesFormatter = () => { const decimalNumberFormatter = useNumberFormatter({ maximumFractionDigits: 1, }) - const KB = 1024 - const MB = 1024 * 1024 + const KB = 1000 + const MB = 1000 * 1000 return { format: (bytes: number) => { diff --git a/test/unit/app/composables/use-number-formatter.spec.ts b/test/unit/app/composables/use-number-formatter.spec.ts index 92fa46ac86..eef5d0f025 100644 --- a/test/unit/app/composables/use-number-formatter.spec.ts +++ b/test/unit/app/composables/use-number-formatter.spec.ts @@ -30,17 +30,17 @@ describe('useBytesFormatter', () => { expect(format(999)).toBe('999 B') }) - it('formats kB values using decimal base (1024)', () => { + it('formats kB values using decimal base (1000)', () => { const { format } = useBytesFormatter() - expect(format(1024)).toBe('1 kB') - expect(format(8704)).toBe('8.5 kB') + expect(format(1000)).toBe('1 kB') + expect(format(8414)).toBe('8.4 kB') }) - it('formats MB values using decimal base (1024 * 1024)', () => { + it('formats MB values using decimal base (1000 * 1000)', () => { const { format } = useBytesFormatter() - expect(format(1_048_576)).toBe('1 MB') - expect(format(1_572_864)).toBe('1.5 MB') + expect(format(1_000_000)).toBe('1 MB') + expect(format(1_500_000)).toBe('1.5 MB') }) })