Skip to content

Commit 7be55d3

Browse files
author
Alan Shaw
committed
feat: add cidPath function
In the IPFS API (e.g. [`ipfs.cat`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#cat), [`ipfs.dag.get`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DAG.md#dagget)) we allow passing of paths like `QmYHNYAaYK5hm3ZhZFx5W9H6xydKDGimjdgJMrMSdnctEm/path/to/file`, and infer a `/ipfs` prefix. This PR adds a method to validate whether the passed input is a path like this. License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent 8804b66 commit 7be55d3

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ isIPFS.ipfsPath('/ipfs/invalid-hash') // false
7878
isIPFS.ipnsPath('/ipfs/QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o') // false
7979
isIPFS.ipnsPath('/ipns/github.com') // true
8080

81+
isIPFS.cidPath('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o/path/to/file') // true
82+
isIPFS.cidPath('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o/') // true
83+
isIPFS.cidPath('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o') // false
84+
isIPFS.cidPath('/ipfs/QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o') // false
85+
isIPFS.cidPath('/ipfs/QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o/file') // false
86+
8187
isIPFS.subdomain('http://bafybeie5gq4jxvzmsym6hjlwxej4rwdoxt7wadqvmmwbqi7r27fclha2va.ipfs.dweb.link') // true
8288
isIPFS.subdomain('http://bafybeiabc2xofh6tdi6vutusorpumwcikw3hf3st4ecjugo6j52f6xwc6q.ipns.dweb.link') // true
8389
isIPFS.subdomain('http://www.bafybeie5gq4jxvzmsym6hjlwxej4rwdoxt7wadqvmmwbqi7r27fclha2va.ipfs.dweb.link') // false

src/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,16 @@ function isIpns (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch
9494
return true
9595
}
9696

97+
function isString (input) {
98+
return typeof input === 'string'
99+
}
100+
97101
function convertToString (input) {
98102
if (Buffer.isBuffer(input)) {
99103
return base58.encode(input)
100104
}
101105

102-
if (typeof input === 'string') {
106+
if (isString(input)) {
103107
return input
104108
}
105109

@@ -125,5 +129,6 @@ module.exports = {
125129
ipnsPath: (path) => isIpns(path, pathPattern),
126130
path: (path) => (isIpfs(path, pathPattern) || isIpns(path, pathPattern)),
127131
pathPattern: pathPattern,
128-
urlOrPath: (x) => (isIpfs(x, urlPattern) || isIpns(x, urlPattern) || isIpfs(x, pathPattern) || isIpns(x, pathPattern))
132+
urlOrPath: (x) => (isIpfs(x, urlPattern) || isIpns(x, urlPattern) || isIpfs(x, pathPattern) || isIpns(x, pathPattern)),
133+
cidPath: path => isString(path) && path.includes('/') && isCID(path.split('/')[0])
129134
}

test/test-path.spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,24 @@ describe('ipfs path', () => {
131131
expect(actual).to.equal(false)
132132
done()
133133
})
134+
135+
it('isIPFS.cidPath should not match a CID path', () => {
136+
const actual = isIPFS.cidPath('QmYHNYAaYK5hm3ZhZFx5W9H6xydKDGimjdgJMrMSdnctEm/path/to/file')
137+
expect(actual).to.equal(true)
138+
})
139+
140+
it('isIPFS.cidPath should not match a CID path with trailing slash', () => {
141+
const actual = isIPFS.cidPath('QmYHNYAaYK5hm3ZhZFx5W9H6xydKDGimjdgJMrMSdnctEm/')
142+
expect(actual).to.equal(true)
143+
})
144+
145+
it('isIPFS.cidPath should not match a CID', () => {
146+
const actual = isIPFS.cidPath('QmYHNYAaYK5hm3ZhZFx5W9H6xydKDGimjdgJMrMSdnctEm')
147+
expect(actual).to.equal(false)
148+
})
149+
150+
it('isIPFS.cidPath should not match a non string', () => {
151+
const actual = isIPFS.cidPath({ toString: () => 'QmYHNYAaYK5hm3ZhZFx5W9H6xydKDGimjdgJMrMSdnctEm/path/to/file' })
152+
expect(actual).to.equal(false)
153+
})
134154
})

0 commit comments

Comments
 (0)