2019-01-26 01:18:21 +01:00
|
|
|
import { logger } from "@coder/logger";
|
2019-01-15 19:36:09 +01:00
|
|
|
import { ReadWriteConnection } from "@coder/protocol";
|
|
|
|
import { Server, ServerOptions } from "@coder/protocol/src/node/server";
|
2019-02-06 01:08:48 +01:00
|
|
|
import { NewSessionMessage } from "@coder/protocol/src/proto";
|
2019-01-19 00:08:44 +01:00
|
|
|
import { ChildProcess } from "child_process";
|
2019-01-15 19:36:09 +01:00
|
|
|
import * as express from "express";
|
2019-02-05 18:15:20 +01:00
|
|
|
//@ts-ignore
|
|
|
|
import * as expressStaticGzip from "express-static-gzip";
|
2019-01-26 20:16:06 +01:00
|
|
|
import * as fs from "fs";
|
2019-01-15 19:36:09 +01:00
|
|
|
import * as http from "http";
|
2019-01-26 20:16:06 +01:00
|
|
|
import * as mime from "mime-types";
|
2019-01-26 01:18:21 +01:00
|
|
|
import * as path from "path";
|
2019-01-26 20:16:06 +01:00
|
|
|
import * as util from "util";
|
2019-01-15 19:36:09 +01:00
|
|
|
import * as ws from "ws";
|
2019-01-19 00:08:44 +01:00
|
|
|
import { forkModule } from "./vscode/bootstrapFork";
|
2019-01-15 19:36:09 +01: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 22:46:40 +01:00
|
|
|
} => {
|
2019-01-15 19:36:09 +01:00
|
|
|
const app = express();
|
|
|
|
if (registerMiddleware) {
|
|
|
|
registerMiddleware(app);
|
|
|
|
}
|
|
|
|
const server = http.createServer(app);
|
|
|
|
const wss = new ws.Server({ server });
|
2019-01-18 22:46:40 +01:00
|
|
|
|
|
|
|
wss.shouldHandle = (req): boolean => {
|
2019-01-19 00:08:44 +01:00
|
|
|
// Should handle auth here
|
2019-01-18 22:46:40 +01:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2019-01-22 18:48:01 +01:00
|
|
|
wss.on("connection", (ws) => {
|
2019-01-15 19:36:09 +01:00
|
|
|
const connection: ReadWriteConnection = {
|
2019-01-18 22:46:40 +01:00
|
|
|
onMessage: (cb): void => {
|
2019-01-15 19:36:09 +01:00
|
|
|
ws.addEventListener("message", (event) => cb(event.data));
|
|
|
|
},
|
2019-01-18 22:46:40 +01:00
|
|
|
close: (): void => ws.close(),
|
2019-01-22 18:48:01 +01:00
|
|
|
send: (data): void => {
|
2019-01-28 18:14:06 +01:00
|
|
|
if (ws.readyState !== ws.OPEN) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-22 18:48:01 +01:00
|
|
|
try {
|
|
|
|
ws.send(data);
|
|
|
|
} catch (error) {
|
2019-01-22 20:48:43 +01:00
|
|
|
logger.error(error.message);
|
2019-01-22 18:48:01 +01:00
|
|
|
}
|
|
|
|
},
|
2019-01-18 22:46:40 +01:00
|
|
|
onClose: (cb): void => ws.addEventListener("close", () => cb()),
|
2019-01-15 19:36:09 +01:00
|
|
|
};
|
2019-01-18 22:46:40 +01:00
|
|
|
|
2019-01-19 00:08:44 +01:00
|
|
|
const server = new Server(connection, options ? {
|
|
|
|
...options,
|
|
|
|
forkProvider: (message: NewSessionMessage): ChildProcess => {
|
2019-01-23 01:28:54 +01:00
|
|
|
let proc: ChildProcess;
|
2019-01-19 00:08:44 +01:00
|
|
|
if (message.getIsBootstrapFork()) {
|
2019-01-26 01:18:21 +01:00
|
|
|
const env: NodeJS.ProcessEnv = {};
|
|
|
|
message.getEnvMap().forEach((value, key) => {
|
|
|
|
env[key] = value;
|
|
|
|
});
|
|
|
|
proc = forkModule(message.getCommand(), env);
|
2019-01-19 00:08:44 +01:00
|
|
|
} else {
|
|
|
|
throw new Error("No support for non bootstrap-forking yet");
|
|
|
|
}
|
|
|
|
|
|
|
|
return proc;
|
|
|
|
},
|
|
|
|
} : undefined);
|
2019-01-15 19:36:09 +01:00
|
|
|
});
|
|
|
|
|
2019-02-05 18:15:20 +01: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 20:16:06 +01:00
|
|
|
app.get("/resource/:url(*)", async (req, res) => {
|
|
|
|
try {
|
|
|
|
const fullPath = `/${req.params.url}`;
|
2019-01-28 18:14:06 +01:00
|
|
|
// const relative = path.relative(options!.dataDirectory, fullPath);
|
|
|
|
// if (relative.startsWith("..")) {
|
|
|
|
// return res.status(403).end();
|
|
|
|
// }
|
2019-02-05 18:15:20 +01:00
|
|
|
const exists = fs.existsSync(fullPath);
|
2019-01-26 20:16:06 +01: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 19:36:09 +01:00
|
|
|
return {
|
|
|
|
express: app,
|
|
|
|
server,
|
|
|
|
wss,
|
|
|
|
};
|
|
|
|
};
|