@naturalcycles/nodejs-lib 15.3.0 → 15.4.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.
Files changed (51) hide show
  1. package/dist/bin/slack-this.js +1 -1
  2. package/dist/{exec2.js → exec2/exec2.js} +1 -1
  3. package/dist/index.d.ts +0 -3
  4. package/dist/index.js +0 -3
  5. package/dist/script/runScript.d.ts +1 -0
  6. package/dist/script/runScript.js +4 -4
  7. package/dist/slack/index.d.ts +2 -0
  8. package/dist/slack/index.js +2 -0
  9. package/dist/slack/slack.service.model.d.ts +1 -1
  10. package/dist/stream/ndjson/ndjsonStreamForEach.d.ts +2 -1
  11. package/dist/stream/ndjson/ndjsonStreamForEach.js +5 -2
  12. package/dist/stream/readable/readableForEach.d.ts +1 -1
  13. package/dist/stream/readable/readableForEach.js +1 -1
  14. package/dist/stream/transform/transformLimit.d.ts +1 -1
  15. package/dist/stream/transform/transformLimit.js +2 -1
  16. package/dist/stream/transform/transformOffset.js +1 -1
  17. package/dist/stream/writable/writableForEach.d.ts +1 -1
  18. package/dist/stream/writable/writableForEach.js +2 -1
  19. package/dist/stream/writable/writableFork.js +2 -1
  20. package/dist/util/git2.js +1 -1
  21. package/dist/validation/ajv/ajvSchema.d.ts +10 -8
  22. package/dist/validation/ajv/ajvSchema.js +29 -27
  23. package/dist/validation/ajv/getAjv.d.ts +10 -1
  24. package/dist/validation/ajv/getAjv.js +11 -1
  25. package/dist/validation/ajv/index.d.ts +0 -1
  26. package/dist/validation/ajv/index.js +0 -1
  27. package/dist/validation/joi/joi.validation.util.d.ts +2 -0
  28. package/dist/validation/joi/joi.validation.util.js +2 -0
  29. package/package.json +5 -3
  30. package/src/bin/slack-this.ts +1 -1
  31. package/src/{exec2.ts → exec2/exec2.ts} +1 -1
  32. package/src/index.ts +0 -3
  33. package/src/script/runScript.ts +4 -6
  34. package/src/slack/index.ts +2 -0
  35. package/src/slack/slack.service.model.ts +1 -1
  36. package/src/slack/slack.service.ts +1 -2
  37. package/src/stream/ndjson/ndjsonStreamForEach.ts +6 -4
  38. package/src/stream/readable/readableForEach.ts +2 -2
  39. package/src/stream/transform/transformLimit.ts +2 -1
  40. package/src/stream/transform/transformOffset.ts +1 -1
  41. package/src/stream/writable/writableForEach.ts +2 -2
  42. package/src/stream/writable/writableFork.ts +3 -3
  43. package/src/util/git2.ts +1 -1
  44. package/src/validation/ajv/ajvSchema.ts +43 -33
  45. package/src/validation/ajv/getAjv.ts +12 -1
  46. package/src/validation/ajv/index.ts +0 -1
  47. package/src/validation/joi/joi.validation.util.ts +2 -0
  48. package/dist/validation/ajv/ajv.util.d.ts +0 -21
  49. package/dist/validation/ajv/ajv.util.js +0 -28
  50. package/src/validation/ajv/ajv.util.ts +0 -38
  51. /package/dist/{exec2.d.ts → exec2/exec2.d.ts} +0 -0
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
- import { SlackService } from '../index.js';
3
2
  import { runScript } from '../script/runScript.js';
3
+ import { SlackService } from '../slack/index.js';
4
4
  import { _yargs } from '../yargs/yargs.util.js';
