diff --git a/src/lib/__snapshots__/create-config.test.ts.snap b/src/lib/__snapshots__/create-config.test.ts.snap index 66585bb673..f7299a9602 100644 --- a/src/lib/__snapshots__/create-config.test.ts.snap +++ b/src/lib/__snapshots__/create-config.test.ts.snap @@ -61,7 +61,6 @@ exports[`should create default config 1`] = ` "_maxListeners": undefined, Symbol(kCapture): false, }, - "eventHook": undefined, "experimental": { "externalResolver": { "isEnabled": [Function], diff --git a/src/lib/create-config.ts b/src/lib/create-config.ts index 43f74a527a..1369a48e18 100644 --- a/src/lib/create-config.ts +++ b/src/lib/create-config.ts @@ -45,13 +45,14 @@ import { import FlagResolver from './util/flag-resolver'; import { validateOrigins } from './util/validateOrigin'; -const safeToUpper = (s: string) => (s ? s.toUpperCase() : s); +const safeToUpper = (s?: string) => (s ? s.toUpperCase() : s); export function authTypeFromString( s?: string, defaultType: IAuthType = IAuthType.OPEN_SOURCE, ): IAuthType { - return IAuthType[safeToUpper(s)] || defaultType; + const upperS = safeToUpper(s); + return upperS && IAuthType[upperS] ? IAuthType[upperS] : defaultType; } function mergeAll(objects: Partial[]): T { @@ -93,7 +94,7 @@ function loadClientCachingOptions( return mergeAll([ defaultClientCachingOptions, - options.clientFeatureCaching, + options.clientFeatureCaching || {}, envs, ]); } @@ -249,7 +250,10 @@ const formatServerOptions = ( }; }; -const loadTokensFromString = (tokenString: String, tokenType: ApiTokenType) => { +const loadTokensFromString = ( + tokenString: String | undefined, + tokenType: ApiTokenType, +) => { if (!tokenString) { return []; } @@ -297,7 +301,7 @@ const loadEnvironmentEnableOverrides = () => { }; const parseCspConfig = ( - cspConfig: ICspDomainOptions, + cspConfig?: ICspDomainOptions, ): ICspDomainConfig | undefined => { if (!cspConfig) { return undefined; @@ -366,12 +370,12 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig { defaultDbOptions, dbPort(extraDbOptions), dbPort(fileDbOptions), - options.db, + options.db || {}, ]); const session: ISessionOption = mergeAll([ defaultSessionOption, - options.session, + options.session || {}, ]); const logLevel = @@ -381,12 +385,12 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig { const server: IServerOption = mergeAll([ defaultServerOption, - formatServerOptions(options.server), + formatServerOptions(options.server) || {}, ]); const versionCheck: IVersionOption = mergeAll([ defaultVersionOption, - options.versionCheck, + options.versionCheck || {}, ]); const initApiTokens = loadInitApiTokens(); @@ -403,7 +407,7 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig { const importSetting: IImportOption = mergeAll([ defaultImport, - options.import, + options.import || {}, ]); const experimental = loadExperimental(options); @@ -411,7 +415,7 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig { const ui = loadUI(options); - const email: IEmailOption = mergeAll([defaultEmail, options.email]); + const email: IEmailOption = mergeAll([defaultEmail, options.email || {}]); let listen: IListeningPipe | IListeningHost; if (server.pipe) { @@ -483,7 +487,6 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig { disableLegacyFeaturesApi, preHook: options.preHook, preRouterHook: options.preRouterHook, - eventHook: options.eventHook, enterpriseVersion: options.enterpriseVersion, eventBus: new EventEmitter(), environmentEnableOverrides, diff --git a/src/lib/event-hook.test.ts b/src/lib/event-hook.test.ts deleted file mode 100644 index 447f49043c..0000000000 --- a/src/lib/event-hook.test.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { EventEmitter } from 'events'; -import { addEventHook } from './event-hook'; -import { - FEATURE_CREATED, - FEATURE_UPDATED, - FEATURE_ARCHIVED, - FEATURE_REVIVED, -} from './types/events'; - -const eventStore = new EventEmitter(); -const o = {}; - -function testHook(feature, data) { - o[feature] = data; -} - -beforeAll(() => { - addEventHook(testHook, eventStore); -}); - -[FEATURE_CREATED, FEATURE_UPDATED, FEATURE_ARCHIVED, FEATURE_REVIVED].forEach( - (feature) => { - test(`should invoke hook on ${feature}`, () => { - const data = { dataKey: feature }; - eventStore.emit(feature, data); - expect(o[feature] === data).toBe(true); - }); - }, -); diff --git a/src/lib/event-hook.ts b/src/lib/event-hook.ts deleted file mode 100644 index cf6bc2a847..0000000000 --- a/src/lib/event-hook.ts +++ /dev/null @@ -1,26 +0,0 @@ -import EventEmitter from 'events'; -import { EventHook } from './types/option'; -import { - FEATURE_CREATED, - FEATURE_UPDATED, - FEATURE_ARCHIVED, - FEATURE_REVIVED, -} from './types/events'; - -export const addEventHook = ( - eventHook: EventHook, - eventStore: Pick, -): void => { - eventStore.on(FEATURE_CREATED, (data) => { - eventHook(FEATURE_CREATED, data); - }); - eventStore.on(FEATURE_UPDATED, (data) => { - eventHook(FEATURE_UPDATED, data); - }); - eventStore.on(FEATURE_ARCHIVED, (data) => { - eventHook(FEATURE_ARCHIVED, data); - }); - eventStore.on(FEATURE_REVIVED, (data) => { - eventHook(FEATURE_REVIVED, data); - }); -}; diff --git a/src/lib/internals.ts b/src/lib/internals.ts index 5cadba2342..6ce89404bd 100644 --- a/src/lib/internals.ts +++ b/src/lib/internals.ts @@ -1,6 +1,5 @@ export * from './logger'; export * from './metrics'; -export * from './event-hook'; export * from './metric-events'; export * from './default-custom-auth-deny-all'; export * from './addons'; diff --git a/src/lib/server-impl.test.ts b/src/lib/server-impl.test.ts index f951fa970f..346b5c68d3 100644 --- a/src/lib/server-impl.test.ts +++ b/src/lib/server-impl.test.ts @@ -93,20 +93,6 @@ test('should call preRouterHook', async () => { await stop(); }); -test('should call eventHook', async () => { - let called = 0; - const config = createTestConfig({ - server: { port: 0 }, - eventHook: () => { - called++; - }, - }); - const { stop } = await start(config); - eventStore.emit('feature-created', {}); - expect(called === 1).toBe(true); - await stop(); -}); - test('should auto-create server on start()', async () => { const { server, stop } = await start( createTestConfig({ server: { port: 0 } }), diff --git a/src/lib/server-impl.ts b/src/lib/server-impl.ts index f762365929..287bf2099b 100644 --- a/src/lib/server-impl.ts +++ b/src/lib/server-impl.ts @@ -7,7 +7,6 @@ import { createMetricsMonitor } from './metrics'; import { createStores } from './db'; import { createServices, scheduleServices } from './services'; import { createConfig } from './create-config'; -import { addEventHook } from './event-hook'; import registerGracefulShutdown from './util/graceful-shutdown'; import { createDb } from './db/db-pool'; import sessionDb from './middleware/session-db'; @@ -70,9 +69,6 @@ async function createApp( } const app = await getApp(config, stores, services, unleashSession, db); - if (typeof config.eventHook === 'function') { - addEventHook(config.eventHook, stores.eventStore); - } await metricsMonitor.startMonitoring( config, stores, diff --git a/src/lib/types/option.ts b/src/lib/types/option.ts index 9773e3541a..17c4c8035d 100644 --- a/src/lib/types/option.ts +++ b/src/lib/types/option.ts @@ -4,8 +4,6 @@ import { ILegacyApiTokenCreate } from './models/api-token'; import { IFlagResolver, IExperimentalOptions, IFlags } from './experimental'; import SMTPTransport from 'nodemailer/lib/smtp-transport'; -export type EventHook = (eventName: string, data: object) => void; - export interface ISSLOption { rejectUnauthorized: boolean; ca?: string; @@ -112,7 +110,6 @@ export interface IUnleashOptions { enableOAS?: boolean; preHook?: Function; preRouterHook?: Function; - eventHook?: EventHook; enterpriseVersion?: string; disableLegacyFeaturesApi?: boolean; inlineSegmentConstraints?: boolean; @@ -195,7 +192,6 @@ export interface IUnleashConfig { enableOAS: boolean; preHook?: Function; preRouterHook?: Function; - eventHook?: EventHook; enterpriseVersion?: string; eventBus: EventEmitter; disableLegacyFeaturesApi?: boolean; diff --git a/src/lib/util/format-base-uri.ts b/src/lib/util/format-base-uri.ts index 328dfc528b..27388336a8 100644 --- a/src/lib/util/format-base-uri.ts +++ b/src/lib/util/format-base-uri.ts @@ -1,14 +1,14 @@ -export const formatBaseUri = (input: string): string => { +export const formatBaseUri = (input?: string): string => { if (!input) return ''; const firstChar = input[0]; const lastChar = input[input.length - 1]; if (firstChar === '/' && lastChar === '/') { - return input.substr(0, input.length - 1); + return input.substring(0, input.length - 1); } if (firstChar !== '/' && lastChar === '/') { - return `/${input.substr(0, input.length - 1)}`; + return `/${input.substring(0, input.length - 1)}`; } if (firstChar !== '/') { diff --git a/src/lib/util/parseEnvVar.ts b/src/lib/util/parseEnvVar.ts index 91d5db0f76..035bf3ece0 100644 --- a/src/lib/util/parseEnvVar.ts +++ b/src/lib/util/parseEnvVar.ts @@ -1,4 +1,10 @@ -export function parseEnvVarNumber(envVar: string, defaultVal: number): number { +export function parseEnvVarNumber( + envVar: string | undefined, + defaultVal: number, +): number { + if (!envVar) { + return defaultVal; + } const parsed = Number.parseInt(envVar, 10); if (Number.isNaN(parsed)) {