+ ],
+
+ hooks: {
+ beforeRequest: [
+ options => {
+ const headers = options.headers || {}
+ headers['host'] = options.url.host
+ },
+
+ options => {
+ const httpSignatureOptions = options.context?.httpSignature
+
+ if (httpSignatureOptions) {
+ const method = options.method ?? 'GET'
+ const path = options.path ?? options.url.pathname
+
+ if (!method || !path) {
+ throw new Error(`Cannot sign request without method (${method}) or path (${path}) ${options}`)
+ }
+
+ httpSignature.signRequest({
+ getHeader: function (header: string) {
+ const value = options.headers[header.toLowerCase()]
+
+ if (!value) logger.warn('Unknown header requested by http-signature.', { headers: options.headers, header })
+ return value
+ },
+
+ setHeader: function (header: string, value: string) {
+ options.headers[header] = value
+ },
+
+ method,
+ path
+ }, httpSignatureOptions)
+ }
+ }
+ ],
+
+ beforeRetry: [
+ (_options: NormalizedOptions, error: RequestError, retryCount: number) => {
+ logger.debug('Retrying request to %s.', error.request.requestUrl, { retryCount, error: buildRequestError(error), ...lTags() })
+ }
+ ]
+ }
+})
+
+function doRequest (url: string, options: PeerTubeRequestOptions = {}) {
+ const gotOptions = buildGotOptions(options)
+
+ return peertubeGot(url, gotOptions)
+ .catch(err => { throw buildRequestError(err) })
+}
+
+function doJSONRequest <T> (url: string, options: PeerTubeRequestOptions = {}) {
+ const gotOptions = buildGotOptions(options)
+
+ return peertubeGot<T>(url, { ...gotOptions, responseType: 'json' })
+ .catch(err => { throw buildRequestError(err) })
+}
+
+async function doRequestAndSaveToFile (
+ url: string,
+ destPath: string,
+ options: PeerTubeRequestOptions = {}
+) {
+ const gotOptions = buildGotOptions({ ...options, timeout: options.timeout ?? REQUEST_TIMEOUTS.FILE })
+
+ const outFile = createWriteStream(destPath)
+
+ try {
+ await pipelinePromise(
+ peertubeGot.stream(url, gotOptions),
+ outFile
+ )
+ } catch (err) {
+ remove(destPath)
+ .catch(err => logger.error('Cannot remove %s after request failure.', destPath, { err, ...lTags() }))
+
+ throw buildRequestError(err)
+ }
+}
+
+function getAgent () {
+ if (!isProxyEnabled()) return {}
+
+ const proxy = getProxy()
+
+ logger.info('Using proxy %s.', proxy, lTags())
+
+ const proxyAgentOptions = {
+ keepAlive: true,
+ keepAliveMsecs: 1000,
+ maxSockets: 256,
+ maxFreeSockets: 256,
+ scheduling: 'lifo' as 'lifo',
+ proxy