@naturalcycles/nodejs-lib 12.75.1 → 12.77.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.
@@ -7,7 +7,7 @@ exports._chunkBuffer = void 0;
7
7
  function _chunkBuffer(buf, size) {
8
8
  const out = [];
9
9
  for (let i = 0; i < buf.length; i += size) {
10
- out.push(buf.slice(i, i + size));
10
+ out.push(buf.subarray(i, i + size));
11
11
  }
12
12
  return out;
13
13
  }
package/dist/fs/del.d.ts CHANGED
@@ -19,3 +19,4 @@ export declare type DelSingleOption = string;
19
19
  * @experimental
20
20
  */
21
21
  export declare function del(_opt: DelOptions | DelSingleOption): Promise<void>;
22
+ export declare function delSync(_opt: DelOptions | DelSingleOption): void;
package/dist/fs/del.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.del = void 0;
3
+ exports.delSync = exports.del = void 0;
4
4
  const js_lib_1 = require("@naturalcycles/js-lib");
5
5
  const fs = require("fs-extra");
6
6
  const colors_1 = require("../colors");
@@ -70,6 +70,59 @@ async function del(_opt) {
70
70
  }
71
71
  }
72
72
  exports.del = del;
73
+ function delSync(_opt) {
74
+ const started = Date.now();
75
+ // Convert DelSingleOption to DelOptions
76
+ if (typeof _opt === 'string') {
77
+ _opt = {
78
+ patterns: [_opt],
79
+ };
80
+ }
81
+ const opt = {
82
+ ...DEF_OPT,
83
+ ..._opt,
84
+ };
85
+ const { patterns, verbose, silent, debug, dry } = opt;
86
+ if (debug) {
87
+ console.log(opt);
88
+ }
89
+ // 1. glob only files, expand dirs, delete
90
+ const filenames = index_1.globby.sync(patterns, {
91
+ dot: true,
92
+ expandDirectories: true,
93
+ onlyFiles: true,
94
+ });
95
+ if (verbose || debug || dry) {
96
+ console.log(`Will delete ${(0, colors_1.yellow)(filenames.length)} files:`, filenames);
97
+ }
98
+ if (dry)
99
+ return;
100
+ filenames.forEach(filepath => fs.removeSync(filepath));
101
+ // 2. glob only dirs, expand, delete only empty!
102
+ let dirnames = index_1.globby.sync(patterns, {
103
+ dot: true,
104
+ expandDirectories: true,
105
+ onlyDirectories: true,
106
+ });
107
+ // Add original patterns (if any of them are dirs)
108
+ dirnames = dirnames.concat(patterns.filter(p => fs.pathExistsSync(p) && fs.lstatSync(p).isDirectory()));
109
+ const dirnamesSorted = dirnames.sort().reverse();
110
+ // console.log({ dirnamesSorted })
111
+ const deletedDirs = [];
112
+ for (const dirpath of dirnamesSorted) {
113
+ if (isEmptyDirSync(dirpath)) {
114
+ // console.log(`empty dir: ${dirpath}`)
115
+ fs.removeSync(dirpath);
116
+ deletedDirs.push(dirpath);
117
+ }
118
+ }
119
+ if (verbose || debug)
120
+ console.log({ deletedDirs });
121
+ if (!silent) {
122
+ console.log(`del deleted ${(0, colors_1.yellow)(filenames.length)} files and ${(0, colors_1.yellow)(deletedDirs.length)} dirs ${(0, colors_1.dimGrey)((0, js_lib_1._since)(started))}`);
123
+ }
124
+ }
125
+ exports.delSync = delSync;
73
126
  // Improved algorithm:
74
127
  // 1. glob only files, expand dirs, delete
75
128
  // 2. glob only dirs, expand, delete only empty!
@@ -77,3 +130,6 @@ exports.del = del;
77
130
  async function isEmptyDir(dir) {
78
131
  return (await fs.readdir(dir)).length === 0;
79
132
  }
133
+ function isEmptyDirSync(dir) {
134
+ return fs.readdirSync(dir).length === 0;
135
+ }
@@ -1,5 +1,5 @@
1
- import { del, DelOptions } from './del';
1
+ import { del, delSync, DelOptions } from './del';
2
2
  import { json2env, objectToShellExport } from './json2env';
3
3
  import { kpy, KpyOptions, kpySync } from './kpy';
4
4
  export type { KpyOptions, DelOptions };
5
- export { kpy, kpySync, del, objectToShellExport, json2env };
5
+ export { kpy, kpySync, del, delSync, objectToShellExport, json2env };
package/dist/fs/index.js CHANGED
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.json2env = exports.objectToShellExport = exports.del = exports.kpySync = exports.kpy = void 0;
3
+ exports.json2env = exports.objectToShellExport = exports.delSync = exports.del = exports.kpySync = exports.kpy = void 0;
4
4
  const del_1 = require("./del");
