Skip to content

Commit

Permalink
Use ProtoDef skipChecks flag to allow full NBT array size support (#91
Browse files Browse the repository at this point in the history
)

* feat: use `skipChecks` flag to allow full NBT array size support

NBT arrays are allowed a length up to 32 bits, so skipping checks is necessary to allow full NBT support.

https://minecraft.wiki/w/NBT_format#Binary_format

* feat: add optional `options` to functions

* improve: update type definitions

* chore: use node-protodef dev branch

* fix: `parse` callback type definition

* chore: update docs

* improve: remove unnecessary parameter defaults

* Revert "improve: remove unnecessary parameter defaults"

This reverts commit d1b29fe.

* Revert "chore: update docs"

This reverts commit 522459c.

* Revert "fix: `parse` callback type definition"

This reverts commit c599564.

* Revert "feat: add optional `options` to functions"

This reverts commit ad494ea.

* Revert "feat: use `skipChecks` flag to allow full NBT array size support"

This reverts commit 6457edb.

* feat: setskip checks variable

* fix: properly set variable to prototype

* revert: previous approach types

* improve: setVariable in parse function

* chore: apply lint standard

* improve: simplify variable setting and don't update

* chore: update docs

* Revert "chore: use node-protodef dev branch"

This reverts commit 867b301.

* fix: lint
  • Loading branch information
bdkopen authored Oct 31, 2024
1 parent 70323d8 commit f433bc8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,18 @@ Minecraft Java Edition uses big-endian format, and Bedrock uses little-endian.

Returns a buffer with a serialized nbt `value`.

### parseUncompressed(data, format='big')
### 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)

Expand Down
11 changes: 9 additions & 2 deletions nbt.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ function writeUncompressed (value, proto = 'big') {
return protos[proto].createPacketBuffer('nbt', value)
}

function parseUncompressed (data, proto = 'big') {
function parseUncompressed (data, proto = 'big', options = {}) {
if (proto === true) proto = 'little'

protos[proto].setVariable('noArraySizeCheck', options.noArraySizeCheck)

return protos[proto].parsePacketBuffer('nbt', data, data.startOffset).data
}

Expand All @@ -70,7 +73,7 @@ const hasGzipHeader = function (data) {
const hasBedrockLevelHeader = (data) =>
data[1] === 0 && data[2] === 0 && data[3] === 0

async function parseAs (data, type) {
async function parseAs (data, type, options = {}) {
if (hasGzipHeader(data)) {
data = await new Promise((resolve, reject) => {
zlib.gunzip(data, (error, uncompressed) => {
Expand All @@ -79,7 +82,11 @@ async function parseAs (data, type) {
})
})
}

protos[type].setVariable('noArraySizeCheck', options.noArraySizeCheck)

const parsed = protos[type].parsePacketBuffer('nbt', data, data.startOffset)

parsed.metadata.buffer = data
parsed.type = type
return parsed
Expand Down
7 changes: 6 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ declare module 'prismarine-nbt'{
[TagType.LongArray]: LongArray;
}

interface ParseOptions {
noArraySizeCheck?: boolean;
}

export type NBTFormat = 'big' | 'little' | 'littleVarint'

export type NBT = Tags['compound'] & {name: string};
Expand All @@ -74,7 +78,8 @@ declare module 'prismarine-nbt'{
size: number
}
export function writeUncompressed(value: NBT, format?: NBTFormat): Buffer;
export function parseUncompressed(value: Buffer, format?: NBTFormat): NBT;
export function parseUncompressed(value: Buffer, format?: NBTFormat, options?: ParseOptions): NBT;
export function parseAs(value: Buffer, type: NBTFormat, options?: ParseOptions): Promise<{parsed: NBT, type: NBTFormat, metadata: Metadata}>;

export function parse(data: Buffer, nbtType?: NBTFormat): Promise<{parsed: NBT, type: NBTFormat, metadata: Metadata}>;
export function simplify(data: Tags[TagType]): any
Expand Down

0 comments on commit f433bc8

Please sign in to comment.