@ingram-tech/nk-auth 0.7.0 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/id.d.ts +1 -31
- package/dist/id.d.ts.map +1 -1
- package/dist/id.js +6 -100
- package/dist/id.js.map +1 -1
- package/package.json +2 -2
package/dist/id.d.ts
CHANGED
|
@@ -1,32 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
* The Ingram id codec — a UUIDv7 and its base58 skin. Dependency-light on
|
|
3
|
-
* purpose (only `node:crypto`), so a site can import it without pulling the
|
|
4
|
-
* bcrypt / passkey machinery in `./options`: `@ingram-tech/nk-auth/id`.
|
|
5
|
-
*
|
|
6
|
-
* The Python twin lives in cloud.ingram.tech's `v1/core.py` (`new_id`); the
|
|
7
|
-
* byte → string vectors in `id.test.ts` and that repo's `tests/test_ids.py` are
|
|
8
|
-
* kept identical, so a Better-Auth id (stored as a hyphenated UUIDv7 by
|
|
9
|
-
* {@link uuidGenerateId}) and an `agt_`/`smt_` id from the API are the same
|
|
10
|
-
* encoding of the same 16 bytes.
|
|
11
|
-
*
|
|
12
|
-
* The split is deliberate: keep storing the hyphenated UUIDv7 at rest (so
|
|
13
|
-
* Supabase `auth.uid()::uuid` / uuid columns keep working) and use
|
|
14
|
-
* {@link toPrefixedId} to skin it as a prefixed base58 id for the wire / display,
|
|
15
|
-
* {@link fromPrefixedId} to recover it. {@link base58Id} mints a fresh one
|
|
16
|
-
* directly, for text-id sites that want API-style ids natively.
|
|
17
|
-
*/
|
|
18
|
-
/**
|
|
19
|
-
* `advanced.database.generateId` for Better Auth — mints a **UUIDv7** (RFC 9562):
|
|
20
|
-
* a 48-bit Unix-ms timestamp prefix + random tail, version `7`, variant `10`.
|
|
21
|
-
* Keeps ids UUID-shaped (Supabase `auth.uid()::uuid`) while staying time-ordered
|
|
22
|
-
* for index locality. Node/Bun's `randomUUID` is v4-only, so we lay the bytes out
|
|
23
|
-
* by hand.
|
|
24
|
-
*/
|
|
25
|
-
export declare const uuidGenerateId: () => string;
|
|
26
|
-
/** Skin a stored hyphenated UUIDv7 as a prefixed base58 id, e.g. `team_…`. */
|
|
27
|
-
export declare function toPrefixedId(uuid: string, prefix: string): string;
|
|
28
|
-
/** Inverse of {@link toPrefixedId}: recover the hyphenated UUIDv7. */
|
|
29
|
-
export declare function fromPrefixedId(id: string): string;
|
|
30
|
-
/** Mint a fresh prefixed base58 id (UUIDv7 core), for text-id sites / API parity. */
|
|
31
|
-
export declare function base58Id(prefix: string): string;
|
|
1
|
+
export { base58Id, fromPrefixedId, toPrefixedId, uuidGenerateId, } from "@ingram-tech/nk-db/id";
|
|
32
2
|
//# sourceMappingURL=id.d.ts.map
|
package/dist/id.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"id.d.ts","sourceRoot":"","sources":["../src/id.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"id.d.ts","sourceRoot":"","sources":["../src/id.ts"],"names":[],"mappings":"AAKA,OAAO,EACN,QAAQ,EACR,cAAc,EACd,YAAY,EACZ,cAAc,GACd,MAAM,uBAAuB,CAAC"}
|
package/dist/id.js
CHANGED
|
@@ -1,101 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* The Python twin lives in cloud.ingram.tech's `v1/core.py` (`new_id`); the
|
|
8
|
-
* byte → string vectors in `id.test.ts` and that repo's `tests/test_ids.py` are
|
|
9
|
-
* kept identical, so a Better-Auth id (stored as a hyphenated UUIDv7 by
|
|
10
|
-
* {@link uuidGenerateId}) and an `agt_`/`smt_` id from the API are the same
|
|
11
|
-
* encoding of the same 16 bytes.
|
|
12
|
-
*
|
|
13
|
-
* The split is deliberate: keep storing the hyphenated UUIDv7 at rest (so
|
|
14
|
-
* Supabase `auth.uid()::uuid` / uuid columns keep working) and use
|
|
15
|
-
* {@link toPrefixedId} to skin it as a prefixed base58 id for the wire / display,
|
|
16
|
-
* {@link fromPrefixedId} to recover it. {@link base58Id} mints a fresh one
|
|
17
|
-
* directly, for text-id sites that want API-style ids natively.
|
|
18
|
-
*/
|
|
19
|
-
/**
|
|
20
|
-
* `advanced.database.generateId` for Better Auth — mints a **UUIDv7** (RFC 9562):
|
|
21
|
-
* a 48-bit Unix-ms timestamp prefix + random tail, version `7`, variant `10`.
|
|
22
|
-
* Keeps ids UUID-shaped (Supabase `auth.uid()::uuid`) while staying time-ordered
|
|
23
|
-
* for index locality. Node/Bun's `randomUUID` is v4-only, so we lay the bytes out
|
|
24
|
-
* by hand.
|
|
25
|
-
*/
|
|
26
|
-
export const uuidGenerateId = () => {
|
|
27
|
-
const bytes = randomBytes(16);
|
|
28
|
-
const ts = Date.now();
|
|
29
|
-
bytes[0] = Math.floor(ts / 2 ** 40) & 0xff;
|
|
30
|
-
bytes[1] = Math.floor(ts / 2 ** 32) & 0xff;
|
|
31
|
-
bytes[2] = Math.floor(ts / 2 ** 24) & 0xff;
|
|
32
|
-
bytes[3] = Math.floor(ts / 2 ** 16) & 0xff;
|
|
33
|
-
bytes[4] = Math.floor(ts / 2 ** 8) & 0xff;
|
|
34
|
-
bytes[5] = ts & 0xff;
|
|
35
|
-
bytes[6] = ((bytes[6] ?? 0) & 0x0f) | 0x70; // version 7
|
|
36
|
-
bytes[8] = ((bytes[8] ?? 0) & 0x3f) | 0x80; // variant 10
|
|
37
|
-
return bytesToUuid(bytes);
|
|
38
|
-
};
|
|
39
|
-
const B58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
40
|
-
// ceil(128 / log2(58)): a 16-byte value never needs more than 22 digits. We
|
|
41
|
-
// left-pad to it so every body is uniform width and sorts lexically ==
|
|
42
|
-
// chronologically (UUIDv7's ms-timestamp prefix lives in the high bytes).
|
|
43
|
-
const WIDTH = 22;
|
|
44
|
-
/** Big-endian base58 (Bitcoin alphabet) of 16 bytes, left-padded to `WIDTH`. */
|
|
45
|
-
function encode58(bytes) {
|
|
46
|
-
let n = 0n;
|
|
47
|
-
for (const b of bytes)
|
|
48
|
-
n = (n << 8n) | BigInt(b);
|
|
49
|
-
let out = "";
|
|
50
|
-
while (n > 0n) {
|
|
51
|
-
out = B58.charAt(Number(n % 58n)) + out;
|
|
52
|
-
n /= 58n;
|
|
53
|
-
}
|
|
54
|
-
return out.padStart(WIDTH, B58.charAt(0));
|
|
55
|
-
}
|
|
56
|
-
/** Inverse of {@link encode58}: a base58 body back to 16 bytes. */
|
|
57
|
-
function decode58(body) {
|
|
58
|
-
let n = 0n;
|
|
59
|
-
for (const ch of body) {
|
|
60
|
-
const v = B58.indexOf(ch);
|
|
61
|
-
if (v < 0)
|
|
62
|
-
throw new Error(`invalid base58 char: ${ch}`);
|
|
63
|
-
n = n * 58n + BigInt(v);
|
|
64
|
-
}
|
|
65
|
-
const bytes = new Uint8Array(16);
|
|
66
|
-
for (let i = 15; i >= 0; i--) {
|
|
67
|
-
bytes[i] = Number(n & 0xffn);
|
|
68
|
-
n >>= 8n;
|
|
69
|
-
}
|
|
70
|
-
return bytes;
|
|
71
|
-
}
|
|
72
|
-
/** A hyphenated UUID string → its 16 raw bytes. */
|
|
73
|
-
function uuidToBytes(uuid) {
|
|
74
|
-
const hex = uuid.replace(/-/g, "");
|
|
75
|
-
if (!/^[0-9a-fA-F]{32}$/.test(hex))
|
|
76
|
-
throw new Error(`not a uuid: ${uuid}`);
|
|
77
|
-
const bytes = new Uint8Array(16);
|
|
78
|
-
for (let i = 0; i < 16; i++) {
|
|
79
|
-
bytes[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
80
|
-
}
|
|
81
|
-
return bytes;
|
|
82
|
-
}
|
|
83
|
-
/** 16 raw bytes → a canonical hyphenated UUID string. */
|
|
84
|
-
function bytesToUuid(bytes) {
|
|
85
|
-
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
86
|
-
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
87
|
-
}
|
|
88
|
-
/** Skin a stored hyphenated UUIDv7 as a prefixed base58 id, e.g. `team_…`. */
|
|
89
|
-
export function toPrefixedId(uuid, prefix) {
|
|
90
|
-
return `${prefix}_${encode58(uuidToBytes(uuid))}`;
|
|
91
|
-
}
|
|
92
|
-
/** Inverse of {@link toPrefixedId}: recover the hyphenated UUIDv7. */
|
|
93
|
-
export function fromPrefixedId(id) {
|
|
94
|
-
const body = id.slice(id.indexOf("_") + 1);
|
|
95
|
-
return bytesToUuid(decode58(body));
|
|
96
|
-
}
|
|
97
|
-
/** Mint a fresh prefixed base58 id (UUIDv7 core), for text-id sites / API parity. */
|
|
98
|
-
export function base58Id(prefix) {
|
|
99
|
-
return toPrefixedId(uuidGenerateId(), prefix);
|
|
100
|
-
}
|
|
1
|
+
// The Ingram id codec moved to `@ingram-tech/nk-db/id` — the lowest package in
|
|
2
|
+
// the graph — so a site can mint ids without pulling this auth slice. This
|
|
3
|
+
// re-export keeps `@ingram-tech/nk-auth/id` (and Better Auth's
|
|
4
|
+
// `advanced.database.generateId`, wired in `./options`) working unchanged. For
|
|
5
|
+
// the typed prefix registry (`createIdRegistry`), import from `@ingram-tech/nk-db/id`.
|
|
6
|
+
export { base58Id, fromPrefixedId, toPrefixedId, uuidGenerateId, } from "@ingram-tech/nk-db/id";
|
|
101
7
|
//# sourceMappingURL=id.js.map
|
package/dist/id.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"id.js","sourceRoot":"","sources":["../src/id.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"id.js","sourceRoot":"","sources":["../src/id.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,2EAA2E;AAC3E,+DAA+D;AAC/D,+EAA+E;AAC/E,uFAAuF;AACvF,OAAO,EACN,QAAQ,EACR,cAAc,EACd,YAAY,EACZ,cAAc,GACd,MAAM,uBAAuB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ingram-tech/nk-auth",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.2",
|
|
4
4
|
"description": "The Ingram Better Auth foundation: composable presets (org, dual-shape JWT, Supabase RLS bridge, active-org hooks, pg pool) for Next.js sites.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"test": "vitest run"
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"@ingram-tech/nk-db": "^0.
|
|
64
|
+
"@ingram-tech/nk-db": "^0.5.0",
|
|
65
65
|
"bcrypt": "^6.0.0",
|
|
66
66
|
"jose": "^6.0.0",
|
|
67
67
|
"zod": "^4.0.0"
|