Skip to content

Commit

Permalink
Utilize @balena/env-parsing module
Browse files Browse the repository at this point in the history
Change-type: patch
Signed-off-by: fisehara <[email protected]>
  • Loading branch information
fisehara committed Dec 15, 2022
1 parent 889c06f commit d605b34
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 35 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 5 additions & 2 deletions src/bin/utils.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
23 changes: 5 additions & 18 deletions src/config-loader/env.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
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;
if (![undefined, '', '0', '1'].includes(PINEJS_DEBUG)) {
// TODO-MAJOR: Throw on invalid value
console.warn(`Invalid value for PINEJS_DEBUG '${PINEJS_DEBUG}'`);
}
const globalDebug = boolVar('DEBUG', false);
const PINEJS_DEBUG = boolVar('PINEJS_DEBUG', false);
// Setting PINEJS_DEBUG to explicitly '0' will disable debug even if global debug is truthy
export const DEBUG =
PINEJS_DEBUG === '1' || (PINEJS_DEBUG !== '0' && !!globalDebug);
export const DEBUG = PINEJS_DEBUG || (PINEJS_DEBUG && !!globalDebug);

type CacheFnOpts<T extends (...args: any[]) => any> =
| {
Expand Down Expand Up @@ -81,17 +78,7 @@ export const createCache = <T extends (...args: any[]) => 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,
Expand Down
3 changes: 2 additions & 1 deletion src/database-layer/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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', null);
const initPool = (config: Pg.PoolConfig) => {
config.max ??= env.db.poolSize;
config.idleTimeoutMillis ??= env.db.idleTimeoutMillis;
Expand Down
3 changes: 2 additions & 1 deletion src/sbvr-api/cached-compile.ts
Original file line number Diff line number Diff line change
@@ -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]: {
Expand Down
9 changes: 5 additions & 4 deletions src/server-glue/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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) {
Expand Down Expand Up @@ -57,7 +58,7 @@ export const init = async <T extends string>(
await cfgLoader.loadConfig(migrator.config);

const promises: Array<Promise<void>> = [];
if (process.env.SBVR_SERVER_ENABLED) {
if (optionalVar('SBVR_SERVER_ENABLED')) {
const sbvrServer = await import('../data-server/sbvr-server');
const transactions = require('../http-transactions/transactions');
promises.push(cfgLoader.loadConfig(sbvrServer.config));
Expand All @@ -67,7 +68,7 @@ export const init = async <T extends string>(
.then(() => transactions.addModelHooks('data')),
);
}
if (!process.env.CONFIG_LOADER_DISABLED) {
if (!optionalVar('CONFIG_LOADER_DISABLED')) {
promises.push(cfgLoader.loadApplicationConfig(config));
}
await Promise.all(promises);
Expand Down
5 changes: 3 additions & 2 deletions src/server-glue/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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');
});
})
Expand Down
7 changes: 4 additions & 3 deletions test/03-async-migrator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/';

Expand Down Expand Up @@ -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);
};
Expand Down
7 changes: 4 additions & 3 deletions test/lib/pine-init.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion test/lib/test-init.ts
Original file line number Diff line number Diff line change
@@ -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}`;
Expand All @@ -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(
Expand Down

0 comments on commit d605b34

Please sign in to comment.