vediksagar
← writing

· 3 min read · #cloudflare #http #android

A 186 MB APK through a 25 MiB door

Cloudflare Pages refuses any single file over 25 MiB. My Android app is about 186 MB. Here is the small Pages Function that splits it into parts and streams it back as one file, with resumable downloads.

PocketDesk, the Android app I use to drive my laptop from my phone, updates itself from a signed feed. Both the feed and the APK are served from a static site on Cloudflare Pages, and that is where I ran into a hard rule: Pages will not host a single file larger than 25 MiB.

The current release is 185,983,457 bytes. I did not want a second storage service just for one file, so the APK goes up in pieces and a tiny function glues it back together on the way out.

Split at build time

The deploy script cuts the APK into fixed 20 MiB parts (20,971,520 bytes each), safely under the cap. The latest release becomes nine parts, p00.bin to p08.bin, stored under a folder named after the file’s SHA-256. Content-addressed names mean a new release never overwrites an old one, and every part can be cached forever.

The build writes a small table into the function so it knows how to reassemble each file:

const BLOBS = {
  "<sha256>.apk": {
    dir: "<sha256>",
    parts: ["p00.bin", "p01.bin", /* … */ "p08.bin"],
    total: 185983457,
    part: 20971520,
    filename: "PocketDesk-1.6.68.apk",
  },
};

Stream the parts back as one file

A Pages Function sits at /app-updates/blobs/[name]. For a plain GET it fetches each part from the site’s own static assets and pipes it into one response body, in order:

const out = new FixedLengthStream(length);
for (let i = first; i <= last; i++) {
  const r = await env.ASSETS.fetch(partUrl(i));
  await r.body.pipeTo(out.writable, { preventClose: true });
}
await out.writable.close();

Two details matter here.

FixedLengthStream. The response declares an exact Content-Length up front. Without it the download would go out chunked, and a client could not show progress or tell a truncated download from a complete one.

preventClose: true. Each pipeTo would normally close the destination when its source ends. Keeping it open lets the next part continue into the same stream; the function closes it once, after the last part.

The copying runs inside waitUntil(...), so the response can start flowing to the phone immediately while the function keeps feeding it.

Resumable downloads with Range

Phones lose signal. The app’s updater resumes an interrupted download by sending a Range header and then checks the Content-Range of the reply exactly, so the function has to get Range right:

  • bytes=a- and bytes=a-b are accepted and answered with 206 Partial Content and Content-Range: bytes a-b/total.
  • Anything malformed, or starting past the end, gets 416 with Content-Range: bytes */total.
  • HEAD returns the same headers with no body, so the client can learn the size first.

A range rarely lines up with the 20 MiB boundaries, so the function works out which parts the range touches. Whole parts are piped straight through. For the first and last parts it asks the asset server for just the needed sub-range; if the reply is not the exact 206 it asked for, it fetches the whole part and cuts out the right bytes itself:

const s = Math.max(from - pos, 0);
const e = Math.min(to + 1 - pos, value.length);
if (e > s) await writer.write(value.subarray(s, e));

That fallback means correctness never depends on how the static asset server treats Range internally.

The headers that ship with it

Content-Type: application/vnd.android.package-archive
Content-Disposition: attachment; filename="PocketDesk-1.6.68.apk"
Accept-Ranges: bytes
Cache-Control: public, max-age=31536000, immutable
X-Robots-Tag: noindex, nofollow

immutable is safe only because the URL contains the file’s hash: a different APK always gets a different URL.

What I would tell myself before starting

  • Read the platform’s limits page before designing the deploy. The 25 MiB cap is per file and does not move.
  • Content-addressed paths make caching trivial and releases impossible to clobber.
  • If a client checks Content-Range strictly, test every edge: open-ended ranges, a range inside one part, a range across a boundary, and a range starting past the end.