@pi-r/gcp 0.8.2 → 0.9.0

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,7 +1,7 @@
1
- Copyright 2024 An Pham
2
-
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
-
5
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
-
7
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ Copyright 2024 An Pham
2
+
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
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
- ### @pi-r/gcp
2
-
3
- https://e-mc.readthedocs.io/en/latest/cloud/gcp.html
4
-
5
- ### LICENSE
6
-
7
- MIT
1
+ ### @pi-r/gcp
2
+
3
+ https://e-mc.readthedocs.io/en/latest/cloud/gcp.html
4
+
5
+ ### LICENSE
6
+
7
+ MIT
package/client/index.js CHANGED
@@ -12,18 +12,19 @@ exports.setBucketTagging = setBucketTagging;
12
12
  exports.setBucketWebsite = setBucketWebsite;
13
13
  exports.deleteObjects = deleteObjects;
14
14
  exports.deleteObjectsV2 = deleteObjectsV2;
15
+ exports.deleteObjectsV3 = deleteObjectsV3;
15
16
  exports.executeQuery = executeQuery;
16
17
  exports.executeBatchQuery = executeBatchQuery;
17
18
  exports.setPublicRead = setPublicRead;
18
19
  exports.setPredefinedAcl = setPredefinedAcl;
19
20
  exports.createBucketRequest = createBucketRequest;
20
21
  exports.getProjectId = getProjectId;
21
- const path = require("path");
22
+ const path = require("node:path");
22
23
  const firestore_1 = require("@google-cloud/firestore");
23
24
  const storage_1 = require("@google-cloud/storage");
24
25
  const Cloud = require("@e-mc/cloud");
25
- const util_1 = require("@e-mc/cloud/util");
26
26
  const types_1 = require("@e-mc/types");
27
+ const util_1 = require("@e-mc/cloud/util");
27
28
  const FIREBASE_DB = {};
28
29
  const FIREBASE_STORAGE = {};
29
30
  const ADMIN_DB = {};
@@ -316,10 +317,19 @@ async function setBucketWebsite(credential, bucket, options, service = 'gcp') {
316
317
  return false;
317
318
  });
318
319
  }
