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 http range header #10

Merged
merged 42 commits into from
Mar 15, 2024

Conversation

SgtPooki
Copy link
Member

@SgtPooki SgtPooki commented Mar 4, 2024

Title

Add Range header support.

Description

Fixes #9

Notes & open questions

  • Still need to add some additional tests, but it's ready for code review.
  • byte-range-context.spec.ts is currently skipping handling ReadableStreams tests, but these are covered by range requests tests.. going to try to fix quickly and if I can't i'll remove them.

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

@SgtPooki SgtPooki requested a review from a team as a code owner March 4, 2024 21:53
@SgtPooki SgtPooki linked an issue Mar 4, 2024 that may be closed by this pull request
@SgtPooki SgtPooki marked this pull request as draft March 4, 2024 22:54
@SgtPooki SgtPooki self-assigned this Mar 4, 2024
Copy link
Member Author

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

self review, some comments

packages/verified-fetch/src/utils/responses.ts Outdated Show resolved Hide resolved
packages/verified-fetch/src/verified-fetch.ts Outdated Show resolved Hide resolved
packages/verified-fetch/src/verified-fetch.ts Outdated Show resolved Hide resolved
packages/verified-fetch/test/range-requests.spec.ts Outdated Show resolved Hide resolved
packages/verified-fetch/src/verified-fetch.ts Outdated Show resolved Hide resolved
@SgtPooki SgtPooki marked this pull request as ready for review March 5, 2024 01:52
@SgtPooki
Copy link
Member Author

SgtPooki commented Mar 5, 2024

On mobile but wanted to give a shout that ive got a refactor going that cleans this up a bunch

@achingbrain
Copy link
Member

ive got a refactor going that cleans this up a bunch

Done a first pass, will look again after the refactor lands.

@SgtPooki SgtPooki marked this pull request as draft March 5, 2024 20:40
Copy link
Member Author

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

self review and response to comments

Comment on lines 148 to 150
public get fileSize (): number | null | undefined {
return this._fileSize
}
Copy link
Member Author

@SgtPooki SgtPooki Mar 15, 2024

Choose a reason for hiding this comment

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

@achingbrain

We don't have to, but it will help prevent future developers from mistakenly trying to set things they shouldn't without understanding the implications. Setters and getters help explicitly state, in the way the code was implemented, that specific things have to be done when a value is applied, whereas a public non-readonly fileSize implies that it can be called at any time.

e.g. we call the setOffsets when setting fileSize. Without the property being protected, it can be called by anyone in the codebase. When this property isn't wrapped with a getter, and is public, it becomes available as a setter externally. When in reality, the fileSize property should not be publicly writeable, but to make it public and remove setter/getter, we have to make it so. We could use readonly and then override protections in the class.. but that feels like a lot of work for encapsulating that protection that we get by doing the conventional thing.

aside: IMHO setting and getting are cleaner than "setFileSize" and ".filesize" or "getFilesize"

Also... microsoft/TypeScript#37487

