@naturalcycles/cloud-storage-lib 1.10.0 → 1.12.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,12 +1,10 @@
1
1
  /// <reference types="node" />
2
- /// <reference types="node" />
3
- import { Readable, Writable } from 'node:stream';
4
- import { Storage, StorageOptions } from '@google-cloud/storage';
2
+ import type { Storage, StorageOptions } from '@google-cloud/storage';
5
3
  import { CommonLogger, LocalTimeInput } from '@naturalcycles/js-lib';
6
- import { ReadableTyped } from '@naturalcycles/nodejs-lib';
7
- import { CommonStorage, CommonStorageGetOptions, FileEntry } from './commonStorage';
8
- import { GCPServiceAccount } from './model';
9
- export { Storage, type StorageOptions, };
4
+ import type { ReadableBinary, ReadableTyped, WritableBinary } from '@naturalcycles/nodejs-lib';
5
+ import type { CommonStorage, CommonStorageGetOptions, FileEntry } from './commonStorage';
6
+ import type { GCPServiceAccount } from './model';
7
+ export { type Storage, type StorageOptions, };
10
8
  /**
11
9
  * This object is intentionally made to NOT extend StorageOptions,
12
10
  * because StorageOptions is complicated and provides just too many ways
@@ -56,9 +54,9 @@ export declare class CloudStorage implements CommonStorage {
56
54
  * Returns a Readable that is NOT object mode,
57
55
  * so you can e.g pipe it to fs.createWriteStream()
58
56
  */
59
- getFileReadStream(bucketName: string, filePath: string): Readable;
57
+ getFileReadStream(bucketName: string, filePath: string): ReadableBinary;
60
58
  saveFile(bucketName: string, filePath: string, content: Buffer): Promise<void>;
61
- getFileWriteStream(bucketName: string, filePath: string): Writable;
59
+ getFileWriteStream(bucketName: string, filePath: string): WritableBinary;
62
60
  uploadFile(localFilePath: string, bucketName: string, bucketFilePath: string): Promise<void>;
63
61
  setFileVisibility(bucketName: string, filePath: string, isPublic: boolean): Promise<void>;
64
62
  getFileVisibility(bucketName: string, filePath: string): Promise<boolean>;
@@ -1,8 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CloudStorage = exports.Storage = void 0;
4
- const storage_1 = require("@google-cloud/storage");
5
- Object.defineProperty(exports, "Storage", { enumerable: true, get: function () { return storage_1.Storage; } });
3
+ exports.CloudStorage = void 0;
6
4
  const js_lib_1 = require("@naturalcycles/js-lib");
7
5
  const MAX_RECURSION_DEPTH = 10;
8
6
  const BATCH_SIZE = 32;
@@ -20,7 +18,8 @@ class CloudStorage {
20
18
  };
21
19
  }
