mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-22 17:56:30 -05:00
Safely parse numbers from environment vars
This adds the function parseOptionalInt to help parse numbers from environment variables Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
36e721d4a9
commit
52f6310e91
5 changed files with 23 additions and 8 deletions
|
@ -7,7 +7,7 @@
|
|||
import { registerAs } from '@nestjs/config';
|
||||
import * as Joi from 'joi';
|
||||
import { Loglevel } from './loglevel.enum';
|
||||
import { buildErrorMessage, toArrayConfig } from './utils';
|
||||
import { buildErrorMessage, parseOptionalInt, toArrayConfig } from './utils';
|
||||
|
||||
export interface AppConfig {
|
||||
domain: string;
|
||||
|
@ -46,11 +46,10 @@ export default registerAs('appConfig', () => {
|
|||
{
|
||||
domain: process.env.HD_DOMAIN,
|
||||
rendererOrigin: process.env.HD_RENDERER_ORIGIN,
|
||||
port: parseInt(process.env.PORT) || undefined,
|
||||
port: parseOptionalInt(process.env.PORT),
|
||||
loglevel: process.env.HD_LOGLEVEL,
|
||||
forbiddenNoteIds: toArrayConfig(process.env.HD_FORBIDDEN_NOTE_IDS, ','),
|
||||
maxDocumentLength:
|
||||
parseInt(process.env.HD_MAX_DOCUMENT_LENGTH) || undefined,
|
||||
maxDocumentLength: parseOptionalInt(process.env.HD_MAX_DOCUMENT_LENGTH),
|
||||
},
|
||||
{
|
||||
abortEarly: false,
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
import * as Joi from 'joi';
|
||||
import { DatabaseDialect } from './database-dialect.enum';
|
||||
import { registerAs } from '@nestjs/config';
|
||||
import { buildErrorMessage } from './utils';
|
||||
import { buildErrorMessage, parseOptionalInt } from './utils';
|
||||
|
||||
export interface DatabaseConfig {
|
||||
username: string;
|
||||
|
@ -62,7 +62,7 @@ export default registerAs('databaseConfig', () => {
|
|||
password: process.env.HD_DATABASE_PASS,
|
||||
database: process.env.HD_DATABASE_NAME,
|
||||
host: process.env.HD_DATABASE_HOST,
|
||||
port: parseInt(process.env.HD_DATABASE_PORT) || undefined,
|
||||
port: parseOptionalInt(process.env.HD_DATABASE_PORT),
|
||||
storage: process.env.HD_DATABASE_STORAGE,
|
||||
dialect: process.env.HD_DATABASE_DIALECT,
|
||||
},
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
import * as Joi from 'joi';
|
||||
import { registerAs } from '@nestjs/config';
|
||||
import { buildErrorMessage } from './utils';
|
||||
import { buildErrorMessage, parseOptionalInt } from './utils';
|
||||
|
||||
export interface HstsConfig {
|
||||
enable: boolean;
|
||||
|
@ -32,7 +32,7 @@ export default registerAs('hstsConfig', () => {
|
|||
const hstsConfig = hstsSchema.validate(
|
||||
{
|
||||
enable: process.env.HD_HSTS_ENABLE,
|
||||
maxAgeSeconds: parseInt(process.env.HD_HSTS_MAX_AGE) || undefined,
|
||||
maxAgeSeconds: parseOptionalInt(process.env.HD_HSTS_MAX_AGE),
|
||||
includeSubdomains: process.env.HD_HSTS_INCLUDE_SUBDOMAINS,
|
||||
preload: process.env.HD_HSTS_PRELOAD,
|
||||
},
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
import {
|
||||
needToLog,
|
||||
parseOptionalInt,
|
||||
replaceAuthErrorsWithEnvironmentVariables,
|
||||
toArrayConfig,
|
||||
} from './utils';
|
||||
|
@ -84,4 +85,12 @@ describe('config utils', () => {
|
|||
expect(needToLog(currentLevel, Loglevel.TRACE)).toBeTruthy();
|
||||
});
|
||||
});
|
||||
describe('parseOptionalInt', () => {
|
||||
it('returns undefined on undefined parameter', () => {
|
||||
expect(parseOptionalInt(undefined)).toEqual(undefined);
|
||||
});
|
||||
it('correctly parses a string', () => {
|
||||
expect(parseOptionalInt('42')).toEqual(42);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -113,3 +113,10 @@ function transformLoglevelToInt(loglevel: Loglevel): number {
|
|||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
export function parseOptionalInt(value?: string): number | undefined {
|
||||
if (value === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
return parseInt(value);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue