@naturalcycles/cloud-storage-lib 1.4.0 → 1.5.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.
@@ -1,6 +1,7 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { Readable, Writable } from 'stream';
3
- import { Storage } from '@google-cloud/storage';
4
+ import { Storage, StorageOptions } from '@google-cloud/storage';
4
5
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
5
6
  import { CommonStorage, CommonStorageGetOptions, FileEntry } from './commonStorage';
6
7
  import { GCPServiceAccount } from './model';
@@ -14,7 +15,10 @@ import { GCPServiceAccount } from './model';
14
15
  * (either personal one or non-personal).
15
16
  */
16
17
  export interface CloudStorageCfg {
17
- credentials: GCPServiceAccount;
18
+ /**
19
+ * It's optional, to allow automatic credentials in AppEngine, or GOOGLE_APPLICATION_CREDENTIALS.
20
+ */
21
+ credentials?: GCPServiceAccount;
18
22
  }
19
23
  export declare class CloudStorage implements CommonStorage {
20
24
  storage: Storage;
@@ -24,6 +28,7 @@ export declare class CloudStorage implements CommonStorage {
24
28
  */
25
29
  constructor(storage: Storage);
26
30
  static createFromGCPServiceAccount(cfg: CloudStorageCfg): CloudStorage;
31
+ static createFromStorageOptions(storageOptions?: StorageOptions): CloudStorage;
27
32
  ping(bucketName?: string): Promise<void>;
28
33
  deletePath(bucketName: string, prefix: string): Promise<void>;
29
34
  fileExists(bucketName: string, filePath: string): Promise<boolean>;
@@ -20,10 +20,14 @@ class CloudStorage {
20
20
  // To learn more about authentication and Google APIs, visit:
21
21
  // https://cloud.google.com/docs/authentication/getting-started
22
22
  // at /root/repo/node_modules/google-auth-library/build/src/auth/googleauth.js:95:31
23
- projectId: cfg.credentials.project_id,
23
+ projectId: cfg.credentials?.project_id,
24
24
  });
25
25
  return new CloudStorage(storage);
26
26
  }
27
+ static createFromStorageOptions(storageOptions) {
28
+ const storage = new storage_1.Storage(storageOptions);
29
+ return new CloudStorage(storage);
30
+ }
27
31
  async ping(bucketName) {
28
32
  await this.storage.bucket(bucketName || 'non-existing-for-sure').exists();
29
33
  }
@@ -59,7 +63,7 @@ class CloudStorage {
59
63
  .pipe((0, nodejs_lib_1.transformMapSimple)(f => fullPaths ? f.name : (0, js_lib_1._substringAfterLast)(f.name, '/')));
60
64
  }
