mirror of https://git.tuxpa.in/a/code-server.git
Implement webview server
This commit is contained in:
parent
fe1d609d1a
commit
f482087475
70
server.ts
70
server.ts
|
@ -80,6 +80,8 @@ export abstract class Server {
|
||||||
// The underlying web server.
|
// The underlying web server.
|
||||||
protected readonly server: http.Server;
|
protected readonly server: http.Server;
|
||||||
|
|
||||||
|
protected rootPath = path.resolve(__dirname, "../../..");
|
||||||
|
|
||||||
private listenPromise: Promise<string> | undefined;
|
private listenPromise: Promise<string> | undefined;
|
||||||
|
|
||||||
public constructor(private readonly port: number) {
|
public constructor(private readonly port: number) {
|
||||||
|
@ -101,7 +103,7 @@ export abstract class Server {
|
||||||
: ["", "", ""];
|
: ["", "", ""];
|
||||||
|
|
||||||
const { content, headers, code } = await this.handleRequest(
|
const { content, headers, code } = await this.handleRequest(
|
||||||
request, parsedUrl, base, requestPath,
|
base, requestPath, parsedUrl, request,
|
||||||
);
|
);
|
||||||
response.writeHead(code || HttpCode.Ok, {
|
response.writeHead(code || HttpCode.Ok, {
|
||||||
"Cache-Control": "max-age=86400",
|
"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> {
|
public listen(): Promise<string> {
|
||||||
if (!this.listenPromise) {
|
if (!this.listenPromise) {
|
||||||
this.listenPromise = new Promise((resolve, reject) => {
|
this.listenPromise = new Promise((resolve, reject) => {
|
||||||
|
@ -145,6 +140,28 @@ export abstract class Server {
|
||||||
: address;
|
: address;
|
||||||
return `http://${endpoint}`;
|
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 {
|
export class MainServer extends Server {
|
||||||
|
@ -152,8 +169,6 @@ export class MainServer extends Server {
|
||||||
public readonly _onDidClientConnect = new Emitter<ClientConnectionEvent>();
|
public readonly _onDidClientConnect = new Emitter<ClientConnectionEvent>();
|
||||||
public readonly onDidClientConnect = this._onDidClientConnect.event;
|
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
|
// This is separate instead of just extending this class since we can't
|
||||||
// use properties in the super call. This manages channels.
|
// use properties in the super call. This manages channels.
|
||||||
private readonly ipc = new IPCServer(this.onDidClientConnect);
|
private readonly ipc = new IPCServer(this.onDidClientConnect);
|
||||||
|
@ -212,10 +227,10 @@ export class MainServer extends Server {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async handleRequest(
|
protected async handleRequest(
|
||||||
request: http.IncomingMessage,
|
|
||||||
parsedUrl: url.UrlWithParsedQuery,
|
|
||||||
base: string,
|
base: string,
|
||||||
requestPath: string,
|
requestPath: string,
|
||||||
|
parsedUrl: url.UrlWithParsedQuery,
|
||||||
|
request: http.IncomingMessage,
|
||||||
): Promise<Response> {
|
): Promise<Response> {
|
||||||
switch (base) {
|
switch (base) {
|
||||||
case "/":
|
case "/":
|
||||||
|
@ -280,21 +295,6 @@ export class MainServer extends Server {
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "text/html",
|
"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 {
|
export class WebviewServer extends Server {
|
||||||
protected async handleRequest(): Promise<Response> {
|
protected async handleRequest(
|
||||||
throw new Error("not implemented");
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue