@naturalcycles/nodejs-lib 12.70.0 → 12.72.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.
@@ -19,6 +19,11 @@ const script_1 = require("../script");
19
19
  desc: 'Populate $BASH_ENV file if BASH_ENV env variable exists',
20
20
  default: true,
21
21
  },
22
+ githubEnv: {
23
+ type: 'boolean',
24
+ desc: 'Populate $GITHUB_ENV file if GITHUB_ENV env variable exists',
25
+ default: true,
26
+ },
22
27
  fail: {
23
28
  type: 'boolean',
24
29
  desc: 'Fail (exit status 1) on non-existing input file',
@@ -31,7 +36,7 @@ const script_1 = require("../script");
31
36
  type: 'boolean',
32
37
  },
33
38
  });
34
- const { _: args, prefix, saveEnvFile, bashEnv, fail, debug, silent } = argv;
39
+ const { _: args, prefix, saveEnvFile, bashEnv, githubEnv, fail, debug, silent } = argv;
35
40
  if (debug)
36
41
  console.log({ argv });
37
42
  const jsonPath = args[0];
@@ -40,6 +45,7 @@ const script_1 = require("../script");
40
45
  prefix,
41
46
  saveEnvFile,
42
47
  bashEnv,
48
+ githubEnv,
43
49
  fail,
44
50
  debug,
45
51
  silent,
