@naturalcycles/nodejs-lib 15.148.0 → 15.149.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.
@@ -16,6 +16,10 @@ export declare function createCloudStorageReadStream(url: string): ReadableTyped
16
16
  * which is an optional peer dependency.
17
17
  */
18
18
  export declare function createCloudStorageWriteStream(url: string): Promise<WritableTyped<Uint8Array>>;
19
+ /**
20
+ * Throws if the given url is not a Cloud Storage url.
21
+ */
22
+ export declare function assertCloudStorageUrl(url: string): void;
19
23
  /**
20
24
  * Returns true for a Cloud Storage url: `gs://bucket/path/to/file`.
21
25
  */
@@ -39,12 +39,18 @@ export async function createCloudStorageWriteStream(url) {
39
39
  return storage.getFileWriteStream(bucketName, filePath);
40
40
  }
41
41
  function parseCloudStorageUrl(url) {
42
- _assert(isCloudStorageUrl(url), `Cloud Storage url should start with gs:// but got: ${url}`);
42
+ assertCloudStorageUrl(url);
43
43
  const [bucketName, ...pathParts] = url.slice(CLOUD_STORAGE_URL_PREFIX.length).split('/');
44
44
  const filePath = pathParts.join('/');
45
45
  _assert(bucketName && filePath, `Cloud Storage url should look like gs://bucket/path/to/file but got: ${url}`);
46
46
  return { bucketName, filePath };
47
47
  }
48
+ /**
49
+ * Throws if the given url is not a Cloud Storage url.
50
+ */
51
+ export function assertCloudStorageUrl(url) {
52
+ _assert(isCloudStorageUrl(url), `Cloud Storage url should start with gs:// but got: ${url}`);
53
+ }
48
54
  /**
49
55
  * Returns true for a Cloud Storage url: `gs://bucket/path/to/file`.
50
56
  */
@@ -13,7 +13,7 @@ import type { ReadableTyped } from '../stream.model.js';
13
13
 
14
14
  createReadStreamAsNDJson().take(100)
15
15
 
16
- inputPath can also be a Cloud Storage url (`gs://bucket/path/to/file`), see `Pipeline.fromCloudStorage`.
16
+ inputPath can also be a Cloud Storage url (`gs://bucket/path/to/file`), see `Pipeline.fromCloudStorageFile`.
17
17
  A Cloud Storage file with `Content-Encoding: gzip` metadata is already decompressed by the storage client.
18
18
  Such a file should not have a `.gz` name, otherwise it is decompressed twice, which fails.
19
19
  */
@@ -19,7 +19,7 @@ import { transformJsonParse } from './transformJsonParse.js';
19
19
 
20
20
  createReadStreamAsNDJson().take(100)
21
21
 
22
- inputPath can also be a Cloud Storage url (`gs://bucket/path/to/file`), see `Pipeline.fromCloudStorage`.
22
+ inputPath can also be a Cloud Storage url (`gs://bucket/path/to/file`), see `Pipeline.fromCloudStorageFile`.
23
23
  A Cloud Storage file with `Content-Encoding: gzip` metadata is already decompressed by the storage client.
24
24
  Such a file should not have a `.gz` name, otherwise it is decompressed twice, which fails.
25
25
  */
