|
| 1 | +import { unixfs } from '@helia/unixfs' |
| 2 | +import { stop } from '@libp2p/interface' |
| 3 | +import { expect } from 'aegir/chai' |
| 4 | +import browserReadableStreamToIt from 'browser-readablestream-to-it' |
| 5 | +import all from 'it-all' |
| 6 | +import last from 'it-last' |
| 7 | +import { pipe } from 'it-pipe' |
| 8 | +import { extract } from 'it-tar' |
| 9 | +import toBuffer from 'it-to-buffer' |
| 10 | +import { VerifiedFetch } from '../src/verified-fetch.js' |
| 11 | +import { createHelia } from './fixtures/create-offline-helia.js' |
| 12 | +import type { Helia } from '@helia/interface' |
| 13 | +import type { FileCandidate } from 'ipfs-unixfs-importer' |
| 14 | + |
| 15 | +describe('tar files', () => { |
| 16 | + let helia: Helia |
| 17 | + let verifiedFetch: VerifiedFetch |
| 18 | + |
| 19 | + beforeEach(async () => { |
| 20 | + helia = await createHelia() |
| 21 | + verifiedFetch = new VerifiedFetch({ |
| 22 | + helia |
| 23 | + }) |
| 24 | + }) |
| 25 | + |
| 26 | + afterEach(async () => { |
| 27 | + await stop(helia, verifiedFetch) |
| 28 | + }) |
| 29 | + |
| 30 | + it('should support fetching a TAR file', async () => { |
| 31 | + const file = Uint8Array.from([0, 1, 2, 3, 4]) |
| 32 | + const fs = unixfs(helia) |
| 33 | + const cid = await fs.addBytes(file) |
| 34 | + |
| 35 | + const resp = await verifiedFetch.fetch(cid, { |
| 36 | + headers: { |
| 37 | + accept: 'application/x-tar' |
| 38 | + } |
| 39 | + }) |
| 40 | + expect(resp.status).to.equal(200) |
| 41 | + expect(resp.headers.get('content-type')).to.equal('application/x-tar') |
| 42 | + expect(resp.headers.get('content-disposition')).to.equal(`attachment; filename="${cid.toString()}.tar"`) |
| 43 | + |
| 44 | + if (resp.body == null) { |
| 45 | + throw new Error('Download failed') |
| 46 | + } |
| 47 | + |
| 48 | + const entries = await pipe( |
| 49 | + browserReadableStreamToIt(resp.body), |
| 50 | + extract(), |
| 51 | + async source => all(source) |
| 52 | + ) |
| 53 | + |
| 54 | + expect(entries).to.have.lengthOf(1) |
| 55 | + await expect(toBuffer(entries[0].body)).to.eventually.deep.equal(file) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should support fetching a TAR file containing a directory', async () => { |
| 59 | + const directory: FileCandidate[] = [{ |
| 60 | + path: 'foo.txt', |
| 61 | + content: Uint8Array.from([0, 1, 2, 3, 4]) |
| 62 | + }, { |
| 63 | + path: 'bar.txt', |
| 64 | + content: Uint8Array.from([5, 6, 7, 8, 9]) |
| 65 | + }, { |
| 66 | + path: 'baz/qux.txt', |
| 67 | + content: Uint8Array.from([1, 2, 3, 4, 5]) |
| 68 | + }] |
| 69 | + |
| 70 | + const fs = unixfs(helia) |
| 71 | + const importResult = await last(fs.addAll(directory, { |
| 72 | + wrapWithDirectory: true |
| 73 | + })) |
| 74 | + |
| 75 | + if (importResult == null) { |
| 76 | + throw new Error('Import failed') |
| 77 | + } |
| 78 | + |
| 79 | + const resp = await verifiedFetch.fetch(importResult.cid, { |
| 80 | + headers: { |
| 81 | + accept: 'application/x-tar' |
| 82 | + } |
| 83 | + }) |
| 84 | + expect(resp.status).to.equal(200) |
| 85 | + expect(resp.headers.get('content-type')).to.equal('application/x-tar') |
| 86 | + expect(resp.headers.get('content-disposition')).to.equal(`attachment; filename="${importResult.cid.toString()}.tar"`) |
| 87 | + |
| 88 | + if (resp.body == null) { |
| 89 | + throw new Error('Download failed') |
| 90 | + } |
| 91 | + |
| 92 | + const entries = await pipe( |
| 93 | + browserReadableStreamToIt(resp.body), |
| 94 | + extract(), |
| 95 | + async source => all(source) |
| 96 | + ) |
| 97 | + |
| 98 | + expect(entries).to.have.lengthOf(5) |
| 99 | + expect(entries[0]).to.have.nested.property('header.name', importResult.cid.toString()) |
| 100 | + |
| 101 | + expect(entries[1]).to.have.nested.property('header.name', `${importResult.cid}/${directory[1].path}`) |
| 102 | + await expect(toBuffer(entries[1].body)).to.eventually.deep.equal(directory[1].content) |
| 103 | + |
| 104 | + expect(entries[2]).to.have.nested.property('header.name', `${importResult.cid}/${directory[2].path?.split('/')[0]}`) |
| 105 | + |
| 106 | + expect(entries[3]).to.have.nested.property('header.name', `${importResult.cid}/${directory[2].path}`) |
| 107 | + await expect(toBuffer(entries[3].body)).to.eventually.deep.equal(directory[2].content) |
| 108 | + |
| 109 | + expect(entries[4]).to.have.nested.property('header.name', `${importResult.cid}/${directory[0].path}`) |
| 110 | + await expect(toBuffer(entries[4].body)).to.eventually.deep.equal(directory[0].content) |
| 111 | + }) |
| 112 | + |
| 113 | + it('should support fetching a TAR file by format', async () => { |
| 114 | + const file = Uint8Array.from([0, 1, 2, 3, 4]) |
| 115 | + const fs = unixfs(helia) |
| 116 | + const cid = await fs.addBytes(file) |
| 117 | + |
| 118 | + const resp = await verifiedFetch.fetch(`ipfs://${cid}?format=tar`) |
| 119 | + expect(resp.status).to.equal(200) |
| 120 | + expect(resp.headers.get('content-type')).to.equal('application/x-tar') |
| 121 | + expect(resp.headers.get('content-disposition')).to.equal(`attachment; filename="${cid.toString()}.tar"`) |
| 122 | + }) |
| 123 | + |
| 124 | + it('should support specifying a filename for a TAR file', async () => { |
| 125 | + const file = Uint8Array.from([0, 1, 2, 3, 4]) |
| 126 | + const fs = unixfs(helia) |
| 127 | + const cid = await fs.addBytes(file) |
| 128 | + |
| 129 | + const resp = await verifiedFetch.fetch(`ipfs://${cid}?filename=foo.bar`, { |
| 130 | + headers: { |
| 131 | + accept: 'application/x-tar' |
| 132 | + } |
| 133 | + }) |
| 134 | + expect(resp.status).to.equal(200) |
| 135 | + expect(resp.headers.get('content-type')).to.equal('application/x-tar') |
| 136 | + expect(resp.headers.get('content-disposition')).to.equal('attachment; filename="foo.bar"') |
| 137 | + }) |
| 138 | + |
| 139 | + it('should support fetching a TAR file by format with a filename', async () => { |
| 140 | + const file = Uint8Array.from([0, 1, 2, 3, 4]) |
| 141 | + const fs = unixfs(helia) |
| 142 | + const cid = await fs.addBytes(file) |
| 143 | + |
| 144 | + const resp = await verifiedFetch.fetch(`ipfs://${cid}?format=tar&filename=foo.bar`) |
| 145 | + expect(resp.status).to.equal(200) |
| 146 | + expect(resp.headers.get('content-type')).to.equal('application/x-tar') |
| 147 | + expect(resp.headers.get('content-disposition')).to.equal('attachment; filename="foo.bar"') |
| 148 | + }) |
| 149 | +}) |
0 commit comments