Adding parameter for easy initialization to RecourceRecord constructor.
All checks were successful
the build was successful

This commit is contained in:
Fabian Stamm 2018-05-17 18:46:50 +02:00
parent 43f4cf358d
commit 9d3d9f8a80
7 changed files with 52 additions and 8 deletions

1
lib/request.d.ts vendored
View File

@ -46,6 +46,7 @@ export declare class Question implements IMessageQuestion {
serialize(): Buffer; serialize(): Buffer;
} }
export declare class RecourceRecord implements MessageRecourceRecord { export declare class RecourceRecord implements MessageRecourceRecord {
constructor(data?: Partial<MessageRecourceRecord>);
/** /**
* This value can be set to identify if specific record is already set * This value can be set to identify if specific record is already set
*/ */

View File

@ -214,6 +214,13 @@ class Question {
} }
exports.Question = Question; exports.Question = Question;
class RecourceRecord { class RecourceRecord {
constructor(data) {
if (data) {
for (let key in data) {
this[key] = data[key];
}
}
}
set TYPE(value) { set TYPE(value) {
if (value < 0 || value > 65535) if (value < 0 || value > 65535)
throw new TypeError("TYPE Range: 0 - 65.535"); throw new TypeError("TYPE Range: 0 - 65.535");
@ -244,7 +251,8 @@ class RecourceRecord {
serialize() { serialize() {
// TODO: Implement compression // TODO: Implement compression
let name = serializeName(this.NAME); let name = serializeName(this.NAME);
let data = Buffer.alloc(name.length + 10 + this.RDLENGTH); // For TYPE, CLASS, TTL, RLENGTH let rdata = this.RDATA;
let data = Buffer.alloc(name.length + 10 + rdata.length); // For TYPE, CLASS, TTL, RLENGTH
name.copy(data, 0, 0, name.length); name.copy(data, 0, 0, name.length);
let offset = name.length; let offset = name.length;
data.writeUInt16BE(this.TYPE, offset); data.writeUInt16BE(this.TYPE, offset);
@ -253,9 +261,9 @@ class RecourceRecord {
offset += 2; offset += 2;
data.writeUInt32BE(this._TTL, offset); data.writeUInt32BE(this._TTL, offset);
offset += 4; offset += 4;
data.writeUInt16BE(this.RDLENGTH, offset); data.writeUInt16BE(rdata.length, offset);
offset += 2; offset += 2;
this.RDATA.copy(data, offset, 0, this.RDLENGTH); rdata.copy(data, offset, 0, rdata.length);
return data; return data;
} }
} }

File diff suppressed because one or more lines are too long

View File

@ -317,6 +317,18 @@ describe("parser", function () {
let res = rr.serialize().toString("hex"); let res = rr.serialize().toString("hex");
chai_1.assert.equal(res, should.replace(/\s/g, "").toLowerCase(), "Serialization not working properly"); chai_1.assert.equal(res, should.replace(/\s/g, "").toLowerCase(), "Serialization not working properly");
}); });
it("recource record constructor value assign", function () {
let should = "07 6578616D706C65 03 636f6D 00 0001 0001 00000640 0004 0A000001";
let rr = new request_1.RecourceRecord({
CLASS: 1,
NAME: "example.com",
TTL: 1600,
TYPE: 1,
RDATA: fromHex("0A 00 00 01")
});
let res = rr.serialize().toString("hex");
chai_1.assert.equal(res, should.replace(/\s/g, "").toLowerCase(), "Serialization not working properly");
});
it("full response serialization", function () { it("full response serialization", function () {
let reqData = fromHex("E835 0100 0001 0000 0000 0000 07 6578616D706c65 03636F6D 00 0001 0001"); let reqData = fromHex("E835 0100 0001 0000 0000 0000 07 6578616D706c65 03636F6D 00 0001 0001");
let should = "E835 8580 0001000100000000 076578616D706C6503636F6D0000010001 07 6578616D706C65 03 636F6D 00 0001 0001 0000 0640 0004 0A000001"; let should = "E835 8580 0001000100000000 076578616D706C6503636F6D0000010001 07 6578616D706C65 03 636F6D 00 0001 0001 0000 0640 0004 0A000001";

File diff suppressed because one or more lines are too long

View File

@ -254,6 +254,14 @@ export class Question implements IMessageQuestion {
} }
export class RecourceRecord implements MessageRecourceRecord { export class RecourceRecord implements MessageRecourceRecord {
constructor(data?: Partial<MessageRecourceRecord>) {
if (data) {
for (let key in data) {
this[key] = data[key];
}
}
}
/** /**
* This value can be set to identify if specific record is already set * This value can be set to identify if specific record is already set
*/ */
@ -299,7 +307,9 @@ export class RecourceRecord implements MessageRecourceRecord {
public serialize() { public serialize() {
// TODO: Implement compression // TODO: Implement compression
let name = serializeName(this.NAME); let name = serializeName(this.NAME);
let data = Buffer.alloc(name.length + 10 + this.RDLENGTH) // For TYPE, CLASS, TTL, RLENGTH let rdata = this.RDATA;
let data = Buffer.alloc(name.length + 10 + rdata.length) // For TYPE, CLASS, TTL, RLENGTH
name.copy(data, 0, 0, name.length); name.copy(data, 0, 0, name.length);
let offset = name.length; let offset = name.length;
data.writeUInt16BE(this.TYPE, offset) data.writeUInt16BE(this.TYPE, offset)
@ -308,9 +318,9 @@ export class RecourceRecord implements MessageRecourceRecord {
offset += 2 offset += 2
data.writeUInt32BE(this._TTL, offset) data.writeUInt32BE(this._TTL, offset)
offset += 4 offset += 4
data.writeUInt16BE(this.RDLENGTH, offset) data.writeUInt16BE(rdata.length, offset)
offset += 2 offset += 2
this.RDATA.copy(data, offset, 0, this.RDLENGTH) rdata.copy(data, offset, 0, rdata.length)
return data; return data;
} }
} }

View File

@ -327,6 +327,19 @@ describe("parser", function () {
assert.equal(res, should.replace(/\s/g, "").toLowerCase(), "Serialization not working properly") assert.equal(res, should.replace(/\s/g, "").toLowerCase(), "Serialization not working properly")
}) })
it("recource record constructor value assign", function () {
let should = "07 6578616D706C65 03 636f6D 00 0001 0001 00000640 0004 0A000001"
let rr = new RecourceRecord({
CLASS: 1,
NAME: "example.com",
TTL: 1600,
TYPE: 1,
RDATA: fromHex("0A 00 00 01")
})
let res = rr.serialize().toString("hex")
assert.equal(res, should.replace(/\s/g, "").toLowerCase(), "Serialization not working properly")
})
it("full response serialization", function () { it("full response serialization", function () {
let reqData = fromHex("E835 0100 0001 0000 0000 0000 07 6578616D706c65 03636F6D 00 0001 0001"); let reqData = fromHex("E835 0100 0001 0000 0000 0000 07 6578616D706c65 03636F6D 00 0001 0001");
let should = "E835 8580 0001000100000000 076578616D706C6503636F6D0000010001 07 6578616D706C65 03 636F6D 00 0001 0001 0000 0640 0004 0A000001" let should = "E835 8580 0001000100000000 076578616D706C6503636F6D0000010001 07 6578616D706C65 03 636F6D 00 0001 0001 0000 0640 0004 0A000001"