Repository navigation
Expand file tree
/
Copy pathrecord.ts
More file actions
205 lines (180 loc) · 7.32 KB
/
Copy pathrecord.ts
File metadata and controls
205 lines (180 loc) · 7.32 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
import { AppError } from '@agent-device/kernel/errors';
import type { DeviceKind, DeviceTarget, PublicPlatform } from '@agent-device/kernel/device';
import { isPositiveFiniteRect } from '@agent-device/kernel/rect';
import type { Point, Rect, SnapshotKeyboardBandFact } from '@agent-device/kernel/snapshot';
function readRequired<T>(
record: Record<string, unknown>,
key: string,
parse: (value: unknown) => T | undefined,
message: string,
): T {
const value = parse(record[key]);
if (value === undefined) {
throw new AppError('COMMAND_FAILED', message, { response: record });
}
return value;
}
function readOptional<T>(
record: Record<string, unknown>,
key: string,
parse: (value: unknown) => T | undefined,
): T | undefined {
return parse(record[key]);
}
function readNullable<T>(
record: Record<string, unknown>,
key: string,
parse: (value: unknown) => T | undefined,
): T | null | undefined {
const value = record[key];
return value === null ? null : parse(value);
}
export function readRequiredString(record: Record<string, unknown>, key: string): string {
return readRequired(record, key, parseNonEmptyString, `Daemon response is missing "${key}".`);
}
export function readOptionalString(
record: Record<string, unknown>,
key: string,
): string | undefined {
return readOptional(record, key, parseNonEmptyString);
}
export function readNullableString(
record: Record<string, unknown>,
key: string,
): string | null | undefined {
return readNullable(record, key, parseNonEmptyString);
}
export function readRequiredNumber(record: Record<string, unknown>, key: string): number {
return readRequired(
record,
key,
parseFiniteNumber,
`Daemon response is missing numeric "${key}".`,
);
}
export function readRequiredPlatform(record: Record<string, unknown>, key: string): PublicPlatform {
return readRequired(record, key, parsePlatform, `Daemon response has invalid "${key}".`);
}
export function readRequiredDeviceKind(record: Record<string, unknown>, key: string): DeviceKind {
return readRequired(record, key, parseDeviceKind, `Daemon response has invalid "${key}".`);
}
export function readDeviceTarget(record: Record<string, unknown>, key: string): DeviceTarget {
return readOptional(record, key, parseDeviceTarget) ?? 'mobile';
}
export function parseRect(value: unknown): Rect | undefined {
if (!isRecord(value)) return undefined;
const x = readNumberField(value, 'x');
const y = readNumberField(value, 'y');
const width = readNumberField(value, 'width');
const height = readNumberField(value, 'height');
if (x === undefined || y === undefined || width === undefined || height === undefined) {
return undefined;
}
return { x, y, width, height };
}
/**
* Reads a producer-declared keyboard band (#2660) out of an undtyped payload, shared by every seam
* that receives one: the Apple runner's wire reader, the daemon's Node client reader. `undefined`
* means the producer published no fact at all, which is how a tier that never reads the keyboard is
* distinguished from one that measured and reported.
*
* A payload that cannot be placed is restated as `unmeasurable` naming where it failed, never dropped
* and never trusted: silence would tell the tap guard the producer never looked. The reasons resolve
* in a fixed order, least to most trusted: a value that is not an object is `malformed-fact`; an
* unrecognized `kind` is `unrecognized-kind`; an `unmeasurable` that never named its failure is
* `unreported-reason`; a `visible` whose frame is not a positive finite rect is
* `invalid-visible-frame`.
*/
export function readSnapshotKeyboardBandFact(value: unknown): SnapshotKeyboardBandFact | undefined {
if (value === undefined) return undefined;
if (!isRecord(value)) return { kind: 'unmeasurable', reason: 'malformed-fact' };
if (value.kind === 'absent') return { kind: 'absent' };
if (value.kind === 'unmeasurable') {
return typeof value.reason === 'string' && value.reason.trim().length > 0
? { kind: 'unmeasurable', reason: value.reason }
: { kind: 'unmeasurable', reason: 'unreported-reason' };
}
if (value.kind === 'visible') {
const frame = parseRect(value.frame);
return isPositiveFiniteRect(frame)
? { kind: 'visible', frame }
: { kind: 'unmeasurable', reason: 'invalid-visible-frame' };
}
return { kind: 'unmeasurable', reason: 'unrecognized-kind' };
}
export function parsePoint(value: unknown): Point | undefined {
if (!isRecord(value)) return undefined;
const x = readNumberField(value, 'x');
const y = readNumberField(value, 'y');
if (x === undefined || y === undefined) {
return undefined;
}
return { x, y };
}
function readNumberField(record: Record<string, unknown>, key: string): number | undefined {
const value = record[key];
return typeof value === 'number' ? value : undefined;
}
function parseNonEmptyString(value: unknown): string | undefined {
return typeof value === 'string' && value.length > 0 ? value : undefined;
}
function parseFiniteNumber(value: unknown): number | undefined {
return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
}
// Client-side parser for the PUBLIC leaf platform a daemon response carries. Under
// approach (b) the daemon always emits leaf strings (`ios`/`macos`), never the
// internal `apple`; the extra `apple` acceptance is forward-compat only, and — being
// unable to disambiguate the Apple OS from the bare token — maps to the dominant
// `ios` leaf (unreachable today, since output is never `apple`).
function parsePlatform(value: unknown): PublicPlatform | undefined {
if (value === 'apple') return 'ios';
return value === 'ios' ||
value === 'macos' ||
value === 'android' ||
value === 'harmonyos' ||
value === 'vega' ||
value === 'linux' ||
value === 'web'
? value
: undefined;
}
function parseDeviceKind(value: unknown): DeviceKind | undefined {
return value === 'simulator' || value === 'emulator' || value === 'device' ? value : undefined;
}
function parseDeviceTarget(value: unknown): DeviceTarget | undefined {
return value === 'tv' || value === 'mobile' || value === 'desktop' ? value : undefined;
}
export function asRecord(value: unknown): Record<string, unknown> {
if (!isRecord(value)) {
throw new AppError('COMMAND_FAILED', 'Daemon returned an unexpected response shape.', {
value,
});
}
return value;
}
export function isRecord(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === 'object' && !Array.isArray(value);
}
export function asOptionalRecord(value: unknown): Record<string, unknown> | undefined {
return isRecord(value) ? value : undefined;
}
type WithoutUndefined<T extends Record<string, unknown>> = {
[K in keyof T as undefined extends T[K] ? never : K]: T[K];
} & {
[K in keyof T as undefined extends T[K] ? K : never]?: Exclude<T[K], undefined>;
};
export function stripUndefined<T extends Record<string, unknown>>(value: T): WithoutUndefined<T> {
const output = {} as WithoutUndefined<T>;
for (const [key, current] of Object.entries(value)) {
if (current !== undefined) {
(output as Record<string, unknown>)[key] = current;
}
}
return output;
}
export function splitNonEmptyTrimmedLines(text: string): string[] {
return text
.split('\n')
.map((line) => line.trim())
.filter((line) => line.length > 0);
}