Skip to content

Commit 4a96bde

Browse files
achingbrainlidel
andauthored
feat: add types (#39)
* feat: add types Adds ts type generation and fixes all tsc errors. * chore: fix up travis * chore: update aegir command * chore: use correct browser name * chore: fix ts config path * chore: update linux version * chore: fix typo * chore: remove reundant npm scripts * chore: add types path to package.json * chore: update deps and remove ts-ignore * fix: restore browser bundle Co-authored-by: Marcin Rataj <[email protected]>
1 parent ec5868b commit 4a96bde

File tree

7 files changed

+100
-12
lines changed

7 files changed

+100
-12
lines changed

.travis.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
language: node_js
22
cache: npm
3+
dist: bionic
34
stages:
45
- check
56
- test
67
- cov
78

9+
branches:
10+
only:
11+
- master
12+
- /^release\/.*$/
13+
814
node_js:
915
- 'lts/*'
1016
- 'node'
@@ -21,7 +27,6 @@ jobs:
2127
include:
2228
- stage: check
2329
script:
24-
- npx aegir commitlint --travis
2530
- npx aegir dep-check
2631
- npm run lint
2732

@@ -35,7 +40,7 @@ jobs:
3540
name: firefox
3641
addons:
3742
firefox: latest
38-
script: npx aegir test -t browser -t webworker -- --browsers FirefoxHeadless
43+
script: npx aegir test -t browser -t webworker -- --browser firefox
3944

4045
- stage: test
4146
name: electron-main

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"src",
2121
"dist"
2222
],
23+
"types": "./dist/src/index.d.ts",
2324
"main": "src/index.js",
2425
"browser": {
2526
"fs": false
@@ -29,28 +30,27 @@
2930
"url": "https://github.com/ipfs/is-ipfs.git"
3031
},
3132
"scripts": {
33+
"prepare": "aegir build --no-bundle",
3234
"test:node": "aegir test --target node",
3335
"test:browser": "aegir test --target browser",
3436
"test": "aegir test",
37+
"prepublishOnly": "aegir build",
3538
"lint": "aegir lint && aegir lint-package-json",
3639
"release": "aegir release",
3740
"release-minor": "aegir release --type minor",
38-
"release-major": "aegir release --type major",
39-
"build": "aegir build",
40-
"coverage": "aegir coverage",
41-
"coverage-publish": "aegir coverage --upload"
41+
"release-major": "aegir release --type major"
4242
},
4343
"dependencies": {
4444
"cids": "^1.1.5",
45-
"iso-url": "^1.0.0",
45+
"iso-url": "^1.1.0",
4646
"mafmt": "^8.0.4",
4747
"multiaddr": "^8.1.2",
48-
"multibase": "^3.1.1",
49-
"multihashes": "^3.1.2",
48+
"multibase": "^4.0.1",
49+
"multihashes": "^4.0.0",
5050
"uint8arrays": "^2.0.5"
5151
},
5252
"devDependencies": {
53-
"aegir": "^30.3.0",
53+
"aegir": "^31.0.0",
5454
"pre-commit": "^1.2.2"
5555
},
5656
"engines": {

src/index.js

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ const subdomainProtocolMatch = 2
2121
// Fully qualified domain name (FQDN) that has an explicit .tld suffix
2222
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])$/
2323

