gcf-common-lib 0.4.2 → 0.5.6

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 +96 -14
  2. package/index.ts +65 -11
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -43,6 +43,59 @@ 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 fname, dataJSON;
77
+ var _a, _b;
78
+ return __generator(this, function (_c) {
79
+ switch (_c.label) {
80
+ case 0:
81
+ 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';
82
+ dataJSON = { error: { name: err.name, message: "GCF [" + fname + "]: " + err.message, stack: err.stack } };
83
+ return [4 /*yield*/, this.publish(event, context, dataJSON).catch(lodash_1.noop)];
84
+ case 1:
85
+ _c.sent();
86
+ throw err;
87
+ }
88
+ });
89
+ }); })];
90
+ });
91
+ });
92
+ };
93
+ /**
94
+ *
95
+ * @param {!Object} event Event payload.
96
+ * @param {!Object} context Metadata for the event.
97
+ * @param json
98
+ */
46
99
  GcfCommon.publish = function (event, context, json) {
47
100
  return __awaiter(this, void 0, void 0, function () {
48
101
  var topic;
@@ -54,46 +107,75 @@ var GcfCommon = /** @class */ (function () {
54
107
  console.log('publish:', topic === null || topic === void 0 ? void 0 : topic.name, json);
55
108
  if (!topic) return [3 /*break*/, 3];
56
109
  return [4 /*yield*/, topic.publishMessage({ json: json })];
57
- case 2:
58
- _a.sent();
59
- _a.label = 3;
110
+ case 2: return [2 /*return*/, _a.sent()];
60
111
  case 3: return [2 /*return*/];
61
112
  }
62
113
  });
63
114
  });
64
115
  };
