All files / lib/serialize/address serializeIPv4.ts

100% Statements 9/9
100% Branches 0/0
100% Functions 1/1
100% Lines 9/9

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 211x     1x           1x 78x   78x   78x 78x 78x   78x    
import { BufferWriter } from '@node-dlc/bufio';
 
import { AddressIPv4 } from '../../domain/AddressIPv4';
import { ipv4StringToBuffer } from './ipv4StringToBuffer';
 
/**
 * Serializes an IPv4 address in a Buffer that can be sent
 * over the wire.
 */
export function serializeIPv4(address: AddressIPv4): Buffer {
  const writer = new BufferWriter(Buffer.alloc(7));
 
  const hostBytes = ipv4StringToBuffer(address.host);
 
  writer.writeUInt8(address.type);
  writer.writeBytes(hostBytes);
  writer.writeUInt16BE(address.port);
 
  return writer.toBuffer();
}