gcf-common-lib 0.8.1 → 0.11.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/index.js CHANGED
@@ -1,19 +1,33 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.GcfCommon = exports.storage = exports.pubSub = void 0;
4
- const lodash_1 = require("lodash");
5
- const storage_1 = require("@google-cloud/storage");
6
7
  const pubsub_1 = require("@google-cloud/pubsub");
8
+ const secret_manager_1 = require("@google-cloud/secret-manager");
9
+ const storage_1 = require("@google-cloud/storage");
10
+ const isEmpty_1 = __importDefault(require("lodash-es/isEmpty"));
11
+ const noop_1 = __importDefault(require("lodash-es/noop"));
7
12
  const utils_1 = require("./utils");
8
13
  exports.pubSub = new pubsub_1.PubSub();
9
14
  exports.storage = new storage_1.Storage();
15
+ const secretClient = new secret_manager_1.SecretManagerServiceClient();
10
16
  class GcfCommon {
17
+ /**
18
+ *
19
+ * @param {!TEvent} event Event payload.
20
+ * @param {!TContext} context Metadata for the event.
21
+ * @param handler
22
+ * @param timeout Seconds
23
+ */
11
24
  static async process(event, context, handler, timeout = 535) {
12
25
  return Promise.race([
13
26
  (0, utils_1.timeoutAfter)(timeout),
14
27
  handler(event, context),
15
28
  ])
16
29
  .then(async (res) => {
30
+ // console.log('res:', res);
17
31
  await this.publish(event, context, res ?? {});
18
32
  })
19
33
  .catch(async (err) => {
@@ -25,7 +39,7 @@ class GcfCommon {
25
39
  stack: err.stack,
26
40
  },
27
41
  };
28
- await this.publish(event, context, response).catch(lodash_1.noop);
42
+ await this.publish(event, context, response).catch(noop_1.default);
29
43
  throw err;
30
44
  });
31
45
  }
@@ -36,6 +50,7 @@ class GcfCommon {
36
50
  return await topic.publishMessage({ json });
37
51
  }
