diff --git a/app/browser/webtorrent.js b/app/browser/webtorrent.js index 5912a040821..b2e0ce3ad57 100644 --- a/app/browser/webtorrent.js +++ b/app/browser/webtorrent.js @@ -4,7 +4,7 @@ const appUrlUtil = require('../../js/lib/appUrlUtil') const appActions = require('../../js/actions/appActions') const messages = require('../../js/constants/messages') const Filtering = require('../filtering') -const urlParse = require('url').parse +const urlParse = require('../common/urlParse') // Set to see communication between WebTorrent and torrent viewer tabs const DEBUG_IPC = false diff --git a/app/common/urlParse.js b/app/common/urlParse.js index 7a73333d69c..1d82cc8a62a 100644 --- a/app/common/urlParse.js +++ b/app/common/urlParse.js @@ -3,8 +3,17 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ const LRUCache = require('lru-cache') -const urlParse = require('url').parse const config = require('../../js/constants/config') + +// muon.url.parse is not available in all environments (ex: unittests) +let urlParse +try { + urlParse = muon.url.parse +} catch (e) { + // TODO: move to the new node URL API: https://nodejs.org/api/url.html#url_url + urlParse = require('url').parse +} + let cachedUrlParse = new LRUCache(config.cache.urlParse) module.exports = (url, ...args) => { diff --git a/js/webtorrent/entry.js b/js/webtorrent/entry.js index 9988784e692..97dc57ae90b 100644 --- a/js/webtorrent/entry.js +++ b/js/webtorrent/entry.js @@ -6,11 +6,12 @@ const path = require('path') const querystring = require('querystring') const React = require('react') const ReactDOM = require('react-dom') -const url = require('url') +const urlFormat = require('url').format const WebTorrentRemoteClient = require('webtorrent-remote/client') const App = require('./components/app') const messages = require('../constants/messages') +const urlParse = require('../../app/common/urlParse') // Stylesheets require('../../less/webtorrent.less') @@ -37,7 +38,7 @@ window.addEventListener('hashchange', init) function init () { store.torrentId = window.decodeURIComponent(window.location.hash.substring(1)) - const parsedUrl = url.parse(store.torrentId) + const parsedUrl = urlParse(store.torrentId) store.torrentIdProtocol = parsedUrl.protocol // `ix` param can be set by query param or hash, e.g. ?ix=1 or #ix=1 @@ -182,10 +183,10 @@ function stop () { } function saveTorrentFile () { - const parsedUrl = url.parse(store.torrentId, true) + const parsedUrl = urlParse(store.torrentId, true) parsedUrl.query.download = true const name = path.basename(parsedUrl.pathname) || 'untitled.torrent' - const href = url.format(parsedUrl) + const href = urlFormat(parsedUrl) const a = document.createElement('a') a.rel = 'noopener'