Implement webview server

This commit is contained in:
Asher 2019-07-08 16:44:01 -05:00
parent fe1d609d1a
commit f482087475
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
1 changed files with 41 additions and 29 deletions

View File

@ -80,6 +80,8 @@ export abstract class Server {
// The underlying web server.
protected readonly server: http.Server;
protected rootPath = path.resolve(__dirname, "../../..");
private listenPromise: Promise<string> | undefined;
public constructor(private readonly port: number) {
@ -101,7 +103,7 @@ export abstract class Server {
: ["", "", ""];
const { content, headers, code } = await this.handleRequest(
request, parsedUrl, base, requestPath,
base, requestPath, parsedUrl, request,
);
response.writeHead(code || HttpCode.Ok, {
"Cache-Control": "max-age=86400",
@ -119,13 +121,6 @@ export abstract class Server {
});
}
protected abstract handleRequest(
request: http.IncomingMessage,
parsedUrl: url.UrlWithParsedQuery,
base: string,
requestPath: string,
): Promise<Response>;
public listen(): Promise<string> {
if (!this.listenPromise) {
this.listenPromise = new Promise((resolve, reject) => {
@ -145,6 +140,28 @@ export abstract class Server {
: address;
return `http://${endpoint}`;
}
protected abstract handleRequest(
base: string,
requestPath: string,
parsedUrl: url.UrlWithParsedQuery,
request: http.IncomingMessage,
): Promise<Response>;
protected async getResource(filePath: string): Promise<Response> {
const content = await util.promisify(fs.readFile)(filePath);
return {
content,
headers: {
"Content-Type": getMediaMime(filePath) || {
".css": "text/css",
".html": "text/html",
".js": "text/javascript",
".json": "application/json",
}[extname(filePath)] || "text/plain",
},
};
}
}
export class MainServer extends Server {
@ -152,8 +169,6 @@ export class MainServer extends Server {
public readonly _onDidClientConnect = new Emitter<ClientConnectionEvent>();
public readonly onDidClientConnect = this._onDidClientConnect.event;
private readonly rootPath = path.resolve(__dirname, "../../..");
// This is separate instead of just extending this class since we can't
// use properties in the super call. This manages channels.
private readonly ipc = new IPCServer(this.onDidClientConnect);
@ -212,10 +227,10 @@ export class MainServer extends Server {
}
protected async handleRequest(
request: http.IncomingMessage,
parsedUrl: url.UrlWithParsedQuery,
base: string,
requestPath: string,
parsedUrl: url.UrlWithParsedQuery,
request: http.IncomingMessage,
): Promise<Response> {
switch (base) {
case "/":
@ -280,21 +295,6 @@ export class MainServer extends Server {
headers: {
"Content-Type": "text/html",
},
}
}
private async getResource(filePath: string): Promise<Response> {
const content = await util.promisify(fs.readFile)(filePath);
return {
content,
headers: {
"Content-Type": getMediaMime(filePath) || {
".css": "text/css",
".html": "text/html",
".js": "text/javascript",
".json": "application/json",
}[extname(filePath)] || "text/plain",
},
};
}
@ -392,7 +392,19 @@ export class MainServer extends Server {
}
export class WebviewServer extends Server {
protected async handleRequest(): Promise<Response> {
throw new Error("not implemented");
protected async handleRequest(
base: string,
requestPath: string,
): Promise<Response> {
const webviewPath = path.join(
this.rootPath,
"out/vs/workbench/contrib/webview/browser/pre",
);
if (base === "/") {
base = "/index.html";
}
return this.getResource(path.join(webviewPath, base, requestPath));
}
}