gcf-common-lib 0.25.39 → 0.25.41

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 (3) hide show
  1. package/package.json +2 -2
  2. package/src/index.js +18 -13
  3. package/src/index.ts +22 -14
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gcf-common-lib",
3
3
  "description": "",
4
- "version": "0.25.39",
4
+ "version": "0.25.41",
5
5
  "publishConfig": {
6
6
  "access": "public",
7
7
  "branches": [
@@ -21,7 +21,7 @@
21
21
  "url": "https://github.com/TopTechnologies/gcf-common.git"
22
22
  },
23
23
  "dependencies": {
24
- "@google-cloud/pubsub": "^4.0.0",
24
+ "@google-cloud/pubsub": "^4.0.1",
25
25
  "@google-cloud/secret-manager": "^5.0.0",
26
26
  "@google-cloud/storage": "^7.0.1",
27
27
  "amqplib": "^0.10.3",
package/src/index.js CHANGED
@@ -64,11 +64,11 @@ class GcfCommon {
64
64
  * @param handler
65
65
  * @param timeout Seconds
66
66
  */
67
- static process(event, context, handler, timeout = 535) {
67
+ static process(event, context, handler = (e, c) => __awaiter(this, void 0, void 0, function* () { return ({}); }), timeout = 535) {
68
68
  return __awaiter(this, void 0, void 0, function* () {
69
- return Promise.race([(0, utils_1.timeoutAfter)(timeout), (handler !== null && handler !== void 0 ? handler : (() => Promise.resolve(undefined)))(event, context)])
69
+ return Promise.race([(0, utils_1.timeoutAfter)(timeout), handler(event, context)])
70
70
  .then((res) => __awaiter(this, void 0, void 0, function* () {
71
- console.log('res:', res);
71
+ // console.log('res:', res);
72
72
  yield this.publish(event, context, res);
73
73
  }))
74
74
  .catch((err) => __awaiter(this, void 0, void 0, function* () {
@@ -89,34 +89,39 @@ class GcfCommon {
89
89
  }
90
90
  static publish(event, context, json, attributes) {
91
91
  return __awaiter(this, void 0, void 0, function* () {
92
+ console.time('safeGetAttributes');
92
93
  const { topic, exchange, queue, appId, env, requestId } = yield this.safeGetAttributes(event, context, [
93
94
  'topic',
94
95
  'exchange',
95
96
  'queue',
96
97
  ]);
98
+ console.timeEnd('safeGetAttributes');
99
+ //
100
+ console.time('publish');
97
101
  if (topic && !(0, isEmpty_1.default)(topic)) {
98
- console.log('publish:', topic, appId, env, json, attributes);
99
- return exports.pubSub.topic(topic).publishMessage({
102
+ console.log('send:', topic, appId, env, json, attributes);
103
+ yield exports.pubSub.topic(topic).publishMessage({
100
104
  json: json !== null && json !== void 0 ? json : {},
101
105
  attributes: Object.assign(Object.assign({}, (0, lodash_1.mapValues)(attributes !== null && attributes !== void 0 ? attributes : {}, v => '' + v)), { appId: appId !== null && appId !== void 0 ? appId : '', env: env !== null && env !== void 0 ? env : '', requestId: requestId !== null && requestId !== void 0 ? requestId : '',
102
106
  //
103
107
  type: 'response', response: '1' }),
104
108
  });
105
109
  }
106
- if (queue && !(0, isEmpty_1.default)(queue)) {
107
- console.log('send:', queue, appId, env, json, attributes);
108
- return (0, utils_1.withAmqpCh)((ch) => __awaiter(this, void 0, void 0, function* () {
109
- yield ch.assertQueue(queue, this.amqpOptions.assertOptions);
110
- yield (0, utils_1.publishAmqp)(ch, undefined, queue, json !== null && json !== void 0 ? json : {}, this.amqpOptions.publishOptions);
111
- }), this.amqpOptions.url);
112
- }
113
110
  if (exchange && !(0, isEmpty_1.default)(exchange)) {
114
111
  console.log('send:', exchange, queue, appId, env, json, attributes);
115
- return (0, utils_1.withAmqpCh)((ch) => __awaiter(this, void 0, void 0, function* () {
112
+ yield (0, utils_1.withAmqpCh)((ch) => __awaiter(this, void 0, void 0, function* () {
116
113
  yield ch.assertExchange(exchange, 'direct', this.amqpOptions.assertExchange);
117
114
  yield (0, utils_1.publishAmqp)(ch, exchange, queue !== null && queue !== void 0 ? queue : '', json !== null && json !== void 0 ? json : {}, this.amqpOptions.publishOptions);
118
115
  }), this.amqpOptions.url);
119
116
  }
117
+ else if (queue && !(0, isEmpty_1.default)(queue)) {
118
+ console.log('send:', queue, appId, env, json, attributes);
119
+ yield (0, utils_1.withAmqpCh)((ch) => __awaiter(this, void 0, void 0, function* () {
120
+ yield ch.assertQueue(queue, this.amqpOptions.assertOptions);
121
+ yield (0, utils_1.publishAmqp)(ch, undefined, queue, json !== null && json !== void 0 ? json : {}, this.amqpOptions.publishOptions);
122
+ }), this.amqpOptions.url);
123
+ }
124
+ console.timeEnd('publish');
120
125
  });
121
126
  }
122
127
  static safeGetAttributes(event, context, props) {
package/src/index.ts CHANGED
@@ -43,12 +43,12 @@ export class GcfCommon {
43
43
  static async process<T extends TResponse>(
44
44
  event: TEvent,
45
45
  context: TContext,
46
- handler?: (event: TEvent, context: TContext) => Promise<T>,
46
+ handler: (event: TEvent, context: TContext) => Promise<T> = async (e, c) => ({}) as T,
47
47
  timeout = 535,
48
48
  ) {
49
- return Promise.race([timeoutAfter(timeout), (handler ?? (() => Promise.resolve(undefined)))(event, context)])
49
+ return Promise.race([timeoutAfter(timeout), handler(event, context)])
50
50
  .then(async (res: T | undefined) => {
51
- console.log('res:', res);
51
+ // console.log('res:', res);
52
52
  await this.publish(event, context, res);
53
53
  })
54
54
  .catch(async (err: Error) => {
@@ -68,15 +68,23 @@ export class GcfCommon {
68
68
  }
69
69
 
70
70
  static async publish(event: TEvent, context: TContext, json?: TResponse, attributes?: Dict<any>) {
71
+ console.time('safeGetAttributes');
72
+
71
73
  const { topic, exchange, queue, appId, env, requestId } = await this.safeGetAttributes(event, context, [
72
74
  'topic',
73
75
  'exchange',
74
76
  'queue',
75
77
  ]);
76
78
 
79
+ console.timeEnd('safeGetAttributes');
80
+
81
+ //
82
+
83
+ console.time('publish');
84
+
77
85
  if (topic && !isEmpty(topic)) {
78
- console.log('publish:', topic, appId, env, json, attributes);
79
- return pubSub.topic(topic).publishMessage({
86
+ console.log('send:', topic, appId, env, json, attributes);
87
+ await pubSub.topic(topic).publishMessage({
80
88
  json: json ?? {},
81
89
  attributes: {
82
90
  ...mapValues(attributes ?? {}, v => '' + v),
@@ -90,21 +98,21 @@ export class GcfCommon {
90
98
  });
91
99
  }
92
100
 
93
- if (queue && !isEmpty(queue)) {
94
- console.log('send:', queue, appId, env, json, attributes);
95
- return withAmqpCh(async ch => {
96
- await ch.assertQueue(queue, this.amqpOptions.assertOptions);
97
- await publishAmqp(ch, undefined, queue, json ?? {}, this.amqpOptions.publishOptions);
98
- }, this.amqpOptions.url as string);
99
- }
100
-
101
101
  if (exchange && !isEmpty(exchange)) {
102
102
  console.log('send:', exchange, queue, appId, env, json, attributes);
103
- return withAmqpCh(async ch => {
103
+ await withAmqpCh(async ch => {
104
104
  await ch.assertExchange(exchange, 'direct', this.amqpOptions.assertExchange);
105
105
  await publishAmqp(ch, exchange, queue ?? '', json ?? {}, this.amqpOptions.publishOptions);
106
106
  }, this.amqpOptions.url as string);
107
+ } else if (queue && !isEmpty(queue)) {
108
+ console.log('send:', queue, appId, env, json, attributes);
109
+ await withAmqpCh(async ch => {
110
+ await ch.assertQueue(queue, this.amqpOptions.assertOptions);
111
+ await publishAmqp(ch, undefined, queue, json ?? {}, this.amqpOptions.publishOptions);
112
+ }, this.amqpOptions.url as string);
107
113
  }
114
+
115
+ console.timeEnd('publish');
108
116
  }
109
117
 
110
118
  static async safeGetAttributes(event: TEvent, context: TContext, props: string[]) {