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

msgpack-typed-numbers

v0.0.1

Published

A minimalistic MessagePack encoder and decoder for JavaScript. Supports typed numbers.

Downloads

9

Readme

msgpack-typed-numbers Build Status

A minimalistic MessagePack encoder and decoder for JavaScript. Supports typed numbers for 64-bit longs and consistent floating point values. Based on Joshua Wise's excellent tiny-msgpack project.

  • Tiny Size (2.63 kB minified and gzipped)
  • Fast performance (Slightly faster than msgpack-lite)
  • Supports true 64-bit longs and consistent floating-point values
  • Extension support
  • No other bells or whistles

By default, msgpack can encode numbers, strings, booleans, nulls, arrays, objects, and binary data (Uint8Array). However, additional types can be registered by using extensions.

Installation

npm install --save msgpack-typed-numbers

Usage

var msgpack = require('msgpack-typed-numbers');

var uint8array = msgpack.encode({foo: 'bar', baz: 123});
var object = msgpack.decode(uint8array);

Typed Numbers

Javascript numbers reqpresent both integer and floating point values. This poses two problems for msgpack:

  1. integer values > 53 bits cannot be represented
  2. floating point values with whole numbers (ie. 1.0) are indistinguishable from integers, so msgpack treats them as integers

When one is using msgpack to send and receive data with other systems that do not have this limitation, like Python or Java, these limitations can ruin the data being transmitted. msgpack-with-numbers provides a way to specify Float and Long values to encode and decode numeric data correctly.

Floating-Point Numbers

To used typed Float values, wrap the value in a msgpack.Float class.

const msgpack = require('msgpack-typed-numbers');

// NOTE: integer number input
const float1 = new msgpack.Float(100); 
const float2 = new msgpack.Float(1.1);

const uint8array = msgpack.encode({
  floatField1: float1, 
  floatField2: float2
});

// float1 is encoed as a 32-bit float
// float2 is encoded as a 64-bit float
const obj = msgpack.decode(uint8array);
console.log(obj);
/* =>
{ floatField1: 100, floatField2: 1.1 }
*/

Long Numbers

To use Long values, create a Long value with the Long library.

const msgpack = require('msgpack-typed-numbers');
const Long = require('long');

// max 64-bit number
const long1 = Long.fromBits(-1, 0x7fffffff);
const long2 = Long.fromNumber(-100);

const uint8array = msgpack.encode({
  longField1: long1, 
  longField2: long2, 
});

// long1 is encoded as a 64-bit uint
// long2 is encoded as an 8-bit int
var obj = msgpack.decode(uint8array);
console.log(obj);
/* =>
{ longField1: Long { low: -1, high: 2147483647, unsigned: false },
  longField2: -100 }
*/

The use of Long and Float are optional. One can still pass primitive JS numbers and they will be treated as variant-type numeric data.

Extensions

var msgpack = require('msgpack-typed-numbers');
var codec = new msgpack.Codec;

function encodeDate(date) {
  return msgpack.encode(+date);
}
function decodeDate(uint8array) {
  return new Date(msgpack.decode(uint8array));
}
codec.register(0x42, Date, encodeDate, decodeDate);

var uint8array = msgpack.encode({timestamp: new Date}, codec);
var object = msgpack.decode(uint8array, codec);
console.log(object.timestamp instanceof Date); // => true

Browser Support

In the browser, msgpack-typed-numbers requires the Encoding API, which is currently only implemented by Chrome and Firefox. However, if you polyfill it, this package is supported by the following browsers:

  • Chrome 9+
  • Firefox 15+
  • Safari 5.1+
  • Opera 12.1+
  • Internet Explorer 10+

Zero copy

In the MessagePack format, binary data is encoded as... binary data! To maximize performance, msgpack-typed-numbers does not copy binary data when encoding or decoding it. So after decoding, the contents of a returned Uint8Array can be affected by modifying the input Uint8Array (the same can happen with encoding).

License

MIT