Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

compact-encoding

Package Overview
Dependencies
Maintainers
2
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

compact-encoding

A series of compact encoding schemes for building small and fast parsers and serializers

  • 2.4.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.9K
increased by74.29%
Maintainers
2
Weekly downloads
 
Created
Source

compact-encoding

A series of compact encoding schemes for building small and fast parsers and serializers

npm install compact-encoding

Usage

const cenc = require('compact-encoding')

const state = { start: 0, end: 0, buffer: null }

// use preencode to figure out how big a buffer is needed
cenc.uint.preencode(state, 42)
cenc.string.preencode(state, 'hi')

console.log(state) // { start: 0, end: 4, buffer: null }

state.buffer = Buffer.allocUnsafe(state.end)

// then use encode to actually encode it to the buffer
cenc.uint.encode(state, 42)
cenc.string.encode(state, 'hi')

// to decode it simply use decode instead

state.start = 0
cenc.uint.decode(state) // 42
cenc.string.decode(state) // 'hi'

Encoder API

state

Should be an object that looks like this { start, end, buffer }.

You can also get a blank state object using cenc.state().

enc.preencode(state, val)

Does a fast preencode dry-run that only sets state.end. Use this to figure out how big of a buffer you need.

enc.encode(state, val)

Encodes val into state.buffer at position state.start. Updates state.start to point after the encoded value when done.

val = enc.decode(state)

Decodes a value from state.buffer as position state.start. Updates state.start to point after the decoded value when done in the buffer.

Helpers

If you are just encoding to a buffer or decoding from one you can use the encode and decode helpers to reduce your boilerplate

const buf = cenc.encode(cenc.bool, true)
const bool = cenc.decode(cenc.bool, buf)

Bundled encodings

The following encodings are bundled as they are primitives that can be used to build others on top. Feel free to PR more that are missing.

  • cenc.uint - Encodes a uint using compact-uint
  • cenc.int - Encodes an int using compact-uint as a signed int using ZigZag encoding.
  • cenc.uint16 - Encodes a fixed size uint16 (useful for things like ports)
  • cenc.uint24 - Encodes a fixed size uint24 (useful for message framing)
  • cenc.uint32 - Encodes a fixes size uint32 (useful for very large message framing)
  • cenc.buffer - Encodes a buffer with it's length uint prefixed. When decoding an empty buf, null is returned.
  • cenc.raw - Pass through encodes a buffer - ie a basic copy.
  • cenc.uint32array - Encodes a uint32array with it's element length uint32 prefixed.
  • cenc.bool - Encodes a boolean as 1 or 0.
  • cenc.string - Encodes a utf-8 string, similar to buffer.
  • cenc.fixed32 - Encodes a fixed 32 byte buffer.
  • cenc.fixed64 - Encodes a fixed 64 byte buffer.
  • cenc.fixed(n) - Makes a fixed sized encoder.
  • cenc.array(enc) - Makes an array encoder from another encoder. Arrays are uint prefixed with their length.
  • cenc.from(enc) - Makes a compact encoder from a codec or abstract-encoding

License

MIT

FAQs

Package last updated on 10 Aug 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc