Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket backend #40

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions bin/bin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/usr/bin/ts-node
/*!
Copyright 2018 Propel http://propel.site/. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/**
* This file contains "propel" command which can be used
* to start a local web server and execute codes on Node.js
* from notebook (browser).
*/

import { fork, isMaster } from "cluster";
import { readFileSync } from "fs";
import * as opn from "opn";
import * as path from "path";
import { TextDecoder } from "util";
import * as vm from "vm";
import * as WebSocket from "ws";
import { ClusterRPC } from "../src/rpc";
import { createHTTPServer } from "./server";

// Constants
const PORT = 8081;
const sandboxSrc = path.join(__dirname, "../build/website/sandbox.js");

if (isMaster) {
// Start a websocket server in master process.
const server = createHTTPServer();
const wss = new WebSocket.Server({ server });

wss.on("connection", (ws: WebSocket) => {
// For now, let's create a new process for each connection and load
// sandbox.ts in there.
const worker = fork();
// Route all messages from child process to client.
worker.on("message", msg => {
const message = JSON.stringify(msg);
ws.send(message);
});
// Handle worker exit.
worker.on("exit", () => {
console.log("[%s] Worker exited.", worker.process.pid);
if (ws.readyStatus === ws.OPEN) {
ws.close();
}
});
// Route all valid messages to the child process.
ws.on("message", rawData => {
try {
const data = JSON.parse(String(rawData));
worker.send(data);
} catch (e) {
console.error(e);
}
});
// Kill a child process whenever user disconnects.
ws.on("close", () => {
worker.kill();
});
});

server.listen(PORT, () => {
console.log("Propel server started on port %s.", PORT);
const wsUrl = `ws://localhost:${PORT}`;
opn(`http://localhost:${PORT}/#/?ws=${wsUrl}`);
});
} else {
console.log("[%s] Worker started.", process.pid);
const sandboxCode = readFileSync(sandboxSrc).toString();
const clusterRPC = new ClusterRPC(process);
const sandbox = {
Buffer,
TextDecoder,
clusterRPC,
process: {
cwd: process.cwd,
env: process.env,
platform: process.platform
},
require: safeRequire
};
vm.createContext(sandbox);
vm.runInContext(sandboxCode, sandbox);
}

function safeRequire(moduleName) {
const allowedModules = ["url", "http", "https"];
if (allowedModules.indexOf(moduleName) < 0) {
console.log(moduleName);
throw new Error("Calling require is forbidden.");
}
return require(moduleName);
}
50 changes: 50 additions & 0 deletions bin/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*!
Copyright 2018 Propel http://propel.site/. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import * as fs from "fs";
import * as http from "http";
import * as mime from "mime";
import * as path from "path";
import * as url from "url";

const basePath = path.join(__dirname, "../build/website");
const index = path.join(basePath, "index.html");

// create a simple HTTP server to serve static files.
export function createHTTPServer() {
const server = http.createServer((req, res) => {
const reqUrl = url.parse(req.url, true);
const filePath = path.join(basePath, reqUrl.pathname);
if (!filePath.startsWith(basePath)) {
res.writeHead(403, { "Content-Type": "text/html; charset=utf-8" });
res.end("Access denied.");
return;
}
fs.stat(filePath, (err, stat) => {
let finalPath = filePath;
if (err && err.code === "ENOENT" || stat.isDirectory()) {
finalPath = index;
} else if (err) {
res.writeHead(500, { "Content-Type": "text/html; charset=utf-8" });
res.end(`Unexpected server error occurred. [#${err.code}]`);
return;
}
res.writeHead(200, { "Content-Type": mime.getType(finalPath) });
const stream = fs.createReadStream(finalPath);
stream.pipe(res);
});
});
return server;
}
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@
"devDependencies": {
"@types/acorn": "^4.0.3",
"@types/codemirror": "0.0.51",
"@types/history": "^4.6.2",
"@types/node": "^8.9.4",
"acorn": "^5.5.0",
"codemirror": "^5.35.0",
"he": "^1.1.1",
"history": "^4.7.2",
"mime": "^2.3.1",
"ncp": "^2.0.0",
"node-fetch": "^1.7.3",
"node-sass": "^4.9.0",
"opn": "^5.3.0",
"parcel-bundler": "^1.8.1",
"parcel-plugin-markdown": "^0.3.1",
"path-to-regexp": "^2.2.1",
"preact": "^8.2.7",
"prettier": "^1.12.1",
"puppeteer": "^0.13.0",
Expand All @@ -43,7 +48,7 @@
"tslint-no-circular-imports": "^0.3.0",
"typescript": "^2.8.3",
"vega-lib": "^3.2.1",
"ws": "^5.0.0"
"ws": "^5.2.0"
},
"browserslist": [
">5%"
Expand Down
Loading