diff options
author | Chocobozzz <me@florianbigard.com> | 2021-03-09 09:58:08 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-03-24 18:18:40 +0100 |
commit | b329abc2f00628f8c6814d304df6faad301d216c (patch) | |
tree | 733baa9746c0cc8497f66f71e14e7478a773ae88 /server/helpers | |
parent | 18b24b2dc5a21007f62b2e625d50b1f4b7dec784 (diff) | |
download | PeerTube-b329abc2f00628f8c6814d304df6faad301d216c.tar.gz PeerTube-b329abc2f00628f8c6814d304df6faad301d216c.tar.zst PeerTube-b329abc2f00628f8c6814d304df6faad301d216c.zip |
Fix request body limit
Diffstat (limited to 'server/helpers')
-rw-r--r-- | server/helpers/requests.ts | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts index 2c9da213c..aee8f6673 100644 --- a/server/helpers/requests.ts +++ b/server/helpers/requests.ts | |||
@@ -30,13 +30,25 @@ const peertubeGot = got.extend({ | |||
30 | handlers: [ | 30 | handlers: [ |
31 | (options, next) => { | 31 | (options, next) => { |
32 | const promiseOrStream = next(options) as CancelableRequest<any> | 32 | const promiseOrStream = next(options) as CancelableRequest<any> |
33 | const bodyKBLimit = options.context?.bodyKBLimit | 33 | const bodyKBLimit = options.context?.bodyKBLimit as number |
34 | if (!bodyKBLimit) throw new Error('No KB limit for this request') | 34 | if (!bodyKBLimit) throw new Error('No KB limit for this request') |
35 | 35 | ||
36 | const bodyLimit = bodyKBLimit * 1000 | ||
37 | |||
36 | /* eslint-disable @typescript-eslint/no-floating-promises */ | 38 | /* eslint-disable @typescript-eslint/no-floating-promises */ |
37 | promiseOrStream.on('downloadProgress', progress => { | 39 | promiseOrStream.on('downloadProgress', progress => { |
38 | if (progress.transferred * 1000 > bodyKBLimit && progress.percent !== 1) { | 40 | if (progress.transferred > bodyLimit && progress.percent !== 1) { |
39 | promiseOrStream.cancel(`Exceeded the download limit of ${bodyKBLimit} bytes`) | 41 | const message = `Exceeded the download limit of ${bodyLimit} B` |
42 | logger.warn(message) | ||
43 | |||
44 | // CancelableRequest | ||
45 | if (promiseOrStream.cancel) { | ||
46 | promiseOrStream.cancel() | ||
47 | return | ||
48 | } | ||
49 | |||
50 | // Stream | ||
51 | (promiseOrStream as any).destroy() | ||
40 | } | 52 | } |
41 | }) | 53 | }) |
42 | 54 | ||
@@ -177,5 +189,5 @@ function buildRequestError (error: any) { | |||
177 | error.responseBody = error.response.body | 189 | error.responseBody = error.response.body |
178 | } | 190 | } |
179 | 191 | ||
180 | return newError | 192 | return error |
181 | } | 193 | } |