61
65
  getFilesStream(bucketName, opt = {}) {
62
- const { prefix } = opt;
66
+ const { prefix, fullPaths = true } = opt;
63
67
  return this.storage
64
68
  .bucket(bucketName)
65
69
  .getFilesStream({
@@ -68,7 +72,7 @@ class CloudStorage {
68
72
  })
69
73
  .pipe((0, nodejs_lib_1.transformMap)(async (f) => {
70
74
  const [content] = await f.download();
71
- return { filePath: f.name, content };
75
+ return { filePath: fullPaths ? f.name : (0, js_lib_1._substringAfterLast)(f.name, '/'), content };
72
76
  }));
73
77
  }
74
78
  async getFile(bucketName, filePath) {
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { Readable, Writable } from 'stream';
3
4
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
4
5
  export interface FileEntry {
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { Readable, Writable } from 'stream';
3
4
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
4
5
  import { CommonStorage, CommonStorageGetOptions, FileEntry } from './commonStorage';
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { Readable, Writable } from 'stream';
3
4
  import { StringMap } from '@naturalcycles/js-lib';
4
5
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
package/package.json CHANGED
@@ -4,16 +4,16 @@
4
4
  "prepare": "husky install"
5
5
  },
6
6
  "dependencies": {
7
- "@google-cloud/storage": "^5.14.0",
7
+ "@google-cloud/storage": "^6.0.0",
8
8
  "@naturalcycles/db-lib": "^8.25.0",
9
9
  "@naturalcycles/js-lib": "^14.41.0",
10
10
  "@naturalcycles/nodejs-lib": "^12.31.0"
11
11
  },
12
12
  "devDependencies": {
13
13
  "@naturalcycles/dev-lib": "^12.1.3",
14
- "@types/node": "^17.0.5",
14
+ "@types/node": "^17.0.8",
15
15
  "firebase-admin": "^10.0.1",
16
- "jest": "^27.1.0"
16
+ "jest": "^28.1.0"
17
17
  },
18
18
  "files": [
19
19
  "dist",
@@ -35,7 +35,7 @@
35
35
  "engines": {
36
36
  "node": ">=14.16.0"
37
37
  },
38
- "version": "1.4.0",
38
+ "version": "1.5.1",
39
39
  "description": "",
40
40
  "author": "Natural Cycles Team",
41
41
  "license": "MIT"
@@ -1,7 +1,6 @@
1
- import * as Buffer from 'buffer'
2
1
  import { Readable, Writable } from 'stream'
3
2
  import { _substringAfterLast } from '@naturalcycles/js-lib'
4
- import { File, Storage } from '@google-cloud/storage'
3
+ import { File, Storage, StorageOptions } from '@google-cloud/storage'
5
4
  import { ReadableTyped, transformMap, transformMapSimple } from '@naturalcycles/nodejs-lib'
6
5
  import { CommonStorage, CommonStorageGetOptions, FileEntry } from './commonStorage'
7
6
  import { GCPServiceAccount } from './model'
@@ -16,7 +15,10 @@ import { GCPServiceAccount } from './model'
16
15
  * (either personal one or non-personal).
17
16
  */
18
17
  export interface CloudStorageCfg {
19
- credentials: GCPServiceAccount
18
+ /**
19
+ * It's optional, to allow automatic credentials in AppEngine, or GOOGLE_APPLICATION_CREDENTIALS.
20
+ */
21
+ credentials?: GCPServiceAccount
20
22
  }
21
23
 
22
24
  export class CloudStorage implements CommonStorage {
@@ -34,12 +36,17 @@ export class CloudStorage implements CommonStorage {
34
36
  // To learn more about authentication and Google APIs, visit:
35
37
  // https://cloud.google.com/docs/authentication/getting-started
36
38
  // at /root/repo/node_modules/google-auth-library/build/src/auth/googleauth.js:95:31
37
- projectId: cfg.credentials.project_id,
39
+ projectId: cfg.credentials?.project_id,
38
40
  })
39
41
 
40
42
  return new CloudStorage(storage)
41
43
  }
42
44
 
45
+ static createFromStorageOptions(storageOptions?: StorageOptions): CloudStorage {
46
+ const storage = new Storage(storageOptions)
47
+ return new CloudStorage(storage)
48
+ }
49
+
43
50
  async ping(bucketName?: string): Promise<void> {
44
51
  await this.storage.bucket(bucketName || 'non-existing-for-sure').exists()
45
52
  }
@@ -87,7 +94,7 @@ export class CloudStorage implements CommonStorage {
87
94
  }
88
95
 
89
96
  getFilesStream(bucketName: string, opt: CommonStorageGetOptions = {}): ReadableTyped<FileEntry> {
90
- const { prefix } = opt
97
+ const { prefix, fullPaths = true } = opt
91
98
 
92
99
  return this.storage
93
100
  .bucket(bucketName)
@@ -98,7 +105,7 @@ export class CloudStorage implements CommonStorage {
98
105
  .pipe(
99
106
  transformMap<File, FileEntry>(async f => {
100
107
  const [content] = await f.download()
101
- return { filePath: f.name, content }
108
+ return { filePath: fullPaths ? f.name : _substringAfterLast(f.name, '/'), content }
102
109
  }),
103
110
  )
104
111
  }
@@ -1,4 +1,3 @@
1
- import * as Buffer from 'buffer'
2
1
  import { CommonDBCreateOptions, CommonKeyValueDB, KeyValueDBTuple } from '@naturalcycles/db-lib'
3
2
  import { pMap, StringMap } from '@naturalcycles/js-lib'
4
3
  import { ReadableTyped, transformMapSimple } from '@naturalcycles/nodejs-lib'