Skip to content

Repository files navigation

LintBuildTestCoverage StatuscodecovKnown VulnerabilitiesCommitizen friendlyLicenseGitHub issuesGitHub starsnpmcode style: prettierTypesTest Coverage

byteify

An npm module that serializes or deserializes your native types to an array of bytes.

Install

To install byteify, run:

$ npm install byteify

Usage

Serialize

constbyteify=require('byteify');// The value will be: [1]constserializedBool=byteify.serializeBool(true);// The value will be: [10]constserializedUint8=byteify.serializeUint8(10);// The value will be: [1, 0]constserializedUint16=byteify.serializeUint16(256,{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: [0, 15, 66, 64]constserializedUint32=byteify.serializeUint32(1000000,{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: [0, 0, 0, 0, 5, 245, 225, 0]constserializedUint64=byteify.serializeUint64(100000000n,{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: [255]constserializedInt8=byteify.serializeInt8(-1);// The value will be: [252, 24]constserializedInt16=byteify.serializeInt16(-1000,{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: [255, 254, 121, 96]constserializedInt32=byteify.serializeInt32(-100000,{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: [255, 255, 255, 255, 255, 255, 255, 255]constserializedInt64=byteify.serializeInt64(-1n,{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: [10, 215, 185, 65] constserializedFloat32=byteify.serializeFloat32(23.23,{endianess: ByteifyEndianess.LITTLE_ENDIAN});// The value will be: [123, 20, 174, 71, 225, 58, 55, 64]constserializedFloat64=byteify.serializeFloat64(23.23,{endianess: ByteifyEndianess.LITTLE_ENDIAN});

Deserialize

constbyteify=require('byteify');constByteifyEndianess=byteify.ByteifyEndianess;// The value will be: trueconstdeserializedBool=byteify.deserializeBool([1]);// The value will be: 10constdeserializedUint8=byteify.deserializeUint8([10]);// The value will be: 1constdeserializedUint16=byteify.deserializeUint16([1,0],{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: 0constdeserializedUint32=byteify.deserializeUint32([0,15,66,64],{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: 100000000nnconstdeserializedUint64=byteify.deserializeUint64([5,245,225,0,5,245,225,0],{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: -1constdeserializedInt8=byteify.deserializeInt8([255]);// The value will be: -1000constdeserializedInt16=byteify.deserializeInt16([252,24],{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: -100000constdeserializedInt32=byteify.deserializeInt32([255,254,121,96],{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: -1nconstdeserializedInt64=byteify.deserializeInt64([255,255,255,255,255,255,255,255],{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: 23.23constdeserializedFloat32=byteify.deserializeFloat32([10,215,185,65],{endianess: ByteifyEndianess.LITTLE_ENDIAN});// The value will be: 23.23constdeserializedFloat64=byteify.deserializeFloat64([123,20,174,71,225,58,55,64],{endianess: ByteifyEndianess.LITTLE_ENDIAN});

Endianess

By default, Little Endian is used. If you want to use Big Endian, you can pass it as an option

constbyteify=require('byteify');constByteifyEndianess=byteify.ByteifyEndianess;// The value will be: 1000000constdeserializedUint64=byteify.deserializeUint64([64,66,15,0,0,0,0,0]);constdeserializedUint64=byteify.deserializeUint64([64,66,15,0,0,0,0,0],{endianess: ByteifyEndianess.LITTLE_ENDIAN});constdeserializedUint64=byteify.deserializeUint64([0,0,0,0,0,15,66,64],{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be [64, 66, 15, 0, 0, 0, 0, 0]constserializedUint64=byteify.serializeUint64(1000000,{endianess: ByteifyEndianess.LITTLE_ENDIAN});// The value will be [0, 0, 0, 0, 0, 15, 66, 64]constserializedUint64=byteify.serializeUint64(1000000,{endianess: ByteifyEndianess.BIG_ENDIAN});

Limits

const{ limits }=require('byteify');/*{ bool: 1, uint8: 255, uint16: 65_535, uint32: 4_294_967_295, uint64: 18_446_744_073_709_551_616n, int8: 127, int16: 32_767, int32: 2_147_483_647, int64: 9_223_372_036_854_775_807n, float32: Number.MAX_VALUE, float64: Number.MAX_VALUE}*/console.log(limits.MAX);/*{ bool: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0n, int8: -128, int16: -32_768, int32: -2_147_483_648, int64: -9_223_372_036_854_775_808n, float32: -Number.MAX_VALUE, float64: -Number.MAX_VALUE}*/console.log(limits.MIN);/*{ bool: 1, uint8: 1, uint16: 2, uint32: 4, uint64: 8, int8: 1, int16: 2, int32: 4, int64: 8, float32: 4, float64: 8}*/console.log(limits.N_OF_BYTES);

API

The documentation site is: byteify documentation

The documentation for development site is: byteify dev documentation

Development

To build the module make sure you have the dev dependencies installed.

The project is written in Typescript, bundled with esbuild, linted with ESLint and tested with Jest.

Lint

In order to lint the code:

$ npm run lint

In order to lint and fix the code:

$ npm run lint:fix

There are also the :source and :test suffix after lint in order to lint only the source code or the test code.

Transpile

To transpile both the source and the test code:

$ npm run transpile

The source and the test folders will be transpiled in the dist folder. Also the type declarations will be generated.

To transpile only the source code:

$ npm run transpile:source

The source folder will be transpiled in the dist folder. Also the type declarations will be generated.

Test

After having transpiled the code, run:

$ npm test

in order to run the tests with jest.

If a coverage report is to be generated, run:

$ npm run cover:generate

Bundle

$ npm run bundle

The source folder will be compiled in the bundled folder. It will contain the bundled index.js and index.d.ts files.

Notes

  • For simplicity, the limits for the floating point numbers are always Number.MAX_VALUE and -Number.MAX_VALUE. For float32 this means that the result is not guaranteed for too precise inputs.
  • For uint64 and int64, the BigInt type is used.

About

An npm module that serializes or deserializes your native types to an array of bytes.

Topics

Resources

Stars

6 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages