@stdlib/net-http-server 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +1 -1
- package/README.md +36 -24
- package/dist/index.d.ts +2 -2
- package/dist/index.js +5 -5
- package/dist/index.js.map +3 -3
- package/docs/types/index.d.ts +12 -8
- package/lib/defaults.json +2 -2
- package/lib/index.js +9 -9
- package/lib/main.js +43 -16
- package/package.json +4 -2
package/NOTICE
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Copyright (c) 2016-
|
|
1
|
+
Copyright (c) 2016-2026 The Stdlib Authors.
|
package/README.md
CHANGED
|
@@ -33,7 +33,7 @@ limitations under the License.
|
|
|
33
33
|
|
|
34
34
|
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->
|
|
35
35
|
|
|
36
|
-
> [HTTP][http] server.
|
|
36
|
+
> [HTTP][nodejs-http] server.
|
|
37
37
|
|
|
38
38
|
<section class="installation">
|
|
39
39
|
|
|
@@ -50,17 +50,22 @@ npm install @stdlib/net-http-server
|
|
|
50
50
|
## Usage
|
|
51
51
|
|
|
52
52
|
```javascript
|
|
53
|
-
var
|
|
53
|
+
var httpServerFactory = require( '@stdlib/net-http-server' );
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
-
####
|
|
56
|
+
#### httpServerFactory( \[options,] \[requestListener] )
|
|
57
57
|
|
|
58
|
-
Returns a function to create an [HTTP][http] server.
|
|
58
|
+
Returns a function to create an [HTTP][nodejs-http] server.
|
|
59
59
|
|
|
60
60
|
```javascript
|
|
61
|
-
var
|
|
61
|
+
var httpServer = httpServerFactory();
|
|
62
62
|
```
|
|
63
63
|
|
|
64
|
+
The function supports the following parameters:
|
|
65
|
+
|
|
66
|
+
- **options**: options (_optional_).
|
|
67
|
+
- **requestListener**: callback to invoke upon receiving an HTTP request (_optional_).
|
|
68
|
+
|
|
64
69
|
To bind a request callback to a server, provide a `requestListener`.
|
|
65
70
|
|
|
66
71
|
```javascript
|
|
@@ -69,17 +74,17 @@ function requestListener( request, response ) {
|
|
|
69
74
|
response.end( 'OK' );
|
|
70
75
|
}
|
|
71
76
|
|
|
72
|
-
var
|
|
77
|
+
var httpServer = httpServerFactory( requestListener );
|
|
73
78
|
```
|
|
74
79
|
|
|
75
|
-
|
|
80
|
+
In addition to the options supported by [`http.createServer`][nodejs-http-create-server], the function accepts the following options:
|
|
76
81
|
|
|
77
82
|
- **port**: server port. Default: `0` (i.e., randomly assigned).
|
|
78
83
|
- **maxport**: max server port when port hunting. Default: `maxport=port`.
|
|
79
84
|
- **hostname**: server hostname.
|
|
80
85
|
- **address**: server address. Default: `127.0.0.1`.
|
|
81
86
|
|
|
82
|
-
To specify server options, provide an
|
|
87
|
+
To specify server options, provide an options object.
|
|
83
88
|
|
|
84
89
|
```javascript
|
|
85
90
|
var opts = {
|
|
@@ -87,7 +92,7 @@ var opts = {
|
|
|
87
92
|
'address': '0.0.0.0'
|
|
88
93
|
};
|
|
89
94
|
|
|
90
|
-
var
|
|
95
|
+
var httpServer = httpServerFactory( opts );
|
|
91
96
|
```
|
|
92
97
|
|
|
93
98
|
To specify a range of permissible ports, set the `maxport` option.
|
|
@@ -97,14 +102,14 @@ var opts = {
|
|
|
97
102
|
'maxport': 9999
|
|
98
103
|
};
|
|
99
104
|
|
|
100
|
-
var
|
|
105
|
+
var httpServer = httpServerFactory( opts );
|
|
101
106
|
```
|
|
102
107
|
|
|
103
108
|
When provided a `maxport` option, a created server will search for the first available `port` on which to listen, starting from `port`.
|
|
104
109
|
|
|
105
|
-
####
|
|
110
|
+
#### httpServer( done )
|
|
106
111
|
|
|
107
|
-
Creates an [HTTP][http] server.
|
|
112
|
+
Creates an [HTTP][nodejs-http] server.
|
|
108
113
|
|
|
109
114
|
```javascript
|
|
110
115
|
function done( error, server ) {
|
|
@@ -115,11 +120,15 @@ function done( error, server ) {
|
|
|
115
120
|
server.close();
|
|
116
121
|
}
|
|
117
122
|
|
|
118
|
-
var
|
|
123
|
+
var httpServer = httpServerFactory();
|
|
119
124
|
|
|
120
|
-
|
|
125
|
+
httpServer( done );
|
|
121
126
|
```
|
|
122
127
|
|
|
128
|
+
The function supports the following parameters:
|
|
129
|
+
|
|
130
|
+
- **done**: callback to invoke once a server is listening and ready to handle requests.
|
|
131
|
+
|
|
123
132
|
</section>
|
|
124
133
|
|
|
125
134
|
<!-- /.usage -->
|
|
@@ -128,7 +137,8 @@ createServer( done );
|
|
|
128
137
|
|
|
129
138
|
## Notes
|
|
130
139
|
|
|
131
|
-
-
|
|
140
|
+
- 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 supported by [`http.createServer`][nodejs-http-create-server] in later Node.js versions are ignored.
|
|
141
|
+
- 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.
|
|
132
142
|
|
|
133
143
|
</section>
|
|
134
144
|
|
|
@@ -145,7 +155,7 @@ createServer( done );
|
|
|
145
155
|
```javascript
|
|
146
156
|
var proc = require( 'process' );
|
|
147
157
|
var http = require( 'http' );
|
|
148
|
-
var
|
|
158
|
+
var httpServerFactory = require( '@stdlib/net-http-server' );
|
|
149
159
|
|
|
150
160
|
function done( error, server ) {
|
|
151
161
|
if ( error ) {
|
|
@@ -172,10 +182,10 @@ var opts = {
|
|
|
172
182
|
};
|
|
173
183
|
|
|
174
184
|
// Create a function for creating an HTTP server...
|
|
175
|
-
var
|
|
185
|
+
var httpServer = httpServerFactory( opts, onRequest );
|
|
176
186
|
|
|
177
187
|
// Create a server:
|
|
178
|
-
|
|
188
|
+
httpServer( done );
|
|
179
189
|
```
|
|
180
190
|
|
|
181
191
|
</section>
|
|
@@ -216,7 +226,7 @@ See [LICENSE][stdlib-license].
|
|
|
216
226
|
|
|
217
227
|
## Copyright
|
|
218
228
|
|
|
219
|
-
Copyright © 2016-
|
|
229
|
+
Copyright © 2016-2026. The Stdlib [Authors][stdlib-authors].
|
|
220
230
|
|
|
221
231
|
</section>
|
|
222
232
|
|
|
@@ -229,8 +239,8 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
|
|
|
229
239
|
[npm-image]: http://img.shields.io/npm/v/@stdlib/net-http-server.svg
|
|
230
240
|
[npm-url]: https://npmjs.org/package/@stdlib/net-http-server
|
|
231
241
|
|
|
232
|
-
[test-image]: https://github.com/stdlib-js/net-http-server/actions/workflows/test.yml/badge.svg?branch=v0.2.
|
|
233
|
-
[test-url]: https://github.com/stdlib-js/net-http-server/actions/workflows/test.yml?query=branch:v0.2.
|
|
242
|
+
[test-image]: https://github.com/stdlib-js/net-http-server/actions/workflows/test.yml/badge.svg?branch=v0.2.3
|
|
243
|
+
[test-url]: https://github.com/stdlib-js/net-http-server/actions/workflows/test.yml?query=branch:v0.2.3
|
|
234
244
|
|
|
235
245
|
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/net-http-server/main.svg
|
|
236
246
|
[coverage-url]: https://codecov.io/github/stdlib-js/net-http-server?branch=main
|
|
@@ -242,8 +252,8 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
|
|
|
242
252
|
|
|
243
253
|
-->
|
|
244
254
|
|
|
245
|
-
[chat-image]: https://img.shields.io/
|
|
246
|
-
[chat-url]: https://
|
|
255
|
+
[chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
|
|
256
|
+
[chat-url]: https://stdlib.zulipchat.com
|
|
247
257
|
|
|
248
258
|
[stdlib]: https://github.com/stdlib-js/stdlib
|
|
249
259
|
|
|
@@ -262,7 +272,9 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
|
|
|
262
272
|
|
|
263
273
|
[stdlib-license]: https://raw.githubusercontent.com/stdlib-js/net-http-server/main/LICENSE
|
|
264
274
|
|
|
265
|
-
[http]: https://nodejs.org/api/http.html
|
|
275
|
+
[nodejs-http]: https://nodejs.org/api/http.html
|
|
276
|
+
|
|
277
|
+
[nodejs-http-create-server]: https://nodejs.org/api/http.html#httpcreateserveroptions-requestlistener
|
|
266
278
|
|
|
267
279
|
</section>
|
|
268
280
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
/// <reference path="../docs/types/index.d.ts" />
|
|
2
|
-
import
|
|
3
|
-
export =
|
|
2
|
+
import factory from '../docs/types/index';
|
|
3
|
+
export = factory;
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
var
|
|
3
|
-
});var
|
|
4
|
-
var
|
|
5
|
-
});var I
|
|
1
|
+
"use strict";var h=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var O=h(function(M,E){
|
|
2
|
+
var S=require('@stdlib/assert-is-nonnegative-integer/dist').isPrimitive,x=require('@stdlib/assert-is-string/dist').isPrimitive,R=require('@stdlib/assert-is-plain-object/dist'),l=require('@stdlib/assert-has-own-property/dist'),u=require('@stdlib/error-tools-fmtprodmsg/dist');function U(e,r){return R(r)?l(r,"port")&&(e.port=r.port,!S(e.port))?new TypeError(u('0kN2t',"port",e.port)):l(r,"maxport")&&(e.maxport=r.maxport,!S(e.maxport))?new TypeError(u('0kN2t',"maxport",e.maxport)):l(r,"hostname")&&(e.hostname=r.hostname,!x(e.hostname))?new TypeError(u('0kN2W',"hostname",e.hostname)):l(r,"address")&&(e.address=r.address,!x(e.address))?new TypeError(u('0kN2W',"address",e.address)):null:new TypeError(u('0kN2V',r));}E.exports=U
|
|
3
|
+
});var T=h(function(X,V){V.exports={port:0,address:"127.0.0.1"}});var I=h(function(B,N){
|
|
4
|
+
var p=require("http"),_=require("debug"),q=require('@stdlib/assert-is-function/dist'),j=require('@stdlib/process-node-version/dist'),C=require('@stdlib/utils-omit/dist'),b=require('@stdlib/error-tools-fmtprodmsg/dist'),w=O(),y=T(),n=_("@stdlib/net-http-server"),P=parseInt(j.split(".")[0],10)>=8,F=["port","maxport","hostname","address"];function k(){var e,r,s,f,v,t,a,d,m,g;if(f=arguments.length,v={},t={},f===1)q(arguments[0])?e=arguments[0]:(s=arguments[0],m=w(t,s),g=!0);else if(f>1){if(s=arguments[0],e=arguments[1],!q(e))throw new TypeError(b('0kN5w',e));m=w(t,s),g=!0}if(m)throw m;return g&&(v=C(s,F)),t.port===void 0?a=y.port:a=t.port,n("Server port: %d",a),t.maxport===void 0?d=a:d=t.maxport,n("Max server port: %d",d),t.hostname?r=t.hostname:t.address?r=t.address:r=y.address,n("Server hostname: %s",r),D;function D(c){var i;if(!q(c))throw new TypeError(b('0kN2b',c));e?P?i=p.createServer(v,e):i=p.createServer(e):P?i=p.createServer(v):i=p.createServer(),i.on("error",L),i.once("listening",A),n("Attempting to listen on %s:%d.",r,a),i.listen(a,r);function L(o){if(o.code==="EADDRINUSE"&&(n("Server address already in use: %s:%d.",r,a),a+=1,a<=d)){n("Attempting to listen on %s:%d.",r,a),i.listen(a,r);return}throw o}function A(){var o=i.address();n("HTTP server initialized. Server is listening for requests on %s:%d.",o.address,o.port),c(null,i)}}}N.exports=k
|
|
5
|
+
});var z=I();module.exports=z;
|
|
6
6
|
/** @license Apache-2.0 */
|
|
7
7
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../lib/validate.js", "../lib/defaults.json", "../lib/main.js", "../lib/index.js"],
|
|
4
|
-
"sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isNonNegativeInteger = require( '@stdlib/assert-is-nonnegative-integer' ).isPrimitive;\nvar isString = require( '@stdlib/assert-is-string' ).isPrimitive;\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {NonNegativeInteger} [options.port] - server port\n* @param {NonNegativeInteger} [options.maxport] - max server port\n* @param {string} [options.hostname] - server hostname\n* @param {string} [options.address] - server address\n* @returns {(Error|null)} error or null\n*\n* @example\n* var options = {\n* 'port': 7331,\n* 'address': '127.0.0.1'\n* };\n* var opts = {};\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'port' ) ) {\n\t\topts.port = options.port;\n\t\tif ( !isNonNegativeInteger( opts.port ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a nonnegative integer. Option: `%s`.', 'port', opts.port ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'maxport' ) ) {\n\t\topts.maxport = options.maxport;\n\t\tif ( !isNonNegativeInteger( opts.maxport ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a nonnegative integer. Option: `%s`.', 'maxport', opts.maxport ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'hostname' ) ) {\n\t\topts.hostname = options.hostname;\n\t\tif ( !isString( opts.hostname ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a string. Option: `%s`.', 'hostname', opts.hostname ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'address' ) ) {\n\t\topts.address = options.address;\n\t\tif ( !isString( opts.address ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a string. Option: `%s`.', 'address', opts.address ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "{\n\t\"port\": 0,\n\t\"address\": \"127.0.0.1\"\n}\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar http = require( 'http' );\nvar logger = require( 'debug' );\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar format = require( '@stdlib/string-format' );\nvar validate = require( './validate.js' );\nvar DEFAULTS = require( './defaults.json' );\n\n\n// VARIABLES //\n\nvar debug = logger( '@stdlib/net-http-server' );\n\n\n// MAIN //\n\n/**\n* Returns a function which creates an HTTP server.\n*\n* @param {Options} [options] - server options\n* @param {NonNegativeInteger} [options.port=0] - server port\n* @param {NonNegativeInteger} [options.maxport] - max server port\n* @param {string} [options.hostname] - server hostname\n* @param {string} [options.address=\"127.0.0.1\"] - server address\n* @param {Callback} [requestListener] - callback invoked upon receiving an HTTP request\n* @throws {TypeError} `requestListener` must be a function\n* @throws {TypeError} must provide valid options\n* @returns {Function} function which creates an HTTP server\n*\n* @example\n* var createServer = httpServer();\n*\n* @example\n* var opts = {\n* 'port': 7331,\n* 'address': '0.0.0.0'\n* };\n* var createServer = httpServer( opts );\n*\n* @example\n* var opts = {\n* 'port': 7331,\n* 'address': '0.0.0.0'\n* };\n* function onRequest( request, response ) {\n* console.log( request.url );\n* response.end( 'OK' );\n* }\n* var createServer = httpServer( opts, onRequest );\n*/\nfunction httpServer() {\n\tvar requestListener;\n\tvar hostname;\n\tvar options;\n\tvar nargs;\n\tvar opts;\n\tvar port;\n\tvar max;\n\tvar err;\n\n\tnargs = arguments.length;\n\topts = {};\n\tif ( nargs === 1 ) {\n\t\tif ( isFunction( arguments[0] )) {\n\t\t\trequestListener = arguments[ 0 ];\n\t\t} else {\n\t\t\toptions = arguments[ 0 ];\n\t\t\terr = validate( opts, options );\n\t\t}\n\t}\n\telse if ( nargs > 1 ) {\n\t\toptions = arguments[ 0 ];\n\t\trequestListener = arguments[ 1 ];\n\t\tif ( !isFunction( requestListener ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Request listener must be a function. Value: `%s`.', requestListener ) );\n\t\t}\n\t\terr = validate( opts, options );\n\t}\n\tif ( err ) {\n\t\tthrow err;\n\t}\n\tif ( opts.port === void 0 ) {\n\t\tport = DEFAULTS.port;\n\t} else {\n\t\tport = opts.port;\n\t}\n\tdebug( 'Server port: %d', port );\n\n\tif ( opts.maxport === void 0 ) {\n\t\tmax = port;\n\t} else {\n\t\tmax = opts.maxport;\n\t}\n\tdebug( 'Max server port: %d', max );\n\n\tif ( opts.hostname ) {\n\t\thostname = opts.hostname;\n\t}\n\telse if ( opts.address ) {\n\t\thostname = opts.address;\n\t}\n\telse {\n\t\thostname = DEFAULTS.address;\n\t}\n\tdebug( 'Server hostname: %s', hostname );\n\n\treturn createServer;\n\n\t/**\n\t* Creates an HTTP server.\n\t*\n\t* @private\n\t* @param {Callback} done - function to invoke after creating a server\n\t* @throws {TypeError} must provide a function\n\t*\n\t* @example\n\t* function done( error, server ) {\n\t* if ( error ) {\n\t* throw error;\n\t* }\n\t* console.log( 'Success!' );\n\t* server.close();\n\t* }\n\t* createServer( done );\n\t*/\n\tfunction createServer( done ) {\n\t\tvar server;\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\tif ( requestListener ) {\n\t\t\tserver = http.createServer( requestListener );\n\t\t} else {\n\t\t\tserver = http.createServer();\n\t\t}\n\t\tserver.on( 'error', errorListener );\n\t\tserver.once( 'listening', onListen );\n\n\t\tdebug( 'Attempting to listen on %s:%d.', hostname, port );\n\t\tserver.listen( port, hostname );\n\n\t\t/**\n\t\t* Server error event handler.\n\t\t*\n\t\t* @private\n\t\t* @param {Error} error - server error\n\t\t* @throws {Error} server error\n\t\t*/\n\t\tfunction errorListener( error ) {\n\t\t\tif ( error.code === 'EADDRINUSE' ) {\n\t\t\t\tdebug( 'Server address already in use: %s:%d.', hostname, port );\n\t\t\t\tport += 1;\n\t\t\t\tif ( port <= max ) {\n\t\t\t\t\tdebug( 'Attempting to listen on %s:%d.', hostname, port );\n\t\t\t\t\tserver.listen( port, hostname );\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\n\t\t/**\n\t\t* Callback invoked once a server is listening and ready to handle requests.\n\t\t*\n\t\t* @private\n\t\t*/\n\t\tfunction onListen() {\n\t\t\tvar addr = server.address();\n\t\t\tdebug( 'HTTP server initialized. Server is listening for requests on %s:%d.', addr.address, addr.port );\n\t\t\tdone( null, server );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = httpServer;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Create an HTTP server.\n*\n* @module @stdlib/net-http-server\n*\n* @example\n* var httpServer = require( '@stdlib/net-http-server' );\n*\n* var opts = {\n* 'port': 7331,\n* 'address': '0.0.0.0'\n* };\n* function done( error, server ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( 'Success!' );\n* server.close();\n* }\n* var createServer = httpServer( opts );\n* createServer( done );\n*/\n\n// MODULES //\n\nvar main = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"],
|
|
5
|
-
"mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAuB,QAAS,uCAAwC,EAAE,YAC1EC,EAAW,QAAS,0BAA2B,EAAE,YACjDC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAML,EAAUK,CAAQ,EAGnBJ,EAAYI,EAAS,MAAO,IAChCD,EAAK,KAAOC,EAAQ,KACf,CAACP,EAAsBM,EAAK,IAAK,GAC9B,IAAI,UAAWF,EAAQ,2EAA4E,OAAQE,EAAK,IAAK,CAAE,EAG3HH,EAAYI,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,QAClB,CAACP,EAAsBM,EAAK,OAAQ,GACjC,IAAI,UAAWF,EAAQ,2EAA4E,UAAWE,EAAK,OAAQ,CAAE,EAGjIH,EAAYI,EAAS,UAAW,IACpCD,EAAK,SAAWC,EAAQ,SACnB,CAACN,EAAUK,EAAK,QAAS,GACtB,IAAI,UAAWF,EAAQ,8DAA+D,WAAYE,EAAK,QAAS,CAAE,EAGtHH,EAAYI,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,QAClB,CAACN,EAAUK,EAAK,OAAQ,GACrB,IAAI,UAAWF,EAAQ,8DAA+D,UAAWE,EAAK,OAAQ,CAAE,EAGlH,KA1BC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CA2BhH,CAKAR,EAAO,QAAUM,ICxFjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,CAAAA,EAAA,
|
|
6
|
-
"names": ["require_validate", "__commonJSMin", "exports", "module", "isNonNegativeInteger", "isString", "isObject", "hasOwnProp", "format", "validate", "opts", "options", "require_defaults", "__commonJSMin", "exports", "module", "require_main", "__commonJSMin", "exports", "module", "http", "logger", "isFunction", "format", "validate", "DEFAULTS", "debug", "
|
|
4
|
+
"sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isNonNegativeInteger = require( '@stdlib/assert-is-nonnegative-integer' ).isPrimitive;\nvar isString = require( '@stdlib/assert-is-string' ).isPrimitive;\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {NonNegativeInteger} [options.port] - server port\n* @param {NonNegativeInteger} [options.maxport] - max server port\n* @param {string} [options.hostname] - server hostname\n* @param {string} [options.address] - server address\n* @returns {(Error|null)} error or null\n*\n* @example\n* var options = {\n* 'port': 7331,\n* 'address': '127.0.0.1'\n* };\n* var opts = {};\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'port' ) ) {\n\t\topts.port = options.port;\n\t\tif ( !isNonNegativeInteger( opts.port ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a nonnegative integer. Option: `%s`.', 'port', opts.port ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'maxport' ) ) {\n\t\topts.maxport = options.maxport;\n\t\tif ( !isNonNegativeInteger( opts.maxport ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a nonnegative integer. Option: `%s`.', 'maxport', opts.maxport ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'hostname' ) ) {\n\t\topts.hostname = options.hostname;\n\t\tif ( !isString( opts.hostname ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a string. Option: `%s`.', 'hostname', opts.hostname ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'address' ) ) {\n\t\topts.address = options.address;\n\t\tif ( !isString( opts.address ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a string. Option: `%s`.', 'address', opts.address ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "{\n \"port\": 0,\n \"address\": \"127.0.0.1\"\n}\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar http = require( 'http' );\nvar logger = require( 'debug' );\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar NODE_VERSION = require( '@stdlib/process-node-version' );\nvar omit = require( '@stdlib/utils-omit' );\nvar format = require( '@stdlib/string-format' );\nvar validate = require( './validate.js' );\nvar DEFAULTS = require( './defaults.json' );\n\n\n// VARIABLES //\n\nvar debug = logger( '@stdlib/net-http-server' );\nvar 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\n\nvar EXCLUDE_OPTIONS = [\n\t'port',\n\t'maxport',\n\t'hostname',\n\t'address'\n];\n\n\n// MAIN //\n\n/**\n* Returns a function which creates an HTTP server.\n*\n* ## Notes\n*\n* - 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 supported by `http.createServer` in later Node.js versions are ignored.\n*\n* @param {Options} [options] - server options\n* @param {NonNegativeInteger} [options.port=0] - server port\n* @param {NonNegativeInteger} [options.maxport] - max server port\n* @param {string} [options.hostname] - server hostname\n* @param {string} [options.address=\"127.0.0.1\"] - server address\n* @param {Callback} [requestListener] - callback invoked upon receiving an HTTP request\n* @throws {TypeError} `requestListener` must be a function\n* @throws {TypeError} must provide valid options\n* @returns {Function} function which creates an HTTP server\n*\n* @example\n* var httpServer = factory();\n*\n* @example\n* var opts = {\n* 'port': 7331,\n* 'address': '0.0.0.0'\n* };\n* var httpServer = factory( opts );\n*\n* @example\n* var opts = {\n* 'port': 7331,\n* 'address': '0.0.0.0'\n* };\n* function onRequest( request, response ) {\n* console.log( request.url );\n* response.end( 'OK' );\n* }\n* var httpServer = factory( opts, onRequest );\n*/\nfunction factory() {\n\tvar requestListener;\n\tvar hostname;\n\tvar options;\n\tvar nargs;\n\tvar sopts;\n\tvar opts;\n\tvar port;\n\tvar max;\n\tvar err;\n\tvar flg;\n\n\tnargs = arguments.length;\n\tsopts = {};\n\topts = {};\n\tif ( nargs === 1 ) {\n\t\tif ( isFunction( arguments[0] ) ) {\n\t\t\trequestListener = arguments[ 0 ];\n\t\t} else {\n\t\t\toptions = arguments[ 0 ];\n\t\t\terr = validate( opts, options );\n\t\t\tflg = true;\n\t\t}\n\t} else if ( nargs > 1 ) {\n\t\toptions = arguments[ 0 ];\n\t\trequestListener = arguments[ 1 ];\n\t\tif ( !isFunction( requestListener ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Request listener must be a function. Value: `%s`.', requestListener ) );\n\t\t}\n\t\terr = validate( opts, options );\n\t\tflg = true;\n\t}\n\tif ( err ) {\n\t\tthrow err;\n\t}\n\tif ( flg ) {\n\t\t// Resolve any server-specific options which should be passed to `http.createServer`:\n\t\tsopts = omit( options, EXCLUDE_OPTIONS );\n\t}\n\tif ( opts.port === void 0 ) {\n\t\tport = DEFAULTS.port;\n\t} else {\n\t\tport = opts.port;\n\t}\n\tdebug( 'Server port: %d', port );\n\n\tif ( opts.maxport === void 0 ) {\n\t\tmax = port;\n\t} else {\n\t\tmax = opts.maxport;\n\t}\n\tdebug( 'Max server port: %d', max );\n\n\tif ( opts.hostname ) {\n\t\thostname = opts.hostname;\n\t} else if ( opts.address ) {\n\t\thostname = opts.address;\n\t} else {\n\t\thostname = DEFAULTS.address;\n\t}\n\tdebug( 'Server hostname: %s', hostname );\n\n\treturn httpServer;\n\n\t/**\n\t* Creates an HTTP server.\n\t*\n\t* @private\n\t* @param {Callback} done - function to invoke after creating a server\n\t* @throws {TypeError} must provide a function\n\t*\n\t* @example\n\t* function done( error, server ) {\n\t* if ( error ) {\n\t* throw error;\n\t* }\n\t* console.log( 'Success!' );\n\t* server.close();\n\t* }\n\t* httpServer( done );\n\t*/\n\tfunction httpServer( done ) {\n\t\tvar server;\n\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\tif ( requestListener ) {\n\t\t\tif ( SUPPORTS_OPTIONS ) {\n\t\t\t\tserver = http.createServer( sopts, requestListener );\n\t\t\t} else {\n\t\t\t\tserver = http.createServer( requestListener );\n\t\t\t}\n\t\t} else if ( SUPPORTS_OPTIONS ) {\n\t\t\tserver = http.createServer( sopts );\n\t\t} else {\n\t\t\tserver = http.createServer();\n\t\t}\n\t\tserver.on( 'error', errorListener );\n\t\tserver.once( 'listening', onListen );\n\n\t\tdebug( 'Attempting to listen on %s:%d.', hostname, port );\n\t\tserver.listen( port, hostname );\n\n\t\t/**\n\t\t* Server error event handler.\n\t\t*\n\t\t* @private\n\t\t* @param {Error} error - server error\n\t\t* @throws {Error} server error\n\t\t*/\n\t\tfunction errorListener( error ) {\n\t\t\tif ( error.code === 'EADDRINUSE' ) {\n\t\t\t\tdebug( 'Server address already in use: %s:%d.', hostname, port );\n\t\t\t\tport += 1;\n\t\t\t\tif ( port <= max ) {\n\t\t\t\t\tdebug( 'Attempting to listen on %s:%d.', hostname, port );\n\t\t\t\t\tserver.listen( port, hostname );\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\n\t\t/**\n\t\t* Callback invoked once a server is listening and ready to handle requests.\n\t\t*\n\t\t* @private\n\t\t*/\n\t\tfunction onListen() {\n\t\t\tvar addr = server.address();\n\t\t\tdebug( 'HTTP server initialized. Server is listening for requests on %s:%d.', addr.address, addr.port );\n\t\t\tdone( null, server );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Create an HTTP server.\n*\n* @module @stdlib/net-http-server\n*\n* @example\n* var httpServerFactory = require( '@stdlib/net-http-server' );\n*\n* var opts = {\n* 'port': 7331,\n* 'address': '0.0.0.0'\n* };\n* function done( error, server ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( 'Success!' );\n* server.close();\n* }\n* var httpServer = httpServerFactory( opts );\n* httpServer( done );\n*/\n\n// MODULES //\n\nvar main = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"],
|
|
5
|
+
"mappings": "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",
|
|
6
|
+
"names": ["require_validate", "__commonJSMin", "exports", "module", "isNonNegativeInteger", "isString", "isObject", "hasOwnProp", "format", "validate", "opts", "options", "require_defaults", "__commonJSMin", "exports", "module", "require_main", "__commonJSMin", "exports", "module", "http", "logger", "isFunction", "NODE_VERSION", "omit", "format", "validate", "DEFAULTS", "debug", "SUPPORTS_OPTIONS", "EXCLUDE_OPTIONS", "factory", "requestListener", "hostname", "options", "nargs", "sopts", "opts", "port", "max", "err", "flg", "httpServer", "done", "server", "errorListener", "onListen", "error", "addr", "main"]
|
|
7
7
|
}
|
package/docs/types/index.d.ts
CHANGED
|
@@ -104,7 +104,7 @@ type Callback = Nullary | Unary | Binary;
|
|
|
104
104
|
*
|
|
105
105
|
* @param done - callback to invoke after creating the server
|
|
106
106
|
*/
|
|
107
|
-
type
|
|
107
|
+
type httpServer = ( done: Callback ) => void;
|
|
108
108
|
|
|
109
109
|
/**
|
|
110
110
|
* Returns a function which creates an HTTP server.
|
|
@@ -113,20 +113,24 @@ type createServer = ( done: Callback ) => void;
|
|
|
113
113
|
* @returns function which creates an HTTP server
|
|
114
114
|
*
|
|
115
115
|
* @example
|
|
116
|
-
* var
|
|
116
|
+
* var httpServer = factory();
|
|
117
117
|
*
|
|
118
118
|
* @example
|
|
119
119
|
* function onRequest( request, response ) {
|
|
120
120
|
* console.log( request.url );
|
|
121
121
|
* response.end( 'OK' );
|
|
122
122
|
* }
|
|
123
|
-
* var
|
|
123
|
+
* var httpServer = factory( onRequest );
|
|
124
124
|
*/
|
|
125
|
-
declare function
|
|
125
|
+
declare function factory( requestListener?: RequestListener ): httpServer;
|
|
126
126
|
|
|
127
127
|
/**
|
|
128
128
|
* Returns a function which creates an HTTP server.
|
|
129
129
|
*
|
|
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 supported by `http.createServer` in later Node.js versions are ignored.
|
|
133
|
+
*
|
|
130
134
|
* @param options - server options
|
|
131
135
|
* @param options.port - server port (default: 0)
|
|
132
136
|
* @param options.maxport - max server port
|
|
@@ -141,7 +145,7 @@ declare function httpServer( requestListener?: RequestListener ): createServer;
|
|
|
141
145
|
* 'port': 7331,
|
|
142
146
|
* 'address': '0.0.0.0'
|
|
143
147
|
* };
|
|
144
|
-
* var
|
|
148
|
+
* var httpServer = factory( opts );
|
|
145
149
|
*
|
|
146
150
|
* @example
|
|
147
151
|
* var opts = {
|
|
@@ -152,11 +156,11 @@ declare function httpServer( requestListener?: RequestListener ): createServer;
|
|
|
152
156
|
* console.log( request.url );
|
|
153
157
|
* response.end( 'OK' );
|
|
154
158
|
* }
|
|
155
|
-
* var
|
|
159
|
+
* var httpServer = factory( opts, onRequest );
|
|
156
160
|
*/
|
|
157
|
-
declare function
|
|
161
|
+
declare function factory<T extends Options>( options: T, requestListener?: RequestListener ): httpServer;
|
|
158
162
|
|
|
159
163
|
|
|
160
164
|
// EXPORTS //
|
|
161
165
|
|
|
162
|
-
export =
|
|
166
|
+
export = factory;
|
package/lib/defaults.json
CHANGED
package/lib/index.js
CHANGED
|
@@ -24,21 +24,21 @@
|
|
|
24
24
|
* @module @stdlib/net-http-server
|
|
25
25
|
*
|
|
26
26
|
* @example
|
|
27
|
-
* var
|
|
27
|
+
* var httpServerFactory = require( '@stdlib/net-http-server' );
|
|
28
28
|
*
|
|
29
29
|
* var opts = {
|
|
30
30
|
* 'port': 7331,
|
|
31
|
-
*
|
|
31
|
+
* 'address': '0.0.0.0'
|
|
32
32
|
* };
|
|
33
33
|
* function done( error, server ) {
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
34
|
+
* if ( error ) {
|
|
35
|
+
* throw error;
|
|
36
|
+
* }
|
|
37
|
+
* console.log( 'Success!' );
|
|
38
|
+
* server.close();
|
|
39
39
|
* }
|
|
40
|
-
* var
|
|
41
|
-
*
|
|
40
|
+
* var httpServer = httpServerFactory( opts );
|
|
41
|
+
* httpServer( done );
|
|
42
42
|
*/
|
|
43
43
|
|
|
44
44
|
// MODULES //
|
package/lib/main.js
CHANGED
|
@@ -23,6 +23,8 @@
|
|
|
23
23
|
var http = require( 'http' );
|
|
24
24
|
var logger = require( 'debug' );
|
|
25
25
|
var isFunction = require( '@stdlib/assert-is-function' );
|
|
26
|
+
var NODE_VERSION = require( '@stdlib/process-node-version' );
|
|
27
|
+
var omit = require( '@stdlib/utils-omit' );
|
|
26
28
|
var format = require( '@stdlib/string-format' );
|
|
27
29
|
var validate = require( './validate.js' );
|
|
28
30
|
var DEFAULTS = require( './defaults.json' );
|
|
@@ -31,6 +33,14 @@ var DEFAULTS = require( './defaults.json' );
|
|
|
31
33
|
// VARIABLES //
|
|
32
34
|
|
|
33
35
|
var debug = logger( '@stdlib/net-http-server' );
|
|
36
|
+
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
|
|
37
|
+
|
|
38
|
+
var EXCLUDE_OPTIONS = [
|
|
39
|
+
'port',
|
|
40
|
+
'maxport',
|
|
41
|
+
'hostname',
|
|
42
|
+
'address'
|
|
43
|
+
];
|
|
34
44
|
|
|
35
45
|
|
|
36
46
|
// MAIN //
|
|
@@ -38,6 +48,10 @@ var debug = logger( '@stdlib/net-http-server' );
|
|
|
38
48
|
/**
|
|
39
49
|
* Returns a function which creates an HTTP server.
|
|
40
50
|
*
|
|
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 supported by `http.createServer` in later Node.js versions are ignored.
|
|
54
|
+
*
|
|
41
55
|
* @param {Options} [options] - server options
|
|
42
56
|
* @param {NonNegativeInteger} [options.port=0] - server port
|
|
43
57
|
* @param {NonNegativeInteger} [options.maxport] - max server port
|
|
@@ -49,14 +63,14 @@ var debug = logger( '@stdlib/net-http-server' );
|
|
|
49
63
|
* @returns {Function} function which creates an HTTP server
|
|
50
64
|
*
|
|
51
65
|
* @example
|
|
52
|
-
* var
|
|
66
|
+
* var httpServer = factory();
|
|
53
67
|
*
|
|
54
68
|
* @example
|
|
55
69
|
* var opts = {
|
|
56
70
|
* 'port': 7331,
|
|
57
71
|
* 'address': '0.0.0.0'
|
|
58
72
|
* };
|
|
59
|
-
* var
|
|
73
|
+
* var httpServer = factory( opts );
|
|
60
74
|
*
|
|
61
75
|
* @example
|
|
62
76
|
* var opts = {
|
|
@@ -67,39 +81,47 @@ var debug = logger( '@stdlib/net-http-server' );
|
|
|
67
81
|
* console.log( request.url );
|
|
68
82
|
* response.end( 'OK' );
|
|
69
83
|
* }
|
|
70
|
-
* var
|
|
84
|
+
* var httpServer = factory( opts, onRequest );
|
|
71
85
|
*/
|
|
72
|
-
function
|
|
86
|
+
function factory() {
|
|
73
87
|
var requestListener;
|
|
74
88
|
var hostname;
|
|
75
89
|
var options;
|
|
76
90
|
var nargs;
|
|
91
|
+
var sopts;
|
|
77
92
|
var opts;
|
|
78
93
|
var port;
|
|
79
94
|
var max;
|
|
80
95
|
var err;
|
|
96
|
+
var flg;
|
|
81
97
|
|
|
82
98
|
nargs = arguments.length;
|
|
99
|
+
sopts = {};
|
|
83
100
|
opts = {};
|
|
84
101
|
if ( nargs === 1 ) {
|
|
85
|
-
if ( isFunction( arguments[0] )) {
|
|
102
|
+
if ( isFunction( arguments[0] ) ) {
|
|
86
103
|
requestListener = arguments[ 0 ];
|
|
87
104
|
} else {
|
|
88
105
|
options = arguments[ 0 ];
|
|
89
106
|
err = validate( opts, options );
|
|
107
|
+
flg = true;
|
|
90
108
|
}
|
|
91
|
-
}
|
|
92
|
-
else if ( nargs > 1 ) {
|
|
109
|
+
} else if ( nargs > 1 ) {
|
|
93
110
|
options = arguments[ 0 ];
|
|
94
111
|
requestListener = arguments[ 1 ];
|
|
95
112
|
if ( !isFunction( requestListener ) ) {
|
|
96
113
|
throw new TypeError( format( 'invalid argument. Request listener must be a function. Value: `%s`.', requestListener ) );
|
|
97
114
|
}
|
|
98
115
|
err = validate( opts, options );
|
|
116
|
+
flg = true;
|
|
99
117
|
}
|
|
100
118
|
if ( err ) {
|
|
101
119
|
throw err;
|
|
102
120
|
}
|
|
121
|
+
if ( flg ) {
|
|
122
|
+
// Resolve any server-specific options which should be passed to `http.createServer`:
|
|
123
|
+
sopts = omit( options, EXCLUDE_OPTIONS );
|
|
124
|
+
}
|
|
103
125
|
if ( opts.port === void 0 ) {
|
|
104
126
|
port = DEFAULTS.port;
|
|
105
127
|
} else {
|
|
@@ -116,16 +138,14 @@ function httpServer() {
|
|
|
116
138
|
|
|
117
139
|
if ( opts.hostname ) {
|
|
118
140
|
hostname = opts.hostname;
|
|
119
|
-
}
|
|
120
|
-
else if ( opts.address ) {
|
|
141
|
+
} else if ( opts.address ) {
|
|
121
142
|
hostname = opts.address;
|
|
122
|
-
}
|
|
123
|
-
else {
|
|
143
|
+
} else {
|
|
124
144
|
hostname = DEFAULTS.address;
|
|
125
145
|
}
|
|
126
146
|
debug( 'Server hostname: %s', hostname );
|
|
127
147
|
|
|
128
|
-
return
|
|
148
|
+
return httpServer;
|
|
129
149
|
|
|
130
150
|
/**
|
|
131
151
|
* Creates an HTTP server.
|
|
@@ -142,15 +162,22 @@ function httpServer() {
|
|
|
142
162
|
* console.log( 'Success!' );
|
|
143
163
|
* server.close();
|
|
144
164
|
* }
|
|
145
|
-
*
|
|
165
|
+
* httpServer( done );
|
|
146
166
|
*/
|
|
147
|
-
function
|
|
167
|
+
function httpServer( done ) {
|
|
148
168
|
var server;
|
|
169
|
+
|
|
149
170
|
if ( !isFunction( done ) ) {
|
|
150
171
|
throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', done ) );
|
|
151
172
|
}
|
|
152
173
|
if ( requestListener ) {
|
|
153
|
-
|
|
174
|
+
if ( SUPPORTS_OPTIONS ) {
|
|
175
|
+
server = http.createServer( sopts, requestListener );
|
|
176
|
+
} else {
|
|
177
|
+
server = http.createServer( requestListener );
|
|
178
|
+
}
|
|
179
|
+
} else if ( SUPPORTS_OPTIONS ) {
|
|
180
|
+
server = http.createServer( sopts );
|
|
154
181
|
} else {
|
|
155
182
|
server = http.createServer();
|
|
156
183
|
}
|
|
@@ -196,4 +223,4 @@ function httpServer() {
|
|
|
196
223
|
|
|
197
224
|
// EXPORTS //
|
|
198
225
|
|
|
199
|
-
module.exports =
|
|
226
|
+
module.exports = factory;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stdlib/net-http-server",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "HTTP server.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": {
|
|
@@ -32,10 +32,12 @@
|
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"@stdlib/assert-has-own-property": "^0.2.2",
|
|
34
34
|
"@stdlib/assert-is-function": "^0.2.2",
|
|
35
|
-
"@stdlib/assert-is-nonnegative-integer": "^0.2.
|
|
35
|
+
"@stdlib/assert-is-nonnegative-integer": "^0.2.2",
|
|
36
36
|
"@stdlib/assert-is-plain-object": "^0.2.2",
|
|
37
37
|
"@stdlib/assert-is-string": "^0.2.2",
|
|
38
|
+
"@stdlib/process-node-version": "^0.2.2",
|
|
38
39
|
"@stdlib/string-format": "^0.2.2",
|
|
40
|
+
"@stdlib/utils-omit": "^0.2.2",
|
|
39
41
|
"debug": "^2.6.9",
|
|
40
42
|
"@stdlib/error-tools-fmtprodmsg": "^0.2.2"
|
|
41
43
|
},
|