5
5
  Object.defineProperty(exports, "del", { enumerable: true, get: function () { return del_1.del; } });
6
+ Object.defineProperty(exports, "delSync", { enumerable: true, get: function () { return del_1.delSync; } });
6
7
  const json2env_1 = require("./json2env");
7
8
  Object.defineProperty(exports, "json2env", { enumerable: true, get: function () { return json2env_1.json2env; } });
8
9
  Object.defineProperty(exports, "objectToShellExport", { enumerable: true, get: function () { return json2env_1.objectToShellExport; } });
@@ -24,8 +24,8 @@ function decryptRandomIVBuffer(input, secretKeyBase64) {
24
24
  // md5 to match aes-256 key length of 32 bytes
25
25
  const key = (0, hash_util_1.md5)(Buffer.from(secretKeyBase64, 'base64'));
26
26
  // iv is first 16 bytes of encrypted buffer, the rest is payload
27
- const iv = input.slice(0, 16);
28
- const payload = input.slice(16);
27
+ const iv = input.subarray(0, 16);
28
+ const payload = input.subarray(16);
29
29
  const decipher = crypto.createDecipheriv(algorithm, key, iv);
30
30
  return Buffer.concat([decipher.update(payload), decipher.final()]);
31
31
  }
@@ -1,9 +1,20 @@
1
1
  /// <reference types="node" />
2
2
  export declare function md5(s: string | Buffer): string;
3
+ export declare function md5AsBase64(s: string | Buffer): string;
3
4
  export declare function md5AsBuffer(s: string | Buffer): Buffer;
5
+ export declare function sha256(s: string | Buffer): string;
6
+ export declare function sha256AsBase64(s: string | Buffer): string;
7
+ export declare function sha256AsBuffer(s: string | Buffer): Buffer;
4
8
  export declare function hash(s: string | Buffer, algorithm: string): string;
5
9
  export declare function hashAsBuffer(s: string | Buffer, algorithm: string): Buffer;
6
- export declare function stringToBase64(s: string): string;
10
+ export declare function base64(s: string | Buffer): string;
7
11
  export declare function base64ToString(strBase64: string): string;
8
- export declare function bufferToBase64(b: Buffer): string;
9
12
  export declare function base64ToBuffer(strBase64: string): Buffer;
13
+ /**
14
+ * @deprecated use `base64`
15
+ */
16
+ export declare function stringToBase64(s: string): string;
17
+ /**
18
+ * @deprecated use `base64`
19
+ */
20
+ export declare function bufferToBase64(b: Buffer): string;
@@ -1,15 +1,31 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.base64ToBuffer = exports.bufferToBase64 = exports.base64ToString = exports.stringToBase64 = exports.hashAsBuffer = exports.hash = exports.md5AsBuffer = exports.md5 = void 0;
3
+ exports.bufferToBase64 = exports.stringToBase64 = exports.base64ToBuffer = exports.base64ToString = exports.base64 = exports.hashAsBuffer = exports.hash = exports.sha256AsBuffer = exports.sha256AsBase64 = exports.sha256 = exports.md5AsBuffer = exports.md5AsBase64 = exports.md5 = void 0;
4
4
  const crypto = require("crypto");
5
5
  function md5(s) {
6
6
  return hash(s, 'md5');
7
7
  }
8
8
  exports.md5 = md5;
9
+ function md5AsBase64(s) {
10
+ return hashAsBuffer(s, 'md5').toString('base64');
11
+ }
12
+ exports.md5AsBase64 = md5AsBase64;
9
13
  function md5AsBuffer(s) {
10
14
  return hashAsBuffer(s, 'md5');
11
15
  }
12
16
  exports.md5AsBuffer = md5AsBuffer;
17
+ function sha256(s) {
18
+ return hash(s, 'sha256');
19
+ }
20
+ exports.sha256 = sha256;
21
+ function sha256AsBase64(s) {
22
+ return hashAsBuffer(s, 'sha256').toString('base64');
23
+ }
24
+ exports.sha256AsBase64 = sha256AsBase64;
25
+ function sha256AsBuffer(s) {
26
+ return hashAsBuffer(s, 'sha256');
27
+ }
28
+ exports.sha256AsBuffer = sha256AsBuffer;
13
29
  function hash(s, algorithm) {
14
30
  return crypto.createHash(algorithm).update(s).digest('hex');
15
31
  }
@@ -18,19 +34,29 @@ function hashAsBuffer(s, algorithm) {
18
34
  return crypto.createHash(algorithm).update(s).digest();
19
35
  }
20
36
  exports.hashAsBuffer = hashAsBuffer;
