node: breaking naming changes to match go version

This commit is contained in:
Elijah Duffy
2026-01-27 18:13:51 -08:00
parent 0ab0168a8d
commit 7522e9e0f3

View File

@@ -1,5 +1,5 @@
/** Return type of a readiness check function */ /** Readiness check function return type */
export type ReadinessFunctionReturn = { export type ReadinessReturn = {
/** Status of the readiness check */ /** Status of the readiness check */
status: ReadinessStatus; status: ReadinessStatus;
/** Optional message providing additional information about the readiness check */ /** Optional message providing additional information about the readiness check */
@@ -7,45 +7,47 @@ export type ReadinessFunctionReturn = {
}; };
/** Function that performs a readiness check */ /** Function that performs a readiness check */
export type ReadinessFunction = (check: ReadinessCheck) => Promise<ReadinessFunctionReturn>; export type ReadinessCheckFn = (check: ReadinessCheck) => Promise<ReadinessReturn>;
/** Status of a readiness check */ /** Status of the service readiness */
export type ReadinessStatus = 'ok' | 'error' | 'degraded'; export type ReadinessStatus = 'fatal' | 'not_ready' | 'degraded' | 'ready';
const aggregateStatus = (statuses: ReadinessStatus[]): ReadinessStatus => { /** Returns the worst readiness status from a list of statuses. */
if (statuses.includes('error')) return 'error'; const worstStatus = (statuses: ReadinessStatus[]): ReadinessStatus => {
if (statuses.includes('fatal')) return 'fatal';
if (statuses.includes('not_ready')) return 'not_ready';
if (statuses.includes('degraded')) return 'degraded'; if (statuses.includes('degraded')) return 'degraded';
return 'ok'; return 'ready';
}; };
/** Represents a readiness check with an optional timeout */ /** Individual readiness check */
export type ReadinessCheck = { export type ReadinessCheck = {
/** Name of the readiness check */ /** Name of the readiness check */
name: string; name: string;
/** Function that performs the readiness check */ /** Function that performs the readiness check */
fn: ReadinessFunction; fn: ReadinessCheckFn;
/** Timeout in milliseconds for the readiness check (default: 5000) */ /** Timeout in milliseconds for the readiness check (default: 5000) */
timeout?: number; timeout?: number;
}; };
/** Result of a system readiness check */ /** System readiness status */
export type ReadinessResult = { export type Readiness = {
/** /**
* Status of the system readiness check, aggregated as the worst status * Status of the system readiness check, aggregated as the worst status
* among individual checks. 'unknown' is a special case indicating that * among individual checks. If no checks have been performed yet, the status
* no checks were performed, used by ScheduledReadiness before the first run. * is 'not_ready'.
* */ */
status: ReadinessStatus | 'unknown'; status: ReadinessStatus;
/** Start time of the system readiness check in milliseconds since the Unix epoch */ /** Start time of the system readiness check in milliseconds since the Unix epoch */
start: number; start: number;
/** Duration of the system readiness check in milliseconds */ /** Duration of the system readiness check in milliseconds */
duration: number; duration: number;
/** Details of individual readiness checks */ /** Details of individual readiness checks */
details: ReadinessDetail[]; details: ReadinessCheckResult[];
}; };
/** Detail of an individual readiness check */ /** System-generated result from an individual readiness check */
export type ReadinessDetail = { export type ReadinessCheckResult = {
/** Name of the readiness check */ /** Name of the readiness check */
name: string; name: string;
/** Status of the readiness check */ /** Status of the readiness check */
@@ -59,12 +61,12 @@ export type ReadinessDetail = {
/** /**
* Performs a readiness check by executing the provided readiness functions. * Performs a readiness check by executing the provided readiness functions.
* @param checks - An array of readiness functions to execute. * @param checks - An array of readiness functions to execute.
* @returns A Promise that resolves to a ReadinessResult object. * @returns A Promise that resolves to a Readiness object.
*/ */
export const readiness = async (checks: ReadinessCheck[]): Promise<ReadinessResult> => { export const readiness = async (checks: ReadinessCheck[]): Promise<Readiness> => {
const start = Date.now(); const start = Date.now();
const t0 = performance.now(); const t0 = performance.now();
const details: ReadinessDetail[] = []; const details: ReadinessCheckResult[] = [];
for (const check of checks) { for (const check of checks) {
const checkt0 = performance.now(); const checkt0 = performance.now();
@@ -83,7 +85,7 @@ export const readiness = async (checks: ReadinessCheck[]): Promise<ReadinessResu
} catch (err) { } catch (err) {
details.push({ details.push({
name: check.name, name: check.name,
status: 'error', status: 'fatal',
message: err instanceof Error ? err.message : String(err), message: err instanceof Error ? err.message : String(err),
duration: performance.now() - checkt0, duration: performance.now() - checkt0,
}); });
@@ -93,7 +95,7 @@ export const readiness = async (checks: ReadinessCheck[]): Promise<ReadinessResu
const duration = performance.now() - t0; const duration = performance.now() - t0;
return { return {
status: aggregateStatus(details.map((d) => d.status)), status: worstStatus(details.map((d) => d.status)),
start, start,
duration, duration,
details, details,
@@ -104,7 +106,7 @@ export const readiness = async (checks: ReadinessCheck[]): Promise<ReadinessResu
* Creates a handler function for readiness HTTP requests. Warning: this runs all * Creates a handler function for readiness HTTP requests. Warning: this runs all
* checks on each request and may be slow. * checks on each request and may be slow.
* @param checks - An array of readiness functions to execute. * @param checks - An array of readiness functions to execute.
* @returns A function that returns a Response object with ReadinessResult in JSON format. * @returns A function that returns a Response object with Readiness in JSON format.
*/ */
export const createReadinessHandler = (checks: ReadinessCheck[]): (() => Promise<Response>) => { export const createReadinessHandler = (checks: ReadinessCheck[]): (() => Promise<Response>) => {
return async () => { return async () => {
@@ -121,8 +123,8 @@ export class ScheduledReadiness {
private interval: number; private interval: number;
private started: boolean = false; private started: boolean = false;
private timer: NodeJS.Timeout | null = null; private timer: NodeJS.Timeout | null = null;
private latestResult: ReadinessResult | null = null; private latestResult: Readiness | null = null;
private nextResult: Promise<ReadinessResult> | null = null; private nextResult: Promise<Readiness> | null = null;
/** /**
* Creates an instance of ScheduledReadiness. * Creates an instance of ScheduledReadiness.
@@ -165,7 +167,7 @@ export class ScheduledReadiness {
} }
/** Gets the next readiness result, waiting if a check is in progress */ /** Gets the next readiness result, waiting if a check is in progress */
async getNextResult(): Promise<ReadinessResult | null> { async getNextResult(): Promise<Readiness | null> {
if (this.nextResult) { if (this.nextResult) {
return await this.nextResult; return await this.nextResult;
} }
@@ -173,7 +175,7 @@ export class ScheduledReadiness {
} }
/** Gets the latest readiness result without waiting */ /** Gets the latest readiness result without waiting */
getResult(): ReadinessResult | null { getResult(): Readiness | null {
return this.latestResult; return this.latestResult;
} }
@@ -192,7 +194,7 @@ export class ScheduledReadiness {
/** /**
* Creates a handler function for readiness HTTP requests using the latest scheduled result. * Creates a handler function for readiness HTTP requests using the latest scheduled result.
* Scheduled handler always returns the most recent result, or 'unknown' if no checks have run yet. * Scheduled handler always returns the most recent result, or 'unknown' if no checks have run yet.
* @returns A function that returns a Response object with ReadinessResult in JSON format. * @returns A function that returns a Response object with Readiness in JSON format.
*/ */
createHandler(): () => Promise<Response> { createHandler(): () => Promise<Response> {
return async () => { return async () => {
@@ -200,13 +202,13 @@ export class ScheduledReadiness {
if (!result) { if (!result) {
return new Response( return new Response(
JSON.stringify({ JSON.stringify({
status: 'unknown', status: 'not_ready',
start: Date.now(), start: Date.now(),
duration: 0, duration: 0,
details: [], details: [],
} as ReadinessResult), } as Readiness),
{ {
status: httpStatusFromReadiness('unknown'), status: httpStatusFromReadiness('not_ready'),
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
}, },
); );
@@ -217,8 +219,8 @@ export class ScheduledReadiness {
} }
} }
/** Returns a Response object with the given ReadinessResult in JSON format */ /** Returns a Response object with the given Readiness in JSON format */
const respondWithResult = (result: ReadinessResult) => { const respondWithResult = (result: Readiness) => {
return new Response(JSON.stringify(result), { return new Response(JSON.stringify(result), {
status: httpStatusFromReadiness(result.status), status: httpStatusFromReadiness(result.status),
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
@@ -226,11 +228,11 @@ const respondWithResult = (result: ReadinessResult) => {
}; };
/** Returns the HTTP status code corresponding to a given readiness status */ /** Returns the HTTP status code corresponding to a given readiness status */
const httpStatusFromReadiness = (status: ReadinessStatus | 'unknown'): number => { const httpStatusFromReadiness = (status: ReadinessStatus): number => {
if (status === 'ok') return 200; if (status === 'ready') return 200;
if (status === 'degraded') return 200; // 206 could also be suitable, but let's avoid false alarms if (status === 'degraded') return 200; // 206 could also be suitable, but let's avoid false alarms
if (status === 'error') return 503; if (status === 'fatal') return 503;
return 200; // unknown, treat as ok to avoid false alarms return 200; // not_ready, treat as ok to avoid false alarms
}; };
const withTimeout = async <T>( const withTimeout = async <T>(