@naturalcycles/nodejs-lib 13.12.0 → 13.14.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.
@@ -20,10 +20,21 @@ function sha256AsBuffer(s) {
20
20
  }
21
21
  exports.sha256AsBuffer = sha256AsBuffer;
22
22
  function hash(s, algorithm, outputEncoding = 'hex') {
23
+ // todo: cleanup after @types/node is updated
24
+ // https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V20.md#crypto-implement-cryptohash
25
+ // Node 20.12+
26
+ if (node_crypto_1.default.hash) {
27
+ return node_crypto_1.default.hash(algorithm, s, outputEncoding);
28
+ }
23
29
  return node_crypto_1.default.createHash(algorithm).update(s).digest(outputEncoding);
24
30
  }
25
31
  exports.hash = hash;
26
32
  function hashAsBuffer(s, algorithm) {
33
+ // todo: cleanup after @types/node is updated
34
+ // Node 20.12+
35
+ if (node_crypto_1.default.hash) {
36
+ return node_crypto_1.default.hash(algorithm, s, 'buffer');
37
+ }
27
38
  return node_crypto_1.default.createHash(algorithm).update(s).digest();
28
39
  }
29
40
  exports.hashAsBuffer = hashAsBuffer;
@@ -14,6 +14,7 @@ export declare const percentageSchema: NumberSchema<number>;
14
14
  export declare const dateStringSchema: StringSchema<string>;
15
15
  export declare const binarySchema: import("joi").BinarySchema<Buffer>;
16
16
  export declare const dateObjectSchema: ObjectSchema<any>;
17
+ export declare const dateIntervalStringSchema: StringSchema<string>;
17
18
  /**
18
19
  * Allows all values of a String Enum.
19
20
  */
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.baseDBEntitySchema = exports.ipAddressSchema = exports.utcOffsetSchema = exports.userAgentSchema = exports.semVerSchema = exports.SEM_VER_REGEX = exports.emailSchema = exports.verSchema = exports.unixTimestampMillis2000Schema = exports.unixTimestampMillisSchema = exports.unixTimestamp2000Schema = exports.unixTimestampSchema = exports.slugSchema = exports.SLUG_REGEX = exports.idBase64UrlSchema = exports.idBase64Schema = exports.idBase62Schema = exports.idSchema = exports.jwtSchema = exports.JWT_REGEX = exports.base64UrlSchema = exports.base64Schema = exports.base62Schema = exports.BASE64URL_REGEX = exports.BASE64_REGEX = exports.BASE62_REGEX = exports.anyObjectSchema = exports.anySchema = exports.oneOfSchema = exports.objectSchema = exports.arraySchema = exports.urlSchema = exports.numberEnumKeySchema = exports.numberEnumValueSchema = exports.stringEnumKeySchema = exports.stringEnumValueSchema = exports.dateObjectSchema = exports.binarySchema = exports.dateStringSchema = exports.percentageSchema = exports.integerSchema = exports.numberSchemaTyped = exports.numberSchema = exports.stringSchemaTyped = exports.stringSchema = exports.booleanDefaultToFalseSchema = exports.booleanSchema = void 0;
3
+ exports.baseDBEntitySchema = exports.ipAddressSchema = exports.utcOffsetSchema = exports.userAgentSchema = exports.semVerSchema = exports.SEM_VER_REGEX = exports.emailSchema = exports.verSchema = exports.unixTimestampMillis2000Schema = exports.unixTimestampMillisSchema = exports.unixTimestamp2000Schema = exports.unixTimestampSchema = exports.slugSchema = exports.SLUG_REGEX = exports.idBase64UrlSchema = exports.idBase64Schema = exports.idBase62Schema = exports.idSchema = exports.jwtSchema = exports.JWT_REGEX = exports.base64UrlSchema = exports.base64Schema = exports.base62Schema = exports.BASE64URL_REGEX = exports.BASE64_REGEX = exports.BASE62_REGEX = exports.anyObjectSchema = exports.anySchema = exports.oneOfSchema = exports.objectSchema = exports.arraySchema = exports.urlSchema = exports.numberEnumKeySchema = exports.numberEnumValueSchema = exports.stringEnumKeySchema = exports.stringEnumValueSchema = exports.dateIntervalStringSchema = exports.dateObjectSchema = exports.binarySchema = exports.dateStringSchema = exports.percentageSchema = exports.integerSchema = exports.numberSchemaTyped = exports.numberSchema = exports.stringSchemaTyped = exports.stringSchema = exports.booleanDefaultToFalseSchema = exports.booleanSchema = void 0;
4
4
  const js_lib_1 = require("@naturalcycles/js-lib");
