@loopback/http-server
Advanced tools
Comparing version 1.4.19 to 1.5.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [1.5.0](https://github.com/strongloop/loopback-next/compare/@loopback/http-server@1.4.19...@loopback/http-server@1.5.0) (2019-12-09) | ||
### Features | ||
* **http-server:** use stoppable to handle http keep-alive gracefully ([c5fc495](https://github.com/strongloop/loopback-next/commit/c5fc495639301a494313afb33c5d156b427257e0)) | ||
## [1.4.19](https://github.com/strongloop/loopback-next/compare/@loopback/http-server@1.4.18...@loopback/http-server@1.4.19) (2019-11-25) | ||
@@ -8,0 +19,0 @@ |
/// <reference types="node" /> | ||
import * as http from 'http'; | ||
import { IncomingMessage, ServerResponse } from 'http'; | ||
import * as https from 'https'; | ||
import http, { IncomingMessage, ServerResponse } from 'http'; | ||
import https from 'https'; | ||
import { AddressInfo, ListenOptions } from 'net'; | ||
/** | ||
* Request listener function for http/https requests | ||
*/ | ||
export declare type RequestListener = (req: IncomingMessage, res: ServerResponse) => void; | ||
/** | ||
* Base options that are common to http and https servers | ||
*/ | ||
export interface BaseHttpOptions extends ListenOptions { | ||
/** | ||
* The `gracePeriodForClose` property controls how to stop the server | ||
* gracefully. Its value is the number of milliseconds to wait before | ||
* in-flight requests finish when the server is being stopped. With this | ||
* setting, we also reject new requests from existing keep-alive connections | ||
* in addition to stopping accepting new connections. | ||
* | ||
* Defaults to Infinity (don't force-close). If you want to immediately | ||
* destroy all sockets set its value to `0`. | ||
* | ||
* @see https://www.npmjs.com/package/stoppable | ||
*/ | ||
gracePeriodForClose?: number; | ||
} | ||
/** | ||
* HTTP server options | ||
* | ||
*/ | ||
export interface HttpOptions extends ListenOptions { | ||
export interface HttpOptions extends BaseHttpOptions { | ||
protocol?: 'http'; | ||
@@ -16,5 +35,4 @@ } | ||
* HTTPS server options | ||
* | ||
*/ | ||
export interface HttpsOptions extends ListenOptions, https.ServerOptions { | ||
export interface HttpsOptions extends BaseHttpOptions, https.ServerOptions { | ||
protocol: 'https'; | ||
@@ -41,2 +59,3 @@ } | ||
readonly server: http.Server | https.Server; | ||
private _stoppable; | ||
private serverOptions; | ||
@@ -43,0 +62,0 @@ /** |
@@ -6,8 +6,12 @@ "use strict"; | ||
// License text available at https://opensource.org/licenses/MIT | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const assert = require("assert"); | ||
const http = require("http"); | ||
const https = require("https"); | ||
const os = require("os"); | ||
const p_event_1 = require("p-event"); | ||
const assert_1 = __importDefault(require("assert")); | ||
const http_1 = __importDefault(require("http")); | ||
const https_1 = __importDefault(require("https")); | ||
const os_1 = __importDefault(require("os")); | ||
const p_event_1 = __importDefault(require("p-event")); | ||
const stoppable_1 = __importDefault(require("stoppable")); | ||
/** | ||
@@ -22,2 +26,3 @@ * HTTP / HTTPS server used by LoopBack's RestServer | ||
constructor(requestListener, serverOptions) { | ||
var _a; | ||
this._listening = false; | ||
@@ -32,9 +37,13 @@ this.requestListener = requestListener; | ||
} | ||
this._protocol = serverOptions ? serverOptions.protocol || 'http' : 'http'; | ||
this._protocol = serverOptions ? (_a = serverOptions.protocol, (_a !== null && _a !== void 0 ? _a : 'http')) : 'http'; | ||
if (this._protocol === 'https') { | ||
this.server = https.createServer(this.serverOptions, this.requestListener); | ||
this.server = https_1.default.createServer(this.serverOptions, this.requestListener); | ||
} | ||
else { | ||
this.server = http.createServer(this.requestListener); | ||
this.server = http_1.default.createServer(this.requestListener); | ||
} | ||
// Set up graceful stop for http server | ||
if (typeof this.serverOptions.gracePeriodForClose === 'number') { | ||
this._stoppable = stoppable_1.default(this.server, this.serverOptions.gracePeriodForClose); | ||
} | ||
} | ||
@@ -49,3 +58,3 @@ /** | ||
const address = this.server.address(); | ||
assert(address != null); | ||
assert_1.default(address != null); | ||
this._address = address; | ||
@@ -59,3 +68,8 @@ } | ||
return; | ||
this.server.close(); | ||
if (this._stoppable != null) { | ||
this._stoppable.stop(); | ||
} | ||
else { | ||
this.server.close(); | ||
} | ||
await p_event_1.default(this.server, 'close'); | ||
@@ -134,3 +148,3 @@ this._listening = false; | ||
const pipes = ['\\\\?\\pipe\\', '\\\\.\\pipe\\']; | ||
assert(pipes.some(p => ipcPath.startsWith(p)), `Named pipe ${ipcPath} does NOT start with + ${pipes.join(' or ')}`); | ||
assert_1.default(pipes.some(p => ipcPath.startsWith(p)), `Named pipe ${ipcPath} does NOT start with + ${pipes.join(' or ')}`); | ||
} | ||
@@ -142,4 +156,4 @@ } | ||
function isWin32() { | ||
return os.platform() === 'win32'; | ||
return os_1.default.platform() === 'win32'; | ||
} | ||
//# sourceMappingURL=http-server.js.map |
{ | ||
"name": "@loopback/http-server", | ||
"version": "1.4.19", | ||
"version": "1.5.0", | ||
"description": "A wrapper for creating HTTP/HTTPS servers", | ||
@@ -19,10 +19,12 @@ "engines": { | ||
"dependencies": { | ||
"p-event": "^4.1.0" | ||
"p-event": "^4.1.0", | ||
"stoppable": "^1.1.0" | ||
}, | ||
"devDependencies": { | ||
"@loopback/build": "^2.1.0", | ||
"@loopback/core": "^1.11.0", | ||
"@loopback/eslint-config": "^4.1.5", | ||
"@loopback/testlab": "^1.9.5", | ||
"@types/node": "^10.17.5" | ||
"@loopback/build": "^3.0.0", | ||
"@loopback/core": "^1.12.0", | ||
"@loopback/eslint-config": "^5.0.0", | ||
"@loopback/testlab": "^1.10.0", | ||
"@types/node": "^10.17.6", | ||
"@types/stoppable": "^1.1.0" | ||
}, | ||
@@ -45,3 +47,3 @@ "files": [ | ||
}, | ||
"gitHead": "c111543a6139c5a2999930c593aabbbdf10a838e" | ||
"gitHead": "89eb61bacaed75e6eb61ae6840cea266cb888659" | ||
} |
@@ -6,10 +6,13 @@ // Copyright IBM Corp. 2018,2019. All Rights Reserved. | ||
import * as assert from 'assert'; | ||
import * as http from 'http'; | ||
import {IncomingMessage, ServerResponse} from 'http'; | ||
import * as https from 'https'; | ||
import assert from 'assert'; | ||
import http, {IncomingMessage, ServerResponse} from 'http'; | ||
import https from 'https'; | ||
import {AddressInfo, ListenOptions} from 'net'; | ||
import * as os from 'os'; | ||
import os from 'os'; | ||
import pEvent from 'p-event'; | ||
import stoppable from 'stoppable'; | ||
/** | ||
* Request listener function for http/https requests | ||
*/ | ||
export type RequestListener = ( | ||
@@ -21,6 +24,24 @@ req: IncomingMessage, | ||
/** | ||
* Base options that are common to http and https servers | ||
*/ | ||
export interface BaseHttpOptions extends ListenOptions { | ||
/** | ||
* The `gracePeriodForClose` property controls how to stop the server | ||
* gracefully. Its value is the number of milliseconds to wait before | ||
* in-flight requests finish when the server is being stopped. With this | ||
* setting, we also reject new requests from existing keep-alive connections | ||
* in addition to stopping accepting new connections. | ||
* | ||
* Defaults to Infinity (don't force-close). If you want to immediately | ||
* destroy all sockets set its value to `0`. | ||
* | ||
* @see https://www.npmjs.com/package/stoppable | ||
*/ | ||
gracePeriodForClose?: number; | ||
} | ||
/** | ||
* HTTP server options | ||
* | ||
*/ | ||
export interface HttpOptions extends ListenOptions { | ||
export interface HttpOptions extends BaseHttpOptions { | ||
protocol?: 'http'; | ||
@@ -31,5 +52,4 @@ } | ||
* HTTPS server options | ||
* | ||
*/ | ||
export interface HttpsOptions extends ListenOptions, https.ServerOptions { | ||
export interface HttpsOptions extends BaseHttpOptions, https.ServerOptions { | ||
protocol: 'https'; | ||
@@ -59,2 +79,3 @@ } | ||
readonly server: http.Server | https.Server; | ||
private _stoppable: stoppable.StoppableServer; | ||
private serverOptions: HttpServerOptions; | ||
@@ -81,3 +102,3 @@ | ||
} | ||
this._protocol = serverOptions ? serverOptions.protocol || 'http' : 'http'; | ||
this._protocol = serverOptions ? serverOptions.protocol ?? 'http' : 'http'; | ||
if (this._protocol === 'https') { | ||
@@ -91,2 +112,9 @@ this.server = https.createServer( | ||
} | ||
// Set up graceful stop for http server | ||
if (typeof this.serverOptions.gracePeriodForClose === 'number') { | ||
this._stoppable = stoppable( | ||
this.server, | ||
this.serverOptions.gracePeriodForClose, | ||
); | ||
} | ||
} | ||
@@ -112,3 +140,7 @@ | ||
if (!this._listening) return; | ||
this.server.close(); | ||
if (this._stoppable != null) { | ||
this._stoppable.stop(); | ||
} else { | ||
this.server.close(); | ||
} | ||
await pEvent(this.server, 'close'); | ||
@@ -115,0 +147,0 @@ this._listening = false; |
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
39481
480
2
6
+ Addedstoppable@^1.1.0
+ Addedstoppable@1.1.0(transitive)