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

Rewrite configuration / environment logic using @wuespace/envar #10

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Fix linter warnings
pklaschka committed Dec 11, 2024
commit 978af9d609b01a815b27525a2ee7753fb94c2dde
4 changes: 2 additions & 2 deletions lib/common/additional-managed-namespaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getConfig, isValidNamespace } from "$common/mod.ts";
import { type Config, getConfig, isValidNamespace } from "$common/mod.ts";

/**
* An additional managed namespace outside of the default range.
@@ -102,7 +102,7 @@ export function deserializeAdditionalManagedNamespaces(
*/
export function isValidAdditionalManagedNamespace(
namespace: number,
config = getConfig(),
config: Config = getConfig(),
): boolean {
if (!isValidNamespace(namespace, config)) {
return false;
11 changes: 6 additions & 5 deletions lib/common/asn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getConfig } from "$common/config.ts";
import {
addTimestampToNamespaceStats,
type Config,
ensureFileContent,
getCounterPath,
getMaximumGenericRangeNamespace,
@@ -79,7 +80,7 @@ export async function generateASN(
metadata: Record<string, unknown> = {},
namespace?: number,
deltaCounter = 1,
config = getConfig(),
config: Config = getConfig(),
): Promise<ASNData> {
if (deltaCounter < 1) {
throw new Error("Delta counter must be at least 1");
@@ -148,7 +149,7 @@ export function isValidCounter(counter: number): boolean {
export function formatASN(
namespace: number,
counter: number,
config = getConfig(),
config: Config = getConfig(),
): string {
if (!isValidNamespace(namespace, config)) {
throw new Error("Invalid namespace: " + namespace);
@@ -172,7 +173,7 @@ export function formatASN(
* @returns a human-readable description of the ASN format that explains the prefix, the namespace, and the counter.
* @remark The description is intended to be used in the console output or other monospaced text.
*/
export function getFormatDescription(config = getConfig()): string {
export function getFormatDescription(config: Config = getConfig()): string {
const {
ASN_PREFIX,
ASN_NAMESPACE_RANGE,
@@ -245,7 +246,7 @@ export function nthNinerExtensionRange(
* @param config The configuration to use for validation. Defaults to the global configuration.
* @returns `true` if the ASN is valid, `false` otherwise
*/
export function isValidASN(asn: string, config = getConfig()): boolean {
export function isValidASN(asn: string, config: Config = getConfig()): boolean {
return new RegExp(
`^(${config.ASN_PREFIX})?(\\d{${
`${config.ASN_NAMESPACE_RANGE}`.length
@@ -263,7 +264,7 @@ export function isValidASN(asn: string, config = getConfig()): boolean {
* @param config The configuration to use for parsing. Defaults to the global configuration.
* @returns An {@link ASNData} object with the parsed ASN data
*/
export function parseASN(asn: string, config = getConfig()): ASNData {
export function parseASN(asn: string, config: Config = getConfig()): ASNData {
if (!isValidASN(asn, config)) {
throw new Error("Invalid ASN");
}
2 changes: 1 addition & 1 deletion lib/common/config.ts
Original file line number Diff line number Diff line change
@@ -294,7 +294,7 @@ const DB_CONFIG_KEY = "config";
* @returns A promise that resolves if the database configuration is valid.
* @throws {Error} If the configuration has changed in an incompatible way.
*/
export async function validateDB(config = getConfig()): Promise<void> {
export async function validateDB(config: Config = getConfig()): Promise<void> {
const db = await getDB();

const dbConfigRes = await db.get([DB_CONFIG_KEY]);
5 changes: 3 additions & 2 deletions lib/common/db.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ensureParentDirExists, getDatabasePath } from "$common/path.ts";
import { getConfig } from "$common/config.ts";
import type { Config } from "$common/mod.ts";

/**
* Ensures that the database file exists and returns its API.
* @param config The configuration object to use. Defaults to the global configuration.
* @returns the key-value store for the application database.
*/
export async function getDB(config = getConfig()): Promise<Deno.Kv> {
export async function getDB(config: Config = getConfig()): Promise<Deno.Kv> {
const databasePath = getDatabasePath(config);
if (!databasePath.startsWith("http")) {
await ensureParentDirExists(databasePath);
@@ -38,7 +39,7 @@ export async function getDB(config = getConfig()): Promise<Deno.Kv> {
*/
export async function performAtomicTransaction(
fn: (db: Deno.Kv) => Promise<Deno.KvCommitResult | Deno.KvCommitError>,
config = getConfig(),
config: Config = getConfig(),
) {
const db = await getDB(config);
let res = { ok: false };
12 changes: 6 additions & 6 deletions lib/common/namespaces.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { getConfig } from "$common/mod.ts";
import { type Config, getConfig } from "$common/mod.ts";

/**
* Returns a list of all managed namespaces.
* This includes the generic namespaces and the additional managed namespaces.
* @returns all managed namespaces
*/
export function allManagedNamespaces(config = getConfig()): number[] {
export function allManagedNamespaces(config: Config = getConfig()): number[] {
const minGeneric = getMinimumGenericRangeNamespace(config);
const maxGeneric = getMaximumGenericRangeNamespace(config);

@@ -23,7 +23,7 @@ export function allManagedNamespaces(config = getConfig()): number[] {
* This is the maximum value smaller than the `ASN_NAMESPACE_RANGE` configuration parameter.
* @returns the maximum namespace value for the generic range
*/
export function getMaximumGenericRangeNamespace(config = getConfig()): number {
export function getMaximumGenericRangeNamespace(config: Config = getConfig()): number {
return config.ASN_NAMESPACE_RANGE - 1;
}

@@ -33,7 +33,7 @@ export function getMaximumGenericRangeNamespace(config = getConfig()): number {
* the `ASN_NAMESPACE_RANGE` configuration parameter.
* @returns the minimum namespace value for the generic range
*/
export function getMinimumGenericRangeNamespace(config = getConfig()): number {
export function getMinimumGenericRangeNamespace(config: Config = getConfig()): number {
return Number.parseInt(
"1" + "0".repeat(config.ASN_NAMESPACE_RANGE.toString().length - 1),
);
@@ -54,7 +54,7 @@ export function getMinimumGenericRangeNamespace(config = getConfig()): number {
* @param namespace the namespace to check
* @returns `true` if the namespace is a valid namespace, `false` otherwise
*/
export function isValidNamespace(namespace: number, config = getConfig()): boolean {
export function isValidNamespace(namespace: number, config: Config = getConfig()): boolean {
if (
!Number.isSafeInteger(namespace) ||
namespace < getMinimumGenericRangeNamespace(config)
@@ -82,7 +82,7 @@ export function isValidNamespace(namespace: number, config = getConfig()): boole
*/
export function isManagedNamespace(
namespace: number,
config = getConfig(),
config: Config = getConfig(),
): boolean {
if (namespace < getMinimumGenericRangeNamespace(config)) {
return false;
12 changes: 6 additions & 6 deletions lib/common/path.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { resolve } from "node:path";
import { getConfig } from "$common/mod.ts";
import { type Config, getConfig } from "$common/mod.ts";

/**
* Resolves the full path to the {@link Config.DATA_DIR}.
* @param config The configuration object to use. Defaults to the global configuration.
* @returns Full path to the data directory.
*/
export function getDataDirectoryPath(config = getConfig()): string {
export function getDataDirectoryPath(config: Config = getConfig()): string {
return resolve(config.DATA_DIR);
}

@@ -17,7 +17,7 @@ export function getDataDirectoryPath(config = getConfig()): string {
* @param config The configuration object to use. Defaults to the global configuration.
* @returns Full path to the database file.
*/
export function getDatabasePath(config = getConfig()): string {
export function getDatabasePath(config: Config = getConfig()): string {
if (config.DB_FILE_NAME.startsWith("http")) {
return config.DB_FILE_NAME;
}
@@ -28,7 +28,7 @@ export function getDatabasePath(config = getConfig()): string {
/**
* Logs relevant paths to the console.
*/
export function logPaths(config = getConfig()) {
export function logPaths(config: Config = getConfig()) {
console.log(`DATA_PATH: ${getDataDirectoryPath(config)}`);
console.log(`DB_FILE_PATH: ${getDatabasePath(config)}`);
}
@@ -43,7 +43,7 @@ export function logPaths(config = getConfig()) {
export function getCounterPath(
namespace: number,
counter: number,
config = getConfig(),
config: Config = getConfig(),
): string {
return resolve(
getDataDirectoryPath(config),
@@ -60,7 +60,7 @@ export function getCounterPath(
*/
export function getNamespaceMetadataPath(
namespace: number,
config = getConfig(),
config: Config = getConfig(),
): string {
return resolve(
getDataDirectoryPath(config),
10 changes: 10 additions & 0 deletions lib/common/zod-helpers.ts
Original file line number Diff line number Diff line change
@@ -16,6 +16,11 @@ export function zBoolString(): z.ZodEffects<ZodBoolean, boolean, unknown> {
}, z.boolean());
}

/**
* Parses a boolean from an environment variable value.
* @param value the environment variable value to parse
* @returns the boolean value of the environment variable
*/
export function toBoolean(value: string | undefined): boolean | undefined {
if (
value === undefined ||
@@ -25,6 +30,11 @@ export function toBoolean(value: string | undefined): boolean | undefined {
return ["1", "true", "yes", "on", "enabled"].includes(value.toLowerCase());
}

/**
* Parses a number from an environment variable value.
* @param value the environment variable value to parse
* @returns the number value of the environment variable
*/
export function toNumber(value: string | undefined): number | undefined {
if (
value === undefined ||
4 changes: 2 additions & 2 deletions lib/http/lookup-url.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { getConfig, isValidASN } from "$common/mod.ts";
import { type Config, getConfig, isValidASN } from "$common/mod.ts";

/**
* Builds the URL to lookup the ASN based on the configuration.
* @param asn the ASN to lookup
* @returns the URL to lookup the ASN if the ASN lookup is enabled
* @throws {Error} when the ASN lookup is disabled or the ASN is invalid
*/
export function getLookupURL(asn: string, config = getConfig()): string {
export function getLookupURL(asn: string, config: Config = getConfig()): string {
const baseUrl = config.ASN_LOOKUP_URL;

if (!baseUrl) {