diff options
Diffstat (limited to 'server/helpers/requests.ts')
-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 | } |