5
5
  runScript(async () => {
6
6
  const { channel, msg, username, emoji, webhook: webhookUrl, } = _yargs().options({
@@ -2,7 +2,7 @@ import { execSync, spawn, spawnSync } from 'node:child_process';
2
2
  import { _since } from '@naturalcycles/js-lib/datetime';
3
3
  import { AppError } from '@naturalcycles/js-lib/error';
4
4
  import { _substringAfterLast } from '@naturalcycles/js-lib/string';
5
- import { dimGrey, dimRed, hasColors, white } from './colors/colors.js';
5
+ import { dimGrey, dimRed, hasColors, white } from '../colors/colors.js';
6
6
  /**
7
7
  * Set of utility functions to work with Spawn / Exec.
8
8
  *
package/dist/index.d.ts CHANGED
@@ -2,14 +2,11 @@ export * from './buffer/buffer.util.js';
2
2
  export * from './diff/tableDiff.js';
3
3
  export * from './infra/process.util.js';
4
4
  export * from './log/log.util.js';
5
- export * from './script/runScript.js';
6
5
  export * from './security/crypto.util.js';
7
6
  export * from './security/hash.util.js';
8
7
  export * from './security/id.util.js';
9
8
  export * from './security/nanoid.js';
10
9
  export * from './security/secret.util.js';
11
- export * from './slack/slack.service.js';
12
- export * from './slack/slack.service.model.js';
13
10
  export * from './string/inspect.js';
14
11
  export * from './util/buildInfo.util.js';
15
12
  export * from './util/env.util.js';
package/dist/index.js CHANGED
@@ -2,14 +2,11 @@ export * from './buffer/buffer.util.js';
2
2
  export * from './diff/tableDiff.js';
3
3
  export * from './infra/process.util.js';
4
4
  export * from './log/log.util.js';
5
- export * from './script/runScript.js';
6
5
  export * from './security/crypto.util.js';
7
6
  export * from './security/hash.util.js';
8
7
  export * from './security/id.util.js';
9
8
  export * from './security/nanoid.js';
10
9
  export * from './security/secret.util.js';
11
- export * from './slack/slack.service.js';
12
- export * from './slack/slack.service.model.js';
13
10
  export * from './string/inspect.js';
14
11
  export * from './util/buildInfo.util.js';
15
12
  export * from './util/env.util.js';
@@ -1,3 +1,4 @@
1
+ import 'dotenv/config';
1
2
  import type { CommonLogger } from '@naturalcycles/js-lib/log';
2
3
  export interface RunScriptOptions {
3
4
  /**
@@ -1,6 +1,8 @@
1
+ import 'dotenv/config';
1
2
  import os from 'node:os';
2
3
  import { pDelay } from '@naturalcycles/js-lib/promise';
3
4
  import { setGlobalStringifyFunction } from '@naturalcycles/js-lib/string';
5
+ import { dimGrey } from '../colors/colors.js';
4
6
  import { inspectStringifyFn } from '../string/inspect.js';
5
7
  const { DEBUG_RUN_SCRIPT } = process.env;
6
8
  /**
@@ -22,6 +24,7 @@ const { DEBUG_RUN_SCRIPT } = process.env;
22
24
  * Set env DEBUG_RUN_SCRIPT for extra debugging.
23
25
  */
24
26
  export function runScript(fn, opt = {}) {
27
+ checkAndlogEnvironment();
25
28
  setGlobalStringifyFunction(inspectStringifyFn);
26
29
  const { logger = console, noExit, registerUncaughtExceptionHandlers = true } = opt;
27
30
  if (registerUncaughtExceptionHandlers || DEBUG_RUN_SCRIPT) {
@@ -40,8 +43,6 @@ export function runScript(fn, opt = {}) {
40
43
  const timeout = setTimeout(() => { }, 10000000);
41
44
  void (async () => {
42
45
  try {
43
- await import('dotenv/config');
44
- await checkAndlogEnvironment();
45
46
  await fn();
46
47
  await pDelay(); // to ensure all async operations are completed
47
48
  if (DEBUG_RUN_SCRIPT)
@@ -62,8 +63,7 @@ export function runScript(fn, opt = {}) {
62
63
  }
63
64
  })();
64
65
  }
65
- async function checkAndlogEnvironment() {
66
- const { dimGrey } = await import('../colors/colors.js');
66
+ function checkAndlogEnvironment() {
67
67
  const { platform, arch, versions: { node }, env: { CPU_LIMIT, NODE_OPTIONS, TZ }, } = process;
68
68
  const cpuLimit = Number(CPU_LIMIT) || undefined;
69
69
  const availableParallelism = os.availableParallelism?.();
@@ -0,0 +1,2 @@
1
+ export * from './slack.service.js';
2
+ export * from './slack.service.model.js';
@@ -0,0 +1,2 @@
1
+ export * from './slack.service.js';
2
+ export * from './slack.service.model.js';
@@ -1,6 +1,6 @@
1
1
  import type { CommonLogger } from '@naturalcycles/js-lib/log';
2
2
  import type { AnyObject } from '@naturalcycles/js-lib/types';
3
- import type { InspectAnyOptions } from '../index.js';
3
+ import type { InspectAnyOptions } from '../string/inspect.js';
4
4
  /**
5
5
  * Properties that exists both in SlackApiBody (as per Slack API) and SlackMessage (our abstraction).
6
6
  */
@@ -1,5 +1,6 @@
1
1
  import type { AbortableAsyncMapper } from '@naturalcycles/js-lib/types';
2
- import { type TransformLogProgressOptions, type TransformMapOptions } from '../index.js';
2
+ import { type TransformLogProgressOptions } from '../transform/transformLogProgress.js';
3
+ import { type TransformMapOptions } from '../transform/transformMap.js';
3
4
  export interface NDJSONStreamForEachOptions<IN = any> extends TransformMapOptions<IN, void>, TransformLogProgressOptions<IN> {
4
5
  inputFilePath: string;
5
6
  }
@@ -1,6 +1,9 @@
1
1
  import { ErrorMode } from '@naturalcycles/js-lib/error';
2
- import { createReadStreamAsNDJSON, } from '../index.js';
3
- import { _pipeline, transformLogProgress, transformMap, writableVoid } from '../index.js';
2
+ import { _pipeline } from '../pipeline/pipeline.js';
3
+ import { transformLogProgress, } from '../transform/transformLogProgress.js';
4
+ import { transformMap } from '../transform/transformMap.js';
5
+ import { writableVoid } from '../writable/writableVoid.js';
6
+ import { createReadStreamAsNDJSON } from './createReadStreamAsNDJSON.js';
4
7
  /**
5
8
  * Convenience function to `forEach` through an ndjson file.
6
9
  */
@@ -1,5 +1,5 @@
1
1
  import type { AbortableAsyncMapper, IndexedMapper } from '@naturalcycles/js-lib/types';
2
- import type { ReadableTyped } from '../index.js';
2
+ import type { ReadableTyped } from '../stream.model.js';
3
3
  import type { TransformMapOptions } from '../transform/transformMap.js';
4
4
  /**
5
5
  * Convenience function to do `.forEach` over a Readable.
@@ -1,5 +1,5 @@
1
1
  import { _passNothingPredicate } from '@naturalcycles/js-lib/types';
2
- import { _pipeline } from '../index.js';
2
+ import { _pipeline } from '../pipeline/pipeline.js';
3
3
  import { transformMap } from '../transform/transformMap.js';
4
4
  /**
5
5
  * Convenience function to do `.forEach` over a Readable.
@@ -1,6 +1,6 @@
1
1
  import type { Readable } from 'node:stream';
2
2
  import type { CommonLogger } from '@naturalcycles/js-lib/log';
3
- import { AbortableTransform } from '../index.js';
3
+ import { AbortableTransform } from '../pipeline/pipeline.js';
4
4
  import type { TransformOptions, TransformTyped } from '../stream.model.js';
5
5
  export interface TransformLimitOptions extends TransformOptions {
6
6
  /**
@@ -1,5 +1,6 @@
1
- import { AbortableTransform, transformNoOp } from '../index.js';
1
+ import { AbortableTransform } from '../pipeline/pipeline.js';
2
2
  import { pipelineClose } from '../stream.util.js';
3
+ import { transformNoOp } from './transformNoOp.js';
3
4
  /**
4
5
  * Class only exists to be able to do `instanceof TransformLimit`
5
6
  * and to set sourceReadable+streamDone to it in `_pipeline`.
@@ -1,5 +1,5 @@
1
1
  import { Transform } from 'node:stream';
2
- import { transformNoOp } from '../index.js';
2
+ import { transformNoOp } from './transformNoOp.js';
3
3
  export function transformOffset(opt) {
4
4
  const { offset } = opt;
5
5
  if (!offset) {
@@ -1,6 +1,6 @@
1
1
  import type { AsyncIndexedMapper, IndexedMapper } from '@naturalcycles/js-lib/types';
2
- import type { TransformMapOptions } from '../index.js';
3
2
  import type { WritableTyped } from '../stream.model.js';
3
+ import { type TransformMapOptions } from '../transform/transformMap.js';
4
4
  /**
5
5
  * Just an alias to transformMap that declares OUT as void.
6
6
  */
@@ -1,5 +1,6 @@
1
1
  import { _passNothingPredicate } from '@naturalcycles/js-lib/types';
2
- import { transformMap, transformMapSync } from '../index.js';
2
+ import { transformMap } from '../transform/transformMap.js';
3
+ import { transformMapSync } from '../transform/transformMapSync.js';
3
4
  /**
4
5
  * Just an alias to transformMap that declares OUT as void.
5
6
  */
@@ -1,5 +1,6 @@
1
1
  import { Writable } from 'node:stream';
2
- import { _pipeline, readableCreate } from '../index.js';
2
+ import { _pipeline } from '../pipeline/pipeline.js';
3
+ import { readableCreate } from '../readable/readableCreate.js';
3
4
  /**
4
5
  * Allows "forking" a stream inside pipeline into a number of pipeline chains (2 or more).
5
6
  * Currently does NOT (!) maintain backpressure.
package/dist/util/git2.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { execSync } from 'node:child_process';
2
2
  import { basename } from 'node:path';
3
- import { exec2 } from '../exec2.js';
3
+ import { exec2 } from '../exec2/exec2.js';
4
4
  /**
5
5
  * Set of utility functions to work with git.
6
6
  */
@@ -15,7 +15,6 @@ export interface AjvSchemaCfg {
15
15
  * Dependent schemas to pass to Ajv instance constructor.
16
16
  * Simpler than instantiating and passing ajv instance yourself.
17
17
  */
18
- schemas?: (JsonSchema | JsonSchemaBuilder | AjvSchema)[];
19
18
  objectName?: string;
20
19
  /**
21
20
  * Option of Ajv.
@@ -24,7 +23,11 @@ export interface AjvSchemaCfg {
24
23
  *
25
24
  * This option is a "shortcut" to skip creating and passing Ajv instance.
26
25
  */
27
- coerceTypes?: boolean;
26
+ /**
27
+ * If true - schema will be compiled on-demand (lazily).
28
+ * Default: false.
29
+ */
30
+ lazy?: boolean;
28
31
  }
29
32
  /**
30
33
  * On creation - compiles ajv validation function.
@@ -35,6 +38,10 @@ export interface AjvSchemaCfg {
35
38
  export declare class AjvSchema<T = unknown> {
36
39
  schema: JsonSchema<T>;
37
40
  private constructor();
41
+ /**
42
+ * Shortcut for AjvSchema.create(schema, { lazy: true })
43
+ */
44
+ static createLazy<T>(schema: JsonSchemaBuilder<T> | JsonSchema<T> | AjvSchema<T>, cfg?: Partial<AjvSchemaCfg>): AjvSchema<T>;
38
45
  /**
39
46
  * Conveniently allows to pass either JsonSchema or JsonSchemaBuilder, or existing AjvSchema.
40
47
  * If it's already an AjvSchema - it'll just return it without any processing.
@@ -45,13 +52,8 @@ export declare class AjvSchema<T = unknown> {
45
52
  * correctly for some reason.
46
53
  */
47
54
  static create<T>(schema: JsonSchemaBuilder<T> | JsonSchema<T> | AjvSchema<T>, cfg?: Partial<AjvSchemaCfg>): AjvSchema<T>;
48
- /**
49
- * Create AjvSchema directly from a filePath of json schema.
50
- * Convenient method that just does fs.readFileSync for you.
51
- */
52
- static readJsonSync<T = unknown>(filePath: string, cfg?: Partial<AjvSchemaCfg>): AjvSchema<T>;
53
55
  readonly cfg: AjvSchemaCfg;
54
- private readonly validateFunction;
56
+ private getValidateFunction;
55
57
  /**
56
58
  * It returns the original object just for convenience.
57
59
  * Reminder: Ajv will MUTATE your object under 2 circumstances:
@@ -1,8 +1,7 @@
1
- import { _isObject } from '@naturalcycles/js-lib';
1
+ import { _isObject, _lazyValue } from '@naturalcycles/js-lib';
2
2
  import { JsonSchemaAnyBuilder } from '@naturalcycles/js-lib/json-schema';
3
3
  import { _filterNullishValues } from '@naturalcycles/js-lib/object';
4
4
  import { _substringBefore } from '@naturalcycles/js-lib/string';
5
- import { fs2 } from '../../fs/fs2.js';
6
5
  import { _inspect } from '../../string/inspect.js';
7
6
  import { AjvValidationError } from './ajvValidationError.js';
8
7
  import { getAjv } from './getAjv.js';
@@ -17,23 +16,35 @@ export class AjvSchema {
17
16
  constructor(schema, cfg = {}) {
18
17
  this.schema = schema;
19
18
  this.cfg = {
19
+ lazy: false,
20
20
  ...cfg,
21
- ajv: cfg.ajv ||
22
- getAjv({
23
- schemas: cfg.schemas?.map(s => {
24
- if (s instanceof AjvSchema)
25
- return s.schema;
26
- if (s instanceof JsonSchemaAnyBuilder)
27
- return s.build();
28
- return s;
29
- }),
30
- coerceTypes: cfg.coerceTypes || false,
31
- // verbose: true,
32
- }),
21
+ ajv: cfg.ajv || getAjv(),
22
+ // ajv:
23
+ // cfg.ajv ||
24
+ // getAjv({
25
+ // schemas: cfg.schemas?.map(s => {
26
+ // if (s instanceof AjvSchema) return s.schema
27
+ // if (s instanceof JsonSchemaAnyBuilder) return s.build()
28
+ // return s as JsonSchema
29
+ // }),
30
+ // coerceTypes: cfg.coerceTypes || false,
31
+ // // verbose: true,
32
+ // }),
33
33
  // Auto-detecting "ObjectName" from $id of the schema (e.g "Address.schema.json")
34
34
  objectName: cfg.objectName || (schema.$id ? _substringBefore(schema.$id, '.') : undefined),
35
35
  };
36
- this.validateFunction = this.cfg.ajv.compile(schema);
36
+ if (!cfg.lazy) {
37
+ this.getValidateFunction(); // compile eagerly
38
+ }
39
+ }
40
+ /**
41
+ * Shortcut for AjvSchema.create(schema, { lazy: true })
42
+ */
43
+ static createLazy(schema, cfg = {}) {
44
+ return AjvSchema.create(schema, {
45
+ lazy: true,
46
+ ...cfg,
47
+ });
37
48
  }
38
49
  /**
39
50
  * Conveniently allows to pass either JsonSchema or JsonSchemaBuilder, or existing AjvSchema.
@@ -52,17 +63,8 @@ export class AjvSchema {
52
63
  }
53
64
  return new AjvSchema(schema, cfg);
54
65
  }
55
- /**
56
- * Create AjvSchema directly from a filePath of json schema.
57
- * Convenient method that just does fs.readFileSync for you.
58
- */
59
- static readJsonSync(filePath, cfg = {}) {
60
- fs2.requireFileToExist(filePath);
61
- const schema = fs2.readJson(filePath);
62
- return new AjvSchema(schema, cfg);
63
- }
64
66
  cfg;
65
- validateFunction;
67
+ getValidateFunction = _lazyValue(() => this.cfg.ajv.compile(this.schema));
66
68
  /**
67
69
  * It returns the original object just for convenience.
68
70
  * Reminder: Ajv will MUTATE your object under 2 circumstances:
@@ -78,12 +80,12 @@ export class AjvSchema {
78
80
  return obj;
79
81
  }
80
82
  isValid(obj) {
81
- return this.validateFunction(obj);
83
+ return this.getValidateFunction()(obj);
82
84
  }
83
85
  getValidationError(obj, opt = {}) {
84
86
  if (this.isValid(obj))
85
87
  return;
86
- const errors = this.validateFunction.errors;
88
+ const errors = this.getValidateFunction().errors;
87
89
  const { objectId = _isObject(obj) ? obj['id'] : undefined, objectName = this.cfg.objectName, } = opt;
88
90
  const name = [objectName || 'Object', objectId].filter(Boolean).join('.');
89
91
  let message = this.cfg.ajv.errorsText(errors, {
@@ -1,8 +1,17 @@
1
1
  import type { Options } from 'ajv';
2
2
  import { Ajv } from 'ajv';
3
+ /**
4
+ * Return cached instance of Ajv with default (recommended) options.
5
+ *
6
+ * This function should be used as much as possible,
7
+ * to benefit from cached Ajv instance.
8
+ */
9
+ export declare const getAjv: import("@naturalcycles/js-lib/types").Lazy<Ajv>;
3
10
  /**
4
11
  * Create Ajv with modified defaults.
5
12
  *
13
+ * !!! Please note that this function is EXPENSIVE computationally !!!
14
+ *
6
15
  * https://ajv.js.org/options.html
7
16
  */
8
- export declare function getAjv(opt?: Options): Ajv;
17
+ export declare function createAjv(opt?: Options): Ajv;
@@ -1,3 +1,4 @@
1
+ import { _lazyValue } from '@naturalcycles/js-lib';
1
2
  import { Ajv } from 'ajv';
2
3
  import ajvFormats from 'ajv-formats';
3
4
  import ajvKeywords from 'ajv-keywords';
@@ -10,12 +11,21 @@ const AJV_OPTIONS = {
10
11
  // https://ajv.js.org/options.html#coercetypes
11
12
  coerceTypes: false, // while `false` - it won't mutate your input
12
13
  };
14
+ /**
15
+ * Return cached instance of Ajv with default (recommended) options.
16
+ *
17
+ * This function should be used as much as possible,
18
+ * to benefit from cached Ajv instance.
19
+ */
20
+ export const getAjv = _lazyValue(createAjv);
13
21
  /**
14
22
  * Create Ajv with modified defaults.
15
23
  *
24
+ * !!! Please note that this function is EXPENSIVE computationally !!!
25
+ *
16
26
  * https://ajv.js.org/options.html
17
27
  */
18
- export function getAjv(opt) {
28
+ export function createAjv(opt) {
19
29
  const ajv = new Ajv({
20
30
  ...AJV_OPTIONS,
21
31
  ...opt,
@@ -1,5 +1,4 @@
1
1
  import Ajv from 'ajv';
2
- export * from './ajv.util.js';
3
2
  export * from './ajvSchema.js';
4
3
  export * from './ajvValidationError.js';
5
4
  export * from './getAjv.js';
@@ -1,5 +1,4 @@
1
1
  import Ajv from 'ajv';
2
- export * from './ajv.util.js';
3
2
  export * from './ajvSchema.js';
4
3
  export * from './ajvValidationError.js';
5
4
  export * from './getAjv.js';
@@ -17,6 +17,8 @@ export declare function validate<T>(input: any, schema?: AnySchema<T>, objectNam
17
17
  * Returns JoiValidationResult with converted value and error (if any).
18
18
  * Does not throw.
19
19
  *
20
+ * Joi does NOT mutate the input.
21
+ *
20
22
  * If `schema` is undefined - returns value as is.
21
23
  */
22
24
  export declare function getValidationResult<T>(input: any, schema?: AnySchema<T>, objectName?: string, options?: ValidationOptions): JoiValidationResult<T>;
@@ -50,6 +50,8 @@ export function validate(input, schema, objectName, opt = {}) {
50
50
  * Returns JoiValidationResult with converted value and error (if any).
51
51
  * Does not throw.
52
52
  *
53
+ * Joi does NOT mutate the input.
54
+ *
53
55
  * If `schema` is undefined - returns value as is.
54
56
  */
55
57
  export function getValidationResult(input, schema, objectName, options = {}) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.3.0",
4
+ "version": "15.4.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@types/js-yaml": "^4",
@@ -30,14 +30,17 @@
30
30
  "./lruMemoCache": "./dist/cache/lruMemoCache.js",
31
31
  "./colors": "./dist/colors/colors.js",
32
32
  "./csv": "./dist/csv/index.js",
33
- "./exec2": "./dist/exec2.js",
33
+ "./exec2": "./dist/exec2/exec2.js",
34
34
  "./fs2": "./dist/fs/fs2.js",
35
35
  "./env": "./dist/fs/json2env.js",
36
36
  "./kpy": "./dist/fs/kpy.js",
37
37
  "./yaml2": "./dist/fs/yaml2.js",
38
38
  "./glob": "./dist/glob/index.js",
39
39
  "./jwt": "./dist/jwt/jwt.service.js",
40
+ "./runScript": "./dist/script/runScript.js",
41
+ "./slack": "./dist/slack/index.js",
40
42
  "./stream": "./dist/stream/index.js",
43
+ "./stream/*.js": "./dist/stream/*.js",
41
44
  "./yargs": "./dist/yargs/yargs.util.js",
42
45
  "./ajv": "./dist/validation/ajv/index.js",
43
46
  "./joi": "./dist/validation/joi/index.js",
@@ -60,7 +63,6 @@
60
63
  "!src/**/__snapshots__",
61
64
  "!src/**/__exclude"
62
65
  ],
63
- "main": "dist/index.js",
64
66
  "types": "dist/index.d.ts",
65
67
  "publishConfig": {
66
68
  "access": "public"
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { SlackService } from '../index.js'
4
3
  import { runScript } from '../script/runScript.js'
4
+ import { SlackService } from '../slack/index.js'
5
5
  import { _yargs } from '../yargs/yargs.util.js'
6
6
 
7
7
  runScript(async () => {
@@ -7,7 +7,7 @@ import type {
7
7
  NumberOfMilliseconds,
8
8
  UnixTimestampMillis,
9
9
  } from '@naturalcycles/js-lib/types'
10
- import { dimGrey, dimRed, hasColors, white } from './colors/colors.js'
10
+ import { dimGrey, dimRed, hasColors, white } from '../colors/colors.js'
11
11
 
12
12
  /**
13
13
  * Set of utility functions to work with Spawn / Exec.
package/src/index.ts CHANGED
@@ -2,14 +2,11 @@ export * from './buffer/buffer.util.js'
2
2
  export * from './diff/tableDiff.js'
3
3
  export * from './infra/process.util.js'
4
4
  export * from './log/log.util.js'
5
- export * from './script/runScript.js'
6
5
  export * from './security/crypto.util.js'
7
6
  export * from './security/hash.util.js'
8
7
  export * from './security/id.util.js'
9
8
  export * from './security/nanoid.js'
10
9
  export * from './security/secret.util.js'
11
- export * from './slack/slack.service.js'
12
- export * from './slack/slack.service.model.js'
13
10
  export * from './string/inspect.js'
14
11
  export * from './util/buildInfo.util.js'
15
12
  export * from './util/env.util.js'
@@ -1,8 +1,10 @@
1
+ import 'dotenv/config'
1
2
  import os from 'node:os'
2
3
  import type { CommonLogger } from '@naturalcycles/js-lib/log'
3
4
  import { pDelay } from '@naturalcycles/js-lib/promise'
4
5
  import { setGlobalStringifyFunction } from '@naturalcycles/js-lib/string'
5
6
  import type { AnyObject } from '@naturalcycles/js-lib/types'
7
+ import { dimGrey } from '../colors/colors.js'
6
8
  import { inspectStringifyFn } from '../string/inspect.js'
7
9
 
8
10
  export interface RunScriptOptions {
@@ -46,6 +48,7 @@ const { DEBUG_RUN_SCRIPT } = process.env
46
48
  * Set env DEBUG_RUN_SCRIPT for extra debugging.
47
49
  */
48
50
  export function runScript(fn: (...args: any[]) => any, opt: RunScriptOptions = {}): void {
51
+ checkAndlogEnvironment()
49
52
  setGlobalStringifyFunction(inspectStringifyFn)
50
53
 
51
54
  const { logger = console, noExit, registerUncaughtExceptionHandlers = true } = opt
@@ -69,9 +72,6 @@ export function runScript(fn: (...args: any[]) => any, opt: RunScriptOptions = {
69
72
 
70
73
  void (async () => {
71
74
  try {
72
- await import('dotenv/config')
73
- await checkAndlogEnvironment()
74
-
75
75
  await fn()
76
76
 
77
77
  await pDelay() // to ensure all async operations are completed
@@ -93,9 +93,7 @@ export function runScript(fn: (...args: any[]) => any, opt: RunScriptOptions = {
93
93
  })()
94
94
  }
95
95
 
96
- async function checkAndlogEnvironment(): Promise<void> {
97
- const { dimGrey } = await import('../colors/colors.js')
98
-
96
+ function checkAndlogEnvironment(): void {
99
97
  const {
100
98
  platform,
101
99
  arch,
@@ -0,0 +1,2 @@
1
+ export * from './slack.service.js'
2
+ export * from './slack.service.model.js'
@@ -1,6 +1,6 @@
1
1
  import type { CommonLogger } from '@naturalcycles/js-lib/log'
2
2
  import type { AnyObject } from '@naturalcycles/js-lib/types'
3
- import type { InspectAnyOptions } from '../index.js'
3
+ import type { InspectAnyOptions } from '../string/inspect.js'
4
4
 
5
5
  /**
6
6
  * Properties that exists both in SlackApiBody (as per Slack API) and SlackMessage (our abstraction).
@@ -8,8 +8,7 @@ import {
8
8
  import { _omit } from '@naturalcycles/js-lib/object'
9
9
  import { PQueue } from '@naturalcycles/js-lib/promise'
10
10
  import type { AnyObject } from '@naturalcycles/js-lib/types'
11
- import type { InspectAnyOptions } from '../index.js'
12
- import { _inspect } from '../index.js'
11
+ import { _inspect, type InspectAnyOptions } from '../index.js'
13
12
  import type {
14
13
  SlackApiBody,
15
14
  SlackAttachmentField,
@@ -1,11 +1,13 @@
1
1
  import { ErrorMode } from '@naturalcycles/js-lib/error'
2
2
  import type { AbortableAsyncMapper } from '@naturalcycles/js-lib/types'
3
+ import { _pipeline } from '../pipeline/pipeline.js'
3
4
  import {
4
- createReadStreamAsNDJSON,
5
+ transformLogProgress,
5
6
  type TransformLogProgressOptions,
6
- type TransformMapOptions,
7
- } from '../index.js'
8
- import { _pipeline, transformLogProgress, transformMap, writableVoid } from '../index.js'
7
+ } from '../transform/transformLogProgress.js'
8
+ import { transformMap, type TransformMapOptions } from '../transform/transformMap.js'
9
+ import { writableVoid } from '../writable/writableVoid.js'
10
+ import { createReadStreamAsNDJSON } from './createReadStreamAsNDJSON.js'
9
11
 
10
12
  export interface NDJSONStreamForEachOptions<IN = any>
11
13
  extends TransformMapOptions<IN, void>,
@@ -1,7 +1,7 @@
1
1
  import type { AbortableAsyncMapper, IndexedMapper } from '@naturalcycles/js-lib/types'
2
2
  import { _passNothingPredicate } from '@naturalcycles/js-lib/types'
3
- import type { ReadableTyped } from '../index.js'
4
- import { _pipeline } from '../index.js'
3
+ import { _pipeline } from '../pipeline/pipeline.js'
4
+ import type { ReadableTyped } from '../stream.model.js'
5
5
  import type { TransformMapOptions } from '../transform/transformMap.js'
6
6
  import { transformMap } from '../transform/transformMap.js'
7
7
 
@@ -1,8 +1,9 @@
1
1
  import type { Readable } from 'node:stream'
2
2
  import type { CommonLogger } from '@naturalcycles/js-lib/log'
3
- import { AbortableTransform, transformNoOp } from '../index.js'
3
+ import { AbortableTransform } from '../pipeline/pipeline.js'
4
4
  import type { TransformOptions, TransformTyped } from '../stream.model.js'
5
5
  import { pipelineClose } from '../stream.util.js'
6
+ import { transformNoOp } from './transformNoOp.js'
6
7
 
7
8
  export interface TransformLimitOptions extends TransformOptions {
8
9
  /**
@@ -1,6 +1,6 @@
1
1
  import { Transform } from 'node:stream'
2
- import { transformNoOp } from '../index.js'
3
2
  import type { TransformOptions, TransformTyped } from '../stream.model.js'
3
+ import { transformNoOp } from './transformNoOp.js'
4
4
 
5
5
  export interface TransformOffsetOptions extends TransformOptions {
6
6
  /**
@@ -1,8 +1,8 @@
1
1
  import type { AsyncIndexedMapper, IndexedMapper } from '@naturalcycles/js-lib/types'
2
2
  import { _passNothingPredicate } from '@naturalcycles/js-lib/types'
3
- import type { TransformMapOptions } from '../index.js'
4
- import { transformMap, transformMapSync } from '../index.js'
5
3
  import type { WritableTyped } from '../stream.model.js'
4
+ import { transformMap, type TransformMapOptions } from '../transform/transformMap.js'
5
+ import { transformMapSync } from '../transform/transformMapSync.js'
6
6
 
7
7
  /**
8
8
  * Just an alias to transformMap that declares OUT as void.
@@ -1,7 +1,7 @@
1
1
  import { Writable } from 'node:stream'
2
- import type { ReadableTyped } from '../index.js'
3
- import { _pipeline, readableCreate } from '../index.js'
4
- import type { TransformOptions, WritableTyped } from '../stream.model.js'
2
+ import { _pipeline } from '../pipeline/pipeline.js'
3
+ import { readableCreate } from '../readable/readableCreate.js'
4
+ import type { ReadableTyped, TransformOptions, WritableTyped } from '../stream.model.js'
5
5
 
6
6
  /**
7
7
  * Allows "forking" a stream inside pipeline into a number of pipeline chains (2 or more).
package/src/util/git2.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { execSync } from 'node:child_process'
2
2
  import { basename } from 'node:path'
3
3
  import type { UnixTimestamp } from '@naturalcycles/js-lib/types'
4
- import { exec2 } from '../exec2.js'
4
+ import { exec2 } from '../exec2/exec2.js'
5
5
 
6
6
  /**
7
7
  * Set of utility functions to work with git.
@@ -1,10 +1,9 @@
1
- import { _isObject } from '@naturalcycles/js-lib'
1
+ import { _isObject, _lazyValue } from '@naturalcycles/js-lib'
2
2
  import type { JsonSchema, JsonSchemaBuilder } from '@naturalcycles/js-lib/json-schema'
3
3
  import { JsonSchemaAnyBuilder } from '@naturalcycles/js-lib/json-schema'
4
4
  import { _filterNullishValues } from '@naturalcycles/js-lib/object'
5
5
  import { _substringBefore } from '@naturalcycles/js-lib/string'
6
- import type { Ajv, ValidateFunction } from 'ajv'
7
- import { fs2 } from '../../fs/fs2.js'
6
+ import type { Ajv } from 'ajv'
8
7
  import { _inspect } from '../../string/inspect.js'
9
8
  import { AjvValidationError } from './ajvValidationError.js'
10
9
  import { getAjv } from './getAjv.js'
@@ -25,7 +24,7 @@ export interface AjvSchemaCfg {
25
24
  * Dependent schemas to pass to Ajv instance constructor.
26
25
  * Simpler than instantiating and passing ajv instance yourself.
27
26
  */
28
- schemas?: (JsonSchema | JsonSchemaBuilder | AjvSchema)[]
27
+ // schemas?: (JsonSchema | JsonSchemaBuilder | AjvSchema)[]
29
28
 
30
29
  objectName?: string
31
30
 
@@ -36,7 +35,13 @@ export interface AjvSchemaCfg {
36
35
  *
37
36
  * This option is a "shortcut" to skip creating and passing Ajv instance.
38
37
  */
39
- coerceTypes?: boolean
38
+ // coerceTypes?: boolean
39
+
40
+ /**
41
+ * If true - schema will be compiled on-demand (lazily).
42
+ * Default: false.
43
+ */
44
+ lazy?: boolean
40
45
  }
41
46
 
42
47
  /**
@@ -51,23 +56,40 @@ export class AjvSchema<T = unknown> {
51
56
  cfg: Partial<AjvSchemaCfg> = {},
52
57
  ) {
53
58
  this.cfg = {
59
+ lazy: false,
54
60
  ...cfg,
55
- ajv:
56
- cfg.ajv ||
57
- getAjv({
58
- schemas: cfg.schemas?.map(s => {
59
- if (s instanceof AjvSchema) return s.schema
60
- if (s instanceof JsonSchemaAnyBuilder) return s.build()
61
- return s as JsonSchema
62
- }),
63
- coerceTypes: cfg.coerceTypes || false,
64
- // verbose: true,
65
- }),
61
+ ajv: cfg.ajv || getAjv(),
62
+ // ajv:
63
+ // cfg.ajv ||
64
+ // getAjv({
65
+ // schemas: cfg.schemas?.map(s => {
66
+ // if (s instanceof AjvSchema) return s.schema
67
+ // if (s instanceof JsonSchemaAnyBuilder) return s.build()
68
+ // return s as JsonSchema
69
+ // }),
70
+ // coerceTypes: cfg.coerceTypes || false,
71
+ // // verbose: true,
72
+ // }),
66
73
  // Auto-detecting "ObjectName" from $id of the schema (e.g "Address.schema.json")
67
74
  objectName: cfg.objectName || (schema.$id ? _substringBefore(schema.$id, '.') : undefined),
68
75
  }
69
76
 
70
- this.validateFunction = this.cfg.ajv.compile<T>(schema)
77
+ if (!cfg.lazy) {
78
+ this.getValidateFunction() // compile eagerly
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Shortcut for AjvSchema.create(schema, { lazy: true })
84
+ */
85
+ static createLazy<T>(
86
+ schema: JsonSchemaBuilder<T> | JsonSchema<T> | AjvSchema<T>,
87
+ cfg: Partial<AjvSchemaCfg> = {},
88
+ ): AjvSchema<T> {
89
+ return AjvSchema.create(schema, {
90
+ lazy: true,
91
+ ...cfg,
92
+ })
71
93
  }
72
94
 
73
95
  /**
@@ -90,21 +112,9 @@ export class AjvSchema<T = unknown> {
90
112
  return new AjvSchema<T>(schema as JsonSchema<T>, cfg)
91
113
  }
92
114
 
93
- /**
94
- * Create AjvSchema directly from a filePath of json schema.
95
- * Convenient method that just does fs.readFileSync for you.
96
- */
97
- static readJsonSync<T = unknown>(
98
- filePath: string,
99
- cfg: Partial<AjvSchemaCfg> = {},
100
- ): AjvSchema<T> {
101
- fs2.requireFileToExist(filePath)
102
- const schema = fs2.readJson<JsonSchema<T>>(filePath)
103
- return new AjvSchema<T>(schema, cfg)
104
- }
105
-
106
115
  readonly cfg: AjvSchemaCfg
107
- private readonly validateFunction: ValidateFunction<T>
116
+
117
+ private getValidateFunction = _lazyValue(() => this.cfg.ajv.compile<T>(this.schema))
108
118
 
109
119
  /**
110
120
  * It returns the original object just for convenience.
@@ -121,13 +131,13 @@ export class AjvSchema<T = unknown> {
121
131
  }
122
132
 
123
133
  isValid(obj: T): boolean {
124
- return this.validateFunction(obj)
134
+ return this.getValidateFunction()(obj)
125
135
  }
126
136
 
127
137
  getValidationError(obj: T, opt: AjvValidationOptions = {}): AjvValidationError | undefined {
128
138
  if (this.isValid(obj)) return
129
139
 
130
- const errors = this.validateFunction.errors!
140
+ const errors = this.getValidateFunction().errors!
131
141
 
132
142
  const {
133
143
  objectId = _isObject(obj) ? (obj['id' as keyof T] as any) : undefined,
@@ -1,3 +1,4 @@
1
+ import { _lazyValue } from '@naturalcycles/js-lib'
1
2
  import type { Options } from 'ajv'
2
3
  import { Ajv } from 'ajv'
3
4
  import ajvFormats from 'ajv-formats'
@@ -13,12 +14,22 @@ const AJV_OPTIONS: Options = {
13
14
  coerceTypes: false, // while `false` - it won't mutate your input
14
15
  }
15
16
 
17
+ /**
18
+ * Return cached instance of Ajv with default (recommended) options.
19
+ *
20
+ * This function should be used as much as possible,
21
+ * to benefit from cached Ajv instance.
22
+ */
23
+ export const getAjv = _lazyValue(createAjv)
24
+
16
25
  /**
17
26
  * Create Ajv with modified defaults.
18
27
  *
28
+ * !!! Please note that this function is EXPENSIVE computationally !!!
29
+ *
19
30
  * https://ajv.js.org/options.html
20
31
  */
21
- export function getAjv(opt?: Options): Ajv {
32
+ export function createAjv(opt?: Options): Ajv {
22
33
  const ajv = new Ajv({
23
34
  ...AJV_OPTIONS,
24
35
  ...opt,
@@ -1,6 +1,5 @@
1
1
  import Ajv from 'ajv'
2
2
 
3
- export * from './ajv.util.js'
4
3
  export * from './ajvSchema.js'
5
4
  export * from './ajvValidationError.js'
6
5
  export * from './getAjv.js'
@@ -70,6 +70,8 @@ export function validate<T>(
70
70
  * Returns JoiValidationResult with converted value and error (if any).
71
71
  * Does not throw.
72
72
  *
73
+ * Joi does NOT mutate the input.
74
+ *
73
75
  * If `schema` is undefined - returns value as is.
74
76
  */
75
77
  export function getValidationResult<T>(
@@ -1,21 +0,0 @@
1
- import type { JsonSchema } from '@naturalcycles/js-lib/json-schema';
2
- import type { GlobOptions } from 'tinyglobby';
3
- import type { AjvSchemaCfg } from './ajvSchema.js';
4
- import { AjvSchema } from './ajvSchema.js';
5
- /**
6
- * Does fs.readFileSync + JSON.parse for ALL files matching the passed `glob` pattern.
7
- * E.g `someDir/**\/*.schema.json`
8
- *
9
- * Returns them as an array of JsonSchema.
10
- *
11
- * @experimental
12
- */
13
- export declare function readJsonSchemas(patterns: string | string[], opt?: Omit<GlobOptions, 'patterns'>): JsonSchema[];
14
- /**
15
- * Reads json schemas from given dir (glob pattern).
16
- * Creates new AjvSchema for each of them (ajv validates them upon creation).
17
- * Passes `schemas` option to ajv, so, schemas may $ref each other and it'll be fine.
18
- *
19
- * @experimental
20
- */
21
- export declare function readAjvSchemas(patterns: string | string[], cfg?: AjvSchemaCfg): AjvSchema[];
@@ -1,28 +0,0 @@
1
- import { globSync } from 'tinyglobby';
2
- import { fs2 } from '../../fs/fs2.js';
3
- import { AjvSchema } from './ajvSchema.js';
4
- /**
5
- * Does fs.readFileSync + JSON.parse for ALL files matching the passed `glob` pattern.
6
- * E.g `someDir/**\/*.schema.json`
7
- *
8
- * Returns them as an array of JsonSchema.
9
- *
10
- * @experimental
11
- */
12
- export function readJsonSchemas(patterns, opt) {
13
- return globSync(patterns, opt).map(fileName => fs2.readJson(fileName));
14
- }
15
- /**
16
- * Reads json schemas from given dir (glob pattern).
17
- * Creates new AjvSchema for each of them (ajv validates them upon creation).
18
- * Passes `schemas` option to ajv, so, schemas may $ref each other and it'll be fine.
19
- *
20
- * @experimental
21
- */
22
- export function readAjvSchemas(patterns, cfg) {
23
- const schemas = readJsonSchemas(patterns);
24
- return schemas.map(schema => AjvSchema.create(schema, {
25
- schemas,
26
- ...cfg,
27
- }));
28
- }
@@ -1,38 +0,0 @@
1
- import type { JsonSchema } from '@naturalcycles/js-lib/json-schema'
2
- import type { GlobOptions } from 'tinyglobby'
3
- import { globSync } from 'tinyglobby'
4
- import { fs2 } from '../../fs/fs2.js'
5
- import type { AjvSchemaCfg } from './ajvSchema.js'
6
- import { AjvSchema } from './ajvSchema.js'
7
-
8
- /**
9
- * Does fs.readFileSync + JSON.parse for ALL files matching the passed `glob` pattern.
10
- * E.g `someDir/**\/*.schema.json`
11
- *
12
- * Returns them as an array of JsonSchema.
13
- *
14
- * @experimental
15
- */
16
- export function readJsonSchemas(
17
- patterns: string | string[],
18
- opt?: Omit<GlobOptions, 'patterns'>,
19
- ): JsonSchema[] {
20
- return globSync(patterns, opt).map(fileName => fs2.readJson(fileName))
21
- }
22
-
23
- /**
24
- * Reads json schemas from given dir (glob pattern).
25
- * Creates new AjvSchema for each of them (ajv validates them upon creation).
26
- * Passes `schemas` option to ajv, so, schemas may $ref each other and it'll be fine.
27
- *
28
- * @experimental
29
- */
30
- export function readAjvSchemas(patterns: string | string[], cfg?: AjvSchemaCfg): AjvSchema[] {
31
- const schemas = readJsonSchemas(patterns)
32
- return schemas.map(schema =>
33
- AjvSchema.create(schema, {
34
- schemas,
35
- ...cfg,
36
- }),
37
- )
38
- }
File without changes