npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sdnv

v1.0.1

Published

Self-Delimiting Numeric Value Codec

Downloads

4

Readme

SDNV Build Status

Self-Delimiting Numeric Value Codec

This package allows you to use Self-Delimiting Numeric Values (SDNVs) in Node.js programs. It is basically a wrapper class that encloses basic Node Buffers encoded as SDNVs. Additionaly, it also provides you utility functions to encode and decode a Buffer on-demand.

What are SDNVs?

SDNVs were created by the DTNRG and they aim to overcome common problems and limitations of fixed size fields in networking protocols (e.g. TPC advertised received window field or even the entire IPv4 header).

As stated in the official spec released as RFC 6256:

An SDNV is simply a way of representing non-negative integers (both positive integers of 
arbitrary magnitude and 0) without expending much unnecessary space.

SDNVs closely resemble certain constructs with ASN.1 but they are focused exclusively on numeric strings or bistrings unlike the latter which was developed for encoding more complex data structures.

How do they work?

An SDNV is a numeric value encoded in N octets, the last of which has its most significant bit (MSB) set to 0; the MSB of every other octet in the SDNV must be set to 1. The value encoded in an SDNV is the unsigned binary number obtained by concatenating into a single bit string the 7 least significant bits of each octet of the SDNV.

The following examples illustrate the encoding scheme for various hexadecimal values:

0xABC  : 1010 1011 1100
         is encoded as
         {1 00 10101} {0 0111100}
         = 10010101 00111100
         
0x1234 : 0001 0010 0011 0100
       =    1 0010 0011 0100
         is encoded as
         {1 0 100100} {0 0110100}
         = 10100100 00110100
         
0x4234 : 0100 0010 0011 0100
       =  100 0010 0011 0100
         is encoded as
         {1 000000 1} {1 0000100} {0 0110100}
         = 10000001 10000100 00110100
         
0x7F   : 0111 1111
       =  111 1111
         is encoded as
         {0 1111111}
         = 01111111

Using the package

var SDNV = require('sdnv');

// Using the SDNV constructor automatically wraps the encoded buffer value
var sdnv = new SDNV(new Buffer([0x12, 0x34]);

// Decode the wrapped value
sdnv.decode();  // <Buffer 12 34>

// Utility functions are also available
SDNV.encode(new Buffer[0x12, 0x34]));  // <Buffer a4 34>
SDNV.decode(new Buffer[0xa4, 0x34]));  // <Buffer 12 34>

You should provide a Node.js buffer object on input, unless you want the following to happen:

Error: the argument should be a Buffer

Also, you can create both encoding and decoding Transform Streams and pipe those to other ones:

var SDNV = require('sdnv');

// create some source stream (don't forget to emit data and "end" it later)
var Stream = require('stream');
var source = new Stream();

// use the API to create encoding or decoding streams
var encoder = SDNV.createEncodeStream();
var decoder = SDNV.createDecodeStream();

// pipe them (in this case, the output should match the input)
source.pipe(encoder).pipe(decoder);

Contributing

You know the drill.