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: support redirects for UnixFS directories #5

Merged
merged 2 commits into from
Mar 3, 2024

Conversation

achingbrain
Copy link
Member

@achingbrain achingbrain commented Feb 29, 2024

Adds support for simulating redirects for UnixFS directories.

We're somewhat in uncharted water here because window.fetch does this transparently unless you specify a redirect option, none of which actually allow you to manually follow a redirect.

The states we can be in are:

  1. URL: ipfs://QmFoo/dir/
  • Happy path
  • 200 response
  • response.redirected = false
  • response.url = 'ipfs://QmFoo/dir'

2: URL: ipfs://QmFoo/dir, redirect: 'follow'

  • The default value
  • Simulates automatically following a redirect
  • 200 response
  • response.redirected = true
  • response.url = 'ipfs://QmFoo/dir/'

3: URL: ipfs://QmFoo/dir, redirect: 'error'

  • Throw an error if a redirect would take place
  • Throws TypeError('Failed to Fetch') same as window.fetch

4: URL: ipfs://QmFoo/dir, redirect: 'manual'

  • Allows a caller to take action on the redirect
  • 301 response
  • response.redirected = false
  • response.url = 'ipfs://QmFoo/dir
  • response.headers.get('location') = 'ipfs://QmFoo/dir/'

Number 4 is the furthest from the fetch spec but to follow the spec would make it impossible to actually follow a redirect.

Refs: ipfs/service-worker-gateway#62

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation if necessary (this includes comments as well)
  • I have added tests that prove my fix is effective or that my feature works

Adds support for simulating redirects for UnixFS directories.

We're somewhat in uncharted water here because window.fetch does this
transparently unless you specify a `redirect` option, none of which
actually allow you to follow a redirect.

The states we can be in are:

1. URL: `ipfs://QmFoo/dir/`
  - Happy path
  - 200 response
  - `response.redirected = false`
  - `response.url = 'ipfs://QmFoo/dir'`

2: URL: `ipfs://QmFoo/dir`, `redirect: 'follow'`
  - The default value
  - Simulates automatically following a redirect
  - 200 response
  - `response.redirected = true`
  - `response.url = 'ipfs://QmFoo/dir/'`

3: URL: `ipfs://QmFoo/dir`, `redirect: 'error'`
  - Return an error if a redirect would take place
  - Throws `TypeError('Failed to Fetch')` same as `window.fetch`

4: URL: `ipfs://QmFoo/dir`, `redirect: 'manual'`
  - Allows a caller to take action on the redirect
  - 301 response
  - `response.redirected = false`
  - `response.url = 'ipfs://QmFoo/dir`
  - `response.headers.get('location') = 'ipfs://QmFoo/dir/'`

Number 4 is the furthest from [the fetch spec](https://fetch.spec.whatwg.org/#concept-request-redirect-mode)
but to follow the spec would make it impossible to actually follow a
redirect.
Copy link
Member

@lidel lidel left a comment

Choose a reason for hiding this comment

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

Thank you for wiring this up!

Option 4 sounds like a sensible compromise to implement fix for ipfs/service-worker-gateway#62, and will make things even easier in future when we accept HTTP URLs as-is.

The { redirect: 'manual' } is a very niche feature, and the fact we don't have opaque response (or any other divergence from regular fetch) should not have negative impact real world users.

The "spirit" of https://fetch.spec.whatwg.org/#atomic-http-redirect-handling is to not leak cross-origin information not otherwise available, and we don't introduce any new info in location header or body (anyone can grab CID from URL and learn it is a directory themselves).

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.

Seems like a reasonable approach. I do wonder if modifying the response type with Object.defineProperty will mess with caching the response at all

@SgtPooki SgtPooki merged commit 4601d46 into main Mar 3, 2024
20 checks passed
@SgtPooki SgtPooki deleted the feat/support-redirects-for-directories branch March 3, 2024 00:02
github-actions bot pushed a commit that referenced this pull request Mar 3, 2024
## @helia/verified-fetch [1.1.0](https://github.com/ipfs/helia-verified-fetch/compare/@helia/verified-fetch-1.0.2...@helia/verified-fetch-1.1.0) (2024-03-03)

### Features

* support IPFS/IPNS paths, Gateways, etc ([#4](#4)) ([e7f1816](e7f1816))
* support redirects for UnixFS directories ([#5](#5)) ([4601d46](4601d46))

### Trivial Changes

* unskip IPNS test ([#6](#6)) ([76485a4](76485a4))
Copy link

github-actions bot commented Mar 3, 2024

🎉 This PR is included in version 1.1.0 🎉

The release is available on npm package (@latest dist-tag)

Your semantic-release bot 📦🚀

Copy link

github-actions bot commented Mar 3, 2024

🎉 This PR is included in version 1.3.0 🎉

The release is available on npm package (@latest dist-tag)

Your semantic-release bot 📦🚀

lidel added a commit to ipfs/service-worker-gateway that referenced this pull request Mar 4, 2024
This ensures fix introduced in
ipfs/helia-verified-fetch#5
is returned to the browser, instead of following it and returning final
bytes.
@SgtPooki SgtPooki mentioned this pull request Mar 25, 2024
3 tasks
Copy link

github-actions bot commented May 9, 2024

🎉 This PR is included in version 1.0.0 🎉

The release is available on npm package (@latest dist-tag)

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants