IGPAPI
    Preparing search index...

    Function mergeJsonStreamChunks

    • Merge newline-delimited feed chunks into the single payload the app sees.

      A naive "concatenate every array, last wins for scalars" is wrong: the server does not only append. From the four multi-document bodies of the fresh 448 capture (feed/reels_tray/ seq 50, feed/timeline/ seq 52, clips/discover/ stream/ seq 163; json-stream.test.ts under private/ replays them):

      • Paged continuations (tray 8 + 42, feed_items 0 + 2 + 4, items) hold disjoint elements and are concatenated.
      • remaining_reel_ids_to_fetch is byte-identical (89) in both reels_tray chunks; concatenating it yields a wrong 178. An array the server restates must survive once.
      • client_gap_enforcer_matrix is [[]] in the timeline shell and a real [{list:[1,1]},{list:[1,1]}] in the last chunk; the shell value is a placeholder and must not leak into the merge.
      • items_with_ads in clips is [lightweight media A], then [full media A] (same media.id), then [B, C, D]; the app shows A once, using the fuller copy, so a restated element replaces the earlier one rather than appending.

      Rule, keyed on structure (no endpoint-specific key list):

      1. A non-array key takes the value of the last chunk that states it (a chunk that omits the key leaves the running value alone).
      2. An array key is merged across the chunks that state it: an element whose identity was already seen in an earlier chunk replaces that occurrence (keeping the richer, later copy at its first position); otherwise it is appended. Elements are identified by id/pk/strong_id__ (their own, or their media's), else by their canonical value.
      3. A chunk marked is_shell_response: true contributes an array key only if no later chunk states that key; its arrays are treated as placeholders.

      A multi-document body that carries no object (e.g. two top-level arrays) is concatenated when it is all arrays and rejected otherwise, so it never silently collapses to {}.

      Parameters

      • documents: readonly unknown[]

      Returns unknown