@naturalcycles/nodejs-lib 12.86.0 → 12.87.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.
@@ -24,8 +24,8 @@ export declare function loadSecretsFromEncryptedJsonFile(filePath: string, secre
24
24
  * For whole-file encryption - use `loadSecretsFromEncryptedJsonFile`
25
25
  */
26
26
  export declare function loadSecretsFromEncryptedJsonFileValues(filePath: string, secretEncryptionKey?: Base64String): void;
27
- export declare function secret<T = string>(k: string): T;
28
- export declare function secretOptional<T = string>(k: string): T | undefined;
27
+ export declare function secret<T = string>(k: string, parseJson?: boolean): T;
28
+ export declare function secretOptional<T = string>(k: string, parseJson?: boolean): T | undefined;
29
29
  export declare function getSecretMap(): StringMap;
30
30
  /**
31
31
  * REPLACES secretMap with new map.
@@ -78,17 +78,23 @@ function loadSecretsFromEncryptedJsonFileValues(filePath, secretEncryptionKey) {
78
78
  .join(', ')}`);
79
79
  }
80
80
  exports.loadSecretsFromEncryptedJsonFileValues = loadSecretsFromEncryptedJsonFileValues;
81
- function secret(k) {
82
- const v = secretOptional(k);
81
+ function secret(k, parseJson = false) {
82
+ const v = secretOptional(k, parseJson);
83
83
  if (!v) {
84
84
  throw new Error(`secret(${k.toUpperCase()}) not found!`);
85
85
  }
86
86
  return v;
87
87
  }
88
88
  exports.secret = secret;
89
- function secretOptional(k) {
89
+ function secretOptional(k, parseJson = false) {
90
90
  requireLoaded();
91
- return secretMap[k.toUpperCase()];
91
+ let v = secretMap[k.toUpperCase()];
92
+ if (!v)
93
+ return;
94
+ if (parseJson) {
95
+ v = (0, js_lib_1._jsonParseIfPossible)(v);
96
+ }
97
+ return v;
92
98
  }
93
99
  exports.secretOptional = secretOptional;
94
100
  function getSecretMap() {
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AbortableTransform = exports._pipelineToArray = exports._pipeline = void 0;
4
4
  const node_stream_1 = require("node:stream");
5
+ const promises_1 = require("node:stream/promises");
5
6
  const js_lib_1 = require("@naturalcycles/js-lib");
6
7
  const index_1 = require("../../index");
7
8
  /**
@@ -52,19 +53,17 @@ async function _pipeline(streams, opt = {}) {
52
53
  }
53
54
  });
54
55
  }
55
- return new Promise((resolve, reject) => {
56
- (0, node_stream_1.pipeline)(first, ...rest, (err) => {
57
- if (err) {
58
- if (opt.allowClose && err?.code === 'ERR_STREAM_PREMATURE_CLOSE') {
59
- console.log('_pipeline closed (as expected)');
60
- return resolve();
61
- }
62
- // console.log(`_pipeline error`, err)
63
- return reject(err);
64
- }
65
- resolve();
66
- });
67
- });
56
+ try {
57
+ return await (0, promises_1.pipeline)(first, ...rest);
58
+ }
59
+ catch (err) {
60
+ if (opt.allowClose && err?.code === 'ERR_STREAM_PREMATURE_CLOSE') {
61
+ console.log('_pipeline closed (as expected)');
62
+ return;
63
+ }
64
+ // console.log(`_pipeline error`, err)
65
+ throw err;
66
+ }
68
67
  }
69
68
  exports._pipeline = _pipeline;
70
69
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "12.86.0",
3
+ "version": "12.87.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
@@ -78,7 +78,7 @@
78
78
  "url": "https://github.com/NaturalCycles/nodejs-lib"
79
79
  },
80
80
  "engines": {
81
- "node": ">=16.10.0"
81
+ "node": ">=18.12.0"
82
82
  },
83
83
  "description": "Standard library for Node.js",
84
84
  "author": "Natural Cycles Team",
@@ -1,5 +1,5 @@
1
1
  import * as fs from 'node:fs'
2
- import { _assert, Base64String, StringMap } from '@naturalcycles/js-lib'
2
+ import { _assert, _jsonParseIfPossible, Base64String, StringMap } from '@naturalcycles/js-lib'
3
3
  import { decryptObject, decryptRandomIVBuffer } from './crypto.util'
4
4
 
5
5
  let loaded = false
@@ -107,8 +107,8 @@ export function loadSecretsFromEncryptedJsonFileValues(
107
107
  )
108
108
  }
109
109
 
110
- export function secret<T = string>(k: string): T {
111
- const v = secretOptional(k)
110
+ export function secret<T = string>(k: string, parseJson = false): T {
111
+ const v = secretOptional(k, parseJson)
112
112
  if (!v) {
113
113
  throw new Error(`secret(${k.toUpperCase()}) not found!`)
114
114
  }
@@ -116,9 +116,16 @@ export function secret<T = string>(k: string): T {
116
116
  return v as any
117
117
  }
118
118
 
119
- export function secretOptional<T = string>(k: string): T | undefined {
119
+ export function secretOptional<T = string>(k: string, parseJson = false): T | undefined {
120
120
  requireLoaded()
121
- return secretMap[k.toUpperCase()] as T | undefined
121
+ let v = secretMap[k.toUpperCase()]
122
+ if (!v) return
123
+
124
+ if (parseJson) {
125
+ v = _jsonParseIfPossible(v)
126
+ }
127
+
128
+ return v as T
122
129
  }
123
130
 
124
131
  export function getSecretMap(): StringMap {
@@ -1,4 +1,5 @@
1
- import { pipeline, Readable, Transform, Writable } from 'node:stream'
1
+ import { Readable, Transform, Writable } from 'node:stream'
2
+ import { pipeline } from 'node:stream/promises'
2
3
  import { _last, AnyFunction, DeferredPromise, pDefer } from '@naturalcycles/js-lib'
3
4
  import { writablePushToArray } from '../../index'
4
5
 
@@ -74,20 +75,16 @@ export async function _pipeline(streams: AnyStream[], opt: PipelineOptions = {})
74
75
  })
75
76
  }
76
77
 
77
- return new Promise<void>((resolve, reject) => {
78
- pipeline(first, ...(rest as any[]), (err: Error) => {
79
- if (err) {
80
- if (opt.allowClose && (err as any)?.code === 'ERR_STREAM_PREMATURE_CLOSE') {
81
- console.log('_pipeline closed (as expected)')
82
- return resolve()
83
- }
84
- // console.log(`_pipeline error`, err)
85
- return reject(err)
86
- }
87
-
88
- resolve()
89
- })
90
- })
78
+ try {
79
+ return await pipeline(first, ...(rest as any[]))
80
+ } catch (err) {
81
+ if (opt.allowClose && (err as any)?.code === 'ERR_STREAM_PREMATURE_CLOSE') {
82
+ console.log('_pipeline closed (as expected)')
83
+ return
84
+ }
85
+ // console.log(`_pipeline error`, err)
86
+ throw err
87
+ }
91
88
  }
92
89
 
93
90
  /**