| 126 | 126 | | * file's ETag gets a `206` with the rest, as registry.npmjs.org answers |
| 127 | 127 | | * one, unless `ranges` is false. |
| 128 | 128 | | */ |
| 129 | | − | function rangedUpstream(cuts: (number | null)[], options: { ranges?: boolean } = {}) { |
| 129 | + | function rangedUpstream(cuts: (number | null)[], options: { ranges?: boolean; encodedRest?: boolean } = {}) { |
| 130 | 130 | | const requests: Request[] = []; |
| 131 | 131 | | const send = async (request: Request) => { |
| 132 | 132 | | requests.push(request); |
| ⋯ |
| 137 | 137 | | const rest = file.slice(start); |
| 138 | 138 | | return new Response(breaking(rest, cut === null ? null : cut - start), { |
| 139 | 139 | | status: 206, |
| 140 | | − | headers: { "content-length": String(rest.length), "content-range": `bytes ${start}-${LARGE - 1}/${LARGE}`, "accept-ranges": "bytes", etag: ETAG }, |
| 140 | + | headers: { "content-length": String(rest.length), "content-range": `bytes ${start}-${LARGE - 1}/${LARGE}`, "accept-ranges": "bytes", etag: ETAG, ...(options.encodedRest ? { "content-encoding": "gzip" } : {}) }, |
| 141 | 141 | | }); |
| 142 | 142 | | } |
| 143 | 143 | | return new Response(breaking(file, cut), { |
| ⋯ |
| 168 | 168 | | assert.deepEqual(waits, [...RETRY_WAITS_MS]); |
| 169 | 169 | | }); |
| 170 | 170 | | |
| 171 | + | test("a rest that comes compressed is never joined on to a body that came as it is", async () => { |
| 172 | + | const { requests, send } = rangedUpstream([3_000_000, null], { encodedRest: true }); |
| 173 | + | const response = await forward(tarball(), send, sleep); |
| 174 | + | await assert.rejects(response.arrayBuffer()); |
| 175 | + | assert.equal(requests.length, 2); |
| 176 | + | }); |
| 177 | + | |
| 171 | 178 | | test("a large tarball sent whole streams as it is, asked for once", async () => { |
| 172 | 179 | | const { requests, send } = rangedUpstream([null]); |
| 173 | 180 | | const response = await forward(tarball(), send, sleep); |