2019-01-26 00:18:21 +00:00
|
|
|
import { 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-02-06 00:08:48 +00:00
|
|
|
import { NewSessionMessage } from "@coder/protocol/src/proto";
|
2019-01-18 23:08:44 +00:00
|
|
|
import { ChildProcess } from "child_process";
|
2019-01-15 18:36:09 +00:00
|
|
|
import * as express from "express";
|
2019-02-05 17:15:20 +00:00
|
|
|
//@ts-ignore
|
|
|
|
import * as expressStaticGzip from "express-static-gzip";
|
2019-01-26 19:16:06 +00:00
|
|
|
import * as fs from "fs";
|
2019-01-15 18:36:09 +00:00
|
|
|
import * as http from "http";
|
2019-01-26 19:16:06 +00:00
|
|
|
import * as mime from "mime-types";
|
2019-01-26 00:18:21 +00:00
|
|
|
import * as path from "path";
|
2019-01-26 19:16:06 +00:00
|
|
|
import * as util from "util";
|
2019-01-15 18:36:09 +00:00
|
|
|
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 => {
|
2019-01-28 17:14:06 +00:00
|
|
|
if (ws.readyState !== ws.OPEN) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-22 17:48:01 +00:00
|
|
|
try {
|
|
|
|
ws.send(data);
|
|
|
|
} catch (error) {
|
2019-01-22 19:48:43 +00:00
|
|
|
logger.error(error.message);
|
2019-01-22 17:48:01 +00:00
|
|
|
}
|
|
|
|
},
|
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 => {
|
2019-01-23 00:28:54 +00:00
|
|
|
let proc: ChildProcess;
|
2019-01-18 23:08:44 +00:00
|
|
|
if (message.getIsBootstrapFork()) {
|
2019-01-26 00:18:21 +00:00
|
|
|
const env: NodeJS.ProcessEnv = {};
|
|
|
|
message.getEnvMap().forEach((value, key) => {
|
|
|
|
env[key] = value;
|
|
|
|
});
|
|
|
|
proc = forkModule(message.getCommand(), env);
|
2019-01-18 23:08:44 +00:00
|
|
|
} else {
|
|
|
|
throw new Error("No support for non bootstrap-forking yet");
|
|
|
|
}
|
|
|
|
|
|
|
|
return proc;
|
|
|
|
},
|
|
|
|
} : undefined);
|
2019-01-15 18:36:09 +00:00
|
|
|
});
|
|
|
|
|
2019-02-05 17:15:20 +00:00
|
|
|
const baseDir = process.env.BUILD_DIR || path.join(__dirname, "..");
|
|
|
|
if (process.env.CLI) {
|
|
|
|
app.use(expressStaticGzip(path.join(baseDir, "build/web")));
|
|
|
|
} else {
|
|
|
|
app.use(express.static(path.join(baseDir, "resources/web")));
|
|
|
|
}
|
2019-01-26 19:16:06 +00:00
|
|
|
app.get("/resource/:url(*)", async (req, res) => {
|
|
|
|
try {
|
|
|
|
const fullPath = `/${req.params.url}`;
|
2019-01-28 17:14:06 +00:00
|
|
|
// const relative = path.relative(options!.dataDirectory, fullPath);
|
|
|
|
// if (relative.startsWith("..")) {
|
|
|
|
// return res.status(403).end();
|
|
|
|
// }
|
2019-02-05 17:15:20 +00:00
|
|
|
const exists = fs.existsSync(fullPath);
|
2019-01-26 19:16:06 +00:00
|
|
|
if (!exists) {
|
|
|
|
res.status(404).end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const stat = await util.promisify(fs.stat)(fullPath);
|
|
|
|
if (!stat.isFile()) {
|
|
|
|
res.write("Resource must be a file.");
|
|
|
|
res.status(422);
|
|
|
|
res.end();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let mimeType = mime.lookup(fullPath);
|
|
|
|
if (mimeType === false) {
|
|
|
|
mimeType = "application/octet-stream";
|
|
|
|
}
|
|
|
|
const content = await util.promisify(fs.readFile)(fullPath);
|
|
|
|
|
|
|
|
res.header("Content-Type", mimeType as string);
|
|
|
|
res.write(content);
|
|
|
|
res.status(200);
|
|
|
|
res.end();
|
|
|
|
} catch (ex) {
|
|
|
|
res.write(ex.toString());
|
|
|
|
res.status(500);
|
|
|
|
res.end();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-01-15 18:36:09 +00:00
|
|
|
return {
|
|
|
|
express: app,
|
|
|
|
server,
|
|
|
|
wss,
|
|
|
|
};
|
|
|
|
};
|