Skip to content

Commit

Permalink
Use stream protocol, update titlebar insert point
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Varache committed Mar 6, 2018
1 parent 9af8d96 commit 0ac76ab
Show file tree
Hide file tree
Showing 8 changed files with 3,977 additions and 32 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
Thumbs.db
2 changes: 1 addition & 1 deletion app/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<link rel="stylesheet" href="./main.css">
</head>
<body>

<h1>Content</h1>
</body>
</html>
4 changes: 2 additions & 2 deletions lib/file-server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { protocol } = require('electron');
const spaHandler = require('./spa-file-protocol-handler');
const spaHandler = require('./spa-stream-protocol-handler');

function registerProtocol(scheme, root, handler = () => {}) {
protocol.registerFileProtocol(scheme, spaHandler(root), handler);
protocol.registerStreamProtocol(scheme, spaHandler(root), handler);
}

module.exports = { registerProtocol };
16 changes: 0 additions & 16 deletions lib/spa-file-protocol-handler.js

This file was deleted.

27 changes: 27 additions & 0 deletions lib/spa-stream-protocol-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const url = require('url');
const path = require('path');
const fs = require('fs');
const mime = require('mime-types');

function getExtension(p) {
const parts = p.split('/');
const lastPart = parts[parts.length - 1];
return lastPart.split('.').pop();
}

module.exports = function getHandler(root, fallback = 'index.html') {
return (req, callback) => {
// Parse the url to extract the pathname
const u = url.parse(req.url);
let extension = getExtension(u.pathname);
// If the path contains an extension, it is a request for a file
const isFile = extension !== '';
// Replace the path with the fallback file if the request is not for a file
const filename = isFile ? u.pathname : fallback;
extension = getExtension(filename);
const file = path.normalize(`${root}/${filename}`);
const mimeType = mime.lookup(extension);
const stream = fs.createReadStream(file);
callback({ statusCode: 200, headers: { 'content-type': mimeType }, data: stream });
};
};
2 changes: 1 addition & 1 deletion lib/titlebar/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class Titlebar {
EVENTS.forEach(eventName => (
this.element.addEventListener(eventName, () => ipcRenderer.send(eventName))
));
container.appendChild(this.element);
container.prepend(this.element);
this.element.setup();
}
}
Expand Down
Loading

0 comments on commit 0ac76ab

Please sign in to comment.