gcf-common-lib 0.23.20 → 0.23.21
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/package.json +4 -2
- package/src/index.js +1 -1
- package/src/index.ts +1 -1
- package/src/mongo-helper.js +10 -9
- package/src/mongo-helper.ts +7 -9
- package/src/mongo-lock.js +11 -1
- package/src/mongo-lock.ts +16 -3
- package/src/utils.js +19 -9
- package/src/utils.ts +19 -9
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gcf-common-lib",
|
|
3
3
|
"description": "",
|
|
4
|
-
"version": "0.23.
|
|
4
|
+
"version": "0.23.21",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
7
7
|
"branches": [
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
"@google-cloud/pubsub": "^3.3.0",
|
|
22
22
|
"@google-cloud/secret-manager": "^4.2.1",
|
|
23
23
|
"@google-cloud/storage": "^6.9.4",
|
|
24
|
-
"@types/amqplib": "^0.10.1",
|
|
25
24
|
"amqplib": "^0.10.3",
|
|
25
|
+
"bluebird": "^3.7.2",
|
|
26
26
|
"lodash": "^4.17.21",
|
|
27
27
|
"moment": "^2.29.4",
|
|
28
28
|
"mongodb": "^4.14.0",
|
|
@@ -30,6 +30,8 @@
|
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@tsconfig/node14": "^1.0.3",
|
|
33
|
+
"@types/amqplib": "^0.10.1",
|
|
34
|
+
"@types/bluebird": "^3.5.38",
|
|
33
35
|
"@types/lodash": "^4.14.191",
|
|
34
36
|
"@types/node": "^14.18.37"
|
|
35
37
|
},
|
package/src/index.js
CHANGED
|
@@ -98,7 +98,7 @@ class GcfCommon {
|
|
|
98
98
|
}
|
|
99
99
|
if (queue && !(0, isEmpty_1.default)(queue)) {
|
|
100
100
|
console.log('send:', queue, appId, env, json, attributes);
|
|
101
|
-
return
|
|
101
|
+
return (0, utils_1.withAmqpCh)((ch) => __awaiter(this, void 0, void 0, function* () {
|
|
102
102
|
const payload = Buffer.from(JSON.stringify(json !== null && json !== void 0 ? json : {}));
|
|
103
103
|
yield ch.assertQueue(queue, this.amqpOptions.assertOptions);
|
|
104
104
|
ch.sendToQueue(queue, payload, this.amqpOptions.publishOptions);
|
package/src/index.ts
CHANGED
|
@@ -83,7 +83,7 @@ export class GcfCommon {
|
|
|
83
83
|
|
|
84
84
|
if (queue && !isEmpty(queue)) {
|
|
85
85
|
console.log('send:', queue, appId, env, json, attributes);
|
|
86
|
-
return
|
|
86
|
+
return withAmqpCh(async ch => {
|
|
87
87
|
const payload = Buffer.from(JSON.stringify(json ?? {}));
|
|
88
88
|
await ch.assertQueue(queue, this.amqpOptions.assertOptions);
|
|
89
89
|
ch.sendToQueue(queue, payload, this.amqpOptions.publishOptions);
|
package/src/mongo-helper.js
CHANGED
|
@@ -8,9 +8,13 @@ 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.MongoHelper = void 0;
|
|
13
16
|
const mongodb_1 = require("mongodb");
|
|
17
|
+
const bluebird_1 = __importDefault(require("bluebird"));
|
|
14
18
|
class MongoHelper {
|
|
15
19
|
static collectionExists(client, name) {
|
|
16
20
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -35,15 +39,12 @@ class MongoHelper {
|
|
|
35
39
|
}
|
|
36
40
|
withMongoClient(fn, url, mongoClientOptions) {
|
|
37
41
|
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
.
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
.finally(() => __awaiter(this, void 0, void 0, function* () {
|
|
45
|
-
yield mongoClient.close();
|
|
46
|
-
}));
|
|
42
|
+
function withDisposer() {
|
|
43
|
+
return bluebird_1.default.method(() => __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
return new mongodb_1.MongoClient(url, mongoClientOptions).connect();
|
|
45
|
+
}))().disposer((client, promise) => client.close());
|
|
46
|
+
}
|
|
47
|
+
return bluebird_1.default.using(withDisposer(), (client) => fn(client));
|
|
47
48
|
});
|
|
48
49
|
}
|
|
49
50
|
}
|
package/src/mongo-helper.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Collection, CreateCollectionOptions, MongoClient, MongoClientOptions } from 'mongodb';
|
|
2
2
|
import { Document } from 'bson';
|
|
3
|
+
import Bluebird from 'bluebird';
|
|
3
4
|
|
|
4
5
|
export class MongoHelper {
|
|
5
6
|
static async collectionExists(client: MongoClient, name: string) {
|
|
@@ -28,15 +29,12 @@ export class MongoHelper {
|
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
async withMongoClient(fn: (db: MongoClient) => Promise<void>, url: string, mongoClientOptions?: MongoClientOptions) {
|
|
31
|
-
|
|
32
|
+
function withDisposer() {
|
|
33
|
+
return Bluebird.method(async () => {
|
|
34
|
+
return new MongoClient(url, mongoClientOptions).connect();
|
|
35
|
+
})().disposer((client, promise) => client.close());
|
|
36
|
+
}
|
|
32
37
|
|
|
33
|
-
return
|
|
34
|
-
.connect()
|
|
35
|
-
.then(async client => {
|
|
36
|
-
return await fn(client);
|
|
37
|
-
})
|
|
38
|
-
.finally(async () => {
|
|
39
|
-
await mongoClient.close();
|
|
40
|
-
});
|
|
38
|
+
return Bluebird.using(withDisposer(), (client) => fn(client));
|
|
41
39
|
}
|
|
42
40
|
}
|
package/src/mongo-lock.js
CHANGED
|
@@ -17,6 +17,7 @@ const moment_1 = __importDefault(require("moment"));
|
|
|
17
17
|
const noop_1 = __importDefault(require("lodash/noop"));
|
|
18
18
|
const rxjs_1 = require("rxjs");
|
|
19
19
|
const mongo_helper_1 = require("./mongo-helper");
|
|
20
|
+
const bluebird_1 = __importDefault(require("bluebird"));
|
|
20
21
|
class MongoLock {
|
|
21
22
|
constructor(client) {
|
|
22
23
|
this.client = client;
|
|
@@ -74,7 +75,16 @@ class MongoLock {
|
|
|
74
75
|
// console.log(locked);
|
|
75
76
|
if (!locked)
|
|
76
77
|
throw new Error('Could not acquire lock');
|
|
77
|
-
}), wait ? (0, rxjs_1.retry)({ count, delay }) : (0, rxjs_1.tap)(rxjs_1.identity), (0, rxjs_1.switchMap)(() => __awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
}), wait ? (0, rxjs_1.retry)({ count, delay }) : (0, rxjs_1.tap)(rxjs_1.identity), (0, rxjs_1.switchMap)(() => __awaiter(this, void 0, void 0, function* () {
|
|
79
|
+
const self = this;
|
|
80
|
+
function withDisposer() {
|
|
81
|
+
return bluebird_1.default.method(Promise.resolve)()
|
|
82
|
+
.disposer((ch, promise) => {
|
|
83
|
+
self.releaseLock(name).catch(noop_1.default);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return bluebird_1.default.using(withDisposer(), () => fn());
|
|
87
|
+
}))));
|
|
78
88
|
});
|
|
79
89
|
}
|
|
80
90
|
checkLock(name, waitSeconds = 0) {
|
package/src/mongo-lock.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { MongoClient, MongoError } from 'mongodb';
|
|
2
2
|
import moment from 'moment';
|
|
3
3
|
import noop from 'lodash/noop';
|
|
4
|
-
import { defer, from, identity, lastValueFrom, map,
|
|
4
|
+
import { defer, from, identity, lastValueFrom, map, retry, switchMap, tap } from 'rxjs';
|
|
5
5
|
import { MongoHelper } from './mongo-helper';
|
|
6
|
+
import Bluebird from 'bluebird';
|
|
6
7
|
|
|
7
8
|
export class MongoLock {
|
|
8
|
-
constructor(private client: MongoClient) {
|
|
9
|
+
constructor(private client: MongoClient) {
|
|
10
|
+
}
|
|
9
11
|
|
|
10
12
|
async getCollection() {
|
|
11
13
|
const locksColl = await MongoHelper.collectionSafeGet(this.client, 'locks');
|
|
@@ -62,7 +64,18 @@ export class MongoLock {
|
|
|
62
64
|
if (!locked) throw new Error('Could not acquire lock');
|
|
63
65
|
}),
|
|
64
66
|
wait ? retry({ count, delay }) : tap(identity),
|
|
65
|
-
switchMap(async () =>
|
|
67
|
+
switchMap(async () => {
|
|
68
|
+
const self = this;
|
|
69
|
+
|
|
70
|
+
function withDisposer() {
|
|
71
|
+
return Bluebird.method(Promise.resolve)()
|
|
72
|
+
.disposer((ch, promise) => {
|
|
73
|
+
self.releaseLock(name).catch(noop);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return Bluebird.using(withDisposer(), () => fn());
|
|
78
|
+
}),
|
|
66
79
|
),
|
|
67
80
|
);
|
|
68
81
|
}
|
package/src/utils.js
CHANGED
|
@@ -8,9 +8,13 @@ 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.withAmqpCh = exports.withAmqpConn = exports.A1ToColNum = exports.colNumToA1 = exports.A1ToIndex = exports.indexToA1 = exports.delay = exports.timeoutAfter = void 0;
|
|
13
16
|
const amqplib_1 = require("amqplib");
|
|
17
|
+
const bluebird_1 = __importDefault(require("bluebird"));
|
|
14
18
|
/**
|
|
15
19
|
*
|
|
16
20
|
* @param seconds Google function v1 timeout limit (max: 9 min)
|
|
@@ -79,20 +83,26 @@ function A1ToColNum(value) {
|
|
|
79
83
|
exports.A1ToColNum = A1ToColNum;
|
|
80
84
|
function withAmqpConn(fn, url) {
|
|
81
85
|
return __awaiter(this, void 0, void 0, function* () {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
86
|
+
function withDisposer() {
|
|
87
|
+
return bluebird_1.default.method(() => __awaiter(this, void 0, void 0, function* () {
|
|
88
|
+
const amqpConn = yield (0, amqplib_1.connect)(url);
|
|
89
|
+
amqpConn.on('close', () => console.info('Amqp connection closed!'));
|
|
90
|
+
return amqpConn;
|
|
91
|
+
}))().disposer((conn, promise) => conn.close());
|
|
92
|
+
}
|
|
93
|
+
return bluebird_1.default.using(withDisposer(), (conn) => fn(conn));
|
|
85
94
|
});
|
|
86
95
|
}
|
|
87
96
|
exports.withAmqpConn = withAmqpConn;
|
|
88
97
|
function withAmqpCh(fn, url) {
|
|
89
98
|
return __awaiter(this, void 0, void 0, function* () {
|
|
90
|
-
return
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
99
|
+
return withAmqpConn((conn) => __awaiter(this, void 0, void 0, function* () {
|
|
100
|
+
function withDisposer() {
|
|
101
|
+
return bluebird_1.default.method(() => __awaiter(this, void 0, void 0, function* () {
|
|
102
|
+
return conn.createChannel();
|
|
103
|
+
}))().disposer((ch, promise) => delay(1000).then(() => ch.close()));
|
|
104
|
+
}
|
|
105
|
+
return bluebird_1.default.using(withDisposer(), (ch) => fn(ch));
|
|
96
106
|
}), url);
|
|
97
107
|
});
|
|
98
108
|
}
|
package/src/utils.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Channel, connect, Connection } from 'amqplib';
|
|
2
|
+
import Bluebird from 'bluebird';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
*
|
|
@@ -67,17 +68,26 @@ export function A1ToColNum(value: string) {
|
|
|
67
68
|
}
|
|
68
69
|
|
|
69
70
|
export async function withAmqpConn(fn: (conn: Connection) => Promise<any>, url: string) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
function withDisposer() {
|
|
72
|
+
return Bluebird.method(async () => {
|
|
73
|
+
const amqpConn = await connect(url);
|
|
74
|
+
amqpConn.on('close', () => console.info('Amqp connection closed!'));
|
|
75
|
+
return amqpConn;
|
|
76
|
+
})().disposer((conn, promise) => conn.close());
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return Bluebird.using(withDisposer(), (conn) => fn(conn));
|
|
73
80
|
}
|
|
74
81
|
|
|
75
82
|
export async function withAmqpCh(fn: (ch: Channel) => Promise<any>, url: string) {
|
|
76
|
-
return
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
83
|
+
return withAmqpConn(async conn => {
|
|
84
|
+
function withDisposer() {
|
|
85
|
+
return Bluebird.method(async () => {
|
|
86
|
+
return conn.createChannel();
|
|
87
|
+
})().disposer((ch, promise) => delay(1000).then(() => ch.close()));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return Bluebird.using(withDisposer(), (ch) => fn(ch));
|
|
91
|
+
|
|
82
92
|
}, url);
|
|
83
93
|
}
|