@naturalcycles/nodejs-lib 12.63.1 → 12.64.3

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/fs/del.js CHANGED
@@ -55,14 +55,14 @@ async function del(_opt) {
55
55
  }));
56
56
  const dirnamesSorted = dirnames.sort().reverse();
57
57
  // console.log({ dirnamesSorted })
58
- const deletedDirs = await (0, js_lib_1.pFilter)(dirnamesSorted, async (dirpath) => {
58
+ const deletedDirs = [];
59
+ for await (const dirpath of dirnamesSorted) {
59
60
  if (await isEmptyDir(dirpath)) {
60
61
  // console.log(`empty dir: ${dirpath}`)
61
62
  await fs.remove(dirpath);
62
- return true;
63
+ deletedDirs.push(dirpath);
63
64
  }
64
- return false;
65
- }, { concurrency: 1 });
65
+ }
66
66
  if (verbose || debug)
67
67
  console.log({ deletedDirs });
68
68
  if (!silent) {
@@ -6,5 +6,6 @@ import { GetGotOptions } from './got.model';
6
6
  * 1. Error handler hook that prints helpful errors.
7
7
  * 2. Hooks that log start/end of request (optional, false by default).
8
8
  * 3. Reasonable defaults(tm), e.g non-infinite Timeout
9
+ * 4. Preserves error stack traces (!) (experimental!)
9
10
  */
10
11
  export declare function getGot(opt?: GetGotOptions): Got;
@@ -10,6 +10,7 @@ const __1 = require("..");
10
10
  * 1. Error handler hook that prints helpful errors.
11
11
  * 2. Hooks that log start/end of request (optional, false by default).
12
12
  * 3. Reasonable defaults(tm), e.g non-infinite Timeout
13
+ * 4. Preserves error stack traces (!) (experimental!)
13
14
  */
14
15
  function getGot(opt = {}) {
15
16
  opt.logger || (opt.logger = console);
@@ -40,6 +41,18 @@ function getGot(opt = {}) {
40
41
  // Which definitely doesn't fit into default "RequestTimeout"
41
42
  timeout: 60000,
42
43
  ...opt,
44
+ handlers: [
45
+ (options, next) => {
46
+ options.context = {
47
+ ...options.context,
48
+ started: Date.now(),
49
+ // This is to preserve original stack trace
50
+ // https://github.com/sindresorhus/got/blob/main/documentation/async-stack-traces.md
51
+ err: new Error('RequestError'),
52
+ };
53
+ return next(options);
54
+ },
55
+ ],
43
56
  hooks: {
44
57
  ...opt.hooks,
45
58
  beforeError: [
@@ -120,15 +133,31 @@ function gotErrorHook(opt = {}) {
120
133
  err.message = [[statusCode, method, shortUrl].filter(Boolean).join(' '), body]
121
134
  .filter(Boolean)
122
135
  .join('\n');
136
+ const stack = err.options.context?.err?.stack;
137
+ if (stack) {
138
+ const originalStack = err.stack.split('\n');
139
+ let originalStackIndex = originalStack.findIndex(line => line.includes(' at '));
140
+ if (originalStackIndex === -1)
141
+ originalStackIndex = originalStack.length - 1;
142
+ // Skipping first line as it has RequestError: ...
143
+ // Skipping second line as it's known to be from e.g at got_1.default.extend.handlers
144
+ const syntheticStack = stack.split('\n').slice(2);
145
+ let firstNonNodeModulesIndex = syntheticStack.findIndex(line => !line.includes('node_modules'));
146
+ if (firstNonNodeModulesIndex === -1)
147
+ firstNonNodeModulesIndex = 0;
148
+ err.stack = [
149
+ // First lines of original error
150
+ ...originalStack.slice(0, originalStackIndex),
151
+ // Other lines from "Synthetic error"
152
+ ...syntheticStack.slice(firstNonNodeModulesIndex),
153
+ ].join('\n');
154
+ // err.stack += '\n --' + stack.replace('Error: RequestError', '')
155
+ }
123
156
  return err;
124
157
  };
125
158
  }
126
159
  function gotBeforeRequestHook(opt) {
127
160
  return options => {
128
- options.context = {
129
- ...options.context,
130
- started: Date.now(),
131
- };
132
161
  if (opt.logStart) {
133
162
  const { retryCount } = options.context;
134
163
  const shortUrl = getShortUrl(opt, options.url, options.prefixUrl);
@@ -183,7 +212,8 @@ function gotBeforeRetryHook(opt) {
183
212
  function gotAfterResponseHook(opt = {}) {
184
213
  return resp => {
185
214
  const success = resp.statusCode >= 200 && resp.statusCode < 400;
186
- if (opt.logFinished) {
215
+ // Errors are not logged here, as they're logged by gotErrorHook
216
+ if (opt.logFinished && success) {
187
217
  const { started, retryCount } = resp.request.options.context;
188
218
  const { url, prefixUrl, method } = resp.request.options;
189
219
  const shortUrl = getShortUrl(opt, url, prefixUrl);
@@ -54,5 +54,6 @@ export interface GotRequestContext extends AnyObject {
54
54
  * Millisecond-timestamp of when the request was started. To be able to count "time spent".
55
55
  */
56
56
  started: number;
57
+ err?: Error;
57
58
  retryCount?: number;
58
59
  }
@@ -43,5 +43,5 @@ export declare const semVerSchema: import("./string.extensions").ExtendedStringS
43
43
  export declare const userAgentSchema: import("./string.extensions").ExtendedStringSchema;
44
44
  export declare const utcOffsetSchema: import("./number.extensions").ExtendedNumberSchema;
45
45
  export declare const ipAddressSchema: import("./string.extensions").ExtendedStringSchema;
46
- export declare const baseDBEntitySchema: ObjectSchemaTyped<Partial<SavedDBEntity>, Partial<SavedDBEntity>>;
47
- export declare const savedDBEntitySchema: ObjectSchemaTyped<SavedDBEntity, SavedDBEntity>;
46
+ export declare const baseDBEntitySchema: ObjectSchemaTyped<Partial<SavedDBEntity<string>>, Partial<SavedDBEntity<string>>>;
47
+ export declare const savedDBEntitySchema: ObjectSchemaTyped<SavedDBEntity<string>, SavedDBEntity<string>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "12.63.1",
3
+ "version": "12.64.3",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
@@ -26,7 +26,7 @@
26
26
  "chalk": "^4.0.0",
27
27
  "cp-file": "^9.0.0",
28
28
  "debug": "^4.1.1",
29
- "dotenv": "^14.2.0",
29
+ "dotenv": "^16.0.0",
30
30
  "execa": "^5.0.0",
31
31
  "fs-extra": "^10.0.0",
32
32
  "globby": "^11.0.0",
package/src/fs/del.ts CHANGED
@@ -90,18 +90,14 @@ export async function del(_opt: DelOptions | DelSingleOption): Promise<void> {
90
90
 
91
91
  // console.log({ dirnamesSorted })
92
92
 
93
- const deletedDirs = await pFilter(
94
- dirnamesSorted,
95
- async dirpath => {
96
- if (await isEmptyDir(dirpath)) {
97
- // console.log(`empty dir: ${dirpath}`)
98
- await fs.remove(dirpath)
99
- return true
100
- }
101
- return false
102
- },
103
- { concurrency: 1 },
104
- )
93
+ const deletedDirs: string[] = []
94
+ for await (const dirpath of dirnamesSorted) {
95
+ if (await isEmptyDir(dirpath)) {
96
+ // console.log(`empty dir: ${dirpath}`)
97
+ await fs.remove(dirpath)
98
+ deletedDirs.push(dirpath)
99
+ }
100
+ }
105
101
 
106
102
  if (verbose || debug) console.log({ deletedDirs })
107
103
 
package/src/got/getGot.ts CHANGED
@@ -16,6 +16,7 @@ import { GetGotOptions, GotRequestContext } from './got.model'
16
16
  * 1. Error handler hook that prints helpful errors.
17
17
  * 2. Hooks that log start/end of request (optional, false by default).
18
18
  * 3. Reasonable defaults(tm), e.g non-infinite Timeout
19
+ * 4. Preserves error stack traces (!) (experimental!)
19
20
  */
20
21
  export function getGot(opt: GetGotOptions = {}): Got {
21
22
  opt.logger ||= console
@@ -48,6 +49,19 @@ export function getGot(opt: GetGotOptions = {}): Got {
48
49
  // Which definitely doesn't fit into default "RequestTimeout"
49
50
  timeout: 60_000,
50
51
  ...opt,
52
+ handlers: [
53
+ (options, next) => {
54
+ options.context = {
55
+ ...options.context,
56
+ started: Date.now(),
57
+ // This is to preserve original stack trace
58
+ // https://github.com/sindresorhus/got/blob/main/documentation/async-stack-traces.md
59
+ err: new Error('RequestError'),
60
+ } as GotRequestContext
61
+
62
+ return next(options)
63
+ },
64
+ ],
51
65
  hooks: {
52
66
  ...opt.hooks,
53
67
  beforeError: [
@@ -135,17 +149,35 @@ function gotErrorHook(opt: GetGotOptions = {}): BeforeErrorHook {
135
149
  .filter(Boolean)
136
150
  .join('\n')
137
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
+ // Skipping first line as it has RequestError: ...
159
+ // Skipping second line as it's known to be from e.g at got_1.default.extend.handlers
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
+ // First lines of original error
168
+ ...originalStack.slice(0, originalStackIndex),
169
+ // Other lines from "Synthetic error"
170
+ ...syntheticStack.slice(firstNonNodeModulesIndex),
171
+ ].join('\n')
172
+ // err.stack += '\n --' + stack.replace('Error: RequestError', '')
173
+ }
174
+
138
175
  return err
139
176
  }
140
177
  }
141
178
 
142
179
  function gotBeforeRequestHook(opt: GetGotOptions): BeforeRequestHook {
143
180
  return options => {
144
- options.context = {
145
- ...options.context,
146
- started: Date.now(),
147
- } as GotRequestContext
148
-
149
181
  if (opt.logStart) {
150
182
  const { retryCount } = options.context as GotRequestContext
151
183
  const shortUrl = getShortUrl(opt, options.url, options.prefixUrl)
@@ -212,7 +244,8 @@ function gotAfterResponseHook(opt: GetGotOptions = {}): AfterResponseHook {
212
244
  return resp => {
213
245
  const success = resp.statusCode >= 200 && resp.statusCode < 400
214
246
 
215
- if (opt.logFinished) {
247
+ // Errors are not logged here, as they're logged by gotErrorHook
248
+ if (opt.logFinished && success) {
216
249
  const { started, retryCount } = resp.request.options.context as GotRequestContext
217
250
  const { url, prefixUrl, method } = resp.request.options
218
251
  const shortUrl = getShortUrl(opt, url, prefixUrl)
@@ -65,5 +65,7 @@ export interface GotRequestContext extends AnyObject {
65
65
  */
66
66
  started: number
67
67
 
68
+ err?: Error
69
+
68
70
  retryCount?: number
69
71
  }