@pi-r/gcp 0.5.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CLOUD_UPLOAD_FROMDISK = 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;
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");
@@ -63,7 +63,7 @@ async function hasBucket(client) {
63
63
  const [exists] = await client.exists().catch(err => isErrorConflict(err) ? [true] : [false]);
64
64
  return exists;
65
65
  }
66
- function updatePredefinedAcl(acl, values, metadata = {}) {
66
+ async function updatePredefinedAcl(acl, values, metadata = {}) {
67
67
  const items = [];
68
68
  let projectNumber;
69
69
  if ('projectNumber' in metadata || 'defaultObjectAcl' in metadata) {
@@ -102,7 +102,7 @@ function updatePredefinedAcl(acl, values, metadata = {}) {
102
102
  })
103
103
  .catch(err => [err]);
104
104
  }
105
- function setUniformBucketLevel(bucket, enabled) {
105
+ async function setUniformBucketLevel(bucket, enabled) {
106
106
  return bucket.setMetadata({ iamConfiguration: { uniformBucketLevelAccess: { enabled } } });
107
107
  }
108
108
  const getFirebasePackageName = (credential) => credential.apiKey && credential.authDomain ? 'firebase' : 'firebase-admin';
@@ -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.product || data.database) || isFirebaseApp(credential);
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) {
@@ -135,36 +135,36 @@ function createStorageClient(credential, bucket) {
135
135
  throw err;
136
136
  }
137
137
  }
138
- credential.projectId || (credential.projectId = getProjectId(credential));
138
+ getProjectId(credential);
139
139
  return new storage.Storage(credential);
140
140
  }
141
141
  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);
151
151
  return getApp(credential, true);
152
152
  }
153
- credential.projectId || (credential.projectId = getProjectId(credential));
153
+ getProjectId(credential);
154
154
  if (data) {
155
- if (product === 'spanner' || !product && name && data.database) {
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 === 'bigtable' || !product && name && table) {
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 === 'datastore' || !product && !table && (data.keys || data.query)) {
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
  }
@@ -263,9 +263,9 @@ async function setBucketPolicy(credential, bucket, options, service = 'gcp') {
263
263
  return true;
264
264
  }
265
265
  exports.setBucketPolicy = setBucketPolicy;
266
- function setBucketWebsite(credential, bucket, options, service = 'gcp') {
266
+ async function setBucketWebsite(credential, bucket, options, service = 'gcp') {
267
267
  if (isFirebaseApp(credential, true, true)) {
268
- return Promise.resolve(false);
268
+ return false;
269
269
  }
270
270
  const website = {};
271
271
  const { indexPage, errorPage } = options;
@@ -286,7 +286,7 @@ function setBucketWebsite(credential, bucket, options, service = 'gcp') {
286
286
  });
287
287
  }
288
288
  exports.setBucketWebsite = setBucketWebsite;
289
- function deleteObjects(credential, Bucket, service) {
289
+ async function deleteObjects(credential, Bucket, service) {
290
290
  return deleteObjectsV2.call(this, credential, Bucket, true, service);
291
291
  }
292
292
  exports.deleteObjects = deleteObjects;
@@ -314,7 +314,7 @@ async function deleteObjectsV2(credential, bucket, recursive = true, service = '
314
314
  }
315
315
  })('');
316
316
  if (files.length) {
317
- promise = Promise.allSettled(files.map(value => deleteObject(ref(instance, value))));
317
+ promise = Promise.allSettled(files.map(async (value) => deleteObject(ref(instance, value))));
318
318
  }
319
319
  }
