- return new Bluebird<{ response: request.RequestResponse, body: T }>((res, rej) => {
- request(requestOptions, (err, response, body) => err ? rej(err) : res({ response, body }))
- .on('data', onRequestDataLengthCheck(bodyKBLimit))
- })
+ return promiseOrStream
+ }
+ ],
+
+ 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) {
+ return options.headers[header]
+ },
+
+ setHeader: function (header, value) {
+ 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) })