-
Notifications
You must be signed in to change notification settings - Fork 644
Expand file tree
/
Copy pathschema.js
More file actions
285 lines (255 loc) · 9.28 KB
/
Copy pathschema.js
File metadata and controls
285 lines (255 loc) · 9.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Telemetry payload builder + validators. The privacy guarantee lives here:
// payloads are constructed from a closed allowlist of fields with closed
// enum values, so it is structurally impossible to attach an excluded field
// (email, paths, project name, dbUri, env, …) — even by accident from an
// upstream caller passing extra keys.
import { KIT_IDS } from '../core/kits.js';
import { CUSTOM_KIT_ID } from '../core/starter.js';
/**
* kitId values accepted on the wire: the six registry kits plus the `'custom'`
* sentinel a `--starter` install reports. Kept separate from {@link KIT_IDS} so
* the registry stays exactly the six known kits while telemetry can still say
* "a custom starter was used" without naming the (potentially private) repo.
* @type {ReadonlyArray<string>}
*/
const TELEMETRY_KIT_IDS = Object.freeze([ ...KIT_IDS, CUSTOM_KIT_ID ]);
/** @typedef {import('../index.js').KitId} KitId */
/** @typedef {import('../index.js').DbChoice} DbChoice */
/** @typedef {import('../index.js').PackageManager} PackageManager */
/** @typedef {import('../index.js').FailStage} FailStage */
/** @typedef {import('../index.js').TelemetryEvent} TelemetryEvent */
/**
* Symbolic error codes. Each value corresponds to a single
* `throw new StageError(..., { code })` site (or the preflight
* `UnsupportedPackageManagerError`). Not a contract type — internal to
* the schema, mirrored from grep'ing `src/core/steps/*` + `errors.js`.
* @typedef {(
* 'unsupported_pm' |
* 'target_exists' |
* 'git_missing' | 'git_spawn_failed' | 'git_clone_failed' |
* 'missing_scaffold_file' | 'scaffold_io' |
* 'npm_missing' | 'npm_spawn_failed' | 'install_failed' | 'apostrophe_missing' |
* 'db_unreachable' | 'db_auth_failed' | 'db_connect_failed' | 'db_drop_failed' |
* 'seed_manifest_invalid' | 'seed_download_failed' | 'seed_checksum_failed' |
* 'seed_unpack_failed' | 'seed_clear_failed' | 'seed_restore_failed' |
* 'seed_uploads_failed' |
* 'node_missing' | 'node_spawn_failed' | 'admin_user_failed'
* )} ErrorCode
*/
/** @type {ReadonlyArray<TelemetryEvent>} */
export const EVENT_NAMES = Object.freeze([ 'install_success', 'install_fail' ]);
/** @type {ReadonlyArray<DbChoice>} */
export const DB_CHOICES = Object.freeze([ 'sqlite', 'mongodb', 'postgres' ]);
/** @type {ReadonlyArray<PackageManager>} */
export const PACKAGE_MANAGERS = Object.freeze([
'npm', 'pnpm', 'yarn', 'unknown'
]);
/**
* FailStage enum, in array form so the validator can `.includes(null)`
* directly against either a string or `null` (preflight).
* @type {ReadonlyArray<FailStage>}
*/
export const FAIL_STAGES = Object.freeze([
'clone', 'dependency_install', 'db_connect', 'scaffold', 'sample_data',
'admin', 'unknown', null
]);
/**
* Strict allowlist of symbolic error codes the orchestrator may attach to a
* payload. Adding a new throw site means adding the code here. Unknown codes
* are dropped from the payload — never pass raw error strings.
* @type {ReadonlyArray<ErrorCode>}
*/
export const ERROR_CODES = Object.freeze([
'unsupported_pm',
'target_exists',
'git_missing', 'git_spawn_failed', 'git_clone_failed',
'missing_scaffold_file', 'scaffold_io',
'npm_missing', 'npm_spawn_failed', 'install_failed', 'apostrophe_missing',
'db_unreachable', 'db_auth_failed', 'db_connect_failed', 'db_drop_failed',
'seed_manifest_invalid', 'seed_download_failed', 'seed_checksum_failed',
'seed_unpack_failed', 'seed_clear_failed', 'seed_restore_failed',
'seed_uploads_failed',
'node_missing', 'node_spawn_failed', 'admin_user_failed'
]);
/** @type {ReadonlySet<string>} */
const ERROR_CODE_SET = new Set(ERROR_CODES);
/**
* Type-guard: narrows a permissive `string` (contract) to the strict
* {@link ErrorCode} allowlist. Used by {@link buildPayload} to drop unknown
* codes without leaking raw error strings into the wire payload.
* @param {string} code
* @returns {code is ErrorCode}
*/
function isErrorCode(code) {
return ERROR_CODE_SET.has(code);
}
/**
* Shared fields every payload carries, regardless of event. `anonymousId` is
* optional at the schema level: consent.js generates and threads it on opt-in,
* but the schema only enforces shape and stays decoupled from generation.
* @typedef {object} BasePayloadInput
* @property {string} cliVersion
* @property {string} [anonymousId]
* @property {KitId} kitId
* @property {DbChoice} dbChoice
* @property {PackageManager} packageManager
* @property {number} durationMs
*/
/**
* Additional input fields for `install_fail`. `errorCode` is typed `string`
* here — the schema validates it against {@link ERROR_CODES} at runtime and
* silently drops unknown values, so the contract stays permissive while the
* wire stays strict.
* @typedef {object} FailPayloadInputFields
* @property {FailStage} failStage
* @property {string} [errorCode]
*/
/**
* Additional output fields for `install_fail` — `errorCode` is narrowed to
* the {@link ErrorCode} allowlist (unknown values are dropped at build time).
* @typedef {object} FailPayloadFields
* @property {FailStage} failStage
* @property {ErrorCode} [errorCode]
*/
/**
* Input the builder accepts. Anything outside this allowlist is ignored —
* the builder copies only from this set.
* @typedef {BasePayloadInput & Partial<FailPayloadInputFields>} PayloadInput
*/
/**
* Outgoing payload for `install_success` — base + `event`.
* @typedef {BasePayloadInput & { event: 'install_success' }} SuccessPayload
*/
/**
* Outgoing payload for `install_fail` — base + fail fields + `event`.
* @typedef {BasePayloadInput & FailPayloadFields & { event: 'install_fail' }} FailPayload
*/
/** @typedef {SuccessPayload | FailPayload} TelemetryPayload */
class SchemaError extends Error {
/** @param {string} message */
constructor(message) {
super(message);
this.name = 'SchemaError';
}
}
/**
* @template T
* @param {string} name
* @param {unknown} value
* @param {ReadonlyArray<T>} allowed
* @returns {asserts value is T}
*/
function requireOneOf(name, value, allowed) {
if (!allowed.includes(/** @type {T} */ (value))) {
throw new SchemaError(`Invalid ${name}: ${JSON.stringify(value)}`);
}
}
/**
* @param {string} name
* @param {unknown} value
* @returns {asserts value is string}
*/
function requireString(name, value) {
if (typeof value !== 'string' || value.length === 0) {
throw new SchemaError(`Invalid ${name}: expected non-empty string`);
}
}
/**
* @param {string} name
* @param {unknown} value
* @returns {asserts value is number}
*/
function requireFiniteNumber(name, value) {
if (typeof value !== 'number' || !Number.isFinite(value)) {
throw new SchemaError(`Invalid ${name}: expected finite number`);
}
}
/**
* @overload
* @param {'install_success'} event
* @param {PayloadInput} input
* @returns {SuccessPayload}
*/
/**
* @overload
* @param {'install_fail'} event
* @param {PayloadInput} input
* @returns {FailPayload}
*/
/**
* @overload
* @param {TelemetryEvent} event
* @param {PayloadInput} input
* @returns {TelemetryPayload}
*/
/**
* Build a validated payload for one of the two events. Strictly allowlisted:
* only fields from {@link BasePayloadInput} (and, for fail, {@link FailPayloadFields})
* appear on the result.
*
* @param {TelemetryEvent} event
* @param {PayloadInput} input
* @returns {TelemetryPayload}
* @throws {SchemaError} for an invalid enum value, missing required field,
* or wrong type.
*/
export function buildPayload(event, input) {
requireOneOf('event', event, EVENT_NAMES);
requireString('cliVersion', input.cliVersion);
requireOneOf('kitId', input.kitId, TELEMETRY_KIT_IDS);
requireOneOf('dbChoice', input.dbChoice, DB_CHOICES);
requireOneOf('packageManager', input.packageManager, PACKAGE_MANAGERS);
requireFiniteNumber('durationMs', input.durationMs);
if (input.anonymousId !== undefined) {
requireString('anonymousId', input.anonymousId);
}
/** @type {BasePayloadInput} */
const base = {
cliVersion: input.cliVersion,
kitId: input.kitId,
dbChoice: input.dbChoice,
packageManager: input.packageManager,
durationMs: input.durationMs,
...(input.anonymousId !== undefined
? { anonymousId: input.anonymousId }
: {})
};
if (event === 'install_success') {
return {
event,
...base
};
}
requireOneOf('failStage', input.failStage, FAIL_STAGES);
/** @type {FailPayload} */
const fail = {
event,
...base,
failStage: input.failStage
};
if (input.errorCode !== undefined && isErrorCode(input.errorCode)) {
fail.errorCode = input.errorCode;
}
return fail;
}
/**
* The closed set of keys that may appear on a payload, by event. Exported for
* the firewall test to make the guarantee explicit.
* @param {TelemetryEvent} event
* @returns {ReadonlyArray<keyof SuccessPayload | keyof FailPayload>}
*/
export function allowedFields(event) {
return event === 'install_fail'
? FAIL_FIELDS
: SUCCESS_FIELDS;
}
/** @type {ReadonlyArray<keyof SuccessPayload>} */
const SUCCESS_FIELDS = Object.freeze([
'event', 'cliVersion', 'anonymousId', 'kitId', 'dbChoice',
'packageManager', 'durationMs'
]);
/** @type {ReadonlyArray<keyof FailPayload>} */
const FAIL_FIELDS = Object.freeze([
...SUCCESS_FIELDS, 'failStage', 'errorCode'
]);
export { SchemaError };