1 import { createWriteStream, remove } from 'fs-extra'
2 import got, { CancelableRequest, Options as GotOptions, RequestError } from 'got'
3 import { join } from 'path'
4 import { CONFIG } from '../initializers/config'
5 import { ACTIVITY_PUB, PEERTUBE_VERSION, REQUEST_TIMEOUT, WEBSERVER } from '../initializers/constants'
6 import { pipelinePromise } from './core-utils'
7 import { processImage } from './image-utils'
8 import { logger } from './logger'
10 export interface PeerTubeRequestError extends Error {
15 const httpSignature = require('http-signature')
17 type PeerTubeRequestOptions = {
19 bodyKBLimit?: number // 1MB
22 authorizationHeaderName: string
28 jsonResponse?: boolean
29 } & Pick<GotOptions, 'headers' | 'json' | 'method' | 'searchParams'>
31 const peertubeGot = got.extend({
33 'user-agent': getUserAgent()
38 const promiseOrStream = next(options) as CancelableRequest<any>
39 const bodyKBLimit = options.context?.bodyKBLimit as number
40 if (!bodyKBLimit) throw new Error('No KB limit for this request')
42 const bodyLimit = bodyKBLimit * 1000
44 /* eslint-disable @typescript-eslint/no-floating-promises */
45 promiseOrStream.on('downloadProgress', progress => {
46 if (progress.transferred > bodyLimit && progress.percent !== 1) {
47 const message = `Exceeded the download limit of ${bodyLimit} B`
51 if (promiseOrStream.cancel) {
52 promiseOrStream.cancel()
57 (promiseOrStream as any).destroy()
61 return promiseOrStream
68 const headers = options.headers || {}
69 headers['host'] = options.url.host
73 const httpSignatureOptions = options.context?.httpSignature
75 if (httpSignatureOptions) {
76 const method = options.method ?? 'GET'
77 const path = options.path ?? options.url.pathname
79 if (!method || !path) {
80 throw new Error(`Cannot sign request without method (${method}) or path (${path}) ${options}`)
83 httpSignature.signRequest({
84 getHeader: function (header) {
85 return options.headers[header]
88 setHeader: function (header, value) {
89 options.headers[header] = value
94 }, httpSignatureOptions)
98 (options: GotOptions) => {
99 options.timeout = REQUEST_TIMEOUT
105 function doRequest (url: string, options: PeerTubeRequestOptions = {}) {
106 const gotOptions = buildGotOptions(options)
108 return peertubeGot(url, gotOptions)
109 .catch(err => { throw buildRequestError(err) })
112 function doJSONRequest <T> (url: string, options: PeerTubeRequestOptions = {}) {
113 const gotOptions = buildGotOptions(options)
115 return peertubeGot<T>(url, { ...gotOptions, responseType: 'json' })
116 .catch(err => { throw buildRequestError(err) })
119 async function doRequestAndSaveToFile (
122 options: PeerTubeRequestOptions = {}
124 const gotOptions = buildGotOptions(options)
126 const outFile = createWriteStream(destPath)
129 await pipelinePromise(
130 peertubeGot.stream(url, gotOptions),
135 .catch(err => logger.error('Cannot remove %s after request failure.', destPath, { err }))
137 throw buildRequestError(err)
141 async function downloadImage (url: string, destDir: string, destName: string, size: { width: number, height: number }) {
142 const tmpPath = join(CONFIG.STORAGE.TMP_DIR, 'pending-' + destName)
143 await doRequestAndSaveToFile(url, tmpPath)
145 const destPath = join(destDir, destName)
148 await processImage(tmpPath, destPath, size)
150 await remove(tmpPath)
156 function getUserAgent () {
157 return `PeerTube/${PEERTUBE_VERSION} (+${WEBSERVER.URL})`
160 // ---------------------------------------------------------------------------
165 doRequestAndSaveToFile,
169 // ---------------------------------------------------------------------------
171 function buildGotOptions (options: PeerTubeRequestOptions) {
172 const { activityPub, bodyKBLimit = 1000 } = options
174 const context = { bodyKBLimit, httpSignature: options.httpSignature }
176 let headers = options.headers || {}
179 headers = { ...headers, date: new Date().toUTCString() }
182 if (activityPub && !headers.accept) {
183 headers = { ...headers, accept: ACTIVITY_PUB.ACCEPT_HEADER }
187 method: options.method,
190 searchParams: options.searchParams,
191 timeout: options.timeout ?? REQUEST_TIMEOUT,
197 function buildRequestError (error: RequestError) {
198 const newError: PeerTubeRequestError = new Error(error.message)
199 newError.name = error.name
200 newError.stack = error.stack
202 if (error.response) {
203 newError.responseBody = error.response.body
204 newError.statusCode = error.response.statusCode