2019-01-22 17:48:01 +00:00
|
|
|
import { field, logger } from "@coder/logger";
|
2019-01-15 18:36:09 +00:00
|
|
|
import { ReadWriteConnection } from "@coder/protocol";
|
|
|
|
import { Server, ServerOptions } from "@coder/protocol/src/node/server";
|
2019-01-18 23:08:44 +00:00
|
|
|
import { NewSessionMessage } from '@coder/protocol/src/proto';
|
|
|
|
import { ChildProcess } from "child_process";
|
2019-01-15 18:36:09 +00:00
|
|
|
import * as express from "express";
|
|
|
|
import * as http from "http";
|
|
|
|
import * as ws from "ws";
|
2019-01-18 23:08:44 +00:00
|
|
|
import { forkModule } from "./vscode/bootstrapFork";
|
2019-01-15 18:36:09 +00:00
|
|
|
|
|
|
|
export const createApp = (registerMiddleware?: (app: express.Application) => void, options?: ServerOptions): {
|
|
|
|
readonly express: express.Application;
|
|
|
|
readonly server: http.Server;
|
|
|
|
readonly wss: ws.Server;
|
2019-01-18 21:46:40 +00:00
|
|
|
} => {
|
2019-01-15 18:36:09 +00:00
|
|
|
const app = express();
|
|
|
|
if (registerMiddleware) {
|
|
|
|
registerMiddleware(app);
|
|
|
|
}
|
|
|
|
const server = http.createServer(app);
|
|
|
|
const wss = new ws.Server({ server });
|
2019-01-18 21:46:40 +00:00
|
|
|
|
|
|
|
wss.shouldHandle = (req): boolean => {
|
2019-01-18 23:08:44 +00:00
|
|
|
// Should handle auth here
|
2019-01-18 21:46:40 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2019-01-22 17:48:01 +00:00
|
|
|
wss.on("connection", (ws) => {
|
2019-01-15 18:36:09 +00:00
|
|
|
const connection: ReadWriteConnection = {
|
2019-01-18 21:46:40 +00:00
|
|
|
onMessage: (cb): void => {
|
2019-01-15 18:36:09 +00:00
|
|
|
ws.addEventListener("message", (event) => cb(event.data));
|
|
|
|
},
|
2019-01-18 21:46:40 +00:00
|
|
|
close: (): void => ws.close(),
|
2019-01-22 17:48:01 +00:00
|
|
|
send: (data): void => {
|
|
|
|
try {
|
|
|
|
ws.send(data);
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(error.message, field("error", error));
|
|
|
|
}
|
|
|
|
},
|
2019-01-18 21:46:40 +00:00
|
|
|
onClose: (cb): void => ws.addEventListener("close", () => cb()),
|
2019-01-15 18:36:09 +00:00
|
|
|
};
|
2019-01-18 21:46:40 +00:00
|
|
|
|
2019-01-18 23:08:44 +00:00
|
|
|
const server = new Server(connection, options ? {
|
|
|
|
...options,
|
|
|
|
forkProvider: (message: NewSessionMessage): ChildProcess => {
|
|
|
|
let proc: ChildProcess;
|
2019-01-19 00:04:24 +00:00
|
|
|
|
2019-01-18 23:08:44 +00:00
|
|
|
if (message.getIsBootstrapFork()) {
|
|
|
|
proc = forkModule(message.getCommand());
|
|
|
|
} else {
|
|
|
|
throw new Error("No support for non bootstrap-forking yet");
|
|
|
|
}
|
|
|
|
|
|
|
|
return proc;
|
|
|
|
},
|
|
|
|
} : undefined);
|
2019-01-15 18:36:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2019-01-18 21:46:40 +00:00
|
|
|
* We should static-serve the `web` package at this point.
|
2019-01-15 18:36:09 +00:00
|
|
|
*/
|
|
|
|
app.get("/", (req, res, next) => {
|
|
|
|
res.write("Example! :)");
|
|
|
|
res.status(200);
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
express: app,
|
|
|
|
server,
|
|
|
|
wss,
|
|
|
|
};
|
|
|
|
};
|