arnavmq 0.10.3 → 0.12.0

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/README.md CHANGED
@@ -74,7 +74,9 @@ arnavmq.publish('queue:name', { message: 'content' }, { rpc: true, timeout: 1000
74
74
 
75
75
  The optional `timeout` option results in a rejection when no answer has been received after the given amount of milliseconds.
76
76
  When '0' is given, there will be no timeout for this call.
77
- This value will overwrite the default timeout set in the config in `rpcTimeout`.
77
+ This value will overwrite the default timeout set in the config in `rpcTimeout`. **Update:** Message-level timeout is deprecated.
78
+ Please use amqp's **expiration** option instead. It gives the same rejection for the producer if the timeout is reached,
79
+ but together with that it also guarantees that this message will not be consumed after the expiration period.
78
80
 
79
81
  ## Routing keys
80
82
 
@@ -116,7 +118,7 @@ You can specify a config object, properties and default values are:
116
118
  hostname: process.env.HOSTNAME || process.env.USER || uuid.v4(),
117
119
 
118
120
  // Deprecated. Use 'logger' instead. The transport to use to debug. If provided, arnavmq will show some logs
119
- transport: utils.emptyLogger
121
+ transport: utils.emptyLogger,
120
122
 
121
123
  /**
122
124
  * A logger object with a log function for each of the log levels ("debug", "info", "warn", or "error").
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arnavmq",
3
- "version": "0.10.3",
3
+ "version": "0.12.0",
4
4
  "description": "ArnavMQ is a RabbitMQ wrapper",
5
5
  "keywords": [
6
6
  "rabbitmq",
@@ -15,7 +15,7 @@
15
15
  "scripts": {
16
16
  "lint": "eslint .",
17
17
  "cover": "test -d .nyc_output && nyc report --reporter lcov",
18
- "test": "nyc mocha --recursive --timeout=30000 --exit"
18
+ "test": "nyc mocha --recursive --timeout=45000 --exit"
19
19
  },
20
20
  "repository": {
21
21
  "type": "git",
@@ -28,24 +28,21 @@
28
28
  },
29
29
  "homepage": "https://github.com/bringg/node-arnavmq#readme",
30
30
  "dependencies": {
31
- "amqplib": "^0.10.0",
31
+ "amqplib": "^0.10.3",
32
32
  "p-defer": "^3.0.0",
33
33
  "serialize-error": "^8.0.1",
34
- "uuid": "^8.3.2"
34
+ "uuid": "^9.0.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "child-process-promise": "^2.2.1",
38
- "eslint": "^7.13.0",
39
- "eslint-config-airbnb": "^18.2.1",
40
- "eslint-plugin-import": "^2.22.1",
41
- "eslint-plugin-jsx-a11y": "^6.4.1",
42
- "eslint-plugin-react": "^7.21.5",
43
- "eslint-plugin-react-hooks": "^4.2.0",
44
- "mocha": "^9.0.1",
38
+ "eslint": "^8.25.0",
39
+ "eslint-config-airbnb-base": "^15.0.0",
40
+ "eslint-plugin-import": "^2.26.0",
41
+ "mocha": "^10.0.0",
45
42
  "nyc": "^15.1.0",
46
- "sinon": "^11.1.1"
43
+ "sinon": "^14.0.1"
47
44
  },
48
45
  "engines": {
49
- "node": ">=10"
46
+ "node": ">=12"
50
47
  }
51
48
  }
package/src/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const uuid = require('uuid');
2
2
  const utils = require('./modules/utils');
3
3
  const connection = require('./modules/connection');
4
+ const { ARNAVMQ_TRANSPORT_LOGGER_DEPRECATED } = require('./modules/warnings');
4
5
 
5
6
  /* eslint global-require: "off" */
6
7
  module.exports = (config) => {
@@ -46,7 +47,7 @@ module.exports = (config) => {
46
47
  };
47
48
 
48
49
  if (configuration.transport !== utils.emptyLogger) {
49
- process.emitWarning("The 'transport' configuration option is deprecated. Please use the 'logger' option instead.", 'DeprecationWarning');
50
+ utils.emitWarn(ARNAVMQ_TRANSPORT_LOGGER_DEPRECATED);
50
51
  }
51
52
 
52
53
  configuration.prefetch = parseInt(configuration.prefetch, 10) || 0;
@@ -2,6 +2,7 @@ const uuid = require('uuid');
2
2
  const pDefer = require('p-defer');
3
3
  const utils = require('./utils');
4
4
  const parsers = require('./message-parsers');
5
+ const { ARNAVMQ_MSG_TIMEOUT_DEPRECATED } = require('./warnings');
5
6
 
6
7
  const ERRORS = {
7
8
  TIMEOUT: 'Timeout reached',
@@ -173,13 +174,28 @@ class Producer {
173
174
  // reply to us if you receive this message!
174
175
  options.replyTo = this.amqpRPCQueues[queue].queue;
175
176
 
177
+ // convert timeout to amqp's expiration. It's message-level expiration.
178
+ // The message will be discarded from a queue once it’s been there longer than the given number of milliseconds
179
+ // This is needed to avoid the case when the message which is already expired from caller's point of view (via timeout)
180
+ // is still waiting in the queue and thus is about to be processed by the consumer.
181
+ // Unfortunately, we can do nothing if the message is already consumed and is being processed at the moment
182
+ // when the timeout appears.
183
+ if (options.timeout && options.timeout > 0) {
184
+ utils.emitWarn(ARNAVMQ_MSG_TIMEOUT_DEPRECATED);
185
+ options.expiration = options.timeout;
186
+ }
187
+ // set expiration if it isn't set yet
188
+ if (!options.expiration && this._connection.config.rpcTimeout > 0) {
189
+ options.expiration = this._connection.config.rpcTimeout;
190
+ }
191
+
176
192
  this.publishOrSendToQueue(queue, msg, options);
177
193
  // defered promise that will resolve when response is received
178
194
  const responsePromise = pDefer();
179
195
  this.amqpRPCQueues[queue][corrId] = responsePromise;
180
196
 
181
197
  // Using given timeout or default one
182
- const timeout = options.timeout || this._connection.config.rpcTimeout || 0;
198
+ const timeout = options.expiration || 0;
183
199
  if (timeout > 0) {
184
200
  this.prepareTimeoutRpc(queue, corrId, timeout);
185
201
  }
@@ -28,5 +28,21 @@ module.exports = {
28
28
  */
29
29
  timeoutPromise: (timer) => new Promise((resolve) => {
30
30
  setTimeout(resolve, timer);
31
- })
31
+ }),
32
+
33
+ /**
34
+ * A function that allows to emit warnings for a specified code. The idea is to
35
+ * limit a warning emission to one per a specific code.
36
+ * @param warning - {code: string, detail: string, message: string}
37
+ */
38
+ emitWarn: function emitWarn(warning) {
39
+ const { code, message, detail } = warning;
40
+ if (!emitWarn.warned) {
41
+ emitWarn.warned = {};
42
+ }
43
+ if (!emitWarn.warned[code]) {
44
+ emitWarn.warned[code] = true;
45
+ process.emitWarning(message, { code, detail });
46
+ }
47
+ }
32
48
  };
@@ -0,0 +1,17 @@
1
+ /**
2
+ * This script is kind of dictionary for possible different types of warning in the lib.
3
+ * Warning object is a value (string code is a key) and has the following structure:
4
+ * @type {code: string, detail: string, message: string}
5
+ */
6
+ module.exports = {
7
+ ARNAVMQ_MSG_TIMEOUT_DEPRECATED: {
8
+ code: 'ARNAVMQ_MSG_TIMEOUT_DEPRECATED',
9
+ message: 'using timeout option on message level is deprecated',
10
+ detail: 'Please use expiration instead'
11
+ },
12
+ ARNAVMQ_TRANSPORT_LOGGER_DEPRECATED: {
13
+ code: 'ARNAVMQ_TRANSPORT_LOGGER_DEPRECATED',
14
+ message: "The 'transport' configuration option is deprecated",
15
+ detail: "Please use the 'logger' option instead"
16
+ }
17
+ };