gcf-common-lib 0.4.2 → 0.5.3

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.
Files changed (3) hide show
  1. package/index.js +60 -6
  2. package/index.ts +48 -11
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -43,6 +43,57 @@ var lodash_1 = require("lodash");
43
43
  var GcfCommon = /** @class */ (function () {
44
44
  function GcfCommon() {
45
45
  }
46
+ /**
47
+ *
48
+ * @param {!Object} event Event payload.
49
+ * @param {!Object} context Metadata for the event.
50
+ * @param handler
51
+ * @param timeout Seconds
52
+ */
53
+ GcfCommon.process = function (event, context, handler, timeout) {
54
+ if (timeout === void 0) { timeout = 535; }
55
+ return __awaiter(this, void 0, void 0, function () {
56
+ var _this = this;
57
+ return __generator(this, function (_a) {
58
+ return [2 /*return*/, Promise.race([
59
+ this.delay(535),
60
+ handler(event, context),
61
+ ])
62
+ .then(function (res) { return __awaiter(_this, void 0, void 0, function () {
63
+ return __generator(this, function (_a) {
64
+ switch (_a.label) {
65
+ case 0:
66
+ // console.log('res:', res);
67
+ return [4 /*yield*/, this.publish(event, context, res !== null && res !== void 0 ? res : {})];
68
+ case 1:
69
+ // console.log('res:', res);
70
+ _a.sent();
71
+ return [2 /*return*/];
72
+ }
73
+ });
74
+ }); })
75
+ .catch(function (err) { return __awaiter(_this, void 0, void 0, function () {
76
+ var dataJSON;
77
+ return __generator(this, function (_a) {
78
+ switch (_a.label) {
79
+ case 0:
80
+ dataJSON = { error: { name: err.name, message: err.message, stack: err.stack } };
81
+ return [4 /*yield*/, this.publish(event, context, dataJSON).catch(lodash_1.noop)];
82
+ case 1:
83
+ _a.sent();
84
+ throw err;
85
+ }
86
+ });
87
+ }); })];
88
+ });
89
+ });
90
+ };
91
+ /**
92
+ *
93
+ * @param {!Object} event Event payload.
94
+ * @param {!Object} context Metadata for the event.
95
+ * @param json
96
+ */
46
97
  GcfCommon.publish = function (event, context, json) {
47
98
  return __awaiter(this, void 0, void 0, function () {
48
99
  var topic;
@@ -54,14 +105,17 @@ var GcfCommon = /** @class */ (function () {
54
105
  console.log('publish:', topic === null || topic === void 0 ? void 0 : topic.name, json);
55
106
  if (!topic) return [3 /*break*/, 3];
56
107
  return [4 /*yield*/, topic.publishMessage({ json: json })];
57
- case 2:
58
- _a.sent();
59
- _a.label = 3;
108
+ case 2: return [2 /*return*/, _a.sent()];
60
109
  case 3: return [2 /*return*/];
61
110
  }
62
111
  });
63
112
  });
64
113
  };
114
+ /**
115
+ *
116
+ * @param {!Object} event Event payload.
117
+ * @param {!Object} context Metadata for the event.
118
+ */
65
119
  GcfCommon.getTopic = function (event, context) {
66
120
  var _a, _b;
67
121
  return __awaiter(this, void 0, void 0, function () {
@@ -80,20 +134,20 @@ var GcfCommon = /** @class */ (function () {
80
134
  console.log('topic:', topicName);
81
135
  _c.label = 2;
82
136
  case 2:
83
- if ((0, lodash_1.isEmpty)(topicName))
84
- return [2 /*return*/];
137
+ if (!!(0, lodash_1.isEmpty)(topicName)) return [3 /*break*/, 4];
85
138
  topic = pubSub.topic(topicName);
86
139
  return [4 /*yield*/, topic.setMetadata({ labels: { date: topicName.split('__')[1] } })];
87
140
  case 3:
88
141
  _c.sent();
89
142
  return [2 /*return*/, topic];
143
+ case 4: return [2 /*return*/];
90
144
  }
91
145
  });
92
146
  });
93
147
  };
