@hop-top/kit 0.4.0-alpha.1 → 0.4.0-alpha.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/CHANGELOG.md +13 -0
- package/README.md +93 -0
- package/dist/id/index.d.ts +83 -0
- package/dist/id/index.js +66 -0
- package/dist/telemetry/index.d.ts +315 -0
- package/dist/telemetry/index.js +605 -0
- package/package.json +13 -3
- package/src/id/contract.test.ts +85 -0
- package/src/id/index.test.ts +159 -0
- package/src/id/index.ts +120 -0
- package/src/telemetry/client.test.ts +355 -0
- package/src/telemetry/client.ts +504 -0
- package/src/telemetry/consent.test.ts +234 -0
- package/src/telemetry/consent.ts +202 -0
- package/src/telemetry/index.ts +42 -0
- package/src/telemetry/installId.test.ts +121 -0
- package/src/telemetry/installId.ts +157 -0
- package/src/telemetry/mode.test.ts +148 -0
- package/src/telemetry/mode.ts +104 -0
- package/src/telemetry/redact.test.ts +133 -0
- package/src/telemetry/redact.ts +92 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Cross-language parity contract test (tlc T-0753).
|
|
2
|
+
//
|
|
3
|
+
// Loads contracts/typeid-v1/fixtures.json from the repo root and
|
|
4
|
+
// asserts that this TS SDK's encode (via typeid-js's TypeID.fromUUID)
|
|
5
|
+
// and decode (via the kit's parse) agree with the canonical wire form
|
|
6
|
+
// shared by go/rs/py/php. A divergence here means either typeid-js
|
|
7
|
+
// drifted or the contract was edited without updating all five SDKs.
|
|
8
|
+
|
|
9
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
10
|
+
import { dirname, resolve } from 'node:path';
|
|
11
|
+
import { fileURLToPath } from 'node:url';
|
|
12
|
+
|
|
13
|
+
import { describe, it, expect } from 'vitest';
|
|
14
|
+
import { TypeID } from 'typeid-js';
|
|
15
|
+
|
|
16
|
+
import { parse } from './index';
|
|
17
|
+
|
|
18
|
+
interface ContractVector {
|
|
19
|
+
readonly name: string;
|
|
20
|
+
readonly prefix: string;
|
|
21
|
+
readonly uuid: string;
|
|
22
|
+
readonly typeid: string;
|
|
23
|
+
readonly skip_in?: ReadonlyArray<string>;
|
|
24
|
+
readonly note?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface ContractFile {
|
|
28
|
+
readonly version: string;
|
|
29
|
+
readonly spec: string;
|
|
30
|
+
readonly vectors: ReadonlyArray<ContractVector>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Walk up from this test file's directory until we hit a directory
|
|
34
|
+
// holding contracts/typeid-v1/fixtures.json. Robust against both
|
|
35
|
+
// `vitest run` (CWD = sdk/ts) and IDE-driven invocations whose CWD
|
|
36
|
+
// may vary.
|
|
37
|
+
function locateContract(): string {
|
|
38
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
39
|
+
let dir = here;
|
|
40
|
+
for (let i = 0; i < 10; i++) {
|
|
41
|
+
const candidate = resolve(dir, 'contracts', 'typeid-v1', 'fixtures.json');
|
|
42
|
+
if (existsSync(candidate)) return candidate;
|
|
43
|
+
const parent = dirname(dir);
|
|
44
|
+
if (parent === dir) break;
|
|
45
|
+
dir = parent;
|
|
46
|
+
}
|
|
47
|
+
throw new Error(
|
|
48
|
+
`contracts/typeid-v1/fixtures.json: not found walking up from ${here}`,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const CONTRACT: ContractFile = JSON.parse(
|
|
53
|
+
readFileSync(locateContract(), 'utf8'),
|
|
54
|
+
) as ContractFile;
|
|
55
|
+
|
|
56
|
+
describe('typeid-v1 contract metadata', () => {
|
|
57
|
+
it('pins version + spec', () => {
|
|
58
|
+
expect(CONTRACT.version).toBe('v1');
|
|
59
|
+
expect(CONTRACT.spec).toBe('jetify-typeid-v0.3');
|
|
60
|
+
expect(CONTRACT.vectors.length).toBeGreaterThan(0);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe('typeid-v1 contract generation (TS)', () => {
|
|
65
|
+
for (const v of CONTRACT.vectors) {
|
|
66
|
+
const skipped = v.skip_in?.includes('ts') ?? false;
|
|
67
|
+
const fn = skipped ? it.skip : it;
|
|
68
|
+
fn(`vector ${v.name}: TypeID.fromUUID produces the pinned canonical`, () => {
|
|
69
|
+
const got = TypeID.fromUUID(v.prefix, v.uuid).toString();
|
|
70
|
+
expect(got).toBe(v.typeid);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe('typeid-v1 contract parse (TS)', () => {
|
|
76
|
+
for (const v of CONTRACT.vectors) {
|
|
77
|
+
const skipped = v.skip_in?.includes('ts') ?? false;
|
|
78
|
+
const fn = skipped ? it.skip : it;
|
|
79
|
+
fn(`vector ${v.name}: parse round-trips to (prefix, uuid)`, () => {
|
|
80
|
+
const parsed = parse(v.typeid);
|
|
81
|
+
expect(parsed.prefix).toBe(v.prefix);
|
|
82
|
+
expect(parsed.uuid).toBe(v.uuid);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
});
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { TypeID } from 'typeid-js';
|
|
3
|
+
import {
|
|
4
|
+
newId,
|
|
5
|
+
parse,
|
|
6
|
+
newTyped,
|
|
7
|
+
parseTyped,
|
|
8
|
+
typeIdSchema,
|
|
9
|
+
type Typed,
|
|
10
|
+
} from './index';
|
|
11
|
+
|
|
12
|
+
// Canonical fixture set — these EXACT uuid inputs are shared with the
|
|
13
|
+
// other language SDKs so future cross-language parity work (T-0753)
|
|
14
|
+
// finds matching outputs. Do not mutate without coordinating across
|
|
15
|
+
// sdk/{go,rs,py,php}.
|
|
16
|
+
const FIXTURES: ReadonlyArray<{ prefix: string; uuid: string }> = [
|
|
17
|
+
{ prefix: 'task', uuid: '01940000-0000-7000-8000-000000000000' },
|
|
18
|
+
{ prefix: 'invoice', uuid: '01940000-0000-7000-8000-000000000001' },
|
|
19
|
+
{ prefix: 'user', uuid: '01940000-0000-7000-8000-0000000000ff' },
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
describe('newId', () => {
|
|
23
|
+
it('emits canonical `<prefix>_<26-char-base32>` form', () => {
|
|
24
|
+
const id = newId('task');
|
|
25
|
+
expect(id).toMatch(/^task_[0-7][0-9a-hjkmnp-tv-z]{25}$/);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('round-trips through parse', () => {
|
|
29
|
+
const id = newId('user');
|
|
30
|
+
const parsed = parse(id);
|
|
31
|
+
expect(parsed.prefix).toBe('user');
|
|
32
|
+
expect(parsed.uuid).toMatch(
|
|
33
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/,
|
|
34
|
+
);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('generates distinct values across calls', () => {
|
|
38
|
+
const a = newId('task');
|
|
39
|
+
const b = newId('task');
|
|
40
|
+
expect(a).not.toBe(b);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe('parse', () => {
|
|
45
|
+
it.each(FIXTURES)(
|
|
46
|
+
'returns matching prefix+uuid for canonical fixture %o',
|
|
47
|
+
({ prefix, uuid }) => {
|
|
48
|
+
// Use typeid-js's fromUUID to compute the expected canonical
|
|
49
|
+
// string; that's the cross-language oracle.
|
|
50
|
+
const expected = TypeID.fromUUID(prefix, uuid).toString();
|
|
51
|
+
const result = parse(expected);
|
|
52
|
+
expect(result.prefix).toBe(prefix);
|
|
53
|
+
expect(result.uuid).toBe(uuid);
|
|
54
|
+
},
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
it('throws on malformed input', () => {
|
|
58
|
+
expect(() => parse('not-a-typeid')).toThrow();
|
|
59
|
+
expect(() => parse('task_too_short')).toThrow();
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe('newTyped', () => {
|
|
64
|
+
it('preserves the literal prefix in the runtime string', () => {
|
|
65
|
+
const id = newTyped('task');
|
|
66
|
+
expect(id.startsWith('task_')).toBe(true);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('return type is assignable to Typed<P>', () => {
|
|
70
|
+
// Compile-time check: the assignment below must type-check.
|
|
71
|
+
const id: Typed<'task'> = newTyped('task');
|
|
72
|
+
expect(typeof id).toBe('string');
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe('parseTyped', () => {
|
|
77
|
+
it('returns the input when prefix matches', () => {
|
|
78
|
+
const generated = newId('task');
|
|
79
|
+
const typed = parseTyped('task', generated);
|
|
80
|
+
expect(typed).toBe(generated);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('throws when prefix mismatches', () => {
|
|
84
|
+
const generated = newId('task');
|
|
85
|
+
expect(() => parseTyped('invoice', generated)).toThrow();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('throws on malformed input', () => {
|
|
89
|
+
expect(() => parseTyped('task', 'task_nope')).toThrow();
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe('cross-language fixtures', () => {
|
|
94
|
+
// Lock in the exact canonical strings so a regression in either
|
|
95
|
+
// typeid-js or our wrapper is caught immediately. These values are
|
|
96
|
+
// produced by typeid-js v1 from the fixture UUIDs; the other-language
|
|
97
|
+
// SDKs must produce the byte-identical strings.
|
|
98
|
+
it.each(FIXTURES)(
|
|
99
|
+
'produces canonical form matching TypeID.fromUUID(%o)',
|
|
100
|
+
({ prefix, uuid }) => {
|
|
101
|
+
const canonical = TypeID.fromUUID(prefix, uuid).toString();
|
|
102
|
+
// structural sanity
|
|
103
|
+
expect(canonical.startsWith(`${prefix}_`)).toBe(true);
|
|
104
|
+
// round-trip through our parse()
|
|
105
|
+
const parsed = parse(canonical);
|
|
106
|
+
expect(parsed).toEqual({ prefix, uuid });
|
|
107
|
+
// round-trip through our parseTyped()
|
|
108
|
+
const typed = parseTyped(prefix, canonical);
|
|
109
|
+
expect(typed).toBe(canonical);
|
|
110
|
+
},
|
|
111
|
+
);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe('typeIdSchema', () => {
|
|
115
|
+
it('accepts canonical typeids for the matching prefix', () => {
|
|
116
|
+
const schema = typeIdSchema('task');
|
|
117
|
+
const id = newId('task');
|
|
118
|
+
expect(() => schema.parse(id)).not.toThrow();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('rejects typeids with a different prefix', () => {
|
|
122
|
+
const schema = typeIdSchema('task');
|
|
123
|
+
const id = newId('invoice');
|
|
124
|
+
expect(() => schema.parse(id)).toThrow();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('rejects malformed suffixes', () => {
|
|
128
|
+
const schema = typeIdSchema('task');
|
|
129
|
+
expect(() => schema.parse('task_TOO_SHORT')).toThrow();
|
|
130
|
+
expect(() => schema.parse('task_zzzzzzzzzzzzzzzzzzzzzzzzzz')).toThrow();
|
|
131
|
+
// Crockford forbids i, l, o, u inside suffix
|
|
132
|
+
expect(() =>
|
|
133
|
+
schema.parse('task_8iiiiiiiiiiiiiiiiiiiiiiiii'),
|
|
134
|
+
).toThrow();
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('rejects non-strings', () => {
|
|
138
|
+
const schema = typeIdSchema('task');
|
|
139
|
+
expect(() => schema.parse(123 as unknown as string)).toThrow();
|
|
140
|
+
expect(() => schema.parse(null as unknown as string)).toThrow();
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it.each(FIXTURES)(
|
|
144
|
+
'accepts the canonical fixture for %o',
|
|
145
|
+
({ prefix, uuid }) => {
|
|
146
|
+
const schema = typeIdSchema(prefix);
|
|
147
|
+
const canonical = TypeID.fromUUID(prefix, uuid).toString();
|
|
148
|
+
expect(schema.parse(canonical)).toBe(canonical);
|
|
149
|
+
},
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
it('escapes regex metacharacters in the prefix safely', () => {
|
|
153
|
+
// The TypeID spec restricts prefixes to [a-z0-9_], so this is
|
|
154
|
+
// defensive only; nevertheless we don't want a future relaxation
|
|
155
|
+
// to make the schema crash or match too broadly.
|
|
156
|
+
const schema = typeIdSchema('a.b');
|
|
157
|
+
expect(() => schema.parse('axb_01h2e8kqv8000000000000000')).toThrow();
|
|
158
|
+
});
|
|
159
|
+
});
|
package/src/id/index.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module id
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* TypeID primitive — thin wrapper around `typeid-js` (jetify-com)
|
|
6
|
+
* implementing the cross-language kit API SHAPE per ADR 0001.
|
|
7
|
+
*
|
|
8
|
+
* Canonical wire form: `<prefix>_<26-char-base32>` where the suffix is
|
|
9
|
+
* a Crockford-base32-encoded UUIDv7. The TS surface adds a
|
|
10
|
+
* template-literal `Typed<P>` for compile-time prefix safety on top of
|
|
11
|
+
* the runtime guarantees provided by `typeid-js`.
|
|
12
|
+
*
|
|
13
|
+
* URI composition (e.g. `tlc://task/task_…`) is NOT in this module —
|
|
14
|
+
* callers should use the `@hop-top/uri` package directly. This
|
|
15
|
+
* keeps `kit/id` free of any transitive dependency on the URI
|
|
16
|
+
* registry, per the ADR's scope boundary.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { typeid, TypeID } from 'typeid-js';
|
|
20
|
+
import { z } from 'zod';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Compile-time-typed canonical TypeID string for prefix `P`.
|
|
24
|
+
*
|
|
25
|
+
* The template-literal `${P}_${string}` form means a `Typed<'task'>`
|
|
26
|
+
* is statically distinguishable from a `Typed<'invoice'>` even though
|
|
27
|
+
* both are runtime `string`s. Pair with {@link newTyped} or
|
|
28
|
+
* {@link parseTyped} to mint values whose prefix is enforced both at
|
|
29
|
+
* compile time and at runtime.
|
|
30
|
+
*/
|
|
31
|
+
export type Typed<P extends string> = `${P}_${string}`;
|
|
32
|
+
|
|
33
|
+
/** Parsed components of a canonical TypeID string. */
|
|
34
|
+
export interface Parsed {
|
|
35
|
+
/** Prefix segment (everything before the final underscore). */
|
|
36
|
+
prefix: string;
|
|
37
|
+
/** Backing UUID in canonical 8-4-4-4-12 hyphenated form. */
|
|
38
|
+
uuid: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Generate a new TypeID with the given prefix, returning the canonical
|
|
43
|
+
* string `<prefix>_<26-char-base32>`.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* newId('task') // => 'task_01h2e8kqv8000000000000000'
|
|
47
|
+
*/
|
|
48
|
+
export function newId(prefix: string): string {
|
|
49
|
+
return typeid(prefix).toString();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Parse a canonical TypeID string into its prefix and backing UUID.
|
|
54
|
+
*
|
|
55
|
+
* @throws {Error} when the input is not a syntactically valid TypeID
|
|
56
|
+
* (e.g. malformed suffix, illegal prefix characters).
|
|
57
|
+
*/
|
|
58
|
+
export function parse(s: string): Parsed {
|
|
59
|
+
const tid = TypeID.fromString(s);
|
|
60
|
+
return {
|
|
61
|
+
prefix: tid.getType(),
|
|
62
|
+
uuid: tid.toUUID(),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Typed variant of {@link newId}. Returns a `Typed<P>` whose static
|
|
68
|
+
* type carries the literal prefix, enabling compile-time discrimination
|
|
69
|
+
* between e.g. `TaskId` and `InvoiceId`.
|
|
70
|
+
*/
|
|
71
|
+
export function newTyped<P extends string>(prefix: P): Typed<P> {
|
|
72
|
+
return typeid(prefix).toString() as Typed<P>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Typed variant of {@link parse}. Validates that the parsed prefix
|
|
77
|
+
* matches the expected `prefix` argument at runtime; returns the
|
|
78
|
+
* canonical string narrowed to `Typed<P>`.
|
|
79
|
+
*
|
|
80
|
+
* @throws {Error} when the parsed prefix does not match `prefix`, or
|
|
81
|
+
* when the input is not a syntactically valid TypeID.
|
|
82
|
+
*/
|
|
83
|
+
export function parseTyped<P extends string>(prefix: P, s: string): Typed<P> {
|
|
84
|
+
// TypeID.fromString with a prefix argument enforces the match and
|
|
85
|
+
// throws on mismatch, giving us a single source of truth for both
|
|
86
|
+
// structural and prefix validation.
|
|
87
|
+
const tid = TypeID.fromString(s, prefix);
|
|
88
|
+
return tid.toString() as Typed<P>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Crockford base32 alphabet used by the TypeID spec (lowercase),
|
|
92
|
+
// excluding i, l, o, u to avoid look-alike confusion. Module-internal:
|
|
93
|
+
// callers should validate via `typeIdSchema(prefix)` instead.
|
|
94
|
+
const SUFFIX_RE = /^[0-7][0-9a-hjkmnp-tv-z]{25}$/;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Zod schema for runtime validation of a `${prefix}_<26-char-base32>`
|
|
98
|
+
* canonical TypeID string.
|
|
99
|
+
*
|
|
100
|
+
* The schema only validates structure (prefix match + 26-char Crockford
|
|
101
|
+
* base32 suffix with the spec's leading-bit constraint). It does not
|
|
102
|
+
* decode the UUIDv7 timestamp; use {@link parse} if you need the
|
|
103
|
+
* underlying UUID.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* const TaskIdSchema = typeIdSchema('task');
|
|
107
|
+
* TaskIdSchema.parse('task_01h2e8kqv8000000000000000');
|
|
108
|
+
*/
|
|
109
|
+
export function typeIdSchema(prefix: string): z.ZodString {
|
|
110
|
+
const pattern = new RegExp(
|
|
111
|
+
`^${escapeRegExp(prefix)}_${SUFFIX_RE.source.slice(1, -1)}$`,
|
|
112
|
+
);
|
|
113
|
+
return z
|
|
114
|
+
.string()
|
|
115
|
+
.regex(pattern, `invalid typeid for prefix '${prefix}'`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function escapeRegExp(s: string): string {
|
|
119
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
120
|
+
}
|