@@ -34,22 +34,28 @@ export declare class Pipeline<T = unknown> {
34
34
  static fromIterable<T>(input: Iterable<T> | AsyncIterable<T>): Pipeline<T>;
35
35
  /**
36
36
  * sourceFilePath can be a local file path, or a Cloud Storage url (`gs://bucket/path/to/file`),
37
- * see `fromCloudStorage`.
37
+ * see `fromCloudStorageFile`.
38
38
  */
39
39
  static fromNDJsonFile<T>(sourceFilePath: string): Pipeline<T>;
40
40
  /**
41
41
  * sourceFilePath can be a local file path, or a Cloud Storage url (`gs://bucket/path/to/file`),
42
- * see `fromCloudStorage`.
42
+ * see `fromCloudStorageFile`.
43
43
  */
44
44
  static fromFile(sourceFilePath: string): Pipeline<Uint8Array>;
45
45
  /**
46
46
  * Reads the Cloud Storage file at the given url (`gs://bucket/path/to/file`) as a binary stream.
47
+ * Same as `fromFile`, but it throws if the url does not start with `gs://`.
47
48
  *
48
49
  * Requires `@naturalcycles/cloud-storage-lib` to be installed, it is an optional peer dependency.
49
50
  * Its default storage is used (see `getDefaultCloudStorage` there):
50
51
  * it authenticates with Application Default Credentials, and it is in-memory when APP_ENV=test.
51
52
  */
52
- static fromCloudStorage(url: string): Pipeline<Uint8Array>;
53
+ static fromCloudStorageFile(url: string): Pipeline<Uint8Array>;
54
+ /**
55
+ * Same as `fromNDJsonFile`, but it throws if the url does not start with `gs://`.
56
+ * See `fromCloudStorageFile` for the requirements.
57
+ */
58
+ static fromCloudStorageNDJsonFile<T>(url: string): Pipeline<T>;
53
59
  /**
54
60
  * Limits the source Readable, but using `.take(limit)` on it.
55
61
  * This is THE preferred way of limiting the source.
@@ -114,11 +120,21 @@ export declare class Pipeline<T = unknown> {
114
120
  opt?: ZstdOptions): Pipeline<Uint8Array>;
115
121
  zstdDecompress(this: Pipeline<Uint8Array>, opt?: ZstdOptions): Pipeline<Uint8Array>;
116
122
  toArray(opt?: TransformOptions): Promise<T[]>;
123
+ /**
124
+ * Same as `toFile`, but it rejects if the url does not start with `gs://`.
125
+ * See `fromCloudStorageFile` for the requirements.
126
+ */
127
+ toCloudStorageFile(url: string): Promise<void>;
117
128
  /**
118
129
  * outputFilePath can be a local file path, or a Cloud Storage url (`gs://bucket/path/to/file`),
119
- * see `fromCloudStorage` for the requirements.
130
+ * see `fromCloudStorageFile` for the requirements.
120
131
  */
121
132
  toFile(outputFilePath: string): Promise<void>;
133
+ /**
134
+ * Same as `toNDJsonFile`, but it rejects if the url does not start with `gs://`.
135
+ * See `fromCloudStorageFile` for the requirements.
136
+ */
137
+ toCloudStorageNDJsonFile(url: string, level?: Integer): Promise<void>;
122
138
  /**
123
139
  * level corresponds to zstd compression level (if filename ends with .zst),
124
140
  * or gzip compression level (if filename ends with .gz).
@@ -127,7 +143,7 @@ export declare class Pipeline<T = unknown> {
127
143
  * zlib: 3 (optimized for throughput, not size, may be larger than gzip at its default level)
128
144
  *
129
145
  * outputFilePath can be a local file path, or a Cloud Storage url (`gs://bucket/path/to/file`),
130
- * see `fromCloudStorage` for the requirements.
146
+ * see `fromCloudStorageFile` for the requirements.
131
147
  */
132
148
  toNDJsonFile(outputFilePath: string, level?: Integer): Promise<void>;
133
149
  to(destination: WritableTyped<T>): Promise<void>;
@@ -139,4 +155,5 @@ export declare class Pipeline<T = unknown> {
139
155
  * Returns a Writable for a local file, or for a Cloud Storage file if outputFilePath is a `gs://` url.
140
156
  */
141
157
  private createFileWriteStream;
158
+ private requireCloudStorageUrl;
142
159
  }
@@ -5,7 +5,7 @@ import { createAbortableSignal } from '@naturalcycles/js-lib';
5
5
  import { _passthroughPredicate } from '@naturalcycles/js-lib/types';
6
6
  import { fs2 } from '../fs/fs2.js';
7
7
  import { zip2 } from '../zip/zip2.js';
8
- import { createCloudStorageReadStream, createCloudStorageWriteStream, isCloudStorageUrl, } from './cloudStorageStream.js';
8
+ import { assertCloudStorageUrl, createCloudStorageReadStream, createCloudStorageWriteStream, isCloudStorageUrl, } from './cloudStorageStream.js';
9
9
  import { createReadStreamAsNDJson } from './ndjson/createReadStreamAsNDJson.js';
10
10
  import { transformJsonParse } from './ndjson/transformJsonParse.js';
11
11
  import { transformToNDJson } from './ndjson/transformToNDJson.js';
@@ -65,7 +65,7 @@ export class Pipeline {
65
65
  }
66
66
  /**
67
67
  * sourceFilePath can be a local file path, or a Cloud Storage url (`gs://bucket/path/to/file`),
68
- * see `fromCloudStorage`.
68
+ * see `fromCloudStorageFile`.
69
69
  */
70
70
  static fromNDJsonFile(sourceFilePath) {
71
71
  // Important that createReadStreamAsNDJson function is used
@@ -77,25 +77,35 @@ export class Pipeline {
77
77
  }
78
78
  /**
79
79
  * sourceFilePath can be a local file path, or a Cloud Storage url (`gs://bucket/path/to/file`),
80
- * see `fromCloudStorage`.
80
+ * see `fromCloudStorageFile`.
81
81
  */
82
82
  static fromFile(sourceFilePath) {
83
83
  if (isCloudStorageUrl(sourceFilePath))
84
- return Pipeline.fromCloudStorage(sourceFilePath);
84
+ return Pipeline.fromCloudStorageFile(sourceFilePath);
85
85
  return new Pipeline(fs2.createReadStream(sourceFilePath, {
86
86
  highWaterMark: 256 * 1024, // 64KB had no observed speedup over the default; 256KB is +6-12% end-to-end on ~9KB rows, neutral on ~180B rows, 1MB brings nothing more
87
87
  }), false);
88
88
  }
89
89
  /**
90
90
  * Reads the Cloud Storage file at the given url (`gs://bucket/path/to/file`) as a binary stream.
91
+ * Same as `fromFile`, but it throws if the url does not start with `gs://`.
91
92
  *
92
93
  * Requires `@naturalcycles/cloud-storage-lib` to be installed, it is an optional peer dependency.
93
94
  * Its default storage is used (see `getDefaultCloudStorage` there):
94
95
  * it authenticates with Application Default Credentials, and it is in-memory when APP_ENV=test.
95
96
  */
96
- static fromCloudStorage(url) {
97
+ static fromCloudStorageFile(url) {
97
98
  return new Pipeline(createCloudStorageReadStream(url), false);
98
99
  }
100
+ /**
101
+ * Same as `fromNDJsonFile`, but it throws if the url does not start with `gs://`.
102
+ * See `fromCloudStorageFile` for the requirements.
103
+ */
104
+ static fromCloudStorageNDJsonFile(url) {
105
+ assertCloudStorageUrl(url);
106
+ // createReadStreamAsNDJson is used for the same reason as in fromNDJsonFile
107
+ return new Pipeline(createReadStreamAsNDJson(url));
108
+ }
99
109
  /**
100
110
  * Limits the source Readable, but using `.take(limit)` on it.
101
111
  * This is THE preferred way of limiting the source.
@@ -293,14 +303,30 @@ export class Pipeline {
293
303
  await this.run();
294
304
  return arr;
295
305
  }
306
+ /**
307
+ * Same as `toFile`, but it rejects if the url does not start with `gs://`.
308
+ * See `fromCloudStorageFile` for the requirements.
309
+ */
310
+ async toCloudStorageFile(url) {
311
+ this.requireCloudStorageUrl(url);
312
+ await this.toFile(url);
313
+ }
296
314
  /**
297
315
  * outputFilePath can be a local file path, or a Cloud Storage url (`gs://bucket/path/to/file`),
298
- * see `fromCloudStorage` for the requirements.
316
+ * see `fromCloudStorageFile` for the requirements.
299
317
  */
300
318
  async toFile(outputFilePath) {
301
319
  this.destination = await this.createFileWriteStream(outputFilePath);
302
320
  await this.run();
303
321
  }
322
+ /**
323
+ * Same as `toNDJsonFile`, but it rejects if the url does not start with `gs://`.
324
+ * See `fromCloudStorageFile` for the requirements.
325
+ */
326
+ async toCloudStorageNDJsonFile(url, level) {
327
+ this.requireCloudStorageUrl(url);
328
+ await this.toNDJsonFile(url, level);
329
+ }
304
330
  /**
305
331
  * level corresponds to zstd compression level (if filename ends with .zst),
306
332
  * or gzip compression level (if filename ends with .gz).
@@ -309,7 +335,7 @@ export class Pipeline {
309
335
  * zlib: 3 (optimized for throughput, not size, may be larger than gzip at its default level)
310
336
  *
311
337
  * outputFilePath can be a local file path, or a Cloud Storage url (`gs://bucket/path/to/file`),
312
- * see `fromCloudStorage` for the requirements.
338
+ * see `fromCloudStorageFile` for the requirements.
313
339
  */
314
340
  async toNDJsonFile(outputFilePath, level) {
315
341
  this.transforms.push(transformToNDJson());
@@ -397,4 +423,14 @@ export class Pipeline {
397
423
  throw err;
398
424
  }
399
425
  }
426
+ requireCloudStorageUrl(url) {
427
+ try {
428
+ assertCloudStorageUrl(url);
429
+ }
430
+ catch (err) {
431
+ // Same as in createFileWriteStream: the pipeline does not run, so the source is closed here
432
+ this.source.destroy();
433
+ throw err;
434
+ }
435
+ }
400
436
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.148.0",
4
+ "version": "15.149.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@standard-schema/spec": "^1",
@@ -22,7 +22,7 @@
22
22
  }
23
23
  },
24
24
  "devDependencies": {
25
- "@naturalcycles/cloud-storage-lib": "2.19.0",
25
+ "@naturalcycles/cloud-storage-lib": "2.0.0",
26
26
  "@naturalcycles/dev-lib": "0.0.0",
27
27
  "typescript": "^7"
28
28
  },
@@ -50,7 +50,7 @@ export async function createCloudStorageWriteStream(
50
50
  }
51
51
 
52
52
  function parseCloudStorageUrl(url: string): CloudStorageUrl {
53
- _assert(isCloudStorageUrl(url), `Cloud Storage url should start with gs:// but got: ${url}`)
53
+ assertCloudStorageUrl(url)
54
54
 
55
55
  const [bucketName, ...pathParts] = url.slice(CLOUD_STORAGE_URL_PREFIX.length).split('/')
56
56
  const filePath = pathParts.join('/')
@@ -63,6 +63,13 @@ function parseCloudStorageUrl(url: string): CloudStorageUrl {
63
63
  return { bucketName, filePath }
64
64
  }
65
65
 
66
+ /**
67
+ * Throws if the given url is not a Cloud Storage url.
68
+ */
69
+ export function assertCloudStorageUrl(url: string): void {
70
+ _assert(isCloudStorageUrl(url), `Cloud Storage url should start with gs:// but got: ${url}`)
71
+ }
72
+
66
73
  /**
67
74
  * Returns true for a Cloud Storage url: `gs://bucket/path/to/file`.
68
75
  */
@@ -22,7 +22,7 @@ import { transformJsonParse } from './transformJsonParse.js'
22
22
 
23
23
  createReadStreamAsNDJson().take(100)
24
24
 
25
- inputPath can also be a Cloud Storage url (`gs://bucket/path/to/file`), see `Pipeline.fromCloudStorage`.
25
+ inputPath can also be a Cloud Storage url (`gs://bucket/path/to/file`), see `Pipeline.fromCloudStorageFile`.
26
26
  A Cloud Storage file with `Content-Encoding: gzip` metadata is already decompressed by the storage client.
27
27
  Such a file should not have a `.gz` name, otherwise it is decompressed twice, which fails.
28
28
  */
@@ -23,6 +23,7 @@ import type {
23
23
  import { fs2 } from '../fs/fs2.js'
24
24
  import { zip2 } from '../zip/zip2.js'
25
25
  import {
26
+ assertCloudStorageUrl,
26
27
  createCloudStorageReadStream,
27
28
  createCloudStorageWriteStream,
28
29
  isCloudStorageUrl,
@@ -108,7 +109,7 @@ export class Pipeline<T = unknown> {
108
109
 
109
110
  /**
110
111
  * sourceFilePath can be a local file path, or a Cloud Storage url (`gs://bucket/path/to/file`),
111
- * see `fromCloudStorage`.
112
+ * see `fromCloudStorageFile`.
112
113
  */
113
114
  static fromNDJsonFile<T>(sourceFilePath: string): Pipeline<T> {
114
115
  // Important that createReadStreamAsNDJson function is used
@@ -121,10 +122,10 @@ export class Pipeline<T = unknown> {
121
122
 
122
123
  /**
123
124
  * sourceFilePath can be a local file path, or a Cloud Storage url (`gs://bucket/path/to/file`),
124
- * see `fromCloudStorage`.
125
+ * see `fromCloudStorageFile`.
125
126
  */
126
127
  static fromFile(sourceFilePath: string): Pipeline<Uint8Array> {
127
- if (isCloudStorageUrl(sourceFilePath)) return Pipeline.fromCloudStorage(sourceFilePath)
128
+ if (isCloudStorageUrl(sourceFilePath)) return Pipeline.fromCloudStorageFile(sourceFilePath)
128
129
 
129
130
  return new Pipeline(
130
131
  fs2.createReadStream(sourceFilePath, {
@@ -136,15 +137,26 @@ export class Pipeline<T = unknown> {
136
137
 
137
138
  /**
138
139
  * Reads the Cloud Storage file at the given url (`gs://bucket/path/to/file`) as a binary stream.
140
+ * Same as `fromFile`, but it throws if the url does not start with `gs://`.
139
141
  *
140
142
  * Requires `@naturalcycles/cloud-storage-lib` to be installed, it is an optional peer dependency.
141
143
  * Its default storage is used (see `getDefaultCloudStorage` there):
142
144
  * it authenticates with Application Default Credentials, and it is in-memory when APP_ENV=test.
143
145
  */
144
- static fromCloudStorage(url: string): Pipeline<Uint8Array> {
146
+ static fromCloudStorageFile(url: string): Pipeline<Uint8Array> {
145
147
  return new Pipeline(createCloudStorageReadStream(url), false)
146
148
  }
147
149
 
150
+ /**
151
+ * Same as `fromNDJsonFile`, but it throws if the url does not start with `gs://`.
152
+ * See `fromCloudStorageFile` for the requirements.
153
+ */
154
+ static fromCloudStorageNDJsonFile<T>(url: string): Pipeline<T> {
155
+ assertCloudStorageUrl(url)
156
+ // createReadStreamAsNDJson is used for the same reason as in fromNDJsonFile
157
+ return new Pipeline<T>(createReadStreamAsNDJson(url))
158
+ }
159
+
148
160
  /**
149
161
  * Limits the source Readable, but using `.take(limit)` on it.
150
162
  * This is THE preferred way of limiting the source.
@@ -400,15 +412,33 @@ export class Pipeline<T = unknown> {
400
412
  return arr
401
413
  }
402
414
 
415
+ /**
416
+ * Same as `toFile`, but it rejects if the url does not start with `gs://`.
417
+ * See `fromCloudStorageFile` for the requirements.
418
+ */
419
+ async toCloudStorageFile(url: string): Promise<void> {
420
+ this.requireCloudStorageUrl(url)
421
+ await this.toFile(url)
422
+ }
423
+
403
424
  /**
404
425
  * outputFilePath can be a local file path, or a Cloud Storage url (`gs://bucket/path/to/file`),
405
- * see `fromCloudStorage` for the requirements.
426
+ * see `fromCloudStorageFile` for the requirements.
406
427
  */
407
428
  async toFile(outputFilePath: string): Promise<void> {
408
429
  this.destination = await this.createFileWriteStream(outputFilePath)
409
430
  await this.run()
410
431
  }
411
432
 
433
+ /**
434
+ * Same as `toNDJsonFile`, but it rejects if the url does not start with `gs://`.
435
+ * See `fromCloudStorageFile` for the requirements.
436
+ */
437
+ async toCloudStorageNDJsonFile(url: string, level?: Integer): Promise<void> {
438
+ this.requireCloudStorageUrl(url)
439
+ await this.toNDJsonFile(url, level)
440
+ }
441
+
412
442
  /**
413
443
  * level corresponds to zstd compression level (if filename ends with .zst),
414
444
  * or gzip compression level (if filename ends with .gz).
@@ -417,7 +447,7 @@ export class Pipeline<T = unknown> {
417
447
  * zlib: 3 (optimized for throughput, not size, may be larger than gzip at its default level)
418
448
  *
419
449
  * outputFilePath can be a local file path, or a Cloud Storage url (`gs://bucket/path/to/file`),
420
- * see `fromCloudStorage` for the requirements.
450
+ * see `fromCloudStorageFile` for the requirements.
421
451
  */
422
452
  async toNDJsonFile(outputFilePath: string, level?: Integer): Promise<void> {
423
453
  this.transforms.push(transformToNDJson())
@@ -523,4 +553,14 @@ export class Pipeline<T = unknown> {
523
553
  throw err
524
554
  }
525
555
  }
556
+
557
+ private requireCloudStorageUrl(url: string): void {
558
+ try {
559
+ assertCloudStorageUrl(url)
560
+ } catch (err) {
561
+ // Same as in createFileWriteStream: the pipeline does not run, so the source is closed here
562
+ this.source.destroy()
563
+ throw err
564
+ }
565
+ }
526
566
  }