From 94a8bf86098020e5e0467053ab7ba04186714a04 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sun, 16 Aug 2026 15:16:12 +0800 Subject: [PATCH] fix(flash-transfer): retry get_flash_file_url until the download id appears create_flash_task can return before the file is downloadable, so an immediate get_flash_file_url failed with no download url (#364). --- .../core/src/bridge/apis/flash-transfer.ts | 35 ++++--- .../tests/flash-transfer-download.test.ts | 99 +++++++++++++++++++ 2 files changed, 123 insertions(+), 11 deletions(-) create mode 100644 packages/core/tests/flash-transfer-download.test.ts diff --git a/packages/core/src/bridge/apis/flash-transfer.ts b/packages/core/src/bridge/apis/flash-transfer.ts index c60fec64..220b2fd6 100644 --- a/packages/core/src/bridge/apis/flash-transfer.ts +++ b/packages/core/src/bridge/apis/flash-transfer.ts @@ -35,6 +35,10 @@ import { deflateSync } from 'node:zlib'; const FLASH_SLICE_SIZE = 1024 * 1024; +/** After create returns, 0x93d4 can still omit the main-file fileId for a + * few seconds (#364). Re-query on this schedule (first lookup is immediate). */ +const FLASH_FILE_ID_RETRY_DELAYS_MS = [1000, 2000, 4000, 4000] as const; + /** Fail cleanly if the staged file changed since the streaming hash pass. */ async function assertUnchanged(filePath: string, baseline: { size: number; mtimeMs: number }): Promise { const now = await fsp.stat(filePath); @@ -267,21 +271,30 @@ export class FlashTransferApi { * 多文件时每条 f6=序号、f14=主文件 fileId),按 fileIndex 选中后走 0x12a9 sub=200 * 拿主文件直链。0x93d3/0x93d4 的 downloadUrl 字段是缩略图(appid=14903/14902), * 主文件必须走 0x12a9 sub=200。 + * + * create 刚返回时 f14 可能还是空的(#364)。fileId 空则按 + * FLASH_FILE_ID_RETRY_DELAYS_MS 再问 0x93d4;已经有 fileId 后 0x12a9 只打一枪。 */ private async getFileDownload( filesetUuid: string, fileIndex: number = 1, ): Promise<{ url: string; fileName: string; fileSize: number } | null> { - const metas = await GetDownloadUrl.invoke(this.ctx, { filesetUuid }); - const meta = metas.find((m) => m.fileIndex === fileIndex); - if (!meta || !meta.fileId) return null; - const url = await GetFlashDownload.invoke(this.ctx, { - filesetUuid: meta.filesetUuid, - fileUuid: meta.fileUuid, - fileId: meta.fileId, - fileName: meta.fileName, - }); - if (!url) return null; - return { url, fileName: meta.fileName, fileSize: meta.fileSize }; + for (let attempt = 0; ; attempt++) { + const metas = await GetDownloadUrl.invoke(this.ctx, { filesetUuid }); + const meta = metas.find((m) => m.fileIndex === fileIndex); + if (meta?.fileId) { + const url = await GetFlashDownload.invoke(this.ctx, { + filesetUuid: meta.filesetUuid, + fileUuid: meta.fileUuid, + fileId: meta.fileId, + fileName: meta.fileName, + }); + if (!url) return null; + return { url, fileName: meta.fileName, fileSize: meta.fileSize }; + } + const delay = FLASH_FILE_ID_RETRY_DELAYS_MS[attempt]; + if (delay === undefined) return null; + await new Promise((resolve) => setTimeout(resolve, delay)); + } } /** diff --git a/packages/core/tests/flash-transfer-download.test.ts b/packages/core/tests/flash-transfer-download.test.ts new file mode 100644 index 00000000..7dfe4ef0 --- /dev/null +++ b/packages/core/tests/flash-transfer-download.test.ts @@ -0,0 +1,99 @@ +// Regression for #364: create_flash_task can return before 0x93d4 fills +// the main-file fileId. get_flash_file_url / download_fileset poll 0x93d4 +// while that field is empty, then take the normal 0x12a9 sub=200 path. + +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { GetDownloadUrl } from '@snowluma/protocol/oidb-services/flash-transfer/get-download-url'; +import { GetFlashDownload } from '@snowluma/protocol/oidb-services/flash-transfer/get-flash-download'; +import { FlashTransferApi } from '../src/bridge/apis/flash-transfer'; + +const FILESET = '8982c4b5-195c-49e2-a3ab-478b32af234c'; +const FILE_ID = 'main-file-id'; +const URL = 'https://multimedia.qfile.qq.com/download?appid=14901&fileid=main-file-id'; + +function meta(fileId: string, fileIndex = 1): GetDownloadUrl.FileMeta { + return { + fileIndex, + fileId, + filesetUuid: FILESET, + fileUuid: 'file-uuid', + fileName: 'sl364-hitpass.txt', + fileSize: 41, + }; +} + +function api(): FlashTransferApi { + return new FlashTransferApi({} as never); +} + +describe('FlashTransferApi.getFlashFileUrl — empty fileId poll (#364)', () => { + beforeEach(() => { + vi.useFakeTimers(); + }); + + afterEach(() => { + vi.useRealTimers(); + vi.restoreAllMocks(); + }); + + it('does not wait when 0x93d4 already has the main-file fileId', async () => { + const lookup = vi.spyOn(GetDownloadUrl, 'invoke').mockResolvedValue([meta(FILE_ID)]); + const download = vi.spyOn(GetFlashDownload, 'invoke').mockResolvedValue(URL); + + await expect(api().getFlashFileUrl(FILESET)).resolves.toBe(URL); + expect(lookup).toHaveBeenCalledOnce(); + expect(download).toHaveBeenCalledOnce(); + expect(download.mock.calls[0]![1]).toMatchObject({ fileId: FILE_ID, filesetUuid: FILESET }); + }); + + it('re-queries 0x93d4 after an empty fileId and then takes the download path', async () => { + const lookup = vi.spyOn(GetDownloadUrl, 'invoke') + .mockResolvedValueOnce([meta('')]) + .mockResolvedValueOnce([meta(FILE_ID)]); + const download = vi.spyOn(GetFlashDownload, 'invoke').mockResolvedValue(URL); + + const pending = api().getFlashFileUrl(FILESET); + await vi.advanceTimersByTimeAsync(1000); + await expect(pending).resolves.toBe(URL); + + expect(lookup).toHaveBeenCalledTimes(2); + expect(download).toHaveBeenCalledOnce(); + expect(download.mock.calls[0]![1]).toMatchObject({ fileId: FILE_ID }); + }); + + it('shares the same poll for download_fileset', async () => { + vi.spyOn(GetDownloadUrl, 'invoke') + .mockResolvedValueOnce([meta('')]) + .mockResolvedValueOnce([meta(FILE_ID)]); + vi.spyOn(GetFlashDownload, 'invoke').mockResolvedValue(URL); + + const pending = api().downloadFileset(FILESET); + await vi.advanceTimersByTimeAsync(1000); + await expect(pending).resolves.toEqual({ + url: URL, + fileName: 'sl364-hitpass.txt', + fileSize: 41, + }); + }); + + it('keeps the original error when fileId stays empty through the budget', async () => { + const lookup = vi.spyOn(GetDownloadUrl, 'invoke').mockResolvedValue([meta('')]); + const download = vi.spyOn(GetFlashDownload, 'invoke').mockResolvedValue(URL); + + const pending = api().getFlashFileUrl(FILESET); + const expectFailed = expect(pending).rejects.toThrow('get_flash_file_url: no download url available'); + await vi.advanceTimersByTimeAsync(20_000); + await expectFailed; + + expect(lookup.mock.calls.length).toBeGreaterThan(1); + expect(download).not.toHaveBeenCalled(); + }); + + it('does not re-query 0x93d4 when fileId is present but 0x12a9 returns no url', async () => { + const lookup = vi.spyOn(GetDownloadUrl, 'invoke').mockResolvedValue([meta(FILE_ID)]); + vi.spyOn(GetFlashDownload, 'invoke').mockResolvedValue(null); + + await expect(api().getFlashFileUrl(FILESET)).rejects.toThrow('get_flash_file_url: no download url available'); + expect(lookup).toHaveBeenCalledOnce(); + }); +});