Skip to content

Commit f482087

Browse files
committed
Implement webview server
1 parent fe1d609 commit f482087

1 file changed

Lines changed: 41 additions & 29 deletions

File tree

server.ts

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ export abstract class Server {
8080
// The underlying web server.
8181
protected readonly server: http.Server;
8282

83+
protected rootPath = path.resolve(__dirname, "../../..");
84+
8385
private listenPromise: Promise<string> | undefined;
8486

8587
public constructor(private readonly port: number) {
@@ -101,7 +103,7 @@ export abstract class Server {
101103
: ["", "", ""];
102104

103105
const { content, headers, code } = await this.handleRequest(
104-
request, parsedUrl, base, requestPath,
106+
base, requestPath, parsedUrl, request,
105107
);
106108
response.writeHead(code || HttpCode.Ok, {
107109
"Cache-Control": "max-age=86400",
@@ -119,13 +121,6 @@ export abstract class Server {
119121
});
120122
}
121123

122-
protected abstract handleRequest(
123-
request: http.IncomingMessage,
124-
parsedUrl: url.UrlWithParsedQuery,
125-
base: string,
126-
requestPath: string,
127-
): Promise<Response>;
128-
129124
public listen(): Promise<string> {
130125
if (!this.listenPromise) {
131126
this.listenPromise = new Promise((resolve, reject) => {
@@ -145,15 +140,35 @@ export abstract class Server {
145140
: address;
146141
return `http://${endpoint}`;
147142
}
143+
144+
protected abstract handleRequest(
145+
base: string,
146+
requestPath: string,
147+
parsedUrl: url.UrlWithParsedQuery,
148+
request: http.IncomingMessage,
149+
): Promise<Response>;
150+
151+
protected async getResource(filePath: string): Promise<Response> {
152+
const content = await util.promisify(fs.readFile)(filePath);
153+
return {
154+
content,
155+
headers: {
156+
"Content-Type": getMediaMime(filePath) || {
157+
".css": "text/css",
158+
".html": "text/html",
159+
".js": "text/javascript",
160+
".json": "application/json",
161+
}[extname(filePath)] || "text/plain",
162+
},
163+
};
164+
}
148165
}
149166

150167
export class MainServer extends Server {
151168
// Used to notify the IPC server that there is a new client.
152169
public readonly _onDidClientConnect = new Emitter<ClientConnectionEvent>();
153170
public readonly onDidClientConnect = this._onDidClientConnect.event;
154171

155-
private readonly rootPath = path.resolve(__dirname, "../../..");
156-
157172
// This is separate instead of just extending this class since we can't
158173
// use properties in the super call. This manages channels.
159174
private readonly ipc = new IPCServer(this.onDidClientConnect);
@@ -212,10 +227,10 @@ export class MainServer extends Server {
212227
}
213228

214229
protected async handleRequest(
215-
request: http.IncomingMessage,
216-
parsedUrl: url.UrlWithParsedQuery,
217230
base: string,
218231
requestPath: string,
232+
parsedUrl: url.UrlWithParsedQuery,
233+
request: http.IncomingMessage,
219234
): Promise<Response> {
220235
switch (base) {
221236
case "/":
@@ -280,21 +295,6 @@ export class MainServer extends Server {
280295
headers: {
281296
"Content-Type": "text/html",
282297
},
283-
}
284-
}
285-
286-
private async getResource(filePath: string): Promise<Response> {
287-
const content = await util.promisify(fs.readFile)(filePath);
288-
return {
289-
content,
290-
headers: {
291-
"Content-Type": getMediaMime(filePath) || {
292-
".css": "text/css",
293-
".html": "text/html",
294-
".js": "text/javascript",
295-
".json": "application/json",
296-
}[extname(filePath)] || "text/plain",
297-
},
298298
};
299299
}
300300

@@ -392,7 +392,19 @@ export class MainServer extends Server {
392392
}
393393

394394
export class WebviewServer extends Server {
395-
protected async handleRequest(): Promise<Response> {
396-
throw new Error("not implemented");
395+
protected async handleRequest(
396+
base: string,
397+
requestPath: string,
398+
): Promise<Response> {
399+
const webviewPath = path.join(
400+
this.rootPath,
401+
"out/vs/workbench/contrib/webview/browser/pre",
402+
);
403+
404+
if (base === "/") {
405+
base = "/index.html";
406+
}
407+
408+
return this.getResource(path.join(webviewPath, base, requestPath));
397409
}
398410
}

0 commit comments

Comments
 (0)