@hop-top/kit 0.4.0-alpha.1 → 0.5.0-alpha.0
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 +24 -0
- package/README.md +97 -2
- 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 +14 -4
- 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
- package/src/uri.ts +7 -7
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import {
|
|
2
|
+
describe,
|
|
3
|
+
it,
|
|
4
|
+
expect,
|
|
5
|
+
beforeEach,
|
|
6
|
+
afterEach,
|
|
7
|
+
vi,
|
|
8
|
+
} from 'vitest';
|
|
9
|
+
import { promises as fs } from 'node:fs';
|
|
10
|
+
import * as os from 'node:os';
|
|
11
|
+
import * as path from 'node:path';
|
|
12
|
+
|
|
13
|
+
import { Client } from './client';
|
|
14
|
+
import { consentPath } from './consent';
|
|
15
|
+
import { installIdPath, resetForTest } from './installId';
|
|
16
|
+
|
|
17
|
+
// Each test gets its own XDG roots so we never touch the real
|
|
18
|
+
// ~/.config/kit/config.yaml or ~/.local/state/kit/telemetry tree.
|
|
19
|
+
let tmpRoot: string;
|
|
20
|
+
let savedConfigHome: string | undefined;
|
|
21
|
+
let savedStateHome: string | undefined;
|
|
22
|
+
let savedMode: string | undefined;
|
|
23
|
+
let savedEndpoint: string | undefined;
|
|
24
|
+
let savedSink: string | undefined;
|
|
25
|
+
let savedSinkFile: string | undefined;
|
|
26
|
+
let savedQueueSize: string | undefined;
|
|
27
|
+
|
|
28
|
+
beforeEach(async () => {
|
|
29
|
+
savedConfigHome = process.env.XDG_CONFIG_HOME;
|
|
30
|
+
savedStateHome = process.env.XDG_STATE_HOME;
|
|
31
|
+
savedMode = process.env.KIT_TELEMETRY_MODE;
|
|
32
|
+
savedEndpoint = process.env.KIT_TELEMETRY_ENDPOINT;
|
|
33
|
+
savedSink = process.env.KIT_TELEMETRY_SINK;
|
|
34
|
+
savedSinkFile = process.env.KIT_TELEMETRY_SINK_FILE;
|
|
35
|
+
savedQueueSize = process.env.KIT_TELEMETRY_QUEUE_SIZE;
|
|
36
|
+
|
|
37
|
+
tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'kit-telemetry-client-'));
|
|
38
|
+
process.env.XDG_CONFIG_HOME = path.join(tmpRoot, 'config');
|
|
39
|
+
process.env.XDG_STATE_HOME = path.join(tmpRoot, 'state');
|
|
40
|
+
delete process.env.KIT_TELEMETRY_MODE;
|
|
41
|
+
delete process.env.KIT_TELEMETRY_ENDPOINT;
|
|
42
|
+
delete process.env.KIT_TELEMETRY_SINK;
|
|
43
|
+
delete process.env.KIT_TELEMETRY_SINK_FILE;
|
|
44
|
+
delete process.env.KIT_TELEMETRY_QUEUE_SIZE;
|
|
45
|
+
|
|
46
|
+
await resetForTest();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
afterEach(async () => {
|
|
50
|
+
function restore(key: string, val: string | undefined): void {
|
|
51
|
+
if (val === undefined) delete process.env[key];
|
|
52
|
+
else process.env[key] = val;
|
|
53
|
+
}
|
|
54
|
+
restore('XDG_CONFIG_HOME', savedConfigHome);
|
|
55
|
+
restore('XDG_STATE_HOME', savedStateHome);
|
|
56
|
+
restore('KIT_TELEMETRY_MODE', savedMode);
|
|
57
|
+
restore('KIT_TELEMETRY_ENDPOINT', savedEndpoint);
|
|
58
|
+
restore('KIT_TELEMETRY_SINK', savedSink);
|
|
59
|
+
restore('KIT_TELEMETRY_SINK_FILE', savedSinkFile);
|
|
60
|
+
restore('KIT_TELEMETRY_QUEUE_SIZE', savedQueueSize);
|
|
61
|
+
await fs.rm(tmpRoot, { recursive: true, force: true });
|
|
62
|
+
vi.restoreAllMocks();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
async function grantConsent(): Promise<void> {
|
|
66
|
+
const p = consentPath();
|
|
67
|
+
await fs.mkdir(path.dirname(p), { recursive: true, mode: 0o700 });
|
|
68
|
+
await fs.writeFile(
|
|
69
|
+
p,
|
|
70
|
+
[
|
|
71
|
+
'kit:',
|
|
72
|
+
' telemetry:',
|
|
73
|
+
' consent:',
|
|
74
|
+
' state: granted',
|
|
75
|
+
' prompt_version: 1',
|
|
76
|
+
' decision_source: prompt',
|
|
77
|
+
' decided_at: 2026-05-19T12:00:00Z',
|
|
78
|
+
].join('\n'),
|
|
79
|
+
{ mode: 0o600 },
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function denyConsent(): Promise<void> {
|
|
84
|
+
const p = consentPath();
|
|
85
|
+
await fs.mkdir(path.dirname(p), { recursive: true, mode: 0o700 });
|
|
86
|
+
await fs.writeFile(
|
|
87
|
+
p,
|
|
88
|
+
'kit:\n telemetry:\n consent:\n state: denied\n',
|
|
89
|
+
{ mode: 0o600 },
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
describe('Client.record gating', () => {
|
|
94
|
+
it('no-ops when consent is denied', async () => {
|
|
95
|
+
await denyConsent();
|
|
96
|
+
process.env.KIT_TELEMETRY_MODE = 'anon';
|
|
97
|
+
const sinkFile = path.join(tmpRoot, 'events.jsonl');
|
|
98
|
+
const c = new Client({ sink: 'jsonl', sinkFile });
|
|
99
|
+
c.record('test.event', { foo: 'bar' });
|
|
100
|
+
await c.shutdown(500);
|
|
101
|
+
await expect(fs.access(sinkFile)).rejects.toThrow();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('no-ops when consent file is missing', async () => {
|
|
105
|
+
process.env.KIT_TELEMETRY_MODE = 'anon';
|
|
106
|
+
const sinkFile = path.join(tmpRoot, 'events.jsonl');
|
|
107
|
+
const c = new Client({ sink: 'jsonl', sinkFile });
|
|
108
|
+
c.record('test.event');
|
|
109
|
+
await c.shutdown(500);
|
|
110
|
+
await expect(fs.access(sinkFile)).rejects.toThrow();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('no-ops when mode is off (default)', async () => {
|
|
114
|
+
await grantConsent();
|
|
115
|
+
const sinkFile = path.join(tmpRoot, 'events.jsonl');
|
|
116
|
+
const c = new Client({ sink: 'jsonl', sinkFile });
|
|
117
|
+
c.record('test.event');
|
|
118
|
+
await c.shutdown(500);
|
|
119
|
+
await expect(fs.access(sinkFile)).rejects.toThrow();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('returns synchronously under saturation (avg << 1ms)', async () => {
|
|
123
|
+
await grantConsent();
|
|
124
|
+
process.env.KIT_TELEMETRY_MODE = 'anon';
|
|
125
|
+
const sinkFile = path.join(tmpRoot, 'events.jsonl');
|
|
126
|
+
const c = new Client({ sink: 'jsonl', sinkFile, queueSize: 4 });
|
|
127
|
+
// Hot loop: amortised cost must be non-blocking. We measure the
|
|
128
|
+
// average rather than each iteration to tolerate GC pauses and
|
|
129
|
+
// YAML-parse jitter on the consent fast-path.
|
|
130
|
+
const start = process.hrtime.bigint();
|
|
131
|
+
const N = 1000;
|
|
132
|
+
for (let i = 0; i < N; i += 1) c.record('hot', { i });
|
|
133
|
+
const totalMs = Number(process.hrtime.bigint() - start) / 1e6;
|
|
134
|
+
expect(totalMs / N).toBeLessThan(1);
|
|
135
|
+
await c.shutdown(2_000);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('bumps droppedCount when the queue is full', async () => {
|
|
139
|
+
await grantConsent();
|
|
140
|
+
process.env.KIT_TELEMETRY_MODE = 'anon';
|
|
141
|
+
const sinkFile = path.join(tmpRoot, 'events.jsonl');
|
|
142
|
+
// Tiny queue + a sink that doesn't drain (we never call shutdown
|
|
143
|
+
// until the end) → guaranteed saturation.
|
|
144
|
+
const c = new Client({ sink: 'jsonl', sinkFile, queueSize: 2 });
|
|
145
|
+
c.record('e', { i: 1 });
|
|
146
|
+
c.record('e', { i: 2 });
|
|
147
|
+
// Queue full now.
|
|
148
|
+
c.record('e', { i: 3 });
|
|
149
|
+
c.record('e', { i: 4 });
|
|
150
|
+
expect(c.droppedCount).toBeGreaterThanOrEqual(2);
|
|
151
|
+
await c.shutdown(2_000);
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
describe('Client.jsonl sink', () => {
|
|
156
|
+
it('writes N envelopes to the file, one JSON object per line', async () => {
|
|
157
|
+
await grantConsent();
|
|
158
|
+
process.env.KIT_TELEMETRY_MODE = 'anon';
|
|
159
|
+
const sinkFile = path.join(tmpRoot, 'events.jsonl');
|
|
160
|
+
const c = new Client({ sink: 'jsonl', sinkFile, sdkVersion: '9.9.9' });
|
|
161
|
+
|
|
162
|
+
c.record('a.event', { k: 1 });
|
|
163
|
+
c.record('b.event', { k: 2 });
|
|
164
|
+
c.record('c.event', { k: 3 });
|
|
165
|
+
await c.shutdown(2_000);
|
|
166
|
+
|
|
167
|
+
const raw = await fs.readFile(sinkFile, 'utf8');
|
|
168
|
+
const lines = raw.trim().split('\n');
|
|
169
|
+
expect(lines).toHaveLength(3);
|
|
170
|
+
for (const line of lines) {
|
|
171
|
+
const env = JSON.parse(line);
|
|
172
|
+
expect(env.schema_version).toBe('1');
|
|
173
|
+
expect(env.sdk_lang).toBe('ts');
|
|
174
|
+
expect(env.sdk_version).toBe('9.9.9');
|
|
175
|
+
expect(env.installation_id).toMatch(/^[0-9a-f]{64}$/);
|
|
176
|
+
expect(env.mode).toBe('anon');
|
|
177
|
+
expect(typeof env.occurred_at).toBe('string');
|
|
178
|
+
expect(typeof env.event).toBe('string');
|
|
179
|
+
expect(typeof env.attrs).toBe('object');
|
|
180
|
+
}
|
|
181
|
+
// Ensure install_id file was created.
|
|
182
|
+
const stat = await fs.stat(installIdPath());
|
|
183
|
+
expect(stat.size).toBe(32);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it('applies the custom redactor BEFORE the default redact pass', async () => {
|
|
187
|
+
await grantConsent();
|
|
188
|
+
process.env.KIT_TELEMETRY_MODE = 'full';
|
|
189
|
+
const sinkFile = path.join(tmpRoot, 'events.jsonl');
|
|
190
|
+
const c = new Client({
|
|
191
|
+
sink: 'jsonl',
|
|
192
|
+
sinkFile,
|
|
193
|
+
redactor: (attrs) => ({ ...attrs, custom: 'TOUCHED' }),
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
c.record('e', { secret: 'sk-deadbeef12345678', plain: 'hi' });
|
|
197
|
+
await c.shutdown(2_000);
|
|
198
|
+
|
|
199
|
+
const raw = await fs.readFile(sinkFile, 'utf8');
|
|
200
|
+
const env = JSON.parse(raw.trim());
|
|
201
|
+
expect(env.attrs.custom).toBe('TOUCHED');
|
|
202
|
+
expect(env.attrs.secret).toBe('<redacted:token>');
|
|
203
|
+
expect(env.attrs.plain).toBe('hi');
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
describe('Client.https sink', () => {
|
|
208
|
+
it('POSTs NDJSON and accepts a 2xx response', async () => {
|
|
209
|
+
await grantConsent();
|
|
210
|
+
process.env.KIT_TELEMETRY_MODE = 'anon';
|
|
211
|
+
|
|
212
|
+
const calls: Array<{ url: string; body: string }> = [];
|
|
213
|
+
const mockFetch = vi.fn(async (url: string, init: RequestInit) => {
|
|
214
|
+
calls.push({ url, body: String(init.body) });
|
|
215
|
+
return new Response(null, { status: 202 });
|
|
216
|
+
});
|
|
217
|
+
vi.stubGlobal('fetch', mockFetch);
|
|
218
|
+
|
|
219
|
+
const c = new Client({ sink: 'https', endpoint: 'https://t.example/ingest' });
|
|
220
|
+
c.record('a', { i: 1 });
|
|
221
|
+
c.record('b', { i: 2 });
|
|
222
|
+
await c.shutdown(2_000);
|
|
223
|
+
|
|
224
|
+
expect(mockFetch).toHaveBeenCalledTimes(1);
|
|
225
|
+
expect(calls[0].url).toBe('https://t.example/ingest');
|
|
226
|
+
const lines = calls[0].body.trim().split('\n');
|
|
227
|
+
expect(lines).toHaveLength(2);
|
|
228
|
+
for (const line of lines) {
|
|
229
|
+
const env = JSON.parse(line);
|
|
230
|
+
expect(env.schema_version).toBe('1');
|
|
231
|
+
expect(env.sdk_lang).toBe('ts');
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('retries once on 5xx, then drops', async () => {
|
|
236
|
+
await grantConsent();
|
|
237
|
+
process.env.KIT_TELEMETRY_MODE = 'anon';
|
|
238
|
+
|
|
239
|
+
const mockFetch = vi.fn(async () => new Response(null, { status: 503 }));
|
|
240
|
+
vi.stubGlobal('fetch', mockFetch);
|
|
241
|
+
|
|
242
|
+
const c = new Client({ sink: 'https', endpoint: 'https://t.example/ingest' });
|
|
243
|
+
c.record('e', {});
|
|
244
|
+
await c.shutdown(2_000);
|
|
245
|
+
|
|
246
|
+
expect(mockFetch).toHaveBeenCalledTimes(2);
|
|
247
|
+
expect(c.droppedCount).toBeGreaterThanOrEqual(1);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it('drops events when no endpoint is configured', async () => {
|
|
251
|
+
await grantConsent();
|
|
252
|
+
process.env.KIT_TELEMETRY_MODE = 'anon';
|
|
253
|
+
|
|
254
|
+
const mockFetch = vi.fn();
|
|
255
|
+
vi.stubGlobal('fetch', mockFetch);
|
|
256
|
+
|
|
257
|
+
const c = new Client({ sink: 'https' });
|
|
258
|
+
c.record('e', {});
|
|
259
|
+
await c.shutdown(2_000);
|
|
260
|
+
|
|
261
|
+
expect(mockFetch).not.toHaveBeenCalled();
|
|
262
|
+
expect(c.droppedCount).toBeGreaterThanOrEqual(1);
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
describe('Client anon mode strips attrs', () => {
|
|
267
|
+
it('drops attrs payload to null in anon mode even with obvious PII', async () => {
|
|
268
|
+
await grantConsent();
|
|
269
|
+
process.env.KIT_TELEMETRY_MODE = 'anon';
|
|
270
|
+
const sinkFile = path.join(tmpRoot, 'events.jsonl');
|
|
271
|
+
const c = new Client({ sink: 'jsonl', sinkFile });
|
|
272
|
+
c.record('user.signup', {
|
|
273
|
+
email: 'alice@example.com',
|
|
274
|
+
ip: '192.168.1.42',
|
|
275
|
+
token: 'sk-deadbeef12345678',
|
|
276
|
+
note: 'totally not PII',
|
|
277
|
+
});
|
|
278
|
+
await c.shutdown(2_000);
|
|
279
|
+
|
|
280
|
+
const raw = await fs.readFile(sinkFile, 'utf8');
|
|
281
|
+
const env = JSON.parse(raw.trim());
|
|
282
|
+
expect(env.mode).toBe('anon');
|
|
283
|
+
// `attrs` key survives (shape stability) but payload is null. Matches
|
|
284
|
+
// the rs SDK's Value::Null strip for cross-lang byte-shape parity.
|
|
285
|
+
expect('attrs' in env).toBe(true);
|
|
286
|
+
expect(env.attrs).toBeNull();
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it('preserves attrs in full mode (regression guard)', async () => {
|
|
290
|
+
await grantConsent();
|
|
291
|
+
process.env.KIT_TELEMETRY_MODE = 'full';
|
|
292
|
+
const sinkFile = path.join(tmpRoot, 'events.jsonl');
|
|
293
|
+
const c = new Client({ sink: 'jsonl', sinkFile });
|
|
294
|
+
c.record('user.signup', { plan: 'pro', seats: 3 });
|
|
295
|
+
await c.shutdown(2_000);
|
|
296
|
+
|
|
297
|
+
const raw = await fs.readFile(sinkFile, 'utf8');
|
|
298
|
+
const env = JSON.parse(raw.trim());
|
|
299
|
+
expect(env.mode).toBe('full');
|
|
300
|
+
expect(env.attrs).toEqual({ plan: 'pro', seats: 3 });
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it('bypasses the custom redactor in anon mode (PII cannot be reinjected)', async () => {
|
|
304
|
+
await grantConsent();
|
|
305
|
+
process.env.KIT_TELEMETRY_MODE = 'anon';
|
|
306
|
+
const sinkFile = path.join(tmpRoot, 'events.jsonl');
|
|
307
|
+
|
|
308
|
+
const calls: Array<Record<string, unknown>> = [];
|
|
309
|
+
const c = new Client({
|
|
310
|
+
sink: 'jsonl',
|
|
311
|
+
sinkFile,
|
|
312
|
+
// A custom redactor that tries to inject PII back. In anon mode
|
|
313
|
+
// we must skip it entirely so the attempt can't land.
|
|
314
|
+
redactor: (attrs) => {
|
|
315
|
+
calls.push(attrs);
|
|
316
|
+
return { ...attrs, smuggled: 'sk-deadbeef12345678' };
|
|
317
|
+
},
|
|
318
|
+
});
|
|
319
|
+
c.record('evt', { email: 'alice@example.com' });
|
|
320
|
+
await c.shutdown(2_000);
|
|
321
|
+
|
|
322
|
+
const raw = await fs.readFile(sinkFile, 'utf8');
|
|
323
|
+
const env = JSON.parse(raw.trim());
|
|
324
|
+
expect(env.attrs).toBeNull();
|
|
325
|
+
// Redactor was never invoked in anon mode.
|
|
326
|
+
expect(calls).toHaveLength(0);
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
describe('Client.shutdown', () => {
|
|
331
|
+
it('drains queued events within the timeout window', async () => {
|
|
332
|
+
await grantConsent();
|
|
333
|
+
process.env.KIT_TELEMETRY_MODE = 'anon';
|
|
334
|
+
const sinkFile = path.join(tmpRoot, 'events.jsonl');
|
|
335
|
+
const c = new Client({ sink: 'jsonl', sinkFile });
|
|
336
|
+
|
|
337
|
+
for (let i = 0; i < 10; i += 1) c.record('drain', { i });
|
|
338
|
+
await c.shutdown(2_000);
|
|
339
|
+
|
|
340
|
+
const raw = await fs.readFile(sinkFile, 'utf8');
|
|
341
|
+
const lines = raw.trim().split('\n');
|
|
342
|
+
expect(lines).toHaveLength(10);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it('is idempotent', async () => {
|
|
346
|
+
await grantConsent();
|
|
347
|
+
process.env.KIT_TELEMETRY_MODE = 'anon';
|
|
348
|
+
const c = new Client({
|
|
349
|
+
sink: 'jsonl',
|
|
350
|
+
sinkFile: path.join(tmpRoot, 'events.jsonl'),
|
|
351
|
+
});
|
|
352
|
+
await c.shutdown(500);
|
|
353
|
+
await expect(c.shutdown(500)).resolves.toBeUndefined();
|
|
354
|
+
});
|
|
355
|
+
});
|