Skip to content

Commit 2996658

Browse files
committed
fix: move server options into factory function
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 1913191 commit 2996658

File tree

4 files changed

+46
-34
lines changed

4 files changed

+46
-34
lines changed

lib/node_modules/@stdlib/net/http-server/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ var httpServer = httpServerFactory();
4040

4141
The function supports the following parameters:
4242

43-
- **options**: options.
44-
- **requestListener**: callback to invoke upon receiving an HTTP request.
43+
- **options**: options (_optional_).
44+
- **requestListener**: callback to invoke upon receiving an HTTP request (_optional_).
4545

4646
To bind a request callback to a server, provide a `requestListener`.
4747

@@ -54,7 +54,7 @@ function requestListener( request, response ) {
5454
var httpServer = httpServerFactory( requestListener );
5555
```
5656

57-
The function accepts the following options:
57+
In addition to the options supported by [`http.createServer`][nodejs-http-create-server], the function accepts the following options:
5858

5959
- **port**: server port. Default: `0` (i.e., randomly assigned).
6060
- **maxport**: max server port when port hunting. Default: `maxport=port`.
@@ -84,7 +84,7 @@ var httpServer = httpServerFactory( opts );
8484

8585
When provided a `maxport` option, a created server will search for the first available `port` on which to listen, starting from `port`.
8686

87-
#### httpServer( \[options,] done )
87+
#### httpServer( done )
8888

8989
Creates an [HTTP][nodejs-http] server.
9090

@@ -104,7 +104,6 @@ httpServer( done );
104104

105105
The function supports the following parameters:
106106

107-
- **options**: server options which are passed directly to [`http.createServer`][nodejs-http-create-server]. Which options are supported depends on the Node.js version. Older Node.js versions (e.g., <= v8.12.0) do not support an options object, and, for those versions, a provided options object is ignored.
108107
- **done**: callback to invoke once a server is listening and ready to handle requests.
109108

110109
</section>
@@ -115,6 +114,7 @@ The function supports the following parameters:
115114

116115
## Notes
117116

117+
- Which server options are supported depends on the Node.js version. Older Node.js versions (e.g., <= v8.12.0) do not support an options object when calling [`http.createServer`][nodejs-http-create-server], and, for those versions, any options specific to Node.js are ignored.
118118
- Port hunting can be useful in a microservice deployment. When a `port` is randomly assigned (`options.port=0`), if a server fails and is restarted, the server is unlikely to bind to its previous `port`. By allowing a constrained search, assuming no lower `ports` within a specified range have freed up in the meantime, the likelihood of listening on the same `port` is increased. A server can typically restart and bind to the same `port` faster than binding to a new `port` and re-registering with a microservice registry, thus minimizing possible service interruption and downtime.
119119

120120
</section>

lib/node_modules/@stdlib/net/http-server/docs/repl.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
{{alias}}( [options,] [requestListener] )
33
Returns a function to create an HTTP server.
44

5+
In addition to options documented below, the function supports any options
6+
supported by `http.createServer`. Which server options are supported depends
7+
on the Node.js version. Older Node.js versions (e.g., <= v8.12.0) do not
8+
support an options object when calling `http.createServer`, and, for those
9+
versions, any options specific to Node.js are ignored.
10+
511
Parameters
612
----------
713
options: Object (optional)
@@ -47,14 +53,11 @@
4753
<Function>
4854

4955

50-
httpServer( [options,] done )
56+
httpServer( done )
5157
Creates an HTTP server.
5258

5359
Parameters
5460
----------
55-
options: Object (optional)
56-
Server options which are passed directly to `http.createServer`.
57-
5861
done: Function
5962
Callback to invoke after creating a server.
6063

lib/node_modules/@stdlib/net/http-server/docs/types/index.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ type Callback = Nullary | Unary | Binary;
104104
*
105105
* @param done - callback to invoke after creating the server
106106
*/
107-
type httpServer = ( done: Callback ) => void; // FIXME: need to allow for an `options` argument with support for built-in server options
107+
type httpServer = ( done: Callback ) => void;
108108

109109
/**
110110
* Returns a function which creates an HTTP server.
@@ -127,6 +127,10 @@ declare function factory( requestListener?: RequestListener ): httpServer;
127127
/**
128128
* Returns a function which creates an HTTP server.
129129
*
130+
* ## Notes
131+
*
132+
* - In addition to options documented below, the function supports any options supported by `http.createServer`. Which server options are supported depends on the Node.js version. Older Node.js versions (e.g., <= v8.12.0) do not support an options object when calling `http.createServer`, and, for those versions, any options specific to Node.js are ignored.
133+
*
130134
* @param options - server options
131135
* @param options.port - server port (default: 0)
132136
* @param options.maxport - max server port
@@ -154,7 +158,7 @@ declare function factory( requestListener?: RequestListener ): httpServer;
154158
* }
155159
* var httpServer = factory( opts, onRequest );
156160
*/
157-
declare function factory( options: Options, requestListener?: RequestListener ): httpServer;
161+
declare function factory<T extends Options>( options: T, requestListener?: RequestListener ): httpServer;
158162

159163

160164
// EXPORTS //

lib/node_modules/@stdlib/net/http-server/lib/main.js

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
var http = require( 'http' );
2424
var logger = require( 'debug' );
2525
var isFunction = require( '@stdlib/assert/is-function' );
26-
var isObject = require( '@stdlib/assert/is-object' );
2726
var NODE_VERSION = require( '@stdlib/process/node-version' );
27+
var omit = require( '@stdlib/utils/omit' );
2828
var format = require( '@stdlib/string/format' );
2929
var validate = require( './validate.js' );
3030
var DEFAULTS = require( './defaults.json' );
@@ -35,12 +35,23 @@ var DEFAULTS = require( './defaults.json' );
3535
var debug = logger( '@stdlib/net/http-server' );
3636
var SUPPORTS_OPTIONS = ( parseInt( NODE_VERSION.split( '.' )[ 0 ], 10 ) >= 8 ); // TODO: this is an imperfect test, as options only added in v8.12.0/v9.6.0
3737

38+
var EXCLUDE_OPTIONS = [
39+
'port',
40+
'maxport',
41+
'hostname',
42+
'address'
43+
];
44+
3845

3946
// MAIN //
4047

4148
/**
4249
* Returns a function which creates an HTTP server.
4350
*
51+
* ## Notes
52+
*
53+
* - In addition to options documented below, the function supports any options supported by `http.createServer`. Which server options are supported depends on the Node.js version. Older Node.js versions (e.g., <= v8.12.0) do not support an options object when calling `http.createServer`, and, for those versions, any options specific to Node.js are ignored.
54+
*
4455
* @param {Options} [options] - server options
4556
* @param {NonNegativeInteger} [options.port=0] - server port
4657
* @param {NonNegativeInteger} [options.maxport] - max server port
@@ -77,19 +88,23 @@ function factory() {
7788
var hostname;
7889
var options;
7990
var nargs;
91+
var sopts;
8092
var opts;
8193
var port;
8294
var max;
8395
var err;
96+
var flg;
8497

8598
nargs = arguments.length;
99+
sopts = {};
86100
opts = {};
87101
if ( nargs === 1 ) {
88102
if ( isFunction( arguments[0] ) ) {
89103
requestListener = arguments[ 0 ];
90104
} else {
91105
options = arguments[ 0 ];
92106
err = validate( opts, options );
107+
flg = true;
93108
}
94109
} else if ( nargs > 1 ) {
95110
options = arguments[ 0 ];
@@ -98,10 +113,15 @@ function factory() {
98113
throw new TypeError( format( 'invalid argument. Request listener must be a function. Value: `%s`.', requestListener ) );
99114
}
100115
err = validate( opts, options );
116+
flg = true;
101117
}
102118
if ( err ) {
103119
throw err;
104120
}
121+
if ( flg ) {
122+
// Resolve any server-specific options which should be passed to `http.createServer`:
123+
sopts = omit( options, EXCLUDE_OPTIONS );
124+
}
105125
if ( opts.port === void 0 ) {
106126
port = DEFAULTS.port;
107127
} else {
@@ -131,7 +151,6 @@ function factory() {
131151
* Creates an HTTP server.
132152
*
133153
* @private
134-
* @param {Options} [options] - server options
135154
* @param {Callback} done - function to invoke after creating a server
136155
* @throws {TypeError} must provide a function
137156
*
@@ -145,34 +164,20 @@ function factory() {
145164
* }
146165
* httpServer( done );
147166
*/
148-
function httpServer( options, done ) {
167+
function httpServer( done ) {
149168
var server;
150-
var nargs;
151-
var opts;
152-
var cb;
153-
154-
nargs = arguments.length;
155-
if ( nargs < 2 ) {
156-
opts = {};
157-
cb = options;
158-
} else {
159-
opts = options;
160-
if ( !isObject( opts ) ) {
161-
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', opts ) );
162-
}
163-
cb = done;
164-
}
165-
if ( !isFunction( cb ) ) {
166-
throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', cb ) );
169+
170+
if ( !isFunction( done ) ) {
171+
throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', done ) );
167172
}
168173
if ( requestListener ) {
169174
if ( SUPPORTS_OPTIONS ) {
170-
server = http.createServer( opts, requestListener );
175+
server = http.createServer( sopts, requestListener );
171176
} else {
172177
server = http.createServer( requestListener );
173178
}
174179
} else if ( SUPPORTS_OPTIONS ) {
175-
server = http.createServer( opts );
180+
server = http.createServer( sopts );
176181
} else {
177182
server = http.createServer();
178183
}
@@ -210,7 +215,7 @@ function factory() {
210215
function onListen() {
211216
var addr = server.address();
212217
debug( 'HTTP server initialized. Server is listening for requests on %s:%d.', addr.address, addr.port );
213-
cb( null, server );
218+
done( null, server );
214219
}
215220
}
216221
}

0 commit comments

Comments
 (0)