-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathrequest-lock-policy.ts
More file actions
132 lines (120 loc) · 3.99 KB
/
request-lock-policy.ts
File metadata and controls
132 lines (120 loc) · 3.99 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
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 allowsSelectorOverride = SELECTOR_OVERRIDE_LOCK_POLICY_COMMANDS.has(req.command);
const conflicts = allowsSelectorOverride
? []
: existingSession
? listSessionSelectorConflicts(existingSession, nextFlags)
: listFreshSessionConflicts(nextFlags, req.meta?.lockPlatform, req.command);
const lockPlatform = req.meta?.lockPlatform;
const shouldApplyLockPlatformDefault =
!existingSession &&
nextFlags.platform === undefined &&
(!allowsSelectorOverride ||
(nextFlags.serial === undefined && nextFlags.androidDeviceAllowlist === undefined));
if (conflicts.length === 0) {
if (lockPlatform && shouldApplyLockPlatformDefault) {
nextFlags.platform = lockPlatform;
}
return {
...req,
flags: nextFlags,
};
}
if (lockPolicy === 'strip') {
if (existingSession) {
stripSessionConflicts(nextFlags, conflicts);
nextFlags.platform = existingSession.device.platform;
} else {
stripFreshSessionConflicts(nextFlags, req.meta?.lockPlatform);
}
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 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 (typeof value === 'string' && value.trim().length > 0) {
conflicts.push({ key: key as SessionSelectorConflictKey, value });
}
}
return conflicts;
}
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];
}
}