gcf-common-lib 0.27.60 → 0.28.62
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 +1 -1
- package/src/index.js +14 -5
- package/src/index.ts +18 -7
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -32,12 +32,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
32
32
|
exports.GcfCommon = exports.secretClient = exports.storage = exports.pubSub = exports.MongoDb = exports.RxJs = exports.SecretManager = exports.PubSub = exports.Storage = void 0;
|
|
33
33
|
const pubsub_1 = require("@google-cloud/pubsub");
|
|
34
34
|
const secret_manager_1 = require("@google-cloud/secret-manager");
|
|
35
|
+
const storage_1 = require("@google-cloud/storage");
|
|
35
36
|
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
|
|
36
37
|
const mapValues_1 = __importDefault(require("lodash/mapValues"));
|
|
37
38
|
const noop_1 = __importDefault(require("lodash/noop"));
|
|
38
39
|
const rxjs_1 = require("rxjs");
|
|
39
40
|
const utils_1 = require("./utils");
|
|
40
|
-
const storage_1 = require("@google-cloud/storage");
|
|
41
41
|
exports.Storage = __importStar(require("@google-cloud/storage"));
|
|
42
42
|
exports.PubSub = __importStar(require("@google-cloud/pubsub"));
|
|
43
43
|
exports.SecretManager = __importStar(require("@google-cloud/secret-manager"));
|
|
@@ -58,6 +58,19 @@ class GcfCommon {
|
|
|
58
58
|
* @param timeoutSec Seconds
|
|
59
59
|
*/
|
|
60
60
|
static async process(event, context, handler = rxjs_1.identity, timeoutSec = 535) {
|
|
61
|
+
const asyncHandler = async (_event, _context) => await handler(_event, _context);
|
|
62
|
+
return (0, rxjs_1.firstValueFrom)((0, rxjs_1.defer)(() => (0, rxjs_1.from)(asyncHandler(event, context))).pipe((0, rxjs_1.first)(), (0, rxjs_1.timeout)({ first: (0, utils_1.ms)({ s: timeoutSec }) })))
|
|
63
|
+
.then(async (res) => {
|
|
64
|
+
// console.log('res:', res);
|
|
65
|
+
await this.publish(event, context, res).catch(noop_1.default);
|
|
66
|
+
return res;
|
|
67
|
+
})
|
|
68
|
+
.catch(async (error) => {
|
|
69
|
+
await this.publish(event, context, GcfCommon.buildResponse(error), { error: '1' }).catch(noop_1.default);
|
|
70
|
+
throw error;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
static async processOLd(event, context, handler = rxjs_1.identity, timeoutSec = 535) {
|
|
61
74
|
return Promise.race([(0, utils_1.timeoutAfter)(timeoutSec), handler(event, context)])
|
|
62
75
|
.then(async (res) => {
|
|
63
76
|
// console.log('res:', res);
|
|
@@ -69,10 +82,6 @@ class GcfCommon {
|
|
|
69
82
|
throw error;
|
|
70
83
|
});
|
|
71
84
|
}
|
|
72
|
-
static async processRxjs(event, context, handler = rxjs_1.identity, timeoutSec = 535) {
|
|
73
|
-
const asyncHandler = async (_event, _context) => await handler(_event, _context);
|
|
74
|
-
return (0, rxjs_1.defer)(() => (0, rxjs_1.from)(asyncHandler(event, context))).pipe((0, rxjs_1.first)(), (0, rxjs_1.timeout)({ first: (0, utils_1.ms)({ s: timeoutSec }) }));
|
|
75
|
-
}
|
|
76
85
|
static buildResponse(error) {
|
|
77
86
|
return {
|
|
78
87
|
error: {
|
package/src/index.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { PubSub } from '@google-cloud/pubsub';
|
|
2
2
|
import { SecretManagerServiceClient } from '@google-cloud/secret-manager';
|
|
3
|
+
import { Storage } from '@google-cloud/storage';
|
|
3
4
|
import { Options } from 'amqplib';
|
|
4
5
|
import isEmpty from 'lodash/isEmpty';
|
|
5
6
|
import mapValues from 'lodash/mapValues';
|
|
6
7
|
import noop from 'lodash/noop';
|
|
7
|
-
import { defer, first, from, identity, timeout } from 'rxjs';
|
|
8
|
+
import { defer, first, firstValueFrom, from, identity, timeout } from 'rxjs';
|
|
8
9
|
import { TContext, TEvent, TGSEvent, TMetadataOrAttributes, TPSEvent, TResponse } from './types';
|
|
9
10
|
import { ms, publishAmqp, timeoutAfter, withAmqpCh } from './utils';
|
|
10
11
|
import Dict = NodeJS.Dict;
|
|
11
|
-
import { Storage } from '@google-cloud/storage';
|
|
12
12
|
|
|
13
13
|
export * as Storage from '@google-cloud/storage';
|
|
14
14
|
export * as PubSub from '@google-cloud/pubsub';
|
|
@@ -49,10 +49,13 @@ export class GcfCommon {
|
|
|
49
49
|
handler: (event: E, context: TContext) => Promise<T | E> | T | E = identity,
|
|
50
50
|
timeoutSec = 535,
|
|
51
51
|
) {
|
|
52
|
-
|
|
52
|
+
const asyncHandler = async (_event: any, _context: any) => await handler(_event, _context);
|
|
53
|
+
return firstValueFrom(
|
|
54
|
+
defer(() => from(asyncHandler(event, context))).pipe(first(), timeout({ first: ms({ s: timeoutSec }) })),
|
|
55
|
+
)
|
|
53
56
|
.then(async res => {
|
|
54
57
|
// console.log('res:', res);
|
|
55
|
-
await this.publish(event, context, res as T);
|
|
58
|
+
await this.publish(event, context, res as T).catch(noop);
|
|
56
59
|
return res;
|
|
57
60
|
})
|
|
58
61
|
.catch(async (error: Error) => {
|
|
@@ -61,14 +64,22 @@ export class GcfCommon {
|
|
|
61
64
|
});
|
|
62
65
|
}
|
|
63
66
|
|
|
64
|
-
static async
|
|
67
|
+
static async processOLd<T extends TResponse, E = TEvent>(
|
|
65
68
|
event: E,
|
|
66
69
|
context: TContext,
|
|
67
70
|
handler: (event: E, context: TContext) => Promise<T | E> | T | E = identity,
|
|
68
71
|
timeoutSec = 535,
|
|
69
72
|
) {
|
|
70
|
-
|
|
71
|
-
|
|
73
|
+
return Promise.race([timeoutAfter(timeoutSec), handler(event, context)])
|
|
74
|
+
.then(async res => {
|
|
75
|
+
// console.log('res:', res);
|
|
76
|
+
await this.publish(event, context, res as T);
|
|
77
|
+
return res;
|
|
78
|
+
})
|
|
79
|
+
.catch(async (error: Error) => {
|
|
80
|
+
await this.publish(event, context, GcfCommon.buildResponse(error), { error: '1' }).catch(noop);
|
|
81
|
+
throw error;
|
|
82
|
+
});
|
|
72
83
|
}
|
|
73
84
|
|
|
74
85
|
static buildResponse(error: Error) {
|