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

binary-nbt

v0.5.7

Published

A serializer and deserializer for Minecraft's NBT archives with a lossless but ergonomic output format

Downloads

6

Readme

Binary NBT

npm npm (tag) npm docs

A serializer and deserializer for Minecraft's NBT archives with a lossless but ergonomic output format.

This also contains a command line tool to convert an NBT file to JSON, called binary-nbt

Lossless here means that a value can be deserialized from NBT and then serialized back into exactly the same NBT value (i.e. the types from the NBT can be recovered). Ergonomic means that the deserialized value can be treated as a plain JavaScript object. E.g. level.Data.allowCommands works as expected for a level.dat file. Other libraries such as nbt use custom objects, e.g. {value: <...>, type: TAG_COMPOUND}.This is possible through the use of ES6 Symbols - every deserialized object has NBTTypeSymbol applied to it, which is used in serialisation (with sensible defaults for when this is not provided). This approach also works for primitives. The primary disadvantage of this approach is that the produced value cannot be safely serialised into any form other than NBT without losing the data required to reconstruct the NBT. However, if such serialisation is required, NBT can be used as the format!

TAG_LONGs are deserialized as Longs from the long package.

This library currently only supports NodeJS 8 and 10. Other javascript environments may be supported, should the need arise. Open an issue if you are interested. We also only support big endian NBT, so the pocket edition nbt is not yet supported

This package is part of the mcfunction-langserver project. It is published automatically - see the publishing strategy

API

Deserialization

Basic usage:

function deserializeNBTUncompressed(buffer: Buffer): any;
const { deserializeNBTUncompressed } = require("binary-nbt");
const fs = require("fs");

const contents = fs.readFileSync("some_nbt.nbt");
console.log(deserializeNBTUncompressed(contents));

Or in Typescript:

const { deserializeNBTUncompressed } = require("binary-nbt");
const fs = require("fs");

const contents = fs.readFileSync("level.dat");
const level: Level = deserializeNBTUncompressed(contents);

Command line

The command line tool converts NBT values to JSON, whether compressed or not:

$ binary-nbt --help
binary-nbt

Usage:
  $ binary-nbt [...files]

Commands:
  [...files]  Convert files from NBT into JSON, outputting them to stdout.
If a directory is passed, it is recursively walked for files

For more info, run any command with the `--help` flag:
  $ binary-nbt --help

Options:
  -o, --out [dir]        Place the resulting files into this directory instead
  -e, --extension [ext]  Extension to output the JSON files in (default: .json)
  -h, --help             Display this message

Examples:
binary-nbt some_dir file.nbt file.nbt.gz --output .
binary-nbt some_dir -o result

Comparison with alternatives

This library is yet another NBT library amongst the wide selection of existing JavaScript NBT libraries. A comparison with a selection of the others is below. The primary advantages of this library are the use of Promises, vanilla JavaScript classes (e.g. Number, String) which can be treated as primitives in most cases, and Typescript type definitions. We also have full API documentation generated using TypeDoc.

A brief rundown of the differences between other packages and this one are below. For example, if they don't support little endian NBT, no attention is brought to that fact as it is not a difference from this package. These packages are described in the order they appear in when searching for nbt on npm.

nbt:

  • Has a very desirable name on npm;
  • Data can be safely serialised and deserialized into formats other than NBT;
  • Supports the browser
  • Has good API documentation, although the following two points aren't well signposted;
  • Every value is wrapped in an unwieldy {value: <actual value>, type: <type>} object;
  • TAG_LONGSs are encoded as an [upper, upper] number array, without any convenience APIs;
  • Doesn't have Typescript type definitions available;
  • Doesn't use Promises (this can be circumvented using util.promisify);

prismarine-nbt

  • Supports little endian NBT;
  • Uses same format as nbt for longs and other values;
  • Has weak API documentation and no Typescript type definitions;
  • Doesn't use Promises;

nbt-ts

  • Uses BigInts for longs, so only supports Node 10;
  • Custom classes are used for numbers, so the value must be accessed using the value property;

Excluded packages

Some packages are not up-to-date with the latest version of NBT or are not NBT packages and so are excluded. These are:

Changelog

The changelog contains a list of all the changes between versions