@naturalcycles/nodejs-lib 12.103.2 → 13.0.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.
Files changed (70) hide show
  1. package/dist/bin/del.js +2 -2
  2. package/dist/bin/generate-build-info.js +6 -6
  3. package/dist/bin/json2env.js +4 -4
  4. package/dist/bin/kpy.js +2 -2
  5. package/dist/bin/secrets-decrypt.js +8 -7
  6. package/dist/bin/secrets-encrypt.js +9 -8
  7. package/dist/bin/secrets-gen-key.js +3 -3
  8. package/dist/bin/slack-this.js +2 -2
  9. package/dist/colors/colors.d.ts +28 -0
  10. package/dist/colors/colors.js +33 -1
  11. package/dist/fs/del.js +1 -1
  12. package/dist/fs/fs.util.d.ts +136 -23
  13. package/dist/fs/fs.util.js +53 -25
  14. package/dist/fs/json2env.d.ts +4 -0
  15. package/dist/fs/json2env.js +16 -5
  16. package/dist/fs/kpy.js +1 -1
  17. package/dist/index.d.ts +4 -1
  18. package/dist/index.js +4 -1
  19. package/dist/secret/secrets-decrypt.util.d.ts +3 -2
  20. package/dist/secret/secrets-decrypt.util.js +7 -5
  21. package/dist/secret/secrets-encrypt.util.d.ts +3 -2
  22. package/dist/secret/secrets-encrypt.util.js +4 -4
  23. package/dist/security/crypto.util.d.ts +22 -12
  24. package/dist/security/crypto.util.js +33 -23
  25. package/dist/security/secret.util.js +4 -2
  26. package/dist/stream/ndjson/ndjson.model.js +1 -1
  27. package/dist/stream/ndjson/pipelineFromNDJsonFile.js +1 -1
  28. package/dist/stream/ndjson/pipelineToNDJsonFile.js +1 -1
  29. package/dist/stream/sizeStack.js +1 -1
  30. package/dist/stream/transform/transformLogProgress.js +2 -3
  31. package/dist/stream/transform/transformMap.js +1 -1
  32. package/dist/stream/transform/transformMapSync.js +1 -1
  33. package/dist/util/git.util.js +1 -1
  34. package/package.json +1 -1
  35. package/readme.md +1 -1
  36. package/src/bin/del.ts +1 -1
  37. package/src/bin/generate-build-info.ts +2 -2
  38. package/src/bin/json2env.ts +2 -2
  39. package/src/bin/kpy.ts +1 -1
  40. package/src/bin/secrets-decrypt.ts +8 -6
  41. package/src/bin/secrets-encrypt.ts +9 -7
  42. package/src/bin/secrets-gen-key.ts +2 -2
  43. package/src/bin/slack-this.ts +1 -1
  44. package/src/colors/colors.ts +35 -0
  45. package/src/fs/del.ts +1 -1
  46. package/src/fs/fs.util.ts +53 -30
  47. package/src/fs/json2env.ts +15 -4
  48. package/src/fs/kpy.ts +1 -1
  49. package/src/index.ts +4 -1
  50. package/src/secret/secrets-decrypt.util.ts +9 -6
  51. package/src/secret/secrets-encrypt.util.ts +5 -5
  52. package/src/security/crypto.util.ts +34 -24
  53. package/src/security/secret.util.ts +4 -2
  54. package/src/stream/ndjson/ndjson.model.ts +1 -1
  55. package/src/stream/ndjson/pipelineFromNDJsonFile.ts +1 -1
  56. package/src/stream/ndjson/pipelineToNDJsonFile.ts +1 -1
  57. package/src/stream/sizeStack.ts +1 -1
  58. package/src/stream/transform/transformLogProgress.ts +1 -2
  59. package/src/stream/transform/transformMap.ts +1 -1
  60. package/src/stream/transform/transformMapSync.ts +1 -1
  61. package/src/util/git.util.ts +1 -1
  62. package/dist/colors/index.d.ts +0 -28
  63. package/dist/colors/index.js +0 -35
  64. package/dist/fs/index.d.ts +0 -3
  65. package/dist/fs/index.js +0 -6
  66. package/src/colors/index.ts +0 -35
  67. package/src/fs/index.ts +0 -3
  68. /package/dist/script/{index.d.ts → runScript.d.ts} +0 -0
  69. /package/dist/script/{index.js → runScript.js} +0 -0
  70. /package/src/script/{index.ts → runScript.ts} +0 -0
package/src/fs/fs.util.ts CHANGED
@@ -19,6 +19,48 @@ import * as fsp from 'node:fs/promises'
19
19
  import * as path from 'node:path'
20
20
  import { _jsonParse } from '@naturalcycles/js-lib'
21
21
 