94
148
  /**
95
149
  *
96
- * @param s (google function limit max = 9 min)
150
+ * @param s Google function time limit (max: 9 min)
97
151
  */
98
152
  GcfCommon.delay = function (s) {
99
153
  if (s === void 0) { s = 540; }
package/index.ts CHANGED
@@ -1,18 +1,55 @@
1
1
  import {File, Storage} from '@google-cloud/storage';
2
- import {PubSub} from '@google-cloud/pubsub';
3
- import {isEmpty} from 'lodash';
2
+ import {PubSub, Topic} from '@google-cloud/pubsub';
3
+ import {isEmpty, noop} from 'lodash';
4
4
 
5
5
  export class GcfCommon {
6
+
7
+ /**
8
+ *
9
+ * @param {!Object} event Event payload.
10
+ * @param {!Object} context Metadata for the event.
11
+ * @param handler
12
+ * @param timeout Seconds
13
+ */
14
+ static async process(event, context, handler: (event, context) => Promise<any>, timeout = 535) {
15
+ return Promise.race([
16
+ this.delay(535),
17
+ handler(event, context),
18
+ ])
19
+ .then(async (res: any) => {
20
+ // console.log('res:', res);
21
+ await this.publish(event, context, res ?? {});
22
+ })
23
+ .catch(async (err: Error) => {
24
+ const dataJSON = {error: {name: err.name, message: err.message, stack: err.stack}};
25
+ await this.publish(event, context, dataJSON).catch(noop);
26
+ throw err;
27
+ })
28
+ ;
29
+ }
30
+
31
+ /**
32
+ *
33
+ * @param {!Object} event Event payload.
34
+ * @param {!Object} context Metadata for the event.
35
+ * @param json
36
+ */
6
37
  static async publish(event, context, json) {
7
38
  const topic = await this.getTopic(event, context);
8
39
  console.log('publish:', topic?.name, json);
9
- if (topic) await topic.publishMessage({json});
40
+ if (topic) return await topic.publishMessage({json});
10
41
  }
11
42
 
12
- static async getTopic(event, context) {
43
+ /**
44
+ *
45
+ * @param {!Object} event Event payload.
46
+ * @param {!Object} context Metadata for the event.
47
+ */
48
+ static async getTopic(event, context): Promise<Topic | undefined> {
13
49
  const pubSub = new PubSub();
50
+
14
51
  /** t_{GUID}__{YYYY-MM-DD} */
15
- let topicName: string;
52
+ let topicName: string | undefined;
16
53
 
17
54
  if (context?.resource?.type === 'storage#object') {
18
55
  const storage = new Storage();
@@ -22,16 +59,16 @@ export class GcfCommon {
22
59
  console.log('topic:', topicName);
23
60
  }
24
61
 
25
- if (isEmpty(topicName)) return;
26
-
27
- const topic = pubSub.topic(topicName);
28
- await topic.setMetadata({labels: {date: topicName.split('__')[1]}});
29
- return topic;
62
+ if (!isEmpty(topicName)) {
63
+ const topic = pubSub.topic(topicName);
64
+ await topic.setMetadata({labels: {date: topicName.split('__')[1]}});
65
+ return topic;
66
+ }
30
67
  }
31
68
 
32
69
  /**
33
70
  *
34
- * @param s (google function limit max = 9 min)
71
+ * @param s Google function time limit (max: 9 min)
35
72
  */
36
73
  static async delay(s: number = 540) {
37
74
  return new Promise<undefined>((resolve, reject) => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gcf-common-lib",
3
3
  "description": "",
4
- "version": "0.4.2",
4
+ "version": "0.5.3",
5
5
  "publishConfig": { "access": "public", "branches": ["master"] },
6
6
  "engines": {},
7
7
  "scripts": {