gcf-common-lib 0.23.25 → 0.23.26

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.23.25",
4
+ "version": "0.23.26",
5
5
  "publishConfig": {
6
6
  "access": "public",
7
7
  "branches": [
package/src/index.js CHANGED
@@ -93,7 +93,9 @@ class GcfCommon {
93
93
  console.log('publish:', topic, appId, env, json, attributes);
94
94
  return exports.pubSub.topic(topic).publishMessage({
95
95
  json: json !== null && json !== void 0 ? json : {},
96
- attributes: Object.assign(Object.assign({}, (0, lodash_1.mapValues)(attributes !== null && attributes !== void 0 ? attributes : {}, (v) => '' + v)), { requestId: requestId !== null && requestId !== void 0 ? requestId : '', appId: appId !== null && appId !== void 0 ? appId : '', env: env !== null && env !== void 0 ? env : '', type: 'response', response: '1' }),
96
+ attributes: Object.assign(Object.assign({}, (0, lodash_1.mapValues)(attributes !== null && attributes !== void 0 ? attributes : {}, (v) => '' + v)), { requestId: requestId !== null && requestId !== void 0 ? requestId : '', env: env !== null && env !== void 0 ? env : '', appId: appId !== null && appId !== void 0 ? appId : '',
97
+ //
98
+ type: 'response', response: '1' }),
97
99
  });
98
100
  }
99
101
  if (queue && !(0, isEmpty_1.default)(queue)) {
package/src/index.ts CHANGED
@@ -73,8 +73,9 @@ export class GcfCommon {
73
73
  attributes: {
74
74
  ...mapValues(attributes ?? {}, (v) => '' + v),
75
75
  requestId: requestId ?? '',
76
- appId: appId ?? '',
77
76
  env: env ?? '',
77
+ appId: appId ?? '',
78
+ //
78
79
  type: 'response',
79
80
  response: '1',
80
81
  },
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.withAmqpCh = exports.withAmqpConn = exports.A1ToColNum = exports.colNumToA1 = exports.A1ToIndex = exports.indexToA1 = exports.delay = exports.timeoutAfter = void 0;
15
+ exports.sentToQueueAsync = exports.withAmqpCh = exports.withAmqpConn = exports.A1ToColNum = exports.colNumToA1 = exports.A1ToIndex = exports.indexToA1 = exports.delay = exports.timeoutAfter = void 0;
16
16
  const amqplib_1 = require("amqplib");
17
17
  const bluebird_1 = __importDefault(require("bluebird"));
18
18
  /**
@@ -21,17 +21,13 @@ const bluebird_1 = __importDefault(require("bluebird"));
21
21
  */
22
22
  function timeoutAfter(seconds = 540) {
23
23
  return __awaiter(this, void 0, void 0, function* () {
24
- return new Promise((resolve, reject) => {
25
- setTimeout(() => reject(new Error(`${seconds} seconds timeout exceeded`)), seconds * 1000);
26
- });
24
+ return new Promise((resolve, reject) => setTimeout(() => reject(new Error(`${seconds} seconds timeout exceeded`)), seconds * 1000));
27
25
  });
28
26
  }
29
27
  exports.timeoutAfter = timeoutAfter;
30
28
  function delay(seconds) {
31
29
  return __awaiter(this, void 0, void 0, function* () {
32
- return new Promise((resolve, reject) => {
33
- setTimeout(() => resolve(undefined), seconds * 1000);
34
- });
30
+ return new Promise((resolve) => setTimeout(() => resolve(undefined), seconds * 1000));
35
31
  });
36
32
  }
37
33
  exports.delay = delay;
@@ -81,6 +77,7 @@ function A1ToColNum(value) {
81
77
  return result;
82
78
  }
83
79
  exports.A1ToColNum = A1ToColNum;
80
+ //
84
81
  function withAmqpConn(fn, url) {
85
82
  return __awaiter(this, void 0, void 0, function* () {
86
83
  function withDisposer() {
@@ -107,3 +104,12 @@ function withAmqpCh(fn, url) {
107
104
  });
108
105
  }
109
106
  exports.withAmqpCh = withAmqpCh;
107
+ function sentToQueueAsync(ch, queue, json, options) {
108
+ return __awaiter(this, void 0, void 0, function* () {
109
+ const payload = Buffer.from(JSON.stringify(json));
110
+ const keepSending = ch.sendToQueue(queue, payload, options);
111
+ if (!keepSending)
112
+ yield new Promise(resolve => ch.once('drain', () => resolve));
113
+ });
114
+ }
115
+ exports.sentToQueueAsync = sentToQueueAsync;
package/src/utils.ts CHANGED
@@ -1,20 +1,18 @@
1
- import { Channel, connect, Connection } from 'amqplib';
1
+ import { Channel, connect, Connection, Options } from 'amqplib';
2
2
  import Bluebird from 'bluebird';
3
+ import { Dictionary } from 'lodash';
3
4
 
4
5
  /**
5
6
  *
6
7
  * @param seconds Google function v1 timeout limit (max: 9 min)
7
8
  */
8
9
  export async function timeoutAfter(seconds: number = 540) {
9
- return new Promise<undefined>((resolve, reject) => {
10
- setTimeout(() => reject(new Error(`${seconds} seconds timeout exceeded`)), seconds * 1000);
11
- });
10
+ return new Promise<undefined>((resolve, reject) =>
11
+ setTimeout(() => reject(new Error(`${seconds} seconds timeout exceeded`)), seconds * 1000));
12
12
  }
13
13
 
14
14
  export async function delay(seconds: number) {
15
- return new Promise<undefined>((resolve, reject) => {
16
- setTimeout(() => resolve(undefined), seconds * 1000);
17
- });
15
+ return new Promise((resolve) => setTimeout(() => resolve(undefined), seconds * 1000));
18
16
  }
19
17
 
20
18
  //
@@ -67,6 +65,8 @@ export function A1ToColNum(value: string) {
67
65
  return result;
68
66
  }
69
67
 
68
+ //
69
+
70
70
  export async function withAmqpConn(fn: (conn: Connection) => Promise<any>, url: string) {
71
71
  function withDisposer() {
72
72
  return Bluebird.method(async () => {
@@ -90,4 +90,10 @@ export async function withAmqpCh(fn: (ch: Channel) => Promise<any>, url: string)
90
90
  return Bluebird.using(withDisposer(), (ch) => fn(ch));
91
91
 
92
92
  }, url);
93
+ }
94
+
95
+ export async function sentToQueueAsync(ch: Channel, queue: string, json: Dictionary<any>, options: Options.Publish) {
96
+ const payload = Buffer.from(JSON.stringify(json));
97
+ const keepSending = ch.sendToQueue(queue, payload, options);
98
+ if (!keepSending) await new Promise(resolve => ch.once('drain', () => resolve));
93
99
  }