gcf-common-lib 0.5.3 → 0.6.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.
Files changed (3) hide show
  1. package/index.js +79 -13
  2. package/index.ts +66 -3
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -73,14 +73,16 @@ var GcfCommon = /** @class */ (function () {
73
73
  });
74
74
  }); })
75
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) {
76
+ var fname, dataJSON;
77
+ var _a, _b;
78
+ return __generator(this, function (_c) {
79
+ switch (_c.label) {
79
80
  case 0:
80
- dataJSON = { error: { name: err.name, message: err.message, stack: err.stack } };
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 } };
81
83
  return [4 /*yield*/, this.publish(event, context, dataJSON).catch(lodash_1.noop)];
82
84
  case 1:
83
- _a.sent();
85
+ _c.sent();
84
86
  throw err;
85
87
  }
86
88
  });
@@ -117,34 +119,60 @@ var GcfCommon = /** @class */ (function () {
117
119
  * @param {!Object} context Metadata for the event.
118
120
  */
119
121
  GcfCommon.getTopic = function (event, context) {
120
- var _a, _b;
122
+ var _a, _b, _c;
121
123
  return __awaiter(this, void 0, void 0, function () {
122
124
  var pubSub, topicName, storage, file, meta, topic;
123
- return __generator(this, function (_c) {
124
- switch (_c.label) {
125
+ return __generator(this, function (_d) {
126
+ switch (_d.label) {
125
127
  case 0:
126
128
  pubSub = new pubsub_1.PubSub();
127
- 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];
128
131
  storage = new storage_1.Storage();
129
132
  file = storage.bucket(event.bucket).file(event.name);
130
133
  return [4 /*yield*/, file.getMetadata()];
131
134
  case 1:
132
- meta = (_c.sent())[0];
133
- 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;
134
137
  console.log('topic:', topicName);
135
- _c.label = 2;
138
+ _d.label = 2;
136
139
  case 2:
137
140
  if (!!(0, lodash_1.isEmpty)(topicName)) return [3 /*break*/, 4];
138
141
  topic = pubSub.topic(topicName);
139
142
  return [4 /*yield*/, topic.setMetadata({ labels: { date: topicName.split('__')[1] } })];
140
143
  case 3:
141
- _c.sent();
144
+ _d.sent();
142
145
  return [2 /*return*/, topic];
143
146
  case 4: return [2 /*return*/];
144
147
  }
145
148
  });
146
149
  });
147
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 : '{}')];
172
+ }
173
+ });
174
+ });
175
+ };
148
176
  /**
149
177
  *
150
178
  * @param s Google function time limit (max: 9 min)
@@ -161,6 +189,44 @@ var GcfCommon = /** @class */ (function () {
161
189
  });
162
190
  });
163
191
  };
192
+ //
193
+ GcfCommon.indexToA1 = function (idx) {
194
+ return this.colNumToA1(idx + 1);
195
+ };
196
+ GcfCommon.colNumToA1 = function (columnNumber) {
197
+ var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
198
+ // To store result (Excel column name)
199
+ var charIdxArr = [];
200
+ while (columnNumber > 0) {
201
+ // Find remainder
202
+ var rem = columnNumber % chars.length;
203
+ // If remainder is 0, then a
204
+ // 'Z' must be there in output
205
+ if (rem === 0) {
206
+ charIdxArr.push(chars.length - 1);
207
+ columnNumber = Math.floor(columnNumber / chars.length) - 1;
208
+ }
209
+ else { // If remainder is non-zero
210
+ charIdxArr.push(rem - 1);
211
+ columnNumber = Math.floor(columnNumber / chars.length);
212
+ }
213
+ }
214
+ // Reverse the string and print result
215
+ return charIdxArr.reverse().map(function (n) { return chars[n]; }).join('');
216
+ };
217
+ GcfCommon.A1ToIndex = function (value) {
218
+ return this.A1ToColNum(value) - 1;
219
+ };
220
+ GcfCommon.A1ToColNum = function (value) {
221
+ var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
222
+ var result = 0;
223
+ // tslint:disable-next-line:prefer-for-of
224
+ for (var i = 0; i < value.length; i++) {
225
+ result *= chars.length;
226
+ result += chars.indexOf(value[i]) + 1;
227
+ }
228
+ return result;
229
+ };
164
230
  return GcfCommon;
