Update ipfs-http-client (for node >= 14)

Updates the client library and API usage to work with the latest
version, and more importantly, with current node.js LTS.
This commit is contained in:
2022-04-28 19:19:58 +02:00
parent 16d5704173
commit 53fafc1c78
5 changed files with 19954 additions and 5600 deletions

View File

@@ -8,10 +8,10 @@ class IPFS {
config = { host: 'localhost', port: '5001', protocol: 'http' };
}
this._config = config;
this._ipfsAPI = ipfsClient(config);
this._ipfsAPI = ipfsClient.create(config);
}
catAndMerge (contractData, deserialize) {
async catAndMerge (contractData, deserialize) {
let data = {...contractData}; // data from ethers.js is not extensible. this copy the attributes in a new object
// if no hash details are found simply return the data; nothing to merge
if (!data.hashSize || data.hashSize === 0) {
@@ -22,20 +22,19 @@ class IPFS {
return this.cat(data.ipfsHash)
.then(deserialize)
.then((attributes) => {
.then(attributes => {
return Object.assign({}, data, attributes);
});
}
add (data) {
return this._ipfsAPI
.add(ipfsClient.Buffer.from(data))
.then((res) => {
return this.decodeHash(res[0].hash);
async add (data) {
return this._ipfsAPI.add(data)
.then(res => {
return this.decodeHash(res.path);
});
}
cat (hashData) {
async cat (hashData) {
let ipfsHash = hashData; // default - if it is a string
if (Object.prototype.hasOwnProperty.call(hashData, 'hashSize')) {
ipfsHash = this.encodeHash(hashData);
@@ -43,7 +42,12 @@ class IPFS {
if (this._config['gatewayUrl']) {
return fetch(`${this._config['gatewayUrl']}/${ipfsHash}`).then(r => r.text());
} else {
return this._ipfsAPI.cat(ipfsHash);
const res = this._ipfsAPI.cat(ipfsHash);
let str = '';
for await (const buffer of res) {
str += buffer.toString();
}
return Promise.resolve(str);
}
}
@@ -66,7 +70,7 @@ class IPFS {
}
encodeHash (hashData) {
let digest = ipfsClient.Buffer.from(hashData.hashDigest.slice(2), 'hex');
const digest = Buffer.from(hashData.hashDigest.slice(2), 'hex');
return multihashes.encode(digest, hashData.hashFunction, hashData.hashSize);
}
}