-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod_test.ts
75 lines (65 loc) · 2.49 KB
/
mod_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { CAREncoderStream, createFileEncoderStream } from 'https://esm.sh/ipfs-car@1.0.0?pin=v133'
import { CID } from 'https://esm.sh/multiformats@11.0.2/cid?pin=v133'
import { assertEquals, assertStringIncludes, describe, it } from './dev_deps.ts'
import { getObject, uploadCar } from './mod.ts'
const placeholderCID = CID.parse('bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi')
describe('getObject', () => {
it('should return a response of a file', async () => {
const res = await getObject({
bucketName: 'filebase-upload-tests',
token: Deno.env.get('FILEBASE_TOKEN')!,
filename: 'hello.txt',
})
assertEquals(await res.text(), 'Hello world')
assertEquals(
res.headers.get('x-amz-meta-cid'),
'QmNRCQWfgze6AbBCaT1rkrkV5tJ2aP4oTNPb5JZcXYywve',
)
})
it('should accept custom `apiUrl`', async () => {
try {
await getObject({
bucketName: 'filebase-upload-tests',
token: Deno.env.get('FILEBASE_TOKEN')!,
filename: 'hello.txt',
apiUrl: 'stauro.dev',
})
} catch (error) {
if (error.cause) {
assertStringIncludes(error.cause.hostname, 'filebase-upload-tests.stauro.dev')
} else assertStringIncludes(error.message, 'filebase-upload-tests.stauro.dev')
}
})
})
async function streamToBlob(stream: ReadableStream): Promise<Blob> {
const reader = stream.getReader()
const chunks: Uint8Array[] = []
while (true) {
const { done, value } = await reader.read()
if (done) break
chunks.push(value)
}
const blob = new Blob(chunks, { type: 'application/octet-stream' })
return blob
}
describe('uploadCar', () => {
it(
'should upload a CAR file and emit a CID from response headers',
async () => {
const stream = createFileEncoderStream(new Blob(['Hello ipfs-car!']))
.pipeThrough(
new TransformStream(),
)
.pipeThrough(new CAREncoderStream([placeholderCID]))
const blob = await streamToBlob(stream)
const file = new File([blob], 'file.car')
const res = await uploadCar({ bucketName: 'filebase-upload-tests', token: Deno.env.get('FILEBASE_TOKEN')!, file })
await res.body?.cancel()
const fetchResource = Object.keys(Deno.resources()).find((key) =>
Deno.resources()[parseInt(key)] === 'fetchResponse'
)
assertEquals(res.headers.get('x-amz-meta-cid'), 'bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi')
Deno.close(parseInt(fetchResource!)) // weird response
},
)
})