0.6.6 • Published 11 months ago

@rtf-dm/protocol v0.6.6

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

Installation

nmp i @rtf-dm/protocol

Simple and small package to describe packets

Usage:

import { Packet, encode, decode, Schema } from '@rtf-dm/protocol';

//1. Describe your packet payload type:

type TestPacketPayload = {
  ID: string;
  numberField1b: number;
  numberField2b: number;
  numberField4b: number;
  numberField2bBE: number;
  numberField4bBE: number;
  arrayField1b: number[];
  arrayField2bLe: number[];
  arrayField2bBe: number[];
  filler: number;
  filler2: number;
  arrayField4bLe: number[];
  arrayField4bBe: number[];
};

/*2. Describe encoding schema:
 *    Note that order of schema fields make sense!
 *    It means if 'ID' field first in array and have a length 5
 *    first five bytes in packet will have value of 'ID' payload;
 * */

const schema = new Schema<TestPacketPayload>([
  ['ID', { size: 1, length: 5, type: 'string', encoding: 'ascii' }],
  ['numberField1b', { size: 1, type: 'number' }],
  ['numberField2b', { size: 2, type: 'number', byteOrder: 'LE' }],
  ['numberField4b', { size: 4, type: 'number', byteOrder: 'LE' }],
  ['numberField2bBE', { size: 2, type: 'number', byteOrder: 'BE' }],
  ['numberField4bBE', { size: 4, type: 'number', byteOrder: 'BE' }],
  ['arrayField1b', { length: 4, type: 'array', size: 1 }],
  ['arrayField2bLe', { length: 4, type: 'array', size: 2, byteOrder: 'LE' }],
  ['arrayField2bBe', { length: 4, type: 'array', size: 2, byteOrder: 'BE' }],
  ['filler', { size: 1, type: 'number' }],
  ['filler2', { size: 1, type: 'number' }],
  ['arrayField4bBe', { length: 4, type: 'array', size: 4, byteOrder: 'BE' }],
  ['arrayField4bLe', { length: 4, type: 'array', size: 4, byteOrder: 'LE' }],
]);

//3. Create a new class that extends Packet class:
class TestPacket extends Packet<TestPacketPayload> {
  constructor(payload: TestPacketPayload) {
    super(payload, schema);
  }
}

const packet = new TestPacket({
  ID: 'TEST',
  numberField1b: 1,
  numberField2b: 1024,
  numberField4b: 123456,
  numberField2bBE: 0xffdd,
  numberField4bBE: 0xaabbccdd,
  arrayField1b: [0xfa, 0xfa, 0xfa, 0xfa],
  arrayField2bLe: [0xfffe, 0xfffe, 0xfffe, 0xfffe],
  arrayField2bBe: [0xfffe, 0xfffe, 0xfffe, 0xfffe],
  filler: 129, // align to
  filler2: 138,
  arrayField4bLe: [0xfafbfcfd, 0xfafbfcfd, 0xfafbfcfd, 0xfafbfcfd],
  arrayField4bBe: [0xfafbfcfd, 0xfafbfcfd, 0xfafbfcfd, 0xfafbfcfd],
});

const buf = packet.encode();
const decoded = packet.decode(buf);

VER 0.6.3

  • Update readme

VER 0.6.1

  • Rename schema "length" field for non array type to "size" for consistency
  • Types improvements

VER 0.5.12

  • Fixed types.
  • Added support for array encoding
  • Added support for strings encoding ('utf8' and 'ascii' was tested other encodings not planned to be tested)
  • Test coverage: 100% at that moment
0.6.6

11 months ago

0.5.12

11 months ago

0.2.1

11 months ago

0.6.3

11 months ago

0.6.2

11 months ago

0.4.1

11 months ago

0.3.2

11 months ago

0.3.1

11 months ago

0.5.1

11 months ago

0.4.2

11 months ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago