amqplib 0.10.8 → 1.0.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/.claude/commands/lint-disable.md +9 -0
- package/.claude/commands/lint-enable.md +12 -0
- package/.claude/settings.local.json +16 -0
- package/.github/workflows/publish.yml +1 -1
- package/.github/workflows/test.yml +1 -1
- package/CHANGELOG.md +34 -0
- package/CLAUDE.md +12 -0
- package/Makefile +5 -12
- package/bin/generate-defs.js +382 -369
- package/biome.json +51 -0
- package/callback_api.js +12 -8
- package/channel_api.js +5 -10
- package/examples/direct_reply_to_client.js +9 -5
- package/examples/direct_reply_to_server.js +8 -6
- package/examples/headers.js +13 -10
- package/examples/receive_generator.js +13 -20
- package/examples/send_generators.js +2 -9
- package/examples/ssl.js +2 -3
- package/examples/stream_queues/receive_stream.js +39 -36
- package/examples/stream_queues/send_stream.js +26 -29
- package/examples/tutorials/callback_api/emit_log.js +4 -3
- package/examples/tutorials/callback_api/emit_log_direct.js +5 -4
- package/examples/tutorials/callback_api/emit_log_topic.js +5 -4
- package/examples/tutorials/callback_api/new_task.js +5 -4
- package/examples/tutorials/callback_api/receive.js +16 -10
- package/examples/tutorials/callback_api/receive_logs.js +17 -11
- package/examples/tutorials/callback_api/receive_logs_direct.js +19 -13
- package/examples/tutorials/callback_api/receive_logs_topic.js +19 -14
- package/examples/tutorials/callback_api/rpc_client.js +20 -16
- package/examples/tutorials/callback_api/rpc_server.js +27 -21
- package/examples/tutorials/callback_api/send.js +4 -3
- package/examples/tutorials/callback_api/worker.js +18 -13
- package/examples/tutorials/emit_log.js +4 -6
- package/examples/tutorials/emit_log_direct.js +5 -7
- package/examples/tutorials/emit_log_topic.js +5 -7
- package/examples/tutorials/new_task.js +4 -6
- package/examples/tutorials/receive.js +8 -4
- package/examples/tutorials/receive_logs.js +10 -6
- package/examples/tutorials/receive_logs_direct.js +16 -10
- package/examples/tutorials/receive_logs_topic.js +16 -11
- package/examples/tutorials/rpc_client.js +17 -15
- package/examples/tutorials/rpc_server.js +9 -8
- package/examples/tutorials/send.js +4 -6
- package/examples/tutorials/worker.js +17 -14
- package/examples/waitForConfirms.js +2 -2
- package/lefthook.yml +14 -0
- package/lib/api_args.js +124 -149
- package/lib/bitset.js +28 -18
- package/lib/callback_model.js +134 -190
- package/lib/channel.js +175 -213
- package/lib/channel_model.js +41 -85
- package/lib/codec.js +257 -228
- package/lib/connect.js +74 -82
- package/lib/connection.js +204 -240
- package/lib/credentials.js +21 -29
- package/lib/defs.js +2559 -1927
- package/lib/error.js +5 -6
- package/lib/format.js +15 -33
- package/lib/frame.js +66 -66
- package/lib/heartbeat.js +17 -24
- package/lib/mux.js +32 -41
- package/package.json +8 -5
- package/test/bitset.test.js +65 -0
- package/test/callback_api.test.js +438 -0
- package/test/channel.test.js +592 -0
- package/test/channel_api.test.js +537 -0
- package/test/codec.test.js +172 -0
- package/test/connect.test.js +196 -0
- package/test/connection.test.js +330 -0
- package/test/data.test.js +33 -0
- package/test/frame.test.js +180 -0
- package/test/lib/data.js +249 -0
- package/test/lib/util.js +223 -0
- package/test/mux.test.js +215 -0
- package/test/bitset.js +0 -69
- package/test/callback_api.js +0 -379
- package/test/channel.js +0 -621
- package/test/channel_api.js +0 -606
- package/test/codec.js +0 -237
- package/test/connect.js +0 -197
- package/test/connection.js +0 -390
- package/test/data.js +0 -269
- package/test/frame.js +0 -191
- package/test/mux.js +0 -203
- package/test/util.js +0 -217
package/biome.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"formatter": {
|
|
3
|
+
"enabled": true,
|
|
4
|
+
"formatWithErrors": false,
|
|
5
|
+
"indentStyle": "space",
|
|
6
|
+
"indentWidth": 2,
|
|
7
|
+
"lineWidth": 140,
|
|
8
|
+
"lineEnding": "lf",
|
|
9
|
+
"attributePosition": "auto",
|
|
10
|
+
"includes": ["**/*.js", "!test"]
|
|
11
|
+
},
|
|
12
|
+
"linter": {
|
|
13
|
+
"enabled": true,
|
|
14
|
+
"rules": {
|
|
15
|
+
"complexity": {
|
|
16
|
+
"noCommaOperator": "error",
|
|
17
|
+
"noUselessCatch": "error",
|
|
18
|
+
"useArrowFunction": "error",
|
|
19
|
+
"useOptionalChain": "off",
|
|
20
|
+
"noArguments": "error",
|
|
21
|
+
"useLiteralKeys": "off"
|
|
22
|
+
},
|
|
23
|
+
"suspicious": {
|
|
24
|
+
"noRedundantUseStrict": "error",
|
|
25
|
+
"noAsyncPromiseExecutor": "off",
|
|
26
|
+
"noGlobalIsNan": "error",
|
|
27
|
+
"noGlobalIsFinite": "error",
|
|
28
|
+
"noPrototypeBuiltins": "error",
|
|
29
|
+
"noVar": "error",
|
|
30
|
+
"noExportsInTest": "off",
|
|
31
|
+
"useIterableCallbackReturn": "off"
|
|
32
|
+
},
|
|
33
|
+
"style": {
|
|
34
|
+
"useSingleVarDeclarator": "error"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"includes": ["**/*.js", "!lib/defs.js"]
|
|
38
|
+
},
|
|
39
|
+
"javascript": {
|
|
40
|
+
"formatter": {
|
|
41
|
+
"arrowParentheses": "always",
|
|
42
|
+
"bracketSameLine": false,
|
|
43
|
+
"bracketSpacing": true,
|
|
44
|
+
"jsxQuoteStyle": "double",
|
|
45
|
+
"quoteProperties": "asNeeded",
|
|
46
|
+
"quoteStyle": "single",
|
|
47
|
+
"semicolons": "always",
|
|
48
|
+
"trailingCommas": "all"
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
package/callback_api.js
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
const raw_connect = require('./lib/connect').connect;
|
|
2
|
+
const CallbackModel = require('./lib/callback_model').CallbackModel;
|
|
3
3
|
|
|
4
4
|
// Supports three shapes:
|
|
5
5
|
// connect(url, options, callback)
|
|
6
6
|
// connect(url, callback)
|
|
7
7
|
// connect(callback)
|
|
8
8
|
function connect(url, options, cb) {
|
|
9
|
-
if (typeof url === 'function')
|
|
10
|
-
cb = url
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
if (typeof url === 'function') {
|
|
10
|
+
cb = url;
|
|
11
|
+
url = false;
|
|
12
|
+
options = false;
|
|
13
|
+
} else if (typeof options === 'function') {
|
|
14
|
+
cb = options;
|
|
15
|
+
options = false;
|
|
16
|
+
}
|
|
13
17
|
|
|
14
|
-
raw_connect(url, options,
|
|
18
|
+
raw_connect(url, options, (err, c) => {
|
|
15
19
|
if (err === null) cb(null, new CallbackModel(c));
|
|
16
20
|
else cb(err);
|
|
17
21
|
});
|
|
18
|
-
}
|
|
22
|
+
}
|
|
19
23
|
|
|
20
24
|
module.exports.connect = connect;
|
|
21
25
|
module.exports.credentials = require('./lib/credentials');
|
package/channel_api.js
CHANGED
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
const raw_connect = require('./lib/connect').connect;
|
|
2
|
+
const ChannelModel = require('./lib/channel_model').ChannelModel;
|
|
3
|
+
const promisify = require('node:util').promisify;
|
|
4
4
|
|
|
5
5
|
function connect(url, connOptions) {
|
|
6
|
-
return promisify(
|
|
7
|
-
|
|
8
|
-
})()
|
|
9
|
-
.then(function(conn) {
|
|
10
|
-
return new ChannelModel(conn);
|
|
11
|
-
});
|
|
12
|
-
};
|
|
6
|
+
return promisify((cb) => raw_connect(url, connOptions, cb))().then((conn) => new ChannelModel(conn));
|
|
7
|
+
}
|
|
13
8
|
|
|
14
9
|
module.exports.connect = connect;
|
|
15
10
|
module.exports.credentials = require('./lib/credentials');
|
|
@@ -8,11 +8,15 @@ const queue = 'rpc_queue';
|
|
|
8
8
|
const connection = await amqp.connect();
|
|
9
9
|
const channel = await connection.createChannel();
|
|
10
10
|
|
|
11
|
-
await channel.consume(
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
await channel.consume(
|
|
12
|
+
'amq.rabbitmq.reply-to',
|
|
13
|
+
async (message) => {
|
|
14
|
+
console.log(message.content.toString());
|
|
15
|
+
await channel.close();
|
|
16
|
+
await connection.close();
|
|
17
|
+
},
|
|
18
|
+
{ noAck: true },
|
|
19
|
+
);
|
|
16
20
|
|
|
17
21
|
await channel.assertQueue(queue, { durable: false });
|
|
18
22
|
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
const amqp = require('../');
|
|
4
|
-
const { v4: uuid } = require('uuid');
|
|
5
4
|
|
|
6
5
|
const queue = 'rpc_queue';
|
|
7
6
|
|
|
@@ -15,11 +14,14 @@ const queue = 'rpc_queue';
|
|
|
15
14
|
});
|
|
16
15
|
|
|
17
16
|
await channel.assertQueue(queue, { durable: false });
|
|
18
|
-
await channel.consume(
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
await channel.consume(
|
|
18
|
+
queue,
|
|
19
|
+
(message) => {
|
|
20
|
+
console.log(message.content.toString());
|
|
21
|
+
channel.sendToQueue(message.properties.replyTo, Buffer.from(' [.] pong'));
|
|
22
|
+
},
|
|
23
|
+
{ noAck: true },
|
|
24
|
+
);
|
|
22
25
|
|
|
23
26
|
console.log(' [x] To exit press CTRL+C.');
|
|
24
|
-
|
|
25
27
|
})();
|
package/examples/headers.js
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
const amqp = require('../');
|
|
4
4
|
|
|
5
5
|
(async () => {
|
|
6
|
-
|
|
7
6
|
const connection = await amqp.connect();
|
|
8
7
|
const channel = await connection.createChannel();
|
|
9
8
|
|
|
@@ -24,17 +23,21 @@ const amqp = require('../');
|
|
|
24
23
|
// matched go in subsequent fields.
|
|
25
24
|
await channel.bindQueue(queue, exchange, '', {
|
|
26
25
|
'x-match': 'any',
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
foo: 'bar',
|
|
27
|
+
baz: 'boo',
|
|
29
28
|
});
|
|
30
29
|
|
|
31
|
-
await channel.consume(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
30
|
+
await channel.consume(
|
|
31
|
+
queue,
|
|
32
|
+
(message) => {
|
|
33
|
+
console.log(message.content.toString());
|
|
34
|
+
},
|
|
35
|
+
{ noAck: true },
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
channel.publish(exchange, '', Buffer.from('hello'), { headers: { baz: 'boo' } });
|
|
39
|
+
channel.publish(exchange, '', Buffer.from('hello'), { headers: { foo: 'bar' } });
|
|
40
|
+
channel.publish(exchange, '', Buffer.from('lost'), { headers: { meh: 'nah' } });
|
|
38
41
|
|
|
39
42
|
console.log(' [x] To exit press CTRL+C.');
|
|
40
43
|
})();
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
'use strict';
|
|
4
2
|
const co = require('co');
|
|
5
3
|
const amqp = require('amqplib');
|
|
6
|
-
const readline = require('readline');
|
|
4
|
+
const readline = require('node:readline');
|
|
7
5
|
|
|
8
6
|
co(function* () {
|
|
9
7
|
const myConsumer = (msg) => {
|
|
@@ -12,28 +10,23 @@ co(function* () {
|
|
|
12
10
|
}
|
|
13
11
|
};
|
|
14
12
|
const conn = yield amqp.connect('amqp://localhost');
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
catch (e) {
|
|
27
|
-
throw e;
|
|
28
|
-
}
|
|
29
|
-
}).catch(err => {
|
|
13
|
+
// create a message to consume
|
|
14
|
+
const q = 'hello';
|
|
15
|
+
const msg = 'Hello World!';
|
|
16
|
+
const channel = yield conn.createChannel();
|
|
17
|
+
yield channel.assertQueue(q);
|
|
18
|
+
channel.sendToQueue(q, Buffer.from(msg));
|
|
19
|
+
console.log(" [x] Sent '%s'", msg);
|
|
20
|
+
// consume the message
|
|
21
|
+
yield channel.consume(q, myConsumer, { noAck: true });
|
|
22
|
+
}).catch((err) => {
|
|
30
23
|
console.warn('Error:', err);
|
|
31
24
|
});
|
|
32
25
|
|
|
33
26
|
const rl = readline.createInterface({
|
|
34
27
|
input: process.stdin,
|
|
35
|
-
output: process.stdout
|
|
28
|
+
output: process.stdout,
|
|
36
29
|
});
|
|
37
30
|
|
|
38
31
|
// pend until message is consumed
|
|
39
|
-
rl.question('newline to exit', () => process.exit());
|
|
32
|
+
rl.question('newline to exit', () => process.exit());
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
'use strict';
|
|
4
|
-
|
|
5
3
|
// NB this requires the module 'co':
|
|
6
4
|
// npm install co
|
|
7
5
|
const co = require('co');
|
|
@@ -29,14 +27,9 @@ co(function* () {
|
|
|
29
27
|
console.log(" [x] Sent '%s'", msg);
|
|
30
28
|
|
|
31
29
|
channel.close();
|
|
32
|
-
}
|
|
33
|
-
catch (e) {
|
|
34
|
-
throw e;
|
|
35
|
-
}
|
|
36
|
-
finally {
|
|
30
|
+
} finally {
|
|
37
31
|
conn.close();
|
|
38
32
|
}
|
|
39
|
-
|
|
40
|
-
}).catch(err => {
|
|
33
|
+
}).catch((err) => {
|
|
41
34
|
console.warn('Error:', err);
|
|
42
35
|
});
|
package/examples/ssl.js
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
// openssl s_client -connect localhost:5671
|
|
20
20
|
|
|
21
21
|
const amqp = require('../');
|
|
22
|
-
const fs = require('fs');
|
|
22
|
+
const fs = require('node:fs');
|
|
23
23
|
|
|
24
24
|
// Assemble the SSL options; for verification we need at least
|
|
25
25
|
// * a certificate to present to the server ('cert', in PEM format)
|
|
@@ -40,7 +40,7 @@ const opts = {
|
|
|
40
40
|
// cert and key or
|
|
41
41
|
// pfx: fs.readFileSync('../etc/client/keycert.p12'),
|
|
42
42
|
passphrase: 'MySecretPassword',
|
|
43
|
-
ca: [fs.readFileSync('../etc/testca/cacert.pem')]
|
|
43
|
+
ca: [fs.readFileSync('../etc/testca/cacert.pem')],
|
|
44
44
|
};
|
|
45
45
|
|
|
46
46
|
// Options for just confidentiality. This requires RabbitMQ's SSL
|
|
@@ -67,4 +67,3 @@ const opts = {
|
|
|
67
67
|
|
|
68
68
|
console.log(' [x] To exit press CTRL+C.');
|
|
69
69
|
})();
|
|
70
|
-
|
|
@@ -1,34 +1,37 @@
|
|
|
1
1
|
const amqp = require('amqplib');
|
|
2
2
|
|
|
3
3
|
(async () => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
4
|
+
try {
|
|
5
|
+
const connection = await amqp.connect('amqp://localhost');
|
|
6
|
+
process.once('SIGINT', connection.close);
|
|
7
|
+
|
|
8
|
+
const channel = await connection.createChannel();
|
|
9
|
+
const queue = 'my_first_stream';
|
|
10
|
+
|
|
11
|
+
// Define the queue stream
|
|
12
|
+
// Mandatory: exclusive: false, durable: true autoDelete: false
|
|
13
|
+
await channel.assertQueue(queue, {
|
|
14
|
+
exclusive: false,
|
|
15
|
+
durable: true,
|
|
16
|
+
autoDelete: false,
|
|
17
|
+
arguments: {
|
|
18
|
+
'x-queue-type': 'stream', // Mandatory to define stream queue
|
|
19
|
+
'x-max-length-bytes': 2_000_000_000, // Set the queue retention to 2GB else the stream doesn't have any limit
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
channel.qos(100); // This is mandatory
|
|
24
|
+
|
|
25
|
+
channel.consume(
|
|
26
|
+
queue,
|
|
27
|
+
(msg) => {
|
|
28
|
+
console.log(" [x] Received '%s'", msg.content.toString());
|
|
29
|
+
channel.ack(msg); // Mandatory
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
noAck: false,
|
|
33
|
+
arguments: {
|
|
34
|
+
/*
|
|
32
35
|
Here you can specify the offset: : first, last, next, offset, timestamp and interval, i.e.
|
|
33
36
|
|
|
34
37
|
'x-stream-offset': 'first'
|
|
@@ -42,14 +45,14 @@ const amqp = require('amqplib');
|
|
|
42
45
|
The interval units can be Y, M, D, h, m, s
|
|
43
46
|
|
|
44
47
|
*/
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
+
'x-stream-offset': 'first',
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
);
|
|
48
52
|
|
|
49
|
-
|
|
50
|
-
|
|
53
|
+
console.log(' [*] Waiting for messages. To exit press CTRL+C');
|
|
54
|
+
} catch (e) {
|
|
51
55
|
// Catch and display any errors in the console
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
56
|
+
console.log(e);
|
|
57
|
+
}
|
|
55
58
|
})();
|
|
@@ -1,38 +1,35 @@
|
|
|
1
1
|
const amqp = require('amqplib');
|
|
2
2
|
|
|
3
|
-
|
|
4
3
|
(async () => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
try {
|
|
5
|
+
const connection = await amqp.connect('amqp://localhost');
|
|
6
|
+
process.once('SIGINT', connection.close);
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
const channel = await connection.createChannel();
|
|
9
|
+
const queue = 'my_first_stream';
|
|
10
|
+
const msg = `Hello World! ${Date.now()}`;
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
12
|
+
// Define the queue stream
|
|
13
|
+
// Mandatory: exclusive: false, durable: true autoDelete: false
|
|
14
|
+
await channel.assertQueue(queue, {
|
|
15
|
+
exclusive: false,
|
|
16
|
+
durable: true,
|
|
17
|
+
autoDelete: false,
|
|
18
|
+
arguments: {
|
|
19
|
+
'x-queue-type': 'stream', // Mandatory to define stream queue
|
|
20
|
+
'x-max-length-bytes': 2_000_000_000, // Set the queue retention to 2GB else the stream doesn't have any limit
|
|
21
|
+
},
|
|
22
|
+
});
|
|
24
23
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
// Send the message to the stream queue
|
|
25
|
+
await channel.sendToQueue(queue, Buffer.from(msg));
|
|
26
|
+
console.log(" [x] Sent '%s'", msg);
|
|
27
|
+
await channel.close();
|
|
29
28
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
// Close connection
|
|
30
|
+
connection.close();
|
|
31
|
+
} catch (e) {
|
|
33
32
|
// Catch and display any errors in the console
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
33
|
+
console.log(e);
|
|
34
|
+
}
|
|
37
35
|
})();
|
|
38
|
-
|
|
@@ -4,7 +4,7 @@ const amqp = require('amqplib/callback_api');
|
|
|
4
4
|
|
|
5
5
|
const exchange = 'direct_logs';
|
|
6
6
|
const args = process.argv.slice(2);
|
|
7
|
-
const routingKey =
|
|
7
|
+
const routingKey = args.length > 0 ? args[0] : 'info';
|
|
8
8
|
const text = args.slice(1).join(' ') || 'Hello World!';
|
|
9
9
|
|
|
10
10
|
amqp.connect((err, connection) => {
|
|
@@ -24,7 +24,8 @@ amqp.connect((err, connection) => {
|
|
|
24
24
|
|
|
25
25
|
function bail(err, connection) {
|
|
26
26
|
console.error(err);
|
|
27
|
-
if (connection)
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
if (connection)
|
|
28
|
+
connection.close(() => {
|
|
29
|
+
process.exit(1);
|
|
30
|
+
});
|
|
30
31
|
}
|
|
@@ -4,7 +4,7 @@ const amqp = require('amqplib/callback_api');
|
|
|
4
4
|
|
|
5
5
|
const exchange = 'topic_logs';
|
|
6
6
|
const args = process.argv.slice(2);
|
|
7
|
-
const routingKey =
|
|
7
|
+
const routingKey = args.length > 0 ? args[0] : 'info';
|
|
8
8
|
const text = args.slice(1).join(' ') || 'Hello World!';
|
|
9
9
|
|
|
10
10
|
amqp.connect((err, connection) => {
|
|
@@ -24,7 +24,8 @@ amqp.connect((err, connection) => {
|
|
|
24
24
|
|
|
25
25
|
function bail(err, connection) {
|
|
26
26
|
console.error(err);
|
|
27
|
-
if (connection)
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
if (connection)
|
|
28
|
+
connection.close(() => {
|
|
29
|
+
process.exit(1);
|
|
30
|
+
});
|
|
30
31
|
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
const amqp = require('amqplib/callback_api');
|
|
4
4
|
|
|
5
5
|
const queue = 'task_queue';
|
|
6
|
-
const text = process.argv.slice(2).join(' ') ||
|
|
6
|
+
const text = process.argv.slice(2).join(' ') || 'Hello World!';
|
|
7
7
|
|
|
8
8
|
amqp.connect((err, connection) => {
|
|
9
9
|
if (err) return bail(err);
|
|
@@ -22,7 +22,8 @@ amqp.connect((err, connection) => {
|
|
|
22
22
|
|
|
23
23
|
function bail(err, connection) {
|
|
24
24
|
console.error(err);
|
|
25
|
-
if (connection)
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
if (connection)
|
|
26
|
+
connection.close(() => {
|
|
27
|
+
process.exit(1);
|
|
28
|
+
});
|
|
28
29
|
}
|
|
@@ -17,20 +17,26 @@ amqp.connect((err, connection) => {
|
|
|
17
17
|
|
|
18
18
|
channel.assertQueue(queue, { durable: false }, (err) => {
|
|
19
19
|
if (err) return bail(err, connection);
|
|
20
|
-
channel.consume(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
20
|
+
channel.consume(
|
|
21
|
+
queue,
|
|
22
|
+
(message) => {
|
|
23
|
+
if (message) console.log(" [x] Received '%s'", message.content.toString());
|
|
24
|
+
else console.warn(' [x] Consumer cancelled');
|
|
25
|
+
},
|
|
26
|
+
{ noAck: true },
|
|
27
|
+
(err) => {
|
|
28
|
+
if (err) return bail(err, connection);
|
|
29
|
+
console.log(' [*] Waiting for logs. To exit press CTRL+C.');
|
|
30
|
+
},
|
|
31
|
+
);
|
|
27
32
|
});
|
|
28
33
|
});
|
|
29
34
|
});
|
|
30
35
|
|
|
31
36
|
function bail(err, connection) {
|
|
32
37
|
console.error(err);
|
|
33
|
-
if (connection)
|
|
34
|
-
|
|
35
|
-
|
|
38
|
+
if (connection)
|
|
39
|
+
connection.close(() => {
|
|
40
|
+
process.exit(1);
|
|
41
|
+
});
|
|
36
42
|
}
|
|
@@ -15,19 +15,24 @@ amqp.connect((err, connection) => {
|
|
|
15
15
|
});
|
|
16
16
|
});
|
|
17
17
|
|
|
18
|
-
channel.assertExchange(exchange, 'fanout', { durable: false }, (err, { queue }) => {
|
|
18
|
+
channel.assertExchange(exchange, 'fanout', { durable: false }, (err, { queue: _queue }) => {
|
|
19
19
|
if (err) return bail(err, connection);
|
|
20
20
|
channel.assertQueue('', { exclusive: true }, (err, { queue }) => {
|
|
21
21
|
if (err) return bail(err, connection);
|
|
22
22
|
channel.bindQueue(queue, exchange, '', {}, (err) => {
|
|
23
23
|
if (err) return bail(err, connection);
|
|
24
|
-
channel.consume(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
channel.consume(
|
|
25
|
+
queue,
|
|
26
|
+
(message) => {
|
|
27
|
+
if (message) console.log(" [x] '%s'", message.content.toString());
|
|
28
|
+
else console.warn(' [x] Consumer cancelled');
|
|
29
|
+
},
|
|
30
|
+
{ noAck: true },
|
|
31
|
+
(err) => {
|
|
32
|
+
if (err) return bail(err, connection);
|
|
33
|
+
console.log(' [*] Waiting for logs. To exit press CTRL+C.');
|
|
34
|
+
},
|
|
35
|
+
);
|
|
31
36
|
});
|
|
32
37
|
});
|
|
33
38
|
});
|
|
@@ -36,7 +41,8 @@ amqp.connect((err, connection) => {
|
|
|
36
41
|
|
|
37
42
|
function bail(err, connection) {
|
|
38
43
|
console.error(err);
|
|
39
|
-
if (connection)
|
|
40
|
-
|
|
41
|
-
|
|
44
|
+
if (connection)
|
|
45
|
+
connection.close(() => {
|
|
46
|
+
process.exit(1);
|
|
47
|
+
});
|
|
42
48
|
}
|