5
5
  const joi_extensions_1 = require("./joi.extensions");
6
6
  exports.booleanSchema = joi_extensions_1.Joi.boolean();
@@ -16,6 +16,10 @@ exports.percentageSchema = joi_extensions_1.Joi.number().integer().min(0).max(10
16
16
  exports.dateStringSchema = exports.stringSchema.dateString();
17
17
  exports.binarySchema = joi_extensions_1.Joi.binary();
18
18
  exports.dateObjectSchema = joi_extensions_1.Joi.object().instance(Date);
19
+ const DATE_INTERVAL_REGEX = /^\d{4}-\d{2}-\d{2}\/\d{4}-\d{2}-\d{2}$/;
20
+ exports.dateIntervalStringSchema = exports.stringSchema.regex(DATE_INTERVAL_REGEX).messages({
21
+ 'string.pattern.base': `must be a DateInterval string`,
22
+ });
19
23
  /**
20
24
  * Allows all values of a String Enum.
21
25
  */
@@ -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.localTimeNow)().add(14, 'hour').toISODate();
44
+ max = (0, js_lib_1.localTimeNow)().plus(14, 'hour').toISODate();
45
45
  }
46
46
  if (min === 'today') {
47
- min = (0, js_lib_1.localTimeNow)().subtract(14, 'hour').toISODate();
47
+ min = (0, js_lib_1.localTimeNow)().minus(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.12.0",
3
+ "version": "13.14.0",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "docs-serve": "vuepress dev docs",
@@ -22,10 +22,23 @@ export function hash(
22
22
  algorithm: string,
23
23
  outputEncoding: BinaryToTextEncoding = 'hex',
24
24
  ): string {
25
+ // todo: cleanup after @types/node is updated
26
+ // https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V20.md#crypto-implement-cryptohash
27
+ // Node 20.12+
28
+ if ((crypto as any).hash) {
29
+ return (crypto as any).hash(algorithm, s, outputEncoding)
30
+ }
31
+
25
32
  return crypto.createHash(algorithm).update(s).digest(outputEncoding)
26
33
  }
27
34
 
28
35
  export function hashAsBuffer(s: string | Buffer, algorithm: string): Buffer {
36
+ // todo: cleanup after @types/node is updated
37
+ // Node 20.12+
38
+ if ((crypto as any).hash) {
39
+ return (crypto as any).hash(algorithm, s, 'buffer')
40
+ }
41
+
29
42
  return crypto.createHash(algorithm).update(s).digest()
30
43
  }
31
44
 
@@ -24,6 +24,11 @@ export const dateStringSchema = stringSchema.dateString()
24
24
  export const binarySchema = Joi.binary()
25
25
  export const dateObjectSchema = Joi.object().instance(Date)
26
26
 
27
+ const DATE_INTERVAL_REGEX = /^\d{4}-\d{2}-\d{2}\/\d{4}-\d{2}-\d{2}$/
28
+ export const dateIntervalStringSchema = stringSchema.regex(DATE_INTERVAL_REGEX).messages({
29
+ 'string.pattern.base': `must be a DateInterval string`,
30
+ })
31
+
27
32
  /**
28
33
  * Allows all values of a String Enum.
29
34
  */
@@ -51,10 +51,10 @@ export function stringExtensions(joi: typeof Joi): Extension {
51
51
 
52
52
  // Today allows +-14 hours gap to account for different timezones
53
53
  if (max === 'today') {
54
- max = localTimeNow().add(14, 'hour').toISODate()
54
+ max = localTimeNow().plus(14, 'hour').toISODate()
55
55
  }
56
56
  if (min === 'today') {
57
- min = localTimeNow().subtract(14, 'hour').toISODate()
57
+ min = localTimeNow().minus(14, 'hour').toISODate()
58
58
  }
59
59
  // console.log('min/max', min, max)
60
60