Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update deps and types #79

Merged
merged 2 commits into from
Apr 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .npmignore

This file was deleted.

24 changes: 16 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "A multiaddr validator",
"leadMaintainer": "Vasco Santos <[email protected]>",
"main": "src/index.js",
"types": "dist/src/index.d.ts",
"scripts": {
"lint": "aegir lint",
"build": "aegir build",
Expand All @@ -30,13 +31,18 @@
"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"
},
"files": [
"src",
"dist",
"!dist/*.tsbuildinfo"
],
"contributors": [
"David Dias <[email protected]>",
"Vasco Santos <[email protected]>",
Expand Down
29 changes: 0 additions & 29 deletions src/index.d.ts

This file was deleted.

119 changes: 85 additions & 34 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
'use strict'

const multiaddr = require('multiaddr')
const { Multiaddr } = require('multiaddr')

/**
* @typedef {import('./types').MatchesFunction} MatchesFunction
* @typedef {import('./types').PartialMatchesFunction} PartialMatchesFunction
* @typedef {import('./types').Mafmt} Mafmt
*/

/*
* Valid combinations
Expand Down Expand Up @@ -118,37 +124,45 @@ 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
achingbrain marked this conversation as resolved.
Show resolved Hide resolved
*/

/**
* @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
}
Expand All @@ -157,28 +171,50 @@ function makeMatchesFunction (partialMatch) {
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 {Array<Mafmt | (() => Mafmt)>} args
* @returns {Mafmt}
*/
function and (...args) {
/**
* @type {PartialMatchesFunction}
*/
function partialMatch (a) {
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 {
Expand All @@ -189,9 +225,14 @@ function and () {
}
}

function or () {
const args = Array.from(arguments)

/**
* @param {Array<Mafmt | (() => Mafmt)>} args
* @returns {Mafmt}
*/
function or (...args) {
/**
* @type {PartialMatchesFunction}
*/
function partialMatch (a) {
let out = null
args.some((arg) => {
Expand All @@ -218,13 +259,20 @@ function or () {
return result
}

/**
* @param {string} n
* @returns {Mafmt}
*/
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
}
Expand All @@ -237,6 +285,9 @@ function base (n) {
return false
}

/**
* @type {PartialMatchesFunction}
*/
function partialMatch (protos) {
if (protos.length === 0) {
return null
Expand Down
10 changes: 10 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 13 additions & 7 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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 {import('../').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) {
Expand All @@ -207,8 +210,11 @@ describe('multiaddr validation', function () {
})
}

function assertMismatches (p) {
const tests = Array.from(arguments).slice(1)
/**
* @param {import('../').Mafmt} p
* @param {...string[]} tests
*/
function assertMismatches (p, ...tests) {
tests.forEach(function (test) {
test.forEach(function (testcase) {
try {
Expand All @@ -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)
Expand Down
Loading