From dbc3b3d008eae12ee0e1e755eb40ffb00e3ba58a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Kr=C3=BCger?= Date: Mon, 10 May 2021 21:02:16 +0200 Subject: [PATCH] Fix cbor encode/decode errors on node Switch to using Uint8Arrays instead of node Buffers. This is something the js-ipfs folks apparently have gone through, too: https://github.com/ipfs/js-ipfs/issues/3220 --- src/ipfs/basic.ts | 6 +++--- src/ipfs/encoded.ts | 10 +++++----- src/ipfs/types.ts | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/ipfs/basic.ts b/src/ipfs/basic.ts index 3fb4a5bbc..bc20c5626 100644 --- a/src/ipfs/basic.ts +++ b/src/ipfs/basic.ts @@ -18,7 +18,7 @@ export const add = async (content: FileContent): Promise => { } } -export const catRaw = async (cid: CID): Promise => { +export const catRaw = async (cid: CID): Promise => { const ipfs = await getIpfs() const chunks = [] await attemptPin(cid) @@ -28,9 +28,9 @@ export const catRaw = async (cid: CID): Promise => { return chunks } -export const catBuf = async (cid: CID): Promise => { +export const catBuf = async (cid: CID): Promise => { const raw = await catRaw(cid) - return Buffer.concat(raw) + return Uint8Array.from(Buffer.concat(raw)) } export const cat = async (cid: CID): Promise => { diff --git a/src/ipfs/encoded.ts b/src/ipfs/encoded.ts index b41aa07fb..ee972bed0 100644 --- a/src/ipfs/encoded.ts +++ b/src/ipfs/encoded.ts @@ -14,13 +14,13 @@ import * as basic from './basic' export const add = async (content: FileContent, key: Maybe): Promise => { // 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): Promise => { 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)) } diff --git a/src/ipfs/types.ts b/src/ipfs/types.ts index d086abae3..ce94f5db9 100644 --- a/src/ipfs/types.ts +++ b/src/ipfs/types.ts @@ -1,5 +1,5 @@ export type IPFS = { - add(data: FileContent, options?: unknown): UnixFSFile + add(data: FileContent, options?: unknown): Promise cat(cid: CID): AsyncIterable ls(cid: CID): AsyncIterable dns(domain: string): Promise @@ -75,8 +75,8 @@ export type CIDObj = { toString(): string } -export type FileContent = Record | Buffer | Blob | string | number | boolean -export type FileContentRaw = Buffer +export type FileContent = Record | Uint8Array | Blob | string | number | boolean +export type FileContentRaw = Uint8Array export type FileMode = number