320
320
  else {
@@ -327,7 +327,7 @@ async function deleteObjectsV2(credential, bucket, recursive = true, service = '
327
327
  try {
328
328
  const result = await client.getFiles();
329
329
  for (const { name } of result[0]) {
330
- if (name.indexOf('/') === -1) {
330
+ if (!name.includes('/')) {
331
331
  tasks.push(client.file(name).delete({ ignoreNotFound: true }).catch(err => this.formatFail(64 /* LOG_TYPE.CLOUD */, service, ["Delete failed" /* ERR_CLOUD.DELETE_FAIL */, name], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: false })));
332
332
  }
333
333
  }
@@ -407,49 +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, keys, limit = 0, update, ignoreCache } = item;
411
- const renewCache = ignoreCache === 0;
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
- if (ignoreCache === 1) {
415
- return;
413
+ if (ignoreCache !== 1) {
414
+ cacheValue.renewCache = ignoreCache === 0;
415
+ return this.getQueryResult(service, credential, value, cacheValue);
416
416
  }
417
- cacheValue.renewCache = renewCache;
418
- return this.getQueryResult(service, credential, value, cacheValue);
419
417
  };
418
+ cacheValue.exclusiveOf = Array.isArray(ignoreCache) ? ignoreCache : undefined;
420
419
  let rows, queryString = '';
421
- if (product === 'bigtable' || !product && name && table && !database) {
422
- const { columns, options = {} } = item;
423
- if ((0, types_1.isObject)(query)) {
424
- options.filter = query;
425
- }
426
- if (!id && limit > 0) {
427
- (options.gaxOptions || (options.gaxOptions = {})).maxResults = limit;
428
- }
429
- if (cached && (rows = getCache(queryString = name + '_' + table + '_' + (id ? id + ((0, types_1.isArray)(columns) ? '_' + Module.asString(columns, true) : '') : '') + Module.asString(options, true)))) {
430
- result[i] = rows;
431
- 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 */);
432
424
  }
433
- const db = createClient(item, 4 /* DB.BIGTABLE */).instance(name).table(table);
434
- if (id) {
435
- const row = db.row(id);
436
- if (Array.isArray(columns)) {
437
- const [data] = await row.get(columns, options);
438
- 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;
439
430
  }
440
- else {
441
- const [response] = await row.get(options);
442
- rows = [getRowData(response.id, response.data)];
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
+ }
443
463
  }
444
464
  }
445
- else {
446
- rows = [];
447
- await new Promise((resolve, reject) => {
448
- db.createReadStream(options)
449
- .on('data', row => rows.push(getRowData(row.id, row.data)))
450
- .on('error', reject)
451
- .on('end', resolve);
452
- });
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];
453
474
  }
454
475
  }
455
476
  else if (product === 'spanner' || !product && name && database && (query || table)) {
@@ -463,8 +484,8 @@ async function executeBatchQuery(credential, batch, sessionKey) {
463
484
  throw (0, util_1.formatError)(item, !name ? "Missing instance name" /* ERR_DB.INSTANCE */ : "Missing database name" /* ERR_DB.NAME */);
464
485
  }
465
486
  const { updateType, options = {} } = item;
466
- if (cached) {
467
- queryString = name + '_' + database + '_' + (table ? table + 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 : '');
468
489
  if (!update && (rows = getCache(queryString))) {
469
490
  result[i] = rows;
470
491
  continue;
@@ -477,7 +498,9 @@ async function executeBatchQuery(credential, batch, sessionKey) {
477
498
  }
478
499
  const request = (0, types_1.isPlainObject)(query) ? query : { columns };
479
500
  request.json = true;
480
- request.columns || (request.columns = item.columns);
501
+ if ((0, types_1.isObject)(item.columns)) {
502
+ request.columns = item.columns;
503
+ }
481
504
  request.keySet = { all: true };
482
505
  if (limit > 0) {
483
506
  request.limit = limit;
@@ -493,7 +516,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
493
516
  }
494
517
  else {
495
518
  transaction.runUpdate(update)
496
- .then(() => transaction.commit().then(() => resolve()).catch(error => reject(error)))
519
+ .then(async () => transaction.commit().then(() => resolve()).catch(error => reject(error)))
497
520
  .catch(error => reject(error));
498
521
  }
499
522
  });
@@ -507,94 +530,115 @@ async function executeBatchQuery(credential, batch, sessionKey) {
507
530
  [rows] = await db.run(request);
508
531
  }
509
532
  }
510
- else if (product === 'firebase' || product === 'bigquery' || !product && !table && typeof query === 'string') {
511
- if (!(0, types_1.isString)(query)) {
533
+ else if (product === 'bigtable' || !product && name && table && !database) {
534
+ if (!name || !table) {
512
535
  closeClient();
513
- throw (0, util_1.formatError)(item, "Missing database query" /* ERR_DB.QUERY */);
536
+ throw (0, util_1.formatError)(item, !name ? "Missing database name" /* ERR_DB.NAME */ : "Missing database table" /* ERR_DB.TABLE */);
537
+ }
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;
514
544
  }
515
- const firebaseApp = product === 'firebase' || isFirebaseApp(credential);
516
- if (cached) {
517
- queryString = query + (firebaseApp ? Module.asString(orderBy, true) : Module.asString(item.params, true) + Module.asString(item.options, true) + limit);
518
- if ((product === 'bigquery' || !update) && (rows = getCache(queryString))) {
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))) {
519
548
  result[i] = rows;
520
549
  continue;
521
550
  }
522
551
  }
