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, 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
27 jsonResponse?: boolean
28 } & Pick<GotOptions, 'headers' | 'json' | 'method' | 'searchParams'>
30 const peertubeGot = got.extend({
32 'user-agent': getUserAgent()
37 const promiseOrStream = next(options) as CancelableRequest<any>
38 const bodyKBLimit = options.context?.bodyKBLimit as number
39 if (!bodyKBLimit) throw new Error('No KB limit for this request')
41 const bodyLimit = bodyKBLimit * 1000
43 /* eslint-disable @typescript-eslint/no-floating-promises */
44 promiseOrStream.on('downloadProgress', progress => {
45 if (progress.transferred > bodyLimit && progress.percent !== 1) {
46 const message = `Exceeded the download limit of ${bodyLimit} B`
50 if (promiseOrStream.cancel) {
51 promiseOrStream.cancel()
56 (promiseOrStream as any).destroy()
60 return promiseOrStream
67 const headers = options.headers || {}
68 headers['host'] = options.url.host
72 const httpSignatureOptions = options.context?.httpSignature
74 if (httpSignatureOptions) {
75 const method = options.method ?? 'GET'
76 const path = options.path ?? options.url.pathname
78 if (!method || !path) {
79 throw new Error(`Cannot sign request without method (${method}) or path (${path}) ${options}`)
82 httpSignature.signRequest({
83 getHeader: function (header) {
84 return options.headers[header]
87 setHeader: function (header, value) {
88 options.headers[header] = value
93 }, httpSignatureOptions)
100 function doRequest (url: string, options: PeerTubeRequestOptions = {}) {
101 const gotOptions = buildGotOptions(options)
103 return peertubeGot(url, gotOptions)
104 .catch(err => { throw buildRequestError(err) })
107 function doJSONRequest <T> (url: string, options: PeerTubeRequestOptions = {}) {
108 const gotOptions = buildGotOptions(options)
110 return peertubeGot<T>(url, { ...gotOptions, responseType: 'json' })
111 .catch(err => { throw buildRequestError(err) })
114 async function doRequestAndSaveToFile (
117 options: PeerTubeRequestOptions = {}
119 const gotOptions = buildGotOptions(options)
121 const outFile = createWriteStream(destPath)
124 await pipelinePromise(
125 peertubeGot.stream(url, gotOptions),
130 .catch(err => logger.error('Cannot remove %s after request failure.', destPath, { err }))
132 throw buildRequestError(err)
136 async function downloadImage (url: string, destDir: string, destName: string, size: { width: number, height: number }) {
137 const tmpPath = join(CONFIG.STORAGE.TMP_DIR, 'pending-' + destName)
138 await doRequestAndSaveToFile(url, tmpPath)
140 const destPath = join(destDir, destName)
143 await processImage(tmpPath, destPath, size)
145 await remove(tmpPath)
151 function getUserAgent () {
152 return `PeerTube/${PEERTUBE_VERSION} (+${WEBSERVER.URL})`
155 // ---------------------------------------------------------------------------
160 doRequestAndSaveToFile,
164 // ---------------------------------------------------------------------------
166 function buildGotOptions (options: PeerTubeRequestOptions) {
167 const { activityPub, bodyKBLimit = 1000 } = options
169 const context = { bodyKBLimit, httpSignature: options.httpSignature }
171 let headers = options.headers || {}
174 headers = { ...headers, date: new Date().toUTCString() }
177 if (activityPub && !headers.accept) {
178 headers = { ...headers, accept: ACTIVITY_PUB.ACCEPT_HEADER }
182 method: options.method,
184 searchParams: options.searchParams,
190 function buildRequestError (error: RequestError) {
191 const newError: PeerTubeRequestError = new Error(error.message)
192 newError.name = error.name
193 newError.stack = error.stack
195 if (error.response) {
196 newError.responseBody = error.response.body
197 newError.statusCode = error.response.statusCode