Skip to content

Commit 6b10592

Browse files
authored
fix: pass duplex option to the fetcher (#242)
* fix: pass duplex option to the fetcher * Add stream test in node env
1 parent b0e2359 commit 6b10592

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/lib/fetch.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface FetchOptions {
88
headers?: {
99
[key: string]: string
1010
}
11+
duplex?: string
1112
noResolveJson?: boolean
1213
}
1314

@@ -58,6 +59,10 @@ const _getRequestParams = (
5859
params.body = body
5960
}
6061

62+
if (options?.duplex) {
63+
params.duplex = options.duplex
64+
}
65+
6166
return { ...params, ...parameters }
6267
}
6368

test/storageFileApiNode.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
5+
import { StorageClient } from '../src/index'
6+
import * as fs from 'fs'
7+
import * as path from 'path'
8+
9+
const URL = 'http://localhost:8000/storage/v1'
10+
const KEY =
11+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiYXV0aGVudGljYXRlZCIsInN1YiI6IjMxN2VhZGNlLTYzMWEtNDQyOS1hMGJiLWYxOWE3YTUxN2I0YSIsImlhdCI6MTcxMzQzMzgwMCwiZXhwIjoyMDI5MDA5ODAwfQ.jVFIR-MB7rNfUuJaUH-_CyDFZEHezzXiqcRcdrGd29o'
12+
13+
const storage = new StorageClient(URL, { Authorization: `Bearer ${KEY}` })
14+
15+
const newBucket = async (isPublic = true, prefix = '') => {
16+
const bucketName = `${prefix ? prefix + '-' : ''}bucket-${Date.now()}`
17+
await storage.createBucket(bucketName, { public: isPublic })
18+
return bucketName
19+
}
20+
21+
const uploadFilePath = (fileName: string) => path.resolve(__dirname, 'fixtures', 'upload', fileName)
22+
23+
describe('Object API', () => {
24+
let bucketName: string
25+
beforeEach(async () => {
26+
bucketName = await newBucket()
27+
})
28+
29+
describe('Stream handling in node', () => {
30+
test('uploading stream with duplex option', async () => {
31+
const file = await fs.createReadStream(uploadFilePath('file.txt'))
32+
const uploadPathWithDuplex = `testpath/file-duplex-${Date.now()}.txt`
33+
34+
const res = await storage.from(bucketName).upload(uploadPathWithDuplex, file, {
35+
duplex: 'half',
36+
})
37+
expect(res.error).toBeNull()
38+
expect(res.data?.path).toEqual(uploadPathWithDuplex)
39+
})
40+
})
41+
})

0 commit comments

Comments
 (0)