import assert from "assert" ; import { DNSLabelCoder } from "../DNSLabelCoder" ; import { DecodedData , RType } from "../DNSPacket" ; import { RecordRepresentation , ResourceRecord } from "../ResourceRecord" ; export class TXTRecord extends ResourceRecord { public static readonly DEFAULT_TTL = ResourceRecord . RR_DEFAULT_TTL ; readonly txt : Buffer [ ] ; constructor ( name , txt , flushFlag ? , ttl ? ) ; constructor ( header , txt ) ; constructor ( name , txt , flushFlag ? , ttl ? ) { if ( typeof name === "string" ) { super ( name , RType . TXT , ttl , flushFlag ) ; } else { assert ( name . type === RType . TXT ) ; super ( name ) ; } this . txt = txt ; } protected getRDataEncodingLength ( ) { let length = 0 ; for ( const buffer of this . txt ) { length += 1 + buffer . length ; assert ( buffer . length <= 255 , "One txt character-string can only have a length of 255 chars" ) ; } return length ; } protected encodeRData ( coder , buffer , offset ) { const oldOffset = offset ; for ( const txt of this . txt ) { buffer . writeUInt8 ( txt . length , offset ++ ) ; txt . copy ( buffer , offset ) ; offset += txt . length ; } return offset - oldOffset ; } public clone ( ) { return new TXTRecord ( this . getRecordRepresentation ( ) , this . txt ) ; } public dataAsString ( ) { return `[${ this . txt . map ( line => `${ line . toString ( "base64" ) }` ) . join ( "," ) }]` ; } public dataEquals ( record ) { if ( this . txt . length !== record . txt . length ) { return false ; } for ( let i = 0 ; i < this . txt . length ; i ++ ) { if ( this . txt [ i ] . toString ( "hex" ) !== record . txt [ i ] . toString ( "hex" ) ) { return false ; } } return true ; } public static decodeData ( coder , header , buffer , offset ) { const oldOffset = offset ; const txtData = [ ] ; while ( offset < buffer . length ) { const length = buffer . readUInt8 ( offset ++ ) ; txtData . push ( buffer . slice ( offset , offset + length ) ) ; offset += length ; } return { data : new TXTRecord ( header , txtData ) , readBytes : offset - oldOffset , } ; } }