Repository navigation
Expand file tree
/
Copy pathplatform-runtime-device-inventory.ts
More file actions
213 lines (201 loc) · 7 KB
/
Copy pathplatform-runtime-device-inventory.ts
File metadata and controls
213 lines (201 loc) · 7 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
import {
assertProviderOutcome,
assertSourceFamily,
filterDeviceInventoryProjection,
LOCAL_DEVICE_INVENTORY_PLATFORM_SELECTORS,
projectProviderDeviceInventoryRequest,
type ProviderDeviceInventorySource,
} from '@agent-device/contracts/device';
import type {
ComposedDeviceInventoryGateways,
DeviceInventoryGateway,
DeviceInventorySource,
InstalledAppProbe,
InventoryPlatformModule,
PlatformModuleRegistry,
ProviderAwareDeviceInventoryGateway,
} from '@agent-device/contracts/platform-module';
import type {
DeviceInventoryHost,
DeviceInventoryHostFor,
PlatformRequestScope,
} from '@agent-device/contracts/platform-runtime-host';
import {
isApplePlatform,
type DeviceInfo,
type Platform,
type PlatformSelector,
} from '@agent-device/kernel/device';
export function createComposedDeviceInventoryGateways(
options: Readonly<{
registry: PlatformModuleRegistry<InventoryPlatformModule>;
loadHost: () => Promise<DeviceInventoryHost>;
provider?: ProviderDeviceInventorySource;
}>,
): ComposedDeviceInventoryGateways {
const localOnly = createLocalDeviceInventoryGateway(options.registry, options.loadHost);
const provider = options.provider;
const discoverWithSource: ProviderAwareDeviceInventoryGateway['discoverWithSource'] = provider
? async (request, scope) => {
scope.signal.throwIfAborted();
const provided = await provider.discover(
projectProviderDeviceInventoryRequest(request),
scope.signal,
);
scope.signal.throwIfAborted();
assertProviderOutcome(provided);
if (provided.kind === 'inventory') {
return {
devices: filterDeviceInventoryProjection(
provided.devices.map((device) => ({ ...device })),
request,
),
source: 'provider',
};
}
return { devices: await localOnly.discover(request, scope), source: 'local' };
}
: async (request, scope) => ({
devices: await localOnly.discover(request, scope),
source: 'local',
});
const providerFirst: ProviderAwareDeviceInventoryGateway = Object.freeze({
discover: async (request, scope) => (await discoverWithSource(request, scope)).devices,
discoverWithSource,
});
// Function-scoped: device selection reads this probe from the request context, and loading it
// must not evaluate Apple app-resolution mechanics until a selection actually narrows.
const findInstalledApp: InstalledAppProbe = async (device, appTarget) => {
const { findIosSimulatorInstalledApp } =
await import('@agent-device/platform-apple/app-resolution');
return await findIosSimulatorInstalledApp(device, appTarget);
};
return Object.freeze({ providerFirst, localOnly, findInstalledApp });
}
function createLocalDeviceInventoryGateway(
registry: PlatformModuleRegistry<InventoryPlatformModule>,
loadHost: () => Promise<DeviceInventoryHost>,
): DeviceInventoryGateway {
let hostLoad: Promise<DeviceInventoryHost> | undefined;
const requiredHost = async (): Promise<DeviceInventoryHost> => {
hostLoad ??= loadHost();
try {
return await hostLoad;
} catch (error) {
hostLoad = undefined;
throw error;
}
};
const sourceLoads = new Map<Platform, Promise<DeviceInventorySource>>();
const loadSource = async (family: Platform): Promise<DeviceInventorySource> => {
const existing = sourceLoads.get(family);
if (existing) return await existing;
const pending = loadFamilyInventory(registry.get(family), requiredHost);
sourceLoads.set(family, pending);
try {
return await pending;
} catch (error) {
if (sourceLoads.get(family) === pending) sourceLoads.delete(family);
throw error;
}
};
return Object.freeze({
discover: async (request, scope) => {
scope.signal.throwIfAborted();
if (request.platform) {
return await discoverFamily(
loadSource,
familyForSelector(request.platform),
request,
scope,
);
}
const perFamily = await Promise.all(
LOCAL_DEVICE_INVENTORY_PLATFORM_SELECTORS.map(async (selector) => {
try {
const devices = await discoverFamily(
loadSource,
familyForSelector(selector),
{ ...request, platform: selector },
scope,
);
return Array.isArray(devices) ? devices : [];
} catch {
return [];
}
}),
);
scope.signal.throwIfAborted();
return perFamily.flat();
},
});
}
async function loadFamilyInventory(
module: InventoryPlatformModule,
requiredHost: () => Promise<DeviceInventoryHost>,
): Promise<DeviceInventorySource> {
switch (module.family) {
case 'apple':
return await module.loadInventory(appleInventoryHost(await requiredHost()));
case 'android':
return await module.loadInventory(androidInventoryHost(await requiredHost()));
case 'harmonyos':
return await module.loadInventory(harmonyInventoryHost(await requiredHost()));
case 'vega':
return await module.loadInventory(vegaInventoryHost(await requiredHost()));
case 'linux':
return await module.loadInventory(linuxInventoryHost(await requiredHost()));
case 'web':
return await module.loadInventory();
}
}
function appleInventoryHost(host: DeviceInventoryHost): DeviceInventoryHostFor<'apple'> {
return Object.freeze({
appleTools: host.appleTools,
files: host.files,
hostName: host.hostName,
hostOs: host.hostOs,
observations: host.observations,
});
}
function androidInventoryHost(host: DeviceInventoryHost): DeviceInventoryHostFor<'android'> {
return Object.freeze({
commands: host.commands,
files: host.files,
homeDirectory: host.homeDirectory,
toolchains: host.toolchains,
});
}
function harmonyInventoryHost(host: DeviceInventoryHost): DeviceInventoryHostFor<'harmonyos'> {
return Object.freeze({
commands: host.commands,
files: host.files,
toolchains: host.toolchains,
});
}
function vegaInventoryHost(host: DeviceInventoryHost): DeviceInventoryHostFor<'vega'> {
return Object.freeze({
commands: host.commands,
files: host.files,
homeDirectory: host.homeDirectory,
});
}
function linuxInventoryHost(host: DeviceInventoryHost): DeviceInventoryHostFor<'linux'> {
return Object.freeze({ hostName: host.hostName, hostOs: host.hostOs });
}
async function discoverFamily(
loadSource: (family: Platform) => Promise<DeviceInventorySource>,
family: Platform,
request: Parameters<DeviceInventorySource['discover']>[0],
scope: PlatformRequestScope,
): Promise<readonly DeviceInfo[]> {
const source = await loadSource(family);
scope.signal.throwIfAborted();
const devices = await source.discover(request, scope);
scope.signal.throwIfAborted();
assertSourceFamily(family, devices);
return filterDeviceInventoryProjection(devices, request);
}
function familyForSelector(selector: PlatformSelector): Platform {
return isApplePlatform(selector) ? 'apple' : selector;
}