I addressed this with 46dc133 (#10), but we should think about reverting it.

packages/verified-fetch/src/utils/byte-range-context.ts Outdated Show resolved Hide resolved
packages/verified-fetch/src/utils/byte-range-context.ts Outdated Show resolved Hide resolved
packages/verified-fetch/src/verified-fetch.ts Outdated Show resolved Hide resolved
…port

FYI: some tests were failing after `npm run reset` and `npm run test` was run. I had to fix them manually.
@SgtPooki
Copy link
Member Author

FYI: some tests were failing when merging main into byte-range PR due to new ipns things. changes at acdd632 (#10)

Copy link
Member

@achingbrain achingbrain left a comment

Choose a reason for hiding this comment

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

LGTM, pending remaining discussion points

Co-authored-by: Alex Potsides <alex@achingbrain.net>
Copy link
Member Author

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

Going over code one last time

packages/verified-fetch/src/utils/byte-range-context.ts Outdated Show resolved Hide resolved
* - Content-Range: <unit> <byteStart>-<byteEnd>/<byteSize>
* - Content-Range: <unit> <byteStart>-<byteEnd>/&#8205;*
*/
// - Content-Range: <unit> */<byteSize> // this is purposefully not in jsdoc block
Copy link
Member Author

Choose a reason for hiding this comment

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

@achingbrain is this better than all as single line comments?

packages/verified-fetch/src/verified-fetch.ts Outdated Show resolved Hide resolved
packages/verified-fetch/src/verified-fetch.ts Outdated Show resolved Hide resolved
} catch (err) {
this.log.error('error parsing resource %s', resource, err)

return badRequestResponse('Invalid resource')
Copy link
Member Author

Choose a reason for hiding this comment

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

Less throwing, more bad fetch responses

packages/verified-fetch/src/utils/byte-range-context.ts Outdated Show resolved Hide resolved
packages/verified-fetch/src/utils/byte-range-context.ts Outdated Show resolved Hide resolved
@SgtPooki
Copy link
Member Author

SgtPooki commented Mar 15, 2024

commenting elsewhere since the thread (#10 (comment)) was collapsed:

we could solve both concerns by setting internalFileSize so we know which value we set vs consumers.. but that could get confusing. i'll leave as is for now.

i've got _isRangeRequest updated locally, and have isValidRangeRequest updated as well.

So now we have:

inaccessable properties outside the ByteRangeContext class:

  1. _fileSize // setter and getter set/get methods. see 8837738 (#10)
  2. _contentRangeHeaderValue // getter (depends on other operations) see 314adca (#10)
  3. _body // set/get methods
  4. _isValidRangeRequest // getter (depends on other operations) see d633456 (#10)

the only outlier here seems to be _body having setBody and getBody methods

@SgtPooki SgtPooki merged commit 9f5078a into main Mar 15, 2024
19 checks passed
@SgtPooki SgtPooki deleted the 9-heliaverified-fetch-http-range-request-support branch March 15, 2024 22:48
github-actions bot pushed a commit that referenced this pull request Mar 15, 2024
## @helia/verified-fetch [1.2.0](https://github.com/ipfs/helia-verified-fetch/compare/@helia/verified-fetch-1.1.3...@helia/verified-fetch-1.2.0) (2024-03-15)

### Features

* support http range header ([#10](#10)) ([9f5078a](9f5078a))

### Trivial Changes

* fix build ([#22](#22)) ([01261fe](01261fe))
Copy link

🎉 This PR is included in version 1.2.0 🎉

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

Your semantic-release bot 📦🚀

Copy link

🎉 This PR is included in version 1.7.0 🎉

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

Your semantic-release bot 📦🚀

SgtPooki added a commit that referenced this pull request Mar 18, 2024
* feat: implement new ipns record&answer properties

* fix: parseUrlString response includes defined ttl, set ttl if ipnsCached

* test: fix firefox failure

* feat: support http range header (#10)

* chore: limit body parameters to the types used

* chore: add response-header helper and tests

* feat: add range header parsing support

* feat: verified-fetch supports range-requests

* test: fix dns test asserting test failure since we are catching it now

* fix: return 500 error when streaming unixfs content throws

* fix: cleanup code and unexecuting tests hiding errors

* chore: some cleanup and code coverage

* tmp: most things working

* fix: stream slicing and test correctness

* chore: fixed some ByteRangeContext tests

* test: add back header helpers

* fix: unixfs tests are passing

* fix: range-requests on raw content

* feat: tests are passing

moved transform stream over to https://github.com/SgtPooki/streams

* chore: log string casing

* chore: use 502 response instead of 500

* chore: use libp2p/interface for types in src

* chore: failing to create range resp logs error

* chore: Apply suggestions from code review

* chore: fix broken tests from github PR patches (my own)

* chore: re-enable stream tests for ByteRangeContext

* chore: clean up getBody a bit

* chore: ByteRangeContext getBody cleanup

* chore: apply suggestions from code review

Co-authored-by: Alex Potsides <alex@achingbrain.net>

* fix: getSlicedBody uses correct types

* chore: remove extra stat call

* chore: fix jsdoc with '*/'

* chore: fileSize is public property, but should not be used

* test: fix blob comparisons that broke or were never worjing properly

* chore: Update byte-range-context.ts

Co-authored-by: Alex Potsides <alex@achingbrain.net>

* chore: jsdoc cleanup

* Revert "chore: fileSize is public property, but should not be used"

This reverts commit 46dc133.

* chore: jsdoc comments explaining .fileSize use

* chore: isRangeRequest is public

* chore: getters/setters update

* chore: remove unnecessary _contentRangeHeaderValue

* chore: ByteRangeContext uses setFileSize and getFileSize

* chore: remove .stat changes that are no longer needed

---------

Co-authored-by: Alex Potsides <alex@achingbrain.net>

* chore(release): 1.2.0 [skip ci]

## @helia/verified-fetch [1.2.0](https://github.com/ipfs/helia-verified-fetch/compare/@helia/verified-fetch-1.1.3...@helia/verified-fetch-1.2.0) (2024-03-15)

### Features

* support http range header ([#10](#10)) ([9f5078a](9f5078a))

### Trivial Changes

* fix build ([#22](#22)) ([01261fe](01261fe))

* chore(release): 1.7.0 [skip ci]

## @helia/verified-fetch-interop [1.7.0](https://github.com/ipfs/helia-verified-fetch/compare/@helia/verified-fetch-interop-1.6.0...@helia/verified-fetch-interop-1.7.0) (2024-03-15)

### Dependencies

* **@helia/verified-fetch:** upgraded to 1.2.0

* chore: apply pr comments

* fix: some ipns ttl precision cleanup

---------

Co-authored-by: Alex Potsides <alex@achingbrain.net>
Co-authored-by: semantic-release-bot <semantic-release-bot@martynus.net>
SgtPooki added a commit that referenced this pull request Mar 21, 2024
* fix: set cache-control header conditionally

Fixes #17

* chore: bump deps

* feat: implement new ipns record&answer properties (#23)

* feat: implement new ipns record&answer properties

* fix: parseUrlString response includes defined ttl, set ttl if ipnsCached

* test: fix firefox failure

* feat: support http range header (#10)

* chore: limit body parameters to the types used

* chore: add response-header helper and tests

* feat: add range header parsing support

* feat: verified-fetch supports range-requests

* test: fix dns test asserting test failure since we are catching it now

* fix: return 500 error when streaming unixfs content throws

* fix: cleanup code and unexecuting tests hiding errors

* chore: some cleanup and code coverage

* tmp: most things working

* fix: stream slicing and test correctness

* chore: fixed some ByteRangeContext tests

* test: add back header helpers

* fix: unixfs tests are passing

* fix: range-requests on raw content

* feat: tests are passing

moved transform stream over to https://github.com/SgtPooki/streams

* chore: log string casing

* chore: use 502 response instead of 500

* chore: use libp2p/interface for types in src

* chore: failing to create range resp logs error

* chore: Apply suggestions from code review

* chore: fix broken tests from github PR patches (my own)

* chore: re-enable stream tests for ByteRangeContext

* chore: clean up getBody a bit

* chore: ByteRangeContext getBody cleanup

* chore: apply suggestions from code review

Co-authored-by: Alex Potsides <alex@achingbrain.net>

* fix: getSlicedBody uses correct types

* chore: remove extra stat call

* chore: fix jsdoc with '*/'

* chore: fileSize is public property, but should not be used

* test: fix blob comparisons that broke or were never worjing properly

* chore: Update byte-range-context.ts

Co-authored-by: Alex Potsides <alex@achingbrain.net>

* chore: jsdoc cleanup

* Revert "chore: fileSize is public property, but should not be used"

This reverts commit 46dc133.

* chore: jsdoc comments explaining .fileSize use

* chore: isRangeRequest is public

* chore: getters/setters update

* chore: remove unnecessary _contentRangeHeaderValue

* chore: ByteRangeContext uses setFileSize and getFileSize

* chore: remove .stat changes that are no longer needed

---------

Co-authored-by: Alex Potsides <alex@achingbrain.net>

* chore(release): 1.2.0 [skip ci]

## @helia/verified-fetch [1.2.0](https://github.com/ipfs/helia-verified-fetch/compare/@helia/verified-fetch-1.1.3...@helia/verified-fetch-1.2.0) (2024-03-15)

### Features

* support http range header ([#10](#10)) ([9f5078a](9f5078a))

### Trivial Changes

* fix build ([#22](#22)) ([01261fe](01261fe))

* chore(release): 1.7.0 [skip ci]

## @helia/verified-fetch-interop [1.7.0](https://github.com/ipfs/helia-verified-fetch/compare/@helia/verified-fetch-interop-1.6.0...@helia/verified-fetch-interop-1.7.0) (2024-03-15)

### Dependencies

* **@helia/verified-fetch:** upgraded to 1.2.0

* chore: apply pr comments

* fix: some ipns ttl precision cleanup

---------

Co-authored-by: Alex Potsides <alex@achingbrain.net>
Co-authored-by: semantic-release-bot <semantic-release-bot@martynus.net>

* chore: add matchUrlGroups typeguard

* chore: remove unnecessary headerValue != null check

* test: remove unnecessary redefinition of verifiedFetch

---------

Co-authored-by: Daniel N <2color@users.noreply.github.com>
Co-authored-by: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com>
Co-authored-by: Alex Potsides <alex@achingbrain.net>
Co-authored-by: semantic-release-bot <semantic-release-bot@martynus.net>
github-actions bot pushed a commit that referenced this pull request Mar 21, 2024
## @helia/verified-fetch [1.3.0](https://github.com/ipfs/helia-verified-fetch/compare/@helia/verified-fetch-1.2.1...@helia/verified-fetch-1.3.0) (2024-03-21)

### Features

* abort signals are respected ([#26](#26)) ([30148fe](30148fe))

### Bug Fixes

* set cache-control header correctly ([#19](#19)) ([114f3a4](114f3a4)), closes [#17](#17) [#23](#23) [#10](#10)
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.

@helia/verified-fetch: HTTP Range request support
2 participants