-
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
- Loading branch information
1 parent
bb21369
commit 16c788c
Showing
13 changed files
with
450 additions
and
497 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,60 @@ | ||
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.status > 299 || !fetchRes.body) { | ||
throw new StoreOperationFailed(fetchRes.statusText) | ||
} | ||
return fetchRes | ||
}), { retries: 5 }) | ||
} catch (err) { | ||
/** @type {RecordNotFound | StoreOperationFailed} */ | ||
// @ts-ignore | ||
const error = err | ||
return { | ||
error | ||
} | ||
} | ||
|
||
// To satisfy typescript | ||
if (!res.body) { | ||
return { | ||
error: new StoreOperationFailed(res.statusText) | ||
} | ||
} | ||
|
||
return { | ||
ok: res.body | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.