gcf-common-lib 0.23.30 → 0.23.31

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.30",
4
+ "version": "0.23.31",
5
5
  "publishConfig": {
6
6
  "access": "public",
7
7
  "branches": [
package/src/index.js CHANGED
@@ -101,14 +101,10 @@ class GcfCommon {
101
101
  if (queue && !(0, isEmpty_1.default)(queue)) {
102
102
  console.log('send:', queue, appId, env, json, attributes);
103
103
  return (0, utils_1.withAmqpCh)((ch) => __awaiter(this, void 0, void 0, function* () {
104
- const payload = Buffer.from(JSON.stringify(json !== null && json !== void 0 ? json : {}));
105
104
  yield ch.assertQueue(queue, this.amqpOptions.assertOptions);
106
- yield new Promise((resolve, reject) => ch.sendToQueue(queue, payload, this.amqpOptions.publishOptions, (err, ok) => {
107
- if (err)
108
- reject(err);
109
- else
110
- resolve(ok);
111
- }));
105
+ // const payload = Buffer.from(JSON.stringify(json ?? {}));
106
+ // ch.sendToQueue(queue, payload, this.amqpOptions.publishOptions);
107
+ yield (0, utils_1.sendToQueueAsync)(ch, queue, json !== null && json !== void 0 ? json : {}, this.amqpOptions.publishOptions);
112
108
  }), this.amqpOptions.url);
113
109
  }
114
110
  });
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { PubSub } from '@google-cloud/pubsub';
2
2
  import isEmpty from 'lodash/isEmpty';
3
3
  import noop from 'lodash/noop';
4
- import { timeoutAfter, withAmqpCh } from './utils';
4
+ import { delay, sendToQueueAsync, timeoutAfter, withAmqpCh } from './utils';
5
5
  import { Storage } from '@google-cloud/storage';
6
6
  import { SecretManagerServiceClient } from '@google-cloud/secret-manager';
7
7
  import { TContext, TEvent, TGSEvent, TMetadata, TPSEvent, TResponse } from './types';
@@ -85,15 +85,10 @@ export class GcfCommon {
85
85
  if (queue && !isEmpty(queue)) {
86
86
  console.log('send:', queue, appId, env, json, attributes);
87
87
  return withAmqpCh(async ch => {
88
- const payload = Buffer.from(JSON.stringify(json ?? {}));
89
88
  await ch.assertQueue(queue, this.amqpOptions.assertOptions);
90
-
91
- await new Promise((resolve, reject) =>
92
- ch.sendToQueue(queue, payload, this.amqpOptions.publishOptions, (err, ok) => {
93
- if (err) reject(err); else resolve(ok);
94
- }),
95
- );
96
-
89
+ // const payload = Buffer.from(JSON.stringify(json ?? {}));
90
+ // ch.sendToQueue(queue, payload, this.amqpOptions.publishOptions);
91
+ await sendToQueueAsync(ch, queue, json ?? {}, this.amqpOptions.publishOptions);
97
92
  }, this.amqpOptions.url as string);
98
93
  }
99
94
  }
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.sendToQueueAsync = exports.withAmqpCh = exports.withAmqpConn = exports.A1ToColNum = exports.colNumToA1 = exports.A1ToIndex = exports.indexToA1 = exports.delay = exports.timeoutAfter = void 0;
15
+ exports.sendToQueueConfAsync = exports.sendToQueueAsync = exports.withAmqpConfCh = 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
  /**
@@ -96,14 +96,27 @@ function withAmqpCh(fn, url) {
96
96
  return withAmqpConn((conn) => __awaiter(this, void 0, void 0, function* () {
97
97
  function withDisposer() {
98
98
  return bluebird_1.default.method(() => __awaiter(this, void 0, void 0, function* () {
99
- return conn.createConfirmChannel();
100
- }))().disposer((ch, promise) => delay(1000).then(() => ch.close()));
99
+ return conn.createChannel();
100
+ }))().disposer((ch, promise) => ch.close());
101
101
  }
102
102
  return bluebird_1.default.using(withDisposer(), (ch) => fn(ch));
103
103
  }), url);
104
104
  });
105
105
  }
106
106
  exports.withAmqpCh = withAmqpCh;
107
+ function withAmqpConfCh(fn, url) {
108
+ return __awaiter(this, void 0, void 0, function* () {
109
+ return withAmqpConn((conn) => __awaiter(this, void 0, void 0, function* () {
110
+ function withDisposer() {
111
+ return bluebird_1.default.method(() => __awaiter(this, void 0, void 0, function* () {
112
+ return conn.createConfirmChannel();
113
+ }))().disposer((ch, promise) => ch.close());
114
+ }
115
+ return bluebird_1.default.using(withDisposer(), (ch) => fn(ch));
116
+ }), url);
117
+ });
118
+ }
119
+ exports.withAmqpConfCh = withAmqpConfCh;
107
120
  function sendToQueueAsync(ch, queue, json, options) {
108
121
  return __awaiter(this, void 0, void 0, function* () {
109
122
  const payload = Buffer.from(JSON.stringify(json));
@@ -113,3 +126,17 @@ function sendToQueueAsync(ch, queue, json, options) {
113
126
  });
114
127
  }
115
128
  exports.sendToQueueAsync = sendToQueueAsync;
129
+ function sendToQueueConfAsync(ch, queue, json, options) {
130
+ return __awaiter(this, void 0, void 0, function* () {
131
+ const payload = Buffer.from(JSON.stringify(json));
132
+ yield new Promise((resolve, reject) => {
133
+ ch.sendToQueue(queue, payload, options, (err, ok) => {
134
+ if (err)
135
+ reject(err);
136
+ else
137
+ resolve(ok);
138
+ });
139
+ });
140
+ });
141
+ }
142
+ exports.sendToQueueConfAsync = sendToQueueConfAsync;
package/src/utils.ts CHANGED
@@ -79,12 +79,25 @@ export async function withAmqpConn(fn: (conn: Connection) => Promise<any>, url:
79
79
  return Bluebird.using(withDisposer(), (conn) => fn(conn));
80
80
  }
81
81
 
82
- export async function withAmqpCh(fn: (ch: ConfirmChannel) => Promise<any>, url: string) {
82
+ export async function withAmqpCh(fn: (ch: Channel) => Promise<any>, url: string) {
83
+ return withAmqpConn(async conn => {
84
+ function withDisposer() {
85
+ return Bluebird.method(async () => {
86
+ return conn.createChannel();
87
+ })().disposer((ch, promise) => ch.close());
88
+ }
89
+
90
+ return Bluebird.using(withDisposer(), (ch) => fn(ch));
91
+
92
+ }, url);
93
+ }
94
+
95
+ export async function withAmqpConfCh(fn: (ch: ConfirmChannel) => Promise<any>, url: string) {
83
96
  return withAmqpConn(async conn => {
84
97
  function withDisposer() {
85
98
  return Bluebird.method(async () => {
86
99
  return conn.createConfirmChannel();
87
- })().disposer((ch, promise) => delay(1000).then(() => ch.close()));
100
+ })().disposer((ch, promise) => ch.close());
88
101
  }
89
102
 
90
103
  return Bluebird.using(withDisposer(), (ch) => fn(ch));
@@ -96,4 +109,13 @@ export async function sendToQueueAsync(ch: Channel, queue: string, json: Diction
96
109
  const payload = Buffer.from(JSON.stringify(json));
97
110
  const keepSending = ch.sendToQueue(queue, payload, options);
98
111
  if (!keepSending) await new Promise(resolve => ch.once('drain', () => resolve));
112
+ }
113
+
114
+ export async function sendToQueueConfAsync(ch: ConfirmChannel, queue: string, json: Dictionary<any>, options?: Options.Publish) {
115
+ const payload = Buffer.from(JSON.stringify(json));
116
+ await new Promise((resolve, reject) => {
117
+ ch.sendToQueue(queue, payload, options, (err, ok) => {
118
+ if (err) reject(err); else resolve(ok);
119
+ });
120
+ });
99
121
  }