Skip to content

Commit

Permalink
fix: support ipns:// urls
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Feb 6, 2024
1 parent fb890b5 commit e3c7f1f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const subdomainProtocolMatch = 2
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])$/

// URI IANA-scheme
const uriSchemePattern = /^(ipfs):\/\/([^/?#]+)/
const uriSchemePattern = /^(ip[fn]s):\/\/([^/?#]+)/

function isMultihash (hash: Uint8Array | string): boolean {
const formatted = convertToString(hash)
Expand Down Expand Up @@ -329,7 +329,7 @@ export const ipfsUrl = (url: string | Uint8Array): boolean => isIpfs(url, pathGa
* Returns `true` if the provided string is a valid IPNS url or `false`
* otherwise.
*/
export const ipnsUrl = (url: string | Uint8Array): boolean => isIpns(url, pathGatewayPattern) || ipnsSubdomain(url)
export const ipnsUrl = (url: string | Uint8Array): boolean => isIpns(url, pathGatewayPattern) || ipnsSubdomain(url) || isIpns(url, uriSchemePattern)

/**
* Returns `true` if the provided string is a valid IPFS or IPNS url or `false`
Expand Down
9 changes: 7 additions & 2 deletions test/test-path.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@ describe('ipfs path', () => {
})

it('isIPFS.cidPath should not match an IPFS path', () => {
const actual = isIPFS.cidPath('/ipfs/QmYHNYAaYK5hm3ZhZFx5W9H6xydKDGimjdgJMrMSdnctEm')
expect(actual).to.equal(false)
expect(isIPFS.cidPath('/ipfs/QmYHNYAaYK5hm3ZhZFx5W9H6xydKDGimjdgJMrMSdnctEm')).to.be.false()
})
})

describe('ipns path', () => {
it('isIPFS.urlOrPath should match an IANA-schema compliant ipns url', () => {
expect(isIPFS.urlOrPath('ipns://QmYHNYAaYK5hm3ZhZFx5W9H6xydKDGimjdgJMrMSdnctEm')).to.be.true()
})
})
14 changes: 14 additions & 0 deletions test/test-url.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,17 @@ describe('ipfs url', () => {
done()
})
})

describe('ipns url', () => {
it('isIPFS.ipnsUrl should match an IANA-schema compliant ipns uri', () => {
expect(isIPFS.ipnsUrl('ipns://QmYHNYAaYK5hm3ZhZFx5W9H6xydKDGimjdgJMrMSdnctEm')).to.be.true()
})

it('isIPFS.ipnsUrl should not match an IANA-schema compliant ipfs uri', () => {
expect(isIPFS.ipnsUrl('ipfs://QmYHNYAaYK5hm3ZhZFx5W9H6xydKDGimjdgJMrMSdnctEm')).to.be.false()
})

it('isIPFS.url should match an IANA-schema compliant ipns uri', () => {
expect(isIPFS.url('ipns://QmYHNYAaYK5hm3ZhZFx5W9H6xydKDGimjdgJMrMSdnctEm')).to.be.true()
})
})

0 comments on commit e3c7f1f

Please sign in to comment.