fix(mobile): show the bytes that cross the wire on the download screen

Download progress counted inflated asset bytes, so the caption read
"x of 12.9 MB" while about 4.3 MB of brotli copies travelled. Progress now
counts what the reader moves: each brotli copy where one is read, raw bytes
otherwise, and nothing for reused assets. The first total before any
progress uses the same rule.
This commit is contained in:
Kelvin Amoaba
2026-11-10 02:31:32 +00:00
parent 75d7ecd3ac
commit 44fefefb8b
5 changed files with 38 additions and 27 deletions
@@ -25,6 +25,7 @@ export type MobileWebShellGates = {
export type MobileWebShellManifestFacts = MobileWebBundleCompatManifest & {
readonly buildId: string
// A download with nothing to reuse, in fetch progress's unit (each brotli copy where read, else asset bytes).
readonly totalBytes: number
readonly totalAssets: number
// Older desktops omit routes and serve none.
@@ -1,4 +1,5 @@
import { fetchMobileWebBundle } from '../transport/web-bundle/mobile-web-bundle-fetch'
import { mobileWebBundleStreamBytes } from '../transport/web-bundle/mobile-web-bundle-window-reader'
import { mobileWebBundleManifestRead } from '../transport/web-bundle/mobile-web-bundle-operations'
import type {
MobileWebBundleManifestRead,
@@ -87,7 +88,7 @@ export async function readManifest(
manifest: {
...compatOf(manifest),
buildId: manifest.buildId,
totalBytes: manifest.totalBytes,
totalBytes: mobileWebBundleStreamBytes(opened),
totalAssets: manifest.assets.length,
routes: manifest.routes
}
@@ -96,7 +96,7 @@ function brotliHost() {
}
describe('fetchMobileWebBundle from a host that ships brotli copies', () => {
it('reads the copy where the manifest lists one, inflates it, and reports progress in asset bytes', async () => {
it('reads the copy where the manifest lists one, inflates it, and reports progress in the bytes it moves', async () => {
const host = brotliHost()
const progress: MobileWebBundleFetchProgress[] = []
@@ -111,6 +111,7 @@ describe('fetchMobileWebBundle from a host that ships brotli copies', () => {
3
)
expect(host.methods.filter((method) => method === 'mobileWeb.bundle.range')).toHaveLength(1)
expect(progress.at(-1)).toMatchObject({ receivedBytes: 1332, totalBytes: 1332 })
// The 46-byte copy plus the 12-byte mark, not the 1,332 bytes they inflate to.
expect(progress.at(-1)).toMatchObject({ receivedBytes: 58, totalBytes: 58 })
})
})
@@ -42,6 +42,7 @@ export const MOBILE_WEB_BUNDLE_WINDOW_STALL_MS = 30_000
const WINDOW_READ_BACKSTOP_MS =
(MAX_CONCURRENT_WINDOW_READS + 1) * MOBILE_WEB_BUNDLE_WINDOW_STALL_MS
// Bytes are what the reader moves (a brotli copy where it reads one, else the asset); reused assets add none.
export type MobileWebBundleFetchProgress = {
readonly completedAssets: number
readonly totalAssets: number
@@ -180,7 +181,7 @@ export async function fetchMobileWebBundle(args: {
downloadedAssets += 1
}
// Per window, not per asset: the largest asset goes first, so asset completions bunch at the end.
receivedBytes += assetBytesCredited(asset, offset, bytes.byteLength)
receivedBytes += bytes.byteLength
reportProgress()
}
@@ -246,7 +247,7 @@ export async function fetchMobileWebBundle(args: {
const countDownload = (entries: readonly MobileWebBundleAssetRead[]): void => {
downloadAssets += entries.length
downloadBytes += entries.reduce((sum, entry) => sum + entry.byteLength, 0)
downloadBytes += entries.reduce((sum, entry) => sum + reader.streamLength(entry), 0)
}
try {
@@ -297,18 +298,6 @@ function planWindowReads(
})
}
// Progress stays in asset bytes, the unit of the session's total: a window of a brotli copy counts for its
// share of the asset, and an asset's windows add up to exactly its byteLength.
function assetBytesCredited(asset: AssetReassembly, offset: number, byteLength: number): number {
const streamBytes = asset.stream.byteLength
if (streamBytes === asset.entry.byteLength) {
return byteLength
}
const at = (position: number): number =>
Math.floor((asset.entry.byteLength * position) / streamBytes)
return at(offset + byteLength) - at(offset)
}
async function verifyReassembledAsset(
asset: AssetReassembly,
reader: MobileWebBundleWindowReader
@@ -66,16 +66,7 @@ export function mobileWebBundleWindowReader(
}
}
const native = pairedE2EENativeCodec()
// Hermes has no brotli, so without the native codec every asset stays on gzip ranges.
const brotliLengths = new Map(
native === null
? []
: opened.manifest.assets.flatMap((asset) =>
asset.brotliByteLength !== undefined && asset.brotliByteLength < asset.byteLength
? [[asset.path, asset.brotliByteLength] as const]
: []
)
)
const brotliLengths = brotliStreamLengths(opened)
return {
windowBytes: rangeBytes,
streamLength: (asset) => brotliLengths.get(asset.path) ?? asset.byteLength,
@@ -108,6 +99,34 @@ export function mobileWebBundleWindowReader(
}
}
// The assets a reader fetches as their brotli copy, by path, with that copy's length. Hermes has no brotli,
// so without the native codec, and on a host with no range grid, every asset stays on gzip ranges or chunks.
function brotliStreamLengths(
opened: Pick<MobileWebBundleManifestReply, 'rangeBytes' | 'manifest'>
): ReadonlyMap<string, number> {
if (opened.rangeBytes === undefined || pairedE2EENativeCodec() === null) {
return new Map()
}
return new Map(
opened.manifest.assets.flatMap((asset) =>
asset.brotliByteLength !== undefined && asset.brotliByteLength < asset.byteLength
? [[asset.path, asset.brotliByteLength] as const]
: []
)
)
}
// Bytes a whole download moves, in the unit of fetch progress: each brotli copy's length where one is read.
export function mobileWebBundleStreamBytes(
opened: Pick<MobileWebBundleManifestReply, 'rangeBytes' | 'manifest'>
): number {
const brotliLengths = brotliStreamLengths(opened)
return opened.manifest.assets.reduce(
(sum, asset) => sum + (brotliLengths.get(asset.path) ?? asset.byteLength),
0
)
}
// Chunks and brotli windows carry their bytes as they are.
function rawWindowReply({
dataBase64,