Skip to content

Commit

Permalink
Formatted using js-beautify.
Browse files Browse the repository at this point in the history
  • Loading branch information
anderejd committed Apr 12, 2019
1 parent b3a2755 commit 6bbe05b
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 77 deletions.
4 changes: 4 additions & 0 deletions .jsbeautifyrc
@@ -0,0 +1,4 @@
{
"indent_with_tabs": true,
"wrap_line_length": 80
}
112 changes: 63 additions & 49 deletions main.js
@@ -1,58 +1,72 @@
let {app, protocol, BrowserWindow} = require("electron");
let {readFile} = require("fs");
let {extname} = require("path");
let {URL} = require("url");
let {
app,
protocol,
BrowserWindow
} = require("electron");
let {
readFile
} = require("fs");
let {
extname
} = require("path");
let {
URL
} = require("url");

let createProtocol = (scheme, normalize = true) => {
protocol.registerBufferProtocol(scheme,
(request, respond) => {
let pathName = new URL(request.url).pathname;
pathName = decodeURI(pathName); // Needed in case URL contains spaces

readFile(__dirname + "/" + pathName, (error, data) => {
let extension = extname(pathName).toLowerCase();
let mimeType = "";
protocol.registerBufferProtocol(scheme,
(request, respond) => {
let pathName = new URL(request.url).pathname;

if (extension === ".js") {
mimeType = "text/javascript";
}
else if (extension === ".html") {
mimeType = "text/html";
}
else if (extension === ".css") {
mimeType = "text/css";
}
else if (extension === ".svg" || extension === ".svgz") {
mimeType = "image/svg+xml";
}
else if (extension === ".json") {
mimeType = "application/json";
}
// Needed in case URL contains spaces
pathName = decodeURI(pathName);

respond({mimeType, data});
});
},
(error) => {
if (error) {
console.error(`Failed to register ${scheme} protocol`, error);
}
}
);
readFile(__dirname + "/" + pathName, (error, data) => {
let extension = extname(pathName).toLowerCase();
let mimeType = "";

if (extension === ".js") {
mimeType = "text/javascript";
} else if (extension === ".html") {
mimeType = "text/html";
} else if (extension === ".css") {
mimeType = "text/css";
} else if (extension === ".svg" || extension ===
".svgz") {
mimeType = "image/svg+xml";
} else if (extension === ".json") {
mimeType = "application/json";
}

respond({
mimeType,
data
});
});
},
(error) => {
if (error) {
console.error(`Failed to register ${scheme} protocol`,
error);
}
}
);
}

// Standard scheme must be registered before the app is ready
protocol.registerStandardSchemes(["app"], { secure: true });
protocol.registerStandardSchemes(["app"], {
secure: true
});

app.on("ready", () => {
createProtocol("app");
let browserWindow = new BrowserWindow({
webPreferences: {
preload: `${__dirname}/preload.js`,
nodeIntegration: false,
contextIsolation: true
}
});

//browserWindow.webContents.openDevTools();
browserWindow.loadFile("index.html");
});
createProtocol("app");
let browserWindow = new BrowserWindow({
webPreferences: {
preload: `${__dirname}/preload.js`,
nodeIntegration: false,
contextIsolation: true
}
});
//browserWindow.webContents.openDevTools();
browserWindow.loadFile("index.html");
});
40 changes: 22 additions & 18 deletions main_module.js
Expand Up @@ -5,23 +5,27 @@
// will "boot" the module and make it ready to use. Currently browsers
// don't support natively imported WebAssembly as an ES module, but
// eventually the manual initialization won't be required!
import { add, default as init } from './wasm/wasm_bindgen_minimal_example.js';
import {
add,
default as init
} from './wasm/wasm_bindgen_minimal_example.js';
async function run() {
// First up we need to actually load the wasm file, so we use the
// default export to inform it where the wasm file is located on the
// server, and then we wait on the returned promise to wait for the
// wasm to be loaded.
//
// Note that instead of a string here you can also pass in an instance
// of `WebAssembly.Module` which allows you to compile your own module.
// Also note that the promise, when resolved, yields the wasm module's
// exports which is the same as importing the `*_bg` module in other
// modes
await init('./wasm/wasm_bindgen_minimal_example_bg.wasm');
// And afterwards we can use all the functionality defined in wasm.
const result = add(1, 2);
console.log(`1 + 2 = ${result}`);
if (result !== 3)
throw new Error("wasm addition doesn't work!");
// First up we need to actually load the wasm file, so we use the
// default export to inform it where the wasm file is located on the
// server, and then we wait on the returned promise to wait for the
// wasm to be loaded.
//
// Note that instead of a string here you can also pass in an instance
// of `WebAssembly.Module` which allows you to compile your own module.
// Also note that the promise, when resolved, yields the wasm module's
// exports which is the same as importing the `*_bg` module in other
// modes
await init('./wasm/wasm_bindgen_minimal_example_bg.wasm');
// And afterwards we can use all the functionality defined in wasm.
const result = add(1, 2);
console.log(`1 + 2 = ${result}`);
if (result !== 3) {
throw new Error("wasm addition doesn't work!");
}
}
run();
run();
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.

20 changes: 11 additions & 9 deletions preload.js
@@ -1,12 +1,14 @@
let {webFrame} = require("electron");
let {
webFrame
} = require("electron");

process.once("loaded", () => {
// Allow window.fetch() to access app files
webFrame.registerURLSchemeAsPrivileged("app", {
secure: true,
bypassCSP: false,
allowServiceWorkers: true,
supportFetchAPI: true,
corsEnabled: false
});
// Allow window.fetch() to access app files
webFrame.registerURLSchemeAsPrivileged("app", {
secure: true,
bypassCSP: false,
allowServiceWorkers: true,
supportFetchAPI: true,
corsEnabled: false
});
});

0 comments on commit 6bbe05b

Please sign in to comment.