From b6fcfad0eebc5f3bbf2182b7649599e420a26a8b Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 26 Feb 2021 16:56:48 +0000 Subject: [PATCH 01/11] feat: add types Adds ts type generation and fixes all tsc errors. --- package.json | 5 ++- src/index.js | 75 ++++++++++++++++++++++++++++++++++++- test/test-cid.spec.js | 1 + test/test-multiaddr.spec.js | 1 + test/test-path.spec.js | 1 + tsconfig.json | 10 +++++ 6 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 tsconfig.json diff --git a/package.json b/package.json index 662c0ee..4bd0936 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "url": "https://github.com/ipfs/is-ipfs.git" }, "scripts": { + "prepare": "aegir build --no-bundle", "test:node": "aegir test --target node", "test:browser": "aegir test --target browser", "test": "aegir test", @@ -36,7 +37,7 @@ "release": "aegir release", "release-minor": "aegir release --type minor", "release-major": "aegir release --type major", - "build": "aegir build", + "prepublishOnly": "aegir build", "coverage": "aegir coverage", "coverage-publish": "aegir coverage --upload" }, @@ -50,7 +51,7 @@ "uint8arrays": "^2.0.5" }, "devDependencies": { - "aegir": "^30.3.0", + "aegir": "^31.0.0", "pre-commit": "^1.2.2" }, "engines": { diff --git a/src/index.js b/src/index.js index ad18edb..ce7bc53 100644 --- a/src/index.js +++ b/src/index.js @@ -5,6 +5,7 @@ const multibase = require('multibase') const Multiaddr = require('multiaddr') const mafmt = require('mafmt') const CID = require('cids') +// @ts-ignore const { URL } = require('iso-url') const uint8ArrayToString = require('uint8arrays/to-string') @@ -21,6 +22,9 @@ const subdomainProtocolMatch = 2 // Fully qualified domain name (FQDN) that has an explicit .tld suffix const fqdnWithTld = /^(([a-z0-9]|[a-z0-9][a-z0-9-]*[a-z0-9])\.)+([a-z0-9]|[a-z0-9][a-z0-9-]*[a-z0-9])$/ +/** + * @param {*} hash + */ function isMultihash (hash) { const formatted = convertToString(hash) try { @@ -31,6 +35,9 @@ function isMultihash (hash) { } } +/** + * @param {*} hash + */ function isMultibase (hash) { try { return multibase.isEncoded(hash) @@ -39,6 +46,9 @@ function isMultibase (hash) { } } +/** + * @param {*} hash + */ function isCID (hash) { try { new CID(hash) // eslint-disable-line no-new @@ -48,6 +58,9 @@ function isCID (hash) { } } +/** + * @param {*} input + */ function isMultiaddr (input) { if (!input) return false if (Multiaddr.isMultiaddr(input)) return true @@ -59,10 +72,19 @@ function isMultiaddr (input) { } } +/** + * @param {string | Uint8Array | Multiaddr} input + */ function isPeerMultiaddr (input) { return isMultiaddr(input) && (mafmt.P2P.matches(input) || mafmt.DNS.matches(input)) } +/** + * @param {string | Uint8Array} input + * @param {RegExp | string} pattern + * @param {number} [protocolMatch=1] + * @param {number} [hashMatch=2] + */ function isIpfs (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch = defaultHashMath) { const formatted = convertToString(input) if (!formatted) { @@ -83,14 +105,21 @@ function isIpfs (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch if (hash && pattern === subdomainGatewayPattern) { // when doing checks for subdomain context // ensure hash is case-insensitive - // (browsers force-lowercase authority compotent anyway) + // (browsers force-lowercase authority competent anyway) hash = hash.toLowerCase() } return isCID(hash) } -function isIpns (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch) { +/** + * + * @param {string | Uint8Array} input + * @param {string | RegExp} pattern + * @param {number} [protocolMatch=1] + * @param {number} [hashMatch=1] + */ +function isIpns (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch = defaultHashMath) { const formatted = convertToString(input) if (!formatted) { return false @@ -133,10 +162,16 @@ function isIpns (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch return true } +/** + * @param {any} input + */ function isString (input) { return typeof input === 'string' } +/** + * @param {Uint8Array | string} input + */ function convertToString (input) { if (input instanceof Uint8Array) { return uint8ArrayToString(input, 'base58btc') @@ -149,14 +184,35 @@ function convertToString (input) { return false } +/** + * @param {string | Uint8Array} url + */ const ipfsSubdomain = (url) => isIpfs(url, subdomainGatewayPattern, subdomainProtocolMatch, subdomainIdMatch) +/** + * @param {string | Uint8Array} url + */ const ipnsSubdomain = (url) => isIpns(url, subdomainGatewayPattern, subdomainProtocolMatch, subdomainIdMatch) +/** + * @param {string | Uint8Array} url + */ const subdomain = (url) => ipfsSubdomain(url) || ipnsSubdomain(url) +/** + * @param {string | Uint8Array} url + */ const ipfsUrl = (url) => isIpfs(url, pathGatewayPattern) || ipfsSubdomain(url) +/** + * @param {string | Uint8Array} url + */ const ipnsUrl = (url) => isIpns(url, pathGatewayPattern) || ipnsSubdomain(url) +/** + * @param {string | Uint8Array} url + */ const url = (url) => ipfsUrl(url) || ipnsUrl(url) || subdomain(url) +/** + * @param {string | Uint8Array} path + */ const path = (path) => isIpfs(path, pathPattern) || isIpns(path, pathPattern) module.exports = { @@ -164,6 +220,9 @@ module.exports = { multiaddr: isMultiaddr, peerMultiaddr: isPeerMultiaddr, cid: isCID, + /** + * @param {CID | string | Uint8Array} cid + */ base32cid: (cid) => (isMultibase(cid) === 'base32' && isCID(cid)), ipfsSubdomain, ipnsSubdomain, @@ -173,10 +232,22 @@ module.exports = { ipnsUrl, url, pathGatewayPattern: pathGatewayPattern, + /** + * @param {string | Uint8Array} path + */ ipfsPath: (path) => isIpfs(path, pathPattern), + /** + * @param {string | Uint8Array} path + */ ipnsPath: (path) => isIpns(path, pathPattern), path, pathPattern, + /** + * @param {string | Uint8Array} x + */ urlOrPath: (x) => url(x) || path(x), + /** + * @param {string | Uint8Array | CID} path + */ cidPath: path => isString(path) && !isCID(path) && isIpfs(`/ipfs/${path}`, pathPattern) } diff --git a/test/test-cid.spec.js b/test/test-cid.spec.js index 68d42d8..f3d090e 100644 --- a/test/test-cid.spec.js +++ b/test/test-cid.spec.js @@ -101,6 +101,7 @@ describe('ipfs base32cid', () => { }) it('isIPFS.base32cid should not match an invalid CID data type', (done) => { + // @ts-ignore data type is invalid const actual = isIPFS.base32cid(4) expect(actual).to.equal(false) done() diff --git a/test/test-multiaddr.spec.js b/test/test-multiaddr.spec.js index ee5a984..0ab9066 100644 --- a/test/test-multiaddr.spec.js +++ b/test/test-multiaddr.spec.js @@ -159,6 +159,7 @@ describe('ipfs peerMultiaddr', () => { }) it('isIPFS.peerMultiaddr should not match an invalid multiaddr data type', (done) => { + // @ts-ignore data type is invalid const actual = isIPFS.peerMultiaddr(4) expect(actual).to.equal(false) done() diff --git a/test/test-path.spec.js b/test/test-path.spec.js index a200f1d..53e8562 100644 --- a/test/test-path.spec.js +++ b/test/test-path.spec.js @@ -148,6 +148,7 @@ describe('ipfs path', () => { }) it('isIPFS.cidPath should not match a non string', () => { + // @ts-ignore data type is invalid const actual = isIPFS.cidPath({ toString: () => 'QmYHNYAaYK5hm3ZhZFx5W9H6xydKDGimjdgJMrMSdnctEm/path/to/file' }) expect(actual).to.equal(false) }) diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..77830df --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "./node_modules/aegir/src/config/tsconfig.aegir.json", + "compilerOptions": { + "outDir": "dist" + }, + "include": [ + "src", + "test" + ] +} From 32e8545391bd86c5be6848a400028ca60a3e88bc Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 26 Feb 2021 17:29:07 +0000 Subject: [PATCH 02/11] chore: fix up travis --- .travis.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f217408..8c55994 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,11 @@ stages: - test - cov +branches: + only: + - master + - /^release\/.*$/ + node_js: - 'lts/*' - 'node' @@ -21,7 +26,6 @@ jobs: include: - stage: check script: - - npx aegir commitlint --travis - npx aegir dep-check - npm run lint From 571e844fd996fe68ecc2dd2628dfecab8b9f35da Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 26 Feb 2021 17:32:13 +0000 Subject: [PATCH 03/11] chore: update aegir command --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8c55994..fa5639e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -39,7 +39,7 @@ jobs: name: firefox addons: firefox: latest - script: npx aegir test -t browser -t webworker -- --browsers FirefoxHeadless + script: npx aegir test -t browser -t webworker -- --browser Firefox - stage: test name: electron-main From e6c858e7114afc79e473c8a10b1e32f0f4971322 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 26 Feb 2021 17:38:16 +0000 Subject: [PATCH 04/11] chore: use correct browser name --- .travis.yml | 2 +- tsconfig.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index fa5639e..c27cf9f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -39,7 +39,7 @@ jobs: name: firefox addons: firefox: latest - script: npx aegir test -t browser -t webworker -- --browser Firefox + script: npx aegir test -t browser -t webworker -- --browser firefox - stage: test name: electron-main diff --git a/tsconfig.json b/tsconfig.json index 77830df..13a3599 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,5 @@ { - "extends": "./node_modules/aegir/src/config/tsconfig.aegir.json", + "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { "outDir": "dist" }, From c095f71bba95db196490e9d975179a67759ca29b Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 26 Feb 2021 17:44:28 +0000 Subject: [PATCH 05/11] chore: fix ts config path --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 13a3599..77830df 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,5 @@ { - "extends": "aegir/src/config/tsconfig.aegir.json", + "extends": "./node_modules/aegir/src/config/tsconfig.aegir.json", "compilerOptions": { "outDir": "dist" }, From b163e2215cf62ed45e2f55e896bf695f470577c6 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 26 Feb 2021 17:49:24 +0000 Subject: [PATCH 06/11] chore: update linux version --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index c27cf9f..b437ba0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: node_js cache: npm +dist: bionic stages: - check - test From 051a33941d0b51212fcffc31e6e7c12eef772391 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 26 Feb 2021 17:53:27 +0000 Subject: [PATCH 07/11] chore: fix typo --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index ce7bc53..8a4364c 100644 --- a/src/index.js +++ b/src/index.js @@ -105,7 +105,7 @@ function isIpfs (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch if (hash && pattern === subdomainGatewayPattern) { // when doing checks for subdomain context // ensure hash is case-insensitive - // (browsers force-lowercase authority competent anyway) + // (browsers force-lowercase authority component anyway) hash = hash.toLowerCase() } From 0fec2ac1fe1d18800295834f02126f9431ee7db2 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Mon, 1 Mar 2021 11:14:15 +0000 Subject: [PATCH 08/11] chore: remove reundant npm scripts --- package.json | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/package.json b/package.json index 4bd0936..d24b362 100644 --- a/package.json +++ b/package.json @@ -36,10 +36,7 @@ "lint": "aegir lint && aegir lint-package-json", "release": "aegir release", "release-minor": "aegir release --type minor", - "release-major": "aegir release --type major", - "prepublishOnly": "aegir build", - "coverage": "aegir coverage", - "coverage-publish": "aegir coverage --upload" + "release-major": "aegir release --type major" }, "dependencies": { "cids": "^1.1.5", From 1064e2de349e3230159db8b36ef2ba42674e88e0 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Mon, 1 Mar 2021 11:34:19 +0000 Subject: [PATCH 09/11] chore: add types path to package.json --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index d24b362..571c785 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "src", "dist" ], + "types": "./dist/src/index.d.ts", "main": "src/index.js", "browser": { "fs": false From b9bad9d7da56bbf3bd86f13806f393bdf2072829 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Mon, 1 Mar 2021 17:46:20 +0000 Subject: [PATCH 10/11] chore: update deps and remove ts-ignore --- package.json | 6 +++--- src/index.js | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 571c785..7411ee8 100644 --- a/package.json +++ b/package.json @@ -41,11 +41,11 @@ }, "dependencies": { "cids": "^1.1.5", - "iso-url": "^1.0.0", + "iso-url": "^1.1.0", "mafmt": "^8.0.4", "multiaddr": "^8.1.2", - "multibase": "^3.1.1", - "multihashes": "^3.1.2", + "multibase": "^4.0.1", + "multihashes": "^4.0.0", "uint8arrays": "^2.0.5" }, "devDependencies": { diff --git a/src/index.js b/src/index.js index 8a4364c..24bde7e 100644 --- a/src/index.js +++ b/src/index.js @@ -5,7 +5,6 @@ const multibase = require('multibase') const Multiaddr = require('multiaddr') const mafmt = require('mafmt') const CID = require('cids') -// @ts-ignore const { URL } = require('iso-url') const uint8ArrayToString = require('uint8arrays/to-string') From 2a03f545d8eab3386eabc49ef5be6f0eac4e62d8 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Wed, 3 Mar 2021 19:44:39 +0100 Subject: [PATCH 11/11] fix: restore browser bundle It is documented in https://github.com/ipfs-shipyard/is-ipfs#in-the-browser-through-script-tag License: MIT Signed-off-by: Marcin Rataj --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 7411ee8..d5d1a9f 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "test:node": "aegir test --target node", "test:browser": "aegir test --target browser", "test": "aegir test", + "prepublishOnly": "aegir build", "lint": "aegir lint && aegir lint-package-json", "release": "aegir release", "release-minor": "aegir release --type minor",