24+
/**
25+
* @param {*} hash
26+
*/
2427
function isMultihash (hash) {
2528
const formatted = convertToString(hash)
2629
try {
@@ -31,6 +34,9 @@ function isMultihash (hash) {
3134
}
3235
}
3336

37+
/**
38+
* @param {*} hash
39+
*/
3440
function isMultibase (hash) {
3541
try {
3642
return multibase.isEncoded(hash)
@@ -39,6 +45,9 @@ function isMultibase (hash) {
3945
}
4046
}
4147

48+
/**
49+
* @param {*} hash
50+
*/
4251
function isCID (hash) {
4352
try {
4453
new CID(hash) // eslint-disable-line no-new
@@ -48,6 +57,9 @@ function isCID (hash) {
4857
}
4958
}
5059

60+
/**
61+
* @param {*} input
62+
*/
5163
function isMultiaddr (input) {
5264
if (!input) return false
5365
if (Multiaddr.isMultiaddr(input)) return true
@@ -59,10 +71,19 @@ function isMultiaddr (input) {
5971
}
6072
}
6173

74+
/**
75+
* @param {string | Uint8Array | Multiaddr} input
76+
*/
6277
function isPeerMultiaddr (input) {
6378
return isMultiaddr(input) && (mafmt.P2P.matches(input) || mafmt.DNS.matches(input))
6479
}
6580

81+
/**
82+
* @param {string | Uint8Array} input
83+
* @param {RegExp | string} pattern
84+
* @param {number} [protocolMatch=1]
85+
* @param {number} [hashMatch=2]
86+
*/
6687
function isIpfs (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch = defaultHashMath) {
6788
const formatted = convertToString(input)
6889
if (!formatted) {
@@ -83,14 +104,21 @@ function isIpfs (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch
83104
if (hash && pattern === subdomainGatewayPattern) {
84105
// when doing checks for subdomain context
85106
// ensure hash is case-insensitive
86-
// (browsers force-lowercase authority compotent anyway)
107+
// (browsers force-lowercase authority component anyway)
87108
hash = hash.toLowerCase()
88109
}
89110

90111
return isCID(hash)
91112
}
92113

93-
function isIpns (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch) {
114+
/**
115+
*
116+
* @param {string | Uint8Array} input
117+
* @param {string | RegExp} pattern
118+
* @param {number} [protocolMatch=1]
119+
* @param {number} [hashMatch=1]
120+
*/
121+
function isIpns (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch = defaultHashMath) {
94122
const formatted = convertToString(input)
95123
if (!formatted) {
96124
return false
@@ -133,10 +161,16 @@ function isIpns (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch
133161
return true
134162
}
135163

164+
/**
165+
* @param {any} input
166+
*/
136167
function isString (input) {
137168
return typeof input === 'string'
138169
}
139170

171+
/**
172+
* @param {Uint8Array | string} input
173+
*/
140174
function convertToString (input) {
141175
if (input instanceof Uint8Array) {
142176
return uint8ArrayToString(input, 'base58btc')
@@ -149,21 +183,45 @@ function convertToString (input) {
149183
return false
150184
}
151185

186+
/**
187+
* @param {string | Uint8Array} url
188+
*/
152189
const ipfsSubdomain = (url) => isIpfs(url, subdomainGatewayPattern, subdomainProtocolMatch, subdomainIdMatch)
190+
/**
191+
* @param {string | Uint8Array} url
192+
*/
153193
const ipnsSubdomain = (url) => isIpns(url, subdomainGatewayPattern, subdomainProtocolMatch, subdomainIdMatch)
194+
/**
195+
* @param {string | Uint8Array} url
196+
*/
154197
const subdomain = (url) => ipfsSubdomain(url) || ipnsSubdomain(url)
155198

199+
/**
200+
* @param {string | Uint8Array} url
201+
*/
156202
const ipfsUrl = (url) => isIpfs(url, pathGatewayPattern) || ipfsSubdomain(url)
203+
/**
204+
* @param {string | Uint8Array} url
205+
*/
157206
const ipnsUrl = (url) => isIpns(url, pathGatewayPattern) || ipnsSubdomain(url)
207+
/**
208+
* @param {string | Uint8Array} url
209+
*/
158210
const url = (url) => ipfsUrl(url) || ipnsUrl(url) || subdomain(url)
159211

212+
/**
213+
* @param {string | Uint8Array} path
214+
*/
160215
const path = (path) => isIpfs(path, pathPattern) || isIpns(path, pathPattern)
161216

162217
module.exports = {
163218
multihash: isMultihash,
164219
multiaddr: isMultiaddr,
165220
peerMultiaddr: isPeerMultiaddr,
166221
cid: isCID,
222+
/**
223+
* @param {CID | string | Uint8Array} cid
224+
*/
167225
base32cid: (cid) => (isMultibase(cid) === 'base32' && isCID(cid)),
168226
ipfsSubdomain,
169227
ipnsSubdomain,
@@ -173,10 +231,22 @@ module.exports = {
173231
ipnsUrl,
174232
url,
175233
pathGatewayPattern: pathGatewayPattern,
234+
/**
235+
* @param {string | Uint8Array} path
236+
*/
176237
ipfsPath: (path) => isIpfs(path, pathPattern),
238+
/**
239+
* @param {string | Uint8Array} path
240+
*/
177241
ipnsPath: (path) => isIpns(path, pathPattern),
178242
path,
179243
pathPattern,
244+
/**
245+
* @param {string | Uint8Array} x
246+
*/
180247
urlOrPath: (x) => url(x) || path(x),
248+
/**
249+
* @param {string | Uint8Array | CID} path
250+
*/
181251
cidPath: path => isString(path) && !isCID(path) && isIpfs(`/ipfs/${path}`, pathPattern)
182252
}

test/test-cid.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ describe('ipfs base32cid', () => {
101101
})
102102

103103
it('isIPFS.base32cid should not match an invalid CID data type', (done) => {
104+
// @ts-ignore data type is invalid
104105
const actual = isIPFS.base32cid(4)
105106
expect(actual).to.equal(false)
106107
done()

test/test-multiaddr.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ describe('ipfs peerMultiaddr', () => {
159159
})
160160

161161
it('isIPFS.peerMultiaddr should not match an invalid multiaddr data type', (done) => {
162+
// @ts-ignore data type is invalid
162163
const actual = isIPFS.peerMultiaddr(4)
163164
expect(actual).to.equal(false)
164165
done()

test/test-path.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ describe('ipfs path', () => {
148148
})
149149

150150
it('isIPFS.cidPath should not match a non string', () => {
151+
// @ts-ignore data type is invalid
151152
const actual = isIPFS.cidPath({ toString: () => 'QmYHNYAaYK5hm3ZhZFx5W9H6xydKDGimjdgJMrMSdnctEm/path/to/file' })
152153
expect(actual).to.equal(false)
153154
})

tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "./node_modules/aegir/src/config/tsconfig.aegir.json",
3+
"compilerOptions": {
4+
"outDir": "dist"
5+
},
6+
"include": [
7+
"src",
8+
"test"
9+
]
10+
}

0 commit comments

Comments
 (0)