dns2 2.0.4 → 2.1.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.
Files changed (3) hide show
  1. package/package.json +2 -2
  2. package/packet.js +30 -6
  3. package/test/index.js +18 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dns2",
3
- "version": "2.0.4",
3
+ "version": "2.1.0",
4
4
  "description": "A DNS Server and Client Implementation in Pure JavaScript with no dependencies.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -18,7 +18,7 @@
18
18
  "keywords": [
19
19
  "dns"
20
20
  ],
21
- "author": "Liu Song <hi@lsong.org>",
21
+ "author": "Liu Song <song940@gmail.com>",
22
22
  "contributors": [
23
23
  "Andris Reinman <andris.reinman@gmail.com>",
24
24
  "Eviltik <eviltik@gmail.com>",
package/packet.js CHANGED
@@ -4,6 +4,32 @@ const BufferWriter = require('./lib/writer');
4
4
 
5
5
  const debug = debuglog('dns2');
6
6
 
7
+ const toIPv6 = buffer => buffer
8
+ .map(part => (part > 0 ? part.toString(16) : '0'))
9
+ .join(':')
10
+ .replace(/\b(?:0+:){1,}/, ':');
11
+
12
+ const fromIPv6 = (address) => {
13
+ const digits = address.split(':');
14
+ // CAVEAT edge case for :: and IPs starting
15
+ // or ending by ::
16
+ if (digits[0] === '') {
17
+ digits.shift();
18
+ }
19
+ if (digits[digits.length - 1] === '') {
20
+ digits.pop();
21
+ }
22
+ // CAVEAT we have to take into account
23
+ // the extra space used by the empty string
24
+ const missingFields = 8 - digits.length + 1;
25
+ return digits.flatMap((digit) => {
26
+ if (digit === '') {
27
+ return Array(missingFields).fill('0');
28
+ }
29
+ return digit.padStart(4, '0');
30
+ });
31
+ };
32
+
7
33
  /**
8
34
  * [Packet description]
9
35
  * @param {[type]} data [description]
@@ -514,16 +540,12 @@ Packet.Resource.AAAA = {
514
540
  length -= 2;
515
541
  parts.push(reader.read(16));
516
542
  }
517
- this.address = parts
518
- .map(part => part > 0 ? part.toString(16) : '')
519
- .join(':')
520
- .replace('::::', '::')
521
- .replace(':::', '::');
543
+ this.address = toIPv6(parts);
522
544
  return this;
523
545
  },
524
546
  encode: function(record, writer) {
525
547
  writer = writer || new Packet.Writer();
526
- const parts = record.address.split(':');
548
+ const parts = fromIPv6(record.address);
527
549
  writer.write(parts.length * 2, 16);
528
550
  parts.forEach(function(part) {
529
551
  writer.write(parseInt(part, 16), 16);
@@ -872,3 +894,5 @@ Packet.prototype.toBase64URL = function() {
872
894
  };
873
895
 
874
896
  module.exports = Packet;
897
+ module.exports.toIPv6 = toIPv6;
898
+ module.exports.fromIPv6 = fromIPv6;
package/test/index.js CHANGED
@@ -85,6 +85,24 @@ test('Question#decode', function() {
85
85
  });
86
86
 
87
87
  //
88
+ test('Package#toIPv6', function() {
89
+ assert.equal(Packet.toIPv6([ 10756, 20034, 512, 0, 0, 0, 0, 803 ]), '2a04:4e42:200::323');
90
+ assert.equal(Packet.toIPv6([ 10755, 45248, 3, 208, 0, 0, 5057, 61441 ]), '2a03:b0c0:3:d0::13c1:f001');
91
+ assert.equal(Packet.toIPv6([ 10752, 5200, 16387, 2055, 0, 0, 0, 8206 ]), '2a00:1450:4003:807::200e');
92
+ assert.equal(Packet.toIPv6([ 9734, 18176, 12552, 0, 0, 0, 44098, 10984 ]), '2606:4700:3108::ac42:2ae8');
93
+ });
94
+
95
+ test('Package#fromIPv6', function() {
96
+ assert.deepEqual(Packet.fromIPv6('2a04:4e42:200::323'), [
97
+ '2a04', '4e42', '0200', '0', '0', '0', '0', '0323' ]);
98
+ assert.deepEqual(Packet.fromIPv6('2a03:b0c0:3:d0::13c1:f001'), [ '2a03', 'b0c0', '0003', '00d0', '0', '0', '13c1', 'f001' ]);
99
+ assert.deepEqual(Packet.fromIPv6('2a00:1450:4003:807::200e'), [ '2a00', '1450', '4003', '0807', '0', '0', '0', '200e' ]);
100
+ assert.deepEqual(Packet.fromIPv6('2606:4700:3108::ac42:2ae8'), [ '2606', '4700', '3108', '0', '0', '0', 'ac42', '2ae8' ]);
101
+ assert.deepEqual(Packet.fromIPv6('::'), [ '0', '0', '0', '0', '0', '0', '0', '0' ]);
102
+ assert.deepEqual(Packet.fromIPv6('::2606:4700:3108'), [ '0', '0', '0', '0', '0', '2606', '4700', '3108' ]);
103
+ assert.deepEqual(Packet.fromIPv6('606:4700:3108::'), [ '0606', '4700', '3108', '0', '0', '0', '0', '0' ]);
104
+ });
105
+
88
106
  test('Packet#parse', function() {
89
107
  const packet = Packet.parse(response);
90
108
  assert.equal(packet.questions[0].name, 'www.z.cn');