diff --git a/src/adapters/browser-files/download-delivery-adapter.ts b/src/adapters/browser-files/download-delivery-adapter.ts index 626c0f70..91ecfa72 100644 --- a/src/adapters/browser-files/download-delivery-adapter.ts +++ b/src/adapters/browser-files/download-delivery-adapter.ts @@ -731,34 +731,34 @@ async function boundedObjectUrlHandoff(context: Readonly<{ ); } const sourceHolder = createSourceHolder(); - const sourceResult = await resolveByteSource( - context.input, - context.input.signal, - context.options, - sourceHolder, - ); - if (!sourceResult.ok) { - closeHeldSource(sourceHolder); - return observeResult(sourceResult, context.options.observer); - } - const source = sourceResult.value; - if ( - source.byteLength !== null && - (source.byteLength > context.input.maxBufferedBytes || - source.byteLength > context.input.maxTransferBytes) - ) { - closeHeldSource(sourceHolder); - return observeResult( - browserDataFailure("LIMIT_EXCEEDED", "DOWNLOAD"), - context.options.observer, - source.byteLength, - ); - } - const chunks: ArrayBuffer[] = []; let transferred = 0; let lease: ObjectUrlLease | undefined; try { + // Inside the try: an open that throws or is aborted must still resolve to + // a Result and reach the finally that latches the holder for late leases. + const sourceResult = await resolveByteSource( + context.input, + context.input.signal, + context.options, + sourceHolder, + ); + if (!sourceResult.ok) { + return observeResult(sourceResult, context.options.observer); + } + const source = sourceResult.value; + if ( + source.byteLength !== null && + (source.byteLength > context.input.maxBufferedBytes || + source.byteLength > context.input.maxTransferBytes) + ) { + return observeResult( + browserDataFailure("LIMIT_EXCEEDED", "DOWNLOAD"), + context.options.observer, + source.byteLength, + ); + } + context.progress.report("PREPARING", 0, source.byteLength, true); for await (const chunkResult of abortableFileChunks( source, diff --git a/tests/unit/browser-file-download.test.ts b/tests/unit/browser-file-download.test.ts index e56232cb..9c0890c4 100644 --- a/tests/unit/browser-file-download.test.ts +++ b/tests/unit/browser-file-download.test.ts @@ -746,6 +746,70 @@ describe("browser download delivery", () => { expect(handoff).not.toHaveBeenCalled(); }); + it("resolves ABORTED and closes a late lease when a bounded source open is aborted", async () => { + const controller = new AbortController(); + let releaseOpen: ((value: unknown) => void) | undefined; + const close = vi.fn(); + const handoff = vi.fn(); + const record = vi.fn(); + const adapter = createDownloadDeliveryAdapter({ + ...HARD_LIMITS, + policies, + host: { handoff }, + browserManagedCapabilities: capabilityResolver(), + userActivation: { isActive: true }, + observer: { record }, + // Ignores the signal and resolves only when the test says so. + openAuthorizedSource: () => + new Promise((resolve) => { + releaseOpen = resolve; + }) as never, + }); + const capability = Object.freeze({ + capabilityReceipt: "capability-bounded-1", + method: "GET" as const, + binding: { kind: "DOWNLOAD" as const, resourceId: "resource-1" }, + mediaType: "application/pdf", + byteLength: 3, + maxBytes: 3, + expectedSha256: "a".repeat(64), + expiresAtEpochMs: Number.MAX_SAFE_INTEGER, + }); + + const delivering = adapter.deliver( + deliveryInput( + { + kind: "AUTHORIZED_STREAM_RESOURCE", + resourceId: "resource-1", + capability: capability as never, + }, + "BOUNDED_OBJECT_URL", + controller.signal, + ), + ); + await new Promise((resolve) => setTimeout(resolve, 0)); + controller.abort(); + + await expect(delivering).resolves.toMatchObject({ + ok: false, + error: { code: "ABORTED" }, + }); + expect(record).toHaveBeenCalled(); + releaseOpen?.({ + ok: true, + value: { + byteLength: 3, + integrity: "VERIFIED_ON_SUCCESSFUL_EXHAUSTION", + capability, + close, + async *stream() {}, + }, + }); + await new Promise((resolve) => setTimeout(resolve, 0)); + expect(close).toHaveBeenCalledOnce(); + expect(handoff).not.toHaveBeenCalled(); + }); + it("rejects caller limits above runtime hard caps before side effects", async () => { const handoff = vi.fn(); const showSaveFilePicker = vi.fn(async () => writableHandle([]));