523
- if (firebaseApp) {
524
- const { ref, get, child, query: queryDB, update: updateDB } = require('@firebase/database');
525
- const constraints = [];
526
- let limiter = 0;
527
- if ((0, types_1.isArray)(orderBy)) {
528
- const sort = require('@firebase/database');
529
- for (const items of orderBy) {
530
- if (!(0, types_1.isArray)(items)) {
531
- continue;
532
- }
533
- const method = items[0];
534
- switch (method) {
535
- case 'orderByChild':
536
- constraints.push(sort.orderByChild(items[1]));
537
- break;
538
- case 'orderByKey':
539
- case 'orderByValue':
540
- case 'orderByPriority':
541
- constraints.push(sort[method]());
542
- break;
543
- case 'limitToFirst':
544
- case 'limitToLast':
545
- constraints.push(sort[method](limiter = +items[1]));
546
- break;
547
- case 'startAt':
548
- case 'startAfter':
549
- case 'endAt':
550
- case 'endBefore':
551
- case 'equalTo':
552
- constraints.push(sort[method](items[1], items[2]));
553
- break;
554
- }
555
- }
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);
556
557
  }
557
- const db = createClient(item, 0 /* DB.FIREBASE */);
558
- if ((0, types_1.isPlainObject)(update)) {
559
- await updateDB(ref(db, query), update);
558
+ if (Array.isArray(columns)) {
559
+ const [data] = await row.get(columns, options);
560
+ rows = [getRowData(id, data)];
560
561
  }
561
- const snapshot = await (constraints.length ? get(queryDB(ref(db, query), ...constraints)) : get(child(ref(db), query)));
562
- if (snapshot.exists()) {
563
- const val = snapshot.val();
564
- const group = limiter === 1 && Object.keys(val);
565
- 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)];
566
565
  }
567
566
  }
568
567
  else {
569
- const { options = {} } = item;
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)) {
570
580
  options.query = query;
571
- options.params || (options.params = item.params);
572
- if (limit > 0) {
573
- options.maxResults = limit;
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);
574
608
  }
575
- const [job] = await createClient(item, 1 /* DB.BIGQUERY */).createQueryJob(options);
576
- [rows] = await job.getQueryResults();
609
+ [job] = await dataset.createQueryJob(options);
610
+ }
611
+ else {
612
+ [job] = await instance.createQueryJob(options);
577
613
  }
614
+ [rows] = await job.getQueryResults();
578
615
  }
