fix: zip resume not working with unknown size

Massimo Melina committed Jan 4, 2023 at 16:08 UTC 469e6d9a77d71a4941b3ea638e9e31dbc7f86413
2 files changed +9 -5
src/serveFile.ts
+4 -3
@@ -89,15 +89,16 @@ export function getRange(ctx: Koa.Context, totalSize: number) {
89 return ctx.throw(400, 'bad range')
90 const max = totalSize - 1
91 const start = bytes[0] ? Number(bytes[0]) : Math.max(0, totalSize-Number(bytes[1])) // a negative start is relative to the end
92 - const end = bytes[0] ? Number(bytes[1] || max) : max // NaN in case we are asked for last N bytes without knowing max
93 - if (isNaN(end) || end > max || start > max) {
92 + const end = bytes[0] ? Number(bytes[1] || max) : max
93 + // we don't support last-bytes without knowing max
94 + if (isNaN(end) && isNaN(max) || end > max || start > max) {
95 ctx.status = 416
96 ctx.set('Content-Range', `bytes ${totalSize}`)
97 ctx.body = 'Requested Range Not Satisfiable'
98 return
99 }
100 ctx.status = 206
100 - ctx.set('Content-Range', `bytes ${start}-${end}/${isNaN(totalSize) ? '*' : totalSize}`)
101 + ctx.set('Content-Range', `bytes ${start}-${isNaN(end) ? '' : end}/${isNaN(totalSize) ? '*' : totalSize}`)
102 ctx.response.length = end - start + 1
103 return { start, end }
104 }
src/zip.ts
+5 -2
@@ -54,8 +54,11 @@ export async function zipStreamFromFolder(node: VfsNode, ctx: Koa.Context) {
54 })
55 const zip = new QuickZipStream(mappedWalker)
56 const time = 1000 * zipSeconds.get()
57 - ctx.response.length = await zip.calculateSize(time)
58 - const range = getRange(ctx, ctx.response.length)
57 + const size = await zip.calculateSize(time)
58 + ctx.response.length = size
59 + const range = getRange(ctx, size) // keep var size as ctx.response.length won't preserve a NaN
60 + if (ctx.status >= 400)
61 + return
62 if (range)
63 zip.applyRange(range.start, range.end)
64 ctx.body = zip