@pi-r/gcp 0.6.0 → 0.6.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.
- package/client/index.js +161 -126
- package/package.json +4 -4
- package/upload/index.js +20 -66
package/client/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.getProjectId = exports.createBucketRequest = exports.setPredefinedAcl = exports.setPublicRead = exports.executeBatchQuery = exports.executeQuery = exports.deleteObjectsV2 = exports.deleteObjects = exports.setBucketWebsite = exports.setBucketPolicy = exports.createBucketV2 = exports.createBucket = exports.createDatabaseClient = exports.createStorageClient = exports.validateDatabase = exports.validateStorage = exports.isFirebaseApp = void 0;
|
|
4
4
|
const path = require("path");
|
|
5
5
|
const storage = require("@google-cloud/storage");
|
|
6
6
|
const firestore = require("@google-cloud/firestore");
|
|
@@ -117,7 +117,7 @@ function validateStorage(credential) {
|
|
|
117
117
|
}
|
|
118
118
|
exports.validateStorage = validateStorage;
|
|
119
119
|
function validateDatabase(credential, data) {
|
|
120
|
-
return hasKeyFile(credential) && !!(data.table || data.query || data.keys || data.
|
|
120
|
+
return hasKeyFile(credential) && !!(data.product || data.database || data.table || data.query || data.id || data.keys || data.kind || data.options?.query) || isFirebaseApp(credential);
|
|
121
121
|
}
|
|
122
122
|
exports.validateDatabase = validateDatabase;
|
|
123
123
|
function createStorageClient(credential, bucket) {
|
|
@@ -142,9 +142,9 @@ exports.createStorageClient = createStorageClient;
|
|
|
142
142
|
function createDatabaseClient(credential, data) {
|
|
143
143
|
let packageName = '';
|
|
144
144
|
try {
|
|
145
|
-
let product, name, table;
|
|
145
|
+
let product, name, database, table, query;
|
|
146
146
|
if (data) {
|
|
147
|
-
({ product, name, table } = data);
|
|
147
|
+
({ product, name, table, query } = data);
|
|
148
148
|
}
|
|
149
149
|
if (product === 'firebase' || isFirebaseApp(credential)) {
|
|
150
150
|
packageName = getFirebasePackageName(credential);
|
|
@@ -152,19 +152,19 @@ function createDatabaseClient(credential, data) {
|
|
|
152
152
|
}
|
|
153
153
|
getProjectId(credential);
|
|
154
154
|
if (data) {
|
|
155
|
-
if (product === 'spanner' || !product && name &&
|
|
155
|
+
if (product === 'spanner' || !product && name && database && (query || table)) {
|
|
156
156
|
const { Spanner } = require(packageName = '@google-cloud/spanner');
|
|
157
157
|
return new Spanner(credential);
|
|
158
158
|
}
|
|
159
|
-
if (product === '
|
|
160
|
-
const { Bigtable } = require(packageName = '@google-cloud/bigtable');
|
|
161
|
-
return new Bigtable(credential);
|
|
162
|
-
}
|
|
163
|
-
if (product === 'bigquery' || !product && !table && typeof data.query === 'string') {
|
|
159
|
+
if (product === 'bigquery' || !product && (!name && table && database || (0, types_1.isString)(query))) {
|
|
164
160
|
const { BigQuery } = require(packageName = '@google-cloud/bigquery');
|
|
165
161
|
return new BigQuery(credential);
|
|
166
162
|
}
|
|
167
|
-
if (product === '
|
|
163
|
+
if (product === 'bigtable' || !product && name && table && !database) {
|
|
164
|
+
const { Bigtable } = require(packageName = '@google-cloud/bigtable');
|
|
165
|
+
return new Bigtable(credential);
|
|
166
|
+
}
|
|
167
|
+
if (product === 'datastore' || !product && (data.keys || data.kind || query && !table && !data.id)) {
|
|
168
168
|
const { Datastore } = require(packageName = '@google-cloud/datastore');
|
|
169
169
|
return new Datastore(credential);
|
|
170
170
|
}
|
|
@@ -407,48 +407,70 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
407
407
|
};
|
|
408
408
|
for (let i = 0; i < length; ++i) {
|
|
409
409
|
const item = batch[i];
|
|
410
|
-
const { service, product, name, database, table, id, query, orderBy,
|
|
411
|
-
const
|
|
412
|
-
const cached = caching && ignoreCache !== true || ignoreCache === false || ignoreCache === 1 || renewCache;
|
|
410
|
+
const { service, product, name, database, table, id, query, orderBy, limit = 0, update, ignoreCache } = item;
|
|
411
|
+
const useCache = caching && ignoreCache !== true;
|
|
413
412
|
const getCache = (value) => {
|
|
414
413
|
if (ignoreCache !== 1) {
|
|
415
|
-
cacheValue.renewCache =
|
|
414
|
+
cacheValue.renewCache = ignoreCache === 0;
|
|
416
415
|
return this.getQueryResult(service, credential, value, cacheValue);
|
|
417
416
|
}
|
|
418
417
|
};
|
|
418
|
+
cacheValue.exclusiveOf = Array.isArray(ignoreCache) ? ignoreCache : undefined;
|
|
419
419
|
let rows, queryString = '';
|
|
420
|
-
if (product === '
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
}
|
|
425
|
-
if (!id && limit > 0) {
|
|
426
|
-
(options.gaxOptions || (options.gaxOptions = {})).maxResults = limit;
|
|
427
|
-
}
|
|
428
|
-
if (cached && (rows = getCache(queryString = name + '_' + table + '_' + (id ? id + ((0, types_1.isArray)(columns) ? '_' + Module.asString(columns, true) : '') : '') + Module.asString(options, true)))) {
|
|
429
|
-
result[i] = rows;
|
|
430
|
-
continue;
|
|
420
|
+
if (product === 'firebase' || isFirebaseApp(credential)) {
|
|
421
|
+
if (!(0, types_1.isString)(query)) {
|
|
422
|
+
closeClient();
|
|
423
|
+
throw (0, util_1.formatError)(item, "Missing database query" /* ERR_DB.QUERY */);
|
|
431
424
|
}
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
rows = [getRowData(id, data)];
|
|
425
|
+
if (useCache) {
|
|
426
|
+
queryString = query + Module.asString(orderBy, true);
|
|
427
|
+
if (!update && (rows = getCache(queryString))) {
|
|
428
|
+
result[i] = rows;
|
|
429
|
+
continue;
|
|
438
430
|
}
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
431
|
+
}
|
|
432
|
+
const { ref, get, child, query: queryDB, update: updateDB } = require('@firebase/database');
|
|
433
|
+
const constraints = [];
|
|
434
|
+
let limiter = 0;
|
|
435
|
+
if ((0, types_1.isArray)(orderBy)) {
|
|
436
|
+
const sort = require('@firebase/database');
|
|
437
|
+
for (const items of orderBy) {
|
|
438
|
+
if (!(0, types_1.isArray)(items)) {
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
const method = items[0];
|
|
442
|
+
switch (method) {
|
|
443
|
+
case 'orderByChild':
|
|
444
|
+
constraints.push(sort.orderByChild(items[1]));
|
|
445
|
+
break;
|
|
446
|
+
case 'orderByKey':
|
|
447
|
+
case 'orderByValue':
|
|
448
|
+
case 'orderByPriority':
|
|
449
|
+
constraints.push(sort[method]());
|
|
450
|
+
break;
|
|
451
|
+
case 'limitToFirst':
|
|
452
|
+
case 'limitToLast':
|
|
453
|
+
constraints.push(sort[method](limiter = +items[1]));
|
|
454
|
+
break;
|
|
455
|
+
case 'startAt':
|
|
456
|
+
case 'startAfter':
|
|
457
|
+
case 'endAt':
|
|
458
|
+
case 'endBefore':
|
|
459
|
+
case 'equalTo':
|
|
460
|
+
constraints.push(sort[method](items[1], items[2]));
|
|
461
|
+
break;
|
|
462
|
+
}
|
|
442
463
|
}
|
|
443
464
|
}
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
await
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
465
|
+
const db = createClient(item, 0 /* DB.FIREBASE */);
|
|
466
|
+
if ((0, types_1.isPlainObject)(update)) {
|
|
467
|
+
await updateDB(ref(db, query), update);
|
|
468
|
+
}
|
|
469
|
+
const snapshot = await (constraints.length ? get(queryDB(ref(db, query), ...constraints)) : get(child(ref(db), query)));
|
|
470
|
+
if (snapshot.exists()) {
|
|
471
|
+
const val = snapshot.val();
|
|
472
|
+
const group = limiter === 1 && Object.keys(val);
|
|
473
|
+
rows = [group && (0, types_1.isObject)(val) && (0, types_1.isObject)(val[group[0]]) ? val[group[0]] : val];
|
|
452
474
|
}
|
|
453
475
|
}
|
|
454
476
|
else if (product === 'spanner' || !product && name && database && (query || table)) {
|
|
@@ -462,8 +484,8 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
462
484
|
throw (0, util_1.formatError)(item, !name ? "Missing instance name" /* ERR_DB.INSTANCE */ : "Missing database name" /* ERR_DB.NAME */);
|
|
463
485
|
}
|
|
464
486
|
const { updateType, options = {} } = item;
|
|
465
|
-
if (
|
|
466
|
-
queryString = name + '_' + database + '_' + (table ? table + (columns ? Module.asString(columns, true) : '') : '') + Module.asString(query, true) + limit;
|
|
487
|
+
if (useCache) {
|
|
488
|
+
queryString = name + '_' + database + '_' + (table ? table + (columns ? Module.asString(columns, true) : '') : '') + Module.asString(query, true) + (limit > 0 ? limit : '');
|
|
467
489
|
if (!update && (rows = getCache(queryString))) {
|
|
468
490
|
result[i] = rows;
|
|
469
491
|
continue;
|
|
@@ -476,7 +498,9 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
476
498
|
}
|
|
477
499
|
const request = (0, types_1.isPlainObject)(query) ? query : { columns };
|
|
478
500
|
request.json = true;
|
|
479
|
-
|
|
501
|
+
if ((0, types_1.isObject)(item.columns)) {
|
|
502
|
+
request.columns = item.columns;
|
|
503
|
+
}
|
|
480
504
|
request.keySet = { all: true };
|
|
481
505
|
if (limit > 0) {
|
|
482
506
|
request.limit = limit;
|
|
@@ -506,82 +530,97 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
506
530
|
[rows] = await db.run(request);
|
|
507
531
|
}
|
|
508
532
|
}
|
|
509
|
-
else if (product === '
|
|
510
|
-
if (!
|
|
533
|
+
else if (product === 'bigtable' || !product && name && table && !database) {
|
|
534
|
+
if (!name || !table) {
|
|
511
535
|
closeClient();
|
|
512
|
-
throw (0, util_1.formatError)(item, "Missing database
|
|
536
|
+
throw (0, util_1.formatError)(item, !name ? "Missing database name" /* ERR_DB.NAME */ : "Missing database table" /* ERR_DB.TABLE */);
|
|
513
537
|
}
|
|
514
|
-
const
|
|
515
|
-
if (
|
|
516
|
-
|
|
517
|
-
|
|
538
|
+
const { columns, options = {} } = item;
|
|
539
|
+
if ((0, types_1.isObject)(query)) {
|
|
540
|
+
options.filter = query;
|
|
541
|
+
}
|
|
542
|
+
if (!id && limit > 0) {
|
|
543
|
+
(options.gaxOptions || (options.gaxOptions = {})).maxResults = limit;
|
|
544
|
+
}
|
|
545
|
+
if (useCache) {
|
|
546
|
+
queryString = name + '_' + table + '_' + (id ? id + ((0, types_1.isArray)(columns) ? '_' + Module.asString(columns, true) : '') : '') + Module.asString(options, true);
|
|
547
|
+
if (!(update && id) && (rows = getCache(queryString))) {
|
|
518
548
|
result[i] = rows;
|
|
519
549
|
continue;
|
|
520
550
|
}
|
|
521
551
|
}
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
const
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
const sort = require('@firebase/database');
|
|
528
|
-
for (const items of orderBy) {
|
|
529
|
-
if (!(0, types_1.isArray)(items)) {
|
|
530
|
-
continue;
|
|
531
|
-
}
|
|
532
|
-
const method = items[0];
|
|
533
|
-
switch (method) {
|
|
534
|
-
case 'orderByChild':
|
|
535
|
-
constraints.push(sort.orderByChild(items[1]));
|
|
536
|
-
break;
|
|
537
|
-
case 'orderByKey':
|
|
538
|
-
case 'orderByValue':
|
|
539
|
-
case 'orderByPriority':
|
|
540
|
-
constraints.push(sort[method]());
|
|
541
|
-
break;
|
|
542
|
-
case 'limitToFirst':
|
|
543
|
-
case 'limitToLast':
|
|
544
|
-
constraints.push(sort[method](limiter = +items[1]));
|
|
545
|
-
break;
|
|
546
|
-
case 'startAt':
|
|
547
|
-
case 'startAfter':
|
|
548
|
-
case 'endAt':
|
|
549
|
-
case 'endBefore':
|
|
550
|
-
case 'equalTo':
|
|
551
|
-
constraints.push(sort[method](items[1], items[2]));
|
|
552
|
-
break;
|
|
553
|
-
}
|
|
554
|
-
}
|
|
552
|
+
const db = createClient(item, 4 /* DB.BIGTABLE */).instance(name).table(table);
|
|
553
|
+
if (id) {
|
|
554
|
+
const row = db.row(id);
|
|
555
|
+
if (update) {
|
|
556
|
+
await row.save(update);
|
|
555
557
|
}
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
558
|
+
if (Array.isArray(columns)) {
|
|
559
|
+
const [data] = await row.get(columns, options);
|
|
560
|
+
rows = [getRowData(id, data)];
|
|
559
561
|
}
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
const group = limiter === 1 && Object.keys(val);
|
|
564
|
-
rows = [group && (0, types_1.isObject)(val) && (0, types_1.isObject)(val[group[0]]) ? val[group[0]] : val];
|
|
562
|
+
else {
|
|
563
|
+
const [response] = await row.get(options);
|
|
564
|
+
rows = [getRowData(response.id, response.data)];
|
|
565
565
|
}
|
|
566
566
|
}
|
|
567
567
|
else {
|
|
568
|
-
|
|
568
|
+
rows = [];
|
|
569
|
+
await new Promise((resolve, reject) => {
|
|
570
|
+
db.createReadStream(options)
|
|
571
|
+
.on('data', row => rows.push(getRowData(row.id, row.data)))
|
|
572
|
+
.on('error', reject)
|
|
573
|
+
.on('end', resolve);
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
else if (product === 'bigquery' || !product && (!name && database && table || (0, types_1.isString)(query) || (0, types_1.isString)(item.options?.query))) {
|
|
578
|
+
const { params, options = {} } = item;
|
|
579
|
+
if ((0, types_1.isString)(query)) {
|
|
569
580
|
options.query = query;
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
581
|
+
}
|
|
582
|
+
if ((0, types_1.isObject)(params)) {
|
|
583
|
+
options.params = params;
|
|
584
|
+
}
|
|
585
|
+
if (limit > 0) {
|
|
586
|
+
options.maxResults = limit;
|
|
587
|
+
}
|
|
588
|
+
if (useCache) {
|
|
589
|
+
queryString = query + (database ? '_' + database + (table ? '_' + table : '') : '') + Module.asString(options, true);
|
|
590
|
+
if (!update && (rows = getCache(queryString))) {
|
|
591
|
+
result[i] = rows;
|
|
592
|
+
continue;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
const instance = createClient(item, 1 /* DB.BIGQUERY */);
|
|
596
|
+
let job;
|
|
597
|
+
if (database) {
|
|
598
|
+
const dataset = instance.dataset(database);
|
|
599
|
+
let target = dataset;
|
|
600
|
+
if (table) {
|
|
601
|
+
target = target.table(table);
|
|
602
|
+
options.destination = target;
|
|
603
|
+
}
|
|
604
|
+
if ((0, types_1.isString)(update)) {
|
|
605
|
+
const [metadata] = await target.getMetadata();
|
|
606
|
+
metadata.view = update;
|
|
607
|
+
await target.setMetadata(metadata);
|
|
573
608
|
}
|
|
574
|
-
|
|
575
|
-
[rows] = await job.getQueryResults();
|
|
609
|
+
[job] = await dataset.createQueryJob(options);
|
|
576
610
|
}
|
|
611
|
+
else {
|
|
612
|
+
[job] = await instance.createQueryJob(options);
|
|
613
|
+
}
|
|
614
|
+
[rows] = await job.getQueryResults();
|
|
577
615
|
}
|
|
578
|
-
else if (product === 'datastore' || !product &&
|
|
616
|
+
else if (product === 'datastore' || !product && (item.keys || item.kind || query && !table && !id)) {
|
|
617
|
+
const { keys, kind } = item;
|
|
579
618
|
if (!keys && !Array.isArray(query)) {
|
|
580
619
|
closeClient();
|
|
581
620
|
throw (0, util_1.formatError)(item, "Missing database query" /* ERR_DB.QUERY */);
|
|
582
621
|
}
|
|
583
|
-
if (
|
|
584
|
-
queryString = Module.asString(keys
|
|
622
|
+
if (useCache) {
|
|
623
|
+
queryString = (keys ? Module.asString(keys, true) : Module.asString(query, true) + (kind || '')) + Module.asString(item.options, true) + (limit > 0 ? limit : 0);
|
|
585
624
|
if (!update && (rows = getCache(queryString))) {
|
|
586
625
|
result[i] = rows;
|
|
587
626
|
continue;
|
|
@@ -594,26 +633,12 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
594
633
|
await db.save({ key: items, data: update });
|
|
595
634
|
}
|
|
596
635
|
rows = await db.get(items, item.options);
|
|
597
|
-
if (rows.length > limit) {
|
|
636
|
+
if (limit > 0 && rows.length > limit) {
|
|
598
637
|
rows = rows.slice(0, limit);
|
|
599
638
|
}
|
|
600
639
|
}
|
|
601
640
|
else {
|
|
602
|
-
const task = db.createQuery(
|
|
603
|
-
if ((0, types_1.isPlainObject)(update)) {
|
|
604
|
-
await new Promise((resolve, reject) => {
|
|
605
|
-
db.runQuery(task.select('__key__'), item.options).then(entities => {
|
|
606
|
-
if ((0, types_1.isArray)(entities)) {
|
|
607
|
-
const items = entities.map((entity) => db.key(entity[db.KEY]));
|
|
608
|
-
db.save({ key: items, data: update }).then(() => resolve()).catch(err => reject(err));
|
|
609
|
-
}
|
|
610
|
-
else {
|
|
611
|
-
resolve();
|
|
612
|
-
}
|
|
613
|
-
})
|
|
614
|
-
.catch(err => reject(err));
|
|
615
|
-
});
|
|
616
|
-
}
|
|
641
|
+
const task = db.createQuery(kind);
|
|
617
642
|
for (const args of query) {
|
|
618
643
|
if ((0, types_1.isArray)(args)) {
|
|
619
644
|
const method = args[0];
|
|
@@ -635,6 +660,20 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
635
660
|
if (limit > 0) {
|
|
636
661
|
task.limit(limit);
|
|
637
662
|
}
|
|
663
|
+
if ((0, types_1.isPlainObject)(update)) {
|
|
664
|
+
await new Promise((resolve, reject) => {
|
|
665
|
+
db.runQuery(task.select('__key__'), item.options).then(entities => {
|
|
666
|
+
if ((0, types_1.isArray)(entities)) {
|
|
667
|
+
const items = entities.map((entity) => db.key(entity[db.KEY]));
|
|
668
|
+
db.save({ key: items, data: update }).then(() => resolve()).catch(err => reject(err));
|
|
669
|
+
}
|
|
670
|
+
else {
|
|
671
|
+
resolve();
|
|
672
|
+
}
|
|
673
|
+
})
|
|
674
|
+
.catch(err => reject(err));
|
|
675
|
+
});
|
|
676
|
+
}
|
|
638
677
|
[rows] = await db.runQuery(task, item.options);
|
|
639
678
|
}
|
|
640
679
|
}
|
|
@@ -643,12 +682,9 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
643
682
|
closeClient();
|
|
644
683
|
throw (0, util_1.formatError)(item, "Missing database table" /* ERR_DB.TABLE */);
|
|
645
684
|
}
|
|
646
|
-
if (cached) {
|
|
647
|
-
queryString = table + '_';
|
|
648
|
-
}
|
|
649
685
|
if (id) {
|
|
650
|
-
if (
|
|
651
|
-
queryString
|
|
686
|
+
if (useCache) {
|
|
687
|
+
queryString = table + '_' + id;
|
|
652
688
|
if (!update && (rows = getCache(queryString))) {
|
|
653
689
|
result[i] = rows;
|
|
654
690
|
continue;
|
|
@@ -664,7 +700,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
664
700
|
}
|
|
665
701
|
}
|
|
666
702
|
else if (Array.isArray(query)) {
|
|
667
|
-
if (
|
|
703
|
+
if (useCache && (rows = getCache(queryString = table + '_' + Module.asString(query, true) + Module.asString(orderBy, true) + (limit > 0 ? limit : '')))) {
|
|
668
704
|
result[i] = rows;
|
|
669
705
|
continue;
|
|
670
706
|
}
|
|
@@ -859,4 +895,3 @@ function getProjectId(credential) {
|
|
|
859
895
|
}
|
|
860
896
|
}
|
|
861
897
|
exports.getProjectId = getProjectId;
|
|
862
|
-
exports.CLOUD_UPLOAD_FROMDISK = true;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/gcp",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Google (GCP) cloud functions for E-mc.",
|
|
5
5
|
"main": "client/index.js",
|
|
6
6
|
"publishConfig": {
|
|
@@ -20,9 +20,9 @@
|
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/cloud": "^0.8.
|
|
24
|
-
"@e-mc/module": "^0.8.
|
|
25
|
-
"@e-mc/types": "^0.8.
|
|
23
|
+
"@e-mc/cloud": "^0.8.1",
|
|
24
|
+
"@e-mc/module": "^0.8.1",
|
|
25
|
+
"@e-mc/types": "^0.8.1",
|
|
26
26
|
"@google-cloud/firestore": "^7.1.0",
|
|
27
27
|
"@google-cloud/storage": "^7.7.0"
|
|
28
28
|
}
|
package/upload/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const path = require("path");
|
|
4
|
-
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");
|
|
@@ -85,15 +84,15 @@ function upload(credential, service = 'gcp') {
|
|
|
85
84
|
this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["File renamed" /* VAL_CLOUD.RENAME_FILE */, current], filename, { ...Cloud.LOG_CLOUD_WARN });
|
|
86
85
|
}
|
|
87
86
|
}
|
|
87
|
+
const getURL = (value) => endpoint ? Module.joinPath(endpoint, value) : Module.joinPath("https://storage.googleapis.com" /* STRINGS.BASE_URL */, bucket, value);
|
|
88
88
|
const Key = [filename];
|
|
89
89
|
const Body = [data.buffer];
|
|
90
90
|
const ContentType = [contentType];
|
|
91
|
-
const getURL = (value) => endpoint ? Module.joinPath(endpoint, value) : Module.joinPath("https://storage.googleapis.com" /* STRINGS.BASE_URL */, bucket, value);
|
|
92
91
|
if (fileGroup) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
92
|
+
const [key, body, type] = (0, util_1.createKeyAndBody)(filename, fileGroup, addLog);
|
|
93
|
+
Key.push(...key);
|
|
94
|
+
Body.push(...body);
|
|
95
|
+
ContentType.push(...type);
|
|
97
96
|
}
|
|
98
97
|
for (let i = 0; i < Key.length; ++i) {
|
|
99
98
|
const first = i === 0;
|
|
@@ -103,21 +102,9 @@ function upload(credential, service = 'gcp') {
|
|
|
103
102
|
}
|
|
104
103
|
return;
|
|
105
104
|
}
|
|
105
|
+
const destination = pathname ? Module.joinPath(pathname, Key[i]) : Key[i];
|
|
106
106
|
if (firebase) {
|
|
107
107
|
const { ref, uploadBytes } = require('@firebase/storage');
|
|
108
|
-
if (!Buffer.isBuffer(Body[i])) {
|
|
109
|
-
try {
|
|
110
|
-
Body[i] = fs.readFileSync(Body[i]);
|
|
111
|
-
}
|
|
112
|
-
catch (err) {
|
|
113
|
-
if (first) {
|
|
114
|
-
uploadResponse(err);
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
addLog(err);
|
|
118
|
-
continue;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
108
|
const params = { ...options };
|
|
122
109
|
if (first) {
|
|
123
110
|
params.contentType || (params.contentType = ContentType[i]);
|
|
@@ -128,7 +115,6 @@ function upload(credential, service = 'gcp') {
|
|
|
128
115
|
else {
|
|
129
116
|
params.contentType = ContentType[i];
|
|
130
117
|
}
|
|
131
|
-
const destination = pathname ? Module.joinPath(pathname, Key[i]) : Key[i];
|
|
132
118
|
uploadBytes(ref(bucketClient, destination), Body[i], params)
|
|
133
119
|
.then(() => {
|
|
134
120
|
const url = getURL(destination);
|
|
@@ -147,33 +133,6 @@ function upload(credential, service = 'gcp') {
|
|
|
147
133
|
});
|
|
148
134
|
}
|
|
149
135
|
else {
|
|
150
|
-
let outputUri, tempDir;
|
|
151
|
-
try {
|
|
152
|
-
const output = Body[i];
|
|
153
|
-
if (Buffer.isBuffer(output) || path.basename(output) !== Key[i]) {
|
|
154
|
-
if (!(tempDir = this.getTempDir({ uuidDir: true }))) {
|
|
155
|
-
throw new Error("Unable to create temp directory" /* ERR_CLOUD.TEMP_DIR */);
|
|
156
|
-
}
|
|
157
|
-
outputUri = path.join(tempDir, Key[i]);
|
|
158
|
-
if (typeof output === 'string') {
|
|
159
|
-
fs.copyFileSync(output, outputUri);
|
|
160
|
-
}
|
|
161
|
-
else {
|
|
162
|
-
fs.writeFileSync(outputUri, output);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
else {
|
|
166
|
-
outputUri = output;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
catch (err) {
|
|
170
|
-
if (first) {
|
|
171
|
-
uploadResponse(err);
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
addLog(err);
|
|
175
|
-
continue;
|
|
176
|
-
}
|
|
177
136
|
const params = { ...options };
|
|
178
137
|
const readable = publicRead || active && publicRead !== false && publicRead !== 0 && !acl;
|
|
179
138
|
let ACL, revoke, predefined;
|
|
@@ -185,7 +144,7 @@ function upload(credential, service = 'gcp') {
|
|
|
185
144
|
else {
|
|
186
145
|
predefined = true;
|
|
187
146
|
}
|
|
188
|
-
if (metadata) {
|
|
147
|
+
if (typeof metadata === 'object') {
|
|
189
148
|
params.metadata = metadata;
|
|
190
149
|
}
|
|
191
150
|
}
|
|
@@ -209,26 +168,21 @@ function upload(credential, service = 'gcp') {
|
|
|
209
168
|
params.predefinedAcl = acl;
|
|
210
169
|
}
|
|
211
170
|
}
|
|
212
|
-
params.
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
}
|
|
220
|
-
if (ACL) {
|
|
221
|
-
client_1.setPredefinedAcl.call(this, file.acl, ACL, file.bucket, file.name, revoke || file.metadata, true);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
else if (first) {
|
|
225
|
-
uploadResponse(err);
|
|
171
|
+
params.resumable ?? (params.resumable = false);
|
|
172
|
+
const file = bucketClient.file(destination);
|
|
173
|
+
file.save(Body[i], params).then(async () => {
|
|
174
|
+
const url = getURL(file.name);
|
|
175
|
+
this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, "Upload success" /* VAL_CLOUD.UPLOAD_FILE */, url, { ...Cloud.LOG_CLOUD_UPLOAD });
|
|
176
|
+
if (first) {
|
|
177
|
+
uploadResponse(null, url);
|
|
226
178
|
}
|
|
227
|
-
|
|
228
|
-
|
|
179
|
+
if (ACL) {
|
|
180
|
+
client_1.setPredefinedAcl.call(this, file.acl, ACL, file.bucket, file.name, revoke || (await file.getMetadata())[0], true);
|
|
229
181
|
}
|
|
230
|
-
|
|
231
|
-
|
|
182
|
+
})
|
|
183
|
+
.catch(err => {
|
|
184
|
+
if (first) {
|
|
185
|
+
uploadResponse(err);
|
|
232
186
|
}
|
|
233
187
|
});
|
|
234
188
|
}
|