@matter/general
Advanced tools
Comparing version 0.11.6-alpha.0-20241126-d77503abd to 0.11.6
@@ -266,3 +266,3 @@ "use strict"; | ||
const elements = []; | ||
while (elementsReader.getRemainingBytesCount() > 0) { | ||
while (elementsReader.remainingBytesCount > 0) { | ||
elements.push(this.decodeRec(elementsReader)); | ||
@@ -269,0 +269,0 @@ } |
@@ -6,2 +6,4 @@ /** | ||
*/ | ||
import { Endian } from "../util/Bytes.js"; | ||
import { DataReader } from "../util/DataReader.js"; | ||
/** | ||
@@ -70,19 +72,19 @@ * The maximum MDNS message size to usually fit into one UDP network MTU packet. Data are split into multiple messages | ||
static decode(message: Uint8Array): DnsMessage | undefined; | ||
private static decodeQuery; | ||
private static decodeRecord; | ||
private static decodeQName; | ||
static decodeQuery(reader: DataReader<Endian.Big>, message: Uint8Array): DnsQuery; | ||
static decodeRecord(reader: DataReader<Endian.Big>, message: Uint8Array): DnsRecord<any>; | ||
static decodeQName(reader: DataReader<Endian.Big>, message: Uint8Array, visited?: Set<number>): string; | ||
private static decodeRecordValue; | ||
private static decodeSrvRecord; | ||
private static decodeTxtRecord; | ||
private static decodeAaaaRecord; | ||
private static decodeARecord; | ||
static decodeSrvRecord(valueBytes: Uint8Array, message: Uint8Array): SrvRecordValue; | ||
static decodeTxtRecord(valueBytes: Uint8Array): string[]; | ||
static decodeAaaaRecord(valueBytes: Uint8Array): string; | ||
static decodeARecord(valueBytes: Uint8Array): string; | ||
static encode({ messageType, transactionId, queries, answers, authorities, additionalRecords, }: Partial<DnsMessagePartiallyPreEncoded>): Uint8Array; | ||
static encodeRecord(record: DnsRecord<any>): Uint8Array; | ||
private static encodeRecordValue; | ||
private static encodeARecord; | ||
private static encodeAaaaRecord; | ||
private static encodeTxtRecord; | ||
private static encodeSrvRecord; | ||
private static encodeQName; | ||
static encodeARecord(ip: string): Uint8Array; | ||
static encodeAaaaRecord(ip: string): Uint8Array; | ||
static encodeTxtRecord(entries: string[]): Uint8Array; | ||
static encodeSrvRecord({ priority, weight, port, target }: SrvRecordValue): Uint8Array; | ||
static encodeQName(qname: string): Uint8Array; | ||
} | ||
//# sourceMappingURL=DnsCodec.d.ts.map |
@@ -157,3 +157,7 @@ "use strict"; | ||
} | ||
static decodeQName(reader, message) { | ||
static decodeQName(reader, message, visited = /* @__PURE__ */ new Set()) { | ||
if (visited.has(reader.offset)) { | ||
throw new import_MatterError.UnexpectedDataError(`QNAME pointer loop detected. Index ${reader.offset} visited twice.`); | ||
} | ||
visited.add(reader.offset); | ||
const messageReader = new import_DataReader.DataReader(message, import_Bytes.Endian.Big); | ||
@@ -165,6 +169,14 @@ const qNameItems = new Array(); | ||
if ((itemLength & 192) !== 0) { | ||
if (reader.remainingBytesCount < 1) { | ||
throw new import_MatterError.UnexpectedDataError("QNAME pointer exceeds remaining bytes."); | ||
} | ||
const indexInMessage = reader.readUInt8() | (itemLength & 63) << 8; | ||
messageReader.setOffset(indexInMessage); | ||
qNameItems.push(this.decodeQName(messageReader, message)); | ||
if (indexInMessage >= message.length) { | ||
throw new import_MatterError.UnexpectedDataError("Invalid compressed QNAME pointer pointing to out of bounds index."); | ||
} | ||
messageReader.offset = indexInMessage; | ||
qNameItems.push(this.decodeQName(messageReader, message, visited)); | ||
break; | ||
} else if (reader.remainingBytesCount < itemLength + 1) { | ||
throw new import_MatterError.UnexpectedDataError(`QNAME item length ${itemLength} exceeds remaining bytes.`); | ||
} | ||
@@ -171,0 +183,0 @@ qNameItems.push(reader.readUtf8String(itemLength)); |
@@ -9,6 +9,3 @@ /** | ||
export declare class DataReader<E extends Endian> { | ||
private readonly buffer; | ||
private readonly littleEndian; | ||
private readonly dataView; | ||
private offset; | ||
#private; | ||
constructor(buffer: Uint8Array, endian: E); | ||
@@ -27,8 +24,9 @@ readUInt8(): number; | ||
readByteArray(length: number): Uint8Array; | ||
getRemainingBytesCount(): number; | ||
getRemainingBytes(): Uint8Array; | ||
getLength(): number; | ||
setOffset(offset: number): void; | ||
get remainingBytesCount(): number; | ||
get remainingBytes(): Uint8Array; | ||
get length(): number; | ||
set offset(offset: number); | ||
get offset(): number; | ||
private getOffsetAndAdvance; | ||
} | ||
//# sourceMappingURL=DataReader.d.ts.map |
@@ -31,68 +31,72 @@ "use strict"; | ||
class DataReader { | ||
#littleEndian; | ||
#dataView; | ||
#buffer; | ||
#offset = 0; | ||
constructor(buffer, endian) { | ||
this.buffer = buffer; | ||
this.dataView = import_Bytes.Bytes.dataViewOf(buffer); | ||
this.littleEndian = endian === import_Bytes.Endian.Little; | ||
this.#buffer = buffer; | ||
this.#dataView = import_Bytes.Bytes.dataViewOf(this.#buffer); | ||
this.#littleEndian = endian === import_Bytes.Endian.Little; | ||
} | ||
littleEndian; | ||
dataView; | ||
offset = 0; | ||
readUInt8() { | ||
return this.dataView.getUint8(this.getOffsetAndAdvance(1)); | ||
return this.#dataView.getUint8(this.getOffsetAndAdvance(1)); | ||
} | ||
readUInt16() { | ||
return this.dataView.getUint16(this.getOffsetAndAdvance(2), this.littleEndian); | ||
return this.#dataView.getUint16(this.getOffsetAndAdvance(2), this.#littleEndian); | ||
} | ||
readUInt32() { | ||
return this.dataView.getUint32(this.getOffsetAndAdvance(4), this.littleEndian); | ||
return this.#dataView.getUint32(this.getOffsetAndAdvance(4), this.#littleEndian); | ||
} | ||
readUInt64() { | ||
return this.dataView.getBigUint64(this.getOffsetAndAdvance(8), this.littleEndian); | ||
return this.#dataView.getBigUint64(this.getOffsetAndAdvance(8), this.#littleEndian); | ||
} | ||
readInt8() { | ||
return this.dataView.getInt8(this.getOffsetAndAdvance(1)); | ||
return this.#dataView.getInt8(this.getOffsetAndAdvance(1)); | ||
} | ||
readInt16() { | ||
return this.dataView.getInt16(this.getOffsetAndAdvance(2), this.littleEndian); | ||
return this.#dataView.getInt16(this.getOffsetAndAdvance(2), this.#littleEndian); | ||
} | ||
readInt32() { | ||
return this.dataView.getInt32(this.getOffsetAndAdvance(4), this.littleEndian); | ||
return this.#dataView.getInt32(this.getOffsetAndAdvance(4), this.#littleEndian); | ||
} | ||
readInt64() { | ||
return this.dataView.getBigInt64(this.getOffsetAndAdvance(8), this.littleEndian); | ||
return this.#dataView.getBigInt64(this.getOffsetAndAdvance(8), this.#littleEndian); | ||
} | ||
readFloat() { | ||
return this.dataView.getFloat32(this.getOffsetAndAdvance(4), this.littleEndian); | ||
return this.#dataView.getFloat32(this.getOffsetAndAdvance(4), this.#littleEndian); | ||
} | ||
readDouble() { | ||
return this.dataView.getFloat64(this.getOffsetAndAdvance(8), this.littleEndian); | ||
return this.#dataView.getFloat64(this.getOffsetAndAdvance(8), this.#littleEndian); | ||
} | ||
readUtf8String(length) { | ||
const offset = this.getOffsetAndAdvance(length); | ||
return new TextDecoder().decode(this.buffer.subarray(offset, offset + length)); | ||
return new TextDecoder().decode(this.#buffer.subarray(offset, offset + length)); | ||
} | ||
readByteArray(length) { | ||
const offset = this.getOffsetAndAdvance(length); | ||
return this.buffer.subarray(offset, offset + length); | ||
return this.#buffer.subarray(offset, offset + length); | ||
} | ||
getRemainingBytesCount() { | ||
return this.dataView.byteLength - this.offset; | ||
get remainingBytesCount() { | ||
return this.#dataView.byteLength - this.#offset; | ||
} | ||
getRemainingBytes() { | ||
return this.buffer.subarray(this.offset); | ||
get remainingBytes() { | ||
return this.#buffer.subarray(this.#offset); | ||
} | ||
getLength() { | ||
return this.dataView.byteLength; | ||
get length() { | ||
return this.#dataView.byteLength; | ||
} | ||
setOffset(offset) { | ||
if (offset > this.dataView.byteLength) { | ||
set offset(offset) { | ||
if (offset > this.#dataView.byteLength) { | ||
throw new Error(`Offset ${offset} is out of bounds.`); | ||
} | ||
this.offset = offset; | ||
this.#offset = offset; | ||
} | ||
get offset() { | ||
return this.#offset; | ||
} | ||
getOffsetAndAdvance(size) { | ||
const result = this.offset; | ||
this.offset += size; | ||
if (this.offset > this.dataView.byteLength) { | ||
this.offset = this.dataView.byteLength; | ||
const result = this.#offset; | ||
this.#offset += size; | ||
if (this.#offset > this.#dataView.byteLength) { | ||
this.#offset = this.#dataView.byteLength; | ||
} | ||
@@ -99,0 +103,0 @@ return result; |
@@ -233,3 +233,3 @@ /** | ||
const elements = []; | ||
while (elementsReader.getRemainingBytesCount() > 0) { | ||
while (elementsReader.remainingBytesCount > 0) { | ||
elements.push(this.decodeRec(elementsReader)); | ||
@@ -236,0 +236,0 @@ } |
@@ -6,2 +6,4 @@ /** | ||
*/ | ||
import { Endian } from "../util/Bytes.js"; | ||
import { DataReader } from "../util/DataReader.js"; | ||
/** | ||
@@ -70,19 +72,19 @@ * The maximum MDNS message size to usually fit into one UDP network MTU packet. Data are split into multiple messages | ||
static decode(message: Uint8Array): DnsMessage | undefined; | ||
private static decodeQuery; | ||
private static decodeRecord; | ||
private static decodeQName; | ||
static decodeQuery(reader: DataReader<Endian.Big>, message: Uint8Array): DnsQuery; | ||
static decodeRecord(reader: DataReader<Endian.Big>, message: Uint8Array): DnsRecord<any>; | ||
static decodeQName(reader: DataReader<Endian.Big>, message: Uint8Array, visited?: Set<number>): string; | ||
private static decodeRecordValue; | ||
private static decodeSrvRecord; | ||
private static decodeTxtRecord; | ||
private static decodeAaaaRecord; | ||
private static decodeARecord; | ||
static decodeSrvRecord(valueBytes: Uint8Array, message: Uint8Array): SrvRecordValue; | ||
static decodeTxtRecord(valueBytes: Uint8Array): string[]; | ||
static decodeAaaaRecord(valueBytes: Uint8Array): string; | ||
static decodeARecord(valueBytes: Uint8Array): string; | ||
static encode({ messageType, transactionId, queries, answers, authorities, additionalRecords, }: Partial<DnsMessagePartiallyPreEncoded>): Uint8Array; | ||
static encodeRecord(record: DnsRecord<any>): Uint8Array; | ||
private static encodeRecordValue; | ||
private static encodeARecord; | ||
private static encodeAaaaRecord; | ||
private static encodeTxtRecord; | ||
private static encodeSrvRecord; | ||
private static encodeQName; | ||
static encodeARecord(ip: string): Uint8Array; | ||
static encodeAaaaRecord(ip: string): Uint8Array; | ||
static encodeTxtRecord(entries: string[]): Uint8Array; | ||
static encodeSrvRecord({ priority, weight, port, target }: SrvRecordValue): Uint8Array; | ||
static encodeQName(qname: string): Uint8Array; | ||
} | ||
//# sourceMappingURL=DnsCodec.d.ts.map |
@@ -125,3 +125,7 @@ /** | ||
} | ||
static decodeQName(reader, message) { | ||
static decodeQName(reader, message, visited = /* @__PURE__ */ new Set()) { | ||
if (visited.has(reader.offset)) { | ||
throw new UnexpectedDataError(`QNAME pointer loop detected. Index ${reader.offset} visited twice.`); | ||
} | ||
visited.add(reader.offset); | ||
const messageReader = new DataReader(message, Endian.Big); | ||
@@ -133,6 +137,14 @@ const qNameItems = new Array(); | ||
if ((itemLength & 192) !== 0) { | ||
if (reader.remainingBytesCount < 1) { | ||
throw new UnexpectedDataError("QNAME pointer exceeds remaining bytes."); | ||
} | ||
const indexInMessage = reader.readUInt8() | (itemLength & 63) << 8; | ||
messageReader.setOffset(indexInMessage); | ||
qNameItems.push(this.decodeQName(messageReader, message)); | ||
if (indexInMessage >= message.length) { | ||
throw new UnexpectedDataError("Invalid compressed QNAME pointer pointing to out of bounds index."); | ||
} | ||
messageReader.offset = indexInMessage; | ||
qNameItems.push(this.decodeQName(messageReader, message, visited)); | ||
break; | ||
} else if (reader.remainingBytesCount < itemLength + 1) { | ||
throw new UnexpectedDataError(`QNAME item length ${itemLength} exceeds remaining bytes.`); | ||
} | ||
@@ -139,0 +151,0 @@ qNameItems.push(reader.readUtf8String(itemLength)); |
@@ -9,6 +9,3 @@ /** | ||
export declare class DataReader<E extends Endian> { | ||
private readonly buffer; | ||
private readonly littleEndian; | ||
private readonly dataView; | ||
private offset; | ||
#private; | ||
constructor(buffer: Uint8Array, endian: E); | ||
@@ -27,8 +24,9 @@ readUInt8(): number; | ||
readByteArray(length: number): Uint8Array; | ||
getRemainingBytesCount(): number; | ||
getRemainingBytes(): Uint8Array; | ||
getLength(): number; | ||
setOffset(offset: number): void; | ||
get remainingBytesCount(): number; | ||
get remainingBytes(): Uint8Array; | ||
get length(): number; | ||
set offset(offset: number); | ||
get offset(): number; | ||
private getOffsetAndAdvance; | ||
} | ||
//# sourceMappingURL=DataReader.d.ts.map |
@@ -8,68 +8,72 @@ /** | ||
class DataReader { | ||
#littleEndian; | ||
#dataView; | ||
#buffer; | ||
#offset = 0; | ||
constructor(buffer, endian) { | ||
this.buffer = buffer; | ||
this.dataView = Bytes.dataViewOf(buffer); | ||
this.littleEndian = endian === Endian.Little; | ||
this.#buffer = buffer; | ||
this.#dataView = Bytes.dataViewOf(this.#buffer); | ||
this.#littleEndian = endian === Endian.Little; | ||
} | ||
littleEndian; | ||
dataView; | ||
offset = 0; | ||
readUInt8() { | ||
return this.dataView.getUint8(this.getOffsetAndAdvance(1)); | ||
return this.#dataView.getUint8(this.getOffsetAndAdvance(1)); | ||
} | ||
readUInt16() { | ||
return this.dataView.getUint16(this.getOffsetAndAdvance(2), this.littleEndian); | ||
return this.#dataView.getUint16(this.getOffsetAndAdvance(2), this.#littleEndian); | ||
} | ||
readUInt32() { | ||
return this.dataView.getUint32(this.getOffsetAndAdvance(4), this.littleEndian); | ||
return this.#dataView.getUint32(this.getOffsetAndAdvance(4), this.#littleEndian); | ||
} | ||
readUInt64() { | ||
return this.dataView.getBigUint64(this.getOffsetAndAdvance(8), this.littleEndian); | ||
return this.#dataView.getBigUint64(this.getOffsetAndAdvance(8), this.#littleEndian); | ||
} | ||
readInt8() { | ||
return this.dataView.getInt8(this.getOffsetAndAdvance(1)); | ||
return this.#dataView.getInt8(this.getOffsetAndAdvance(1)); | ||
} | ||
readInt16() { | ||
return this.dataView.getInt16(this.getOffsetAndAdvance(2), this.littleEndian); | ||
return this.#dataView.getInt16(this.getOffsetAndAdvance(2), this.#littleEndian); | ||
} | ||
readInt32() { | ||
return this.dataView.getInt32(this.getOffsetAndAdvance(4), this.littleEndian); | ||
return this.#dataView.getInt32(this.getOffsetAndAdvance(4), this.#littleEndian); | ||
} | ||
readInt64() { | ||
return this.dataView.getBigInt64(this.getOffsetAndAdvance(8), this.littleEndian); | ||
return this.#dataView.getBigInt64(this.getOffsetAndAdvance(8), this.#littleEndian); | ||
} | ||
readFloat() { | ||
return this.dataView.getFloat32(this.getOffsetAndAdvance(4), this.littleEndian); | ||
return this.#dataView.getFloat32(this.getOffsetAndAdvance(4), this.#littleEndian); | ||
} | ||
readDouble() { | ||
return this.dataView.getFloat64(this.getOffsetAndAdvance(8), this.littleEndian); | ||
return this.#dataView.getFloat64(this.getOffsetAndAdvance(8), this.#littleEndian); | ||
} | ||
readUtf8String(length) { | ||
const offset = this.getOffsetAndAdvance(length); | ||
return new TextDecoder().decode(this.buffer.subarray(offset, offset + length)); | ||
return new TextDecoder().decode(this.#buffer.subarray(offset, offset + length)); | ||
} | ||
readByteArray(length) { | ||
const offset = this.getOffsetAndAdvance(length); | ||
return this.buffer.subarray(offset, offset + length); | ||
return this.#buffer.subarray(offset, offset + length); | ||
} | ||
getRemainingBytesCount() { | ||
return this.dataView.byteLength - this.offset; | ||
get remainingBytesCount() { | ||
return this.#dataView.byteLength - this.#offset; | ||
} | ||
getRemainingBytes() { | ||
return this.buffer.subarray(this.offset); | ||
get remainingBytes() { | ||
return this.#buffer.subarray(this.#offset); | ||
} | ||
getLength() { | ||
return this.dataView.byteLength; | ||
get length() { | ||
return this.#dataView.byteLength; | ||
} | ||
setOffset(offset) { | ||
if (offset > this.dataView.byteLength) { | ||
set offset(offset) { | ||
if (offset > this.#dataView.byteLength) { | ||
throw new Error(`Offset ${offset} is out of bounds.`); | ||
} | ||
this.offset = offset; | ||
this.#offset = offset; | ||
} | ||
get offset() { | ||
return this.#offset; | ||
} | ||
getOffsetAndAdvance(size) { | ||
const result = this.offset; | ||
this.offset += size; | ||
if (this.offset > this.dataView.byteLength) { | ||
this.offset = this.dataView.byteLength; | ||
const result = this.#offset; | ||
this.#offset += size; | ||
if (this.#offset > this.#dataView.byteLength) { | ||
this.#offset = this.#dataView.byteLength; | ||
} | ||
@@ -76,0 +80,0 @@ return result; |
{ | ||
"name": "@matter/general", | ||
"version": "0.11.6-alpha.0-20241126-d77503abd", | ||
"version": "0.11.6", | ||
"description": "Non-Matter support for Matter.js", | ||
@@ -39,3 +39,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"@matter/testing": "0.11.6-alpha.0-20241126-d77503abd" | ||
"@matter/testing": "0.11.6" | ||
}, | ||
@@ -42,0 +42,0 @@ "files": [ |
@@ -280,3 +280,3 @@ /** | ||
const elements: DerNode[] = []; | ||
while (elementsReader.getRemainingBytesCount() > 0) { | ||
while (elementsReader.remainingBytesCount > 0) { | ||
elements.push(this.decodeRec(elementsReader)); | ||
@@ -283,0 +283,0 @@ } |
@@ -156,3 +156,3 @@ /** | ||
private static decodeQuery(reader: DataReader<Endian.Big>, message: Uint8Array): DnsQuery { | ||
static decodeQuery(reader: DataReader<Endian.Big>, message: Uint8Array): DnsQuery { | ||
const name = this.decodeQName(reader, message); | ||
@@ -166,3 +166,3 @@ const recordType = reader.readUInt16(); | ||
private static decodeRecord(reader: DataReader<Endian.Big>, message: Uint8Array): DnsRecord<any> { | ||
static decodeRecord(reader: DataReader<Endian.Big>, message: Uint8Array): DnsRecord<any> { | ||
const name = this.decodeQName(reader, message); | ||
@@ -180,3 +180,8 @@ const recordType = reader.readUInt16(); | ||
private static decodeQName(reader: DataReader<Endian.Big>, message: Uint8Array) { | ||
static decodeQName(reader: DataReader<Endian.Big>, message: Uint8Array, visited = new Set<number>()): string { | ||
if (visited.has(reader.offset)) { | ||
throw new UnexpectedDataError(`QNAME pointer loop detected. Index ${reader.offset} visited twice.`); | ||
} | ||
visited.add(reader.offset); | ||
const messageReader = new DataReader(message, Endian.Big); | ||
@@ -188,7 +193,16 @@ const qNameItems = new Array<string>(); | ||
if ((itemLength & 0xc0) !== 0) { | ||
if (reader.remainingBytesCount < 1) { | ||
throw new UnexpectedDataError("QNAME pointer exceeds remaining bytes."); | ||
} | ||
// Compressed Qname | ||
const indexInMessage = reader.readUInt8() | ((itemLength & 0x3f) << 8); | ||
messageReader.setOffset(indexInMessage); | ||
qNameItems.push(this.decodeQName(messageReader, message)); | ||
if (indexInMessage >= message.length) { | ||
throw new UnexpectedDataError("Invalid compressed QNAME pointer pointing to out of bounds index."); | ||
} | ||
messageReader.offset = indexInMessage; | ||
qNameItems.push(this.decodeQName(messageReader, message, visited)); | ||
break; | ||
} else if (reader.remainingBytesCount < itemLength + 1) { | ||
// There needs to be a string end 0x00 at the end, so + 1 | ||
throw new UnexpectedDataError(`QNAME item length ${itemLength} exceeds remaining bytes.`); | ||
} | ||
@@ -218,3 +232,3 @@ qNameItems.push(reader.readUtf8String(itemLength)); | ||
private static decodeSrvRecord(valueBytes: Uint8Array, message: Uint8Array): SrvRecordValue { | ||
static decodeSrvRecord(valueBytes: Uint8Array, message: Uint8Array): SrvRecordValue { | ||
const reader = new DataReader(valueBytes, Endian.Big); | ||
@@ -228,3 +242,3 @@ const priority = reader.readUInt16(); | ||
private static decodeTxtRecord(valueBytes: Uint8Array): string[] { | ||
static decodeTxtRecord(valueBytes: Uint8Array): string[] { | ||
const reader = new DataReader(valueBytes, Endian.Big); | ||
@@ -241,3 +255,3 @@ const result = new Array<string>(); | ||
private static decodeAaaaRecord(valueBytes: Uint8Array): string { | ||
static decodeAaaaRecord(valueBytes: Uint8Array): string { | ||
const reader = new DataReader(valueBytes, Endian.Big); | ||
@@ -268,3 +282,3 @@ const ipItems = new Array<string>(); | ||
private static decodeARecord(valueBytes: Uint8Array): string { | ||
static decodeARecord(valueBytes: Uint8Array): string { | ||
const reader = new DataReader(valueBytes, Endian.Big); | ||
@@ -345,3 +359,3 @@ const ipItems = new Array<string>(); | ||
private static encodeARecord(ip: string) { | ||
static encodeARecord(ip: string) { | ||
if (!isIPv4(ip)) throw new UnexpectedDataError(`Invalid A Record value: ${ip}`); | ||
@@ -355,3 +369,3 @@ const writer = new DataWriter(Endian.Big); | ||
private static encodeAaaaRecord(ip: string) { | ||
static encodeAaaaRecord(ip: string) { | ||
if (!isIPv6(ip)) throw new UnexpectedDataError(`Invalid AAAA Record value: ${ip}`); | ||
@@ -372,3 +386,3 @@ const writer = new DataWriter(Endian.Big); | ||
private static encodeTxtRecord(entries: string[]) { | ||
static encodeTxtRecord(entries: string[]) { | ||
const writer = new DataWriter(Endian.Big); | ||
@@ -383,3 +397,3 @@ entries.forEach(entry => { | ||
private static encodeSrvRecord({ priority, weight, port, target }: SrvRecordValue) { | ||
static encodeSrvRecord({ priority, weight, port, target }: SrvRecordValue) { | ||
const writer = new DataWriter(Endian.Big); | ||
@@ -393,5 +407,6 @@ writer.writeUInt16(priority); | ||
private static encodeQName(qname: string) { | ||
static encodeQName(qname: string) { | ||
const writer = new DataWriter(Endian.Big); | ||
if (qname.length > 0) { | ||
// TODO: Implement compression | ||
qname.split(".").forEach(label => { | ||
@@ -398,0 +413,0 @@ const labelData = Bytes.fromString(label); |
@@ -11,52 +11,51 @@ /** | ||
export class DataReader<E extends Endian> { | ||
private readonly littleEndian: boolean; | ||
private readonly dataView: DataView; | ||
private offset = 0; | ||
readonly #littleEndian: boolean; | ||
readonly #dataView: DataView; | ||
readonly #buffer: Uint8Array; | ||
#offset = 0; | ||
constructor( | ||
private readonly buffer: Uint8Array, | ||
endian: E, | ||
) { | ||
this.dataView = Bytes.dataViewOf(buffer); | ||
this.littleEndian = endian === Endian.Little; | ||
constructor(buffer: Uint8Array, endian: E) { | ||
this.#buffer = buffer; | ||
this.#dataView = Bytes.dataViewOf(this.#buffer); | ||
this.#littleEndian = endian === Endian.Little; | ||
} | ||
readUInt8() { | ||
return this.dataView.getUint8(this.getOffsetAndAdvance(1)); | ||
return this.#dataView.getUint8(this.getOffsetAndAdvance(1)); | ||
} | ||
readUInt16() { | ||
return this.dataView.getUint16(this.getOffsetAndAdvance(2), this.littleEndian); | ||
return this.#dataView.getUint16(this.getOffsetAndAdvance(2), this.#littleEndian); | ||
} | ||
readUInt32() { | ||
return this.dataView.getUint32(this.getOffsetAndAdvance(4), this.littleEndian); | ||
return this.#dataView.getUint32(this.getOffsetAndAdvance(4), this.#littleEndian); | ||
} | ||
readUInt64() { | ||
return this.dataView.getBigUint64(this.getOffsetAndAdvance(8), this.littleEndian); | ||
return this.#dataView.getBigUint64(this.getOffsetAndAdvance(8), this.#littleEndian); | ||
} | ||
readInt8() { | ||
return this.dataView.getInt8(this.getOffsetAndAdvance(1)); | ||
return this.#dataView.getInt8(this.getOffsetAndAdvance(1)); | ||
} | ||
readInt16() { | ||
return this.dataView.getInt16(this.getOffsetAndAdvance(2), this.littleEndian); | ||
return this.#dataView.getInt16(this.getOffsetAndAdvance(2), this.#littleEndian); | ||
} | ||
readInt32() { | ||
return this.dataView.getInt32(this.getOffsetAndAdvance(4), this.littleEndian); | ||
return this.#dataView.getInt32(this.getOffsetAndAdvance(4), this.#littleEndian); | ||
} | ||
readInt64() { | ||
return this.dataView.getBigInt64(this.getOffsetAndAdvance(8), this.littleEndian); | ||
return this.#dataView.getBigInt64(this.getOffsetAndAdvance(8), this.#littleEndian); | ||
} | ||
readFloat() { | ||
return this.dataView.getFloat32(this.getOffsetAndAdvance(4), this.littleEndian); | ||
return this.#dataView.getFloat32(this.getOffsetAndAdvance(4), this.#littleEndian); | ||
} | ||
readDouble() { | ||
return this.dataView.getFloat64(this.getOffsetAndAdvance(8), this.littleEndian); | ||
return this.#dataView.getFloat64(this.getOffsetAndAdvance(8), this.#littleEndian); | ||
} | ||
@@ -66,3 +65,3 @@ | ||
const offset = this.getOffsetAndAdvance(length); | ||
return new TextDecoder().decode(this.buffer.subarray(offset, offset + length)); | ||
return new TextDecoder().decode(this.#buffer.subarray(offset, offset + length)); | ||
} | ||
@@ -72,29 +71,33 @@ | ||
const offset = this.getOffsetAndAdvance(length); | ||
return this.buffer.subarray(offset, offset + length); | ||
return this.#buffer.subarray(offset, offset + length); | ||
} | ||
getRemainingBytesCount() { | ||
return this.dataView.byteLength - this.offset; | ||
get remainingBytesCount() { | ||
return this.#dataView.byteLength - this.#offset; | ||
} | ||
getRemainingBytes() { | ||
return this.buffer.subarray(this.offset); | ||
get remainingBytes() { | ||
return this.#buffer.subarray(this.#offset); | ||
} | ||
getLength() { | ||
return this.dataView.byteLength; | ||
get length() { | ||
return this.#dataView.byteLength; | ||
} | ||
setOffset(offset: number) { | ||
if (offset > this.dataView.byteLength) { | ||
set offset(offset: number) { | ||
if (offset > this.#dataView.byteLength) { | ||
throw new Error(`Offset ${offset} is out of bounds.`); | ||
} | ||
this.offset = offset; | ||
this.#offset = offset; | ||
} | ||
get offset() { | ||
return this.#offset; | ||
} | ||
private getOffsetAndAdvance(size: number) { | ||
const result = this.offset; | ||
this.offset += size; | ||
if (this.offset > this.dataView.byteLength) { | ||
this.offset = this.dataView.byteLength; | ||
const result = this.#offset; | ||
this.#offset += size; | ||
if (this.#offset > this.#dataView.byteLength) { | ||
this.#offset = this.#dataView.byteLength; | ||
} | ||
@@ -101,0 +104,0 @@ return result; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1585940
32058