Skip to content

Commit d20caf5

Browse files
authoredApr 11, 2021
chore: update deps and types (#79)
* chore: update deps and types Updates all deps, pulls in tsconfig from aegir and fixes the new ts issues that identified. * chore: pr comments, tidy up generated types
1 parent 5c177cd commit d20caf5

File tree

9 files changed

+138
-121
lines changed

9 files changed

+138
-121
lines changed
 

‎.npmignore

-7
This file was deleted.

‎.travis.yml

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,50 @@
11
language: node_js
22
cache: npm
3+
dist: bionic
4+
5+
branches:
6+
only:
7+
- master
8+
- /^release\/.*$/
9+
310
stages:
411
- check
512
- test
613
- cov
714

815
node_js:
9-
- '12'
10-
- '10'
16+
- 'lts/*'
17+
- 'node'
1118

1219
os:
1320
- linux
1421
- osx
1522
- windows
1623

17-
script: npx nyc -s npm run test:node -- --bail
24+
script: npm run test:node -- --cov
1825
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov
1926

2027
jobs:
2128
include:
29+
- os: windows
30+
cache: false
31+
2232
- stage: check
2333
script:
24-
- npx aegir dep-check -- -i wrtc -i electron-webrtc
34+
- npx aegir dep-check
2535
- npm run lint
2636

2737
- stage: test
2838
name: chrome
2939
addons:
3040
chrome: stable
31-
script:
32-
- npx aegir test -t browser
41+
script: npx aegir test -t browser -t webworker
3342

3443
- stage: test
3544
name: firefox
3645
addons:
3746
firefox: latest
38-
script:
39-
- npx aegir test -t browser -- --browsers FirefoxHeadless
47+
script: npx aegir test -t browser -t webworker -- --browser firefox
4048

4149
notifications:
4250
email: false

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ Where `multiaddr` may be:
5959

6060
* a [Multiaddr](https://www.npmjs.com/package/multiaddr)
6161
* a String
62-
* a [Buffer](https://www.npmjs.com/package/buffer)
62+
* a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)
6363

6464
Returns `true`/`false`

‎package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "A multiaddr validator",
55
"leadMaintainer": "Vasco Santos <vasco.santos@moxy.studio>",
66
"main": "src/index.js",
7+
"types": "dist/src/index.d.ts",
78
"scripts": {
89
"lint": "aegir lint",
910
"build": "aegir build",
@@ -30,13 +31,18 @@
3031
"devDependencies": {
3132
"@types/chai": "^4.2.8",
3233
"@types/mocha": "^8.0.0",
33-
"aegir": "^32.0.2",
34+
"aegir": "^33.0.0",
3435
"uint8arrays": "^2.0.5",
3536
"util": "^0.12.3"
3637
},
3738
"dependencies": {
38-
"multiaddr": "^8.0.0"
39+
"multiaddr": "^9.0.1"
3940
},
41+
"files": [
42+
"src",
43+
"dist",
44+
"!dist/*.tsbuildinfo"
45+
],
4046
"contributors": [
4147
"David Dias <daviddias.p@gmail.com>",
4248
"Vasco Santos <vasco.santos@moxy.studio>",

‎src/index.d.ts

-29
This file was deleted.

‎src/index.js

+85-34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
'use strict'
22

3-
const multiaddr = require('multiaddr')
3+
const { Multiaddr } = require('multiaddr')
4+
5+
/**
6+
* @typedef {import('./types').MatchesFunction} MatchesFunction
7+
* @typedef {import('./types').PartialMatchesFunction} PartialMatchesFunction
8+
* @typedef {import('./types').Mafmt} Mafmt
9+
*/
410

511
/*
612
* Valid combinations
@@ -118,37 +124,45 @@ const P2P = or(
118124
_P2P
119125
)
120126

121-
exports.DNS = DNS
122-
exports.DNS4 = DNS4
123-
exports.DNS6 = DNS6
124-
exports.DNSADDR = DNSADDR
125-
exports.IP = IP
126-
exports.TCP = TCP
127-
exports.UDP = UDP
128-
exports.QUIC = QUIC
129-
exports.UTP = UTP
130-
exports.HTTP = HTTP
131-
exports.HTTPS = HTTPS
132-
exports.WebSockets = WebSockets
133-
exports.WebSocketsSecure = WebSocketsSecure
134-
exports.WebSocketStar = WebSocketStar
135-
exports.WebRTCStar = WebRTCStar
136-
exports.WebRTCDirect = WebRTCDirect
137-
exports.Reliable = Reliable
138-
exports.Stardust = Stardust
139-
exports.Circuit = Circuit
140-
exports.P2P = P2P
141-
exports.IPFS = P2P
127+
module.exports = {
128+
DNS,
129+
DNS4,
130+
DNS6,
131+
DNSADDR,
132+
IP,
133+
TCP,
134+
UDP,
135+
QUIC,
136+
UTP,
137+
HTTP,
138+
HTTPS,
139+
WebSockets,
140+
WebSocketsSecure,
141+
WebSocketStar,
142+
WebRTCStar,
143+
WebRTCDirect,
144+
Reliable,
145+
Stardust,
146+
Circuit,
147+
P2P,
148+
IPFS: P2P
149+
}
142150

143151
/*
144152
* Validation funcs
145153
*/
146154

155+
/**
156+
* @param {PartialMatchesFunction} partialMatch
157+
*/
147158
function makeMatchesFunction (partialMatch) {
148-
return function matches (a) {
149-
if (!multiaddr.isMultiaddr(a)) {
159+
/**
160+
* @type {MatchesFunction}
161+
*/
162+
function matches (a) {
163+
if (!Multiaddr.isMultiaddr(a)) {
150164
try {
151-
a = multiaddr(a)
165+
a = new Multiaddr(a)
152166
} catch (err) { // catch error
153167
return false // also if it's invalid it's propably not matching as well so return false
154168
}
@@ -157,28 +171,50 @@ function makeMatchesFunction (partialMatch) {
157171
if (out === null) {
158172
return false
159173
}
174+
175+
if (out === true || out === false) {
176+
return out
177+
}
178+
160179
return out.length === 0
161180
}
181+
182+
return matches
162183
}
163184

164-
function and () {
165-
const args = Array.from(arguments)
185+
/**
186+
* @param {Array<Mafmt | (() => Mafmt)>} args
187+
* @returns {Mafmt}
188+
*/
189+
function and (...args) {
190+
/**
191+
* @type {PartialMatchesFunction}
192+
*/
166193
function partialMatch (a) {
167194
if (a.length < args.length) {
168195
return null
169196
}
197+
198+
/** @type {boolean | string[] | null} */
199+
let out = a
200+
170201
args.some((arg) => {
171-
a = typeof arg === 'function'
202+
out = typeof arg === 'function'
172203
? arg().partialMatch(a)
173204
: arg.partialMatch(a)
174205

175-
if (a === null) {
206+
if (Array.isArray(out)) {
207+
a = out
208+
}
209+
210+
if (out === null) {
176211
return true
177212
}
213+
178214
return false
179215
})
180216

181-
return a
217+
return out
182218
}
183219

184220
return {
@@ -189,9 +225,14 @@ function and () {
189225
}
190226
}
191227

192-
function or () {
193-
const args = Array.from(arguments)
194-
228+
/**
229+
* @param {Array<Mafmt | (() => Mafmt)>} args
230+
* @returns {Mafmt}
231+
*/
232+
function or (...args) {
233+
/**
234+
* @type {PartialMatchesFunction}
235+
*/
195236
function partialMatch (a) {
196237
let out = null
197238
args.some((arg) => {
@@ -218,13 +259,20 @@ function or () {
218259
return result
219260
}
220261

262+
/**
263+
* @param {string} n
264+
* @returns {Mafmt}
265+
*/
221266
function base (n) {
222267
const name = n
223268

269+
/**
270+
* @type {MatchesFunction}
271+
*/
224272
function matches (a) {
225273
if (typeof a === 'string') {
226274
try {
227-
a = multiaddr(a)
275+
a = new Multiaddr(a)
228276
} catch (err) { // catch error
229277
return false // also if it's invalid it's propably not matching as well so return false
230278
}
@@ -237,6 +285,9 @@ function base (n) {
237285
return false
238286
}
239287

288+
/**
289+
* @type {PartialMatchesFunction}
290+
*/
240291
function partialMatch (protos) {
241292
if (protos.length === 0) {
242293
return null

‎src/types.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
export type MatchesFunction = (a: string | Uint8Array | Multiaddr) => boolean
3+
export type PartialMatchesFunction = (protos: string[]) => boolean | string[] | null
4+
5+
export interface Mafmt {
6+
toString: () => string
7+
input?: (Mafmt | (() => Mafmt))[]
8+
matches: MatchesFunction
9+
partialMatch: PartialMatchesFunction
10+
}

‎test/index.spec.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
'use strict'
44

55
const { expect } = require('aegir/utils/chai')
6-
const multiaddr = require('multiaddr')
6+
const { Multiaddr } = require('multiaddr')
77
const uint8ArrayFromString = require('uint8arrays/from-string')
88

99
const mafmt = require('./../src')
@@ -190,13 +190,16 @@ describe('multiaddr validation', function () {
190190
'/dns6/nyc-2.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64'
191191
].concat(goodCircuit)
192192

193-
function assertMatches (p) {
194-
const tests = Array.from(arguments).slice(1)
193+
/**
194+
* @param {import('../').Mafmt} p
195+
* @param {...string[]} tests
196+
*/
197+
function assertMatches (p, ...tests) {
195198
tests.forEach(function (test) {
196199
test.forEach(function (testcase) {
197200
try {
198201
expect(p.matches(testcase), `assertMatches: ${testcase} (string)`).to.be.eql(true)
199-
const ma = multiaddr(testcase)
202+
const ma = new Multiaddr(testcase)
200203
expect(p.matches(ma), `assertMatches: ${testcase} (multiaddr object)`).to.be.eql(true)
201204
expect(p.matches(ma.bytes), `assertMatches: ${testcase} (multiaddr.bytes)`).to.be.eql(true)
202205
} catch (err) {
@@ -207,8 +210,11 @@ describe('multiaddr validation', function () {
207210
})
208211
}
209212

210-
function assertMismatches (p) {
211-
const tests = Array.from(arguments).slice(1)
213+
/**
214+
* @param {import('../').Mafmt} p
215+
* @param {...string[]} tests
216+
*/
217+
function assertMismatches (p, ...tests) {
212218
tests.forEach(function (test) {
213219
test.forEach(function (testcase) {
214220
try {
@@ -217,7 +223,7 @@ describe('multiaddr validation', function () {
217223
try {
218224
// if testcase string happens to be a valid multiaddr,
219225
// we expect 'p' test to also return false for Multiaddr object and Uint8Array versions
220-
validMultiaddrObj = multiaddr(testcase)
226+
validMultiaddrObj = new Multiaddr(testcase)
221227
} catch (e) {
222228
// Ignoring testcase as the string is not a multiaddr
223229
// (There is a separate 'Uint8Array is invalid' test later below)

0 commit comments

Comments
 (0)
Please sign in to comment.