22
+ /**
23
+ * fs2 conveniently groups filesystem functions together.
24
+ * Supposed to be almost a drop-in replacement for these things together:
25
+ *
26
+ * 1. node:fs
27
+ * 2. node:fs/promises
28
+ * 3. fs-extra
29
+ */
30
+ export const fs2 = {
31
+ // "Omit" is here to workaround this TS error:
32
+ // Exported variable 'fs2' has or is using name 'StreamOptions' from external module "fs" but cannot be named.
33
+ ...(fs as Omit<typeof fs, 'StreamOptions'>),
34
+ readFile: _readFile,
35
+ readFileSync: _readFileSync,
36
+ readFileAsBuffer: _readFileAsBuffer,
37
+ readFileAsBufferSync: _readFileAsBufferSync,
38
+ readJson: _readJson,
39
+ readJsonSync: _readJsonSync,
40
+ writeFile: _writeFile,
41
+ writeFileSync: _writeFileSync,
42
+ outputJson: _outputJson,
43
+ outputJsonSync: _outputJsonSync,
44
+ writeJson: _writeJson,
45
+ writeJsonSync: _writeJsonSync,
46
+ outputFile: _outputFile,
47
+ outputFileSync: _outputFileSync,
48
+ pathExists: _pathExists,
49
+ pathExistsSync: _pathExistsSync,
50
+ ensureDir: _ensureDir,
51
+ ensureDirSync: _ensureDirSync,
52
+ ensureFile: _ensureFile,
53
+ ensureFileSync: _ensureFileSync,
54
+ removePath: _removePath,
55
+ removePathSync: _removePathSync,
56
+ emptyDir: _emptyDir,
57
+ emptyDirSync: _emptyDirSync,
58
+ copyPath: _copyPath,
59
+ copyPathSync: _copyPathSync,
60
+ movePath: _movePath,
61
+ movePathSync: _movePathSync,
62
+ }
63
+
22
64
  export interface JsonOptions {
23
65
  spaces?: number
24
66
  }
@@ -30,6 +72,10 @@ export async function _readFile(filePath: string): Promise<string> {
30
72
  return await fsp.readFile(filePath, 'utf8')
31
73
  }
32
74
 
75
+ export async function _readFileAsBuffer(filePath: string): Promise<Buffer> {
76
+ return await fsp.readFile(filePath)
77
+ }
78
+
33
79
  /**
34
80
  * Convenience wrapper that defaults to utf-8 string output.
35
81
  */
@@ -37,26 +83,23 @@ export function _readFileSync(filePath: string): string {
37
83
  return fs.readFileSync(filePath, 'utf8')
38
84
  }
39
85
 
86
+ /**
87
+ * Convenience wrapper that defaults to utf-8 string output.
88
+ */
89
+ export function _readFileAsBufferSync(filePath: string): Buffer {
90
+ return fs.readFileSync(filePath)
91
+ }
92
+
40
93
  export async function _readJson<T = unknown>(filePath: string): Promise<T> {
41
94
  const str = await fsp.readFile(filePath, 'utf8')
42
95
  return _jsonParse(str)
43
96
  }
44
97
 
45
- /**
46
- * @deprecated use _readJson
47
- */
48
- export const _readJsonFile = _readJson
49
-
50
98
  export function _readJsonSync<T = unknown>(filePath: string): T {
51
99
  const str = fs.readFileSync(filePath, 'utf8')
52
100
  return _jsonParse(str)
53
101
  }
54
102
 
55
- /**
56
- * @deprecated use _readJsonSync
57
- */
58
- export const _readJsonFileSync = _readJsonSync
59
-
60
103
  export async function _writeFile(filePath: string, data: string | Buffer): Promise<void> {
61
104
  await fsp.writeFile(filePath, data)
62
105
  }
@@ -88,41 +131,21 @@ export async function _writeJson(filePath: string, data: any, opt?: JsonOptions)
88
131
  await fsp.writeFile(filePath, str)
89
132
  }
90
133
 
91
- /**
92
- * @deprecated use _writeJson
93
- */
94
- export const _writeJsonFile = _writeJson
95
-
96
134
  export function _writeJsonSync(filePath: string, data: any, opt?: JsonOptions): void {
97
135
  const str = JSON.stringify(data, null, opt?.spaces)
98
136
  fs.writeFileSync(filePath, str)
99
137
  }
100
138
 
101
- /**
102
- * @deprecated use _writeJsonSync
103
- */
104
- export const _writeJsonFileSync = _writeJsonSync
105
-
106
139
  export async function _outputJson(filePath: string, data: any, opt?: JsonOptions): Promise<void> {
107
140
  const str = JSON.stringify(data, null, opt?.spaces)
108
141
  await _outputFile(filePath, str)
109
142
  }
110
143
 
111
- /**
112
- * @deprecated use _outputJson
113
- */
114
- export const _outputJsonFile = _outputJson
115
-
116
144
  export function _outputJsonSync(filePath: string, data: any, opt?: JsonOptions): void {
117
145
  const str = JSON.stringify(data, null, opt?.spaces)
118
146
  _outputFileSync(filePath, str)
119
147
  }
