@azteam/rabbitmq-async 1.0.143 → 1.0.145

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.
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+
3
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports["default"] = void 0;
8
+ var _RabbitMQAsync = _interopRequireDefault(require("./RabbitMQAsync"));
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
10
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return generator._invoke = function (innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; }(innerFn, self, context), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; this._invoke = function (method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); }; } function maybeInvokeDelegate(delegate, context) { var method = delegate.iterator[context.method]; if (undefined === method) { if (context.delegate = null, "throw" === context.method) { if (delegate.iterator["return"] && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel; context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method"); } return ContinueSentinel; } var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) { if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; } return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (object) { var keys = []; for (var key in object) { keys.push(key); } return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) { "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); } }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, "catch": function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; }
11
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
12
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
13
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
14
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
15
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
16
+ var Provider = /*#__PURE__*/function () {
17
+ function Provider() {
18
+ var _this = this;
19
+ var configs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
20
+ _classCallCheck(this, Provider);
21
+ this.connections = {};
22
+ this.configs = {};
23
+ if (Array.isArray(configs)) {
24
+ configs.map(function (config) {
25
+ _this.configs[config.name] = config;
26
+ return true;
27
+ });
28
+ } else {
29
+ this.configs.main = configs;
30
+ }
31
+ }
32
+ _createClass(Provider, [{
33
+ key: "getConnection",
34
+ value: function getConnection() {
35
+ var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'main';
36
+ if (!this.connections[name]) {
37
+ this.connections[name] = new _RabbitMQAsync["default"](this.configs[name]);
38
+ }
39
+ return this.connections[name];
40
+ }
41
+ }, {
42
+ key: "waitAllConnection",
43
+ value: function () {
44
+ var _waitAllConnection = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
45
+ var _this2 = this;
46
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
47
+ while (1) {
48
+ switch (_context.prev = _context.next) {
49
+ case 0:
50
+ _context.next = 2;
51
+ return Promise.all(Object.keys(this.configs).map(function (keyConfig) {
52
+ var connection = _this2.getConnection(keyConfig);
53
+ return connection.waitConnection();
54
+ }));
55
+ case 2:
56
+ case "end":
57
+ return _context.stop();
58
+ }
59
+ }
60
+ }, _callee, this);
61
+ }));
62
+ function waitAllConnection() {
63
+ return _waitAllConnection.apply(this, arguments);
64
+ }
65
+ return waitAllConnection;
66
+ }()
67
+ }]);
68
+ return Provider;
69
+ }();
70
+ var _default = Provider;
71
+ exports["default"] = _default;
@@ -0,0 +1,511 @@
1
+ "use strict";
2
+
3
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports["default"] = void 0;
8
+ var _amqplib = _interopRequireDefault(require("amqplib"));
9
+ var _util = require("@azteam/util");
10
+ var _lodash = _interopRequireDefault(require("lodash"));
11
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
12
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return generator._invoke = function (innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; }(innerFn, self, context), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; this._invoke = function (method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); }; } function maybeInvokeDelegate(delegate, context) { var method = delegate.iterator[context.method]; if (undefined === method) { if (context.delegate = null, "throw" === context.method) { if (delegate.iterator["return"] && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel; context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method"); } return ContinueSentinel; } var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) { if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; } return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (object) { var keys = []; for (var key in object) { keys.push(key); } return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) { "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); } }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, "catch": function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; }
13
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
14
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
15
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
16
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
17
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
18
+ var RabbitMQAsync = /*#__PURE__*/function () {
19
+ function RabbitMQAsync(config) {
20
+ _classCallCheck(this, RabbitMQAsync);
21
+ this.connected = false;
22
+ this.username = config.username;
23
+ this.password = config.password;
24
+ this.host = config.host;
25
+ this.prefix = config.prefix;
26
+ this.connect();
27
+ }
28
+ _createClass(RabbitMQAsync, [{
29
+ key: "waitConnection",
30
+ value: function () {
31
+ var _waitConnection = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
32
+ var n,
33
+ i,
34
+ _args = arguments;
35
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
36
+ while (1) {
37
+ switch (_context.prev = _context.next) {
38
+ case 0:
39
+ n = _args.length > 0 && _args[0] !== undefined ? _args[0] : 10;
40
+ i = 0;
41
+ case 2:
42
+ if (!(!this.connected || i < n)) {
43
+ _context.next = 8;
44
+ break;
45
+ }
46
+ _context.next = 5;
47
+ return (0, _util.timeout)(1000);
48
+ case 5:
49
+ i += 1;
50
+ _context.next = 2;
51
+ break;
52
+ case 8:
53
+ if (this.connected) {
54
+ _context.next = 10;
55
+ break;
56
+ }
57
+ throw new Error('Rabbit not connected');
58
+ case 10:
59
+ case "end":
60
+ return _context.stop();
61
+ }
62
+ }
63
+ }, _callee, this);
64
+ }));
65
+ function waitConnection() {
66
+ return _waitConnection.apply(this, arguments);
67
+ }
68
+ return waitConnection;
69
+ }()
70
+ }, {
71
+ key: "connect",
72
+ value: function () {
73
+ var _connect = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
74
+ var _this = this;
75
+ var opt;
76
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
77
+ while (1) {
78
+ switch (_context2.prev = _context2.next) {
79
+ case 0:
80
+ this._alert('connecting', 'MQ connecting...');
81
+ opt = {
82
+ credentials: _amqplib["default"].credentials.plain(this.username, this.password)
83
+ };
84
+ _context2.prev = 2;
85
+ _context2.next = 5;
86
+ return _amqplib["default"].connect("amqp://".concat(this.host), opt);
87
+ case 5:
88
+ this.client = _context2.sent;
89
+ this.connected = true;
90
+ this._alert('connect', 'MQ connected');
91
+ this.client.on('error', function (err) {
92
+ _this.connected = false;
93
+ _this._alert('error', "MQ Error".concat(err));
94
+ _this.reconnect();
95
+ });
96
+ this.client.on('close', function () {
97
+ _this.connected = false;
98
+ _this._alert('close', 'MQ closed');
99
+ _this.reconnect();
100
+ });
101
+ _context2.next = 18;
102
+ break;
103
+ case 12:
104
+ _context2.prev = 12;
105
+ _context2.t0 = _context2["catch"](2);
106
+ this.connected = false;
107
+ this._alert('error', "MQ connect".concat(_context2.t0));
108
+ _context2.next = 18;
109
+ return this.reconnect();
110
+ case 18:
111
+ case "end":
112
+ return _context2.stop();
113
+ }
114
+ }
115
+ }, _callee2, this, [[2, 12]]);
116
+ }));
117
+ function connect() {
118
+ return _connect.apply(this, arguments);
119
+ }
120
+ return connect;
121
+ }()
122
+ }, {
123
+ key: "reconnect",
124
+ value: function () {
125
+ var _reconnect = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
126
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
127
+ while (1) {
128
+ switch (_context3.prev = _context3.next) {
129
+ case 0:
130
+ this._alert('reconnect', 'MQ try reconnect...');
131
+ _context3.next = 3;
132
+ return (0, _util.timeout)(5000);
133
+ case 3:
134
+ _context3.next = 5;
135
+ return this.connect();
136
+ case 5:
137
+ case "end":
138
+ return _context3.stop();
139
+ }
140
+ }
141
+ }, _callee3, this);
142
+ }));
143
+ function reconnect() {
144
+ return _reconnect.apply(this, arguments);
145
+ }
146
+ return reconnect;
147
+ }()
148
+ }, {
149
+ key: "close",
150
+ value: function close() {
151
+ this.connected = false;
152
+ this.client.close();
153
+ }
154
+ }, {
155
+ key: "parsePrefix",
156
+ value: function parsePrefix(name) {
157
+ if (this.prefix) {
158
+ return "".concat(this.prefix, ":").concat(name);
159
+ }
160
+ return name;
161
+ }
162
+ }, {
163
+ key: "send",
164
+ value: function () {
165
+ var _send = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(queueName) {
166
+ var msg,
167
+ force,
168
+ prefixQueueName,
169
+ channel,
170
+ queueInfo,
171
+ _args4 = arguments;
172
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
173
+ while (1) {
174
+ switch (_context4.prev = _context4.next) {
175
+ case 0:
176
+ msg = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : {};
177
+ force = _args4.length > 2 && _args4[2] !== undefined ? _args4[2] : true;
178
+ prefixQueueName = this.parsePrefix(queueName);
179
+ if (!this.connected) {
180
+ _context4.next = 33;
181
+ break;
182
+ }
183
+ _context4.prev = 4;
184
+ _context4.next = 7;
185
+ return this.client.createChannel();
186
+ case 7:
187
+ channel = _context4.sent;
188
+ _context4.next = 10;
189
+ return channel.assertQueue(prefixQueueName, {
190
+ durable: true
191
+ });
192
+ case 10:
193
+ queueInfo = _context4.sent;
194
+ if (!(force || queueInfo.messageCount === 0)) {
195
+ _context4.next = 14;
196
+ break;
197
+ }
198
+ _context4.next = 14;
199
+ return channel.sendToQueue(prefixQueueName, Buffer.from(JSON.stringify(msg)), {
200
+ persistent: true
201
+ });
202
+ case 14:
203
+ return _context4.abrupt("return", true);
204
+ case 17:
205
+ _context4.prev = 17;
206
+ _context4.t0 = _context4["catch"](4);
207
+ _context4.next = 21;
208
+ return (0, _util.timeout)(2000);
209
+ case 21:
210
+ return _context4.abrupt("return", this.send(queueName, msg));
211
+ case 22:
212
+ _context4.prev = 22;
213
+ _context4.prev = 23;
214
+ _context4.next = 26;
215
+ return channel.close();
216
+ case 26:
217
+ _context4.next = 30;
218
+ break;
219
+ case 28:
220
+ _context4.prev = 28;
221
+ _context4.t1 = _context4["catch"](23);
222
+ case 30:
223
+ return _context4.finish(22);
224
+ case 31:
225
+ _context4.next = 36;
226
+ break;
227
+ case 33:
228
+ _context4.next = 35;
229
+ return (0, _util.timeout)(2000);
230
+ case 35:
231
+ return _context4.abrupt("return", this.send(queueName, msg));
232
+ case 36:
233
+ case "end":
234
+ return _context4.stop();
235
+ }
236
+ }
237
+ }, _callee4, this, [[4, 17, 22, 31], [23, 28]]);
238
+ }));
239
+ function send(_x) {
240
+ return _send.apply(this, arguments);
241
+ }
242
+ return send;
243
+ }()
244
+ }, {
245
+ key: "getInfo",
246
+ value: function () {
247
+ var _getInfo = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(queueName) {
248
+ var prefixQueueName, channel;
249
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
250
+ while (1) {
251
+ switch (_context5.prev = _context5.next) {
252
+ case 0:
253
+ prefixQueueName = this.parsePrefix(queueName);
254
+ if (!this.connected) {
255
+ _context5.next = 25;
256
+ break;
257
+ }
258
+ _context5.prev = 2;
259
+ _context5.next = 5;
260
+ return this.client.createChannel();
261
+ case 5:
262
+ channel = _context5.sent;
263
+ _context5.next = 8;
264
+ return channel.assertQueue(prefixQueueName, {
265
+ durable: true
266
+ });
267
+ case 8:
268
+ return _context5.abrupt("return", _context5.sent);
269
+ case 11:
270
+ _context5.prev = 11;
271
+ _context5.t0 = _context5["catch"](2);
272
+ return _context5.abrupt("return", this.getInfo(queueName));
273
+ case 14:
274
+ _context5.prev = 14;
275
+ _context5.prev = 15;
276
+ _context5.next = 18;
277
+ return channel.close();
278
+ case 18:
279
+ _context5.next = 22;
280
+ break;
281
+ case 20:
282
+ _context5.prev = 20;
283
+ _context5.t1 = _context5["catch"](15);
284
+ case 22:
285
+ return _context5.finish(14);
286
+ case 23:
287
+ _context5.next = 26;
288
+ break;
289
+ case 25:
290
+ return _context5.abrupt("return", this.getInfo(queueName));
291
+ case 26:
292
+ case "end":
293
+ return _context5.stop();
294
+ }
295
+ }
296
+ }, _callee5, this, [[2, 11, 14, 23], [15, 20]]);
297
+ }));
298
+ function getInfo(_x2) {
299
+ return _getInfo.apply(this, arguments);
300
+ }
301
+ return getInfo;
302
+ }()
303
+ }, {
304
+ key: "receiving",
305
+ value: function () {
306
+ var _receiving = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(queueName, cb) {
307
+ var callbackError,
308
+ prefixQueueName,
309
+ channel,
310
+ messageQueue,
311
+ _args7 = arguments;
312
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
313
+ while (1) {
314
+ switch (_context7.prev = _context7.next) {
315
+ case 0:
316
+ callbackError = _args7.length > 2 && _args7[2] !== undefined ? _args7[2] : null;
317
+ prefixQueueName = this.parsePrefix(queueName);
318
+ if (!this.connected) {
319
+ _context7.next = 30;
320
+ break;
321
+ }
322
+ _context7.prev = 3;
323
+ _context7.next = 6;
324
+ return this.client.createChannel();
325
+ case 6:
326
+ channel = _context7.sent;
327
+ _context7.next = 9;
328
+ return channel.assertQueue(prefixQueueName, {
329
+ durable: true
330
+ });
331
+ case 9:
332
+ _context7.next = 11;
333
+ return channel.prefetch(1);
334
+ case 11:
335
+ messageQueue = this;
336
+ _context7.next = 14;
337
+ return new Promise(function (resolve, reject) {
338
+ channel.consume(prefixQueueName, /*#__PURE__*/function () {
339
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(msg) {
340
+ var data, errString;
341
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
342
+ while (1) {
343
+ switch (_context6.prev = _context6.next) {
344
+ case 0:
345
+ if (!msg) {
346
+ _context6.next = 50;
347
+ break;
348
+ }
349
+ data = JSON.parse(msg.content.toString());
350
+ if (!data) {
351
+ _context6.next = 47;
352
+ break;
353
+ }
354
+ _context6.prev = 3;
355
+ if (!msg.fields.redelivered) {
356
+ _context6.next = 18;
357
+ break;
358
+ }
359
+ if (!data.retry) {
360
+ data.retry = 0;
361
+ }
362
+ data.retry += 1;
363
+ if (!(data.retry < 5)) {
364
+ _context6.next = 12;
365
+ break;
366
+ }
367
+ _context6.next = 10;
368
+ return messageQueue.send(queueName, data);
369
+ case 10:
370
+ _context6.next = 14;
371
+ break;
372
+ case 12:
373
+ _context6.next = 14;
374
+ return messageQueue.send(messageQueue.parsePrefix('RETRY'), {
375
+ queueName: queueName,
376
+ data: data,
377
+ retry_time: new Date()
378
+ });
379
+ case 14:
380
+ _context6.next = 16;
381
+ return channel.ack(msg);
382
+ case 16:
383
+ _context6.next = 40;
384
+ break;
385
+ case 18:
386
+ _context6.prev = 18;
387
+ _context6.next = 21;
388
+ return cb(data);
389
+ case 21:
390
+ _context6.next = 23;
391
+ return channel.ack(msg);
392
+ case 23:
393
+ _context6.next = 40;
394
+ break;
395
+ case 25:
396
+ _context6.prev = 25;
397
+ _context6.t0 = _context6["catch"](18);
398
+ errString = _context6.t0.toString();
399
+ if (!_lodash["default"].some(['IllegalOperationError: Channel closed'], function (el) {
400
+ return _lodash["default"].includes(errString, el);
401
+ })) {
402
+ _context6.next = 32;
403
+ break;
404
+ }
405
+ reject(new Error(errString));
406
+ _context6.next = 40;
407
+ break;
408
+ case 32:
409
+ if (callbackError) {
410
+ callbackError(prefixQueueName, _context6.t0);
411
+ }
412
+ if (!data.noRetry) {
413
+ _context6.next = 38;
414
+ break;
415
+ }
416
+ _context6.next = 36;
417
+ return channel.ack(msg);
418
+ case 36:
419
+ _context6.next = 40;
420
+ break;
421
+ case 38:
422
+ _context6.next = 40;
423
+ return channel.nack(msg);
424
+ case 40:
425
+ _context6.next = 45;
426
+ break;
427
+ case 42:
428
+ _context6.prev = 42;
429
+ _context6.t1 = _context6["catch"](3);
430
+ reject(_context6.t1);
431
+ case 45:
432
+ _context6.next = 48;
433
+ break;
434
+ case 47:
435
+ channel.ack(msg);
436
+ case 48:
437
+ _context6.next = 51;
438
+ break;
439
+ case 50:
440
+ reject(new Error('msg null'));
441
+ case 51:
442
+ case "end":
443
+ return _context6.stop();
444
+ }
445
+ }
446
+ }, _callee6, null, [[3, 42], [18, 25]]);
447
+ }));
448
+ return function (_x5) {
449
+ return _ref.apply(this, arguments);
450
+ };
451
+ }());
452
+ });
453
+ case 14:
454
+ _context7.next = 28;
455
+ break;
456
+ case 16:
457
+ _context7.prev = 16;
458
+ _context7.t0 = _context7["catch"](3);
459
+ _context7.prev = 18;
460
+ _context7.next = 21;
461
+ return channel.close();
462
+ case 21:
463
+ _context7.next = 25;
464
+ break;
465
+ case 23:
466
+ _context7.prev = 23;
467
+ _context7.t1 = _context7["catch"](18);
468
+ case 25:
469
+ _context7.next = 27;
470
+ return (0, _util.timeout)(5000);
471
+ case 27:
472
+ return _context7.abrupt("return", this.receiving(queueName, cb, callbackError));
473
+ case 28:
474
+ _context7.next = 33;
475
+ break;
476
+ case 30:
477
+ _context7.next = 32;
478
+ return (0, _util.timeout)(5000);
479
+ case 32:
480
+ return _context7.abrupt("return", this.receiving(queueName, cb, callbackError));
481
+ case 33:
482
+ case "end":
483
+ return _context7.stop();
484
+ }
485
+ }
486
+ }, _callee7, this, [[3, 16], [18, 23]]);
487
+ }));
488
+ function receiving(_x3, _x4) {
489
+ return _receiving.apply(this, arguments);
490
+ }
491
+ return receiving;
492
+ }()
493
+ }, {
494
+ key: "setAlertCallback",
495
+ value: function setAlertCallback(callback) {
496
+ this.alertCallback = callback;
497
+ }
498
+ }, {
499
+ key: "_alert",
500
+ value: function _alert(status, msg) {
501
+ if (typeof this.alertCallback === 'function') {
502
+ this.alertCallback(status, msg);
503
+ } else {
504
+ console.info(status, msg);
505
+ }
506
+ }
507
+ }]);
508
+ return RabbitMQAsync;
509
+ }();
510
+ var _default = RabbitMQAsync;
511
+ exports["default"] = _default;
package/lib/index.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "RabbitMQAsync", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _RabbitMQAsync["default"];
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "RabbitMQProvider", {
13
+ enumerable: true,
14
+ get: function get() {
15
+ return _Provider["default"];
16
+ }
17
+ });
18
+ var _Provider = _interopRequireDefault(require("./Provider"));
19
+ var _RabbitMQAsync = _interopRequireDefault(require("./RabbitMQAsync"));
20
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
package/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "@azteam/rabbitmq-async",
3
- "version": "1.0.143",
3
+ "version": "1.0.145",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./src/index.js",
7
7
  "scripts": {
8
- "build": "babel src -d lib",
9
- "prepublish": "npm run build"
8
+ "build": "babel src -d lib"
10
9
  },
11
10
  "keywords": [
12
11
  "toda",
@@ -17,7 +16,7 @@
17
16
  "author": "toda <sp.azsolution.net@gmail.com>",
18
17
  "license": "MIT",
19
18
  "dependencies": {
20
- "@azteam/util": "1.0.13",
19
+ "@azteam/util": "1.0.15",
21
20
  "amqplib": "0.8.0",
22
21
  "fs-extra": "10.1.0",
23
22
  "lodash": "4.17.21"