22
20
  static createFromGCPServiceAccount(credentials, cfg) {
23
- const storage = new storage_1.Storage({
21
+ const storageLib = require('@google-cloud/storage');
22
+ const storage = new storageLib.Storage({
24
23
  credentials,
25
24
  // Explicitly passing it here to fix this error:
26
25
  // Error: Unable to detect a Project Id in the current environment.
@@ -32,7 +31,8 @@ class CloudStorage {
32
31
  return new CloudStorage(storage, cfg);
33
32
  }
34
33
  static createFromStorageOptions(storageOptions, cfg) {
35
- const storage = new storage_1.Storage(storageOptions);
34
+ const storageLib = require('@google-cloud/storage');
35
+ const storage = new storageLib.Storage(storageOptions);
36
36
  return new CloudStorage(storage, cfg);
37
37
  }
38
38
  /**
@@ -172,6 +172,12 @@ class CloudStorage {
172
172
  async combineFiles(bucketName, filePaths, toPath, toBucket, currentRecursionDepth = 0) {
173
173
  (0, js_lib_1._assert)(currentRecursionDepth <= MAX_RECURSION_DEPTH, `combineFiles reached max recursion depth of ${MAX_RECURSION_DEPTH}`);
174
174
  const { logger, debug } = this.cfg;
175
+ if (filePaths.length === 0) {
176
+ if (debug) {
177
+ logger.log(`[${currentRecursionDepth}] Nothing to compose, returning early!`);
178
+ }
179
+ return;
180
+ }
175
181
  if (debug) {
176
182
  logger.log(`[${currentRecursionDepth}] Will compose ${filePaths.length} files, by batches of ${BATCH_SIZE}`);
177
183
  }
@@ -1,8 +1,6 @@
1
1
  /// <reference types="node" />
2
- /// <reference types="node" />
3
- import { Readable, Writable } from 'node:stream';
4
- import { LocalTimeInput } from '@naturalcycles/js-lib';
5
- import { ReadableTyped } from '@naturalcycles/nodejs-lib';
2
+ import type { LocalTimeInput } from '@naturalcycles/js-lib';
3
+ import type { ReadableBinary, ReadableTyped, WritableBinary } from '@naturalcycles/nodejs-lib';
6
4
  export interface FileEntry {
7
5
  filePath: string;
8
6
  content: Buffer;
@@ -74,8 +72,8 @@ export interface CommonStorage {
74
72
  getFileNames: (bucketName: string, opt?: CommonStorageGetOptions) => Promise<string[]>;
75
73
  getFileNamesStream: (bucketName: string, opt?: CommonStorageGetOptions) => ReadableTyped<string>;
76
74
  getFilesStream: (bucketName: string, opt?: CommonStorageGetOptions) => ReadableTyped<FileEntry>;
77
- getFileReadStream: (bucketName: string, filePath: string) => Readable;
78
- getFileWriteStream: (bucketName: string, filePath: string) => Writable;
75
+ getFileReadStream: (bucketName: string, filePath: string) => ReadableBinary;
76
+ getFileWriteStream: (bucketName: string, filePath: string) => WritableBinary;
79
77
  /**
80
78
  * Upload local file to the bucket (by streaming it).
81
79
  */
@@ -1,8 +1,6 @@
1
1
  /// <reference types="node" />
2
- /// <reference types="node" />
3
- import { Readable, Writable } from 'node:stream';
4
2
  import { LocalTimeInput, StringMap } from '@naturalcycles/js-lib';
5
- import { ReadableTyped } from '@naturalcycles/nodejs-lib';
3
+ import { ReadableBinary, ReadableTyped, WritableBinary } from '@naturalcycles/nodejs-lib';
6
4
  import { CommonStorage, CommonStorageGetOptions, FileEntry } from './commonStorage';
7
5
  export declare class InMemoryCommonStorage implements CommonStorage {
8
6
  /**
@@ -22,8 +20,8 @@ export declare class InMemoryCommonStorage implements CommonStorage {
22
20
  getFileNames(bucketName: string, opt?: CommonStorageGetOptions): Promise<string[]>;
23
21
  getFileNamesStream(bucketName: string, opt?: CommonStorageGetOptions): ReadableTyped<string>;
24
22
  getFilesStream(bucketName: string, opt?: CommonStorageGetOptions): ReadableTyped<FileEntry>;
25
- getFileReadStream(bucketName: string, filePath: string): Readable;
26
- getFileWriteStream(_bucketName: string, _filePath: string): Writable;
23
+ getFileReadStream(bucketName: string, filePath: string): ReadableBinary;
24
+ getFileWriteStream(_bucketName: string, _filePath: string): WritableBinary;
27
25
  uploadFile(localFilePath: string, bucketName: string, bucketFilePath: string): Promise<void>;
28
26
  setFileVisibility(bucketName: string, filePath: string, isPublic: boolean): Promise<void>;
29
27
  getFileVisibility(bucketName: string, filePath: string): Promise<boolean>;
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InMemoryCommonStorage = void 0;
4
- const node_stream_1 = require("node:stream");
5
4
  const js_lib_1 = require("@naturalcycles/js-lib");
6
5
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
7
6
  class InMemoryCommonStorage {
@@ -17,7 +16,7 @@ class InMemoryCommonStorage {
17
16
  return Object.keys(this.data);
18
17
  }
19
18
  getBucketNamesStream() {
20
- return node_stream_1.Readable.from(Object.keys(this.data));
19
+ return (0, nodejs_lib_1.readableFrom)(Object.keys(this.data));
21
20
  }
22
21
  async fileExists(bucketName, filePath) {
23
22
  return !!this.data[bucketName]?.[filePath];
@@ -52,14 +51,14 @@ class InMemoryCommonStorage {
52
51
  }
53
52
  getFileNamesStream(bucketName, opt = {}) {
54
53
  const { prefix = '', fullPaths = true } = opt;
55
- return node_stream_1.Readable.from(Object.keys(this.data[bucketName] || {})
54
+ return (0, nodejs_lib_1.readableFrom)(Object.keys(this.data[bucketName] || {})
56
55
  .filter(filePath => filePath.startsWith(prefix))
57
56
  .slice(0, opt.limit)
58
57
  .map(n => (fullPaths ? n : (0, js_lib_1._substringAfterLast)(n, '/'))));
59
58
  }
60
59
  getFilesStream(bucketName, opt = {}) {
61
60
  const { prefix = '', fullPaths = true } = opt;
62
- return node_stream_1.Readable.from(Object.entries(this.data[bucketName] || {})
61
+ return (0, nodejs_lib_1.readableFrom)((0, js_lib_1._stringMapEntries)(this.data[bucketName] || {})
63
62
  .map(([filePath, content]) => ({
64
63
  filePath,
65
64
  content,
@@ -69,7 +68,7 @@ class InMemoryCommonStorage {
69
68
  .map(f => (fullPaths ? f : { ...f, filePath: (0, js_lib_1._substringAfterLast)(f.filePath, '/') })));
70
69
  }
71
70
  getFileReadStream(bucketName, filePath) {
72
- return node_stream_1.Readable.from(this.data[bucketName][filePath]);
71
+ return (0, nodejs_lib_1.readableFrom)(this.data[bucketName][filePath]);
73
72
  }
74
73
  getFileWriteStream(_bucketName, _filePath) {
75
74
  throw new Error('Method not implemented.');
package/package.json CHANGED
@@ -35,7 +35,7 @@
35
35
  "engines": {
36
36
  "node": ">=18.12.0"
37
37
  },
38
- "version": "1.10.0",
38
+ "version": "1.12.0",
39
39
  "description": "CommonStorage implementation based on Google Cloud Storage",
40
40
  "author": "Natural Cycles Team",
41
41
  "license": "MIT"
@@ -1,5 +1,7 @@
1
- import { Readable, Writable } from 'node:stream'
2
- import { File, Storage, StorageOptions } from '@google-cloud/storage'
1
+ // eslint-disable-next-line import/no-duplicates
2
+ import type { File, Storage, StorageOptions } from '@google-cloud/storage'
3
+ // eslint-disable-next-line import/no-duplicates
4
+ import type * as StorageLib from '@google-cloud/storage'
3
5
  import {
4
6
  _assert,
5
7
  _chunk,
@@ -11,13 +13,13 @@ import {
11
13
  pMap,
12
14
  SKIP,
13
15
  } from '@naturalcycles/js-lib'
14
- import { ReadableTyped } from '@naturalcycles/nodejs-lib'
15
- import { CommonStorage, CommonStorageGetOptions, FileEntry } from './commonStorage'
16
- import { GCPServiceAccount } from './model'
16
+ import type { ReadableBinary, ReadableTyped, WritableBinary } from '@naturalcycles/nodejs-lib'
17
+ import type { CommonStorage, CommonStorageGetOptions, FileEntry } from './commonStorage'
18
+ import type { GCPServiceAccount } from './model'
17
19
 
18
20
  export {
19
21
  // This is the latest version, to be imported by consumers
20
- Storage,
22
+ type Storage,
21
23
  type StorageOptions,
22
24
  }
23
25
 
@@ -69,7 +71,9 @@ export class CloudStorage implements CommonStorage {
69
71
  credentials?: GCPServiceAccount,
70
72
  cfg?: CloudStorageCfg,
71
73
  ): CloudStorage {
72
- const storage = new Storage({
74
+ const storageLib = require('@google-cloud/storage') as typeof StorageLib
75
+
76
+ const storage = new storageLib.Storage({
73
77
  credentials,
74
78
  // Explicitly passing it here to fix this error:
75
79
  // Error: Unable to detect a Project Id in the current environment.
@@ -86,7 +90,8 @@ export class CloudStorage implements CommonStorage {
86
90
  storageOptions?: StorageOptions,
87
91
  cfg?: CloudStorageCfg,
88
92
  ): CloudStorage {
89
- const storage = new Storage(storageOptions)
93
+ const storageLib = require('@google-cloud/storage') as typeof StorageLib
94
+ const storage = new storageLib.Storage(storageOptions)
90
95
  return new CloudStorage(storage, cfg)
91
96
  }
92
97
 
@@ -192,7 +197,7 @@ export class CloudStorage implements CommonStorage {
192
197
  * Returns a Readable that is NOT object mode,
193
198
  * so you can e.g pipe it to fs.createWriteStream()
194
199
  */
195
- getFileReadStream(bucketName: string, filePath: string): Readable {
200
+ getFileReadStream(bucketName: string, filePath: string): ReadableBinary {
196
201
  return this.storage.bucket(bucketName).file(filePath).createReadStream()
197
202
  }
198
203
 
@@ -200,7 +205,7 @@ export class CloudStorage implements CommonStorage {
200
205
  await this.storage.bucket(bucketName).file(filePath).save(content)
201
206
  }
202
207
 
203
- getFileWriteStream(bucketName: string, filePath: string): Writable {
208
+ getFileWriteStream(bucketName: string, filePath: string): WritableBinary {
204
209
  return this.storage.bucket(bucketName).file(filePath).createWriteStream()
205
210
  }
206
211
 
@@ -286,6 +291,12 @@ export class CloudStorage implements CommonStorage {
286
291
  `combineFiles reached max recursion depth of ${MAX_RECURSION_DEPTH}`,
287
292
  )
288
293
  const { logger, debug } = this.cfg
294
+ if (filePaths.length === 0) {
295
+ if (debug) {
296
+ logger.log(`[${currentRecursionDepth}] Nothing to compose, returning early!`)
297
+ }
298
+ return
299
+ }
289
300
 
290
301
  if (debug) {
291
302
  logger.log(
@@ -1,6 +1,5 @@
1
- import { Readable, Writable } from 'node:stream'
2
- import { LocalTimeInput } from '@naturalcycles/js-lib'
3
- import { ReadableTyped } from '@naturalcycles/nodejs-lib'
1
+ import type { LocalTimeInput } from '@naturalcycles/js-lib'
2
+ import type { ReadableBinary, ReadableTyped, WritableBinary } from '@naturalcycles/nodejs-lib'
4
3
 
5
4
  export interface FileEntry {
6
5
  filePath: string
@@ -91,9 +90,9 @@ export interface CommonStorage {
91
90
 
92
91
  getFilesStream: (bucketName: string, opt?: CommonStorageGetOptions) => ReadableTyped<FileEntry>
93
92
 
94
- getFileReadStream: (bucketName: string, filePath: string) => Readable
93
+ getFileReadStream: (bucketName: string, filePath: string) => ReadableBinary
95
94
 
96
- getFileWriteStream: (bucketName: string, filePath: string) => Writable
95
+ getFileWriteStream: (bucketName: string, filePath: string) => WritableBinary
97
96
 
98
97
  /**
99
98
  * Upload local file to the bucket (by streaming it).
@@ -1,4 +1,3 @@
1
- import { Readable, Writable } from 'node:stream'
2
1
  import {
3
2
  _assert,
4
3
  _isTruthy,
@@ -8,7 +7,14 @@ import {
8
7
  LocalTimeInput,
9
8
  StringMap,
10
9
  } from '@naturalcycles/js-lib'
11
- import { fs2, md5, ReadableTyped } from '@naturalcycles/nodejs-lib'
10
+ import {
11
+ fs2,
12
+ md5,
13
+ ReadableBinary,
14
+ readableFrom,
15
+ ReadableTyped,
16
+ WritableBinary,
17
+ } from '@naturalcycles/nodejs-lib'
12
18
  import { CommonStorage, CommonStorageGetOptions, FileEntry } from './commonStorage'
13
19
 
14
20
  export class InMemoryCommonStorage implements CommonStorage {
@@ -26,7 +32,7 @@ export class InMemoryCommonStorage implements CommonStorage {
26
32
  }
27
33
 
28
34
  getBucketNamesStream(): ReadableTyped<string> {
29
- return Readable.from(Object.keys(this.data))
35
+ return readableFrom(Object.keys(this.data))
30
36
  }
31
37
 
32
38
  async fileExists(bucketName: string, filePath: string): Promise<boolean> {
@@ -69,7 +75,7 @@ export class InMemoryCommonStorage implements CommonStorage {
69
75
  getFileNamesStream(bucketName: string, opt: CommonStorageGetOptions = {}): ReadableTyped<string> {
70
76
  const { prefix = '', fullPaths = true } = opt
71
77
 
72
- return Readable.from(
78
+ return readableFrom(
73
79
  Object.keys(this.data[bucketName] || {})
74
80
  .filter(filePath => filePath.startsWith(prefix))
75
81
  .slice(0, opt.limit)
@@ -80,8 +86,8 @@ export class InMemoryCommonStorage implements CommonStorage {
80
86
  getFilesStream(bucketName: string, opt: CommonStorageGetOptions = {}): ReadableTyped<FileEntry> {
81
87
  const { prefix = '', fullPaths = true } = opt
82
88
 
83
- return Readable.from(
84
- Object.entries(this.data[bucketName] || {})
89
+ return readableFrom(
90
+ _stringMapEntries(this.data[bucketName] || {})
85
91
  .map(([filePath, content]) => ({
86
92
  filePath,
87
93
  content,
@@ -92,11 +98,11 @@ export class InMemoryCommonStorage implements CommonStorage {
92
98
  )
93
99
  }
94
100
 
95
- getFileReadStream(bucketName: string, filePath: string): Readable {
96
- return Readable.from(this.data[bucketName]![filePath]!)
101
+ getFileReadStream(bucketName: string, filePath: string): ReadableBinary {
102
+ return readableFrom(this.data[bucketName]![filePath]!)
97
103
  }
98
104
 
99
- getFileWriteStream(_bucketName: string, _filePath: string): Writable {
105
+ getFileWriteStream(_bucketName: string, _filePath: string): WritableBinary {
100
106
  throw new Error('Method not implemented.')
101
107
  }
102
108