Skip to content

Commit

Permalink
add subpath exports for sub-modules
Browse files Browse the repository at this point in the history
  • Loading branch information
hunts committed May 22, 2023
1 parent 8c9193a commit 222b330
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 46 deletions.
25 changes: 24 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
{
"name": "@dnspect/dns-ts",
"version": "0.1.1",
"version": "0.1.3",
"description": "DNS library in TypeScript",
"author": "Minghang Chen <chen@minghang.dev> (https://minghang.dev)",
"license": "MIT",
"type": "module",
"types": "./dist/index.d.ts",
"main": "./dist/index.js",
"exports": {
".": "./dist/index.js",
"./edns": "./dist/edns/index.js",
"./records": "./dist/records/index.js",
"./records/dnssec": "./dist/records/dnssec/index.js",
"./rr": "./dist/rr.js"
},
"typesVersions": {
"*": {
"edns": [
"./dist/edns/index.d.ts"
],
"records": [
"./dist/records/index.d.ts"
],
"records/dnssec": [
"./dist/records/dnssec/index.d.ts"
],
"rr": [
"./dist/rr.d.ts"
]
}
},
"dependencies": {
"@dnspect/ip-address-ts": "0.1",
"base64-js": "1"
Expand Down
2 changes: 1 addition & 1 deletion src/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Uint16, Uint8, Uint32 } from "./types";
* Maximum size of a TCP packet is 64K (65535 bytes), length of a DNS message is 2 bytes.
* Thus, the message size itself should not exceed 65535-2, which is 0xfffd.
*/
const MAX_MESSAGE_SIZE = 0xFFFD;
export const MAX_MESSAGE_SIZE = 0xFFFD;

/**
* Allows for reading bytes from a source.
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export { Header, Message, Section, RecordSection } from "./message";
export { MessageBuilder } from "./message-builder";
export { HexString } from "./packet";
export { HexString, CharacterString, Slice } from "./packet";
export { Question } from "./question";
export { stringToBinary, binaryToString } from "./encoding";
export { ParseError, SemanticError } from "./error";
export { FQDN, ROOT_ZONE, ARPA_ZONE, IN_ADDR_ARPA_ZONE, IP6_ARPA_ZONE, EXAMPLE_ZONE } from "./fqdn";
export { Uint8, Uint16, Uint32, Uint48, Uint16Max, Opcode, Class, QClass, Rcode, RRType, QType } from "./types";
4 changes: 2 additions & 2 deletions src/message-builder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { A, AAAA, CNAME, OPT, OptHeader, TXT } from "./records";
import { Address4, Address6 } from "@dnspect/ip-address-ts";
import { CharactorString } from "./packet";
import { CharacterString } from "./packet";
import { Class, Opcode, QType, RRType, Rcode, Uint32 } from "./types";
import { FQDN } from "./fqdn";
import { Message, Header } from "./message";
Expand Down Expand Up @@ -329,7 +329,7 @@ class AnswerBuilder extends SectionBuilder {
*/
push_txt(owner: Owner, ttl: Uint32, content: string[], cls: Class = Class.INET) {
const txt = new TXT(new RRHeader(owner, RRType.TXT, cls, ttl));
txt.content = content.map((str) => new CharactorString(str));
txt.content = content.map((str) => new CharacterString(str));
this.push(txt);
}
}
Expand Down
18 changes: 8 additions & 10 deletions src/packet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Uint16, Uint32, Uint48, Uint8 } from "./types";
import { ParseError } from "./error";
import { FQDN } from "./fqdn";
import * as base32 from "./base32";
import { EncodingScheme, binaryToString } from "./encoding";
import { Writer, Reader, OctetBuffer } from "./buffer";

Expand Down Expand Up @@ -80,11 +79,11 @@ export class HexString {
}

