Repository navigation
Expand file tree
/
Copy pathios.ts
More file actions
318 lines (297 loc) · 10 KB
/
Copy pathios.ts
File metadata and controls
318 lines (297 loc) · 10 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import type { LimrunSyncCache } from './sync-cache.ts';
import type { SettingOptions } from '@agent-device/contracts/settings';
import type {
DeviceLease,
ProviderDeviceInstallOptions,
ProviderDeviceInstallResult,
} from '@agent-device/contracts/device';
import type { Interactor, RunnerContext } from '@agent-device/contracts/interactor-types';
import type { DeviceInfo } from '@agent-device/kernel/device';
import { AppError } from '@agent-device/kernel/errors';
import {
createInstanceClient as createIosInstanceClient,
type InstanceClient as LimrunIosClient,
} from '@limrun/api/ios-client';
import fs from 'node:fs';
import path from 'node:path';
import { setTimeout as sleep } from 'node:timers/promises';
import { normalizeOptionalString } from './strings.ts';
import {
awaitLimrunDeploymentOperation,
type LimrunRequestOperationDrain,
} from './request-cancellation.ts';
import type { LimrunInstanceOwnership } from './instance-access.ts';
import type { LimrunRuntimeDependencies, LimrunIosRunner } from './runtime-dependencies.ts';
export type LimrunIosSession = {
platform: 'ios';
lease: DeviceLease;
instanceId: string;
readonly apiUrl: string;
runner: LimrunIosRunner;
appBundleId?: string;
readonly ownership: LimrunInstanceOwnership;
device: DeviceInfo;
syncCache?: LimrunSyncCache;
client: LimrunIosClient;
/** Instance bearer token; the recording download the SDK would run inline is done by the host instead. */
readonly token: string;
readonly dependencies: Pick<LimrunRuntimeDependencies, 'host' | 'ios'>;
};
export type LimrunIosRemoteInstallOptions = {
md5?: string;
relaunch?: boolean;
appIdentifierHint?: string;
};
export type LimrunIosRemoteInstallResult = {
appId?: string;
};
type LimrunIosApp = Awaited<ReturnType<LimrunIosClient['listApps']>>[number];
export async function createLimrunIosSession(
options: {
lease: DeviceLease;
instanceId: string;
ownership: LimrunInstanceOwnership;
device: DeviceInfo;
apiUrl: string;
token: string;
},
dependencies: Pick<LimrunRuntimeDependencies, 'host' | 'ios'>,
): Promise<LimrunIosSession> {
const client = await createIosInstanceClient({
apiUrl: options.apiUrl,
token: options.token,
logLevel: 'warn',
});
try {
const runner = await dependencies.ios.createRunner({ ...options, client });
return { ...options, platform: 'ios', client, runner, dependencies };
} catch (error) {
client.disconnect();
throw error;
}
}
export async function installLimrunIosApp(
session: LimrunIosSession,
installablePath: string,
options?: ProviderDeviceInstallOptions,
signal?: AbortSignal,
operationDrain?: LimrunRequestOperationDrain,
): Promise<ProviderDeviceInstallResult> {
signal?.throwIfAborted();
const { LimrunSyncCache } = await import('./sync-cache.ts');
const syncCache = (session.syncCache ??= new LimrunSyncCache());
const stat = await fs.promises.stat(installablePath);
const appName =
(stat.isDirectory()
? await session.dependencies.ios.readBundleAppName(installablePath)
: undefined) ?? inferAppNameFromPath(installablePath);
const result = await verifyLimrunIosInstall(
session,
async () => {
signal?.throwIfAborted();
// The shared deployment owner must retain the artifact until sync finishes reading it.
const synced = await awaitLimrunDeploymentOperation(
operationDrain,
syncCache.run(
installablePath,
'ios',
(basisCacheDir) =>
session.client.syncApp(installablePath, {
basisCacheDir,
install: true,
watch: false,
launchMode: options?.relaunch ? 'RelaunchIfRunning' : 'None',
}),
signal,
),
);
signal?.throwIfAborted();
return synced.installedBundleId;
},
options,
signal,
operationDrain,
);
const bundleId = result.appId;
return {
...(bundleId ? { bundleId, launchTarget: bundleId } : {}),
...(appName ? { appName } : {}),
};
}
export async function installLimrunIosRemoteApp(
session: LimrunIosSession,
url: string,
options?: LimrunIosRemoteInstallOptions,
signal?: AbortSignal,
operationDrain?: LimrunRequestOperationDrain,
): Promise<LimrunIosRemoteInstallResult> {
return await verifyLimrunIosInstall(
session,
async () => {
const result = await awaitLimrunDeploymentOperation(
operationDrain,
session.client.installApp(url, {
// The server's digest cache survives uninstall, so every requested install must run.
launchMode: options?.relaunch ? 'RelaunchIfRunning' : undefined,
}),
signal,
);
return result.bundleId;
},
options,
signal,
operationDrain,
);
}
async function verifyLimrunIosInstall(
session: LimrunIosSession,
install: () => Promise<string | undefined>,
options?: LimrunIosRemoteInstallOptions,
signal?: AbortSignal,
operationDrain?: LimrunRequestOperationDrain,
): Promise<LimrunIosRemoteInstallResult> {
signal?.throwIfAborted();
const beforeInstallApps = await awaitLimrunDeploymentOperation(
operationDrain,
session.client.listApps(),
signal,
).catch((error: unknown) => {
if (signal?.aborted) throw error;
return undefined;
});
const resultBundleId = normalizeOptionalString(await install());
const requestedBundleId = normalizeOptionalString(options?.appIdentifierHint);
let afterInstallApps: LimrunIosApp[] = [];
for (const delayMs of IOS_APP_INVENTORY_RETRY_DELAYS_MS) {
if (delayMs > 0) await sleep(delayMs, undefined, { signal });
afterInstallApps = await awaitLimrunDeploymentOperation(
operationDrain,
session.client.listApps(),
signal,
);
const verifiedBundleId = resolveInstalledIosAppId({
resultBundleId,
requestedBundleId,
beforeInstallApps,
afterInstallApps,
});
if (verifiedBundleId) return { appId: verifiedBundleId };
}
throw new AppError('COMMAND_FAILED', 'Limrun iOS app installation could not be verified.', {
resultBundleId,
requestedBundleId,
installedUserApps: afterInstallApps
.filter(isUserInstalledIosApp)
.map((app) => app.bundleId)
.sort(),
});
}
export function createLimrunIosInteractor(
session: LimrunIosSession,
runner?: RunnerContext,
): Interactor {
const context = {
...runner,
get appBundleId() {
return runner?.appBundleId ?? session.appBundleId;
},
};
const shared = session.runner.createInteractor(context);
const host = new LimrunIosHostInteractor(session, shared, context);
return {
...shared,
open: host.open.bind(host),
openDevice: host.openDevice.bind(host),
close: host.close.bind(host),
setSetting: host.setSetting.bind(host),
};
}
class LimrunIosHostInteractor {
private readonly session: LimrunIosSession;
private readonly signal?: AbortSignal;
private readonly context: RunnerContext;
private readonly shared: Interactor;
constructor(session: LimrunIosSession, shared: Interactor, context: RunnerContext) {
this.session = session;
this.shared = shared;
this.context = context;
this.signal = context.signal;
}
async open(app: string, options?: Parameters<Interactor['open']>[1]): Promise<void> {
const target =
options?.appBundleId ??
(
await this.session.runner.applicationTools.resolveOpenTarget(this.session.device, {
target: app,
currentAppBundleId: this.context.appBundleId,
surface: 'app',
})
).appBundleId;
await this.shared.open(app, { ...options, appBundleId: target });
this.session.appBundleId = target;
}
async openDevice(): Promise<void> {}
async close(app: string): Promise<void> {
const { appBundleId } = await this.session.runner.applicationTools.resolveOpenTarget(
this.session.device,
{
target: app,
surface: 'app',
},
);
await this.shared.close(appBundleId ?? app);
if (appBundleId !== undefined && this.session.appBundleId === appBundleId) {
await this.session.runner.resetTarget(this.signal);
this.session.appBundleId = undefined;
}
}
async setSetting(
setting: string,
state: string,
appId?: string,
options?: SettingOptions,
): Promise<Record<string, unknown> | void> {
if (setting.toLowerCase() === 'reset-keychain') {
throw new AppError('UNSUPPORTED_OPERATION', 'Limrun does not expose keychain reset.');
}
return await this.shared.setSetting(setting, state, appId, options);
}
}
function inferAppNameFromPath(appPath: string): string | undefined {
const base = path.basename(appPath).replace(/\.(?:app|ipa|apk|aab|zip)$/i, '');
return base || undefined;
}
const IOS_APP_INVENTORY_RETRY_DELAYS_MS = [0, 250] as const;
function resolveInstalledIosAppId(params: {
resultBundleId?: string;
requestedBundleId?: string;
beforeInstallApps: LimrunIosApp[] | undefined;
afterInstallApps: LimrunIosApp[];
}): string | undefined {
const installedBundleIds = new Set(params.afterInstallApps.map((app) => app.bundleId));
return (
(params.resultBundleId && installedBundleIds.has(params.resultBundleId)
? params.resultBundleId
: undefined) ??
(params.requestedBundleId && installedBundleIds.has(params.requestedBundleId)
? params.requestedBundleId
: undefined) ??
inferNewUserInstalledApp(params.beforeInstallApps, params.afterInstallApps)
);
}
function inferNewUserInstalledApp(
beforeInstallApps: LimrunIosApp[] | undefined,
afterInstallApps: LimrunIosApp[],
): string | undefined {
if (!beforeInstallApps) return undefined;
const beforeBundleIds = new Set(beforeInstallApps.map((app) => app.bundleId));
const candidates = afterInstallApps.filter(
(app) => isUserInstalledIosApp(app) && !beforeBundleIds.has(app.bundleId),
);
return candidates.length === 1 ? candidates[0]?.bundleId : undefined;
}
export function isUserInstalledIosApp(app: LimrunIosApp): boolean {
return (
!app.bundleId.startsWith('com.apple.') && !app.installType.toLowerCase().includes('system')
);
}