Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(subdomain): support inlined DNSLink names #36

Merged
merged 1 commit into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ function isIpns (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch
if (isCID(ipnsId)) return true
// Check if it looks like FQDN
try {
if (!ipnsId.includes('.') && ipnsId.includes('-')) {
// name without tld, assuming its inlined into a single DNS label
// (https://github.com/ipfs/in-web-browsers/issues/169)
// en-wikipedia--on--ipfs-org → en.wikipedia-on-ipfs.org
ipnsId = ipnsId.replace(/--/g, '@').replace(/-/g, '.').replace(/@/g, '-')
}
// URL implementation in web browsers forces lowercase of the hostname
const { hostname } = new URL(`http://${ipnsId}`) // eslint-disable-line no-new
// Check if potential FQDN has an explicit TLD
Expand Down
18 changes: 15 additions & 3 deletions test/test-subdomain.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,18 @@ describe('ipfs subdomain', () => {
done()
})

it('isIPFS.ipnsSubdomain should not match if *.ipns is not a fqdn with tld', (done) => {
const actual = isIPFS.ipnsSubdomain('http://no-fqdn-with-tld.ipns.dweb.link')
it('isIPFS.ipnsSubdomain should not match if *.ipns is not a valid hostname', (done) => {
const actual = isIPFS.ipnsSubdomain('http://invalid-hostname-.ipns.dweb.link')
expect(actual).to.equal(false)
done()
})

it('isIPFS.ipnsSubdomain should match if *.ipns is FQDN inlined into a single DNS label', (done) => {
const actual = isIPFS.ipnsSubdomain('https://en-wikipedia--on--ipfs-org.ipns.dweb.link')
expect(actual).to.equal(true)
done()
})

it('isIPFS.subdomain should match an ipfs subdomain', (done) => {
const actual = isIPFS.subdomain('http://bafybeie5gq4jxvzmsym6hjlwxej4rwdoxt7wadqvmmwbqi7r27fclha2va.ipfs.dweb.link')
expect(actual).to.equal(true)
Expand Down Expand Up @@ -148,11 +154,17 @@ describe('ipfs subdomain', () => {
})

it('isIPFS.subdomain should not match if *.ipns is not libp2pkey nor fqdn', (done) => {
const actual = isIPFS.subdomain('http://not-a-cid-or-dnslink.ipns.dweb.link')
const actual = isIPFS.subdomain('http://not-a-cid-or-valid-hostname-.ipns.dweb.link')
expect(actual).to.equal(false)
done()
})

it('isIPFS.subdomain should match if *.ipns looks like an inlined DNSLink name', (done) => {
const actual = isIPFS.subdomain('http://en-wikipedia--on--ipfs-org.ipns.dweb.link')
expect(actual).to.equal(true)
done()
})

it('isIPFS.subdomain should not match a Uint8Array data type', (done) => {
const actual = isIPFS.subdomain(uint8ArrayFromString('QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR', 'base58btc'))
expect(actual).to.equal(false)
Expand Down