@nullplatform/ingest
Version:
Client SDK for the nullplatform ingest API: push snapshots and delta events from a collector into the catalog and other targets, follow runs, replay and drift checks
33 lines (32 loc) • 1.43 kB
JavaScript
//#region src/uuid.ts
/**
* UUIDv7 via the Web Crypto API (Node ≥ 22, Bun, browsers, edge runtimes). A
* UUIDv7 starts with a big-endian Unix-millisecond timestamp, so ids sort by
* creation time. The API requires one as the id of every input.
* @param now The Unix-millisecond time it starts with (default: now).
* @returns A UUIDv7, lowercase.
*/
function uuidv7(now = Date.now()) {
const bytes = /* @__PURE__ */ new Uint8Array(16);
globalThis.crypto.getRandomValues(bytes);
let ms = now;
for (let position = 5; position >= 0; position -= 1) {
bytes[position] = ms % 256;
ms = Math.floor(ms / 256);
}
bytes[6] = 112 + bytes[6] % 16;
bytes[8] = 128 + bytes[8] % 64;
const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
}
/** Version nibble 7, RFC 9562 variant (10xx) — the same pattern the API validates. */
const UUID_V7 = /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
/** True when `id` is a UUIDv7. */
const isUuidV7 = (id) => typeof id === "string" && UUID_V7.test(id);
//#endregion
//#region src/collector/types.ts
/** The target a mapping writes to when it names none. */
const DEFAULT_MAPPING_TARGET = "catalog";
//#endregion
export { isUuidV7 as n, uuidv7 as r, DEFAULT_MAPPING_TARGET as t };
//# sourceMappingURL=types-SnDuBaoJ.js.map