@pi-r/gcp 0.6.3 → 0.6.6

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 CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2023 An Pham
1
+ Copyright 2024 An Pham
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
@@ -1,7 +1,10 @@
1
- ### @pi-r/gcp
1
+ # @pi-r/gcp
2
2
 
3
- https://e-mc.readthedocs.io/en/latest/cloud/gcp.html
3
+ ## Documentation
4
4
 
5
- ### LICENSE
5
+ - [E-mc](https://e-mc.readthedocs.io/en/latest/cloud/gcp.html)
6
+ - [squared](https://squared.readthedocs.io)
7
+
8
+ ## LICENSE
6
9
 
7
10
  MIT
package/client/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
2
  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
3
  const path = require("path");
5
4
  const storage = require("@google-cloud/storage");
@@ -194,7 +193,7 @@ async function createBucketV2(credential, bucket, acl, options, service = 'gcp')
194
193
  }
195
194
  return instance.createBucket(bucket, options)
196
195
  .then(async ([target]) => {
197
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Bucket created" /* VAL_CLOUD.CREATE_BUCKET */, bucket], '', { ...Cloud.LOG_CLOUD_COMMAND });
196
+ this.formatMessage(64, service, ["Bucket created", bucket], '', { ...Cloud.LOG_CLOUD_COMMAND });
198
197
  if (acl) {
199
198
  await setBucketPolicy.call(this, credential, bucket, { acl, bucket: target }, service);
200
199
  }
@@ -204,7 +203,7 @@ async function createBucketV2(credential, bucket, acl, options, service = 'gcp')
204
203
  if (isErrorConflict(err)) {
205
204
  return acl ? await setBucketPolicy.call(this, credential, bucket, { acl }, service) : true;
206
205
  }
207
- this.formatFail(64 /* LOG_TYPE.CLOUD */, service, ["Unable to create bucket" /* ERR_CLOUD.CREATE_BUCKET */, bucket], err, { ...Cloud.LOG_CLOUD_FAIL });
206
+ this.formatFail(64, service, ["Unable to create bucket", bucket], err, { ...Cloud.LOG_CLOUD_FAIL });
208
207
  return false;
209
208
  });
210
209
  }
@@ -215,7 +214,7 @@ async function setBucketPolicy(credential, bucket, options, service = 'gcp') {
215
214
  }
216
215
  const client = createStorageClient.call(this, credential, bucket).bucket(bucket);
