import BufferList from 'bl' ; import { CoapPacket , CoapRequestParams , OptionValue } from '../models/models' ; import { genAck , toCode , setOption } from './helpers' ; import RetrySend from './retry_send' ; import { SegmentedTransmission } from './segmentation' ; import IncomingMessage from './incoming_message' ; import { OptionName , Packet } from 'coap-packet' ; export default class OutgoingMessage extends BufferList { _packet : Packet ; _ackTimer : NodeJS . Timeout | null ; _send : ( req : OutgoingMessage , packet : Packet ) => void ; statusCode : string ; code : string ; multicast : boolean ; _request : CoapPacket ; url : CoapRequestParams ; sender : RetrySend ; _totalPayload : Buffer ; multicastTimer : NodeJS . Timeout ; segmentedSender ? : SegmentedTransmission ; response : IncomingMessage ; constructor ( request , send ) { super ( ) ; this . _packet = { messageId : request . messageId , token : request . token , options : [ ] , confirmable : false , ack : false , reset : false } ; if ( request . confirmable === true ) { this . _packet . ack = true ; this . _ackTimer = setTimeout ( ( ) => { send ( this , genAck ( request ) ) ; this . _packet . confirmable = true ; this . _packet . ack = false ; delete this . _packet . messageId ; this . _ackTimer = null ; } , request . piggybackReplyMs ) ; } this . _send = send ; this . statusCode = '' ; this . code = '' ; } end ( a ? , b ? ) { super . end ( a , b ) ; const packet = this . _packet ; const code = this . code !== '' ? this . code : this . statusCode ; packet . code = toCode ( code ) ; packet . payload = this as unknown as Buffer ; if ( this . _ackTimer != null ) { clearTimeout ( this . _ackTimer ) ; } this . _send ( this , packet ) ; delete this . _packet . payload ; return this ; } reset ( ) { super . end ( ) ; const packet = this . _packet ; packet . code = '0.00' ; packet . payload = Buffer . alloc ( 0 ) ; packet . reset = true ; packet . ack = false ; packet . token = Buffer . alloc ( 0 ) ; if ( this . _ackTimer != null ) { clearTimeout ( this . _ackTimer ) ; } this . _send ( this , packet ) ; delete this . _packet . payload ; return this ; } param { OptionName | number } code param { Partial < Record < OptionName , OptionValue > > } headers writeHead ( code , headers ) { const packet = this . _packet ; packet . code = String ( code ) . replace ( /(^\d[^.])/ , '$1.' ) ; for ( const [ header , value ] of Object . entries ( headers ) ) { this . setOption ( header as OptionName , value ) ; } } setOption ( name , values ) { setOption ( this . _packet , name , values ) ; return this ; } setHeader ( name , values ) { return this . setOption ( name , values ) ; } }