Skip to content

Commit cbc6251

Browse files
committed
refactor: allow download as stream
1 parent ae7bbb0 commit cbc6251

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/packages/StorageFileApi.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -521,13 +521,14 @@ export default class StorageFileApi {
521521
*
522522
* @param path The full path and file name of the file to be downloaded. For example `folder/image.png`.
523523
* @param options.transform Transform the asset before serving it to the client.
524+
* @param options.stream If set to true, the response will be a ReadableStream. Otherwise, it will be a Blob (default).
524525
*/
525-
async download(
526+
async download<Options extends { transform?: TransformOptions, stream?: boolean }>(
526527
path: string,
527-
options?: { transform?: TransformOptions }
528+
options?: Options
528529
): Promise<
529530
| {
530-
data: Blob
531+
data: Options['stream'] extends true ? ReadableStream : Blob
531532
error: null
532533
}
533534
| {
@@ -546,8 +547,16 @@ export default class StorageFileApi {
546547
headers: this.headers,
547548
noResolveJson: true,
548549
})
549-
const data = await res.blob()
550-
return { data, error: null }
550+
551+
if (!options?.stream) {
552+
const data = await res.blob()
553+
return { data, error: null }
554+
}
555+
556+
return {
557+
data: res.body,
558+
error: null,
559+
}
551560
} catch (error) {
552561
if (this.shouldThrowOnError) {
553562
throw error

test/storageFileApi.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,16 @@ describe('Object API', () => {
420420
).rejects.toThrow()
421421
})
422422

423+
test('downloads an object as a stream', async () => {
424+
await storage.from(bucketName).upload(uploadPath, file)
425+
const res = await storage.from(bucketName).download(uploadPath, {
426+
stream: true,
427+
})
428+
429+
expect(res.error).toBeNull()
430+
expect(res.data).toBeInstanceOf(ReadableStream)
431+
})
432+
423433
test('removes an object', async () => {
424434
await storage.from(bucketName).upload(uploadPath, file)
425435
const res = await storage.from(bucketName).remove([uploadPath])

0 commit comments

Comments
 (0)