Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let context = createContext(session);
context.scopes.unshift(require("./lib/model/market").markets);
if (config.hooks.setup) await config.hooks.setup(session, context);
if (config.verbose) console.log("Opening subscriptions...");
let subscriptions = await session.subscribe(config.subscriptions || { });
if (config.log) {
fs.appendFile(config.log, JSON.stringify({ type: "sync", state: subscriptions }), err => err ? config.hooks.traceError(err) || console.error(err) : null)
}
if (config.http) {
if (config.verbose) console.log(`Starting HTTP server on port ${Number.isInteger(config.http) ? config.http : 8080}...`);
const app = createApp(context);
if (config.html) app.use(express.static(config.html));
const server = http.createServer(app), endpoint = wss(new WebSocket.Server({ server }));
context.scopes.unshift(endpoint.host(subscriptions));
server.listen(Number.isInteger(config.http) ? config.http : 8080);
}
else {
subscriptions = Object.assign(new ObservableObject({ }, { bubble: true, batch: true }), subscriptions);
context.scopes.unshift(subscriptions);
}
if (config.log) {
subscriptions.__handler = (keys, value, old, proxy) => {
fs.appendFile(config.log, JSON.stringify({ type: "update", keys: keys, value: value }), err => err ? config.hooks.traceError(err) || console.error(err) : null)
};
}
if (config.repl) {
if (config.verbose) console.log("Starting REPL...\n");
session(config).then(async session => {
if (config.verbose) console.log("Session established");
session.on("error", config.hooks.error || console.error);
if (config.verbose) console.log("Opening subscriptions...");
let subscriptions = await session.subscribe(config.subscriptions);
let context = new Context(constants, { observe, computed, dispose, Observable, Computable }, global);
context.resolvers.push(name => session.quote(name));
if (config.hooks.context) await config.hooks.context(context);
if (config.http) {
if (config.verbose) console.log(`Starting HTTP server on port ${Number.isInteger(config.http) ? config.http : 8080}...`);
const server = http.createServer(createApp(context)), endpoint = wss(new WebSocket.Server({ server }));
context.scopes.unshift(endpoint.host(subscriptions));
server.listen(Number.isInteger(config.http) ? config.http : 8080);
}
else {
subscriptions = Object.assign(new ObservableObject({ }), subscriptions);
context.scopes.unshift(subscriptions);
}
if (config.repl) {
if (config.verbose) console.log("Starting REPL...\n");
let terminal = repl.start({ prompt: "> ", eval: context.replEval });
terminal.on("exit", () => session.close(true));
}
else if (config.verbose) console.log("Ready.");
process.on("exit", config.hooks.exit || (() => session.close()));
function newServer(subscriptions, port) {
const app = express();
const server = http.createServer(app);
const wss = overactiv(new WebSocket.Server({ server }));
wss.host(subscriptions);
app.use(express.static("./static"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/static'));
app.get("/cmd/:cmd", async (req, res) => {
let cmd = req.params.cmd;
res.send(cmd);
});
app.post("/eval", async (req, res) => {
let src = req.body.src.trim();
if (src.length) {