1+ import { util } from '@appium/support' ;
2+
13import { AX_OBJECT_TYPE } from './ax-deserialize.js' ;
24
35/**
@@ -73,17 +75,22 @@ function toBuffer(value: unknown): Buffer | undefined {
7375 return undefined ;
7476}
7577
76- /** Parses a deserialized `AXAuditElement_v1`. */
78+ /**
79+ * Parses a deserialized `AXAuditElement_v1`.
80+ *
81+ * The `_v1` suffixes are the daemon's own wire keys, not our assumption. A
82+ * future shape would carry different keys, so this returns `undefined` rather
83+ * than misreading one.
84+ */
7785export function toAxElement ( value : unknown ) : AxElement | undefined {
78- if ( typeof value !== 'object' || value === null ) {
86+ if ( ! util . isPlainObject ( value ) ) {
7987 return undefined ;
8088 }
8189 const fields = value as Record < string , unknown > ;
8290 const platformValue = fields . PlatformElementValue_v1 ;
83- const container =
84- typeof platformValue === 'object' && platformValue !== null
85- ? ( ( platformValue as Record < string , unknown > ) [ 'NS.data' ] ?? platformValue )
86- : undefined ;
91+ const container = util . isPlainObject ( platformValue )
92+ ? ( ( platformValue as Record < string , unknown > ) [ 'NS.data' ] ?? platformValue )
93+ : undefined ;
8794 const platformElement = toBuffer ( container ) ;
8895 if ( ! platformElement ) {
8996 return undefined ;
@@ -113,7 +120,7 @@ export function serializeAxElement(element: AxElement): Record<string, unknown>
113120}
114121
115122function toAttribute ( value : unknown ) : AxElementAttribute | undefined {
116- if ( typeof value !== 'object' || value === null ) {
123+ if ( ! util . isPlainObject ( value ) ) {
117124 return undefined ;
118125 }
119126 const fields = value as Record < string , unknown > ;
@@ -134,21 +141,14 @@ function toAttribute(value: unknown): AxElementAttribute | undefined {
134141
135142/** Drops the decoder's type tag so the object round-trips as the daemon sent it. */
136143function stripTag ( fields : Record < string , unknown > ) : Record < string , unknown > {
137- const out : Record < string , unknown > = { } ;
138- for ( const [ key , value ] of Object . entries ( fields ) ) {
139- if ( key !== AX_OBJECT_TYPE ) {
140- out [ key ] = value ;
141- }
142- }
143- return out ;
144+ return Object . fromEntries ( Object . entries ( fields ) . filter ( ( [ key ] ) => key !== AX_OBJECT_TYPE ) ) ;
144145}
145146
146147/** Rebuilds an attribute descriptor for the wire. */
147148export function serializeAxAttribute ( attribute : AxElementAttribute ) : Record < string , unknown > {
148- const value : Record < string , unknown > = { } ;
149- for ( const [ key , inner ] of Object . entries ( attribute . raw ) ) {
150- value [ key ] = { ObjectType : 'passthrough' , Value : inner } ;
151- }
149+ const value = Object . fromEntries (
150+ Object . entries ( attribute . raw ) . map ( ( [ key , inner ] ) => [ key , { ObjectType : 'passthrough' , Value : inner } ] ) ,
151+ ) ;
152152 return {
153153 ObjectType : 'AXAuditElementAttribute_v1' ,
154154 Value : { ObjectType : 'passthrough' , Value : value } ,
@@ -157,11 +157,11 @@ export function serializeAxAttribute(attribute: AxElementAttribute): Record<stri
157157
158158/** Parses the payload of an inbound `hostInspectorCurrentElementChanged:`. */
159159export function toInspectedElement ( value : unknown ) : AxInspectedElement {
160- const fields = ( typeof value === 'object' && value !== null ? value : { } ) as Record < string , unknown > ;
160+ const fields = ( util . isPlainObject ( value ) ? value : { } ) as Record < string , unknown > ;
161161 const rawSections = Array . isArray ( fields . InspectorSectionsValue_v1 ) ? fields . InspectorSectionsValue_v1 : [ ] ;
162162 const sections : AxInspectorSection [ ] = [ ] ;
163163 for ( const rawSection of rawSections ) {
164- if ( typeof rawSection !== 'object' || rawSection === null ) {
164+ if ( ! util . isPlainObject ( rawSection ) ) {
165165 continue ;
166166 }
167167 const section = rawSection as Record < string , unknown > ;
0 commit comments