|
| 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