0.5.7 • Published 5 years ago

binary-nbt v0.5.7

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

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

0.5.7

5 years ago

0.5.7-alpha.1

5 years ago

0.5.7-alpha.0

5 years ago

0.5.6

5 years ago

0.5.6-alpha.2

5 years ago

0.5.6-alpha.1

5 years ago

0.5.6-alpha.0

5 years ago

0.5.5

5 years ago

0.5.4

5 years ago

0.5.3

5 years ago

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.3

5 years ago

0.4.2

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.0

5 years ago