Skip to content
This repository has been archived by the owner on Jun 20, 2022. It is now read-only.

Commit

Permalink
Use local server to host offline extensions, 3.0.22
Browse files Browse the repository at this point in the history
  • Loading branch information
moughxyz committed Nov 21, 2019
1 parent 48996ea commit b4dc38e
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/index.html
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8"/>
<meta content="IE=edge" http-equiv="X-UA-Compatible"/>
<meta content="width=device-width, initial-scale=1" name="viewport"/>
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; connect-src *; style-src 'unsafe-inline' 'self'; frame-src *">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; connect-src *; style-src 'unsafe-inline' 'self' http://localhost:45653; frame-src *">
<meta name="theme-color" content="#ffffff">

<title>Notes</title>
Expand Down
3 changes: 3 additions & 0 deletions app/index.js
Expand Up @@ -7,6 +7,9 @@ const shell = require('electron').shell;
const log = require('electron-log');
const Store = require('./javascripts/main/store.js');

const ExtensionsServer = require('./javascripts/main/extServer.js');
ExtensionsServer.instance().createServer();

import menuManager from './javascripts/main/menuManager.js'
import archiveManager from './javascripts/main/archiveManager.js';
import packageManager from './javascripts/main/packageManager.js';
Expand Down
66 changes: 66 additions & 0 deletions app/javascripts/main/extServer.js
@@ -0,0 +1,66 @@
const {app} = require('electron');
var http = require('http');
var fs = require('fs');
var path = require('path');

const mimes = {
'.ico': 'image/x-icon',
'.html': 'text/html',
'.js': 'text/javascript',
'.json': 'application/json',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg'
};

let instance = null;

class ExtensionsServer {

static instance() {
if(instance == null) {
instance = new ExtensionsServer();
}
return instance;
}

constructor() {
this.port = 45653;
}

getHost() {
return `http://localhost:${this.port}/`;
}

createServer() {
function handleRequest(req, res) {
const extensionsFolder = "Extensions";
const extensionsDir = path.join(app.getPath('userData'), extensionsFolder);
const modifiedReqUrl = req.url.replace(extensionsFolder, "");
const filePath = path.join(extensionsDir, modifiedReqUrl);

fs.exists(filePath, function(exists) {
if(exists && fs.lstatSync(filePath).isFile()) {
const ext = path.parse(filePath).ext;
res.setHeader("Content-Type", mimes[ext] || 'text/plain');
res.writeHead(200, {
'Access-Control-Allow-Origin': '*'
});
fs.createReadStream(filePath).pipe(res);
return;
}

res.writeHead(404);
res.write('404 Not Found');
res.end();
});
}

var server = http.createServer(handleRequest);
server.listen(this.port, () => {
console.log(`Extensions server started at http://localhost:${this.port}`);
});
}
}

module.exports = ExtensionsServer;
5 changes: 3 additions & 2 deletions app/javascripts/renderer/preload.js
Expand Up @@ -3,6 +3,7 @@ const osLocale = require('os-locale');
const os = require('os');

const Store = require('../main/store.js');
const ExtensionsServer = require('../main/extServer.js')
const buildEditorContextMenu = remote.require('electron-editor-context-menu');
const rendererPath = 'file://' + __dirname + '/renderer.js';

Expand All @@ -23,7 +24,7 @@ const transmitter = new Transmitter(messageBus,
},
isMacOS: PropertyType.VALUE,
appVersion: PropertyType.VALUE,
userDataPath: PropertyType.VALUE,
extServerHost: PropertyType.VALUE,
useSystemMenuBar: PropertyType.VALUE,
sendIpcMessage: {
type: PropertyType.METHOD,
Expand Down Expand Up @@ -52,7 +53,7 @@ function loadTransmitter() {

transmitter.expose({
spellcheck: spellcheck,
userDataPath: remote.app.getPath('userData'),
extServerHost: ExtensionsServer.instance().getHost(),
rendererPath: rendererPath,
isMacOS: process.platform === "darwin",
appVersion: remote.app.getVersion(),
Expand Down
4 changes: 2 additions & 2 deletions app/javascripts/renderer/renderer.js
Expand Up @@ -83,8 +83,8 @@ async function configureWindow() {
}

async function configureDesktopManager() {
const userDataPath = await bridge.userDataPath;
desktopManager.desktop_setApplicationDataPath(userDataPath);
const extServerHost = await bridge.extServerHost;
desktopManager.desktop_setExtServerHost(extServerHost);

/* Handled by PackageManager */
desktopManager.desktop_setComponentInstallationSyncHandler(async (componentsData) => {
Expand Down
4 changes: 2 additions & 2 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/package.json
Expand Up @@ -3,7 +3,7 @@
"productName": "Standard Notes",
"description": "A simple and private place for your notes, thoughts, and life's work.",
"author": "Standard Notes <help@standardnotes.org>",
"version": "3.0.21",
"version": "3.0.22",
"main": "./dist/index.js",
"dependencies": {
"adm-zip": "^0.4.7",
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "standard-notes",
"main": "./app/dist/index.js",
"version": "3.0.21",
"version": "3.0.22",
"license": "AGPL-3.0-or-later",
"devDependencies": {
"@babel/cli": "^7.6.4",
Expand Down

0 comments on commit b4dc38e

Please sign in to comment.