From a813586b39dd8a35cd99c80c14840e50c9d69063 Mon Sep 17 00:00:00 2001 From: Banciur Date: Sun, 10 Apr 2022 17:49:47 +0200 Subject: [PATCH 1/6] prepare structure --- src/cli.js | 6 ++++++ src/deploy.js | 36 ++++++++++++++++++++++++++++++++++++ src/dnslinkers/cloudflare.js | 7 +++++++ src/dnslinkers/dnsimple.js | 7 +++++++ src/dnslinkers/dreamhost.js | 4 ++++ src/dnslinkers/route53.js | 4 ++++ src/dnslinkers/types.ts | 1 + src/pinners/ipfs-cluster.js | 7 +++++++ src/pinners/ipfs-node.js | 7 +++++++ src/pinners/pinata.js | 7 +++++++ src/pinners/types.ts | 1 + src/types.ts | 1 + 12 files changed, 88 insertions(+) diff --git a/src/cli.js b/src/cli.js index 75c0c4c..6cdb25b 100755 --- a/src/cli.js +++ b/src/cli.js @@ -78,6 +78,11 @@ const argv = yargs describe: 'Add hidden (dot) files to IPFS', type: 'boolean', default: false + }, + 'unpin-old': { + describe: 'Unpin old requires a DNS linker', + type: 'boolean', + default: false } }) .example( @@ -116,6 +121,7 @@ const options = { copyUrl: !argv.C, openUrls: argv.open, hiddenFiles: argv.hidden, + unpinOld: argv.unpinOld, uploadServices: arrayFromString(argv.upload), pinningServices: arrayFromString(argv.pinner), diff --git a/src/deploy.js b/src/deploy.js index fead1f4..a65dc00 100644 --- a/src/deploy.js +++ b/src/deploy.js @@ -185,6 +185,34 @@ async function checkDirAndCid (dir, cid, logger) { return { cid, dir } } +/** + * @param {DNSLinker[]} dnsServices + * @param {PinningService[]} pinServices + * @param {Logger} logger + */ +async function unpin (dnsServices, pinServices, logger) { + /** @type {string[]} */ + const linkedCids = [] + + for (const dnsProvider of dnsServices) { + logger.info(`Getting linked cid from ${dnsProvider.displayName}`) + const cid = await dnsProvider.getLinkedCid() + logger.info(`Got cid: ${cid}`) + linkedCids.push(cid) + } + + if (linkedCids.some(v => v !== linkedCids[0])) { + throw new Error(`Found inconsistency in linked CIDs: ${linkedCids}`) + } + + const cidToUnpin = linkedCids[0] + + for (const pinProvider of pinServices) { + logger.info(`Unpinning ${cidToUnpin} from ${pinProvider.displayName}`) + pinProvider.unpinCid(cidToUnpin) + } +} + /** * @param {DeployOptions} options * @returns {Promise} @@ -197,6 +225,7 @@ async function deploy ({ copyUrl = false, openUrls = false, hiddenFiles = false, + unpinOld = false, uploadServices: uploadServicesIds = [], pinningServices: pinningServicesIds = [], @@ -245,6 +274,13 @@ async function deploy ({ return new DNSLinker(dnsProvidersCredentials[name]) }) + if (unpinOld) { + if (dnsProviders.length === 0) { + throw new Error('If you want to unpin you must provide dns provider') + } + await unpin(dnsProviders, pinningServices, logger) + } + const pinnedCids = /** @type {string[]} */([]) const gatewayUrls = /** @type {string[]} */([]) diff --git a/src/dnslinkers/cloudflare.js b/src/dnslinkers/cloudflare.js index bc9700e..a3dbfb8 100644 --- a/src/dnslinkers/cloudflare.js +++ b/src/dnslinkers/cloudflare.js @@ -52,6 +52,13 @@ class Cloudflare { } } + /** + * @returns {Promise} + */ + async getLinkedCid () { + throw new Error('getLinkedCid not implemented in Cloudflare') + } + static get displayName () { return 'Cloudflare' } diff --git a/src/dnslinkers/dnsimple.js b/src/dnslinkers/dnsimple.js index 1d8282f..3bdbcbe 100644 --- a/src/dnslinkers/dnsimple.js +++ b/src/dnslinkers/dnsimple.js @@ -51,6 +51,13 @@ class DNSimple { } } + /** + * @returns {Promise} + */ + async getLinkedCid () { + throw new Error('getLinkedCid not implemented in DNSimple') + } + static get displayName () { return 'DNSimple' } diff --git a/src/dnslinkers/dreamhost.js b/src/dnslinkers/dreamhost.js index 4dfeba3..79a9581 100644 --- a/src/dnslinkers/dreamhost.js +++ b/src/dnslinkers/dreamhost.js @@ -68,6 +68,10 @@ class DreamHost { }) } + async getLinkedCid () { + throw new Error('getLinkedCid not implemented in DreamHost') + } + static get displayName () { return 'DreamHost' } diff --git a/src/dnslinkers/route53.js b/src/dnslinkers/route53.js index fac7abe..1b2a9f7 100644 --- a/src/dnslinkers/route53.js +++ b/src/dnslinkers/route53.js @@ -68,6 +68,10 @@ class Route53 { } } + async getLinkedCid () { + throw new Error('getLinkedCid not implemented in Route53') + } + static get displayName () { return 'Route53' } diff --git a/src/dnslinkers/types.ts b/src/dnslinkers/types.ts index 98ad301..60f833a 100644 --- a/src/dnslinkers/types.ts +++ b/src/dnslinkers/types.ts @@ -5,6 +5,7 @@ export interface DNSRecord { export interface DNSLinker { link: (cid: string) => Promise + getLinkedCid: () => Promise displayName: string } diff --git a/src/pinners/ipfs-cluster.js b/src/pinners/ipfs-cluster.js index 55682c0..6b3c19d 100644 --- a/src/pinners/ipfs-cluster.js +++ b/src/pinners/ipfs-cluster.js @@ -73,6 +73,13 @@ class IpfsCluster { }) } + /** + * @param {string} cid + */ + unpinCid (cid) { + throw new Error('unpinCid not implemented in IpfsCluster') + } + /** * @param {string} cid * @returns string diff --git a/src/pinners/ipfs-node.js b/src/pinners/ipfs-node.js index 364ce96..18a33e3 100644 --- a/src/pinners/ipfs-node.js +++ b/src/pinners/ipfs-node.js @@ -43,6 +43,13 @@ class IpfsNode { await this.ipfs.pin.add(cid) } + /** + * @param {string} cid + */ + unpinCid (cid) { + throw new Error('unpinCid not implemented in IpfsNode') + } + /** * @param {string} cid * @returns string diff --git a/src/pinners/pinata.js b/src/pinners/pinata.js index 66ab698..10d09e0 100644 --- a/src/pinners/pinata.js +++ b/src/pinners/pinata.js @@ -91,6 +91,13 @@ class Pinata { await axios.post(PIN_HASH_URL, body, config) } + /** + * @param {string} cid + */ + unpinCid (cid) { + throw new Error('unpinCid not implemented in Pinata') + } + /** * @param {string} cid * @returns string diff --git a/src/pinners/types.ts b/src/pinners/types.ts index 4ee8e76..f18ed02 100644 --- a/src/pinners/types.ts +++ b/src/pinners/types.ts @@ -6,6 +6,7 @@ export interface PinDirOptions { export interface PinningService { pinDir: (dir: string, options: PinDirOptions|undefined) => Promise pinCid: (cid: string, tag: string|undefined) => void + unpinCid: (cid: string) => void gatewayUrl: (cid: string) => string displayName: string } diff --git a/src/types.ts b/src/types.ts index 7065bf0..6d007a6 100644 --- a/src/types.ts +++ b/src/types.ts @@ -15,6 +15,7 @@ export interface DeployOptions { hiddenFiles?: boolean copyUrl?: boolean openUrls?: boolean + unpinOld?: boolean uploadServices: string[] pinningServices: string[] From 8737bc8449539df742310c90a6f6a3c31aacfaa3 Mon Sep 17 00:00:00 2001 From: Banciur Date: Sun, 10 Apr 2022 22:45:47 +0200 Subject: [PATCH 2/6] implement geting CID from Cloudflare --- md/usage.md | 5 +++ package.json | 1 + src/dnslinkers/cloudflare.js | 5 +-- src/lib/cloudflareCid.js | 66 ++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/lib/cloudflareCid.js diff --git a/md/usage.md b/md/usage.md index 8bfd0a6..4ce0273 100644 --- a/md/usage.md +++ b/md/usage.md @@ -75,6 +75,7 @@ Options: -c, --cid Pin this CID instead of uploading -q, --quiet Only print the CID in the end [boolean] [default: false] -H, --hidden Add hidden (dot) files to IPFS [boolean] [default: false] + --unpin-old Unpin old requires a DNS linker [boolean] [default: false] -h, --help Show help [boolean] Examples: @@ -104,6 +105,10 @@ Please keep in mind: sure you want them to be added, use the flag `-H, --hidden`. - All of the services are subject to their terms. +## Unpinning +It works by getting CID to unpin from dnslinker and then tries to unpin it from every +upload and pin service. Currently, implemented only for CloudFlare and pinners that +derive from IpfsNode (c4rex, DAppNode, Infura). ## Uploading and Pining diff --git a/package.json b/package.json index c669a14..266dbaf 100644 --- a/package.json +++ b/package.json @@ -76,6 +76,7 @@ "dotenv": "^16.0.0", "dreamhost": "^1.0.5", "form-data": "^4.0.0", + "got": "^11.5.1", "ipfs-http-client": "^50.1.0", "it-all": "^1.0.6", "lodash.isempty": "^4.4.0", diff --git a/src/dnslinkers/cloudflare.js b/src/dnslinkers/cloudflare.js index a3dbfb8..be4dd4b 100644 --- a/src/dnslinkers/cloudflare.js +++ b/src/dnslinkers/cloudflare.js @@ -3,6 +3,7 @@ // @ts-ignore const dnslink = require('dnslink-cloudflare') const isEmpty = require('lodash.isempty') +const getLinkedCid = require('../lib/cloudflareCid') /** * @typedef {import('./types').DNSRecord} DNSRecord @@ -53,10 +54,10 @@ class Cloudflare { } /** - * @returns {Promise} + * @returns {Promise} */ async getLinkedCid () { - throw new Error('getLinkedCid not implemented in Cloudflare') + return getLinkedCid(this.api, this.opts) } static get displayName () { diff --git a/src/lib/cloudflareCid.js b/src/lib/cloudflareCid.js new file mode 100644 index 0000000..f89a2be --- /dev/null +++ b/src/lib/cloudflareCid.js @@ -0,0 +1,66 @@ +// @ts-nocheck +'use strict' +// This is copy of dnslink-cloudflare with removed update and added getLinkedCid. +const got = require('got') + +async function getZoneId (api, name) { + let res + + for (let i = 1; (res = await api(`zones?page=${i}`)) && res.body.result_info.total_pages >= i; i++) { + for (const zone of res.body.result) { + if (zone.name === name) { + return zone.id + } + } + } + + throw new Error(`zone ${name} couldn't be found`) +} + +async function getRecord (api, id, name) { + let res + + for (let i = 1; (res = await api(`zones/${id}/dns_records?type=TXT&page=${i}`)) && res.body.result_info.total_pages >= i; i++) { + for (const record of res.body.result) { + if (record.name === name && record.content.startsWith('dnslink=')) { + return record + } + } + } + + return null +} + +function getClient (apiOpts) { + const opts = { + prefixUrl: 'https://api.cloudflare.com/client/v4', + responseType: 'json' + } + + if (apiOpts.token) { + opts.headers = { + Authorization: `Bearer ${apiOpts.token}` + } + } else { + opts.headers = { + 'X-Auth-Email': apiOpts.email, + 'X-Auth-Key': apiOpts.key + } + } + + return got.extend(opts) +} + +async function getLinkedCid (apiOpts, { zone, record }) { + const api = getClient(apiOpts) + const id = await getZoneId(api, zone) + const zoneRecord = await getRecord(api, id, record) + if (zoneRecord) { + // assuming content 'dnslink=/ipfs/QmZ4tDuvesekSs4qM5ZBKpXiZGun7S2CYtEZRB3DYXkjGx' + return zoneRecord.content.slice(zoneRecord.content.lastIndexOf('/') + 1) + } else { + return null + } +} + +module.exports = getLinkedCid From ae07144d5e4cf8a5b66d3837987291a63f4d47c3 Mon Sep 17 00:00:00 2001 From: Banciur Date: Sun, 10 Apr 2022 23:41:07 +0200 Subject: [PATCH 3/6] implement unpinning for ipfs-node.js --- src/deploy.js | 16 ++++++++++++++-- src/pinners/ipfs-cluster.js | 2 +- src/pinners/ipfs-node.js | 4 ++-- src/pinners/pinata.js | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/deploy.js b/src/deploy.js index a65dc00..ea2e738 100644 --- a/src/deploy.js +++ b/src/deploy.js @@ -206,10 +206,22 @@ async function unpin (dnsServices, pinServices, logger) { } const cidToUnpin = linkedCids[0] + if (!cidToUnpin) { + logger.info('There is nothing to unpin') + return + } for (const pinProvider of pinServices) { logger.info(`Unpinning ${cidToUnpin} from ${pinProvider.displayName}`) - pinProvider.unpinCid(cidToUnpin) + try { + await pinProvider.unpinCid(cidToUnpin) + } catch (e) { + if (e.name === 'HTTPError' && e.message === 'not pinned or pinned indirectly') { + logger.info(`${cidToUnpin} not pinned to ${pinProvider.displayName}, moving forward`) + } else { + throw (e) + } + } } } @@ -278,7 +290,7 @@ async function deploy ({ if (dnsProviders.length === 0) { throw new Error('If you want to unpin you must provide dns provider') } - await unpin(dnsProviders, pinningServices, logger) + await unpin(dnsProviders, uploadServices.concat(pinningServices), logger) } const pinnedCids = /** @type {string[]} */([]) diff --git a/src/pinners/ipfs-cluster.js b/src/pinners/ipfs-cluster.js index 6b3c19d..f5555e1 100644 --- a/src/pinners/ipfs-cluster.js +++ b/src/pinners/ipfs-cluster.js @@ -76,7 +76,7 @@ class IpfsCluster { /** * @param {string} cid */ - unpinCid (cid) { + async unpinCid (cid) { throw new Error('unpinCid not implemented in IpfsCluster') } diff --git a/src/pinners/ipfs-node.js b/src/pinners/ipfs-node.js index 18a33e3..50b5ac2 100644 --- a/src/pinners/ipfs-node.js +++ b/src/pinners/ipfs-node.js @@ -46,8 +46,8 @@ class IpfsNode { /** * @param {string} cid */ - unpinCid (cid) { - throw new Error('unpinCid not implemented in IpfsNode') + async unpinCid (cid) { + await this.ipfs.pin.rm(cid) } /** diff --git a/src/pinners/pinata.js b/src/pinners/pinata.js index 10d09e0..0b7cdcb 100644 --- a/src/pinners/pinata.js +++ b/src/pinners/pinata.js @@ -94,7 +94,7 @@ class Pinata { /** * @param {string} cid */ - unpinCid (cid) { + async unpinCid (cid) { throw new Error('unpinCid not implemented in Pinata') } From e42679e2f0f56e3594c58943aa3473a0b1c20033 Mon Sep 17 00:00:00 2001 From: Banciur Date: Sun, 10 Apr 2022 23:53:36 +0200 Subject: [PATCH 4/6] move catch pinning error to correct place --- src/deploy.js | 10 +--------- src/pinners/ipfs-cluster.js | 4 +++- src/pinners/ipfs-node.js | 14 ++++++++++++-- src/pinners/pinata.js | 4 +++- src/pinners/types.ts | 4 +++- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/deploy.js b/src/deploy.js index ea2e738..72ee257 100644 --- a/src/deploy.js +++ b/src/deploy.js @@ -213,15 +213,7 @@ async function unpin (dnsServices, pinServices, logger) { for (const pinProvider of pinServices) { logger.info(`Unpinning ${cidToUnpin} from ${pinProvider.displayName}`) - try { - await pinProvider.unpinCid(cidToUnpin) - } catch (e) { - if (e.name === 'HTTPError' && e.message === 'not pinned or pinned indirectly') { - logger.info(`${cidToUnpin} not pinned to ${pinProvider.displayName}, moving forward`) - } else { - throw (e) - } - } + await pinProvider.unpinCid(cidToUnpin, logger) } } diff --git a/src/pinners/ipfs-cluster.js b/src/pinners/ipfs-cluster.js index f5555e1..323a67c 100644 --- a/src/pinners/ipfs-cluster.js +++ b/src/pinners/ipfs-cluster.js @@ -8,6 +8,7 @@ const { getDirFormData } = require('./utils') /** * @typedef {import('./types').IPFSClusterOptions} IPFSClusterOptions * @typedef {import('./types').PinDirOptions} PinDirOptions + * @typedef {import('../types').Logger} Logger */ class IpfsCluster { @@ -75,8 +76,9 @@ class IpfsCluster { /** * @param {string} cid + * @param {Logger} logger */ - async unpinCid (cid) { + async unpinCid (cid, logger) { throw new Error('unpinCid not implemented in IpfsCluster') } diff --git a/src/pinners/ipfs-node.js b/src/pinners/ipfs-node.js index 50b5ac2..26f6f1c 100644 --- a/src/pinners/ipfs-node.js +++ b/src/pinners/ipfs-node.js @@ -7,6 +7,7 @@ const path = require('path') /** * @typedef {import('ipfs-http-client').Options} IpfsOptions * @typedef {import('./types').PinDirOptions} PinDirOptions + * @typedef {import('../types').Logger} Logger */ class IpfsNode { @@ -45,9 +46,18 @@ class IpfsNode { /** * @param {string} cid + * @param {Logger} logger */ - async unpinCid (cid) { - await this.ipfs.pin.rm(cid) + async unpinCid (cid, logger) { + try { + await this.ipfs.pin.rm(cid) + } catch (e) { + if (e.name === 'HTTPError' && e.message === 'not pinned or pinned indirectly') { + logger.info(`${cid} not pinned to ${this.displayName}, moving forward`) + } else { + throw (e) + } + } } /** diff --git a/src/pinners/pinata.js b/src/pinners/pinata.js index 0b7cdcb..37df384 100644 --- a/src/pinners/pinata.js +++ b/src/pinners/pinata.js @@ -7,6 +7,7 @@ const { getDirFormData } = require('./utils') /** * @typedef {import('./types').PinataOptions} PinataOptions * @typedef {import('./types').PinDirOptions} PinDirOptions + * @typedef {import('../types').Logger} Logger */ const MAX_RETRIES = 3 @@ -93,8 +94,9 @@ class Pinata { /** * @param {string} cid + * @param {Logger} logger */ - async unpinCid (cid) { + async unpinCid (cid, logger) { throw new Error('unpinCid not implemented in Pinata') } diff --git a/src/pinners/types.ts b/src/pinners/types.ts index f18ed02..88304f4 100644 --- a/src/pinners/types.ts +++ b/src/pinners/types.ts @@ -1,3 +1,5 @@ +import type { Logger } from '../types' + export interface PinDirOptions { tag?: string hidden?: boolean @@ -6,7 +8,7 @@ export interface PinDirOptions { export interface PinningService { pinDir: (dir: string, options: PinDirOptions|undefined) => Promise pinCid: (cid: string, tag: string|undefined) => void - unpinCid: (cid: string) => void + unpinCid: (cid: string, logger: Logger) => void gatewayUrl: (cid: string) => string displayName: string } From d06835f077a65080cd4934963ffe6e1f6819bb9e Mon Sep 17 00:00:00 2001 From: Banciur Date: Sun, 3 Jul 2022 16:54:45 +0200 Subject: [PATCH 5/6] Use dnslink-cloudflare that handles web3 feature --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 266dbaf..815d7b4 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,7 @@ "byte-size": "^8.1.0", "chalk": "^4.1.1", "clipboardy": "^2.3.0", - "dnslink-cloudflare": "^3.0.0", + "dnslink-cloudflare": "banciur/dnslink-cloudflare#banciur/handle_web3", "dnslink-dnsimple": "^1.0.1", "dotenv": "^16.0.0", "dreamhost": "^1.0.5", From ecd87cd8b6ddd597545d82ded85b232cb8eb3957 Mon Sep 17 00:00:00 2001 From: Banciur Date: Thu, 7 Jul 2022 21:01:14 +0200 Subject: [PATCH 6/6] Finalize changes --- md/usage.md | 6 +++--- src/cli.js | 2 +- src/deploy.js | 1 + src/dnslinkers/cloudflare.js | 10 +++++----- src/dnslinkers/types.ts | 2 +- src/lib/cloudflareCid.js | 6 +++--- 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/md/usage.md b/md/usage.md index 4ce0273..682728f 100644 --- a/md/usage.md +++ b/md/usage.md @@ -217,18 +217,18 @@ being updated. - `IPFS_DEPLOY_CLOUDFLARE__API_TOKEN=` - Configuration - `IPFS_DEPLOY_CLOUDFLARE__ZONE=` - - `IPFS_DEPLOY_CLOUDFLARE__RECORD=` + - `IPFS_DEPLOY_CLOUDFLARE__WEB3_HOSTNAME=` #### Examples ```bash # Top level domain IPFS_DEPLOY_CLOUDFLARE__ZONE=example.com -IPFS_DEPLOY_CLOUDFLARE__RECORD=_dnslink.example.com +IPFS_DEPLOY_CLOUDFLARE__WEB3_HOSTNAME=example.com # Subdomain IPFS_DEPLOY_CLOUDFLARE__ZONE=example.com -IPFS_DEPLOY_CLOUDFLARE__RECORD=_dnslink.mysubdomain.example.com +IPFS_DEPLOY_CLOUDFLARE__WEB3_HOSTNAME=mysubdomain.example.com ``` ### [AWS-Route53](https://aws.amazon.com/route53/) diff --git a/src/cli.js b/src/cli.js index 6cdb25b..d11f799 100755 --- a/src/cli.js +++ b/src/cli.js @@ -133,7 +133,7 @@ const options = { apiToken: argv.cloudflare && argv.cloudflare.apiToken, apiEmail: argv.cloudflare && argv.cloudflare.apiEmail, zone: argv.cloudflare && argv.cloudflare.zone, - record: argv.cloudflare && argv.cloudflare.record + web3Hostname: argv.cloudflare && argv.cloudflare.web3Hostname }, route53: { accessKeyId: argv.route53 && argv.route53.accessKeyId, diff --git a/src/deploy.js b/src/deploy.js index 72ee257..ddb8702 100644 --- a/src/deploy.js +++ b/src/deploy.js @@ -275,6 +275,7 @@ async function deploy ({ logger.info('⚙️ Validating DNS providers configurations…') const dnsProviders = dnsProvidersIds.map(name => { const DNSLinker = dnsLinkersMap.get(name) + // logger.info(dnsProvidersCredentials[name]) return new DNSLinker(dnsProvidersCredentials[name]) }) diff --git a/src/dnslinkers/cloudflare.js b/src/dnslinkers/cloudflare.js index be4dd4b..d77c36b 100644 --- a/src/dnslinkers/cloudflare.js +++ b/src/dnslinkers/cloudflare.js @@ -14,13 +14,13 @@ class Cloudflare { /** * @param {CloudflareOptions} options */ - constructor ({ apiEmail, apiKey, apiToken, zone, record }) { + constructor ({ apiEmail, apiKey, apiToken, zone, web3Hostname }) { if ([apiKey, apiEmail, apiToken].every(isEmpty)) { throw new Error('apiEmail and apiKey or apiToken are required for Cloudflare') } - if ([zone, record].some(isEmpty)) { - throw new Error('zone and record are required for CloudFlare') + if ([zone, web3Hostname].some(isEmpty)) { + throw new Error('zone and web3 hostname are required for CloudFlare') } if (isEmpty(apiKey)) { @@ -32,7 +32,7 @@ class Cloudflare { } } - this.opts = { record, zone } + this.opts = { web3Hostname, zone } } /** @@ -48,7 +48,7 @@ class Cloudflare { const content = await dnslink(this.api, opts) return { - record: opts.record, + record: opts.web3Hostname, value: content } } diff --git a/src/dnslinkers/types.ts b/src/dnslinkers/types.ts index 60f833a..58f0bc4 100644 --- a/src/dnslinkers/types.ts +++ b/src/dnslinkers/types.ts @@ -14,7 +14,7 @@ export interface CloudflareOptions { apiKey?: string apiToken?: string zone: string - record: string + web3Hostname: string } export interface DNSimpleOptions { diff --git a/src/lib/cloudflareCid.js b/src/lib/cloudflareCid.js index f89a2be..7ad07dd 100644 --- a/src/lib/cloudflareCid.js +++ b/src/lib/cloudflareCid.js @@ -22,7 +22,7 @@ async function getRecord (api, id, name) { for (let i = 1; (res = await api(`zones/${id}/dns_records?type=TXT&page=${i}`)) && res.body.result_info.total_pages >= i; i++) { for (const record of res.body.result) { - if (record.name === name && record.content.startsWith('dnslink=')) { + if (record.name.includes(name) && record.content.startsWith('dnslink=')) { return record } } @@ -51,10 +51,10 @@ function getClient (apiOpts) { return got.extend(opts) } -async function getLinkedCid (apiOpts, { zone, record }) { +async function getLinkedCid (apiOpts, { zone, web3Hostname }) { const api = getClient(apiOpts) const id = await getZoneId(api, zone) - const zoneRecord = await getRecord(api, id, record) + const zoneRecord = await getRecord(api, id, web3Hostname) if (zoneRecord) { // assuming content 'dnslink=/ipfs/QmZ4tDuvesekSs4qM5ZBKpXiZGun7S2CYtEZRB3DYXkjGx' return zoneRecord.content.slice(zoneRecord.content.lastIndexOf('/') + 1)