@snap/valdi 1.1.7 → 1.2.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.
Files changed (74) hide show
  1. package/.metadata/.bazelrc.template +8 -2
  2. package/.metadata/MODULE.bazel.template +20 -3
  3. package/.metadata/bzl/patches/rules_python/1.9.0/BUILD.bazel.template +1 -0
  4. package/.metadata/bzl/patches/rules_python/1.9.0/remove_setuptools_spaces.patch.template +96 -0
  5. package/README.md +35 -1
  6. package/bundled-skills/registry.json +2 -1
  7. package/bundled-skills/skills/valdi-animations/skill.md +49 -0
  8. package/bundled-skills/skills/valdi-async/skill.md +3 -2
  9. package/bundled-skills/skills/valdi-bazel/skill.md +2 -6
  10. package/bundled-skills/skills/valdi-cpp-runtime/skill.md +18 -0
  11. package/bundled-skills/skills/valdi-error-handling/skill.md +1 -1
  12. package/bundled-skills/skills/valdi-gestures/skill.md +3 -3
  13. package/bundled-skills/skills/valdi-migrate/skill.md +2 -1
  14. package/bundled-skills/skills/valdi-mock-breakdown/skill.md +4 -4
  15. package/bundled-skills/skills/valdi-mock-to-module/skill.md +9 -6
  16. package/bundled-skills/skills/valdi-overview/skill.md +2 -1
  17. package/bundled-skills/skills/valdi-perf/skill.md +121 -5
  18. package/bundled-skills/skills/valdi-pr-review/skill.md +189 -0
  19. package/bundled-skills/skills/valdi-profile/skill.md +5 -3
  20. package/bundled-skills/skills/valdi-setup/skill.md +3 -6
  21. package/bundled-skills/skills/valdi-testing/skill.md +1 -1
  22. package/bundled-skills/skills/valdi-tsx/skill.md +47 -11
  23. package/debugger/README.md +356 -0
  24. package/debugger/debugger-actions.js +373 -0
  25. package/debugger/debugger-api.js +225 -0
  26. package/debugger/debugger-bootstrap.js +295 -0
  27. package/debugger/debugger-model.js +790 -0
  28. package/debugger/debugger-performance.js +573 -0
  29. package/debugger/debugger-preview-html.js +676 -0
  30. package/debugger/debugger-providers.js +389 -0
  31. package/debugger/debugger-render.js +529 -0
  32. package/debugger/debugger-runtime.js +387 -0
  33. package/debugger/debugger-session.js +188 -0
  34. package/debugger/debugger-settings.js +209 -0
  35. package/debugger/debugger-state.js +221 -0
  36. package/debugger/debugger-tree-model.js +539 -0
  37. package/debugger/debugger.css +1865 -0
  38. package/debugger/devtools-panel.css +1318 -0
  39. package/debugger/devtools-panel.html +273 -0
  40. package/debugger/devtools-panel.js +3329 -0
  41. package/debugger/index.html +279 -0
  42. package/dist/commands/bootstrap.js +6 -5
  43. package/dist/commands/debugger.js +163 -0
  44. package/dist/commands/doctor.js +3 -62
  45. package/dist/commands/inspect.js +5 -2
  46. package/dist/commands/inspect_commands/input.js +176 -0
  47. package/dist/commands/inspect_commands/snapshot.js +1 -1
  48. package/dist/commands/inspect_commands/tree.js +1 -1
  49. package/dist/commands/projectsync.js +15 -0
  50. package/dist/commands/webHotreload.js +31 -0
  51. package/dist/core/constants.js +22 -6
  52. package/dist/debugger/chromiumConsole.js +279 -0
  53. package/dist/debugger/inputClient.js +473 -0
  54. package/dist/debugger/server.js +3801 -0
  55. package/dist/debugger/targetRegistry.js +436 -0
  56. package/dist/debugger/webPreviewPerformance.js +864 -0
  57. package/dist/index.js +0 -0
  58. package/dist/utils/BazelClient.js +34 -5
  59. package/dist/utils/applicationUtils.js +3 -0
  60. package/dist/utils/chromiumDevToolsClient.js +350 -0
  61. package/dist/utils/daemonClient.js +364 -44
  62. package/dist/utils/hermesClient.js +82 -176
  63. package/dist/utils/loopbackHost.js +14 -0
  64. package/dist/utils/owlCdpClient.js +282 -0
  65. package/dist/utils/owlDevToolsExtension.js +142 -0
  66. package/dist/utils/stringUtils.js +13 -1
  67. package/package.json +4 -2
  68. package/dist/commands/inspect_commands/snapshot.spec.js +0 -105
  69. package/dist/core/packageFiles.spec.js +0 -29
  70. package/dist/preview/previewGenerator.spec.js +0 -203
  71. package/dist/preview/viewModelParser.spec.js +0 -171
  72. package/dist/utils/linuxDistro.spec.js +0 -122
  73. package/dist/utils/skillsAdapters.spec.js +0 -141
  74. package/dist/utils/stringUtils.spec.js +0 -81
