From 3268bfff8e08e5a53e7fc7e14483c233d107295f Mon Sep 17 00:00:00 2001 From: fisehara Date: Fri, 16 Dec 2022 11:49:11 +0100 Subject: [PATCH] Utilize @balena/env-parsing module Skip webpack DefinitionPlugin replacer process.env. Change-type: patch Signed-off-by: fisehara --- package.json | 1 + src/bin/utils.ts | 7 +++++-- src/config-loader/env.ts | 16 ++++------------ src/database-layer/db.ts | 3 ++- src/sbvr-api/cached-compile.ts | 3 ++- src/server-glue/module.ts | 9 +++++++-- src/server-glue/server.ts | 5 +++-- test/03-async-migrator.test.ts | 7 ++++--- test/lib/pine-init.ts | 7 ++++--- test/lib/test-init.ts | 3 ++- 10 files changed, 34 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index a074ae715..5e8109143 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "dependencies": { "@balena/abstract-sql-compiler": "^7.23.0", "@balena/abstract-sql-to-typescript": "^1.3.0", + "@balena/env-parsing": "^1.1.0", "@balena/lf-to-abstract-sql": "^4.6.0", "@balena/odata-parser": "^2.4.2", "@balena/odata-to-abstract-sql": "^5.7.2", diff --git a/src/bin/utils.ts b/src/bin/utils.ts index 624590eca..ac0d5fdaf 100644 --- a/src/bin/utils.ts +++ b/src/bin/utils.ts @@ -1,5 +1,8 @@ -process.env.PINEJS_CACHE_FILE = - process.env.PINEJS_CACHE_FILE || __dirname + '/.pinejs-cache.json'; +import { optionalVar } from '@balena/env-parsing'; +process.env.PINEJS_CACHE_FILE = optionalVar( + 'PINEJS_CACHE_FILE', + __dirname + '/.pinejs-cache.json', +); import type { SqlModel } from '@balena/abstract-sql-compiler'; import type { Config, Model } from '../config-loader/config-loader'; diff --git a/src/config-loader/env.ts b/src/config-loader/env.ts index 6dda1c14c..57eb6ab7e 100644 --- a/src/config-loader/env.ts +++ b/src/config-loader/env.ts @@ -1,5 +1,7 @@ +import { boolVar, intVar, optionalVar } from '@balena/env-parsing'; // TODO-MAJOR: Drop the support for the global `DEBUG` env var -const { DEBUG: globalDebug, PINEJS_DEBUG } = process.env; +const globalDebug = boolVar('DEBUG', false); +const PINEJS_DEBUG = optionalVar('PINEJS_DEBUG'); if (![undefined, '', '0', '1'].includes(PINEJS_DEBUG)) { // TODO-MAJOR: Throw on invalid value console.warn(`Invalid value for PINEJS_DEBUG '${PINEJS_DEBUG}'`); @@ -81,17 +83,7 @@ export const createCache = any>( }); }; -let timeoutMS: number; -if (process.env.TRANSACTION_TIMEOUT_MS) { - timeoutMS = parseInt(process.env.TRANSACTION_TIMEOUT_MS, 10); - if (Number.isNaN(timeoutMS) || timeoutMS <= 0) { - throw new Error( - `Invalid valid for TRANSACTION_TIMEOUT_MS: ${process.env.TRANSACTION_TIMEOUT_MS}`, - ); - } -} else { - timeoutMS = 10000; -} +const timeoutMS = intVar('TRANSACTION_TIMEOUT_MS', 10000); export const db = { poolSize: 50, diff --git a/src/database-layer/db.ts b/src/database-layer/db.ts index 1c5338762..916647660 100644 --- a/src/database-layer/db.ts +++ b/src/database-layer/db.ts @@ -10,6 +10,7 @@ import * as _ from 'lodash'; import { TypedError } from 'typed-error'; import * as env from '../config-loader/env'; import { fromCallback, timeout } from '../sbvr-api/control-flow'; +import { optionalVar } from '@balena/env-parsing'; export const metrics = new EventEmitter(); @@ -469,7 +470,7 @@ if (maybePg != null) { const PG_CHECK_CONSTRAINT_VIOLATION = '23514'; const PG_EXCLUSION_CONSTRAINT_VIOLATION = '23P01'; - const { PG_SCHEMA } = process.env; + const PG_SCHEMA = optionalVar('PG_SCHEMA', undefined); const initPool = (config: Pg.PoolConfig) => { config.max ??= env.db.poolSize; config.idleTimeoutMillis ??= env.db.idleTimeoutMillis; diff --git a/src/sbvr-api/cached-compile.ts b/src/sbvr-api/cached-compile.ts index 0f200c5e2..be9a88047 100644 --- a/src/sbvr-api/cached-compile.ts +++ b/src/sbvr-api/cached-compile.ts @@ -1,8 +1,9 @@ +import { optionalVar } from '@balena/env-parsing'; import type * as Fs from 'fs'; import * as _ from 'lodash'; -const cacheFile = process.env.PINEJS_CACHE_FILE || '.pinejs-cache.json'; +const cacheFile = optionalVar('PINEJS_CACHE_FILE', '.pinejs-cache.json'); let cache: null | { [name: string]: { [version: string]: { diff --git a/src/server-glue/module.ts b/src/server-glue/module.ts index 35286c0cb..1f9a52a4f 100644 --- a/src/server-glue/module.ts +++ b/src/server-glue/module.ts @@ -8,6 +8,7 @@ import * as migrator from '../migrator/sync'; import * as migratorUtils from '../migrator/utils'; import * as sbvrUtils from '../sbvr-api/sbvr-utils'; +import { optionalVar } from '@balena/env-parsing'; export * as dbModule from '../database-layer/db'; export { PinejsSessionStore } from '../pinejs-session-store/pinejs-session-store'; @@ -28,8 +29,8 @@ if (dbModule.engines.websql != null) { }; } else { let databaseURL: string; - if (process.env.DATABASE_URL) { - databaseURL = process.env.DATABASE_URL; + if (optionalVar('DATABASE_URL')) { + databaseURL = optionalVar('DATABASE_URL', ''); } else if (dbModule.engines.postgres != null) { databaseURL = 'postgres://postgres:.@localhost:5432/postgres'; } else if (dbModule.engines.mysql == null) { @@ -57,6 +58,8 @@ export const init = async ( await cfgLoader.loadConfig(migrator.config); const promises: Array> = []; + // cannot be replaced with env-parsing module as it's overwritten in webpack process with a text-match plugin. + // needs to remain `process.env.SBVR_SERVER_ENABLED` as this is the string the plugin will search for. if (process.env.SBVR_SERVER_ENABLED) { const sbvrServer = await import('../data-server/sbvr-server'); const transactions = require('../http-transactions/transactions'); @@ -67,6 +70,8 @@ export const init = async ( .then(() => transactions.addModelHooks('data')), ); } + // cannot be replaced with env-parsing module as it's overwritten in webpack process with a text-match plugin. + // needs to remain `process.env.CONFIG_LOADER_DISABLED` as this is the string the plugin will search for. if (!process.env.CONFIG_LOADER_DISABLED) { promises.push(cfgLoader.loadApplicationConfig(config)); } diff --git a/src/server-glue/server.ts b/src/server-glue/server.ts index 596871716..9b2c1f3d1 100644 --- a/src/server-glue/server.ts +++ b/src/server-glue/server.ts @@ -16,6 +16,7 @@ export { ExtendedSBVRParser } from '../extended-sbvr-parser/extended-sbvr-parser import * as passportPinejs from '../passport-pinejs/passport-pinejs'; import * as express from 'express'; +import { intVar, optionalVar } from '@balena/env-parsing'; const app = express(); @@ -89,7 +90,7 @@ export const initialised = Pinejs.init(app) if ( typeof process === 'undefined' || process == null || - !process.env.DISABLE_DEFAULT_AUTH + !optionalVar('DISABLE_DEFAULT_AUTH') ) { app.post( '/login', @@ -118,7 +119,7 @@ export const initialised = Pinejs.init(app) }); } - app.listen(process.env.PORT || 1337, () => { + app.listen(intVar('PORT', 1337), () => { console.info('Server started'); }); }) diff --git a/test/03-async-migrator.test.ts b/test/03-async-migrator.test.ts index 4e8fa91ed..1a0b4eb26 100644 --- a/test/03-async-migrator.test.ts +++ b/test/03-async-migrator.test.ts @@ -5,6 +5,7 @@ import { setTimeout } from 'timers'; import { dbModule } from '../src/server-glue/module'; import { testInit, testDeInit, testLocalServer } from './lib/test-init'; import { MigrationStatus } from '../src/migrator/utils'; +import { optionalVar } from '@balena/env-parsing'; const fixturesBasePath = __dirname + '/fixtures/03-async-migrator/'; @@ -40,11 +41,11 @@ function delay(ms: number) { const getDbUnderTest = async function () { const initDbOptions = { engine: - process.env.DATABASE_URL?.slice( + optionalVar('DATABASE_URL')?.slice( 0, - process.env.DATABASE_URL?.indexOf(':'), + optionalVar('DATABASE_URL')?.indexOf(':'), ) || 'postgres', - params: process.env.DATABASE_URL || 'localhost', + params: optionalVar('DATABASE_URL', 'localhost'), }; return dbModule.connect(initDbOptions); }; diff --git a/test/lib/pine-init.ts b/test/lib/pine-init.ts index 3f6972a75..ef8629f00 100644 --- a/test/lib/pine-init.ts +++ b/test/lib/pine-init.ts @@ -1,3 +1,4 @@ +import { optionalVar } from '@balena/env-parsing'; import * as express from 'express'; import { exit } from 'process'; import * as pine from '../../src/server-glue/module'; @@ -38,11 +39,11 @@ async function cleanInit(deleteDb: boolean = false) { try { const initDbOptions = { engine: - process.env.DATABASE_URL?.slice( + optionalVar('DATABASE_URL')?.slice( 0, - process.env.DATABASE_URL?.indexOf(':'), + optionalVar('DATABASE_URL')?.indexOf(':'), ) || 'postgres', - params: process.env.DATABASE_URL || 'localhost', + params: optionalVar('DATABASE_URL', 'localhost'), }; const initDb = pine.dbModule.connect(initDbOptions); await initDb.executeSql( diff --git a/test/lib/test-init.ts b/test/lib/test-init.ts index 26bb620f3..655cf132c 100644 --- a/test/lib/test-init.ts +++ b/test/lib/test-init.ts @@ -1,3 +1,4 @@ +import { optionalVar } from '@balena/env-parsing'; import { ChildProcess, fork } from 'child_process'; export const testListenPort = 1337; export const testLocalServer = `http://localhost:${testListenPort}`; @@ -11,7 +12,7 @@ export async function testInit( const processArgs = { fixturePath, testListenPort: pineListenPort, - deleteDb: deleteDb || process.env.DELETE_DB, + deleteDb: deleteDb || optionalVar('DELETE_DB'), }; const testServer = fork(