@naturalcycles/nodejs-lib 13.2.1 → 13.3.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.
@@ -1,15 +1,20 @@
1
1
  /// <reference types="node" />
2
- import { Base64String } from '@naturalcycles/js-lib';
3
- export declare function md5(s: string | Buffer): string;
4
- export declare function md5AsBase64(s: string | Buffer): Base64String;
2
+ /// <reference types="node" />
3
+ import { BinaryToTextEncoding } from 'node:crypto';
4
+ import type { Base64String, Base64UrlString } from '@naturalcycles/js-lib';
5
+ export declare function md5(s: string | Buffer, outputEncoding?: BinaryToTextEncoding): string;
5
6
  export declare function md5AsBuffer(s: string | Buffer): Buffer;
6
- export declare function sha256(s: string | Buffer): string;
7
- export declare function sha256AsBase64(s: string | Buffer): Base64String;
7
+ export declare function sha256(s: string | Buffer, outputEncoding?: BinaryToTextEncoding): string;
8
8
  export declare function sha256AsBuffer(s: string | Buffer): Buffer;
9
- export declare function hash(s: string | Buffer, algorithm: string): string;
9
+ export declare function hash(s: string | Buffer, algorithm: string, outputEncoding?: BinaryToTextEncoding): string;
10
10
  export declare function hashAsBuffer(s: string | Buffer, algorithm: string): Buffer;
11
11
  export declare function base64(s: string | Buffer): Base64String;
12
+ export declare function base64Url(s: string | Buffer): Base64UrlString;
12
13
  export declare function base64ToString(strBase64: Base64String): string;
13
- export declare function base64ToBuffer(strBase64: string): Buffer;
14
+ export declare function base64UrlToString(strBase64Url: Base64UrlString): string;
15
+ export declare function base64ToBuffer(strBase64: Base64String): Buffer;
16
+ export declare function base64UrlToBuffer(strBase64Url: Base64UrlString): Buffer;
14
17
  export declare function stringToBase64(s: string): Base64String;
18
+ export declare function stringToBase64Url(s: string): Base64UrlString;
15
19
  export declare function bufferToBase64(b: Buffer): Base64String;
