@naturalcycles/cloud-storage-lib 1.1.0 → 1.2.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.
@@ -50,8 +50,12 @@ export declare class CommonStorageBucket {
50
50
  * Important difference between `prefix` and `path` is that `prefix` will
51
51
  * return all files from sub-directories too!
52
52
  */
53
- getFileNames(prefix: string): Promise<string[]>;
54
- getFileNamesStream(prefix: string, opt?: CommonStorageGetOptions): ReadableTyped<string>;
53
+ getFileNames(prefix: string, opt?: {
54
+ fullPath?: boolean;
55
+ }): Promise<string[]>;
56
+ getFileNamesStream(prefix: string, opt?: CommonStorageGetOptions & {
57
+ fullPath?: boolean;
58
+ }): ReadableTyped<string>;
55
59
  getFilesStream(prefix: string, opt?: CommonStorageGetOptions): ReadableTyped<FileEntry>;
56
60
  getFileReadStream(filePath: string): Readable;
57
61
  getFileWriteStream(filePath: string): Writable;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CommonStorageBucket = void 0;
4
4
  const js_lib_1 = require("@naturalcycles/js-lib");
5
+ const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
5
6
  /**
6
7
  * Convenience wrapper around CommonStorage for a given Bucket.
7
8
  *
@@ -106,11 +107,24 @@ class CommonStorageBucket {
106
107
  * Important difference between `prefix` and `path` is that `prefix` will
107
108
  * return all files from sub-directories too!
108
109
  */
109
- async getFileNames(prefix) {
110
- return await this.cfg.storage.getFileNames(this.cfg.bucketName, prefix);
111
- }
112
- getFileNamesStream(prefix, opt) {
113
- return this.cfg.storage.getFileNamesStream(this.cfg.bucketName, prefix, opt);
110
+ async getFileNames(prefix, opt = {}) {
111
+ const { fullPath = true } = opt;
112
+ const names = await this.cfg.storage.getFileNames(this.cfg.bucketName, prefix);
113
+ if (!fullPath && prefix) {
114
+ const start = `${prefix}/`.length;
115
+ return names.map(n => n.slice(start));
116
+ }
117
+ return names;
118
+ }
119
+ getFileNamesStream(prefix, opt = {}) {
120
+ const { fullPath = true } = opt;
121
+ if (fullPath || !prefix) {
122
+ return this.cfg.storage.getFileNamesStream(this.cfg.bucketName, prefix, opt);
123
+ }
124
+ const start = `${prefix}/`.length;
125
+ return this.cfg.storage
126
+ .getFileNamesStream(this.cfg.bucketName, prefix, opt)
127
+ .pipe((0, nodejs_lib_1.transformMapSimple)(f => f.slice(start)));
114
128
  }
115
129
  getFilesStream(prefix, opt) {
116
130
  return this.cfg.storage.getFilesStream(this.cfg.bucketName, prefix, opt);
package/package.json CHANGED
@@ -34,7 +34,7 @@
34
34
  "engines": {
35
35
  "node": ">=14.16.0"
36
36
  },
37
- "version": "1.1.0",
37
+ "version": "1.2.0",
38
38
  "description": "",
39
39
  "author": "Natural Cycles Team",
40
40
  "license": "MIT"
@@ -1,6 +1,6 @@
1
1
  import { Readable, Writable } from 'stream'
2
2
  import { AppError, pMap } from '@naturalcycles/js-lib'
3
- import { ReadableTyped } from '@naturalcycles/nodejs-lib'
3
+ import { ReadableTyped, transformMapSimple } from '@naturalcycles/nodejs-lib'
4
4
  import { CommonStorage, CommonStorageGetOptions, FileEntry } from './commonStorage'
5
5
 
6
6
  export interface CommonStorageBucketCfg {
@@ -145,12 +145,32 @@ export class CommonStorageBucket {
145
145
  * Important difference between `prefix` and `path` is that `prefix` will
146
146
  * return all files from sub-directories too!
147
147
  */
148
- async getFileNames(prefix: string): Promise<string[]> {
149
- return await this.cfg.storage.getFileNames(this.cfg.bucketName, prefix)
148
+ async getFileNames(prefix: string, opt: { fullPath?: boolean } = {}): Promise<string[]> {
149
+ const { fullPath = true } = opt
150
+ const names = await this.cfg.storage.getFileNames(this.cfg.bucketName, prefix)
151
+
152
+ if (!fullPath && prefix) {
153
+ const start = `${prefix}/`.length
154
+ return names.map(n => n.slice(start))
155
+ }
156
+
157
+ return names
150
158
  }
151
159
 
152
- getFileNamesStream(prefix: string, opt?: CommonStorageGetOptions): ReadableTyped<string> {
153
- return this.cfg.storage.getFileNamesStream(this.cfg.bucketName, prefix, opt)
160
+ getFileNamesStream(
161
+ prefix: string,
162
+ opt: CommonStorageGetOptions & { fullPath?: boolean } = {},
163
+ ): ReadableTyped<string> {
164
+ const { fullPath = true } = opt
165
+
166
+ if (fullPath || !prefix) {
167
+ return this.cfg.storage.getFileNamesStream(this.cfg.bucketName, prefix, opt)
168
+ }
169
+
170
+ const start = `${prefix}/`.length
171
+ return this.cfg.storage
172
+ .getFileNamesStream(this.cfg.bucketName, prefix, opt)
173
+ .pipe(transformMapSimple<string, string>(f => f.slice(start)))
154
174
  }
155
175
 
156
176
  getFilesStream(prefix: string, opt?: CommonStorageGetOptions): ReadableTyped<FileEntry> {