Skip to content
This repository has been archived by the owner on Jan 6, 2020. It is now read-only.

Commit

Permalink
Enable localhost dev with safe apis (#48)
Browse files Browse the repository at this point in the history
* yarn lock

* feat/localhost: Enable localhost urls to be accessible and use safe: apis
  • Loading branch information
joshuef authored and hitman401 committed Aug 2, 2017
1 parent 4d455e9 commit 9468a40
Show file tree
Hide file tree
Showing 3 changed files with 4,957 additions and 14 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const safeProtocol = require('./dist/protocol');
const safeProtocols = require('./dist/protocol');
const api = require('./dist/api');

module.exports = {
Expand All @@ -8,6 +8,6 @@ module.exports = {
label: 'SAFE App',
href: 'https://safenetforum.org/t/safe-network-alpha-release/10687/1'
}],
protocols: [safeProtocol],
protocols: safeProtocols,
webAPIs: api
};
47 changes: 35 additions & 12 deletions src/protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const mime = require('mime');
const ipc = require('./api/ipc');
/* eslint-disable import/no-extraneous-dependencies, import/no-unresolved */
const protocol = require('electron').protocol;
/* eslint-enable import/no-extraneous-dependencies, import/no-unresolved */

const safeScheme = 'safe';
const safeLocalScheme = 'localhost';

const appInfo = {
id: 'net.maidsafe.app.browser',
Expand All @@ -34,7 +34,7 @@ const authoriseApp = () => {
resolve(true);
});
})));
})
});
};

const fetchData = (url) => {
Expand All @@ -44,17 +44,34 @@ const fetchData = (url) => {
return appObj.webFetch(url)
};

const registerSafeAuthProtocol = () => {

const handleError = (err) => {
if (mimeType === 'html') {
return cb({ mimeType, data: err.message });
}
cb(null);
};


const registerSafeLocalProtocol = () => {
protocol.registerHttpProtocol(safeLocalScheme, (req, cb) => {
const parsed = urlParse(req.url);

if (!parsed.host) { return; }

const path = parsed.pathname;
const port = parsed.port;
const newUrl = `http://localhost:${port}${path}`;

cb({ url: newUrl });
});
};

const registerSafeProtocol = () => {
protocol.registerBufferProtocol(safeScheme, (req, cb) => {
const parsedUrl = urlParse(req.url);
const fileExt = path.basename(parsedUrl.pathname) || 'html';
const mimeType = mime.lookup(fileExt);
const handleError = (err) => {
if (mimeType === 'html') {
return cb({ mimeType, data: err.message });
}
cb(null);
};

authoriseApp()
.then(() => fetchData(req.url))
Expand All @@ -65,10 +82,16 @@ const registerSafeAuthProtocol = () => {
});
};

module.exports = {
module.exports = [{
scheme: safeScheme,
label: 'SAFE',
isStandardURL: true,
isInternal: true,
register: registerSafeAuthProtocol
};
register: registerSafeProtocol
}, {
scheme: safeLocalScheme,
label: 'SAFE-localhost',
isStandardURL: true,
isInternal: true,
register: registerSafeLocalProtocol
}];

0 comments on commit 9468a40

Please sign in to comment.