All files / lib/messages PingMessage.ts

100% Statements 22/22
100% Branches 0/0
100% Functions 4/4
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 841x   1x 1x                       1x           3x 3x   3x 3x   3x   3x 3x           14x                   14x             14x             2x         2x 2x 2x 2x 2x 2x                   2x      
import { BufferReader, BufferWriter } from '@node-dlc/bufio';
 
import { PONG_BYTE_THRESHOLD } from '../Constants';
import { MessageType } from '../MessageType';
import { IWireMessage } from './IWireMessage';
 
/**
 * In order to allow for the existence of long-lived TCP
 * connections,  at times it may be required that both ends keep
 * alive the TCP connection at the application level.
 *
 * The ping message is sent by an initiator and includes a value
 * for the number of pong bytes it expects to receive as
 * a reply. The ignored bits should be set to 0.
 */
export class PingMessage implements IWireMessage {
  /**
   * Deserialize a message and return a new instance of the
   * PingMessage type.
   */
  public static deserialize(payload: Buffer): PingMessage {
    const cursor = new BufferReader(payload);
    cursor.readUInt16BE();
 
    const instance = new PingMessage();
    instance.numPongBytes = cursor.readUInt16BE();
 
    const bytesLength = cursor.readUInt16BE();
 
    instance.ignored = cursor.readBytes(bytesLength);
    return instance;
  }
 
  /**
   * Ping message type is 18
   */
  public type: MessageType = MessageType.Ping;
 
  /**
   * The number of bytes that should be returned in the pong message.
   * Can be set to 65532 to indicate that no pong message should be
   * sent. Setting to any number below 65532 will require a pong
   * matching the corresponding number of bytes. If the reply
   * byteslen does not match this, you may terminate the channels
   * with the client.
   */
  public numPongBytes = 1;
 
  /**
   * Should set ignored to 0s. Must not set ignored to
   * sensitive data such as secrets or portions of initialized
   * memory.
   */
  public ignored: Buffer = Buffer.alloc(0);
 
  /**
   * Serialize the PingMessage and return a Buffer
   */
  public serialize(): Buffer {
    const len =
      2 + // type
      2 + // num_pong_bytes
      2 + // byteslen
      this.ignored.length;
 
    const br = new BufferWriter(Buffer.alloc(len));
    br.writeUInt16BE(this.type);
    br.writeUInt16BE(this.numPongBytes);
    br.writeUInt16BE(this.ignored.length);
    br.writeBytes(this.ignored);
    return br.toBuffer();
  }
 
  /**
   * triggersReply indicates if a pong message must send a reply.
   * Ping messages than are smaller than 65532 must send a reply
   * with the corresponding number of bytes. Above this value
   * no reply is necessary.  Refer to BOLT #1.
   */
  public get triggersReply(): boolean {
    return this.numPongBytes < PONG_BYTE_THRESHOLD;
  }
}