/**
* CharactorString (aka <character-string> in the RFCs) is a single length octet followed by that
* number of characters. CharactorString is treated as binary information, and can be up to 256
* CharacterString (aka <character-string> in the RFCs) is a single length octet followed by that
* number of characters. CharacterString is treated as binary information, and can be up to 256
* characters in length (including the length octet).
*/
export class CharactorString {
export class CharacterString {
private str: string;

constructor(str: string) {
Expand All @@ -104,10 +103,10 @@ export class CharactorString {
return buf.writeUint8(this.str.length) + buf.writeString(this.str, "ascii");
}

static unpack(s: Slice): CharactorString {
const len = s.readOctet();
static unpack(s: Slice): CharacterString {
const len = s.readUint8();
const str = s.readString("ascii", len);
return new CharactorString(str);
return new CharacterString(str);
}
}

Expand All @@ -124,7 +123,6 @@ export class Slice {
private cur: number;
private len: number;
private lookupLabels?: (offset: Uint16, pointers: number) => [string[], Uint16];
private base32HexDec: base32.Decoder | null = null;

/**
* Creates a new slice from passed data.
Expand Down Expand Up @@ -212,11 +210,11 @@ export class Slice {
}

/**
* Reads a 8-bit octet and advances the read cursor.
* Reads a byte and advances the read cursor.
*
* @returns
*/
readOctet(): Uint8 {
readUint8(): Uint8 {
this.check(1);
const n = this.buf.readUint8(this.offset + this.cur);
this.cur += 1;
Expand Down
4 changes: 2 additions & 2 deletions src/records/dnssec/dnskey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ export class DNSKEY extends RR {

unpackRdata(rdata: Slice): void {
this.flags = rdata.readUint16();
this.protocol = rdata.readOctet();
this.algorithm = rdata.readOctet();
this.protocol = rdata.readUint8();
this.algorithm = rdata.readUint8();
this.publicKey = rdata.readUint8Array(this.header.rdlength - 4);
}

Expand Down
4 changes: 2 additions & 2 deletions src/records/dnssec/ds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ export class DS extends RR {

unpackRdata(rdata: Slice): void {
this.keyTag = rdata.readUint16();
this.algorithm = rdata.readOctet();
this.digestType = rdata.readOctet();
this.algorithm = rdata.readUint8();
this.digestType = rdata.readUint8();
this.digest = rdata.readUint8Array(this.header.rdlength - 4);
}

Expand Down
8 changes: 4 additions & 4 deletions src/records/dnssec/nsec3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ export class NSEC3 extends RR {
typeBitMaps!: TypeBitMaps;

unpackRdata(rdata: Slice): void {
this.hashAlg = rdata.readOctet();
this.flags = rdata.readOctet();
this.hashAlg = rdata.readUint8();
this.flags = rdata.readUint8();
this.iterations = rdata.readUint16();
this.salt = rdata.readUint8Array(rdata.readOctet());
this.nexHashedOwnerName = rdata.readUint8Array(rdata.readOctet());
this.salt = rdata.readUint8Array(rdata.readUint8());
this.nexHashedOwnerName = rdata.readUint8Array(rdata.readUint8());
this.typeBitMaps = TypeBitMaps.unpack(rdata.readSlice(rdata.remaining()));
}

Expand Down
6 changes: 3 additions & 3 deletions src/records/dnssec/nsec3param.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ export class NSEC3PARAM extends RR {
salt!: Uint8Array;

unpackRdata(rdata: Slice): void {
this.hashAlg = rdata.readOctet();
this.flags = rdata.readOctet();
this.hashAlg = rdata.readUint8();
this.flags = rdata.readUint8();
this.iterations = rdata.readUint16();
this.salt = rdata.readUint8Array(rdata.readOctet());
this.salt = rdata.readUint8Array(rdata.readUint8());
}

packRdata(buf: Writer): number {
Expand Down
4 changes: 2 additions & 2 deletions src/records/dnssec/rrsig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export class RRSIG extends RR {
const bytesLeft = rdata.remaining();

this.typeCovered = rdata.readUint16();
this.algorithm = rdata.readOctet();
this.labels = rdata.readOctet();
this.algorithm = rdata.readUint8();
this.labels = rdata.readUint8();
this.originalTTL = rdata.readUint32();
this.expiration = rdata.readUint32();
this.inception = rdata.readUint32();
Expand Down
4 changes: 2 additions & 2 deletions src/records/dnssec/type-bitmaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export class TypeBitMaps {

let lastWindow = -1;
while (bin.remaining() >= 2) {
const window = bin.readOctet();
const len = bin.readOctet();
const window = bin.readUint8();
const len = bin.readUint8();

if (window <= lastWindow) {
// Blocks are present in the NSEC RR RDATA in increasing numerical order.
Expand Down
10 changes: 5 additions & 5 deletions src/records/hinfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CharactorString, Slice } from "../packet";
import { CharacterString, Slice } from "../packet";
import { RR } from "../rr";
import { Writer } from "../buffer";

Expand All @@ -22,16 +22,16 @@ export class HINFO extends RR {
/**
* A <character-string> which specifies the CPU type.
*/
cpu!: CharactorString;
cpu!: CharacterString;
/**
* A <character-string> which specifies the operating system type.
*/
os!: CharactorString;
os!: CharacterString;

unpackRdata(rdata: Slice): void {
const s = rdata.readSlice(this.header.rdlength);
this.cpu = CharactorString.unpack(s);
this.os = CharactorString.unpack(s);
this.cpu = CharacterString.unpack(s);
this.os = CharacterString.unpack(s);
}

packRdata(buf: Writer): number {
Expand Down
8 changes: 4 additions & 4 deletions src/records/loc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ export class LOC extends RR {
altitude!: Uint32;

unpackRdata(rdata: Slice): void {
this.version = rdata.readOctet();
this.size = rdata.readOctet();
this.horizPrecision = rdata.readOctet();
this.vertPrecision = rdata.readOctet();
this.version = rdata.readUint8();
this.size = rdata.readUint8();
this.horizPrecision = rdata.readUint8();
this.vertPrecision = rdata.readUint8();
this.latitude = rdata.readUint32();
this.longitude = rdata.readUint32();
this.altitude = rdata.readUint32();
Expand Down
4 changes: 2 additions & 2 deletions src/records/sshfp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export class SSHFP extends RR {
fingerprint!: Uint8Array;

unpackRdata(rdata: Slice): void {
this.algorithm = rdata.readOctet();
this.fpType = rdata.readOctet();
this.algorithm = rdata.readUint8();
this.fpType = rdata.readUint8();
this.fingerprint = rdata.readUint8Array(this.header.rdlength - 2);
}

Expand Down
8 changes: 4 additions & 4 deletions src/records/txt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Writer } from "../buffer";
import { CharactorString, Slice } from "../packet";
import { CharacterString, Slice } from "../packet";
import { RR } from "../rr";

/**
Expand All @@ -18,12 +18,12 @@ export class TXT extends RR {
/**
* One or more <character-string>s.
*/
content!: CharactorString[];
content!: CharacterString[];

unpackRdata(rdata: Slice): void {
this.content = new Array<CharactorString>();
this.content = new Array<CharacterString>();
while (rdata.remaining() > 0) {
this.content.push(CharactorString.unpack(rdata));
this.content.push(CharacterString.unpack(rdata));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/rr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Class, RRType, Uint16, Uint32, classAbbr } from "./types";
import { Writer } from "./buffer";

/**
* RRHeader represents the fields before the record data.
* Header represents the fields before the record data.
*
* The answer, authority, and additional sections all share the same format: a variable number of resource records,
* where the number of records is specified in the corresponding count field in the header. Each resource record has
Expand Down

0 comments on commit 222b330

Please sign in to comment.