fix: content-length was lost with plugins sending Buffer (like thumbnails)
Massimo Melina committed
Apr 28, 2024 at 10:52 UTC
54956c7b3f4d7993df2b2916e9f74eb189bc9286
1 file changed
+3
-3
src/throttler.ts
+3
-3
@@ -28,6 +28,7 @@ const maxKbpsPerIp = defineConfig('max_kbps_per_ip', Infinity)
28
export const throttler: Koa.Middleware = async (ctx, next) => {
29
await next()
30
let { body } = ctx
31
+ const downloadTotal: number = ctx.response.length
32
if (typeof body === 'string' || body && body instanceof Buffer)
33
ctx.body = body = Readable.from(body)
34
if (!body || !(body instanceof Readable))
@@ -75,12 +76,11 @@ export const throttler: Koa.Middleware = async (ctx, next) => {
76
delete ip2group[ctx.ip]
77
})
78
78
- const downloadTotal: number = ctx.response.length
79
ctx.state.originalStream = body
80
ctx.body = body.pipe(ts)
81
82
- if (downloadTotal) // preserve this info
83
- ctx.response.length = downloadTotal
82
+ if (downloadTotal !== undefined) // undefined will break SSE
83
+ ctx.response.length = downloadTotal // preserve this info
84
ts.once('close', () => // in case of compressed response, we offer calculation of real size
85
ctx.state.length = ts.getBytesSent() - offset)
86
}