mirror of
https://github.com/nimbold/Firelink.git
synced 2026-08-10 11:37:21 +00:00
fix: harden media handoff and live logs
Reject stale extension media cookie headers before yt-dlp metadata work, preserve ordinary capture cookies, and advance the companion extension. Stream redacted diagnostic logs only while the visible Logs view is active, with bounded batched updates and race-safe snapshot handoff.
This commit is contained in:
@@ -397,6 +397,12 @@ function App() {
|
||||
invoke('toggle_log_pause', { pause: !logsEnabled }).catch(console.error);
|
||||
}, [logsEnabled]);
|
||||
|
||||
useEffect(() => {
|
||||
if (activeView !== 'logs') {
|
||||
invoke('set_log_stream_active', { active: false }).catch(console.error);
|
||||
}
|
||||
}, [activeView]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!extensionPairingToken) return;
|
||||
invoke('set_extension_pairing_token', { token: extensionPairingToken }).catch(error => {
|
||||
|
||||
@@ -120,9 +120,8 @@ export const AddDownloadsModal = () => {
|
||||
if (context) return extensionHeaders(context).trim();
|
||||
return hasExtensionRequestContext ? '' : headers.trim();
|
||||
};
|
||||
const cookiesForRow = (sourceUrl: string, omitRequestCookies = false) => {
|
||||
const cookiesForRow = (sourceUrl: string) => {
|
||||
if (cookiesManuallyEditedRef.current) return cookies.trim();
|
||||
if (omitRequestCookies) return '';
|
||||
const context = requestContextForUrl(sourceUrl);
|
||||
if (context) return context.cookies.trim();
|
||||
return hasExtensionRequestContext ? '' : cookies.trim();
|
||||
@@ -303,25 +302,7 @@ export const AddDownloadsModal = () => {
|
||||
cookies: rowCookies || null,
|
||||
proxy
|
||||
};
|
||||
let requestCookiesOmitted = false;
|
||||
let mediaData;
|
||||
try {
|
||||
mediaData = await fetchMediaMetadataDeduped(mediaMetadataArgs);
|
||||
} catch (error) {
|
||||
const capturedCookies = requestContextForUrl(row.sourceUrl)?.cookies.trim();
|
||||
if (!rowCookies || !capturedCookies || cookiesManuallyEditedRef.current) {
|
||||
throw error;
|
||||
}
|
||||
console.warn(
|
||||
'Media metadata rejected the captured Cookie header; retrying without request cookies',
|
||||
error
|
||||
);
|
||||
mediaData = await fetchMediaMetadataDeduped({
|
||||
...mediaMetadataArgs,
|
||||
cookies: null
|
||||
});
|
||||
requestCookiesOmitted = true;
|
||||
}
|
||||
const mediaData = await fetchMediaMetadataDeduped(mediaMetadataArgs);
|
||||
if (mediaData && mediaData.formats.length > 0) {
|
||||
const mappedFormats = mediaData.formats.map(f => {
|
||||
const quality = f.resolution || 'Video';
|
||||
@@ -353,7 +334,6 @@ export const AddDownloadsModal = () => {
|
||||
size: mappedFormats[0].bytes ? mappedFormats[0].detail : undefined,
|
||||
sizeBytes: mappedFormats[0].bytes || undefined,
|
||||
status: 'ready',
|
||||
requestCookiesOmitted,
|
||||
formats: mappedFormats,
|
||||
selectedFormat: 0
|
||||
})
|
||||
@@ -729,7 +709,7 @@ export const AddDownloadsModal = () => {
|
||||
checksum: checksumEnabled && checksumValue.trim()
|
||||
? `${checksumAlgo}=${checksumValue.trim()}`
|
||||
: undefined,
|
||||
cookies: cookiesForRow(item.sourceUrl, item.requestCookiesOmitted) || undefined,
|
||||
cookies: cookiesForRow(item.sourceUrl) || undefined,
|
||||
mirrors: mirrors.trim() || undefined,
|
||||
destination: useSharedDestination
|
||||
? finalLocation
|
||||
@@ -1148,11 +1128,6 @@ export const AddDownloadsModal = () => {
|
||||
className="add-download-control w-full px-3 py-1.5 text-xs font-mono"
|
||||
aria-label="Cookies"
|
||||
/>
|
||||
{!cookiesManuallyEditedRef.current && parsedItems.some(item => item.requestCookiesOmitted) && (
|
||||
<p className="mt-1 text-[10px] text-amber-400">
|
||||
Media metadata only worked without the captured cookies, so they will be omitted for affected rows. Edit this field to force a manual value.
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-[10px] uppercase font-bold tracking-wider text-text-muted mb-1">Mirrors</label>
|
||||
|
||||
+81
-43
@@ -7,11 +7,15 @@ import { FileDown, Trash2, Terminal, Filter, Play, Pause, Info, Copy } from 'luc
|
||||
import { WindowDragRegion } from './WindowDragRegion';
|
||||
import { useToast } from '../contexts/ToastContext';
|
||||
import { useSettingsStore } from '../store/useSettingsStore';
|
||||
|
||||
interface LogEntry {
|
||||
level: 'Trace' | 'Debug' | 'Info' | 'Warn' | 'Error';
|
||||
message: string;
|
||||
}
|
||||
import {
|
||||
MAX_LOG_LINES,
|
||||
appendBoundedLogEntries,
|
||||
liveLogEntry,
|
||||
mergeLogSnapshotAndLiveEntries,
|
||||
persistedLogEntry,
|
||||
pushBoundedLogEntry,
|
||||
type LogEntry
|
||||
} from '../utils/logEntries';
|
||||
|
||||
export default function LogsView() {
|
||||
const { addToast } = useToast();
|
||||
@@ -20,56 +24,75 @@ export default function LogsView() {
|
||||
const [logs, setLogs] = useState<LogEntry[]>([]);
|
||||
const [levelFilter, setLevelFilter] = useState<LogEntry['level'] | 'All'>('All');
|
||||
const [contextMenu, setContextMenu] = useState<{ x: number; y: number; text: string } | null>(null);
|
||||
const [pageVisible, setPageVisible] = useState(() => document.visibilityState !== 'hidden');
|
||||
const scrollRef = useRef<HTMLDivElement>(null);
|
||||
const rawLineCountRef = useRef(0);
|
||||
|
||||
const MAX_LOG_LINES = 2000;
|
||||
const liveBatchRef = useRef<LogEntry[]>([]);
|
||||
const liveFrameRef = useRef<number | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const handleVisibilityChange = () => setPageVisible(document.visibilityState !== 'hidden');
|
||||
document.addEventListener('visibilitychange', handleVisibilityChange);
|
||||
return () => document.removeEventListener('visibilitychange', handleVisibilityChange);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!pageVisible) {
|
||||
void invoke('set_log_stream_active', { active: false }).catch(console.error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!logsEnabled) {
|
||||
void invoke('set_log_stream_active', { active: false }).catch(console.error);
|
||||
}
|
||||
|
||||
let active = true;
|
||||
let unlisten: (() => void) | undefined;
|
||||
let initialized = false;
|
||||
let pendingLiveEntries: LogEntry[] = [];
|
||||
let unlistenPromise: Promise<() => void> | undefined;
|
||||
|
||||
const scheduleLiveEntry = (entry: LogEntry) => {
|
||||
if (!initialized) {
|
||||
pushBoundedLogEntry(pendingLiveEntries, entry);
|
||||
return;
|
||||
}
|
||||
|
||||
pushBoundedLogEntry(liveBatchRef.current, entry);
|
||||
if (liveFrameRef.current !== null) return;
|
||||
liveFrameRef.current = window.requestAnimationFrame(() => {
|
||||
liveFrameRef.current = null;
|
||||
if (!active || liveBatchRef.current.length === 0) return;
|
||||
const batch = liveBatchRef.current;
|
||||
liveBatchRef.current = [];
|
||||
setLogs(current => appendBoundedLogEntries(current, batch));
|
||||
});
|
||||
};
|
||||
|
||||
const init = async () => {
|
||||
try {
|
||||
await initLogger();
|
||||
const [lines] = await Promise.all([
|
||||
invoke('read_logs', { limit: MAX_LOG_LINES })
|
||||
]);
|
||||
if (!active) return;
|
||||
const initialLogs = lines.map(message => {
|
||||
const level: LogEntry['level'] = message.includes('[ERROR]') ? 'Error'
|
||||
: message.includes('[WARN]') ? 'Warn'
|
||||
: message.includes('[INFO]') ? 'Info'
|
||||
: message.includes('[TRACE]') ? 'Trace'
|
||||
: 'Debug';
|
||||
return { level, message };
|
||||
});
|
||||
|
||||
setLogs(initialLogs);
|
||||
rawLineCountRef.current = initialLogs.length;
|
||||
|
||||
if (logsEnabled) {
|
||||
unlisten = await attachLogger((log) => {
|
||||
unlistenPromise = attachLogger((log) => {
|
||||
if (!active) return;
|
||||
const levelStr: LogEntry['level'] = log.level === 5 ? 'Error'
|
||||
: log.level === 4 ? 'Warn'
|
||||
: log.level === 3 ? 'Info'
|
||||
: log.level === 1 ? 'Trace'
|
||||
: 'Debug';
|
||||
|
||||
const timeStr = new Date().toISOString().replace('T', ' ').substring(0, 19);
|
||||
const formattedMsg = `[${timeStr}] [${levelStr.toUpperCase()}] ${log.message}`;
|
||||
|
||||
setLogs(prev => {
|
||||
const newLogs = [...prev, { level: levelStr, message: formattedMsg }];
|
||||
rawLineCountRef.current = newLogs.length;
|
||||
if (newLogs.length > MAX_LOG_LINES + 500) {
|
||||
return newLogs.slice(newLogs.length - MAX_LOG_LINES);
|
||||
}
|
||||
return newLogs;
|
||||
});
|
||||
scheduleLiveEntry(liveLogEntry(log.level, log.message));
|
||||
});
|
||||
await unlistenPromise;
|
||||
if (!active) return;
|
||||
await invoke('set_log_stream_active', { active: true });
|
||||
if (!active) {
|
||||
await invoke('set_log_stream_active', { active: false }).catch(console.error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const lines = await invoke('read_logs', { limit: MAX_LOG_LINES });
|
||||
if (!active) return;
|
||||
const snapshot = lines.map(persistedLogEntry);
|
||||
initialized = true;
|
||||
const caughtUpLogs = mergeLogSnapshotAndLiveEntries(snapshot, pendingLiveEntries);
|
||||
pendingLiveEntries = [];
|
||||
setLogs(caughtUpLogs);
|
||||
} catch (e) {
|
||||
console.error('Failed to init logs:', e);
|
||||
}
|
||||
@@ -78,9 +101,19 @@ export default function LogsView() {
|
||||
|
||||
return () => {
|
||||
active = false;
|
||||
if (unlisten) unlisten();
|
||||
liveBatchRef.current = [];
|
||||
if (liveFrameRef.current !== null) {
|
||||
window.cancelAnimationFrame(liveFrameRef.current);
|
||||
liveFrameRef.current = null;
|
||||
}
|
||||
if (logsEnabled) {
|
||||
void invoke('set_log_stream_active', { active: false }).catch(console.error);
|
||||
}
|
||||
if (unlistenPromise) {
|
||||
void unlistenPromise.then(unlisten => unlisten()).catch(console.error);
|
||||
}
|
||||
};
|
||||
}, [logsEnabled]);
|
||||
}, [logsEnabled, pageVisible]);
|
||||
|
||||
useEffect(() => {
|
||||
if (scrollRef.current) {
|
||||
@@ -139,6 +172,11 @@ export default function LogsView() {
|
||||
};
|
||||
|
||||
const handleClear = async () => {
|
||||
liveBatchRef.current = [];
|
||||
if (liveFrameRef.current !== null) {
|
||||
window.cancelAnimationFrame(liveFrameRef.current);
|
||||
liveFrameRef.current = null;
|
||||
}
|
||||
setLogs([]);
|
||||
await invoke('clear_logs').catch(console.error);
|
||||
};
|
||||
|
||||
@@ -76,6 +76,7 @@ type CommandMap = {
|
||||
clear_logs: { args: undefined; result: void };
|
||||
toggle_log_pause: { args: { pause: boolean }; result: void };
|
||||
is_log_paused: { args: undefined; result: boolean };
|
||||
set_log_stream_active: { args: { active: boolean }; result: void };
|
||||
get_pending_order: { args: { queueId: string | null }; result: string[] };
|
||||
enqueue_download: { args: { item: EnqueueItem }; result: EnqueueAccepted };
|
||||
cancel_enqueue_generation: { args: { id: string; generation: string }; result: void };
|
||||
|
||||
@@ -819,7 +819,7 @@ describe('useDownloadStore', () => {
|
||||
silent: false,
|
||||
filename: null,
|
||||
headers: 'User-Agent: Firefox Test',
|
||||
cookies: 'session=secret',
|
||||
cookies: `oversized=${'x'.repeat(64 * 1024)}`,
|
||||
media: true
|
||||
});
|
||||
|
||||
@@ -827,7 +827,21 @@ describe('useDownloadStore', () => {
|
||||
expect(state.isAddModalOpen).toBe(true);
|
||||
expect(state.pendingAddUrls).toBe('https://adult.example/watch/123');
|
||||
expect(state.pendingAddMediaUrls).toEqual(['https://adult.example/watch/123']);
|
||||
expect(state.pendingAddCookies).toBe('session=secret');
|
||||
expect(state.pendingAddCookies).toBe('');
|
||||
});
|
||||
|
||||
it('preserves extension cookies for ordinary captured downloads', async () => {
|
||||
await useDownloadStore.getState().handleExtensionDownload({
|
||||
urls: ['https://example.com/private.zip'],
|
||||
referer: 'https://example.com/downloads',
|
||||
silent: true,
|
||||
filename: 'private.zip',
|
||||
headers: null,
|
||||
cookies: 'session=secret',
|
||||
media: false
|
||||
});
|
||||
|
||||
expect(useDownloadStore.getState().pendingAddCookies).toBe('session=secret');
|
||||
});
|
||||
|
||||
it('clears stale request context when the same URL is captured without it later', async () => {
|
||||
|
||||
@@ -551,12 +551,17 @@ export const useDownloadStore = create<DownloadState>((set, get) => ({
|
||||
const urls = [...new Set(request.urls.map(url => url.trim()).filter(Boolean))];
|
||||
if (urls.length === 0) return;
|
||||
|
||||
// Explicit media authentication belongs to yt-dlp's configured browser
|
||||
// cookie source. Keep this frontend guard for events from older desktop or
|
||||
// extension builds; ordinary captured downloads retain their cookies.
|
||||
const cookies = request.media === true ? null : request.cookies;
|
||||
|
||||
get().openAddModalWithUrls(
|
||||
urls.join('\n'),
|
||||
request.referer,
|
||||
urls.length === 1 ? request.filename : null,
|
||||
request.headers,
|
||||
request.cookies,
|
||||
cookies,
|
||||
request.media === true
|
||||
);
|
||||
},
|
||||
|
||||
@@ -94,7 +94,6 @@ describe('add download metadata workflow', () => {
|
||||
status: 'ready',
|
||||
generation: 4,
|
||||
requestContextVersion: 1,
|
||||
requestCookiesOmitted: true,
|
||||
formats: [{
|
||||
name: '1080p MP4',
|
||||
selector: '137+140',
|
||||
@@ -121,7 +120,6 @@ describe('add download metadata workflow', () => {
|
||||
status: 'loading',
|
||||
generation: 5,
|
||||
requestContextVersion: 2,
|
||||
requestCookiesOmitted: false,
|
||||
formats: undefined,
|
||||
selectedFormat: undefined
|
||||
});
|
||||
|
||||
@@ -27,7 +27,6 @@ export interface AddDownloadDraftRow {
|
||||
status: MetadataStatus;
|
||||
generation: number;
|
||||
requestContextVersion?: number;
|
||||
requestCookiesOmitted?: boolean;
|
||||
isMedia: boolean;
|
||||
resumable?: boolean;
|
||||
formats?: AddMediaFormat[];
|
||||
@@ -94,7 +93,6 @@ export const reconcileDownloadRows = (
|
||||
status: 'loading',
|
||||
generation: preserved.generation + 1,
|
||||
requestContextVersion,
|
||||
requestCookiesOmitted: false,
|
||||
isMedia: preserved.isMedia || forcedMedia,
|
||||
formats: preserved.isMedia || forcedMedia ? undefined : preserved.formats,
|
||||
selectedFormat: preserved.isMedia || forcedMedia ? undefined : preserved.selectedFormat
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
appendBoundedLogEntries,
|
||||
liveLogEntry,
|
||||
mergeLogSnapshotAndLiveEntries,
|
||||
persistedLogEntry,
|
||||
pushBoundedLogEntry,
|
||||
type LogEntry
|
||||
} from './logEntries';
|
||||
|
||||
const entry = (message: string): LogEntry => ({ level: 'Info', message });
|
||||
|
||||
describe('log entry streaming', () => {
|
||||
it('derives levels from persisted formatted lines', () => {
|
||||
expect(persistedLogEntry('[2026-07-10][18:00:00][ERROR][firelink] failed')).toEqual({
|
||||
level: 'Error',
|
||||
message: '[2026-07-10][18:00:00][ERROR][firelink] failed'
|
||||
});
|
||||
});
|
||||
|
||||
it('does not double-format backend Webview log lines', () => {
|
||||
const message = '[2026-07-10][18:00:00][WARN][firelink] retrying';
|
||||
expect(liveLogEntry(4, message).message).toBe(message);
|
||||
});
|
||||
|
||||
it('formats an unformatted plugin event with its numeric level', () => {
|
||||
expect(liveLogEntry(3, 'download started', new Date('2026-07-10T14:30:00Z'))).toEqual({
|
||||
level: 'Info',
|
||||
message: '[2026-07-10 14:30:00] [INFO] download started'
|
||||
});
|
||||
});
|
||||
|
||||
it('merges only the ordered snapshot-to-stream overlap', () => {
|
||||
expect(mergeLogSnapshotAndLiveEntries(
|
||||
[entry('one'), entry('repeat'), entry('three')],
|
||||
[entry('three'), entry('repeat'), entry('four')]
|
||||
).map(item => item.message)).toEqual(['one', 'repeat', 'three', 'repeat', 'four']);
|
||||
});
|
||||
|
||||
it('bounds burst updates to the newest entries', () => {
|
||||
expect(appendBoundedLogEntries(
|
||||
[entry('old')],
|
||||
Array.from({ length: 5 }, (_, index) => entry(`new-${index}`)),
|
||||
3
|
||||
).map(item => item.message)).toEqual(['new-2', 'new-3', 'new-4']);
|
||||
});
|
||||
|
||||
it('bounds the mutable pre-render queue without copying on every event', () => {
|
||||
const queue = [entry('old')];
|
||||
for (let index = 0; index < 5; index += 1) {
|
||||
pushBoundedLogEntry(queue, entry(`new-${index}`), 3);
|
||||
}
|
||||
expect(queue.map(item => item.message)).toEqual(['new-2', 'new-3', 'new-4']);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,92 @@
|
||||
export type LogLevel = 'Trace' | 'Debug' | 'Info' | 'Warn' | 'Error';
|
||||
|
||||
export interface LogEntry {
|
||||
level: LogLevel;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export const MAX_LOG_LINES = 2000;
|
||||
|
||||
const LEVEL_NAMES: LogLevel[] = ['Trace', 'Debug', 'Info', 'Warn', 'Error'];
|
||||
const LIVE_LEVELS: Record<number, LogLevel> = {
|
||||
1: 'Trace',
|
||||
2: 'Debug',
|
||||
3: 'Info',
|
||||
4: 'Warn',
|
||||
5: 'Error'
|
||||
};
|
||||
|
||||
const levelFromMessage = (message: string): LogLevel | undefined =>
|
||||
LEVEL_NAMES.find(level => message.includes(`[${level.toUpperCase()}]`));
|
||||
|
||||
export const persistedLogEntry = (message: string): LogEntry => ({
|
||||
level: levelFromMessage(message) || 'Debug',
|
||||
message
|
||||
});
|
||||
|
||||
export const liveLogEntry = (
|
||||
numericLevel: number,
|
||||
message: string,
|
||||
now: Date = new Date()
|
||||
): LogEntry => {
|
||||
const level = levelFromMessage(message) || LIVE_LEVELS[numericLevel] || 'Debug';
|
||||
const alreadyFormatted = /^\[\d{4}-\d{2}-\d{2}\]\[\d{2}:\d{2}:\d{2}\]\[(TRACE|DEBUG|INFO|WARN|ERROR)\]/.test(message);
|
||||
|
||||
return {
|
||||
level,
|
||||
message: alreadyFormatted
|
||||
? message
|
||||
: `[${now.toISOString().replace('T', ' ').substring(0, 19)}] [${level.toUpperCase()}] ${message}`
|
||||
};
|
||||
};
|
||||
|
||||
export const appendBoundedLogEntries = (
|
||||
current: LogEntry[],
|
||||
additions: LogEntry[],
|
||||
limit = MAX_LOG_LINES
|
||||
): LogEntry[] => {
|
||||
if (additions.length === 0) return current;
|
||||
const combined = [...current, ...additions];
|
||||
return combined.length > limit ? combined.slice(combined.length - limit) : combined;
|
||||
};
|
||||
|
||||
export const pushBoundedLogEntry = (
|
||||
queue: LogEntry[],
|
||||
entry: LogEntry,
|
||||
limit = MAX_LOG_LINES
|
||||
): void => {
|
||||
queue.push(entry);
|
||||
if (queue.length > limit) {
|
||||
queue.splice(0, queue.length - limit);
|
||||
}
|
||||
};
|
||||
|
||||
// The live target writes after the file target, so entries received while the
|
||||
// initial disk snapshot is loading can also appear at the snapshot's tail.
|
||||
// Remove only the exact ordered overlap; global message de-duplication would
|
||||
// incorrectly hide legitimate repeated log lines.
|
||||
export const mergeLogSnapshotAndLiveEntries = (
|
||||
snapshot: LogEntry[],
|
||||
liveEntries: LogEntry[],
|
||||
limit = MAX_LOG_LINES
|
||||
): LogEntry[] => {
|
||||
const maxOverlap = Math.min(snapshot.length, liveEntries.length);
|
||||
let overlap = 0;
|
||||
|
||||
for (let candidate = maxOverlap; candidate > 0; candidate -= 1) {
|
||||
const snapshotStart = snapshot.length - candidate;
|
||||
let matches = true;
|
||||
for (let index = 0; index < candidate; index += 1) {
|
||||
if (snapshot[snapshotStart + index].message !== liveEntries[index].message) {
|
||||
matches = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (matches) {
|
||||
overlap = candidate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return appendBoundedLogEntries(snapshot, liveEntries.slice(overlap), limit);
|
||||
};
|
||||
Reference in New Issue
Block a user