Cater for pinata unpin rate limits (#2939)

* Cater for pinata unpin rate limits

* Delay after pin

* Additional delay
This commit is contained in:
Jaco
2023-02-09 19:56:53 +02:00
committed by GitHub
parent 0c614aee05
commit f587f1c273
+39 -16
View File
@@ -11,31 +11,44 @@ const PINMETA = { name: `${SUB_DOMAIN}.${DOMAIN}` };
const pinata = pinataSDK(process.env.PINATA_API_KEY, process.env.PINATA_SECRET_KEY); const pinata = pinataSDK(process.env.PINATA_API_KEY, process.env.PINATA_SECRET_KEY);
async function wait (delay = 2500) {
return new Promise((resolve) => {
setTimeout(() => resolve(), delay);
});
}
async function pin () { async function pin () {
const result = await pinata.pinFromFS(DST, { pinataMetadata: PINMETA }); const result = await pinata.pinFromFS(DST, { pinataMetadata: PINMETA });
console.log(`Pinned ${result.IpfsHash}`); console.log(`Pinned ${result.IpfsHash}`);
await wait();
return result.IpfsHash; return result.IpfsHash;
} }
async function unpin (exclude) { async function unpin (exclude) {
const result = await pinata.pinList({ metadata: PINMETA, status: 'pinned' }); const result = await pinata.pinList({ metadata: PINMETA, status: 'pinned' });
await wait();
if (result.count > 1) { if (result.count > 1) {
const filtered = result.rows const hashes = result.rows
.map((r) => r.ipfs_pin_hash) .map((r) => r.ipfs_pin_hash)
.filter((hash) => hash !== exclude); .filter((hash) => hash !== exclude);
if (filtered.length) { for (let i = 0; i < hashes.length; i++) {
await Promise.all( const hash = hashes[i];
filtered.map((hash) =>
pinata try {
.unpin(hash) await pinata.unpin(hash);
.then(() => console.log(`Unpinned ${hash}`))
.catch(console.error) console.log(`Unpinned ${hash}`);
) } catch (error) {
); console.error(`Failed unpinning ${hash}`, error);
}
await wait();
} }
} }
} }
@@ -43,12 +56,22 @@ async function unpin (exclude) {
async function dnslink (hash) { async function dnslink (hash) {
const records = [`_dnslink.${SUB_DOMAIN}.${DOMAIN}`]; const records = [`_dnslink.${SUB_DOMAIN}.${DOMAIN}`];
await Promise.all(records.map((record) => for (let i = 0; i < records.length; i++) {
cloudflare( const record = records[i];
{ token: process.env.CF_API_TOKEN },
{ link: `/ipfs/${hash}`, record, zone: DOMAIN } try {
) await cloudflare(
)); { token: process.env.CF_API_TOKEN },
{ link: `/ipfs/${hash}`, record, zone: DOMAIN }
);
console.log(`Updated dnslink ${record}`);
await wait();
} catch (error) {
console.error(`Failed updating dnslink ${record}`, error);
}
}
console.log(`Dnslink ${hash} for ${records.join(', ')}`); console.log(`Dnslink ${hash} for ${records.join(', ')}`);
} }