gcf-common-lib 0.4.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 (4) hide show
  1. package/README.md +1 -0
  2. package/index.js +112 -0
  3. package/index.ts +43 -0
  4. package/package.json +24 -0
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # gcf-common
package/index.js ADDED
@@ -0,0 +1,112 @@
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
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.GcfCommon = void 0;
40
+ var storage_1 = require("@google-cloud/storage");
41
+ var pubsub_1 = require("@google-cloud/pubsub");
42
+ var lodash_1 = require("lodash");
43
+ var GcfCommon = /** @class */ (function () {
44
+ function GcfCommon() {
45
+ }
46
+ GcfCommon.publish = function (event, context, json) {
47
+ return __awaiter(this, void 0, void 0, function () {
48
+ var topic;
49
+ return __generator(this, function (_a) {
50
+ switch (_a.label) {
51
+ case 0: return [4 /*yield*/, this.getTopic(event, context)];
52
+ case 1:
53
+ topic = _a.sent();
54
+ console.log('publish:', topic === null || topic === void 0 ? void 0 : topic.name, json);
55
+ if (!topic) return [3 /*break*/, 3];
56
+ return [4 /*yield*/, topic.publishMessage({ json: json })];
57
+ case 2:
58
+ _a.sent();
59
+ _a.label = 3;
60
+ case 3: return [2 /*return*/];
61
+ }
62
+ });
63
+ });
64
+ };
65
+ GcfCommon.getTopic = function (event, context) {
66
+ var _a, _b;
67
+ return __awaiter(this, void 0, void 0, function () {
68
+ var pubSub, topicName, storage, file, meta, tpc;
69
+ return __generator(this, function (_c) {
70
+ switch (_c.label) {
71
+ case 0:
72
+ pubSub = new pubsub_1.PubSub();
73
+ 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];
74
+ storage = new storage_1.Storage();
75
+ file = storage.bucket(event.bucket).file(event.name);
76
+ return [4 /*yield*/, file.getMetadata()];
77
+ case 1:
78
+ meta = (_c.sent())[0];
79
+ topicName = (_b = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _b === void 0 ? void 0 : _b.topic;
80
+ console.log('topic:', topicName);
81
+ _c.label = 2;
82
+ case 2:
83
+ if ((0, lodash_1.isEmpty)(topicName))
84
+ return [2 /*return*/];
85
+ tpc = pubSub.topic(topicName);
86
+ return [4 /*yield*/, tpc.setMetadata({ labels: { date: topicName.split('__')[1] } })];
87
+ case 3:
88
+ _c.sent();
89
+ return [2 /*return*/, tpc];
90
+ }
91
+ });
92
+ });
93
+ };
94
+ /**
95
+ *
96
+ * @param s (google function limit max = 9 min)
97
+ */
98
+ GcfCommon.delay = function (s) {
99
+ if (s === void 0) { s = 540; }
100
+ return __awaiter(this, void 0, void 0, function () {
101
+ return __generator(this, function (_a) {
102
+ return [2 /*return*/, new Promise(function (resolve, reject) {
103
+ setTimeout(function () {
104
+ reject(new Error(s + " seconds timeout exceeded"));
105
+ }, s * 1000);
106
+ })];
107
+ });
108
+ });
109
+ };
110
+ return GcfCommon;
111
+ }());
112
+ exports.GcfCommon = GcfCommon;
package/index.ts ADDED
@@ -0,0 +1,43 @@
1
+ import {File, Storage} from '@google-cloud/storage';
2
+ import {PubSub} from '@google-cloud/pubsub';
3
+ import {isEmpty} from 'lodash';
4
+
5
+ export class GcfCommon {
6
+ static async publish(event, context, json) {
7
+ const topic = await this.getTopic(event, context);
8
+ console.log('publish:', topic?.name, json);
9
+ if (topic) await topic.publishMessage({json});
10
+ }
11
+
12
+ static async getTopic(event, context) {
13
+ const pubSub = new PubSub();
14
+ /** t_{GUID}__{YYYY-MM-DD} */
15
+ let topicName: string;
16
+
17
+ if (context?.resource?.type === 'storage#object') {
18
+ const storage = new Storage();
19
+ const file: File = storage.bucket(event.bucket).file(event.name);
20
+ const [meta] = await file.getMetadata();
21
+ topicName = meta?.metadata?.topic;
22
+ console.log('topic:', topicName);
23
+ }
24
+
25
+ if (isEmpty(topicName)) return;
26
+
27
+ const tpc = pubSub.topic(topicName);
28
+ await tpc.setMetadata({labels: {date: topicName.split('__')[1]}});
29
+ return tpc;
30
+ }
31
+
32
+ /**
33
+ *
34
+ * @param s (google function limit max = 9 min)
35
+ */
36
+ static async delay(s: number = 540) {
37
+ return new Promise<undefined>((resolve, reject) => {
38
+ setTimeout(() => {
39
+ reject(new Error(`${s} seconds timeout exceeded`));
40
+ }, s * 1000);
41
+ });
42
+ }
43
+ }
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "gcf-common-lib",
3
+ "description": "",
4
+ "version": "0.4.0",
5
+ "engines": {},
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/TopTechnologies/gcf-common.git"
12
+ },
13
+ "dependencies": {
14
+ "@google-cloud/pubsub": "^2.18.4",
15
+ "@google-cloud/storage": "^5.16.1",
16
+ "lodash": "^4.17.21"
17
+ },
18
+ "devDependencies": {
19
+ "@types/lodash": "^4.14.178"
20
+ },
21
+ "keywords": [],
22
+ "author": "alert83@gmail.com",
23
+ "license": "MIT"
24
+ }