217
216
  if (!await hasBucket(client)) {
218
- this.formatFail(64 /* LOG_TYPE.CLOUD */, service, ["Bucket does not exist" /* ERR_CLOUD.EXISTS_BUCKET */, bucket], (0, types_1.errorMessage)(service, "Bucket does not exist" /* ERR_CLOUD.EXISTS_BUCKET */), { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
217
+ 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
218
  return false;
220
219
  }
221
220
  const errors = [];
@@ -256,10 +255,10 @@ async function setBucketPolicy(credential, bucket, options, service = 'gcp') {
256
255
  errors.push(...await updatePredefinedAcl(client.acl, options.acl, options.metadata));
257
256
  }
258
257
  if (errors.length) {
259
- this.formatFail(64 /* LOG_TYPE.CLOUD */, service, ["Unable to update bucket policy" /* ERR_CLOUD.POLICY_BUCKET */, bucket], errors[0], { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
258
+ this.formatFail(64, service, ["Unable to update bucket policy", bucket], errors[0], { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
260
259
  return false;
261
260
  }
262
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Bucket ACL configured" /* VAL_CLOUD.ACL_BUCKET */, bucket], (0, types_1.isString)(acl) ? acl : '', { ...Cloud.LOG_CLOUD_COMMAND });
261
+ this.formatMessage(64, service, ["Bucket ACL configured", bucket], (0, types_1.isString)(acl) ? acl : '', { ...Cloud.LOG_CLOUD_COMMAND });
263
262
  return true;
264
263
  }
265
264
  exports.setBucketPolicy = setBucketPolicy;
@@ -277,11 +276,11 @@ async function setBucketWebsite(credential, bucket, options, service = 'gcp') {
277
276
  }
278
277
  return createStorageClient.call(this, credential, bucket).bucket(bucket).setMetadata({ website })
279
278
  .then(() => {
280
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Bucket configured" /* VAL_CLOUD.CONFIGURE_BUCKET */, bucket], website, { ...Cloud.LOG_CLOUD_COMMAND });
279
+ this.formatMessage(64, service, ["Bucket configured", bucket], website, { ...Cloud.LOG_CLOUD_COMMAND });
281
280
  return true;
282
281
  })
283
282
  .catch(err => {
284
- this.formatFail(64 /* LOG_TYPE.CLOUD */, service, ["Unable to configure bucket" /* ERR_CLOUD.CONFIGURE_BUCKET */, bucket], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
283
+ this.formatFail(64, service, ["Unable to configure bucket", bucket], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
285
284
  return false;
286
285
  });
287
286
  }
@@ -321,14 +320,14 @@ async function deleteObjectsV2(credential, bucket, recursive = true, service = '
321
320
  const client = instance.bucket(bucket);
322
321
  if (await hasBucket(client)) {
323
322
  if (recursive) {
324
- return client.deleteFiles({ force: true }).then(() => this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, "Bucket emptied" /* VAL_CLOUD.EMPTY_BUCKET */ + ' (recursive)', bucket, { ...Cloud.LOG_CLOUD_COMMAND }));
323
+ return client.deleteFiles({ force: true }).then(() => this.formatMessage(64, service, "Bucket emptied" + ' (recursive)', bucket, { ...Cloud.LOG_CLOUD_COMMAND }));
325
324
  }
326
325
  const tasks = [];
327
326
  try {
328
327
  const result = await client.getFiles();
329
328
  for (const { name } of result[0]) {
330
329
  if (!name.includes('/')) {
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 })));
330
+ 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
331
  }
333
332
  }
334
333
  }
@@ -342,7 +341,7 @@ async function deleteObjectsV2(credential, bucket, recursive = true, service = '
342
341
  }
343
342
  const checkErrors = () => {
344
343
  if (errors.length) {
345
- this.formatFail(64 /* LOG_TYPE.CLOUD */, service, ["Unable to empty bucket" /* ERR_CLOUD.DELETE_BUCKET */ + (recursive ? ' (recursive)' : ''), errors.length + ' errors'], errors[0], { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
344
+ this.formatFail(64, service, ["Unable to empty bucket" + (recursive ? ' (recursive)' : ''), errors.length + ' errors'], errors[0], { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
346
345
  return true;
347
346
  }
348
347
  return false;
@@ -352,7 +351,7 @@ async function deleteObjectsV2(credential, bucket, recursive = true, service = '
352
351
  errors.push(...result.filter(item => item.status === 'rejected').map((item) => item.reason));
353
352
  if (!checkErrors()) {
354
353
  const files = result.length + ' files';
355
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Bucket emptied" /* VAL_CLOUD.EMPTY_BUCKET */ + ` (${recursive ? 'recursive' : files})`, bucket], recursive ? files : '', { ...Cloud.LOG_CLOUD_COMMAND });
354
+ this.formatMessage(64, service, ["Bucket emptied" + ` (${recursive ? 'recursive' : files})`, bucket], recursive ? files : '', { ...Cloud.LOG_CLOUD_COMMAND });
356
355
  }
357
356
  })
358
357
  .catch(err => {
@@ -383,7 +382,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
383
382
  return (clients[type] = createDatabaseClient.call(this, item, data));
384
383
  };
385
384
  const closeClient = () => {
386
- const fs = clients[3 /* DB.FIRESTORE */];
385
+ const fs = clients[3];
387
386
  fs?.terminate();
388
387
  };
389
388
  const getFamily = (data, row, family) => {
@@ -420,7 +419,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
420
419
  if (product === 'firebase' || isFirebaseApp(credential)) {
421
420
  if (!(0, types_1.isString)(query)) {
422
421
  closeClient();
423
- throw (0, util_1.formatError)(item, "Missing database query" /* ERR_DB.QUERY */);
422
+ throw (0, util_1.formatError)(item, "Missing database query");
424
423
  }
425
424
  const orderBy = item.orderBy;
426
425
  if (useCache) {
@@ -462,7 +461,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
462
461
  }
463
462
  }
464
463
  }
465
- const db = createClient(item, 0 /* DB.FIREBASE */);
464
+ const db = createClient(item, 0);
466
465
  if ((0, types_1.isPlainObject)(update)) {
467
466
  await updateDB(ref(db, query), update);
468
467
  }
@@ -477,11 +476,11 @@ async function executeBatchQuery(credential, batch, sessionKey) {
477
476
  const columns = (0, types_1.isPlainObject)(query) && query.columns || item.columns;
478
477
  if (!(query || table && columns)) {
479
478
  closeClient();
480
- throw (0, util_1.formatError)(item, "Missing database query" /* ERR_DB.QUERY */);
479
+ throw (0, util_1.formatError)(item, "Missing database query");
481
480
  }
482
481
  if (!name || !database) {
483
482
  closeClient();
484
- throw (0, util_1.formatError)(item, !name ? "Missing instance name" /* ERR_DB.INSTANCE */ : "Missing database name" /* ERR_DB.NAME */);
483
+ throw (0, util_1.formatError)(item, !name ? "Missing instance name" : "Missing database name");
485
484
  }
486
485
  const { updateType, options = {} } = item;
487
486
  if (useCache) {
@@ -491,7 +490,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
491
490
  continue;
492
491
  }
493
492
  }
494
- const db = createClient(item, 5 /* DB.SPANNER */).instance(name).database(database, options.databasePool, options.databaseQuery);
493
+ const db = createClient(item, 5).instance(name).database(database, options.databasePool, options.databaseQuery);
495
494
  if (table) {
496
495
  if ((0, types_1.isPlainObject)(update)) {
497
496
  await db.table(table)[updateType === 1 ? 'insert' : updateType === 2 ? 'replace' : 'update'](update, options.tableUpdate);
@@ -512,7 +511,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
512
511
  await new Promise((resolve, reject) => {
513
512
  db.runTransaction((err, transaction) => {
514
513
  if (err || !transaction) {
515
- reject(err || new Error("Unknown" /* ERR_MESSAGE.UNKNOWN */));
514
+ reject(err || new Error("Unknown"));
516
515
  }
517
516
  else {
518
517
  transaction.runUpdate(update)
@@ -533,7 +532,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
533
532
  else if (product === 'bigtable' || !product && name && table && !database) {
534
533
  if (!name || !table) {
535
534
  closeClient();
536
- throw (0, util_1.formatError)(item, !name ? "Missing database name" /* ERR_DB.NAME */ : "Missing database table" /* ERR_DB.TABLE */);
535
+ throw (0, util_1.formatError)(item, !name ? "Missing database name" : "Missing database table");
537
536
  }
538
537
  const { columns, options = {} } = item;
539
538
  if ((0, types_1.isObject)(query)) {
@@ -549,7 +548,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
549
548
  continue;
550
549
  }
551
550
  }
552
- const db = createClient(item, 4 /* DB.BIGTABLE */).instance(name).table(table);
551
+ const db = createClient(item, 4).instance(name).table(table);
553
552
  if ((0, types_1.isString)(id)) {
554
553
  const row = db.row(id);
555
554
  if (update) {
@@ -592,7 +591,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
592
591
  continue;
593
592
  }
594
593
  }
595
- const instance = createClient(item, 1 /* DB.BIGQUERY */);
594
+ const instance = createClient(item, 1);
596
595
  let job;
597
596
  if (database) {
598
597
  const dataset = instance.dataset(database);
@@ -617,7 +616,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
617
616
  const { keys, kind } = item;
618
617
  if (!keys && !kind && !Array.isArray(query)) {
619
618
  closeClient();
620
- throw (0, util_1.formatError)(item, "Missing database query" /* ERR_DB.QUERY */);
619
+ throw (0, util_1.formatError)(item, "Missing database query");
621
620
  }
622
621
  if (useCache) {
623
622
  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);
@@ -626,7 +625,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
626
625
  continue;
627
626
  }
628
627
  }
629
- const db = createClient(item, 2 /* DB.DATASTORE */);
628
+ const db = createClient(item, 2);
630
629
  if (keys) {
631
630
  const items = !Array.isArray(keys) || !Array.isArray(keys[0]) ? db.key(keys) : keys.map((key) => db.key(key));
632
631
  if ((0, types_1.isPlainObject)(update)) {
@@ -652,7 +651,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
652
651
  case 'order':
653
652
  case 'select':
654
653
  case 'start':
655
- task[method].apply(task, args.slice(1)); // eslint-disable-line prefer-spread
654
+ task[method].apply(task, args.slice(1));
656
655
  break;
657
656
  case 'hasAncestor':
658
657
  task.hasAncestor(db.key(args[1]));
@@ -684,7 +683,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
684
683
  else if (product === 'firestore' || !product && table) {
685
684
  if (!table) {
686
685
  closeClient();
687
- throw (0, util_1.formatError)(item, "Missing database table" /* ERR_DB.TABLE */);
686
+ throw (0, util_1.formatError)(item, "Missing database table");
688
687
  }
689
688
  if (id) {
690
689
  if (useCache) {
@@ -694,7 +693,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
694
693
  continue;
695
694
  }
696
695
  }
697
- const db = createClient(item, 3 /* DB.FIRESTORE */);
696
+ const db = createClient(item, 3);
698
697
  if (Array.isArray(id)) {
699
698
  const items = id.map(ref => db.doc(table + '/' + ref));
700
699
  if ((0, types_1.isPlainObject)(item.options)) {
@@ -719,7 +718,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
719
718
  result[i] = rows;
720
719
  continue;
721
720
  }
722
- let db = createClient(item, 3 /* DB.FIRESTORE */).collection(table);
721
+ let db = createClient(item, 3).collection(table);
723
722
  for (const args of query) {
724
723
  if ((0, types_1.isArray)(args)) {
725
724
  const method = args[0];
@@ -745,7 +744,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
745
744
  case 'startAt':
746
745
  case 'where':
747
746
  case 'withConverter':
748
- db = db[method].apply(db, params); // eslint-disable-line prefer-spread
747
+ db = db[method].apply(db, params);
749
748
  break;
750
749
  }
751
750
  }
@@ -764,12 +763,12 @@ async function executeBatchQuery(credential, batch, sessionKey) {
764
763
  }
765
764
  else {
766
765
  closeClient();
767
- throw (0, util_1.formatError)(item, "Missing database query" /* ERR_DB.QUERY */);
766
+ throw (0, util_1.formatError)(item, "Missing database query");
768
767
  }
769
768
  }
770
769
  else {
771
770
  closeClient();
772
- throw (0, util_1.formatError)(item, "Missing database product" /* ERR_DB.PRODUCT */);
771
+ throw (0, util_1.formatError)(item, "Missing database product");
773
772
  }
774
773
  result[i] = this.setQueryResult(service, credential, queryString, rows, cacheValue);
775
774
  }
@@ -781,12 +780,12 @@ function setPublicRead(acl, pathname, requested, service = 'gcp') {
781
780
  acl.add({ entity: 'allUsers', role: 'READER' })
782
781
  .then(() => {
783
782
  if (requested) {
784
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, "Grant public-read" /* VAL_CLOUD.PUBLIC_READ */, pathname, { ...Cloud.LOG_CLOUD_COMMAND });
783
+ this.formatMessage(64, service, "Grant public-read", pathname, { ...Cloud.LOG_CLOUD_COMMAND });
785
784
  }
786
785
  })
787
786
  .catch(err => {
788
787
  if (requested) {
789
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Unable to grant public-read" /* ERR_CLOUD.PUBLIC_READ */, pathname], err, { ...Cloud.LOG_CLOUD_WARN });
788
+ this.formatMessage(64, service, ["Unable to grant public-read", pathname], err, { ...Cloud.LOG_CLOUD_WARN });
790
789
  }
791
790
  });
792
791
  }
@@ -861,10 +860,10 @@ async function setPredefinedAcl(target, value, bucket, pathname = '', metadata =
861
860
  }
862
861
  return updatePredefinedAcl(target, items, metadata).then(errors => {
863
862
  if (errors.length) {
864
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, [`Unable to ${revoke ? 'revoke' : 'grant'} ` + value, bucket && pathname ? bucket.name + '/' + pathname : bucket ? bucket.name : pathname], errors[0], { ...Cloud.LOG_CLOUD_WARN });
863
+ 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 });
865
864
  }
866
865
  else if (requested && (bucket || pathname)) {
867
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, [(revoke ? 'Revoke' : 'Grant') + ' ' + value, bucket?.name], pathname, { ...Cloud.LOG_CLOUD_COMMAND });
866
+ this.formatMessage(64, service, [(revoke ? 'Revoke' : 'Grant') + ' ' + value, bucket?.name], pathname, { ...Cloud.LOG_CLOUD_COMMAND });
868
867
  }
869
868
  });
870
869
  }
package/download/index.js CHANGED
@@ -1,13 +1,12 @@
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("../client");
10
- function download(credential, service = 'gcp') {
8
+ const client_1 = require("@pi-r/gcp");
9
+ module.exports = function download(credential, service = 'gcp') {
11
10
  const storage = client_1.createStorageClient.call(this, credential);
12
11
  return (data, callback) => {
13
12
  const { bucket: Bucket, download: target } = data;
@@ -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" /* ERR_CLOUD.TEMP_DIR */));
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 /* LOG_TYPE.CLOUD */, service, "Delete success" /* VAL_CLOUD.DELETE_FILE */, location, { ...Cloud.LOG_CLOUD_DELETE });
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 /* LOG_TYPE.CLOUD */, service, ["Delete failed" /* ERR_CLOUD.DELETE_FAIL */, location], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: !!target.active }));
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 /* LOG_TYPE.CLOUD */, service, ["Delete failed" /* ERR_CLOUD.DELETE_FAIL */, location], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: !!target.active });
71
+ this.formatFail(64, service, ["Delete failed", location], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: !!target.active });
73
72
  }
74
73
  });
75
74
  }
@@ -77,6 +76,4 @@ function download(credential, service = 'gcp') {
77
76
  .catch(err => downloadResponse(err));
78
77
  }
79
78
  };
80
- }
81
-
82
- module.exports = download;
79
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/gcp",
3
- "version": "0.6.3",
3
+ "version": "0.6.6",
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.3",
24
- "@e-mc/module": "^0.8.3",
25
- "@e-mc/types": "^0.8.3",
26
- "@google-cloud/firestore": "^7.2.0",
27
- "@google-cloud/storage": "^7.7.0"
23
+ "@e-mc/cloud": "^0.8.7",
24
+ "@e-mc/module": "^0.8.7",
25
+ "@e-mc/types": "^0.8.7",
26
+ "@google-cloud/firestore": "^7.6.0",
27
+ "@google-cloud/storage": "^7.9.0"
28
28
  }