579
- else if (product === 'datastore' || !product && !table && (keys || query)) {
616
+ else if (product === 'datastore' || !product && (item.keys || item.kind || query && !table && !id)) {
617
+ const { keys, kind } = item;
580
618
  if (!keys && !Array.isArray(query)) {
581
619
  closeClient();
582
620
  throw (0, util_1.formatError)(item, "Missing database query" /* ERR_DB.QUERY */);
583
621
  }
584
- if (cached && (rows = getCache(queryString = Module.asString(keys || query, true) + Module.asString(item.options, true) + limit))) {
585
- result[i] = rows;
586
- continue;
622
+ if (useCache) {
623
+ queryString = (keys ? Module.asString(keys, true) : Module.asString(query, true) + (kind || '')) + Module.asString(item.options, true) + (limit > 0 ? limit : 0);
624
+ if (!update && (rows = getCache(queryString))) {
625
+ result[i] = rows;
626
+ continue;
627
+ }
587
628
  }
588
629
  const db = createClient(item, 2 /* DB.DATASTORE */);
589
630
  if (keys) {
590
- const items = !Array.isArray(keys) ? db.key(keys) : keys.map((key) => db.key(key));
631
+ const items = !Array.isArray(keys) || !Array.isArray(keys[0]) ? db.key(keys) : keys.map((key) => db.key(key));
632
+ if ((0, types_1.isPlainObject)(update)) {
633
+ await db.save({ key: items, data: update });
634
+ }
591
635
  rows = await db.get(items, item.options);
592
- if (rows.length > limit) {
636
+ if (limit > 0 && rows.length > limit) {
593
637
  rows = rows.slice(0, limit);
594
638
  }
595
639
  }
596
640
  else {
597
- const task = db.createQuery(item.kind);
641
+ const task = db.createQuery(kind);
598
642
  for (const args of query) {
599
643
  if ((0, types_1.isArray)(args)) {
600
644
  const method = args[0];
@@ -616,6 +660,20 @@ async function executeBatchQuery(credential, batch, sessionKey) {
616
660
  if (limit > 0) {
617
661
  task.limit(limit);
618
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
+ }
619
677
  [rows] = await db.runQuery(task, item.options);
620
678
  }
621
679
  }
@@ -624,12 +682,9 @@ async function executeBatchQuery(credential, batch, sessionKey) {
624
682
  closeClient();
625
683
  throw (0, util_1.formatError)(item, "Missing database table" /* ERR_DB.TABLE */);
626
684
  }
627
- if (cached) {
628
- queryString = table + '_';
629
- }
630
685
  if (id) {
631
- if (cached) {
632
- queryString += id;
686
+ if (useCache) {
687
+ queryString = table + '_' + id;
633
688
  if (!update && (rows = getCache(queryString))) {
634
689
  result[i] = rows;
635
690
  continue;
@@ -645,7 +700,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
645
700
  }
646
701
  }
647
702
  else if (Array.isArray(query)) {
648
- if (cached && (rows = getCache(queryString += Module.asString(query, true) + Module.asString(orderBy, true) + limit))) {
703
+ if (useCache && (rows = getCache(queryString = table + '_' + Module.asString(query, true) + Module.asString(orderBy, true) + (limit > 0 ? limit : '')))) {
649
704
  result[i] = rows;
650
705
  continue;
651
706
  }
@@ -775,7 +830,7 @@ async function setPredefinedAcl(target, value, bucket, pathname = '', metadata =
775
830
  }
776
831
  break;
777
832
  default:
778
- return Promise.resolve();
833
+ return;
779
834
  }
780
835
  }
781
836
  }
@@ -819,18 +874,24 @@ function createBucketRequest(credential) {
819
874
  }
820
875
  exports.createBucketRequest = createBucketRequest;
821
876
  function getProjectId(credential) {
822
- try {
823
- let pathname = credential.keyFilename || credential.keyFile;
824
- if (pathname && Module.isPath(pathname = path.resolve(pathname)) || (pathname = process.env.GOOGLE_APPLICATION_CREDENTIALS) && Module.isPath(pathname = path.resolve(pathname))) {
825
- const projectId = require(pathname).project_id;
826
- if ((0, types_1.isString)(projectId)) {
827
- return projectId;
877
+ let pathname = credential.keyFilename || credential.keyFile;
878
+ if (pathname && Module.isPath(pathname = path.resolve(pathname)) || (pathname = process.env.GOOGLE_APPLICATION_CREDENTIALS) && Module.isPath(pathname = path.resolve(pathname))) {
879
+ if (credential.keyFilename) {
880
+ credential.keyFilename = pathname;
881
+ }
882
+ else {
883
+ credential.keyFile = pathname;
884
+ }
885
+ if (!credential.projectId) {
886
+ try {
887
+ const projectId = require(pathname).project_id;
888
+ if ((0, types_1.isString)(projectId)) {
889
+ credential.projectId = projectId;
890
+ }
891
+ }
892
+ catch {
828
893
  }
829
894
  }
830
895
  }
831
- catch {
832
- }
833
- return '';
834
896
  }
835
897
  exports.getProjectId = getProjectId;
836
- exports.CLOUD_UPLOAD_FROMDISK = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/gcp",
3
- "version": "0.5.0",
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.7.0",
24
- "@e-mc/module": "^0.7.0",
25
- "@e-mc/types": "^0.7.0",
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");
@@ -27,7 +26,7 @@ function upload(credential, service = 'gcp') {
27
26
  }
28
27
  callback(err, url);
29
28
  };
30
- const addLog = (err) => err instanceof Error && this.addLog(this.statusType.WARN, err.message, service + ': ' + bucket);
29
+ const addLog = (err) => err instanceof Error && this.addLog(this.statusType.WARN, err, service, bucket);
31
30
  if (firebase) {
32
31
  const { ref, listAll } = require('@firebase/storage');
33
32
  bucketClient = ref(storage, bucket);
@@ -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
- for (const [content, ext, localFile] of fileGroup) {
94
- Key.push(ext === '.map' && localFile ? path.basename(localFile) : filename + ext);
95
- Body.push(content);
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.destination = pathname ? Module.joinPath(pathname, path.basename(outputUri)) : undefined;
213
- bucketClient.upload(outputUri, params, (err, file) => {
214
- if (file) {
215
- const url = getURL(file.name);
216
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, "Upload success" /* VAL_CLOUD.UPLOAD_FILE */, url, { ...Cloud.LOG_CLOUD_UPLOAD });
217
- if (first) {
218
- uploadResponse(null, url);
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
- else {
228
- addLog(err);
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
- if (tempDir) {
231
- queueMicrotask(() => Module.removeDir(tempDir));
182
+ })
183
+ .catch(err => {
184
+ if (first) {
185
+ uploadResponse(err);
232
186
  }
233
187
  });
234
188
  }