* Stream a presigned URL into the Output's chunk writer using the * deps-provided fetch impl, with no API-key headers — presigned URLs * carry their own authority. Three failure shapes the caller might * see, all routed through the typed envelope so `index.ts` produces * the documented exit code
(url: string, out: Output, deps: TestDeps)
| 7872 | * the reader may want to start indexing before the download finishes. |
| 7873 | */ |
| 7874 | async function streamPresignedBody(url: string, out: Output, deps: TestDeps): Promise<void> { |
| 7875 | const fetchImpl = deps.fetchImpl ?? globalThis.fetch.bind(globalThis); |
| 7876 | let response: Response; |
| 7877 | try { |
| 7878 | response = await fetchImpl(url); |
| 7879 | } catch (err) { |
| 7880 | const message = err instanceof Error ? err.message : String(err); |
| 7881 | throw new TransportError(`Failed to download presigned code body: ${message}`); |
| 7882 | } |
| 7883 | if (!response.ok) { |
| 7884 | throw ApiError.fromEnvelope({ |
| 7885 | error: { |
| 7886 | code: 'UNAVAILABLE', |
| 7887 | message: `Failed to download presigned code body (HTTP ${response.status}).`, |
| 7888 | nextAction: |
| 7889 | 'Re-run `testsprite test code get`. Presigned URLs expire after a short window.', |
| 7890 | requestId: 'local', |
| 7891 | details: { status: response.status, url }, |
| 7892 | }, |
| 7893 | }); |
| 7894 | } |
| 7895 | if (!response.body) { |
| 7896 | // No streamable body (some test runtimes / fetch polyfills). Fall |
| 7897 | // back to text() — same correctness, just no streaming benefit. |
| 7898 | await out.writeChunk(await response.text()); |
| 7899 | return; |
| 7900 | } |
| 7901 | const reader = response.body.getReader(); |
| 7902 | const decoder = new TextDecoder(); |
| 7903 | try { |
| 7904 | while (true) { |
| 7905 | const { value, done } = await reader.read(); |
| 7906 | if (done) break; |
| 7907 | if (value && value.byteLength > 0) { |
| 7908 | // `await` is load-bearing: the default rawStdout writer |
| 7909 | // resolves on `'drain'` when stdout's kernel buffer is full, |
| 7910 | // which pauses this loop so we don't pull more chunks from |
| 7911 | // the network than the consumer can absorb. |
| 7912 | await out.writeChunk(decoder.decode(value, { stream: true })); |
| 7913 | } |
| 7914 | } |
| 7915 | // Flush any remaining buffered bytes from a multi-byte UTF-8 codepoint |
| 7916 | // straddling a chunk boundary. Without this the last code point of a |
| 7917 | // file ending in (say) a Chinese character would be silently dropped. |
| 7918 | const tail = decoder.decode(); |
| 7919 | if (tail.length > 0) await out.writeChunk(tail); |
| 7920 | } catch (err) { |
| 7921 | const message = err instanceof Error ? err.message : String(err); |
| 7922 | throw new TransportError(`Failed mid-download of presigned code body: ${message}`); |
| 7923 | } |
| 7924 | } |
| 7925 | |
| 7926 | function renderTestListText(page: Page<CliTest>): string { |
| 7927 | if (page.items.length === 0) { |
no test coverage detected