Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: download tars from @helia/verified-fetch #442

Merged
merged 10 commits into from
Feb 22, 2024

Conversation

achingbrain
Copy link
Member

Adds support for downloading tar archives of UnixFS directories

Let users get raw data back from CIDs that would otherwise trigger
decoding as JSON or CBOR etc by specifying an `Accept` header.

```typescript
const res = await verifiedFetch(cid, {
  headers: {
    accept: 'application/octet-stream'
  }
})
console.info(res.headers.get('accept')) // application/octet-stream
```

Make sure the content-type matches the accept header:

```typescript
const res = await verifiedFetch(cid, {
  headers: {
    accept: 'application/vnd.ipld.raw'
  }
})
console.info(res.headers.get('accept')) // application/vnd.ipld.raw
```

Support multiple values, match the first one:

```typescript
const res = await verifiedFetch(cid, {
  headers: {
    accept: 'application/vnd.ipld.raw, application/octet-stream, */*'
  }
})
console.info(res.headers.get('accept')) // application/vnd.ipld.raw
```

If they specify an Accept header we can't honor, return a [406](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/406):

```typescript
const res = await verifiedFetch(cid, {
  headers: {
    accept: 'application/what-even-is-this'
  }
})
console.info(res.status) // 406
```
Adds support for the `application/vnd.ipld.car` accept header to
allow downloading CAR files of DAGs.
Adds support for downloading tar archives of UnixFS directories
@achingbrain achingbrain requested a review from a team as a code owner February 21, 2024 13:37
Copy link
Member

@SgtPooki SgtPooki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

packages/verified-fetch/test/tar.spec.ts Outdated Show resolved Hide resolved
Comment on lines +98 to +111
expect(entries).to.have.lengthOf(5)
expect(entries[0]).to.have.nested.property('header.name', importResult.cid.toString())

expect(entries[1]).to.have.nested.property('header.name', `${importResult.cid}/${directory[1].path}`)
await expect(toBuffer(entries[1].body)).to.eventually.deep.equal(directory[1].content)

expect(entries[2]).to.have.nested.property('header.name', `${importResult.cid}/${directory[2].path?.split('/')[0]}`)

expect(entries[3]).to.have.nested.property('header.name', `${importResult.cid}/${directory[2].path}`)
await expect(toBuffer(entries[3].body)).to.eventually.deep.equal(directory[2].content)

expect(entries[4]).to.have.nested.property('header.name', `${importResult.cid}/${directory[0].path}`)
await expect(toBuffer(entries[4].body)).to.eventually.deep.equal(directory[0].content)
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the order here seems odd.. should ${importResult.cid}/${directory[0].path} be at entries[1] ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alex let me know it-tar handles ordering and it's stable, so it should be fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some interesting discussion on tar file ordering here: https://unix.stackexchange.com/questions/120143/how-is-the-order-in-which-tar-works-on-files-determined

Either way, it's external to @helia/verified-fetch.

name: file.path,
mode,
mtime,
size: Number(file.size),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to squash the bigint from Exportable.size to a Number here?

Copy link
Member Author

@achingbrain achingbrain Feb 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to convert it because it-tar expects the field to be a number.

We may lose some precision but Number.MAX_SAFE_INTEGER is 9PB so famous last words but I think files of that size may be uncommon.

Base automatically changed from feat/download-cars-from-verified-fetch to main February 22, 2024 10:16
achingbrain and others added 2 commits February 22, 2024 10:17
Co-authored-by: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com>
@achingbrain achingbrain merged commit 2d965be into main Feb 22, 2024
18 checks passed
@achingbrain achingbrain deleted the feat/download-tars-from-verified-fetch branch February 22, 2024 10:55
@achingbrain achingbrain mentioned this pull request Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants