-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathrequest-lock-policy.ts
More file actions
157 lines (142 loc) · 4.53 KB
/
request-lock-policy.ts
File metadata and controls
157 lines (142 loc) · 4.53 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
import { AppError } from '../utils/errors.ts';
import type { CommandFlags } from '../core/dispatch.ts';
import type { SessionState, DaemonRequest } from './types.ts';
import { PUBLIC_COMMANDS } from '../command-catalog.ts';
import {
formatSessionSelectorConflict,
listSessionSelectorConflicts,
type SessionSelectorConflict,
type SessionSelectorConflictKey,
} from './session-selector.ts';
import { isApplePlatform, normalizePlatformSelector } from '../utils/device.ts';
type LockPlatform = NonNullable<DaemonRequest['meta']>['lockPlatform'];
const LOCKABLE_SELECTOR_KEYS: Array<keyof CommandFlags> = [
'target',
'device',
'udid',
'serial',
'iosSimulatorDeviceSet',
'androidDeviceAllowlist',
];
const SELECTOR_OVERRIDE_LOCK_POLICY_COMMANDS: ReadonlySet<string> = new Set([
PUBLIC_COMMANDS.apps,
PUBLIC_COMMANDS.devices,
]);
export function applyRequestLockPolicy(
req: DaemonRequest,
existingSession?: SessionState,
): DaemonRequest {
const lockPolicy = req.meta?.lockPolicy;
if (!lockPolicy) {
return req;
}
const nextFlags: CommandFlags = { ...(req.flags ?? {}) };
const canOverrideSelector = SELECTOR_OVERRIDE_LOCK_POLICY_COMMANDS.has(req.command);
const conflicts = canOverrideSelector
? []
: existingSession
? listSessionSelectorConflicts(existingSession, nextFlags)
: listFreshSessionConflicts(nextFlags, req.meta?.lockPlatform, req.command);
const lockPlatform = req.meta?.lockPlatform;
if (conflicts.length === 0) {
if (
shouldApplyLockPlatformDefault(canOverrideSelector, existingSession, nextFlags, lockPlatform)
) {
nextFlags.platform = lockPlatform;
}
return {
...req,
flags: nextFlags,
};
}
if (lockPolicy === 'strip') {
applyStripLockPolicy(nextFlags, conflicts, lockPlatform, existingSession);
return {
...req,
flags: nextFlags,
};
}
throw new AppError(
'INVALID_ARGS',
`${req.command} cannot override session lock policy with ${conflicts.map(formatSessionSelectorConflict).join(', ')}. ` +
'Unset those selectors or remove the request lock policy.',
);
}
function shouldApplyLockPlatformDefault(
canOverrideSelector: boolean,
existingSession: SessionState | undefined,
flags: CommandFlags,
lockPlatform: LockPlatform,
): boolean {
if (!lockPlatform || existingSession || flags.platform !== undefined) {
return false;
}
if (!canOverrideSelector) {
return true;
}
return !LOCKABLE_SELECTOR_KEYS.some((key) => hasSelectorValue(flags[key]));
}
function applyStripLockPolicy(
flags: CommandFlags,
conflicts: SessionSelectorConflict[],
lockPlatform: LockPlatform,
existingSession: SessionState | undefined,
): void {
if (existingSession) {
stripSessionConflicts(flags, conflicts);
flags.platform = existingSession.device.platform;
return;
}
stripFreshSessionConflicts(flags, lockPlatform);
}
function listFreshSessionConflicts(
flags: CommandFlags,
lockPlatform: LockPlatform,
command: DaemonRequest['command'],
): SessionSelectorConflict[] {
const conflicts: SessionSelectorConflict[] = [];
const normalizedLockPlatform = normalizePlatformSelector(lockPlatform);
if (
flags.platform !== undefined &&
normalizedLockPlatform &&
platformSelectorsConflict(normalizePlatformSelector(flags.platform), normalizedLockPlatform)
) {
conflicts.push({ key: 'platform', value: flags.platform });
}
if (command === 'open') {
return conflicts;
}
for (const key of LOCKABLE_SELECTOR_KEYS) {
const value = flags[key];
if (hasSelectorValue(value)) {
conflicts.push({ key: key as SessionSelectorConflictKey, value });
}
}
return conflicts;
}
function hasSelectorValue(value: unknown): value is string {
return typeof value === 'string' && value.trim().length > 0;
}
function platformSelectorsConflict(
requested: ReturnType<typeof normalizePlatformSelector>,
locked: ReturnType<typeof normalizePlatformSelector>,
): boolean {
if (!requested || !locked) return false;
if (requested === locked) return false;
if (requested === 'apple') return !isApplePlatform(locked);
if (locked === 'apple') return !isApplePlatform(requested);
return true;
}
function stripFreshSessionConflicts(flags: CommandFlags, lockPlatform: LockPlatform): void {
for (const key of LOCKABLE_SELECTOR_KEYS) {
delete flags[key];
}
if (lockPlatform) {
flags.platform = lockPlatform;
}
}
function stripSessionConflicts(flags: CommandFlags, conflicts: SessionSelectorConflict[]): void {
for (const conflict of conflicts) {
delete flags[conflict.key];
}
}