amqplib 0.4.1 → 0.5.2

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.
Files changed (52) hide show
  1. package/CHANGELOG.md +51 -0
  2. package/Makefile +1 -1
  3. package/README.md +26 -17
  4. package/callback_api.js +1 -0
  5. package/channel_api.js +7 -6
  6. package/examples/headers.js +2 -2
  7. package/examples/receive_generator.js +39 -0
  8. package/examples/send_generators.js +42 -0
  9. package/examples/ssl.js +1 -1
  10. package/examples/tutorials/README.md +1 -1
  11. package/examples/tutorials/callback_api/emit_log.js +1 -1
  12. package/examples/tutorials/callback_api/emit_log_direct.js +1 -1
  13. package/examples/tutorials/callback_api/emit_log_topic.js +1 -1
  14. package/examples/tutorials/callback_api/new_task.js +1 -1
  15. package/examples/tutorials/callback_api/rpc_client.js +1 -1
  16. package/examples/tutorials/callback_api/rpc_server.js +1 -1
  17. package/examples/tutorials/callback_api/send.js +1 -1
  18. package/examples/tutorials/emit_log.js +4 -5
  19. package/examples/tutorials/emit_log_direct.js +4 -5
  20. package/examples/tutorials/emit_log_topic.js +4 -5
  21. package/examples/tutorials/new_task.js +5 -6
  22. package/examples/tutorials/package.json +2 -3
  23. package/examples/tutorials/receive.js +4 -4
  24. package/examples/tutorials/receive_logs.js +1 -1
  25. package/examples/tutorials/receive_logs_direct.js +2 -2
  26. package/examples/tutorials/receive_logs_topic.js +6 -6
  27. package/examples/tutorials/rpc_client.js +25 -26
  28. package/examples/tutorials/rpc_server.js +2 -2
  29. package/examples/tutorials/send.js +10 -6
  30. package/examples/tutorials/worker.js +1 -1
  31. package/examples/waitForConfirms.js +2 -2
  32. package/lib/callback_model.js +13 -12
  33. package/lib/channel.js +2 -2
  34. package/lib/channel_model.js +58 -66
  35. package/lib/codec.js +64 -38
  36. package/lib/connect.js +33 -12
  37. package/lib/connection.js +7 -6
  38. package/lib/credentials.js +6 -3
  39. package/lib/defs.js +75 -75
  40. package/lib/frame.js +2 -1
  41. package/lib/heartbeat.js +1 -1
  42. package/package.json +4 -3
  43. package/test/callback_api.js +6 -5
  44. package/test/channel.js +52 -46
  45. package/test/channel_api.js +125 -136
  46. package/test/codec.js +48 -9
  47. package/test/connect.js +63 -3
  48. package/test/connection.js +10 -9
  49. package/test/data.js +32 -9
  50. package/test/frame.js +4 -4
  51. package/test/util.js +33 -19
  52. package/.npmignore +0 -9
package/test/connect.js CHANGED
@@ -1,21 +1,65 @@
1
1
  'use strict';
2
2
 
3
3
  var connect = require('../lib/connect').connect;
4
+ var Buffer = require('safe-buffer').Buffer
5
+ var credentialsFromUrl = require('../lib/connect').credentialsFromUrl;
4
6
  var assert = require('assert');
5
7
  var util = require('./util');
6
8
  var net = require('net');
7
9
  var fail = util.fail, succeed = util.succeed,
8
- kCallback = util.kCallback;
10
+ kCallback = util.kCallback,
11
+ succeedIfAttributeEquals = util.succeedIfAttributeEquals;
12
+ var format = require('util').format;
9
13
 
10
14
  var URL = process.env.URL || 'amqp://localhost';
11
15
 
