@pjts/server 0.0.50

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/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Domain-Driven Design TypeScript Utilities / Server
2
+
3
+ Common web server utility classes and functions for managing runtime and deployment
@@ -0,0 +1,9 @@
1
+ export declare class DeploymentStatus {
2
+ private _isReady;
3
+ private server;
4
+ constructor(port: number);
5
+ ready(): void;
6
+ notReady(): void;
7
+ stop(): Promise<void>;
8
+ private write;
9
+ }
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DeploymentStatus = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const http_1 = tslib_1.__importDefault(require("http"));
6
+ const process_1 = require("process");
7
+ const core_1 = require("@pjts/core");
8
+ class DeploymentStatus {
9
+ _isReady = false;
10
+ server;
11
+ constructor(port) {
12
+ this.server = http_1.default.createServer((request, response) => {
13
+ switch (request.url) {
14
+ case '/readiness':
15
+ if (this._isReady) {
16
+ return this.write(response, 200, '200 OK');
17
+ }
18
+ return this.write(response, 503, '503 NOT READY');
19
+ default:
20
+ return this.write(response, 404, '404 PROBE NOT FOUND');
21
+ }
22
+ });
23
+ (0, process_1.nextTick)(() => {
24
+ this.server.listen(port);
25
+ core_1.Logger.info(`Probe Server running on port: ${port}`);
26
+ });
27
+ }
28
+ ready() {
29
+ core_1.Logger.info('Deployment is ready');
30
+ this._isReady = true;
31
+ }
32
+ notReady() {
33
+ this._isReady = false;
34
+ }
35
+ stop() {
36
+ this._isReady = false;
37
+ return new Promise((resolve, reject) => {
38
+ this.server.close(err => {
39
+ if (err) {
40
+ return reject(err);
41
+ }
42
+ resolve();
43
+ });
44
+ });
45
+ }
46
+ write(response, statusCode, body) {
47
+ response.writeHead(statusCode, { 'Content-Type': 'text/javascript' });
48
+ response.end(body, 'utf-8');
49
+ }
50
+ }
51
+ exports.DeploymentStatus = DeploymentStat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,EAAE,EAAE;YACrC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE;gBACtB,IAAI,GAAG,EAAE;oBACP,OAAO,MAAM,CAAC,GAAG,CAAC,CAAA;iBACnB;gBACD,OAAO,EAAE,CAAA;YACX,CAAC,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;IACJ,CAAC;IAEO,KAAK,CAAC,QAA6B,EAAE,UAAkB,EAAE,IAAY;QAC3E,QAAQ,CAAC,SAAS,CAAC,UAAU,EAAE,EAAE,cAAc,EAAE,iBAAiB,EAAE,CAAC,CAAA;QACrE,QAAQ,CAAC,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;IAC7B,CAAC;CACF;AAhDD,4CAgDC"}
@@ -0,0 +1,3 @@
1
+ export * from './DeploymentStatus';
2
+ export * from './onShutdown';
3
+ export * from './workers';
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./DeploymentStatus"), exports);
5
+ tslib_1.__exportStar(require("./onShutdown"), exports);
6
+ tslib_1.__exportStar(require("./workers"), exports);
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,6DAAkC;AAClC,uDAA4B;AAC5B,oDAAyB"}
@@ -0,0 +1 @@
1
+ export declare const onShutdown: (onShutdown: () => Promise<void>, timeoutMs?: number) => void;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.onShutdown = void 0;
4
+ const core_1 = require("@pjts/core");
5
+ const onShutdown = (onShutdown, timeoutMs = 5000) => {
6
+ const signals = ['SIGINT', 'SIGTERM'];
7
+ let received = false;
8
+ signals.forEach(signal => {
9
+ process.on(signal, async () => {
10
+ if (received)
11
+ return;
12
+ received = true;
13
+ process.exitCode = 0;
14
+ const timeout = setTimeout(() => {
15
+ core_1.Logger.error(`Killing process after timeout of ${timeoutMs} ms from receiving singnal: ${signal}`);
16
+ process.exit(1);
17
+ }, timeoutMs).unref();
18
+ await onShutdown();
19
+ core_1.Logger.info('Shutdown completed');
20
+ process.exit(0);
21
+ clearTimeout(timeout);
22
+ });
23
+ });
24
+ process.on('unhandledRejection', (err) => {
25
+ console.error(err);
26
+ process.exit(1);
27
+ });
28
+ };
29
+ exports.onShutdown = onShutdown;
30
+ //# sourceMappingURL=onShutdown.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"onShutdown.js","sourceRoot":"","sources":["../src/onShutdown.ts"],"names":[],"mappings":";;;AAAA,qCAAmC;AAE5B,MAAM,UAAU,GAAG,CAAC,UAA+B,EAAE,SAAS,GAAG,IAAI,EAAQ,EAAE;IACpF,MAAM,OAAO,GAAgC,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAA;IAClE,IAAI,QAAQ,GAAG,KAAK,CAAC;IACrB,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;QACvB,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,KAAK,IAAI,EAAE;YAC5B,IAAI,QAAQ;gBAAE,OAAO;YACrB,QAAQ,GAAG,IAAI,CAAC;YAChB,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAA;YACpB,MAAM,OAAO,GAAG,UAAU,CAAC,GAAG,EAAE;gBAC9B,aAAM,CAAC,KAAK,CAAC,oCAAoC,SAAS,+BAA+B,MAAM,EAAE,CAAC,CAAA;gBAClG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;YACjB,CAAC,EAAE,SAAS,CAAC,CAAC,KAAK,EAAE,CAAA;YAErB,MAAM,UAAU,EAAE,CAAA;YAClB,aAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAA;YACjC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;YACf,YAAY,CAAC,OAAO,CAAC,CAAA;QACvB,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;IACF,OAAO,CAAC,EAAE,CAAC,oBAAoB,EAAE,CAAC,GAAY,EAAE,EAAE;QAChD,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QAClB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IACjB,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA;AAvBY,QAAA,UAAU,cAuBtB"}
@@ -0,0 +1,6 @@
1
+ export type Abort = () => Promise<void>;
2
+ export interface Worker {
3
+ start(): Promise<void>;
4
+ stop(): Promise<void>;
5
+ }
6
+ export declare const runWorkers: (workers: Worker[], processExitTimeout: number) => Promise<void>;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runWorkers = void 0;
4
+ const core_1 = require("@pjts/core");
5
+ const runWorker = async (worker, processExitTimeout) => {
6
+ try {
7
+ await worker.start();
8
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
+ }
10
+ catch (err) {
11
+ console.log(err);
12
+ core_1.Logger.error(`runWorker: ${worker.constructor.name}: ${err}`);
13
+ core_1.Logger.error(`exiting process on timeout: ${processExitTimeout}`);
14
+ setTimeout(() => {
15
+ process.exit(1);
16
+ }, processExitTimeout);
17
+ }
18
+ };
19
+ const runWorkers = async (workers, processExitTimeout) => {
20
+ for (const worker of workers) {
21
+ const timeout = setTimeout(() => {
22
+ core_1.Logger.warning(`Worker ${worker.constructor.name} startup takes longer than expected...`);
23
+ }, 5 * 1000);
24
+ await runWorker(worker, processExitTimeout);
25
+ clearTimeout(timeout);
26
+ }
27
+ };
28
+ exports.runWorkers = runWorkers;
29
+ //# sourceMappingURL=workers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"workers.js","sourceRoot":"","sources":["../src/workers.ts"],"names":[],"mappings":";;;AAAA,qCAAmC;AASnC,MAAM,SAAS,GAAG,KAAK,EAAE,MAAc,EAAE,kBAA0B,EAAiB,EAAE;IACpF,IAAI;QACF,MAAM,MAAM,CAAC,KAAK,EAAE,CAAA;QACpB,8DAA8D;KAC/D;IAAC,OAAO,GAAQ,EAAE;QACjB,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAChB,aAAM,CAAC,KAAK,CAAC,cAAc,MAAM,CAAC,WAAW,CAAC,IAAI,KAAK,GAAG,EAAE,CAAC,CAAA;QAC7D,aAAM,CAAC,KAAK,CAAC,+BAA+B,kBAAkB,EAAE,CAAC,CAAA;QACjE,UAAU,CAAC,GAAG,EAAE;YACd,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QACjB,CAAC,EAAE,kBAAkB,CAAC,CAAA;KACvB;AACH,CAAC,CAAA;AAEM,MAAM,UAAU,GAAG,KAAK,EAAE,OAAiB,EAAE,kBAA0B,EAAiB,EAAE;IAC/F,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE;QAC5B,MAAM,OAAO,GAAG,UAAU,CAAC,GAAG,EAAE;YAC9B,aAAM,CAAC,OAAO,CAAC,UAAU,MAAM,CAAC,WAAW,CAAC,IAAI,wCAAwC,CAAC,CAAA;QAC3F,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,CAAA;QACZ,MAAM,SAAS,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAA;QAC3C,YAAY,CAAC,OAAO,CAAC,CAAA;KACtB;AACH,CAAC,CAAA;AARY,QAAA,UAAU,cAQtB"}
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@pjts/server",
3
+ "version": "0.0.50",
4
+ "description": "Piotr Józefów DDD utility package",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "author": "Piotr Józefów",
8
+ "license": "MIT",
9
+ "engines": {
10
+ "node": "^18.14.0"
11
+ },
12
+ "engineStrict": true,
13
+ "scripts": {
14
+ "lint": "eslint src/ --ext .js,.jsx,.ts,.tsx",
15
+ "test": "NODE_ENV=test jest --coverage=false --verbose",
16
+ "test:watch": "NODE_ENV=test jest --coverage=false --watchAll --testNamePattern=",
17
+ "test:coverage": "npm run test --collectCoverage",
18
+ "build": "rm -rf ./dist && tsc",
19
+ "package": "npm run build && npm publish --access=public",
20
+ "prepare": "npm install @pjts/core@latest & npm version patch"
21
+ },
22
+ "devDependencies": {
23
+ "@types/jest": "29.4.0",
24
+ "jest": "29.4.2",
25
+ "prettier": "2.8.4",
26
+ "ts-jest": "29.0.5",
27
+ "ts-node": "10.9.1",
28
+ "typescript": "4.9.5"
29
+ },
30
+ "dependencies": {
31
+ "@pjts/core": "^0.0.59",
32
+ "@types/node": "18.13.0"
33
+ },
34
+ "publishConfig": {
35
+ "registry": "https://registry.npmjs.org"
36
+ }
37
+ }