Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utilize @balena/env-parsing module #616

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 2 additions & 12 deletions src/config-loader/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const cache = {
apiKeyActorId: false as CacheOpts,
};

import { boolVar } from '@balena/env-parsing';
import { boolVar, intVar } from '@balena/env-parsing';
import * as memoize from 'memoizee';
import memoizeWeak = require('memoizee/weak');
export const createCache = <T extends (...args: any[]) => any>(
Expand Down Expand Up @@ -82,17 +82,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 @@ -483,7 +484,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;
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: 7 additions & 2 deletions src/server-glue/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as migratorUtils from '../migrator/utils';

import * as sbvrUtils from '../sbvr-api/sbvr-utils';
import { PINEJS_ADVISORY_LOCK } from '../config-loader/env';
import { optionalVar } from '@balena/env-parsing';

export * as dbModule from '../database-layer/db';
export { PinejsSessionStore } from '../pinejs-session-store/pinejs-session-store';
Expand All @@ -29,8 +30,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 @@ -63,6 +64,8 @@ export const init = async <T extends string>(
await cfgLoader.loadConfig(migrator.config);

const promises: Array<Promise<void>> = [];
// 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');
Expand All @@ -73,6 +76,8 @@ export const init = async <T extends string>(
.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));
}
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, boolVar } 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
!boolVar('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 @@ -55,11 +56,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