@engineers/gcloud-storage 0.0.0-temp → 0.0.8-temp
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/index.d.ts +48 -0
- package/{index.mjs → index.js} +21 -17
- package/index.spec.d.ts +1 -0
- package/package.json +3 -23
package/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { Bucket, CreateBucketRequest, File, SaveOptions, Storage, UploadResponse, StorageOptions as _StorageOptions } from '@google-cloud/storage';
|
|
4
|
+
import { PathLike } from 'node:fs';
|
|
5
|
+
export interface StorageOptions extends _StorageOptions {
|
|
6
|
+
bucket: string;
|
|
7
|
+
rootDir?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface UploadOptions {
|
|
10
|
+
destination: string;
|
|
11
|
+
}
|
|
12
|
+
export interface DownloadOptions {
|
|
13
|
+
destination?: string;
|
|
14
|
+
encoding?: BufferEncoding | null;
|
|
15
|
+
}
|
|
16
|
+
export interface DeleteOptions {
|
|
17
|
+
ignoreNotFound?: boolean;
|
|
18
|
+
}
|
|
19
|
+
export interface ExistsOptions {
|
|
20
|
+
}
|
|
21
|
+
export interface CreateBucketMeta extends CreateBucketRequest {
|
|
22
|
+
labels?: {
|
|
23
|
+
[key: string]: string;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export default class {
|
|
27
|
+
storage: Storage;
|
|
28
|
+
bucket: Bucket;
|
|
29
|
+
private baseDir;
|
|
30
|
+
constructor(options: StorageOptions);
|
|
31
|
+
upload(path: PathLike, options?: UploadOptions | string): Promise<UploadResponse | void>;
|
|
32
|
+
download(file: PathLike | File, options?: DownloadOptions | string): Promise<Buffer | string | Array<any> | {
|
|
33
|
+
[key: string]: any;
|
|
34
|
+
} | void>;
|
|
35
|
+
downloadAll(destination: PathLike, directory?: PathLike, filter?: RegExp | ((file: string) => boolean), options?: {
|
|
36
|
+
start?: number;
|
|
37
|
+
end?: number;
|
|
38
|
+
}): Promise<{
|
|
39
|
+
[key: string]: boolean;
|
|
40
|
+
}>;
|
|
41
|
+
write(path: PathLike, content?: any, options?: SaveOptions): Promise<any>;
|
|
42
|
+
read(file: PathLike | File, options?: DownloadOptions | string): Promise<string | void | any[] | Buffer | {
|
|
43
|
+
[key: string]: any;
|
|
44
|
+
}>;
|
|
45
|
+
delete(path: PathLike, options?: DeleteOptions): Promise<any>;
|
|
46
|
+
exists(path: PathLike, options?: ExistsOptions): Promise<boolean>;
|
|
47
|
+
create(metaData?: CreateBucketMeta): Promise<import("@google-cloud/storage").CreateBucketResponse>;
|
|
48
|
+
}
|
package/{index.mjs → index.js}
RENAMED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
Storage
|
|
4
4
|
} from "@google-cloud/storage";
|
|
5
5
|
import { mkdirSync } from "node:fs";
|
|
6
|
-
import { dirname } from "node:path";
|
|
6
|
+
import { dirname, resolve } from "node:path";
|
|
7
7
|
import stripJsonComments from "strip-json-comments";
|
|
8
8
|
import { URL } from "node:url";
|
|
9
9
|
|
|
@@ -107,7 +107,7 @@ var plainLink = toRegExp(
|
|
|
107
107
|
var src_default = class {
|
|
108
108
|
storage;
|
|
109
109
|
bucket;
|
|
110
|
-
|
|
110
|
+
baseDir;
|
|
111
111
|
/**
|
|
112
112
|
* creates a new bucket
|
|
113
113
|
*
|
|
@@ -126,7 +126,7 @@ var src_default = class {
|
|
|
126
126
|
if (opts.bucket.includes("/")) {
|
|
127
127
|
let parts = opts.bucket.split("/");
|
|
128
128
|
opts.bucket = parts.shift();
|
|
129
|
-
this.
|
|
129
|
+
this.baseDir = parts.join("/") + opts.rootDir ? "/" + opts.rootDir : "";
|
|
130
130
|
}
|
|
131
131
|
this.bucket = this.storage.bucket(opts.bucket);
|
|
132
132
|
}
|
|
@@ -144,8 +144,8 @@ var src_default = class {
|
|
|
144
144
|
let opts = {
|
|
145
145
|
...typeof options === "string" ? { destination: options } : options || {}
|
|
146
146
|
};
|
|
147
|
-
if (this.
|
|
148
|
-
opts.destination = `${this.
|
|
147
|
+
if (this.baseDir && opts?.destination) {
|
|
148
|
+
opts.destination = `${this.baseDir}/${opts.destination}`;
|
|
149
149
|
}
|
|
150
150
|
return this.bucket.upload(path.toString(), opts);
|
|
151
151
|
}
|
|
@@ -166,7 +166,7 @@ var src_default = class {
|
|
|
166
166
|
download(file, options) {
|
|
167
167
|
if (typeof file === "string" || file instanceof Buffer || file instanceof URL) {
|
|
168
168
|
file = this.bucket.file(
|
|
169
|
-
this.
|
|
169
|
+
this.baseDir ? `${this.baseDir}/${file.toString()}` : file.toString()
|
|
170
170
|
);
|
|
171
171
|
}
|
|
172
172
|
let opts = Object.assign(
|
|
@@ -211,14 +211,14 @@ var src_default = class {
|
|
|
211
211
|
} else if (filter instanceof RegExp) {
|
|
212
212
|
filter = (file) => filter.test(file);
|
|
213
213
|
}
|
|
214
|
-
if (this.
|
|
215
|
-
directory = `${this.
|
|
214
|
+
if (this.baseDir) {
|
|
215
|
+
directory = `${this.baseDir}/${directory}`;
|
|
216
216
|
}
|
|
217
217
|
return this.bucket.getFiles({ prefix: directory?.toString() }).then((files) => files[0].filter((file) => filter(file.name))).then(
|
|
218
218
|
(files) => Promise.all(
|
|
219
219
|
files.slice(options.start, options.end).map(
|
|
220
220
|
(file) => this.download(file, {
|
|
221
|
-
destination: `${destination.toString()}/${file.name}`
|
|
221
|
+
destination: resolve(`${destination.toString()}/${file.name}`)
|
|
222
222
|
}).then((result) => {
|
|
223
223
|
results[file.name] = true;
|
|
224
224
|
}).catch((error) => {
|
|
@@ -237,19 +237,23 @@ var src_default = class {
|
|
|
237
237
|
* @returns the original content
|
|
238
238
|
*/
|
|
239
239
|
write(path, content = "", options) {
|
|
240
|
-
if (this.
|
|
241
|
-
path = `${this.
|
|
240
|
+
if (this.baseDir) {
|
|
241
|
+
path = `${this.baseDir}/${path}`;
|
|
242
242
|
}
|
|
243
243
|
return this.bucket.file(path.toString()).save(
|
|
244
244
|
["array", "object"].includes(objectType(content)) ? JSON.stringify(content) : content,
|
|
245
245
|
options
|
|
246
246
|
).then(() => content);
|
|
247
247
|
}
|
|
248
|
+
/** an alias to download() to be compatible with node:fs functions */
|
|
249
|
+
read(file, options) {
|
|
250
|
+
return this.download(file, options);
|
|
251
|
+
}
|
|
248
252
|
// eslint-disable-next-line no-secrets/no-secrets
|
|
249
253
|
// https://stackoverflow.com/a/64539948/12577650
|
|
250
254
|
delete(path, options) {
|
|
251
|
-
if (this.
|
|
252
|
-
path = `${this.
|
|
255
|
+
if (this.baseDir) {
|
|
256
|
+
path = `${this.baseDir}/${path}`;
|
|
253
257
|
}
|
|
254
258
|
return this.bucket.file(path.toString()).delete(options);
|
|
255
259
|
}
|
|
@@ -264,8 +268,8 @@ var src_default = class {
|
|
|
264
268
|
* @returns boolean
|
|
265
269
|
*/
|
|
266
270
|
exists(path, options) {
|
|
267
|
-
if (this.
|
|
268
|
-
path = `/${this.
|
|
271
|
+
if (this.baseDir) {
|
|
272
|
+
path = `/${this.baseDir}/${path}`;
|
|
269
273
|
}
|
|
270
274
|
return this.bucket.file(path.toString()).exists(options).then((result) => result[0]);
|
|
271
275
|
}
|
|
@@ -274,11 +278,11 @@ var src_default = class {
|
|
|
274
278
|
* add the desired labels as key-value pairs to metaData.labels
|
|
275
279
|
* https://cloud.google.com/storage/docs/using-bucket-labels#storage-modify-bucket-label-nodejs
|
|
276
280
|
*
|
|
277
|
-
* @param bucketName
|
|
278
281
|
* @param metaData
|
|
279
282
|
* @returns
|
|
280
283
|
*/
|
|
281
|
-
create(
|
|
284
|
+
create(metaData) {
|
|
285
|
+
let bucketName = this.bucket.name;
|
|
282
286
|
let { labels, ...meta } = metaData || {};
|
|
283
287
|
return this.storage.createBucket(bucketName, meta).then((result) => {
|
|
284
288
|
this.bucket.name = bucketName;
|
package/index.spec.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,32 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@engineers/gcloud-storage",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8-temp",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"private": false,
|
|
6
|
-
"repository": {
|
|
7
|
-
"type": "git",
|
|
8
|
-
"url": "https://github.com/its-dibo/dibo.git"
|
|
9
|
-
},
|
|
10
|
-
"homepage": "https://github.com/its-dibo/dibo#readme",
|
|
11
|
-
"bugs": {
|
|
12
|
-
"url": "https://github.com/its-dibo/dibo/issues",
|
|
13
|
-
"email": "sh.eldeeb.2010+github@gmail.com"
|
|
14
|
-
},
|
|
15
|
-
"license": "MIT",
|
|
16
|
-
"author": "Sherif Eldeeb <sh.eldeeb.2010+github@gmail.com> (https://github.com/its-dibo)",
|
|
17
|
-
"funding": [
|
|
18
|
-
{
|
|
19
|
-
"type": "paypal",
|
|
20
|
-
"url": "https://paypal.me/group99001"
|
|
21
|
-
},
|
|
22
|
-
{
|
|
23
|
-
"type": "patreon",
|
|
24
|
-
"url": "https://www.patreon.com/GoogleDev"
|
|
25
|
-
}
|
|
26
|
-
],
|
|
27
6
|
"description": "google cloud storage",
|
|
28
7
|
"dependencies": {
|
|
29
|
-
"@google-cloud/storage": "^6.10.1"
|
|
8
|
+
"@google-cloud/storage": "^6.10.1",
|
|
9
|
+
"strip-json-comments": "^5.0.1"
|
|
30
10
|
},
|
|
31
11
|
"imports": {
|
|
32
12
|
"#*": "./*"
|