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

Type-Check CID #365

Merged
merged 4 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions src/ipfs/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,48 @@ export const add = async (content: ImportCandidate): Promise<AddResult> => {
}

export const catRaw = async (cid: CID): Promise<Uint8Array[]> => {
const newCID = typeCheckCID(cid)

const ipfs = await getIpfs()
const chunks = []
await attemptPin(cid)
for await (const chunk of ipfs.cat(cid)) {
await attemptPin(newCID)
for await (const chunk of ipfs.cat(newCID)) {
chunks.push(chunk)
}
return chunks
}

export const catBuf = async (cid: CID): Promise<Uint8Array> => {
const raw = await catRaw(cid)
const newCID = typeCheckCID(cid)

const raw = await catRaw(newCID)
return uint8arrays.concat(raw)
}

export const cat = async (cid: CID): Promise<string> => {
const buf = await catBuf(cid)
const newCID = typeCheckCID(cid)

const buf = await catBuf(newCID)
return buf.toString()
}

export const ls = async (cid: CID): Promise<IPFSEntry[]> => {
const newCID = typeCheckCID(cid)

const ipfs = await getIpfs()
const links = []
for await (const link of ipfs.ls(cid)) {
for await (const link of ipfs.ls(newCID)) {
links.push({ ...link, cid: decodeCID(link.cid) })
}
return links
}

export const dagGet = async (cid: CID): Promise<PBNode> => {
const newCID = typeCheckCID(cid)

const ipfs = await getIpfs()
await attemptPin(cid)
const raw = await ipfs.dag.get(cid)
await attemptPin(newCID)
const raw = await ipfs.dag.get(newCID)
const node = util.rawToDAGNode(raw)
return node
}
Expand Down Expand Up @@ -99,12 +109,14 @@ export const dagPutLinks = async (links: PBLink[]): Promise<AddResult> => {
}

export const size = async (cid: CID): Promise<number> => {
const newCID = typeCheckCID(cid)

const ipfs = await getIpfs()
const stat = await ipfs.files.stat(`/ipfs/${cid}`)
const stat = await ipfs.files.stat(`/ipfs/${newCID}`)
return stat.cumulativeSize
}

export const attemptPin = async (cid: CID): Promise<void> => {
const attemptPin = async (cid: CID): Promise<void> => {
if (!setup.shouldPin) return

const ipfs = await getIpfs()
Expand All @@ -118,3 +130,9 @@ export const attemptPin = async (cid: CID): Promise<void> => {
}
}
}

const typeCheckCID = (cid: CID): CID => {
const newCID = CID.asCID(cid)
if (!newCID) throw new Error(`Expected a CID class instance: Found ${cid}`)
return newCID
}
2 changes: 1 addition & 1 deletion tests/fs/share.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe("the filesystem", () => {
))

const entryIndexInfo = JSON.parse(new TextDecoder().decode(await crypto.aes.decrypt(
await protocol.basic.getFile(decryptedPayload.cid),
await protocol.basic.getFile(decodeCID(decryptedPayload.cid)),
uint8arrays.toString(decryptedPayload.key, "base64pad"),
SymmAlg.AES_GCM
)))
Expand Down