gcf-common-lib 0.25.44 → 0.25.46

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gcf-common-lib",
3
3
  "description": "",
4
- "version": "0.25.44",
4
+ "version": "0.25.46",
5
5
  "publishConfig": {
6
6
  "access": "public",
7
7
  "branches": [
package/src/types.ts CHANGED
@@ -51,8 +51,9 @@ export type TGSEvent = {
51
51
 
52
52
  export type TPSEvent = {
53
53
  '@type': string; // 'type.googleapis.com/google.pubsub.v1.PubsubMessage'
54
- attributes?: TMetadataOrAttributes;
55
- data?: string | Dict<any>;
54
+ attributes: TMetadataOrAttributes;
55
+ data: string;
56
+ json?: Dict<string>;
56
57
  };
57
58
 
58
59
  export type TEvent = TGSEvent | TPSEvent;
package/src/utils.js CHANGED
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.sendToQueueConfAmqp = exports.publishAmqp = exports.withAmqpCh = exports.withAmqpConn = exports.A1ToColNum = exports.colNumToA1 = exports.A1ToIndex = exports.indexToA1 = exports.sec = exports.ms = exports.delay = exports.timeoutAfter = void 0;
15
+ exports.sendToQueueConfAmqp = exports.publishAmqp = exports.withAmqpCh = exports.withAmqpConn = exports.safeJsonParse = exports.A1ToColNum = exports.colNumToA1 = exports.A1ToIndex = exports.indexToA1 = exports.sec = exports.ms = exports.delay = exports.timeoutAfter = void 0;
16
16
  const amqplib_1 = require("amqplib");
17
17
  const bluebird_1 = __importDefault(require("bluebird"));
18
18
  const lodash_1 = require("lodash");
@@ -28,7 +28,7 @@ function timeoutAfter(seconds = 540) {
28
28
  exports.timeoutAfter = timeoutAfter;
29
29
  function delay(seconds) {
30
30
  return __awaiter(this, void 0, void 0, function* () {
31
- return new Promise((resolve) => setTimeout(() => resolve(undefined), seconds * 1000));
31
+ return new Promise(resolve => setTimeout(() => resolve(undefined), seconds * 1000));
32
32
  });
33
33
  }
34
34
  exports.delay = delay;
@@ -95,6 +95,15 @@ function A1ToColNum(value) {
95
95
  return result;
96
96
  }
97
97
  exports.A1ToColNum = A1ToColNum;
98
+ function safeJsonParse(value, fallbackValue) {
99
+ try {
100
+ return JSON.parse(value);
101
+ }
102
+ catch (err) {
103
+ return fallbackValue;
104
+ }
105
+ }
106
+ exports.safeJsonParse = safeJsonParse;
98
107
  //
99
108
  function withAmqpConn(fn, url) {
100
109
  return __awaiter(this, void 0, void 0, function* () {
@@ -105,7 +114,7 @@ function withAmqpConn(fn, url) {
105
114
  return amqpConn;
106
115
  }))().disposer((conn, promise) => conn.close());
107
116
  }
108
- return bluebird_1.default.using(withDisposer(), (conn) => fn(conn));
117
+ return bluebird_1.default.using(withDisposer(), conn => fn(conn));
109
118
  });
110
119
  }
111
120
  exports.withAmqpConn = withAmqpConn;
@@ -114,10 +123,10 @@ function withAmqpCh(fn, url, useConfirmChannel = false) {
114
123
  return withAmqpConn((conn) => __awaiter(this, void 0, void 0, function* () {
115
124
  function withDisposer() {
116
125
  return bluebird_1.default.method(() => __awaiter(this, void 0, void 0, function* () {
117
- return (useConfirmChannel ? conn.createConfirmChannel() : conn.createChannel());
126
+ return useConfirmChannel ? conn.createConfirmChannel() : conn.createChannel();
118
127
  }))().disposer((ch, promise) => ch.close());
119
128
  }
120
- return bluebird_1.default.using(withDisposer(), (ch) => fn(ch));
129
+ return bluebird_1.default.using(withDisposer(), ch => fn(ch));
121
130
  }), url);
122
131
  });
123
132
  }
