Skip to content

Commit

Permalink
return json objects rather than json strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Hunts Chen committed May 27, 2023
1 parent 009785d commit d68a46e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dnspect/dns-ts",
"version": "0.2.0",
"version": "0.2.1",
"description": "DNS library in TypeScript",
"author": "Minghang Chen <[email protected]> (https://minghang.dev)",
"license": "MIT",
Expand Down
6 changes: 3 additions & 3 deletions src/question.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { Class, RRType } from "./types";
describe("test stringify", () => {
const q = new Question(FQDN.parse('example.com'), RRType.A, Class.IN);

it("should get dig-like", () => {
it("should get dig-like text", () => {
expect(q.toString()).to.be.equal(';example.com.\t\tIN\tA');
});

it("should get dns-json", () => {
expect(q.toJSON()).to.be.equal('{"name": "example.com.", "type": 1}');
it("should get dns-json object", () => {
expect(JSON.stringify(q.toJsonObject())).to.be.equal(JSON.stringify({ "name": "example.com.", "type": 1 }));
});
});
9 changes: 6 additions & 3 deletions src/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,15 @@ export class Question {
}

/**
* Generates textual representation in dns-json format.
* Returns JSON object of the question that will generate textual in application/dns-json format.
*
* @returns
*/
toJSON(): string {
return `{"name": "${this.qname.toString()}", "type": ${this.qclass}}`;
toJsonObject(): object {
return {
"name": this.qname.toString(),
"type": this.qclass,
};
}

pack(buf: Writer): number {
Expand Down
11 changes: 8 additions & 3 deletions src/records/txt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ describe("test stringify", () => {
const txt = new TXT(new Header('example.com', RRType.TXT, Class.IN, 300));
txt.content = [new CharacterString('abc'), new CharacterString('d "hi" e')];

it("should generate dns-json", () => {
expect(txt.toJSON()).to.be.equal(`{"name": "example.com.", "type": 16, "TTL": 300, "data": "\\"abc\\" \\"d \\"hi\\" e\\""}`);
it("should generate dns-json object", () => {
expect(JSON.stringify(txt.toJsonObject())).to.be.equal(JSON.stringify({
"name": "example.com.",
"type": 16,
"TTL": 300,
"data": "\"abc\" \"d \"hi\" e\"",
}));
});

it("should generate dig-like", () => {
it("should generate dig-like text", () => {
expect(txt.toString()).to.be.equal('example.com.\t\t300\tIN\tTXT\t"abc" "d "hi" e"');
});
});
11 changes: 8 additions & 3 deletions src/rr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,15 @@ export abstract class RR {
}

/**
* Returns texual representation of the RR in application/dns-json format.
* Returns JSON object of the RR that will generate textual in application/dns-json format.
*/
toJSON(): string {
return `{"name": "${this.header.name}", "type": ${this.header.type}, "TTL": ${this.header.ttl}, "data": ${JSON.stringify(this.dataString())}}`;
toJsonObject(): object {
return {
"name": this.header.name.toString(),
"type": this.header.type,
"TTL": this.header.ttl,
"data": this.dataString(),
};
}
}

Expand Down

0 comments on commit d68a46e

Please sign in to comment.