@appium/coresim 1.12.0 → 1.13.1

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 (52) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +1 -0
  3. package/lib/src/commands/permissions.d.ts +15 -7
  4. package/lib/src/commands/permissions.d.ts.map +1 -1
  5. package/lib/src/commands/permissions.js +66 -6
  6. package/lib/src/commands/permissions.js.map +1 -1
  7. package/lib/src/index.d.ts +1 -1
  8. package/lib/src/index.d.ts.map +1 -1
  9. package/lib/src/index.js.map +1 -1
  10. package/lib/src/native-simctl.d.ts +27 -9
  11. package/lib/src/native-simctl.d.ts.map +1 -1
  12. package/lib/src/native-simctl.js +9 -16
  13. package/lib/src/native-simctl.js.map +1 -1
  14. package/lib/src/types.d.ts +6 -4
  15. package/lib/src/types.d.ts.map +1 -1
  16. package/lib/src/utils/bulletin-board-model-v2.d.ts +10 -0
  17. package/lib/src/utils/bulletin-board-model-v2.d.ts.map +1 -0
  18. package/lib/src/utils/bulletin-board-model-v2.js +73 -0
  19. package/lib/src/utils/bulletin-board-model-v2.js.map +1 -0
  20. package/lib/src/utils/bulletin-board-model-v3.d.ts +4 -0
  21. package/lib/src/utils/bulletin-board-model-v3.d.ts.map +1 -0
  22. package/lib/src/utils/bulletin-board-model-v3.js +16 -0
  23. package/lib/src/utils/bulletin-board-model-v3.js.map +1 -0
  24. package/lib/src/utils/bulletin-board-model.d.ts +19 -0
  25. package/lib/src/utils/bulletin-board-model.d.ts.map +1 -0
  26. package/lib/src/utils/bulletin-board-model.js +2 -0
  27. package/lib/src/utils/bulletin-board-model.js.map +1 -0
  28. package/lib/src/utils/bulletin-board-store.d.ts +41 -0
  29. package/lib/src/utils/bulletin-board-store.d.ts.map +1 -0
  30. package/lib/src/utils/bulletin-board-store.js +114 -0
  31. package/lib/src/utils/bulletin-board-store.js.map +1 -0
  32. package/lib/src/utils/index.d.ts +3 -0
  33. package/lib/src/utils/index.d.ts.map +1 -1
  34. package/lib/src/utils/index.js +2 -0
  35. package/lib/src/utils/index.js.map +1 -1
  36. package/lib/src/utils/springboard.d.ts +26 -0
  37. package/lib/src/utils/springboard.d.ts.map +1 -0
  38. package/lib/src/utils/springboard.js +106 -0
  39. package/lib/src/utils/springboard.js.map +1 -0
  40. package/package.json +3 -3
  41. package/prebuilds/darwin-arm64/@appium+coresim.node +0 -0
  42. package/src/commands/permissions.ts +75 -11
  43. package/src/coresim.mm +7 -0
  44. package/src/index.ts +1 -1
  45. package/src/native-simctl.ts +45 -19
  46. package/src/types.ts +6 -3
  47. package/src/utils/bulletin-board-model-v2.ts +79 -0
  48. package/src/utils/bulletin-board-model-v3.ts +16 -0
  49. package/src/utils/bulletin-board-model.ts +20 -0
  50. package/src/utils/bulletin-board-store.ts +126 -0
  51. package/src/utils/index.ts +3 -0
  52. package/src/utils/springboard.ts +119 -0
