gcf-common-lib 0.8.1 → 0.8.2

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,4 +1,13 @@
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
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.GcfCommon = exports.storage = exports.pubSub = void 0;
4
13
  const lodash_1 = require("lodash");
@@ -8,56 +17,67 @@ const utils_1 = require("./utils");
8
17
  exports.pubSub = new pubsub_1.PubSub();
9
18
  exports.storage = new storage_1.Storage();
10
19
  class GcfCommon {
11
- static async process(event, context, handler, timeout = 535) {
12
- return Promise.race([
13
- (0, utils_1.timeoutAfter)(timeout),
14
- handler(event, context),
15
- ])
16
- .then(async (res) => {
17
- await this.publish(event, context, res ?? {});
18
- })
19
- .catch(async (err) => {
20
- const fname = process?.env?.K_SERVICE ?? 'UNKNOWN';
21
- const response = {
22
- error: {
23
- name: err.name,
24
- message: `GCF [${fname}]: ${err.message}`,
25
- stack: err.stack,
26
- },
27
- };
28
- await this.publish(event, context, response).catch(lodash_1.noop);
29
- throw err;
20
+ static process(event, context, handler, timeout = 535) {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ return Promise.race([
23
+ (0, utils_1.timeoutAfter)(timeout),
24
+ handler(event, context),
25
+ ])
26
+ .then((res) => __awaiter(this, void 0, void 0, function* () {
27
+ yield this.publish(event, context, res !== null && res !== void 0 ? res : {});
28
+ }))
29
+ .catch((err) => __awaiter(this, void 0, void 0, function* () {
30
+ var _a, _b;
31
+ 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';
32
+ const response = {
33
+ error: {
34
+ name: err.name,
35
+ message: `GCF [${fname}]: ${err.message}`,
36
+ stack: err.stack,
37
+ },
38
+ };
39
+ yield this.publish(event, context, response).catch(lodash_1.noop);
40
+ throw err;
41
+ }));
30
42
  });
31
43
  }
32
- static async publish(event, context, json) {
33
- const topic = await this.getTopic(event, context);
34
- console.log('publish:', topic?.name, json);
35
- if (topic)
36
- return await topic.publishMessage({ json });
44
+ static publish(event, context, json) {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ const topic = yield this.getTopic(event, context);
47
+ console.log('publish:', topic === null || topic === void 0 ? void 0 : topic.name, json);
48
+ if (topic)
49
+ return yield topic.publishMessage({ json });
50
+ });
37
51
  }
38
- static async getTopic(event, context) {
39
- let topicName = event?.metadata?.topic;
40
- if (!topicName && context?.resource?.type === 'storage#object') {
41
- const file = exports.storage.bucket(event.bucket).file(event.name);
42
- const [meta] = await file.getMetadata();
43
- topicName = meta?.metadata?.topic;
44
- console.log('topic:', topicName);
45
- }
46
- if (topicName && !(0, lodash_1.isEmpty)(topicName)) {
47
- const topic = exports.pubSub.topic(topicName);
48
- await topic.setMetadata({ labels: { date: topicName.split('__')[1] } });
49
- return topic;
50
- }
52
+ static getTopic(event, context) {
53
+ var _a, _b, _c;
54
+ return __awaiter(this, void 0, void 0, function* () {
55
+ let topicName = (_a = event === null || event === void 0 ? void 0 : event.metadata) === null || _a === void 0 ? void 0 : _a.topic;
56
+ if (!topicName && ((_b = context === null || context === void 0 ? void 0 : context.resource) === null || _b === void 0 ? void 0 : _b.type) === 'storage#object') {
57
+ const file = exports.storage.bucket(event.bucket).file(event.name);
58
+ const [meta] = yield file.getMetadata();
59
+ topicName = (_c = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _c === void 0 ? void 0 : _c.topic;
60
+ console.log('topic:', topicName);
61
+ }
62
+ if (topicName && !(0, lodash_1.isEmpty)(topicName)) {
63
+ const topic = exports.pubSub.topic(topicName);
64
+ yield topic.setMetadata({ labels: { date: topicName.split('__')[1] } });
65
+ return topic;
66
+ }
67
+ });
51
68
  }
52
- static async getOptions(event, context) {
53
- if (event?.metadata?.options) {
54
- return JSON.parse(event?.metadata?.options ?? '{}');
55
- }
56
- else {
57
- const file = exports.storage.bucket(event.bucket).file(event.name);
58
- const [meta] = await file.getMetadata();
59
- return JSON.parse(meta?.metadata?.options ?? '{}');
60
- }
69
+ static getOptions(event, context) {
70
+ var _a, _b, _c, _d, _e;
71
+ return __awaiter(this, void 0, void 0, function* () {
72
+ if ((_a = event === null || event === void 0 ? void 0 : event.metadata) === null || _a === void 0 ? void 0 : _a.options) {
73
+ 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 : '{}');
74
+ }
75
+ else {
76
+ const file = exports.storage.bucket(event.bucket).file(event.name);
77
+ const [meta] = yield file.getMetadata();
78
+ 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 : '{}');
79
+ }
80
+ });
61
81
  }
62
82
  }
63
83
  exports.GcfCommon = GcfCommon;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gcf-common-lib",
3
3
  "description": "",
4
- "version": "0.8.1",
4
+ "version": "0.8.2",
5
5
  "publishConfig": {
6
6
  "access": "public",
7
7
  "branches": [
package/tsconfig.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "experimentalDecorators": true,
12
12
  "module": "commonjs",
13
13
  "moduleResolution": "node",
14
- "target": "ES2020",
14
+ "target": "ES6",
15
15
  "typeRoots": [
16
16
  "node_modules/@types"
17
17
  ],
package/utils.js CHANGED
@@ -1,11 +1,22 @@
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
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.A1ToColNum = exports.A1ToIndex = exports.colNumToA1 = exports.indexToA1 = exports.timeoutAfter = void 0;
4
- async function timeoutAfter(seconds = 540) {
5
- return new Promise((resolve, reject) => {
6
- setTimeout(() => {
7
- reject(new Error(`${seconds} seconds timeout exceeded`));
8
- }, seconds * 1000);
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
+ });
9
20
  });
10
21
  }
11
22
  exports.timeoutAfter = timeoutAfter;