1 | import { URL } from 'url'
|
2 | import { _since } from '@naturalcycles/js-lib'
|
3 | import got, {
|
4 | AfterResponseHook,
|
5 | BeforeErrorHook,
|
6 | BeforeRequestHook,
|
7 | BeforeRetryHook,
|
8 | Got,
|
9 | } from 'got'
|
10 | import { inspectAny } from '..'
|
11 | import { GetGotOptions, GotRequestContext } from './got.model'
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | export function getGot(opt: GetGotOptions = {}): Got {
|
22 | opt.logger ||= console
|
23 |
|
24 | if (opt.debug) {
|
25 | opt.logStart = opt.logFinished = opt.logResponse = opt.logRequest = true
|
26 | }
|
27 |
|
28 | return got.extend({
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | timeout: 60_000,
|
51 | ...opt,
|
52 | handlers: [
|
53 | (options, next) => {
|
54 | options.context = {
|
55 | ...options.context,
|
56 | started: Date.now(),
|
57 |
|
58 |
|
59 | err: new Error('RequestError'),
|
60 | } as GotRequestContext
|
61 |
|
62 | return next(options)
|
63 | },
|
64 | ],
|
65 | hooks: {
|
66 | ...opt.hooks,
|
67 | beforeError: [
|
68 | ...(opt.hooks?.beforeError || []),
|
69 |
|
70 | gotErrorHook(opt),
|
71 | ],
|
72 | beforeRequest: [
|
73 | gotBeforeRequestHook(opt),
|
74 |
|
75 | ...(opt.hooks?.beforeRequest || []),
|
76 | ],
|
77 | beforeRetry: [
|
78 | gotBeforeRetryHook(opt),
|
79 |
|
80 | ...(opt.hooks?.beforeRetry || []),
|
81 | ],
|
82 | afterResponse: [
|
83 | ...(opt.hooks?.afterResponse || []),
|
84 |
|
85 | gotAfterResponseHook(opt),
|
86 | ],
|
87 | },
|
88 | })
|
89 | }
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | function gotErrorHook(opt: GetGotOptions = {}): BeforeErrorHook {
|
116 | const { maxResponseLength = 10_000 } = opt
|
117 |
|
118 | return err => {
|
119 | const statusCode = err.response?.statusCode || 0
|
120 | const { method, url, prefixUrl } = err.options
|
121 | const shortUrl = getShortUrl(opt, url, prefixUrl)
|
122 | const { started, retryCount } = (err.request?.options.context || {}) as GotRequestContext
|
123 |
|
124 | const body = err.response?.body
|
125 | ? inspectAny(err.response.body, {
|
126 | maxLen: maxResponseLength,
|
127 | colors: false,
|
128 | })
|
129 | : err.message
|
130 |
|
131 |
|
132 | opt.logger!.log(
|
133 | [
|
134 | ' <<',
|
135 | statusCode,
|
136 | method,
|
137 | shortUrl,
|
138 | retryCount && `(retry ${retryCount})`,
|
139 | 'error',
|
140 | started && 'in ' + _since(started),
|
141 | ]
|
142 | .filter(Boolean)
|
143 | .join(' '),
|
144 | )
|
145 |
|
146 |
|
147 |
|
148 | err.message = [[statusCode, method, shortUrl].filter(Boolean).join(' '), body]
|
149 | .filter(Boolean)
|
150 | .join('\n')
|
151 |
|
152 | const stack = (err.options.context as GotRequestContext)?.err?.stack
|
153 | if (stack) {
|
154 | const originalStack = err.stack.split('\n')
|
155 | let originalStackIndex = originalStack.findIndex(line => line.includes(' at '))
|
156 | if (originalStackIndex === -1) originalStackIndex = originalStack.length - 1
|
157 |
|
158 |
|
159 |
|
160 | const syntheticStack = stack.split('\n').slice(2)
|
161 | let firstNonNodeModulesIndex = syntheticStack.findIndex(
|
162 | line => !line.includes('node_modules'),
|
163 | )
|
164 | if (firstNonNodeModulesIndex === -1) firstNonNodeModulesIndex = 0
|
165 |
|
166 | err.stack = [
|
167 |
|
168 | ...originalStack.slice(0, originalStackIndex),
|
169 |
|
170 | ...syntheticStack.slice(firstNonNodeModulesIndex),
|
171 | ].join('\n')
|
172 |
|
173 | }
|
174 |
|
175 | return err
|
176 | }
|
177 | }
|
178 |
|
179 | function gotBeforeRequestHook(opt: GetGotOptions): BeforeRequestHook {
|
180 | return options => {
|
181 | if (opt.logStart) {
|
182 | const { retryCount } = options.context as GotRequestContext
|
183 | const shortUrl = getShortUrl(opt, options.url, options.prefixUrl)
|
184 | opt.logger!.log(
|
185 | [' >>', options.method, shortUrl, retryCount && `(retry ${retryCount})`].join(' '),
|
186 | )
|
187 | }
|
188 |
|
189 | if (opt.logRequest) {
|
190 | const body = options.json || options.body
|
191 |
|
192 | if (body) {
|
193 | opt.logger!.log(body)
|
194 | }
|
195 | }
|
196 | }
|
197 | }
|
198 |
|
199 |
|
200 |
|
201 | function gotBeforeRetryHook(opt: GetGotOptions): BeforeRetryHook {
|
202 | const { maxResponseLength = 10_000 } = opt
|
203 |
|
204 | return (options, err, retryCount) => {
|
205 |
|
206 | const statusCode = err?.response?.statusCode || 0
|
207 |
|
208 | if (statusCode && statusCode < 300) {
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | return
|
214 | }
|
215 |
|
216 | const { method, url, prefixUrl } = options
|
217 | const shortUrl = getShortUrl(opt, url, prefixUrl)
|
218 | const { started } = options.context as GotRequestContext
|
219 | Object.assign(options.context, { retryCount })
|
220 |
|
221 | const body = err?.response?.body
|
222 | ? inspectAny(err.response.body, {
|
223 | maxLen: maxResponseLength,
|
224 | colors: false,
|
225 | })
|
226 | : err?.message
|
227 |
|
228 |
|
229 | opt.logger!.warn(
|
230 | [
|
231 | [
|
232 | ' <<',
|
233 | statusCode,
|
234 | method,
|
235 | shortUrl,
|
236 | retryCount && retryCount > 1 ? `(retry ${retryCount - 1})` : '(first try)',
|
237 | 'error',
|
238 | started && 'in ' + _since(started),
|
239 | ]
|
240 | .filter(Boolean)
|
241 | .join(' '),
|
242 | body,
|
243 | ]
|
244 | .filter(Boolean)
|
245 | .join('\n'),
|
246 | )
|
247 | }
|
248 | }
|
249 |
|
250 |
|
251 |
|
252 | function gotAfterResponseHook(opt: GetGotOptions = {}): AfterResponseHook {
|
253 | return resp => {
|
254 | const success = resp.statusCode >= 200 && resp.statusCode < 400
|
255 |
|
256 |
|
257 | if (opt.logFinished && success) {
|
258 | const { started, retryCount } = resp.request.options.context as GotRequestContext
|
259 | const { url, prefixUrl, method } = resp.request.options
|
260 | const shortUrl = getShortUrl(opt, url, prefixUrl)
|
261 |
|
262 | opt.logger!.log(
|
263 | [
|
264 | ' <<',
|
265 | resp.statusCode,
|
266 | method,
|
267 | shortUrl,
|
268 | retryCount && `(retry ${retryCount - 1})`,
|
269 | started && 'in ' + _since(started),
|
270 | ]
|
271 | .filter(Boolean)
|
272 | .join(' '),
|
273 | )
|
274 |
|
275 | }
|
276 |
|
277 |
|
278 | if (opt.logResponse && success) {
|
279 | opt.logger!.log(inspectAny(resp.body, { maxLen: opt.maxResponseLength }))
|
280 | }
|
281 |
|
282 | return resp
|
283 | }
|
284 | }
|
285 |
|
286 | function getShortUrl(opt: GetGotOptions, url: URL, prefixUrl?: string): string {
|
287 | if (url.password) {
|
288 | url = new URL(url.toString())
|
289 | url.password = '[redacted]'
|
290 | }
|
291 |
|
292 | let shortUrl = url.toString()
|
293 |
|
294 | if (opt.logWithSearchParams === false) {
|
295 | shortUrl = shortUrl.split('?')[0]!
|
296 | }
|
297 |
|
298 | if (opt.logWithPrefixUrl === false && prefixUrl && shortUrl.startsWith(prefixUrl)) {
|
299 | shortUrl = shortUrl.slice(prefixUrl.length)
|
300 | }
|
301 |
|
302 | return shortUrl
|
303 | }
|