2021-04-22 10:07:10 +02:00
|
|
|
import { parse } from 'pg-connection-string';
|
|
|
|
import merge from 'deepmerge';
|
2021-05-05 11:36:22 +02:00
|
|
|
import * as fs from 'fs';
|
2021-04-22 10:07:10 +02:00
|
|
|
import {
|
|
|
|
IUnleashOptions,
|
|
|
|
IUnleashConfig,
|
|
|
|
IDBOption,
|
|
|
|
ISessionOption,
|
|
|
|
IServerOption,
|
|
|
|
IVersionOption,
|
|
|
|
IAuthOption,
|
|
|
|
IAuthType,
|
|
|
|
IImportOption,
|
|
|
|
IEmailOption,
|
|
|
|
IListeningPipe,
|
|
|
|
IListeningHost,
|
2021-10-13 20:52:44 +02:00
|
|
|
IUIConfig,
|
2022-05-31 11:32:15 +02:00
|
|
|
ICspDomainConfig,
|
|
|
|
ICspDomainOptions,
|
2022-06-08 09:43:37 +02:00
|
|
|
IClientCachingOption,
|
2021-04-22 10:07:10 +02:00
|
|
|
} from './types/option';
|
2021-04-23 15:31:12 +02:00
|
|
|
import { getDefaultLogProvider, LogLevel, validateLogProvider } from './logger';
|
2021-04-29 15:18:58 +02:00
|
|
|
import { defaultCustomAuthDenyAll } from './default-custom-auth-deny-all';
|
2021-05-03 12:28:59 +02:00
|
|
|
import { formatBaseUri } from './util/format-base-uri';
|
2021-11-02 15:13:46 +01:00
|
|
|
import { minutesToMilliseconds, secondsToMilliseconds } from 'date-fns';
|
2021-12-09 21:02:58 +01:00
|
|
|
import EventEmitter from 'events';
|
2022-04-06 08:11:41 +02:00
|
|
|
import {
|
|
|
|
ApiTokenType,
|
|
|
|
mapLegacyToken,
|
|
|
|
validateApiToken,
|
|
|
|
} from './types/models/api-token';
|
2022-08-19 08:09:44 +02:00
|
|
|
import {
|
|
|
|
parseEnvVarBoolean,
|
|
|
|
parseEnvVarNumber,
|
|
|
|
parseEnvVarStrings,
|
|
|
|
} from './util/parseEnvVar';
|
2022-08-26 08:22:42 +02:00
|
|
|
import {
|
|
|
|
defaultExperimentalOptions,
|
|
|
|
IExperimentalOptions,
|
|
|
|
} from './types/experimental';
|
2022-06-02 10:44:45 +02:00
|
|
|
import {
|
|
|
|
DEFAULT_SEGMENT_VALUES_LIMIT,
|
|
|
|
DEFAULT_STRATEGY_SEGMENTS_LIMIT,
|
|
|
|
} from './util/segments';
|
2022-08-26 08:22:42 +02:00
|
|
|
import FlagResolver from './util/flag-resolver';
|
2022-08-26 09:09:48 +02:00
|
|
|
import { validateOrigins } from './util/validateOrigin';
|
2021-04-22 10:07:10 +02:00
|
|
|
|
2021-04-22 15:04:08 +02:00
|
|
|
const safeToUpper = (s: string) => (s ? s.toUpperCase() : s);
|
|
|
|
|
|
|
|
export function authTypeFromString(
|
|
|
|
s?: string,
|
|
|
|
defaultType: IAuthType = IAuthType.OPEN_SOURCE,
|
|
|
|
): IAuthType {
|
|
|
|
return IAuthType[safeToUpper(s)] || defaultType;
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
function mergeAll<T>(objects: Partial<T>[]): T {
|
2021-08-12 15:04:37 +02:00
|
|
|
return merge.all<T>(objects.filter((i) => i));
|
2021-04-22 10:07:10 +02:00
|
|
|
}
|
|
|
|
|
2022-06-02 10:44:45 +02:00
|
|
|
function loadExperimental(options: IUnleashOptions): IExperimentalOptions {
|
2022-08-19 10:38:26 +02:00
|
|
|
return {
|
2022-08-26 08:22:42 +02:00
|
|
|
...defaultExperimentalOptions,
|
2022-08-19 10:38:26 +02:00
|
|
|
...options.experimental,
|
2022-08-26 08:22:42 +02:00
|
|
|
flags: {
|
|
|
|
...defaultExperimentalOptions.flags,
|
|
|
|
...options.experimental?.flags,
|
|
|
|
},
|
2022-08-19 10:38:26 +02:00
|
|
|
};
|
2021-10-13 20:52:44 +02:00
|
|
|
}
|
2022-08-19 10:38:26 +02:00
|
|
|
|
2022-06-08 09:43:37 +02:00
|
|
|
const defaultClientCachingOptions: IClientCachingOption = {
|
|
|
|
enabled: true,
|
|
|
|
maxAge: 600,
|
|
|
|
};
|
|
|
|
|
|
|
|
function loadClientCachingOptions(
|
|
|
|
options: IUnleashOptions,
|
|
|
|
): IClientCachingOption {
|
|
|
|
let envs: Partial<IClientCachingOption> = {};
|
|
|
|
if (process.env.CLIENT_FEATURE_CACHING_MAXAGE) {
|
|
|
|
envs.maxAge = parseEnvVarNumber(
|
|
|
|
process.env.CLIENT_FEATURE_CACHING_MAXAGE,
|
|
|
|
600,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (process.env.CLIENT_FEATURE_CACHING_ENABLED) {
|
|
|
|
envs.enabled = parseEnvVarBoolean(
|
|
|
|
process.env.CLIENT_FEATURE_CACHING_ENABLED,
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mergeAll([
|
|
|
|
defaultClientCachingOptions,
|
|
|
|
options.clientFeatureCaching,
|
|
|
|
envs,
|
|
|
|
]);
|
|
|
|
}
|
2021-10-13 20:52:44 +02:00
|
|
|
|
2021-11-26 15:31:36 +01:00
|
|
|
function loadUI(options: IUnleashOptions): IUIConfig {
|
2021-10-13 20:52:44 +02:00
|
|
|
const uiO = options.ui || {};
|
2022-10-25 13:10:27 +02:00
|
|
|
const ui: IUIConfig = {
|
|
|
|
environment: 'Open Source',
|
|
|
|
};
|
2021-10-13 20:52:44 +02:00
|
|
|
|
2021-11-26 15:31:36 +01:00
|
|
|
ui.flags = {
|
2022-08-26 08:22:42 +02:00
|
|
|
ENABLE_DARK_MODE_SUPPORT: false,
|
2021-11-26 15:31:36 +01:00
|
|
|
};
|
2022-11-04 14:43:12 +01:00
|
|
|
return mergeAll([ui, uiO]);
|
2021-10-13 20:52:44 +02:00
|
|
|
}
|
|
|
|
|
2022-06-10 08:47:31 +02:00
|
|
|
const dateHandlingCallback = (connection, callback) => {
|
|
|
|
connection.query("set datestyle to 'ISO, DMY';", (err: any) => {
|
|
|
|
callback(err, connection);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
const defaultDbOptions: IDBOption = {
|
|
|
|
user: process.env.DATABASE_USERNAME,
|
|
|
|
password: process.env.DATABASE_PASSWORD,
|
|
|
|
host: process.env.DATABASE_HOST,
|
2022-06-02 10:44:45 +02:00
|
|
|
port: parseEnvVarNumber(process.env.DATABASE_PORT, 5432),
|
2021-04-22 10:07:10 +02:00
|
|
|
database: process.env.DATABASE_NAME || 'unleash',
|
|
|
|
ssl:
|
|
|
|
process.env.DATABASE_SSL != null
|
|
|
|
? JSON.parse(process.env.DATABASE_SSL)
|
2021-04-28 10:55:11 +02:00
|
|
|
: { rejectUnauthorized: false },
|
2021-04-22 10:07:10 +02:00
|
|
|
driver: 'postgres',
|
|
|
|
version: process.env.DATABASE_VERSION,
|
2021-11-02 15:13:46 +01:00
|
|
|
acquireConnectionTimeout: secondsToMilliseconds(30),
|
2021-04-22 10:07:10 +02:00
|
|
|
pool: {
|
2022-06-02 10:44:45 +02:00
|
|
|
min: parseEnvVarNumber(process.env.DATABASE_POOL_MIN, 0),
|
|
|
|
max: parseEnvVarNumber(process.env.DATABASE_POOL_MAX, 4),
|
|
|
|
idleTimeoutMillis: parseEnvVarNumber(
|
2021-04-22 10:07:10 +02:00
|
|
|
process.env.DATABASE_POOL_IDLE_TIMEOUT_MS,
|
2021-11-02 15:13:46 +01:00
|
|
|
secondsToMilliseconds(30),
|
2021-04-22 10:07:10 +02:00
|
|
|
),
|
2022-06-10 08:47:31 +02:00
|
|
|
...(parseEnvVarBoolean(process.env.ALLOW_NON_STANDARD_DB_DATES, false)
|
|
|
|
? { afterCreate: dateHandlingCallback }
|
|
|
|
: {}),
|
2021-09-03 12:06:58 +02:00
|
|
|
propagateCreateError: false,
|
2021-04-22 10:07:10 +02:00
|
|
|
},
|
|
|
|
schema: process.env.DATABASE_SCHEMA || 'public',
|
|
|
|
disableMigration: false,
|
2022-04-07 20:55:56 +02:00
|
|
|
applicationName: process.env.DATABASE_APPLICATION_NAME || 'unleash',
|
2021-04-22 10:07:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultSessionOption: ISessionOption = {
|
2022-06-02 10:44:45 +02:00
|
|
|
ttlHours: parseEnvVarNumber(process.env.SESSION_TTL_HOURS, 48),
|
2022-06-03 11:50:58 +02:00
|
|
|
clearSiteDataOnLogout: parseEnvVarBoolean(
|
|
|
|
process.env.SESSION_CLEAR_SITE_DATA_ON_LOGOUT,
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
cookieName: 'unleash-session',
|
2021-04-22 10:07:10 +02:00
|
|
|
db: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultServerOption: IServerOption = {
|
|
|
|
pipe: undefined,
|
|
|
|
host: process.env.HTTP_HOST,
|
2022-06-02 10:44:45 +02:00
|
|
|
port: parseEnvVarNumber(process.env.HTTP_PORT || process.env.PORT, 4242),
|
2021-05-03 12:28:59 +02:00
|
|
|
baseUriPath: formatBaseUri(process.env.BASE_URI_PATH),
|
2022-01-06 10:31:00 +01:00
|
|
|
cdnPrefix: process.env.CDN_PREFIX,
|
2021-04-22 10:07:10 +02:00
|
|
|
unleashUrl: process.env.UNLEASH_URL || 'http://localhost:4242',
|
|
|
|
serverMetrics: true,
|
2022-12-08 11:25:39 +01:00
|
|
|
enableHeapSnapshotEnpoint: parseEnvVarBoolean(
|
|
|
|
process.env.ENABLE_HEAP_SNAPSHOT_ENPOINT,
|
|
|
|
false,
|
|
|
|
),
|
2021-11-02 15:13:46 +01:00
|
|
|
keepAliveTimeout: minutesToMilliseconds(1),
|
|
|
|
headersTimeout: secondsToMilliseconds(61),
|
2021-04-22 10:07:10 +02:00
|
|
|
enableRequestLogger: false,
|
2022-06-02 10:44:45 +02:00
|
|
|
gracefulShutdownEnable: parseEnvVarBoolean(
|
2021-06-17 20:33:34 +02:00
|
|
|
process.env.GRACEFUL_SHUTDOWN_ENABLE,
|
|
|
|
true,
|
|
|
|
),
|
2022-06-02 10:44:45 +02:00
|
|
|
gracefulShutdownTimeout: parseEnvVarNumber(
|
2021-06-17 20:33:34 +02:00
|
|
|
process.env.GRACEFUL_SHUTDOWN_TIMEOUT,
|
2021-11-02 15:13:46 +01:00
|
|
|
secondsToMilliseconds(1),
|
2021-06-17 20:33:34 +02:00
|
|
|
),
|
2021-04-22 10:07:10 +02:00
|
|
|
secret: process.env.UNLEASH_SECRET || 'super-secret',
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultVersionOption: IVersionOption = {
|
|
|
|
url: process.env.UNLEASH_VERSION_URL || 'https://version.unleash.run',
|
2022-06-02 10:44:45 +02:00
|
|
|
enable: parseEnvVarBoolean(process.env.CHECK_VERSION, true),
|
2021-04-22 10:07:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultAuthentication: IAuthOption = {
|
2022-06-02 10:44:45 +02:00
|
|
|
enableApiToken: parseEnvVarBoolean(process.env.AUTH_ENABLE_API_TOKEN, true),
|
2021-04-22 15:04:08 +02:00
|
|
|
type: authTypeFromString(process.env.AUTH_TYPE),
|
2021-04-29 15:18:58 +02:00
|
|
|
customAuthHandler: defaultCustomAuthDenyAll,
|
2021-04-22 22:54:08 +02:00
|
|
|
createAdminUser: true,
|
2022-01-05 10:00:59 +01:00
|
|
|
initApiTokens: [],
|
2021-04-22 10:07:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultImport: IImportOption = {
|
|
|
|
file: process.env.IMPORT_FILE,
|
2022-06-02 10:44:45 +02:00
|
|
|
dropBeforeImport: parseEnvVarBoolean(
|
|
|
|
process.env.IMPORT_DROP_BEFORE_IMPORT,
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
keepExisting: parseEnvVarBoolean(process.env.IMPORT_KEEP_EXISTING, false),
|
2021-04-22 10:07:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultEmail: IEmailOption = {
|
|
|
|
host: process.env.EMAIL_HOST,
|
2022-06-02 10:44:45 +02:00
|
|
|
secure: parseEnvVarBoolean(process.env.EMAIL_SECURE, false),
|
|
|
|
port: parseEnvVarNumber(process.env.EMAIL_PORT, 587),
|
2021-04-22 10:07:10 +02:00
|
|
|
sender: process.env.EMAIL_SENDER || 'noreply@unleash-hosted.com',
|
|
|
|
smtpuser: process.env.EMAIL_USER,
|
|
|
|
smtppass: process.env.EMAIL_PASSWORD,
|
|
|
|
};
|
|
|
|
|
|
|
|
const dbPort = (dbConfig: Partial<IDBOption>): Partial<IDBOption> => {
|
|
|
|
if (typeof dbConfig.port === 'string') {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
dbConfig.port = Number.parseInt(dbConfig.port, 10);
|
|
|
|
}
|
|
|
|
return dbConfig;
|
|
|
|
};
|
|
|
|
|
2021-04-29 15:18:58 +02:00
|
|
|
const removeUndefinedKeys = (o: object): object =>
|
|
|
|
Object.keys(o).reduce((a, key) => {
|
|
|
|
if (o[key] !== undefined) {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
a[key] = o[key];
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}, {});
|
|
|
|
|
2021-05-03 12:28:59 +02:00
|
|
|
const formatServerOptions = (
|
|
|
|
serverOptions?: Partial<IServerOption>,
|
|
|
|
): Partial<IServerOption> | undefined => {
|
2022-11-08 15:29:14 +01:00
|
|
|
if (!serverOptions) {
|
|
|
|
return {
|
|
|
|
baseUriPath: formatBaseUri(process.env.BASE_URI_PATH),
|
|
|
|
};
|
|
|
|
}
|
2021-05-03 12:28:59 +02:00
|
|
|
|
|
|
|
/* eslint-disable-next-line */
|
|
|
|
return {
|
|
|
|
...serverOptions,
|
2022-11-08 15:29:14 +01:00
|
|
|
baseUriPath: formatBaseUri(
|
|
|
|
process.env.BASE_URI_PATH || serverOptions.baseUriPath,
|
|
|
|
),
|
2021-05-03 12:28:59 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-03-02 12:44:46 +01:00
|
|
|
const loadTokensFromString = (tokenString: String, tokenType: ApiTokenType) => {
|
|
|
|
if (!tokenString) {
|
2022-01-05 10:00:59 +01:00
|
|
|
return [];
|
|
|
|
}
|
2022-03-02 12:44:46 +01:00
|
|
|
const initApiTokens = tokenString.split(/,\s?/);
|
|
|
|
const tokens = initApiTokens.map((secret) => {
|
|
|
|
const [project = '*', rest] = secret.split(':');
|
|
|
|
const [environment = '*'] = rest.split('.');
|
|
|
|
const token = {
|
|
|
|
createdAt: undefined,
|
|
|
|
project,
|
|
|
|
environment,
|
|
|
|
secret,
|
|
|
|
type: tokenType,
|
|
|
|
username: 'admin',
|
|
|
|
};
|
2022-04-06 08:11:41 +02:00
|
|
|
validateApiToken(mapLegacyToken(token));
|
2022-03-02 12:44:46 +01:00
|
|
|
return token;
|
|
|
|
});
|
|
|
|
return tokens;
|
|
|
|
};
|
|
|
|
|
|
|
|
const loadInitApiTokens = () => {
|
|
|
|
return [
|
|
|
|
...loadTokensFromString(
|
|
|
|
process.env.INIT_ADMIN_API_TOKENS,
|
|
|
|
ApiTokenType.ADMIN,
|
|
|
|
),
|
|
|
|
...loadTokensFromString(
|
|
|
|
process.env.INIT_CLIENT_API_TOKENS,
|
|
|
|
ApiTokenType.CLIENT,
|
|
|
|
),
|
2022-12-12 20:40:18 +01:00
|
|
|
...loadTokensFromString(
|
|
|
|
process.env.INIT_FRONTEND_API_TOKENS,
|
|
|
|
ApiTokenType.FRONTEND,
|
|
|
|
),
|
2022-03-02 12:44:46 +01:00
|
|
|
];
|
2022-01-05 10:00:59 +01:00
|
|
|
};
|
|
|
|
|
2022-03-11 10:16:58 +01:00
|
|
|
const loadEnvironmentEnableOverrides = () => {
|
|
|
|
const environmentsString = process.env.ENABLED_ENVIRONMENTS;
|
|
|
|
if (environmentsString) {
|
|
|
|
return environmentsString.split(',');
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
2022-05-31 11:32:15 +02:00
|
|
|
const parseCspConfig = (
|
|
|
|
cspConfig: ICspDomainOptions,
|
|
|
|
): ICspDomainConfig | undefined => {
|
|
|
|
if (!cspConfig) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
defaultSrc: cspConfig.defaultSrc || [],
|
|
|
|
fontSrc: cspConfig.fontSrc || [],
|
|
|
|
scriptSrc: cspConfig.scriptSrc || [],
|
|
|
|
imgSrc: cspConfig.imgSrc || [],
|
|
|
|
styleSrc: cspConfig.styleSrc || [],
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const parseCspEnvironmentVariables = (): ICspDomainConfig => {
|
|
|
|
const defaultSrc = process.env.CSP_ALLOWED_DEFAULT?.split(',') || [];
|
|
|
|
const fontSrc = process.env.CSP_ALLOWED_FONT?.split(',') || [];
|
|
|
|
const styleSrc = process.env.CSP_ALLOWED_STYLE?.split(',') || [];
|
|
|
|
const scriptSrc = process.env.CSP_ALLOWED_SCRIPT?.split(',') || [];
|
|
|
|
const imgSrc = process.env.CSP_ALLOWED_IMG?.split(',') || [];
|
|
|
|
return {
|
|
|
|
defaultSrc,
|
|
|
|
fontSrc,
|
|
|
|
styleSrc,
|
|
|
|
scriptSrc,
|
|
|
|
imgSrc,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-08-26 09:09:48 +02:00
|
|
|
const parseFrontendApiOrigins = (options: IUnleashOptions): string[] => {
|
|
|
|
const frontendApiOrigins = parseEnvVarStrings(
|
|
|
|
process.env.UNLEASH_FRONTEND_API_ORIGINS,
|
|
|
|
options.frontendApiOrigins || ['*'],
|
|
|
|
);
|
|
|
|
|
|
|
|
const error = validateOrigins(frontendApiOrigins);
|
|
|
|
if (error) {
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return frontendApiOrigins;
|
|
|
|
};
|
|
|
|
|
2021-04-22 15:04:08 +02:00
|
|
|
export function createConfig(options: IUnleashOptions): IUnleashConfig {
|
2021-04-22 10:07:10 +02:00
|
|
|
let extraDbOptions = {};
|
2021-05-03 12:28:59 +02:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
if (options.databaseUrl) {
|
|
|
|
extraDbOptions = parse(options.databaseUrl);
|
|
|
|
} else if (process.env.DATABASE_URL) {
|
|
|
|
extraDbOptions = parse(process.env.DATABASE_URL);
|
|
|
|
}
|
2021-05-05 11:36:22 +02:00
|
|
|
let fileDbOptions = {};
|
|
|
|
if (options.databaseUrlFile && fs.existsSync(options.databaseUrlFile)) {
|
|
|
|
fileDbOptions = parse(
|
|
|
|
fs.readFileSync(options.databaseUrlFile, 'utf-8'),
|
|
|
|
);
|
|
|
|
} else if (
|
|
|
|
process.env.DATABASE_URL_FILE &&
|
|
|
|
fs.existsSync(process.env.DATABASE_URL_FILE)
|
|
|
|
) {
|
|
|
|
fileDbOptions = parse(
|
|
|
|
fs.readFileSync(process.env.DATABASE_URL_FILE, 'utf-8'),
|
|
|
|
);
|
|
|
|
}
|
2021-04-22 10:07:10 +02:00
|
|
|
const db: IDBOption = mergeAll<IDBOption>([
|
|
|
|
defaultDbOptions,
|
|
|
|
dbPort(extraDbOptions),
|
2021-05-05 11:36:22 +02:00
|
|
|
dbPort(fileDbOptions),
|
2021-04-22 10:07:10 +02:00
|
|
|
options.db,
|
|
|
|
]);
|
|
|
|
|
|
|
|
const session: ISessionOption = mergeAll([
|
|
|
|
defaultSessionOption,
|
|
|
|
options.session,
|
|
|
|
]);
|
|
|
|
|
2021-04-23 15:31:12 +02:00
|
|
|
const logLevel =
|
|
|
|
options.logLevel || LogLevel[process.env.LOG_LEVEL] || LogLevel.error;
|
|
|
|
const getLogger = options.getLogger || getDefaultLogProvider(logLevel);
|
2021-04-22 10:07:10 +02:00
|
|
|
validateLogProvider(getLogger);
|
|
|
|
|
|
|
|
const server: IServerOption = mergeAll([
|
|
|
|
defaultServerOption,
|
2021-05-03 12:28:59 +02:00
|
|
|
formatServerOptions(options.server),
|
2021-04-22 10:07:10 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
const versionCheck: IVersionOption = mergeAll([
|
|
|
|
defaultVersionOption,
|
|
|
|
options.versionCheck,
|
|
|
|
]);
|
|
|
|
|
2022-01-05 10:00:59 +01:00
|
|
|
const initApiTokens = loadInitApiTokens();
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
const authentication: IAuthOption = mergeAll([
|
|
|
|
defaultAuthentication,
|
2021-04-29 15:18:58 +02:00
|
|
|
options.authentication
|
|
|
|
? removeUndefinedKeys(options.authentication)
|
|
|
|
: options.authentication,
|
2022-01-05 10:00:59 +01:00
|
|
|
{ initApiTokens: initApiTokens },
|
2021-04-22 10:07:10 +02:00
|
|
|
]);
|
|
|
|
|
2022-03-11 10:16:58 +01:00
|
|
|
const environmentEnableOverrides = loadEnvironmentEnableOverrides();
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
const importSetting: IImportOption = mergeAll([
|
|
|
|
defaultImport,
|
|
|
|
options.import,
|
|
|
|
]);
|
|
|
|
|
2021-10-13 20:52:44 +02:00
|
|
|
const experimental = loadExperimental(options);
|
2022-08-26 08:22:42 +02:00
|
|
|
const flagResolver = new FlagResolver(experimental);
|
2021-04-22 10:07:10 +02:00
|
|
|
|
2021-11-26 15:31:36 +01:00
|
|
|
const ui = loadUI(options);
|
2021-10-08 10:09:22 +02:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
const email: IEmailOption = mergeAll([defaultEmail, options.email]);
|
|
|
|
|
|
|
|
let listen: IListeningPipe | IListeningHost;
|
|
|
|
if (server.pipe) {
|
|
|
|
listen = { path: server.pipe };
|
|
|
|
} else {
|
|
|
|
listen = { host: server.host || undefined, port: server.port };
|
|
|
|
}
|
|
|
|
|
2022-09-28 14:23:41 +02:00
|
|
|
const frontendApi = options.frontendApi || {
|
|
|
|
refreshIntervalInMs: parseEnvVarNumber(
|
|
|
|
process.env.FRONTEND_API_REFRESH_INTERVAL_MS,
|
|
|
|
10000,
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
2021-05-25 12:48:37 +02:00
|
|
|
const secureHeaders =
|
2022-06-02 10:44:45 +02:00
|
|
|
options.secureHeaders ||
|
|
|
|
parseEnvVarBoolean(process.env.SECURE_HEADERS, false);
|
2021-05-25 12:48:37 +02:00
|
|
|
|
|
|
|
const enableOAS =
|
2022-06-02 10:44:45 +02:00
|
|
|
options.enableOAS || parseEnvVarBoolean(process.env.ENABLE_OAS, false);
|
2021-05-25 12:48:37 +02:00
|
|
|
|
2022-01-13 11:14:17 +01:00
|
|
|
const disableLegacyFeaturesApi =
|
|
|
|
options.disableLegacyFeaturesApi ||
|
2022-06-02 10:44:45 +02:00
|
|
|
parseEnvVarBoolean(process.env.DISABLE_LEGACY_FEATURES_API, false);
|
2022-01-13 11:14:17 +01:00
|
|
|
|
2022-05-31 11:32:15 +02:00
|
|
|
const additionalCspAllowedDomains: ICspDomainConfig =
|
|
|
|
parseCspConfig(options.additionalCspAllowedDomains) ||
|
|
|
|
parseCspEnvironmentVariables();
|
|
|
|
|
2022-06-02 10:44:45 +02:00
|
|
|
const inlineSegmentConstraints =
|
|
|
|
typeof options.inlineSegmentConstraints === 'boolean'
|
|
|
|
? options.inlineSegmentConstraints
|
|
|
|
: true;
|
|
|
|
|
|
|
|
const segmentValuesLimit = parseEnvVarNumber(
|
|
|
|
process.env.UNLEASH_SEGMENT_VALUES_LIMIT,
|
|
|
|
DEFAULT_SEGMENT_VALUES_LIMIT,
|
|
|
|
);
|
|
|
|
|
|
|
|
const strategySegmentsLimit = parseEnvVarNumber(
|
|
|
|
process.env.UNLEASH_STRATEGY_SEGMENTS_LIMIT,
|
|
|
|
DEFAULT_STRATEGY_SEGMENTS_LIMIT,
|
|
|
|
);
|
|
|
|
|
2022-11-24 16:14:47 +01:00
|
|
|
const accessControlMaxAge = options.accessControlMaxAge
|
|
|
|
? options.accessControlMaxAge
|
2022-12-05 10:04:35 +01:00
|
|
|
: parseEnvVarNumber(process.env.ACCESS_CONTROL_MAX_AGE, 86400);
|
2022-11-24 16:14:47 +01:00
|
|
|
|
2022-06-08 09:43:37 +02:00
|
|
|
const clientFeatureCaching = loadClientCachingOptions(options);
|
|
|
|
|
2022-12-12 14:05:56 +01:00
|
|
|
const prometheusApi = options.prometheusApi || process.env.PROMETHEUS_API;
|
2021-04-22 10:07:10 +02:00
|
|
|
return {
|
|
|
|
db,
|
|
|
|
session,
|
|
|
|
getLogger,
|
|
|
|
server,
|
|
|
|
listen,
|
|
|
|
versionCheck,
|
|
|
|
authentication,
|
|
|
|
ui,
|
|
|
|
import: importSetting,
|
2021-10-13 20:52:44 +02:00
|
|
|
experimental,
|
2022-08-26 08:22:42 +02:00
|
|
|
flagResolver,
|
2022-09-28 14:23:41 +02:00
|
|
|
frontendApi,
|
2021-04-22 10:07:10 +02:00
|
|
|
email,
|
2021-05-25 12:48:37 +02:00
|
|
|
secureHeaders,
|
|
|
|
enableOAS,
|
2022-01-13 11:14:17 +01:00
|
|
|
disableLegacyFeaturesApi,
|
2021-04-22 10:07:10 +02:00
|
|
|
preHook: options.preHook,
|
|
|
|
preRouterHook: options.preRouterHook,
|
|
|
|
eventHook: options.eventHook,
|
2021-04-23 11:05:25 +02:00
|
|
|
enterpriseVersion: options.enterpriseVersion,
|
2021-12-09 21:02:58 +01:00
|
|
|
eventBus: new EventEmitter(),
|
2022-03-11 10:16:58 +01:00
|
|
|
environmentEnableOverrides,
|
2022-05-31 11:32:15 +02:00
|
|
|
additionalCspAllowedDomains,
|
2022-08-26 09:09:48 +02:00
|
|
|
frontendApiOrigins: parseFrontendApiOrigins(options),
|
2022-06-02 10:44:45 +02:00
|
|
|
inlineSegmentConstraints,
|
|
|
|
segmentValuesLimit,
|
|
|
|
strategySegmentsLimit,
|
2022-06-08 09:43:37 +02:00
|
|
|
clientFeatureCaching,
|
2022-11-24 16:14:47 +01:00
|
|
|
accessControlMaxAge,
|
2022-12-12 14:05:56 +01:00
|
|
|
prometheusApi,
|
2021-04-22 10:07:10 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-22 15:04:08 +02:00
|
|
|
module.exports = {
|
|
|
|
createConfig,
|
|
|
|
authTypeFromString,
|
|
|
|
};
|