@httptoolkit/httpolyglot
Advanced tools
Comparing version 2.0.1 to 2.1.0
/// <reference types="node" /> | ||
import * as net from 'net'; | ||
import * as tls from 'tls'; | ||
import * as http from 'http'; | ||
@@ -14,8 +15,39 @@ import * as https from 'https'; | ||
private _tlsServer; | ||
/** | ||
* 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. | ||
*/ | ||
constructor(requestListener: http.RequestListener); | ||
constructor(tlsConfig: https.ServerOptions, requestListener: http.RequestListener); | ||
/** | ||
* Call with a full TLS configuration to create a TLS+HTTP+HTTP/2 server, which can | ||
* support all protocols on the same port. | ||
*/ | ||
constructor(config: https.ServerOptions, requestListener: http.RequestListener); | ||
/** | ||
* Pass an existing TLS server, instead of TLS configuration, to create a TLS+HTTP+HTTP/2 | ||
* server. All incoming TLS requests 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. | ||
*/ | ||
constructor(tlsServer: tls.Server, requestListener: http.RequestListener); | ||
private connectionListener; | ||
private tlsListener; | ||
private http2Listener; | ||
} | ||
/** | ||
* 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 instance with a full TLS configuration to create a TLS+HTTP+HTTP/2 server, which can | ||
* support all protocols on the same port. | ||
*/ | ||
export declare function createServer(tlsConfig: https.ServerOptions, requestListener: http.RequestListener): Server; | ||
/** | ||
* Create an instance around an existing TLS server, instead of TLS configuration, to create a | ||
* TLS+HTTP+HTTP/2 server with custom TLS handling. All incoming TLS requests 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. | ||
*/ | ||
export declare function createServer(tlsServer: tls.Server, requestListener: http.RequestListener): Server; |
@@ -15,3 +15,3 @@ "use strict"; | ||
class Server extends net.Server { | ||
constructor(configOrListener, listener) { | ||
constructor(configOrServerOrListener, listener) { | ||
// We just act as a plain TCP server, accepting and examing | ||
@@ -21,9 +21,14 @@ // each connection, then passing it to the right subserver. | ||
let tlsConfig; | ||
let tlsServer; | ||
let requestListener; | ||
if (typeof configOrListener === 'function') { | ||
requestListener = configOrListener; | ||
if (typeof configOrServerOrListener === 'function') { | ||
requestListener = configOrServerOrListener; | ||
tlsConfig = undefined; | ||
} | ||
else if (configOrServerOrListener instanceof tls.Server) { | ||
tlsServer = configOrServerOrListener; | ||
requestListener = listener; | ||
} | ||
else { | ||
tlsConfig = configOrListener; | ||
tlsConfig = configOrServerOrListener; | ||
requestListener = listener; | ||
@@ -37,13 +42,12 @@ } | ||
this._http2Server = http2.createServer({}, boundListener); | ||
if (typeof tlsConfig === 'object') { | ||
if (tlsServer) { | ||
// If we've been given a preconfigured TLS server, we use that directly, and | ||
// subscribe to connections there | ||
this._tlsServer = tlsServer; | ||
this._tlsServer.on('secureConnection', this.tlsListener.bind(this)); | ||
} | ||
else if (typeof tlsConfig === 'object') { | ||
// If we have TLS config, create a TLS server, which will pass sockets to | ||
// the relevant subserver once the TLS connection is set up. | ||
this._tlsServer = new tls.Server(tlsConfig, (tlsSocket) => { | ||
if (tlsSocket.alpnProtocol === false || tlsSocket.alpnProtocol === 'http/1.1') { | ||
this._httpServer.emit('connection', tlsSocket); | ||
} | ||
else { | ||
this._http2Server.emit('connection', tlsSocket); | ||
} | ||
}); | ||
this._tlsServer = new tls.Server(tlsConfig, this.tlsListener.bind(this)); | ||
} | ||
@@ -106,2 +110,10 @@ else { | ||
} | ||
tlsListener(tlsSocket) { | ||
if (tlsSocket.alpnProtocol === false || tlsSocket.alpnProtocol === 'http/1.1') { | ||
this._httpServer.emit('connection', tlsSocket); | ||
} | ||
else { | ||
this._http2Server.emit('connection', tlsSocket); | ||
} | ||
} | ||
http2Listener(socket, pastData) { | ||
@@ -152,4 +164,4 @@ const h1Server = this._httpServer; | ||
exports.Server = Server; | ||
function createServer(configOrListener, listener) { | ||
return new Server(configOrListener, listener); | ||
function createServer(configOrServerOrListener, listener) { | ||
return new Server(configOrServerOrListener, listener); | ||
} | ||
@@ -156,0 +168,0 @@ exports.createServer = createServer; |
{ | ||
"name": "@httptoolkit/httpolyglot", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"author": "Tim Perry <pimterry@gmail.com>", | ||
@@ -5,0 +5,0 @@ "description": "Serve http and https connections over the same port with node.js", |
@@ -31,5 +31,26 @@ import * as net from 'net'; | ||
/** | ||
* 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. | ||
*/ | ||
constructor(requestListener: http.RequestListener); | ||
constructor(tlsConfig: https.ServerOptions, requestListener: http.RequestListener); | ||
constructor(configOrListener: https.ServerOptions | http.RequestListener, listener?: http.RequestListener) { | ||
/** | ||
* Call with a full TLS configuration to create a TLS+HTTP+HTTP/2 server, which can | ||
* support all protocols on the same port. | ||
*/ | ||
constructor(config: https.ServerOptions, requestListener: http.RequestListener); | ||
/** | ||
* Pass an existing TLS server, instead of TLS configuration, to create a TLS+HTTP+HTTP/2 | ||
* server. All incoming TLS requests 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. | ||
*/ | ||
constructor(tlsServer: tls.Server, requestListener: http.RequestListener); | ||
constructor( | ||
configOrServerOrListener: | ||
| https.ServerOptions | ||
| tls.Server | ||
| http.RequestListener, | ||
listener?: http.RequestListener | ||
) { | ||
// We just act as a plain TCP server, accepting and examing | ||
@@ -40,9 +61,13 @@ // each connection, then passing it to the right subserver. | ||
let tlsConfig: https.ServerOptions | undefined; | ||
let tlsServer: tls.Server | undefined; | ||
let requestListener: http.RequestListener; | ||
if (typeof configOrListener === 'function') { | ||
requestListener = configOrListener; | ||
if (typeof configOrServerOrListener === 'function') { | ||
requestListener = configOrServerOrListener; | ||
tlsConfig = undefined; | ||
} else if (configOrServerOrListener instanceof tls.Server) { | ||
tlsServer = configOrServerOrListener; | ||
requestListener = listener!; | ||
} else { | ||
tlsConfig = configOrListener; | ||
tlsConfig = configOrServerOrListener; | ||
requestListener = listener!; | ||
@@ -59,12 +84,11 @@ } | ||
if (typeof tlsConfig === 'object') { | ||
if (tlsServer) { | ||
// If we've been given a preconfigured TLS server, we use that directly, and | ||
// subscribe to connections there | ||
this._tlsServer = tlsServer; | ||
this._tlsServer.on('secureConnection', this.tlsListener.bind(this)); | ||
} else if (typeof tlsConfig === 'object') { | ||
// If we have TLS config, create a TLS server, which will pass sockets to | ||
// the relevant subserver once the TLS connection is set up. | ||
this._tlsServer = new tls.Server(tlsConfig, (tlsSocket) => { | ||
if (tlsSocket.alpnProtocol === false || tlsSocket.alpnProtocol === 'http/1.1') { | ||
this._httpServer.emit('connection', tlsSocket); | ||
} else { | ||
this._http2Server.emit('connection', tlsSocket); | ||
} | ||
}); | ||
this._tlsServer = new tls.Server(tlsConfig, this.tlsListener.bind(this)); | ||
} else { | ||
@@ -132,2 +156,10 @@ // Fake server that rejects all connections: | ||
private tlsListener(tlsSocket: tls.TLSSocket) { | ||
if (tlsSocket.alpnProtocol === false || tlsSocket.alpnProtocol === 'http/1.1') { | ||
this._httpServer.emit('connection', tlsSocket); | ||
} else { | ||
this._http2Server.emit('connection', tlsSocket); | ||
} | ||
} | ||
private http2Listener(socket: net.Socket, pastData?: Buffer) { | ||
@@ -180,6 +212,26 @@ const h1Server = this._httpServer; | ||
/** | ||
* 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 function createServer(requestListener: http.RequestListener): Server; | ||
/** | ||
* Create an instance with a full TLS configuration to create a TLS+HTTP+HTTP/2 server, which can | ||
* support all protocols on the same port. | ||
*/ | ||
export function createServer(tlsConfig: https.ServerOptions, requestListener: http.RequestListener): Server; | ||
export function createServer(configOrListener: https.ServerOptions | http.RequestListener, listener?: http.RequestListener) { | ||
return new Server(configOrListener as any, listener as any); | ||
/** | ||
* Create an instance around an existing TLS server, instead of TLS configuration, to create a | ||
* TLS+HTTP+HTTP/2 server with custom TLS handling. All incoming TLS requests 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. | ||
*/ | ||
export function createServer(tlsServer: tls.Server, requestListener: http.RequestListener): Server; | ||
export function createServer(configOrServerOrListener: | ||
| https.ServerOptions | ||
| http.RequestListener | ||
| tls.Server, | ||
listener?: http.RequestListener | ||
) { | ||
return new Server(configOrServerOrListener as any, listener as any); | ||
}; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28923
418