@naturalcycles/nodejs-lib 13.0.2 → 13.1.1

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.
Files changed (77) hide show
  1. package/dist/bin/del.js +3 -2
  2. package/dist/bin/generate-build-info.js +8 -7
  3. package/dist/bin/json2env.js +3 -2
  4. package/dist/bin/kpy.js +3 -2
  5. package/dist/bin/secrets-decrypt.js +3 -2
  6. package/dist/bin/secrets-encrypt.js +3 -2
  7. package/dist/bin/secrets-gen-key.js +5 -4
  8. package/dist/bin/slack-this.js +3 -2
  9. package/dist/colors/colors.d.ts +1 -1
  10. package/dist/colors/colors.js +31 -30
  11. package/dist/fs/del.js +11 -10
  12. package/dist/fs/fs.util.d.ts +1 -1
  13. package/dist/fs/fs.util.js +42 -41
  14. package/dist/fs/json2env.js +6 -5
  15. package/dist/fs/kpy.js +8 -7
  16. package/dist/index.d.ts +4 -8
  17. package/dist/index.js +6 -11
  18. package/dist/infra/process.util.js +8 -7
  19. package/dist/jwt/jwt.service.d.ts +1 -1
  20. package/dist/jwt/jwt.service.js +6 -5
  21. package/dist/secret/secrets-decrypt.util.js +9 -8
  22. package/dist/secret/secrets-encrypt.util.js +8 -7
  23. package/dist/security/crypto.util.js +9 -8
  24. package/dist/security/hash.util.js +4 -3
  25. package/dist/security/id.util.js +4 -3
  26. package/dist/security/secret.util.js +7 -6
  27. package/dist/stream/ndjson/ndjsonStreamForEach.js +3 -2
  28. package/dist/stream/ndjson/pipelineFromNDJsonFile.js +4 -3
  29. package/dist/stream/ndjson/pipelineToNDJsonFile.js +4 -3
  30. package/dist/util/env.util.js +3 -2
  31. package/dist/util/exec.util.js +4 -3
  32. package/dist/util/git.util.js +9 -8
  33. package/dist/util/zip.util.js +6 -5
  34. package/dist/validation/ajv/getAjv.js +2 -1
  35. package/dist/validation/joi/joi.extensions.d.ts +1 -1
  36. package/dist/validation/joi/joi.extensions.js +3 -2
  37. package/dist/validation/joi/number.extensions.d.ts +1 -1
  38. package/dist/validation/joi/string.extensions.d.ts +1 -1
  39. package/package.json +1 -2
  40. package/src/bin/del.ts +1 -1
  41. package/src/bin/generate-build-info.ts +3 -3
  42. package/src/bin/json2env.ts +1 -1
  43. package/src/bin/kpy.ts +1 -1
  44. package/src/bin/secrets-decrypt.ts +1 -1
  45. package/src/bin/secrets-encrypt.ts +1 -1
  46. package/src/bin/secrets-gen-key.ts +2 -2
  47. package/src/bin/slack-this.ts +1 -1
  48. package/src/colors/colors.ts +2 -2
  49. package/src/fs/del.ts +2 -2
  50. package/src/fs/fs.util.ts +3 -3
  51. package/src/fs/json2env.ts +1 -1
  52. package/src/fs/kpy.ts +1 -1
  53. package/src/index.ts +3 -11
  54. package/src/infra/process.util.ts +1 -1
  55. package/src/jwt/jwt.service.ts +1 -1
  56. package/src/secret/secrets-decrypt.util.ts +2 -2
  57. package/src/secret/secrets-encrypt.util.ts +2 -2
  58. package/src/security/crypto.util.ts +1 -1
  59. package/src/security/hash.util.ts +1 -1
  60. package/src/security/id.util.ts +1 -1
  61. package/src/security/secret.util.ts +1 -1
  62. package/src/stream/ndjson/ndjsonStreamForEach.ts +1 -1
  63. package/src/stream/ndjson/pipelineFromNDJsonFile.ts +1 -1
  64. package/src/stream/ndjson/pipelineToNDJsonFile.ts +1 -1
  65. package/src/util/env.util.ts +1 -1
  66. package/src/util/exec.util.ts +1 -1
  67. package/src/util/git.util.ts +2 -2
  68. package/src/util/zip.util.ts +1 -1
  69. package/src/validation/joi/joi.extensions.ts +1 -1
  70. package/src/validation/joi/number.extensions.ts +1 -1
  71. package/src/validation/joi/string.extensions.ts +1 -1
  72. package/dist/got/getGot.d.ts +0 -13
  73. package/dist/got/getGot.js +0 -262
  74. package/dist/got/got.model.d.ts +0 -59
  75. package/dist/got/got.model.js +0 -2
  76. package/src/got/getGot.ts +0 -305
  77. package/src/got/got.model.ts +0 -71
