Skip to content

Commit

Permalink
feat: add cidPath function
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Alan Shaw committed Jan 29, 2019
1 parent 8804b66 commit 7be55d3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ isIPFS.ipfsPath('/ipfs/invalid-hash') // false
isIPFS.ipnsPath('/ipfs/QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o') // false
isIPFS.ipnsPath('/ipns/github.com') // true

isIPFS.cidPath('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o/path/to/file') // true
isIPFS.cidPath('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o/') // true
isIPFS.cidPath('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o') // false
isIPFS.cidPath('/ipfs/QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o') // false
isIPFS.cidPath('/ipfs/QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o/file') // false

isIPFS.subdomain('http://bafybeie5gq4jxvzmsym6hjlwxej4rwdoxt7wadqvmmwbqi7r27fclha2va.ipfs.dweb.link') // true
isIPFS.subdomain('http://bafybeiabc2xofh6tdi6vutusorpumwcikw3hf3st4ecjugo6j52f6xwc6q.ipns.dweb.link') // true
isIPFS.subdomain('http://www.bafybeie5gq4jxvzmsym6hjlwxej4rwdoxt7wadqvmmwbqi7r27fclha2va.ipfs.dweb.link') // false
Expand Down
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,16 @@ function isIpns (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch
return true
}

function isString (input) {
return typeof input === 'string'
}

function convertToString (input) {
if (Buffer.isBuffer(input)) {
return base58.encode(input)
}

if (typeof input === 'string') {
if (isString(input)) {
return input
}

Expand All @@ -125,5 +129,6 @@ module.exports = {
ipnsPath: (path) => isIpns(path, pathPattern),
path: (path) => (isIpfs(path, pathPattern) || isIpns(path, pathPattern)),
pathPattern: pathPattern,
urlOrPath: (x) => (isIpfs(x, urlPattern) || isIpns(x, urlPattern) || isIpfs(x, pathPattern) || isIpns(x, pathPattern))
urlOrPath: (x) => (isIpfs(x, urlPattern) || isIpns(x, urlPattern) || isIpfs(x, pathPattern) || isIpns(x, pathPattern)),
cidPath: path => isString(path) && path.includes('/') && isCID(path.split('/')[0])
}
20 changes: 20 additions & 0 deletions test/test-path.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,24 @@ describe('ipfs path', () => {
expect(actual).to.equal(false)
done()
})

it('isIPFS.cidPath should not match a CID path', () => {
const actual = isIPFS.cidPath('QmYHNYAaYK5hm3ZhZFx5W9H6xydKDGimjdgJMrMSdnctEm/path/to/file')
expect(actual).to.equal(true)
})

it('isIPFS.cidPath should not match a CID path with trailing slash', () => {
const actual = isIPFS.cidPath('QmYHNYAaYK5hm3ZhZFx5W9H6xydKDGimjdgJMrMSdnctEm/')
expect(actual).to.equal(true)
})

it('isIPFS.cidPath should not match a CID', () => {
const actual = isIPFS.cidPath('QmYHNYAaYK5hm3ZhZFx5W9H6xydKDGimjdgJMrMSdnctEm')
expect(actual).to.equal(false)
})

it('isIPFS.cidPath should not match a non string', () => {
const actual = isIPFS.cidPath({ toString: () => 'QmYHNYAaYK5hm3ZhZFx5W9H6xydKDGimjdgJMrMSdnctEm/path/to/file' })
expect(actual).to.equal(false)
})
})

0 comments on commit 7be55d3

Please sign in to comment.