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