mirror of
https://github.com/nimbold/Firelink.git
synced 2026-08-25 18:17:21 +00:00
fix: close post-release lifecycle state gaps
This commit is contained in:
+49
-1
@@ -756,7 +756,14 @@ fn remove_persisted_transfer_secrets(value: &mut Value) {
|
|||||||
// These values are accepted from users, browser extensions, or URLs and
|
// These values are accepted from users, browser extensions, or URLs and
|
||||||
// may contain credentials or bearer tokens. Portable queues keep their
|
// may contain credentials or bearer tokens. Portable queues keep their
|
||||||
// useful metadata, but never persist these values beside the executable.
|
// useful metadata, but never persist these values beside the executable.
|
||||||
|
let mut removed_transfer_context = false;
|
||||||
for key in ["password", "cookies", "headers", "mirrors", "proxy"] {
|
for key in ["password", "cookies", "headers", "mirrors", "proxy"] {
|
||||||
|
if object
|
||||||
|
.get(key)
|
||||||
|
.is_some_and(|value| !value.is_null() && !value_is_empty(value))
|
||||||
|
{
|
||||||
|
removed_transfer_context = true;
|
||||||
|
}
|
||||||
object.remove(key);
|
object.remove(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -790,6 +797,20 @@ fn remove_persisted_transfer_secrets(value: &mut Value) {
|
|||||||
mark_portable_download_unresumable(object);
|
mark_portable_download_unresumable(object);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Do not silently resume a queued transfer after removing request
|
||||||
|
// credentials or other transfer-specific context. The URL may still be
|
||||||
|
// valid, but its semantics have changed and the user must re-add it with
|
||||||
|
// the required request settings.
|
||||||
|
if removed_transfer_context {
|
||||||
|
mark_portable_download_unresumable(object);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn value_is_empty(value: &Value) -> bool {
|
||||||
|
value.as_str().is_some_and(str::is_empty)
|
||||||
|
|| value.as_array().is_some_and(Vec::is_empty)
|
||||||
|
|| value.as_object().is_some_and(serde_json::Map::is_empty)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mark_portable_download_unresumable(object: &mut serde_json::Map<String, Value>) {
|
fn mark_portable_download_unresumable(object: &mut serde_json::Map<String, Value>) {
|
||||||
@@ -803,7 +824,7 @@ fn mark_portable_download_unresumable(object: &mut serde_json::Map<String, Value
|
|||||||
object.insert(
|
object.insert(
|
||||||
"lastError".to_string(),
|
"lastError".to_string(),
|
||||||
Value::String(
|
Value::String(
|
||||||
"Portable mode removed credentials from this persisted download; add it again to resume."
|
"Portable mode removed credentials or transfer settings from this persisted download; add it again to resume."
|
||||||
.to_string(),
|
.to_string(),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -1644,6 +1665,33 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn portable_download_persistence_marks_context_dependent_queue_items_unresumable() {
|
||||||
|
let temp = TempDir::new().unwrap();
|
||||||
|
let state = init_at_path(temp.path()).unwrap();
|
||||||
|
let mut connection = state.lock().unwrap();
|
||||||
|
let data = json!([{
|
||||||
|
"id": "download-context",
|
||||||
|
"status": "queued",
|
||||||
|
"queueId": "main",
|
||||||
|
"url": "https://example.com/file",
|
||||||
|
"headers": "Authorization: Bearer secret"
|
||||||
|
}])
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
replace_downloads(&mut connection, &data, true).unwrap();
|
||||||
|
|
||||||
|
let saved: Value = serde_json::from_str(&load_downloads(&connection).unwrap()[0]).unwrap();
|
||||||
|
assert_eq!(saved["url"], "https://example.com/file");
|
||||||
|
assert_eq!(saved["status"], "failed");
|
||||||
|
assert_eq!(saved["resumable"], false);
|
||||||
|
assert_eq!(
|
||||||
|
saved["lastError"],
|
||||||
|
"Portable mode removed credentials or transfer settings from this persisted download; add it again to resume."
|
||||||
|
);
|
||||||
|
assert!(saved.get("headers").is_none());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn portable_download_persistence_redacts_error_secrets_but_preserves_safe_errors_and_standard_details() {
|
fn portable_download_persistence_redacts_error_secrets_but_preserves_safe_errors_and_standard_details() {
|
||||||
let temp = TempDir::new().unwrap();
|
let temp = TempDir::new().unwrap();
|
||||||
|
|||||||
@@ -3515,6 +3515,12 @@ async fn pause_download(
|
|||||||
state.queue_manager.next_aria2_control_epoch(&id).await;
|
state.queue_manager.next_aria2_control_epoch(&id).await;
|
||||||
state.queue_manager.cancel_aria2_retries(&id).await;
|
state.queue_manager.cancel_aria2_retries(&id).await;
|
||||||
if retrying && matches!(terminal, "error" | "removed") {
|
if retrying && matches!(terminal, "error" | "removed") {
|
||||||
|
// The terminal GID was forgotten above, so this
|
||||||
|
// lifecycle can no longer be resumed in place. Release
|
||||||
|
// backend ownership as well; otherwise a paused row
|
||||||
|
// keeps a dead registration until another command happens
|
||||||
|
// to repair it.
|
||||||
|
state.queue_manager.release_registered_id(&id).await;
|
||||||
use tauri::Emitter;
|
use tauri::Emitter;
|
||||||
let _ = app_handle.emit(
|
let _ = app_handle.emit(
|
||||||
"download-state",
|
"download-state",
|
||||||
|
|||||||
@@ -589,6 +589,44 @@ describe('useDownloadStore', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('keeps accepted startup registrations when pending-order refresh fails', async () => {
|
||||||
|
vi.mocked(ipc.invokeCommand).mockImplementation(async (cmd: string) => {
|
||||||
|
if (cmd === 'db_get_all_queues') return [];
|
||||||
|
if (cmd === 'db_get_all_downloads') {
|
||||||
|
return [JSON.stringify({
|
||||||
|
id: 'startup-accepted',
|
||||||
|
url: 'https://example.com/file.bin',
|
||||||
|
fileName: 'file.bin',
|
||||||
|
status: 'queued',
|
||||||
|
category: 'Other',
|
||||||
|
dateAdded: '',
|
||||||
|
queueId: '00000000-0000-0000-0000-000000000001',
|
||||||
|
hasBeenDispatched: true
|
||||||
|
})];
|
||||||
|
}
|
||||||
|
if (cmd === 'enqueue_many') {
|
||||||
|
return [{ id: 'startup-accepted', success: true, filename: 'file.bin' }];
|
||||||
|
}
|
||||||
|
if (cmd === 'get_pending_order') throw new Error('queue state unavailable');
|
||||||
|
if (cmd === 'resume_download') return true;
|
||||||
|
return undefined;
|
||||||
|
});
|
||||||
|
|
||||||
|
await useDownloadStore.getState().initDB();
|
||||||
|
|
||||||
|
expect(useDownloadStore.getState().backendRegisteredIds.has('startup-accepted')).toBe(true);
|
||||||
|
expect(useDownloadStore.getState().downloads[0]).toMatchObject({
|
||||||
|
status: 'queued',
|
||||||
|
hasBeenDispatched: true
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(useDownloadStore.getState().startQueue('00000000-0000-0000-0000-000000000001'))
|
||||||
|
.resolves.toEqual(['startup-accepted']);
|
||||||
|
expect(
|
||||||
|
vi.mocked(ipc.invokeCommand).mock.calls.filter(call => call[0] === 'enqueue_download')
|
||||||
|
).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
it('redownloads fallback media without requiring a format selector', async () => {
|
it('redownloads fallback media without requiring a format selector', async () => {
|
||||||
useDownloadStore.setState({
|
useDownloadStore.setState({
|
||||||
downloads: [{
|
downloads: [{
|
||||||
@@ -620,6 +658,38 @@ describe('useDownloadStore', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('does not claim a redownload when removing the old backend lifecycle fails', async () => {
|
||||||
|
useDownloadStore.setState({
|
||||||
|
downloads: [{
|
||||||
|
id: 'redownload-remove-failed',
|
||||||
|
url: 'https://example.com/file.bin',
|
||||||
|
fileName: 'file.bin',
|
||||||
|
destination: '/tmp',
|
||||||
|
status: 'paused',
|
||||||
|
category: 'Other',
|
||||||
|
dateAdded: '2026-07-15T00:00:00.000Z',
|
||||||
|
hasBeenDispatched: true
|
||||||
|
}] as any[],
|
||||||
|
backendRegisteredIds: new Set(['redownload-remove-failed'])
|
||||||
|
});
|
||||||
|
vi.mocked(ipc.invokeCommand).mockImplementation(async (cmd: string) => {
|
||||||
|
if (cmd === 'remove_download') throw new Error('aria2 did not stop');
|
||||||
|
return undefined;
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(useDownloadStore.getState().redownload('redownload-remove-failed'))
|
||||||
|
.rejects.toThrow('aria2 did not stop');
|
||||||
|
expect(useDownloadStore.getState().downloads[0]).toMatchObject({
|
||||||
|
status: 'paused',
|
||||||
|
dateAdded: '2026-07-15T00:00:00.000Z',
|
||||||
|
hasBeenDispatched: true
|
||||||
|
});
|
||||||
|
expect(useDownloadStore.getState().backendRegisteredIds.has('redownload-remove-failed')).toBe(true);
|
||||||
|
expect(
|
||||||
|
vi.mocked(ipc.invokeCommand).mock.calls.some(call => call[0] === 'enqueue_download')
|
||||||
|
).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
it('starts and pauses all items regardless of legacy missing queue ids', async () => {
|
it('starts and pauses all items regardless of legacy missing queue ids', async () => {
|
||||||
useDownloadStore.setState({
|
useDownloadStore.setState({
|
||||||
downloads: [
|
downloads: [
|
||||||
|
|||||||
@@ -818,6 +818,7 @@ export const useDownloadStore = create<DownloadState>((set, get) => ({
|
|||||||
get().unregisterBackendIds([id]);
|
get().unregisterBackendIds([id]);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn("Could not remove old download from backend", e);
|
console.warn("Could not remove old download from backend", e);
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
get().updateDownload(id, {
|
get().updateDownload(id, {
|
||||||
@@ -1175,9 +1176,12 @@ export const useDownloadStore = create<DownloadState>((set, get) => ({
|
|||||||
.filter(result => !result.success)
|
.filter(result => !result.success)
|
||||||
.map(result => [result.id, result.error || 'Backend rejected the queued download.'])
|
.map(result => [result.id, result.error || 'Backend rejected the queued download.'])
|
||||||
);
|
);
|
||||||
const order = await invoke('get_pending_order', { queueId: null });
|
|
||||||
|
// Commit backend ownership as soon as enqueue_many accepts an item.
|
||||||
|
// The order query is a separate best-effort view read; if it fails,
|
||||||
|
// forgetting these registrations would let a later queue start
|
||||||
|
// enqueue the same backend lifecycle a second time.
|
||||||
set(state => ({
|
set(state => ({
|
||||||
pendingOrder: order,
|
|
||||||
backendRegisteredIds: new Set([
|
backendRegisteredIds: new Set([
|
||||||
...state.backendRegisteredIds,
|
...state.backendRegisteredIds,
|
||||||
...registeredIds
|
...registeredIds
|
||||||
@@ -1189,11 +1193,18 @@ export const useDownloadStore = create<DownloadState>((set, get) => ({
|
|||||||
status: 'failed' as const,
|
status: 'failed' as const,
|
||||||
lastError: failedErrors.get(download.id)
|
lastError: failedErrors.get(download.id)
|
||||||
}
|
}
|
||||||
: registeredIds.includes(download.id)
|
: registeredIds.includes(download.id)
|
||||||
? { ...download, hasBeenDispatched: true, lastError: undefined }
|
? { ...download, hasBeenDispatched: true, lastError: undefined }
|
||||||
: download
|
: download
|
||||||
)
|
)
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
try {
|
||||||
|
const order = await invoke('get_pending_order', { queueId: null });
|
||||||
|
set({ pendingOrder: order });
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Failed to refresh pending order after auto-resume:", e);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Failed to auto-resume active downloads:", e);
|
console.error("Failed to auto-resume active downloads:", e);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user