-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(commons): env utils + timed cache
feat(specifications): remove register signature feat(specifications): move api to get batches
- Loading branch information
1 parent
b8c2419
commit 8583846
Showing
11 changed files
with
118 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export default class TimedCache<T> { | ||
public set value(value: T) { | ||
this.#value = value; | ||
if (value !== null) { | ||
this.#expireTime = Date.now() + this.cacheSeconds * 1e3; | ||
} | ||
} | ||
|
||
public get value(): T { | ||
if (this.#expireTime && this.#expireTime < Date.now()) this.#value = null; | ||
return this.#value; | ||
} | ||
|
||
#value: T; | ||
#expireTime: number; | ||
|
||
constructor(readonly cacheSeconds: number) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import * as Fs from 'fs'; | ||
import * as Path from 'path'; | ||
|
||
/** | ||
* Will load env files with this precedence (.env.defaults, .env.<NODE_ENV>, .env) | ||
*/ | ||
export function loadEnv(baseDir: string): void { | ||
const envName = process.env.NODE_ENV?.toLowerCase() ?? 'development'; | ||
const env: Record<string, string> = {}; | ||
for (const envFile of ['.env.defaults', `.env.${envName}`, '.env']) { | ||
const path = Path.join(baseDir, envFile); | ||
if (!Fs.existsSync(path)) continue; | ||
applyEnvironmentVariables(path, env); | ||
} | ||
// don't overwrite already set variables | ||
for (const [key, value] of Object.entries(env)) { | ||
if (!process.env[key]) process.env[key] = value; | ||
} | ||
} | ||
|
||
// NOTE: imported from dotenv | ||
export function applyEnvironmentVariables(path: string, env: Record<string, string>): void { | ||
let lines = Fs.readFileSync(path, 'utf8'); | ||
|
||
// Convert line breaks to same format | ||
lines = lines.replace(/\r\n?/gm, '\n'); | ||
|
||
const LineRegex = | ||
/(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/gm; | ||
|
||
const StripSurroundingQuotesRegex = /^(['"`])([\s\S]*)\1$/gm; | ||
|
||
let match = LineRegex.exec(lines); | ||
while (match) { | ||
// eslint-disable-next-line prefer-const | ||
let [, key, value] = match; | ||
|
||
value = (value ?? '').trim(); | ||
|
||
// Check if double quoted before stripping | ||
const isQuoted = StripSurroundingQuotesRegex.test(value); | ||
|
||
value = value.replace(StripSurroundingQuotesRegex, '$2'); | ||
|
||
if (isQuoted) { | ||
// Expand newlines if double quoted | ||
value = value.replace(/\\n/g, '\n').replace(/\\r/g, '\r'); | ||
} | ||
|
||
if (value === '') { | ||
delete env[key]; | ||
} else { | ||
env[key] = value; | ||
} | ||
match = LineRegex.exec(lines); | ||
} | ||
} | ||
|
||
export function parseEnvList(envvar: string): string[] { | ||
if (!envvar) return []; | ||
return envvar.split(',').map(x => x.trim()); | ||
} | ||
|
||
export function parseEnvInt(envvar: string): number | null { | ||
if (!envvar) return null; | ||
return parseInt(envvar, 10); | ||
} | ||
|
||
export function parseEnvBigint(envvar: string): bigint | null { | ||
if (!envvar) return null; | ||
return BigInt(envvar); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters