diff --git a/src/ipfs/basic.ts b/src/ipfs/basic.ts index 37e3eff6a..5c4b443aa 100644 --- a/src/ipfs/basic.ts +++ b/src/ipfs/basic.ts @@ -28,38 +28,48 @@ export const add = async (content: ImportCandidate): Promise => { } export const catRaw = async (cid: CID): Promise => { + 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 => { - 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 => { - const buf = await catBuf(cid) + const newCID = typeCheckCID(cid) + + const buf = await catBuf(newCID) return buf.toString() } export const ls = async (cid: CID): Promise => { + 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 => { + 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 } @@ -99,12 +109,14 @@ export const dagPutLinks = async (links: PBLink[]): Promise => { } export const size = async (cid: CID): Promise => { + 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 => { +const attemptPin = async (cid: CID): Promise => { if (!setup.shouldPin) return const ipfs = await getIpfs() @@ -118,3 +130,9 @@ export const attemptPin = async (cid: CID): Promise => { } } } + +const typeCheckCID = (cid: CID): CID => { + const newCID = CID.asCID(cid) + if (!newCID) throw new Error(`Expected a CID class instance: Found ${cid}`) + return newCID +} diff --git a/tests/fs/share.node.test.ts b/tests/fs/share.node.test.ts index 886f12176..a02e19ced 100644 --- a/tests/fs/share.node.test.ts +++ b/tests/fs/share.node.test.ts @@ -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 )))