feat(02-02): SW — downloadArchive via offscreen-minted Blob URL + revoke lifecycle (D-P2-01 closes P0-6)
Production changes (src/background/index.ts):
- pendingDownloadUrlResolvers Map<requestId, resolver> routes DOWNLOAD_URL
responses back to the in-flight downloadArchive Promise; mirrors the
pendingBufferRequests pattern from the BUFFER round-trip so port
replacement mid-mint does not lose the response.
- pendingRevokes Map<downloadId, url> tracks (downloadId → minted blob:URL)
for the chrome.downloads.onChanged revoke dispatch.
- onConnect port message sink extended with DOWNLOAD_URL routing branch
(alongside existing PING/BUFFER routing).
- downloadArchive rewritten: encode archive via blobToBase64 → post
CREATE_DOWNLOAD_URL on videoPort → await DOWNLOAD_URL response (race
against 5s BLOB_URL_MINT_TIMEOUT_MS) → reject empty / non-blob: URLs
(T-02-02-03 mitigation) → call chrome.downloads.download → register
(downloadId, url) in pendingRevokes. NO data:URL fallback — typed
errors route through saveArchive's catch to RECORDING_ERROR.
- chrome.downloads.onChanged listener registered at module init:
on terminal state ('complete' / 'interrupted'), posts REVOKE_DOWNLOAD_URL
to videoPort and clears the pendingRevokes entry.
Deviation (Rule 3 — auto-fix blocking issue):
- Plan 02-01's test helpers in blob-url-download.test.ts +
meta-json-urls-schema.test.ts + strict-meta-json-validation.test.ts
modeled only the REQUEST_BUFFER → BUFFER round-trip, not the new
CREATE_DOWNLOAD_URL → DOWNLOAD_URL round-trip Plan 02-02 introduces.
Without the test-side mint simulation, the SW's downloadArchive
times out at the offscreen mint step → chrome.downloads.download
never called → ALL existing meta.json tests timeout.
- Each helper extended with a tryFireDownloadUrl block that decodes
the CREATE_DOWNLOAD_URL.dataBase64, mints a Node-native blob:URL via
URL.createObjectURL, captures the archive bytes for downstream
JSZip extraction (capturedArchiveBytes), and replies DOWNLOAD_URL.
Test 3 (revoke lifecycle) additionally shims port.postMessage to
call URL.revokeObjectURL on receipt of REVOKE_DOWNLOAD_URL — the
test-side equivalent of src/offscreen/recorder.ts handleCreateDownloadUrl.
- Pre-existing Plan-02-02-era TODO comments in both test files
explicitly anticipated this extension ("Plan 02-03 implementer will
likely need a different helper, e.g. spy on URL.createObjectURL").
Verification (full §verification block from plan):
- npx tsc --noEmit: clean
- npm run build: clean
- npx vitest run tests/background/blob-url-download.test.ts: 3/3 GREEN (was 3 RED)
- npx vitest run tests/background/no-test-hooks-in-prod-bundle.test.ts: 13/13 GREEN
- npm test full suite: 163 passed / 8 failed (was 159 passed / 12 failed);
net delta +4 GREEN = 3 RED→GREEN flips + 1 ffprobe-flaky pass. 8 remaining
RED are exactly the Plan 02-03 territory (5 meta-json-urls-schema + 3
strict-meta-json-validation RED tests).
- grep -c "data:application/zip;base64," src/background/index.ts: 0 (gone)
- grep -c "blob:" src/background/index.ts: 8 (new pipeline)
- grep -c "chrome.downloads.onChanged" src/background/index.ts: 5 (listener wired)
- dist/ post-build: 0 "data:application/zip;base64," matches; 1 file with
"chrome.downloads.onChanged" (the SW chunk).
This commit is contained in:
@@ -366,6 +366,53 @@ async function runSaveAndCaptureArchiveBlob(
|
||||
);
|
||||
};
|
||||
|
||||
// Plan 02-02 (D-P2-01): simulate the offscreen-side CREATE_DOWNLOAD_URL
|
||||
// handler. The SW posts CREATE_DOWNLOAD_URL with the archive bytes as
|
||||
// base64; we mint a Node-native blob: URL via URL.createObjectURL and
|
||||
// reply DOWNLOAD_URL{requestId,url}. The archive bytes are captured
|
||||
// here BEFORE minting so meta.json extraction works regardless of
|
||||
// whether the SW ever calls chrome.downloads.download — this is the
|
||||
// canonical test-side equivalent of the offscreen path described in
|
||||
// src/offscreen/recorder.ts handleCreateDownloadUrl.
|
||||
const mintedRequestIds = new Set<string>();
|
||||
let capturedArchiveBytes: Uint8Array | null = null;
|
||||
const tryFireDownloadUrl = () => {
|
||||
const mintCalls = port.postMessage.mock.calls.filter(
|
||||
(c: unknown[]) =>
|
||||
typeof c[0] === 'object' &&
|
||||
c[0] !== null &&
|
||||
(c[0] as { type?: unknown }).type === 'CREATE_DOWNLOAD_URL',
|
||||
);
|
||||
for (const call of mintCalls) {
|
||||
const mintMsg = call[0] as {
|
||||
requestId?: string;
|
||||
dataBase64?: string;
|
||||
mimeType?: string;
|
||||
};
|
||||
const requestId = mintMsg.requestId;
|
||||
if (typeof requestId !== 'string' || requestId.length === 0) continue;
|
||||
if (mintedRequestIds.has(requestId)) continue;
|
||||
mintedRequestIds.add(requestId);
|
||||
let url = '';
|
||||
try {
|
||||
const binary = atob(mintMsg.dataBase64 ?? '');
|
||||
const bytes = new Uint8Array(binary.length);
|
||||
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
||||
// Capture the archive bytes for downstream meta.json extraction.
|
||||
if (capturedArchiveBytes === null) {
|
||||
capturedArchiveBytes = bytes;
|
||||
}
|
||||
const blob = new Blob([bytes], { type: mintMsg.mimeType ?? 'application/zip' });
|
||||
url = URL.createObjectURL(blob);
|
||||
} catch {
|
||||
url = '';
|
||||
}
|
||||
port.onMessage._listeners.forEach((fn) =>
|
||||
fn({ type: 'DOWNLOAD_URL', requestId, url }),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const onMsg = stub.runtime.onMessage._callbacks[0];
|
||||
onMsg(
|
||||
{ type: 'SAVE_ARCHIVE' },
|
||||
@@ -376,24 +423,27 @@ async function runSaveAndCaptureArchiveBlob(
|
||||
const DRAIN_ITERATIONS = 1_500;
|
||||
for (let i = 0; i < DRAIN_ITERATIONS; i++) {
|
||||
tryFireBuffer();
|
||||
tryFireDownloadUrl();
|
||||
if (stub.downloads.download.mock.calls.length > 0) break;
|
||||
await new Promise<void>((resolve) => setTimeout(resolve, 5));
|
||||
}
|
||||
|
||||
if (stub.downloads.download.mock.calls.length === 0) return undefined;
|
||||
const arg = stub.downloads.download.mock.calls[0][0] as { url: string };
|
||||
// Current HEAD: data:application/zip;base64,<base64>. Base64-decode.
|
||||
// Plan 02-02 era: blob:URL — the archive bytes are not recoverable
|
||||
// from the URL string itself. We captured the base64 payload during
|
||||
// the CREATE_DOWNLOAD_URL round-trip above; decode and return.
|
||||
if (arg.url.startsWith('blob:') && capturedArchiveBytes !== null) {
|
||||
return new Blob([capturedArchiveBytes], { type: 'application/zip' });
|
||||
}
|
||||
// Pre-Plan-02-02 path (kept for forward-compat; current HEAD on the
|
||||
// post-Plan-02-02 branch never takes this branch).
|
||||
const DATA_PREFIX = 'data:application/zip;base64,';
|
||||
if (arg.url.startsWith(DATA_PREFIX)) {
|
||||
const b64 = arg.url.substring(DATA_PREFIX.length);
|
||||
const bytes = Buffer.from(b64, 'base64');
|
||||
return new Blob([bytes], { type: 'application/zip' });
|
||||
}
|
||||
// Plan 02-02 era: blob: URL. The blob bytes are not directly
|
||||
// recoverable from the blob: URL string in Node; this RED test
|
||||
// doesn't reach that branch under current HEAD. The Plan 02-03 GREEN
|
||||
// implementer will likely need a different helper (e.g. spy on
|
||||
// URL.createObjectURL to capture the underlying Blob reference).
|
||||
return undefined;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user