Fix IPFS cat decoding for ipfs-http-client 56 (Uint8Array)
Release Drafter / Update release notes draft (pull_request) Failing after 3s
Release Drafter / Update release notes draft (pull_request) Failing after 3s
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.
This commit is contained in:
2 files changed
+8
-4
No files matched your search
+7
-3
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -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": {
|
||||
|
||||
Reference in new issue
Block a user