@naturalcycles/nodejs-lib 13.22.0 → 13.23.1

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.
package/dist/fs/fs2.d.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  import type { RmOptions } from 'node:fs';
5
5
  import fs from 'node:fs';
6
6
  import { DumpOptions } from 'js-yaml';
7
- import { ReadableTyped, WritableTyped } from '../stream/stream.model';
7
+ import { ReadableTyped, TransformTyped } from '../stream/stream.model';
8
8
  /**
9
9
  * fs2 conveniently groups filesystem functions together.
10
10
  * Supposed to be almost a drop-in replacement for these things together:
@@ -77,7 +77,7 @@ declare class FS2 {
77
77
  createWriteStream: typeof fs.createWriteStream;
78
78
  createReadStream: typeof fs.createReadStream;
79
79
  createReadStreamAsNDJSON<ROW = any>(inputPath: string): ReadableTyped<ROW>;
80
- createWriteStreamAsNDJSON(outputPath: string): WritableTyped<any>;
80
+ createWriteStreamAsNDJSON(outputPath: string): TransformTyped<any, any>[];
81
81
  }
82
82
  export declare const fs2: FS2;
83
83
  export interface JsonOptions {
package/dist/fs/fs2.js CHANGED
@@ -311,7 +311,8 @@ class FS2 {
311
311
  // .pipe(transformJsonParse<ROW>())
312
312
  }
313
313
  /*
314
- Returns a Writable.
314
+ Returns an array of Transforms, so that you can ...destructure them at
315
+ the end of the _pipeline.
315
316
 
316
317
  Replaces a list of operations:
317
318
  - transformToNDJson
@@ -319,17 +320,18 @@ class FS2 {
319
320
  - fs.createWriteStream
320
321
  */
321
322
  createWriteStreamAsNDJSON(outputPath) {
322
- const transform1 = (0, transformToNDJson_1.transformToNDJson)();
323
- let transform = transform1;
324
- if (outputPath.endsWith('.gz')) {
325
- transform = transform.pipe((0, node_zlib_1.createGzip)({
326
- // chunkSize: 64 * 1024, // no observed speedup
327
- }));
328
- }
329
- transform.pipe(node_fs_1.default.createWriteStream(outputPath, {
330
- // highWaterMark: 64 * 1024, // no observed speedup
331
- }));
332
- return transform1;
323
+ this.ensureFile(outputPath);
324
+ return [
325
+ (0, transformToNDJson_1.transformToNDJson)(),
326
+ outputPath.endsWith('.gz')
327
+ ? (0, node_zlib_1.createGzip)({
328
+ // chunkSize: 64 * 1024, // no observed speedup
329
+ })
330
+ : undefined,
331
+ node_fs_1.default.createWriteStream(outputPath, {
332
+ // highWaterMark: 64 * 1024, // no observed speedup
333
+ }),
334
+ ].filter(js_lib_1._isTruthy);
333
335
  }
334
336
  }
335
337
  exports.fs2 = new FS2();
@@ -26,7 +26,7 @@ async function ndjsonMap(mapper, opt) {
26
26
  }),
27
27
  (0, __1.transformLimit)({ limit: limitOutput, sourceReadable: readable }),
28
28
  (0, __1.transformLogProgress)({ metric: 'saved', logEvery: logEveryOutput }),
29
- __1.fs2.createWriteStreamAsNDJSON(outputFilePath),
29
+ ...__1.fs2.createWriteStreamAsNDJSON(outputFilePath),
30
30
  ]);
31
31
  }
32
32
  exports.ndjsonMap = ndjsonMap;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "13.22.0",
3
+ "version": "13.23.1",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "docs-serve": "vuepress dev docs",
package/src/fs/fs2.ts CHANGED
@@ -19,10 +19,10 @@ import fs from 'node:fs'
19
19
  import fsp from 'node:fs/promises'
20
20
  import path from 'node:path'
21
21
  import { createGzip, createUnzip } from 'node:zlib'
22
- import { _jsonParse } from '@naturalcycles/js-lib'
22
+ import { _isTruthy, _jsonParse } from '@naturalcycles/js-lib'
23
23
  import yaml, { DumpOptions } from 'js-yaml'
24
24
  import { transformToNDJson } from '../stream/ndjson/transformToNDJson'
25
- import { ReadableTyped, WritableTyped } from '../stream/stream.model'
25
+ import { ReadableTyped, TransformTyped } from '../stream/stream.model'
26
26
  import { transformSplitOnNewline } from '../stream/transform/transformSplit'
27
27
  import { requireFileToExist } from '../util/env.util'
28
28
 
@@ -353,29 +353,28 @@ class FS2 {
353
353
  }
354
354
 
355
355
  /*
356
- Returns a Writable.
356
+ Returns an array of Transforms, so that you can ...destructure them at
357
+ the end of the _pipeline.
357
358
 
358
359
  Replaces a list of operations:
359
360
  - transformToNDJson
360
361
  - createGzip (only if path ends with '.gz')
361
362
  - fs.createWriteStream
362
363
  */
363
- createWriteStreamAsNDJSON(outputPath: string): WritableTyped<any> {
364
- const transform1 = transformToNDJson()
365
- let transform = transform1
366
- if (outputPath.endsWith('.gz')) {
367
- transform = transform.pipe(
368
- createGzip({
369
- // chunkSize: 64 * 1024, // no observed speedup
370
- }),
371
- )
372
- }
373
- transform.pipe(
364
+ createWriteStreamAsNDJSON(outputPath: string): TransformTyped<any, any>[] {
365
+ this.ensureFile(outputPath)
366
+
367
+ return [
368
+ transformToNDJson(),
369
+ outputPath.endsWith('.gz')
370
+ ? createGzip({
371
+ // chunkSize: 64 * 1024, // no observed speedup
372
+ })
373
+ : undefined,
374
374
  fs.createWriteStream(outputPath, {
375
375
  // highWaterMark: 64 * 1024, // no observed speedup
376
376
  }),
377
- )
378
- return transform1
377
+ ].filter(_isTruthy) as TransformTyped<any, any>[]
379
378
  }
380
379
  }
381
380
 
@@ -60,6 +60,6 @@ export async function ndjsonMap<IN = any, OUT = any>(
60
60
  }),
61
61
  transformLimit({ limit: limitOutput, sourceReadable: readable }),
62
62
  transformLogProgress({ metric: 'saved', logEvery: logEveryOutput }),
63
- fs2.createWriteStreamAsNDJSON(outputFilePath),
63
+ ...fs2.createWriteStreamAsNDJSON(outputFilePath),
64
64
  ])
65
65
  }