@httptoolkit/httpolyglot
Version:
Serve http and https connections over the same port with node.js
61 lines (60 loc) • 2.82 kB
TypeScript
/// <reference types="node" />
/// <reference types="node" />
/// <reference types="node" />
/// <reference types="node" />
import * as net from 'net';
import * as tls from 'tls';
import * as http from 'http';
import * as https from 'https';
export interface HttpolyglotOptions {
/**
* Enable support for incoming TLS connections. If a TLS configuration is provided, this will
* be used to instantiate a TLS server to handle the connections. If a TLS server is provided,
* then all incoming TLS connections will be emitted as 'connection' events on the given
* TLS server, and all 'secureConnection' events coming from the TLS server will be handled
* according to the connection type detected on that socket.
*/
tls?: https.ServerOptions | tls.Server;
/**
* A SOCKS server instance. This will allow incoming SOCKS connections, which will
* be emitted as 'connection' events on this server.
*/
socks?: net.Server;
/**
* A custom handler for unknown protocols. If provided, any unrecognized protocol sockets will
* be emitted on this server as 'connection' events. If not provided, the default behavior is to
* pass the socket to the HTTP server, which will typically reject the connection as
* unparseable, with a 400 response.
*/
unknownProtocol?: net.Server;
}
declare class Server extends net.Server {
private _httpServer;
private _http2Server;
private _tlsServer;
private _socksHandler;
private _unknownProtocolHandler;
constructor(config: HttpolyglotOptions, requestListener: http.RequestListener);
private connectionListener;
private tlsListener;
private http2Listener;
}
export type { Server };
/**
* Create an Httpolyglot instance with just a request listener to support plain-text
* HTTP & HTTP/2 on the same port, with incoming TLS connections being closed immediately.
*/
export declare function createServer(requestListener: http.RequestListener): Server;
/**
* Create an Httpolyglot server with advanced configuration, to enable TLS and/or SOCKS
* connections containing HTTP, accepting all protocols on the same port.
*
* The options can contain:
* - `tls`: A TLS configuration object, or an existing TLS server. If a TLS server is provided,
* all incoming TLS connections will be emitted as 'connection' events on the given TLS server,
* and all 'secureConnection' events coming from the TLS server will be handled according to
* the connection type (HTTP/1 or HTTP/2) detected on that socket.
* - `socks`: A SOCKS server instance. This will allow incoming SOCKS connections, which will
* be emitted as 'connection' events on this server.
*/
export declare function createServer(config: HttpolyglotOptions, requestListener: http.RequestListener): Server;