@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,234 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { promises as fs } from 'node:fs';
|
|
3
|
+
import * as os from 'node:os';
|
|
4
|
+
import * as path from 'node:path';
|
|
5
|
+
import {
|
|
6
|
+
loadConsent,
|
|
7
|
+
consentPath,
|
|
8
|
+
legacyConsentPath,
|
|
9
|
+
deniedConsent,
|
|
10
|
+
} from './consent';
|
|
11
|
+
|
|
12
|
+
// Each test gets its own XDG_CONFIG_HOME pointed at a fresh temp dir
|
|
13
|
+
// so we never touch the real ~/.config/kit/config.yaml or its legacy
|
|
14
|
+
// telemetry.yaml sibling.
|
|
15
|
+
let tmpRoot: string;
|
|
16
|
+
let savedConfigHome: string | undefined;
|
|
17
|
+
|
|
18
|
+
beforeEach(async () => {
|
|
19
|
+
savedConfigHome = process.env.XDG_CONFIG_HOME;
|
|
20
|
+
tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'kit-telemetry-consent-'));
|
|
21
|
+
process.env.XDG_CONFIG_HOME = tmpRoot;
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
afterEach(async () => {
|
|
25
|
+
if (savedConfigHome === undefined) {
|
|
26
|
+
delete process.env.XDG_CONFIG_HOME;
|
|
27
|
+
} else {
|
|
28
|
+
process.env.XDG_CONFIG_HOME = savedConfigHome;
|
|
29
|
+
}
|
|
30
|
+
await fs.rm(tmpRoot, { recursive: true, force: true });
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
async function writeConsentFile(contents: string): Promise<void> {
|
|
34
|
+
const p = consentPath();
|
|
35
|
+
await fs.mkdir(path.dirname(p), { recursive: true, mode: 0o700 });
|
|
36
|
+
await fs.writeFile(p, contents, { mode: 0o600 });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function writeLegacyConsentFile(contents: string): Promise<void> {
|
|
40
|
+
const p = legacyConsentPath();
|
|
41
|
+
await fs.mkdir(path.dirname(p), { recursive: true, mode: 0o700 });
|
|
42
|
+
await fs.writeFile(p, contents, { mode: 0o600 });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe('consentPath', () => {
|
|
46
|
+
it('resolves under XDG_CONFIG_HOME/kit/config.yaml', () => {
|
|
47
|
+
expect(consentPath()).toBe(path.join(tmpRoot, 'kit', 'config.yaml'));
|
|
48
|
+
});
|
|
49
|
+
it('exposes the legacy telemetry.yaml as a sibling', () => {
|
|
50
|
+
expect(legacyConsentPath()).toBe(
|
|
51
|
+
path.join(tmpRoot, 'kit', 'telemetry.yaml'),
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('loadConsent', () => {
|
|
57
|
+
it('returns deniedConsent when the file is missing', async () => {
|
|
58
|
+
const got = await loadConsent();
|
|
59
|
+
expect(got).toEqual(deniedConsent);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('returns deniedConsent for malformed YAML', async () => {
|
|
63
|
+
await writeConsentFile('this: is: not: yaml: [oops');
|
|
64
|
+
const got = await loadConsent();
|
|
65
|
+
expect(got).toEqual(deniedConsent);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('returns deniedConsent when the root is not a mapping', async () => {
|
|
69
|
+
await writeConsentFile('- just-a-list\n- of-strings\n');
|
|
70
|
+
const got = await loadConsent();
|
|
71
|
+
expect(got).toEqual(deniedConsent);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('returns deniedConsent when telemetry block is missing', async () => {
|
|
75
|
+
await writeConsentFile('unrelated:\n key: value\n');
|
|
76
|
+
const got = await loadConsent();
|
|
77
|
+
expect(got).toEqual(deniedConsent);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('returns deniedConsent when kit.telemetry.consent block is missing', async () => {
|
|
81
|
+
await writeConsentFile('kit:\n telemetry:\n other: stuff\n');
|
|
82
|
+
const got = await loadConsent();
|
|
83
|
+
expect(got).toEqual(deniedConsent);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('returns deniedConsent for unknown state value', async () => {
|
|
87
|
+
await writeConsentFile(
|
|
88
|
+
[
|
|
89
|
+
'kit:',
|
|
90
|
+
' telemetry:',
|
|
91
|
+
' consent:',
|
|
92
|
+
' state: maybe',
|
|
93
|
+
' prompt_version: 1',
|
|
94
|
+
'',
|
|
95
|
+
].join('\n'),
|
|
96
|
+
);
|
|
97
|
+
const got = await loadConsent();
|
|
98
|
+
expect(got).toEqual(deniedConsent);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('parses a granted decision with all fields', async () => {
|
|
102
|
+
await writeConsentFile(
|
|
103
|
+
[
|
|
104
|
+
'kit:',
|
|
105
|
+
' telemetry:',
|
|
106
|
+
' consent:',
|
|
107
|
+
' state: granted',
|
|
108
|
+
' prompt_version: 1',
|
|
109
|
+
' decision_source: prompt',
|
|
110
|
+
' decided_at: 2026-05-19T12:00:00Z',
|
|
111
|
+
'',
|
|
112
|
+
].join('\n'),
|
|
113
|
+
);
|
|
114
|
+
const got = await loadConsent();
|
|
115
|
+
expect(got).toEqual({
|
|
116
|
+
allowed: true,
|
|
117
|
+
promptVersion: 1,
|
|
118
|
+
decisionSource: 'prompt',
|
|
119
|
+
decidedAt: '2026-05-19T12:00:00Z',
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('parses a denied decision with all fields', async () => {
|
|
124
|
+
await writeConsentFile(
|
|
125
|
+
[
|
|
126
|
+
'kit:',
|
|
127
|
+
' telemetry:',
|
|
128
|
+
' consent:',
|
|
129
|
+
' state: denied',
|
|
130
|
+
' prompt_version: 2',
|
|
131
|
+
' decision_source: prompt',
|
|
132
|
+
' decided_at: 2026-05-19T12:00:00Z',
|
|
133
|
+
'',
|
|
134
|
+
].join('\n'),
|
|
135
|
+
);
|
|
136
|
+
const got = await loadConsent();
|
|
137
|
+
expect(got).toEqual({
|
|
138
|
+
allowed: false,
|
|
139
|
+
promptVersion: 2,
|
|
140
|
+
decisionSource: 'prompt',
|
|
141
|
+
decidedAt: '2026-05-19T12:00:00Z',
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('defaults missing optional fields to safe values', async () => {
|
|
146
|
+
await writeConsentFile(
|
|
147
|
+
[
|
|
148
|
+
'kit:',
|
|
149
|
+
' telemetry:',
|
|
150
|
+
' consent:',
|
|
151
|
+
' state: granted',
|
|
152
|
+
'',
|
|
153
|
+
].join('\n'),
|
|
154
|
+
);
|
|
155
|
+
const got = await loadConsent();
|
|
156
|
+
expect(got.allowed).toBe(true);
|
|
157
|
+
expect(got.promptVersion).toBe(0);
|
|
158
|
+
expect(got.decisionSource).toBe('config');
|
|
159
|
+
expect(got.decidedAt).toBeUndefined();
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('preserves sibling config keys without breaking parsing', async () => {
|
|
163
|
+
// The file is the kit AppConfig; siblings of kit.telemetry.consent
|
|
164
|
+
// may exist. Loader must ignore them.
|
|
165
|
+
await writeConsentFile(
|
|
166
|
+
[
|
|
167
|
+
'unrelated:',
|
|
168
|
+
' partition: ok',
|
|
169
|
+
'kit:',
|
|
170
|
+
' bus:',
|
|
171
|
+
' enforce: strict',
|
|
172
|
+
' telemetry:',
|
|
173
|
+
' other_partition: yes',
|
|
174
|
+
' consent:',
|
|
175
|
+
' state: granted',
|
|
176
|
+
' prompt_version: 3',
|
|
177
|
+
'',
|
|
178
|
+
].join('\n'),
|
|
179
|
+
);
|
|
180
|
+
const got = await loadConsent();
|
|
181
|
+
expect(got.allowed).toBe(true);
|
|
182
|
+
expect(got.promptVersion).toBe(3);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('falls back to legacy telemetry.yaml when config.yaml is absent', async () => {
|
|
186
|
+
// Pre-refactor shape: bare telemetry.consent at the top level.
|
|
187
|
+
await writeLegacyConsentFile(
|
|
188
|
+
[
|
|
189
|
+
'telemetry:',
|
|
190
|
+
' consent:',
|
|
191
|
+
' state: granted',
|
|
192
|
+
' prompt_version: 1',
|
|
193
|
+
' decision_source: prompt',
|
|
194
|
+
' decided_at: 2026-05-19T12:00:00Z',
|
|
195
|
+
'',
|
|
196
|
+
].join('\n'),
|
|
197
|
+
);
|
|
198
|
+
const got = await loadConsent();
|
|
199
|
+
expect(got).toEqual({
|
|
200
|
+
allowed: true,
|
|
201
|
+
promptVersion: 1,
|
|
202
|
+
decisionSource: 'prompt',
|
|
203
|
+
decidedAt: '2026-05-19T12:00:00Z',
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it('prefers canonical config.yaml when both files exist', async () => {
|
|
208
|
+
await writeLegacyConsentFile(
|
|
209
|
+
[
|
|
210
|
+
'telemetry:',
|
|
211
|
+
' consent:',
|
|
212
|
+
' state: granted',
|
|
213
|
+
' prompt_version: 1',
|
|
214
|
+
' decision_source: prompt',
|
|
215
|
+
'',
|
|
216
|
+
].join('\n'),
|
|
217
|
+
);
|
|
218
|
+
await writeConsentFile(
|
|
219
|
+
[
|
|
220
|
+
'kit:',
|
|
221
|
+
' telemetry:',
|
|
222
|
+
' consent:',
|
|
223
|
+
' state: denied',
|
|
224
|
+
' prompt_version: 2',
|
|
225
|
+
' decision_source: flag',
|
|
226
|
+
'',
|
|
227
|
+
].join('\n'),
|
|
228
|
+
);
|
|
229
|
+
const got = await loadConsent();
|
|
230
|
+
expect(got.allowed).toBe(false);
|
|
231
|
+
expect(got.promptVersion).toBe(2);
|
|
232
|
+
expect(got.decisionSource).toBe('flag');
|
|
233
|
+
});
|
|
234
|
+
});
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module telemetry/consent
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* Read-only consent-file loader — the TS mirror of the read path in
|
|
6
|
+
* `go/core/consent/file_store.go`.
|
|
7
|
+
*
|
|
8
|
+
* On-disk shape (YAML under `<XDG_CONFIG_HOME>/kit/config.yaml` at the
|
|
9
|
+
* `kit.telemetry.consent` partition):
|
|
10
|
+
*
|
|
11
|
+
* ```yaml
|
|
12
|
+
* kit:
|
|
13
|
+
* telemetry:
|
|
14
|
+
* consent:
|
|
15
|
+
* state: granted # | denied
|
|
16
|
+
* prompt_version: 1
|
|
17
|
+
* decision_source: prompt # | flag | config | env
|
|
18
|
+
* decided_at: 2026-05-19T12:00:00Z
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* A pre-refactor layout at `<XDG_CONFIG_HOME>/kit/telemetry.yaml` (with
|
|
22
|
+
* a bare `telemetry.consent` block at the top level) is read as a
|
|
23
|
+
* fallback so installs that have not been migrated yet still work.
|
|
24
|
+
*
|
|
25
|
+
* This TS module is **read-only**: writing the consent file remains the
|
|
26
|
+
* job of the Go `kit consent` CLI. SDK consumers just need to know
|
|
27
|
+
* whether emission is allowed.
|
|
28
|
+
*
|
|
29
|
+
* Anything that isn't a clean, parseable, `state: granted` block
|
|
30
|
+
* collapses to `deniedConsent` — a fail-closed default so a corrupt
|
|
31
|
+
* config can never accidentally enable emission.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
import { promises as fs } from 'node:fs';
|
|
35
|
+
import { existsSync } from 'node:fs';
|
|
36
|
+
import { homedir } from 'node:os';
|
|
37
|
+
import * as path from 'node:path';
|
|
38
|
+
import * as yaml from 'js-yaml';
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Consent is the resolved telemetry decision for the current
|
|
42
|
+
* installation. The shape mirrors the relevant subset of the Go
|
|
43
|
+
* `consent.Decision` struct.
|
|
44
|
+
*/
|
|
45
|
+
export interface Consent {
|
|
46
|
+
/** allowed is true only when state == "granted". */
|
|
47
|
+
readonly allowed: boolean;
|
|
48
|
+
/** promptVersion records which prompt revision the user accepted. */
|
|
49
|
+
readonly promptVersion: number;
|
|
50
|
+
/**
|
|
51
|
+
* decisionSource records how the decision was captured —
|
|
52
|
+
* "prompt" | "flag" | "config" | "env". Defaults to "config" when
|
|
53
|
+
* the file is missing or unreadable.
|
|
54
|
+
*/
|
|
55
|
+
readonly decisionSource: string;
|
|
56
|
+
/** decidedAt is the RFC3339 timestamp of the decision, if present. */
|
|
57
|
+
readonly decidedAt?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* deniedConsent is the canonical fail-closed default. Returned whenever
|
|
62
|
+
* the file is missing, unreadable, malformed, or carries an unknown
|
|
63
|
+
* state value.
|
|
64
|
+
*/
|
|
65
|
+
export const deniedConsent: Consent = {
|
|
66
|
+
allowed: false,
|
|
67
|
+
promptVersion: 0,
|
|
68
|
+
decisionSource: 'config',
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** xdgConfigHome returns `$XDG_CONFIG_HOME` or `~/.config`. */
|
|
72
|
+
function xdgConfigHome(): string {
|
|
73
|
+
return process.env.XDG_CONFIG_HOME ?? path.join(homedir(), '.config');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* consentPath returns the canonical on-disk path used by this module
|
|
78
|
+
* (`<XDG_CONFIG_HOME>/kit/config.yaml`). Useful for `kit telemetry
|
|
79
|
+
* status` diagnostics and SDK consumers that want to surface the
|
|
80
|
+
* location.
|
|
81
|
+
*/
|
|
82
|
+
export function consentPath(): string {
|
|
83
|
+
return path.join(xdgConfigHome(), 'kit', 'config.yaml');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* legacyConsentPath returns the pre-refactor consent file location
|
|
88
|
+
* (`<XDG_CONFIG_HOME>/kit/telemetry.yaml`). Read-only fallback consumed
|
|
89
|
+
* by `loadConsent`; SDK callers should prefer `consentPath`.
|
|
90
|
+
*/
|
|
91
|
+
export function legacyConsentPath(): string {
|
|
92
|
+
return path.join(xdgConfigHome(), 'kit', 'telemetry.yaml');
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* loadConsent reads and parses the consent file, returning a fully
|
|
97
|
+
* resolved Consent struct. Failure modes fold to `deniedConsent`:
|
|
98
|
+
*
|
|
99
|
+
* - File missing → denied.
|
|
100
|
+
* - File unreadable → denied.
|
|
101
|
+
* - YAML parse error → denied.
|
|
102
|
+
* - Missing `kit.telemetry.consent` block (and no legacy) → denied.
|
|
103
|
+
* - `state` neither "granted" nor "denied" → denied.
|
|
104
|
+
*
|
|
105
|
+
* `state: "denied"` returns a Consent with `allowed=false` but with the
|
|
106
|
+
* other fields populated, so callers can distinguish an explicit deny
|
|
107
|
+
* from a fail-closed default.
|
|
108
|
+
*
|
|
109
|
+
* Read order: canonical `config.yaml` (`kit.telemetry.consent`) first,
|
|
110
|
+
* then legacy `telemetry.yaml` (`telemetry.consent`) as a fallback.
|
|
111
|
+
*/
|
|
112
|
+
export async function loadConsent(): Promise<Consent> {
|
|
113
|
+
const canonical = await readConsentBlock(consentPath(), 'canonical');
|
|
114
|
+
if (canonical !== null) {
|
|
115
|
+
return canonical;
|
|
116
|
+
}
|
|
117
|
+
const legacy = await readConsentBlock(legacyConsentPath(), 'legacy');
|
|
118
|
+
if (legacy !== null) {
|
|
119
|
+
return legacy;
|
|
120
|
+
}
|
|
121
|
+
return deniedConsent;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* readConsentBlock loads one YAML file and tries to decode a consent
|
|
126
|
+
* block from it. Returns the resolved Consent, or null when the file
|
|
127
|
+
* is missing / unreadable / malformed / lacks the expected block.
|
|
128
|
+
* Distinguishing null from `deniedConsent` lets the caller fall
|
|
129
|
+
* through to the next candidate path.
|
|
130
|
+
*/
|
|
131
|
+
async function readConsentBlock(
|
|
132
|
+
p: string,
|
|
133
|
+
variant: 'canonical' | 'legacy',
|
|
134
|
+
): Promise<Consent | null> {
|
|
135
|
+
if (!existsSync(p)) {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
let data: unknown;
|
|
140
|
+
try {
|
|
141
|
+
const raw = await fs.readFile(p, 'utf8');
|
|
142
|
+
data = yaml.load(raw);
|
|
143
|
+
} catch {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (typeof data !== 'object' || data === null) {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
const root = data as Record<string, unknown>;
|
|
151
|
+
|
|
152
|
+
// Canonical: kit.telemetry.consent. Legacy: telemetry.consent (no kit).
|
|
153
|
+
let telemetry: unknown;
|
|
154
|
+
if (variant === 'canonical') {
|
|
155
|
+
const kit = root.kit;
|
|
156
|
+
if (typeof kit !== 'object' || kit === null) {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
telemetry = (kit as Record<string, unknown>).telemetry;
|
|
160
|
+
} else {
|
|
161
|
+
telemetry = root.telemetry;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (typeof telemetry !== 'object' || telemetry === null) {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
const block = (telemetry as Record<string, unknown>).consent;
|
|
168
|
+
if (typeof block !== 'object' || block === null) {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
const c = block as Record<string, unknown>;
|
|
172
|
+
|
|
173
|
+
const state = c.state;
|
|
174
|
+
if (state !== 'granted' && state !== 'denied') {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
allowed: state === 'granted',
|
|
180
|
+
promptVersion: typeof c.prompt_version === 'number' ? c.prompt_version : 0,
|
|
181
|
+
decisionSource:
|
|
182
|
+
typeof c.decision_source === 'string' ? c.decision_source : 'config',
|
|
183
|
+
decidedAt: parseDecidedAt(c.decided_at),
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* parseDecidedAt coerces the raw YAML value into an RFC3339 string.
|
|
189
|
+
* js-yaml's default schema promotes unquoted ISO timestamps to JS
|
|
190
|
+
* `Date` objects, so we accept either form. Go writes `time.Time` as
|
|
191
|
+
* an RFC3339 string with the YAML `!!timestamp` tag — both reader
|
|
192
|
+
* paths converge here.
|
|
193
|
+
*/
|
|
194
|
+
function parseDecidedAt(v: unknown): string | undefined {
|
|
195
|
+
if (typeof v === 'string') {
|
|
196
|
+
return v;
|
|
197
|
+
}
|
|
198
|
+
if (v instanceof Date && !Number.isNaN(v.getTime())) {
|
|
199
|
+
return v.toISOString().replace(/\.\d{3}Z$/, 'Z');
|
|
200
|
+
}
|
|
201
|
+
return undefined;
|
|
202
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module telemetry
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* SDK-side telemetry primitives — TS mirror of
|
|
6
|
+
* `go/runtime/telemetry` (Mode + install_id) and the read path of
|
|
7
|
+
* `go/core/consent` (consent file reader).
|
|
8
|
+
*
|
|
9
|
+
* This module covers the three building blocks SDK consumers need
|
|
10
|
+
* before composing an emitter:
|
|
11
|
+
*
|
|
12
|
+
* - `Mode` / `parseMode` / `resolveMode` — what tier do we emit?
|
|
13
|
+
* - `getInstallId` / `rotate` / `installIdPath` — anonymous installation id.
|
|
14
|
+
* - `loadConsent` / `consentPath` / `Consent` — has the operator opted in?
|
|
15
|
+
*
|
|
16
|
+
* The emit path itself, the BatchProcessor, and the OTLP exporter land
|
|
17
|
+
* in follow-up sdk-telemetry tasks.
|
|
18
|
+
*
|
|
19
|
+
* Ground truth:
|
|
20
|
+
* - Event-schema doc `sdk/docs/telemetry-event-schema.md`
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export { Mode, parseMode, resolveMode } from './mode';
|
|
24
|
+
export {
|
|
25
|
+
getInstallId,
|
|
26
|
+
rotate,
|
|
27
|
+
installIdPath,
|
|
28
|
+
resetForTest,
|
|
29
|
+
} from './installId';
|
|
30
|
+
export {
|
|
31
|
+
loadConsent,
|
|
32
|
+
consentPath,
|
|
33
|
+
deniedConsent,
|
|
34
|
+
type Consent,
|
|
35
|
+
} from './consent';
|
|
36
|
+
export { redact, redactString } from './redact';
|
|
37
|
+
export {
|
|
38
|
+
Client,
|
|
39
|
+
type ClientOptions,
|
|
40
|
+
type SinkKind,
|
|
41
|
+
type Envelope,
|
|
42
|
+
} from './client';
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { promises as fs } from 'node:fs';
|
|
3
|
+
import * as os from 'node:os';
|
|
4
|
+
import * as path from 'node:path';
|
|
5
|
+
import {
|
|
6
|
+
getInstallId,
|
|
7
|
+
rotate,
|
|
8
|
+
resetForTest,
|
|
9
|
+
installIdPath,
|
|
10
|
+
} from './installId';
|
|
11
|
+
|
|
12
|
+
// Each test gets its own XDG_STATE_HOME pointed at a fresh temp dir
|
|
13
|
+
// so we never touch the real ~/.local/state/kit/telemetry tree.
|
|
14
|
+
let tmpRoot: string;
|
|
15
|
+
let savedStateHome: string | undefined;
|
|
16
|
+
|
|
17
|
+
beforeEach(async () => {
|
|
18
|
+
savedStateHome = process.env.XDG_STATE_HOME;
|
|
19
|
+
tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'kit-telemetry-installid-'));
|
|
20
|
+
process.env.XDG_STATE_HOME = tmpRoot;
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
afterEach(async () => {
|
|
24
|
+
if (savedStateHome === undefined) {
|
|
25
|
+
delete process.env.XDG_STATE_HOME;
|
|
26
|
+
} else {
|
|
27
|
+
process.env.XDG_STATE_HOME = savedStateHome;
|
|
28
|
+
}
|
|
29
|
+
await fs.rm(tmpRoot, { recursive: true, force: true });
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('installIdPath', () => {
|
|
33
|
+
it('resolves under XDG_STATE_HOME/kit/telemetry/installation_id', () => {
|
|
34
|
+
expect(installIdPath()).toBe(
|
|
35
|
+
path.join(tmpRoot, 'kit', 'telemetry', 'installation_id'),
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe('getInstallId', () => {
|
|
41
|
+
it('generates a 64-char lowercase hex string on first call', async () => {
|
|
42
|
+
const id = await getInstallId();
|
|
43
|
+
expect(id).toMatch(/^[0-9a-f]{64}$/);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('writes exactly 32 bytes to disk with mode 0600', async () => {
|
|
47
|
+
await getInstallId();
|
|
48
|
+
const stat = await fs.stat(installIdPath());
|
|
49
|
+
expect(stat.size).toBe(32);
|
|
50
|
+
// On POSIX, mask off the high bits; on win32 perms are advisory
|
|
51
|
+
// so just check we wrote *something* reasonable.
|
|
52
|
+
if (process.platform !== 'win32') {
|
|
53
|
+
expect(stat.mode & 0o777).toBe(0o600);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('creates the parent directory with mode 0700', async () => {
|
|
58
|
+
await getInstallId();
|
|
59
|
+
const stat = await fs.stat(path.dirname(installIdPath()));
|
|
60
|
+
expect(stat.isDirectory()).toBe(true);
|
|
61
|
+
if (process.platform !== 'win32') {
|
|
62
|
+
expect(stat.mode & 0o777).toBe(0o700);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('returns the same hex on repeated calls (stable across reads)', async () => {
|
|
67
|
+
const a = await getInstallId();
|
|
68
|
+
const b = await getInstallId();
|
|
69
|
+
const c = await getInstallId();
|
|
70
|
+
expect(a).toBe(b);
|
|
71
|
+
expect(b).toBe(c);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('throws when the on-disk file has wrong size', async () => {
|
|
75
|
+
// Write 16 bytes manually, then read should throw.
|
|
76
|
+
const p = installIdPath();
|
|
77
|
+
await fs.mkdir(path.dirname(p), { recursive: true, mode: 0o700 });
|
|
78
|
+
await fs.writeFile(p, Buffer.alloc(16, 0xab), { mode: 0o600 });
|
|
79
|
+
await expect(getInstallId()).rejects.toThrow(/wrong size 16 bytes/);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe('rotate', () => {
|
|
84
|
+
it('replaces the persisted bytes with a fresh value', async () => {
|
|
85
|
+
const first = await getInstallId();
|
|
86
|
+
const rotated = await rotate();
|
|
87
|
+
expect(rotated).toMatch(/^[0-9a-f]{64}$/);
|
|
88
|
+
expect(rotated).not.toBe(first);
|
|
89
|
+
// Stable after rotate.
|
|
90
|
+
const after = await getInstallId();
|
|
91
|
+
expect(after).toBe(rotated);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('works even when no file exists yet (cold rotate)', async () => {
|
|
95
|
+
const id = await rotate();
|
|
96
|
+
expect(id).toMatch(/^[0-9a-f]{64}$/);
|
|
97
|
+
const stat = await fs.stat(installIdPath());
|
|
98
|
+
expect(stat.size).toBe(32);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe('resetForTest', () => {
|
|
103
|
+
it('deletes the persisted file', async () => {
|
|
104
|
+
await getInstallId();
|
|
105
|
+
await resetForTest();
|
|
106
|
+
await expect(fs.access(installIdPath())).rejects.toThrow();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('is idempotent (no error when file is already gone)', async () => {
|
|
110
|
+
await resetForTest();
|
|
111
|
+
await expect(resetForTest()).resolves.toBeUndefined();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('regenerates a fresh ID after reset', async () => {
|
|
115
|
+
const a = await getInstallId();
|
|
116
|
+
await resetForTest();
|
|
117
|
+
const b = await getInstallId();
|
|
118
|
+
expect(b).toMatch(/^[0-9a-f]{64}$/);
|
|
119
|
+
expect(b).not.toBe(a);
|
|
120
|
+
});
|
|
121
|
+
});
|