@naturalcycles/nodejs-lib 13.1.2 → 13.1.4

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.
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.secretsDecrypt = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const node_path_1 = tslib_1.__importDefault(require("node:path"));
6
5
  const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
6
+ const node_path_1 = tslib_1.__importDefault(require("node:path"));
7
7
  const js_lib_1 = require("@naturalcycles/js-lib");
8
8
  const colors_1 = require("../colors/colors");
9
9
  const index_1 = require("../index");
@@ -130,7 +130,7 @@ class SlackService {
130
130
  }
131
131
  exports.SlackService = SlackService;
132
132
  function slackDefaultMessagePrefixHook(msg) {
133
- const tokens = [(0, js_lib_1.localTime)().toPretty()];
133
+ const tokens = [(0, js_lib_1.localTimeNow)().toPretty()];
134
134
  const { ctx } = msg;
135
135
  // AppEngine-specific decoration
136
136
  if (GAE && ctx && typeof ctx === 'object' && typeof ctx.header === 'function') {
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.pipelineFromNDJsonFile = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const node_zlib_1 = require("node:zlib");
6
5
  const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
6
+ const node_zlib_1 = require("node:zlib");
7
7
  const js_lib_1 = require("@naturalcycles/js-lib");
8
8
  const __1 = require("../..");
9
9
  const colors_1 = require("../../colors/colors");
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.pipelineToNDJsonFile = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const node_zlib_1 = require("node:zlib");
6
5
  const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
6
+ const node_zlib_1 = require("node:zlib");
7
7
  const js_lib_1 = require("@naturalcycles/js-lib");
8
8
  const __1 = require("../..");
9
9
  const colors_1 = require("../../colors/colors");
@@ -92,7 +92,7 @@ function transformLogProgress(opt = {}) {
92
92
  if (perHour > 900) {
93
93
  perHour = Math.round(perHour / 1000) + 'K';
94
94
  }
95
- logger.log(`${(0, colors_1.dimGrey)((0, js_lib_1.localTime)().toPretty())} ${(0, colors_1.white)(metric)} took ${(0, colors_1.yellow)((0, js_lib_1._since)(started))} so far to process ${(0, colors_1.yellow)(batchedProgress)} rows, ~${(0, colors_1.yellow)(perHour)}/hour`);
95
+ logger.log(`${(0, colors_1.dimGrey)((0, js_lib_1.localTimeNow)().toPretty())} ${(0, colors_1.white)(metric)} took ${(0, colors_1.yellow)((0, js_lib_1._since)(started))} so far to process ${(0, colors_1.yellow)(batchedProgress)} rows, ~${(0, colors_1.yellow)(perHour)}/hour`);
96
96
  }
97
97
  else if (final) {
98
98
  logger.log(`${(0, colors_1.boldWhite)(metric)} took ${(0, colors_1.yellow)((0, js_lib_1._since)(started))} to process ${(0, colors_1.yellow)(batchedProgress)} rows with total RPS of ${(0, colors_1.yellow)(rpsTotal)}`);
@@ -5,7 +5,7 @@ const js_lib_1 = require("@naturalcycles/js-lib");
5
5
  const fs_util_1 = require("../fs/fs.util");
6
6
  const git_util_1 = require("./git.util");
7
7
  function generateBuildInfo(opt = {}) {
8
- const now = (0, js_lib_1.localTime)(opt.overrideTimestamp);
8
+ const now = (0, js_lib_1.localTimeOrNow)(opt.overrideTimestamp);
9
9
  const ts = now.unix();
10
10
  const rev = (0, git_util_1.gitCurrentCommitSha)();
11
11
  const branchName = (0, git_util_1.gitCurrentBranchName)();
@@ -11,7 +11,7 @@ export interface JoiValidationResult<T = any> {
11
11
  *
12
12
  * If `schema` is undefined - returns value as is.
13
13
  */
14
- export declare function validate<T>(value: T, schema?: AnySchema<T>, objectName?: string, options?: ValidationOptions): T;
14
+ export declare function validate<T>(input: any, schema?: AnySchema<T>, objectName?: string, opt?: ValidationOptions): T;
15
15
  /**
16
16
  * Validates with Joi.
17
17
  * Returns JoiValidationResult with converted value and error (if any).
@@ -19,15 +19,15 @@ export declare function validate<T>(value: T, schema?: AnySchema<T>, objectName?
19
19
  *
20
20
  * If `schema` is undefined - returns value as is.
21
21
  */
22
- export declare function getValidationResult<T>(value: T, schema?: AnySchema<T>, objectName?: string, options?: ValidationOptions): JoiValidationResult<T>;
22
+ export declare function getValidationResult<T>(input: any, schema?: AnySchema<T>, objectName?: string, options?: ValidationOptions): JoiValidationResult<T>;
23
23
  /**
24
24
  * Convenience function that returns true if !error.
25
25
  */
26
- export declare function isValid<T>(value: T, schema?: AnySchema<T>): boolean;
27
- export declare function undefinedIfInvalid<T>(value: T, schema?: AnySchema<T>): T | undefined;
26
+ export declare function isValid<T>(input: T, schema?: AnySchema<T>): boolean;
27
+ export declare function undefinedIfInvalid<T>(input: any, schema?: AnySchema<T>): T | undefined;
28
28
  /**
29
- * Will do joi-convertation, regardless of error/validity of value.
29
+ * Will do joi-conversion, regardless of error/validity of value.
30
30
  *
31
31
  * @returns converted value
32
32
  */
33
- export declare function convert<T>(value: T, schema?: AnySchema<T>): T;
33
+ export declare function convert<T>(input: any, schema?: AnySchema<T>): T;
@@ -40,8 +40,8 @@ const defaultOptions = {
40
40
  *
41
41
  * If `schema` is undefined - returns value as is.
42
42
  */
43
- function validate(value, schema, objectName, options = {}) {
44
- const { value: returnValue, error } = getValidationResult(value, schema, objectName, options);
43
+ function validate(input, schema, objectName, opt = {}) {
44
+ const { value: returnValue, error } = getValidationResult(input, schema, objectName, opt);
45
45
  if (error) {
46
46
  throw error;
47
47
  }
@@ -55,18 +55,18 @@ exports.validate = validate;
55
55
  *
56
56
  * If `schema` is undefined - returns value as is.
57
57
  */
58
- function getValidationResult(value, schema, objectName, options = {}) {
58
+ function getValidationResult(input, schema, objectName, options = {}) {
59
59
  if (!schema)
60
- return { value };
61
- const { value: returnValue, error } = schema.validate(value, {
60
+ return { value: input };
61
+ const { value, error } = schema.validate(input, {
62
62
  ...defaultOptions,
63
63
  ...options,
64
64
  });
65
65
  const vr = {
66
- value: returnValue,
66
+ value,
67
67
  };
68
68
  if (error) {
69
- vr.error = createError(value, error, objectName);
69
+ vr.error = createError(input, error, objectName);
70
70
  }
71
71
  return vr;
72
72
  }
@@ -74,35 +74,33 @@ exports.getValidationResult = getValidationResult;
74
74
  /**
75
75
  * Convenience function that returns true if !error.
76
76
  */
77
- function isValid(value, schema) {
77
+ function isValid(input, schema) {
78
78
  if (!schema)
79
- return { value };
80
- const { error } = schema.validate(value, defaultOptions);
79
+ return true;
80
+ const { error } = schema.validate(input, defaultOptions);
81
81
  return !error;
82
82
  }
83
83
  exports.isValid = isValid;
84
- function undefinedIfInvalid(value, schema) {
84
+ function undefinedIfInvalid(input, schema) {
85
85
  if (!schema)
86
- return { value };
87
- const { value: returnValue, error } = schema.validate(value, defaultOptions);
88
- return error ? undefined : returnValue;
86
+ return input;
87
+ const { value, error } = schema.validate(input, defaultOptions);
88
+ return error ? undefined : value;
89
89
  }
90
90
  exports.undefinedIfInvalid = undefinedIfInvalid;
91
91
  /**
92
- * Will do joi-convertation, regardless of error/validity of value.
92
+ * Will do joi-conversion, regardless of error/validity of value.
93
93
  *
94
94
  * @returns converted value
95
95
  */
96
- function convert(value, schema) {
96
+ function convert(input, schema) {
97
97
  if (!schema)
98
- return value;
99
- const { value: returnValue } = schema.validate(value, defaultOptions);
100
- return returnValue;
98
+ return input;
99
+ const { value } = schema.validate(input, defaultOptions);
100
+ return value;
101
101
  }
102
102
  exports.convert = convert;
103
103
  function createError(value, err, objectName) {
104
- if (!err)
105
- return undefined;
106
104
  const tokens = [];
107
105
  const objectId = (0, js_lib_1._isObject)(value) ? value['id'] : undefined;
108
106
  if (objectId || objectName) {
@@ -1,5 +1,4 @@
1
- import Joi from 'joi';
2
- import { Extension, NumberSchema as JoiNumberSchema } from 'joi';
1
+ import Joi, { Extension, NumberSchema as JoiNumberSchema } from 'joi';
3
2
  export interface NumberSchema<TSchema = number> extends JoiNumberSchema<TSchema> {
4
3
  dividable: (q: number) => this;
5
4
  }
@@ -1,5 +1,4 @@
1
- import { Extension, StringSchema as JoiStringSchema } from 'joi';
2
- import Joi from 'joi';
1
+ import Joi, { Extension, StringSchema as JoiStringSchema } from 'joi';
3
2
  export interface StringSchema<TSchema = string> extends JoiStringSchema<TSchema> {
4
3
  dateString: (min?: string, max?: string) => this;
5
4
  }
@@ -41,10 +41,10 @@ function stringExtensions(joi) {
41
41
  let { min, max } = args;
42
42
  // Today allows +-14 hours gap to account for different timezones
43
43
  if (max === 'today') {
44
- max = (0, js_lib_1.localTime)().add(14, 'hour').toISODate();
44
+ max = (0, js_lib_1.localTimeNow)().add(14, 'hour').toISODate();
45
45
  }
46
46
  if (min === 'today') {
47
- min = (0, js_lib_1.localTime)().subtract(14, 'hour').toISODate();
47
+ min = (0, js_lib_1.localTimeNow)().subtract(14, 'hour').toISODate();
48
48
  }
49
49
  // console.log('min/max', min, max)
50
50
  const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(v);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "13.1.2",
3
+ "version": "13.1.4",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
@@ -1,5 +1,5 @@
1
- import path from 'node:path'
2
1
  import fs from 'node:fs'
2
+ import path from 'node:path'
3
3
  import { _assert } from '@naturalcycles/js-lib'
4
4
  import { dimGrey, yellow } from '../colors/colors'
5
5
  import { _readJsonSync, _writeJsonSync, fastGlob } from '../index'
@@ -6,7 +6,7 @@ import {
6
6
  CommonLogLevel,
7
7
  Fetcher,
8
8
  getFetcher,
9
- localTime,
9
+ localTimeNow,
10
10
  PQueue,
11
11
  } from '@naturalcycles/js-lib'
12
12
  import { inspectAny, InspectAnyOptions } from '..'
@@ -180,7 +180,7 @@ export class SlackService<CTX = any> {
180
180
  }
181
181
 
182
182
  export function slackDefaultMessagePrefixHook(msg: SlackMessage): string[] {
183
- const tokens = [localTime().toPretty()]
183
+ const tokens = [localTimeNow().toPretty()]
184
184
  const { ctx } = msg
185
185
 
186
186
  // AppEngine-specific decoration
@@ -1,5 +1,5 @@
1
- import { createUnzip, ZlibOptions } from 'node:zlib'
2
1
  import fs from 'node:fs'
2
+ import { createUnzip, ZlibOptions } from 'node:zlib'
3
3
  import { _hb } from '@naturalcycles/js-lib'
4
4
  import { transformTap, _pipeline, transformSplit } from '../..'
5
5
  import { dimWhite, grey } from '../../colors/colors'
@@ -1,5 +1,5 @@
1
- import { createGzip, ZlibOptions } from 'node:zlib'
2
1
  import fs from 'node:fs'
2
+ import { createGzip, ZlibOptions } from 'node:zlib'
3
3
  import { AppError } from '@naturalcycles/js-lib'
4
4
  import { transformTap, _pipeline, _pathExistsSync, _ensureFileSync } from '../..'
5
5
  import { grey } from '../../colors/colors'
@@ -6,7 +6,7 @@ import {
6
6
  _since,
7
7
  AnyObject,
8
8
  CommonLogger,
9
- localTime,
9
+ localTimeNow,
10
10
  } from '@naturalcycles/js-lib'
11
11
  import { hasColors, boldWhite, dimGrey, white, yellow } from '../../colors/colors'
12
12
  import { SizeStack } from '../sizeStack'
@@ -260,7 +260,7 @@ export function transformLogProgress<IN = any>(
260
260
  }
261
261
 
262
262
  logger.log(
263
- `${dimGrey(localTime().toPretty())} ${white(metric)} took ${yellow(
263
+ `${dimGrey(localTimeNow().toPretty())} ${white(metric)} took ${yellow(
264
264
  _since(started),
265
265
  )} so far to process ${yellow(batchedProgress)} rows, ~${yellow(perHour)}/hour`,
266
266
  )
@@ -2,7 +2,7 @@ import {
2
2
  _filterUndefinedValues,
3
3
  AnyObject,
4
4
  BuildInfo,
5
- localTime,
5
+ localTimeOrNow,
6
6
  UnixTimestampNumber,
7
7
  } from '@naturalcycles/js-lib'
8
8
  import { _pathExistsSync, _readJsonSync } from '../fs/fs.util'
@@ -21,7 +21,7 @@ export interface GenerateBuildInfoOptions {
21
21
  }
22
22
 
23
23
  export function generateBuildInfo(opt: GenerateBuildInfoOptions = {}): BuildInfo {
24
- const now = localTime(opt.overrideTimestamp)
24
+ const now = localTimeOrNow(opt.overrideTimestamp)
25
25
  const ts = now.unix()
26
26
 
27
27
  const rev = gitCurrentCommitSha()
@@ -1,6 +1,5 @@
1
1
  import { promisify } from 'node:util'
2
- import { ZlibOptions } from 'node:zlib'
3
- import zlib from 'node:zlib'
2
+ import zlib, { ZlibOptions } from 'node:zlib'
4
3
 
5
4
  const deflate = promisify(zlib.deflate.bind(zlib))
6
5
  const inflate = promisify(zlib.inflate.bind(zlib))
@@ -49,12 +49,12 @@ const defaultOptions: ValidationOptions = {
49
49
  * If `schema` is undefined - returns value as is.
50
50
  */
51
51
  export function validate<T>(
52
- value: T,
52
+ input: any,
53
53
  schema?: AnySchema<T>,
54
54
  objectName?: string,
55
- options: ValidationOptions = {},
55
+ opt: ValidationOptions = {},
56
56
  ): T {
57
- const { value: returnValue, error } = getValidationResult(value, schema, objectName, options)
57
+ const { value: returnValue, error } = getValidationResult(input, schema, objectName, opt)
58
58
 
59
59
  if (error) {
60
60
  throw error
@@ -71,24 +71,24 @@ export function validate<T>(
71
71
  * If `schema` is undefined - returns value as is.
72
72
  */
73
73
  export function getValidationResult<T>(
74
- value: T,
74
+ input: any,
75
75
  schema?: AnySchema<T>,
76
76
  objectName?: string,
77
77
  options: ValidationOptions = {},
78
78
  ): JoiValidationResult<T> {
79
- if (!schema) return { value } as any
79
+ if (!schema) return { value: input }
80
80
 
81
- const { value: returnValue, error } = schema.validate(value, {
81
+ const { value, error } = schema.validate(input, {
82
82
  ...defaultOptions,
83
83
  ...options,
84
84
  })
85
85
 
86
86
  const vr: JoiValidationResult<T> = {
87
- value: returnValue!,
87
+ value,
88
88
  }
89
89
 
90
90
  if (error) {
91
- vr.error = createError(value, error, objectName)
91
+ vr.error = createError(input, error, objectName)
92
92
  }
93
93
 
94
94
  return vr
@@ -97,34 +97,33 @@ export function getValidationResult<T>(
97
97
  /**
98
98
  * Convenience function that returns true if !error.
99
99
  */
100
- export function isValid<T>(value: T, schema?: AnySchema<T>): boolean {
101
- if (!schema) return { value } as any
100
+ export function isValid<T>(input: T, schema?: AnySchema<T>): boolean {
101
+ if (!schema) return true
102
102
 
103
- const { error } = schema.validate(value, defaultOptions)
103
+ const { error } = schema.validate(input, defaultOptions)
104
104
  return !error
105
105
  }
106
106
 
107
- export function undefinedIfInvalid<T>(value: T, schema?: AnySchema<T>): T | undefined {
108
- if (!schema) return { value } as any
107
+ export function undefinedIfInvalid<T>(input: any, schema?: AnySchema<T>): T | undefined {
108
+ if (!schema) return input
109
109
 
110
- const { value: returnValue, error } = schema.validate(value, defaultOptions)
110
+ const { value, error } = schema.validate(input, defaultOptions)
111
111
 
112
- return error ? undefined : returnValue
112
+ return error ? undefined : value
113
113
  }
114
114
 
115
115
  /**
116
- * Will do joi-convertation, regardless of error/validity of value.
116
+ * Will do joi-conversion, regardless of error/validity of value.
117
117
  *
118
118
  * @returns converted value
119
119
  */
120
- export function convert<T>(value: T, schema?: AnySchema<T>): T {
121
- if (!schema) return value as any
122
- const { value: returnValue } = schema.validate(value, defaultOptions)
123
- return returnValue!
120
+ export function convert<T>(input: any, schema?: AnySchema<T>): T {
121
+ if (!schema) return input
122
+ const { value } = schema.validate(input, defaultOptions)
123
+ return value
124
124
  }
125
125
 
126
126
  function createError(value: any, err: ValidationError, objectName?: string): JoiValidationError {
127
- if (!err) return undefined as any
128
127
  const tokens: string[] = []
129
128
 
130
129
  const objectId = _isObject(value) ? (value['id'] as string) : undefined
@@ -1,5 +1,4 @@
1
- import Joi from 'joi'
2
- import { Extension, NumberSchema as JoiNumberSchema } from 'joi'
1
+ import Joi, { Extension, NumberSchema as JoiNumberSchema } from 'joi'
3
2
 
4
3
  export interface NumberSchema<TSchema = number> extends JoiNumberSchema<TSchema> {
5
4
  dividable: (q: number) => this
@@ -1,6 +1,5 @@
1
- import { LocalDate, localTime } from '@naturalcycles/js-lib'
2
- import { Extension, StringSchema as JoiStringSchema } from 'joi'
3
- import Joi from 'joi'
1
+ import { LocalDate, localTimeNow } from '@naturalcycles/js-lib'
2
+ import Joi, { Extension, StringSchema as JoiStringSchema } from 'joi'
4
3
 
5
4
  export interface StringSchema<TSchema = string> extends JoiStringSchema<TSchema> {
6
5
  dateString: (min?: string, max?: string) => this
@@ -52,10 +51,10 @@ export function stringExtensions(joi: typeof Joi): Extension {
52
51
 
53
52
  // Today allows +-14 hours gap to account for different timezones
54
53
  if (max === 'today') {
55
- max = localTime().add(14, 'hour').toISODate()
54
+ max = localTimeNow().add(14, 'hour').toISODate()
56
55
  }
57
56
  if (min === 'today') {
58
- min = localTime().subtract(14, 'hour').toISODate()
57
+ min = localTimeNow().subtract(14, 'hour').toISODate()
59
58
  }
60
59
  // console.log('min/max', min, max)
61
60