gcf-common-lib 0.11.0 → 0.11.1
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 +83 -58
- package/index.ts +2 -2
- package/package.json +7 -5
- package/tsconfig.json +1 -0
- 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
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
13
|
};
|
|
@@ -7,8 +16,8 @@ exports.GcfCommon = exports.storage = exports.pubSub = void 0;
|
|
|
7
16
|
const pubsub_1 = require("@google-cloud/pubsub");
|
|
8
17
|
const secret_manager_1 = require("@google-cloud/secret-manager");
|
|
9
18
|
const storage_1 = require("@google-cloud/storage");
|
|
10
|
-
const isEmpty_1 = __importDefault(require("lodash
|
|
11
|
-
const noop_1 = __importDefault(require("lodash
|
|
19
|
+
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
|
|
20
|
+
const noop_1 = __importDefault(require("lodash/noop"));
|
|
12
21
|
const utils_1 = require("./utils");
|
|
13
22
|
exports.pubSub = new pubsub_1.PubSub();
|
|
14
23
|
exports.storage = new storage_1.Storage();
|
|
@@ -21,68 +30,84 @@ class GcfCommon {
|
|
|
21
30
|
* @param handler
|
|
22
31
|
* @param timeout Seconds
|
|
23
32
|
*/
|
|
24
|
-
static
|
|
25
|
-
return
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
33
|
+
static process(event, context, handler, timeout = 535) {
|
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
return Promise.race([
|
|
36
|
+
(0, utils_1.timeoutAfter)(timeout),
|
|
37
|
+
handler(event, context),
|
|
38
|
+
])
|
|
39
|
+
.then((res) => __awaiter(this, void 0, void 0, function* () {
|
|
40
|
+
// console.log('res:', res);
|
|
41
|
+
yield this.publish(event, context, res !== null && res !== void 0 ? res : {});
|
|
42
|
+
}))
|
|
43
|
+
.catch((err) => __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
var _a, _b;
|
|
45
|
+
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';
|
|
46
|
+
const response = {
|
|
47
|
+
error: {
|
|
48
|
+
name: err.name,
|
|
49
|
+
message: `GCF [${fname}]: ${err.message}`,
|
|
50
|
+
stack: err.stack,
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
yield this.publish(event, context, response).catch(noop_1.default);
|
|
54
|
+
throw err;
|
|
55
|
+
}));
|
|
44
56
|
});
|
|
45
57
|
}
|
|
46
|
-
static
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
58
|
+
static publish(event, context, json) {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
const topic = yield this.getTopic(event, context);
|
|
61
|
+
console.log('publish:', topic === null || topic === void 0 ? void 0 : topic.name, json);
|
|
62
|
+
if (topic)
|
|
63
|
+
return yield topic.publishMessage({ json });
|
|
64
|
+
});
|
|
51
65
|
}
|
|
52
|
-
static
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
+
static getTopic(event, context) {
|
|
67
|
+
var _a, _b, _c;
|
|
68
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
/** t_{GUID}__{YYYY-MM-DD} */
|
|
70
|
+
let topicName = (_a = event === null || event === void 0 ? void 0 : event.metadata) === null || _a === void 0 ? void 0 : _a.topic;
|
|
71
|
+
if (!topicName && ((_b = context === null || context === void 0 ? void 0 : context.resource) === null || _b === void 0 ? void 0 : _b.type) === 'storage#object') {
|
|
72
|
+
const file = exports.storage.bucket(event.bucket).file(event.name);
|
|
73
|
+
const [meta] = yield file.getMetadata();
|
|
74
|
+
topicName = (_c = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _c === void 0 ? void 0 : _c.topic;
|
|
75
|
+
console.log('topic:', topicName);
|
|
76
|
+
}
|
|
77
|
+
if (topicName && !(0, isEmpty_1.default)(topicName)) {
|
|
78
|
+
const topic = exports.pubSub.topic(topicName);
|
|
79
|
+
yield topic.setMetadata({ labels: { date: topicName.split('__')[1] } });
|
|
80
|
+
return topic;
|
|
81
|
+
}
|
|
82
|
+
});
|
|
66
83
|
}
|
|
67
|
-
static
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
84
|
+
static getOptions(event, context) {
|
|
85
|
+
var _a, _b, _c, _d, _e;
|
|
86
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
if ((_a = event === null || event === void 0 ? void 0 : event.metadata) === null || _a === void 0 ? void 0 : _a.options) {
|
|
88
|
+
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 : '{}');
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
const file = exports.storage.bucket(event.bucket).file(event.name);
|
|
92
|
+
const [meta] = yield file.getMetadata();
|
|
93
|
+
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 : '{}');
|
|
94
|
+
}
|
|
95
|
+
});
|
|
76
96
|
}
|
|
77
|
-
static
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
97
|
+
static getSecret(name, version) {
|
|
98
|
+
var _a, _b;
|
|
99
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
100
|
+
const projectId = yield secretClient.getProjectId();
|
|
101
|
+
const secretName = `projects/${projectId}/secrets/${name}`;
|
|
102
|
+
const secretVersion = `${secretName}/versions/${version !== null && version !== void 0 ? version : 'latest'}`;
|
|
103
|
+
const [response] = yield secretClient.accessSecretVersion({ name: secretVersion });
|
|
104
|
+
return (_b = (_a = response === null || response === void 0 ? void 0 : response.payload) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.toString();
|
|
105
|
+
});
|
|
83
106
|
}
|
|
84
|
-
static
|
|
85
|
-
return
|
|
107
|
+
static getSecrets(names, versions) {
|
|
108
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
109
|
+
return Promise.all(names.map((name, idx) => __awaiter(this, void 0, void 0, function* () { return this.getSecret(name, versions === null || versions === void 0 ? void 0 : versions[idx]); })));
|
|
110
|
+
});
|
|
86
111
|
}
|
|
87
112
|
}
|
|
88
113
|
exports.GcfCommon = GcfCommon;
|
package/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {PubSub} from "@google-cloud/pubsub";
|
|
2
2
|
import {SecretManagerServiceClient} from "@google-cloud/secret-manager";
|
|
3
3
|
import {File, Storage} from "@google-cloud/storage";
|
|
4
|
-
import isEmpty from "lodash
|
|
5
|
-
import noop from "lodash
|
|
4
|
+
import isEmpty from "lodash/isEmpty";
|
|
5
|
+
import noop from "lodash/noop";
|
|
6
6
|
import {timeoutAfter} from "./utils";
|
|
7
7
|
|
|
8
8
|
export type TEvent = {
|
package/package.json
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gcf-common-lib",
|
|
3
3
|
"description": "",
|
|
4
|
-
"version": "0.11.
|
|
4
|
+
"version": "0.11.1",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
7
7
|
"branches": [
|
|
8
8
|
"master"
|
|
9
9
|
]
|
|
10
10
|
},
|
|
11
|
-
"engines": {
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": "14",
|
|
13
|
+
"npm": "8"
|
|
14
|
+
},
|
|
12
15
|
"scripts": {
|
|
13
16
|
"test": "echo \"Error: no test specified\" || exit 0"
|
|
14
17
|
},
|
|
@@ -20,13 +23,12 @@
|
|
|
20
23
|
"@google-cloud/pubsub": "^2.18.5",
|
|
21
24
|
"@google-cloud/secret-manager": "^3.10.1",
|
|
22
25
|
"@google-cloud/storage": "^5.18.1",
|
|
23
|
-
"lodash
|
|
26
|
+
"lodash": "^4.17.21"
|
|
24
27
|
},
|
|
25
28
|
"devDependencies": {
|
|
26
29
|
"@tsconfig/node14": "^1.0.1",
|
|
27
|
-
"@types/lodash
|
|
30
|
+
"@types/lodash": "^4.14.178"
|
|
28
31
|
},
|
|
29
|
-
"keywords": [],
|
|
30
32
|
"author": "alert83@gmail.com",
|
|
31
33
|
"license": "MIT"
|
|
32
34
|
}
|
package/tsconfig.json
CHANGED
package/utils.js
CHANGED
|
@@ -1,15 +1,26 @@
|
|
|
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
13
|
/**
|
|
5
14
|
*
|
|
6
15
|
* @param seconds Google function timeout limit (max: 9 min)
|
|
7
16
|
*/
|
|
8
|
-
|
|
9
|
-
return
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
17
|
+
function timeoutAfter(seconds = 540) {
|
|
18
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
setTimeout(() => {
|
|
21
|
+
reject(new Error(`${seconds} seconds timeout exceeded`));
|
|
22
|
+
}, seconds * 1000);
|
|
23
|
+
});
|
|
13
24
|
});
|
|
14
25
|
}
|
|
15
26
|
exports.timeoutAfter = timeoutAfter;
|