Skip to content

Commit

Permalink
Fix cbor encode/decode errors on node
Browse files Browse the repository at this point in the history
Switch to using Uint8Arrays instead of node Buffers.
This is something the js-ipfs folks apparently have gone through, too:
ipfs/js-ipfs#3220
  • Loading branch information
matheus23 committed May 18, 2021
1 parent 21e43ce commit dbc3b3d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/ipfs/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const add = async (content: FileContent): Promise<AddResult> => {
}
}

export const catRaw = async (cid: CID): Promise<Buffer[]> => {
export const catRaw = async (cid: CID): Promise<Uint8Array[]> => {
const ipfs = await getIpfs()
const chunks = []
await attemptPin(cid)
Expand All @@ -28,9 +28,9 @@ export const catRaw = async (cid: CID): Promise<Buffer[]> => {
return chunks
}

export const catBuf = async (cid: CID): Promise<Buffer> => {
export const catBuf = async (cid: CID): Promise<Uint8Array> => {
const raw = await catRaw(cid)
return Buffer.concat(raw)
return Uint8Array.from(Buffer.concat(raw))
}

export const cat = async (cid: CID): Promise<string> => {
Expand Down
10 changes: 5 additions & 5 deletions src/ipfs/encoded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import * as basic from './basic'
export const add = async (content: FileContent, key: Maybe<string>): Promise<AddResult> => {
// can't cbor encode blobs ie file streams
const normalized = isBlob(content) ? await blob.toBuffer(content) : content
const encoded = cbor.encode(normalized)
const toAdd = isJust(key) ? await crypto.aes.encrypt(encoded, key) : encoded
return basic.add(toAdd)
const encoded: Uint8Array = Uint8Array.from(cbor.encode(normalized))
const toAdd: Uint8Array = isJust(key) ? await crypto.aes.encrypt(encoded, key) : encoded
return await basic.add(toAdd)
}

export const catAndDecode = async (cid: CID, key: Maybe<string>): Promise<unknown> => {
const buf = await basic.catBuf(cid)
const toDecode = isJust(key) ? await crypto.aes.decrypt(buf, key) : buf
return cbor.decode(toDecode)
const toDecode: Uint8Array = isJust(key) ? await crypto.aes.decrypt(buf, key) : buf
return cbor.decode(Buffer.from(toDecode))
}
6 changes: 3 additions & 3 deletions src/ipfs/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type IPFS = {
add(data: FileContent, options?: unknown): UnixFSFile
add(data: FileContent, options?: unknown): Promise<UnixFSFile>
cat(cid: CID): AsyncIterable<FileContentRaw>
ls(cid: CID): AsyncIterable<UnixFSFile>
dns(domain: string): Promise<CID>
Expand Down Expand Up @@ -75,8 +75,8 @@ export type CIDObj = {
toString(): string
}

export type FileContent = Record<string, unknown> | Buffer | Blob | string | number | boolean
export type FileContentRaw = Buffer
export type FileContent = Record<string, unknown> | Uint8Array | Blob | string | number | boolean
export type FileContentRaw = Uint8Array

export type FileMode = number

Expand Down

0 comments on commit dbc3b3d

Please sign in to comment.