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

prismarine-nbt

v2.7.0

Published

A parser and serializer for NBT archives

Downloads

42,560

Readme

Prismarine-NBT

NPM version Build Status Discord Try it on gitpod

Prismarine-NBT is a JavaScript parser and serializer for NBT archives. It supports big, little, and little-varint encoded NBT files.

Usage

as a async promise

const fs = require('fs')
const nbt = require('prismarine-nbt')

async function main(file) {
  const buffer = fs.readFileSync(file)
  const { parsed, type } = await nbt.parse(buffer)
  console.log('JSON serialized', JSON.stringify(parsed, null, 2))
  fs.createWriteStream('bigtest.nbt').write(nbt.writeUncompressed(parsed, type)) // Write it back 
}
main('bigtest.nbt')

as a callback

var fs = require('fs'),
    nbt = require('prismarine-nbt');

fs.readFile('bigtest.nbt', function(error, data) {
    if (error) throw error;

    nbt.parse(data, function(error, data) {
        console.log(data.value.stringTest.value);
        console.log(data.value['nested compound test'].value);
    });
});

If the data is gzipped, it is automatically decompressed, for the buffer see metadata.buffer

API

parse(data, [format]): Promise<{ parsed, type, metadata: { size, buffer? } }>

parse(data, [format,] callback)

Takes an optionally compressed data buffer and reads the nbt data.

If the endian format is known, it can be specified as 'big', 'little' or 'littleVarint'. If not specified, the library will try to sequentially load as big, little and little varint until the parse is successful. The deduced type is returned as type.

Minecraft Java Edition uses big-endian format, and Bedrock uses little-endian.

writeUncompressed(value, format='big')

Returns a buffer with a serialized nbt value.

parseUncompressed(data, format='big', options?= {noArraySizeCheck?: boolean})

Takes a buffer data and returns a parsed nbt value.

The options parameter is optional. When noArraySizeCheck is true, an array size check is disabled which allows for parsing of large arrays.

parseAs(data, type, options?= {noArraySizeCheck?: boolean})

Takes a buffer data and returns a parsed nbt value. If the buffer is gzipped, it will unzip the data first.

The options parameter is optional. When noArraySizeCheck is true, an array size check is disabled which allows for parsing of large arrays.

simplify(nbt)

Returns a simplified nbt representation : keep only the value to remove one level. This loses the types so you cannot use the resulting representation to write it back to nbt.

equal(nbt1, nbt2)

Checks whether two NBT objects are equal, returns a boolean.

protos : { big, little, littleVarint }

Provides compiled protodef instances used to parse and serialize nbt

proto

Provide the big-endian protodef instance used to parse and serialize nbt.

protoLE

Provide the little-endian protodef instance used to parse and serialize little endian nbt.

addTypesToCompiler (type, compiler)

Adds prismarine-nbt types to an ProtoDef compiler instance

addTypesToInterpreter (type, interperter)

Adds prismarine-nbt types to a ProtoDef interpreter instance

builder

Provides a way to build complex nbt structures simply:

const nbt = require('prismarine-nbt')
const tag = nbt.comp({
  Air: nbt.short(300),
  Armor: nbt.list(nbt.comp([
    { Count: nbt.byte(0), Damage: nbt.short(0), Name: nbt.string('a') },
    { Count: nbt.byte(0), Damage: nbt.short(0), Name: nbt.string('b') },
    { Count: nbt.byte(0), Damage: nbt.short(0), Name: nbt.string('c') }
  ]))
})
nbt.writeUncompressed(tag) // now do something with this nbt buffer...

See index.d.ts for methods

Browser usage

For webpack usage, see an example configuration here.

For a web bundle with browserify (after you ran npm install prismarine-nbt in your project):

npx browserify -r prismarine-nbt -r buffer -o pnbt.js
<script src="./pnbt.js"></script>
<script>
  const nbt = require('prismarine-nbt')
  const { Buffer } = require('buffer')
  fetch('test.nbt').then(resp => resp.arrayBuffer())
    .then(buf => nbt.parse(Buffer.from(buf))).then(console.log)
</script>