@@ -0,0 +1,473 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SUPPORTED_DEBUGGER_INPUT_TYPES = exports.DebuggerInputType = exports.DEBUGGER_INPUT_IDENTIFIER = void 0;
4
+ exports.validateDebuggerInputRequest = validateDebuggerInputRequest;
5
+ exports.unwrapDebuggerInputResponse = unwrapDebuggerInputResponse;
6
+ exports.sendDebuggerInput = sendDebuggerInput;
7
+ exports.DEBUGGER_INPUT_IDENTIFIER = 'ValdiDebuggerInput';
8
+ const DEBUGGER_INPUT_TIMEOUT_MS = 5000;
9
+ const DEBUGGER_INPUT_NAMED_KEYS = new Set(['Enter', 'Return', 'Escape', 'Backspace', 'Delete']);
10
+ var DebuggerInputType;
11
+ (function (DebuggerInputType) {
12
+ DebuggerInputType["Capabilities"] = "capabilities";
13
+ DebuggerInputType["Query"] = "query";
14
+ DebuggerInputType["Tap"] = "tap";
15
+ DebuggerInputType["Focus"] = "focus";
16
+ DebuggerInputType["Text"] = "text";
17
+ DebuggerInputType["Key"] = "key";
18
+ DebuggerInputType["Scroll"] = "scroll";
19
+ })(DebuggerInputType || (exports.DebuggerInputType = DebuggerInputType = {}));
20
+ const DEBUGGER_INPUT_RETURN_KEY_ACTIONS = new Set(['onReturn', 'onChange', 'focused', 'return']);
21
+ const DEBUGGER_INPUT_ESCAPE_KEY_ACTIONS = new Set(['focused']);
22
+ const DEBUGGER_INPUT_EDIT_KEY_ACTIONS = new Set(['onChange']);
23
+ const DEBUGGER_INPUT_TAP_ACTIONS = new Set(['onTap']);
24
+ const DEBUGGER_INPUT_FOCUS_ACTIONS = new Set(['focused']);
25
+ const DEBUGGER_INPUT_TEXT_ACTIONS = new Set(['onChange']);
26
+ const DEBUGGER_INPUT_SCROLL_ACTIONS = new Set(['contentOffset']);
27
+ exports.SUPPORTED_DEBUGGER_INPUT_TYPES = new Set(Object.values(DebuggerInputType));
28
+ const DEBUGGER_INPUT_SELECTOR_FIELDS = new Set(['elementId', 'accessibilityId', 'tag']);
29
+ const DEBUGGER_INPUT_COMMON_FIELDS = [
30
+ 'type',
31
+ 'contextId',
32
+ 'elementId',
33
+ 'accessibilityId',
34
+ 'selector',
35
+ ];
36
+ const DEBUGGER_INPUT_FIELDS_BY_TYPE = new Map([
37
+ [DebuggerInputType.Capabilities, new Set(['type', 'contextId'])],
38
+ [DebuggerInputType.Query, new Set(DEBUGGER_INPUT_COMMON_FIELDS)],
39
+ [DebuggerInputType.Tap, new Set([...DEBUGGER_INPUT_COMMON_FIELDS, 'x', 'y'])],
40
+ [DebuggerInputType.Focus, new Set([...DEBUGGER_INPUT_COMMON_FIELDS, 'focused'])],
41
+ [
42
+ DebuggerInputType.Text,
43
+ new Set([...DEBUGGER_INPUT_COMMON_FIELDS, 'text', 'value', 'selectionStart', 'selectionEnd']),
44
+ ],
45
+ [DebuggerInputType.Key, new Set([...DEBUGGER_INPUT_COMMON_FIELDS, 'key', 'selectionStart', 'selectionEnd'])],
46
+ [DebuggerInputType.Scroll, new Set([...DEBUGGER_INPUT_COMMON_FIELDS, 'x', 'y', 'deltaX', 'deltaY'])],
47
+ ]);
48
+ function isRecord(value) {
49
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
50
+ }
51
+ function containsLoneSurrogate(value) {
52
+ for (let index = 0; index < value.length; index += 1) {
53
+ const codeUnit = value.slice(index, index + 1).codePointAt(0) ?? 0;
54
+ if (codeUnit >= 0xd8_00 && codeUnit <= 0xdb_ff) {
55
+ if (index + 1 >= value.length)
56
+ return true;
57
+ const nextCodeUnit = value.slice(index + 1, index + 2).codePointAt(0) ?? 0;
58
+ if (nextCodeUnit < 0xdc_00 || nextCodeUnit > 0xdf_ff)
59
+ return true;
60
+ index += 1;
61
+ }
62
+ else if (codeUnit >= 0xdc_00 && codeUnit <= 0xdf_ff) {
63
+ return true;
64
+ }
65
+ }
66
+ return false;
67
+ }
68
+ function isCombiningCodePoint(codePoint) {
69
+ return ((codePoint >= 0x03_00 && codePoint <= 0x03_6f) ||
70
+ (codePoint >= 0x04_83 && codePoint <= 0x04_89) ||
71
+ (codePoint >= 0x05_91 && codePoint <= 0x05_bd) ||
72
+ codePoint === 0x05_bf ||
73
+ (codePoint >= 0x05_c1 && codePoint <= 0x05_c2) ||
74
+ (codePoint >= 0x06_10 && codePoint <= 0x06_1a) ||
75
+ (codePoint >= 0x06_4b && codePoint <= 0x06_5f) ||
76
+ codePoint === 0x06_70 ||
77
+ (codePoint >= 0x06_d6 && codePoint <= 0x06_ed) ||
78
+ (codePoint >= 0x1a_b0 && codePoint <= 0x1a_ff) ||
79
+ (codePoint >= 0x1d_c0 && codePoint <= 0x1d_ff) ||
80
+ (codePoint >= 0x20_d0 && codePoint <= 0x20_ff) ||
81
+ (codePoint >= 0xfe_20 && codePoint <= 0xfe_2f));
82
+ }
83
+ function isGraphemeExtension(codePoint) {
84
+ return (isCombiningCodePoint(codePoint) ||
85
+ (codePoint >= 0xfe_00 && codePoint <= 0xfe_0f) ||
86
+ (codePoint >= 0xe_01_00 && codePoint <= 0xe_01_ef) ||
87
+ (codePoint >= 0x1_f3_fb && codePoint <= 0x1_f3_ff) ||
88
+ codePoint === 0x20_e3);
89
+ }
90
+ function isRegionalIndicator(codePoint) {
91
+ return codePoint >= 0x1_f1_e6 && codePoint <= 0x1_f1_ff;
92
+ }
93
+ function fallbackGraphemeCount(value) {
94
+ if (value.length === 0)
95
+ return 0;
96
+ let count = 1;
97
+ let previousCodePoint = value.codePointAt(0) ?? 0;
98
+ let currentIndex = previousCodePoint > 0xff_ff ? 2 : 1;
99
+ let regionalIndicatorCount = isRegionalIndicator(previousCodePoint) ? 1 : 0;
100
+ while (currentIndex < value.length) {
101
+ const currentCodePoint = value.codePointAt(currentIndex) ?? 0;
102
+ const joinsPrevious = (previousCodePoint === 0x00_0d && currentCodePoint === 0x00_0a) ||
103
+ isGraphemeExtension(currentCodePoint) ||
104
+ previousCodePoint === 0x20_0d ||
105
+ currentCodePoint === 0x20_0d ||
106
+ (isRegionalIndicator(previousCodePoint) &&
107
+ isRegionalIndicator(currentCodePoint) &&
108
+ regionalIndicatorCount % 2 === 1);
109
+ if (!joinsPrevious) {
110
+ count += 1;
111
+ if (count > 1)
112
+ return count;
113
+ }
114
+ if (isRegionalIndicator(currentCodePoint)) {
115
+ regionalIndicatorCount = isRegionalIndicator(previousCodePoint) ? regionalIndicatorCount + 1 : 1;
116
+ }
117
+ else if (!isGraphemeExtension(currentCodePoint)) {
118
+ regionalIndicatorCount = 0;
119
+ }
120
+ previousCodePoint = currentCodePoint;
121
+ currentIndex += currentCodePoint > 0xff_ff ? 2 : 1;
122
+ }
123
+ return count;
124
+ }
125
+ function isSingleGrapheme(value) {
126
+ const segmenterConstructor = typeof Intl === 'undefined' ? undefined : Intl.Segmenter;
127
+ if (segmenterConstructor) {
128
+ try {
129
+ const segmenter = new segmenterConstructor(undefined, { granularity: 'grapheme' });
130
+ const iterator = segmenter.segment(value)[Symbol.iterator]();
131
+ return !iterator.next().done && Boolean(iterator.next().done);
132
+ }
133
+ catch {
134
+ }
135
+ }
136
+ return fallbackGraphemeCount(value) === 1;
137
+ }
138
+ function isSupportedDebuggerInputKey(value) {
139
+ if (DEBUGGER_INPUT_NAMED_KEYS.has(value))
140
+ return true;
141
+ if (value.length === 0 || containsLoneSurrogate(value) || !isSingleGrapheme(value))
142
+ return false;
143
+ let index = 0;
144
+ while (index < value.length) {
145
+ const codePoint = value.codePointAt(index) ?? 0;
146
+ if (codePoint < 0x20 || (codePoint >= 0x7f && codePoint <= 0x9f))
147
+ return false;
148
+ index += codePoint > 0xff_ff ? 2 : 1;
149
+ }
150
+ return true;
151
+ }
152
+ function validateOptionalString(request, fieldName, requireNonEmpty) {
153
+ const value = request[fieldName];
154
+ if (value === undefined)
155
+ return undefined;
156
+ if (typeof value !== 'string')
157
+ return `${fieldName} must be a string.`;
158
+ if (requireNonEmpty && value.length === 0)
159
+ return `${fieldName} must not be empty.`;
160
+ if (containsLoneSurrogate(value))
161
+ return `${fieldName} must contain valid Unicode.`;
162
+ return undefined;
163
+ }
164
+ function validateOptionalNumber(request, fieldName, requireInteger) {
165
+ const value = request[fieldName];
166
+ if (value === undefined)
167
+ return undefined;
168
+ if (typeof value !== 'number' || !Number.isFinite(value) || (requireInteger && !Number.isInteger(value))) {
169
+ return `${fieldName} must be a finite ${requireInteger ? 'integer' : 'number'}.`;
170
+ }
171
+ return undefined;
172
+ }
173
+ function responseValidationError(message) {
174
+ throw new TypeError(`Invalid debugger input response: ${message}`);
175
+ }
176
+ function validateOptionalResponseString(value, fieldName, requireNonEmpty) {
177
+ const fieldValue = value[fieldName];
178
+ if (fieldValue === undefined)
179
+ return;
180
+ if (typeof fieldValue !== 'string')
181
+ responseValidationError(`${fieldName} must be a string.`);
182
+ if (requireNonEmpty && fieldValue.length === 0)
183
+ responseValidationError(`${fieldName} must not be empty.`);
184
+ if (containsLoneSurrogate(fieldValue))
185
+ responseValidationError(`${fieldName} must contain valid Unicode.`);
186
+ }
187
+ function validateOptionalResponseNumber(value, fieldName, requireInteger) {
188
+ const fieldValue = value[fieldName];
189
+ if (fieldValue === undefined)
190
+ return;
191
+ if (typeof fieldValue !== 'number' ||
192
+ !Number.isFinite(fieldValue) ||
193
+ (requireInteger && !Number.isInteger(fieldValue))) {
194
+ responseValidationError(`${fieldName} must be a finite ${requireInteger ? 'integer' : 'number'}.`);
195
+ }
196
+ }
197
+ function requireResponseString(value, fieldName) {
198
+ validateOptionalResponseString(value, fieldName, false);
199
+ const fieldValue = value[fieldName];
200
+ if (typeof fieldValue !== 'string')
201
+ responseValidationError(`${fieldName} must be a string.`);
202
+ return fieldValue;
203
+ }
204
+ function requireResponseNumber(value, fieldName, requireInteger) {
205
+ validateOptionalResponseNumber(value, fieldName, requireInteger);
206
+ const fieldValue = value[fieldName];
207
+ if (typeof fieldValue !== 'number') {
208
+ responseValidationError(`${fieldName} must be a finite ${requireInteger ? 'integer' : 'number'}.`);
209
+ }
210
+ return fieldValue;
211
+ }
212
+ function requireResponseAction(data, allowedActions) {
213
+ const action = data['action'];
214
+ if (typeof action !== 'string' || !allowedActions.has(action)) {
215
+ responseValidationError(`action must be one of: ${Array.from(allowedActions).join(', ')}.`);
216
+ }
217
+ }
218
+ function validateStringArray(value, fieldName) {
219
+ if (!Array.isArray(value) || value.some(item => typeof item !== 'string')) {
220
+ responseValidationError(`${fieldName} must be an array of strings.`);
221
+ }
222
+ }
223
+ function validateElementFrame(value, fieldName) {
224
+ if (!isRecord(value))
225
+ responseValidationError(`${fieldName} must be an object.`);
226
+ for (const coordinate of ['x', 'y', 'width', 'height']) {
227
+ const coordinateValue = value[coordinate];
228
+ if (typeof coordinateValue !== 'number' || !Number.isFinite(coordinateValue)) {
229
+ responseValidationError(`${fieldName}.${coordinate} must be a finite number.`);
230
+ }
231
+ }
232
+ }
233
+ function validateElementDescriptor(value, index) {
234
+ const fieldName = `elements[${index}]`;
235
+ if (!isRecord(value))
236
+ responseValidationError(`${fieldName} must be an object.`);
237
+ if (typeof value['elementId'] !== 'number' || !Number.isInteger(value['elementId'])) {
238
+ responseValidationError(`${fieldName}.elementId must be a finite integer.`);
239
+ }
240
+ validateOptionalResponseNumber(value, 'parentElementId', true);
241
+ if (typeof value['tag'] !== 'string' || value['tag'].length === 0) {
242
+ responseValidationError(`${fieldName}.tag must be a non-empty string.`);
243
+ }
244
+ for (const stringField of [
245
+ 'accessibilityId',
246
+ 'accessibilityCategory',
247
+ 'accessibilityNavigation',
248
+ 'accessibilityLabel',
249
+ 'accessibilityHint',
250
+ 'accessibilityValue',
251
+ ]) {
252
+ validateOptionalResponseString(value, stringField, false);
253
+ }
254
+ for (const booleanField of ['selected', 'enabled', 'focused']) {
255
+ if (typeof value[booleanField] !== 'boolean') {
256
+ responseValidationError(`${fieldName}.${booleanField} must be a boolean.`);
257
+ }
258
+ }
259
+ validateElementFrame(value['frame'], `${fieldName}.frame`);
260
+ validateElementFrame(value['absoluteFrame'], `${fieldName}.absoluteFrame`);
261
+ validateStringArray(value['actions'], `${fieldName}.actions`);
262
+ }
263
+ function validateDebuggerInputResult(data, request) {
264
+ const requestType = request['type'];
265
+ validateOptionalResponseString(data, 'contextId', true);
266
+ validateOptionalResponseString(data, 'accessibilityId', false);
267
+ validateOptionalResponseString(data, 'action', true);
268
+ validateOptionalResponseString(data, 'message', false);
269
+ validateOptionalResponseString(data, 'value', false);
270
+ for (const fieldName of ['elementId', 'actionElementId', 'selectionStart', 'selectionEnd']) {
271
+ validateOptionalResponseNumber(data, fieldName, true);
272
+ }
273
+ for (const fieldName of ['contentOffsetX', 'contentOffsetY']) {
274
+ validateOptionalResponseNumber(data, fieldName, false);
275
+ }
276
+ if (data['elements'] !== undefined) {
277
+ if (!Array.isArray(data['elements']))
278
+ responseValidationError('elements must be an array.');
279
+ data['elements'].forEach((element, index) => validateElementDescriptor(element, index));
280
+ }
281
+ if (data['supportedTypes'] !== undefined)
282
+ validateStringArray(data['supportedTypes'], 'supportedTypes');
283
+ if (data['selectorForms'] !== undefined)
284
+ validateStringArray(data['selectorForms'], 'selectorForms');
285
+ if (!data['handled'])
286
+ return;
287
+ if (requestType === DebuggerInputType.Query) {
288
+ if (data['action'] !== DebuggerInputType.Query)
289
+ responseValidationError('query action must be query.');
290
+ if (!Array.isArray(data['elements']))
291
+ responseValidationError('query elements must be an array.');
292
+ }
293
+ if (requestType === DebuggerInputType.Capabilities) {
294
+ if (data['action'] !== DebuggerInputType.Capabilities) {
295
+ responseValidationError('capabilities action must be capabilities.');
296
+ }
297
+ validateStringArray(data['supportedTypes'], 'supportedTypes');
298
+ validateStringArray(data['selectorForms'], 'selectorForms');
299
+ return;
300
+ }
301
+ if (requestType !== DebuggerInputType.Query) {
302
+ requireResponseNumber(data, 'elementId', true);
303
+ requireResponseNumber(data, 'actionElementId', true);
304
+ }
305
+ switch (requestType) {
306
+ case DebuggerInputType.Tap: {
307
+ requireResponseAction(data, DEBUGGER_INPUT_TAP_ACTIONS);
308
+ break;
309
+ }
310
+ case DebuggerInputType.Focus: {
311
+ requireResponseAction(data, DEBUGGER_INPUT_FOCUS_ACTIONS);
312
+ break;
313
+ }
314
+ case DebuggerInputType.Text: {
315
+ requireResponseAction(data, DEBUGGER_INPUT_TEXT_ACTIONS);
316
+ requireResponseString(data, 'value');
317
+ requireResponseNumber(data, 'selectionStart', true);
318
+ requireResponseNumber(data, 'selectionEnd', true);
319
+ break;
320
+ }
321
+ case DebuggerInputType.Key: {
322
+ const key = request['key'];
323
+ if (typeof key !== 'string')
324
+ responseValidationError('key request must contain a string key.');
325
+ const allowedActions = key === 'Enter' || key === 'Return'
326
+ ? DEBUGGER_INPUT_RETURN_KEY_ACTIONS
327
+ : key === 'Escape'
328
+ ? DEBUGGER_INPUT_ESCAPE_KEY_ACTIONS
329
+ : DEBUGGER_INPUT_EDIT_KEY_ACTIONS;
330
+ requireResponseAction(data, allowedActions);
331
+ requireResponseString(data, 'value');
332
+ if (key !== 'Escape') {
333
+ requireResponseNumber(data, 'selectionStart', true);
334
+ requireResponseNumber(data, 'selectionEnd', true);
335
+ }
336
+ break;
337
+ }
338
+ case DebuggerInputType.Scroll: {
339
+ requireResponseAction(data, DEBUGGER_INPUT_SCROLL_ACTIONS);
340
+ requireResponseNumber(data, 'contentOffsetX', false);
341
+ requireResponseNumber(data, 'contentOffsetY', false);
342
+ break;
343
+ }
344
+ default: {
345
+ break;
346
+ }
347
+ }
348
+ }
349
+ function validateSelector(request) {
350
+ const selectorCount = [request['elementId'], request['accessibilityId'], request['selector']].filter(value => value !== undefined).length;
351
+ if (selectorCount > 1)
352
+ return 'Use only one of elementId, accessibilityId, or selector.';
353
+ const elementIdError = validateOptionalNumber(request, 'elementId', true);
354
+ if (elementIdError)
355
+ return elementIdError;
356
+ const accessibilityIdError = validateOptionalString(request, 'accessibilityId', true);
357
+ if (accessibilityIdError)
358
+ return accessibilityIdError;
359
+ const selector = request['selector'];
360
+ if (selector === undefined)
361
+ return undefined;
362
+ if (typeof selector === 'string') {
363
+ return validateOptionalString(request, 'selector', true);
364
+ }
365
+ if (!isRecord(selector))
366
+ return 'selector must be a string or an object.';
367
+ const unknownField = Object.keys(selector)
368
+ .sort()
369
+ .find(fieldName => !DEBUGGER_INPUT_SELECTOR_FIELDS.has(fieldName));
370
+ if (unknownField)
371
+ return `Unsupported selector field '${unknownField}'.`;
372
+ if (Object.keys(selector).length === 0)
373
+ return 'selector object must include elementId, accessibilityId, or tag.';
374
+ return (validateOptionalNumber(selector, 'elementId', true) ??
375
+ validateOptionalString(selector, 'accessibilityId', true) ??
376
+ validateOptionalString(selector, 'tag', true));
377
+ }
378
+ function validateDebuggerInputRequest(request) {
379
+ if (!isRecord(request))
380
+ return 'Debugger input request must be an object.';
381
+ const type = request['type'];
382
+ if (typeof type !== 'string' || !exports.SUPPORTED_DEBUGGER_INPUT_TYPES.has(type)) {
383
+ return `Unsupported input type ${String(type)}.`;
384
+ }
385
+ const inputType = type;
386
+ const supportedFields = DEBUGGER_INPUT_FIELDS_BY_TYPE.get(inputType);
387
+ if (!supportedFields)
388
+ return `Unsupported input type ${type}.`;
389
+ const unknownField = Object.keys(request)
390
+ .sort()
391
+ .find(fieldName => !supportedFields.has(fieldName));
392
+ if (unknownField)
393
+ return `Field '${unknownField}' is not supported for ${type} input.`;
394
+ const contextError = validateOptionalString(request, 'contextId', true);
395
+ if (contextError)
396
+ return contextError;
397
+ const selectorError = validateSelector(request);
398
+ if (selectorError)
399
+ return selectorError;
400
+ const hasSelector = request['elementId'] !== undefined || request['accessibilityId'] !== undefined || request['selector'] !== undefined;
401
+ if (inputType !== DebuggerInputType.Capabilities && inputType !== DebuggerInputType.Query && !hasSelector) {
402
+ return 'An elementId, accessibilityId, or selector is required.';
403
+ }
404
+ if (request['focused'] !== undefined && typeof request['focused'] !== 'boolean') {
405
+ return 'focused must be a boolean.';
406
+ }
407
+ for (const fieldName of ['text', 'value', 'key']) {
408
+ const error = validateOptionalString(request, fieldName, fieldName === 'key');
409
+ if (error)
410
+ return error;
411
+ }
412
+ for (const fieldName of ['selectionStart', 'selectionEnd']) {
413
+ const error = validateOptionalNumber(request, fieldName, true);
414
+ if (error)
415
+ return error;
416
+ }
417
+ for (const fieldName of ['x', 'y', 'deltaX', 'deltaY']) {
418
+ const error = validateOptionalNumber(request, fieldName, false);
419
+ if (error)
420
+ return error;
421
+ }
422
+ if (inputType === DebuggerInputType.Text) {
423
+ if (request['text'] === undefined && request['value'] === undefined)
424
+ return 'Text input requires a string text or value.';
425
+ if (request['text'] !== undefined && request['value'] !== undefined)
426
+ return 'Use only one of text or value.';
427
+ }
428
+ if (inputType === DebuggerInputType.Key) {
429
+ if (request['key'] === undefined)
430
+ return 'Key input requires a string key.';
431
+ if (!isSupportedDebuggerInputKey(request['key'])) {
432
+ return 'key must be Enter, Return, Escape, Backspace, Delete, or one printable grapheme.';
433
+ }
434
+ }
435
+ return undefined;
436
+ }
437
+ function unwrapDebuggerInputResponse(customBody, request) {
438
+ if (!isRecord(customBody) || typeof customBody['handled'] !== 'boolean') {
439
+ throw new TypeError('Invalid debugger input response: handled must be a boolean.');
440
+ }
441
+ if (customBody['handled']) {
442
+ const data = customBody['data'];
443
+ if (!isRecord(data)) {
444
+ throw new TypeError('Invalid debugger input response: data must be an object.');
445
+ }
446
+ if (typeof data['handled'] !== 'boolean') {
447
+ throw new TypeError('Invalid debugger input response: data.handled must be a boolean.');
448
+ }
449
+ const contractVersion = data['contractVersion'];
450
+ if (typeof contractVersion !== 'number' || !Number.isInteger(contractVersion) || contractVersion < 1) {
451
+ throw new TypeError('Invalid debugger input response: data.contractVersion must be a positive integer.');
452
+ }
453
+ if (typeof data['type'] !== 'string' || data['type'] !== request['type']) {
454
+ throw new TypeError('Invalid debugger input response: data.type must match the request type.');
455
+ }
456
+ validateDebuggerInputResult(data, request);
457
+ return data;
458
+ }
459
+ return {
460
+ handled: false,
461
+ type: request['type'],
462
+ elementId: request['elementId'],
463
+ message: 'The target app did not register the Valdi debugger input handler.',
464
+ };
465
+ }
466
+ async function sendDebuggerInput(conn, clientId, request) {
467
+ const validationError = validateDebuggerInputRequest(request);
468
+ if (validationError) {
469
+ throw new Error(validationError);
470
+ }
471
+ const customBody = await conn.customRequest(clientId, exports.DEBUGGER_INPUT_IDENTIFIER, request, DEBUGGER_INPUT_TIMEOUT_MS);
472
+ return unwrapDebuggerInputResponse(customBody, request);
473
+ }