gcf-common-lib 0.35.0 → 0.36.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.
package/src/mongo-lock.js CHANGED
@@ -1,86 +1,86 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.MongoLock = void 0;
7
- const moment_1 = __importDefault(require("moment/moment"));
8
- const noop_1 = __importDefault(require("lodash/noop"));
9
- const rxjs_1 = require("rxjs");
10
- const mongo_helper_1 = require("./mongo-helper");
11
- const bluebird_1 = __importDefault(require("bluebird"));
12
- /**
13
- * @deprecated
14
- */
15
- class MongoLock {
16
- client;
17
- constructor(client) {
18
- this.client = client;
19
- }
20
- async getCollection() {
21
- const locksColl = await mongo_helper_1.MongoHelper.collectionSafeGet(this.client, 'locks');
22
- await locksColl.indexExists('expiresAt').then(async (exists) => {
23
- if (!exists)
24
- await locksColl.createIndex({ expiresAt: 1 }, { expireAfterSeconds: 0 });
25
- return;
26
- });
27
- return locksColl;
28
- }
29
- /**
30
- * Create the MongoDB collection and an expiring index on a field named "expiresAt".
31
- *
32
- * db.createCollection('locks');
33
- * db.locks.createIndex( { "expiresAt": 1 }, { expireAfterSeconds: 0 } )
34
- **/
35
- async acquireLock(name, ttlSeconds) {
36
- const collection = await this.getCollection();
37
- // Entry gets removed automatically due to an expiry index in Mongo
38
- const expiresAt = (0, moment_1.default)().add(ttlSeconds, 'seconds').toDate();
39
- try {
40
- await collection.insertOne({
41
- _id: name,
42
- expiresAt,
43
- });
44
- return true;
45
- }
46
- catch (error) {
47
- // 11000 means duplicate key error, which is expected on an active lock
48
- if (error?.code !== 11_000) {
49
- // Unexpected error, what happened here :o
50
- throw error;
51
- }
52
- // As we got a duplicate key exception, no lock could be acquired
53
- return false;
54
- }
55
- }
56
- async releaseLock(name) {
57
- const collection = await this.getCollection();
58
- return collection.deleteOne({ _id: name });
59
- }
60
- async acquireAndExecute(name, ttlSeconds, wait, fn) {
61
- const delay = 200;
62
- const count = Math.round((1000 / delay) * ttlSeconds);
63
- return (0, rxjs_1.lastValueFrom)((0, rxjs_1.defer)(() => (0, rxjs_1.from)(this.acquireLock(name, ttlSeconds))).pipe((0, rxjs_1.map)(locked => {
64
- // console.log(locked);
65
- if (!locked)
66
- throw new Error('Could not acquire lock');
67
- }), wait ? (0, rxjs_1.retry)({ count, delay }) : (0, rxjs_1.tap)(rxjs_1.identity), (0, rxjs_1.switchMap)(async () => {
68
- const method = bluebird_1.default.method(rxjs_1.identity);
69
- const withDisposer = () => method(this).disposer(mongoLock => {
70
- mongoLock.releaseLock(name).catch(noop_1.default);
71
- });
72
- return bluebird_1.default.using(withDisposer(), () => fn());
73
- })));
74
- }
75
- async checkLock(name, waitSeconds = 0) {
76
- const delay = 200;
77
- const count = Math.round((1000 / delay) * waitSeconds);
78
- const collection = await this.getCollection();
79
- return (0, rxjs_1.lastValueFrom)((0, rxjs_1.defer)(() => (0, rxjs_1.from)(collection.findOne({ _id: name }))).pipe((0, rxjs_1.map)(lock => {
80
- // console.log(lock);
81
- if (lock)
82
- throw lock;
83
- }), waitSeconds > 0 ? (0, rxjs_1.retry)({ count, delay }) : (0, rxjs_1.tap)(rxjs_1.identity)));
84
- }
85
- }
86
- exports.MongoLock = MongoLock;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.MongoLock = void 0;
7
+ const moment_1 = __importDefault(require("moment/moment"));
8
+ const noop_1 = __importDefault(require("lodash/noop"));
9
+ const rxjs_1 = require("rxjs");
10
+ const mongo_helper_1 = require("./mongo-helper");
11
+ const bluebird_1 = __importDefault(require("bluebird"));
12
+ /**
13
+ * @deprecated
14
+ */
15
+ class MongoLock {
16
+ client;
17
+ constructor(client) {
18
+ this.client = client;
19
+ }
20
+ async getCollection() {
21
+ const locksColl = await mongo_helper_1.MongoHelper.collectionSafeGet(this.client, 'locks');
22
+ await locksColl.indexExists('expiresAt').then(async (exists) => {
23
+ if (!exists)
24
+ await locksColl.createIndex({ expiresAt: 1 }, { expireAfterSeconds: 0 });
25
+ return;
26
+ });
27
+ return locksColl;
28
+ }
29
+ /**
30
+ * Create the MongoDB collection and an expiring index on a field named "expiresAt".
31
+ *
32
+ * db.createCollection('locks');
33
+ * db.locks.createIndex( { "expiresAt": 1 }, { expireAfterSeconds: 0 } )
34
+ **/
35
+ async acquireLock(name, ttlSeconds) {
36
+ const collection = await this.getCollection();
37
+ // Entry gets removed automatically due to an expiry index in Mongo
38
+ const expiresAt = (0, moment_1.default)().add(ttlSeconds, 'seconds').toDate();
39
+ try {
40
+ await collection.insertOne({
41
+ _id: name,
42
+ expiresAt,
43
+ });
44
+ return true;
45
+ }
46
+ catch (error) {
47
+ // 11000 means duplicate key error, which is expected on an active lock
48
+ if (error?.code !== 11_000) {
49
+ // Unexpected error, what happened here :o
50
+ throw error;
51
+ }
52
+ // As we got a duplicate key exception, no lock could be acquired
53
+ return false;
54
+ }
55
+ }
56
+ async releaseLock(name) {
57
+ const collection = await this.getCollection();
58
+ return collection.deleteOne({ _id: name });
59
+ }
60
+ async acquireAndExecute(name, ttlSeconds, wait, fn) {
61
+ const delay = 200;
62
+ const count = Math.round((1000 / delay) * ttlSeconds);
63
+ return (0, rxjs_1.lastValueFrom)((0, rxjs_1.defer)(() => (0, rxjs_1.from)(this.acquireLock(name, ttlSeconds))).pipe((0, rxjs_1.map)(locked => {
64
+ // console.log(locked);
65
+ if (!locked)
66
+ throw new Error('Could not acquire lock');
67
+ }), wait ? (0, rxjs_1.retry)({ count, delay }) : (0, rxjs_1.tap)(rxjs_1.identity), (0, rxjs_1.switchMap)(async () => {
68
+ const method = bluebird_1.default.method(rxjs_1.identity);
69
+ const withDisposer = () => method(this).disposer(mongoLock => {
70
+ mongoLock.releaseLock(name).catch(noop_1.default);
71
+ });
72
+ return bluebird_1.default.using(withDisposer(), () => fn());
73
+ })));
74
+ }
75
+ async checkLock(name, waitSeconds = 0) {
76
+ const delay = 200;
77
+ const count = Math.round((1000 / delay) * waitSeconds);
78
+ const collection = await this.getCollection();
79
+ return (0, rxjs_1.lastValueFrom)((0, rxjs_1.defer)(() => (0, rxjs_1.from)(collection.findOne({ _id: name }))).pipe((0, rxjs_1.map)(lock => {
80
+ // console.log(lock);
81
+ if (lock)
82
+ throw lock;
83
+ }), waitSeconds > 0 ? (0, rxjs_1.retry)({ count, delay }) : (0, rxjs_1.tap)(rxjs_1.identity)));
84
+ }
85
+ }
86
+ exports.MongoLock = MongoLock;
package/src/mongo-lock.ts CHANGED
@@ -1,99 +1,99 @@
1
- import moment from 'moment/moment';
2
- import { MongoClient, MongoError } from 'mongodb';
3
- import noop from 'lodash/noop';
4
- import { defer, from, identity, lastValueFrom, map, retry, switchMap, tap } from 'rxjs';
5
- import { MongoHelper } from './mongo-helper';
6
- import Bluebird from 'bluebird';
7
-
8
- /**
9
- * @deprecated
10
- */
11
- export class MongoLock {
12
- constructor(private client: MongoClient) {}
13
-
14
- async getCollection() {
15
- const locksColl = await MongoHelper.collectionSafeGet(this.client, 'locks');
16
- await locksColl.indexExists('expiresAt').then(async exists => {
17
- if (!exists) await locksColl.createIndex({ expiresAt: 1 }, { expireAfterSeconds: 0 });
18
- return;
19
- });
20
- return locksColl;
21
- }
22
-
23
- /**
24
- * Create the MongoDB collection and an expiring index on a field named "expiresAt".
25
- *
26
- * db.createCollection('locks');
27
- * db.locks.createIndex( { "expiresAt": 1 }, { expireAfterSeconds: 0 } )
28
- **/
29
- async acquireLock(name: string, ttlSeconds: number) {
30
- const collection = await this.getCollection();
31
-
32
- // Entry gets removed automatically due to an expiry index in Mongo
33
- const expiresAt = moment().add(ttlSeconds, 'seconds').toDate();
34
-
35
- try {
36
- await collection.insertOne({
37
- _id: name as any,
38
- expiresAt,
39
- });
40
-
41
- return true;
42
- } catch (error) {
43
- // 11000 means duplicate key error, which is expected on an active lock
44
- if ((error as MongoError)?.code !== 11_000) {
45
- // Unexpected error, what happened here :o
46
- throw error;
47
- }
48
-
49
- // As we got a duplicate key exception, no lock could be acquired
50
- return false;
51
- }
52
- }
53
-
54
- async releaseLock(name: string) {
55
- const collection = await this.getCollection();
56
- return collection.deleteOne({ _id: name as any });
57
- }
58
-
59
- async acquireAndExecute<T = any>(name: string, ttlSeconds: number, wait: boolean, fn: () => Promise<T>) {
60
- const delay = 200;
61
- const count = Math.round((1000 / delay) * ttlSeconds);
62
-
63
- return lastValueFrom(
64
- defer(() => from(this.acquireLock(name, ttlSeconds))).pipe(
65
- map(locked => {
66
- // console.log(locked);
67
- if (!locked) throw new Error('Could not acquire lock');
68
- }),
69
- wait ? retry({ count, delay }) : tap(identity),
70
- switchMap(async () => {
71
- const method = Bluebird.method(identity);
72
-
73
- const withDisposer = () =>
74
- method(this).disposer(mongoLock => {
75
- mongoLock.releaseLock(name).catch(noop);
76
- });
77
-
78
- return Bluebird.using(withDisposer(), () => fn());
79
- }),
80
- ),
81
- );
82
- }
83
-
84
- async checkLock(name: string, waitSeconds: number = 0) {
85
- const delay = 200;
86
- const count = Math.round((1000 / delay) * waitSeconds);
87
- const collection = await this.getCollection();
88
-
89
- return lastValueFrom(
90
- defer(() => from(collection.findOne({ _id: name as any }))).pipe(
91
- map(lock => {
92
- // console.log(lock);
93
- if (lock) throw lock;
94
- }),
95
- waitSeconds > 0 ? retry({ count, delay }) : tap(identity),
96
- ),
97
- );
98
- }
99
- }
1
+ import moment from 'moment/moment';
2
+ import { MongoClient, MongoError } from 'mongodb';
3
+ import noop from 'lodash/noop';
4
+ import { defer, from, identity, lastValueFrom, map, retry, switchMap, tap } from 'rxjs';
5
+ import { MongoHelper } from './mongo-helper';
6
+ import Bluebird from 'bluebird';
7
+
8
+ /**
9
+ * @deprecated
10
+ */
11
+ export class MongoLock {
12
+ constructor(private client: MongoClient) {}
13
+
14
+ async getCollection() {
15
+ const locksColl = await MongoHelper.collectionSafeGet(this.client, 'locks');
16
+ await locksColl.indexExists('expiresAt').then(async exists => {
17
+ if (!exists) await locksColl.createIndex({ expiresAt: 1 }, { expireAfterSeconds: 0 });
18
+ return;
19
+ });
20
+ return locksColl;
21
+ }
22
+
23
+ /**
24
+ * Create the MongoDB collection and an expiring index on a field named "expiresAt".
25
+ *
26
+ * db.createCollection('locks');
27
+ * db.locks.createIndex( { "expiresAt": 1 }, { expireAfterSeconds: 0 } )
28
+ **/
29
+ async acquireLock(name: string, ttlSeconds: number) {
30
+ const collection = await this.getCollection();
31
+
32
+ // Entry gets removed automatically due to an expiry index in Mongo
33
+ const expiresAt = moment().add(ttlSeconds, 'seconds').toDate();
34
+
35
+ try {
36
+ await collection.insertOne({
37
+ _id: name as any,
38
+ expiresAt,
39
+ });
40
+
41
+ return true;
42
+ } catch (error) {
43
+ // 11000 means duplicate key error, which is expected on an active lock
44
+ if ((error as MongoError)?.code !== 11_000) {
45
+ // Unexpected error, what happened here :o
46
+ throw error;
47
+ }
48
+
49
+ // As we got a duplicate key exception, no lock could be acquired
50
+ return false;
51
+ }
52
+ }
53
+
54
+ async releaseLock(name: string) {
55
+ const collection = await this.getCollection();
56
+ return collection.deleteOne({ _id: name as any });
57
+ }
58
+
59
+ async acquireAndExecute<T = any>(name: string, ttlSeconds: number, wait: boolean, fn: () => Promise<T>) {
60
+ const delay = 200;
61
+ const count = Math.round((1000 / delay) * ttlSeconds);
62
+
63
+ return lastValueFrom(
64
+ defer(() => from(this.acquireLock(name, ttlSeconds))).pipe(
65
+ map(locked => {
66
+ // console.log(locked);
67
+ if (!locked) throw new Error('Could not acquire lock');
68
+ }),
69
+ wait ? retry({ count, delay }) : tap(identity),
70
+ switchMap(async () => {
71
+ const method = Bluebird.method(identity);
72
+
73
+ const withDisposer = () =>
74
+ method(this).disposer(mongoLock => {
75
+ mongoLock.releaseLock(name).catch(noop);
76
+ });
77
+
78
+ return Bluebird.using(withDisposer(), () => fn());
79
+ }),
80
+ ),
81
+ );
82
+ }
83
+
84
+ async checkLock(name: string, waitSeconds: number = 0) {
85
+ const delay = 200;
86
+ const count = Math.round((1000 / delay) * waitSeconds);
87
+ const collection = await this.getCollection();
88
+
89
+ return lastValueFrom(
90
+ defer(() => from(collection.findOne({ _id: name as any }))).pipe(
91
+ map(lock => {
92
+ // console.log(lock);
93
+ if (lock) throw lock;
94
+ }),
95
+ waitSeconds > 0 ? retry({ count, delay }) : tap(identity),
96
+ ),
97
+ );
98
+ }
99
+ }
package/src/types.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/src/types.ts CHANGED
@@ -1,84 +1,84 @@
1
- import Dict = NodeJS.Dict;
2
- import { Request, Response } from 'express';
3
-
4
- export type TPayload<E = TEvent> = {
5
- event?: E;
6
- context?: TContext;
7
- request?: Request;
8
- response?: Response;
9
- };
10
-
11
- export type TGSEvent<M = TMetadataOrAttributes> = {
12
- bucket: string;
13
- contentType: string;
14
- crc32c: string;
15
- etag: string;
16
- generation: string;
17
- id: string;
18
- kind: string; // 'storage#object'
19
- md5Hash: string;
20
- mediaLink: string;
21
- metadata: M;
22
- metageneration: string;
23
- name: string;
24
- selfLink: string;
25
- size: string;
26
- storageClass: string;
27
- timeCreated: string;
28
- timeStorageClassUpdated: string;
29
- updated: string;
30
- };
31
-
32
- export type TPSEvent<A = TMetadataOrAttributes> = {
33
- '@type': string; // 'type.googleapis.com/google.pubsub.v1.PubsubMessage'
34
- attributes?: A;
35
- data?: string;
36
- json?: Dict<any>;
37
- };
38
-
39
- export type TEvent = TGSEvent | TPSEvent;
40
-
41
- export type TMetadataOrAttributes = {
42
- request_id?: string; // for rpc response [GUID]
43
- consumer_id?: string; // for rpc response [GUID]
44
- topic?: string; // response PubSub topic [t_{GUID}__{YYYY-MM-DD}]
45
- exchange?: string; // response amqp exchange
46
- queue?: string; // response amqp queue
47
- //
48
- filename?: string;
49
- referer?: string;
50
- remote_address?: string;
51
- upload_id?: string;
52
- user_agent?: string;
53
- timestamp?: string;
54
- //
55
- action?: string;
56
- pipeline?: string;
57
- options?: string;
58
- job_uid?: string;
59
- user_id?: string;
60
- tenant_id?: string;
61
- //
62
- app_id?: string; // app id
63
- env?: string; // app environment
64
- };
65
-
66
- export type TContext = {
67
- eventId: string;
68
- timestamp: string;
69
- eventType: 'google.storage.object.finalize' | 'google.pubsub.topic.publish';
70
- resource: {
71
- service: 'storage.googleapis.com' | 'pubsub.googleapis.com';
72
- type: 'storage#object' | 'type.googleapis.com/google.pubsub.v1.PubsubMessage';
73
- name: string;
74
- };
75
- };
76
-
77
- export type TResponse = {
78
- [prop: string]: any;
79
- error?: {
80
- name: string;
81
- message: string;
82
- stack?: string;
83
- };
84
- };
1
+ import Dict = NodeJS.Dict;
2
+ import { Request, Response } from 'express';
3
+
4
+ export type TPayload<E = TEvent> = {
5
+ event?: E;
6
+ context?: TContext;
7
+ request?: Request;
8
+ response?: Response;
9
+ };
10
+
11
+ export type TGSEvent<M = TMetadataOrAttributes> = {
12
+ bucket: string;
13
+ contentType: string;
14
+ crc32c: string;
15
+ etag: string;
16
+ generation: string;
17
+ id: string;
18
+ kind: string; // 'storage#object'
19
+ md5Hash: string;
20
+ mediaLink: string;
21
+ metadata: M;
22
+ metageneration: string;
23
+ name: string;
24
+ selfLink: string;
25
+ size: string;
26
+ storageClass: string;
27
+ timeCreated: string;
28
+ timeStorageClassUpdated: string;
29
+ updated: string;
30
+ };
31
+
32
+ export type TPSEvent<A = TMetadataOrAttributes> = {
33
+ '@type': string; // 'type.googleapis.com/google.pubsub.v1.PubsubMessage'
34
+ attributes?: A;
35
+ data?: string;
36
+ json?: Dict<any>;
37
+ };
38
+
39
+ export type TEvent = TGSEvent | TPSEvent;
40
+
41
+ export type TMetadataOrAttributes = {
42
+ request_id?: string; // for rpc response [GUID]
43
+ consumer_id?: string; // for rpc response [GUID]
44
+ topic?: string; // response PubSub topic [t_{GUID}__{YYYY-MM-DD}]
45
+ exchange?: string; // response amqp exchange
46
+ queue?: string; // response amqp queue
47
+ //
48
+ filename?: string;
49
+ referer?: string;
50
+ remote_address?: string;
51
+ upload_id?: string;
52
+ user_agent?: string;
53
+ timestamp?: string;
54
+ //
55
+ action?: string;
56
+ pipeline?: string;
57
+ options?: string;
58
+ job_uid?: string;
59
+ user_id?: string;
60
+ tenant_id?: string;
61
+ //
62
+ app_id?: string; // app id
63
+ env?: string; // app environment
64
+ };
65
+
66
+ export type TContext = {
67
+ eventId: string;
68
+ timestamp: string;
69
+ eventType: 'google.storage.object.finalize' | 'google.pubsub.topic.publish';
70
+ resource: {
71
+ service: 'storage.googleapis.com' | 'pubsub.googleapis.com';
72
+ type: 'storage#object' | 'type.googleapis.com/google.pubsub.v1.PubsubMessage';
73
+ name: string;
74
+ };
75
+ };
76
+
77
+ export type TResponse = {
78
+ [prop: string]: any;
79
+ error?: {
80
+ name: string;
81
+ message: string;
82
+ stack?: string;
83
+ };
84
+ };