@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 37 lines 1.65 KB
Raw
1 /**
2 * Shared name-mangling helpers for Prisma physical names and binding env
3 * keys. Internal — not exported from the Prisma package surface.
4 */
5
6 export const fnv1a64 = (value: string) => {
7 let hash = 0xcbf29ce484222325n;
8 for (const byte of new TextEncoder().encode(value)) {
9 hash ^= BigInt(byte);
10 hash = BigInt.asUintN(64, hash * 0x100000001b3n);
11 }
12 return hash.toString(16).padStart(16, "0").toUpperCase();
13 };
14
15 /**
16 * Deterministic physical name for a cloud object owned by one resource
17 * instance: the logical name plus a 12-character token of the instance
18 * identity, inside a 65-character budget. Stable across retries, so a create
19 * whose response was lost (crash after the POST but before the state persist)
20 * can be recovered by name instead of minting a second object.
21 */
22 export const physicalInstanceName = (name: string, instanceId: string) => {
23 const instanceToken = instanceId.replaceAll(/[^a-zA-Z0-9]/g, "");
24 const effectiveSuffix =
25 instanceToken.length >= 12 ? instanceToken.slice(0, 12) : fnv1a64(instanceId).slice(0, 12);
26 const maxPrefixLength = 65 - effectiveSuffix.length - 1;
27 return `${name.trim().slice(0, maxPrefixLength)}-${effectiveSuffix}`;
28 };
29
30 export const envName = (value: string) => {
31 const normalized = value.replaceAll(/[^a-zA-Z0-9]/g, "_").toUpperCase();
32 // Preserve the established keys for conventional PascalCase FQNs while
33 // disambiguating arbitrary logical IDs whose lossy normalization can
34 // collide (`db-a`, `db_a`, and `db.a`, for example).
35 const canonical = value.split("/").every((segment) => /^[A-Z][a-z0-9]*$/.test(segment));
36 return canonical ? normalized : `${normalized}_${fnv1a64(value)}`;
37 };