-
Notifications
You must be signed in to change notification settings - Fork 167
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
fix: add backup url parts to list #2675
Changes from all commits
1bb699e
62c5328
9d2db55
cfafe3d
2818c54
579a7f4
637bf17
d90eccb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,6 +111,7 @@ async function cborEncode(value, bs) { | |
const bytes = CBOR.encode(value) | ||
const digest = await sha256.digest(bytes) | ||
const cid = CID.createV1(CBOR.code, digest) | ||
// @ts-expect-error different CID versions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all the mess of versioning of CID in a project stalled 😭 |
||
await bs.put(cid, bytes) | ||
return cid | ||
} | ||
|
@@ -160,6 +161,7 @@ async function unixFsEncodeDir(files, bs) { | |
const content = new Uint8Array(await f.arrayBuffer()) | ||
input.push({ path: f.name, content }) | ||
} | ||
// @ts-expect-error different CID versions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all the mess of versioning of CID in a project stalled 😭 |
||
return unixFsEncode(input, bs, { | ||
wrapWithDirectory: true, | ||
}) | ||
|
@@ -174,6 +176,7 @@ async function unixFsEncodeDir(files, bs) { | |
async function unixFsEncodeString(str, bs) { | ||
const content = new TextEncoder().encode(str) | ||
const ic = { path: '', content } | ||
// @ts-expect-error different CID versions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all the mess of versioning of CID in a project stalled 😭 |
||
return unixFsEncode(ic, bs, { | ||
wrapWithDirectory: false, | ||
}) | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,4 +1,6 @@ | ||||
import * as cluster from '../cluster.js' | ||||
import * as Link from 'multiformats/link' | ||||
import * as Digest from 'multiformats/hashes/digest' | ||||
import { fromString } from 'uint8arrays' | ||||
|
||||
/** | ||||
* We mixed upload type and content type. This was split into `type` and | ||||
|
@@ -20,6 +22,13 @@ const typeMap = { | |||
* @param {string} [sourceCid] - User input CID so we can return the same cid version back | ||||
*/ | ||||
export function toNFTResponse(upload, sourceCid) { | ||||
// get hash links to CARs that contain parts of this upload | ||||
/** @type {string[]} */ | ||||
const parts = [ | ||||
// from upload table 'backup_urls' column | ||||
...carCidV1Base32sFromBackupUrls(upload.backup_urls ?? []), | ||||
] | ||||
|
||||
/** @type {import('../bindings').NFTResponse} */ | ||||
Comment on lines
+25
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adapted from https://github.com/web3-storage/web3.storage/blob/main/packages/db/utils.js#L19 in nft.storage we ran the migration to get backUrls from Migration: https://gist.github.com/yocontra/4ae719b42a54c4582ca28fcac74199d0 I double checked a few rows of |
||||
const nft = { | ||||
cid: sourceCid || upload.source_cid, | ||||
|
@@ -29,6 +38,7 @@ export function toNFTResponse(upload, sourceCid) { | |||
files: upload.files, | ||||
size: upload.content.dag_size || 0, | ||||
name: upload.name, | ||||
parts, | ||||
pin: { | ||||
cid: sourceCid || upload.source_cid, | ||||
created: upload.content.pin[0].inserted_at, | ||||
|
@@ -101,3 +111,53 @@ function transformPinStatus(status) { | |||
return 'failed' | ||||
} | ||||
} | ||||
|
||||
/** | ||||
* given array of backup_urls from uploads table, return a corresponding set of CAR CIDv1 using base32 multihash | ||||
* for any CAR files in the backup_urls. | ||||
* | ||||
* @param {unknown[]} backupUrls | ||||
* @returns {Iterable<string>} | ||||
*/ | ||||
function carCidV1Base32sFromBackupUrls(backupUrls) { | ||||
const carCidStrings = new Set() | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||
for (const backupUrl of backupUrls) { | ||||
let carCid | ||||
try { | ||||
// @ts-expect-error database exported types assumes unknown | ||||
carCid = bucketKeyToPartCID(backupUrl) | ||||
} catch (error) { | ||||
console.warn('error extracting car CID from bucket URL', error) | ||||
} | ||||
if (!carCid) continue | ||||
carCidStrings.add(carCid.toString()) | ||||
} | ||||
return carCidStrings | ||||
} | ||||
|
||||
const CAR_CODE = 0x0202 | ||||
|
||||
/** | ||||
* Attempts to extract a CAR CID from a bucket key. | ||||
* | ||||
* @param {string} key | ||||
*/ | ||||
const bucketKeyToPartCID = (key) => { | ||||
const filename = String(key.split('/').at(-1)) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think should be fine looking at the code but worth a double check - it needs to now also account for backup URL in this format:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am going to add a test |
||||
const [hash] = filename.split('.') | ||||
try { | ||||
// recent buckets encode CAR CID in filename | ||||
const cid = Link.parse(hash).toV1() | ||||
if (cid.code === CAR_CODE) return cid | ||||
throw new Error('not a CAR CID') | ||||
} catch (err) { | ||||
// older buckets base32 encode a CAR multihash <base32(car-multihash)>.car | ||||
try { | ||||
const digestBytes = fromString(hash, 'base32') | ||||
const digest = Digest.decode(digestBytes) | ||||
return Link.create(CAR_CODE, digest) | ||||
} catch (error) { | ||||
// console.warn('error trying to create CID from s3 key', error) | ||||
} | ||||
} | ||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,7 +58,6 @@ export class LinkdexApi { | |
if (!res || !res.body) throw new Error(`failed to get CAR: ${cid}`) | ||
const carBlocks = await CarBlockIterator.fromIterable(res.body) | ||
for await (const block of carBlocks) { | ||
// @ts-expect-error block types not match up | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all the mess of versioning of CID in a project stalled 😭 |
||
index.decodeAndIndex(block) | ||
} | ||
}) | ||
|
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.
needed to upgrade to use Link, which made needed to upgrade IPLD deps to decrease TS issues