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