-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: upgrade filecoin api with content store to rely on roundabout (#360
) Today Storefront relies directly on a bucket (in S3) to read bytes to derive PieceCID for validation. With blob protocol, we are moving to write to R2 only 🙌🏼 therefore, we aim to make filecoin Storefront able to read from anywhere, but also not increase the infra costs with this change. This PR changes Storefront to rely on Roundabout to read Blob bytes to derive Piece CID from anywhere instead of `contentStore` being directly hooked to a bucket. See [usage](https://github.com/w3s-project/w3up/blob/main/packages/filecoin-api/src/storefront/events.js#L43) Relying on Roundabout introduces a layer of indirection, but in current implementation where this validation runs in AWS and content will likely be read from CF, makes it cost optimal to read from Roundabout. Closes storacha/w3up#1349
- Loading branch information
1 parent
bb21369
commit 05ce2c0
Showing
12 changed files
with
442 additions
and
496 deletions.
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
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,53 @@ | ||
import { StoreOperationFailed, RecordNotFound } from '@web3-storage/filecoin-api/errors' | ||
import pRetry from 'p-retry' | ||
|
||
/** | ||
* @typedef {import('multiformats').UnknownLink} UnknownLink | ||
* @typedef {import('@web3-storage/filecoin-api/storefront/api').ContentStore<UnknownLink, Uint8Array>} ContentStore | ||
*/ | ||
|
||
/** | ||
* @param {URL} storeHttpEndpoint | ||
* @returns {ContentStore} | ||
*/ | ||
export const useContentStore = (storeHttpEndpoint) => { | ||
return { | ||
/** | ||
* Stream Blob bytes for a given CID. | ||
* | ||
* @param {import('@ucanto/interface').UnknownLink} cid | ||
*/ | ||
stream: async (cid) => { | ||
// create URL for the link to be fetched | ||
const getUrl = new URL(`/${cid.toString()}`, storeHttpEndpoint) | ||
|
||
// Retry a few times as it looks like R2 sometimes takes a bit to make it available | ||
let res | ||
try { | ||
res = await pRetry((async () => { | ||
const fetchRes = await fetch(getUrl, { | ||
// Follow potential redirects | ||
redirect: 'follow', | ||
}) | ||
if (fetchRes.status === 404) { | ||
throw new RecordNotFound(`blob ${cid.toString()} not found in store`) | ||
} else if (!fetchRes.ok || !fetchRes.body) { | ||
throw new StoreOperationFailed(fetchRes.statusText) | ||
} | ||
return fetchRes.body | ||
}), { retries: 5 }) | ||
} catch (err) { | ||
/** @type {RecordNotFound | StoreOperationFailed} */ | ||
// @ts-ignore | ||
const error = err | ||
return { | ||
error | ||
} | ||
} | ||
|
||
return { | ||
ok: res | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.