2026-09-30 00:41:05 +08:00
import { createHash } from "node:crypto" ;
2026-10-06 23:58:59 -07:00
import { parseBinary } from "plist" ;
2026-09-30 00:41:05 +08:00
2026-10-07 21:09:33 +08:00
import { DirectoryArtifactReader } from "../DirectoryArtifactReader.js" ;
import { projectPlistValue } from "../../domain/apple/plistValue.js" ;
2026-10-07 07:51:37 +00:00
import {
omittedPrototypeKeysLimitation ,
parseXmlPropertyList ,
2026-10-07 21:09:33 +08:00
} from "../../domain/propertyListKeys.js" ;
import { decodeNibArchive , type NibArchiveDocument } from "./NibArchive.js" ;
2026-10-09 05:09:15 +08:00
import {
2026-10-11 07:02:39 +08:00
preflightPropertyListDecode ,
2026-10-09 05:09:15 +08:00
InterfaceBuilderDecodeBudget ,
InterfaceBuilderDecodeBudgetExceeded ,
} from "./InterfaceBuilderDecodeBudget.js" ;
2026-10-07 21:09:33 +08:00
import type { ArtifactEntry } from "../ArtifactReader.js" ;
2026-09-30 00:41:05 +08:00
import {
buildInterfaceBuilderAnalysis ,
interfaceBuilderLimitsSchema ,
type InterfaceBuilderDocumentInput ,
2026-10-07 21:09:33 +08:00
} from "../../domain/apple/interfaceBuilderGraph.js" ;
2026-10-07 10:33:10 +08:00
import {
mergeNibHierarchies ,
projectNibViewHierarchy ,
type NibHierarchyNode ,
} from "./NibViewHierarchy.js" ;
2026-10-07 21:09:33 +08:00
import { jsonValueSchema , type JsonValue } from "../../domain/jsonValue.js" ;
2026-10-11 00:03:18 +08:00
import { compareUnicodeCodePoints } from "../../domain/unicodeCodePointOrder.js" ;
2026-09-30 00:41:05 +08:00
2026-10-09 05:50:49 +08:00
import { decodeXmlPlistText } from "../../domain/propertyListXmlText.js" ;
2026-10-08 13:11:49 -04:00
2026-10-09 05:09:15 +08:00
const MAX_AGGREGATE_INPUT_BYTES = 32 * 1024 * 1024 ;
const MAX_AGGREGATE_DECODE_BYTES = 256 * 1024 * 1024 ;
const MAX_REPORTED_OMITTED_ARCHIVES = 64 ;
2026-09-30 00:41:05 +08:00
/** Decode compiled Interface Builder archives from a local app bundle. */
export const analyzeInterfaceBuilderBundle = async ( input : {
readonly bundlePath : string ;
readonly targetSha256 : string ;
readonly limits? : unknown ;
readonly signal? : AbortSignal ;
}) => {
const limits = interfaceBuilderLimitsSchema . parse ( input . limits ?? {});
const reader = new DirectoryArtifactReader ( input . bundlePath );
const documents : InterfaceBuilderDocumentInput [] = [];
const invalid : string [] = [];
2026-10-09 05:09:15 +08:00
const omittedArchives : Array < { path : string ; reason : string } > = [];
let unreportedOmittedArchives = 0 ;
2026-10-07 10:33:10 +08:00
const incompleteHierarchies = new Map < string , number >();
2026-10-11 07:02:39 +08:00
const prototypeKeyOmissions = new Map < string , number | null >();
2026-09-30 00:41:05 +08:00
let omitted = 0 ;
let attempted = 0 ;
2026-10-09 05:09:15 +08:00
let aggregateInputBytes = 0 ;
const decodeBudget = new InterfaceBuilderDecodeBudget (
MAX_AGGREGATE_DECODE_BYTES ,
);
const recordOmittedArchive = ( path : string , reason : string ) : void => {
if ( omittedArchives . length < MAX_REPORTED_OMITTED_ARCHIVES )
omittedArchives . push ({ path , reason });
else unreportedOmittedArchives += 1 ;
};
2026-09-30 00:41:05 +08:00
try {
2026-10-11 00:03:18 +08:00
const selection = await selectInterfaceBuilderCandidates (
reader ,
input . signal ,
limits . max_documents ,
);
omitted = selection . omitted ;
for ( const entry of selection . omittedSamples )
recordOmittedArchive ( entry . path , "max_documents" );
unreportedOmittedArchives += selection . unreportedOmitted ;
for ( const entry of selection . selected ) {
2026-09-30 00:41:05 +08:00
try {
2026-10-09 05:09:15 +08:00
const remainingInputBytes =
MAX_AGGREGATE_INPUT_BYTES - aggregateInputBytes ;
if (
entry . declaredSize !== null &&
entry . declaredSize > remainingInputBytes
) {
omitted += 1 ;
recordOmittedArchive ( entry . path , "aggregate_decode_budget_exhausted" );
continue ;
}
attempted += 1 ;
const bytes = await readEntry (
reader ,
entry ,
input . signal ,
remainingInputBytes ,
);
aggregateInputBytes += bytes . length ;
const isNibArchive =
bytes . subarray ( 0 , 10 ). toString ( "ascii" ) === "NIBArchive" ;
let nib : ReturnType < typeof projectNibArchive > | null = null ;
let xmlText : string | undefined ;
2026-10-11 07:02:39 +08:00
let binaryPrototypeKeyObserved = false ;
2026-10-09 05:09:15 +08:00
if ( isNibArchive ) {
const documentBudget = new InterfaceBuilderDecodeBudget (
decodeBudget . remainingBytes ,
);
nib = projectNibArchive (
decodeNibArchive ( bytes , documentBudget ),
documentBudget ,
limits . max_objects ,
);
decodeBudget . reserve (
documentBudget . usedBytes ,
"NIBArchive representation exceeds the aggregate Interface Builder decode budget" ,
);
} else {
if ( bytes . subarray ( 0 , 8 ). toString ( "ascii" ) !== "bplist00" )
xmlText = decodeXmlPlistText ( bytes );
2026-10-11 07:02:39 +08:00
const decodedReservation = preflightPropertyListDecode (
2026-10-09 05:09:15 +08:00
bytes ,
decodeBudget . remainingBytes ,
xmlText ,
);
decodeBudget . reserve (
2026-10-11 07:02:39 +08:00
decodedReservation . estimatedBytes ,
2026-10-09 05:09:15 +08:00
"plist representation exceeds the aggregate Interface Builder decode budget" ,
);
2026-10-11 07:02:39 +08:00
binaryPrototypeKeyObserved =
decodedReservation . binaryPrototypeKeyObserved ;
2026-10-09 05:09:15 +08:00
}
2026-10-07 07:51:37 +00:00
const { value : raw , omittedPrototypeKeys } =
nib === null
2026-10-11 07:02:39 +08:00
? decodePlist ( bytes , xmlText , binaryPrototypeKeyObserved )
2026-10-07 07:51:37 +00:00
: { value : nib.raw , omittedPrototypeKeys : 0 };
2026-10-07 10:33:10 +08:00
if ( nib !== null && nib . omitted > 0 )
incompleteHierarchies . set ( entry . path , nib . omitted );
2026-10-11 07:02:39 +08:00
if ( omittedPrototypeKeys !== 0 )
2026-10-07 07:51:37 +00:00
prototypeKeyOmissions . set ( entry . path , omittedPrototypeKeys );
2026-09-30 00:41:05 +08:00
const documentHash = createHash ( "sha256" ). update ( bytes ). digest ( "hex" );
documents . push ({
relativePath : entry.path ,
archiveSha256 : documentHash ,
documentKind : entry.path.includes ( ".storyboardc/" )
? "storyboard_scene"
: "nib" ,
raw ,
});
} catch ( cause : unknown ) {
if ( input . signal ? . aborted === true ) throw cause ;
2026-10-09 05:09:15 +08:00
if ( cause instanceof InterfaceBuilderDecodeBudgetExceeded ) {
omitted += 1 ;
recordOmittedArchive ( entry . path , cause . reason );
continue ;
}
2026-09-30 00:41:05 +08:00
invalid . push (
` ${ entry . path } : ${ cause instanceof Error ? cause . message : "archive decode failed" } ` ,
);
}
}
} finally {
await reader . close ();
}
const result = buildInterfaceBuilderAnalysis ({
targetSha256 : input.targetSha256 ,
toolVersion : "rea-interface-builder/1" ,
documents ,
limits ,
});
2026-10-07 10:33:10 +08:00
return finalizeHierarchyCoverage ( result , {
incompleteHierarchies ,
2026-10-07 07:51:37 +00:00
prototypeKeyOmissions ,
2026-10-07 10:33:10 +08:00
invalid ,
2026-10-09 05:09:15 +08:00
omittedArchives ,
unreportedOmittedArchives ,
2026-10-07 10:33:10 +08:00
attempted ,
omitted ,
});
};
2026-10-11 00:03:18 +08:00
/** Select lexically earliest document paths independent of host traversal order. */
const selectInterfaceBuilderCandidates = async (
reader : DirectoryArtifactReader ,
signal : AbortSignal | undefined ,
maximumDocuments : number ,
) : Promise < {
readonly selected : readonly ArtifactEntry [];
readonly omittedSamples : readonly ArtifactEntry [];
readonly omitted : number ;
readonly unreportedOmitted : number ;
} > => {
const capacity = maximumDocuments + MAX_REPORTED_OMITTED_ARCHIVES ;
const candidates : ArtifactEntry [] = [];
let total = 0 ;
for await ( const entry of reader . entries ( signal )) {
if ( entry . kind !== "file" || ! isInterfaceBuilderArchive ( entry . path ))
continue ;
total += 1 ;
retainCandidate ( candidates , entry , capacity );
}
const omittedSamples = candidates . slice ( maximumDocuments , capacity );
return {
selected : candidates.slice ( 0 , maximumDocuments ),
omittedSamples ,
omitted : Math.max ( 0 , total - maximumDocuments ),
unreportedOmitted : Math.max (
0 ,
total - maximumDocuments - omittedSamples . length ,
),
};
};
/** Keep the earliest bounded path set; directory iteration order is host-defined. */
const retainCandidate = (
candidates : ArtifactEntry [],
entry : ArtifactEntry ,
maximum : number ,
) : void => {
let low = 0 ;
let high = candidates . length ;
while ( low < high ) {
const middle = Math . floor (( low + high ) / 2 );
const existing = candidates [ middle ];
if (
existing !== undefined &&
compareUnicodeCodePoints ( existing . path , entry . path ) < 0
)
low = middle + 1 ;
else high = middle ;
}
candidates . splice ( low , 0 , entry );
if ( candidates . length > maximum ) candidates . pop ();
};
2026-10-07 10:33:10 +08:00
const finalizeHierarchyCoverage = (
result : ReturnType < typeof buildInterfaceBuilderAnalysis >,
counts : {
incompleteHierarchies : ReadonlyMap < string , number >;
2026-10-11 07:02:39 +08:00
prototypeKeyOmissions : ReadonlyMap < string , number | null >;
2026-10-07 10:33:10 +08:00
invalid : readonly string [];
2026-10-09 05:09:15 +08:00
omittedArchives : readonly { path : string ; reason : string }[];
unreportedOmittedArchives : number ;
2026-10-07 10:33:10 +08:00
attempted : number ;
omitted : number ;
},
) => {
2026-10-09 05:09:15 +08:00
const {
incompleteHierarchies ,
prototypeKeyOmissions ,
invalid ,
omittedArchives ,
unreportedOmittedArchives ,
} = counts ;
2026-10-07 07:51:37 +00:00
const archiveDecodePartial =
2026-10-09 05:09:15 +08:00
invalid . length > 0 ||
prototypeKeyOmissions . size > 0 ||
omittedArchives . length > 0 ;
2026-09-30 00:41:05 +08:00
return {
... result ,
2026-10-07 10:33:10 +08:00
documents : result.documents.map (( document ) => ({
... document ,
hierarchy_complete :
document.hierarchy_complete &&
! incompleteHierarchies . has ( document . relative_path ),
})),
2026-09-30 00:41:05 +08:00
graph : {
... result . graph ,
coverage : [
2026-10-07 10:33:10 +08:00
... result . graph . coverage . map (( facet ) => {
const omitted = incompleteHierarchies . get (
facet . facet . replace ( /^hierarchy:/u, ""),
);
return facet . facet . startsWith ( "hierarchy:" ) && omitted !== undefined
? {
... facet ,
status : "partial" as const ,
reason : "serialized_view_hierarchy_incomplete" ,
omitted : facet.omitted + omitted ,
}
: facet ;
}),
2026-09-30 00:41:05 +08:00
{
facet : "archive_decode" ,
2026-10-07 07:51:37 +00:00
status : archiveDecodePartial
? ( "partial" as const )
: ( "complete" as const ),
2026-10-09 05:09:15 +08:00
reason : omittedArchives.some (
({ reason }) => reason === "aggregate_decode_budget_exhausted" ,
)
? "aggregate_decode_budget_exhausted"
: omittedArchives . some (({ reason }) => reason === "max_documents" )
? "document_limit_reached"
: omittedArchives . length > 0
? "archive_decode_budget_exhausted"
: invalid . length > 0
? "one_or_more_archives_invalid"
: prototypeKeyOmissions . size > 0
? "dictionary_entries_omitted"
: null ,
2026-10-07 10:33:10 +08:00
examined : counts.attempted ,
omitted : counts.omitted ,
2026-09-30 00:41:05 +08:00
},
],
2026-10-07 10:33:10 +08:00
truncated :
result.graph.truncated ||
counts . omitted > 0 ||
2026-10-07 07:51:37 +00:00
archiveDecodePartial ||
2026-10-07 10:33:10 +08:00
incompleteHierarchies . size > 0 ,
2026-09-30 00:41:05 +08:00
},
limitations : [
... result . limitations ,
"Compiled Interface Builder archives are private serialized object graphs. The decoder reports only recognized keyed-archive objects and connections; unrecognized object classes and fields remain unknown." ,
2026-10-07 10:33:10 +08:00
...( incompleteHierarchies . size === 0
? []
: [
"Some serialized hierarchy links could not be projected within the bounded hierarchy; their parentage remains unknown." ,
]),
2026-09-30 00:41:05 +08:00
...( invalid . length === 0
? []
: [
`Some Interface Builder archives could not be decoded: ${ invalid . slice ( 0 , 16 ). join ( "; " ) } ` ,
]),
2026-10-07 07:51:37 +00:00
...[... prototypeKeyOmissions ]. map (
([ path , count ]) => ` ${ path } : ${ omittedPrototypeKeysLimitation ( count ) } ` ,
),
2026-10-09 05:09:15 +08:00
... omittedArchives . map (
({ path , reason }) =>
` ${ path } : omitted because ${ reason . replaceAll ( "_" , " " ) } .` ,
),
...( unreportedOmittedArchives === 0
? []
: [
` ${ unreportedOmittedArchives } additional omitted archive path(s) and their reasons were not retained; reporting is bounded to ${ MAX_REPORTED_OMITTED_ARCHIVES } paths.` ,
]),
2026-09-30 00:41:05 +08:00
],
};
};
/** Project decoded NIB records into the same bounded graph input as ibtool. */
2026-10-09 05:09:15 +08:00
const projectNibArchive = (
archive : NibArchiveDocument ,
budget : InterfaceBuilderDecodeBudget ,
maxHierarchyNodes : number ,
) => {
2026-09-30 00:41:05 +08:00
const byId = new Map ( archive . objects . map (( object ) => [ object . id , object ]));
2026-10-09 05:53:00 +08:00
const normalizedClassNames = new Map < string , string >();
const normalizeClassName = ( value : string ) : string => {
const known = normalizedClassNames . get ( value );
if ( known !== undefined ) return known ;
const normalized = value . replace ( /\0+$/u, "");
normalizedClassNames . set ( value , normalized );
return normalized ;
};
2026-10-09 05:09:15 +08:00
const decodedStrings = new Map < number , string | null >();
const decodedString = (
objectId : number ,
object : NibArchiveDocument [ "objects" ][ number ],
) : string | null => {
if ( decodedStrings . has ( objectId ))
return decodedStrings . get ( objectId ) ?? null ;
const data = object . values [ "NS.bytes" ];
if ( typeof data !== "object" || data === null || Array . isArray ( data )) {
decodedStrings . set ( objectId , null );
return null ;
}
const encoded = data . $nib_data_base64 ;
if ( typeof encoded !== "string" ) {
decodedStrings . set ( objectId , null );
return null ;
}
budget . reserve (
64 + encoded . length * 2 ,
"NIBArchive string projection exceeds the aggregate Interface Builder decode budget" ,
);
const value = Buffer . from ( encoded , "base64" ). toString ( "utf8" );
decodedStrings . set ( objectId , value );
return value ;
};
let projectedFields = 0 ;
for ( const object of archive . objects )
projectedFields += Object . keys ( object . values ). length ;
budget . reserve (
archive . objects . length * 128 + projectedFields * 128 ,
"NIBArchive graph projection exceeds the aggregate Interface Builder decode budget" ,
);
budget . reserve (
archive . classes . length * 96 ,
"NIBArchive class projection exceeds the aggregate Interface Builder decode budget" ,
);
2026-09-30 00:41:05 +08:00
const dereference = ( value : JsonValue | undefined ) : JsonValue | undefined => {
if ( typeof value !== "object" || value === null || Array . isArray ( value ))
return value ;
2026-10-09 05:53:00 +08:00
const encodedData = value . $nib_data_base64 ;
if ( typeof encodedData === "string" ) {
budget . reserveBase64JsonStringOccurrence ( encodedData );
return value ;
}
2026-09-30 00:41:05 +08:00
const ref = value . $nib_object_ref ;
if ( typeof ref !== "number" ) return value ;
const target = byId . get ( ref );
if ( target === undefined ) return undefined ;
2026-10-09 05:53:00 +08:00
if ( normalizeClassName ( target . class_name ) === "NSString" ) {
2026-10-09 05:09:15 +08:00
const string = decodedString ( ref , target );
2026-10-09 05:53:00 +08:00
if ( string !== null ) {
budget . reserveJsonStringOccurrence ( string );
return string ;
}
2026-09-30 00:41:05 +08:00
}
return { objectID : String ( ref ) };
};
const referencedString = ( value : JsonValue | undefined ) : string | null => {
if ( typeof value !== "object" || value === null || Array . isArray ( value ))
return null ;
const ref = value . $nib_object_ref ;
if ( typeof ref !== "number" ) return null ;
const target = byId . get ( ref );
2026-10-09 05:53:00 +08:00
if (
target === undefined ||
normalizeClassName ( target . class_name ) !== "NSString"
)
return null ;
const string = decodedString ( ref , target );
if ( string === null ) return null ;
budget . reserveJsonStringOccurrence ( string );
return string ;
2026-09-30 00:41:05 +08:00
};
2026-10-09 05:53:00 +08:00
const runtimeClasses = new Map < number , string | null >();
2026-09-30 00:41:05 +08:00
const runtimeClass = ( objectId : number ) : string | null => {
2026-10-09 05:53:00 +08:00
if ( runtimeClasses . has ( objectId ))
return runtimeClasses . get ( objectId ) ?? null ;
2026-09-30 00:41:05 +08:00
const object = byId . get ( objectId );
if ( object === undefined ) return null ;
2026-10-09 05:53:00 +08:00
const storedClass = normalizeClassName ( object . class_name );
const resolvedClass =
storedClass === "NSClassSwapper"
? ( referencedString ( object . values . NSClassName ) ?? storedClass )
: storedClass === "UIClassSwapper"
? ( referencedString ( object . values . UIClassName ) ?? storedClass )
: storedClass ;
runtimeClasses . set ( objectId , resolvedClass );
return resolvedClass ;
2026-09-30 00:41:05 +08:00
};
const objects = Object . fromEntries (
archive . objects
. filter (( object ) => {
const name = runtimeClass ( object . id ) ?? "" ;
2026-10-07 08:42:44 +00:00
return ! /^(?:NSObject|NSIBObjectData|NSString|NSMutableString|NSNumber|NSArray|NSMutableArray|NSSet|NSMutableSet|NSDictionary|NSMutableDictionary|NSApplication|NSNib.*Connector|UIRuntime\w*Connection)$/u.test(
2026-09-30 00:41:05 +08:00
name ,
);
})
. map (( object ) => {
const className =
2026-10-09 05:53:00 +08:00
runtimeClass ( object . id ) ?? normalizeClassName ( object . class_name );
budget . reserveJsonStringOccurrence ( className );
let name = className ;
for ( const [ key , value ] of Object . entries ( object . values )) {
if (
! /(?:title|label|identifier|accessibility|name|contents)/iu.test(
key ,
)
)
continue ;
const resolved = dereference ( value );
if ( typeof resolved !== "string" ) continue ;
name = resolved ;
break ;
}
budget . reserveJsonStringOccurrence ( name );
2026-09-30 00:41:05 +08:00
return [
String ( object . id ),
{
customClass : className ,
label : name ,
objectID : String ( object . id ),
nibValues : Object.fromEntries (
2026-10-09 05:53:00 +08:00
Object . entries ( object . values ). map (([ key , value ]) => {
budget . reserveJsonPropertyNameOccurrence ( key );
return [ key , dereference ( value ) ?? null ];
}),
2026-09-30 00:41:05 +08:00
),
},
];
}),
);
const connections : Record < string , JsonValue [] > = {};
2026-10-07 10:33:10 +08:00
const hierarchy : NibHierarchyNode [] = [];
2026-09-30 00:41:05 +08:00
const reference = ( value : JsonValue | undefined ) : number | null => {
if ( typeof value !== "object" || value === null || Array . isArray ( value ))
return null ;
return typeof value . $nib_object_ref === "number"
? value.$nib_object_ref
: null ;
};
for ( const object of archive . objects ) {
2026-10-09 05:53:00 +08:00
const className = normalizeClassName ( object . class_name );
2026-10-07 08:42:44 +00:00
// AppKit connectors use NS-prefixed keys; UIKit's runtime outlet and
// event connections use UI-prefixed keys with the same roles.
const uiKit = /^UIRuntime\w*Connection$/u.test(className);
if ( ! uiKit && ! className . includes ( "Connector" )) continue ;
const values = object . values ;
const source = reference ( uiKit ? values.UISource : values.NSSource );
if ( source === null ) continue ;
const destination = reference (
uiKit ? values.UIDestination : values.NSDestination ,
);
const label = referencedString ( uiKit ? values.UILabel : values.NSLabel );
const type = className . includes ( "Outlet" )
? "outlet"
: className . includes ( "Control" ) || className . includes ( "Event" )
? "action"
: className ;
2026-10-09 05:53:00 +08:00
budget . reserveJsonStringOccurrence ( type );
2026-10-07 08:42:44 +00:00
const eventMask = values . UIEventMask ;
// The source is the outlet owner or the sending control; the destination
// is the outlet value or the action target, and a nil target is the
// first responder.
( connections [ String ( source )] ??= []). push ({
type ,
"destination-id" : destination === null ? null : String ( destination ),
label ,
source_id : String ( source ),
archive_object_id : String ( object . id ),
...( typeof eventMask === "number" ? { ui_event_mask : eventMask } : {}),
});
2026-09-30 00:41:05 +08:00
}
2026-10-07 10:33:10 +08:00
let hierarchyOmitted = 0 ;
2026-10-09 05:09:15 +08:00
let hierarchyNodeCount = 0 ;
2026-09-30 00:41:05 +08:00
const hierarchyFor = (
objectId : number ,
seen : Set < number >,
depth : number ,
2026-10-07 10:33:10 +08:00
) : NibHierarchyNode | null => {
if ( depth > 32 || seen . has ( objectId )) {
hierarchyOmitted += 1 ;
return null ;
}
2026-10-09 05:09:15 +08:00
if ( hierarchyNodeCount >= maxHierarchyNodes ) {
hierarchyOmitted += 1 ;
return null ;
}
if ( ! budget . tryReserve ( 128 )) {
hierarchyOmitted += 1 ;
return null ;
}
2026-09-30 00:41:05 +08:00
const object = byId . get ( objectId );
2026-10-07 10:33:10 +08:00
if ( object === undefined ) {
hierarchyOmitted += 1 ;
return null ;
}
2026-10-09 05:09:15 +08:00
hierarchyNodeCount += 1 ;
2026-09-30 00:41:05 +08:00
const nextSeen = new Set ( seen ). add ( objectId );
2026-10-07 10:33:10 +08:00
const children : NibHierarchyNode [] = [];
2026-09-30 00:41:05 +08:00
for ( const [ key , value ] of Object . entries ( object . values )) {
if ( ! /(?:subviews|contentview|childviewcontrollers|view)$/iu.test(key))
continue ;
const direct = reference ( value );
if ( direct === null ) continue ;
const target = byId . get ( direct );
2026-10-07 10:33:10 +08:00
if ( target === undefined ) {
hierarchyOmitted += 1 ;
continue ;
}
2026-09-30 00:41:05 +08:00
if ( /array|set/iu.test(target.class_name)) {
for ( const candidate of Object . values ( target . values )) {
const child = reference ( candidate );
const nested =
child === null ? null : hierarchyFor ( child , nextSeen , depth + 1 );
if ( nested !== null ) children . push ( nested );
}
} else {
const nested = hierarchyFor ( direct , nextSeen , depth + 1 );
if ( nested !== null ) children . push ( nested );
}
}
return { objectID : String ( objectId ), children };
};
const ibData = archive . objects . find (
2026-10-09 05:53:00 +08:00
({ class_name }) => normalizeClassName ( class_name ) === "NSIBObjectData" ,
2026-09-30 00:41:05 +08:00
);
const rootId = reference ( ibData ? . values . NSRoot );
const hierarchyRoots = rootId === null ? [] : [ rootId ];
for ( const hierarchyRoot of hierarchyRoots ) {
const root = hierarchyFor ( hierarchyRoot , new Set (), 0 );
if ( root !== null ) hierarchy . push ( root );
}
2026-10-07 10:33:10 +08:00
const viewHierarchy = projectNibViewHierarchy (
archive . objects ,
new Set ( Object . keys ( objects ). map ( Number )),
2026-10-09 05:09:15 +08:00
() => budget . tryReserve ( 128 ),
maxHierarchyNodes ,
);
const merged = mergeNibHierarchies (
hierarchy ,
viewHierarchy . hierarchy ?? [],
() => budget . tryReserve ( 96 ),
maxHierarchyNodes ,
2026-10-07 10:33:10 +08:00
);
return {
omitted : viewHierarchy.omitted + hierarchyOmitted + merged . omitted ,
raw : jsonValueSchema.parse ({
"com.apple.ibtool.document.objects" : objects ,
"com.apple.ibtool.document.connections" : connections ,
"com.apple.ibtool.document.hierarchy" : merged . hierarchy ,
"com.apple.ibtool.document.classes" : Object . fromEntries (
2026-10-09 05:53:00 +08:00
archive . classes . map (( name ) => {
const className = normalizeClassName ( name );
budget . reserveJsonPropertyNameOccurrence ( className );
return [ className , {}];
}),
2026-10-07 10:33:10 +08:00
),
}),
};
2026-09-30 00:41:05 +08:00
};
const isInterfaceBuilderArchive = ( path : string ) : boolean => {
const lower = path . toLowerCase ();
return lower . endsWith ( ".nib" ) || lower . endsWith ( "/objects.nib" );
};
const readEntry = async (
reader : DirectoryArtifactReader ,
entry : ArtifactEntry ,
signal? : AbortSignal ,
2026-10-09 05:09:15 +08:00
maxInputBytes = MAX_AGGREGATE_INPUT_BYTES ,
2026-09-30 00:41:05 +08:00
) : Promise < Buffer > => {
2026-10-09 05:09:15 +08:00
if ( entry . declaredSize !== null && entry . declaredSize > maxInputBytes )
throw new InterfaceBuilderDecodeBudgetExceeded (
"aggregate_decode_budget_exhausted" ,
"archive exceeds the aggregate Interface Builder decode budget" ,
2026-09-30 00:41:05 +08:00
);
const stream = await reader . open ( entry , signal );
const chunks : Buffer [] = [];
let length = 0 ;
for await ( const chunk of stream ) {
const bytes = Buffer . isBuffer ( chunk )
? chunk
: Buffer.from ( chunk as Uint8Array );
length += bytes . length ;
2026-10-09 05:09:15 +08:00
if ( length > maxInputBytes ) {
2026-09-30 00:41:05 +08:00
stream . destroy ();
2026-10-09 05:09:15 +08:00
throw new InterfaceBuilderDecodeBudgetExceeded (
"aggregate_decode_budget_exhausted" ,
"archive exceeded the aggregate Interface Builder decode budget" ,
2026-09-30 00:41:05 +08:00
);
}
chunks . push ( bytes );
}
return Buffer . concat ( chunks , length );
};
2026-10-07 17:28:55 +08:00
/** Project decoded plist data and dates while retaining omitted-key coverage. */
2026-10-07 07:51:37 +00:00
const decodePlist = (
bytes : Buffer ,
2026-10-11 07:02:39 +08:00
xmlText : string | undefined ,
binaryPrototypeKeyObserved : boolean ,
) : {
readonly value : JsonValue ;
readonly omittedPrototypeKeys : number | null ;
} => {
const binary = bytes . subarray ( 0 , 8 ). toString ( "ascii" ) === "bplist00" ;
const { value , omittedPrototypeKeys } = binary
? {
value : parseBinary ( bytes ),
omittedPrototypeKeys : binaryPrototypeKeyObserved ? null : 0 ,
}
: parseXmlPropertyList ( xmlText ?? decodeXmlPlistText ( bytes ));
2026-10-07 17:28:55 +08:00
return { value : projectPlistValue ( value ). value , omittedPrototypeKeys };
2026-09-30 00:41:05 +08:00
};