Coder Social home page Coder Social logo

bstruct's Introduction

bstruct

NPM version Pipeline Status Coverage Status Sonarcloud Status Dependency Status Dev Dependency Status

Declaratively encode and decode binary data

Inspired by restructure and binary-parser

Example

import * as b from "@sighmir/bstruct";

export const login = b.struct({
  length: b.int16le,
  op: b.int16le,
  s1: b.skip(20),
  account: b.string(33, "utf8"),
  s2: b.skip(26),
  password: b.string(16, "base64"),
});

const loginObj = {
  length: 4080,
  op: 3,
  account: "abcde",
  password: "ZWZnaGkK",
};

const buf = login.encode(loginObj);
const obj = login.decode<typeof loginObj>(buf);

API

b.struct({})

Create a binary structure encoder/decoder, functions encode and decode are available, property struct returns the argument used to build the structure encode/decoder, property size returns the added size of all binary parsers used on the structure.

b.skip(bytes)

Skip a certain amount of bytes, if the variable is a buffer on the encoded object, simply copy it to the new buffer, if it's something else, create a new buffer and fill it with zeros. When decoding it simply copies part of the buffer to the new object.

b.string(length, encoding)

Parse bytes as a string. Encoding can be any BufferEncoding.

b.bool

Parse bytes as boolean.

b.[u]int{8, 16, 32, 64}{le, be}

Parse bytes as integer.

b.float{le, be}

Parse bytes as float.

b.double{le, be}

Parse bytes as double.

b.buffer(bytes)

Copy part of the buffer to the object when decoding, or a buffer variable from the object to the new buffer when encoding.

b.array(length, parser)

Parse an array using one of the previous parsers.

b.choice(key, structs)

Uses the value of a variable on the parent object to pick a parser for a nested object. You can also use b.struct as a parser for nested objects.

License

bstruct - Declaratively encode and decode binary data
Copyright (C) 2020  Guilherme Caulada

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

bstruct's People

Contributors

dependabot[bot] avatar guicaulada avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.