@@ -150,7 +150,7 @@ export async function resolveCoreDeviceTunnelIp(
150150 * that spells the command out, and an unreadable device stays unreadable instead of becoming an
151151 * assumption about what is wrong with it (#2683).
152152 */
153- export async function readIosDeviceDetails (
153+ async function readIosDeviceDetails (
154154 device : DeviceInfo ,
155155 timeoutBudgetMs : number ,
156156 signal ?: AbortSignal ,
@@ -239,67 +239,88 @@ export type IosDeviceDetails = {
239239 bootState ?: string ;
240240} ;
241241
242- export function parseIosDeviceDetailsPayload ( payload : unknown ) : IosDeviceDetails {
243- const result = ( payload as { result ?: unknown } | null | undefined ) ?. result ;
244- if ( ! result || typeof result !== 'object' ) return { } ;
245- const direct = (
246- result as {
247- connectionProperties ?: { tunnelState ?: unknown ; tunnelIPAddress ?: unknown } ;
248- }
249- ) . connectionProperties ;
250- const nested = (
251- result as {
252- device ?: { connectionProperties ?: { tunnelState ?: unknown ; tunnelIPAddress ?: unknown } } ;
253- }
254- ) . device ?. connectionProperties ;
255- const tunnelState =
256- readNonEmptyString ( direct ?. tunnelState ) ?? readNonEmptyString ( nested ?. tunnelState ) ;
257- const tunnelIp =
258- readNonEmptyString ( direct ?. tunnelIPAddress ) ?? readNonEmptyString ( nested ?. tunnelIPAddress ) ;
259- const deviceProperties = readDeviceProperties ( result ) ;
260- const developerModeStatus =
261- readNonEmptyString ( deviceProperties ?. developerModeStatus ) ??
262- readNonEmptyString ( nestedDevice ( result ) ?. deviceProperties ?. developerModeStatus ) ;
263- const developerDiskImageServicesAvailable =
264- readBoolean ( deviceProperties ?. ddiServicesAvailable ) ??
265- readBoolean ( nestedDevice ( result ) ?. deviceProperties ?. ddiServicesAvailable ) ;
266- const bootState =
267- readNonEmptyString ( deviceProperties ?. bootState ) ??
268- readNonEmptyString ( nestedDevice ( result ) ?. deviceProperties ?. bootState ) ;
269- const outcome = readNonEmptyString (
270- ( payload as { info ?: { outcome ?: unknown } } | null | undefined ) ?. info ?. outcome ,
271- ) ;
242+ /**
243+ * What one payload reports, whichever of the two shapes CoreDevice used. Fields arrive either on
244+ * `result` or nested under `result.device`, and that is a fact about the payload rather than about
245+ * any field, so the shapes are collapsed into one pair of sections before anything is read: a parser
246+ * that prefers the direct value per field has to spell the fallback out every time it grows a field,
247+ * which is how a new state ends up read from only one of the two shapes (#2683).
248+ */
249+ /** The payload\'s two sections, after the shape difference between releases is resolved. */
250+ type ReportedSections = {
251+ connectionProperties ?: Record < string , unknown > ;
252+ deviceProperties ?: Record < string , unknown > ;
253+ } ;
254+
255+ function readReportedSections ( result : object ) : ReportedSections {
256+ const source = result as {
257+ device ?: unknown ;
258+ connectionProperties ?: unknown ;
259+ deviceProperties ?: unknown ;
260+ } ;
261+ const nested = asRecord ( source . device ) ;
272262 return {
273- ... ( outcome ? { outcome } : { } ) ,
274- ... ( tunnelState ? { tunnelState } : { } ) ,
275- ... ( tunnelIp ? { tunnelIp } : { } ) ,
276- ... ( developerModeStatus ? { developerModeStatus } : { } ) ,
277- ... ( developerDiskImageServicesAvailable === undefined
278- ? { }
279- : { developerDiskImageServicesAvailable } ) ,
280- ... ( bootState ? { bootState } : { } ) ,
263+ connectionProperties : mergeReportedSections (
264+ asRecord ( source . connectionProperties ) ,
265+ asRecord ( nested ?. connectionProperties ) ,
266+ ) ,
267+ deviceProperties : mergeReportedSections (
268+ asRecord ( source . deviceProperties ) ,
269+ asRecord ( nested ?. deviceProperties ) ,
270+ ) ,
281271 } ;
282272}
283273
284- function readDeviceProperties ( result : object ) : DeviceProperties | undefined {
285- const properties = ( result as { deviceProperties ?: unknown } ) . deviceProperties ;
286- return properties && typeof properties === 'object'
287- ? ( properties as DeviceProperties )
288- : undefined ;
274+ /** Direct wins wherever it spelled a value; the nested section fills the fields it left blank. */
275+ function mergeReportedSections (
276+ direct : Record < string , unknown > | undefined ,
277+ nested : Record < string , unknown > | undefined ,
278+ ) : Record < string , unknown > | undefined {
279+ if ( ! direct ) return nested ;
280+ if ( ! nested ) return direct ;
281+ const merged : Record < string , unknown > = { ...nested } ;
282+ for ( const [ key , value ] of Object . entries ( direct ) ) {
283+ if ( value !== undefined ) merged [ key ] = value ;
284+ }
285+ return merged ;
289286}
290287
291- function nestedDevice ( result : object ) : { deviceProperties ?: DeviceProperties } | undefined {
292- const device = ( result as { device ?: unknown } ) . device ;
293- return device && typeof device === 'object'
294- ? ( device as { deviceProperties ?: DeviceProperties } )
295- : undefined ;
288+ function asRecord ( value : unknown ) : Record < string , unknown > | undefined {
289+ return value && typeof value === 'object' ? ( value as Record < string , unknown > ) : undefined ;
296290}
297291
298- type DeviceProperties = {
299- developerModeStatus ?: unknown ;
300- ddiServicesAvailable ?: unknown ;
301- bootState ?: unknown ;
302- } ;
292+ export function parseIosDeviceDetailsPayload ( payload : unknown ) : IosDeviceDetails {
293+ const result = asRecord ( ( payload as { result ?: unknown } | undefined ) ?. result ) ;
294+ if ( ! result ) return { } ;
295+ const { connectionProperties, deviceProperties } = readReportedSections ( result ) ;
296+ return {
297+ ...withValue (
298+ 'outcome' ,
299+ readNonEmptyString ( ( payload as { info ?: { outcome ?: unknown } } ) ?. info ?. outcome ) ,
300+ ) ,
301+ ...withValue ( 'tunnelState' , readNonEmptyString ( connectionProperties ?. tunnelState ) ) ,
302+ ...withValue ( 'tunnelIp' , readNonEmptyString ( connectionProperties ?. tunnelIPAddress ) ) ,
303+ ...withValue ( 'developerModeStatus' , readNonEmptyString ( deviceProperties ?. developerModeStatus ) ) ,
304+ ...withValue (
305+ 'developerDiskImageServicesAvailable' ,
306+ readBoolean ( deviceProperties ?. ddiServicesAvailable ) ,
307+ ) ,
308+ ...withValue ( 'bootState' , readNonEmptyString ( deviceProperties ?. bootState ) ) ,
309+ } ;
310+ }
311+
312+ /**
313+ * Reports a field only when the payload carried it. An absent state has to stay absent so the reader
314+ * can tell that the device said nothing from the device saying no.
315+ */
316+ function withValue < K extends keyof IosDeviceDetails > (
317+ key : K ,
318+ value : IosDeviceDetails [ K ] ,
319+ ) : Pick < IosDeviceDetails , K > {
320+ return value === undefined
321+ ? ( { } as Pick < IosDeviceDetails , K > )
322+ : ( { [ key ] : value } as Pick < IosDeviceDetails , K > ) ;
323+ }
303324
304325function readNonEmptyString ( value : unknown ) : string | undefined {
305326 return typeof value === 'string' && value . trim ( ) ? value . trim ( ) : undefined ;
0 commit comments