Skip to content

AlexXanderGrib/ts-struct

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TS Struct

Typed binary serializer

Test Status Downloads last commit codecov GitHub ts-struct Known Vulnerabilities Quality npm license MIT Size

📦 Installation

  • Using npm
    npm i ts-struct
  • Using Yarn
    yarn add ts-struct
  • Using pnpm
    pnpm add ts-struct

⚙️ Usage

Model raw packets

import { struct, types } from "ts-struct";

export enum EtherType {
  Ether = 0x00_01,
  IPv4 = 0x08_00,
  ARP = 0x08_06,
  IPv6 = 0x86_dd
}

export class MacAddress extends struct.bigEndian({
  value: types.bytes(6)
}) {
  static readonly TYPE = EtherType.Ether;
  static readonly DEFAULT = MacAddress.parse("00:00:00:00:00:00");

  static parse(address: string) {
    const raw = address.replace(/[:-]/g, "");
    const value = Buffer.from(raw, "hex").slice(0, MacAddress.SIZE);

    return new this({ value });
  }

  get address() {
    return [...this.data.value]
      .map((value) => value.toString(16).padStart(2, "0"))
      .join(":");
  }
}

export class IPv4Address extends struct.bigEndian({
  value: types.bytes(4)
}) {
  static readonly TYPE = EtherType.IPv4;
  static readonly DEFAULT = IPv4Address.from("0.0.0.0");

  static from(address: string) {
    const raw = address.split(".").map((part) => Number.parseInt(part, 10));
    const value = Buffer.from(raw).slice(0, IPv4Address.SIZE);

    return new this({ value });
  }
}

export enum ARPOperation {
  REQUEST = 0x00_01,
  REPLY = 0x00_02
}

const net = Object.freeze({
  ether: types.enum<EtherType>(types.uint16, EtherType, EtherType.Ether),
  mac: types.any(MacAddress, MacAddress.DEFAULT.data),
  ipv4: types.any(IPv4Address, IPv4Address.DEFAULT.data),
  arpOperation: types.enum<ARPOperation>(
    types.uint16,
    ARPOperation,
    ARPOperation.REPLY
  )
});

export const Ethernet = struct.bigEndian({
  source: net.mac,
  target: net.mac,
  type: net.ether
});

About

Typed binary data serializer/deserializer

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published