38
52
  static async getTopic(event, context) {
53
+ /** t_{GUID}__{YYYY-MM-DD} */
39
54
  let topicName = event?.metadata?.topic;
40
55
  if (!topicName && context?.resource?.type === 'storage#object') {
41
56
  const file = exports.storage.bucket(event.bucket).file(event.name);
@@ -43,7 +58,7 @@ class GcfCommon {
43
58
  topicName = meta?.metadata?.topic;
44
59
  console.log('topic:', topicName);
45
60
  }
46
- if (topicName && !(0, lodash_1.isEmpty)(topicName)) {
61
+ if (topicName && !(0, isEmpty_1.default)(topicName)) {
47
62
  const topic = exports.pubSub.topic(topicName);
48
63
  await topic.setMetadata({ labels: { date: topicName.split('__')[1] } });
49
64
  return topic;
@@ -59,5 +74,15 @@ class GcfCommon {
59
74
  return JSON.parse(meta?.metadata?.options ?? '{}');
60
75
  }
61
76
  }
77
+ static async getSecret(name, version) {
78
+ const projectId = await secretClient.getProjectId();
79
+ const secretName = `projects/${projectId}/secrets/${name}`;
80
+ const secretVersion = `${secretName}/versions/${version ?? 'latest'}`;
81
+ const [response] = await secretClient.accessSecretVersion({ name: secretVersion });
82
+ return response?.payload?.data?.toString();
83
+ }
84
+ static async getSecrets(names, versions) {
85
+ return Promise.all(names.map(async (name, idx) => this.getSecret(name, versions?.[idx])));
86
+ }
62
87
  }
63
88
  exports.GcfCommon = GcfCommon;
package/index.ts CHANGED
@@ -1,6 +1,8 @@
1
- import {isEmpty, noop} from 'lodash';
2
- import {File, Storage} from "@google-cloud/storage";
3
1
  import {PubSub} from "@google-cloud/pubsub";
2
+ import {SecretManagerServiceClient} from "@google-cloud/secret-manager";
3
+ import {File, Storage} from "@google-cloud/storage";
4
+ import isEmpty from "lodash-es/isEmpty";
5
+ import noop from "lodash-es/noop";
4
6
  import {timeoutAfter} from "./utils";
5
7
 
6
8
  export type TEvent = {
@@ -45,6 +47,7 @@ export type TResponse = {
45
47
 
46
48
  export const pubSub = new PubSub();
47
49
  export const storage = new Storage();
50
+ const secretClient = new SecretManagerServiceClient();
48
51
 
49
52
  export class GcfCommon {
50
53
 
@@ -118,4 +121,16 @@ export class GcfCommon {
118
121
  return JSON.parse(meta?.metadata?.options ?? '{}');
119
122
  }
120
123
  }
124
+
125
+ static async getSecret(name: string, version?: string) {
126
+ const projectId = await secretClient.getProjectId();
127
+ const secretName = `projects/${projectId}/secrets/${name}`;
128
+ const secretVersion = `${secretName}/versions/${version ?? 'latest'}`;
129
+ const [response] = await secretClient.accessSecretVersion({name: secretVersion});
130
+ return response?.payload?.data?.toString();
131
+ }
132
+
133
+ static async getSecrets(names: string[], versions?: string[]) {
134
+ return Promise.all(names.map(async (name, idx) => this.getSecret(name, versions?.[idx])));
135
+ }
121
136
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gcf-common-lib",
3
3
  "description": "",
4
- "version": "0.8.1",
4
+ "version": "0.11.0",
5
5
  "publishConfig": {
6
6
  "access": "public",
7
7
  "branches": [
@@ -17,13 +17,14 @@
17
17
  "url": "https://github.com/TopTechnologies/gcf-common.git"
18
18
  },
19
19
  "dependencies": {
20
- "@google-cloud/pubsub": "^2.18.4",
21
- "@google-cloud/storage": "^5.17.0",
22
- "lodash": "^4.17.21"
20
+ "@google-cloud/pubsub": "^2.18.5",
21
+ "@google-cloud/secret-manager": "^3.10.1",
22
+ "@google-cloud/storage": "^5.18.1",
23
+ "lodash-es": "^4.17.21"
23
24
  },
24
25
  "devDependencies": {
25
- "@types/lodash": "^4.14.178",
26
- "@tsconfig/node14": "^1.0.1"
26
+ "@tsconfig/node14": "^1.0.1",
27
+ "@types/lodash-es": "^4.17.6"
27
28
  },
28
29
  "keywords": [],
29
30
  "author": "alert83@gmail.com",
package/tsconfig.json CHANGED
@@ -2,28 +2,14 @@
2
2
  "$schema": "http://json.schemastore.org/tsconfig",
3
3
  "extends": "@tsconfig/node14/tsconfig.json",
4
4
  "compilerOptions": {
5
- "allowJs": false,
6
- "removeComments": true,
7
- "esModuleInterop": true,
8
- "allowSyntheticDefaultImports": true,
9
- "isolatedModules": false,
10
- "emitDecoratorMetadata": true,
11
- "experimentalDecorators": true,
12
- "module": "commonjs",
13
5
  "moduleResolution": "node",
14
- "target": "ES2020",
15
- "typeRoots": [
16
- "node_modules/@types"
17
- ],
18
6
  "lib": [
19
7
  "ES2018",
20
8
  "ES2019",
21
9
  "ES2020",
22
10
  "ES2021",
23
11
  "ESNext"
24
- ],
25
- "types": [],
26
- "plugins": []
12
+ ]
27
13
  },
28
14
  "exclude": [
29
15
  "node_modules/**",
package/utils.js CHANGED
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.A1ToColNum = exports.A1ToIndex = exports.colNumToA1 = exports.indexToA1 = exports.timeoutAfter = void 0;
4
+ /**
5
+ *
6
+ * @param seconds Google function timeout limit (max: 9 min)
7
+ */
4
8
  async function timeoutAfter(seconds = 540) {
5
9
  return new Promise((resolve, reject) => {
6
10
  setTimeout(() => {
@@ -9,24 +13,30 @@ async function timeoutAfter(seconds = 540) {
9
13
  });
10
14
  }
11
15
  exports.timeoutAfter = timeoutAfter;
16
+ //
12
17
  function indexToA1(idx) {
13
18
  return colNumToA1(idx + 1);
14
19
  }
15
20
  exports.indexToA1 = indexToA1;
16
21
  function colNumToA1(columnNumber) {
17
22
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
23
+ // To store result (Excel column name)
18
24
  const charIdxArr = [];
19
25
  while (columnNumber > 0) {
26
+ // Find remainder
20
27
  const rem = columnNumber % chars.length;
28
+ // If remainder is 0, then a
29
+ // 'Z' must be there in output
21
30
  if (rem === 0) {
22
31
  charIdxArr.push(chars.length - 1);
23
32
  columnNumber = Math.floor(columnNumber / chars.length) - 1;
24
33
  }
25
- else {
34
+ else { // If remainder is non-zero
26
35
  charIdxArr.push(rem - 1);
27
36
  columnNumber = Math.floor(columnNumber / chars.length);
28
37
  }
29
38
  }
39
+ // Reverse the string and print result
30
40
  return charIdxArr.reverse().map((n) => chars[n]).join('');
31
41
  }
32
42
  exports.colNumToA1 = colNumToA1;
@@ -37,6 +47,7 @@ exports.A1ToIndex = A1ToIndex;
37
47
  function A1ToColNum(value) {
38
48
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
39
49
  let result = 0;
50
+ // tslint:disable-next-line:prefer-for-of
40
51
  for (let i = 0; i < value.length; i++) {
41
52
  result *= chars.length;
42
53
  result += chars.indexOf(value[i]) + 1;