116
+ /**
117
+ *
118
+ * @param {!Object} event Event payload.
119
+ * @param {!Object} context Metadata for the event.
120
+ */
65
121
  GcfCommon.getTopic = function (event, context) {
66
- var _a, _b;
122
+ var _a, _b, _c;
67
123
  return __awaiter(this, void 0, void 0, function () {
68
124
  var pubSub, topicName, storage, file, meta, topic;
69
- return __generator(this, function (_c) {
70
- switch (_c.label) {
125
+ return __generator(this, function (_d) {
126
+ switch (_d.label) {
71
127
  case 0:
72
128
  pubSub = new pubsub_1.PubSub();
73
- if (!(((_a = context === null || context === void 0 ? void 0 : context.resource) === null || _a === void 0 ? void 0 : _a.type) === 'storage#object')) return [3 /*break*/, 2];
129
+ topicName = (_a = event === null || event === void 0 ? void 0 : event.metadata) === null || _a === void 0 ? void 0 : _a.topic;
130
+ if (!(!topicName && ((_b = context === null || context === void 0 ? void 0 : context.resource) === null || _b === void 0 ? void 0 : _b.type) === 'storage#object')) return [3 /*break*/, 2];
74
131
  storage = new storage_1.Storage();
75
132
  file = storage.bucket(event.bucket).file(event.name);
76
133
  return [4 /*yield*/, file.getMetadata()];
77
134
  case 1:
78
- meta = (_c.sent())[0];
79
- topicName = (_b = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _b === void 0 ? void 0 : _b.topic;
135
+ meta = (_d.sent())[0];
136
+ topicName = (_c = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _c === void 0 ? void 0 : _c.topic;
80
137
  console.log('topic:', topicName);
81
- _c.label = 2;
138
+ _d.label = 2;
82
139
  case 2:
83
- if ((0, lodash_1.isEmpty)(topicName))
84
- return [2 /*return*/];
140
+ if (!!(0, lodash_1.isEmpty)(topicName)) return [3 /*break*/, 4];
85
141
  topic = pubSub.topic(topicName);
86
142
  return [4 /*yield*/, topic.setMetadata({ labels: { date: topicName.split('__')[1] } })];
87
143
  case 3:
88
- _c.sent();
144
+ _d.sent();
89
145
  return [2 /*return*/, topic];
146
+ case 4: return [2 /*return*/];
147
+ }
148
+ });
149
+ });
150
+ };
151
+ /**
152
+ *
153
+ * @param {!Object} event Event payload.
154
+ * @param {!Object} context Metadata for the event.
155
+ */
156
+ GcfCommon.getOptions = function (event, context) {
157
+ var _a, _b, _c, _d, _e;
158
+ return __awaiter(this, void 0, void 0, function () {
159
+ var storage, file, meta;
160
+ return __generator(this, function (_f) {
161
+ switch (_f.label) {
162
+ case 0:
163
+ if (!((_a = event === null || event === void 0 ? void 0 : event.metadata) === null || _a === void 0 ? void 0 : _a.options)) return [3 /*break*/, 1];
164
+ return [2 /*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 : '{}')];
165
+ case 1:
166
+ storage = new storage_1.Storage();
167
+ file = storage.bucket(event.bucket).file(event.name);
168
+ return [4 /*yield*/, file.getMetadata()];
169
+ case 2:
170
+ meta = (_f.sent())[0];
171
+ return [2 /*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 : '{}')];
90
172
  }
91
173
  });
92
174
  });
93
175
  };
94
176
  /**
95
177
  *
96
- * @param s (google function limit max = 9 min)
178
+ * @param s Google function time limit (max: 9 min)
97
179
  */
98
180
  GcfCommon.delay = function (s) {
99
181
  if (s === void 0) { s = 540; }
package/index.ts CHANGED
@@ -1,20 +1,58 @@
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 fname = process?.env?.K_SERVICE ?? 'UNKNOWN';
25
+ const dataJSON = {error: {name: err.name, message: `GCF [${fname}]: ${err.message}`, stack: err.stack}};
26
+ await this.publish(event, context, dataJSON).catch(noop);
27
+ throw err;
28
+ })
29
+ ;
30
+ }
31
+
32
+ /**
33
+ *
34
+ * @param {!Object} event Event payload.
35
+ * @param {!Object} context Metadata for the event.
36
+ * @param json
37
+ */
6
38
  static async publish(event, context, json) {
7
39
  const topic = await this.getTopic(event, context);
8
40
  console.log('publish:', topic?.name, json);
9
- if (topic) await topic.publishMessage({json});
41
+ if (topic) return await topic.publishMessage({json});
10
42
  }
11
43
 
12
- static async getTopic(event, context) {
44
+ /**
45
+ *
46
+ * @param {!Object} event Event payload.
47
+ * @param {!Object} context Metadata for the event.
48
+ */
49
+ static async getTopic(event, context): Promise<Topic | undefined> {
13
50
  const pubSub = new PubSub();
51
+
14
52
  /** t_{GUID}__{YYYY-MM-DD} */
15
- let topicName: string;
53
+ let topicName: string | undefined = event?.metadata?.topic;
16
54
 
17
- if (context?.resource?.type === 'storage#object') {
55
+ if (!topicName && context?.resource?.type === 'storage#object') {
18
56
  const storage = new Storage();
19
57
  const file: File = storage.bucket(event.bucket).file(event.name);
20
58
  const [meta] = await file.getMetadata();
@@ -22,16 +60,32 @@ export class GcfCommon {
22
60
  console.log('topic:', topicName);
23
61
  }
24
62
 
25
- if (isEmpty(topicName)) return;
63
+ if (!isEmpty(topicName)) {
64
+ const topic = pubSub.topic(topicName);
65
+ await topic.setMetadata({labels: {date: topicName.split('__')[1]}});
66
+ return topic;
67
+ }
68
+ }
26
69
 
27
- const topic = pubSub.topic(topicName);
28
- await topic.setMetadata({labels: {date: topicName.split('__')[1]}});
29
- return topic;
70
+ /**
71
+ *
72
+ * @param {!Object} event Event payload.
73
+ * @param {!Object} context Metadata for the event.
74
+ */
75
+ static async getOptions(event, context) {
76
+ if (event?.metadata?.options) {
77
+ return JSON.parse(event?.metadata?.options ?? '{}');
78
+ } else {
79
+ const storage = new Storage();
80
+ const file: File = storage.bucket(event.bucket).file(event.name);
81
+ const [meta] = await file.getMetadata();
82
+ return JSON.parse(meta?.metadata?.options ?? '{}');
83
+ }
30
84
  }
31
85
 
32
86
  /**
33
87
  *
34
- * @param s (google function limit max = 9 min)
88
+ * @param s Google function time limit (max: 9 min)
35
89
  */
36
90
  static async delay(s: number = 540) {
37
91
  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.6",
5
5
  "publishConfig": { "access": "public", "branches": ["master"] },
6
6
  "engines": {},
7
7
  "scripts": {