Skip to content

Commit

Permalink
2023.04.06
Browse files Browse the repository at this point in the history
- imports Buffer from Deno's internals
- adds NAPTR support from upstream
- declares optional parameters in functions
- adds tests
- removes import map
- updates README
  • Loading branch information
NetOpWibby committed Apr 7, 2023
1 parent 2b4e7aa commit 16cf08d
Show file tree
Hide file tree
Showing 40 changed files with 1,003 additions and 164 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# CHANGELOG

## 2023.04.06

- imports `Buffer` from Deno's internals
- adds `NAPTR` support from upstream
- declares optional parameters in functions
- adds tests
- removes import map
- updates README
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,14 @@

This module is forked from [mafintosh/dns-packet](https://github.com/mafintosh/dns-packet) because I was trying to track down a Deno compile issue and thought `dns-packet` was the culprit. I was mistaken but I went through the work of refactoring this so no need for that to go to waste. Improvements? Modularity. I'm not a fan of everything in one massive file. Added benefit of TypeScript is that several linting issues were caught and fixed.

This README will be updated when I'm done with my larger project that contains this module. For now, just import what you need from `mod.ts` and refer to the original repo for details.
This README will be updated when I'm done with my larger project that imports this module. For now, just import what you need from `mod.ts` and refer to [`test.ts`](/test.ts) for usage/reference.

High-level changes:
- written in TypeScript (for Deno)
- caught and fixed issues with upstream code
- you can still import into Node.js projects
- modularity
- every record is its own file
- utility functions are in their own folder as well
- record functions are now classes so they must be instantiated with `new`
- replaced `decode.bytes` and `encode.bytes` with `decodeBytes`and `encodeBytes` respectively
6 changes: 6 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# TODO

- [ ] get rid of implicit `any` types
- [ ] get all tests running
- [ ] figure out `rrtpyes` issue in the NSEC test
- [ ] add examples
1 change: 0 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"compilerOptions": {
"noImplicitAny": false
},
"importMap": "import_map.json",
"lint": {
"rules": {
"exclude": ["no-explicit-any"]
Expand Down
44 changes: 15 additions & 29 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions import_map.json

This file was deleted.

1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export { DS } from "./src/records/ds.ts";
export { PTR as CNAME } from "./src/records/ptr.ts";
export { PTR as DNAME } from "./src/records/ptr.ts";
export { HINFO } from "./src/records/hinfo.ts";
export { NAPTR } from "./src/records/naptr.ts";
export { MX } from "./src/records/mx.ts";
export { NS } from "./src/records/ns.ts";
export { NSEC } from "./src/records/nsec.ts";
Expand Down
13 changes: 7 additions & 6 deletions src/answer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/// import

import { Buffer } from "https://deno.land/std@0.166.0/node/internal/buffer.mjs";
import { Buffer } from "node:buffer";

/// util

Expand All @@ -22,10 +22,11 @@ export class Answer {
decodeBytes = 0;
encodeBytes = 0;

decode(buf, offset) {
if (!offset) offset = 0;
decode(buf, offset?) {
if (!offset)
offset = 0;

const a: { [key: string]: any; } = {};
const a: { [key: string]: unknown; } = {};
const name = new Name();
const oldOffset = offset;
const opt = new OPT();
Expand All @@ -39,7 +40,7 @@ export class Answer {
a.extendedRcode = buf.readUInt8(offset + 4);
a.ednsVersion = buf.readUInt8(offset + 5);
a.flags = buf.readUInt16BE(offset + 6);
a.flag_do = ((a.flags >> 15) & 0x1) === 1;
a.flag_do = ((Number(a.flags) >> 15) & 0x1) === 1;
a.options = opt.decode(buf, offset + 8);
offset += 8 + opt.decodeBytes;
} else {
Expand All @@ -59,7 +60,7 @@ export class Answer {
return a;
}

encode(a, buf, offset) {
encode(a, buf?, offset?) {
if (!buf)
buf = Buffer.alloc(this.encodingLength(a));

Expand Down
2 changes: 1 addition & 1 deletion src/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/// import

import { Buffer } from "https://deno.land/std@0.166.0/node/internal/buffer.mjs";
import { Buffer } from "node:buffer";

/// util

Expand Down
4 changes: 2 additions & 2 deletions src/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class Header {
decodeBytes = 12;
encodeBytes = 12;

decode(buf, offset) {
decode(buf, offset?) {
if (!offset)
offset = 0;

Expand Down Expand Up @@ -47,7 +47,7 @@ export class Header {
}
}

encode(h, buf, offset) {
encode(h, buf?, offset?) {
if (!buf)
buf = this.encodingLength();

Expand Down
6 changes: 3 additions & 3 deletions src/name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/// import

import { Buffer } from "https://deno.land/std@0.166.0/node/internal/buffer.mjs";
import { Buffer } from "node:buffer";



Expand All @@ -13,7 +13,7 @@ export class Name {
decodeBytes = 0;
encodeBytes = 0;

decode(buf, offset) {
decode(buf, offset?) {
if (!offset)
offset = 0;

Expand Down Expand Up @@ -74,7 +74,7 @@ export class Name {
list.join(".");
}

encode(str, buf, offset) {
encode(str, buf?, offset?) {
if (!buf)
buf = Buffer.alloc(this.encodingLength(str));

Expand Down
14 changes: 7 additions & 7 deletions src/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/// import

import { Buffer } from "https://deno.land/std@0.166.0/node/internal/buffer.mjs";
import { Buffer } from "node:buffer";

/// util

Expand All @@ -20,13 +20,13 @@ export class Question {
decodeBytes = 0;
encodeBytes = 0;

decode(buf, offset) {
decode(buf, offset?) {
if (!offset)
offset = 0;

const name = new Name();
const oldOffset = offset;
const q: { [key: string]: any; } = {};
const q: { [key: string]: unknown; } = {};

q.name = name.decode(buf, offset);
offset += name.decodeBytes;
Expand All @@ -35,16 +35,16 @@ export class Question {
q.class = classes.toString(buf.readUInt16BE(offset));
offset += 2;

const qu = !!(q.class & QU_MASK);
const qu = !!(Number(q.class) & QU_MASK);

if (qu)
q.class &= NOT_QU_MASK;
if (qu) // `q.class &= NOT_QU_MASK` is equivalent to `q.class = q.class & NOT_QU_MASK`.
q.class = Number(q.class) & NOT_QU_MASK;

this.decodeBytes = offset - oldOffset;
return q;
}

encode(q, buf, offset) {
encode(q, buf?, offset?) {
if (!buf)
buf = Buffer.alloc(this.encodingLength(q));

Expand Down
4 changes: 4 additions & 0 deletions src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { DNSKEY } from "./records/dnskey.ts";
import { DS } from "./records/ds.ts";
import { HINFO } from "./records/hinfo.ts";
import { MX } from "./records/mx.ts";
import { NAPTR } from "./records/naptr.ts";
import { NS } from "./records/ns.ts";
import { NSEC } from "./records/nsec.ts";
import { NSEC3 } from "./records/nsec3.ts";
Expand Down Expand Up @@ -56,6 +57,9 @@ export function Record(type) {
case "MX":
return new MX();

case "NAPTR":
return new NAPTR();

case "NS":
return new NS();

Expand Down
6 changes: 3 additions & 3 deletions src/records/a.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/// import

import { Buffer } from "https://deno.land/std@0.166.0/node/internal/buffer.mjs";
import { Buffer } from "node:buffer";

/// util

Expand All @@ -17,7 +17,7 @@ export class A {
decodeBytes = 0;
encodeBytes = 0;

decode(buf, offset) {
decode(buf, offset?) {
if (!offset)
offset = 0;

Expand All @@ -28,7 +28,7 @@ export class A {
return host;
}

encode(host, buf, offset) {
encode(host, buf?, offset?) {
if (!buf)
buf = Buffer.alloc(this.encodingLength());

Expand Down
6 changes: 3 additions & 3 deletions src/records/aaaa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/// import

import { Buffer } from "https://deno.land/std@0.166.0/node/internal/buffer.mjs";
import { Buffer } from "node:buffer";

/// util

Expand All @@ -17,7 +17,7 @@ export class AAAA {
decodeBytes = 0;
encodeBytes = 0;

decode(buf, offset) {
decode(buf, offset?) {
if (!offset)
offset = 0;

Expand All @@ -28,7 +28,7 @@ export class AAAA {
return host;
}

encode(host, buf, offset) {
encode(host, buf?, offset?) {
if (!buf)
buf = Buffer.alloc(this.encodingLength());

Expand Down
28 changes: 14 additions & 14 deletions src/records/caa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/// import

import { Buffer } from "https://deno.land/std@0.166.0/node/internal/buffer.mjs";
import { Buffer } from "node:buffer";

/// util

Expand All @@ -18,31 +18,31 @@ export class CAA {
encodeBytes = 0;
ISSUER_CRITICAL = 1 << 7;

decode(buf, offset) {
decode(buf, offset?) {
if (!offset)
offset = 0;

const len = buf.readUInt16BE(offset);
offset += 2;

const data: { [key: string]: any; } = {};
const data: { [key: string]: unknown; } = {};
const oldOffset = offset;
const sstring = new String();
const string = new String();

data.flags = buf.readUInt8(offset);
offset += 1;
data.tag = sstring.decode(buf, offset);
offset += sstring.decodeBytes;
data.tag = string.decode(buf, offset);
offset += string.decodeBytes;
data.value = buf.toString("utf-8", offset, oldOffset + len);
data.issuerCritical = !!(data.flags & this.ISSUER_CRITICAL);
data.issuerCritical = !!(Number(data.flags) & this.ISSUER_CRITICAL);
this.decodeBytes = len + 2;

return data;
}

encode(data, buf, offset) {
encode(data, buf?, offset?) {
const len = this.encodingLength(data);
const sstring = new String();
const string = new String();

if (!buf)
buf = Buffer.alloc(this.encodingLength(data));
Expand All @@ -57,8 +57,8 @@ export class CAA {
offset += 2;
buf.writeUInt8(data.flags || 0, offset);
offset += 1;
sstring.encode(data.tag, buf, offset);
offset += sstring.encodeBytes;
string.encode(data.tag, buf, offset);
offset += string.encodeBytes;
buf.write(data.value, offset);
offset += Buffer.byteLength(data.value);
this.encodeBytes = len;
Expand All @@ -67,10 +67,10 @@ export class CAA {
}

encodingLength(data) {
const sstring = new String();
const string = new String();

return sstring.encodingLength(data.tag) +
sstring.encodingLength(data.value) +
return string.encodingLength(data.tag) +
string.encodingLength(data.value) +
2;
}
}
Loading

0 comments on commit 16cf08d

Please sign in to comment.