diff --git a/mobile/src/mobile-web-shell/mobile-web-shell-session-contract.ts b/mobile/src/mobile-web-shell/mobile-web-shell-session-contract.ts index 1a6f608e8c2..656adbf6b78 100644 --- a/mobile/src/mobile-web-shell/mobile-web-shell-session-contract.ts +++ b/mobile/src/mobile-web-shell/mobile-web-shell-session-contract.ts @@ -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. diff --git a/mobile/src/mobile-web-shell/mobile-web-shell-session-effects.ts b/mobile/src/mobile-web-shell/mobile-web-shell-session-effects.ts index c5995d21023..b1b413b9886 100644 --- a/mobile/src/mobile-web-shell/mobile-web-shell-session-effects.ts +++ b/mobile/src/mobile-web-shell/mobile-web-shell-session-effects.ts @@ -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 } diff --git a/mobile/src/transport/web-bundle/mobile-web-bundle-brotli-fetch.test.ts b/mobile/src/transport/web-bundle/mobile-web-bundle-brotli-fetch.test.ts index d88a73b0ad5..2df9a40a4e7 100644 --- a/mobile/src/transport/web-bundle/mobile-web-bundle-brotli-fetch.test.ts +++ b/mobile/src/transport/web-bundle/mobile-web-bundle-brotli-fetch.test.ts @@ -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 }) }) }) diff --git a/mobile/src/transport/web-bundle/mobile-web-bundle-fetch.ts b/mobile/src/transport/web-bundle/mobile-web-bundle-fetch.ts index abd4440ef64..e27bef83ddb 100644 --- a/mobile/src/transport/web-bundle/mobile-web-bundle-fetch.ts +++ b/mobile/src/transport/web-bundle/mobile-web-bundle-fetch.ts @@ -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 diff --git a/mobile/src/transport/web-bundle/mobile-web-bundle-window-reader.ts b/mobile/src/transport/web-bundle/mobile-web-bundle-window-reader.ts index 7a27848b680..9ed37e44bdd 100644 --- a/mobile/src/transport/web-bundle/mobile-web-bundle-window-reader.ts +++ b/mobile/src/transport/web-bundle/mobile-web-bundle-window-reader.ts @@ -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 +): ReadonlyMap { + 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 +): 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,