@ibr/buffy
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

@ibr/buffy

Structured data decoder / encoder

import { Encoder, Decoder, DefinitionRegistry, PropertiesConfig, NumberSize } from '@ibr/buffy';

const config: PropertiesConfig = ([
  {
    reserved: NumberSize.UInt8,
    fixed: 1,
  },
  {
    name: 'simple',
    value: { size: NumberSize.UInt8 },
  },
  {
    name: 'nested',
    properties: [
      {
        name: 'prop',
        value: { size: NumberSize.UInt8 },
      },
    ],
  },
  {
    name: 'array',
    value: [
      { size: NumberSize.UInt4 },
      { size: NumberSize.UInt2 },
      { size: NumberSize.UInt2 },
      { size: NumberSize.UInt16 },
    ],
  },
  {
    name: 'nestedArray',
    value: [
      { size: NumberSize.UInt2 },
      [
        { size: NumberSize.UInt2 },
        { size: NumberSize.UInt4 },
        {
          properties: [
            {
              name: 'value',
              value: { size: NumberSize.UInt16 },
            },
          ],
        },
      ],
    ],
  },
]);

const data = {
  simple: null,
  nested: { prop: 1 },
  array: [1, 1, 1, 1],
  nestedArray: [1, [1, 1, { value: 97 }]],
};

const buff = Buffer.from([
  0b00000001,
  0b11111111,
  0b00000001,
  0b00010101,
  0b00000001,
  0b00000000,
  0b01010001,
  0b01100001,
  0b00000000,
]);

const encoder = new Encoder([config]);
const decoder = new Decoder(new DefinitionRegistry(), config);

encoder.resultSize // => 9 (bytes)
encoder.encode(data) // => is equal to buff
decoder.decode(buff) // => is equal to data

Readme

Keywords

none

Package Sidebar

Install

npm i @ibr/buffy

Weekly Downloads

0

Version

0.2.0

License

MIT

Unpacked Size

38 kB

Total Files

23

Last publish

Collaborators

  • michalb