@@ -125,7 +134,9 @@ exports.withAmqpCh = withAmqpCh;
125
134
  function publishAmqp(ch, exchange, routingKey, json, options) {
126
135
  return __awaiter(this, void 0, void 0, function* () {
127
136
  const payload = Buffer.from(JSON.stringify(json));
128
- const keepSending = exchange ? ch.publish(exchange, routingKey, payload, options) : ch.sendToQueue(routingKey, payload, options);
137
+ const keepSending = exchange
138
+ ? ch.publish(exchange, routingKey, payload, options)
139
+ : ch.sendToQueue(routingKey, payload, options);
129
140
  if (!keepSending)
130
141
  yield new Promise(resolve => ch.once('drain', () => resolve(undefined)));
131
142
  });
@@ -134,7 +145,7 @@ exports.publishAmqp = publishAmqp;
134
145
  function sendToQueueConfAmqp(ch, queue, json, options) {
135
146
  return __awaiter(this, void 0, void 0, function* () {
136
147
  const payload = Buffer.from(JSON.stringify(json));
137
- yield new Promise((resolve, reject) => ch.sendToQueue(queue, payload, options, (err, ok) => err ? reject(err) : resolve(ok)));
148
+ yield new Promise((resolve, reject) => ch.sendToQueue(queue, payload, options, (err, ok) => (err ? reject(err) : resolve(ok))));
138
149
  });
139
150
  }
140
151
  exports.sendToQueueConfAmqp = sendToQueueConfAmqp;
package/src/utils.ts CHANGED
@@ -8,11 +8,12 @@ import { chain, Dictionary } from 'lodash';
8
8
  */
9
9
  export async function timeoutAfter(seconds: number = 540) {
10
10
  return new Promise<undefined>((resolve, reject) =>
11
- setTimeout(() => reject(new Error(`${seconds} seconds timeout exceeded`)), seconds * 1000));
11
+ setTimeout(() => reject(new Error(`${seconds} seconds timeout exceeded`)), seconds * 1000),
12
+ );
12
13
  }
13
14
 
14
15
  export async function delay(seconds: number) {
15
- return new Promise((resolve) => setTimeout(() => resolve(undefined), seconds * 1000));
16
+ return new Promise(resolve => setTimeout(() => resolve(undefined), seconds * 1000));
16
17
  }
17
18
 
18
19
  //
@@ -83,6 +84,14 @@ export function A1ToColNum(value: string) {
83
84
  return result;
84
85
  }
85
86
 
87
+ export function safeJsonParse(value: string, fallbackValue?: any) {
88
+ try {
89
+ return JSON.parse(value);
90
+ } catch (err) {
91
+ return fallbackValue;
92
+ }
93
+ }
94
+
86
95
  //
87
96
 
88
97
  export async function withAmqpConn(fn: (conn: Connection) => Promise<any>, url: string) {
@@ -94,29 +103,43 @@ export async function withAmqpConn(fn: (conn: Connection) => Promise<any>, url:
94
103
  })().disposer((conn, promise) => conn.close());
95
104
  }
96
105
 
97
- return Bluebird.using(withDisposer(), (conn) => fn(conn));
106
+ return Bluebird.using(withDisposer(), conn => fn(conn));
98
107
  }
99
108
 
100
109
  export async function withAmqpCh(fn: (ch: Channel) => Promise<any>, url: string, useConfirmChannel = false) {
101
110
  return withAmqpConn(async conn => {
102
111
  function withDisposer() {
103
112
  return Bluebird.method(async () => {
104
- return (useConfirmChannel ? conn.createConfirmChannel() : conn.createChannel());
113
+ return useConfirmChannel ? conn.createConfirmChannel() : conn.createChannel();
105
114
  })().disposer((ch, promise) => ch.close());
106
115
  }
107
116
 
108
- return Bluebird.using(withDisposer(), (ch) => fn(ch));
109
-
117
+ return Bluebird.using(withDisposer(), ch => fn(ch));
110
118
  }, url);
111
119
  }
112
120
 
113
- export async function publishAmqp(ch: Channel, exchange: string | undefined, routingKey: string, json: Dictionary<any>, options?: Options.Publish) {
121
+ export async function publishAmqp(
122
+ ch: Channel,
123
+ exchange: string | undefined,
124
+ routingKey: string,
125
+ json: Dictionary<any>,
126
+ options?: Options.Publish,
127
+ ) {
114
128
  const payload = Buffer.from(JSON.stringify(json));
115
- const keepSending = exchange ? ch.publish(exchange, routingKey, payload, options) : ch.sendToQueue(routingKey, payload, options);
129
+ const keepSending = exchange
130
+ ? ch.publish(exchange, routingKey, payload, options)
131
+ : ch.sendToQueue(routingKey, payload, options);
116
132
  if (!keepSending) await new Promise(resolve => ch.once('drain', () => resolve(undefined)));
117
133
  }
118
134
 
119
- export async function sendToQueueConfAmqp(ch: ConfirmChannel, queue: string, json: Dictionary<any>, options?: Options.Publish) {
135
+ export async function sendToQueueConfAmqp(
136
+ ch: ConfirmChannel,
137
+ queue: string,
138
+ json: Dictionary<any>,
139
+ options?: Options.Publish,
140
+ ) {
120
141
  const payload = Buffer.from(JSON.stringify(json));
121
- await new Promise((resolve, reject) => ch.sendToQueue(queue, payload, options, (err, ok) => err ? reject(err) : resolve(ok)));
122
- }
142
+ await new Promise((resolve, reject) =>
143
+ ch.sendToQueue(queue, payload, options, (err, ok) => (err ? reject(err) : resolve(ok))),
144
+ );
145
+ }