All files main.js

99.54% Statements 221/222
96.77% Branches 30/31
100% Functions 4/4
99.54% Lines 221/222

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 2231x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 9x   9x 9x 9x 9x 9x 19x 10x 10x 10x 8x 8x 2x 2x 2x 19x 2x 2x 9x 9x 9x 9x 19x 4x 19x 5x 5x 9x 9x 19x 8x 19x 1x 1x 9x 9x 19x 1x 19x 1x 8x 7x 7x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 15x 15x 15x 8x 8x 15x 1x 15x 6x 6x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 3x 2x 2x 2x 1x 1x 1x 1x 2x 2x 3x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 15x 19x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
'use strict';
 
// MODULES //
 
var http2 = require( 'http2' ); // eslint-disable-line n/no-unsupported-features/node-builtins
var logger = require( 'debug' );
var isFunction = require( '@stdlib/assert/is-function' );
var omit = require( '@stdlib/utils/omit' );
var format = require( '@stdlib/string/format' );
var validate = require( './validate.js' );
var DEFAULTS = require( './defaults.json' );
 
 
// VARIABLES //
 
var debug = logger( '@stdlib/net/http2-secure-server' );
var EXCLUDE_OPTIONS = [
	'port',
	'maxport',
	'hostname',
	'address'
];
 
 
// MAIN //
 
/**
* Returns a function which creates an HTTP/2 server.
*
* ## Notes
*
* -   The function requires that either the PFX option is provided or a cert/key pair is provided.
* -   In addition to options documented below, the function supports any options supported by `http2.createSecureServer`. Which server options are supported depends on the Node.js version.
*
* @param {Options} [options] - server options
* @param {(string|Array<string>|Buffer|Array<Buffer>|Array<Object>)} [options.pfx] - PFX or PKCS12 encoded private key and certificate chain
* @param {(string|Array<string>|Buffer|Array<Buffer>)} [options.cert] - cert chains in PEM format
* @param {(string|Array<string>|Buffer|Array<Buffer>|Array<Object>)} [options.key] - private keys in PEM format
* @param {NonNegativeInteger} [options.port=0] - server port
* @param {NonNegativeInteger} [options.maxport] - max server port
* @param {string} [options.hostname] - server hostname
* @param {string} [options.address="127.0.0.1"] - server address
* @param {Callback} [requestListener] - callback invoked upon receiving an HTTP request
* @throws {TypeError} `requestListener` must be a function
* @throws {TypeError} must provide valid options
* @returns {Function} function which creates an HTTP server
*
* @example
* var resolve = require( 'path' ).resolve;
* var readFileSync = require( '@stdlib/fs/read-file' ).sync;
*
* var opts = {
*     'port': 7331,
*     'address': 'localhost',
*     'cert': readFileSync( resolve( __dirname, '..', 'examples', 'localhost-cert.pem' ) ),
*     'key': readFileSync( resolve( __dirname, '..', 'examples', 'localhost-privkey.pem' ) )
* };
*
* function done( error, server ) {
*     if ( error ) {
*         throw error;
*     }
*     console.log( 'Success!' );
*     server.close();
* }
*
* var http2Server = factory( opts );
* http2Server( done );
*/
function factory() {
	var requestListener;
	var hostname;
	var options;
	var nargs;
	var sopts;
	var opts;
	var port;
	var max;
	var err;
	var flg;
 
	nargs = arguments.length;
	sopts = {};
	opts = {};
	if ( nargs === 1 ) {
		if ( isFunction( arguments[0] ) ) {
			requestListener = arguments[ 0 ];
		} else {
			options = arguments[ 0 ];
			err = validate( opts, options );
			flg = true;
		}
	} else if ( nargs > 1 ) {
		options = arguments[ 0 ];
		requestListener = arguments[ 1 ];
		if ( !isFunction( requestListener ) ) {
			throw new TypeError( format( 'invalid argument. Request listener must be a function. Value: `%s`.', requestListener ) );
		}
		err = validate( opts, options );
		flg = true;
	}
	if ( err ) {
		throw err;
	}
	if ( flg ) {
		// Resolve any server-specific options which should be passed to `http2.createSecureServer`:
		sopts = omit( options, EXCLUDE_OPTIONS );
	}
	if ( opts.port === void 0 ) {
		port = DEFAULTS.port;
	} else {
		port = opts.port;
	}
	debug( 'Server port: %d', port );
 
	if ( opts.maxport === void 0 ) {
		max = port;
	} else {
		max = opts.maxport;
	}
	debug( 'Max server port: %d', max );
 
	if ( opts.hostname ) {
		hostname = opts.hostname;
	} else if ( opts.address ) {
		hostname = opts.address;
	} else {
		hostname = DEFAULTS.address;
	}
	debug( 'Server hostname: %s', hostname );
 
	return http2Server;
 
	/**
	* Creates an HTTP/2 server.
	*
	* @private
	* @param {Callback} done - function to invoke after creating a server
	* @throws {TypeError} must provide a function
	*
	* @example
	* function done( error, server ) {
	*     if ( error ) {
	*         throw error;
	*     }
	*     console.log( 'Success!' );
	*     server.close();
	* }
	* http2Server( done );
	*/
	function http2Server( done ) {
		var server;
 
		if ( !isFunction( done ) ) {
			throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', done ) );
		}
		if ( requestListener ) {
			server = http2.createSecureServer( sopts, requestListener ); // eslint-disable-line n/no-unsupported-features/node-builtins
		} else {
			server = http2.createSecureServer( sopts ); // eslint-disable-line n/no-unsupported-features/node-builtins
		}
		server.on( 'error', errorListener );
		server.once( 'listening', onListen );
 
		debug( 'Attempting to listen on %s:%d.', hostname, port );
		server.listen( port, hostname );
 
		/**
		* Server error event handler.
		*
		* @private
		* @param {Error} error - server error
		* @throws {Error} server error
		*/
		function errorListener( error ) {
			if ( error.code === 'EADDRINUSE' ) {
				debug( 'Server address already in use: %s:%d.', hostname, port );
				port += 1;
				if ( port <= max ) {
					debug( 'Attempting to listen on %s:%d.', hostname, port );
					server.listen( port, hostname );
					return;
				}
			}
			throw error;
		}
 
		/**
		* Callback invoked once a server is listening and ready to handle requests.
		*
		* @private
		*/
		function onListen() {
			var addr = server.address();
			debug( 'HTTP/2 server initialized. Server is listening for requests on %s:%d.', addr.address, addr.port );
			done( null, server );
		}
	}
}
 
 
// EXPORTS //
 
module.exports = factory;