Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improvements to usage and browser compat #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const NBT = require('mcnbt');
There're several function for you to parse an NBT structure after you instantiating
an object:

+ `loadFromBuffer(buff, callback)`
+ `loadFromBuffer(buff, callback?)`: callback is optional
+ `loadFromZlibCompressedBuffer(buff, callback)`
+ `loadFromFile(filename, callback)`
+ `loadFromZlibCompressedFile(filename, callback)`
Expand Down Expand Up @@ -112,8 +112,7 @@ No extra method.

### TAGLong

The value's type is long long ([bignum](https://github.com/justmoon/node-bignum)
in js).
The value's type is long (bigint in js).

No extra method.

Expand All @@ -125,7 +124,7 @@ No extra method.

### TAGDouble

The value's type is double (nbumer in js).
The value's type is double (number in js).

No extra method.

Expand Down
6 changes: 2 additions & 4 deletions lib/base_tag.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const Long = require('long');

const TAG_TYPE_OFFSET = 1;
let _tagIds;

Expand Down Expand Up @@ -194,8 +192,8 @@ class BaseTag {
return val;
}

if (val instanceof Long) {
return val;
if (typeof val === 'bigint') {
return val.toString();
}

if (this.type === 'TAG_Int_Array' || this.type === 'TAG_Byte_Array') {
Expand Down
104 changes: 0 additions & 104 deletions lib/bvffer.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/tags/byte_array.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class TAGByteArray extends BaseTag {

/**
* Write body to buffer
* @param {Bvffer} buff The buffer to write to
* @param {Buffer} buff The buffer to write to
* @param {number} offset The offset to start writing from
*/
writeBuffer(buff) {
Expand Down
2 changes: 1 addition & 1 deletion lib/tags/compound.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class TAGCompound extends BaseTag {

/**
* Write body to buffer
* @param {Bvffer} buff The buffer to write to
* @param {Buffer} buff The buffer to write to
* @param {number} offset The offset to start writing to
*/
writeBuffer(buff) {
Expand Down
2 changes: 1 addition & 1 deletion lib/tags/int.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TAGInt extends BaseTag {

/**
* Write body to buffer
* @param {Bvffer} buff The buffer to write to
* @param {Buffer} buff The buffer to write to
*/
writeBuffer(buff) {
buff.writeInt32BE(this.value);
Expand Down
23 changes: 9 additions & 14 deletions lib/tags/long.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict';

const BaseTag = require('../base_tag');
const Long = require('long');

const LONG_BOUND = {
MIN: Long.fromString('-9223372036854775808'),
MAX: Long.fromString('9223372036854775807'),
MIN: BigInt('-9223372036854775808'),
MAX: BigInt('9223372036854775807'),
};

class TAGLong extends BaseTag {
Expand All @@ -15,16 +14,12 @@ class TAGLong extends BaseTag {
}

_readBodyFromBuffer(buff, offset) {
const sliced = buff.slice(offset, offset + 8);
this.value = Long.fromBytesBE(sliced, true);
this.value = buff.readBigInt64BE(offset);
return 8;
}

writeBuffer(buff) {
const bytes = this.value.toBytesBE();
for (let i = 0; i < 8; i++) {
buff.writeUInt8(bytes[i]);
}
buff.writeBigInt64BE(this.value);
}

toJSON() {
Expand All @@ -34,18 +29,18 @@ class TAGLong extends BaseTag {
setValue(value) {
let temp = -1;
if (typeof value === 'string') {
temp = new Long(value);
} else if (value instanceof Long) {
temp = Long.fromString(value.toString());
temp = BigInt(value);
} else if (typeof value === 'bigint') {
temp = value;
} else if (typeof value === 'number' && !isNaN(value)) {
temp = Long.fromNumber(value);
temp = BigInt(value);
}

if (temp === -1) {
throw new Error('Wrong type to set TAG_Long\'s value.');
}

if (temp.lessThan(LONG_BOUND.MIN) || temp.greaterThan(LONG_BOUND.MAX)) {
if (temp < LONG_BOUND.MIN || temp > LONG_BOUND.MAX) {
throw new RangeError(
'Value of TAG_Long should between -9223372036854775808 and ' +
'9223372036854775807');
Expand Down
2 changes: 1 addition & 1 deletion lib/tags/short.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TAGShort extends BaseTag {

/**
* Write body to buffer
* @param {Bvffer} buff The buffer to write to
* @param {Buffer} buff The buffer to write to
*/
writeBuffer(buff) {
buff.writeInt16BE(this.value);
Expand Down
20 changes: 13 additions & 7 deletions nbt.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

const fs = require('fs');
const zlib = require('zlib');
let fs;
try { fs = require('fs'); } catch (e) { /* Not running on Node.js */ }
let zlib;
try { zlib = require('zlib'); } catch (e) { /* Not running on Node.js */ }

const Tag = require('./lib/base_tag');
const { Bvffer } = require('./lib/bvffer');

/**
* The NBT class
Expand All @@ -18,9 +19,10 @@ class NBT {
/**
* Load from buffer
* @param {Buffer} buff The buffer to load from
* @param {(err?: Error) => void} callback The callback to call when done
* @param {(err?: Error) => void} [callback] The callback to call when done
*/
loadFromBuffer(buff, callback) {
let err;
try {
this._buff = buff;
let offset = 0;
Expand All @@ -33,10 +35,14 @@ class NBT {
offset += len;
}
} catch (e) {
return callback(e);
err = e;
}

callback();
if (callback) {
callback(err);
} else {
throw err;
}
}

/**
Expand Down Expand Up @@ -73,7 +79,7 @@ class NBT {
* @return {Buffer} The buffer
*/
writeToBuffer() {
const buff = new Bvffer(this.calcBufferLength());
const buff = new Buffer(this.calcBufferLength());
for (const key in this.root) {
if (!this.root.hasOwnProperty(key)) continue;

Expand Down
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@
"url": "https://github.com/XadillaX/mcnbt/issues"
},
"homepage": "https://github.com/XadillaX/mcnbt",
"dependencies": {
"long": "^4.0.0"
},
"types": "types/nbt.d.ts",
"devDependencies": {
"@types/long": "^4.0.1",
"@types/node": "^17.0.18",
"eslint": "^8.9.0",
"eslint-config-egg": "^11.0.0",
Expand Down
11 changes: 9 additions & 2 deletions test/archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
const fs = require('fs');
const util = require('util');

const Long = require('long');
const should = require('should');

const NBT = require('../nbt');

function tryBigInt(value) {
try {
return BigInt(value);
} catch (e) {
return BigInt(0);
}
}

describe('Real archive test', function() {
const archive = new NBT();
let stdData = fs.readFileSync(__dirname + '/files/level.dat.json', {
Expand Down Expand Up @@ -58,7 +65,7 @@ describe('Real archive test', function() {
} else if (typeof result === 'string') {
if (result === '') {
node.getType().should.match(/^TAG_String/);
} else if (Long.fromString(result).toString() === result) {
} else if (tryBigInt(result).toString() === result) {
node.getType().should.match(/^TAG_(Long|String)$/);
} else {
node.getType().should.match(/^TAG_String$/);
Expand Down
11 changes: 9 additions & 2 deletions test/bigtest_archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
const fs = require('fs');
const util = require('util');

const Long = require('long');
const should = require('should');

const NBT = require('../nbt');

function tryBigInt(value) {
try {
return BigInt(value);
} catch (e) {
return BigInt(0);
}
}

describe('Real bugtest archive test', function() {
const archive = new NBT();
const stdData = require('./files/bigtest.nbt.json');
Expand Down Expand Up @@ -57,7 +64,7 @@ describe('Real bugtest archive test', function() {
} else if (typeof result === 'object' && !util.isArray(result)) {
node.getType().should.be.eql('TAG_Compound');
} else if (typeof result === 'string') {
if (Long.fromString(result).toString() === result) {
if (tryBigInt(result).toString() === result) {
node.getType().should.match(/^TAG_(String|Long)$/);
} else {
node.getType().should.be.eql('TAG_String');
Expand Down
30 changes: 0 additions & 30 deletions types/lib/bvffer.d.ts

This file was deleted.

Loading