Adding parameter for easy initialization to RecourceRecord constructor.
All checks were successful
the build was successful
All checks were successful
the build was successful
This commit is contained in:
parent
43f4cf358d
commit
9d3d9f8a80
1
lib/request.d.ts
vendored
1
lib/request.d.ts
vendored
@ -46,6 +46,7 @@ export declare class Question implements IMessageQuestion {
|
||||
serialize(): Buffer;
|
||||
}
|
||||
export declare class RecourceRecord implements MessageRecourceRecord {
|
||||
constructor(data?: Partial<MessageRecourceRecord>);
|
||||
/**
|
||||
* This value can be set to identify if specific record is already set
|
||||
*/
|
||||
|
@ -214,6 +214,13 @@ class Question {
|
||||
}
|
||||
exports.Question = Question;
|
||||
class RecourceRecord {
|
||||
constructor(data) {
|
||||
if (data) {
|
||||
for (let key in data) {
|
||||
this[key] = data[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
set TYPE(value) {
|
||||
if (value < 0 || value > 65535)
|
||||
throw new TypeError("TYPE Range: 0 - 65.535");
|
||||
@ -244,7 +251,8 @@ class RecourceRecord {
|
||||
serialize() {
|
||||
// TODO: Implement compression
|
||||
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);
|
||||
let offset = name.length;
|
||||
data.writeUInt16BE(this.TYPE, offset);
|
||||
@ -253,9 +261,9 @@ class RecourceRecord {
|
||||
offset += 2;
|
||||
data.writeUInt32BE(this._TTL, offset);
|
||||
offset += 4;
|
||||
data.writeUInt16BE(this.RDLENGTH, offset);
|
||||
data.writeUInt16BE(rdata.length, offset);
|
||||
offset += 2;
|
||||
this.RDATA.copy(data, offset, 0, this.RDLENGTH);
|
||||
rdata.copy(data, offset, 0, rdata.length);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because one or more lines are too long
12
lib/test.js
12
lib/test.js
@ -317,6 +317,18 @@ describe("parser", function () {
|
||||
let res = rr.serialize().toString("hex");
|
||||
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 () {
|
||||
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";
|
||||
|
File diff suppressed because one or more lines are too long
@ -254,6 +254,14 @@ export class Question implements IMessageQuestion {
|
||||
}
|
||||
|
||||
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
|
||||
*/
|
||||
@ -299,7 +307,9 @@ export class RecourceRecord implements MessageRecourceRecord {
|
||||
public serialize() {
|
||||
// TODO: Implement compression
|
||||
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);
|
||||
let offset = name.length;
|
||||
data.writeUInt16BE(this.TYPE, offset)
|
||||
@ -308,9 +318,9 @@ export class RecourceRecord implements MessageRecourceRecord {
|
||||
offset += 2
|
||||
data.writeUInt32BE(this._TTL, offset)
|
||||
offset += 4
|
||||
data.writeUInt16BE(this.RDLENGTH, offset)
|
||||
data.writeUInt16BE(rdata.length, offset)
|
||||
offset += 2
|
||||
this.RDATA.copy(data, offset, 0, this.RDLENGTH)
|
||||
rdata.copy(data, offset, 0, rdata.length)
|
||||
return data;
|
||||
}
|
||||
}
|
13
src/test.ts
13
src/test.ts
@ -327,6 +327,19 @@ describe("parser", function () {
|
||||
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 () {
|
||||
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"
|
||||
|
Loading…
Reference in New Issue
Block a user