@naturalcycles/nodejs-lib 15.145.1 → 15.147.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
  }
package/dist/fs/fs2.js CHANGED
@@ -271,11 +271,9 @@ class FS2 {
271
271
  * Doesn't throw, returns false instead.
272
272
  */
273
273
  isDirectory(filePath) {
274
- return (fs2
275
- .stat(filePath, {
274
+ return (this.stat(filePath, {
276
275
  throwIfNoEntry: false,
277
- })
278
- ?.isDirectory() || false);
276
+ })?.isDirectory() || false);
279
277
  }
280
278
  // Re-export the whole fs/fsp, for the edge cases where they are needed
281
279
  fs = fs;
@@ -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
  }, {});
@@ -430,17 +430,15 @@ export declare class JObjectInfer<PROPS extends Record<string, JSchema<any, any>
430
430
  * When set, the validation will not strip away properties that are not specified explicitly in the schema.
431
431
  */
432
432
  allowAdditionalProperties(): this;
433
- extend<NEW_PROPS extends Record<string, JSchema<any, any>>>(props: NEW_PROPS): JObjectInfer<{
434
- [K in keyof PROPS | keyof NEW_PROPS]: K extends keyof NEW_PROPS ? NEW_PROPS[K] : K extends keyof PROPS ? PROPS[K] : never;
435
- }, Opt>;
433
+ extend<NEW_PROPS extends Record<string, JSchema<any, any>>>(props: NEW_PROPS): JObjectInfer<ExtendedProps<PROPS, NEW_PROPS>, Opt>;
436
434
  /**
437
435
  * Extends the current schema with `id`, `created` and `updated` according to NC DB conventions.
438
436
  */
439
- dbEntity(): JObjectInfer<{ [K in "created" | "id" | "updated" | keyof PROPS]: K extends "created" | "id" | "updated" ? {
437
+ dbEntity(): JObjectInfer<ExtendedProps<PROPS, {
440
438
  id: JString<string, false>;
441
439
  created: JNumber<UnixTimestamp, false>;
442
440
  updated: JNumber<UnixTimestamp, false>;
443
- }[K] : K extends keyof PROPS ? PROPS[K] : never; }, Opt>;
441
+ }>, Opt>;
444
442
  }
445
443
  export declare class JArray<OUT, Opt, ARR extends OUT[] = OUT[]> extends JBuilder<ARR, Opt> {
446
444
  constructor(itemsSchema: JSchema<OUT, Opt>);
@@ -709,6 +707,15 @@ type RelaxIndexSignature<T> = T extends readonly unknown[] ? T : T extends AnyOb
709
707
  [K in keyof T]: RelaxIndexSignature<T[K]>;
710
708
  } : T;
711
709
  type Override<T, U> = Omit<T, keyof U> & U;
710
+ /**
711
+ * Props of `JObjectInfer.extend()`: NEW_PROPS override PROPS with the same key.
712
+ *
713
+ * A named alias keeps the return type annotation and the cast in `extend()` identical.
714
+ * TypeScript 7.1 reports two separately written copies of this mapped type as unrelated (TS2719).
715
+ */
716
+ type ExtendedProps<PROPS, NEW_PROPS> = {
717
+ [K in keyof PROPS | keyof NEW_PROPS]: K extends keyof NEW_PROPS ? NEW_PROPS[K] : K extends keyof PROPS ? PROPS[K] : never;
718
+ };
712
719
  declare const allowExtraKeysSymbol: unique symbol;
713
720
  type HasAllowExtraKeys<T> = T extends {
714
721
  readonly [allowExtraKeysSymbol]?: true;
@@ -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.147.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.76.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
  }
package/src/fs/fs2.ts CHANGED
@@ -312,11 +312,9 @@ class FS2 {
312
312
  */
313
313
  isDirectory(filePath: string): boolean {
314
314
  return (
315
- fs2
316
- .stat(filePath, {
317
- throwIfNoEntry: false,
318
- })
319
- ?.isDirectory() || false
315
+ this.stat(filePath, {
316
+ throwIfNoEntry: false,
317
+ })?.isDirectory() || false
320
318
  )
321
319
  }
322
320
 
@@ -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
  },
@@ -1306,16 +1306,7 @@ export class JObjectInfer<
1306
1306
 
1307
1307
  extend<NEW_PROPS extends Record<string, JSchema<any, any>>>(
1308
1308
  props: NEW_PROPS,
1309
- ): JObjectInfer<
1310
- {
1311
- [K in keyof PROPS | keyof NEW_PROPS]: K extends keyof NEW_PROPS
1312
- ? NEW_PROPS[K]
1313
- : K extends keyof PROPS
1314
- ? PROPS[K]
1315
- : never
1316
- },
1317
- Opt
1318
- > {
1309
+ ): JObjectInfer<ExtendedProps<PROPS, NEW_PROPS>, Opt> {
1319
1310
  const newBuilder = new JObjectInfer<PROPS, Opt>()
1320
1311
  _objectAssign(newBuilder.schema, deepCopyPreservingFunctions(this.schema))
1321
1312
 
@@ -1327,16 +1318,7 @@ export class JObjectInfer<
1327
1318
  // the new schema loses that quality.
1328
1319
  _objectAssign(newBuilder.schema, { hasIsOfTypeCheck: false })
1329
1320
 
1330
- return newBuilder as any as JObjectInfer<
1331
- {
1332
- [K in keyof PROPS | keyof NEW_PROPS]: K extends keyof NEW_PROPS
1333
- ? NEW_PROPS[K]
1334
- : K extends keyof PROPS
1335
- ? PROPS[K]
1336
- : never
1337
- },
1338
- Opt
1339
- >
1321
+ return newBuilder as any as JObjectInfer<ExtendedProps<PROPS, NEW_PROPS>, Opt>
1340
1322
  }
1341
1323
 
1342
1324
  /**
@@ -2283,6 +2265,20 @@ type RelaxIndexSignature<T> = T extends readonly unknown[]
2283
2265
 
2284
2266
  type Override<T, U> = Omit<T, keyof U> & U
2285
2267
 
2268
+ /**
2269
+ * Props of `JObjectInfer.extend()`: NEW_PROPS override PROPS with the same key.
2270
+ *
2271
+ * A named alias keeps the return type annotation and the cast in `extend()` identical.
2272
+ * TypeScript 7.1 reports two separately written copies of this mapped type as unrelated (TS2719).
2273
+ */
2274
+ type ExtendedProps<PROPS, NEW_PROPS> = {
2275
+ [K in keyof PROPS | keyof NEW_PROPS]: K extends keyof NEW_PROPS
2276
+ ? NEW_PROPS[K]
2277
+ : K extends keyof PROPS
2278
+ ? PROPS[K]
2279
+ : never
2280
+ }
2281
+
2286
2282
  declare const allowExtraKeysSymbol: unique symbol
2287
2283
 
2288
2284
  type HasAllowExtraKeys<T> = T extends { readonly [allowExtraKeysSymbol]?: true } ? true : false
@@ -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