]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/requests.ts
Use got instead of request
[github/Chocobozzz/PeerTube.git] / server / helpers / requests.ts
1 import { createWriteStream, remove } from 'fs-extra'
2 import got, { CancelableRequest, Options as GotOptions } 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'
9
10 const httpSignature = require('http-signature')
11
12 type PeerTubeRequestOptions = {
13 activityPub?: boolean
14 bodyKBLimit?: number // 1MB
15 httpSignature?: {
16 algorithm: string
17 authorizationHeaderName: string
18 keyId: string
19 key: string
20 headers: string[]
21 }
22 jsonResponse?: boolean
23 } & Pick<GotOptions, 'headers' | 'json' | 'method' | 'searchParams'>
24
25 const peertubeGot = got.extend({
26 headers: {
27 'user-agent': getUserAgent()
28 },
29
30 handlers: [
31 (options, next) => {
32 const promiseOrStream = next(options) as CancelableRequest<any>
33 const bodyKBLimit = options.context?.bodyKBLimit
34 if (!bodyKBLimit) throw new Error('No KB limit for this request')
35
36 /* eslint-disable @typescript-eslint/no-floating-promises */
37 promiseOrStream.on('downloadProgress', progress => {
38 if (progress.transferred * 1000 > bodyKBLimit && progress.percent !== 1) {
39 promiseOrStream.cancel(`Exceeded the download limit of ${bodyKBLimit} bytes`)
40 }
41 })
42
43 return promiseOrStream
44 }
45 ],
46
47 hooks: {
48 beforeRequest: [
49 options => {
50 const headers = options.headers || {}
51 headers['host'] = options.url.host
52 },
53
54 options => {
55 const httpSignatureOptions = options.context?.httpSignature
56
57 if (httpSignatureOptions) {
58 const method = options.method ?? 'GET'
59 const path = options.path ?? options.url.pathname
60
61 if (!method || !path) {
62 throw new Error(`Cannot sign request without method (${method}) or path (${path}) ${options}`)
63 }
64
65 httpSignature.signRequest({
66 getHeader: function (header) {
67 return options.headers[header]
68 },
69
70 setHeader: function (header, value) {
71 options.headers[header] = value
72 },
73
74 method,
75 path
76 }, httpSignatureOptions)
77 }
78 }
79 ]
80 }
81 })
82
83 function doRequest (url: string, options: PeerTubeRequestOptions = {}) {
84 const gotOptions = buildGotOptions(options)
85
86 return peertubeGot(url, gotOptions)
87 .catch(err => { throw buildRequestError(err) })
88 }
89
90 function doJSONRequest <T> (url: string, options: PeerTubeRequestOptions = {}) {
91 const gotOptions = buildGotOptions(options)
92
93 return peertubeGot<T>(url, { ...gotOptions, responseType: 'json' })
94 .catch(err => { throw buildRequestError(err) })
95 }
96
97 async function doRequestAndSaveToFile (
98 url: string,
99 destPath: string,
100 options: PeerTubeRequestOptions = {}
101 ) {
102 const gotOptions = buildGotOptions(options)
103
104 const outFile = createWriteStream(destPath)
105
106 try {
107 await pipelinePromise(
108 peertubeGot.stream(url, gotOptions),
109 outFile
110 )
111 } catch (err) {
112 remove(destPath)
113 .catch(err => logger.error('Cannot remove %s after request failure.', destPath, { err }))
114
115 throw buildRequestError(err)
116 }
117 }
118
119 async function downloadImage (url: string, destDir: string, destName: string, size: { width: number, height: number }) {
120 const tmpPath = join(CONFIG.STORAGE.TMP_DIR, 'pending-' + destName)
121 await doRequestAndSaveToFile(url, tmpPath)
122
123 const destPath = join(destDir, destName)
124
125 try {
126 await processImage(tmpPath, destPath, size)
127 } catch (err) {
128 await remove(tmpPath)
129
130 throw err
131 }
132 }
133
134 function getUserAgent () {
135 return `PeerTube/${PEERTUBE_VERSION} (+${WEBSERVER.URL})`
136 }
137
138 // ---------------------------------------------------------------------------
139
140 export {
141 doRequest,
142 doJSONRequest,
143 doRequestAndSaveToFile,
144 downloadImage
145 }
146
147 // ---------------------------------------------------------------------------
148
149 function buildGotOptions (options: PeerTubeRequestOptions) {
150 const { activityPub, bodyKBLimit = 1000 } = options
151
152 const context = { bodyKBLimit, httpSignature: options.httpSignature }
153
154 let headers = options.headers || {}
155
156 headers = { ...headers, date: new Date().toUTCString() }
157
158 if (activityPub) {
159 headers = { ...headers, accept: ACTIVITY_PUB.ACCEPT_HEADER }
160 }
161
162 return {
163 method: options.method,
164 json: options.json,
165 searchParams: options.searchParams,
166 headers,
167 context
168 }
169 }
170
171 function buildRequestError (error: any) {
172 const newError = new Error(error.message)
173 newError.name = error.name
174 newError.stack = error.stack
175
176 if (error.response?.body) {
177 error.responseBody = error.response.body
178 }
179
180 return newError
181 }