319
- async function deleteObjects(credential, Bucket, service) {
320
- return deleteObjectsV2.call(this, credential, Bucket, true, service);
320
+ async function deleteObjects(credential, bucket, service, sdk, recursive = true) {
321
+ return deleteObjectsV2.call(this, credential, bucket, recursive, service);
321
322
  }
322
- async function deleteObjectsV2(credential, bucket, recursive = true, service = 'gcp') {
323
+ async function deleteObjectsV2(credential, bucket, recursive = true, service) {
324
+ return deleteObjectsV3.call(this, credential, bucket, { recursive }, service);
325
+ }
326
+ async function deleteObjectsV3(credential, bucket, options = {}, service = 'gcp') {
327
+ options = { ...options };
328
+ let recursive = true;
329
+ if ('recursive' in options) {
330
+ recursive = options.recursive;
331
+ delete options.recursive;
332
+ }
323
333
  const instance = createStorageClient.call(this, credential, bucket);
324
334
  const errors = [];
325
335
  let promise;
@@ -350,13 +360,13 @@ async function deleteObjectsV2(credential, bucket, recursive = true, service = '
350
360
  const client = instance.bucket(bucket);
351
361
  if (await hasBucket(client)) {
352
362
  if (recursive) {
353
- return client.deleteFiles({ force: true }).then(() => {
363
+ return client.deleteFiles({ ...options, force: true }).then(() => {
354
364
  this.formatMessage(64, service, "Bucket emptied" + ' (recursive)', bucket, { ...Cloud.LOG_CLOUD_COMMAND });
355
365
  });
356
366
  }
357
367
  const tasks = [];
358
368
  try {
359
- const result = await client.getFiles();
369
+ const result = await client.getFiles(options);
360
370
  for (const { name } of result[0]) {
361
371
  if (!name.includes('/')) {
362
372
  tasks.push(client.file(name).delete({ ignoreNotFound: true }).catch((err) => {
@@ -922,7 +932,7 @@ function setPublicRead(acl, pathname, requested, service = 'gcp') {
922
932
  });
923
933
  }
924
934
  async function setPredefinedAcl(target, value, bucket, pathname = '', metadata = {}, requested = true, service = 'gcp') {
925
- let revoke;
935
+ let revoke = false;
926
936
  if (metadata === true) {
927
937
  revoke = true;
928
938
  metadata = {};
package/download/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
- const path = require("path");
3
- const fs = require("fs");
4
- const https = require("https");
2
+ const path = require("node:path");
3
+ const fs = require("node:fs");
4
+ const https = require("node:https");
5
5
  const storage_1 = require("@google-cloud/storage");
6
- const types_1 = require("@e-mc/types");
7
6
  const Cloud = require("@e-mc/cloud");
7
+ const types_1 = require("@e-mc/types");
8
8
  const client_1 = require("@pi-r/gcp");
9
9
  function download(credential, service = 'gcp') {
10
10
  const storage = client_1.createStorageClient.call(this, credential);
@@ -15,7 +15,7 @@ function download(credential, service = 'gcp') {
15
15
  const downloadResponse = (err, url = null) => {
16
16
  callback(err, url);
17
17
  if (tempDir) {
18
- queueMicrotask(() => {
18
+ setImmediate(() => {
19
19
  Cloud.removeDir(tempDir);
20
20
  });
21
21
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/gcp",
3
- "version": "0.8.2",
3
+ "version": "0.9.0",
4
4
  "description": "Google (GCP) cloud functions for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "publishConfig": {
@@ -13,17 +13,16 @@
13
13
  },
14
14
  "keywords": [
15
15
  "squared",
16
- "e-mc",
17
- "squared-functions"
16
+ "e-mc"
18
17
  ],
19
18
  "author": "An Pham <anpham6@gmail.com>",
20
19
  "license": "MIT",
21
20
  "homepage": "https://github.com/anpham6/pi-r#readme",
22
21
  "dependencies": {
23
- "@e-mc/cloud": "^0.10.2",
24
- "@e-mc/module": "^0.10.2",
25
- "@e-mc/types": "^0.10.2",
26
- "@google-cloud/firestore": "^7.10.0",
27
- "@google-cloud/storage": "^7.13.0"
22
+ "@e-mc/cloud": "^0.11.0",
23
+ "@e-mc/module": "^0.11.0",
24
+ "@e-mc/types": "^0.11.0",
25
+ "@google-cloud/firestore": "^7.11.0",
26
+ "@google-cloud/storage": "^7.14.0"
28
27
  }
29
28
  }
package/types/index.d.ts CHANGED
@@ -1,36 +1,36 @@
1
- import type { CloudDatabase, CloudStorage } from '@e-mc/types/lib/cloud';
2
-
3
- import type { StorageOptions } from '@google-cloud/storage';
4
- import type { PathType } from '@google-cloud/datastore';
5
- import type { entity } from '@google-cloud/datastore/build/src/entity';
6
- import type { FirebaseOptions as IFirebaseOptions } from '@firebase/app';
7
-
8
- // @ts-ignore
9
- import type { GoogleAuthOptions } from 'google-auth-library';
10
-
11
- export type GCPStorage = CloudStorage<GCPStorageCredential, "gcp | gcloud">;
12
-
13
- export interface GCPStorageCredential extends StorageOptions, IFirebaseOptions {}
14
-
15
- export interface GCPDatabaseQuery<T = PlainObject> extends Omit<CloudDatabase<unknown, T, unknown, string | unknown[] | StandardMap, GCPDatabaseCredential>, "id"> {
16
- source: "cloud";
17
- service: "gcp" | "gcloud";
18
- product?: "firestore" | "firebase" | "bigquery" | "bigtable" | "datastore" | "spanner";
19
- database?: string;
20
- id?: ArrayOf<string>;
21
- updateType?: 0 | 1 | 2 | 3;
22
- columns?: string[];
23
- keys?: ArrayOf<DatastoreKey>;
24
- kind?: ArrayOf<string>;
25
- orderBy?: unknown[][];
26
- }
27
-
28
- export type GCPDatabaseCredential = GoogleAuthOptions;
29
-
30
- export interface FirebaseOptions extends IFirebaseOptions {
31
- product?: "firebase";
32
- admin?: boolean;
33
- }
34
-
35
- export type DatastoreKey = string | PathType[] | entity.KeyOptions;
1
+ import type { CloudDatabase, CloudStorage } from '@e-mc/types/lib/cloud';
2
+
3
+ import type { StorageOptions } from '@google-cloud/storage';
4
+ import type { PathType } from '@google-cloud/datastore';
5
+ import type { entity } from '@google-cloud/datastore/build/src/entity';
6
+ import type { FirebaseOptions as IFirebaseOptions } from '@firebase/app';
7
+
8
+ // @ts-ignore
9
+ import type { GoogleAuthOptions } from 'google-auth-library';
10
+
11
+ export type GCPStorage = CloudStorage<GCPStorageCredential, "gcp | gcloud">;
12
+
13
+ export interface GCPStorageCredential extends StorageOptions, IFirebaseOptions {}
14
+
15
+ export interface GCPDatabaseQuery<T = PlainObject> extends Omit<CloudDatabase<unknown, T, unknown, string | unknown[] | StandardMap, GCPDatabaseCredential>, "id"> {
16
+ source: "cloud";
17
+ service: "gcp" | "gcloud";
18
+ product?: "firestore" | "firebase" | "bigquery" | "bigtable" | "datastore" | "spanner";
19
+ database?: string;
20
+ id?: string | string[];
21
+ updateType?: 0 | 1 | 2 | 3;
22
+ columns?: string[];
23
+ keys?: DatastoreKey | DatastoreKey[];
24
+ kind?: string | string[];
25
+ orderBy?: unknown[][];
26
+ }
27
+
28
+ export type GCPDatabaseCredential = GoogleAuthOptions;
29
+
30
+ export interface FirebaseOptions extends IFirebaseOptions {
31
+ product?: "firebase";
32
+ admin?: boolean;
33
+ }
34
+
35
+ export type DatastoreKey = string | PathType[] | entity.KeyOptions;
36
36
  export type BucketAcl = "authenticatedRead" | "private" | "projectPrivate" | "publicRead" | "publicReadWrite" | "bucketAccessUniform" | "bucketAccessACL";
package/upload/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
- const path = require("path");
3
- const fs = require("fs");
4
- const stream = require("stream");
2
+ const path = require("node:path");
3
+ const fs = require("node:fs");
4
+ const stream = require("node:stream");
5
5
  const Cloud = require("@e-mc/cloud");
6
- const util_1 = require("@e-mc/cloud/util");
7
6
  const types_1 = require("@e-mc/types");
7
+ const util_1 = require("@e-mc/cloud/util");
8
8
  const client_1 = require("@pi-r/gcp");
9
9
  const BUCKET_SESSION = new Set();
10
10
  const BUCKET_RESPONSE = {};
@@ -65,9 +65,7 @@ function upload(credential, service = 'gcp') {
65
65
  .then(([response]) => {
66
66
  commandMessage('Retention Policy', response.retentionPolicy?.effectiveTime);
67
67
  })
68
- .catch((err) => {
69
- addLog(err);
70
- });
68
+ .catch(addLog);
71
69
  }
72
70
  if ((0, types_1.isObject)(cors)) {
73
71
  bucketClient.setCorsConfiguration(Array.isArray(cors) ? cors : [cors])
@@ -75,12 +73,10 @@ function upload(credential, service = 'gcp') {
75
73
  const length = response.cors?.length;
76
74
  commandMessage('CORS', length ? length + ' rules' : null);
77
75
  })
78
- .catch((err) => {
79
- addLog(err);
80
- });
76
+ .catch(addLog);
81
77
  }
82
78
  if ((0, types_1.isObject)(lifecycle)) {
83
- let append;
79
+ let append = false;
84
80
  if (Array.isArray(lifecycle)) {
85
81
  if (lifecycle.length === 0) {
86
82
  append = false;
@@ -98,9 +94,7 @@ function upload(credential, service = 'gcp') {
98
94
  const length = response.lifecycle?.rule?.length;
99
95
  commandMessage('Lifecycle', length ? length + ' rules' : null);
100
96
  })
101
- .catch((err) => {
102
- addLog(err);
103
- });
97
+ .catch(addLog);
104
98
  }
105
99
  }
106
100
  if (flags & 4) {
@@ -113,7 +107,7 @@ function upload(credential, service = 'gcp') {
113
107
  if (!overwrite && (!listFiles || listFiles.length)) {
114
108
  const current = filename;
115
109
  const next = (0, util_1.generateFilename)(filename);
116
- let i = 0, exists;
110
+ let i = 0, exists = false;
117
111
  do {
118
112
  if (i > 0) {
119
113
  [filename, exists] = next(i);
@@ -271,7 +265,7 @@ function upload(credential, service = 'gcp') {
271
265
  else {
272
266
  const params = { ...options };
273
267
  const readable = publicRead || active && publicRead !== false && publicRead !== 0 && !acl;
274
- let ACL, revoke, predefined;
268
+ let ACL, revoke = false, predefined = false;
275
269
  if (first) {
276
270
  params.contentType ||= ContentType[i];
277
271
  if (readable) {