gcf-common-lib 0.4.0 → 0.5.5
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/.github/workflows/npm-publish.yml +34 -0
- package/index.js +169 -112
- package/index.ts +50 -12
- package/package.json +3 -2
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
|
|
2
|
+
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
|
|
3
|
+
|
|
4
|
+
name: Node.js Package
|
|
5
|
+
on: [ push ]
|
|
6
|
+
|
|
7
|
+
# on:
|
|
8
|
+
# release:
|
|
9
|
+
# types: [created]
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
build:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v2
|
|
16
|
+
- uses: actions/setup-node@v2
|
|
17
|
+
with:
|
|
18
|
+
node-version: 14
|
|
19
|
+
- run: npm ci
|
|
20
|
+
- run: npm test
|
|
21
|
+
|
|
22
|
+
publish-npm:
|
|
23
|
+
needs: build
|
|
24
|
+
runs-on: ubuntu-latest
|
|
25
|
+
steps:
|
|
26
|
+
- uses: actions/checkout@v2
|
|
27
|
+
- uses: actions/setup-node@v2
|
|
28
|
+
with:
|
|
29
|
+
node-version: 14
|
|
30
|
+
registry-url: https://registry.npmjs.org/
|
|
31
|
+
- run: npm ci
|
|
32
|
+
- run: npm publish
|
|
33
|
+
env:
|
|
34
|
+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
|
package/index.js
CHANGED
|
@@ -1,112 +1,169 @@
|
|
|
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
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
*
|
|
96
|
-
* @param
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
+
/**
|
|
47
|
+
*
|
|
48
|
+
* @param {!Object} event Event payload.
|
|
49
|
+
* @param {!Object} context Metadata for the event.
|
|
50
|
+
* @param handler
|
|
51
|
+
* @param timeout Seconds
|
|
52
|
+
*/
|
|
53
|
+
GcfCommon.process = function (event, context, handler, timeout) {
|
|
54
|
+
if (timeout === void 0) { timeout = 535; }
|
|
55
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
56
|
+
var _this = this;
|
|
57
|
+
return __generator(this, function (_a) {
|
|
58
|
+
return [2 /*return*/, Promise.race([
|
|
59
|
+
this.delay(535),
|
|
60
|
+
handler(event, context),
|
|
61
|
+
])
|
|
62
|
+
.then(function (res) { return __awaiter(_this, void 0, void 0, function () {
|
|
63
|
+
return __generator(this, function (_a) {
|
|
64
|
+
switch (_a.label) {
|
|
65
|
+
case 0:
|
|
66
|
+
// console.log('res:', res);
|
|
67
|
+
return [4 /*yield*/, this.publish(event, context, res !== null && res !== void 0 ? res : {})];
|
|
68
|
+
case 1:
|
|
69
|
+
// console.log('res:', res);
|
|
70
|
+
_a.sent();
|
|
71
|
+
return [2 /*return*/];
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}); })
|
|
75
|
+
.catch(function (err) { return __awaiter(_this, void 0, void 0, function () {
|
|
76
|
+
var fname, dataJSON;
|
|
77
|
+
var _a, _b;
|
|
78
|
+
return __generator(this, function (_c) {
|
|
79
|
+
switch (_c.label) {
|
|
80
|
+
case 0:
|
|
81
|
+
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';
|
|
82
|
+
dataJSON = { error: { name: err.name, message: "GCF [" + fname + "]: " + err.message, stack: err.stack } };
|
|
83
|
+
return [4 /*yield*/, this.publish(event, context, dataJSON).catch(lodash_1.noop)];
|
|
84
|
+
case 1:
|
|
85
|
+
_c.sent();
|
|
86
|
+
throw err;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}); })];
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
*
|
|
95
|
+
* @param {!Object} event Event payload.
|
|
96
|
+
* @param {!Object} context Metadata for the event.
|
|
97
|
+
* @param json
|
|
98
|
+
*/
|
|
99
|
+
GcfCommon.publish = function (event, context, json) {
|
|
100
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
101
|
+
var topic;
|
|
102
|
+
return __generator(this, function (_a) {
|
|
103
|
+
switch (_a.label) {
|
|
104
|
+
case 0: return [4 /*yield*/, this.getTopic(event, context)];
|
|
105
|
+
case 1:
|
|
106
|
+
topic = _a.sent();
|
|
107
|
+
console.log('publish:', topic === null || topic === void 0 ? void 0 : topic.name, json);
|
|
108
|
+
if (!topic) return [3 /*break*/, 3];
|
|
109
|
+
return [4 /*yield*/, topic.publishMessage({ json: json })];
|
|
110
|
+
case 2: return [2 /*return*/, _a.sent()];
|
|
111
|
+
case 3: return [2 /*return*/];
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
*
|
|
118
|
+
* @param {!Object} event Event payload.
|
|
119
|
+
* @param {!Object} context Metadata for the event.
|
|
120
|
+
*/
|
|
121
|
+
GcfCommon.getTopic = function (event, context) {
|
|
122
|
+
var _a, _b, _c;
|
|
123
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
124
|
+
var pubSub, topicName, storage, file, meta, topic;
|
|
125
|
+
return __generator(this, function (_d) {
|
|
126
|
+
switch (_d.label) {
|
|
127
|
+
case 0:
|
|
128
|
+
pubSub = new pubsub_1.PubSub();
|
|
129
|
+
topicName = (_a = event === null || event === void 0 ? void 0 : event.metadata) === null || _a === void 0 ? void 0 : _a.topic;
|
|
130
|
+
if (!(!topicName && ((_b = context === null || context === void 0 ? void 0 : context.resource) === null || _b === void 0 ? void 0 : _b.type) === 'storage#object')) return [3 /*break*/, 2];
|
|
131
|
+
storage = new storage_1.Storage();
|
|
132
|
+
file = storage.bucket(event.bucket).file(event.name);
|
|
133
|
+
return [4 /*yield*/, file.getMetadata()];
|
|
134
|
+
case 1:
|
|
135
|
+
meta = (_d.sent())[0];
|
|
136
|
+
topicName = (_c = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _c === void 0 ? void 0 : _c.topic;
|
|
137
|
+
console.log('topic:', topicName);
|
|
138
|
+
_d.label = 2;
|
|
139
|
+
case 2:
|
|
140
|
+
if (!!(0, lodash_1.isEmpty)(topicName)) return [3 /*break*/, 4];
|
|
141
|
+
topic = pubSub.topic(topicName);
|
|
142
|
+
return [4 /*yield*/, topic.setMetadata({ labels: { date: topicName.split('__')[1] } })];
|
|
143
|
+
case 3:
|
|
144
|
+
_d.sent();
|
|
145
|
+
return [2 /*return*/, topic];
|
|
146
|
+
case 4: return [2 /*return*/];
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
*
|
|
153
|
+
* @param s Google function time limit (max: 9 min)
|
|
154
|
+
*/
|
|
155
|
+
GcfCommon.delay = function (s) {
|
|
156
|
+
if (s === void 0) { s = 540; }
|
|
157
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
158
|
+
return __generator(this, function (_a) {
|
|
159
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
160
|
+
setTimeout(function () {
|
|
161
|
+
reject(new Error(s + " seconds timeout exceeded"));
|
|
162
|
+
}, s * 1000);
|
|
163
|
+
})];
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
};
|
|
167
|
+
return GcfCommon;
|
|
168
|
+
}());
|
|
169
|
+
exports.GcfCommon = GcfCommon;
|
package/index.ts
CHANGED
|
@@ -1,20 +1,58 @@
|
|
|
1
1
|
import {File, Storage} from '@google-cloud/storage';
|
|
2
|
-
import {PubSub} from '@google-cloud/pubsub';
|
|
3
|
-
import {isEmpty} from 'lodash';
|
|
2
|
+
import {PubSub, Topic} from '@google-cloud/pubsub';
|
|
3
|
+
import {isEmpty, noop} from 'lodash';
|
|
4
4
|
|
|
5
5
|
export class GcfCommon {
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @param {!Object} event Event payload.
|
|
10
|
+
* @param {!Object} context Metadata for the event.
|
|
11
|
+
* @param handler
|
|
12
|
+
* @param timeout Seconds
|
|
13
|
+
*/
|
|
14
|
+
static async process(event, context, handler: (event, context) => Promise<any>, timeout = 535) {
|
|
15
|
+
return Promise.race([
|
|
16
|
+
this.delay(535),
|
|
17
|
+
handler(event, context),
|
|
18
|
+
])
|
|
19
|
+
.then(async (res: any) => {
|
|
20
|
+
// console.log('res:', res);
|
|
21
|
+
await this.publish(event, context, res ?? {});
|
|
22
|
+
})
|
|
23
|
+
.catch(async (err: Error) => {
|
|
24
|
+
const fname = process?.env?.K_SERVICE ?? 'UNKNOWN';
|
|
25
|
+
const dataJSON = {error: {name: err.name, message: `GCF [${fname}]: ${err.message}`, stack: err.stack}};
|
|
26
|
+
await this.publish(event, context, dataJSON).catch(noop);
|
|
27
|
+
throw err;
|
|
28
|
+
})
|
|
29
|
+
;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
*
|
|
34
|
+
* @param {!Object} event Event payload.
|
|
35
|
+
* @param {!Object} context Metadata for the event.
|
|
36
|
+
* @param json
|
|
37
|
+
*/
|
|
6
38
|
static async publish(event, context, json) {
|
|
7
39
|
const topic = await this.getTopic(event, context);
|
|
8
40
|
console.log('publish:', topic?.name, json);
|
|
9
|
-
if (topic) await topic.publishMessage({json});
|
|
41
|
+
if (topic) return await topic.publishMessage({json});
|
|
10
42
|
}
|
|
11
43
|
|
|
12
|
-
|
|
44
|
+
/**
|
|
45
|
+
*
|
|
46
|
+
* @param {!Object} event Event payload.
|
|
47
|
+
* @param {!Object} context Metadata for the event.
|
|
48
|
+
*/
|
|
49
|
+
static async getTopic(event, context): Promise<Topic | undefined> {
|
|
13
50
|
const pubSub = new PubSub();
|
|
51
|
+
|
|
14
52
|
/** t_{GUID}__{YYYY-MM-DD} */
|
|
15
|
-
let topicName: string;
|
|
53
|
+
let topicName: string | undefined = event?.metadata?.topic;
|
|
16
54
|
|
|
17
|
-
if (context?.resource?.type === 'storage#object') {
|
|
55
|
+
if (!topicName && context?.resource?.type === 'storage#object') {
|
|
18
56
|
const storage = new Storage();
|
|
19
57
|
const file: File = storage.bucket(event.bucket).file(event.name);
|
|
20
58
|
const [meta] = await file.getMetadata();
|
|
@@ -22,16 +60,16 @@ export class GcfCommon {
|
|
|
22
60
|
console.log('topic:', topicName);
|
|
23
61
|
}
|
|
24
62
|
|
|
25
|
-
if (isEmpty(topicName))
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
63
|
+
if (!isEmpty(topicName)) {
|
|
64
|
+
const topic = pubSub.topic(topicName);
|
|
65
|
+
await topic.setMetadata({labels: {date: topicName.split('__')[1]}});
|
|
66
|
+
return topic;
|
|
67
|
+
}
|
|
30
68
|
}
|
|
31
69
|
|
|
32
70
|
/**
|
|
33
71
|
*
|
|
34
|
-
* @param s
|
|
72
|
+
* @param s Google function time limit (max: 9 min)
|
|
35
73
|
*/
|
|
36
74
|
static async delay(s: number = 540) {
|
|
37
75
|
return new Promise<undefined>((resolve, reject) => {
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gcf-common-lib",
|
|
3
3
|
"description": "",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.5.5",
|
|
5
|
+
"publishConfig": { "access": "public", "branches": ["master"] },
|
|
5
6
|
"engines": {},
|
|
6
7
|
"scripts": {
|
|
7
|
-
"test": "echo \"Error: no test specified\"
|
|
8
|
+
"test": "echo \"Error: no test specified\" || exit 0"
|
|
8
9
|
},
|
|
9
10
|
"repository": {
|
|
10
11
|
"type": "git",
|