Skip to content

Commit f433bc8

Browse files
authored
Use ProtoDef skipChecks flag to allow full NBT array size support (#91)
* 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
1 parent 70323d8 commit f433bc8

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,18 @@ Minecraft Java Edition uses big-endian format, and Bedrock uses little-endian.
5858

5959
Returns a buffer with a serialized nbt `value`.
6060

61-
### parseUncompressed(data, format='big')
61+
### parseUncompressed(data, format='big', options?= {noArraySizeCheck?: boolean})
6262

6363
Takes a buffer `data` and returns a parsed nbt value.
6464

65+
The `options` parameter is optional. When `noArraySizeCheck` is `true`, an array size check is disabled which allows for parsing of large arrays.
66+
67+
### parseAs(data, type, options?= {noArraySizeCheck?: boolean})
68+
69+
Takes a buffer `data` and returns a parsed nbt value. If the buffer is gzipped, it will unzip the data first.
70+
71+
The `options` parameter is optional. When `noArraySizeCheck` is `true`, an array size check is disabled which allows for parsing of large arrays.
72+
6573

6674
### simplify(nbt)
6775

nbt.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@ function writeUncompressed (value, proto = 'big') {
5555
return protos[proto].createPacketBuffer('nbt', value)
5656
}
5757

58-
function parseUncompressed (data, proto = 'big') {
58+
function parseUncompressed (data, proto = 'big', options = {}) {
5959
if (proto === true) proto = 'little'
60+
61+
protos[proto].setVariable('noArraySizeCheck', options.noArraySizeCheck)
62+
6063
return protos[proto].parsePacketBuffer('nbt', data, data.startOffset).data
6164
}
6265

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

73-
async function parseAs (data, type) {
76+
async function parseAs (data, type, options = {}) {
7477
if (hasGzipHeader(data)) {
7578
data = await new Promise((resolve, reject) => {
7679
zlib.gunzip(data, (error, uncompressed) => {
@@ -79,7 +82,11 @@ async function parseAs (data, type) {
7982
})
8083
})
8184
}
85+
86+
protos[type].setVariable('noArraySizeCheck', options.noArraySizeCheck)
87+
8288
const parsed = protos[type].parsePacketBuffer('nbt', data, data.startOffset)
89+
8390
parsed.metadata.buffer = data
8491
parsed.type = type
8592
return parsed

typings/index.d.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ declare module 'prismarine-nbt'{
6464
[TagType.LongArray]: LongArray;
6565
}
6666

67+
interface ParseOptions {
68+
noArraySizeCheck?: boolean;
69+
}
70+
6771
export type NBTFormat = 'big' | 'little' | 'littleVarint'
6872

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

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

0 commit comments

Comments
 (0)