@gquittet/graceful-server
Advanced tools
Comparing version 2.4.1 to 2.5.0
@@ -1,1 +0,1 @@ | ||
(()=>{var e={636:(e,t,s)=>{"use strict";s.d(t,{u:()=>r,Z:()=>i});const o={closePromises:[],timeout:1e3,healthCheck:!0,livenessEndpoint:"/live",readinessEndpoint:"/ready",gracePeriod:0};let n=!0;const r=e=>(n&&(Object.freeze(Object.assign(o,e)),n=!1),o),i=o},31:(e,t,s)=>{"use strict";s.d(t,{Z:()=>y});const o=require("events"),n=[{type:"SIGHUP",code:1},{type:"SIGBREAK",code:1},{type:"SIGINT",code:2},{type:"SIGTERM",code:15},{type:"uncaughtException",code:2}];var r=s(636),i=s(825);const a=e=>new Promise((t=>setTimeout(t,e))),c=e=>{const t=e||new Set,s=e=>{e.destroy(),t.delete(e)};return{onConnection:e=>{t.add(e),e.once("close",(()=>t.delete(e)))},closeAll:async()=>t.forEach(s)}},{livenessEndpoint:d,readinessEndpoint:u}=r.Z,l=(e,t)=>{const{healthCheck:s}=r.Z,o=c(),n=c();let i=!1;e.on("connection",o.onConnection),e.on("secureConnection",n.onConnection);const a=e.listeners("request");return e.removeAllListeners("request"),s?(e.on("request",(e=>(t,s)=>{const{livenessEndpoint:o,readinessEndpoint:n}=r.Z;if(!s.headersSent)return t.url===o&&"GET"===t.method?(s.statusCode=200,s.setHeader("Content-Type","application/json"),s.end(JSON.stringify({uptime:0|process.uptime()}))):t.url===n&&"GET"===t.method?e.isReady()?(s.statusCode=200,s.setHeader("Content-Type","application/json"),s.end(JSON.stringify({status:"ready"}))):(s.statusCode=503,s.end()):void 0})(t)),a.forEach((s=>e.on("request",((e,o)=>{if(t.isReady()){if(e.url!==d&&e.url!==u||"GET"!==e.method)return s(e,o)}else e.socket.destroy()}))))):a.forEach((s=>e.on("request",((e,o)=>{if(t.isReady())return s(e,o);e.socket.destroy()})))),Object.assign(e,{stop:async()=>{if(e.listening&&!i)return i=!0,e.removeAllListeners("request"),e.on("request",((e,t)=>{if(!t.headersSent)return t.setHeader("connection","close")})),await Promise.all([o.closeAll(),n.closeAll()]),new Promise(((t,s)=>{e.close((e=>{e?s(e):t()}))}))}})},p=e=>{let t=i.Z.STARTING;return{set:function(s,o){return t=s,e.emit(s,o),this},get:()=>t,setReady:function(){this.set(i.Z.READY)},isReady:()=>t===i.Z.READY}},y=e=>{const t=new o.EventEmitter,s=p(t),c=l(e,s);return{status:s,init:function(){return(e=>{for(const t of n)process.on(t.type,(async s=>{await e.shutdown(t.type,t.code,s)}));return e})(this)},shutdown:function(e,t,s){return((e,t)=>async(s,o,n)=>{const{timeout:c,closePromises:d,gracePeriod:u}=r.Z,l=n&&n.message?n:new Error(s);u>0&&await a(1e3*u),t.status.set(i.Z.SHUTTING_DOWN,l),await a(c),await Promise.all(d.map((e=>e()))),await e.stop(),t.status.set(i.Z.SHUTDOWN,l),process.exit(128+o)})(c,this)(e,t,s)},on:(e,s)=>t.on(e,s)}}},825:(e,t,s)=>{"use strict";var o;s.d(t,{Z:()=>n}),function(e){e.STARTING="STARTING",e.READY="READY",e.SHUTTING_DOWN="SHUTTING_DOWN",e.SHUTDOWN="SHUTDOWN"}(o||(o={}));const n=o},878:()=>{}},t={};function s(o){var n=t[o];if(void 0!==n)return n.exports;var r=t[o]={exports:{}};return e[o](r,r.exports,s),r.exports}s.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return s.d(t,{a:t}),t},s.d=(e,t)=>{for(var o in t)s.o(t,o)&&!s.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},s.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),s.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var o={};(()=>{"use strict";s.r(o),s.d(o,{default:()=>a});var e=s(636),t=s(31),n=s(825),r=s(878),i={};for(const e in r)"default"!==e&&(i[e]=()=>r[e]);s.d(o,i);const a=Object.assign(((s,o)=>{(0,e.u)(o);const n=(0,t.Z)(s).init();return{isReady:()=>n.status.isReady(),setReady:()=>n.status.setReady(),on:n.on}}),n.Z)})(),module.exports=o})();const __export__=module.exports;module.exports=__export__.default,Object.assign(module.exports,__export__); | ||
(()=>{var e={636:(e,t,s)=>{"use strict";s.d(t,{u:()=>r,Z:()=>i});const n={closePromises:[],timeout:1e3,healthCheck:!0,kubernetes:!1,livenessEndpoint:"/live",readinessEndpoint:"/ready"};let o=!0;const r=e=>(o&&(Object.freeze(Object.assign(n,e)),o=!1),n),i=n},751:(e,t,s)=>{"use strict";s.d(t,{Z:()=>y});const n=require("events"),o=[{type:"SIGHUP",code:1},{type:"SIGBREAK",code:1},{type:"SIGINT",code:2},{type:"SIGTERM",code:15},{type:"uncaughtException",code:2}];var r=s(636),i=s(825);const a=(e,t)=>async(s,n,o)=>{const{timeout:a,closePromises:c}=r.Z,u=o&&o.message?o:new Error(s);var d;t.status.set(i.Z.SHUTTING_DOWN,u),await(d=a,new Promise((e=>setTimeout(e,d)))),await Promise.all(c.map((e=>e()))),await e.stop(),t.status.set(i.Z.SHUTDOWN,u),process.exit(128+n)},c=e=>{const t=e||new Set,s=e=>{e.destroy(),t.delete(e)};return{onConnection:e=>{t.add(e),e.once("close",(()=>t.delete(e)))},closeAll:async()=>t.forEach(s)}},{livenessEndpoint:u,readinessEndpoint:d}=r.Z,l=(e,t)=>{const{healthCheck:s,kubernetes:n}=r.Z,o=c(),i=c();let a=!1;e.on("connection",o.onConnection),e.on("secureConnection",i.onConnection);const l=e.listeners("request");return e.removeAllListeners("request"),s?(e.on("request",(e=>(t,s)=>{const{livenessEndpoint:n,readinessEndpoint:o}=r.Z;if(!s.headersSent)return t.url===n&&"GET"===t.method?(s.statusCode=200,s.setHeader("Content-Type","application/json"),s.end(JSON.stringify({uptime:0|process.uptime()}))):t.url===o&&"GET"===t.method?e.isReady()?(s.statusCode=200,s.setHeader("Content-Type","application/json"),s.end(JSON.stringify({status:"ready"}))):(s.statusCode=503,s.end()):void 0})(t)),l.forEach((s=>e.on("request",((e,o)=>(n?t.isReady()||t.isShuttingDown():t.isReady())?e.url!==u&&e.url!==d||"GET"!==e.method?s(e,o):void 0:e.socket.destroy()))))):l.forEach((s=>e.on("request",((e,n)=>{if(t.isReady())return s(e,n);e.socket.destroy()})))),Object.assign(e,{stop:async()=>{if(e.listening&&!a)return a=!0,e.removeAllListeners("request"),e.on("request",((e,t)=>{if(!t.headersSent)return t.setHeader("connection","close")})),await Promise.all([o.closeAll(),i.closeAll()]),new Promise(((t,s)=>{e.close((e=>{e?s(e):t()}))}))}})},p=e=>{let t=i.Z.STARTING;return{set:function(s,n){return t=s,e.emit(s,n),this},get:()=>t,setReady:function(){this.set(i.Z.READY)},isReady:()=>t===i.Z.READY,isShuttingDown:()=>t===i.Z.SHUTTING_DOWN}},y=e=>{const t=new n.EventEmitter,s=p(t),r=l(e,s);return{status:s,init:function(){return(e=>{for(const t of o)process.on(t.type,(async s=>{await e.shutdown(t.type,t.code,s)}));return e})(this)},shutdown:function(e,t,s){return a(r,this)(e,t,s)},on:(e,s)=>t.on(e,s)}}},825:(e,t,s)=>{"use strict";var n;s.d(t,{Z:()=>o}),function(e){e.STARTING="STARTING",e.READY="READY",e.SHUTTING_DOWN="SHUTTING_DOWN",e.SHUTDOWN="SHUTDOWN"}(n||(n={}));const o=n},878:()=>{}},t={};function s(n){var o=t[n];if(void 0!==o)return o.exports;var r=t[n]={exports:{}};return e[n](r,r.exports,s),r.exports}s.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return s.d(t,{a:t}),t},s.d=(e,t)=>{for(var n in t)s.o(t,n)&&!s.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},s.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),s.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var n={};(()=>{"use strict";s.r(n),s.d(n,{default:()=>a});var e=s(636),t=s(751),o=s(825),r=s(878),i={};for(const e in r)"default"!==e&&(i[e]=()=>r[e]);s.d(n,i);const a=Object.assign(((s,n)=>{(0,e.u)(n);const o=(0,t.Z)(s).init();return{isReady:()=>o.status.isReady(),setReady:()=>o.status.setReady(),on:o.on}}),o.Z)})(),module.exports=n})();const __export__=module.exports;module.exports=__export__.default,Object.assign(module.exports,__export__); |
@@ -5,5 +5,5 @@ export default interface IOptions { | ||
healthCheck: boolean; | ||
kubernetes: boolean; | ||
livenessEndpoint: string; | ||
readinessEndpoint: string; | ||
gracePeriod: number; | ||
} |
@@ -7,2 +7,3 @@ import State from "../core/state"; | ||
isReady: () => boolean; | ||
isShuttingDown: () => boolean; | ||
} |
@@ -15,3 +15,3 @@ { | ||
}, | ||
"version": "2.4.1", | ||
"version": "2.5.0", | ||
"main": "./lib/index.js", | ||
@@ -18,0 +18,0 @@ "scripts": { |
@@ -72,2 +72,4 @@ <h1 align="center"> | ||
✔ Kubernetes compliant. | ||
✔ Dependency-free. | ||
@@ -249,3 +251,2 @@ | ||
server.listen(8080, async () => { | ||
await connectToDb() | ||
gracefulServer.setReady() | ||
@@ -311,11 +312,15 @@ }) | ||
| Name | Type | Default | Description | | ||
| ----------------- | :------------------------: | :-----: | -----------------------------------------------------------------------: | | ||
| closePromises | (() => Promise<unknown>)[] | [] | The functions to run when the API is stopping | | ||
| timeout | number | 1000 | The time in milliseconds to wait before shutting down the server | | ||
| healthCheck | boolean | true | Enable/Disable the default endpoints (liveness and readiness) | | ||
| livenessEndpoint | string | /live | The liveness endpoint | | ||
| readinessEndpoint | string | /ready | The readiness endpoint | | ||
| gracePeriod | number | 0 | Number of seconds to wait before stopping to accept the incoming traffic | | ||
| Name | Type | Default | Description | | ||
| ----------------- | :------------------------: | :-----: | ---------------------------------------------------------------: | | ||
| closePromises | (() => Promise<unknown>)[] | [] | The functions to run when the API is stopping | | ||
| timeout | number | 1000 | The time in milliseconds to wait before shutting down the server | | ||
| healthCheck | boolean | true | Enable/Disable the default endpoints (liveness and readiness) | | ||
| kubernetes | boolean | false | Enable/Disable the kubernetes mode | | ||
| livenessEndpoint | string | /live | The liveness endpoint | | ||
| readinessEndpoint | string | /ready | The readiness endpoint | | ||
If you use Kubernetes, enable the kubernetes mode to let it handles the incoming traffic of your application. | ||
The Kubernetes mode will only work if you haven't disabled the health checks. | ||
### GracefulServer Instance | ||
@@ -441,4 +446,3 @@ | ||
In the following example, you have to setup the gracePeriod to 5 seconds because you set up the readiness timeout to 5 seconds. | ||
[(Related to this issue)](https://github.com/gquittet/graceful-server/issues/5) | ||
Don't forget to enable the kubernetes mode. [Check here](#igracefulserveroptions) (related to this [issue](https://github.com/gquittet/graceful-server/issues/5)) | ||
@@ -515,2 +519,2 @@ ```yml | ||
If you like my job, don't hesite to contribute to this project! ❤️ | ||
If you like my job, don't hesitate to contribute to this project! ❤️ |
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
28471
131
516