gcf-common-lib 0.10.0 → 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,100 +1,88 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
10
4
  };
11
5
  Object.defineProperty(exports, "__esModule", { value: true });
12
6
  exports.GcfCommon = exports.storage = exports.pubSub = void 0;
13
7
  const pubsub_1 = require("@google-cloud/pubsub");
14
8
  const secret_manager_1 = require("@google-cloud/secret-manager");
15
9
  const storage_1 = require("@google-cloud/storage");
16
- const lodash_1 = require("lodash");
10
+ const isEmpty_1 = __importDefault(require("lodash-es/isEmpty"));
11
+ const noop_1 = __importDefault(require("lodash-es/noop"));
17
12
  const utils_1 = require("./utils");
18
13
  exports.pubSub = new pubsub_1.PubSub();
19
14
  exports.storage = new storage_1.Storage();
20
15
  const secretClient = new secret_manager_1.SecretManagerServiceClient();
21
16
  class GcfCommon {
22
- static process(event, context, handler, timeout = 535) {
23
- return __awaiter(this, void 0, void 0, function* () {
24
- return Promise.race([
25
- (0, utils_1.timeoutAfter)(timeout),
26
- handler(event, context),
27
- ])
28
- .then((res) => __awaiter(this, void 0, void 0, function* () {
29
- yield this.publish(event, context, res !== null && res !== void 0 ? res : {});
30
- }))
31
- .catch((err) => __awaiter(this, void 0, void 0, function* () {
32
- var _a, _b;
33
- const fname = (_b = (_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0 ? void 0 : _a.K_SERVICE) !== null && _b !== void 0 ? _b : 'UNKNOWN';
34
- const response = {
35
- error: {
36
- name: err.name,
37
- message: `GCF [${fname}]: ${err.message}`,
38
- stack: err.stack,
39
- },
40
- };
41
- yield this.publish(event, context, response).catch(lodash_1.noop);
42
- throw err;
43
- }));
17
+ /**
18
+ *
19
+ * @param {!TEvent} event Event payload.
20
+ * @param {!TContext} context Metadata for the event.
21
+ * @param handler
22
+ * @param timeout Seconds
23
+ */
24
+ static async process(event, context, handler, timeout = 535) {
25
+ return Promise.race([
26
+ (0, utils_1.timeoutAfter)(timeout),
27
+ handler(event, context),
28
+ ])
29
+ .then(async (res) => {
30
+ // console.log('res:', res);
31
+ await this.publish(event, context, res ?? {});
32
+ })
33
+ .catch(async (err) => {
34
+ const fname = process?.env?.K_SERVICE ?? 'UNKNOWN';
35
+ const response = {
36
+ error: {
37
+ name: err.name,
38
+ message: `GCF [${fname}]: ${err.message}`,
39
+ stack: err.stack,
40
+ },
41
+ };
42
+ await this.publish(event, context, response).catch(noop_1.default);
43
+ throw err;
44
44
  });
45
45
  }
46
- static publish(event, context, json) {
47
- return __awaiter(this, void 0, void 0, function* () {
48
- const topic = yield this.getTopic(event, context);
49
- console.log('publish:', topic === null || topic === void 0 ? void 0 : topic.name, json);
50
- if (topic)
51
- return yield topic.publishMessage({ json });
52
- });
46
+ static async publish(event, context, json) {
47
+ const topic = await this.getTopic(event, context);
48
+ console.log('publish:', topic?.name, json);
49
+ if (topic)
50
+ return await topic.publishMessage({ json });
53
51
  }
54
- static getTopic(event, context) {
55
- var _a, _b, _c;
56
- return __awaiter(this, void 0, void 0, function* () {
57
- let topicName = (_a = event === null || event === void 0 ? void 0 : event.metadata) === null || _a === void 0 ? void 0 : _a.topic;
58
- if (!topicName && ((_b = context === null || context === void 0 ? void 0 : context.resource) === null || _b === void 0 ? void 0 : _b.type) === 'storage#object') {
59
- const file = exports.storage.bucket(event.bucket).file(event.name);
60
- const [meta] = yield file.getMetadata();
61
- topicName = (_c = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _c === void 0 ? void 0 : _c.topic;
62
- console.log('topic:', topicName);
63
- }
64
- if (topicName && !(0, lodash_1.isEmpty)(topicName)) {
65
- const topic = exports.pubSub.topic(topicName);
66
- yield topic.setMetadata({ labels: { date: topicName.split('__')[1] } });
67
- return topic;
68
- }
69
- });
52
+ static async getTopic(event, context) {
53
+ /** t_{GUID}__{YYYY-MM-DD} */
54
+ let topicName = event?.metadata?.topic;
55
+ if (!topicName && context?.resource?.type === 'storage#object') {
56
+ const file = exports.storage.bucket(event.bucket).file(event.name);
57
+ const [meta] = await file.getMetadata();
58
+ topicName = meta?.metadata?.topic;
59
+ console.log('topic:', topicName);
60
+ }
61
+ if (topicName && !(0, isEmpty_1.default)(topicName)) {
62
+ const topic = exports.pubSub.topic(topicName);
63
+ await topic.setMetadata({ labels: { date: topicName.split('__')[1] } });
64
+ return topic;
65
+ }
70
66
  }
71
- static getOptions(event, context) {
72
- var _a, _b, _c, _d, _e;
73
- return __awaiter(this, void 0, void 0, function* () {
74
- if ((_a = event === null || event === void 0 ? void 0 : event.metadata) === null || _a === void 0 ? void 0 : _a.options) {
75
- return JSON.parse((_c = (_b = event === null || event === void 0 ? void 0 : event.metadata) === null || _b === void 0 ? void 0 : _b.options) !== null && _c !== void 0 ? _c : '{}');
76
- }
77
- else {
78
- const file = exports.storage.bucket(event.bucket).file(event.name);
79
- const [meta] = yield file.getMetadata();
80
- return JSON.parse((_e = (_d = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _d === void 0 ? void 0 : _d.options) !== null && _e !== void 0 ? _e : '{}');
81
- }
82
- });
67
+ static async getOptions(event, context) {
68
+ if (event?.metadata?.options) {
69
+ return JSON.parse(event?.metadata?.options ?? '{}');
70
+ }
71
+ else {
72
+ const file = exports.storage.bucket(event.bucket).file(event.name);
73
+ const [meta] = await file.getMetadata();
74
+ return JSON.parse(meta?.metadata?.options ?? '{}');
75
+ }
83
76
  }
84
- static getSecret(name, version) {
85
- var _a, _b;
86
- return __awaiter(this, void 0, void 0, function* () {
87
- const projectId = yield secretClient.getProjectId();
88
- const secretName = `projects/${projectId}/secrets/${name}`;
89
- const secretVersion = `${secretName}/versions/${version !== null && version !== void 0 ? version : 'latest'}`;
90
- const [response] = yield secretClient.accessSecretVersion({ name: secretVersion });
91
- 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();
92
- });
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();
93
83
  }
94
- static getSecrets(names, versions) {
95
- return __awaiter(this, void 0, void 0, function* () {
96
- 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]); })));
97
- });
84
+ static async getSecrets(names, versions) {
85
+ return Promise.all(names.map(async (name, idx) => this.getSecret(name, versions?.[idx])));
98
86
  }
99
87
  }
100
88
  exports.GcfCommon = GcfCommon;
package/index.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import {PubSub} from "@google-cloud/pubsub";
2
2
  import {SecretManagerServiceClient} from "@google-cloud/secret-manager";
3
3
  import {File, Storage} from "@google-cloud/storage";
4
- import {isEmpty, noop} from 'lodash';
4
+ import isEmpty from "lodash-es/isEmpty";
5
+ import noop from "lodash-es/noop";
5
6
  import {timeoutAfter} from "./utils";
6
7
 
7
8
  export type TEvent = {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gcf-common-lib",
3
3
  "description": "",
4
- "version": "0.10.0",
4
+ "version": "0.11.0",
5
5
  "publishConfig": {
6
6
  "access": "public",
7
7
  "branches": [
@@ -19,12 +19,12 @@
19
19
  "dependencies": {
20
20
  "@google-cloud/pubsub": "^2.18.5",
21
21
  "@google-cloud/secret-manager": "^3.10.1",
22
- "@google-cloud/storage": "^5.18.0",
23
- "lodash": "^4.17.21"
22
+ "@google-cloud/storage": "^5.18.1",
23
+ "lodash-es": "^4.17.21"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@tsconfig/node14": "^1.0.1",
27
- "@types/lodash": "^4.14.178"
27
+ "@types/lodash-es": "^4.17.6"
28
28
  },
29
29
  "keywords": [],
30
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": "ES6",
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,43 +1,42 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.A1ToColNum = exports.A1ToIndex = exports.colNumToA1 = exports.indexToA1 = exports.timeoutAfter = void 0;
13
- function timeoutAfter(seconds = 540) {
14
- return __awaiter(this, void 0, void 0, function* () {
15
- return new Promise((resolve, reject) => {
16
- setTimeout(() => {
17
- reject(new Error(`${seconds} seconds timeout exceeded`));
18
- }, seconds * 1000);
19
- });
4
+ /**
5
+ *
6
+ * @param seconds Google function timeout limit (max: 9 min)
7
+ */
8
+ async function timeoutAfter(seconds = 540) {
9
+ return new Promise((resolve, reject) => {
10
+ setTimeout(() => {
11
+ reject(new Error(`${seconds} seconds timeout exceeded`));
12
+ }, seconds * 1000);
20
13
  });
21
14
  }
22
15
  exports.timeoutAfter = timeoutAfter;
16
+ //
23
17
  function indexToA1(idx) {
24
18
  return colNumToA1(idx + 1);
25
19
  }
26
20
  exports.indexToA1 = indexToA1;
27
21
  function colNumToA1(columnNumber) {
28
22
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
23
+ // To store result (Excel column name)
29
24
  const charIdxArr = [];
30
25
  while (columnNumber > 0) {
26
+ // Find remainder
31
27
  const rem = columnNumber % chars.length;
28
+ // If remainder is 0, then a
29
+ // 'Z' must be there in output
32
30
  if (rem === 0) {
33
31
  charIdxArr.push(chars.length - 1);
34
32
  columnNumber = Math.floor(columnNumber / chars.length) - 1;
35
33
  }
36
- else {
34
+ else { // If remainder is non-zero
37
35
  charIdxArr.push(rem - 1);
38
36
  columnNumber = Math.floor(columnNumber / chars.length);
39
37
  }
40
38
  }
39
+ // Reverse the string and print result
41
40
  return charIdxArr.reverse().map((n) => chars[n]).join('');
42
41
  }
43
42
  exports.colNumToA1 = colNumToA1;
@@ -48,6 +47,7 @@ exports.A1ToIndex = A1ToIndex;
48
47
  function A1ToColNum(value) {
49
48
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
50
49
  let result = 0;
50
+ // tslint:disable-next-line:prefer-for-of
51
51
  for (let i = 0; i < value.length; i++) {
52
52
  result *= chars.length;
53
53
  result += chars.indexOf(value[i]) + 1;