@@ -1,14 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Joi = void 0;
4
- const JoiLib = require("joi");
4
+ const tslib_1 = require("tslib");
5
+ const joi_1 = tslib_1.__importDefault(require("joi"));
5
6
  const number_extensions_1 = require("./number.extensions");
6
7
  const string_extensions_1 = require("./string.extensions");
7
8
  /**
8
9
  * This is the only right place to import Joi from
9
10
  */
10
11
  // eslint-disable-next-line @typescript-eslint/naming-convention
11
- exports.Joi = JoiLib.defaults(schema => {
12
+ exports.Joi = joi_1.default.defaults(schema => {
12
13
  // hack to prevent infinite recursion due to .empty('') where '' is a stringSchema itself
13
14
  if (schema.type === 'string') {
14
15
  return (schema
@@ -1,4 +1,4 @@
1
- import * as Joi from 'joi';
1
+ import Joi from 'joi';
2
2
  import { Extension, NumberSchema as JoiNumberSchema } from 'joi';
3
3
  export interface NumberSchema<TSchema = number> extends JoiNumberSchema<TSchema> {
4
4
  dividable: (q: number) => this;
@@ -1,5 +1,5 @@
1
1
  import { Extension, StringSchema as JoiStringSchema } from 'joi';
2
- import * as Joi from 'joi';
2
+ import Joi from 'joi';
3
3
  export interface StringSchema<TSchema = string> extends JoiStringSchema<TSchema> {
4
4
  dateString: (min?: string, max?: string) => this;
5
5
  }
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.1",
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/bin/del.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import * as yargs from 'yargs'
3
+ import yargs from 'yargs'
4
4
  import { delSync } from '../fs/del'
5
5
  import { runScript } from '../script/runScript'
6
6
 
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import * as fs from 'node:fs'
4
- import * as path from 'node:path'
5
- import * as yargs from 'yargs'
3
+ import fs from 'node:fs'
4
+ import path from 'node:path'
5
+ import yargs from 'yargs'
6
6
  import { appendToBashEnv, appendToGithubEnv, appendToGithubOutput } from '../fs/json2env'
7
7
  import { runScript } from '../script/runScript'
8
8
  import { generateBuildInfo } from '../util/buildInfo.util'
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import * as yargs from 'yargs'
3
+ import yargs from 'yargs'
4
4
  import { json2env } from '../fs/json2env'
5
5
  import { runScript } from '../script/runScript'
6
6
 
package/src/bin/kpy.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import * as yargs from 'yargs'
3
+ import yargs from 'yargs'
4
4
  import { kpySync } from '../fs/kpy'
5
5
  import { runScript } from '../script/runScript'
6
6
 
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import * as yargs from 'yargs'
3
+ import yargs from 'yargs'
4
4
  import { dimGrey } from '../colors/colors'
5
5
  import { runScript } from '../script/runScript'
6
6
  import { DecryptCLIOptions, secretsDecrypt } from '../secret/secrets-decrypt.util'
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import * as yargs from 'yargs'
3
+ import yargs from 'yargs'
4
4
  import { dimGrey } from '../colors/colors'
5
5
  import { runScript } from '../script/runScript'
6
6
  import { EncryptCLIOptions, secretsEncrypt } from '../secret/secrets-encrypt.util'
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import * as crypto from 'node:crypto'
4
- import * as yargs from 'yargs'
3
+ import crypto from 'node:crypto'
4
+ import yargs from 'yargs'
5
5
  import { dimGrey } from '../colors/colors'
6
6
  import { runScript } from '../script/runScript'
7
7
 
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import * as yargs from 'yargs'
3
+ import yargs from 'yargs'
4
4
  import { SlackService } from '..'
5
5
  import { runScript } from '../script/runScript'
6
6
 
@@ -1,5 +1,5 @@
1
- import * as tty from 'node:tty'
2
- import * as chalk from 'chalk'
1
+ import tty from 'node:tty'
2
+ import chalk from 'chalk'
3
3
 
4
4
  export { chalk }
5
5
 
package/src/fs/del.ts CHANGED
@@ -1,5 +1,5 @@
1
- import * as fs from 'node:fs'
2
- import * as fsp from 'node:fs/promises'
1
+ import fs from 'node:fs'
2
+ import fsp from 'node:fs/promises'
3
3
  import { pFilter, pMap, _since } from '@naturalcycles/js-lib'
4
4
  import { dimGrey, yellow } from '../colors/colors'
5
5
  import { _pathExists, _pathExistsSync, globby } from '../index'
package/src/fs/fs.util.ts CHANGED
@@ -14,9 +14,9 @@ Credit to: fs-extra (https://github.com/jprichardson/node-fs-extra)
14
14
 
15
15
  */
16
16
 
17
- import * as fs from 'node:fs'
18
- import * as fsp from 'node:fs/promises'
19
- import * as path from 'node:path'
17
+ import fs from 'node:fs'
18
+ import fsp from 'node:fs/promises'
19
+ import path from 'node:path'
20
20
  import { _jsonParse } from '@naturalcycles/js-lib'
21
21
 
22
22
  /**
@@ -1,4 +1,4 @@
1
- import * as fs from 'node:fs'
1
+ import fs from 'node:fs'
2
2
  import { AnyObject } from '@naturalcycles/js-lib'
3
3
  import { dimGrey } from '../colors/colors'
4
4
  import { _pathExistsSync, _readJsonSync, _writeFileSync } from './fs.util'
package/src/fs/kpy.ts CHANGED
@@ -1,4 +1,4 @@
1
- import * as path from 'node:path'
1
+ import path from 'node:path'
2
2
  import { _since } from '@naturalcycles/js-lib'
3
3
  import { boldWhite, dimGrey, grey, yellow } from '../colors/colors'
4
4
  import {
package/src/index.ts CHANGED
@@ -1,10 +1,8 @@
1
1
  import Ajv from 'ajv'
2
- import * as fastGlob from 'fast-glob'
2
+ import fastGlob from 'fast-glob'
3
3
  import type { Options as FastGlobOptions } from 'fast-glob'
4
- import * as globby from 'globby'
4
+ import 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,4 +1,4 @@
1
- import * as os from 'node:os'
1
+ import os from 'node:os'
2
2
  import { _mb } from '@naturalcycles/js-lib'
3
3
 
4
4
  export function memoryUsage(): {
@@ -1,7 +1,7 @@
1
1
  import { _assert, _errorDataAppend, AnyObject, ErrorData, JWTString } from '@naturalcycles/js-lib'
2
2
  import { AnySchema } from 'joi'
3
3
  import type { Algorithm, VerifyOptions, JwtHeader, SignOptions } from 'jsonwebtoken'
4
- import * as jsonwebtoken from 'jsonwebtoken'
4
+ import jsonwebtoken from 'jsonwebtoken'
5
5
  import { anyObjectSchema } from '../validation/joi/joi.shared.schemas'
6
6
  import { validate } from '../validation/joi/joi.validation.util'
7
7
  export { jsonwebtoken }
@@ -1,5 +1,5 @@
1
- import * as path from 'node:path'
2
- import * as fs from 'node:fs'
1
+ import path from 'node:path'
2
+ import fs from 'node:fs'
3
3
  import { _assert } from '@naturalcycles/js-lib'
4
4
  import { dimGrey, yellow } from '../colors/colors'
5
5
  import { _readJsonSync, _writeJsonSync, fastGlob } from '../index'
@@ -1,5 +1,5 @@
1
- import * as fs from 'node:fs'
2
- import * as path from 'node:path'
1
+ import fs from 'node:fs'
2
+ import path from 'node:path'
3
3
  import { _assert } from '@naturalcycles/js-lib'
4
4
  import { dimGrey, yellow } from '../colors/colors'
5
5
  import { _readJsonSync, _writeJsonSync, fastGlob } from '../index'
@@ -1,4 +1,4 @@
1
- import * as crypto from 'node:crypto'
1
+ import crypto from 'node:crypto'
2
2
  import { _stringMapEntries, Base64String, StringMap } from '@naturalcycles/js-lib'
3
3
  import { md5AsBuffer, sha256AsBuffer } from './hash.util'
4
4
 
@@ -1,4 +1,4 @@
1
- import * as crypto from 'node:crypto'
1
+ import crypto from 'node:crypto'
2
2
  import { Base64String } from '@naturalcycles/js-lib'
3
3
 
4
4
  export function md5(s: string | Buffer): string {
@@ -1,4 +1,4 @@
1
- import * as crypto from 'node:crypto'
1
+ import crypto from 'node:crypto'
2
2
  import {
3
3
  ALPHABET_ALPHANUMERIC,
4
4
  ALPHABET_ALPHANUMERIC_LOWERCASE,
@@ -1,4 +1,4 @@
1
- import * as fs from 'node:fs'
1
+ import fs from 'node:fs'
2
2
  import { _assert, _jsonParseIfPossible, Base64String, StringMap } from '@naturalcycles/js-lib'
3
3
  import { decryptObject, decryptRandomIVBuffer } from './crypto.util'
4
4
 
@@ -1,4 +1,4 @@
1
- import * as fs from 'node:fs'
1
+ import fs from 'node:fs'
2
2
  import { createUnzip } from 'node:zlib'
3
3
  import { AbortableAsyncMapper, ErrorMode } from '@naturalcycles/js-lib'
4
4
  import {
@@ -1,5 +1,5 @@
1
1
  import { createUnzip, ZlibOptions } from 'node:zlib'
2
- import * as fs from 'node:fs'
2
+ import fs from 'node:fs'
3
3
  import { _hb } from '@naturalcycles/js-lib'
4
4
  import { transformTap, _pipeline, transformSplit } from '../..'
5
5
  import { dimWhite, grey } from '../../colors/colors'
@@ -1,5 +1,5 @@
1
1
  import { createGzip, ZlibOptions } from 'node:zlib'
2
- import * as fs from 'node:fs'
2
+ import fs from 'node:fs'
3
3
  import { AppError } from '@naturalcycles/js-lib'
4
4
  import { transformTap, _pipeline, _pathExistsSync, _ensureFileSync } from '../..'
5
5
  import { grey } from '../../colors/colors'
@@ -1,4 +1,4 @@
1
- import * as fs from 'node:fs'
1
+ import fs from 'node:fs'
2
2
  import type { ValuesOf } from '@naturalcycles/js-lib'
3
3
  import 'dotenv/config' // ensure .env is read before requiring keys
4
4
 
@@ -1,5 +1,5 @@
1
1
  import type { ProcessEnvOptions, SpawnOptions } from 'node:child_process'
2
- import * as cp from 'node:child_process'
2
+ import cp from 'node:child_process'
3
3
 
4
4
  export async function execVoidCommand(
5
5
  cmd: string,
@@ -1,5 +1,5 @@
1
- import * as cp from 'node:child_process'
2
- import * as path from 'node:path'
1
+ import cp from 'node:child_process'
2
+ import path from 'node:path'
3
3
  import type { UnixTimestampNumber } from '@naturalcycles/js-lib'
4
4
  import { grey } from '../colors/colors'
5
5
 
@@ -1,6 +1,6 @@
1
1
  import { promisify } from 'node:util'
2
2
  import { ZlibOptions } from 'node:zlib'
3
- import * as zlib from 'node:zlib'
3
+ import zlib from 'node:zlib'
4
4
 
5
5
  const deflate = promisify(zlib.deflate.bind(zlib))
6
6
  const inflate = promisify(zlib.inflate.bind(zlib))
@@ -1,4 +1,4 @@
1
- import * as JoiLib from 'joi'
1
+ import JoiLib from 'joi'
2
2
  import { NumberSchema, numberExtensions } from './number.extensions'
3
3
  import { StringSchema, stringExtensions } from './string.extensions'
4
4
 
@@ -1,4 +1,4 @@
1
- import * as Joi from 'joi'
1
+ import Joi from 'joi'
2
2
  import { Extension, NumberSchema as JoiNumberSchema } from 'joi'
3
3
 
4
4
  export interface NumberSchema<TSchema = number> extends JoiNumberSchema<TSchema> {
@@ -1,6 +1,6 @@
1
1
  import { LocalDate, localTime } from '@naturalcycles/js-lib'
2
2
  import { Extension, StringSchema as JoiStringSchema } from 'joi'
3
- import * as Joi from 'joi'
3
+ import Joi from 'joi'
4
4
 
5
5
  export interface StringSchema<TSchema = string> extends JoiStringSchema<TSchema> {
6
6
  dateString: (min?: string, max?: string) => this
@@ -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
- }