diff options
author | Leo Kettmeir <crowlkats@toaxl.com> | 2023-06-13 04:15:08 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-13 04:15:08 +0200 |
commit | b4ae37a617e5e2a1c248d0d1ac66dcead11e04cd (patch) | |
tree | 7d604b5dcc37f054e1de37eb50afe1cb3571a9de /ext/node/polyfills/http.ts | |
parent | d2c638464d108b945440429cfba0594c20089d76 (diff) |
feat(node): HTTPS server (#19362)
Diffstat (limited to 'ext/node/polyfills/http.ts')
-rw-r--r-- | ext/node/polyfills/http.ts | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/ext/node/polyfills/http.ts b/ext/node/polyfills/http.ts index 697de6414..a207f57ce 100644 --- a/ext/node/polyfills/http.ts +++ b/ext/node/polyfills/http.ts @@ -18,6 +18,7 @@ import { nextTick } from "ext:deno_node/_next_tick.ts"; import { validateBoolean, validateInteger, + validateObject, validatePort, } from "ext:deno_node/internal/validators.mjs"; import { @@ -1443,16 +1444,16 @@ export class IncomingMessageForServer extends NodeReadable { } } -type ServerHandler = ( +export type ServerHandler = ( req: IncomingMessageForServer, res: ServerResponse, ) => void; -export function Server(handler?: ServerHandler): ServerImpl { - return new ServerImpl(handler); +export function Server(opts, requestListener?: ServerHandler): ServerImpl { + return new ServerImpl(opts, requestListener); } -class ServerImpl extends EventEmitter { +export class ServerImpl extends EventEmitter { #httpConnections: Set<Deno.HttpConn> = new Set(); #listener?: Deno.Listener; @@ -1464,12 +1465,24 @@ class ServerImpl extends EventEmitter { #servePromise: Deferred<void>; listening = false; - constructor(handler?: ServerHandler) { + constructor(opts, requestListener?: ServerHandler) { super(); + + if (typeof opts === "function") { + requestListener = opts; + opts = kEmptyObject; + } else if (opts == null) { + opts = kEmptyObject; + } else { + validateObject(opts, "options"); + } + + this._opts = opts; + this.#servePromise = deferred(); this.#servePromise.then(() => this.emit("close")); - if (handler !== undefined) { - this.on("request", handler); + if (requestListener !== undefined) { + this.on("request", requestListener); } } @@ -1498,12 +1511,12 @@ class ServerImpl extends EventEmitter { port, } as Deno.NetAddr; this.listening = true; - nextTick(() => this.#serve()); + nextTick(() => this._serve()); return this; } - #serve() { + _serve() { const ac = new AbortController(); const handler = (request: Request, info: Deno.ServeHandlerInfo) => { const req = new IncomingMessageForServer(request, info.remoteAddr); @@ -1536,6 +1549,7 @@ class ServerImpl extends EventEmitter { this.#addr!.port = port; this.emit("listening"); }, + ...this._additionalServeOptions?.(), }, ); if (this.#unref) { @@ -1598,8 +1612,8 @@ class ServerImpl extends EventEmitter { Server.prototype = ServerImpl.prototype; -export function createServer(handler?: ServerHandler) { - return Server(handler); +export function createServer(opts, requestListener?: ServerHandler) { + return Server(opts, requestListener); } /** Makes an HTTP request. */ |