diff --git a/src/config/app.config.ts b/src/config/app.config.ts index 0bfb85341..80548078a 100644 --- a/src/config/app.config.ts +++ b/src/config/app.config.ts @@ -7,7 +7,7 @@ import { registerAs } from '@nestjs/config'; import * as Joi from 'joi'; import { Loglevel } from './loglevel.enum'; -import { buildErrorMessage, parseOptionalInt } from './utils'; +import { buildErrorMessage, parseOptionalNumber } from './utils'; export interface AppConfig { domain: string; @@ -48,7 +48,7 @@ export default registerAs('appConfig', () => { { domain: process.env.HD_DOMAIN, rendererOrigin: process.env.HD_RENDERER_ORIGIN, - port: parseOptionalInt(process.env.PORT), + port: parseOptionalNumber(process.env.PORT), loglevel: process.env.HD_LOGLEVEL, }, { diff --git a/src/config/auth.config.ts b/src/config/auth.config.ts index a9a98fa40..911a4ba40 100644 --- a/src/config/auth.config.ts +++ b/src/config/auth.config.ts @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) + * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) * * SPDX-License-Identifier: AGPL-3.0-only */ @@ -9,7 +9,7 @@ import * as Joi from 'joi'; import { GitlabScope, GitlabVersion } from './gitlab.enum'; import { buildErrorMessage, - parseOptionalInt, + parseOptionalNumber, replaceAuthErrorsWithEnvironmentVariables, toArrayConfig, } from './utils'; @@ -346,7 +346,7 @@ export default registerAs('authConfig', () => { { session: { secret: process.env.HD_SESSION_SECRET, - lifetime: parseOptionalInt(process.env.HD_SESSION_LIFETIME), + lifetime: parseOptionalNumber(process.env.HD_SESSION_LIFETIME), }, local: { enableLogin: process.env.HD_AUTH_LOCAL_ENABLE_LOGIN, diff --git a/src/config/database.config.ts b/src/config/database.config.ts index 2b0b2ee53..0aab632df 100644 --- a/src/config/database.config.ts +++ b/src/config/database.config.ts @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) + * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) * * SPDX-License-Identifier: AGPL-3.0-only */ @@ -7,7 +7,7 @@ import { registerAs } from '@nestjs/config'; import * as Joi from 'joi'; import { DatabaseDialect } from './database-dialect.enum'; -import { buildErrorMessage, parseOptionalInt } from './utils'; +import { buildErrorMessage, parseOptionalNumber } 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: parseOptionalInt(process.env.HD_DATABASE_PORT), + port: parseOptionalNumber(process.env.HD_DATABASE_PORT), storage: process.env.HD_DATABASE_STORAGE, dialect: process.env.HD_DATABASE_DIALECT, }, diff --git a/src/config/hsts.config.ts b/src/config/hsts.config.ts index f4a636e8d..1895000e8 100644 --- a/src/config/hsts.config.ts +++ b/src/config/hsts.config.ts @@ -1,12 +1,12 @@ /* - * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) + * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) * * SPDX-License-Identifier: AGPL-3.0-only */ import { registerAs } from '@nestjs/config'; import * as Joi from 'joi'; -import { buildErrorMessage, parseOptionalInt } from './utils'; +import { buildErrorMessage, parseOptionalNumber } 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: parseOptionalInt(process.env.HD_HSTS_MAX_AGE), + maxAgeSeconds: parseOptionalNumber(process.env.HD_HSTS_MAX_AGE), includeSubdomains: process.env.HD_HSTS_INCLUDE_SUBDOMAINS, preload: process.env.HD_HSTS_PRELOAD, }, diff --git a/src/config/note.config.ts b/src/config/note.config.ts index 4746c3702..9217bcff5 100644 --- a/src/config/note.config.ts +++ b/src/config/note.config.ts @@ -6,7 +6,7 @@ import { registerAs } from '@nestjs/config'; import * as Joi from 'joi'; -import { buildErrorMessage, parseOptionalInt, toArrayConfig } from './utils'; +import { buildErrorMessage, parseOptionalNumber, toArrayConfig } from './utils'; export interface NoteConfig { forbiddenNoteIds: string[]; @@ -29,7 +29,9 @@ export default registerAs('noteConfig', () => { const noteConfig = schema.validate( { forbiddenNoteIds: toArrayConfig(process.env.HD_FORBIDDEN_NOTE_IDS, ','), - maxDocumentLength: parseOptionalInt(process.env.HD_MAX_DOCUMENT_LENGTH), + maxDocumentLength: parseOptionalNumber( + process.env.HD_MAX_DOCUMENT_LENGTH, + ), }, { abortEarly: false, diff --git a/src/config/utils.spec.ts b/src/config/utils.spec.ts index a807ace7a..74d3cace4 100644 --- a/src/config/utils.spec.ts +++ b/src/config/utils.spec.ts @@ -1,12 +1,12 @@ /* - * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) + * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) * * SPDX-License-Identifier: AGPL-3.0-only */ import { Loglevel } from './loglevel.enum'; import { needToLog, - parseOptionalInt, + parseOptionalNumber, replaceAuthErrorsWithEnvironmentVariables, toArrayConfig, } from './utils'; @@ -84,12 +84,15 @@ describe('config utils', () => { expect(needToLog(currentLevel, Loglevel.TRACE)).toBeTruthy(); }); }); - describe('parseOptionalInt', () => { + describe('parseOptionalNumber', () => { it('returns undefined on undefined parameter', () => { - expect(parseOptionalInt(undefined)).toEqual(undefined); + expect(parseOptionalNumber(undefined)).toEqual(undefined); }); - it('correctly parses a string', () => { - expect(parseOptionalInt('42')).toEqual(42); + it('correctly parses a integer string', () => { + expect(parseOptionalNumber('42')).toEqual(42); + }); + it('correctly parses a float string', () => { + expect(parseOptionalNumber('3.14')).toEqual(3.14); }); }); }); diff --git a/src/config/utils.ts b/src/config/utils.ts index cef55b2a4..6878b2874 100644 --- a/src/config/utils.ts +++ b/src/config/utils.ts @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) + * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) * * SPDX-License-Identifier: AGPL-3.0-only */ @@ -113,9 +113,9 @@ function transformLoglevelToInt(loglevel: Loglevel): number { } } -export function parseOptionalInt(value?: string): number | undefined { +export function parseOptionalNumber(value?: string): number | undefined { if (value === undefined) { return undefined; } - return parseInt(value); + return Number(value); }