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 +66 -46
- package/package.json +1 -1
- package/tsconfig.json +1 -1
- package/utils.js +16 -5
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
|
|
12
|
-
return
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
package/tsconfig.json
CHANGED
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
|
-
|
|
5
|
-
return
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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;
|