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

Browserify src #311

Merged
merged 15 commits into from
Nov 20, 2017
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ build
npm-debug.log
crowdin.yml
.*~
add-on/dist
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ before_script:
install:
- npm install --no-optional
script:
- npm run clean
- npm run build
- npm test
- npm run clean
- npm run lint
6 changes: 3 additions & 3 deletions add-on/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
],

"background": {
"page": "src/background/background.html"
"page": "dist/background/background.html"
},

"browser_action": {
Expand All @@ -46,12 +46,12 @@
"128": "icons/png/ipfs-logo-off_128.png"
},
"default_title": "__MSG_browserAction_title__",
"default_popup": "src/popup/browser-action.html"
"default_popup": "dist/popup/browser-action.html"
},

"options_ui": {
"browser_style": false,
"page": "src/options/options.html"
"page": "dist/options/options.html"
},

"web_accessible_resources": [
Expand Down
7 changes: 1 addition & 6 deletions add-on/src/background/background.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
<!DOCTYPE html>
<meta charset="utf-8">
<script src="../lib/npm/is-ipfs.min.js"></script>
<script src="../lib/npm/ipfs-api.min.js"></script>
<script src="../lib/npm/lru.js"></script>
<script src="../lib/npm/browser-polyfill.min.js"></script>
<script src="../lib/option-defaults.js"></script>
<script src="../lib/ipfs-companion.js"></script>
<script src="../ipfs-companion-common.js"></script>
<script src="background.js"></script>
3 changes: 2 additions & 1 deletion add-on/src/background/background.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'
/* eslint-env browser, webextensions */
/* global init */

const init = require('../lib/ipfs-companion')

// init add-on after all libs are loaded
document.addEventListener('DOMContentLoaded', init)
27 changes: 15 additions & 12 deletions add-on/src/lib/data-i18n.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
'use strict'
/* eslint-env browser, webextensions */

const browser = require('webextension-polyfill')

function safeTranslation (key) {
const translation = browser.i18n.getMessage(key)
return translation || '[i18n: ' + key + ']'
}

// Search for items with data-i18n attribute and replace them with values from current locale
const items = document.querySelectorAll('[data-i18n]')
for (let item of items) {
const key = item.getAttribute('data-i18n')
if (key) {
const translation = safeTranslation(key)
if (typeof item.value !== 'undefined' && item.value === 'i18n') {
// things like inputs can trigger translation with value equal "i18n"
item.value = translation
} else {
item.innerText = translation
module.exports = function () {
// Search for items with data-i18n attribute and replace them with values from current locale
const items = document.querySelectorAll('[data-i18n]')
for (let item of items) {
const key = item.getAttribute('data-i18n')
if (key) {
const translation = safeTranslation(key)
if (typeof item.value !== 'undefined' && item.value === 'i18n') {
// things like inputs can trigger translation with value equal "i18n"
item.value = translation
} else {
item.innerText = translation
}
}
}
}
97 changes: 97 additions & 0 deletions add-on/src/lib/dns-link.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
'use strict'
/* eslint-env browser */

const IsIpfs = require('is-ipfs')
const { LRUMap } = require('lru_map')

module.exports = function createDnsLink (getState) {
const cache = new LRUMap(1000)

const dnsLink = {
isDnslookupPossible () {
// DNS lookups require IPFS API to be up
// and have a confirmed connection to the internet
return getState().peerCount > 0
},

isDnslookupSafeForURL (requestUrl) {
// skip URLs that could produce infinite recursion or weird loops
return dnsLink.isDnslookupPossible() &&
requestUrl.startsWith('http') &&
!IsIpfs.url(requestUrl) &&
!requestUrl.startsWith(getState().apiURLString) &&
!requestUrl.startsWith(getState().gwURLString)
},

dnslinkLookupAndOptionalRedirect (requestUrl) {
const url = new URL(requestUrl)
const fqdn = url.hostname
const dnslink = dnsLink.cachedDnslinkLookup(fqdn)
if (dnslink) {
// redirect to IPNS and leave it up to the gateway
// to load the correct path from IPFS
// - https://github.com/ipfs/ipfs-companion/issues/298
return dnsLink.redirectToIpnsPath(url)
}
},

cachedDnslinkLookup (fqdn) {
let dnslink = cache.get(fqdn)
if (typeof dnslink === 'undefined') {
try {
console.info('dnslink cache miss for: ' + fqdn)
dnslink = dnsLink.readDnslinkFromTxtRecord(fqdn)
if (dnslink) {
cache.set(fqdn, dnslink)
console.info(`Resolved dnslink: '${fqdn}' -> '${dnslink}'`)
} else {
cache.set(fqdn, false)
console.info(`Resolved NO dnslink for '${fqdn}'`)
}
} catch (error) {
console.error(`Error in dnslinkLookupAndOptionalRedirect for '${fqdn}'`)
console.error(error)
}
} else {
console.info(`Resolved via cached dnslink: '${fqdn}' -> '${dnslink}'`)
}
return dnslink
},

readDnslinkFromTxtRecord (fqdn) {
// js-ipfs-api does not provide method for fetching this
// TODO: revisit after https://github.com/ipfs/js-ipfs-api/issues/501 is addressed
const apiCall = `${getState().apiURLString}api/v0/dns/${fqdn}`
const xhr = new XMLHttpRequest() // older XHR API us used because window.fetch appends Origin which causes error 403 in go-ipfs
// synchronous mode with small timeout
// (it is okay, because we do it only once, then it is cached and read via cachedDnslinkLookup)
xhr.open('GET', apiCall, false)
xhr.setRequestHeader('Accept', 'application/json')
xhr.send(null)
if (xhr.status === 200) {
const dnslink = JSON.parse(xhr.responseText).Path
// console.log('readDnslinkFromTxtRecord', readDnslinkFromTxtRecord)
if (!IsIpfs.path(dnslink)) {
throw new Error(`dnslink for '${fqdn}' is not a valid IPFS path: '${dnslink}'`)
}
return dnslink
} else if (xhr.status === 500) {
// go-ipfs returns 500 if host has no dnslink
// TODO: find/fill an upstream bug to make this more intuitive
return false
} else {
throw new Error(xhr.statusText)
}
},

redirectToIpnsPath (url) {
const fqdn = url.hostname
url.protocol = getState().gwURL.protocol
url.host = getState().gwURL.host
url.pathname = `/ipns/${fqdn}${url.pathname}`
return { redirectUrl: url.toString() }
}
}

return dnsLink
}
Loading