X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Frequests.ts;h=fd2a56f30c717e6ce892b1fd90deca6af228a327;hb=ca4b4b2e5590c1b37cff1fe1be7f797b93351229;hp=2c9da213c112068a666c431ad50d7e586210f5bc;hpb=db4b15f21fbf4e33434e930ffc7fb768cdcf9d42;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts index 2c9da213c..fd2a56f30 100644 --- a/server/helpers/requests.ts +++ b/server/helpers/requests.ts @@ -1,5 +1,5 @@ import { createWriteStream, remove } from 'fs-extra' -import got, { CancelableRequest, Options as GotOptions } from 'got' +import got, { CancelableRequest, Options as GotOptions, RequestError } from 'got' import { join } from 'path' import { CONFIG } from '../initializers/config' import { ACTIVITY_PUB, PEERTUBE_VERSION, WEBSERVER } from '../initializers/constants' @@ -7,6 +7,11 @@ import { pipelinePromise } from './core-utils' import { processImage } from './image-utils' import { logger } from './logger' +export interface PeerTubeRequestError extends Error { + statusCode?: number + responseBody?: any +} + const httpSignature = require('http-signature') type PeerTubeRequestOptions = { @@ -30,13 +35,25 @@ const peertubeGot = got.extend({ handlers: [ (options, next) => { const promiseOrStream = next(options) as CancelableRequest - const bodyKBLimit = options.context?.bodyKBLimit + const bodyKBLimit = options.context?.bodyKBLimit as number if (!bodyKBLimit) throw new Error('No KB limit for this request') + const bodyLimit = bodyKBLimit * 1000 + /* eslint-disable @typescript-eslint/no-floating-promises */ promiseOrStream.on('downloadProgress', progress => { - if (progress.transferred * 1000 > bodyKBLimit && progress.percent !== 1) { - promiseOrStream.cancel(`Exceeded the download limit of ${bodyKBLimit} bytes`) + if (progress.transferred > bodyLimit && progress.percent !== 1) { + const message = `Exceeded the download limit of ${bodyLimit} B` + logger.warn(message) + + // CancelableRequest + if (promiseOrStream.cancel) { + promiseOrStream.cancel() + return + } + + // Stream + (promiseOrStream as any).destroy() } }) @@ -153,9 +170,11 @@ function buildGotOptions (options: PeerTubeRequestOptions) { let headers = options.headers || {} - headers = { ...headers, date: new Date().toUTCString() } + if (!headers.date) { + headers = { ...headers, date: new Date().toUTCString() } + } - if (activityPub) { + if (activityPub && !headers.accept) { headers = { ...headers, accept: ACTIVITY_PUB.ACCEPT_HEADER } } @@ -168,13 +187,14 @@ function buildGotOptions (options: PeerTubeRequestOptions) { } } -function buildRequestError (error: any) { - const newError = new Error(error.message) +function buildRequestError (error: RequestError) { + const newError: PeerTubeRequestError = new Error(error.message) newError.name = error.name newError.stack = error.stack - if (error.response?.body) { - error.responseBody = error.response.body + if (error.response) { + newError.responseBody = error.response.body + newError.statusCode = error.response.statusCode } return newError