amqplib 1.2.0 → 2.0.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Change log for amqplib
2
2
 
3
+ ## v2.0.1
4
+ - Remove `buffer-more-ints` dependency; use Node.js built-in BigInt Buffer methods (`readBigInt64BE`, `readBigUInt64BE`, `writeBigInt64BE`, `writeBigUInt64BE`) which have been available since Node.js v10.4
5
+
6
+ ## v2.0.0
7
+ - **BREAKING**: `heartbeat: 0` now disables heartbeats, overriding any value suggested by the server. Previously, `0` was treated as "no preference" and the server's suggested value was used. If you are passing `heartbeat: 0` and want to preserve the old behaviour, omit the option or pass `null` instead (fixes #467)
8
+
3
9
  ## v1.2.0
4
10
  - Add bundled TypeScript type definitions (fixes #843)
5
11
 
@@ -113,7 +113,6 @@ println("'use strict';");
113
113
  nl();
114
114
  nl();
115
115
  println('var codec = require("./codec");');
116
- println('var ints = require("buffer-more-ints");');
117
116
  println('var encodeTable = codec.encodeTable;');
118
117
  println('var decodeFields = codec.decodeFields;');
119
118
  nl();
@@ -412,7 +411,7 @@ function encoderFn(method) {
412
411
  case 'longlong':
413
412
  case 'timestamp':
414
413
  checkAssignArg(a);
415
- println('ints.writeUInt64BE(buffer, val, offset); offset += 8;');
414
+ println('buffer.writeBigUInt64BE(BigInt(val), offset); offset += 8;');
416
415
  break;
417
416
  case 'bit':
418
417
  checkAssignArg(a);
@@ -493,7 +492,7 @@ function decoderFn(method) {
493
492
  break;
494
493
  case 'longlong':
495
494
  case 'timestamp':
496
- println('val = ints.readUInt64BE(buffer, offset); offset += 8;');
495
+ println('val = Number(buffer.readBigUInt64BE(offset)); offset += 8;');
497
496
  break;
498
497
  case 'bit': {
499
498
  const bit = 1 << bitsInARow;
@@ -612,7 +611,7 @@ function encodePropsFn(props) {
612
611
  // skip frame size for now, we'll write it in when we know.
613
612
 
614
613
  // body size
615
- println('ints.writeUInt64BE(buffer, size, 11);');
614
+ println('buffer.writeBigUInt64BE(BigInt(size), 11);');
616
615
 
617
616
  println('flags = 0;');
618
617
  // we'll write the flags later too
@@ -642,7 +641,7 @@ function encodePropsFn(props) {
642
641
  break;
643
642
  case 'longlong':
644
643
  case 'timestamp':
645
- println('ints.writeUInt64BE(buffer, val, offset);');
644
+ println('buffer.writeBigUInt64BE(BigInt(val), offset);');
646
645
  println('offset += 8;');
647
646
  break;
648
647
  case 'shortstr': {
@@ -706,7 +705,7 @@ function decodePropsFn(props) {
706
705
  break;
707
706
  case 'longlong':
708
707
  case 'timestamp':
709
- println('val = ints.readUInt64BE(buffer, offset); offset += 8;');
708
+ println('val = Number(buffer.readBigUInt64BE(offset)); offset += 8;');
710
709
  break;
711
710
  case 'longstr':
712
711
  println('len = buffer.readUInt32BE(offset); offset += 4;');
package/lib/codec.js CHANGED
@@ -45,8 +45,6 @@ properties are present. This scheme can save ones of bytes per message
45
45
  (messages which take a minimum of three frames each to send).
46
46
  */
47
47
 
48
- const ints = require('buffer-more-ints');
49
-
50
48
  // JavaScript uses only doubles so what I'm testing for is whether
51
49
  // it's *better* to encode a number as a float or double. This really
52
50
  // just amounts to testing whether there's a fractional part to the
@@ -219,7 +217,7 @@ function encodeFieldValue(buffer, value, offset) {
219
217
  case 'long':
220
218
  case 'int64':
221
219
  tag('l');
222
- ints.writeInt64BE(buffer, val, offset);
220
+ buffer.writeBigInt64BE(BigInt(val), offset);
223
221
  offset += 8;
224
222
  break;
225
223
 
@@ -227,7 +225,7 @@ function encodeFieldValue(buffer, value, offset) {
227
225
  // `{'!': type, value: val}
228
226
  case 'timestamp':
229
227
  tag('T');
230
- ints.writeUInt64BE(buffer, val, offset);
228
+ buffer.writeBigUInt64BE(BigInt(val), offset);
231
229
  offset += 8;
232
230
  break;
233
231
  case 'float':
@@ -296,7 +294,7 @@ function decodeFields(slice) {
296
294
  break;
297
295
  }
298
296
  case 'T':
299
- val = ints.readUInt64BE(slice, offset);
297
+ val = Number(slice.readBigUInt64BE(offset));
300
298
  offset += 8;
301
299
  val = { '!': 'timestamp', value: val };
302
300
  break;
@@ -321,7 +319,7 @@ function decodeFields(slice) {
321
319
  offset += 4;
322
320
  break;
323
321
  case 'l':
324
- val = ints.readInt64BE(slice, offset);
322
+ val = Number(slice.readBigInt64BE(offset));
325
323
  offset += 8;
326
324
  break;
327
325
  case 's':
package/lib/connect.js CHANGED
@@ -59,7 +59,7 @@ function openFrames(vhost, query, credentials, extraClientProperties) {
59
59
  // tune-ok
60
60
  channelMax: intOrDefault(query.channelMax, 0),
61
61
  frameMax: intOrDefault(query.frameMax, 131072),
62
- heartbeat: intOrDefault(query.heartbeat, 0),
62
+ heartbeat: query.heartbeat != null ? intOrDefault(query.heartbeat, 0) : null,
63
63
 
64
64
  // open
65
65
  virtualHost: vhost,
package/lib/connection.js CHANGED
@@ -159,7 +159,13 @@ class Connection extends EventEmitter {
159
159
  const fields = reply.fields;
160
160
  tunedOptions.frameMax = negotiate(fields.frameMax, allFields.frameMax);
161
161
  tunedOptions.channelMax = negotiate(fields.channelMax, allFields.channelMax);
162
- tunedOptions.heartbeat = negotiate(fields.heartbeat, allFields.heartbeat);
162
+ if (allFields.heartbeat == null) {
163
+ tunedOptions.heartbeat = fields.heartbeat; // no preference, accept server value
164
+ } else if (allFields.heartbeat === 0) {
165
+ tunedOptions.heartbeat = 0; // explicitly disable heartbeats
166
+ } else {
167
+ tunedOptions.heartbeat = negotiate(fields.heartbeat, allFields.heartbeat);
168
+ }
163
169
  try {
164
170
  send(defs.ConnectionTuneOk);
165
171
  send(defs.ConnectionOpen);
package/lib/defs.js CHANGED
@@ -501,7 +501,7 @@ function decodeBasicDeliver(buffer) {
501
501
  val = buffer.toString('utf8', offset, offset + len);
502
502
  offset += len;
503
503
  fields.consumerTag = val;
504
- val = ints.readUInt64BE(buffer, offset);
504
+ val = Number(buffer.readBigUInt64BE(offset));
505
505
  offset += 8;
506
506
  fields.deliveryTag = val;
507
507
  val = !!(1 & buffer[offset]);
@@ -557,7 +557,7 @@ function encodeBasicDeliver(channel, fields) {
557
557
  val = fields.deliveryTag;
558
558
  if (void 0 === val) throw new Error("Missing value for mandatory field 'deliveryTag'");
559
559
  if ('number' != typeof val || isNaN(val)) throw new TypeError("Field 'deliveryTag' is the wrong type; must be a number (but not NaN)");
560
- ints.writeUInt64BE(buffer, val, offset);
560
+ buffer.writeBigUInt64BE(BigInt(val), offset);
561
561
  offset += 8;
562
562
  val = fields.redelivered;
563
563
  void 0 === val && (val = !1);
@@ -652,7 +652,7 @@ function decodeBasicGetOk(buffer) {
652
652
  routingKey: void 0,
653
653
  messageCount: void 0,
654
654
  };
655
- val = ints.readUInt64BE(buffer, offset);
655
+ val = Number(buffer.readBigUInt64BE(offset));
656
656
  offset += 8;
657
657
  fields.deliveryTag = val;
658
658
  val = !!(1 & buffer[offset]);
@@ -699,7 +699,7 @@ function encodeBasicGetOk(channel, fields) {
699
699
  val = fields.deliveryTag;
700
700
  if (void 0 === val) throw new Error("Missing value for mandatory field 'deliveryTag'");
701
701
  if ('number' != typeof val || isNaN(val)) throw new TypeError("Field 'deliveryTag' is the wrong type; must be a number (but not NaN)");
702
- ints.writeUInt64BE(buffer, val, offset);
702
+ buffer.writeBigUInt64BE(BigInt(val), offset);
703
703
  offset += 8;
704
704
  val = fields.redelivered;
705
705
  void 0 === val && (val = !1);
@@ -777,7 +777,7 @@ function decodeBasicAck(buffer) {
777
777
  deliveryTag: void 0,
778
778
  multiple: void 0,
779
779
  };
780
- val = ints.readUInt64BE(buffer, offset);
780
+ val = Number(buffer.readBigUInt64BE(offset));
781
781
  offset += 8;
782
782
  fields.deliveryTag = val;
783
783
  val = !!(1 & buffer[offset]);
@@ -798,7 +798,7 @@ function encodeBasicAck(channel, fields) {
798
798
  if (void 0 === val) val = 0;
799
799
  else if ('number' != typeof val || isNaN(val))
800
800
  throw new TypeError("Field 'deliveryTag' is the wrong type; must be a number (but not NaN)");
801
- ints.writeUInt64BE(buffer, val, offset);
801
+ buffer.writeBigUInt64BE(BigInt(val), offset);
802
802
  offset += 8;
803
803
  val = fields.multiple;
804
804
  void 0 === val && (val = !1);
@@ -817,7 +817,7 @@ function decodeBasicReject(buffer) {
817
817
  deliveryTag: void 0,
818
818
  requeue: void 0,
819
819
  };
820
- val = ints.readUInt64BE(buffer, offset);
820
+ val = Number(buffer.readBigUInt64BE(offset));
821
821
  offset += 8;
822
822
  fields.deliveryTag = val;
823
823
  val = !!(1 & buffer[offset]);
@@ -837,7 +837,7 @@ function encodeBasicReject(channel, fields) {
837
837
  val = fields.deliveryTag;
838
838
  if (void 0 === val) throw new Error("Missing value for mandatory field 'deliveryTag'");
839
839
  if ('number' != typeof val || isNaN(val)) throw new TypeError("Field 'deliveryTag' is the wrong type; must be a number (but not NaN)");
840
- ints.writeUInt64BE(buffer, val, offset);
840
+ buffer.writeBigUInt64BE(BigInt(val), offset);
841
841
  offset += 8;
842
842
  val = fields.requeue;
843
843
  void 0 === val && (val = !0);
@@ -931,7 +931,7 @@ function decodeBasicNack(buffer) {
931
931
  multiple: void 0,
932
932
  requeue: void 0,
933
933
  };
934
- val = ints.readUInt64BE(buffer, offset);
934
+ val = Number(buffer.readBigUInt64BE(offset));
935
935
  offset += 8;
936
936
  fields.deliveryTag = val;
937
937
  val = !!(1 & buffer[offset]);
@@ -954,7 +954,7 @@ function encodeBasicNack(channel, fields) {
954
954
  if (void 0 === val) val = 0;
955
955
  else if ('number' != typeof val || isNaN(val))
956
956
  throw new TypeError("Field 'deliveryTag' is the wrong type; must be a number (but not NaN)");
957
- ints.writeUInt64BE(buffer, val, offset);
957
+ buffer.writeBigUInt64BE(BigInt(val), offset);
958
958
  offset += 8;
959
959
  val = fields.multiple;
960
960
  void 0 === val && (val = !1);
@@ -3313,7 +3313,7 @@ function encodeBasicProperties(channel, size, fields) {
3313
3313
  buffer[0] = 2;
3314
3314
  buffer.writeUInt16BE(channel, 1);
3315
3315
  buffer.writeUInt32BE(3932160, 7);
3316
- ints.writeUInt64BE(buffer, size, 11);
3316
+ buffer.writeBigUInt64BE(BigInt(size), 11);
3317
3317
  flags = 0;
3318
3318
  offset = 21;
3319
3319
  val = fields.contentType;
@@ -3384,7 +3384,7 @@ function encodeBasicProperties(channel, size, fields) {
3384
3384
  val = fields.timestamp;
3385
3385
  if (void 0 != val) {
3386
3386
  flags += 64;
3387
- ints.writeUInt64BE(buffer, val, offset);
3387
+ buffer.writeBigUInt64BE(BigInt(val), offset);
3388
3388
  offset += 8;
3389
3389
  }
3390
3390
  val = fields.type;
@@ -3508,7 +3508,7 @@ function decodeBasicProperties(buffer) {
3508
3508
  fields.messageId = val;
3509
3509
  }
3510
3510
  if (64 & flags) {
3511
- val = ints.readUInt64BE(buffer, offset);
3511
+ val = Number(buffer.readBigUInt64BE(offset));
3512
3512
  offset += 8;
3513
3513
  fields.timestamp = val;
3514
3514
  }
@@ -3544,7 +3544,6 @@ function decodeBasicProperties(buffer) {
3544
3544
  }
3545
3545
 
3546
3546
  var codec = require('./codec'),
3547
- ints = require('buffer-more-ints'),
3548
3547
  encodeTable = codec.encodeTable,
3549
3548
  decodeFields = codec.decodeFields,
3550
3549
  SCRATCH = Buffer.alloc(65536),
package/lib/frame.js CHANGED
@@ -1,4 +1,3 @@
1
- const ints = require('buffer-more-ints');
2
1
  const defs = require('./defs');
3
2
  const constants = defs.constants;
4
3
  const decode = defs.decode;
@@ -43,22 +42,8 @@ const FRAME_END_BYTES = 1;
43
42
  * }} FrameStructure
44
43
  */
45
44
 
46
- /**
47
- * This is a polyfill which will read a big int 64 bit as a number.
48
- * @arg { Buffer } buffer
49
- * @arg { number } offset
50
- * @returns { number }
51
- */
52
45
  function readInt64BE(buffer, offset) {
53
- /**
54
- * We try to use native implementation if available here because
55
- * buffer-more-ints does not
56
- */
57
- if (typeof Buffer.prototype.readBigInt64BE === 'function') {
58
- return Number(buffer.readBigInt64BE(offset));
59
- }
60
-
61
- return ints.readInt64BE(buffer, offset);
46
+ return Number(buffer.readBigInt64BE(offset));
62
47
  }
63
48
 
64
49
  // %%% TESTME possibly better to cons the first bit and write the
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "amqplib",
3
3
  "homepage": "http://amqp-node.github.io/amqplib/",
4
4
  "main": "./channel_api.js",
5
- "version": "1.2.0",
5
+ "version": "2.0.1",
6
6
  "types": "./index.d.ts",
7
7
  "exports": {
8
8
  ".": {
@@ -22,9 +22,7 @@
22
22
  "engines": {
23
23
  "node": ">=18"
24
24
  },
25
- "dependencies": {
26
- "buffer-more-ints": "~1.0.0"
27
- },
25
+ "dependencies": {},
28
26
  "devDependencies": {
29
27
  "@biomejs/biome": "^2.2.2",
30
28
  "@types/node": "^25.6.0",
@@ -1,6 +1,5 @@
1
1
  const { describe, it } = require('node:test');
2
2
  const assert = require('node:assert');
3
- const ints = require('buffer-more-ints');
4
3
  const { forAll } = require('claire');
5
4
  const codec = require('../lib/codec');
6
5
  const defs = require('../lib/defs');
@@ -152,7 +151,7 @@ describe('Codec', () => {
152
151
  const buf = defs.encodeProperties(properties.id, 0, properties.size, properties.fields);
153
152
  // FIXME depends on framing, ugh
154
153
  const fs1 = defs.decode(properties.id, buf.subarray(19, buf.length));
155
- assert.equal(properties.size, ints.readUInt64BE(buf, 11));
154
+ assert.equal(properties.size, Number(buf.readBigUInt64BE(11)));
156
155
  assertEqualModuloDefaults(properties, fs1);
157
156
  return true;
158
157
  });
@@ -696,6 +696,77 @@ describe('Connection', () => {
696
696
  .then(cb, cb);
697
697
  // conspicuously not sending anything ...
698
698
  }));
699
+
700
+ it('without a heartbeat option, server suggested value is used', connectionTest((c, cb) => {
701
+ // OPEN_OPTS.heartbeat is null — no preference set by client
702
+ c.once('error', (err) => {
703
+ assert.match(err.message, /Heartbeat timeout/);
704
+ cb();
705
+ });
706
+ c.open(OPEN_OPTS, (err) => {
707
+ assert.ifError(err);
708
+ assert.strictEqual(c.heartbeat, 1);
709
+ });
710
+ }, (send, wait, cb) => {
711
+ send(defs.ConnectionStart, {
712
+ versionMajor: 0, versionMinor: 9,
713
+ serverProperties: {},
714
+ mechanisms: Buffer.from('PLAIN'),
715
+ locales: Buffer.from('en_US'),
716
+ });
717
+ wait(defs.ConnectionStartOk)()
718
+ .then(() => send(defs.ConnectionTune, { channelMax: 0, heartbeat: 1, frameMax: 0 }))
719
+ .then(wait(defs.ConnectionTuneOk))
720
+ .then(wait(defs.ConnectionOpen))
721
+ .then(() => send(defs.ConnectionOpenOk, { knownHosts: '' }))
722
+ .then(cb, cb);
723
+ }));
724
+
725
+ it('heartbeat:0 means no heartbeater is started', connectionTest((c, cb) => {
726
+ const opts = Object.create(OPEN_OPTS);
727
+ opts.heartbeat = 0;
728
+ c.open(opts, (err) => {
729
+ assert.ifError(err);
730
+ assert.strictEqual(c.heartbeat, 0);
731
+ assert.strictEqual(c.heartbeater, null);
732
+ cb();
733
+ });
734
+ }, (send, wait, cb) => {
735
+ send(defs.ConnectionStart, {
736
+ versionMajor: 0, versionMinor: 9,
737
+ serverProperties: {},
738
+ mechanisms: Buffer.from('PLAIN'),
739
+ locales: Buffer.from('en_US'),
740
+ });
741
+ wait(defs.ConnectionStartOk)()
742
+ .then(() => send(defs.ConnectionTune, { channelMax: 0, heartbeat: 60, frameMax: 0 }))
743
+ .then(wait(defs.ConnectionTuneOk))
744
+ .then(wait(defs.ConnectionOpen))
745
+ .then(() => send(defs.ConnectionOpenOk, { knownHosts: '' }))
746
+ .then(cb, cb);
747
+ }));
748
+
749
+ it('heartbeat:0 disables heartbeats even if server suggests one', connectionTest((c, cb) => {
750
+ const opts = Object.create(OPEN_OPTS);
751
+ opts.heartbeat = 0;
752
+ c.open(opts, cb);
753
+ }, (send, wait, cb) => {
754
+ send(defs.ConnectionStart, {
755
+ versionMajor: 0, versionMinor: 9,
756
+ serverProperties: {},
757
+ mechanisms: Buffer.from('PLAIN'),
758
+ locales: Buffer.from('en_US'),
759
+ });
760
+ wait(defs.ConnectionStartOk)()
761
+ .then(() => send(defs.ConnectionTune, { channelMax: 0, heartbeat: 60, frameMax: 0 }))
762
+ .then(wait(defs.ConnectionTuneOk))
763
+ .then((tuneOk) => {
764
+ assert.strictEqual(tuneOk.fields.heartbeat, 0);
765
+ })
766
+ .then(wait(defs.ConnectionOpen))
767
+ .then(() => send(defs.ConnectionOpenOk, { knownHosts: '' }))
768
+ .then(cb, cb);
769
+ }));
699
770
  });
700
771
 
701
772
  });
package/test/lib/data.js CHANGED
@@ -214,7 +214,7 @@ const OPEN_OPTS = {
214
214
  // tune-ok
215
215
  channelMax: 0,
216
216
  frameMax: 0,
217
- heartbeat: 0,
217
+ heartbeat: null,
218
218
 
219
219
  // open
220
220
  virtualHost: '/',