@naturalcycles/nodejs-lib 13.0.2 → 13.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -3,13 +3,9 @@ import * as fastGlob from 'fast-glob';
3
3
  import type { Options as FastGlobOptions } from 'fast-glob';
4
4
  import * as globby from 'globby';
5
5
  import type { GlobbyOptions } from 'globby';
6
- import { RequestError, TimeoutError } from 'got';
7
- import type { AfterResponseHook, BeforeErrorHook, BeforeRequestHook, Got } from 'got';
8
6
  import type { AnySchema, ValidationErrorItem, AlternativesSchema, ArraySchema, BinarySchema, BooleanSchema, DateSchema, FunctionSchema, ObjectSchema } from 'joi';
9
7
  export * from './buffer/buffer.util';
10
8
  export * from './diff/tableDiff';
11
- export * from './got/getGot';
12
- export * from './got/got.model';
13
9
  export * from './infra/process.util';
14
10
  export * from './log/debug';
15
11
  export * from './security/hash.util';
@@ -84,5 +80,5 @@ export * from './fs/fs.util';
84
80
  export * from './fs/del';
85
81
  export * from './fs/json2env';
86
82
  export * from './fs/kpy';
87
- export type { GlobbyOptions, FastGlobOptions, ValidationErrorItem, Got, AfterResponseHook, BeforeErrorHook, BeforeRequestHook, AlternativesSchema, AnySchema, ArraySchema, BinarySchema, BooleanSchema, DateSchema, FunctionSchema, ObjectSchema, };
88
- export { globby, fastGlob, RequestError, TimeoutError, Ajv };
83
+ export type { GlobbyOptions, FastGlobOptions, ValidationErrorItem, AlternativesSchema, AnySchema, ArraySchema, BinarySchema, BooleanSchema, DateSchema, FunctionSchema, ObjectSchema, };
84
+ export { globby, fastGlob, Ajv };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Ajv = exports.TimeoutError = exports.RequestError = exports.fastGlob = exports.globby = void 0;
3
+ exports.Ajv = exports.fastGlob = exports.globby = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const ajv_1 = require("ajv");
6
6
  exports.Ajv = ajv_1.default;
@@ -8,13 +8,8 @@ const fastGlob = require("fast-glob");
8
8
  exports.fastGlob = fastGlob;
9
9
  const globby = require("globby");
10
10
  exports.globby = globby;
11
- const got_1 = require("got");
12
- Object.defineProperty(exports, "RequestError", { enumerable: true, get: function () { return got_1.RequestError; } });
13
- Object.defineProperty(exports, "TimeoutError", { enumerable: true, get: function () { return got_1.TimeoutError; } });
14
11
  tslib_1.__exportStar(require("./buffer/buffer.util"), exports);
15
12
  tslib_1.__exportStar(require("./diff/tableDiff"), exports);
16
- tslib_1.__exportStar(require("./got/getGot"), exports);
17
- tslib_1.__exportStar(require("./got/got.model"), exports);
18
13
  tslib_1.__exportStar(require("./infra/process.util"), exports);
19
14
  tslib_1.__exportStar(require("./log/debug"), exports);
