@controlplane/cli
Version:
Control Plane Corporation CLI
127 lines • 4.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.debounce = debounce;
exports.unixNow = unixNow;
exports.unixTime = unixTime;
exports.toNanos = toNanos;
exports.sleep = sleep;
exports.incrementNanos = incrementNanos;
exports.parseTimeValue = parseTimeValue;
exports.parseTimeToUnixSeconds = parseTimeToUnixSeconds;
const parse_duration_1 = require("./parse-duration");
function debounce(func, wait) {
let timeout = null;
return function executedFunction(...args) {
const later = () => {
timeout = null;
func(...args);
};
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(later, wait);
};
}
function unixNow() {
return unixTime(new Date());
}
function unixTime(d) {
return Math.round(d.getTime() / 1000);
}
function toNanos(nowMillis, since, ts) {
if (!since && !ts) {
return undefined;
}
if (ts) {
const ms = Date.parse(ts);
if (Number.isNaN(ms)) {
throw new Error(`Cannot parse ${ts} into a valid date`);
}
return '' + ms + '000000';
}
const ms = (0, parse_duration_1.parseDuration)(since);
if (ms === null) {
throw new Error(`Cannot parse ${since} into a duration`);
}
return '' + (nowMillis - ms) + '000000';
}
function sleep(millis, sentinel) {
return new Promise((resolve) => {
setTimeout(resolve, millis, sentinel);
});
}
/**
* Increments a nanosecond timestamp string by 1.
*
* @param {string} nanos - A nanosecond timestamp as a string.
* @returns {string} The incremented nanosecond timestamp as a string.
*/
function incrementNanos(nanos) {
return (BigInt(nanos) + BigInt(1)).toString();
}
/**
* Normalize duration string to handle common aliases.
* Converts uppercase M to mo (months) to avoid confusion with m (minutes).
* Also handles "now-" prefix format (e.g., "now-3M" -> "3mo").
*/
function normalizeDurationString(value) {
let normalized = value.trim();
// Handle "now-" prefix (e.g., "now-3M" -> "3M")
if (normalized.toLowerCase().startsWith('now-')) {
normalized = normalized.slice(4);
}
// Convert standalone M to mo (months) - must be careful not to affect "ms"
// Replace XM with Xmo where X is a number (e.g., "3M" -> "3mo", but "3ms" stays "3ms")
normalized = normalized.replace(/(\d)M(?!o|s)/g, '$1mo');
return normalized;
}
/**
* Parse a time value that can be either an ISO 8601 date string or a relative duration.
* Relative durations (e.g., "5m", "1h", "7d", "3M", "3mo") are interpreted as "X time ago from now".
* Also supports "now-" prefix format (e.g., "now-7d", "now-3M").
* @param {string} value The time value to parse
* @returns Parsed time value with date and relative flag, or null if invalid
*/
function parseTimeValue(value) {
// Normalize the value to handle common aliases and formats
const normalized = normalizeDurationString(value);
// Try to parse as a relative duration
const durationMs = (0, parse_duration_1.parseDuration)(normalized);
if (durationMs !== null && durationMs > 0) {
const now = new Date();
return { date: new Date(now.getTime() - durationMs), isRelative: true };
}
// Try to parse as ISO date (use original value, not normalized)
const date = new Date(value);
if (!isNaN(date.getTime())) {
return { date, isRelative: false };
}
return null;
}
/**
* Parses a time value into Unix seconds. Accepts the literal `now`, a Unix
* timestamp in seconds, an ISO 8601 date, or a relative duration (e.g., 7d, now-5m).
* Relative durations resolve against the process clock (see parseTimeValue).
*
* @param {string} value - The time value to parse.
* @param {number} nowMillis - The current time in milliseconds, used for the `now` literal.
* @returns {number | null} The time in Unix seconds, or null when the value cannot be parsed.
*/
function parseTimeToUnixSeconds(value, nowMillis) {
const trimmed = value.trim();
// Treat a bare `now` as the current time
if (trimmed.toLowerCase() === 'now') {
return Math.round(nowMillis / 1000);
}
// A bare number is a Unix timestamp in seconds
if (/^\d+(?:\.\d+)?$/.test(trimmed)) {
return Math.round(Number(trimmed));
}
// Otherwise defer to the shared ISO / relative-duration parser
const parsed = parseTimeValue(value);
if (!parsed) {
return null;
}
return unixTime(parsed.date);
}
//# sourceMappingURL=time.js.map