From d31bbca3bc01c3676ec67dc57fc9cc22ffbb0dae Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 9 Apr 2021 16:41:35 +0100 Subject: [PATCH 1/2] chore: update deps and types Updates all deps, pulls in tsconfig from aegir and fixes the new ts issues that identified. --- package.json | 4 ++-- src/index.js | 52 +++++++++++++++++++++++++++++++++++++++------- test/index.spec.js | 20 +++++++++++------- tsconfig.json | 38 +++++---------------------------- 4 files changed, 64 insertions(+), 50 deletions(-) diff --git a/package.json b/package.json index 421be13..d509575 100644 --- a/package.json +++ b/package.json @@ -30,12 +30,12 @@ "devDependencies": { "@types/chai": "^4.2.8", "@types/mocha": "^8.0.0", - "aegir": "^32.0.2", + "aegir": "^33.0.0", "uint8arrays": "^2.0.5", "util": "^0.12.3" }, "dependencies": { - "multiaddr": "^8.0.0" + "multiaddr": "^9.0.1" }, "contributors": [ "David Dias ", diff --git a/src/index.js b/src/index.js index b2a6c49..a759118 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,11 @@ 'use strict' -const multiaddr = require('multiaddr') +const { Multiaddr } = require('multiaddr') + +/** + * @typedef {(a: Multiaddr | string) => boolean} MatchesFunction + * @typedef {(a: string[]) => boolean | null | string[]} PartialMatchesFunction + */ /* * Valid combinations @@ -144,25 +149,44 @@ exports.IPFS = P2P * Validation funcs */ +/** + * @param {PartialMatchesFunction} partialMatch + */ function makeMatchesFunction (partialMatch) { - return function matches (a) { - if (!multiaddr.isMultiaddr(a)) { + /** + * @type {MatchesFunction} + */ + function matches (a) { + if (!Multiaddr.isMultiaddr(a)) { try { - a = multiaddr(a) + a = new Multiaddr(a) } catch (err) { // catch error - return false // also if it's invalid it's propably not matching as well so return false + return false // also if it's invalid it's probably not matching as well so return false } } const out = partialMatch(a.protoNames()) + if (out === null) { return false } + + if (out === true || out === false) { + return out + } + return out.length === 0 } + + return matches } -function and () { - const args = Array.from(arguments) +/** + * @param {...any} args + */ +function and (...args) { + /** + * @type {PartialMatchesFunction} + */ function partialMatch (a) { if (a.length < args.length) { return null @@ -192,6 +216,9 @@ function and () { function or () { const args = Array.from(arguments) + /** + * @type {PartialMatchesFunction} + */ function partialMatch (a) { let out = null args.some((arg) => { @@ -218,13 +245,19 @@ function or () { return result } +/** + * @param {string} n + */ function base (n) { const name = n + /** + * @type {MatchesFunction} + */ function matches (a) { if (typeof a === 'string') { try { - a = multiaddr(a) + a = new Multiaddr(a) } catch (err) { // catch error return false // also if it's invalid it's propably not matching as well so return false } @@ -237,6 +270,9 @@ function base (n) { return false } + /** + * @type {PartialMatchesFunction} + */ function partialMatch (protos) { if (protos.length === 0) { return null diff --git a/test/index.spec.js b/test/index.spec.js index 7a1e9e6..86637ef 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -3,7 +3,7 @@ 'use strict' const { expect } = require('aegir/utils/chai') -const multiaddr = require('multiaddr') +const { Multiaddr } = require('multiaddr') const uint8ArrayFromString = require('uint8arrays/from-string') const mafmt = require('./../src') @@ -190,13 +190,16 @@ describe('multiaddr validation', function () { '/dns6/nyc-2.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64' ].concat(goodCircuit) - function assertMatches (p) { - const tests = Array.from(arguments).slice(1) + /** + * @param {mafmt.Mafmt} p + * @param {...string[]} tests + */ + function assertMatches (p, ...tests) { tests.forEach(function (test) { test.forEach(function (testcase) { try { expect(p.matches(testcase), `assertMatches: ${testcase} (string)`).to.be.eql(true) - const ma = multiaddr(testcase) + const ma = new Multiaddr(testcase) expect(p.matches(ma), `assertMatches: ${testcase} (multiaddr object)`).to.be.eql(true) expect(p.matches(ma.bytes), `assertMatches: ${testcase} (multiaddr.bytes)`).to.be.eql(true) } catch (err) { @@ -207,8 +210,11 @@ describe('multiaddr validation', function () { }) } - function assertMismatches (p) { - const tests = Array.from(arguments).slice(1) + /** + * @param {mafmt.Mafmt} p + * @param {...string[]} tests + */ + function assertMismatches (p, ...tests) { tests.forEach(function (test) { test.forEach(function (testcase) { try { @@ -217,7 +223,7 @@ describe('multiaddr validation', function () { try { // if testcase string happens to be a valid multiaddr, // we expect 'p' test to also return false for Multiaddr object and Uint8Array versions - validMultiaddrObj = multiaddr(testcase) + validMultiaddrObj = new Multiaddr(testcase) } catch (e) { // Ignoring testcase as the string is not a multiaddr // (There is a separate 'Uint8Array is invalid' test later below) diff --git a/tsconfig.json b/tsconfig.json index 5a029cc..05bfbfd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,38 +1,10 @@ - { + "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { - "module": "commonjs", - "lib": [ - "es6" - ], - "target": "ES5", - "noImplicitAny": false, - "noImplicitThis": true, - "strictFunctionTypes": true, - "strictNullChecks": true, - "esModuleInterop": true, - "resolveJsonModule": true, - "allowJs": true, - "checkJs": true, - "baseUrl": ".", - "paths": { - "mafmt": [ - "./src", - "../src" - ] - }, - "types": [ - "node", - "mocha", - "chai" - ], - "noEmit": true, - "forceConsistentCasingInFileNames": true + "outDir": "dist" }, - "files": [ - "./src/index.d.ts" - ], "include": [ - "./test/**/*.spec.js" + "src", + "test" ] -} + } From 86ce30394710e7a7ffda6979a3c90d1d2e0d0e36 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 9 Apr 2021 18:06:59 +0100 Subject: [PATCH 2/2] chore: pr comments, tidy up generated types --- .npmignore | 7 ---- .travis.yml | 24 +++++++++----- README.md | 2 +- package.json | 6 ++++ src/index.d.ts | 29 ----------------- src/index.js | 79 +++++++++++++++++++++++++++------------------- src/types.d.ts | 10 ++++++ test/index.spec.js | 4 +-- 8 files changed, 82 insertions(+), 79 deletions(-) delete mode 100644 .npmignore delete mode 100644 src/index.d.ts create mode 100644 src/types.d.ts diff --git a/.npmignore b/.npmignore deleted file mode 100644 index bea1fc0..0000000 --- a/.npmignore +++ /dev/null @@ -1,7 +0,0 @@ -node_modules - -coverage - -*.log - -test diff --git a/.travis.yml b/.travis.yml index 1390f8c..0e3ac6b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,42 +1,50 @@ language: node_js cache: npm +dist: bionic + +branches: + only: + - master + - /^release\/.*$/ + stages: - check - test - cov node_js: - - '12' - - '10' + - 'lts/*' + - 'node' os: - linux - osx - windows -script: npx nyc -s npm run test:node -- --bail +script: npm run test:node -- --cov after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov jobs: include: + - os: windows + cache: false + - stage: check script: - - npx aegir dep-check -- -i wrtc -i electron-webrtc + - npx aegir dep-check - npm run lint - stage: test name: chrome addons: chrome: stable - script: - - npx aegir test -t browser + script: npx aegir test -t browser -t webworker - stage: test name: firefox addons: firefox: latest - script: - - npx aegir test -t browser -- --browsers FirefoxHeadless + script: npx aegir test -t browser -t webworker -- --browser firefox notifications: email: false diff --git a/README.md b/README.md index 79765e0..95b7527 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,6 @@ Where `multiaddr` may be: * a [Multiaddr](https://www.npmjs.com/package/multiaddr) * a String -* a [Buffer](https://www.npmjs.com/package/buffer) +* a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) Returns `true`/`false` diff --git a/package.json b/package.json index d509575..1082d9e 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "A multiaddr validator", "leadMaintainer": "Vasco Santos ", "main": "src/index.js", + "types": "dist/src/index.d.ts", "scripts": { "lint": "aegir lint", "build": "aegir build", @@ -37,6 +38,11 @@ "dependencies": { "multiaddr": "^9.0.1" }, + "files": [ + "src", + "dist", + "!dist/*.tsbuildinfo" + ], "contributors": [ "David Dias ", "Vasco Santos ", diff --git a/src/index.d.ts b/src/index.d.ts deleted file mode 100644 index 8b9ae98..0000000 --- a/src/index.d.ts +++ /dev/null @@ -1,29 +0,0 @@ -import Multiaddr = require('multiaddr'); - -export declare interface Mafmt { - toString(): string; - input?: (Mafmt | (() => Mafmt))[]; - matches: (a: string | Uint8Array | Multiaddr) => boolean; - partialMatch: (protos: string[]) => boolean; -} - -export const DNS: Mafmt; -export const DNS4: Mafmt; -export const DNS6: Mafmt; -export const IP: Mafmt; -export const TCP: Mafmt; -export const UDP: Mafmt; -export const QUIC: Mafmt; -export const UTP: Mafmt; -export const HTTP: Mafmt; -export const HTTPS: Mafmt; -export const WebSockets: Mafmt; -export const WebSocketsSecure: Mafmt; -export const WebSocketStar: Mafmt; -export const WebRTCStar: Mafmt; -export const WebRTCDirect: Mafmt; -export const Reliable: Mafmt; -export const Stardust: Mafmt; -export const Circuit: Mafmt; -export const P2P: Mafmt; -export const IPFS: Mafmt; diff --git a/src/index.js b/src/index.js index a759118..155e470 100644 --- a/src/index.js +++ b/src/index.js @@ -3,8 +3,9 @@ const { Multiaddr } = require('multiaddr') /** - * @typedef {(a: Multiaddr | string) => boolean} MatchesFunction - * @typedef {(a: string[]) => boolean | null | string[]} PartialMatchesFunction + * @typedef {import('./types').MatchesFunction} MatchesFunction + * @typedef {import('./types').PartialMatchesFunction} PartialMatchesFunction + * @typedef {import('./types').Mafmt} Mafmt */ /* @@ -123,27 +124,29 @@ const P2P = or( _P2P ) -exports.DNS = DNS -exports.DNS4 = DNS4 -exports.DNS6 = DNS6 -exports.DNSADDR = DNSADDR -exports.IP = IP -exports.TCP = TCP -exports.UDP = UDP -exports.QUIC = QUIC -exports.UTP = UTP -exports.HTTP = HTTP -exports.HTTPS = HTTPS -exports.WebSockets = WebSockets -exports.WebSocketsSecure = WebSocketsSecure -exports.WebSocketStar = WebSocketStar -exports.WebRTCStar = WebRTCStar -exports.WebRTCDirect = WebRTCDirect -exports.Reliable = Reliable -exports.Stardust = Stardust -exports.Circuit = Circuit -exports.P2P = P2P -exports.IPFS = P2P +module.exports = { + DNS, + DNS4, + DNS6, + DNSADDR, + IP, + TCP, + UDP, + QUIC, + UTP, + HTTP, + HTTPS, + WebSockets, + WebSocketsSecure, + WebSocketStar, + WebRTCStar, + WebRTCDirect, + Reliable, + Stardust, + Circuit, + P2P, + IPFS: P2P +} /* * Validation funcs @@ -161,11 +164,10 @@ function makeMatchesFunction (partialMatch) { try { a = new Multiaddr(a) } catch (err) { // catch error - return false // also if it's invalid it's probably not matching as well so return false + return false // also if it's invalid it's propably not matching as well so return false } } const out = partialMatch(a.protoNames()) - if (out === null) { return false } @@ -181,7 +183,8 @@ function makeMatchesFunction (partialMatch) { } /** - * @param {...any} args + * @param {Array Mafmt)>} args + * @returns {Mafmt} */ function and (...args) { /** @@ -191,18 +194,27 @@ function and (...args) { if (a.length < args.length) { return null } + + /** @type {boolean | string[] | null} */ + let out = a + args.some((arg) => { - a = typeof arg === 'function' + out = typeof arg === 'function' ? arg().partialMatch(a) : arg.partialMatch(a) - if (a === null) { + if (Array.isArray(out)) { + a = out + } + + if (out === null) { return true } + return false }) - return a + return out } return { @@ -213,9 +225,11 @@ function and (...args) { } } -function or () { - const args = Array.from(arguments) - +/** + * @param {Array Mafmt)>} args + * @returns {Mafmt} + */ +function or (...args) { /** * @type {PartialMatchesFunction} */ @@ -247,6 +261,7 @@ function or () { /** * @param {string} n + * @returns {Mafmt} */ function base (n) { const name = n diff --git a/src/types.d.ts b/src/types.d.ts new file mode 100644 index 0000000..89ac523 --- /dev/null +++ b/src/types.d.ts @@ -0,0 +1,10 @@ + +export type MatchesFunction = (a: string | Uint8Array | Multiaddr) => boolean +export type PartialMatchesFunction = (protos: string[]) => boolean | string[] | null + +export interface Mafmt { + toString: () => string + input?: (Mafmt | (() => Mafmt))[] + matches: MatchesFunction + partialMatch: PartialMatchesFunction +} diff --git a/test/index.spec.js b/test/index.spec.js index 86637ef..263a835 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -191,7 +191,7 @@ describe('multiaddr validation', function () { ].concat(goodCircuit) /** - * @param {mafmt.Mafmt} p + * @param {import('../').Mafmt} p * @param {...string[]} tests */ function assertMatches (p, ...tests) { @@ -211,7 +211,7 @@ describe('multiaddr validation', function () { } /** - * @param {mafmt.Mafmt} p + * @param {import('../').Mafmt} p * @param {...string[]} tests */ function assertMismatches (p, ...tests) {