gcf-common-lib 0.8.2 → 0.11.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/index.js CHANGED
@@ -8,15 +8,28 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.GcfCommon = exports.storage = exports.pubSub = void 0;
13
- const lodash_1 = require("lodash");
14
- const storage_1 = require("@google-cloud/storage");
15
16
  const pubsub_1 = require("@google-cloud/pubsub");
17
+ const secret_manager_1 = require("@google-cloud/secret-manager");
18
+ const storage_1 = require("@google-cloud/storage");
19
+ const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
20
+ const noop_1 = __importDefault(require("lodash/noop"));
16
21
  const utils_1 = require("./utils");
17
22
  exports.pubSub = new pubsub_1.PubSub();
18
23
  exports.storage = new storage_1.Storage();
24
+ const secretClient = new secret_manager_1.SecretManagerServiceClient();
19
25
  class GcfCommon {
26
+ /**
27
+ *
28
+ * @param {!TEvent} event Event payload.
29
+ * @param {!TContext} context Metadata for the event.
30
+ * @param handler
31
+ * @param timeout Seconds
32
+ */
20
33
  static process(event, context, handler, timeout = 535) {
21
34
  return __awaiter(this, void 0, void 0, function* () {
22
35
  return Promise.race([
@@ -24,6 +37,7 @@ class GcfCommon {
24
37
  handler(event, context),
25
38
  ])
26
39
  .then((res) => __awaiter(this, void 0, void 0, function* () {
40
+ // console.log('res:', res);
27
41
  yield this.publish(event, context, res !== null && res !== void 0 ? res : {});
28
42
  }))
29
43
  .catch((err) => __awaiter(this, void 0, void 0, function* () {
@@ -36,7 +50,7 @@ class GcfCommon {
36
50
  stack: err.stack,
37
51
  },
38
52
  };
39
- yield this.publish(event, context, response).catch(lodash_1.noop);
53
+ yield this.publish(event, context, response).catch(noop_1.default);
40
54
  throw err;
41
55
  }));
42
56
  });
@@ -52,6 +66,7 @@ class GcfCommon {
52
66
  static getTopic(event, context) {
53
67
  var _a, _b, _c;
54
68
  return __awaiter(this, void 0, void 0, function* () {
69
+ /** t_{GUID}__{YYYY-MM-DD} */
55
70
  let topicName = (_a = event === null || event === void 0 ? void 0 : event.metadata) === null || _a === void 0 ? void 0 : _a.topic;
56
71
  if (!topicName && ((_b = context === null || context === void 0 ? void 0 : context.resource) === null || _b === void 0 ? void 0 : _b.type) === 'storage#object') {
57
72
  const file = exports.storage.bucket(event.bucket).file(event.name);
@@ -59,7 +74,7 @@ class GcfCommon {
59
74
  topicName = (_c = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _c === void 0 ? void 0 : _c.topic;
60
75
  console.log('topic:', topicName);
61
76
  }
62
- if (topicName && !(0, lodash_1.isEmpty)(topicName)) {
77
+ if (topicName && !(0, isEmpty_1.default)(topicName)) {
63
78
  const topic = exports.pubSub.topic(topicName);
64
79
  yield topic.setMetadata({ labels: { date: topicName.split('__')[1] } });
65
80
  return topic;
@@ -79,5 +94,20 @@ class GcfCommon {
79
94
  }
80
95
  });
81
96
  }
97
+ static getSecret(name, version) {
98
+ var _a, _b;
99
+ return __awaiter(this, void 0, void 0, function* () {
100
+ const projectId = yield secretClient.getProjectId();
101
+ const secretName = `projects/${projectId}/secrets/${name}`;
102
+ const secretVersion = `${secretName}/versions/${version !== null && version !== void 0 ? version : 'latest'}`;
103
+ const [response] = yield secretClient.accessSecretVersion({ name: secretVersion });
104
+ return (_b = (_a = response === null || response === void 0 ? void 0 : response.payload) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.toString();
105
+ });
106
+ }
107
+ static getSecrets(names, versions) {
108
+ return __awaiter(this, void 0, void 0, function* () {
109
+ return Promise.all(names.map((name, idx) => __awaiter(this, void 0, void 0, function* () { return this.getSecret(name, versions === null || versions === void 0 ? void 0 : versions[idx]); })));
110
+ });
111
+ }
82
112
  }
83
113
  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/isEmpty";
5
+ import noop from "lodash/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,14 +1,17 @@
1
1
  {
2
2
  "name": "gcf-common-lib",
3
3
  "description": "",
4
- "version": "0.8.2",
4
+ "version": "0.11.1",
5
5
  "publishConfig": {
6
6
  "access": "public",
7
7
  "branches": [
8
8
  "master"
9
9
  ]
10
10
  },
11
- "engines": {},
11
+ "engines": {
12
+ "node": "14",
13
+ "npm": "8"
14
+ },
12
15
  "scripts": {
13
16
  "test": "echo \"Error: no test specified\" || exit 0"
14
17
  },
@@ -17,15 +20,15 @@
17
20
  "url": "https://github.com/TopTechnologies/gcf-common.git"
18
21
  },
19
22
  "dependencies": {
20
- "@google-cloud/pubsub": "^2.18.4",
21
- "@google-cloud/storage": "^5.17.0",
23
+ "@google-cloud/pubsub": "^2.18.5",
24
+ "@google-cloud/secret-manager": "^3.10.1",
25
+ "@google-cloud/storage": "^5.18.1",
22
26
  "lodash": "^4.17.21"
23
27
  },
24
28
  "devDependencies": {
25
- "@types/lodash": "^4.14.178",
26
- "@tsconfig/node14": "^1.0.1"
29
+ "@tsconfig/node14": "^1.0.1",
30
+ "@types/lodash": "^4.14.178"
27
31
  },
28
- "keywords": [],
29
32
  "author": "alert83@gmail.com",
30
33
  "license": "MIT"
31
34
  }
package/tsconfig.json CHANGED
@@ -2,28 +2,15 @@
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
6
  "target": "ES6",
15
- "typeRoots": [
16
- "node_modules/@types"
17
- ],
18
7
  "lib": [
19
8
  "ES2018",
20
9
  "ES2019",
21
10
  "ES2020",
22
11
  "ES2021",
23
12
  "ESNext"
24
- ],
25
- "types": [],
26
- "plugins": []
13
+ ]
27
14
  },
28
15
  "exclude": [
29
16
  "node_modules/**",
package/utils.js CHANGED
@@ -10,6 +10,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.A1ToColNum = exports.A1ToIndex = exports.colNumToA1 = exports.indexToA1 = exports.timeoutAfter = void 0;
13
+ /**
14
+ *
15
+ * @param seconds Google function timeout limit (max: 9 min)
16
+ */
13
17
  function timeoutAfter(seconds = 540) {
14
18
  return __awaiter(this, void 0, void 0, function* () {
15
19
  return new Promise((resolve, reject) => {
@@ -20,24 +24,30 @@ function timeoutAfter(seconds = 540) {
20
24
  });
21
25
  }
22
26
  exports.timeoutAfter = timeoutAfter;
27
+ //
23
28
  function indexToA1(idx) {
24
29
  return colNumToA1(idx + 1);
25
30
  }
26
31
  exports.indexToA1 = indexToA1;
27
32
  function colNumToA1(columnNumber) {
28
33
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
34
+ // To store result (Excel column name)
29
35
  const charIdxArr = [];
30
36
  while (columnNumber > 0) {
37
+ // Find remainder
31
38
  const rem = columnNumber % chars.length;
39
+ // If remainder is 0, then a
40
+ // 'Z' must be there in output
32
41
  if (rem === 0) {
33
42
  charIdxArr.push(chars.length - 1);
34
43
  columnNumber = Math.floor(columnNumber / chars.length) - 1;
35
44
  }
36
- else {
45
+ else { // If remainder is non-zero
37
46
  charIdxArr.push(rem - 1);
38
47
  columnNumber = Math.floor(columnNumber / chars.length);
39
48
  }
40
49
  }
50
+ // Reverse the string and print result
41
51
  return charIdxArr.reverse().map((n) => chars[n]).join('');
42
52
  }
43
53
  exports.colNumToA1 = colNumToA1;
@@ -48,6 +58,7 @@ exports.A1ToIndex = A1ToIndex;
48
58
  function A1ToColNum(value) {
49
59
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
50
60
  let result = 0;
61
+ // tslint:disable-next-line:prefer-for-of
51
62
  for (let i = 0; i < value.length; i++) {
52
63
  result *= chars.length;
53
64
  result += chars.indexOf(value[i]) + 1;