index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.QUEUE_EVENT_SUFFIX = exports.toString = exports.errorToJSON = exports.parseObjectValues = exports.isRedisVersionLowerThan = exports.childSend = exports.asyncSend = exports.DELAY_TIME_1 = exports.DELAY_TIME_5 = exports.clientCommandMessageReg = exports.optsEncodeMap = exports.optsDecodeMap = exports.errorObject = void 0;
  4. exports.tryCatch = tryCatch;
  5. exports.lengthInUtf8Bytes = lengthInUtf8Bytes;
  6. exports.isEmpty = isEmpty;
  7. exports.array2obj = array2obj;
  8. exports.objectToFlatArray = objectToFlatArray;
  9. exports.delay = delay;
  10. exports.increaseMaxListeners = increaseMaxListeners;
  11. exports.invertObject = invertObject;
  12. exports.isRedisInstance = isRedisInstance;
  13. exports.isRedisCluster = isRedisCluster;
  14. exports.decreaseMaxListeners = decreaseMaxListeners;
  15. exports.removeAllQueueData = removeAllQueueData;
  16. exports.getParentKey = getParentKey;
  17. exports.isNotConnectionError = isNotConnectionError;
  18. exports.removeUndefinedFields = removeUndefinedFields;
  19. exports.trace = trace;
  20. const ioredis_1 = require("ioredis");
  21. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  22. // @ts-ignore
  23. const utils_1 = require("ioredis/built/utils");
  24. const semver = require("semver");
  25. const enums_1 = require("../enums");
  26. exports.errorObject = { value: null };
  27. function tryCatch(fn, ctx, args) {
  28. try {
  29. return fn.apply(ctx, args);
  30. }
  31. catch (e) {
  32. exports.errorObject.value = e;
  33. return exports.errorObject;
  34. }
  35. }
  36. /**
  37. * Checks the size of string for ascii/non-ascii characters
  38. * @see https://stackoverflow.com/a/23318053/1347170
  39. * @param str -
  40. */
  41. function lengthInUtf8Bytes(str) {
  42. return Buffer.byteLength(str, 'utf8');
  43. }
  44. function isEmpty(obj) {
  45. for (const key in obj) {
  46. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  47. return false;
  48. }
  49. }
  50. return true;
  51. }
  52. function array2obj(arr) {
  53. const obj = {};
  54. for (let i = 0; i < arr.length; i += 2) {
  55. obj[arr[i]] = arr[i + 1];
  56. }
  57. return obj;
  58. }
  59. function objectToFlatArray(obj) {
  60. const arr = [];
  61. for (const key in obj) {
  62. if (Object.prototype.hasOwnProperty.call(obj, key) &&
  63. obj[key] !== undefined) {
  64. arr[arr.length] = key;
  65. arr[arr.length] = obj[key];
  66. }
  67. }
  68. return arr;
  69. }
  70. function delay(ms, abortController) {
  71. return new Promise(resolve => {
  72. // eslint-disable-next-line prefer-const
  73. let timeout;
  74. const callback = () => {
  75. abortController === null || abortController === void 0 ? void 0 : abortController.signal.removeEventListener('abort', callback);
  76. clearTimeout(timeout);
  77. resolve();
  78. };
  79. timeout = setTimeout(callback, ms);
  80. abortController === null || abortController === void 0 ? void 0 : abortController.signal.addEventListener('abort', callback);
  81. });
  82. }
  83. function increaseMaxListeners(emitter, count) {
  84. const maxListeners = emitter.getMaxListeners();
  85. emitter.setMaxListeners(maxListeners + count);
  86. }
  87. function invertObject(obj) {
  88. return Object.entries(obj).reduce((result, [key, value]) => {
  89. result[value] = key;
  90. return result;
  91. }, {});
  92. }
  93. exports.optsDecodeMap = {
  94. de: 'deduplication',
  95. fpof: 'failParentOnFailure',
  96. cpof: 'continueParentOnFailure',
  97. idof: 'ignoreDependencyOnFailure',
  98. kl: 'keepLogs',
  99. rdof: 'removeDependencyOnFailure',
  100. };
  101. exports.optsEncodeMap = Object.assign(Object.assign({}, invertObject(exports.optsDecodeMap)), {
  102. /*/ Legacy for backwards compatibility */ debounce: 'de' });
  103. function isRedisInstance(obj) {
  104. if (!obj) {
  105. return false;
  106. }
  107. const redisApi = ['connect', 'disconnect', 'duplicate'];
  108. return redisApi.every(name => typeof obj[name] === 'function');
  109. }
  110. function isRedisCluster(obj) {
  111. return isRedisInstance(obj) && obj.isCluster;
  112. }
  113. function decreaseMaxListeners(emitter, count) {
  114. increaseMaxListeners(emitter, -count);
  115. }
  116. async function removeAllQueueData(client, queueName, prefix = process.env.BULLMQ_TEST_PREFIX || 'bull') {
  117. if (client instanceof ioredis_1.Cluster) {
  118. // todo compat with cluster ?
  119. // @see https://github.com/luin/ioredis/issues/175
  120. return Promise.resolve(false);
  121. }
  122. const pattern = `${prefix}:${queueName}:*`;
  123. const pendingOperations = [];
  124. await new Promise((resolve, reject) => {
  125. const stream = client.scanStream({
  126. match: pattern,
  127. });
  128. stream.on('data', (keys) => {
  129. if (keys.length) {
  130. const pipeline = client.pipeline();
  131. keys.forEach(key => {
  132. pipeline.del(key);
  133. });
  134. const execPromise = pipeline.exec().catch(error => {
  135. reject(error);
  136. throw error;
  137. });
  138. pendingOperations.push(execPromise);
  139. }
  140. });
  141. stream.on('end', () => resolve());
  142. stream.on('error', error => reject(error));
  143. });
  144. // Wait for all pipeline operations to complete before closing the connection
  145. await Promise.all(pendingOperations);
  146. // Handle connection close with better error handling for Dragonfly
  147. try {
  148. await client.quit();
  149. }
  150. catch (error) {
  151. if (isNotConnectionError(error)) {
  152. throw error;
  153. }
  154. }
  155. }
  156. function getParentKey(opts) {
  157. if (opts) {
  158. return `${opts.queue}:${opts.id}`;
  159. }
  160. }
  161. exports.clientCommandMessageReg = /ERR unknown command ['`]\s*client\s*['`]/;
  162. exports.DELAY_TIME_5 = 5000;
  163. exports.DELAY_TIME_1 = 100;
  164. function isNotConnectionError(error) {
  165. const { code, message: errorMessage } = error;
  166. return (errorMessage !== utils_1.CONNECTION_CLOSED_ERROR_MSG &&
  167. !errorMessage.includes('ECONNREFUSED') &&
  168. code !== 'ECONNREFUSED');
  169. }
  170. const asyncSend = (proc, msg) => {
  171. return new Promise((resolve, reject) => {
  172. if (typeof proc.send === 'function') {
  173. proc.send(msg, (err) => {
  174. if (err) {
  175. reject(err);
  176. }
  177. else {
  178. resolve();
  179. }
  180. });
  181. }
  182. else if (typeof proc.postMessage === 'function') {
  183. resolve(proc.postMessage(msg));
  184. }
  185. else {
  186. resolve();
  187. }
  188. });
  189. };
  190. exports.asyncSend = asyncSend;
  191. const childSend = (proc, msg) => (0, exports.asyncSend)(proc, msg);
  192. exports.childSend = childSend;
  193. const isRedisVersionLowerThan = (currentVersion, minimumVersion, currentDatabaseType, desiredDatabaseType = 'redis') => {
  194. if (currentDatabaseType === desiredDatabaseType) {
  195. const version = semver.valid(semver.coerce(currentVersion));
  196. return semver.lt(version, minimumVersion);
  197. }
  198. return false;
  199. };
  200. exports.isRedisVersionLowerThan = isRedisVersionLowerThan;
  201. const parseObjectValues = (obj) => {
  202. const accumulator = {};
  203. for (const value of Object.entries(obj)) {
  204. accumulator[value[0]] = JSON.parse(value[1]);
  205. }
  206. return accumulator;
  207. };
  208. exports.parseObjectValues = parseObjectValues;
  209. const getCircularReplacer = (rootReference) => {
  210. const references = new WeakSet();
  211. references.add(rootReference);
  212. return (_, value) => {
  213. if (typeof value === 'object' && value !== null) {
  214. if (references.has(value)) {
  215. return '[Circular]';
  216. }
  217. references.add(value);
  218. }
  219. return value;
  220. };
  221. };
  222. const errorToJSON = (value) => {
  223. const error = {};
  224. Object.getOwnPropertyNames(value).forEach(function (propName) {
  225. error[propName] = value[propName];
  226. });
  227. return JSON.parse(JSON.stringify(error, getCircularReplacer(value)));
  228. };
  229. exports.errorToJSON = errorToJSON;
  230. const INFINITY = 1 / 0;
  231. const toString = (value) => {
  232. if (value == null) {
  233. return '';
  234. }
  235. // Exit early for strings to avoid a performance hit in some environments.
  236. if (typeof value === 'string') {
  237. return value;
  238. }
  239. if (Array.isArray(value)) {
  240. // Recursively convert values (susceptible to call stack limits).
  241. return `${value.map(other => (other == null ? other : (0, exports.toString)(other)))}`;
  242. }
  243. if (typeof value == 'symbol' ||
  244. Object.prototype.toString.call(value) == '[object Symbol]') {
  245. return value.toString();
  246. }
  247. const result = `${value}`;
  248. return result === '0' && 1 / value === -INFINITY ? '-0' : result;
  249. };
  250. exports.toString = toString;
  251. exports.QUEUE_EVENT_SUFFIX = ':qe';
  252. function removeUndefinedFields(obj) {
  253. const newObj = {};
  254. for (const key in obj) {
  255. if (obj[key] !== undefined) {
  256. newObj[key] = obj[key];
  257. }
  258. }
  259. return newObj;
  260. }
  261. /**
  262. * Wraps the code with telemetry and provides a span for configuration.
  263. *
  264. * @param telemetry - telemetry configuration. If undefined, the callback will be executed without telemetry.
  265. * @param spanKind - kind of the span: Producer, Consumer, Internal
  266. * @param queueName - queue name
  267. * @param operation - operation name (such as add, process, etc)
  268. * @param destination - destination name (normally the queue name)
  269. * @param callback - code to wrap with telemetry
  270. * @param srcPropagationMetadata -
  271. * @returns
  272. */
  273. async function trace(telemetry, spanKind, queueName, operation, destination, callback, srcPropagationMetadata) {
  274. if (!telemetry) {
  275. return callback();
  276. }
  277. else {
  278. const { tracer, contextManager } = telemetry;
  279. const currentContext = contextManager.active();
  280. let parentContext;
  281. if (srcPropagationMetadata) {
  282. parentContext = contextManager.fromMetadata(currentContext, srcPropagationMetadata);
  283. }
  284. const spanName = destination ? `${operation} ${destination}` : operation;
  285. const span = tracer.startSpan(spanName, {
  286. kind: spanKind,
  287. }, parentContext);
  288. try {
  289. span.setAttributes({
  290. [enums_1.TelemetryAttributes.QueueName]: queueName,
  291. [enums_1.TelemetryAttributes.QueueOperation]: operation,
  292. });
  293. let messageContext;
  294. let dstPropagationMetadata;
  295. if (spanKind === enums_1.SpanKind.CONSUMER && parentContext) {
  296. messageContext = span.setSpanOnContext(parentContext);
  297. }
  298. else {
  299. messageContext = span.setSpanOnContext(currentContext);
  300. }
  301. if (callback.length == 2) {
  302. dstPropagationMetadata = contextManager.getMetadata(messageContext);
  303. }
  304. return await contextManager.with(messageContext, () => callback(span, dstPropagationMetadata));
  305. }
  306. catch (err) {
  307. span.recordException(err);
  308. throw err;
  309. }
  310. finally {
  311. span.end();
  312. }
  313. }
  314. }
  315. //# sourceMappingURL=index.js.map