@pi-r/gcp 0.6.0 → 0.6.2

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/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  ### @pi-r/gcp
2
2
 
3
+ https://e-mc.readthedocs.io/en/latest/cloud/gcp.html
4
+
3
5
  ### LICENSE
4
6
 
5
7
  MIT
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");
@@ -106,18 +106,18 @@ 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';
109
- const hasKeyFile = (credential) => !!(credential.keyFile || credential.keyFilename || process.env.GOOGLE_APPLICATION_CREDENTIALS);
109
+ const hasKeyFile = (credential) => (0, types_1.isString)(credential.keyFilename || credential.keyFile || process.env.GOOGLE_APPLICATION_CREDENTIALS) || (0, types_1.isObject)(credential.credentials);
110
110
  const isErrorConflict = (err) => (err instanceof Error) && err.code === 409;
111
111
  function isFirebaseApp(credential, store, admin) {
112
112
  return !!(credential.apiKey && credential.authDomain && (store || credential.databaseURL) || credential.product === 'firebase' && process.env.GOOGLE_APPLICATION_CREDENTIALS) && (!admin || !credential.admin);
113
113
  }
114
114
  exports.isFirebaseApp = isFirebaseApp;
115
115
  function validateStorage(credential) {
116
- return hasKeyFile(credential) || isFirebaseApp(credential, true);
116
+ return hasKeyFile(credential) || !!credential.authClient || isFirebaseApp(credential, true);
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) || !!credential.authClient) && !!(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 && 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
  }