16
+ suite("Credentials", function() {
17
+
18
+ function checkCreds(creds, user, pass, done) {
19
+ if (creds.mechanism != 'PLAIN') {
20
+ return done('expected mechanism PLAIN');
21
+ }
22
+ if (creds.username != user || creds.password != pass) {
23
+ return done(format("expected '%s', '%s'; got '%s', '%s'",
24
+ user, pass, creds.username, creds.password));
25
+ }
26
+ done();
27
+ }
28
+
29
+ test("no creds", function(done) {
30
+ var parts = {auth: ''};
31
+ var creds = credentialsFromUrl(parts);
32
+ checkCreds(creds, 'guest', 'guest', done);
33
+ });
34
+ test("usual user:pass", function(done) {
35
+ var parts = {auth: 'user:pass'};
36
+ var creds = credentialsFromUrl(parts);
37
+ checkCreds(creds, 'user', 'pass', done);
38
+ });
39
+ test("missing user", function(done) {
40
+ var parts = {auth: ':password'};
41
+ var creds = credentialsFromUrl(parts);
42
+ checkCreds(creds, '', 'password', done);
43
+ });
44
+ test("missing password", function(done) {
45
+ var parts = {auth: 'username'};
46
+ var creds = credentialsFromUrl(parts);
47
+ checkCreds(creds, 'username', '', done);
48
+ });
49
+ test("colon in password", function(done) {
50
+ var parts = {auth: 'username:pass:word'};
51
+ var creds = credentialsFromUrl(parts);
52
+ checkCreds(creds, 'username', 'pass:word', done);
53
+ });
54
+ });
55
+
12
56
  suite("Connect API", function() {
13
57
 
14
58
  test("Connection refused", function(done) {
15
59
  connect('amqp://localhost:23450', {},
16
60
  kCallback(fail(done), succeed(done)));
17
61
  });
18
-
62
+
19
63
  // %% this ought to fail the promise, rather than throwing an error
20
64
  test("bad URL", function() {
21
65
  assert.throws(function() {
@@ -33,6 +77,22 @@ suite("Connect API", function() {
33
77
  connect(u, {}, kCallback(fail(done), succeed(done)));
34
78
  });
35
79
 
80
+ test("using custom heartbeat option", function(done) {
81
+ var url = require('url');
82
+ var parts = url.parse(URL, true);
83
+ var config = parts.query || {};
84
+ config.heartbeat = 20;
85
+ connect(config, {}, kCallback(succeedIfAttributeEquals('heartbeat', 20, done), fail(done)));
86
+ });
87
+
88
+ test("wrongly typed heartbeat option", function(done) {
89
+ var url = require('url');
90
+ var parts = url.parse(URL, true);
91
+ var config = parts.query || {};
92
+ config.heartbeat = 'NOT A NUMBER';
93
+ connect(config, {}, kCallback(fail(done), succeed(done)));
94
+ });
95
+
36
96
  test("using plain credentials", function(done) {
37
97
  var url = require('url');
38
98
  var parts = url.parse(URL, true);
@@ -48,7 +108,7 @@ suite("Connect API", function() {
48
108
  test("using unsupported mechanism", function(done) {
49
109
  var creds = {
50
110
  mechanism: 'UNSUPPORTED',
51
- response: function() { return new Buffer(''); }
111
+ response: function() { return Buffer.from(''); }
52
112
  };
53
113
  connect(URL, {credentials: creds},
54
114
  kCallback(fail(done), succeed(done)));
@@ -2,6 +2,7 @@
2
2
 
3
3
  var assert = require('assert');
4
4
  var defs = require('../lib/defs');
5
+ var Buffer = require('safe-buffer').Buffer;
5
6
  var Connection = require('../lib/connection').Connection;
6
7
  var HEARTBEAT = require('../lib/frame').HEARTBEAT;
7
8
  var HB_BUF = require('../lib/frame').HEARTBEAT_BUF;
@@ -16,7 +17,7 @@ var OPEN_OPTS = {
16
17
  // start-ok
17
18
  'clientProperties': {},
18
19
  'mechanism': 'PLAIN',
19
- 'response': new Buffer(['', 'guest', 'guest'].join(String.fromCharCode(0))),
20
+ 'response': Buffer.from(['', 'guest', 'guest'].join(String.fromCharCode(0))),
20
21
  'locale': 'en_US',
21
22
 
22
23
  // tune-ok
@@ -37,8 +38,8 @@ function happy_open(send, await) {
37
38
  {versionMajor: 0,
38
39
  versionMinor: 9,
39
40
  serverProperties: {},
40
- mechanisms: new Buffer('PLAIN'),
41
- locales: new Buffer('en_US')});
41
+ mechanisms: Buffer.from('PLAIN'),
42
+ locales: Buffer.from('en_US')});
42
43
  return await(defs.ConnectionStartOk)()
43
44
  .then(function(f) {
44
45
  send(defs.ConnectionTune,
@@ -65,7 +66,7 @@ function connectionTest(client, server) {
65
66
 
66
67
  // NB only not a race here because the writes are synchronous
67
68
  var protocolHeader = pair.server.read(8);
68
- assert.deepEqual(new Buffer("AMQP" + String.fromCharCode(0,0,9,1)),
69
+ assert.deepEqual(Buffer.from("AMQP" + String.fromCharCode(0,0,9,1)),
69
70
  protocolHeader);
70
71
 
71
72
  var s = util.runServer(pair.server, function(send, await) {
@@ -92,7 +93,7 @@ suite("Connection errors", function() {
92
93
  var ss = util.socketPair();
93
94
  var conn = new (require('../lib/connection').Connection)(ss.client);
94
95
  ss.server.on('readable', function() {
95
- ss.server.write(new Buffer([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
96
+ ss.server.write(Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
96
97
  });
97
98
  conn.open({}, kCallback(fail(done), succeed(done)));
98
99
  });
@@ -132,8 +133,8 @@ test("unexpected socket close", connectionTest(
132
133
  {versionMajor: 0,
133
134
  versionMinor: 9,
134
135
  serverProperties: {},
135
- mechanisms: new Buffer('PLAIN'),
136
- locales: new Buffer('en_US')});
136
+ mechanisms: Buffer.from('PLAIN'),
137
+ locales: Buffer.from('en_US')});
137
138
  return await(defs.ConnectionStartOk)()
138
139
  .then(function() {
139
140
  socket.end();
@@ -156,7 +157,7 @@ test("wrong frame on channel 0", connectionTest(
156
157
  // there's actually nothing that would plausibly be sent to a
157
158
  // just opened connection, so this is violating more than one
158
159
  // rule. Nonetheless.
159
- send(defs.ChannelOpenOk, {channelId: new Buffer('')}, 0);
160
+ send(defs.ChannelOpenOk, {channelId: Buffer.from('')}, 0);
160
161
  })
161
162
  .then(await(defs.ConnectionClose))
162
163
  .then(function(close) {
@@ -175,7 +176,7 @@ test("unopened channel", connectionTest(
175
176
  // there's actually nothing that would plausibly be sent to a
176
177
  // just opened connection, so this is violating more than one
177
178
  // rule. Nonetheless.
178
- send(defs.ChannelOpenOk, {channelId: new Buffer('')}, 3);
179
+ send(defs.ChannelOpenOk, {channelId: Buffer.from('')}, 3);
179
180
  })
180
181
  .then(await(defs.ConnectionClose))
181
182
  .then(function(close) {
package/test/data.js CHANGED
@@ -3,7 +3,7 @@
3
3
  'use strict';
4
4
 
5
5
  var C = require('claire');
6
-
6
+ var Buffer = require('safe-buffer').Buffer;
7
7
  var forAll = C.forAll;
8
8
  var arb = C.data;
9
9
  var transform = C.transform;
@@ -18,7 +18,7 @@ var Undefined = C.Undefined;
18
18
 
19
19
  // Stub these out so we can use outside tests
20
20
  // if (!suite) var suite = function() {}
21
- // if (!test) var test = function() {}
21
+ // if (!test) var test = function() {}
22
22
 
23
23
  // These aren't exported in claire/index. so I could have to reproduce
24
24
  // them I guess.
@@ -36,7 +36,7 @@ function rangeInt(name, a, b) {
36
36
  }
37
37
 
38
38
  function toFloat32(i) {
39
- var b = new Buffer(4);
39
+ var b = Buffer.alloc(4);
40
40
  b.writeFloatBE(i, 0);
41
41
  return b.readFloatBE(0);
42
42
  }
@@ -53,6 +53,12 @@ function floatChooser(maxExp) {
53
53
  }
54
54
  }
55
55
 
56
+ function explicitType(t, underlying) {
57
+ return label(t, transform(function(n) {
58
+ return {'!': t, value: n};
59
+ }, underlying));
60
+ }
61
+
56
62
  // FIXME null, byte array, others?
57
63
 
58
64
  var Octet = rangeInt('octet', 0, 255);
@@ -63,7 +69,7 @@ var ShortStr = label('shortstr',
63
69
 
64
70
  var LongStr = label('longstr',
65
71
  transform(
66
- function(bytes) { return new Buffer(bytes); },
72
+ function(bytes) { return Buffer.from(bytes); },
67
73
  repeat(Octet)));
68
74
 
69
75
  var UShort = rangeInt('short-uint', 0, 0xffff);
@@ -85,12 +91,27 @@ var Decimal = label('decimal', transform(
85
91
  return {'!': 'decimal', value: {places: args[1], digits: args[0]}};
86
92
  }, sequence(arb.UInt, Octet)));
87
93
 
94
+ // Signed 8 bit int
95
+ var Byte = rangeInt('byte', -128, 127);
96
+
97
+ // Explicitly typed values
98
+ var ExByte = explicitType('byte', Byte);
99
+ var ExInt8 = explicitType('int8', Byte);
100
+ var ExShort = explicitType('short', Short);
101
+ var ExInt16 = explicitType('int16', Short);
102
+ var ExInt = explicitType('int', Long);
103
+ var ExInt32 = explicitType('int32', Long);
104
+ var ExLong = explicitType('long', LongLong);
105
+ var ExInt64 = explicitType('int64', LongLong);
106
+
88
107
  var FieldArray = label('field-array', recursive(function() {
89
108
  return arb.Array(
90
109
  arb.Null,
91
- LongStr, ShortStr, Octet,
92
- UShort, ULong, ULongLong,
93
- Short, Long, LongLong,
110
+ LongStr, ShortStr,
111
+ Octet, UShort, ULong, ULongLong,
112
+ Byte, Short, Long, LongLong,
113
+ ExByte, ExInt8, ExShort, ExInt16,
114
+ ExInt, ExInt32, ExLong, ExInt64,
94
115
  Bit, Float, Double, FieldTable, FieldArray)
95
116
  }));
96
117
 
@@ -100,7 +121,9 @@ var FieldTable = label('table', recursive(function() {
100
121
  arb.Null,
101
122
  LongStr, ShortStr, Octet,
102
123
  UShort, ULong, ULongLong,
103
- Short, Long, LongLong,
124
+ Byte, Short, Long, LongLong,
125
+ ExByte, ExInt8, ExShort, ExInt16,
126
+ ExInt, ExInt32, ExLong, ExInt64,
104
127
  Bit, Float, Double, FieldArray, FieldTable))
105
128
  }));
106
129
 
@@ -184,7 +207,7 @@ function method(info) {
184
207
  function properties(info) {
185
208
  var types = info.args.map(argtype);
186
209
  types.unshift(ULongLong); // size
187
- var domain = sequence.apply(null, types);
210
+ var domain = sequence.apply(null, types);
188
211
  var names = info.args.map(name);
189
212
  return label(info.name, transform(function(fieldVals) {
190
213
  return {id: info.id,
package/test/frame.js CHANGED
@@ -3,7 +3,7 @@
3
3
  var assert = require('assert');
4
4
  var succeed = require('./util').succeed;
5
5
  var fail = require('./util').fail;
6
-
6
+ var Buffer = require('safe-buffer').Buffer;
7
7
  var connection = require('../lib/connection');
8
8
  var Frames = connection.Connection;
9
9
  var HEARTBEAT = require('../lib/frame').HEARTBEAT;
@@ -21,7 +21,7 @@ function inputs() {
21
21
  return new PassThrough({objectMode: true});
22
22
  }
23
23
 
24
- var HB = new Buffer([defs.constants.FRAME_HEARTBEAT,
24
+ var HB = Buffer.from([defs.constants.FRAME_HEARTBEAT,
25
25
  0, 0, // channel 0
26
26
  0, 0, 0, 0, // zero size
27
27
  defs.constants.FRAME_END]);
@@ -51,7 +51,7 @@ suite("Explicit parsing", function() {
51
51
  var input = inputs();
52
52
  var frames = new Frames(input);
53
53
  frames.frameMax = 5; //for the max frame test
54
- input.write(new Buffer(bytes));
54
+ input.write(Buffer.from(bytes));
55
55
  frames.step(function(err, frame) {
56
56
  if (err != null) done();
57
57
  else done(new Error('Was a bogus frame!'));
@@ -166,7 +166,7 @@ var Content = transform(function(args) {
166
166
  return {
167
167
  method: args[0].fields,
168
168
  header: args[1].fields,
169
- body: new Buffer(args[2])
169
+ body: Buffer.from(args[2])
170
170
  }
171
171
  }, sequence(amqp.methods['BasicDeliver'],
172
172
  amqp.properties['BasicProperties'], Body));
package/test/util.js CHANGED
@@ -1,11 +1,11 @@
1
1
  'use strict';
2
2
 
3
+ var Promise = require('bluebird');
3
4
  var crypto = require('crypto');
4
5
  var Connection = require('../lib/connection').Connection;
5
6
  var PassThrough =
6
7
  require('stream').PassThrough ||
7
8
  require('readable-stream/passthrough');
8
- var defer = require('when').defer;
9
9
  var defs = require('../lib/defs');
10
10
  var assert = require('assert');
11
11
 
@@ -78,24 +78,24 @@ function runServer(socket, run) {
78
78
 
79
79
  function await(method) {
80
80
  return function() {
81
- var d = defer();
82
- if (method) {
83
- frames.step(function(e, f) {
84
- if (e !== null) return d.reject(e);
85
- if (f.id === method)
86
- d.resolve(f);
87
- else
88
- d.reject(new Error("Expected method: " + method +
89
- ", got " + f.id));
90
- });
91
- }
92
- else {
93
- frames.step(function(e, f) {
94
- if (e !== null) return d.reject(e);
95
- else d.resolve(f);
96
- });
97
- }
98
- return d.promise;
81
+ return new Promise(function(resolve, reject) {
82
+ if (method) {
83
+ frames.step(function(e, f) {
84
+ if (e !== null) return reject(e);
85
+ if (f.id === method)
86
+ resolve(f);
87
+ else
88
+ reject(new Error("Expected method: " + method +
89
+ ", got " + f.id));
90
+ });
91
+ }
92
+ else {
93
+ frames.step(function(e, f) {
94
+ if (e !== null) return reject(e);
95
+ else resolve(f);
96
+ });
97
+ }
98
+ });
99
99
  };
100
100
  }
101
101
  run(send, await);
@@ -107,6 +107,19 @@ function succeed(done) {
107
107
  return function() { done(); }
108
108
  }
109
109
 
110
+ // Produce a callback that will complete the test successfully
111
+ // only if the value is an object, it has the specified
112
+ // attribute, and its value is equals to the expected value
113
+ function succeedIfAttributeEquals(attribute, value, done) {
114
+ return function(object) {
115
+ if (object && !(object instanceof Error) && value === object[attribute]) {
116
+ return done();
117
+ }
118
+
119
+ done(new Error(attribute + " is not equal to " + value));
120
+ };
121
+ }
122
+
110
123
  // Produce a callback that will fail the test, given either an error
111
124
  // (to be used as a failure continuation) or any other value (to be
112
125
  // used as a success continuation when failure is expected)
@@ -196,6 +209,7 @@ module.exports = {
196
209
  socketPair: socketPair,
197
210
  runServer: runServer,
198
211
  succeed: succeed,
212
+ succeedIfAttributeEquals: succeedIfAttributeEquals,
199
213
  fail: fail,
200
214
  latch: latch,
201
215
  completes: completes,
package/.npmignore DELETED
@@ -1,9 +0,0 @@
1
- *~
2
- scratch
3
- # do not ignore lib/defs.js, we need that
4
- # node_modules is ignored anyway
5
- .travis.yml
6
- bin/amqp-rabbitmq-0.9.1.json
7
- bin/generate-defs.js
8
- etc/
9
- coverage/