21
- function stringToBase64(s) {
22
- return Buffer.from(s, 'utf8').toString('base64');
37
+ function base64(s) {
38
+ return (Buffer.isBuffer(s) ? s : Buffer.from(s, 'utf8')).toString('base64');
23
39
  }
24
- exports.stringToBase64 = stringToBase64;
40
+ exports.base64 = base64;
25
41
  function base64ToString(strBase64) {
26
42
  return Buffer.from(strBase64, 'base64').toString('utf8');
27
43
  }
28
44
  exports.base64ToString = base64ToString;
29
- function bufferToBase64(b) {
30
- return b.toString('base64');
31
- }
32
- exports.bufferToBase64 = bufferToBase64;
33
45
  function base64ToBuffer(strBase64) {
34
46
  return Buffer.from(strBase64, 'base64');
35
47
  }
36
48
  exports.base64ToBuffer = base64ToBuffer;
49
+ /**
50
+ * @deprecated use `base64`
51
+ */
52
+ function stringToBase64(s) {
53
+ return Buffer.from(s, 'utf8').toString('base64');
54
+ }
55
+ exports.stringToBase64 = stringToBase64;
56
+ /**
57
+ * @deprecated use `base64`
58
+ */
59
+ function bufferToBase64(b) {
60
+ return b.toString('base64');
61
+ }
62
+ exports.bufferToBase64 = bufferToBase64;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "12.75.1",
3
+ "version": "12.77.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
@@ -42,17 +42,14 @@
42
42
  "@naturalcycles/dev-lib": "^13.0.0",
43
43
  "@types/node": "^18.0.0",
44
44
  "@types/yargs": "^16.0.0",
45
- "jest": "^28.0.3",
45
+ "jest": "^29.0.0",
46
46
  "nock": "^13.0.2",
47
47
  "patch-package": "^6.2.1",
48
48
  "prettier": "^2.0.0",
49
49
  "vue-class-component": "^7.2.6",
50
50
  "vuepress": "^1.7.1",
51
51
  "vuepress-plugin-typescript": "^0.3.1",
52
- "weak-napi": "^2.0.0"
53
- },
54
- "resolutions": {
55
- "prettier": "^2.0.0"
52
+ "zod": "^3.19.1"
56
53
  },