29
29
  }
package/upload/index.js CHANGED
@@ -1,15 +1,14 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
2
  const path = require("path");
4
3
  const util_1 = require("@e-mc/cloud/util");
5
4
  const types_1 = require("@e-mc/types");
6
5
  const Module = require("@e-mc/module");
7
6
  const Cloud = require("@e-mc/cloud");
8
- const client_1 = require("../client");
7
+ const client_1 = require("@pi-r/gcp");
9
8
  const BUCKET_SESSION = new Set();
10
9
  const BUCKET_RESPONSE = {};
11
10
  const getBucketKey = (credential, bucket, acl = '') => Module.asString(credential, true) + bucket + '_' + acl;
12
- function upload(credential, service = 'gcp') {
11
+ module.exports = function upload(credential, service = 'gcp') {
13
12
  const storage = client_1.createStorageClient.call(this, credential);
14
13
  return async (data, callback) => {
15
14
  var _a;
@@ -56,7 +55,7 @@ function upload(credential, service = 'gcp') {
56
55
  if (typeof retentionPolicy === 'number') {
57
56
  bucketClient.setRetentionPeriod(retentionPolicy)
58
57
  .then(([response]) => {
59
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Bucket configured" /* VAL_CLOUD.CONFIGURE_BUCKET */ + ' (retention policy)', bucket], response.retentionPolicy?.effectiveTime, { ...Cloud.LOG_CLOUD_COMMAND });
58
+ this.formatMessage(64, service, ["Bucket configured" + ' (retention policy)', bucket], response.retentionPolicy?.effectiveTime, { ...Cloud.LOG_CLOUD_COMMAND });
60
59
  })
61
60
  .catch(err => addLog(err));
62
61
  }
@@ -81,10 +80,10 @@ function upload(credential, service = 'gcp') {
81
80
  }
82
81
  } while (exists && ++i);
83
82
  if (i > 0) {
84
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["File renamed" /* VAL_CLOUD.RENAME_FILE */, current], filename, { ...Cloud.LOG_CLOUD_WARN });
83
+ this.formatMessage(64, service, ["File renamed", current], filename, { ...Cloud.LOG_CLOUD_WARN });
85
84
  }
86
85
  }
87
- const getURL = (value) => endpoint ? Module.joinPath(endpoint, value) : Module.joinPath("https://storage.googleapis.com" /* STRINGS.BASE_URL */, bucket, value);
86
+ const getURL = (value) => endpoint ? Module.joinPath(endpoint, value) : Module.joinPath("https://storage.googleapis.com", bucket, value);
88
87
  const Key = [filename];
89
88
  const Body = [data.buffer];
90
89
  const ContentType = [contentType];
@@ -118,7 +117,7 @@ function upload(credential, service = 'gcp') {
118
117
  uploadBytes(ref(bucketClient, destination), Body[i], params)
119
118
  .then(() => {
120
119
  const url = getURL(destination);
121
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, "Upload success" /* VAL_CLOUD.UPLOAD_FILE */, url, { ...Cloud.LOG_CLOUD_UPLOAD });
120
+ this.formatMessage(64, service, "Upload success", url, { ...Cloud.LOG_CLOUD_UPLOAD });
122
121
  if (first) {
123
122
  uploadResponse(null, url);
124
123
  }
@@ -172,7 +171,7 @@ function upload(credential, service = 'gcp') {
172
171
  const file = bucketClient.file(destination);
173
172
  file.save(Body[i], params).then(async () => {
174
173
  const url = getURL(file.name);
175
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, "Upload success" /* VAL_CLOUD.UPLOAD_FILE */, url, { ...Cloud.LOG_CLOUD_UPLOAD });
174
+ this.formatMessage(64, service, "Upload success", url, { ...Cloud.LOG_CLOUD_UPLOAD });
176
175
  if (first) {
177
176
  uploadResponse(null, url);
178
177
  }
@@ -191,6 +190,4 @@ function upload(credential, service = 'gcp') {
191
190
  }
192
191
  }
193
192
  };
194
- }
195
-
196
- module.exports = upload;
193
+ };