MCPcopy Index your code
hub / github.com/ColmapView/Colmapview.github.io / readResponseStreamToBlob

Function readResponseStreamToBlob

src/utils/urlUtils.ts:156–182  ·  view source on GitHub ↗

* Read a fetch response body to a Blob, reporting bytes as they arrive. Builds * the Blob directly from the chunk array (no intermediate full-buffer copy) to * keep peak memory near one copy of the file.

(
  body: ReadableStream<Uint8Array>,
  total: number,
  onProgress: DownloadProgressCallback
)

Source from the content-addressed store, hash-verified

154 * keep peak memory near one copy of the file.
155 */
156async function readResponseStreamToBlob(
157 body: ReadableStream<Uint8Array>,
158 total: number,
159 onProgress: DownloadProgressCallback
160): Promise<Blob> {
161 const reader = body.getReader();
162 const chunks: Uint8Array<ArrayBuffer>[] = [];
163 let loaded = 0;
164 onProgress(0, total);
165 try {
166 for (;;) {
167 const { done, value } = await reader.read();
168 if (done) break;
169 // Copy into an ArrayBuffer-backed view: detaches from the stream buffer and
170 // satisfies the BlobPart type (the raw chunk is Uint8Array<ArrayBufferLike>).
171 chunks.push(new Uint8Array(value));
172 loaded += value.length;
173 onProgress(loaded, total);
174 }
175 } finally {
176 // Tear down the stream if the loop exits abnormally (read rejection, or a
177 // throwing progress callback) so the underlying connection isn't left open.
178 // A no-op once the stream is fully drained or already errored.
179 reader.cancel().catch(() => {});
180 }
181 return new Blob(chunks);
182}
183
184/**
185 * Extract filename from a URL path, percent-decoded so it matches the

Callers 1

readResponseToBlobFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected