gcf-common-lib 0.10.0 → 0.12.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 +31 -10
- package/index.ts +30 -12
- package/package.json +9 -7
- package/tsconfig.json +1 -14
- package/utils.js +12 -1
package/index.js
CHANGED
|
@@ -8,17 +8,28 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
11
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
15
|
exports.GcfCommon = exports.storage = exports.pubSub = void 0;
|
|
13
16
|
const pubsub_1 = require("@google-cloud/pubsub");
|
|
14
17
|
const secret_manager_1 = require("@google-cloud/secret-manager");
|
|
15
18
|
const storage_1 = require("@google-cloud/storage");
|
|
16
|
-
const
|
|
19
|
+
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
|
|
20
|
+
const noop_1 = __importDefault(require("lodash/noop"));
|
|
17
21
|
const utils_1 = require("./utils");
|
|
18
22
|
exports.pubSub = new pubsub_1.PubSub();
|
|
19
23
|
exports.storage = new storage_1.Storage();
|
|
20
24
|
const secretClient = new secret_manager_1.SecretManagerServiceClient();
|
|
21
25
|
class GcfCommon {
|
|
26
|
+
/**
|
|
27
|
+
*
|
|
28
|
+
* @param {!TEvent} event Event payload.
|
|
29
|
+
* @param {!TContext} context Metadata for the event.
|
|
30
|
+
* @param handler
|
|
31
|
+
* @param timeout Seconds
|
|
32
|
+
*/
|
|
22
33
|
static process(event, context, handler, timeout = 535) {
|
|
23
34
|
return __awaiter(this, void 0, void 0, function* () {
|
|
24
35
|
return Promise.race([
|
|
@@ -26,6 +37,7 @@ class GcfCommon {
|
|
|
26
37
|
handler(event, context),
|
|
27
38
|
])
|
|
28
39
|
.then((res) => __awaiter(this, void 0, void 0, function* () {
|
|
40
|
+
// console.log('res:', res);
|
|
29
41
|
yield this.publish(event, context, res !== null && res !== void 0 ? res : {});
|
|
30
42
|
}))
|
|
31
43
|
.catch((err) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -38,7 +50,7 @@ class GcfCommon {
|
|
|
38
50
|
stack: err.stack,
|
|
39
51
|
},
|
|
40
52
|
};
|
|
41
|
-
yield this.publish(event, context, response).catch(
|
|
53
|
+
yield this.publish(event, context, response).catch(noop_1.default);
|
|
42
54
|
throw err;
|
|
43
55
|
}));
|
|
44
56
|
});
|
|
@@ -52,16 +64,25 @@ class GcfCommon {
|
|
|
52
64
|
});
|
|
53
65
|
}
|
|
54
66
|
static getTopic(event, context) {
|
|
55
|
-
var _a, _b, _c;
|
|
67
|
+
var _a, _b, _c, _d;
|
|
56
68
|
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const
|
|
61
|
-
topicName = (
|
|
62
|
-
|
|
69
|
+
/** t_{GUID}__{YYYY-MM-DD} */
|
|
70
|
+
let topicName;
|
|
71
|
+
if ((context === null || context === void 0 ? void 0 : context.eventType) === 'google.storage.object.finalize') {
|
|
72
|
+
const gsEvent = event;
|
|
73
|
+
topicName = (_a = gsEvent === null || gsEvent === void 0 ? void 0 : gsEvent.metadata) === null || _a === void 0 ? void 0 : _a.topic;
|
|
74
|
+
if (!topicName && ((_b = context === null || context === void 0 ? void 0 : context.resource) === null || _b === void 0 ? void 0 : _b.type) === 'storage#object') {
|
|
75
|
+
const file = exports.storage.bucket(gsEvent.bucket).file(gsEvent.name);
|
|
76
|
+
const [meta] = yield file.getMetadata();
|
|
77
|
+
topicName = (_c = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _c === void 0 ? void 0 : _c.topic;
|
|
78
|
+
console.log('topic:', topicName);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else if ((context === null || context === void 0 ? void 0 : context.eventType) === 'google.pubsub.topic.publish') {
|
|
82
|
+
const psEvent = event;
|
|
83
|
+
topicName = (_d = psEvent.data) === null || _d === void 0 ? void 0 : _d.topic;
|
|
63
84
|
}
|
|
64
|
-
if (topicName && !(0,
|
|
85
|
+
if (topicName && !(0, isEmpty_1.default)(topicName)) {
|
|
65
86
|
const topic = exports.pubSub.topic(topicName);
|
|
66
87
|
yield topic.setMetadata({ labels: { date: topicName.split('__')[1] } });
|
|
67
88
|
return topic;
|
package/index.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
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
|
|
4
|
+
import isEmpty from "lodash/isEmpty";
|
|
5
|
+
import noop from "lodash/noop";
|
|
5
6
|
import {timeoutAfter} from "./utils";
|
|
6
7
|
|
|
7
|
-
export type
|
|
8
|
+
export type TGSEvent = {
|
|
8
9
|
bucket: string;
|
|
9
10
|
contentType: string;
|
|
10
11
|
crc32c: string;
|
|
@@ -25,14 +26,22 @@ export type TEvent = {
|
|
|
25
26
|
updated: string;
|
|
26
27
|
}
|
|
27
28
|
|
|
29
|
+
export type TPSEvent = {
|
|
30
|
+
'@type': string; // 'type.googleapis.com/google.pubsub.v1.PubsubMessage'
|
|
31
|
+
attributes?: string | null;
|
|
32
|
+
data: string | { [k: string]: any };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type TEvent = TGSEvent | TPSEvent;
|
|
36
|
+
|
|
28
37
|
export type TContext = {
|
|
29
38
|
eventId: string;
|
|
30
39
|
timestamp: string;
|
|
31
|
-
eventType:
|
|
40
|
+
eventType: 'google.storage.object.finalize' | 'google.pubsub.topic.publish';
|
|
32
41
|
resource: {
|
|
33
|
-
service:
|
|
42
|
+
service: 'storage.googleapis.com' | 'pubsub.googleapis.com';
|
|
34
43
|
name: string;
|
|
35
|
-
type: string; // 'storage#object'
|
|
44
|
+
type: string; // 'storage#object' | 'type.googleapis.com/google.pubsub.v1.PubsubMessage'
|
|
36
45
|
}
|
|
37
46
|
}
|
|
38
47
|
|
|
@@ -95,13 +104,22 @@ export class GcfCommon {
|
|
|
95
104
|
static async getTopic(event: TEvent, context: TContext) {
|
|
96
105
|
|
|
97
106
|
/** t_{GUID}__{YYYY-MM-DD} */
|
|
98
|
-
let topicName: string | undefined
|
|
107
|
+
let topicName: string | undefined;
|
|
99
108
|
|
|
100
|
-
if (
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
topicName =
|
|
104
|
-
|
|
109
|
+
if (context?.eventType === 'google.storage.object.finalize') {
|
|
110
|
+
const gsEvent = event as TGSEvent;
|
|
111
|
+
|
|
112
|
+
topicName = gsEvent?.metadata?.topic;
|
|
113
|
+
|
|
114
|
+
if (!topicName && context?.resource?.type === 'storage#object') {
|
|
115
|
+
const file: File = storage.bucket(gsEvent.bucket).file(gsEvent.name);
|
|
116
|
+
const [meta] = await file.getMetadata();
|
|
117
|
+
topicName = meta?.metadata?.topic;
|
|
118
|
+
console.log('topic:', topicName);
|
|
119
|
+
}
|
|
120
|
+
} else if (context?.eventType === 'google.pubsub.topic.publish') {
|
|
121
|
+
const psEvent = event as TPSEvent;
|
|
122
|
+
topicName = (psEvent.data as any)?.topic;
|
|
105
123
|
}
|
|
106
124
|
|
|
107
125
|
if (topicName && !isEmpty(topicName)) {
|
|
@@ -111,7 +129,7 @@ export class GcfCommon {
|
|
|
111
129
|
}
|
|
112
130
|
}
|
|
113
131
|
|
|
114
|
-
static async getOptions(event:
|
|
132
|
+
static async getOptions(event: TGSEvent, context: TContext) {
|
|
115
133
|
if (event?.metadata?.options) {
|
|
116
134
|
return JSON.parse(event?.metadata?.options ?? '{}');
|
|
117
135
|
} else {
|
package/package.json
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gcf-common-lib",
|
|
3
3
|
"description": "",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.12.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
|
},
|
|
@@ -17,16 +20,15 @@
|
|
|
17
20
|
"url": "https://github.com/TopTechnologies/gcf-common.git"
|
|
18
21
|
},
|
|
19
22
|
"dependencies": {
|
|
20
|
-
"@google-cloud/pubsub": "^2.
|
|
21
|
-
"@google-cloud/secret-manager": "^3.
|
|
22
|
-
"@google-cloud/storage": "^5.18.
|
|
23
|
+
"@google-cloud/pubsub": "^2.19.0",
|
|
24
|
+
"@google-cloud/secret-manager": "^3.11.0",
|
|
25
|
+
"@google-cloud/storage": "^5.18.2",
|
|
23
26
|
"lodash": "^4.17.21"
|
|
24
27
|
},
|
|
25
28
|
"devDependencies": {
|
|
26
29
|
"@tsconfig/node14": "^1.0.1",
|
|
27
|
-
"@types/lodash": "^4.14.
|
|
30
|
+
"@types/lodash": "^4.14.179"
|
|
28
31
|
},
|
|
29
|
-
"keywords": [],
|
|
30
32
|
"author": "alert83@gmail.com",
|
|
31
33
|
"license": "MIT"
|
|
32
34
|
}
|
package/tsconfig.json
CHANGED
|
@@ -2,28 +2,15 @@
|
|
|
2
2
|
"$schema": "http://json.schemastore.org/tsconfig",
|
|
3
3
|
"extends": "@tsconfig/node14/tsconfig.json",
|
|
4
4
|
"compilerOptions": {
|
|
5
|
-
"allowJs": false,
|
|
6
|
-
"removeComments": true,
|
|
7
|
-
"esModuleInterop": true,
|
|
8
|
-
"allowSyntheticDefaultImports": true,
|
|
9
|
-
"isolatedModules": false,
|
|
10
|
-
"emitDecoratorMetadata": true,
|
|
11
|
-
"experimentalDecorators": true,
|
|
12
|
-
"module": "commonjs",
|
|
13
5
|
"moduleResolution": "node",
|
|
14
6
|
"target": "ES6",
|
|
15
|
-
"typeRoots": [
|
|
16
|
-
"node_modules/@types"
|
|
17
|
-
],
|
|
18
7
|
"lib": [
|
|
19
8
|
"ES2018",
|
|
20
9
|
"ES2019",
|
|
21
10
|
"ES2020",
|
|
22
11
|
"ES2021",
|
|
23
12
|
"ESNext"
|
|
24
|
-
]
|
|
25
|
-
"types": [],
|
|
26
|
-
"plugins": []
|
|
13
|
+
]
|
|
27
14
|
},
|
|
28
15
|
"exclude": [
|
|
29
16
|
"node_modules/**",
|
package/utils.js
CHANGED
|
@@ -10,6 +10,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.A1ToColNum = exports.A1ToIndex = exports.colNumToA1 = exports.indexToA1 = exports.timeoutAfter = void 0;
|
|
13
|
+
/**
|
|
14
|
+
*
|
|
15
|
+
* @param seconds Google function timeout limit (max: 9 min)
|
|
16
|
+
*/
|
|
13
17
|
function timeoutAfter(seconds = 540) {
|
|
14
18
|
return __awaiter(this, void 0, void 0, function* () {
|
|
15
19
|
return new Promise((resolve, reject) => {
|
|
@@ -20,24 +24,30 @@ function timeoutAfter(seconds = 540) {
|
|
|
20
24
|
});
|
|
21
25
|
}
|
|
22
26
|
exports.timeoutAfter = timeoutAfter;
|
|
27
|
+
//
|
|
23
28
|
function indexToA1(idx) {
|
|
24
29
|
return colNumToA1(idx + 1);
|
|
25
30
|
}
|
|
26
31
|
exports.indexToA1 = indexToA1;
|
|
27
32
|
function colNumToA1(columnNumber) {
|
|
28
33
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
34
|
+
// To store result (Excel column name)
|
|
29
35
|
const charIdxArr = [];
|
|
30
36
|
while (columnNumber > 0) {
|
|
37
|
+
// Find remainder
|
|
31
38
|
const rem = columnNumber % chars.length;
|
|
39
|
+
// If remainder is 0, then a
|
|
40
|
+
// 'Z' must be there in output
|
|
32
41
|
if (rem === 0) {
|
|
33
42
|
charIdxArr.push(chars.length - 1);
|
|
34
43
|
columnNumber = Math.floor(columnNumber / chars.length) - 1;
|
|
35
44
|
}
|
|
36
|
-
else {
|
|
45
|
+
else { // If remainder is non-zero
|
|
37
46
|
charIdxArr.push(rem - 1);
|
|
38
47
|
columnNumber = Math.floor(columnNumber / chars.length);
|
|
39
48
|
}
|
|
40
49
|
}
|
|
50
|
+
// Reverse the string and print result
|
|
41
51
|
return charIdxArr.reverse().map((n) => chars[n]).join('');
|
|
42
52
|
}
|
|
43
53
|
exports.colNumToA1 = colNumToA1;
|
|
@@ -48,6 +58,7 @@ exports.A1ToIndex = A1ToIndex;
|
|
|
48
58
|
function A1ToColNum(value) {
|
|
49
59
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
50
60
|
let result = 0;
|
|
61
|
+
// tslint:disable-next-line:prefer-for-of
|
|
51
62
|
for (let i = 0; i < value.length; i++) {
|
|
52
63
|
result *= chars.length;
|
|
53
64
|
result += chars.indexOf(value[i]) + 1;
|