From f482087475ededa4b9d68e1ad400110f8532bd52 Mon Sep 17 00:00:00 2001 From: Asher Date: Mon, 8 Jul 2019 16:44:01 -0500 Subject: [PATCH] Implement webview server --- server.ts | 70 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/server.ts b/server.ts index 96ff1260..2c1eca32 100644 --- a/server.ts +++ b/server.ts @@ -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 | 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; - public listen(): Promise { 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; + + protected async getResource(filePath: string): Promise { + 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(); 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 { switch (base) { case "/": @@ -280,21 +295,6 @@ export class MainServer extends Server { headers: { "Content-Type": "text/html", }, - } - } - - private async getResource(filePath: string): Promise { - 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 { - throw new Error("not implemented"); + protected async handleRequest( + base: string, + requestPath: string, + ): Promise { + 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)); } }