20
15
  tslib_1.__exportStar(require("./security/hash.util"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "13.0.2",
3
+ "version": "13.1.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
@@ -27,7 +27,6 @@
27
27
  "dotenv": "^16.0.0",
28
28
  "fast-glob": "^3.2.11",
29
29
  "globby": "^11.0.0",
30
- "got": "^11.0.1",
31
30
  "joi": "^17.9.2",
32
31
  "jsonwebtoken": "^9.0.0",
33
32
  "lru-cache": "^10.0.0",
package/src/index.ts CHANGED
@@ -3,8 +3,6 @@ import * as fastGlob from 'fast-glob'
3
3
  import type { Options as FastGlobOptions } from 'fast-glob'
4
4
  import * as globby from 'globby'
5
5
  import type { GlobbyOptions } from 'globby'
6
- import { RequestError, TimeoutError } from 'got'
7
- import type { AfterResponseHook, BeforeErrorHook, BeforeRequestHook, Got } from 'got'
8
6
  import type {
9
7
  AnySchema,
10
8
  ValidationErrorItem,
@@ -18,8 +16,6 @@ import type {
18
16
  } from 'joi'
19
17
  export * from './buffer/buffer.util'
20
18
  export * from './diff/tableDiff'
21
- export * from './got/getGot'
22
- export * from './got/got.model'
23
19
  export * from './infra/process.util'
24
20
  export * from './log/debug'
25
21
  export * from './security/hash.util'
@@ -99,10 +95,6 @@ export type {
99
95
  GlobbyOptions,
100
96
  FastGlobOptions,
101
97
  ValidationErrorItem,
102
- Got,
103
- AfterResponseHook,
104
- BeforeErrorHook,
105
- BeforeRequestHook,
106
98
  AlternativesSchema,
107
99
  AnySchema,
108
100
  ArraySchema,
@@ -116,4 +108,4 @@ export type {
116
108
  // StringSchema,
117
109
  }
118
110
 
119
- export { globby, fastGlob, RequestError, TimeoutError, Ajv }
111
+ export { globby, fastGlob, Ajv }
@@ -1,13 +0,0 @@
1
- import { Got } from 'got';
2
- import { GetGotOptions } from './got.model';
3
- /**
4
- * Returns instance of Got with "reasonable defaults":
5
- *
6
- * 1. Error handler hook that prints helpful errors.
7
- * 2. Hooks that log start/end of request (optional, false by default).
8
- * 3. Reasonable defaults(tm), e.g non-infinite Timeout
9
- * 4. Preserves error stack traces (!) (experimental!)
10
- *
11
- * @deprecated `getGot` (together with `getKy`) is deprecated in favor of `getFetcher` of js-lib
12
- */
13
- export declare function getGot(opt?: GetGotOptions): Got;
@@ -1,262 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getGot = void 0;
4
- const node_url_1 = require("node:url");
5
- const js_lib_1 = require("@naturalcycles/js-lib");
6
- const got_1 = require("got");
7
- const __1 = require("..");
8
- /**
9
- * Returns instance of Got with "reasonable defaults":
10
- *
11
- * 1. Error handler hook that prints helpful errors.
12
- * 2. Hooks that log start/end of request (optional, false by default).
13
- * 3. Reasonable defaults(tm), e.g non-infinite Timeout
14
- * 4. Preserves error stack traces (!) (experimental!)
15
- *
16
- * @deprecated `getGot` (together with `getKy`) is deprecated in favor of `getFetcher` of js-lib
17
- */
18
- function getGot(opt = {}) {
19
- opt.logger ||= console;
20
- if (opt.debug) {
21
- opt.logStart = opt.logFinished = opt.logResponse = opt.logRequest = true;
22
- }
23
- return got_1.default.extend({
24
- // Most-important is to set to anything non-empty (so, requests don't "hang" by default).
25
- // Should be long enough to handle for slow responses from scaled cloud APIs in times of spikes
26
- // Ideally should be LESS than default Request timeout in backend-lib (so, it has a chance to error
27
- // before server times out with 503).
28
- //
29
- // UPD 2021-11-27
30
- // There are 2 types/strategies for requests:
31
- // 1. Optimized to get result no matter what. E.g in Cron jobs, where otherwise there'll be a job failure
32
- // 2. Part of the Backend request, where we better retry quickly and fail on timeout before Backend aborts it with "503 Request timeout"
33
- //
34
- // Here it's hard to set the default timeout right for both use-cases.
35
- // So, if it's important, you should override it according to your use-cases:
36
- // - set it longer for Type 1 (e.g 120 seconds)
37
- // - set it shorter for Type 2 (e.g 10/20 seconds)
38
- // Please beware of default Retry strategy of Got:
39
- // by default it will retry 2 times (after first try)
40
- // First delay between tries will be ~1 second, then ~2 seconds
41
- // Each retry it'll wait up to `timeout` (so, up to 60 seconds by default).
42
- // So, for 3 tries it multiplies your timeout by 3 (+3 seconds between the tries).
43
- // So, e.g 60 seconds timeout with 2 retries becomes up to 183 seconds.
44
- // Which definitely doesn't fit into default "RequestTimeout"
45
- timeout: 60000,
46
- ...opt,
47
- handlers: [
48
- (options, next) => {
49
- options.context = {
50
- ...options.context,
51
- started: Date.now(),
52
- // This is to preserve original stack trace
53
- // https://github.com/sindresorhus/got/blob/main/documentation/async-stack-traces.md
54
- err: new Error('RequestError'),
55
- };
56
- return next(options);
57
- },
58
- ],
59
- hooks: {
60
- ...opt.hooks,
61
- beforeError: [
62
- ...(opt.hooks?.beforeError || []),
63
- // User hooks go BEFORE
64
- gotErrorHook(opt),
65
- ],
66
- beforeRequest: [
67
- gotBeforeRequestHook(opt),
68
- // User hooks go AFTER
69
- ...(opt.hooks?.beforeRequest || []),
70
- ],
71
- beforeRetry: [
72
- gotBeforeRetryHook(opt),
73
- // User hooks go AFTER
74
- ...(opt.hooks?.beforeRetry || []),
75
- ],
76
- afterResponse: [
77
- ...(opt.hooks?.afterResponse || []),
78
- // User hooks go BEFORE
79
- gotAfterResponseHook(opt),
80
- ],
81
- },
82
- });
83
- }
84
- exports.getGot = getGot;
85
- /**
86
- * Without this hook (default behaviour):
87
- *
88
- * HTTPError: Response code 422 (Unprocessable Entity)
89
- * at EventEmitter.<anonymous> (.../node_modules/got/dist/source/as-promise.js:118:31)
90
- * at processTicksAndRejections (internal/process/task_queues.js:97:5) {
91
- * name: 'HTTPError'
92
- *
93
- *
94
- * With this hook:
95
- *
96
- * HTTPError 422 GET http://a.com/err?q=1 in 8 ms
97
- * {
98
- * message: 'Reference already exists',
99
- * documentation_url: 'https://developer.github.com/v3/git/refs/#create-a-reference'
100
- * }
101
- *
102
- * Features:
103
- * 1. Includes original method and URL (including e.g searchParams) in the error message.
104
- * 2. Includes response.body in the error message (limited length).
105
- * 3. Auto-detects and parses JSON response body (limited length).
106
- * 4. Includes time spent (gotBeforeRequestHook must also be enabled).
107
- * UPD: excluded now to allow automatic Sentry error grouping
108
- */
109
- function gotErrorHook(opt = {}) {
110
- const { maxResponseLength = 10000 } = opt;
111
- return err => {
112
- const statusCode = err.response?.statusCode || 0;
113
- const { method, url, prefixUrl } = err.options;
114
- const shortUrl = getShortUrl(opt, url, prefixUrl);
115
- const { started, retryCount } = (err.request?.options.context || {});
116
- const body = err.response?.body
117
- ? (0, __1.inspectAny)(err.response.body, {
118
- maxLen: maxResponseLength,
119
- colors: false,
120
- })
121
- : err.message;
122
- // We don't include Response/Body/Message in the log, because it's included in the Error thrown from here
123
- opt.logger.log([
124
- ' <<',
125
- statusCode,
126
- method,
127
- shortUrl,
128
- retryCount && `(retry ${retryCount})`,
129
- 'error',
130
- started && 'in ' + (0, js_lib_1._since)(started),
131
- ]
132
- .filter(Boolean)
133
- .join(' '));
134
- // timings are not part of err.message to allow automatic error grouping in Sentry
135
- // Colors are not used, because there's high chance that this Error will be propagated all the way to the Frontend
136
- err.message = [[statusCode, method, shortUrl].filter(Boolean).join(' '), body]
137
- .filter(Boolean)
138
- .join('\n');
139
- const stack = err.options.context?.err?.stack;
140
- if (stack) {
141
- const originalStack = err.stack.split('\n');
142
- let originalStackIndex = originalStack.findIndex(line => line.includes(' at '));
143
- if (originalStackIndex === -1)
144
- originalStackIndex = originalStack.length - 1;
145
- // Skipping first line as it has RequestError: ...
146
- // Skipping second line as it's known to be from e.g at got_1.default.extend.handlers
147
- const syntheticStack = stack.split('\n').slice(2);
148
- let firstNonNodeModulesIndex = syntheticStack.findIndex(line => !line.includes('node_modules'));
149
- if (firstNonNodeModulesIndex === -1)
150
- firstNonNodeModulesIndex = 0;
151
- err.stack = [
152
- // First lines of original error
153
- ...originalStack.slice(0, originalStackIndex),
154
- // Other lines from "Synthetic error"
155
- ...syntheticStack.slice(firstNonNodeModulesIndex),
156
- ].join('\n');
157
- // err.stack += '\n --' + stack.replace('Error: RequestError', '')
158
- }
159
- return err;
160
- };
161
- }
162
- function gotBeforeRequestHook(opt) {
163
- return options => {
164
- if (opt.logStart) {
165
- const { retryCount } = options.context;
166
- const shortUrl = getShortUrl(opt, options.url, options.prefixUrl);
167
- opt.logger.log([' >>', options.method, shortUrl, retryCount && `(retry ${retryCount})`].join(' '));
168
- }
169
- if (opt.logRequest) {
170
- const body = options.json || options.body;
171
- if (body) {
172
- opt.logger.log(body);
173
- }
174
- }
175
- };
176
- }
177
- // Here we log always, because it's similar to ErrorHook - we always log errors
178
- // Because Retries are always result of some Error
179
- function gotBeforeRetryHook(opt) {
180
- const { maxResponseLength = 10000 } = opt;
181
- return (options, err, retryCount) => {
182
- // opt.logger!.log('beforeRetry', retryCount)
183
- const statusCode = err?.response?.statusCode || 0;
184
- if (statusCode && statusCode < 300) {
185
- // todo: possibly remove the log message completely in the future
186
- // opt.logger!.log(
187
- // `skipping got.beforeRetry hook as statusCode is ${statusCode}, err.msg is ${err?.message}`,
188
- // )
189
- return;
190
- }
191
- const { method, url, prefixUrl } = options;
192
- const shortUrl = getShortUrl(opt, url, prefixUrl);
193
- const { started } = options.context;
194
- Object.assign(options.context, { retryCount });
195
- const body = err?.response?.body
196
- ? (0, __1.inspectAny)(err.response.body, {
197
- maxLen: maxResponseLength,
198
- colors: false,
199
- })
200
- : err?.message;
201
- // We don't include Response/Body/Message in the log, because it's included in the Error thrown from here
202
- opt.logger.warn([
203
- [
204
- ' <<',
205
- statusCode,
206
- method,
207
- shortUrl,
208
- retryCount && retryCount > 1 ? `(retry ${retryCount - 1})` : '(first try)',
209
- 'error',
210
- started && 'in ' + (0, js_lib_1._since)(started),
211
- ]
212
- .filter(Boolean)
213
- .join(' '),
214
- body,
215
- ]
216
- .filter(Boolean)
217
- .join('\n'));
218
- };
219
- }
220
- // AfterResponseHook is never called on Error
221
- // So, coloredHttpCode(resp.statusCode) is probably useless
222
- function gotAfterResponseHook(opt = {}) {
223
- return resp => {
224
- const success = resp.statusCode >= 200 && resp.statusCode < 400;
225
- // Errors are not logged here, as they're logged by gotErrorHook
226
- if (opt.logFinished && success) {
227
- const { started, retryCount } = resp.request.options.context;
228
- const { url, prefixUrl, method } = resp.request.options;
229
- const shortUrl = getShortUrl(opt, url, prefixUrl);
230
- opt.logger.log([
231
- ' <<',
232
- resp.statusCode,
233
- method,
234
- shortUrl,
235
- retryCount && `(retry ${retryCount - 1})`,
236
- started && 'in ' + (0, js_lib_1._since)(started),
237
- ]
238
- .filter(Boolean)
239
- .join(' '));
240
- // console.log(`afterResp! ${resp.request.options.method} ${resp.url}`, { context: resp.request.options.context })
241
- }
242
- // Error responses are not logged, cause they're included in Error message already
243
- if (opt.logResponse && success) {
244
- opt.logger.log((0, __1.inspectAny)(resp.body, { maxLen: opt.maxResponseLength }));
245
- }
246
- return resp;
247
- };
248
- }
249
- function getShortUrl(opt, url, prefixUrl) {
250
- if (url.password) {
251
- url = new node_url_1.URL(url.toString()); // prevent original url mutation
252
- url.password = '[redacted]';
253
- }
254
- let shortUrl = url.toString();
255
- if (opt.logWithSearchParams === false) {
256
- shortUrl = shortUrl.split('?')[0];
257
- }
258
- if (opt.logWithPrefixUrl === false && prefixUrl && shortUrl.startsWith(prefixUrl)) {
259
- shortUrl = shortUrl.slice(prefixUrl.length);
260
- }
261
- return shortUrl;
262
- }
@@ -1,59 +0,0 @@
1
- import { AnyObject, CommonLogger } from '@naturalcycles/js-lib';
2
- import type { Options } from 'got';
3
- export interface GetGotOptions extends Options {
4
- /**
5
- * Set to `true` to enable all possible debug logging.
6
- * Not safe in prod (as it logs Responses), but great to use during development.
7
- */
8
- debug?: boolean;
9
- /**
10
- * @default false
11
- */
12
- logStart?: boolean;
13
- /**
14
- * Log when request is finished.
15
- *
16
- * @default false
17
- */
18
- logFinished?: boolean;
19
- /**
20
- * Log request object.
21
- *
22
- * @default false
23
- */
24
- logRequest?: boolean;
25
- /**
26
- * Log actual response object.
27
- *
28
- * @default false
29
- */
30
- logResponse?: boolean;
31
- /**
32
- * @default true
33
- * Set to false to exclude `prefixUrl` from logs (both success and error)
34
- */
35
- logWithPrefixUrl?: boolean;
36
- /**
37
- * @default true
38
- * Set to false to strip searchParams from url when logging (both success and error)
39
- */
40
- logWithSearchParams?: boolean;
41
- /**
42
- * Defaults to `console`
43
- */
44
- logger?: CommonLogger;
45
- /**
46
- * Max length of response object before it's truncated.
47
- *
48
- * @default 10_000
49
- */
50
- maxResponseLength?: number;
51
- }
52
- export interface GotRequestContext extends AnyObject {
53
- /**
54
- * Millisecond-timestamp of when the request was started. To be able to count "time spent".
55
- */
56
- started: number;
57
- err?: Error;
58
- retryCount?: number;
59
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
package/src/got/getGot.ts DELETED
@@ -1,305 +0,0 @@
1
- import { URL } from 'node: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
- * Returns instance of Got with "reasonable defaults":
15
- *
16
- * 1. Error handler hook that prints helpful errors.
17
- * 2. Hooks that log start/end of request (optional, false by default).
18
- * 3. Reasonable defaults(tm), e.g non-infinite Timeout
19
- * 4. Preserves error stack traces (!) (experimental!)
20
- *
21
- * @deprecated `getGot` (together with `getKy`) is deprecated in favor of `getFetcher` of js-lib
22
- */
23
- export function getGot(opt: GetGotOptions = {}): Got {
24
- opt.logger ||= console
25
-
26
- if (opt.debug) {
27
- opt.logStart = opt.logFinished = opt.logResponse = opt.logRequest = true
28
- }
29
-
30
- return got.extend({
31
- // Most-important is to set to anything non-empty (so, requests don't "hang" by default).
32
- // Should be long enough to handle for slow responses from scaled cloud APIs in times of spikes
33
- // Ideally should be LESS than default Request timeout in backend-lib (so, it has a chance to error
34
- // before server times out with 503).
35
- //
36
- // UPD 2021-11-27
37
- // There are 2 types/strategies for requests:
38
- // 1. Optimized to get result no matter what. E.g in Cron jobs, where otherwise there'll be a job failure
39
- // 2. Part of the Backend request, where we better retry quickly and fail on timeout before Backend aborts it with "503 Request timeout"
40
- //
41
- // Here it's hard to set the default timeout right for both use-cases.
42
- // So, if it's important, you should override it according to your use-cases:
43
- // - set it longer for Type 1 (e.g 120 seconds)
44
- // - set it shorter for Type 2 (e.g 10/20 seconds)
45
- // Please beware of default Retry strategy of Got:
46
- // by default it will retry 2 times (after first try)
47
- // First delay between tries will be ~1 second, then ~2 seconds
48
- // Each retry it'll wait up to `timeout` (so, up to 60 seconds by default).
49
- // So, for 3 tries it multiplies your timeout by 3 (+3 seconds between the tries).
50
- // So, e.g 60 seconds timeout with 2 retries becomes up to 183 seconds.
51
- // Which definitely doesn't fit into default "RequestTimeout"
52
- timeout: 60_000,
53
- ...opt,
54
- handlers: [
55
- (options, next) => {
56
- options.context = {
57
- ...options.context,
58
- started: Date.now(),
59
- // This is to preserve original stack trace
60
- // https://github.com/sindresorhus/got/blob/main/documentation/async-stack-traces.md
61
- err: new Error('RequestError'),
62
- } as GotRequestContext
63
-
64
- return next(options)
65
- },
66
- ],
67
- hooks: {
68
- ...opt.hooks,
69
- beforeError: [
70
- ...(opt.hooks?.beforeError || []),
71
- // User hooks go BEFORE
72
- gotErrorHook(opt),
73
- ],
74
- beforeRequest: [
75
- gotBeforeRequestHook(opt),
76
- // User hooks go AFTER
77
- ...(opt.hooks?.beforeRequest || []),
78
- ],
79
- beforeRetry: [
80
- gotBeforeRetryHook(opt),
81
- // User hooks go AFTER
82
- ...(opt.hooks?.beforeRetry || []),
83
- ],
84
- afterResponse: [
85
- ...(opt.hooks?.afterResponse || []),
86
- // User hooks go BEFORE
87
- gotAfterResponseHook(opt),
88
- ],
89
- },
90
- })
91
- }
92
-
93
- /**
94
- * Without this hook (default behaviour):
95
- *
96
- * HTTPError: Response code 422 (Unprocessable Entity)
97
- * at EventEmitter.<anonymous> (.../node_modules/got/dist/source/as-promise.js:118:31)
98
- * at processTicksAndRejections (internal/process/task_queues.js:97:5) {
99
- * name: 'HTTPError'
100
- *
101
- *
102
- * With this hook:
103
- *
104
- * HTTPError 422 GET http://a.com/err?q=1 in 8 ms
105
- * {
106
- * message: 'Reference already exists',
107
- * documentation_url: 'https://developer.github.com/v3/git/refs/#create-a-reference'
108
- * }
109
- *
110
- * Features:
111
- * 1. Includes original method and URL (including e.g searchParams) in the error message.
112
- * 2. Includes response.body in the error message (limited length).
113
- * 3. Auto-detects and parses JSON response body (limited length).
114
- * 4. Includes time spent (gotBeforeRequestHook must also be enabled).
115
- * UPD: excluded now to allow automatic Sentry error grouping
116
- */
117
- function gotErrorHook(opt: GetGotOptions = {}): BeforeErrorHook {
118
- const { maxResponseLength = 10_000 } = opt
119
-
120
- return err => {
121
- const statusCode = err.response?.statusCode || 0
122
- const { method, url, prefixUrl } = err.options
123
- const shortUrl = getShortUrl(opt, url, prefixUrl)
124
- const { started, retryCount } = (err.request?.options.context || {}) as GotRequestContext
125
-
126
- const body = err.response?.body
127
- ? inspectAny(err.response.body, {
128
- maxLen: maxResponseLength,
129
- colors: false,
130
- })
131
- : err.message
132
-
133
- // We don't include Response/Body/Message in the log, because it's included in the Error thrown from here
134
- opt.logger!.log(
135
- [
136
- ' <<',
137
- statusCode,
138
- method,
139
- shortUrl,
140
- retryCount && `(retry ${retryCount})`,
141
- 'error',
142
- started && 'in ' + _since(started),
143
- ]
144
- .filter(Boolean)
145
- .join(' '),
146
- )
147
-
148
- // timings are not part of err.message to allow automatic error grouping in Sentry
149
- // Colors are not used, because there's high chance that this Error will be propagated all the way to the Frontend
150
- err.message = [[statusCode, method, shortUrl].filter(Boolean).join(' '), body]
151
- .filter(Boolean)
152
- .join('\n')
153
-
154
- const stack = (err.options.context as GotRequestContext)?.err?.stack
155
- if (stack) {
156
- const originalStack = err.stack.split('\n')
157
- let originalStackIndex = originalStack.findIndex(line => line.includes(' at '))
158
- if (originalStackIndex === -1) originalStackIndex = originalStack.length - 1
159
-
160
- // Skipping first line as it has RequestError: ...
161
- // Skipping second line as it's known to be from e.g at got_1.default.extend.handlers
162
- const syntheticStack = stack.split('\n').slice(2)
163
- let firstNonNodeModulesIndex = syntheticStack.findIndex(
164
- line => !line.includes('node_modules'),
165
- )
166
- if (firstNonNodeModulesIndex === -1) firstNonNodeModulesIndex = 0
167
-
168
- err.stack = [
169
- // First lines of original error
170
- ...originalStack.slice(0, originalStackIndex),
171
- // Other lines from "Synthetic error"
172
- ...syntheticStack.slice(firstNonNodeModulesIndex),
173
- ].join('\n')
174
- // err.stack += '\n --' + stack.replace('Error: RequestError', '')
175
- }
176
-
177
- return err
178
- }
179
- }
180
-
181
- function gotBeforeRequestHook(opt: GetGotOptions): BeforeRequestHook {
182
- return options => {
183
- if (opt.logStart) {
184
- const { retryCount } = options.context as GotRequestContext
185
- const shortUrl = getShortUrl(opt, options.url, options.prefixUrl)
186
- opt.logger!.log(
187
- [' >>', options.method, shortUrl, retryCount && `(retry ${retryCount})`].join(' '),
188
- )
189
- }
190
-
191
- if (opt.logRequest) {
192
- const body = options.json || options.body
193
-
194
- if (body) {
195
- opt.logger!.log(body)
196
- }
197
- }
198
- }
199
- }
200
-
201
- // Here we log always, because it's similar to ErrorHook - we always log errors
202
- // Because Retries are always result of some Error
203
- function gotBeforeRetryHook(opt: GetGotOptions): BeforeRetryHook {
204
- const { maxResponseLength = 10_000 } = opt
205
-
206
- return (options, err, retryCount) => {
207
- // opt.logger!.log('beforeRetry', retryCount)
208
- const statusCode = err?.response?.statusCode || 0
209
-
210
- if (statusCode && statusCode < 300) {
211
- // todo: possibly remove the log message completely in the future
212
- // opt.logger!.log(
213
- // `skipping got.beforeRetry hook as statusCode is ${statusCode}, err.msg is ${err?.message}`,
214
- // )
215
- return
216
- }
217
-
218
- const { method, url, prefixUrl } = options
219
- const shortUrl = getShortUrl(opt, url, prefixUrl)
220
- const { started } = options.context as GotRequestContext
221
- Object.assign(options.context, { retryCount })
222
-
223
- const body = err?.response?.body
224
- ? inspectAny(err.response.body, {
225
- maxLen: maxResponseLength,
226
- colors: false,
227
- })
228
- : err?.message
229
-
230
- // We don't include Response/Body/Message in the log, because it's included in the Error thrown from here
231
- opt.logger!.warn(
232
- [
233
- [
234
- ' <<',
235
- statusCode,
236
- method,
237
- shortUrl,
238
- retryCount && retryCount > 1 ? `(retry ${retryCount - 1})` : '(first try)',
239
- 'error',
240
- started && 'in ' + _since(started),
241
- ]
242
- .filter(Boolean)
243
- .join(' '),
244
- body,
245
- ]
246
- .filter(Boolean)
247
- .join('\n'),
248
- )
249
- }
250
- }
251
-
252
- // AfterResponseHook is never called on Error
253
- // So, coloredHttpCode(resp.statusCode) is probably useless
254
- function gotAfterResponseHook(opt: GetGotOptions = {}): AfterResponseHook {
255
- return resp => {
256
- const success = resp.statusCode >= 200 && resp.statusCode < 400
257
-
258
- // Errors are not logged here, as they're logged by gotErrorHook
259
- if (opt.logFinished && success) {
260
- const { started, retryCount } = resp.request.options.context as GotRequestContext
261
- const { url, prefixUrl, method } = resp.request.options
262
- const shortUrl = getShortUrl(opt, url, prefixUrl)
263
-
264
- opt.logger!.log(
265
- [
266
- ' <<',
267
- resp.statusCode,
268
- method,
269
- shortUrl,
270
- retryCount && `(retry ${retryCount - 1})`,
271
- started && 'in ' + _since(started),
272
- ]
273
- .filter(Boolean)
274
- .join(' '),
275
- )
276
- // console.log(`afterResp! ${resp.request.options.method} ${resp.url}`, { context: resp.request.options.context })
277
- }
278
-
279
- // Error responses are not logged, cause they're included in Error message already
280
- if (opt.logResponse && success) {
281
- opt.logger!.log(inspectAny(resp.body, { maxLen: opt.maxResponseLength }))
282
- }
283
-
284
- return resp
285
- }
286
- }
287
-
288
- function getShortUrl(opt: GetGotOptions, url: URL, prefixUrl?: string): string {
289
- if (url.password) {
290
- url = new URL(url.toString()) // prevent original url mutation
291
- url.password = '[redacted]'
292
- }
293
-
294
- let shortUrl = url.toString()
295
-
296
- if (opt.logWithSearchParams === false) {
297
- shortUrl = shortUrl.split('?')[0]!
298
- }
299
-
300
- if (opt.logWithPrefixUrl === false && prefixUrl && shortUrl.startsWith(prefixUrl)) {
301
- shortUrl = shortUrl.slice(prefixUrl.length)
302
- }
303
-
304
- return shortUrl
305
- }
@@ -1,71 +0,0 @@
1
- import { AnyObject, CommonLogger } from '@naturalcycles/js-lib'
2
- import type { Options } from 'got'
3
-
4
- export interface GetGotOptions extends Options {
5
- /**
6
- * Set to `true` to enable all possible debug logging.
7
- * Not safe in prod (as it logs Responses), but great to use during development.
8
- */
9
- debug?: boolean
10
-
11
- /**
12
- * @default false
13
- */
14
- logStart?: boolean
15
-
16
- /**
17
- * Log when request is finished.
18
- *
19
- * @default false
20
- */
21
- logFinished?: boolean
22
-
23
- /**
24
- * Log request object.
25
- *
26
- * @default false
27
- */
28
- logRequest?: boolean
29
-
30
- /**
31
- * Log actual response object.
32
- *
33
- * @default false
34
- */
35
- logResponse?: boolean
36
-
37
- /**
38
- * @default true
39
- * Set to false to exclude `prefixUrl` from logs (both success and error)
40
- */
41
- logWithPrefixUrl?: boolean
42
-
43
- /**
44
- * @default true
45
- * Set to false to strip searchParams from url when logging (both success and error)
46
- */
47
- logWithSearchParams?: boolean
48
-
49
- /**
50
- * Defaults to `console`
51
- */
52
- logger?: CommonLogger
53
-
54
- /**
55
- * Max length of response object before it's truncated.
56
- *
57
- * @default 10_000
58
- */
59
- maxResponseLength?: number
60
- }
61
-
62
- export interface GotRequestContext extends AnyObject {
63
- /**
64
- * Millisecond-timestamp of when the request was started. To be able to count "time spent".
65
- */
66
- started: number
67
-
68
- err?: Error
69
-
70
- retryCount?: number
71
- }