Chunked

Patch made by Jack Marr for https://bugzilla.mozilla.org/show_bug.cgi?id=1004547.
Also factored out chunking pattern