Skip to content

Commit dbc5c06

Browse files
committed
Improve HTTP provider registration
1 parent 4a54e91 commit dbc5c06

File tree

4 files changed

+40
-16
lines changed

4 files changed

+40
-16
lines changed

src/node/api/server.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface LoginPayload extends PostData {
1818
export class ApiHttpProvider extends HttpProvider {
1919
private readonly ws = new ws.Server({ noServer: true })
2020

21-
public constructor(private readonly server: HttpServer, options: HttpProviderOptions) {
21+
public constructor(options: HttpProviderOptions, private readonly server: HttpServer) {
2222
super(options)
2323
}
2424

src/node/entry.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ export interface Args {
2020
}
2121

2222
const main = async (args: Args = {}): Promise<void> => {
23+
const auth = args.auth || AuthType.Password
24+
const originalPassword = auth === AuthType.Password && (process.env.PASSWORD || (await generatePassword()))
25+
2326
// Spawn the main HTTP server.
2427
const options = {
2528
basePath: args["base-path"],
@@ -28,6 +31,8 @@ const main = async (args: Args = {}): Promise<void> => {
2831
host: args.host || (args.auth === AuthType.Password && typeof args.cert !== "undefined" ? "0.0.0.0" : "localhost"),
2932
port: typeof args.port !== "undefined" ? parseInt(args.port, 10) : 8080,
3033
socket: args.socket,
34+
auth,
35+
password: originalPassword ? hash(originalPassword) : undefined,
3136
}
3237
if (!options.cert && typeof options.cert !== "undefined") {
3338
const { cert, certKey } = await generateCertificate()
@@ -37,17 +42,9 @@ const main = async (args: Args = {}): Promise<void> => {
3742
const httpServer = new HttpServer(options)
3843

3944
// Register all the providers.
40-
// TODO: Might be cleaner to be able to register with just the class name
41-
// then let HttpServer instantiate with the common arguments.
42-
const auth = args.auth || AuthType.Password
43-
const originalPassword = auth === AuthType.Password && (process.env.PASSWORD || (await generatePassword()))
44-
const password = originalPassword && hash(originalPassword)
45-
httpServer.registerHttpProvider("/", new MainHttpProvider({ base: "/", auth, password }))
46-
httpServer.registerHttpProvider("/api", new ApiHttpProvider(httpServer, { base: "/", auth, password }))
47-
httpServer.registerHttpProvider(
48-
"/vscode-embed",
49-
new VscodeHttpProvider([], { base: "/vscode-embed", auth, password })
50-
)
45+
httpServer.registerHttpProvider("/", MainHttpProvider)
46+
httpServer.registerHttpProvider("/api", ApiHttpProvider, httpServer)
47+
httpServer.registerHttpProvider("/vscode-embed", VscodeHttpProvider, [])
5148

5249
ipcMain().onDispose(() => httpServer.dispose())
5350

src/node/http.ts

+30-3
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,12 @@ export interface HttpStringFileResponse extends HttpResponse {
8888
}
8989

9090
export interface HttpServerOptions {
91+
readonly auth?: AuthType
9192
readonly basePath?: string
9293
readonly cert?: string
9394
readonly certKey?: string
9495
readonly host?: string
96+
readonly password?: string
9597
readonly port?: number
9698
readonly socket?: string
9799
}
@@ -108,7 +110,7 @@ interface ProviderRoute {
108110
export interface HttpProviderOptions {
109111
readonly base: string
110112
readonly auth: AuthType
111-
readonly password: string | false
113+
readonly password?: string
112114
}
113115

114116
/**
@@ -320,6 +322,14 @@ export class Heart {
320322
}
321323
}
322324

325+
export interface HttpProvider0<T> {
326+
new (options: HttpProviderOptions): T
327+
}
328+
329+
export interface HttpProvider1<A1, T> {
330+
new (options: HttpProviderOptions, a1: A1): T
331+
}
332+
323333
/**
324334
* An HTTP server. Its main role is to route incoming HTTP requests to the
325335
* appropriate provider for that endpoint then write out the response. It also
@@ -372,15 +382,32 @@ export class HttpServer {
372382
/**
373383
* Register a provider for a top-level endpoint.
374384
*/
375-
public registerHttpProvider<T extends HttpProvider>(endpoint: string, provider: T): void {
385+
public registerHttpProvider<T extends HttpProvider>(endpoint: string, provider: HttpProvider0<T>): void
386+
public registerHttpProvider<A1, T extends HttpProvider>(
387+
endpoint: string,
388+
provider: HttpProvider1<A1, T>,
389+
a1: A1
390+
): void
391+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
392+
public registerHttpProvider(endpoint: string, provider: any, a1?: any): void {
376393
endpoint = endpoint.replace(/^\/+|\/+$/g, "")
377394
if (this.providers.has(`/${endpoint}`)) {
378395
throw new Error(`${endpoint} is already registered`)
379396
}
380397
if (/\//.test(endpoint)) {
381398
throw new Error(`Only top-level endpoints are supported (got ${endpoint})`)
382399
}
383-
this.providers.set(`/${endpoint}`, provider)
400+
this.providers.set(
401+
`/${endpoint}`,
402+
new provider(
403+
{
404+
auth: this.options.auth || AuthType.None,
405+
base: endpoint,
406+
password: this.options.password,
407+
},
408+
a1
409+
)
410+
)
384411
}
385412

386413
/**

src/node/vscode/server.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class VscodeHttpProvider extends HttpProvider {
2424
private _vscode?: Promise<cp.ChildProcess>
2525
private workbenchOptions?: WorkbenchOptions
2626

27-
public constructor(private readonly args: string[], options: HttpProviderOptions) {
27+
public constructor(options: HttpProviderOptions, private readonly args: string[]) {
2828
super(options)
2929
this.vsRootPath = path.resolve(this.rootPath, "lib/vscode")
3030
this.serverRootPath = path.join(this.vsRootPath, "out/vs/server")

0 commit comments

Comments
 (0)