@naturalcycles/nodejs-lib 12.72.0 → 12.75.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.
- package/dist/fs/del.js +5 -5
- package/dist/fs/kpy.js +3 -3
- package/dist/index.d.ts +4 -2
- package/dist/index.js +3 -1
- package/dist/secret/secrets-decrypt.util.js +2 -2
- package/dist/secret/secrets-encrypt.util.js +2 -2
- package/dist/util/env.util.js +2 -2
- package/dist/validation/ajv/ajv.util.d.ts +2 -2
- package/dist/validation/ajv/ajv.util.js +2 -2
- package/dist/validation/ajv/getAjv.js +16 -4
- package/dist/validation/joi/joi.shared.schemas.d.ts +15 -0
- package/dist/validation/joi/joi.shared.schemas.js +29 -7
- package/package.json +4 -4
- package/src/fs/del.ts +5 -5
- package/src/fs/kpy.ts +3 -3
- package/src/index.ts +4 -0
- package/src/secret/secrets-decrypt.util.ts +2 -2
- package/src/secret/secrets-encrypt.util.ts +2 -2
- package/src/util/env.util.ts +3 -3
- package/src/validation/ajv/ajv.util.ts +4 -4
- package/src/validation/ajv/ajvSchema.ts +1 -1
- package/src/validation/ajv/getAjv.ts +17 -4
- package/src/validation/joi/joi.shared.schemas.ts +29 -6
package/dist/fs/del.js
CHANGED
|
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.del = void 0;
|
|
4
4
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
5
5
|
const fs = require("fs-extra");
|
|
6
|
-
const globby = require("globby");
|
|
7
6
|
const colors_1 = require("../colors");
|
|
7
|
+
const index_1 = require("../index");
|
|
8
8
|
const DEF_OPT = {
|
|
9
9
|
patterns: [],
|
|
10
10
|
concurrency: Number.POSITIVE_INFINITY,
|
|
@@ -32,9 +32,9 @@ async function del(_opt) {
|
|
|
32
32
|
console.log(opt);
|
|
33
33
|
}
|
|
34
34
|
// 1. glob only files, expand dirs, delete
|
|
35
|
-
const filenames = await
|
|
35
|
+
const filenames = await (0, index_1.fastGlob)(patterns, {
|
|
36
36
|
dot: true,
|
|
37
|
-
expandDirectories: true,
|
|
37
|
+
// expandDirectories: true,
|
|
38
38
|
onlyFiles: true,
|
|
39
39
|
});
|
|
40
40
|
if (verbose || debug || dry) {
|
|
@@ -44,9 +44,9 @@ async function del(_opt) {
|
|
|
44
44
|
return;
|
|
45
45
|
await (0, js_lib_1.pMap)(filenames, filepath => fs.remove(filepath), { concurrency });
|
|
46
46
|
// 2. glob only dirs, expand, delete only empty!
|
|
47
|
-
let dirnames = await
|
|
47
|
+
let dirnames = await (0, index_1.fastGlob)(patterns, {
|
|
48
48
|
dot: true,
|
|
49
|
-
expandDirectories: true,
|
|
49
|
+
// expandDirectories: true,
|
|
50
50
|
onlyDirectories: true,
|
|
51
51
|
});
|
|
52
52
|
// Add original patterns (if any of them are dirs)
|
package/dist/fs/kpy.js
CHANGED
|
@@ -5,13 +5,13 @@ const path = require("path");
|
|
|
5
5
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
6
6
|
const cpFile = require("cp-file");
|
|
7
7
|
const fs = require("fs-extra");
|
|
8
|
-
const globby = require("globby");
|
|
9
8
|
const moveFile = require("move-file");
|
|
10
9
|
const colors_1 = require("../colors");
|
|
10
|
+
const index_1 = require("../index");
|
|
11
11
|
async function kpy(opt) {
|
|
12
12
|
const started = Date.now();
|
|
13
13
|
kpyPrepare(opt);
|
|
14
|
-
const filenames = await
|
|
14
|
+
const filenames = await (0, index_1.fastGlob)(opt.inputPatterns, {
|
|
15
15
|
cwd: opt.baseDir,
|
|
16
16
|
dot: opt.dotfiles,
|
|
17
17
|
});
|
|
@@ -39,7 +39,7 @@ exports.kpy = kpy;
|
|
|
39
39
|
function kpySync(opt) {
|
|
40
40
|
const started = Date.now();
|
|
41
41
|
kpyPrepare(opt);
|
|
42
|
-
const filenames =
|
|
42
|
+
const filenames = index_1.fastGlob.sync(opt.inputPatterns, {
|
|
43
43
|
cwd: opt.baseDir,
|
|
44
44
|
dot: opt.dotfiles,
|
|
45
45
|
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import Ajv from 'ajv';
|
|
2
|
+
import * as fastGlob from 'fast-glob';
|
|
3
|
+
import { Options as FastGlobOptions } from 'fast-glob';
|
|
2
4
|
import { RequestError, TimeoutError } from 'got';
|
|
3
5
|
import type { AfterResponseHook, BeforeErrorHook, BeforeRequestHook, Got } from 'got';
|
|
4
6
|
import { AnySchema, ValidationErrorItem } from 'joi';
|
|
@@ -67,5 +69,5 @@ export * from './validation/joi/joi.shared.schemas';
|
|
|
67
69
|
import { JoiValidationError, JoiValidationErrorData } from './validation/joi/joi.validation.error';
|
|
68
70
|
import { convert, getValidationResult, isValid, JoiValidationResult, undefinedIfInvalid, validate } from './validation/joi/joi.validation.util';
|
|
69
71
|
import { runScript, RunScriptOptions } from './script';
|
|
70
|
-
export type { RunScriptOptions, JoiValidationErrorData, JoiValidationResult, ValidationErrorItem, ExtendedJoi, SchemaTyped, AnySchema, AnySchemaTyped, ArraySchemaTyped, BooleanSchemaTyped, NumberSchemaTyped, ObjectSchemaTyped, StringSchemaTyped, IDebug, IDebugger, SlackServiceCfg, SlackMessage, SlackMessageProps, SlackApiBody, SlackMessagePrefixHook, ReadableTyped, WritableTyped, TransformTyped, PipelineFromNDJsonFileOptions, PipelineToNDJsonFileOptions, TransformJsonParseOptions, TransformToNDJsonOptions, TransformMapOptions, TransformMapSyncOptions, NDJSONStreamForEachOptions, TransformOptions, TransformMultiThreadedOptions, WorkerClassInterface, WorkerInput, WorkerOutput, TableDiffOptions, InspectAnyOptions, Got, GetGotOptions, AfterResponseHook, BeforeErrorHook, BeforeRequestHook, AjvValidationOptions, AjvSchemaCfg, AjvValidationErrorData, };
|
|
71
|
-
export { JoiValidationError, validate, getValidationResult, isValid, undefinedIfInvalid, convert, Joi, Debug, SlackService, slackDefaultMessagePrefixHook, ndjsonStreamForEach, pipelineFromNDJsonFile, pipelineToNDJsonFile, NDJsonStats, streamToNDJsonFile, transformJsonParse, bufferReviver, transformToNDJson, transformMap, transformMapSync, transformMultiThreaded, BaseWorkerClass, tableDiff, inspectAny, inspectAnyStringifyFn, RequestError, TimeoutError, _chunkBuffer, Ajv, AjvSchema, AjvValidationError, readJsonSchemas, readAjvSchemas, runScript, };
|
|
72
|
+
export type { FastGlobOptions, RunScriptOptions, JoiValidationErrorData, JoiValidationResult, ValidationErrorItem, ExtendedJoi, SchemaTyped, AnySchema, AnySchemaTyped, ArraySchemaTyped, BooleanSchemaTyped, NumberSchemaTyped, ObjectSchemaTyped, StringSchemaTyped, IDebug, IDebugger, SlackServiceCfg, SlackMessage, SlackMessageProps, SlackApiBody, SlackMessagePrefixHook, ReadableTyped, WritableTyped, TransformTyped, PipelineFromNDJsonFileOptions, PipelineToNDJsonFileOptions, TransformJsonParseOptions, TransformToNDJsonOptions, TransformMapOptions, TransformMapSyncOptions, NDJSONStreamForEachOptions, TransformOptions, TransformMultiThreadedOptions, WorkerClassInterface, WorkerInput, WorkerOutput, TableDiffOptions, InspectAnyOptions, Got, GetGotOptions, AfterResponseHook, BeforeErrorHook, BeforeRequestHook, AjvValidationOptions, AjvSchemaCfg, AjvValidationErrorData, };
|
|
73
|
+
export { fastGlob, JoiValidationError, validate, getValidationResult, isValid, undefinedIfInvalid, convert, Joi, Debug, SlackService, slackDefaultMessagePrefixHook, ndjsonStreamForEach, pipelineFromNDJsonFile, pipelineToNDJsonFile, NDJsonStats, streamToNDJsonFile, transformJsonParse, bufferReviver, transformToNDJson, transformMap, transformMapSync, transformMultiThreaded, BaseWorkerClass, tableDiff, inspectAny, inspectAnyStringifyFn, RequestError, TimeoutError, _chunkBuffer, Ajv, AjvSchema, AjvValidationError, readJsonSchemas, readAjvSchemas, runScript, };
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.runScript = exports.readAjvSchemas = exports.readJsonSchemas = exports.AjvValidationError = exports.AjvSchema = exports.Ajv = exports._chunkBuffer = exports.TimeoutError = exports.RequestError = exports.inspectAnyStringifyFn = exports.inspectAny = exports.tableDiff = exports.BaseWorkerClass = exports.transformMultiThreaded = exports.transformMapSync = exports.transformMap = exports.transformToNDJson = exports.bufferReviver = exports.transformJsonParse = exports.streamToNDJsonFile = exports.NDJsonStats = exports.pipelineToNDJsonFile = exports.pipelineFromNDJsonFile = exports.ndjsonStreamForEach = exports.slackDefaultMessagePrefixHook = exports.SlackService = exports.Debug = exports.Joi = exports.convert = exports.undefinedIfInvalid = exports.isValid = exports.getValidationResult = exports.validate = exports.JoiValidationError = void 0;
|
|
3
|
+
exports.runScript = exports.readAjvSchemas = exports.readJsonSchemas = exports.AjvValidationError = exports.AjvSchema = exports.Ajv = exports._chunkBuffer = exports.TimeoutError = exports.RequestError = exports.inspectAnyStringifyFn = exports.inspectAny = exports.tableDiff = exports.BaseWorkerClass = exports.transformMultiThreaded = exports.transformMapSync = exports.transformMap = exports.transformToNDJson = exports.bufferReviver = exports.transformJsonParse = exports.streamToNDJsonFile = exports.NDJsonStats = exports.pipelineToNDJsonFile = exports.pipelineFromNDJsonFile = exports.ndjsonStreamForEach = exports.slackDefaultMessagePrefixHook = exports.SlackService = exports.Debug = exports.Joi = exports.convert = exports.undefinedIfInvalid = exports.isValid = exports.getValidationResult = exports.validate = exports.JoiValidationError = exports.fastGlob = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const ajv_1 = require("ajv");
|
|
6
6
|
exports.Ajv = ajv_1.default;
|
|
7
|
+
const fastGlob = require("fast-glob");
|
|
8
|
+
exports.fastGlob = fastGlob;
|
|
7
9
|
const got_1 = require("got");
|
|
8
10
|
Object.defineProperty(exports, "RequestError", { enumerable: true, get: function () { return got_1.RequestError; } });
|
|
9
11
|
Object.defineProperty(exports, "TimeoutError", { enumerable: true, get: function () { return got_1.TimeoutError; } });
|
|
@@ -4,8 +4,8 @@ exports.secretsDecrypt = void 0;
|
|
|
4
4
|
const path = require("path");
|
|
5
5
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
6
6
|
const fs = require("fs-extra");
|
|
7
|
-
const globby = require("globby");
|
|
8
7
|
const colors_1 = require("../colors");
|
|
8
|
+
const index_1 = require("../index");
|
|
9
9
|
const crypto_util_1 = require("../security/crypto.util");
|
|
10
10
|
// Debug it like this:
|
|
11
11
|
// yarn tsn ./src/bin/secrets-decrypt.ts --file ./src/test/secrets2.json --jsonMode --encKey MPd/30v0Zcce4I5mfwF4NSXrpTYD9OO4/fIqw6rjNiWp2b1GN9Xm8nQZqr7c9kKSsATqtwe0HkJFDUGzDSow44GDgDICgB1u1rGa5eNqtxnOVGRR+lIinCvN/1OnpjzeoJy2bStXPj1DKx8anMqgA8SoOZdlWRNSkEeZlolru8Ey0ujZo22dfwMyRIEniLcqvBm/iMiAkV82fn/TxYw05GarAoJcrfPeDBvuOXsARnMCyX18qTFL0iojxeTU8JHxr8TX3eXDq9cJJmridEKlwRIAzADwtetI4ttlP8lwJj1pmgsBIN3iqYssZYCkZ3HMV6BoEc7LTI5z/45rKrAT1A==
|
|
@@ -17,7 +17,7 @@ const crypto_util_1 = require("../security/crypto.util");
|
|
|
17
17
|
function secretsDecrypt(dir, file, encKey, del = false, jsonMode = false) {
|
|
18
18
|
// If `file` is provided - only this one file is used
|
|
19
19
|
const patterns = file ? [file] : dir.map(d => `${d}/**/*.enc`);
|
|
20
|
-
const filenames =
|
|
20
|
+
const filenames = index_1.fastGlob.sync(patterns);
|
|
21
21
|
filenames.forEach(filename => {
|
|
22
22
|
let plainFilename;
|
|
23
23
|
if (jsonMode) {
|
|
@@ -4,8 +4,8 @@ exports.secretsEncrypt = void 0;
|
|
|
4
4
|
const path = require("path");
|
|
5
5
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
6
6
|
const fs = require("fs-extra");
|
|
7
|
-
const globby = require("globby");
|
|
8
7
|
const colors_1 = require("../colors");
|
|
8
|
+
const index_1 = require("../index");
|
|
9
9
|
const crypto_util_1 = require("../security/crypto.util");
|
|
10
10
|
/**
|
|
11
11
|
* Encrypts all files in given directory (except *.enc), saves encrypted versions as filename.ext.enc.
|
|
@@ -18,7 +18,7 @@ function secretsEncrypt(pattern, file, encKey, del = false, jsonMode = false) {
|
|
|
18
18
|
...pattern,
|
|
19
19
|
`!**/*.enc`, // excluding already encoded
|
|
20
20
|
];
|
|
21
|
-
const filenames =
|
|
21
|
+
const filenames = index_1.fastGlob.sync(patterns);
|
|
22
22
|
let encFilename;
|
|
23
23
|
filenames.forEach(filename => {
|
|
24
24
|
if (jsonMode) {
|
package/dist/util/env.util.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.requireFileToExist = exports.requireEnvKeys = void 0;
|
|
4
|
+
const fs = require("fs");
|
|
4
5
|
require("dotenv/config"); // ensure .env is read before requiring keys
|
|
5
|
-
const fs = require("fs-extra");
|
|
6
6
|
/**
|
|
7
7
|
* @example
|
|
8
8
|
*
|
|
@@ -22,7 +22,7 @@ function requireEnvKeys(...keys) {
|
|
|
22
22
|
}
|
|
23
23
|
exports.requireEnvKeys = requireEnvKeys;
|
|
24
24
|
function requireFileToExist(filePath) {
|
|
25
|
-
if (!fs.
|
|
25
|
+
if (!fs.existsSync(filePath)) {
|
|
26
26
|
throw new Error(`Required file should exist: ${filePath}`);
|
|
27
27
|
}
|
|
28
28
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { JsonSchema } from '@naturalcycles/js-lib';
|
|
2
|
-
import {
|
|
2
|
+
import type { FastGlobOptions } from '../..';
|
|
3
3
|
import { AjvSchema, AjvSchemaCfg } from './ajvSchema';
|
|
4
4
|
/**
|
|
5
5
|
* Does fs.readFileSync + JSON.parse for ALL files matching the passed `glob` pattern.
|
|
@@ -9,7 +9,7 @@ import { AjvSchema, AjvSchemaCfg } from './ajvSchema';
|
|
|
9
9
|
*
|
|
10
10
|
* @experimental
|
|
11
11
|
*/
|
|
12
|
-
export declare function readJsonSchemas(patterns: string | string[], opt?:
|
|
12
|
+
export declare function readJsonSchemas(patterns: string | string[], opt?: FastGlobOptions): JsonSchema[];
|
|
13
13
|
/**
|
|
14
14
|
* Reads json schemas from given dir (glob pattern).
|
|
15
15
|
* Creates new AjvSchema for each of them (ajv validates them upon creation).
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.readAjvSchemas = exports.readJsonSchemas = void 0;
|
|
4
4
|
const fs = require("fs");
|
|
5
|
-
const
|
|
5
|
+
const __1 = require("../..");
|
|
6
6
|
const ajvSchema_1 = require("./ajvSchema");
|
|
7
7
|
/**
|
|
8
8
|
* Does fs.readFileSync + JSON.parse for ALL files matching the passed `glob` pattern.
|
|
@@ -13,7 +13,7 @@ const ajvSchema_1 = require("./ajvSchema");
|
|
|
13
13
|
* @experimental
|
|
14
14
|
*/
|
|
15
15
|
function readJsonSchemas(patterns, opt) {
|
|
16
|
-
return
|
|
16
|
+
return __1.fastGlob.sync(patterns, opt).map(fileName => JSON.parse(fs.readFileSync(fileName, 'utf8')));
|
|
17
17
|
}
|
|
18
18
|
exports.readJsonSchemas = readJsonSchemas;
|
|
19
19
|
/**
|
|
@@ -38,6 +38,8 @@ function getAjv(opt) {
|
|
|
38
38
|
return ajv;
|
|
39
39
|
}
|
|
40
40
|
exports.getAjv = getAjv;
|
|
41
|
+
const TS_2500 = 16725225600; // 2500-01-01
|
|
42
|
+
const TS_2000 = 946684800; // 2000-01-01
|
|
41
43
|
function addCustomAjvFormats(ajv) {
|
|
42
44
|
return (ajv
|
|
43
45
|
.addFormat('id', /^[a-z0-9_]{6,64}$/)
|
|
@@ -50,15 +52,25 @@ function addCustomAjvFormats(ajv) {
|
|
|
50
52
|
.addFormat('unixTimestamp', {
|
|
51
53
|
type: 'number',
|
|
52
54
|
validate: (n) => {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
+
return n >= 0 && n < TS_2500;
|
|
56
|
+
},
|
|
57
|
+
})
|
|
58
|
+
.addFormat('unixTimestamp2000', {
|
|
59
|
+
type: 'number',
|
|
60
|
+
validate: (n) => {
|
|
61
|
+
return n >= TS_2000 && n < TS_2500;
|
|
55
62
|
},
|
|
56
63
|
})
|
|
57
64
|
.addFormat('unixTimestampMillis', {
|
|
58
65
|
type: 'number',
|
|
59
66
|
validate: (n) => {
|
|
60
|
-
|
|
61
|
-
|
|
67
|
+
return n >= 0 && n < TS_2500 * 1000;
|
|
68
|
+
},
|
|
69
|
+
})
|
|
70
|
+
.addFormat('unixTimestampMillis2000', {
|
|
71
|
+
type: 'number',
|
|
72
|
+
validate: (n) => {
|
|
73
|
+
return n >= TS_2000 * 1000 && n < TS_2500 * 1000;
|
|
62
74
|
},
|
|
63
75
|
})
|
|
64
76
|
.addFormat('utcOffset', {
|
|
@@ -29,7 +29,22 @@ export declare const SLUG_PATTERN: RegExp;
|
|
|
29
29
|
* "Slug" - a valid URL, filename, etc.
|
|
30
30
|
*/
|
|
31
31
|
export declare const slugSchema: import("./string.extensions").ExtendedStringSchema;
|
|
32
|
+
/**
|
|
33
|
+
* Between years 1970 and 2050
|
|
34
|
+
*/
|
|
32
35
|
export declare const unixTimestampSchema: import("./number.extensions").ExtendedNumberSchema;
|
|
36
|
+
/**
|
|
37
|
+
* Between years 2000 and 2050
|
|
38
|
+
*/
|
|
39
|
+
export declare const unixTimestamp2000Schema: import("./number.extensions").ExtendedNumberSchema;
|
|
40
|
+
/**
|
|
41
|
+
* Between years 1970 and 2050
|
|
42
|
+
*/
|
|
43
|
+
export declare const unixTimestampMillisSchema: import("./number.extensions").ExtendedNumberSchema;
|
|
44
|
+
/**
|
|
45
|
+
* Between years 2000 and 2050
|
|
46
|
+
*/
|
|
47
|
+
export declare const unixTimestampMillis2000Schema: import("./number.extensions").ExtendedNumberSchema;
|
|
33
48
|
export declare const verSchema: import("./number.extensions").ExtendedNumberSchema;
|
|
34
49
|
/**
|
|
35
50
|
* Be careful, by default emailSchema does TLD validation. To disable it - use `stringSchema.email({tld: false}).lowercase()`
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.savedDBEntitySchema = exports.baseDBEntitySchema = exports.ipAddressSchema = exports.utcOffsetSchema = exports.userAgentSchema = exports.semVerSchema = exports.SEM_VER_PATTERN = exports.emailSchema = exports.verSchema = exports.unixTimestampSchema = exports.slugSchema = exports.SLUG_PATTERN = exports.idSchema = exports.anyObjectSchema = exports.anySchema = exports.oneOfSchema = exports.objectSchema = exports.arraySchema = exports.urlSchema = exports.binarySchema = exports.dateStringSchema = exports.percentageSchema = exports.integerSchema = exports.numberSchema = exports.stringSchema = exports.booleanDefaultToFalseSchema = exports.booleanSchema = void 0;
|
|
3
|
+
exports.savedDBEntitySchema = exports.baseDBEntitySchema = exports.ipAddressSchema = exports.utcOffsetSchema = exports.userAgentSchema = exports.semVerSchema = exports.SEM_VER_PATTERN = exports.emailSchema = exports.verSchema = exports.unixTimestampMillis2000Schema = exports.unixTimestampMillisSchema = exports.unixTimestamp2000Schema = exports.unixTimestampSchema = exports.slugSchema = exports.SLUG_PATTERN = exports.idSchema = exports.anyObjectSchema = exports.anySchema = exports.oneOfSchema = exports.objectSchema = exports.arraySchema = exports.urlSchema = exports.binarySchema = exports.dateStringSchema = exports.percentageSchema = exports.integerSchema = exports.numberSchema = exports.stringSchema = exports.booleanDefaultToFalseSchema = exports.booleanSchema = void 0;
|
|
4
4
|
const joi_extensions_1 = require("./joi.extensions");
|
|
5
5
|
exports.booleanSchema = joi_extensions_1.Joi.boolean();
|
|
6
6
|
exports.booleanDefaultToFalseSchema = joi_extensions_1.Joi.boolean().default(false);
|
|
@@ -40,8 +40,30 @@ exports.SLUG_PATTERN = /^[a-z0-9-]*$/;
|
|
|
40
40
|
* "Slug" - a valid URL, filename, etc.
|
|
41
41
|
*/
|
|
42
42
|
exports.slugSchema = exports.stringSchema.regex(/^[a-z0-9-]{1,255}$/);
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
const TS_2500 = 16725225600; // 2500-01-01
|
|
44
|
+
const TS_2000 = 946684800; // 2000-01-01
|
|
45
|
+
/**
|
|
46
|
+
* Between years 1970 and 2050
|
|
47
|
+
*/
|
|
48
|
+
exports.unixTimestampSchema = exports.numberSchema.integer().min(0).max(TS_2500);
|
|
49
|
+
/**
|
|
50
|
+
* Between years 2000 and 2050
|
|
51
|
+
*/
|
|
52
|
+
exports.unixTimestamp2000Schema = exports.numberSchema.integer().min(0).min(TS_2000).max(TS_2500);
|
|
53
|
+
/**
|
|
54
|
+
* Between years 1970 and 2050
|
|
55
|
+
*/
|
|
56
|
+
exports.unixTimestampMillisSchema = exports.numberSchema
|
|
57
|
+
.integer()
|
|
58
|
+
.min(0)
|
|
59
|
+
.max(TS_2500 * 1000);
|
|
60
|
+
/**
|
|
61
|
+
* Between years 2000 and 2050
|
|
62
|
+
*/
|
|
63
|
+
exports.unixTimestampMillis2000Schema = exports.numberSchema
|
|
64
|
+
.integer()
|
|
65
|
+
.min(TS_2000 * 1000)
|
|
66
|
+
.max(TS_2500 * 1000);
|
|
45
67
|
// 2
|
|
46
68
|
exports.verSchema = exports.numberSchema.optional().integer().min(1).max(100);
|
|
47
69
|
/**
|
|
@@ -64,11 +86,11 @@ exports.utcOffsetSchema = exports.numberSchema
|
|
|
64
86
|
exports.ipAddressSchema = exports.stringSchema.ip();
|
|
65
87
|
exports.baseDBEntitySchema = objectSchema({
|
|
66
88
|
id: exports.stringSchema.optional(),
|
|
67
|
-
created: exports.
|
|
68
|
-
updated: exports.
|
|
89
|
+
created: exports.unixTimestamp2000Schema.optional(),
|
|
90
|
+
updated: exports.unixTimestamp2000Schema.optional(),
|
|
69
91
|
});
|
|
70
92
|
exports.savedDBEntitySchema = objectSchema({
|
|
71
93
|
id: exports.stringSchema,
|
|
72
|
-
created: exports.
|
|
73
|
-
updated: exports.
|
|
94
|
+
created: exports.unixTimestamp2000Schema,
|
|
95
|
+
updated: exports.unixTimestamp2000Schema,
|
|
74
96
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/nodejs-lib",
|
|
3
|
-
"version": "12.
|
|
3
|
+
"version": "12.75.0",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"prepare": "husky install",
|
|
6
6
|
"docs-serve": "vuepress dev docs",
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
"debug": "^4.1.1",
|
|
27
27
|
"dotenv": "^16.0.0",
|
|
28
28
|
"execa": "^5.0.0",
|
|
29
|
+
"fast-glob": "^3.2.11",
|
|
29
30
|
"fs-extra": "^10.0.0",
|
|
30
|
-
"globby": "^11.0.0",
|
|
31
31
|
"got": "^11.0.1",
|
|
32
32
|
"joi": "17.4.2",
|
|
33
33
|
"lru-cache": "^7.4.0",
|
|
@@ -38,8 +38,8 @@
|
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@naturalcycles/bench-lib": "^1.0.7",
|
|
41
|
-
"@naturalcycles/dev-lib": "^
|
|
42
|
-
"@types/node": "^
|
|
41
|
+
"@naturalcycles/dev-lib": "^13.0.0",
|
|
42
|
+
"@types/node": "^18.0.0",
|
|
43
43
|
"@types/yargs": "^16.0.0",
|
|
44
44
|
"jest": "^28.0.3",
|
|
45
45
|
"nock": "^13.0.2",
|
package/src/fs/del.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { pFilter, pMap, _since } from '@naturalcycles/js-lib'
|
|
2
2
|
import * as fs from 'fs-extra'
|
|
3
|
-
import * as globby from 'globby'
|
|
4
3
|
import { dimGrey, yellow } from '../colors'
|
|
4
|
+
import { fastGlob } from '../index'
|
|
5
5
|
|
|
6
6
|
export interface DelOptions {
|
|
7
7
|
/**
|
|
@@ -58,9 +58,9 @@ export async function del(_opt: DelOptions | DelSingleOption): Promise<void> {
|
|
|
58
58
|
|
|
59
59
|
// 1. glob only files, expand dirs, delete
|
|
60
60
|
|
|
61
|
-
const filenames = await
|
|
61
|
+
const filenames = await fastGlob(patterns, {
|
|
62
62
|
dot: true,
|
|
63
|
-
expandDirectories: true,
|
|
63
|
+
// expandDirectories: true,
|
|
64
64
|
onlyFiles: true,
|
|
65
65
|
})
|
|
66
66
|
|
|
@@ -73,9 +73,9 @@ export async function del(_opt: DelOptions | DelSingleOption): Promise<void> {
|
|
|
73
73
|
await pMap(filenames, filepath => fs.remove(filepath), { concurrency })
|
|
74
74
|
|
|
75
75
|
// 2. glob only dirs, expand, delete only empty!
|
|
76
|
-
let dirnames = await
|
|
76
|
+
let dirnames = await fastGlob(patterns, {
|
|
77
77
|
dot: true,
|
|
78
|
-
expandDirectories: true,
|
|
78
|
+
// expandDirectories: true,
|
|
79
79
|
onlyDirectories: true,
|
|
80
80
|
})
|
|
81
81
|
|
package/src/fs/kpy.ts
CHANGED
|
@@ -2,9 +2,9 @@ import * as path from 'path'
|
|
|
2
2
|
import { _since } from '@naturalcycles/js-lib'
|
|
3
3
|
import * as cpFile from 'cp-file'
|
|
4
4
|
import * as fs from 'fs-extra'
|
|
5
|
-
import * as globby from 'globby'
|
|
6
5
|
import * as moveFile from 'move-file'
|
|
7
6
|
import { boldWhite, dimGrey, grey, yellow } from '../colors'
|
|
7
|
+
import { fastGlob } from '../index'
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Everything defaults to `undefined`.
|
|
@@ -50,7 +50,7 @@ export async function kpy(opt: KpyOptions): Promise<void> {
|
|
|
50
50
|
|
|
51
51
|
kpyPrepare(opt)
|
|
52
52
|
|
|
53
|
-
const filenames = await
|
|
53
|
+
const filenames = await fastGlob(opt.inputPatterns!, {
|
|
54
54
|
cwd: opt.baseDir,
|
|
55
55
|
dot: opt.dotfiles,
|
|
56
56
|
})
|
|
@@ -87,7 +87,7 @@ export function kpySync(opt: KpyOptions): void {
|
|
|
87
87
|
|
|
88
88
|
kpyPrepare(opt)
|
|
89
89
|
|
|
90
|
-
const filenames =
|
|
90
|
+
const filenames = fastGlob.sync(opt.inputPatterns!, {
|
|
91
91
|
cwd: opt.baseDir,
|
|
92
92
|
dot: opt.dotfiles,
|
|
93
93
|
})
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import Ajv from 'ajv'
|
|
2
|
+
import * as fastGlob from 'fast-glob'
|
|
3
|
+
import { Options as FastGlobOptions } from 'fast-glob'
|
|
2
4
|
import { RequestError, TimeoutError } from 'got'
|
|
3
5
|
import type { AfterResponseHook, BeforeErrorHook, BeforeRequestHook, Got } from 'got'
|
|
4
6
|
import { AnySchema, ValidationErrorItem } from 'joi'
|
|
@@ -111,6 +113,7 @@ import {
|
|
|
111
113
|
import { runScript, RunScriptOptions } from './script'
|
|
112
114
|
|
|
113
115
|
export type {
|
|
116
|
+
FastGlobOptions,
|
|
114
117
|
RunScriptOptions,
|
|
115
118
|
JoiValidationErrorData,
|
|
116
119
|
JoiValidationResult,
|
|
@@ -159,6 +162,7 @@ export type {
|
|
|
159
162
|
}
|
|
160
163
|
|
|
161
164
|
export {
|
|
165
|
+
fastGlob,
|
|
162
166
|
JoiValidationError,
|
|
163
167
|
validate,
|
|
164
168
|
getValidationResult,
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as path from 'path'
|
|
2
2
|
import { _assert } from '@naturalcycles/js-lib'
|
|
3
3
|
import * as fs from 'fs-extra'
|
|
4
|
-
import globby = require('globby')
|
|
5
4
|
import { dimGrey, yellow } from '../colors'
|
|
5
|
+
import { fastGlob } from '../index'
|
|
6
6
|
import { decryptObject, decryptRandomIVBuffer } from '../security/crypto.util'
|
|
7
7
|
|
|
8
8
|
export interface DecryptCLIOptions {
|
|
@@ -31,7 +31,7 @@ export function secretsDecrypt(
|
|
|
31
31
|
// If `file` is provided - only this one file is used
|
|
32
32
|
const patterns = file ? [file] : dir.map(d => `${d}/**/*.enc`)
|
|
33
33
|
|
|
34
|
-
const filenames =
|
|
34
|
+
const filenames = fastGlob.sync(patterns)
|
|
35
35
|
|
|
36
36
|
filenames.forEach(filename => {
|
|
37
37
|
let plainFilename
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as path from 'path'
|
|
2
2
|
import { _assert } from '@naturalcycles/js-lib'
|
|
3
3
|
import * as fs from 'fs-extra'
|
|
4
|
-
import globby = require('globby')
|
|
5
4
|
import { dimGrey, yellow } from '../colors'
|
|
5
|
+
import { fastGlob } from '../index'
|
|
6
6
|
import { encryptObject, encryptRandomIVBuffer } from '../security/crypto.util'
|
|
7
7
|
|
|
8
8
|
export interface EncryptCLIOptions {
|
|
@@ -30,7 +30,7 @@ export function secretsEncrypt(
|
|
|
30
30
|
...pattern,
|
|
31
31
|
`!**/*.enc`, // excluding already encoded
|
|
32
32
|
]
|
|
33
|
-
const filenames =
|
|
33
|
+
const filenames = fastGlob.sync(patterns)
|
|
34
34
|
let encFilename
|
|
35
35
|
|
|
36
36
|
filenames.forEach(filename => {
|
package/src/util/env.util.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import * as fs from 'fs'
|
|
1
2
|
import type { ValuesOf } from '@naturalcycles/js-lib'
|
|
2
3
|
import 'dotenv/config' // ensure .env is read before requiring keys
|
|
3
|
-
import * as fs from 'fs-extra'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* @example
|
|
@@ -16,13 +16,13 @@ export function requireEnvKeys<T extends readonly string[]>(
|
|
|
16
16
|
return keys.reduce((r, k) => {
|
|
17
17
|
const v = process.env[k]
|
|
18
18
|
if (!v) throw new Error(`${k} env variable is required, but missing`)
|
|
19
|
-
r[k] = v
|
|
19
|
+
r[k as ValuesOf<T>] = v
|
|
20
20
|
return r
|
|
21
21
|
}, {} as { [k in ValuesOf<T>]: string })
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
export function requireFileToExist(filePath: string): void {
|
|
25
|
-
if (!fs.
|
|
25
|
+
if (!fs.existsSync(filePath)) {
|
|
26
26
|
throw new Error(`Required file should exist: ${filePath}`)
|
|
27
27
|
}
|
|
28
28
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as fs from 'fs'
|
|
2
2
|
import { JsonSchema } from '@naturalcycles/js-lib'
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
3
|
+
import { fastGlob } from '../..'
|
|
4
|
+
import type { FastGlobOptions } from '../..'
|
|
5
5
|
import { AjvSchema, AjvSchemaCfg } from './ajvSchema'
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -12,8 +12,8 @@ import { AjvSchema, AjvSchemaCfg } from './ajvSchema'
|
|
|
12
12
|
*
|
|
13
13
|
* @experimental
|
|
14
14
|
*/
|
|
15
|
-
export function readJsonSchemas(patterns: string | string[], opt?:
|
|
16
|
-
return
|
|
15
|
+
export function readJsonSchemas(patterns: string | string[], opt?: FastGlobOptions): JsonSchema[] {
|
|
16
|
+
return fastGlob.sync(patterns, opt).map(fileName => JSON.parse(fs.readFileSync(fileName, 'utf8')))
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
/**
|
|
@@ -159,7 +159,7 @@ export class AjvSchema<T = unknown> {
|
|
|
159
159
|
const errors = this.validateFunction.errors!
|
|
160
160
|
|
|
161
161
|
const {
|
|
162
|
-
objectId = _isObject(obj) ? (obj['id'] as
|
|
162
|
+
objectId = _isObject(obj) ? (obj['id' as keyof T] as any) : undefined,
|
|
163
163
|
objectName = this.cfg.objectName,
|
|
164
164
|
logErrors = this.cfg.logErrors,
|
|
165
165
|
separator = this.cfg.separator,
|
|
@@ -43,6 +43,9 @@ export function getAjv(opt?: Options): Ajv {
|
|
|
43
43
|
return ajv
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
const TS_2500 = 16725225600 // 2500-01-01
|
|
47
|
+
const TS_2000 = 946684800 // 2000-01-01
|
|
48
|
+
|
|
46
49
|
function addCustomAjvFormats(ajv: Ajv): Ajv {
|
|
47
50
|
return (
|
|
48
51
|
ajv
|
|
@@ -56,15 +59,25 @@ function addCustomAjvFormats(ajv: Ajv): Ajv {
|
|
|
56
59
|
.addFormat('unixTimestamp', {
|
|
57
60
|
type: 'number',
|
|
58
61
|
validate: (n: number) => {
|
|
59
|
-
|
|
60
|
-
|
|
62
|
+
return n >= 0 && n < TS_2500
|
|
63
|
+
},
|
|
64
|
+
})
|
|
65
|
+
.addFormat('unixTimestamp2000', {
|
|
66
|
+
type: 'number',
|
|
67
|
+
validate: (n: number) => {
|
|
68
|
+
return n >= TS_2000 && n < TS_2500
|
|
61
69
|
},
|
|
62
70
|
})
|
|
63
71
|
.addFormat('unixTimestampMillis', {
|
|
64
72
|
type: 'number',
|
|
65
73
|
validate: (n: number) => {
|
|
66
|
-
|
|
67
|
-
|
|
74
|
+
return n >= 0 && n < TS_2500 * 1000
|
|
75
|
+
},
|
|
76
|
+
})
|
|
77
|
+
.addFormat('unixTimestampMillis2000', {
|
|
78
|
+
type: 'number',
|
|
79
|
+
validate: (n: number) => {
|
|
80
|
+
return n >= TS_2000 * 1000 && n < TS_2500 * 1000
|
|
68
81
|
},
|
|
69
82
|
})
|
|
70
83
|
.addFormat('utcOffset', {
|
|
@@ -57,8 +57,31 @@ export const SLUG_PATTERN = /^[a-z0-9-]*$/
|
|
|
57
57
|
*/
|
|
58
58
|
export const slugSchema = stringSchema.regex(/^[a-z0-9-]{1,255}$/)
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
const TS_2500 = 16725225600 // 2500-01-01
|
|
61
|
+
const TS_2000 = 946684800 // 2000-01-01
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Between years 1970 and 2050
|
|
65
|
+
*/
|
|
66
|
+
export const unixTimestampSchema = numberSchema.integer().min(0).max(TS_2500)
|
|
67
|
+
/**
|
|
68
|
+
* Between years 2000 and 2050
|
|
69
|
+
*/
|
|
70
|
+
export const unixTimestamp2000Schema = numberSchema.integer().min(0).min(TS_2000).max(TS_2500)
|
|
71
|
+
/**
|
|
72
|
+
* Between years 1970 and 2050
|
|
73
|
+
*/
|
|
74
|
+
export const unixTimestampMillisSchema = numberSchema
|
|
75
|
+
.integer()
|
|
76
|
+
.min(0)
|
|
77
|
+
.max(TS_2500 * 1000)
|
|
78
|
+
/**
|
|
79
|
+
* Between years 2000 and 2050
|
|
80
|
+
*/
|
|
81
|
+
export const unixTimestampMillis2000Schema = numberSchema
|
|
82
|
+
.integer()
|
|
83
|
+
.min(TS_2000 * 1000)
|
|
84
|
+
.max(TS_2500 * 1000)
|
|
62
85
|
|
|
63
86
|
// 2
|
|
64
87
|
export const verSchema = numberSchema.optional().integer().min(1).max(100)
|
|
@@ -88,12 +111,12 @@ export const ipAddressSchema = stringSchema.ip()
|
|
|
88
111
|
|
|
89
112
|
export const baseDBEntitySchema = objectSchema<BaseDBEntity>({
|
|
90
113
|
id: stringSchema.optional(),
|
|
91
|
-
created:
|
|
92
|
-
updated:
|
|
114
|
+
created: unixTimestamp2000Schema.optional(),
|
|
115
|
+
updated: unixTimestamp2000Schema.optional(),
|
|
93
116
|
})
|
|
94
117
|
|
|
95
118
|
export const savedDBEntitySchema = objectSchema<SavedDBEntity>({
|
|
96
119
|
id: stringSchema,
|
|
97
|
-
created:
|
|
98
|
-
updated:
|
|
120
|
+
created: unixTimestamp2000Schema,
|
|
121
|
+
updated: unixTimestamp2000Schema,
|
|
99
122
|
})
|