UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

217 lines (216 loc) • 10.2 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; //#region packages/normalization-core/src/number-coercion.ts function asFiniteNumber(e) { return typeof e === "number" && Number.isFinite(e) ? e : void 0; } function asPositiveFiniteNumber(e) { const t = asFiniteNumber(e); return t && t > 0 ? t : void 0; } /** Floors positive finite input; fractions below one become zero. */ function floorPositiveFiniteNumber(e) { const t = asPositiveFiniteNumber(e); return t === void 0 ? void 0 : Math.floor(t); } function asNonNegativeFiniteNumber(e) { const t = asFiniteNumber(e); return t && t < 0 ? void 0 : t; } /** Returns a finite number only when it satisfies the supplied inclusive/exclusive bounds. */ function asFiniteNumberInRange(e, t) { const n = asFiniteNumber(e); if (n === void 0) return; if (t.min !== void 0) { if (t.minExclusive ? n <= t.min : n < t.min) return; } if (t.max !== void 0) { if (t.maxExclusive ? n >= t.max : n > t.max) return; } return n; } /** Returns a safe integer only when it satisfies the supplied inclusive bounds. */ function asSafeIntegerInRange(e, t) { if (typeof e !== "number" || !Number.isSafeInteger(e)) return; if (t.min !== void 0 && e < t.min) return; if (t.max !== void 0 && e > t.max) return; return e; } /** Parses finite numbers from number values or strict numeric string tokens. */ function parseFiniteNumber(e) { return parseStrictFiniteNumber(e); } /** Parses only safe integer numbers or base-10 integer strings. */ function parseStrictInteger(e) { if (typeof e === "number") return Number.isSafeInteger(e) ? e : void 0; const t = normalizeOptionalString(e); if (!t || !/^[+-]?\d+$/.test(t)) return; const n = Number(t); return Number.isSafeInteger(n) ? n : void 0; } /** Parses only finite decimal/scientific string tokens, rejecting partial numbers. */ function parseStrictFiniteNumber(e) { if (typeof e === "number") return asFiniteNumber(e); const t = normalizeOptionalString(e); if (!t || !/^[+-]?(?:(?:\d+\.?\d*)|(?:\.\d+))(?:e[+-]?\d+)?$/i.test(t)) return; return asFiniteNumber(Number(t)); } function asPositiveSafeInteger(e) { return typeof e === "number" && Number.isSafeInteger(e) && e > 0 ? e : void 0; } function asPositiveInteger(e) { return typeof e === "number" && Number.isInteger(e) && e > 0 ? e : void 0; } function asNonNegativeInteger(e) { return typeof e === "number" && Number.isInteger(e) && e >= 0 ? e : void 0; } /** Conservative upper bound for Node timer delays. */ const MAX_TIMER_TIMEOUT_MS = 2147e6; /** Timer bound expressed in whole seconds for env/config inputs. */ const MAX_TIMER_TIMEOUT_SECONDS = Math.floor(MAX_TIMER_TIMEOUT_MS / 1e3); /** Largest timestamp accepted by JavaScript Date. */ const MAX_DATE_TIMESTAMP_MS = 864e13; /** Fallback ISO value for invalid timestamp inputs. */ const UNIX_EPOCH_ISO_STRING = "1970-01-01T00:00:00.000Z"; function asDateTimestampMs(e) { return asFiniteNumberInRange(e, { min: -864e13, max: MAX_DATE_TIMESTAMP_MS }); } /** Parses Date-valid timestamp strings using JavaScript date-string semantics. */ function parseDateStringTimestampMs(e) { return typeof e === "string" ? asDateTimestampMs(Date.parse(e)) : void 0; } /** Reads finite numbers as milliseconds and parses strings using date-string semantics. */ function parseDateFirstTimestampMs(e) { return typeof e === "number" ? asFiniteNumber(e) : parseDateStringTimestampMs(e); } function isFutureDateTimestampMs(e, t = {}) { const n = asDateTimestampMs(e); const r = asDateTimestampMs(t.nowMs ?? Date.now()); return n !== void 0 && r !== void 0 && n > r; } function timestampMsToIsoString(e) { const t = asDateTimestampMs(e); return t === void 0 ? void 0 : new Date(t).toISOString(); } function resolveDateTimestampMs(e, t = Date.now()) { return asDateTimestampMs(e) ?? asDateTimestampMs(t) ?? 0; } /** Resolves a Date-valid timestamp to ISO, falling back to Unix epoch if needed. */ function resolveTimestampMsToIsoString(e, t = Date.now()) { return timestampMsToIsoString(e) ?? timestampMsToIsoString(t) ?? "1970-01-01T00:00:00.000Z"; } /** Formats Date-valid timestamps for filenames by replacing colon separators. */ function timestampMsToIsoFileStamp(e, t = Date.now()) { return resolveTimestampMsToIsoString(e, t).replaceAll(":", "-"); } /** Clamps finite millisecond values into the Node-safe timer range. */ function clampTimerTimeoutMs(e, t = 1) { const n = asFiniteNumber(e); if (n === void 0) return; return Math.min(Math.max(Math.floor(n), Math.max(1, Math.floor(t))), MAX_TIMER_TIMEOUT_MS); } /** Clamps positive finite millisecond values into the Node-safe timer range. */ function clampPositiveTimerTimeoutMs(e) { const t = asFiniteNumber(e); if (t === void 0 || t <= 0) return; return clampTimerTimeoutMs(t); } /** Resolves a positive timer timeout or falls back through safe timer clamping. */ function resolvePositiveTimerTimeoutMs(e, t) { return clampPositiveTimerTimeoutMs(e) ?? resolveTimerTimeoutMs(t, 1); } /** Resolves arbitrary timeout input with fallback and minimum timer bounds. */ function resolveTimerTimeoutMs(e, t, n = 1) { const r = asFiniteNumber(e) ?? asFiniteNumber(t); const i = Math.max(0, Math.floor(n)); if (r === void 0) return i; return Math.min(Math.max(Math.floor(r), i), MAX_TIMER_TIMEOUT_MS); } /** Adds grace time to a finite timeout and clamps the result to Node-safe bounds. */ function addTimerTimeoutGraceMs(e, t = 5e3) { const n = asFiniteNumber(e); const r = asFiniteNumber(t); if (n === void 0 || r === void 0) return; const i = n + r; return Number.isFinite(i) ? clampTimerTimeoutMs(i) : MAX_TIMER_TIMEOUT_MS; } /** Converts finite positive seconds to Node-safe milliseconds. */ function finiteSecondsToTimerSafeMilliseconds(e, t = {}) { const n = asFiniteNumber(e); if (n === void 0 || n <= 0) return; const r = t.floorSeconds ? Math.floor(n) : n; if (r >= 2147e3) return MAX_TIMER_TIMEOUT_MS; const i = Math.floor(r * 1e3); return i > 0 ? i : void 0; } /** Resolves an integer option from finite numeric input or fallback, then clamps bounds. */ function resolveIntegerOption(e, t, n = {}) { const r = asFiniteNumber(e) ?? t; const i = Math.floor(r); const a = n.min === void 0 ? i : Math.max(n.min, i); return n.max === void 0 ? a : Math.min(n.max, a); } /** Resolves an optional integer option, returning undefined for non-finite input. */ function resolveOptionalIntegerOption(e, t = {}) { if (typeof e !== "number" || !Number.isFinite(e)) return; return resolveIntegerOption(e, e, t); } function resolveNonNegativeIntegerOption(e, t) { return resolveIntegerOption(e, t, { min: 0 }); } function parseStrictPositiveInteger(e) { const t = parseStrictInteger(e); return t !== void 0 && t > 0 ? t : void 0; } function parseStrictNonNegativeInteger(e) { const t = parseStrictInteger(e); return t !== void 0 && t >= 0 ? t : void 0; } function safeMillisecondsFromSeconds(e) { const t = e === void 0 ? void 0 : e * 1e3; return Number.isSafeInteger(t) ? t : void 0; } function positiveSecondsToSafeMilliseconds(e) { return safeMillisecondsFromSeconds(parseStrictPositiveInteger(e)); } function nonNegativeSecondsToSafeMilliseconds(e) { return safeMillisecondsFromSeconds(parseStrictNonNegativeInteger(e)); } function resolveExpiresAtMsFromDurationMs(e, t = {}) { const n = asPositiveSafeInteger(e); if (n === void 0) return; const r = asDateTimestampMs(t.nowMs ?? Date.now()); const i = asFiniteNumber(t.bufferMs ?? 0); if (r === void 0 || i === void 0) return; const a = r + n - i; if (!Number.isSafeInteger(a) || asDateTimestampMs(a) === void 0) return; const o = t.minRemainingMs; if (o === void 0) return a; const s = r + o; if (!Number.isSafeInteger(s) || asDateTimestampMs(s) === void 0) return a; return Math.max(a, s); } function resolveExpiresAtMsFromDurationSeconds(e, t = {}) { const n = positiveSecondsToSafeMilliseconds(e); return n === void 0 ? void 0 : resolveExpiresAtMsFromDurationMs(n, t); } function resolveExpiresAtMsFromEpochSeconds(e, t = {}) { const n = typeof e === "number" && Number.isFinite(e) && e > 0 ? Math.trunc(e) * 1e3 : positiveSecondsToSafeMilliseconds(e); if (n === void 0) return; const r = n - (t.bufferMs ?? 0); if (!Number.isSafeInteger(r) || asDateTimestampMs(r) === void 0) return; const i = t.maxMs; return i === void 0 || r <= i ? r : void 0; } /** Resolves expiration input that may be relative seconds, epoch seconds, or epoch milliseconds. */ function resolveExpiresAtMsFromDurationOrEpoch(e, t = {}) { const n = parseStrictPositiveInteger(e); if (n === void 0) return; if (n < (t.relativeSecondsThreshold ?? 1e9)) return resolveExpiresAtMsFromDurationSeconds(n, { nowMs: t.nowMs }); if (n < (t.absoluteMillisecondsThreshold ?? 0xe8d4a51000)) return resolveExpiresAtMsFromEpochSeconds(n); return asDateTimestampMs(n); } //#endregion export { resolveExpiresAtMsFromDurationMs as A, timestampMsToIsoFileStamp as B, parseFiniteNumber as C, parseStrictPositiveInteger as D, parseStrictNonNegativeInteger as E, resolveNonNegativeIntegerOption as F, resolveOptionalIntegerOption as I, resolvePositiveTimerTimeoutMs as L, resolveExpiresAtMsFromDurationSeconds as M, resolveExpiresAtMsFromEpochSeconds as N, positiveSecondsToSafeMilliseconds as O, resolveIntegerOption as P, resolveTimerTimeoutMs as R, parseDateStringTimestampMs as S, parseStrictInteger as T, timestampMsToIsoString as V, finiteSecondsToTimerSafeMilliseconds as _, addTimerTimeoutGraceMs as a, nonNegativeSecondsToSafeMilliseconds as b, asFiniteNumberInRange as c, asPositiveFiniteNumber as d, asPositiveInteger as f, clampTimerTimeoutMs as g, clampPositiveTimerTimeoutMs as h, UNIX_EPOCH_ISO_STRING as i, resolveExpiresAtMsFromDurationOrEpoch as j, resolveDateTimestampMs as k, asNonNegativeFiniteNumber as l, asSafeIntegerInRange as m, MAX_TIMER_TIMEOUT_MS as n, asDateTimestampMs as o, asPositiveSafeInteger as p, MAX_TIMER_TIMEOUT_SECONDS as r, asFiniteNumber as s, MAX_DATE_TIMESTAMP_MS as t, asNonNegativeInteger as u, floorPositiveFiniteNumber as v, parseStrictFiniteNumber as w, parseDateFirstTimestampMs as x, isFutureDateTimestampMs as y, resolveTimestampMsToIsoString as z };