@pi-r/gcp 0.5.0 → 0.5.3
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/LICENSE +1 -1
- package/README.md +4 -2
- package/client/index.js +34 -34
- package/download/index.js +5 -6
- package/package.json +7 -7
- package/types/index.d.ts +36 -0
- package/upload/index.js +7 -8
package/LICENSE
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright 2023
|
|
1
|
+
Copyright 2023 Haruhi Suzumiya
|
|
2
2
|
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
4
|
|
package/README.md
CHANGED
package/client/index.js
CHANGED
|
@@ -194,7 +194,7 @@ async function createBucketV2(credential, bucket, acl, options, service = 'gcp')
|
|
|
194
194
|
}
|
|
195
195
|
return instance.createBucket(bucket, options)
|
|
196
196
|
.then(async ([target]) => {
|
|
197
|
-
this.formatMessage(64
|
|
197
|
+
this.formatMessage(64, service, ["Bucket created", bucket], '', { ...Cloud.LOG_CLOUD_COMMAND });
|
|
198
198
|
if (acl) {
|
|
199
199
|
await setBucketPolicy.call(this, credential, bucket, { acl, bucket: target }, service);
|
|
200
200
|
}
|
|
@@ -204,7 +204,7 @@ async function createBucketV2(credential, bucket, acl, options, service = 'gcp')
|
|
|
204
204
|
if (isErrorConflict(err)) {
|
|
205
205
|
return acl ? await setBucketPolicy.call(this, credential, bucket, { acl }, service) : true;
|
|
206
206
|
}
|
|
207
|
-
this.formatFail(64
|
|
207
|
+
this.formatFail(64, service, ["Unable to create bucket", bucket], err, { ...Cloud.LOG_CLOUD_FAIL });
|
|
208
208
|
return false;
|
|
209
209
|
});
|
|
210
210
|
}
|
|
@@ -215,7 +215,7 @@ async function setBucketPolicy(credential, bucket, options, service = 'gcp') {
|
|
|
215
215
|
}
|
|
216
216
|
const client = createStorageClient.call(this, credential, bucket).bucket(bucket);
|
|
217
217
|
if (!await hasBucket(client)) {
|
|
218
|
-
this.formatFail(64
|
|
218
|
+
this.formatFail(64, service, ["Bucket does not exist", bucket], (0, types_1.errorMessage)(service, "Bucket does not exist"), { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
|
|
219
219
|
return false;
|
|
220
220
|
}
|
|
221
221
|
const errors = [];
|
|
@@ -256,10 +256,10 @@ async function setBucketPolicy(credential, bucket, options, service = 'gcp') {
|
|
|
256
256
|
errors.push(...await updatePredefinedAcl(client.acl, options.acl, options.metadata));
|
|
257
257
|
}
|
|
258
258
|
if (errors.length) {
|
|
259
|
-
this.formatFail(64
|
|
259
|
+
this.formatFail(64, service, ["Unable to update bucket policy", bucket], errors[0], { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
|
|
260
260
|
return false;
|
|
261
261
|
}
|
|
262
|
-
this.formatMessage(64
|
|
262
|
+
this.formatMessage(64, service, ["Bucket ACL configured", bucket], (0, types_1.isString)(acl) ? acl : '', { ...Cloud.LOG_CLOUD_COMMAND });
|
|
263
263
|
return true;
|
|
264
264
|
}
|
|
265
265
|
exports.setBucketPolicy = setBucketPolicy;
|
|
@@ -277,11 +277,11 @@ function setBucketWebsite(credential, bucket, options, service = 'gcp') {
|
|
|
277
277
|
}
|
|
278
278
|
return createStorageClient.call(this, credential, bucket).bucket(bucket).setMetadata({ website })
|
|
279
279
|
.then(() => {
|
|
280
|
-
this.formatMessage(64
|
|
280
|
+
this.formatMessage(64, service, ["Bucket configured", bucket], website, { ...Cloud.LOG_CLOUD_COMMAND });
|
|
281
281
|
return true;
|
|
282
282
|
})
|
|
283
283
|
.catch(err => {
|
|
284
|
-
this.formatFail(64
|
|
284
|
+
this.formatFail(64, service, ["Unable to configure bucket", bucket], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
|
|
285
285
|
return false;
|
|
286
286
|
});
|
|
287
287
|
}
|
|
@@ -321,14 +321,14 @@ async function deleteObjectsV2(credential, bucket, recursive = true, service = '
|
|
|
321
321
|
const client = instance.bucket(bucket);
|
|
322
322
|
if (await hasBucket(client)) {
|
|
323
323
|
if (recursive) {
|
|
324
|
-
return client.deleteFiles({ force: true }).then(() => this.formatMessage(64
|
|
324
|
+
return client.deleteFiles({ force: true }).then(() => this.formatMessage(64, service, "Bucket emptied" + ' (recursive)', bucket, { ...Cloud.LOG_CLOUD_COMMAND }));
|
|
325
325
|
}
|
|
326
326
|
const tasks = [];
|
|
327
327
|
try {
|
|
328
328
|
const result = await client.getFiles();
|
|
329
329
|
for (const { name } of result[0]) {
|
|
330
330
|
if (name.indexOf('/') === -1) {
|
|
331
|
-
tasks.push(client.file(name).delete({ ignoreNotFound: true }).catch(err => this.formatFail(64
|
|
331
|
+
tasks.push(client.file(name).delete({ ignoreNotFound: true }).catch(err => this.formatFail(64, service, ["Delete failed", name], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: false })));
|
|
332
332
|
}
|
|
333
333
|
}
|
|
334
334
|
}
|
|
@@ -342,7 +342,7 @@ async function deleteObjectsV2(credential, bucket, recursive = true, service = '
|
|
|
342
342
|
}
|
|
343
343
|
const checkErrors = () => {
|
|
344
344
|
if (errors.length) {
|
|
345
|
-
this.formatFail(64
|
|
345
|
+
this.formatFail(64, service, ["Unable to empty bucket" + (recursive ? ' (recursive)' : ''), errors.length + ' errors'], errors[0], { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
|
|
346
346
|
return true;
|
|
347
347
|
}
|
|
348
348
|
return false;
|
|
@@ -352,7 +352,7 @@ async function deleteObjectsV2(credential, bucket, recursive = true, service = '
|
|
|
352
352
|
errors.push(...result.filter(item => item.status === 'rejected').map((item) => item.reason));
|
|
353
353
|
if (!checkErrors()) {
|
|
354
354
|
const files = result.length + ' files';
|
|
355
|
-
this.formatMessage(64
|
|
355
|
+
this.formatMessage(64, service, ["Bucket emptied" + ` (${recursive ? 'recursive' : files})`, bucket], recursive ? files : '', { ...Cloud.LOG_CLOUD_COMMAND });
|
|
356
356
|
}
|
|
357
357
|
})
|
|
358
358
|
.catch(err => {
|
|
@@ -383,7 +383,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
383
383
|
return (clients[type] = createDatabaseClient.call(this, item, data));
|
|
384
384
|
};
|
|
385
385
|
const closeClient = () => {
|
|
386
|
-
const fs = clients[3
|
|
386
|
+
const fs = clients[3];
|
|
387
387
|
fs?.terminate();
|
|
388
388
|
};
|
|
389
389
|
const getFamily = (data, row, family) => {
|
|
@@ -430,7 +430,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
430
430
|
result[i] = rows;
|
|
431
431
|
continue;
|
|
432
432
|
}
|
|
433
|
-
const db = createClient(item, 4
|
|
433
|
+
const db = createClient(item, 4).instance(name).table(table);
|
|
434
434
|
if (id) {
|
|
435
435
|
const row = db.row(id);
|
|
436
436
|
if (Array.isArray(columns)) {
|
|
@@ -456,21 +456,21 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
456
456
|
const columns = (0, types_1.isPlainObject)(query) && query.columns || item.columns;
|
|
457
457
|
if (!(query || table && columns)) {
|
|
458
458
|
closeClient();
|
|
459
|
-
throw (0, util_1.formatError)(item, "Missing database query"
|
|
459
|
+
throw (0, util_1.formatError)(item, "Missing database query");
|
|
460
460
|
}
|
|
461
461
|
if (!name || !database) {
|
|
462
462
|
closeClient();
|
|
463
|
-
throw (0, util_1.formatError)(item, !name ? "Missing instance name"
|
|
463
|
+
throw (0, util_1.formatError)(item, !name ? "Missing instance name" : "Missing database name");
|
|
464
464
|
}
|
|
465
465
|
const { updateType, options = {} } = item;
|
|
466
466
|
if (cached) {
|
|
467
|
-
queryString = name + '_' + database + '_' + (table ? table + Module.asString(columns, true) : '') + Module.asString(query, true) + limit;
|
|
467
|
+
queryString = name + '_' + database + '_' + (table ? table + (columns ? Module.asString(columns, true) : '') : '') + Module.asString(query, true) + limit;
|
|
468
468
|
if (!update && (rows = getCache(queryString))) {
|
|
469
469
|
result[i] = rows;
|
|
470
470
|
continue;
|
|
471
471
|
}
|
|
472
472
|
}
|
|
473
|
-
const db = createClient(item, 5
|
|
473
|
+
const db = createClient(item, 5).instance(name).database(database, options.databasePool, options.databaseQuery);
|
|
474
474
|
if (table) {
|
|
475
475
|
if ((0, types_1.isPlainObject)(update)) {
|
|
476
476
|
await db.table(table)[updateType === 1 ? 'insert' : updateType === 2 ? 'replace' : 'update'](update, options.tableUpdate);
|
|
@@ -489,7 +489,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
489
489
|
await new Promise((resolve, reject) => {
|
|
490
490
|
db.runTransaction((err, transaction) => {
|
|
491
491
|
if (err || !transaction) {
|
|
492
|
-
reject(err || new Error("Unknown"
|
|
492
|
+
reject(err || new Error("Unknown"));
|
|
493
493
|
}
|
|
494
494
|
else {
|
|
495
495
|
transaction.runUpdate(update)
|
|
@@ -510,7 +510,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
510
510
|
else if (product === 'firebase' || product === 'bigquery' || !product && !table && typeof query === 'string') {
|
|
511
511
|
if (!(0, types_1.isString)(query)) {
|
|
512
512
|
closeClient();
|
|
513
|
-
throw (0, util_1.formatError)(item, "Missing database query"
|
|
513
|
+
throw (0, util_1.formatError)(item, "Missing database query");
|
|
514
514
|
}
|
|
515
515
|
const firebaseApp = product === 'firebase' || isFirebaseApp(credential);
|
|
516
516
|
if (cached) {
|
|
@@ -554,7 +554,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
554
554
|
}
|
|
555
555
|
}
|
|
556
556
|
}
|
|
557
|
-
const db = createClient(item, 0
|
|
557
|
+
const db = createClient(item, 0);
|
|
558
558
|
if ((0, types_1.isPlainObject)(update)) {
|
|
559
559
|
await updateDB(ref(db, query), update);
|
|
560
560
|
}
|
|
@@ -572,20 +572,20 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
572
572
|
if (limit > 0) {
|
|
573
573
|
options.maxResults = limit;
|
|
574
574
|
}
|
|
575
|
-
const [job] = await createClient(item, 1
|
|
575
|
+
const [job] = await createClient(item, 1).createQueryJob(options);
|
|
576
576
|
[rows] = await job.getQueryResults();
|
|
577
577
|
}
|
|
578
578
|
}
|
|
579
579
|
else if (product === 'datastore' || !product && !table && (keys || query)) {
|
|
580
580
|
if (!keys && !Array.isArray(query)) {
|
|
581
581
|
closeClient();
|
|
582
|
-
throw (0, util_1.formatError)(item, "Missing database query"
|
|
582
|
+
throw (0, util_1.formatError)(item, "Missing database query");
|
|
583
583
|
}
|
|
584
584
|
if (cached && (rows = getCache(queryString = Module.asString(keys || query, true) + Module.asString(item.options, true) + limit))) {
|
|
585
585
|
result[i] = rows;
|
|
586
586
|
continue;
|
|
587
587
|
}
|
|
588
|
-
const db = createClient(item, 2
|
|
588
|
+
const db = createClient(item, 2);
|
|
589
589
|
if (keys) {
|
|
590
590
|
const items = !Array.isArray(keys) ? db.key(keys) : keys.map((key) => db.key(key));
|
|
591
591
|
rows = await db.get(items, item.options);
|
|
@@ -608,7 +608,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
608
608
|
case 'order':
|
|
609
609
|
case 'select':
|
|
610
610
|
case 'start':
|
|
611
|
-
task[method].apply(task, args.slice(1));
|
|
611
|
+
task[method].apply(task, args.slice(1));
|
|
612
612
|
break;
|
|
613
613
|
}
|
|
614
614
|
}
|
|
@@ -622,7 +622,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
622
622
|
else if (product === 'firestore' || !product && table) {
|
|
623
623
|
if (!table) {
|
|
624
624
|
closeClient();
|
|
625
|
-
throw (0, util_1.formatError)(item, "Missing database table"
|
|
625
|
+
throw (0, util_1.formatError)(item, "Missing database table");
|
|
626
626
|
}
|
|
627
627
|
if (cached) {
|
|
628
628
|
queryString = table + '_';
|
|
@@ -635,7 +635,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
635
635
|
continue;
|
|
636
636
|
}
|
|
637
637
|
}
|
|
638
|
-
const db = createClient(item, 3
|
|
638
|
+
const db = createClient(item, 3).collection(table).doc(id);
|
|
639
639
|
if ((0, types_1.isPlainObject)(update)) {
|
|
640
640
|
await db.update(update, { exists: true });
|
|
641
641
|
}
|
|
@@ -649,7 +649,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
649
649
|
result[i] = rows;
|
|
650
650
|
continue;
|
|
651
651
|
}
|
|
652
|
-
let db = createClient(item, 3
|
|
652
|
+
let db = createClient(item, 3).collection(table);
|
|
653
653
|
for (const args of query) {
|
|
654
654
|
if ((0, types_1.isArray)(args)) {
|
|
655
655
|
const method = args[0];
|
|
@@ -665,7 +665,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
665
665
|
case 'startAt':
|
|
666
666
|
case 'where':
|
|
667
667
|
case 'withConverter':
|
|
668
|
-
db = db[method].apply(db, args.slice(1));
|
|
668
|
+
db = db[method].apply(db, args.slice(1));
|
|
669
669
|
break;
|
|
670
670
|
}
|
|
671
671
|
}
|
|
@@ -684,12 +684,12 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
684
684
|
}
|
|
685
685
|
else {
|
|
686
686
|
closeClient();
|
|
687
|
-
throw (0, util_1.formatError)(item, "Missing database query"
|
|
687
|
+
throw (0, util_1.formatError)(item, "Missing database query");
|
|
688
688
|
}
|
|
689
689
|
}
|
|
690
690
|
else {
|
|
691
691
|
closeClient();
|
|
692
|
-
throw (0, util_1.formatError)(item, "Missing database product"
|
|
692
|
+
throw (0, util_1.formatError)(item, "Missing database product");
|
|
693
693
|
}
|
|
694
694
|
result[i] = this.setQueryResult(service, credential, queryString, rows, cacheValue);
|
|
695
695
|
}
|
|
@@ -701,12 +701,12 @@ function setPublicRead(acl, pathname, requested, service = 'gcp') {
|
|
|
701
701
|
acl.add({ entity: 'allUsers', role: 'READER' })
|
|
702
702
|
.then(() => {
|
|
703
703
|
if (requested) {
|
|
704
|
-
this.formatMessage(64
|
|
704
|
+
this.formatMessage(64, service, "Grant public-read", pathname, { ...Cloud.LOG_CLOUD_COMMAND });
|
|
705
705
|
}
|
|
706
706
|
})
|
|
707
707
|
.catch(err => {
|
|
708
708
|
if (requested) {
|
|
709
|
-
this.formatMessage(64
|
|
709
|
+
this.formatMessage(64, service, ["Unable to grant public-read", pathname], err, { ...Cloud.LOG_CLOUD_WARN });
|
|
710
710
|
}
|
|
711
711
|
});
|
|
712
712
|
}
|
|
@@ -781,10 +781,10 @@ async function setPredefinedAcl(target, value, bucket, pathname = '', metadata =
|
|
|
781
781
|
}
|
|
782
782
|
return updatePredefinedAcl(target, items, metadata).then(errors => {
|
|
783
783
|
if (errors.length) {
|
|
784
|
-
this.formatMessage(64
|
|
784
|
+
this.formatMessage(64, service, [`Unable to ${revoke ? 'revoke' : 'grant'} ` + value, bucket && pathname ? bucket.name + '/' + pathname : bucket ? bucket.name : pathname], errors[0], { ...Cloud.LOG_CLOUD_WARN });
|
|
785
785
|
}
|
|
786
786
|
else if (requested && (bucket || pathname)) {
|
|
787
|
-
this.formatMessage(64
|
|
787
|
+
this.formatMessage(64, service, [(revoke ? 'Revoke' : 'Grant') + ' ' + value, bucket?.name], pathname, { ...Cloud.LOG_CLOUD_COMMAND });
|
|
788
788
|
}
|
|
789
789
|
});
|
|
790
790
|
}
|
package/download/index.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
2
|
const path = require("path");
|
|
4
3
|
const fs = require("fs");
|
|
5
4
|
const https = require("https");
|
|
6
5
|
const types_1 = require("@e-mc/types");
|
|
7
6
|
const Module = require("@e-mc/module");
|
|
8
7
|
const Cloud = require("@e-mc/cloud");
|
|
9
|
-
const client_1 = require("
|
|
8
|
+
const client_1 = require("@pi-r/gcp");
|
|
10
9
|
function download(credential, service = 'gcp') {
|
|
11
10
|
const storage = client_1.createStorageClient.call(this, credential);
|
|
12
11
|
return (data, callback) => {
|
|
@@ -25,12 +24,12 @@ function download(credential, service = 'gcp') {
|
|
|
25
24
|
return;
|
|
26
25
|
}
|
|
27
26
|
if (!(tempDir = this.getTempDir({ uuidDir: true }))) {
|
|
28
|
-
downloadResponse(new Error("Unable to create temp directory"
|
|
27
|
+
downloadResponse(new Error("Unable to create temp directory"));
|
|
29
28
|
return;
|
|
30
29
|
}
|
|
31
30
|
const location = Module.joinPath(Bucket, Key);
|
|
32
31
|
const destination = path.join(tempDir, path.basename(Key));
|
|
33
|
-
const deleteMessage = () => this.formatMessage(64
|
|
32
|
+
const deleteMessage = () => this.formatMessage(64, service, "Delete success", location, { ...Cloud.LOG_CLOUD_DELETE });
|
|
34
33
|
if (firebase) {
|
|
35
34
|
const { ref, getDownloadURL, deleteObject } = require('@firebase/storage');
|
|
36
35
|
const objectRef = ref(storage, Key);
|
|
@@ -45,7 +44,7 @@ function download(credential, service = 'gcp') {
|
|
|
45
44
|
if (target.deleteObject) {
|
|
46
45
|
deleteObject(objectRef)
|
|
47
46
|
.then(() => deleteMessage())
|
|
48
|
-
.catch(err => this.formatFail(64
|
|
47
|
+
.catch(err => this.formatFail(64, service, ["Delete failed", location], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: !!target.active }));
|
|
49
48
|
}
|
|
50
49
|
})
|
|
51
50
|
.on('error', err => downloadResponse(err)));
|
|
@@ -69,7 +68,7 @@ function download(credential, service = 'gcp') {
|
|
|
69
68
|
deleteMessage();
|
|
70
69
|
}
|
|
71
70
|
else {
|
|
72
|
-
this.formatFail(64
|
|
71
|
+
this.formatFail(64, service, ["Delete failed", location], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: !!target.active });
|
|
73
72
|
}
|
|
74
73
|
});
|
|
75
74
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/gcp",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.3",
|
|
4
4
|
"description": "Google (GCP) cloud functions for E-mc.",
|
|
5
5
|
"main": "client/index.js",
|
|
6
6
|
"publishConfig": {
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
},
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "https://github.com/anpham6/pi-r.git",
|
|
11
|
+
"url": "git+https://github.com/anpham6/pi-r.git",
|
|
12
12
|
"directory": "src/cloud/gcp"
|
|
13
13
|
},
|
|
14
14
|
"keywords": [
|
|
@@ -20,10 +20,10 @@
|
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/cloud": "^0.7.
|
|
24
|
-
"@e-mc/module": "^0.7.
|
|
25
|
-
"@e-mc/types": "^0.7.
|
|
26
|
-
"@google-cloud/firestore": "^7.
|
|
27
|
-
"@google-cloud/storage": "^7.
|
|
23
|
+
"@e-mc/cloud": "^0.7.3",
|
|
24
|
+
"@e-mc/module": "^0.7.3",
|
|
25
|
+
"@e-mc/types": "^0.7.3",
|
|
26
|
+
"@google-cloud/firestore": "^7.6.0",
|
|
27
|
+
"@google-cloud/storage": "^7.9.0"
|
|
28
28
|
}
|
|
29
29
|
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { CloudDatabase, CloudStorage } from '@e-mc/types/lib/cloud';
|
|
2
|
+
|
|
3
|
+
import type { StorageOptions } from '@google-cloud/storage';
|
|
4
|
+
import type { PathType } from '@google-cloud/datastore';
|
|
5
|
+
import type { entity } from '@google-cloud/datastore/build/src/entity';
|
|
6
|
+
import type { FirebaseOptions as IFirebaseOptions } from '@firebase/app';
|
|
7
|
+
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
import type { GoogleAuthOptions } from 'google-auth-library';
|
|
10
|
+
|
|
11
|
+
export type GCPStorage = CloudStorage<GCPStorageCredential, "gcp | gcloud">;
|
|
12
|
+
|
|
13
|
+
export interface GCPStorageCredential extends StorageOptions, IFirebaseOptions {}
|
|
14
|
+
|
|
15
|
+
export interface GCPDatabaseQuery<T = PlainObject> extends Omit<CloudDatabase<unknown, T, unknown, unknown[] | StandardMap, GCPDatabaseCredential>, "id"> {
|
|
16
|
+
source: "cloud";
|
|
17
|
+
service: "gcp" | "gcloud";
|
|
18
|
+
product?: "firestore" | "firebase" | "bigquery" | "bigtable" | "datastore" | "spanner";
|
|
19
|
+
database?: string;
|
|
20
|
+
id?: ArrayOf<string>;
|
|
21
|
+
updateType?: 0 | 1 | 2 | 3;
|
|
22
|
+
columns?: string[];
|
|
23
|
+
keys?: ArrayOf<DatastoreKey>;
|
|
24
|
+
kind?: ArrayOf<string>;
|
|
25
|
+
orderBy?: unknown[][];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type GCPDatabaseCredential = GoogleAuthOptions;
|
|
29
|
+
|
|
30
|
+
export interface FirebaseOptions extends IFirebaseOptions {
|
|
31
|
+
product?: "firebase";
|
|
32
|
+
admin?: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type DatastoreKey = string | PathType[] | entity.KeyOptions;
|
|
36
|
+
export type BucketAcl = "authenticatedRead" | "private" | "projectPrivate" | "publicRead" | "publicReadWrite" | "bucketAccessUniform" | "bucketAccessACL";
|
package/upload/index.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
2
|
const path = require("path");
|
|
4
3
|
const fs = require("fs");
|
|
5
4
|
const util_1 = require("@e-mc/cloud/util");
|
|
6
5
|
const types_1 = require("@e-mc/types");
|
|
7
6
|
const Module = require("@e-mc/module");
|
|
8
7
|
const Cloud = require("@e-mc/cloud");
|
|
9
|
-
const client_1 = require("
|
|
8
|
+
const client_1 = require("@pi-r/gcp");
|
|
10
9
|
const BUCKET_SESSION = new Set();
|
|
11
10
|
const BUCKET_RESPONSE = {};
|
|
12
11
|
const getBucketKey = (credential, bucket, acl = '') => Module.asString(credential, true) + bucket + '_' + acl;
|
|
@@ -57,7 +56,7 @@ function upload(credential, service = 'gcp') {
|
|
|
57
56
|
if (typeof retentionPolicy === 'number') {
|
|
58
57
|
bucketClient.setRetentionPeriod(retentionPolicy)
|
|
59
58
|
.then(([response]) => {
|
|
60
|
-
this.formatMessage(64
|
|
59
|
+
this.formatMessage(64, service, ["Bucket configured" + ' (retention policy)', bucket], response.retentionPolicy?.effectiveTime, { ...Cloud.LOG_CLOUD_COMMAND });
|
|
61
60
|
})
|
|
62
61
|
.catch(err => addLog(err));
|
|
63
62
|
}
|
|
@@ -82,13 +81,13 @@ function upload(credential, service = 'gcp') {
|
|
|
82
81
|
}
|
|
83
82
|
} while (exists && ++i);
|
|
84
83
|
if (i > 0) {
|
|
85
|
-
this.formatMessage(64
|
|
84
|
+
this.formatMessage(64, service, ["File renamed", current], filename, { ...Cloud.LOG_CLOUD_WARN });
|
|
86
85
|
}
|
|
87
86
|
}
|
|
88
87
|
const Key = [filename];
|
|
89
88
|
const Body = [data.buffer];
|
|
90
89
|
const ContentType = [contentType];
|
|
91
|
-
const getURL = (value) => endpoint ? Module.joinPath(endpoint, value) : Module.joinPath("https://storage.googleapis.com"
|
|
90
|
+
const getURL = (value) => endpoint ? Module.joinPath(endpoint, value) : Module.joinPath("https://storage.googleapis.com", bucket, value);
|
|
92
91
|
if (fileGroup) {
|
|
93
92
|
for (const [content, ext, localFile] of fileGroup) {
|
|
94
93
|
Key.push(ext === '.map' && localFile ? path.basename(localFile) : filename + ext);
|
|
@@ -132,7 +131,7 @@ function upload(credential, service = 'gcp') {
|
|
|
132
131
|
uploadBytes(ref(bucketClient, destination), Body[i], params)
|
|
133
132
|
.then(() => {
|
|
134
133
|
const url = getURL(destination);
|
|
135
|
-
this.formatMessage(64
|
|
134
|
+
this.formatMessage(64, service, "Upload success", url, { ...Cloud.LOG_CLOUD_UPLOAD });
|
|
136
135
|
if (first) {
|
|
137
136
|
uploadResponse(null, url);
|
|
138
137
|
}
|
|
@@ -152,7 +151,7 @@ function upload(credential, service = 'gcp') {
|
|
|
152
151
|
const output = Body[i];
|
|
153
152
|
if (Buffer.isBuffer(output) || path.basename(output) !== Key[i]) {
|
|
154
153
|
if (!(tempDir = this.getTempDir({ uuidDir: true }))) {
|
|
155
|
-
throw new Error("Unable to create temp directory"
|
|
154
|
+
throw new Error("Unable to create temp directory");
|
|
156
155
|
}
|
|
157
156
|
outputUri = path.join(tempDir, Key[i]);
|
|
158
157
|
if (typeof output === 'string') {
|
|
@@ -213,7 +212,7 @@ function upload(credential, service = 'gcp') {
|
|
|
213
212
|
bucketClient.upload(outputUri, params, (err, file) => {
|
|
214
213
|
if (file) {
|
|
215
214
|
const url = getURL(file.name);
|
|
216
|
-
this.formatMessage(64
|
|
215
|
+
this.formatMessage(64, service, "Upload success", url, { ...Cloud.LOG_CLOUD_UPLOAD });
|
|
217
216
|
if (first) {
|
|
218
217
|
uploadResponse(null, url);
|
|
219
218
|
}
|