Skip to content
This repository has been archived by the owner on May 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request #5 from lgou2w/develop
Browse files Browse the repository at this point in the history
v0.1.2
  • Loading branch information
lgou2w authored May 14, 2020
2 parents 8b89805 + 3982df1 commit b4f4f9b
Show file tree
Hide file tree
Showing 31 changed files with 1,637 additions and 329 deletions.
26 changes: 26 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
codecov:
notify:
require_ci_to_pass: yes

coverage:
precision: 2
round: down
range: "50...80"

status:
project: yes
patch: yes
changes: no

parsers:
gcov:
branch_detection:
conditional: yes
loop: yes
method: no
macro: no

comment:
layout: "header, diff"
behavior: default
require_changes: no
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,4 @@ dist
dist/
lib/
!test/
coverage/
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ sudo: false
language: node_js
node_js:
- '10'
branches:
only:
- master
- develop
script:
- npm run test && npm run build
after_success:
- bash <(curl -s https://codecov.io/bash) -f coverage/cobertura.xml
cache:
directories:
- node_modules
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<p>
<a href="https://github.com/lgou2w/l2nbt.js/actions"><img src="https://img.shields.io/github/workflow/status/lgou2w/l2nbt.js/Node CI/develop?logo=github&style=flat-square" /></a>
<a href="https://travis-ci.org/lgou2w/l2nbt.js"><img src="https://img.shields.io/travis/lgou2w/l2nbt.js?style=flat-square&logo=travis" /></a>
<a href="https://codecov.io/gh/lgou2w/l2nbt.js/branch/develop"><img src="https://img.shields.io/codecov/c/github/lgou2w/l2nbt.js/develop?label=codecov&logo=codecov&style=flat-square" /></a>
<a href="https://www.npmjs.com/package/l2nbt"><img src="https://img.shields.io/npm/v/l2nbt?logo=npm&style=flat-square" /></a>
<a href="https://github.com/lgou2w/l2nbt.js/blob/master/LICENSE"><img src="https://img.shields.io/npm/l/l2nbt?logo=apache&style=flat-square" /></a>
</p>
Expand All @@ -11,16 +12,22 @@ A lgou2w Minecraft NBT library for Javascript

## Install

```shell
```
npm install l2nbt --save
```

or

```shell
```
yarn add l2nbt
```

There is no default export. The correct way to import `l2nbt.js` is:

```javascript
import * as l2nbt from 'l2nbt'
```

## API

TODO
Expand Down
27 changes: 17 additions & 10 deletions karma.config.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
const webpackConfig = require('./webpack.config')

process.env.CHROME_BIN = process.env.CHROME_BIN || require('puppeteer').executablePath()

module.exports = (config) => {
config.set({
basePath: '',
frameworks: ['mocha'],
files: ['test/**/*.spec.ts'],
frameworks: ['mocha', 'karma-typescript'],
files: [
'src/**/*.ts',
'test/**/*.ts'
],
reporters: ['progress', 'karma-typescript', 'coverage'],
preprocessors: {
'**/*.ts': ['webpack']
'src/**/*.ts': ['karma-typescript', 'coverage'],
'test/**/*.ts': ['karma-typescript']
},
webpack: webpackConfig,
webpackMiddleware: {
stats: 'errors-only'
coverageReporter: {
dir: 'coverage/',
reporters: [
{ type: 'json', subdir: '.', file: 'coverage.json' },
{ type: 'cobertura', subdir: '.', file: 'cobertura.xml' },
{ type: 'html' }
]
},
mime: {
'text/x-typescript': ['ts']
},
plugins: [
require('karma-chrome-launcher'),
require('karma-mocha'),
require('karma-webpack')
require('karma-typescript'),
require('karma-coverage')
],
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
Expand Down
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
{
"name": "l2nbt",
"version": "0.1.1",
"version": "0.1.2",
"description": "A lgou2w Minecraft NBT library for Javascript",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"clean": "rimraf lib dist",
"clean": "npm run clean:build && npm run clean:coverage",
"clean:build": "rimraf lib dist",
"clean:coverage": "rimraf coverage",
"build": "npm run build:prod",
"build:dev": "npm run clean && tsc && cross-env NODE_ENV=development webpack",
"build:prod": "npm run clean && tsc && cross-env NODE_ENV=production webpack",
"build:dev": "npm run clean:build && tsc && cross-env NODE_ENV=development webpack",
"build:prod": "npm run clean:build && tsc && cross-env NODE_ENV=production webpack",
"test": "npm run test:browser && npm run test:node",
"test:browser": "cross-env NODE_ENV=development karma start karma.config.js",
"test:browser": "rimraf coverage && cross-env NODE_ENV=development karma start karma.config.js",
"test:node": "cross-env NODE_ENV=development ts-mocha -p tsconfig.json test/**/*.spec.ts --exit",
"lint": "eslint --ext .js,.ts,.tsx --ignore-path .gitignore .",
"prepublishOnly": "npm test && npm run build"
Expand Down Expand Up @@ -56,8 +58,9 @@
"eslint-plugin-standard": "^4.0.1",
"karma": "^5.0.5",
"karma-chrome-launcher": "^3.1.0",
"karma-coverage": "^2.0.2",
"karma-mocha": "^2.0.1",
"karma-webpack": "^4.0.2",
"karma-typescript": "^5.0.2",
"mocha": "^7.1.2",
"puppeteer": "^3.0.4",
"rimraf": "^3.0.2",
Expand Down
4 changes: 2 additions & 2 deletions src/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function encode (input: string) {
) {
charCode = str.charCodeAt (idx += 3 / 4);
if (charCode > 0xFF) {
throw new Error("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.");
throw new Error('Encode failed: The string to be encoded contains characters outside of the Latin1 range.');
}
// @ts-ignore
block = block << 8 | charCode;
Expand All @@ -49,7 +49,7 @@ export function encode (input: string) {
export function decode (input: string) {
var str = (String (input)).replace (/[=]+$/, ''); // #31: ExtendScript bad parse of /=
if (str.length % 4 === 1) {
throw new Error("'atob' failed: The string to be decoded is not correctly encoded.");
throw new Error('Decode failed: The string to be decoded is not correctly encoded.');
}
for (
// initialize result and counters
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export {
NBTList,
NBTCompound,
isNBT,
tag,
tagByte,
tagShort,
tagInt,
Expand All @@ -17,7 +18,8 @@ export {
tagList,
tagCompound,
tagIntArray,
tagLongArray
tagLongArray,
resolve
} from './nbt'

export {
Expand Down
9 changes: 6 additions & 3 deletions src/nbt-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
tagList,
tagCompound,
tagIntArray,
tagLongArray
tagLongArray,
resolve
} from './nbt'

/// Write Mojangson
Expand Down Expand Up @@ -455,11 +456,13 @@ class MojangsonParser {
export function readMojangson (mojangsonWithoutColor: string): NBT {
const reader = new StringReader(mojangsonWithoutColor)
const parser = new MojangsonParser(reader)
return parser.readValue()
const tag = parser.readValue()
return resolve(tag)
}

export function readMojangsonCompound (mojangsonWithoutColor: string): NBTCompound {
const reader = new StringReader(mojangsonWithoutColor)
const parser = new MojangsonParser(reader)
return parser.readSingleStruct() as NBTCompound
const compound = parser.readSingleStruct() as NBTCompound
return resolve(compound)
}
17 changes: 9 additions & 8 deletions src/nbt-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class NBTWriter {
const c = value.charCodeAt(i)
if ((c >= 0x0001) && (c <= 0x007F)) {
utflen++
} else if (c > 0x007F) {
} else if (c > 0x07FF) {
utflen += 3
} else {
utflen += 2
Expand Down Expand Up @@ -169,20 +169,20 @@ function writeValue (writer: NBTWriter, nbt: NBT): NBTWriter {
case NBTTypes.TAG_LIST:
return writeList(writer, nbt)
case NBTTypes.TAG_COMPOUND:
return writeCompound(writer, nbt as NBTCompound)
return writeCompound(writer, nbt)
default:
throw new Error(`Unsupported nbt type id: ${type}`)
}
}

function writeArray (writer: NBTWriter, value: NBT[], type: NBTType): NBTWriter {
function writeArray (writer: NBTWriter, value: (number | bigint)[], type: NBTType): NBTWriter {
writer.writeInt(value.length)
for (let i = 0; i < value.length; i++) {
const val = value[i].__value__
const val = value[i]
type === NBTTypes.TAG_BYTE_ARRAY
? writer.writeByte(val)
? writer.writeByte(val as number)
: type === NBTTypes.TAG_INT_ARRAY
? writer.writeInt(val)
? writer.writeInt(val as number)
: writer.writeLong(val)
}
return writer
Expand Down Expand Up @@ -213,8 +213,9 @@ function writeList (writer: NBTWriter, nbt: NBTList): NBTWriter {
}

function writeCompound (writer: NBTWriter, nbt: NBTCompound): NBTWriter {
for (const key in nbt) {
const val = nbt[key]
const values: {[key: string]: NBT} = nbt.__value__
for (const key in values) {
const val = values[key]
writeMetadata(writer, <NBTMetadata> { type: val.__type__, name: key })
writeValue(writer, val)
}
Expand Down
Loading

0 comments on commit b4f4f9b

Please sign in to comment.