@@ -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, 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, limit = 0, update, ignoreCache } = item;
411
+ const useCache = caching && ignoreCache !== true;
413
412
  const getCache = (value) => {
414
413
  if (ignoreCache !== 1) {
415
- cacheValue.renewCache = 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 === 'bigtable' || !product && name && table && !database) {
421
- const { columns, options = {} } = item;
422
- if ((0, types_1.isObject)(query)) {
423
- options.filter = query;
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
- const db = createClient(item, 4 /* DB.BIGTABLE */).instance(name).table(table);
433
- if (id) {
434
- const row = db.row(id);
435
- if (Array.isArray(columns)) {
436
- const [data] = await row.get(columns, options);
437
- rows = [getRowData(id, data)];
425
+ const orderBy = item.orderBy;
426
+ if (useCache) {
427
+ queryString = query + Module.asString(orderBy, true);
428
+ if (!update && (rows = getCache(queryString))) {
429
+ result[i] = rows;
430
+ continue;
438
431
  }
439
- else {
440
- const [response] = await row.get(options);
441
- rows = [getRowData(response.id, response.data)];
432
+ }
433
+ const { ref, get, child, query: queryDB, update: updateDB } = require('@firebase/database');
434
+ const constraints = [];
435
+ let limiter = 0;
436
+ if ((0, types_1.isArray)(orderBy)) {
437
+ const sort = require('@firebase/database');
438
+ for (const items of orderBy) {
439
+ if ((0, types_1.isArray)(items)) {
440
+ const method = items[0];
441
+ switch (method) {
442
+ case 'orderByChild':
443
+ constraints.push(sort.orderByChild(items[1]));
444
+ break;
445
+ case 'orderByKey':
446
+ case 'orderByValue':
447
+ case 'orderByPriority':
448
+ constraints.push(sort[method]());
449
+ break;
450
+ case 'limitToFirst':
451
+ case 'limitToLast':
452
+ constraints.push(sort[method](limiter = +items[1]));
453
+ break;
454
+ case 'startAt':
455
+ case 'startAfter':
456
+ case 'endAt':
457
+ case 'endBefore':
458
+ case 'equalTo':
459
+ constraints.push(sort[method](items[1], items[2]));
460
+ break;
461
+ }
462
+ }
442
463
  }
443
464
  }
444
- else {
445
- rows = [];
446
- await new Promise((resolve, reject) => {
447
- db.createReadStream(options)
448
- .on('data', row => rows.push(getRowData(row.id, row.data)))
449
- .on('error', reject)
450
- .on('end', resolve);
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 (cached) {
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;
@@ -474,9 +496,11 @@ async function executeBatchQuery(credential, batch, sessionKey) {
474
496
  if ((0, types_1.isPlainObject)(update)) {
475
497
  await db.table(table)[updateType === 1 ? 'insert' : updateType === 2 ? 'replace' : 'update'](update, options.tableUpdate);
476
498
  }
477
- const request = (0, types_1.isPlainObject)(query) ? query : { columns };
499
+ const request = (0, types_1.isPlainObject)(query) ? query : {};
478
500
  request.json = true;
479
- request.columns || (request.columns = item.columns);
501
+ if ((0, types_1.isObject)(columns)) {
502
+ request.columns = 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 === 'firebase' || product === 'bigquery' || !product && !table && typeof query === 'string') {
510
- if (!(0, types_1.isString)(query)) {
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 query" /* ERR_DB.QUERY */);
536
+ throw (0, util_1.formatError)(item, !name ? "Missing database name" /* ERR_DB.NAME */ : "Missing database table" /* ERR_DB.TABLE */);
513
537
  }
514
- const firebaseApp = product === 'firebase' || isFirebaseApp(credential);
515
- if (cached) {
516
- queryString = query + (firebaseApp ? Module.asString(orderBy, true) : Module.asString(item.params, true) + Module.asString(item.options, true) + limit);
517
- if ((product === 'bigquery' || !update) && (rows = getCache(queryString))) {
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 + '_' + ((0, types_1.isString)(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
- if (firebaseApp) {
523
- const { ref, get, child, query: queryDB, update: updateDB } = require('@firebase/database');
524
- const constraints = [];
525
- let limiter = 0;
526
- if ((0, types_1.isArray)(orderBy)) {
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 ((0, types_1.isString)(id)) {
554
+ const row = db.row(id);
555
+ if (update) {
556
+ await row.save(update);
555
557
  }
556
- const db = createClient(item, 0 /* DB.FIREBASE */);
557
- if ((0, types_1.isPlainObject)(update)) {
558
- await updateDB(ref(db, query), update);
558
+ if (Array.isArray(columns)) {
559
+ const [data] = await row.get(columns, options);
560
+ rows = [getRowData(id, data)];
559
561
  }
560
- const snapshot = await (constraints.length ? get(queryDB(ref(db, query), ...constraints)) : get(child(ref(db), query)));
561
- if (snapshot.exists()) {
562
- const val = snapshot.val();
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
- 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)) {
569
580
  options.query = query;
570
- options.params || (options.params = item.params);
571
- if (limit > 0) {
572
- 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;
573
593
  }
574
- const [job] = await createClient(item, 1 /* DB.BIGQUERY */).createQueryJob(options);
575
- [rows] = await job.getQueryResults();
576
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);
608
+ }
609
+ [job] = await dataset.createQueryJob(options);
610
+ }
611
+ else {
612
+ [job] = await instance.createQueryJob(options);
613
+ }
614
+ [rows] = await job.getQueryResults();
577
615
  }
578
- else if (product === 'datastore' || !product && !table && (keys || query)) {
579
- if (!keys && !Array.isArray(query)) {
616
+ else if (product === 'datastore' || !product && (item.keys || item.kind || query && !table && !id)) {
617
+ const { keys, kind } = item;
618
+ if (!keys && !kind && !Array.isArray(query)) {
580
619
  closeClient();
581
620
  throw (0, util_1.formatError)(item, "Missing database query" /* ERR_DB.QUERY */);
582
621
  }
583
- if (cached) {
584
- queryString = Module.asString(keys || query, true) + Module.asString(item.options, true) + limit;
622
+ if (useCache) {
623
+ queryString = (keys ? Cloud.asString(keys, true) : (kind ? (name ? name + '_' : '') + Cloud.asString(kind, true) : '') + (limit > 0 ? '_' + limit : 0) + Cloud.asString(query, true)) + Cloud.asString(item.options, true);
585
624
  if (!update && (rows = getCache(queryString))) {
586
625
  result[i] = rows;
587
626
  continue;
@@ -594,12 +633,37 @@ 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(item.kind);
641
+ const task = name && kind ? db.createQuery(name, kind) : db.createQuery(kind);
642
+ if ((0, types_1.isArray)(query)) {
643
+ for (const args of query) {
644
+ if ((0, types_1.isArray)(args)) {
645
+ const method = args[0];
646
+ switch (method) {
647
+ case 'end':
648
+ case 'filter':
649
+ case 'groupBy':
650
+ case 'limit':
651
+ case 'offset':
652
+ case 'order':
653
+ case 'select':
654
+ case 'start':
655
+ task[method].apply(task, args.slice(1)); // eslint-disable-line prefer-spread
656
+ break;
657
+ case 'hasAncestor':
658
+ task.hasAncestor(db.key(args[1]));
659
+ break;
660
+ }
661
+ }
662
+ }
663
+ }
664
+ if (limit > 0) {
665
+ task.limit(limit);
666
+ }
603
667
  if ((0, types_1.isPlainObject)(update)) {
604
668
  await new Promise((resolve, reject) => {
605
669
  db.runQuery(task.select('__key__'), item.options).then(entities => {
@@ -614,27 +678,6 @@ async function executeBatchQuery(credential, batch, sessionKey) {
614
678
  .catch(err => reject(err));
615
679
  });
616
680
  }
617
- for (const args of query) {
618
- if ((0, types_1.isArray)(args)) {
619
- const method = args[0];
620
- switch (method) {
621
- case 'end':
622
- case 'filter':
623
- case 'groupBy':
624
- case 'hasAncestor':
625
- case 'limit':
626
- case 'offset':
627
- case 'order':
628
- case 'select':
629
- case 'start':
630
- task[method].apply(task, args.slice(1)); // eslint-disable-line prefer-spread
631
- break;
632
- }
633
- }
634
- }
635
- if (limit > 0) {
636
- task.limit(limit);
637
- }
638
681
  [rows] = await db.runQuery(task, item.options);
639
682
  }
640
683
  }
@@ -643,28 +686,36 @@ async function executeBatchQuery(credential, batch, sessionKey) {
643
686
  closeClient();
644
687
  throw (0, util_1.formatError)(item, "Missing database table" /* ERR_DB.TABLE */);
645
688
  }
646
- if (cached) {
647
- queryString = table + '_';
648
- }
649
689
  if (id) {
650
- if (cached) {
651
- queryString += id;
652
- if (!update && (rows = getCache(queryString))) {
690
+ if (useCache) {
691
+ queryString = table + '_' + id;
692
+ if ((!update || Array.isArray(id)) && (rows = getCache(queryString))) {
653
693
  result[i] = rows;
654
694
  continue;
655
695
  }
656
696
  }
657
- const db = createClient(item, 3 /* DB.FIRESTORE */).collection(table).doc(id);
658
- if ((0, types_1.isPlainObject)(update)) {
659
- await db.update(update, { exists: true });
697
+ const db = createClient(item, 3 /* DB.FIRESTORE */);
698
+ if (Array.isArray(id)) {
699
+ const items = id.map(ref => db.doc(table + '/' + ref));
700
+ if ((0, types_1.isPlainObject)(item.options)) {
701
+ items.push(item.options);
702
+ }
703
+ rows = await db.getAll(...items);
660
704
  }
661
- const data = (await db.get()).data();
662
- if (data) {
663
- rows = [data];
705
+ else {
706
+ const doc = db.collection(table).doc(id);
707
+ if ((0, types_1.isPlainObject)(update)) {
708
+ await doc.update(update, { exists: true });
709
+ }
710
+ const data = (await doc.get()).data();
711
+ if (data) {
712
+ rows = [data];
713
+ }
664
714
  }
665
715
  }
666
716
  else if (Array.isArray(query)) {
667
- if (cached && (rows = getCache(queryString += Module.asString(query, true) + Module.asString(orderBy, true) + limit))) {
717
+ const orderBy = item.orderBy;
718
+ if (useCache && (rows = getCache(queryString = table + '_' + Module.asString(query, true) + Module.asString(orderBy, true) + (limit > 0 ? limit : '')))) {
668
719
  result[i] = rows;
669
720
  continue;
670
721
  }
@@ -857,6 +908,6 @@ function getProjectId(credential) {
857
908
  }
858
909
  }
859
910
  }
911
+ return credential.projectId;
860
912
  }
861
913
  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.0",
3
+ "version": "0.6.2",
4
4
  "description": "Google (GCP) cloud functions for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "publishConfig": {
@@ -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.8.0",
24
- "@e-mc/module": "^0.8.0",
25
- "@e-mc/types": "^0.8.0",
26
- "@google-cloud/firestore": "^7.1.0",
23
+ "@e-mc/cloud": "^0.8.2",
24
+ "@e-mc/module": "^0.8.2",
25
+ "@e-mc/types": "^0.8.2",
26
+ "@google-cloud/firestore": "^7.2.0",
27
27
  "@google-cloud/storage": "^7.7.0"
28
28
  }
29
29
  }
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
- 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,27 +168,25 @@ 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
- }
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);
178
+ }
179
+ if (ACL) {
180
+ client_1.setPredefinedAcl.call(this, file.acl, ACL, file.bucket, file.name, revoke || (await file.getMetadata())[0], true);
223
181
  }
224
- else if (first) {
182
+ })
183
+ .catch(err => {
184
+ if (first) {
225
185
  uploadResponse(err);
226
186
  }
227
187
  else {
228
188
  addLog(err);
229
189
  }
230
- if (tempDir) {
231
- queueMicrotask(() => Module.removeDir(tempDir));
232
- }
233
190
  });
234
191
  }
235
192
  }