@naturalcycles/nodejs-lib 15.145.1 → 15.146.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
1
  // Inspired by: https://gist.github.com/Jezternz/c8e9fafc2c114e079829974e3764db75
2
+ import { _last } from '@naturalcycles/js-lib/array/array.util.js';
2
3
  import { _assert } from '@naturalcycles/js-lib/error/assert.js';
3
4
  // export class CSVReader {
4
5
  // constructor (cfg: CSVReaderConfig) {
@@ -37,7 +38,7 @@ export function csvStringToArray(str) {
37
38
  if (matches[1].length && matches[1] !== ',') {
38
39
  arr.push([]);
39
40
  }
40
- arr[arr.length - 1].push(matches[2] ? matches[2].replaceAll('""', '"') : matches[3]);
41
+ _last(arr).push(matches[2] ? matches[2].replaceAll('""', '"') : matches[3]);
41
42
  }
42
43
  return arr;
43
44
  }
@@ -1,4 +1,5 @@
1
1
  import fs from 'node:fs';
2
+ import { _assert } from '@naturalcycles/js-lib/error/assert.js';
2
3
  import { dimGrey } from '../colors/colors.js';
3
4
  import { fs2 } from './fs2.js';
4
5
  const JSON2ENV_OPT_DEF = {
@@ -13,9 +14,7 @@ export function json2env(opt) {
13
14
  ...opt,
14
15
  };
15
16
  if (!fs2.pathExists(jsonPath)) {
16
- if (fail) {
17
- throw new Error(`Path doesn't exist: ${jsonPath}`);
18
- }
17
+ _assert(!fail, `Path doesn't exist: ${jsonPath}`);
19
18
  if (!silent) {
20
19
  console.log(`json2env input file doesn't exist, skipping without error (${jsonPath})`);
21
20
  }
@@ -1,3 +1,4 @@
1
+ import { _assert } from '@naturalcycles/js-lib/error/assert.js';
1
2
  import { fs2 } from '../fs/fs2.js';
2
3
  /**
3
4
  * @example
@@ -10,8 +11,7 @@ export function requireEnvKeys(...keys) {
10
11
  // oxlint-disable-next-line unicorn/no-array-reduce
11
12
  return keys.reduce((r, k) => {
12
13
  const v = process.env[k];
13
- if (!v)
14
- throw new Error(`${k} env variable is required, but missing`);
14
+ _assert(v, `${k} env variable is required, but missing`);
15
15
  r[k] = v;
16
16
  return r;
17
17
  }, {});
@@ -391,9 +391,7 @@ function decodeBuffer(buf, hasUtf8Flag) {
391
391
  }
392
392
  function assertPathInside(root, fileName) {
393
393
  const targetPath = path.resolve(root, fileName);
394
- if (targetPath !== root && !targetPath.startsWith(root + path.sep)) {
395
- throw new Error(`zip entry escapes destination directory: ${fileName}`);
396
- }
394
+ _assert(targetPath === root || targetPath.startsWith(root + path.sep), `zip entry escapes destination directory: ${fileName}`);
397
395
  return targetPath;
398
396
  }
399
397
  class FileSource {
@@ -413,9 +411,7 @@ class FileSource {
413
411
  let read = 0;
414
412
  while (read < length) {
415
413
  const { bytesRead } = await this.fileHandle.read(buf, read, length - read, position + read);
416
- if (bytesRead === 0) {
417
- throw new Error(`unexpected EOF: read ${read} of ${length} bytes at offset ${position}`);
418
- }
414
+ _assert(bytesRead, `unexpected EOF: read ${read} of ${length} bytes at offset ${position}`);
419
415
  read += bytesRead;
420
416
  }
421
417
  return buf;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.145.1",
4
+ "version": "15.146.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@standard-schema/spec": "^1",
@@ -14,7 +14,7 @@
14
14
  "yaml": "^2"
15
15
  },
16
16
  "devDependencies": {
17
- "@naturalcycles/dev-lib": "0.0.0",
17
+ "@naturalcycles/dev-lib": "20.73.0",
18
18
  "typescript": "^7"
19
19
  },
20
20
  "exports": {
@@ -1,5 +1,6 @@
1
1
  // Inspired by: https://gist.github.com/Jezternz/c8e9fafc2c114e079829974e3764db75
2
2
 
3
+ import { _last } from '@naturalcycles/js-lib/array/array.util.js'
3
4
  import { _assert } from '@naturalcycles/js-lib/error/assert.js'
4
5
  import type { AnyObject } from '@naturalcycles/js-lib/types'
5
6
 
@@ -65,7 +66,7 @@ export function csvStringToArray(str: string): string[][] {
65
66
  if (matches[1]!.length && matches[1] !== ',') {
66
67
  arr.push([])
67
68
  }
68
- arr[arr.length - 1]!.push(matches[2] ? matches[2].replaceAll('""', '"') : matches[3])
69
+ _last(arr).push(matches[2] ? matches[2].replaceAll('""', '"') : matches[3])
69
70
  }
70
71
  return arr
71
72
  }
@@ -1,4 +1,5 @@
1
1
  import fs from 'node:fs'
2
+ import { _assert } from '@naturalcycles/js-lib/error/assert.js'
2
3
  import type { AnyObject } from '@naturalcycles/js-lib/types'
3
4
  import { dimGrey } from '../colors/colors.js'
4
5
  import { fs2 } from './fs2.js'
@@ -47,9 +48,7 @@ export function json2env(opt: Json2EnvOptions): void {
47
48
  }
48
49
 
49
50
  if (!fs2.pathExists(jsonPath)) {
50
- if (fail) {
51
- throw new Error(`Path doesn't exist: ${jsonPath}`)
52
- }
51
+ _assert(!fail, `Path doesn't exist: ${jsonPath}`)
53
52
 
54
53
  if (!silent) {
55
54
  console.log(`json2env input file doesn't exist, skipping without error (${jsonPath})`)
@@ -1,3 +1,4 @@
1
+ import { _assert } from '@naturalcycles/js-lib/error/assert.js'
1
2
  import type { ValuesOf } from '@naturalcycles/js-lib/types'
2
3
  import { fs2 } from '../fs/fs2.js'
3
4
 
@@ -15,7 +16,7 @@ export function requireEnvKeys<T extends readonly string[]>(
15
16
  return keys.reduce(
16
17
  (r, k) => {
17
18
  const v = process.env[k]
18
- if (!v) throw new Error(`${k} env variable is required, but missing`)
19
+ _assert(v, `${k} env variable is required, but missing`)
19
20
  r[k as ValuesOf<T>] = v
20
21
  return r
21
22
  },
@@ -477,9 +477,10 @@ function decodeBuffer(buf: Buffer, hasUtf8Flag: boolean): string {
477
477
 
478
478
  function assertPathInside(root: string, fileName: string): string {
479
479
  const targetPath = path.resolve(root, fileName)
480
- if (targetPath !== root && !targetPath.startsWith(root + path.sep)) {
481
- throw new Error(`zip entry escapes destination directory: ${fileName}`)
482
- }
480
+ _assert(
481
+ targetPath === root || targetPath.startsWith(root + path.sep),
482
+ `zip entry escapes destination directory: ${fileName}`,
483
+ )
483
484
  return targetPath
484
485
  }
485
486
 
@@ -510,9 +511,7 @@ class FileSource implements ZipSource {
510
511
  let read = 0
511
512
  while (read < length) {
512
513
  const { bytesRead } = await this.fileHandle.read(buf, read, length - read, position + read)
513
- if (bytesRead === 0) {
514
- throw new Error(`unexpected EOF: read ${read} of ${length} bytes at offset ${position}`)
515
- }
514
+ _assert(bytesRead, `unexpected EOF: read ${read} of ${length} bytes at offset ${position}`)
516
515
  read += bytesRead
517
516
  }
518
517
  return buf