-
Notifications
You must be signed in to change notification settings - Fork 2
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: IPFS retrieval client #243
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
2bce5d3
test: setup env logger in integration tests
bajtos 1a30f47
feat: add Lassie - IPFS retrieval client
bajtos 5be5140
deps: upgrade lassie to v0.2.0
bajtos 884f05a
fixup! remove unused lifetime param
bajtos 93f2c31
feat: fetch('ipfs://bafycid')
bajtos f74672b
REVERT ME: temporarily disable aarch64
bajtos 31a5b8c
Revert "REVERT ME: temporarily disable aarch64"
bajtos 3c5826a
temporarily use Lassie from git main branch
bajtos f810edd
upgrade lassie to 0.3.0
bajtos 7e4d990
fix tests + code cleanup
bajtos d6f4cdf
add support for URL and Request inputs
bajtos 460b94d
fix clippy warning
bajtos 19809a5
Merge remote-tracking branch 'origin/main' into feat-lassie
bajtos 66b5837
add docs for module builders
bajtos ed48be7
Apply suggestions from code review
bajtos 12d2c7f
fixup! prettier --write
bajtos 3078d7f
add Go to build dependencies
bajtos 75f8239
fix a bug in test assertions
bajtos 00e396d
tweak setup-go config
bajtos 78f629a
fixup!: go-version latest -> stable
bajtos 7db4ee8
fixup! improve code comment
bajtos 371b24a
fixup! remove non-ASCII characters from fetch.js
bajtos b2e1cca
fix syntax error to fix a failing test
bajtos 831b954
document temp_dir setting in zinnia CLI
bajtos 6093b21
Merge branch 'main' into feat-lassie
bajtos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { fetch as fetchImpl } from "ext:deno_fetch/26_fetch.js"; | ||
import { fromInnerResponse, toInnerResponse } from "ext:deno_fetch/23_response.js"; | ||
import { toInnerRequest, fromInnerRequest, Request } from "ext:deno_fetch/23_request.js"; | ||
import { guardFromHeaders } from "ext:deno_fetch/20_headers.js"; | ||
|
||
const ipfsScheme = "ipfs://"; | ||
let ipfsBaseUrl = undefined; | ||
|
||
export function setLassieUrl(/** @type {string} */ value) { | ||
ipfsBaseUrl = value + "ipfs/"; | ||
} | ||
|
||
export function fetch(resource, options) { | ||
let request = new Request(resource, options); | ||
// The `resource` arg can be a string or any other object with a stringifier - including a URL | ||
// object - that provides the URL of the resource you want to fetch; or a Request object. | ||
// See https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters | ||
// Fortunately, Request's constructor handles the conversions, and Request#url is always a string. | ||
// See https://developer.mozilla.org/en-US/docs/Web/API/Request/url | ||
if (request.url.startsWith(ipfsScheme)) { | ||
return fetchFromIpfs(request); | ||
} else { | ||
return fetchImpl(request); | ||
} | ||
} | ||
|
||
async function fetchFromIpfs(request) { | ||
// Rewrite request URL to use Lassie | ||
request = buildIpfsRequest(request); | ||
|
||
// Call Deno's `fetch` using the rewritten URL to make the actual HTTP request | ||
const response = await fetchImpl(request); | ||
|
||
// Patch the response object to hide the fact that we are calling Lassie | ||
// We don't want to leak Lassie's URL | ||
return patchIpfsResponse(response); | ||
} | ||
|
||
// Deno's Fetch Request is a thin immutable wrapper around InnerRequest. In order to modify the | ||
// request URL, we must convert Request to InnerRequest first, make changes on the inner object, | ||
// and finally convert the InnerRequest back to a new Request instance. | ||
function buildIpfsRequest(request) { | ||
const inner = toInnerRequest(request); | ||
|
||
inner.urlList = /** @type {(() => string)[]}*/ (inner.urlList).map((urlFn) => { | ||
const url = urlFn(); | ||
if (!url.startsWith(ipfsScheme)) return urlFn; | ||
const newUrl = ipfsBaseUrl + url.slice(ipfsScheme.length); | ||
return () => newUrl; | ||
}); | ||
inner.urlListProcessed = /** @type {string[]} */ (inner.urlListProcessed).map((url) => | ||
url.startsWith(ipfsScheme) ? ipfsBaseUrl + url.slice(ipfsScheme.length) : url, | ||
); | ||
|
||
return fromInnerRequest(inner, request.signal, guardFromHeaders(request.headers)); | ||
} | ||
|
||
// Deno's Fetch Response is a thin immutable wrapper around InnerResponse. In order to modify the | ||
// response URL, we must convert Response to InnerResponse first, make changes on the inner object, | ||
// and finally convert the InnerResponse back to a new Response instance. | ||
function patchIpfsResponse(response) { | ||
const inner = toInnerResponse(response); | ||
|
||
inner.urlList = /** @type {string[])} */ (inner.urlList).map((url) => | ||
url.startsWith(ipfsBaseUrl) ? "ipfs://" + url.slice(ipfsBaseUrl.length) : url, | ||
); | ||
|
||
return fromInnerResponse(inner, guardFromHeaders(response.headers)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,6 @@ mod reporter; | |
pub use console_reporter::*; | ||
pub use reporter::*; | ||
|
||
pub use lassie; | ||
|
||
mod ext; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat. I didn't know there is a Rust client!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a thin Rust wrapper embedding the original Go Lassie, I started the project three weeks ago :)
https://github.com/filecoin-station/rusty-lassie