From 0d0ab14e401f79a284fea854c3554d94ee88089d Mon Sep 17 00:00:00 2001 From: Scott Chang Date: Tue, 9 Apr 2019 14:38:18 -0400 Subject: [PATCH 1/2] Create Typedef for lightship --- types/lightship/index.d.ts | 43 ++++++++++++++++++++++++++++++ types/lightship/lightship-tests.ts | 17 ++++++++++++ types/lightship/tsconfig.json | 23 ++++++++++++++++ types/lightship/tslint.json | 1 + 4 files changed, 84 insertions(+) create mode 100644 types/lightship/index.d.ts create mode 100644 types/lightship/lightship-tests.ts create mode 100644 types/lightship/tsconfig.json create mode 100644 types/lightship/tslint.json diff --git a/types/lightship/index.d.ts b/types/lightship/index.d.ts new file mode 100644 index 0000000000..1bccba1fc5 --- /dev/null +++ b/types/lightship/index.d.ts @@ -0,0 +1,43 @@ +// Type definitions for lightship 1.0 +// Project: https://github.com/gajus/lightship#readme +// Definitions by: Scott Chang +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +/// + +/** + * A teardown function called when shutdown is initialized. + * @param userConfiguration User configuration object + */ +export function createLightship(userConfiguration?: UserConfigurationType): LightshipType; + +export interface UserConfigurationType { + /* Run Iapetus only if service is detected ro be running in Kubernetes. Default: true. */ + detectKubernetes?: boolean; + /* The port on which the Lightship service listens. This port must be different than your main service port, if any. The default port is 9000.*/ + port?: number; + /* An a array of [signal events]{@link https://nodejs.org/api/process.html#process_signal_events}. Default: [SIGTERM]. */ + signals?: ReadonlyArray; + /* A number of milliseconds before force full termination. Default: 60000. */ + timeout?: number; +} + +export interface LightshipType { + /* Checks if server is in SERVER_IS_READY state */ + isServerReady: () => boolean; + /* Checks if server is in SERVER_IS_SHUTTING_DOWN state */ + isServerShuttingDown: () => boolean; + /** + * Registers teardown functions that are called when shutdown is initialized. + * All registered shutdown handlers are executed in the order they have been registered. + * After all shutdown handlers have been executed, Lightship asks `process.exit()` to terminate the process synchronously. + */ + registerShutdownHandler: (shutdownHandler: ShutdownHandlerType) => void; + /* Changes server state to SERVER_IS_SHUTTING_DOWN and initialises the shutdown of the application.*/ + shutdown: () => Promise; + /* Changes server state to SERVER_IS_NOT_READY. */ + signalNotReady: () => void; + /* Changes server state to SERVER_IS_READY. */ + signalReady: () => void; +} + +export type ShutdownHandlerType = () => Promise | void; diff --git a/types/lightship/lightship-tests.ts b/types/lightship/lightship-tests.ts new file mode 100644 index 0000000000..11d8d35100 --- /dev/null +++ b/types/lightship/lightship-tests.ts @@ -0,0 +1,17 @@ +import { createLightship, UserConfigurationType, LightshipType } from 'lightship'; + +const lightshipParams: UserConfigurationType = { + detectKubernetes: false, + timeout: 1000, + port: 50, + signals: ['SIGBUS'] +}; + +const lightship: LightshipType = createLightship(lightshipParams); + +lightship.isServerReady(); // $ExpectType boolean +lightship.isServerShuttingDown(); // $ExpectType boolean +lightship.registerShutdownHandler(() => {}); // $ExpectType void +lightship.shutdown(); // $ExpectType Promise +lightship.signalNotReady(); // $ExpectType void +lightship.signalReady(); // $ExpectType void diff --git a/types/lightship/tsconfig.json b/types/lightship/tsconfig.json new file mode 100644 index 0000000000..f28d94a99f --- /dev/null +++ b/types/lightship/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "lightship-tests.ts" + ] +} diff --git a/types/lightship/tslint.json b/types/lightship/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/lightship/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 669776892f7eb06a4c888b315df3462e05cf5f03 Mon Sep 17 00:00:00 2001 From: Scott Chang Date: Tue, 9 Apr 2019 14:47:43 -0400 Subject: [PATCH 2/2] Correct Lightship version --- types/lightship/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/lightship/index.d.ts b/types/lightship/index.d.ts index 1bccba1fc5..f4a30fda74 100644 --- a/types/lightship/index.d.ts +++ b/types/lightship/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for lightship 1.0 +// Type definitions for lightship 3.0 // Project: https://github.com/gajus/lightship#readme // Definitions by: Scott Chang // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped