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

messagepack

v1.1.12

Published

A MessagePack implementation for JavaScript.

Downloads

53,518

Readme

msgpack-js

msgpack-js is a MessagePack implementation for JavaScript and TypeScript.

Encoding

To encode objects into the binary MessagePack format, an encode function is provided:

function encode<T>(v: T, typ?: Type<T>): Uint8Array;

This function takes an object of an arbitrary type and converts it to its binary representation. If the type of the object is known in advance, an optional typ parameter could be passed to indicate the encoding algorithm. For an automatic type detection this parameter could be omitted.

Decoding

To decode binary MessagePack data into objects, a decode function is provided:

function decode<T>(buf: BufferSource, typ?: Type<T>): T;

This function takes a buffer containing the binary data and converts it to an object. The buffer could either be an ArrayBuffer or an ArrayBufferView and should contain valid MessagePack data. If a certain type is expected, an optional typ parameter could be passed. For automatic detection from the buffer's content this parameter could be omitted.

Example

import {encode, decode} from "messagepack";

const bin1 = encode({foo: 7, bar: "seven"});
const obj = decode(bin1);
console.log(obj);

const bin2 = encode("foobar");
const str = decode(bin2);
console.log(str);

Types

Sometimes even a JavaScript developer wants to have a little bit more type safety. In this situation specific types could be passed to the encode and decode functions. If the object or the binary data has an incompatible type, an error will be thrown.

The following types are supported:

  • Nil for null values,
  • Bool for boolean values,
  • Int for signed integer values,
  • Uint for unsigned integer values,
  • Float for floating-point values,
  • Bytes for binary data,
  • Str for string values,
  • Arr for arrays,
  • Map for objects,
  • Raw for already encoded values,
  • Time for date and time values, and
  • Any for automatically detecting the type and forward it to one of the types above.

The Arr and Map types provide generic encoding and decoding for their elements, i.e. Arr and Map essentially equal Any[] and Map<Str, Any> respectively. If more stringent element types are required, the TypedArr and TypedMap functions could be used instead:

import {TypedArr, TypedMap, Int, Str} from "messagepack";

const IntArray = TypedArr(Int);
const IntStrMap = TypedMap(Int, Str);

Structs

A struct is an object type with a predefined shape. To define such a type, the function

function Struct(fields: Fields): Type<Obj<any>>;

can be used, which creates a type out of the predefined fields. All fields, that do not belong to the struct definition, will be omitted during the encoding and decoding process. To save some bytes and allow name changes, a struct is not simply encoded as a map with string keys. Instead, each field consists of a name, a type, and an ordinal, where the ordinal is used to uniquely identify a field.

Here is an example, how define a struct:

import {Struct, Int, Str} from "messagepack";

const S = Struct({
    // ordinal: [name, type],
    1: ["foo", Int],
    2: ["bar", Str],
});

If only the encoding or decoding capability is necessary, the functions

function structEncoder(fields: Fields): EncodeFunc<any>;
function structDecoder(fields: Fields): DecodeFunc<any>;

can be used to create encoder and decoder functions respectively.

Unions

A union is a value, that can be one of several types. To define a union type, the function

function Union(branches: Branches): Type<any>;

can be used, which creates a type out of the predefined branches. Each branch consists of an ordinal and a type. If a type should be encoded or decoded, that is not part of the union definition, an exception will be thrown.

Here is an example, how to define a union:

import {Union, Int, Str} from "messagepack";

const U = Union({
    // ordinal: type,
    1: Int,
    2: Str,
});

If only the encoding or decoding capability is necessary, the functions

function unionEncoder(branches: Branches): EncodeFunc<any>;
function unionDecoder(branches: Branches): DecodeFunc<any>;

can be used to create encoder and decoder functions respectively.