1.4.1 • Published 11 months ago

@ryencode/binarypacker v1.4.1

Weekly downloads
-
License
GPL-3.0-or-later
Repository
github
Last release
11 months ago

BinaryPacker

BinaryPacker is a utility to serialize a JavaScript object into an ArrayBuffer according to a provided schmea and deserialize an ArrayBuffer according to a given schema. Sending binary data across a socket or in a file will likely be much more size efficient than a stringified version of the data. Being able to define a schema for a binary data type or document saves the developer the stress of manually having to serialize and deserialize each component by hand.

Installation

npm install @ryencode/binarypacker

Using BinaryPacker

Basic usage

The following example assumes a schema has been defined.

const bp = new BinaryPacker(mySchema);

//encoding
const binary = bp.encode(inObject);

//decoding
const outObject = bp.decode(binary);

Defining a Schema

A schema is a JavaScript Object that details how to encode/decode the object to and from the binary representation.

const mySchmea = {
  definitions:{
	  customA: {
		  type: "string",
		  options: {content:"staticValue"}
		},
		customB:{
			type:"complex",
			schema:{
				customASubProp: "string"
			}
		}
  },
  schema:{
		stringProp:{type:"string"},
		numberProp:{type:"uint16"},
		customAProp:{type:"customA"},
		customBProp:{type:"customB"},
		arrayProp:{
			type:"array",
			options:{type:"string"}
		}
  }
}

The above schema describes a serialization scheme for objects that look like the following:

{
	stringProp: "string a",
	numberProp: 3139,
	customAProp: "staticValue",
	customBProp: {
		customASubProp: "string b"
	},
	arrayProp: ["A","B","C"]
}

Schema Type

A schema type has a type property which must be one of the supported internal types, or a type defined in the Definitions section of the schema object. Optionally an options object may be passed in to provided extra details to the serialization/serialization processes. The specifics of the options depends on the type used.

{
	type: "type",
	options: {...}
}

Definitions

It is possible to create complex hierarchies by defining your own types within the definitions section of the schema object.

Alias an existing type with specific options

Define the type

const mySchema = {
  definitions: {
		typeName: {
			type: "stirng",
			options: { content:"Always This Value" }
		}
	}
	...
}

Using the type:

const mySchema = {
	...
	schmea: {
		//the following uses the options from the defined types above
		myProp: {type: "typeName"}
		
	}
}
Define a complex type
const mySchema = {
  "definitions":{
    "name": { "type":"string"},
    "person": {
      "type": "complex",
      "schema": {
        "firstName": {"type": "name"},
        "middleName": {"type": "name"},
        "lastName": {"type": "name"},
        "friends": {"type": "personList"}
      }
    },
    "personList": {
      "type": "array",
      "options": {"type": "person"}
    }
  },
  "schema": {
    "attendees": { "type": "personList"  }
  }
}
Data working with schema
const myDataObj = {
	"attendees": [
		{fistName:"Bob", lastName:"Ratchet"},
		{fistName:"Stan", middleName: "Frum", lastName:"Winston",
			friends:[
			  {fistName:"Bill", lastName:"Manly"}
			]
		}
	]
};

Currently recursive data is NOT supported and will cause a stack overload

Supported Types

TypebytesDetails
string4+4 bytes for the length of the string then the encoded bytes for the string.
array4+4 bytes for the length of the string then the bytes for each element
int81Signed 8bit integer
uint81Unsigned 8bit integer
int162Signed 16bit integer
uint162Unsigned 16bit integer
int324Signed 32bit integer
uint324Unsigned 32bit integer
int648Signed 64bit integer as BigInt
uint648Unsigned 64bit integer as BigInt
float324Signed 32bit floating point
float648Signed 64bit floating point
datetime8Date object (internally stores the getTime() result as a uint64 value)
binary4+4 bytes for the number of bytes then the encoded bytes. Value must be a ArrayBuffer. Decoded value will be ArrayBuffer

String

String type supports options |Option|Description| |--|--| |content|Fixed content written to the binary stream, and verified on read.|

Array

String type supports options |Option|Description| |--|--| |type|Required The type of each element in the array. An array can only have one element type.|

Contributing

Coding Standards

Have you looked at the code? No standard here. OK committed code should be beautified, and should have relevant tests added and existing tests passed. If an existing test no longer passes it should be because the existing test is no longer relevant and thus removed or altered to be correct. And updated documentation (using JSDoc please.)

I make no promise of reviewing pull requests or answering questions on why one may be rejected or not. Though I will try to deal with these in a timely and respectful manner.

Copyright & License

Copyright

All files and content Copyright 2023 Ryan Brown (ryanatlightdepriveddotnet)

License

GNU General Public License v3.0

Written with StackEdit.

1.4.1

11 months ago

1.4.0

11 months ago

1.3.1

11 months ago

1.3.0

11 months ago

1.2.0

1 year ago

1.1.4

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago