Skip to content

Commit 9424acc

Browse files
authoredJul 23, 2018
Merge pull request #537 from ipfs-shipyard/feat/subdomain-context
feat: support page actions for CID subdomains
2 parents 0b5635a + a0c0ca8 commit 9424acc

File tree

7 files changed

+72
-18
lines changed

7 files changed

+72
-18
lines changed
 

‎add-on/src/lib/ipfs-companion.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ module.exports = async function init () {
342342
async function updatePageActionIndicator (tabId, url) {
343343
// Chrome does not permit for both pageAction and browserAction to be enabled at the same time
344344
// https://github.com/ipfs-shipyard/ipfs-companion/issues/398
345-
if (runtime.isFirefox && ipfsPathValidator.validIpfsOrIpnsUrl(url)) {
345+
if (runtime.isFirefox && ipfsPathValidator.isIpfsPageActionsContext(url)) {
346346
if (url.startsWith(state.gwURLString) || url.startsWith(state.apiURLString)) {
347347
await browser.pageAction.setIcon({ tabId: tabId, path: '/icons/ipfs-logo-on.svg' })
348348
await browser.pageAction.setTitle({ tabId: tabId, title: browser.i18n.getMessage('pageAction_titleIpfsAtCustomGateway') })

‎add-on/src/lib/ipfs-path.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@
44
const IsIpfs = require('is-ipfs')
55

66
function safeIpfsPath (urlOrPath) {
7+
if (IsIpfs.subdomain(urlOrPath)) {
8+
urlOrPath = subdomainToIpfsPath(urlOrPath)
9+
}
710
// better safe than sorry: https://github.com/ipfs/ipfs-companion/issues/303
811
return decodeURIComponent(urlOrPath.replace(/^.*(\/ip(f|n)s\/.+)$/, '$1'))
912
}
1013

14+
function subdomainToIpfsPath (urlString) {
15+
const url = new URL(urlString)
16+
const fqdn = url.hostname.split('.')
17+
const cid = fqdn[0]
18+
const protocol = fqdn[1]
19+
return `/${protocol}/${cid}${url.pathname}`
20+
}
21+
1122
exports.safeIpfsPath = safeIpfsPath
1223

1324
function urlAtPublicGw (path, pubGwUrl) {
@@ -41,9 +52,8 @@ function createIpfsPathValidator (getState, dnsLink) {
4152
},
4253

4354
// Test if actions such as 'copy URL', 'pin/unpin' should be enabled for the URL
44-
// (we explicitly disable IPNS for now as there is no support for pins)
4555
isIpfsPageActionsContext (url) {
46-
return IsIpfs.url(url) && !url.startsWith(getState().apiURLString)
56+
return (IsIpfs.url(url) && !url.startsWith(getState().apiURLString)) || IsIpfs.subdomain(url)
4757
}
4858
}
4959

‎add-on/src/popup/browser-action/store.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ module.exports = (state, emitter) => {
7171

7272
try {
7373
const ipfs = await getIpfsApi()
74-
const currentPath = await resolveToIPFS(ipfs, new URL(state.currentTab.url).pathname)
74+
const currentPath = await resolveToIPFS(ipfs, state.currentTab.url)
7575
const pinResult = await ipfs.pin.add(currentPath, { recursive: true })
7676
console.log('ipfs.pin.add result', pinResult)
7777
state.isPinned = true
@@ -90,7 +90,7 @@ module.exports = (state, emitter) => {
9090

9191
try {
9292
const ipfs = await getIpfsApi()
93-
const currentPath = await resolveToIPFS(ipfs, new URL(state.currentTab.url).pathname)
93+
const currentPath = await resolveToIPFS(ipfs, state.currentTab.url)
9494
const result = await ipfs.pin.rm(currentPath, {recursive: true})
9595
state.isPinned = false
9696
console.log('ipfs.pin.rm result', result)
@@ -244,7 +244,7 @@ module.exports = (state, emitter) => {
244244
// skip update if there is an ongoing pin or unpin
245245
if (state.isPinning || state.isUnPinning) return
246246
try {
247-
const currentPath = await resolveToIPFS(ipfs, new URL(status.currentTab.url).pathname)
247+
const currentPath = await resolveToIPFS(ipfs, status.currentTab.url)
248248
const response = await ipfs.pin.ls(currentPath, {quiet: true})
249249
console.log(`positive ipfs.pin.ls for ${currentPath}: ${JSON.stringify(response)}`)
250250
state.isPinned = true
@@ -273,8 +273,8 @@ async function getIpfsApi () {
273273
return (bg && bg.ipfsCompanion) ? bg.ipfsCompanion.ipfs : null
274274
}
275275

276-
async function resolveToIPFS (ipfs, path) {
277-
path = safeIpfsPath(path) // https://github.com/ipfs/ipfs-companion/issues/303
276+
async function resolveToIPFS (ipfs, urlOrPath) {
277+
let path = safeIpfsPath(urlOrPath) // https://github.com/ipfs/ipfs-companion/issues/303
278278
if (/^\/ipns/.test(path)) {
279279
const response = await ipfs.name.resolve(path, {recursive: true, nocache: false})
280280
return response.Path

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
"ipfs-css": "0.5.2",
9494
"ipfs-http-response": "0.1.2",
9595
"ipfs-postmsg-proxy": "3.0.0",
96-
"is-ipfs": "0.3.2",
96+
"is-ipfs": "0.4.2",
9797
"is-svg": "3.0.0",
9898
"lru_map": "0.3.3",
9999
"mime-types": "2.1.18",

‎test/functional/lib/ipfs-path.test.js

+20
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,24 @@ describe('ipfs-path.js', function () {
9999
})
100100
})
101101
})
102+
describe('isIpfsPageActionsContext', function () {
103+
it('should return true for URL at IPFS Gateway', function () {
104+
const url = 'https://ipfs.io/ipfs/QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR?argTest#hashTest'
105+
expect(ipfsPathValidator.isIpfsPageActionsContext(url)).to.equal(true)
106+
})
107+
it('should return true for URL at IPFS Gateway with Base32 CIDv1 in subdomain', function () {
108+
// context-actions are shown on publick gateways that use CID in subdomain as well
109+
const url = 'http://bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy.ipfs.dweb.link/'
110+
expect(ipfsPathValidator.isIpfsPageActionsContext(url)).to.equal(true)
111+
})
112+
it('should return false for URL at IPFS Gateway with Base58 CIDv0 in subdomain', function () {
113+
// context-actions are shown on publick gateways that use CID in subdomain as well
114+
const url = 'http://QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR.ipfs.dweb.link/'
115+
expect(ipfsPathValidator.isIpfsPageActionsContext(url)).to.equal(false)
116+
})
117+
it('should return false for non-IPFS URL', function () {
118+
const url = 'https://ipfs.io/ipfs/NotACid?argTest#hashTest'
119+
expect(ipfsPathValidator.isIpfsPageActionsContext(url)).to.equal(false)
120+
})
121+
})
102122
})

‎test/functional/lib/ipfs-request.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,21 @@ describe('modifyRequest.onBeforeRequest', function () {
433433
})
434434
})
435435

436+
describe('request to FQDN with valid CID in subdomain', function () {
437+
// we do not touch such requests for now, as HTTP-based local node usually can't provide the same origin-based guarantees
438+
// we will redirect subdomains to ipfs:// when native handler is available
439+
it('should be left untouched for IPFS', function () {
440+
state.redirect = true
441+
const request = url2request('http://bafybeigxjv2o4jse2lajbd5c7xxl5rluhyqg5yupln42252e5tcao7hbge.ipfs.dweb.link/')
442+
expect(modifyRequest.onBeforeRequest(request)).to.equal(undefined)
443+
})
444+
it('should be left untouched for IPNS', function () {
445+
state.redirect = true
446+
const request = url2request('http://bafybeigxjv2o4jse2lajbd5c7xxl5rluhyqg5yupln42252e5tcao7hbge.ipns.dweb.link/')
447+
expect(modifyRequest.onBeforeRequest(request)).to.equal(undefined)
448+
})
449+
})
450+
436451
describe('request to FQDN with dnslink experiment enabled', function () {
437452
let activeGateway
438453
beforeEach(function () {

‎yarn.lock

+18-9
Original file line numberDiff line numberDiff line change
@@ -1602,16 +1602,16 @@ browserslist@^3.2.6:
16021602
caniuse-lite "^1.0.30000844"
16031603
electron-to-chromium "^1.3.47"
16041604

1605-
bs58@=2.0.0:
1606-
version "2.0.0"
1607-
resolved "https://registry.yarnpkg.com/bs58/-/bs58-2.0.0.tgz#72b713bed223a0ac518bbda0e3ce3f4817f39eb5"
1608-
1609-
bs58@^4.0.0, bs58@^4.0.1:
1605+
bs58@4.0.1, bs58@^4.0.0, bs58@^4.0.1:
16101606
version "4.0.1"
16111607
resolved "https://registry.yarnpkg.com/bs58/-/bs58-4.0.1.tgz#be161e76c354f6f788ae4071f63f34e8c4f0a42a"
16121608
dependencies:
16131609
base-x "^3.0.2"
16141610

1611+
bs58@=2.0.0:
1612+
version "2.0.0"
1613+
resolved "https://registry.yarnpkg.com/bs58/-/bs58-2.0.0.tgz#72b713bed223a0ac518bbda0e3ce3f4817f39eb5"
1614+
16151615
bs58check@<3.0.0, bs58check@^2.0.0:
16161616
version "2.1.1"
16171617
resolved "https://registry.yarnpkg.com/bs58check/-/bs58check-2.1.1.tgz#8a5d0e587af97b784bf9cbf1b29f454d82bc0222"
@@ -1994,7 +1994,7 @@ ci-info@^1.0.0:
19941994
version "1.1.3"
19951995
resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-1.1.3.tgz#710193264bb05c77b8c90d02f5aaf22216a667b2"
19961996

1997-
cids@^0.5.2, cids@^0.5.3, cids@~0.5.1, cids@~0.5.2, cids@~0.5.3:
1997+
cids@0.5.3, cids@^0.5.2, cids@^0.5.3, cids@~0.5.1, cids@~0.5.2, cids@~0.5.3:
19981998
version "0.5.3"
19991999
resolved "https://registry.yarnpkg.com/cids/-/cids-0.5.3.tgz#9a25b697eb76faf807afcec35c4ab936edfbd0a4"
20002000
dependencies:
@@ -5329,7 +5329,16 @@ is-installed-globally@^0.1.0:
53295329
global-dirs "^0.1.0"
53305330
is-path-inside "^1.0.0"
53315331

5332-
is-ipfs@0.3.2, is-ipfs@~0.3.2:
5332+
is-ipfs@0.4.2:
5333+
version "0.4.2"
5334+
resolved "https://registry.yarnpkg.com/is-ipfs/-/is-ipfs-0.4.2.tgz#06a858769cbfdac39a3e0ed1ca157e3f3fcc84fc"
5335+
dependencies:
5336+
bs58 "4.0.1"
5337+
cids "0.5.3"
5338+
multibase "0.4.0"
5339+
multihashes "0.4.13"
5340+
5341+
is-ipfs@~0.3.2:
53335342
version "0.3.2"
53345343
resolved "https://registry.yarnpkg.com/is-ipfs/-/is-ipfs-0.3.2.tgz#c4650b838e36fd0151de5896b2ff319fe8936182"
53355344
dependencies:
@@ -7016,7 +7025,7 @@ multiaddr@^5.0.0:
70167025
varint "^5.0.0"
70177026
xtend "^4.0.1"
70187027

7019-
multibase@~0.4.0:
7028+
multibase@0.4.0, multibase@~0.4.0:
70207029
version "0.4.0"
70217030
resolved "https://registry.yarnpkg.com/multibase/-/multibase-0.4.0.tgz#1bdb62c82de0114f822a1d8751bcbee91cd2efba"
70227031
dependencies:
@@ -7041,7 +7050,7 @@ multicodec@~0.2.7:
70417050
dependencies:
70427051
varint "^5.0.0"
70437052

7044-
multihashes@^0.4.13, multihashes@~0.4.12, multihashes@~0.4.13, multihashes@~0.4.9:
7053+
multihashes@0.4.13, multihashes@^0.4.13, multihashes@~0.4.12, multihashes@~0.4.13, multihashes@~0.4.9:
70457054
version "0.4.13"
70467055
resolved "https://registry.yarnpkg.com/multihashes/-/multihashes-0.4.13.tgz#d10bd71bd51d24aa894e2a6f1457146bb7bac125"
70477056
dependencies:

0 commit comments

Comments
 (0)
Please sign in to comment.