Skip to content

Commit 9d7ea4e

Browse files
committed
Liberal protocol handlers
- closes #49 - if protocol prefix is present in path, leave it as-is - if protocol prefix is missing, add value from handler template
1 parent d2f9043 commit 9d7ea4e

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

lib/protocols.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ CommonProtocolHandler.prototype = Object.freeze({
4747
protocolFlags: Ci.nsIProtocolHandler.URI_NOAUTH |
4848
Ci.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
4949

50+
normalizedIpfsPath: function (uriSpec) {
51+
let schemeExp = this.scheme.replace(/\+/, '\\+') // fix for web+fs etc
52+
let ipfsPath = uriSpec.replace(new RegExp('^' + schemeExp + '\\:\\/*'), '')
53+
// add protocol prefix if missing
54+
if (!(/^ip(?:f|n)s\//.test(ipfsPath))) {
55+
ipfsPath = this.pathPrefix + ipfsPath
56+
}
57+
return ipfsPath
58+
},
59+
5060
newURI: function (aSpec, aOriginCharset, aBaseURI) {
5161
// console.info('Detected newURI with IPFS protocol: ' + aSpec)
5262

@@ -65,9 +75,7 @@ CommonProtocolHandler.prototype = Object.freeze({
6575
return uri
6676
*/
6777

68-
let schemeExp = this.scheme.replace(/\+/, '\\+') // web+fs etc
69-
let ipfsPath = aSpec.replace(new RegExp('^' + schemeExp + '\\:\\/*'), '')
70-
let http = gw.publicUri.spec + this.pathPrefix + ipfsPath
78+
let http = gw.publicUri.spec + this.normalizedIpfsPath(aSpec)
7179
let uri = ioservice.newURI(http, aOriginCharset, null)
7280

7381
// console.info('newURI routed to HTTP gateway: ' + uri.spec)

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
"description": "Access IPFS resources via custom HTTP2IPFS gateway",
66
"author": "Marcin Rataj",
7-
"version": "1.4.1",
7+
"version": "1.4.2",
88
"license": "CC0-1.0",
99
"homepage": "https://github.com/lidel/ipfs-firefox-addon",
1010
"icon": "data/icon-on-64.png",

test/test-protocols.js

+8
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ exports['test newURI(web+ipfs://<path>)'] = function (assert) {
5252
var newURI = webIpfsHandler.newURI('web+ipfs://' + ipfsPath, 'utf8', null)
5353
assert.equal(newURI.spec, pubGwUri.spec + 'ipfs/' + ipfsPath, 'newURI spec')
5454
}
55+
exports['test newURI(ipfs:/ipfs/<path>)'] = function (assert) {
56+
var newURI = ipfsHandler.newURI('ipfs:/ipfs/' + ipfsPath, 'utf8', null)
57+
assert.equal(newURI.spec, pubGwUri.spec + 'ipfs/' + ipfsPath, 'newURI spec')
58+
}
5559

5660
exports['test newURI(ipns:<path>)'] = function (assert) {
5761
var newURI = ipnsHandler.newURI('ipns:' + ipnsPath, 'utf8', null)
@@ -69,6 +73,10 @@ exports['test newURI(web+ipns://<path>)'] = function (assert) {
6973
var newURI = webIpnsHandler.newURI('web+ipns://' + ipnsPath, 'utf8', null)
7074
assert.equal(newURI.spec, pubGwUri.spec + 'ipns/' + ipnsPath, 'newURI spec')
7175
}
76+
exports['test newURI(ipns:/ipns/<path>)'] = function (assert) {
77+
var newURI = ipnsHandler.newURI('ipns:/ipns/' + ipnsPath, 'utf8', null)
78+
assert.equal(newURI.spec, pubGwUri.spec + 'ipns/' + ipnsPath, 'newURI spec')
79+
}
7280

7381
// The fs:<resource> protocol for easier interoperability with legacy applications
7482
// It is a simple prefix to the canonical UNIX-like IPFS address

0 commit comments

Comments
 (0)