165
231
  }());
166
232
  exports.GcfCommon = GcfCommon;
package/index.ts CHANGED
@@ -21,7 +21,8 @@ export class GcfCommon {
21
21
  await this.publish(event, context, res ?? {});
22
22
  })
23
23
  .catch(async (err: Error) => {
24
- const dataJSON = {error: {name: err.name, message: err.message, stack: err.stack}};
24
+ const fname = process?.env?.K_SERVICE ?? 'UNKNOWN';
25
+ const dataJSON = {error: {name: err.name, message: `GCF [${fname}]: ${err.message}`, stack: err.stack}};
25
26
  await this.publish(event, context, dataJSON).catch(noop);
26
27
  throw err;
27
28
  })
@@ -49,9 +50,9 @@ export class GcfCommon {
49
50
  const pubSub = new PubSub();
50
51
 
51
52
  /** t_{GUID}__{YYYY-MM-DD} */
52
- let topicName: string | undefined;
53
+ let topicName: string | undefined = event?.metadata?.topic;
53
54
 
54
- if (context?.resource?.type === 'storage#object') {
55
+ if (!topicName && context?.resource?.type === 'storage#object') {
55
56
  const storage = new Storage();
56
57
  const file: File = storage.bucket(event.bucket).file(event.name);
57
58
  const [meta] = await file.getMetadata();
@@ -66,6 +67,22 @@ export class GcfCommon {
66
67
  }
67
68
  }
68
69
 
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
+ }
84
+ }
85
+
69
86
  /**
70
87
  *
71
88
  * @param s Google function time limit (max: 9 min)
@@ -77,4 +94,50 @@ export class GcfCommon {
77
94
  }, s * 1000);
78
95
  });
79
96
  }
97
+
98
+ //
99
+
100
+ static indexToA1(idx: number) {
101
+ return this.colNumToA1(idx + 1);
102
+ }
103
+
104
+ static colNumToA1(columnNumber: number) {
105
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
106
+
107
+ // To store result (Excel column name)
108
+ const charIdxArr: number[] = [];
109
+
110
+ while (columnNumber > 0) {
111
+ // Find remainder
112
+ const rem = columnNumber % chars.length;
113
+
114
+ // If remainder is 0, then a
115
+ // 'Z' must be there in output
116
+ if (rem === 0) {
117
+ charIdxArr.push(chars.length - 1);
118
+ columnNumber = Math.floor(columnNumber / chars.length) - 1;
119
+ } else { // If remainder is non-zero
120
+ charIdxArr.push(rem - 1);
121
+ columnNumber = Math.floor(columnNumber / chars.length);
122
+ }
123
+ }
124
+
125
+ // Reverse the string and print result
126
+ return charIdxArr.reverse().map((n) => chars[n]).join('');
127
+ }
128
+
129
+ static A1ToIndex(value: string) {
130
+ return this.A1ToColNum(value) - 1;
131
+ }
132
+
133
+ static A1ToColNum(value: string) {
134
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
135
+ let result = 0;
136
+ // tslint:disable-next-line:prefer-for-of
137
+ for (let i = 0; i < value.length; i++) {
138
+ result *= chars.length;
139
+ result += chars.indexOf(value[i]) + 1;
140
+ }
141
+ return result;
142
+ }
80
143
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gcf-common-lib",
3
3
  "description": "",
4
- "version": "0.5.3",
4
+ "version": "0.6.0",
5
5
  "publishConfig": { "access": "public", "branches": ["master"] },
6
6
  "engines": {},
7
7
  "scripts": {