57
54
  "bin": {
58
55
  "del": "dist/bin/del.js",
@@ -5,7 +5,7 @@ export function _chunkBuffer(buf: Buffer, size: number): Buffer[] {
5
5
  const out: Buffer[] = []
6
6
 
7
7
  for (let i = 0; i < buf.length; i += size) {
8
- out.push(buf.slice(i, i + size))
8
+ out.push(buf.subarray(i, i + size))
9
9
  }
10
10
 
11
11
  return out
package/src/fs/del.ts CHANGED
@@ -110,6 +110,78 @@ export async function del(_opt: DelOptions | DelSingleOption): Promise<void> {
110
110
  }
111
111
  }
112
112
 
113
+ export function delSync(_opt: DelOptions | DelSingleOption): void {
114
+ const started = Date.now()
115
+
116
+ // Convert DelSingleOption to DelOptions
117
+ if (typeof _opt === 'string') {
118
+ _opt = {
119
+ patterns: [_opt],
120
+ }
121
+ }
122
+
123
+ const opt = {
124
+ ...DEF_OPT,
125
+ ..._opt,
126
+ }
127
+ const { patterns, verbose, silent, debug, dry } = opt
128
+
129
+ if (debug) {
130
+ console.log(opt)
131
+ }
132
+
133
+ // 1. glob only files, expand dirs, delete
134
+
135
+ const filenames = globby.sync(patterns, {
136
+ dot: true,
137
+ expandDirectories: true,
138
+ onlyFiles: true,
139
+ })
140
+
141
+ if (verbose || debug || dry) {
142
+ console.log(`Will delete ${yellow(filenames.length)} files:`, filenames)
143
+ }
144
+
145
+ if (dry) return
146
+
147
+ filenames.forEach(filepath => fs.removeSync(filepath))
148
+
149
+ // 2. glob only dirs, expand, delete only empty!
150
+ let dirnames = globby.sync(patterns, {
151
+ dot: true,
152
+ expandDirectories: true,
153
+ onlyDirectories: true,
154
+ })
155
+
156
+ // Add original patterns (if any of them are dirs)
157
+ dirnames = dirnames.concat(
158
+ patterns.filter(p => fs.pathExistsSync(p) && fs.lstatSync(p).isDirectory()),
159
+ )
160
+
161
+ const dirnamesSorted = dirnames.sort().reverse()
162
+
163
+ // console.log({ dirnamesSorted })
164
+
165
+ const deletedDirs: string[] = []
166
+ for (const dirpath of dirnamesSorted) {
167
+ if (isEmptyDirSync(dirpath)) {
168
+ // console.log(`empty dir: ${dirpath}`)
169
+ fs.removeSync(dirpath)
170
+ deletedDirs.push(dirpath)
171
+ }
172
+ }
173
+
174
+ if (verbose || debug) console.log({ deletedDirs })
175
+
176
+ if (!silent) {
177
+ console.log(
178
+ `del deleted ${yellow(filenames.length)} files and ${yellow(
179
+ deletedDirs.length,
180
+ )} dirs ${dimGrey(_since(started))}`,
181
+ )
182
+ }
183
+ }
184
+
113
185
  // Improved algorithm:
114
186
  // 1. glob only files, expand dirs, delete
115
187
  // 2. glob only dirs, expand, delete only empty!
@@ -118,3 +190,7 @@ export async function del(_opt: DelOptions | DelSingleOption): Promise<void> {
118
190
  async function isEmptyDir(dir: string): Promise<boolean> {
119
191
  return (await fs.readdir(dir)).length === 0
120
192
  }
193
+
194
+ function isEmptyDirSync(dir: string): boolean {
195
+ return fs.readdirSync(dir).length === 0
196
+ }
package/src/fs/index.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { del, DelOptions } from './del'
1
+ import { del, delSync, DelOptions } from './del'
2
2
  import { json2env, objectToShellExport } from './json2env'
3
3
  import { kpy, KpyOptions, kpySync } from './kpy'
4
4
 
5
5
  export type { KpyOptions, DelOptions }
6
6
 
7
- export { kpy, kpySync, del, objectToShellExport, json2env }
7
+ export { kpy, kpySync, del, delSync, objectToShellExport, json2env }
@@ -26,8 +26,8 @@ export function decryptRandomIVBuffer(input: Buffer, secretKeyBase64: string): B
26
26
  const key = md5(Buffer.from(secretKeyBase64, 'base64'))
27
27
 
28
28
  // iv is first 16 bytes of encrypted buffer, the rest is payload
29
- const iv = input.slice(0, 16)
30
- const payload = input.slice(16)
29
+ const iv = input.subarray(0, 16)
30
+ const payload = input.subarray(16)
31
31
 
32
32
  const decipher = crypto.createDecipheriv(algorithm, key, iv)
33
33
 
@@ -4,10 +4,26 @@ export function md5(s: string | Buffer): string {
4
4
  return hash(s, 'md5')
5
5
  }
6
6
 
7
+ export function md5AsBase64(s: string | Buffer): string {
8
+ return hashAsBuffer(s, 'md5').toString('base64')
9
+ }
10
+
7
11
  export function md5AsBuffer(s: string | Buffer): Buffer {
8
12
  return hashAsBuffer(s, 'md5')
9
13
  }
10
14
 
15
+ export function sha256(s: string | Buffer): string {
16
+ return hash(s, 'sha256')
17
+ }
18
+
19
+ export function sha256AsBase64(s: string | Buffer): string {
20
+ return hashAsBuffer(s, 'sha256').toString('base64')
21
+ }
22
+
23
+ export function sha256AsBuffer(s: string | Buffer): Buffer {
24
+ return hashAsBuffer(s, 'sha256')
25
+ }
26
+
11
27
  export function hash(s: string | Buffer, algorithm: string): string {
12
28
  return crypto.createHash(algorithm).update(s).digest('hex')
13
29
  }
@@ -16,18 +32,28 @@ export function hashAsBuffer(s: string | Buffer, algorithm: string): Buffer {
16
32
  return crypto.createHash(algorithm).update(s).digest()
17
33
  }
18
34
 
19
- export function stringToBase64(s: string): string {
20
- return Buffer.from(s, 'utf8').toString('base64')
35
+ export function base64(s: string | Buffer): string {
36
+ return (Buffer.isBuffer(s) ? s : Buffer.from(s, 'utf8')).toString('base64')
21
37
  }
22
38
 
23
39
  export function base64ToString(strBase64: string): string {
24
40
  return Buffer.from(strBase64, 'base64').toString('utf8')
25
41
  }
26
42
 
27
- export function bufferToBase64(b: Buffer): string {
28
- return b.toString('base64')
29
- }
30
-
31
43
  export function base64ToBuffer(strBase64: string): Buffer {
32
44
  return Buffer.from(strBase64, 'base64')
33
45
  }
46
+
47
+ /**
48
+ * @deprecated use `base64`
49
+ */
50
+ export function stringToBase64(s: string): string {
51
+ return Buffer.from(s, 'utf8').toString('base64')
52
+ }
53
+
54
+ /**
55
+ * @deprecated use `base64`
56
+ */
57
+ export function bufferToBase64(b: Buffer): string {
58
+ return b.toString('base64')
59
+ }