dns2 2.0.5 → 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 +23 -1
  3. package/test/index.js +11 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dns2",
3
- "version": "2.0.5",
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
@@ -9,6 +9,27 @@ const toIPv6 = buffer => buffer
9
9
  .join(':')
10
10
  .replace(/\b(?:0+:){1,}/, ':');
11
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
+
12
33
  /**
13
34
  * [Packet description]
14
35
  * @param {[type]} data [description]
@@ -524,7 +545,7 @@ Packet.Resource.AAAA = {
524
545
  },
525
546
  encode: function(record, writer) {
526
547
  writer = writer || new Packet.Writer();
527
- const parts = record.address.split(':');
548
+ const parts = fromIPv6(record.address);
528
549
  writer.write(parts.length * 2, 16);
529
550
  parts.forEach(function(part) {
530
551
  writer.write(parseInt(part, 16), 16);
@@ -874,3 +895,4 @@ Packet.prototype.toBase64URL = function() {
874
895
 
875
896
  module.exports = Packet;
876
897
  module.exports.toIPv6 = toIPv6;
898
+ module.exports.fromIPv6 = fromIPv6;
package/test/index.js CHANGED
@@ -92,6 +92,17 @@ test('Package#toIPv6', function() {
92
92
  assert.equal(Packet.toIPv6([ 9734, 18176, 12552, 0, 0, 0, 44098, 10984 ]), '2606:4700:3108::ac42:2ae8');
93
93
  });
94
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
+
95
106
  test('Packet#parse', function() {
96
107
  const packet = Packet.parse(response);
97
108
  assert.equal(packet.questions[0].name, 'www.z.cn');