JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 18
  • Score
    100M100P100Q56074F
  • License MIT

a typescript library for reading and writing binary files to and from plain javascript objects

Package Exports

  • @binary-format/binary-format
  • @binary-format/binary-format/dist/index.es.js
  • @binary-format/binary-format/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@binary-format/binary-format) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

binary-format

a typescript library for reading and writing binary files to and from plain javascript objects.

there is a plethora of exiting libraries that accomplish the same goal, but all come with tradeoffs.

i am trading some speed in favor or using "plain" javascript objects that immer supports because i wanted to use immutable data structures while working on an online editor that supports the editing of various file formats.

quick start

import BinaryFormat from '@skratchdot/binary-format';
const helloWorldParser = new BinaryFormat<{
  hello: string;
  asciiSpace: number;
  world: string;
  asciiExclaimation: number;
}>()
  .string('hello', 5)
  .uint8('asciiSpace')
  .string('world', 5)
  .uint8('asciiExclaimation')
  .done();
const buffer = Buffer.from('hello world!');
const readResult = helloWorldParser.read(buffer);
const writeResult = helloWorldParser.write(readResult);
console.log({ readResult, writeResult });
/*
{
  readResult: {
    hello: 'hello',
    asciiSpace: 32,
    world: 'world',
    asciiExclaimation: 33
  },
  writeResult: <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64 21>
}
*/

features

  • written in typescript
  • supports both reading and writing of binary files
  • supports formatting to/from "plain" javascript objects (that are serializable and supported via immutability libs like immer)
  • supports formatting to/from "complex" javascript objects like typed arrays and buffers

documentation

coming soon. for now check out the examples folder or the unit tests

Custom

export const stringWithLength = (length: number): ReadAndWrite => ({
  read: (r: SmartBuffer) => r.readString(length),
  write: (w: SmartBuffer, v: unknown) => w.writeString(String(v), length),
});

see also

parsing libs

buffer libs:

todos

  • detect cycles
  • add streaming support
  • add example parsers
  • remove all dependencies
    • update BinaryBuffer so it supports all the functionality of bit-buffer and smart-buffer (with bit/byte offset logic)
  • consider "export parser" so endusers don't need to include unneeded generator code in their project
  • generate api documentation and add better comments
  • support signed/unsigned and big/little endian for everything (right now i'm assuming unsigned for bits)
  • add library comparison chart
  • add benchmarks and make performance improvements
  • add "assertion" logic
    • assert at EOF after reading (optional)
    • add .assert(data => data.foo === 'must be this value') to api
  • add seek/pointer logic
  • let toArray() take a funtion instead of a number as the only parameter (potentially other objects that take a length: string() and buffer() etc)
  • add typedarray logic (maybe things like uint8array())
  • can we ever support toArray() being called on bits()?