X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Frequests.ts;h=36e69458e533064a584e93a061d6361b57dc4db6;hb=a9c58393d36d221197b48884a1960e6126ab31d7;hp=aee8f66736e956bc677ca2991ac0bfd79c2db3ff;hpb=b329abc2f00628f8c6814d304df6faad301d216c;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts index aee8f6673..36e69458e 100644 --- a/server/helpers/requests.ts +++ b/server/helpers/requests.ts @@ -1,12 +1,17 @@ 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' +import { ACTIVITY_PUB, PEERTUBE_VERSION, REQUEST_TIMEOUT, WEBSERVER } from '../initializers/constants' 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 = { @@ -19,6 +24,7 @@ type PeerTubeRequestOptions = { key: string headers: string[] } + timeout?: number jsonResponse?: boolean } & Pick @@ -87,6 +93,10 @@ const peertubeGot = got.extend({ path }, httpSignatureOptions) } + }, + + (options: GotOptions) => { + options.timeout = REQUEST_TIMEOUT } ] } @@ -165,29 +175,34 @@ 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 } } return { method: options.method, + dnsCache: true, json: options.json, searchParams: options.searchParams, + timeout: options.timeout ?? REQUEST_TIMEOUT, headers, context } } -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 error + return newError }