120
148
 
121
- /**
122
- * @deprecated use _outputJsonSync
123
- */
124
- export const _outputJsonFileSync = _outputJsonSync
125
-
126
149
  export async function _pathExists(filePath: string): Promise<boolean> {
127
150
  try {
128
151
  await fsp.access(filePath)
@@ -1,6 +1,6 @@
1
1
  import * as fs from 'node:fs'
2
2
  import { AnyObject } from '@naturalcycles/js-lib'
3
- import { dimGrey } from '../colors'
3
+ import { dimGrey } from '../colors/colors'
4
4
  import { _pathExistsSync, _readJsonSync, _writeFileSync } from './fs.util'
5
5
 
6
6
  export interface Json2EnvOptions {
@@ -95,7 +95,7 @@ export function appendToBashEnv(obj: AnyObject, prefix = ''): void {
95
95
  if (BASH_ENV) {
96
96
  const data = objectToShellExport(obj, prefix)
97
97
  fs.appendFileSync(BASH_ENV, data)
98
- console.log(`BASH_ENV file appended (${dimGrey(BASH_ENV)})\n${data}`)
98
+ console.log(`BASH_ENV appended:\n${data}`)
99
99
  }
100
100
  }
101
101
 
@@ -104,7 +104,7 @@ export function appendToGithubEnv(obj: AnyObject, prefix = ''): void {
104
104
  if (GITHUB_ENV) {
105
105
  const data = objectToGithubActionsEnv(obj, prefix)
106
106
  fs.appendFileSync(GITHUB_ENV, data)
107
- console.log(`GITHUB_ENV file appended (${dimGrey(GITHUB_ENV)})\n${data}`)
107
+ console.log(`GITHUB_ENV appended:\n${data}`)
108
108
  }
109
109
  }
110
110
 
@@ -113,7 +113,18 @@ export function appendToGithubOutput(obj: AnyObject, prefix = ''): void {
113
113
  if (GITHUB_OUTPUT) {
114
114
  const data = objectToGithubActionsEnv(obj, prefix)
115
115
  fs.appendFileSync(GITHUB_OUTPUT, data)
116
- console.log(`GITHUB_OUTPUT file appended (${dimGrey(GITHUB_OUTPUT)})\n${data}`)
116
+ console.log(`GITHUB_OUTPUT appended:\n${data}`)
117
+ }
118
+ }
119
+
120
+ /**
121
+ * https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#adding-a-job-summary
122
+ */
123
+ export function appendToGithubSummary(str: string): void {
124
+ const { GITHUB_STEP_SUMMARY } = process.env
125
+ if (GITHUB_STEP_SUMMARY) {
126
+ fs.appendFileSync(GITHUB_STEP_SUMMARY, str + '\n')
127
+ console.log(`GITHUB_STEP_SUMMARY appended:\n${str}`)
117
128
  }
118
129
  }
119
130
 
package/src/fs/kpy.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as path from 'node:path'
2
2
  import { _since } from '@naturalcycles/js-lib'
3
- import { boldWhite, dimGrey, grey, yellow } from '../colors'
3
+ import { boldWhite, dimGrey, grey, yellow } from '../colors/colors'
4
4
  import {
5
5
  _copyPath,
6
6
  _copyPathSync,
package/src/index.ts CHANGED
@@ -88,9 +88,12 @@ export * from './validation/joi/joi.model'
88
88
  export * from './validation/joi/joi.shared.schemas'
89
89
  export * from './validation/joi/joi.validation.error'
90
90
  export * from './validation/joi/joi.validation.util'
91
- export * from './script'
91
+ export * from './script/runScript'
92
92
  export * from './jwt/jwt.service'
93
93
  export * from './fs/fs.util'
94
+ export * from './fs/del'
95
+ export * from './fs/json2env'
96
+ export * from './fs/kpy'
94
97
 
95
98
  export type {
96
99
  GlobbyOptions,
@@ -1,21 +1,24 @@
1
1
  import * as path from 'node:path'
2
2
  import * as fs from 'node:fs'
3
3
  import { _assert } from '@naturalcycles/js-lib'
4
- import { dimGrey, yellow } from '../colors'
4
+ import { dimGrey, yellow } from '../colors/colors'
5
5
  import { _readJsonSync, _writeJsonSync, fastGlob } from '../index'
6
6
  import { decryptObject, decryptRandomIVBuffer } from '../security/crypto.util'
7
7
 
8
8
  export interface DecryptCLIOptions {
9
9
  dir: string[]
10
10
  file?: string
11
- encKey: string
11
+ encKeyBuffer: Buffer
12
12
  del?: boolean
13
13
  jsonMode?: boolean
14
14
  }
15
15
 
16
16
  // Debug it like this:
17
- // yarn tsn ./src/bin/secrets-decrypt.ts --file ./src/test/secrets2.json --jsonMode --encKey MPd/30v0Zcce4I5mfwF4NSXrpTYD9OO4/fIqw6rjNiWp2b1GN9Xm8nQZqr7c9kKSsATqtwe0HkJFDUGzDSow44GDgDICgB1u1rGa5eNqtxnOVGRR+lIinCvN/1OnpjzeoJy2bStXPj1DKx8anMqgA8SoOZdlWRNSkEeZlolru8Ey0ujZo22dfwMyRIEniLcqvBm/iMiAkV82fn/TxYw05GarAoJcrfPeDBvuOXsARnMCyX18qTFL0iojxeTU8JHxr8TX3eXDq9cJJmridEKlwRIAzADwtetI4ttlP8lwJj1pmgsBIN3iqYssZYCkZ3HMV6BoEc7LTI5z/45rKrAT1A==
18
17
  // yarn tsn ./src/bin/secrets-encrypt.ts --file ./src/test/secrets2.plain.json --jsonMode --encKey MPd/30v0Zcce4I5mfwF4NSXrpTYD9OO4/fIqw6rjNiWp2b1GN9Xm8nQZqr7c9kKSsATqtwe0HkJFDUGzDSow44GDgDICgB1u1rGa5eNqtxnOVGRR+lIinCvN/1OnpjzeoJy2bStXPj1DKx8anMqgA8SoOZdlWRNSkEeZlolru8Ey0ujZo22dfwMyRIEniLcqvBm/iMiAkV82fn/TxYw05GarAoJcrfPeDBvuOXsARnMCyX18qTFL0iojxeTU8JHxr8TX3eXDq9cJJmridEKlwRIAzADwtetI4ttlP8lwJj1pmgsBIN3iqYssZYCkZ3HMV6BoEc7LTI5z/45rKrAT1A==
18
+ // yarn tsn ./src/bin/secrets-decrypt.ts --file ./src/test/secrets2.json --jsonMode --encKey MPd/30v0Zcce4I5mfwF4NSXrpTYD9OO4/fIqw6rjNiWp2b1GN9Xm8nQZqr7c9kKSsATqtwe0HkJFDUGzDSow44GDgDICgB1u1rGa5eNqtxnOVGRR+lIinCvN/1OnpjzeoJy2bStXPj1DKx8anMqgA8SoOZdlWRNSkEeZlolru8Ey0ujZo22dfwMyRIEniLcqvBm/iMiAkV82fn/TxYw05GarAoJcrfPeDBvuOXsARnMCyX18qTFL0iojxeTU8JHxr8TX3eXDq9cJJmridEKlwRIAzADwtetI4ttlP8lwJj1pmgsBIN3iqYssZYCkZ3HMV6BoEc7LTI5z/45rKrAT1A==
19
+
20
+ // yarn tsn ./src/bin/secrets-encrypt.ts --file ./src/test/secrets.json -encKey MPd/30v0Zcce4I5mfwF4NSXrpTYD9OO4/fIqw6rjNiWp2b1GN9Xm8nQZqr7c9kKSsATqtwe0HkJFDUGzDSow44GDgDICgB1u1rGa5eNqtxnOVGRR+lIinCvN/1OnpjzeoJy2bStXPj1DKx8anMqgA8SoOZdlWRNSkEeZlolru8Ey0ujZo22dfwMyRIEniLcqvBm/iMiAkV82fn/TxYw05GarAoJcrfPeDBvuOXsARnMCyX18qTFL0iojxeTU8JHxr8TX3eXDq9cJJmridEKlwRIAzADwtetI4ttlP8lwJj1pmgsBIN3iqYssZYCkZ3HMV6BoEc7LTI5z/45rKrAT1A==
21
+ // yarn tsn ./src/bin/secrets-decrypt.ts --file ./src/test/secrets.json.enc -encKey MPd/30v0Zcce4I5mfwF4NSXrpTYD9OO4/fIqw6rjNiWp2b1GN9Xm8nQZqr7c9kKSsATqtwe0HkJFDUGzDSow44GDgDICgB1u1rGa5eNqtxnOVGRR+lIinCvN/1OnpjzeoJy2bStXPj1DKx8anMqgA8SoOZdlWRNSkEeZlolru8Ey0ujZo22dfwMyRIEniLcqvBm/iMiAkV82fn/TxYw05GarAoJcrfPeDBvuOXsARnMCyX18qTFL0iojxeTU8JHxr8TX3eXDq9cJJmridEKlwRIAzADwtetI4ttlP8lwJj1pmgsBIN3iqYssZYCkZ3HMV6BoEc7LTI5z/45rKrAT1A==
19
22
 
20
23
  /**
21
24
  * Decrypts all files in given directory (*.enc), saves decrypted versions without ending `.enc`.
@@ -24,7 +27,7 @@ export interface DecryptCLIOptions {
24
27
  export function secretsDecrypt(
25
28
  dir: string[],
26
29
  file: string | undefined,
27
- encKey: string,
30
+ encKeyBuffer: Buffer,
28
31
  del = false,
29
32
  jsonMode = false,
30
33
  ): void {
@@ -44,12 +47,12 @@ export function secretsDecrypt(
44
47
  )
45
48
  plainFilename = filename.replace('.json', '.plain.json')
46
49
 
47
- const json = decryptObject(_readJsonSync(filename), encKey)
50
+ const json = decryptObject(_readJsonSync(filename), encKeyBuffer)
48
51
 
49
52
  _writeJsonSync(plainFilename, json, { spaces: 2 })
50
53
  } else {
51
54
  const enc = fs.readFileSync(filename)
52
- const plain = decryptRandomIVBuffer(enc, encKey)
55
+ const plain = decryptRandomIVBuffer(enc, encKeyBuffer)
53
56
  plainFilename = filename.slice(0, filename.length - '.enc'.length)
54
57
  fs.writeFileSync(plainFilename, plain)
55
58
  }
@@ -1,14 +1,14 @@
1
1
  import * as fs from 'node:fs'
2
2
  import * as path from 'node:path'
3
3
  import { _assert } from '@naturalcycles/js-lib'
4
- import { dimGrey, yellow } from '../colors'
4
+ import { dimGrey, yellow } from '../colors/colors'
5
5
  import { _readJsonSync, _writeJsonSync, fastGlob } from '../index'
6
6
  import { encryptObject, encryptRandomIVBuffer } from '../security/crypto.util'
7
7
 
8
8
  export interface EncryptCLIOptions {
9
9
  pattern: string[]
10
10
  file?: string
11
- encKey: string
11
+ encKeyBuffer: Buffer
12
12
  del?: boolean
13
13
  jsonMode?: boolean
14
14
  }
@@ -20,7 +20,7 @@ export interface EncryptCLIOptions {
20
20
  export function secretsEncrypt(
21
21
  pattern: string[],
22
22
  file: string | undefined,
23
- encKey: string,
23
+ encKeyBuffer: Buffer,
24
24
  del = false,
25
25
  jsonMode = false,
26
26
  ): void {
@@ -41,12 +41,12 @@ export function secretsEncrypt(
41
41
  )
42
42
  encFilename = filename.replace('.plain', '')
43
43
 
44
- const json = encryptObject(_readJsonSync(filename), encKey)
44
+ const json = encryptObject(_readJsonSync(filename), encKeyBuffer)
45
45
 
46
46
  _writeJsonSync(encFilename, json, { spaces: 2 })
47
47
  } else {
48
48
  const plain = fs.readFileSync(filename)
49
- const enc = encryptRandomIVBuffer(plain, encKey)
49
+ const enc = encryptRandomIVBuffer(plain, encKeyBuffer)
50
50
  encFilename = `${filename}.enc`
51
51
  fs.writeFileSync(encFilename, enc)
52
52
  }
@@ -1,15 +1,15 @@
1
1
  import * as crypto from 'node:crypto'
2
2
  import { _stringMapEntries, Base64String, StringMap } from '@naturalcycles/js-lib'
3
- import { md5 } from './hash.util'
3
+ import { md5AsBuffer, sha256AsBuffer } from './hash.util'
4
4
 
5
5
  const algorithm = 'aes-256-cbc'
6
6
 
7
7
  /**
8
- * Using aes-256-cbc
8
+ * Using aes-256-cbc.
9
9
  */
10
- export function encryptRandomIVBuffer(input: Buffer, secretKeyBase64: Base64String): Buffer {
11
- // md5 to match aes-256 key length of 32 bytes
12
- const key = md5(Buffer.from(secretKeyBase64, 'base64'))
10
+ export function encryptRandomIVBuffer(input: Buffer, secretKeyBuffer: Buffer): Buffer {
11
+ // sha256 to match aes-256 key length
12
+ const key = sha256AsBuffer(secretKeyBuffer)
13
13
 
14
14
  // Random iv to achieve non-deterministic encryption (but deterministic decryption)
15
15
  const iv = crypto.randomBytes(16)
@@ -19,11 +19,11 @@ export function encryptRandomIVBuffer(input: Buffer, secretKeyBase64: Base64Stri
19
19
  }
20
20
 
21
21
  /**
22
- * Using aes-256-cbc
22
+ * Using aes-256-cbc.
23
23
  */
24
- export function decryptRandomIVBuffer(input: Buffer, secretKeyBase64: Base64String): Buffer {
25
- // md5 to match aes-256 key length of 32 bytes
26
- const key = md5(Buffer.from(secretKeyBase64, 'base64'))
24
+ export function decryptRandomIVBuffer(input: Buffer, secretKeyBuffer: Buffer): Buffer {
25
+ // sha256 to match aes-256 key length
26
+ const key = sha256AsBuffer(secretKeyBuffer)
27
27
 
28
28
  // iv is first 16 bytes of encrypted buffer, the rest is payload
29
29
  const iv = input.subarray(0, 16)
@@ -35,11 +35,11 @@ export function decryptRandomIVBuffer(input: Buffer, secretKeyBase64: Base64Stri
35
35
  }
36
36
 
37
37
  /**
38
- * Decrypts all object values.
39
- * Returns object with decrypted values.
38
+ * Decrypts all object values (base64 strings).
39
+ * Returns object with decrypted values (utf8 strings).
40
40
  */
41
- export function decryptObject(obj: StringMap<Base64String>, secretKey: string): StringMap {
42
- const { key, iv } = getCryptoParams(secretKey)
41
+ export function decryptObject(obj: StringMap<Base64String>, secretKeyBuffer: Buffer): StringMap {
42
+ const { key, iv } = getCryptoParams(secretKeyBuffer)
43
43
 
44
44
  const r: StringMap = {}
45
45
  _stringMapEntries(obj).forEach(([k, v]) => {
@@ -49,8 +49,12 @@ export function decryptObject(obj: StringMap<Base64String>, secretKey: string):
49
49
  return r
50
50
  }
51
51
 
52
- export function encryptObject(obj: StringMap, secretKey: string): StringMap<Base64String> {
53
- const { key, iv } = getCryptoParams(secretKey)
52
+ /**
53
+ * Encrypts all object values (utf8 strings).
54
+ * Returns object with encrypted values (base64 strings).
55
+ */
56
+ export function encryptObject(obj: StringMap, secretKeyBuffer: Buffer): StringMap<Base64String> {
57
+ const { key, iv } = getCryptoParams(secretKeyBuffer)
54
58
 
55
59
  const r: StringMap = {}
56
60
  _stringMapEntries(obj).forEach(([k, v]) => {
@@ -61,25 +65,31 @@ export function encryptObject(obj: StringMap, secretKey: string): StringMap<Base
61
65
  }
62
66
 
63
67
  /**
64
- * Using aes-256-cbc
68
+ * Using aes-256-cbc.
69
+ *
70
+ * Input is base64 string.
71
+ * Output is utf8 string.
65
72
  */
66
- export function decryptString(str: Base64String, secretKey: string): string {
67
- const { key, iv } = getCryptoParams(secretKey)
73
+ export function decryptString(str: Base64String, secretKeyBuffer: Buffer): string {
74
+ const { key, iv } = getCryptoParams(secretKeyBuffer)
68
75
  const decipher = crypto.createDecipheriv(algorithm, key, iv)
69
76
  return decipher.update(str, 'base64', 'utf8') + decipher.final('utf8')
70
77
  }
71
78
 
72
79
  /**
73
- * Using aes-256-cbc
80
+ * Using aes-256-cbc.
81
+ *
82
+ * Input is utf8 string.
83
+ * Output is base64 string.
74
84
  */
75
- export function encryptString(str: string, secretKey: string): Base64String {
76
- const { key, iv } = getCryptoParams(secretKey)
85
+ export function encryptString(str: string, secretKeyBuffer: Buffer): Base64String {
86
+ const { key, iv } = getCryptoParams(secretKeyBuffer)
77
87
  const cipher = crypto.createCipheriv(algorithm, key, iv)
78
88
  return cipher.update(str, 'utf8', 'base64') + cipher.final('base64')
79
89
  }
80
90
 
81
- function getCryptoParams(secretKey: string): { key: string; iv: string } {
82
- const key = md5(secretKey)
83
- const iv = md5(secretKey + key).slice(0, 16)
91
+ function getCryptoParams(secretKeyBuffer: Buffer): { key: Buffer; iv: Buffer } {
92
+ const key = sha256AsBuffer(secretKeyBuffer)
93
+ const iv = md5AsBuffer(Buffer.concat([secretKeyBuffer, key]))
84
94
  return { key, iv }
85
95
  }
@@ -62,7 +62,8 @@ export function loadSecretsFromEncryptedJsonFile(
62
62
 
63
63
  if (secretEncryptionKey) {
64
64
  const buf = fs.readFileSync(filePath)
65
- const plain = decryptRandomIVBuffer(buf, secretEncryptionKey).toString('utf8')
65
+ const encKeyBuffer = Buffer.from(secretEncryptionKey, 'base64')
66
+ const plain = decryptRandomIVBuffer(buf, encKeyBuffer).toString('utf8')
66
67
  secrets = JSON.parse(plain)
67
68
  } else {
68
69
  secrets = JSON.parse(fs.readFileSync(filePath, 'utf8'))
@@ -94,7 +95,8 @@ export function loadSecretsFromEncryptedJsonFileValues(
94
95
  let secrets: StringMap = JSON.parse(fs.readFileSync(filePath, 'utf8'))
95
96
 
96
97
  if (secretEncryptionKey) {
97
- secrets = decryptObject(secrets, secretEncryptionKey)
98
+ const encKeyBuffer = Buffer.from(secretEncryptionKey, 'base64')
99
+ secrets = decryptObject(secrets, encKeyBuffer)
98
100
  }
99
101
 
100
102
  Object.entries(secrets).forEach(([k, v]) => (secretMap[k.toUpperCase()] = v))
@@ -1,5 +1,5 @@
1
1
  import { _hb, _ms } from '@naturalcycles/js-lib'
2
- import { boldWhite, dimWhite } from '../../colors'
2
+ import { boldWhite, dimWhite } from '../../colors/colors'
3
3
 
4
4
  export class NDJsonStats {
5
5
  static create(o: Partial<NDJsonStats> = {}): NDJsonStats {
@@ -2,7 +2,7 @@ import { createUnzip, ZlibOptions } from 'node:zlib'
2
2
  import * as fs from 'node:fs'
3
3
  import { _hb } from '@naturalcycles/js-lib'
4
4
  import { transformTap, _pipeline, transformSplit } from '../..'
5
- import { dimWhite, grey } from '../../colors'
5
+ import { dimWhite, grey } from '../../colors/colors'
6
6
  import { NDJsonStats } from './ndjson.model'
7
7
  import { transformJsonParse, TransformJsonParseOptions } from './transformJsonParse'
8
8
 
@@ -2,7 +2,7 @@ import { createGzip, ZlibOptions } from 'node:zlib'
2
2
  import * as fs from 'node:fs'
3
3
  import { AppError } from '@naturalcycles/js-lib'
4
4
  import { transformTap, _pipeline, _pathExistsSync, _ensureFileSync } from '../..'
5
- import { grey } from '../../colors'
5
+ import { grey } from '../../colors/colors'
6
6
  import { NDJsonStats } from './ndjson.model'
7
7
  import { transformToNDJson, TransformToNDJsonOptions } from './transformToNDJson'
8
8
 
@@ -1,5 +1,5 @@
1
1
  import { _hb, CommonLogger, NumberStack } from '@naturalcycles/js-lib'
2
- import { yellow } from '../colors'
2
+ import { yellow } from '../colors/colors'
3
3
  import { gzipBuffer } from '../util/zip.util'
4
4
 
5
5
  export class SizeStack extends NumberStack {
@@ -8,8 +8,7 @@ import {
8
8
  CommonLogger,
9
9
  localTime,
10
10
  } from '@naturalcycles/js-lib'
11
- import { boldWhite, dimGrey, white, yellow } from '../../colors'
12
- import { hasColors } from '../../colors/colors'
11
+ import { hasColors, boldWhite, dimGrey, white, yellow } from '../../colors/colors'
13
12
  import { SizeStack } from '../sizeStack'
14
13
  import { TransformOptions, TransformTyped } from '../stream.model'
15
14
 
@@ -9,7 +9,7 @@ import {
9
9
  SKIP,
10
10
  } from '@naturalcycles/js-lib'
11
11
  import through2Concurrent = require('through2-concurrent')
12
- import { yellow } from '../../colors'
12
+ import { yellow } from '../../colors/colors'
13
13
  import { AbortableTransform } from '../pipeline/pipeline'
14
14
  import { TransformTyped } from '../stream.model'
15
15
  import { pipelineClose } from '../stream.util'
@@ -7,7 +7,7 @@ import {
7
7
  Predicate,
8
8
  SKIP,
9
9
  } from '@naturalcycles/js-lib'
10
- import { yellow } from '../../colors'
10
+ import { yellow } from '../../colors/colors'
11
11
  import { AbortableTransform } from '../pipeline/pipeline'
12
12
  import { TransformTyped } from '../stream.model'
13
13
  import { pipelineClose } from '../stream.util'
@@ -1,7 +1,7 @@
1
1
  import * as cp from 'node:child_process'
2
2
  import * as path from 'node:path'
3
3
  import type { UnixTimestampNumber } from '@naturalcycles/js-lib'
4
- import { grey } from '../colors'
4
+ import { grey } from '../colors/colors'
5
5
 
6
6
  export function getLastGitCommitMsg(): string {
7
7
  return execSync('git log -1 --pretty=%B')
@@ -1,28 +0,0 @@
1
- import * as chalk from 'chalk';
2
- export { chalk };
3
- export declare const white: chalk.Chalk;
4
- export declare const dimWhite: chalk.Chalk;
5
- export declare const boldWhite: chalk.Chalk;
6
- export declare const inverseWhite: chalk.Chalk;
7
- export declare const grey: chalk.Chalk;
8
- export declare const dimGrey: chalk.Chalk;
9
- export declare const boldGrey: chalk.Chalk;
10
- export declare const yellow: chalk.Chalk;
11
- export declare const dimYellow: chalk.Chalk;
12
- export declare const boldYellow: chalk.Chalk;
13
- export declare const inverseYellow: chalk.Chalk;
14
- export declare const green: chalk.Chalk;
15
- export declare const dimGreen: chalk.Chalk;
16
- export declare const boldGreen: chalk.Chalk;
17
- export declare const red: chalk.Chalk;
18
- export declare const dimRed: chalk.Chalk;
19
- export declare const boldRed: chalk.Chalk;
20
- export declare const blue: chalk.Chalk;
21
- export declare const dimBlue: chalk.Chalk;
22
- export declare const boldBlue: chalk.Chalk;
23
- export declare const magenta: chalk.Chalk;
24
- export declare const dimMagenta: chalk.Chalk;
25
- export declare const boldMagenta: chalk.Chalk;
26
- export declare const cyan: chalk.Chalk;
27
- export declare const dimCyan: chalk.Chalk;
28
- export declare const boldCyan: chalk.Chalk;
@@ -1,35 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.boldCyan = exports.dimCyan = exports.cyan = exports.boldMagenta = exports.dimMagenta = exports.magenta = exports.boldBlue = exports.dimBlue = exports.blue = exports.boldRed = exports.dimRed = exports.red = exports.boldGreen = exports.dimGreen = exports.green = exports.inverseYellow = exports.boldYellow = exports.dimYellow = exports.yellow = exports.boldGrey = exports.dimGrey = exports.grey = exports.inverseWhite = exports.boldWhite = exports.dimWhite = exports.white = exports.chalk = void 0;
4
- const chalk = require("chalk");
5
- exports.chalk = chalk;
6
- // The point of re-exporting is:
7
- // 1. Fix typings to allow to pass `number` (very common case)
8
- // 2. Easier/shorter to import, rather than from 'chalk'
9
- // export type ColorFn = (...args: (string | number)[]) => string
10
- exports.white = chalk.white;
11
- exports.dimWhite = chalk.dim.white;
12
- exports.boldWhite = chalk.bold.white;
13
- exports.inverseWhite = chalk.inverse.white;
14
- exports.grey = chalk.grey;
15
- exports.dimGrey = chalk.dim.grey;
16
- exports.boldGrey = chalk.bold.grey;
17
- exports.yellow = chalk.yellow;
18
- exports.dimYellow = chalk.dim.yellow;
19
- exports.boldYellow = chalk.bold.yellow;
20
- exports.inverseYellow = chalk.inverse.yellow;
21
- exports.green = chalk.green;
22
- exports.dimGreen = chalk.dim.green;
23
- exports.boldGreen = chalk.bold.green;
24
- exports.red = chalk.red;
25
- exports.dimRed = chalk.dim.red;
26
- exports.boldRed = chalk.bold.red;
27
- exports.blue = chalk.blue;
28
- exports.dimBlue = chalk.dim.blue;
29
- exports.boldBlue = chalk.bold.blue;
30
- exports.magenta = chalk.magenta;
31
- exports.dimMagenta = chalk.dim.magenta;
32
- exports.boldMagenta = chalk.bold.magenta;
33
- exports.cyan = chalk.cyan;
34
- exports.dimCyan = chalk.dim.cyan;
35
- exports.boldCyan = chalk.bold.cyan;
@@ -1,3 +0,0 @@
1
- export * from './del';
2
- export * from './json2env';
3
- export * from './kpy';
package/dist/fs/index.js DELETED
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./del"), exports);
5
- tslib_1.__exportStar(require("./json2env"), exports);
6
- tslib_1.__exportStar(require("./kpy"), exports);
@@ -1,35 +0,0 @@
1
- import * as chalk from 'chalk'
2
-
3
- export { chalk }
4
-
5
- // The point of re-exporting is:
6
- // 1. Fix typings to allow to pass `number` (very common case)
7
- // 2. Easier/shorter to import, rather than from 'chalk'
8
- // export type ColorFn = (...args: (string | number)[]) => string
9
-
10
- export const white = chalk.white
11
- export const dimWhite = chalk.dim.white
12
- export const boldWhite = chalk.bold.white
13
- export const inverseWhite = chalk.inverse.white
14
- export const grey = chalk.grey
15
- export const dimGrey = chalk.dim.grey
16
- export const boldGrey = chalk.bold.grey
17
- export const yellow = chalk.yellow
18
- export const dimYellow = chalk.dim.yellow
19
- export const boldYellow = chalk.bold.yellow
20
- export const inverseYellow = chalk.inverse.yellow
21
- export const green = chalk.green
22
- export const dimGreen = chalk.dim.green
23
- export const boldGreen = chalk.bold.green
24
- export const red = chalk.red
25
- export const dimRed = chalk.dim.red
26
- export const boldRed = chalk.bold.red
27
- export const blue = chalk.blue
28
- export const dimBlue = chalk.dim.blue
29
- export const boldBlue = chalk.bold.blue
30
- export const magenta = chalk.magenta
31
- export const dimMagenta = chalk.dim.magenta
32
- export const boldMagenta = chalk.bold.magenta
33
- export const cyan = chalk.cyan
34
- export const dimCyan = chalk.dim.cyan
35
- export const boldCyan = chalk.bold.cyan
package/src/fs/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from './del'
2
- export * from './json2env'
3
- export * from './kpy'
File without changes
File without changes
File without changes