@@ -9,6 +9,10 @@ export interface Json2EnvOptions {
9
9
  * @default true
10
10
  */
11
11
  bashEnv?: boolean;
12
+ /**
13
+ * @default true
14
+ */
15
+ githubEnv?: boolean;
12
16
  /**
13
17
  * @default true
14
18
  */
@@ -29,3 +33,15 @@ export declare function json2env(opt: Json2EnvOptions): void;
29
33
  * export b="c"
30
34
  */
31
35
  export declare function objectToShellExport(o: any, prefix?: string): string;
36
+ /**
37
+ * Turns Object with keys/values into a file of key-value pairs
38
+ *
39
+ * @example
40
+ * { a: 'b', b: 'c'}
41
+ *
42
+ * will turn into:
43
+ *
44
+ * a=b
45
+ * b=c
46
+ */
47
+ export declare function objectToGithubActionsEnv(o: any, prefix?: string): string;
@@ -1,15 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.objectToShellExport = exports.json2env = void 0;
3
+ exports.objectToGithubActionsEnv = exports.objectToShellExport = exports.json2env = void 0;
4
4
  const fs = require("fs-extra");
5
5
  const colors_1 = require("../colors");
6
6
  const JSON2ENV_OPT_DEF = {
7
7
  saveEnvFile: true,
8
8
  bashEnv: true,
9
+ githubEnv: true,
9
10
  fail: true,
10
11
  };
11
12
  function json2env(opt) {
12
- const { jsonPath, prefix, saveEnvFile, bashEnv, fail, debug, silent } = {
13
+ const { jsonPath, prefix, saveEnvFile, bashEnv, githubEnv, fail, debug, silent } = {
13
14
  ...JSON2ENV_OPT_DEF,
14
15
  ...opt,
15
16
  };
@@ -28,8 +29,9 @@ function json2env(opt) {
28
29
  // read file
29
30
  const json = fs.readJsonSync(jsonPath);
30
31
  const exportStr = objectToShellExport(json, prefix);
32
+ const githubStr = objectToGithubActionsEnv(json, prefix);
31
33
  if (debug) {
32
- console.log(json, exportStr);
34
+ console.log(json, exportStr, githubStr);
33
35
  }
34
36
  if (saveEnvFile) {
35
37
  const shPath = `${jsonPath}.sh`;
@@ -42,6 +44,9 @@ function json2env(opt) {
42
44
  if (bashEnv) {
43
45
  appendBashEnv(exportStr);
44
46
  }
47
+ if (githubEnv) {
48
+ appendGithubEnv(githubStr);
49
+ }
45
50
  }
46
51
  exports.json2env = json2env;
47
52
  function appendBashEnv(exportStr) {
@@ -51,6 +56,13 @@ function appendBashEnv(exportStr) {
51
56
  console.log(`BASH_ENV file appended (${(0, colors_1.dimGrey)(BASH_ENV)})`);
52
57
  }
53
58
  }
59
+ function appendGithubEnv(exportStr) {
60
+ const { GITHUB_ENV } = process.env;
61
+ if (GITHUB_ENV) {
62
+ fs.appendFileSync(GITHUB_ENV, exportStr + '\n');
63
+ console.log(`GITHUB_ENV file appended (${(0, colors_1.dimGrey)(GITHUB_ENV)})`);
64
+ }
65
+ }
54
66
  /**
55
67
  * Turns Object with keys/values into a *.sh script that exports all keys as values.
56
68
  *
@@ -74,3 +86,26 @@ function objectToShellExport(o, prefix = '') {
74
86
  .join('\n');
75
87
  }
76
88
  exports.objectToShellExport = objectToShellExport;
89
+ /**
90
+ * Turns Object with keys/values into a file of key-value pairs
91
+ *
92
+ * @example
93
+ * { a: 'b', b: 'c'}
94
+ *
95
+ * will turn into:
96
+ *
97
+ * a=b
98
+ * b=c
99
+ */
100
+ function objectToGithubActionsEnv(o, prefix = '') {
101
+ return Object.keys(o)
102
+ .map(k => {
103
+ const v = o[k];
104
+ if (v) {
105
+ return `${prefix}${k}=${v}`;
106
+ }
107
+ })
108
+ .filter(Boolean)
109
+ .join('\n');
110
+ }
111
+ exports.objectToGithubActionsEnv = objectToGithubActionsEnv;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getGot = void 0;
4
+ const url_1 = require("url");
4
5
  const js_lib_1 = require("@naturalcycles/js-lib");
5
6
  const got_1 = require("got");
6
7
  const __1 = require("..");
@@ -180,7 +181,9 @@ function gotBeforeRetryHook(opt) {
180
181
  const statusCode = err?.response?.statusCode || 0;
181
182
  if (statusCode && statusCode < 300) {
182
183
  // todo: possibly remove the log message completely in the future
183
- opt.logger.log(`skipping got.beforeRetry hook as statusCode is ${statusCode}, err.msg is ${err?.message}`);
184
+ // opt.logger!.log(
185
+ // `skipping got.beforeRetry hook as statusCode is ${statusCode}, err.msg is ${err?.message}`,
186
+ // )
184
187
  return;
185
188
  }
186
189
  const { method, url, prefixUrl } = options;
@@ -242,6 +245,10 @@ function gotAfterResponseHook(opt = {}) {
242
245
  };
243
246
  }
244
247
  function getShortUrl(opt, url, prefixUrl) {
248
+ if (url.password) {
249
+ url = new url_1.URL(url.toString()); // prevent original url mutation
250
+ url.password = '[redacted]';
251
+ }
245
252
  let shortUrl = url.toString();
246
253
  if (opt.logWithSearchParams === false) {
247
254
  shortUrl = shortUrl.split('?')[0];
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { ZlibOptions } from 'zlib';
3
4
  import { NDJsonStats } from './ndjson.model';
4
5
  import { TransformJsonParseOptions } from './transformJsonParse';
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { ZlibOptions } from 'zlib';
3
4
  import { NDJsonStats } from './ndjson.model';
4
5
  import { TransformToNDJsonOptions } from './transformToNDJson';
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { Readable, Transform } from 'stream';
3
4
  import { DeferredPromise } from '@naturalcycles/js-lib';
4
5
  declare type AnyStream = NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream;
@@ -1,5 +1,5 @@
1
1
  import { MemoCache } from '@naturalcycles/js-lib';
2
- import LRUCache = require('lru-cache');
2
+ import * as LRUCache from 'lru-cache';
3
3
  export declare type LRUMemoCacheOptions<KEY, VALUE> = Partial<LRUCache.Options<KEY, VALUE>>;
4
4
  /**
5
5
  * @example
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { ZlibOptions } from 'zlib';
3
4
  /**
4
5
  * deflateBuffer uses `deflate`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "12.70.0",
3
+ "version": "12.72.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
@@ -15,7 +15,6 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "@naturalcycles/js-lib": "^14.0.0",
18
- "@types/lru-cache": "^7.4.0",
19
18
  "@types/through2-concurrent": "^2.0.0",
20
19
  "ajv": "^8.6.2",
21
20
  "ajv-formats": "^2.1.0",
@@ -42,7 +41,7 @@
42
41
  "@naturalcycles/dev-lib": "^12.0.0",
43
42
  "@types/node": "^17.0.0",
44
43
  "@types/yargs": "^16.0.0",
45
- "jest": "^27.0.1",
44
+ "jest": "^28.0.3",
46
45
  "nock": "^13.0.2",
47
46
  "patch-package": "^6.2.1",
48
47
  "prettier": "^2.0.0",
@@ -19,6 +19,11 @@ runScript(() => {
19
19
  desc: 'Populate $BASH_ENV file if BASH_ENV env variable exists',
20
20
  default: true,
21
21
  },
22
+ githubEnv: {
23
+ type: 'boolean',
24
+ desc: 'Populate $GITHUB_ENV file if GITHUB_ENV env variable exists',
25
+ default: true,
26
+ },
22
27
  fail: {
23
28
  type: 'boolean',
24
29
  desc: 'Fail (exit status 1) on non-existing input file',
@@ -32,7 +37,7 @@ runScript(() => {
32
37
  },
33
38
  })
34
39
 
35
- const { _: args, prefix, saveEnvFile, bashEnv, fail, debug, silent } = argv
40
+ const { _: args, prefix, saveEnvFile, bashEnv, githubEnv, fail, debug, silent } = argv
36
41
  if (debug) console.log({ argv })
37
42
 
38
43
  const jsonPath = args[0] as string
@@ -42,6 +47,7 @@ runScript(() => {
42
47
  prefix,
43
48
  saveEnvFile,
44
49
  bashEnv,
50
+ githubEnv,
45
51
  fail,
46
52
  debug,
47
53
  silent,
@@ -15,6 +15,11 @@ export interface Json2EnvOptions {
15
15
  */
16
16
  bashEnv?: boolean
17
17
 
18
+ /**
19
+ * @default true
20
+ */
21
+ githubEnv?: boolean
22
+
18
23
  /**
19
24
  * @default true
20
25
  */
@@ -27,11 +32,12 @@ export interface Json2EnvOptions {
27
32
  const JSON2ENV_OPT_DEF: Partial<Json2EnvOptions> = {
28
33
  saveEnvFile: true,
29
34
  bashEnv: true,
35
+ githubEnv: true,
30
36
  fail: true,
31
37
  }
32
38
 
33
39
  export function json2env(opt: Json2EnvOptions): void {
34
- const { jsonPath, prefix, saveEnvFile, bashEnv, fail, debug, silent } = {
40
+ const { jsonPath, prefix, saveEnvFile, bashEnv, githubEnv, fail, debug, silent } = {
35
41
  ...JSON2ENV_OPT_DEF,
36
42
  ...opt,
37
43
  }
@@ -56,8 +62,10 @@ export function json2env(opt: Json2EnvOptions): void {
56
62
  const json = fs.readJsonSync(jsonPath)
57
63
 
58
64
  const exportStr = objectToShellExport(json, prefix)
65
+ const githubStr = objectToGithubActionsEnv(json, prefix)
66
+
59
67
  if (debug) {
60
- console.log(json, exportStr)
68
+ console.log(json, exportStr, githubStr)
61
69
  }
62
70
 
63
71
  if (saveEnvFile) {
@@ -73,6 +81,10 @@ export function json2env(opt: Json2EnvOptions): void {
73
81
  if (bashEnv) {
74
82
  appendBashEnv(exportStr)
75
83
  }
84
+
85
+ if (githubEnv) {
86
+ appendGithubEnv(githubStr)
87
+ }
76
88
  }
77
89
 
78
90
  function appendBashEnv(exportStr: string): void {
@@ -84,6 +96,15 @@ function appendBashEnv(exportStr: string): void {
84
96
  }
85
97
  }
86
98
 
99
+ function appendGithubEnv(exportStr: string): void {
100
+ const { GITHUB_ENV } = process.env
101
+ if (GITHUB_ENV) {
102
+ fs.appendFileSync(GITHUB_ENV, exportStr + '\n')
103
+
104
+ console.log(`GITHUB_ENV file appended (${dimGrey(GITHUB_ENV)})`)
105
+ }
106
+ }
107
+
87
108
  /**
88
109
  * Turns Object with keys/values into a *.sh script that exports all keys as values.
89
110
  *
@@ -106,3 +127,26 @@ export function objectToShellExport(o: any, prefix = ''): string {
106
127
  .filter(Boolean)
107
128
  .join('\n')
108
129
  }
130
+
131
+ /**
132
+ * Turns Object with keys/values into a file of key-value pairs
133
+ *
134
+ * @example
135
+ * { a: 'b', b: 'c'}
136
+ *
137
+ * will turn into:
138
+ *
139
+ * a=b
140
+ * b=c
141
+ */
142
+ export function objectToGithubActionsEnv(o: any, prefix = ''): string {
143
+ return Object.keys(o)
144
+ .map(k => {
145
+ const v = o[k]
146
+ if (v) {
147
+ return `${prefix}${k}=${v}`
148
+ }
149
+ })
150
+ .filter(Boolean)
151
+ .join('\n')
152
+ }
package/src/got/getGot.ts CHANGED
@@ -207,9 +207,9 @@ function gotBeforeRetryHook(opt: GetGotOptions): BeforeRetryHook {
207
207
 
208
208
  if (statusCode && statusCode < 300) {
209
209
  // todo: possibly remove the log message completely in the future
210
- opt.logger!.log(
211
- `skipping got.beforeRetry hook as statusCode is ${statusCode}, err.msg is ${err?.message}`,
212
- )
210
+ // opt.logger!.log(
211
+ // `skipping got.beforeRetry hook as statusCode is ${statusCode}, err.msg is ${err?.message}`,
212
+ // )
213
213
  return
214
214
  }
215
215
 
@@ -284,6 +284,11 @@ function gotAfterResponseHook(opt: GetGotOptions = {}): AfterResponseHook {
284
284
  }
285
285
 
286
286
  function getShortUrl(opt: GetGotOptions, url: URL, prefixUrl?: string): string {
287
+ if (url.password) {
288
+ url = new URL(url.toString()) // prevent original url mutation
289
+ url.password = '[redacted]'
290
+ }
291
+
287
292
  let shortUrl = url.toString()
288
293
 
289
294
  if (opt.logWithSearchParams === false) {
@@ -1,5 +1,5 @@
1
1
  import { MemoCache } from '@naturalcycles/js-lib'
2
- import LRUCache = require('lru-cache')
2
+ import * as LRUCache from 'lru-cache'
3
3
 
4
4
  // Partial, to be able to provide default `max`
5
5
  export type LRUMemoCacheOptions<KEY, VALUE> = Partial<LRUCache.Options<KEY, VALUE>>