@@ -56,6 +56,13 @@ import {
56
56
  import {isVideoRecording, startVideoRecording, stopVideoRecording} from './commands/video-recording.js';
57
57
  import {startVideoStream} from './commands/video-stream.js';
58
58
  import {getWebInspectorSocket} from './commands/webinspector.js';
59
+ import {NativeSimUnavailableError} from './errors.js';
60
+ import type {
61
+ NativeCoreSimModule,
62
+ NativeDeviceHandle,
63
+ NativeDeviceSetHandle,
64
+ NativeServiceContextHandle,
65
+ } from './types.js';
59
66
  // Bare re-imports so `declare module './native-simctl.js'` augmentations in the command modules
60
67
  // (which add their methods to NativeSimctl's type) reach downstream consumers' emitted .d.ts
61
68
  // import graph — the named imports above alone aren't part of NativeSimctl's emitted type
@@ -78,17 +85,26 @@ import './commands/ui.js';
78
85
  import './commands/video-recording.js';
79
86
  import './commands/video-stream.js';
80
87
  import './commands/webinspector.js';
81
- import {NativeSimUnavailableError} from './errors.js';
82
- import type {
83
- NativeCoreSimModule,
84
- NativeDeviceHandle,
85
- NativeDeviceSetHandle,
86
- NativeServiceContextHandle,
87
- } from './types.js';
88
88
  import {getPkgRoot, PACKAGE_NAME, runCatchingAsync} from './utils/index.js';
89
89
 
90
90
  const require = createRequire(import.meta.url);
91
91
 
92
+ export interface NativeSimctlOptions {
93
+ /** Defaults to `xcode-select -p` (the active Xcode's Developer dir). */
94
+ developerDir?: string;
95
+ /**
96
+ * Defaults to the default device set (`~/Library/Developer/CoreSimulator/Devices`), same as
97
+ * `simctl --set <path>`.
98
+ */
99
+ deviceSetPath?: string;
100
+ /**
101
+ * The versions of SpringBoard's notifications store (`sectionInfoVersionNumber`) the runtimes in
102
+ * use may have, the oldest first. Defaults to all the known ones; callers that know their Xcode
103
+ * narrow it. A store of any other version is refused.
104
+ */
105
+ bulletinBoardVersions?: readonly number[];
106
+ }
107
+
92
108
  /**
93
109
  * Drives `CoreSimulator.framework` directly — no `simctl`/`xcrun` subprocess — for the operations
94
110
  * `node-simctl`'s `Simctl` class exposes over the CLI. Devices are addressed by UDID; each call
@@ -110,18 +126,28 @@ const require = createRequire(import.meta.url);
110
126
  export class NativeSimctl {
111
127
  private cachedServiceContext: Promise<NativeServiceContextHandle> | undefined;
112
128
 
129
+ /** @internal Read by the command mixins. */
130
+ readonly _options: Readonly<NativeSimctlOptions>;
131
+
113
132
  /**
114
- * @param developerDir — defaults to `xcode-select -p` (the active Xcode's Developer dir).
115
- * @param deviceSetPath — defaults to the default device set
116
- * (`~/Library/Developer/CoreSimulator/Devices`), same as `simctl --set <path>`. Constructing a
117
- * NativeSimctl must never throw — the platform check and the native sharedServiceContext call
118
- * both happen lazily in the `_serviceContext` getter below, so only a method that actually needs
119
- * the simulator (getDevices(), createDevice(), ...) can raise.
133
+ * Constructing a NativeSimctl must never throw — the platform check and the native
134
+ * sharedServiceContext call both happen lazily in the `_serviceContext` getter below, so only a
135
+ * method that actually needs the simulator (getDevices(), createDevice(), ...) can raise.
136
+ *
137
+ * @param options — see {@link NativeSimctlOptions}.
120
138
  */
121
- constructor(
122
- private readonly developerDir?: string,
123
- private readonly deviceSetPath?: string,
124
- ) {}
139
+ constructor(options?: NativeSimctlOptions);
140
+ /**
141
+ * @deprecated Pass {@link NativeSimctlOptions} instead.
142
+ * @param developerDir — see {@link NativeSimctlOptions.developerDir}.
143
+ * @param deviceSetPath — see {@link NativeSimctlOptions.deviceSetPath}.
144
+ */
145
+ constructor(developerDir?: string, deviceSetPath?: string);
146
+ constructor(optionsOrDeveloperDir?: NativeSimctlOptions | string, deviceSetPath?: string) {
147
+ this._options = util.isPlainObject(optionsOrDeveloperDir)
148
+ ? {...(optionsOrDeveloperDir as NativeSimctlOptions)}
149
+ : {developerDir: optionsOrDeveloperDir as string | undefined, deviceSetPath};
150
+ }
125
151
 
126
152
  /** @returns `CFBundleVersion` of the loaded `CoreSimulator.framework` (e.g. `"1171.6"`). */
127
153
  static async frameworkVersion(): Promise<string> {
@@ -143,7 +169,7 @@ export class NativeSimctl {
143
169
  async _serviceContext(): Promise<NativeServiceContextHandle> {
144
170
  if (!this.cachedServiceContext) {
145
171
  this.cachedServiceContext = runCatchingAsync(() =>
146
- loadNative().sharedServiceContext(this.developerDir ?? defaultDeveloperDir()),
172
+ loadNative().sharedServiceContext(this._options.developerDir ?? defaultDeveloperDir()),
147
173
  ).catch((err: unknown) => {
148
174
  this.cachedServiceContext = undefined;
149
175
  throw err;
@@ -155,7 +181,7 @@ export class NativeSimctl {
155
181
  /** @internal Not part of the public API — exposed only so `src/commands/*.ts` mixins can call it. */
156
182
  async _deviceSet(): Promise<NativeDeviceSetHandle> {
157
183
  const context = await this._serviceContext();
158
- const deviceSetPath = this.deviceSetPath;
184
+ const deviceSetPath = this._options.deviceSetPath;
159
185
  return runCatchingAsync(() =>
160
186
  deviceSetPath ? context.deviceSetWithPath(deviceSetPath) : context.defaultDeviceSet(),
161
187
  );
package/src/types.ts CHANGED
@@ -87,9 +87,10 @@ export type SimPermissionStatus = 'unset' | 'denied' | 'granted' | 'limited';
87
87
 
88
88
  /**
89
89
  * A privacy permission grantable via `NativeSimctl.grantPermission`/`revokePermission`/
90
- * `resetPermission`. Each is backed by a row in the simulator's own TCC (privacy) database —
91
- * `location` isn't included since CoreLocation simulation has its own subsystem, not a plain TCC
92
- * row (see CLAUDE.md).
90
+ * `resetPermission`. Each except `notifications` is backed by a row in the simulator's own TCC
91
+ * (privacy) database — `location` isn't included since CoreLocation simulation has its own
92
+ * subsystem, not a plain TCC row (see CLAUDE.md). `notifications` is SpringBoard's own setting:
93
+ * changing it restarts SpringBoard, and its status can't be read back via `getPermission`.
93
94
  */
94
95
  export type SimPermissionService =
95
96
  | 'calendar'
@@ -101,6 +102,7 @@ export type SimPermissionService =
101
102
  | 'medialibrary'
102
103
  | 'microphone'
103
104
  | 'motion'
105
+ | 'notifications'
104
106
  | 'photos'
105
107
  | 'reminders'
106
108
  | 'siri'
@@ -424,6 +426,7 @@ export interface NativeDeviceHandle {
424
426
  deviceTypeIdentifier(): string;
425
427
  runtimeIdentifier(): string;
426
428
  runtimeRootPath(): string;
429
+ dataPath(): string;
427
430
  boot(options?: Record<string, unknown>): Promise<void>;
428
431
  getBootStatus(): Promise<SimBootInfo | null>;
429
432
  shutdown(): Promise<void>;
@@ -0,0 +1,79 @@
1
+ import {plist} from '@appium/support';
2
+
3
+ import type {BulletinBoardModel, NotificationsStatus} from './bulletin-board-model.js';
4
+
5
+ /** The format used by SpringBoard since iOS 18. */
6
+ export const bulletinBoardModelV2: BulletinBoardModel = {
7
+ version: 2,
8
+ createStore: () => ({sectionInfoVersionNumber: 2}),
9
+ setNotificationsStatus(store, bundleId, status) {
10
+ const sectionInfo = (store.sectionInfo ??= {});
11
+ if (status === 'unset') {
12
+ delete sectionInfo[bundleId];
13
+ } else {
14
+ sectionInfo[bundleId] = buildSectionInfo(bundleId, status);
15
+ }
16
+ },
17
+ };
18
+
19
+ /**
20
+ * Builds the NSKeyedArchiver-encoded `BBSectionInfo` of `bundleId`, the same archive
21
+ * AppleSimulatorUtils writes (SetNotificationsPermission.m). SpringBoard migrates its
22
+ * `allowsNotifications` flag to the current `authorizationStatus` when it loads the section.
23
+ */
24
+ export function buildSectionInfo(
25
+ bundleId: string,
26
+ status: Exclude<NotificationsStatus, 'unset'>,
27
+ extraSectionKeys: Record<string, unknown> = {},
28
+ ): Buffer {
29
+ const nil = {UID: 0};
30
+ return plist.createBinaryPlist({
31
+ $version: 100000,
32
+ $objects: [
33
+ '$null',
34
+ {
35
+ suppressFromSettings: false,
36
+ suppressedSettings: 0,
37
+ hideWeeApp: false,
38
+ sectionID: {UID: 2},
39
+ displayName: {UID: 5},
40
+ icon: nil,
41
+ displaysCriticalBulletins: false,
42
+ subsections: nil,
43
+ sectionInfoSettings: {UID: 3},
44
+ $class: {UID: 7},
45
+ sectionCategory: 0,
46
+ subsectionPriority: 0,
47
+ version: {UID: 6},
48
+ managedSectionInfoSettings: nil,
49
+ appName: {UID: 5},
50
+ sectionType: 0,
51
+ factorySectionID: nil,
52
+ dataProviderIDs: nil,
53
+ subsectionID: nil,
54
+ filters: nil,
55
+ pathToWeeAppPluginBundle: nil,
56
+ ...extraSectionKeys,
57
+ },
58
+ bundleId,
59
+ {
60
+ pushSettings: 63,
61
+ showsInNotificationCenter: true,
62
+ allowsNotifications: status !== 'denied',
63
+ showsOnExternalDevices: true,
64
+ contentPreviewSetting: 0,
65
+ carPlaySetting: 0,
66
+ $class: {UID: 4},
67
+ showsInLockScreen: true,
68
+ alertType: 1,
69
+ criticalAlertSetting: status === 'critical' ? 2 : 0,
70
+ },
71
+ {$classname: 'BBSectionInfoSettings', $classes: ['BBSectionInfoSettings', 'NSObject']},
72
+ bundleId,
73
+ 0,
74
+ {$classname: 'BBSectionInfo', $classes: ['BBSectionInfo', 'NSObject']},
75
+ ],
76
+ $archiver: 'NSKeyedArchiver',
77
+ $top: {root: {UID: 1}},
78
+ });
79
+ }
@@ -0,0 +1,16 @@
1
+ import {buildSectionInfo} from './bulletin-board-model-v2.js';
2
+ import type {BulletinBoardModel} from './bulletin-board-model.js';
3
+
4
+ /** The format used by SpringBoard since iOS 27: v2 with `isLocked` in the `BBSectionInfo`. */
5
+ export const bulletinBoardModelV3: BulletinBoardModel = {
6
+ version: 3,
7
+ createStore: () => ({sectionInfoVersionNumber: 3}),
8
+ setNotificationsStatus(store, bundleId, status) {
9
+ const sectionInfo = (store.sectionInfo ??= {});
10
+ if (status === 'unset') {
11
+ delete sectionInfo[bundleId];
12
+ } else {
13
+ sectionInfo[bundleId] = buildSectionInfo(bundleId, status, {isLocked: false});
14
+ }
15
+ },
16
+ };
@@ -0,0 +1,20 @@
1
+ export type NotificationsStatus = 'granted' | 'denied' | 'critical' | 'unset';
2
+
3
+ /** Content of `VersionedSectionInfo.plist`. */
4
+ export interface BulletinBoardStoreContent {
5
+ sectionInfoVersionNumber?: number;
6
+ sectionInfo?: Record<string, Uint8Array>;
7
+ }
8
+
9
+ /**
10
+ * The data model of one `sectionInfoVersionNumber` of the BulletinBoard store. The store operations
11
+ * (locking, atomic replace, ...) don't depend on it, so a new format version only needs a new model.
12
+ */
13
+ export interface BulletinBoardModel {
14
+ /** The `sectionInfoVersionNumber` this model reads and writes. */
15
+ readonly version: number;
16
+ /** An empty store, as SpringBoard of this version would start it. */
17
+ createStore(): BulletinBoardStoreContent;
18
+ /** Adds, replaces or (`unset`) removes the app's notifications section in place. */
19
+ setNotificationsStatus(store: BulletinBoardStoreContent, bundleId: string, status: NotificationsStatus): void;
20
+ }
@@ -0,0 +1,126 @@
1
+ import {execFile} from 'node:child_process';
2
+ import {randomUUID} from 'node:crypto';
3
+ import path from 'node:path';
4
+ import {promisify} from 'node:util';
5
+
6
+ import {fs, plist} from '@appium/support';
7
+
8
+ import {bulletinBoardModelV2} from './bulletin-board-model-v2.js';
9
+ import {bulletinBoardModelV3} from './bulletin-board-model-v3.js';
10
+ import type {BulletinBoardModel, BulletinBoardStoreContent, NotificationsStatus} from './bulletin-board-model.js';
11
+
12
+ const execFileAsync = promisify(execFile);
13
+
14
+ // All the known data models, the oldest first.
15
+ const KNOWN_MODELS: readonly BulletinBoardModel[] = [bulletinBoardModelV2, bulletinBoardModelV3];
16
+
17
+ // The last pending update of each store, see BulletinBoardStore.withLock.
18
+ const pendingUpdates = new Map<string, Promise<void>>();
19
+
20
+ /**
21
+ * SpringBoard's BulletinBoard store of a device (`Library/BulletinBoard/VersionedSectionInfo.plist`),
22
+ * which keeps the notifications settings of every app as one archived `BBSectionInfo` per bundle id.
23
+ */
24
+ export class BulletinBoardStore {
25
+ readonly path: string;
26
+ private readonly models: readonly BulletinBoardModel[];
27
+
28
+ /**
29
+ * @param dataPath — data directory of the device
30
+ * @param versions — the `sectionInfoVersionNumber`s supported by the runtime, the oldest first
31
+ * (all the known ones by default). A store of any other version is refused, as the format may
32
+ * change in a future runtime. A missing store (a never-booted device) is created in the first
33
+ * one, as SpringBoard migrates older versions but not newer ones.
34
+ * @throws {Error} if a version is unknown
35
+ */
36
+ constructor(dataPath: string, versions: readonly number[] = KNOWN_MODELS.map(({version}) => version)) {
37
+ this.models = versions.map((version) => {
38
+ const model = KNOWN_MODELS.find((m) => m.version === version);
39
+ if (!model) {
40
+ throw new Error(`Unknown BulletinBoard store version ${version}`);
41
+ }
42
+ return model;
43
+ });
44
+ this.path = path.join(dataPath, 'Library', 'BulletinBoard', 'VersionedSectionInfo.plist');
45
+ }
46
+
47
+ /**
48
+ * Runs `update` once every earlier update of the same store has settled: concurrent updates for
49
+ * different apps would otherwise read the same store, and the last write would drop the other app's
50
+ * section. Only the calls made by this process are serialized.
51
+ */
52
+ async withLock<T>(update: () => Promise<T>): Promise<T> {
53
+ const result = (pendingUpdates.get(this.path) ?? Promise.resolve()).then(update);
54
+ const settled = result.then(
55
+ () => {},
56
+ () => {},
57
+ );
58
+ pendingUpdates.set(this.path, settled);
59
+ try {
60
+ return await result;
61
+ } finally {
62
+ if (pendingUpdates.get(this.path) === settled) {
63
+ pendingUpdates.delete(this.path);
64
+ }
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Adds, replaces or (`unset`) removes the app's section. The store is replaced atomically, so a
70
+ * failed write leaves the previous one intact. A device that has never been booted has no store
71
+ * yet: it is created with just this section, and SpringBoard adds the rest on boot.
72
+ *
73
+ * @returns `false` if there was nothing to write (`unset` without a store)
74
+ * @throws {Error} if the existing store is of an unsupported format version
75
+ */
76
+ async setNotificationsStatus(bundleId: string, status: NotificationsStatus): Promise<boolean> {
77
+ let store: BulletinBoardStoreContent;
78
+ let model: BulletinBoardModel;
79
+ if (await fs.exists(this.path)) {
80
+ store = (await plist.parsePlistFile(this.path)) as BulletinBoardStoreContent;
81
+ model = this.modelOf(store);
82
+ } else if (status === 'unset') {
83
+ return false;
84
+ } else {
85
+ await fs.mkdir(path.dirname(this.path), {recursive: true});
86
+ model = this.models[0];
87
+ store = model.createStore();
88
+ }
89
+ model.setNotificationsStatus(store, bundleId, status);
90
+ await this.replace(store);
91
+ return true;
92
+ }
93
+
94
+ /**
95
+ * Makes the store immutable (`uchg`) so that nothing, including a dying SpringBoard, can rewrite it.
96
+ * Best effort.
97
+ */
98
+ async setImmutable(immutable: boolean): Promise<void> {
99
+ try {
100
+ await execFileAsync('chflags', [immutable ? 'uchg' : 'nouchg', this.path]);
101
+ } catch {}
102
+ }
103
+
104
+ private modelOf(store: BulletinBoardStoreContent): BulletinBoardModel {
105
+ const model = this.models.find(({version}) => version === store.sectionInfoVersionNumber);
106
+ if (!model) {
107
+ throw new Error(
108
+ `Unsupported version of ${this.path}: sectionInfoVersionNumber is ${store.sectionInfoVersionNumber}, ` +
109
+ `supported: ${this.models.map(({version}) => version).join(', ')}`,
110
+ );
111
+ }
112
+ return model;
113
+ }
114
+
115
+ private async replace(store: BulletinBoardStoreContent): Promise<void> {
116
+ // In the same directory, so that the rename is atomic.
117
+ const tmpPath = `${this.path}.${randomUUID()}.tmp`;
118
+ try {
119
+ await fs.writeFile(tmpPath, plist.createPlist(store, false), {flag: 'wx'});
120
+ await fs.rename(tmpPath, this.path);
121
+ } catch (err) {
122
+ await fs.unlink(tmpPath).catch(() => {});
123
+ throw err;
124
+ }
125
+ }
126
+ }
@@ -1,3 +1,6 @@
1
1
  export {getPkgRoot, PACKAGE_NAME} from './pkg-root.js';
2
2
  export {runCatchingAsync, toTypedError} from './run-catching.js';
3
3
  export {resolveDisplayPortId, validateDisplayId} from './display.js';
4
+ export {BulletinBoardStore} from './bulletin-board-store.js';
5
+ export type {NotificationsStatus} from './bulletin-board-model.js';
6
+ export {SpringBoard} from './springboard.js';
@@ -0,0 +1,119 @@
1
+ import {once} from 'node:events';
2
+
3
+ import {waitForCondition} from 'asyncbox';
4
+
5
+ import type {NativeSimctl} from '../native-simctl.js';
6
+
7
+ const SPRINGBOARD_SERVICE = 'com.apple.SpringBoard';
8
+ const SPRINGBOARD_RESTART_TIMEOUT_MS = 30000;
9
+
10
+ /**
11
+ * SpringBoard of a booted device: the process that owns the BulletinBoard store, see
12
+ * setNotificationsAccess. The simulator shares the host kernel, so it is signalled directly.
13
+ */
14
+ export class SpringBoard {
15
+ private pausedPid: number | undefined;
16
+
17
+ constructor(
18
+ private readonly simctl: NativeSimctl,
19
+ private readonly udid: string,
20
+ ) {}
21
+
22
+ /**
23
+ * Pauses the running SpringBoard so that it cannot rewrite the BulletinBoard store. It can be in
24
+ * the middle of a restart, hence the retries.
25
+ */
26
+ async pause(): Promise<void> {
27
+ try {
28
+ await waitForCondition(
29
+ async () => {
30
+ const pid = await this.pid().catch(() => undefined);
31
+ if (pid && sendSignal(pid, 'SIGSTOP')) {
32
+ this.pausedPid = pid;
33
+ }
34
+ return Boolean(this.pausedPid);
35
+ },
36
+ {waitMs: SPRINGBOARD_RESTART_TIMEOUT_MS, intervalMs: 300},
37
+ );
38
+ } catch {}
39
+ if (!this.pausedPid) {
40
+ throw new Error(`${SPRINGBOARD_SERVICE} is not running`);
41
+ }
42
+ }
43
+
44
+ /** Lets the paused SpringBoard run again. */
45
+ resume(): void {
46
+ if (this.pausedPid) {
47
+ sendSignal(this.pausedPid, 'SIGCONT');
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Replaces the paused SpringBoard with a new one: asks launchd to stop it (launchd starts a new
53
+ * one right away), resumes it so that it can exit, then waits for the new process.
54
+ */
55
+ async restart(): Promise<void> {
56
+ const previousPid = this.pausedPid;
57
+ try {
58
+ await this.stop();
59
+ } finally {
60
+ this.resume();
61
+ this.pausedPid = undefined;
62
+ }
63
+ try {
64
+ await waitForCondition(
65
+ async () => {
66
+ const pid = await this.pid().catch(() => undefined);
67
+ return Number.isInteger(pid) && pid !== previousPid;
68
+ },
69
+ {waitMs: SPRINGBOARD_RESTART_TIMEOUT_MS, intervalMs: 300},
70
+ );
71
+ } catch {
72
+ throw new Error(`${SPRINGBOARD_SERVICE} did not restart within ${SPRINGBOARD_RESTART_TIMEOUT_MS}ms`);
73
+ }
74
+ }
75
+
76
+ private async pid(): Promise<number | undefined> {
77
+ return (await this.simctl.listProcesses(this.udid)).find(({name}) => name === SPRINGBOARD_SERVICE)?.pid;
78
+ }
79
+
80
+ private async stop(): Promise<void> {
81
+ // Bare name: resolved against the guest runtime's own bin dirs, like in listProcesses.
82
+ const proc = await this.simctl.spawnProcess(this.udid, 'launchctl', {
83
+ arguments: ['launchctl', 'stop', SPRINGBOARD_SERVICE],
84
+ });
85
+ let stderr = '';
86
+ proc.stdout.resume();
87
+ proc.stderr.on('data', (chunk: Buffer) => {
88
+ stderr += chunk;
89
+ });
90
+ const streamError = Promise.race([once(proc.stdout, 'error'), once(proc.stderr, 'error')]).then(([err]) => {
91
+ throw err;
92
+ });
93
+ const [[code, signal]] = await Promise.race([
94
+ Promise.all([once(proc, 'exit'), once(proc.stderr, 'end')]),
95
+ streamError,
96
+ ]);
97
+ if (code !== 0) {
98
+ const reason = signal ? `signal ${signal}` : `exit code ${code}`;
99
+ throw new Error(
100
+ `'launchctl stop ${SPRINGBOARD_SERVICE}' failed with ${reason}${stderr.trim() ? `: ${stderr.trim()}` : ''}`,
101
+ );
102
+ }
103
+ }
104
+ }
105
+
106
+ /**
107
+ * @returns `false` if the process no longer exists
108
+ */
109
+ function sendSignal(pid: number, name: NodeJS.Signals): boolean {
110
+ try {
111
+ process.kill(pid, name);
112
+ return true;
113
+ } catch (err: any) {
114
+ if (err?.code === 'ESRCH') {
115
+ return false;
116
+ }
117
+ throw err;
118
+ }
119
+ }