From f24e9a8a0b5f0695c25c53a4205611f074be6d71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Thu, 29 Oct 2026 20:02:40 +0400 Subject: [PATCH] Fix IPFS cat decoding for ipfs-http-client 56 (Uint8Array) ipfs-http-client 56 yields Uint8Array chunks, whose toString() returns comma-separated byte values rather than text. Concatenating those turned every document into an invalid JSON string ("123,10,32,..."), so JSON.parse failed with "Unexpected non-whitespace character after JSON at position 3" and no IPFS-backed record could be read. Decode the chunks explicitly with a streaming TextDecoder, which works in both Node and browsers. Bump version to 7.5.1. --- lib/utils/ipfs.js | 10 +++++++--- package.json | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/utils/ipfs.js b/lib/utils/ipfs.js index 7382bda..a2440a8 100644 --- a/lib/utils/ipfs.js +++ b/lib/utils/ipfs.js @@ -50,11 +50,15 @@ class IPFS { return fetch(`${this._config['gatewayUrl']}/${ipfsHash}`).then(r => r.text()); } else { const res = this._ipfsAPI.cat(ipfsHash); + // ipfs-http-client >= 56 yields Uint8Array chunks. Their toString() + // returns comma-separated byte values instead of text, so decode the + // bytes explicitly (TextDecoder works in both Node and browsers). + const decoder = new TextDecoder(); let str = ''; - for await (const buffer of res) { - str += buffer.toString(); + for await (const chunk of res) { + str += decoder.decode(chunk, { stream: true }); } - return Promise.resolve(str); + return Promise.resolve(str + decoder.decode()); } } diff --git a/package.json b/package.json index 65ecb90..0678443 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kredits/contracts", - "version": "7.5.0", + "version": "7.5.1", "description": "Smart contracts and JavaScript API for Kredits", "main": "./lib/kredits.js", "directories": {