20
+ export declare function bufferToBase64Url(b: Buffer): Base64UrlString;
@@ -1,34 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
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;
3
+ exports.bufferToBase64Url = exports.bufferToBase64 = exports.stringToBase64Url = exports.stringToBase64 = exports.base64UrlToBuffer = exports.base64ToBuffer = exports.base64UrlToString = exports.base64ToString = exports.base64Url = exports.base64 = exports.hashAsBuffer = exports.hash = exports.sha256AsBuffer = exports.sha256 = exports.md5AsBuffer = exports.md5 = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const node_crypto_1 = tslib_1.__importDefault(require("node:crypto"));
6
- function md5(s) {
7
- return hash(s, 'md5');
6
+ function md5(s, outputEncoding = 'hex') {
7
+ return hash(s, 'md5', outputEncoding);
8
8
  }
9
9
  exports.md5 = md5;
10
- function md5AsBase64(s) {
11
- return hashAsBuffer(s, 'md5').toString('base64');
12
- }
13
- exports.md5AsBase64 = md5AsBase64;
14
10
  function md5AsBuffer(s) {
15
11
  return hashAsBuffer(s, 'md5');
16
12
  }
17
13
  exports.md5AsBuffer = md5AsBuffer;
18
- function sha256(s) {
19
- return hash(s, 'sha256');
14
+ function sha256(s, outputEncoding = 'hex') {
15
+ return hash(s, 'sha256', outputEncoding);
20
16
  }
21
17
  exports.sha256 = sha256;
22
- function sha256AsBase64(s) {
23
- return hashAsBuffer(s, 'sha256').toString('base64');
24
- }
25
- exports.sha256AsBase64 = sha256AsBase64;
26
18
  function sha256AsBuffer(s) {
27
19
  return hashAsBuffer(s, 'sha256');
28
20
  }
29
21
  exports.sha256AsBuffer = sha256AsBuffer;
30
- function hash(s, algorithm) {
31
- return node_crypto_1.default.createHash(algorithm).update(s).digest('hex');
22
+ function hash(s, algorithm, outputEncoding = 'hex') {
23
+ return node_crypto_1.default.createHash(algorithm).update(s).digest(outputEncoding);
32
24
  }
33
25
  exports.hash = hash;
34
26
  function hashAsBuffer(s, algorithm) {
@@ -39,19 +31,39 @@ function base64(s) {
39
31
  return (typeof s === 'string' ? Buffer.from(s) : s).toString('base64');
40
32
  }
41
33
  exports.base64 = base64;
34
+ function base64Url(s) {
35
+ return (typeof s === 'string' ? Buffer.from(s) : s).toString('base64url');
36
+ }
37
+ exports.base64Url = base64Url;
42
38
  function base64ToString(strBase64) {
43
39
  return Buffer.from(strBase64, 'base64').toString('utf8');
44
40
  }
45
41
  exports.base64ToString = base64ToString;
42
+ function base64UrlToString(strBase64Url) {
43
+ return Buffer.from(strBase64Url, 'base64url').toString('utf8');
44
+ }
45
+ exports.base64UrlToString = base64UrlToString;
46
46
  function base64ToBuffer(strBase64) {
47
47
  return Buffer.from(strBase64, 'base64');
48
48
  }
49
49
  exports.base64ToBuffer = base64ToBuffer;
50
+ function base64UrlToBuffer(strBase64Url) {
51
+ return Buffer.from(strBase64Url, 'base64url');
52
+ }
53
+ exports.base64UrlToBuffer = base64UrlToBuffer;
50
54
  function stringToBase64(s) {
51
55
  return Buffer.from(s, 'utf8').toString('base64');
52
56
  }
53
57
  exports.stringToBase64 = stringToBase64;
58
+ function stringToBase64Url(s) {
59
+ return Buffer.from(s, 'utf8').toString('base64url');
60
+ }
61
+ exports.stringToBase64Url = stringToBase64Url;
54
62
  function bufferToBase64(b) {
55
63
  return b.toString('base64');
56
64
  }
57
65
  exports.bufferToBase64 = bufferToBase64;
66
+ function bufferToBase64Url(b) {
67
+ return b.toString('base64url');
68
+ }
69
+ exports.bufferToBase64Url = bufferToBase64Url;
@@ -22,7 +22,7 @@ async function ndjsonMap(mapper, opt) {
22
22
  await (0, __1._pipeline)([
23
23
  readable,
24
24
  ...transformUnzip,
25
- (0, __1.transformSplit)(),
25
+ (0, __1.transformSplit)(), // splits by \n
26
26
  (0, __1.transformJsonParse)(),
27
27
  (0, __1.transformLimit)({ limit: limitInput, sourceReadable: readable }),
28
28
  (0, __1.transformLogProgress)({ metric: 'read', ...opt }),
@@ -21,7 +21,7 @@ async function pipelineFromNDJsonFile(streams, opt) {
21
21
  await (0, __1._pipeline)([
22
22
  node_fs_1.default.createReadStream(filePath),
23
23
  ...(gzip ? [(0, node_zlib_1.createUnzip)(opt.zlibOptions)] : []),
24
- (0, __1.transformSplit)(separator),
24
+ (0, __1.transformSplit)(separator), // splits by separator
25
25
  (0, transformJsonParse_1.transformJsonParse)(opt),
26
26
  (0, __1.transformTap)(() => rows++),
27
27
  ...streams,
@@ -27,7 +27,7 @@ async function pipelineToNDJsonFile(streams, opt) {
27
27
  ...streams,
28
28
  (0, __1.transformTap)(() => rows++),
29
29
  (0, transformToNDJson_1.transformToNDJson)(opt),
30
- ...(gzip ? [(0, node_zlib_1.createGzip)(opt.zlibOptions)] : []),
30
+ ...(gzip ? [(0, node_zlib_1.createGzip)(opt.zlibOptions)] : []), // optional gzip
31
31
  node_fs_1.default.createWriteStream(filePath),
32
32
  ]);
33
33
  const { size: sizeBytes } = node_fs_1.default.statSync(filePath);
@@ -28,7 +28,7 @@ function transformMultiThreaded(opt) {
28
28
  const worker = new node_worker_threads_1.Worker(workerProxyFilePath, {
29
29
  workerData: {
30
30
  workerIndex,
31
- workerFile,
31
+ workerFile, // pass it, so workerProxy can require() it
32
32
  ...workerData,
33
33
  },
34
34
  });
@@ -4,7 +4,7 @@ exports.inspectAny = exports._inspect = exports.inspectStringifyFn = void 0;
4
4
  const node_util_1 = require("node:util");
5
5
  const js_lib_1 = require("@naturalcycles/js-lib");
6
6
  const INSPECT_OPT = {
7
- breakLength: 80,
7
+ breakLength: 80, // default: ??
8
8
  depth: 10, // default: 2
9
9
  };
10
10
  /**
@@ -7,7 +7,7 @@ const AJV_OPTIONS = {
7
7
  removeAdditional: true,
8
8
  allErrors: true,
9
9
  // https://ajv.js.org/options.html#usedefaults
10
- useDefaults: 'empty',
10
+ useDefaults: 'empty', // this will mutate your input!
11
11
  // these are important and kept same as default:
12
12
  // https://ajv.js.org/options.html#coercetypes
13
13
  coerceTypes: false, // while `false` - it won't mutate your input
@@ -29,13 +29,14 @@ function getAjv(opt) {
29
29
  require('ajv-formats')(ajv);
30
30
  // https://ajv.js.org/packages/ajv-keywords.html
31
31
  require('ajv-keywords')(ajv, [
32
- 'transform',
32
+ 'transform', // trim, toLowerCase, etc.
33
33
  'uniqueItemProperties',
34
34
  'instanceof',
35
35
  ]);
36
36
  // Adds $merge, $patch keywords
37
37
  // https://github.com/ajv-validator/ajv-merge-patch
38
- require('ajv-merge-patch')(ajv);
38
+ // Kirill: temporarily disabled, as it creates a noise of CVE warnings
39
+ // require('ajv-merge-patch')(ajv)
39
40
  return ajv;
40
41
  }
41
42
  exports.getAjv = getAjv;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "13.2.1",
3
+ "version": "13.3.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
@@ -20,7 +20,6 @@
20
20
  "ajv": "^8.6.2",
21
21
  "ajv-formats": "^2.1.0",
22
22
  "ajv-keywords": "^5.0.0",
23
- "ajv-merge-patch": "^5.0.1",
24
23
  "binary-split": "^1.0.5",
25
24
  "chalk": "^4.0.0",
26
25
  "debug": "^4.1.1",
@@ -1,32 +1,28 @@
1
- import crypto from 'node:crypto'
2
- import { Base64String } from '@naturalcycles/js-lib'
1
+ import crypto, { BinaryToTextEncoding } from 'node:crypto'
2
+ import type { Base64String, Base64UrlString } from '@naturalcycles/js-lib'
3
3
 
4
- export function md5(s: string | Buffer): string {
5
- return hash(s, 'md5')
6
- }
7
-
8
- export function md5AsBase64(s: string | Buffer): Base64String {
9
- return hashAsBuffer(s, 'md5').toString('base64')
4
+ export function md5(s: string | Buffer, outputEncoding: BinaryToTextEncoding = 'hex'): string {
5
+ return hash(s, 'md5', outputEncoding)
10
6
  }
11
7
 
12
8
  export function md5AsBuffer(s: string | Buffer): Buffer {
13
9
  return hashAsBuffer(s, 'md5')
14
10
  }
15
11
 
16
- export function sha256(s: string | Buffer): string {
17
- return hash(s, 'sha256')
18
- }
19
-
20
- export function sha256AsBase64(s: string | Buffer): Base64String {
21
- return hashAsBuffer(s, 'sha256').toString('base64')
12
+ export function sha256(s: string | Buffer, outputEncoding: BinaryToTextEncoding = 'hex'): string {
13
+ return hash(s, 'sha256', outputEncoding)
22
14
  }
23
15
 
24
16
  export function sha256AsBuffer(s: string | Buffer): Buffer {
25
17
  return hashAsBuffer(s, 'sha256')
26
18
  }
27
19
 
28
- export function hash(s: string | Buffer, algorithm: string): string {
29
- return crypto.createHash(algorithm).update(s).digest('hex')
20
+ export function hash(
21
+ s: string | Buffer,
22
+ algorithm: string,
23
+ outputEncoding: BinaryToTextEncoding = 'hex',
24
+ ): string {
25
+ return crypto.createHash(algorithm).update(s).digest(outputEncoding)
30
26
  }
31
27
 
32
28
  export function hashAsBuffer(s: string | Buffer, algorithm: string): Buffer {
@@ -37,18 +33,38 @@ export function base64(s: string | Buffer): Base64String {
37
33
  return (typeof s === 'string' ? Buffer.from(s) : s).toString('base64')
38
34
  }
39
35
 
36
+ export function base64Url(s: string | Buffer): Base64UrlString {
37
+ return (typeof s === 'string' ? Buffer.from(s) : s).toString('base64url')
38
+ }
39
+
40
40
  export function base64ToString(strBase64: Base64String): string {
41
41
  return Buffer.from(strBase64, 'base64').toString('utf8')
42
42
  }
43
43
 
44
- export function base64ToBuffer(strBase64: string): Buffer {
44
+ export function base64UrlToString(strBase64Url: Base64UrlString): string {
45
+ return Buffer.from(strBase64Url, 'base64url').toString('utf8')
46
+ }
47
+
48
+ export function base64ToBuffer(strBase64: Base64String): Buffer {
45
49
  return Buffer.from(strBase64, 'base64')
46
50
  }
47
51
 
52
+ export function base64UrlToBuffer(strBase64Url: Base64UrlString): Buffer {
53
+ return Buffer.from(strBase64Url, 'base64url')
54
+ }
55
+
48
56
  export function stringToBase64(s: string): Base64String {
49
57
  return Buffer.from(s, 'utf8').toString('base64')
50
58
  }
51
59
 
60
+ export function stringToBase64Url(s: string): Base64UrlString {
61
+ return Buffer.from(s, 'utf8').toString('base64url')
62
+ }
63
+
52
64
  export function bufferToBase64(b: Buffer): Base64String {
53
65
  return b.toString('base64')
54
66
  }
67
+
68
+ export function bufferToBase64Url(b: Buffer): Base64UrlString {
69
+ return b.toString('base64url')
70
+ }
@@ -38,7 +38,8 @@ export function getAjv(opt?: Options): Ajv {
38
38
 
39
39
  // Adds $merge, $patch keywords
40
40
  // https://github.com/ajv-validator/ajv-merge-patch
41
- require('ajv-merge-patch')(ajv)
41
+ // Kirill: temporarily disabled, as it creates a noise of CVE warnings
42
+ // require('ajv-merge-patch')(ajv)
42
43
 
43
44
  return ajv
44
45
  }