Skip to content

Commit 1cd6bf4

Browse files
committed
refactor: allow download as stream
1 parent 9b7b2df commit 1cd6bf4

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
@@ -360,13 +360,14 @@ export default class StorageFileApi {
360360
*
361361
* @param path The full path and file name of the file to be downloaded. For example `folder/image.png`.
362362
* @param options.transform Transform the asset before serving it to the client.
363+
* @param options.stream If set to true, the response will be a ReadableStream. Otherwise, it will be a Blob (default).
363364
*/
364-
async download(
365+
async download<Options extends { transform?: TransformOptions, stream?: boolean }>(
365366
path: string,
366-
options?: { transform?: TransformOptions }
367+
options?: Options
367368
): Promise<
368369
| {
369-
data: Blob
370+
data: Options['stream'] extends true ? ReadableStream : Blob
370371
error: null
371372
}
372373
| {
@@ -385,8 +386,16 @@ export default class StorageFileApi {
385386
headers: this.headers,
386387
noResolveJson: true,
387388
})
388-
const data = await res.blob()
389-
return { data, error: null }
389+
390+
if (!options?.stream) {
391+
const data = await res.blob()
392+
return { data, error: null }
393+
}
394+
395+
return {
396+
data: res.body,
397+
error: null,
398+
}
390399
} catch (error) {
391400
if (isStorageError(error)) {
392401
return { data: null, error }

test/storageFileApi.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,16 @@ describe('Object API', () => {
201201
expect(res.data?.type).toEqual('text/plain;charset=utf-8')
202202
})
203203

204+
test('downloads an object as a stream', async () => {
205+
await storage.from(bucketName).upload(uploadPath, file)
206+
const res = await storage.from(bucketName).download(uploadPath, {
207+
stream: true,
208+
})
209+
210+
expect(res.error).toBeNull()
211+
expect(res.data).toBeInstanceOf(ReadableStream)
212+
})
213+
204214
test('removes an object', async () => {
205215
await storage.from(bucketName).upload(uploadPath, file)
206216
const res = await storage.from(bucketName).remove([uploadPath])

0 commit comments

Comments
 (0)