Skip to content

Commit

Permalink
feat: ✨ add uuids + rename varint
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardDorian committed Apr 4, 2022
1 parent 380244c commit 3e02aa5
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Encode and decode:
- Long
- Array of strings
- Array of ints
- UUID

⚠️ They are all signed and big endian (As Java does)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@minecraft-js/bufwrapper",
"version": "1.0.1",
"version": "1.2.0",
"description": "Encode and decode data using buffers",
"main": "dist/index.js",
"scripts": {
Expand Down
64 changes: 54 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export default class BufWrapper {
* @example
* ```javascript
* const buf = new BufWrapper();
* buf.writeVarint(300);
* buf.writeVarInt(300);
* console.log(buf.buffer); // <Buffer ac 02>
* ```
*/
public writeVarint(value: number): void {
public writeVarInt(value: number): void {
const encoded = varint.encode(value);
this.buffer = Buffer.concat([this.buffer, Buffer.from(encoded)]);
}
Expand All @@ -38,11 +38,11 @@ export default class BufWrapper {
* ```javascript
* const buffer = Buffer.from([0xac, 0x02]);
* const buf = new BufWrapper(buffer);
* const decoded = buf.readVarint();
* const decoded = buf.readVarInt();
* console.log(decoded); // 300
* ```
*/
public readVarint(): number {
public readVarInt(): number {
const value = varint.decode(this.buffer, this.offset);
this.offset += varint.decode.bytes;
return value;
Expand All @@ -59,7 +59,7 @@ export default class BufWrapper {
* ```
*/
public writeString(value: string): void {
this.writeVarint(value.length);
this.writeVarInt(value.length);
this.buffer = Buffer.concat([this.buffer, Buffer.from(value)]);
}

Expand All @@ -75,7 +75,7 @@ export default class BufWrapper {
* ```
*/
public readString(): string {
const length = this.readVarint();
const length = this.readVarInt();
const value = this.buffer.toString(
'utf8',
this.offset,
Expand Down Expand Up @@ -163,7 +163,7 @@ export default class BufWrapper {
* ```
*/
public writeStringArray(value: string[]): void {
this.writeVarint(value.length);
this.writeVarInt(value.length);
value.forEach((v) => this.writeString(v));
}

Expand All @@ -179,7 +179,7 @@ export default class BufWrapper {
* ```
*/
public readStringArray(): string[] {
const length = this.readVarint();
const length = this.readVarInt();
const value: string[] = [];
for (let i = 0; i < length; i++) {
value.push(this.readString());
Expand All @@ -198,7 +198,7 @@ export default class BufWrapper {
* ```
*/
public writeIntArray(value: number[]): void {
this.writeVarint(value.length);
this.writeVarInt(value.length);
value.forEach((v) => this.writeInt(v));
}

Expand All @@ -214,11 +214,55 @@ export default class BufWrapper {
* ```
*/
public readIntArray(): number[] {
const length = this.readVarint();
const length = this.readVarInt();
const value: number[] = [];
for (let i = 0; i < length; i++) {
value.push(this.readInt());
}
return value;
}

/**
* Write an UUID to the buffer
* @param value The value to write (string, uuid with dashes or not)
* @example
* ```javascript
* const buf = new BufWrapper();
* buf.writeUUID('c09b74b4-8c14-44cb-b567-6576a2daf1f9');
* console.log(buf.buffer); // <Buffer C0 9B 74 B4 8C 14 44 CB B5 67 65 76 A2 DA F1 F9>
* ```
*/
public writeUUID(value: string): void {
const buf = Buffer.alloc(16);
buf.write(value.replace(/-/g, ''), 0, 'hex');
this.buffer = Buffer.concat([this.buffer, buf]);
}

/**
* Read an UUID from the buffer
* @param {boolean} [dashes] If true, the UUID will be returned with dashes. Otherwise, it will be returned without dashes. Defaults to true.
* @returns The UUID read from the buffer
* @example
* ```javascript
* const buffer = Buffer.from([ 0xC0, 0x9B, 0x74, 0xB4, 0x8C, 0x14, 0x44, 0xCB, 0xB5, 0x67, 0x65, 0x76, 0xA2, 0xDA, 0xF1, 0xF9 ]);
* const buf = new BufWrapper(buffer);
* const decoded = buf.readUUID();
* console.log(decoded); // c09b74b4-8c14-44cb-b567-6576a2daf1f9
* ```
*/
public readUUID(dashes = true): string {
const value = this.buffer.toString('hex', this.offset, this.offset + 16);
this.offset += 16;
return dashes
? value.substr(0, 8) +
'-' +
value.substr(8, 4) +
'-' +
value.substr(12, 4) +
'-' +
value.substr(16, 4) +
'-' +
value.substr(20)
: value;
}
}
42 changes: 42 additions & 0 deletions test/uuid.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { assert } = require('chai');
const BufWrapper = require('../dist').default;

describe('UUID', () => {
it('Write 1', () => {
const buf = new BufWrapper();
buf.writeUUID('c09b74b4-8c14-44cb-b567-6576a2daf1f9');
assert.equal(
buf.buffer.toString('hex'),
'c09b74b48c1444cbb5676576a2daf1f9'
);
});

it('Write 2', () => {
const buf = new BufWrapper();
buf.writeUUID('12ba1036085a49c29fbd24517289ce58');
assert.equal(
buf.buffer.toString('hex'),
'12ba1036085a49c29fbd24517289ce58'
);
});

it('Read 1', () => {
const buf = new BufWrapper(
Buffer.from([
0xc0, 0x9b, 0x74, 0xb4, 0x8c, 0x14, 0x44, 0xcb, 0xb5, 0x67, 0x65, 0x76,
0xa2, 0xda, 0xf1, 0xf9,
])
);
assert.equal(buf.readUUID(), 'c09b74b4-8c14-44cb-b567-6576a2daf1f9');
});

it('Read 2', () => {
const buf = new BufWrapper(
Buffer.from([
0x12, 0xba, 0x10, 0x36, 0x08, 0x5a, 0x49, 0xc2, 0x9f, 0xbd, 0x24, 0x51,
0x72, 0x89, 0xce, 0x58,
])
);
assert.equal(buf.readUUID(false), '12ba1036085a49c29fbd24517289ce58');
});
});
8 changes: 4 additions & 4 deletions test/varint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ const BufWrapper = require('../dist').default;
describe('Varint', () => {
it('Write 1', () => {
const buf = new BufWrapper();
buf.writeVarint(42);
buf.writeVarInt(42);
assert.equal(buf.buffer.toString('hex'), '2a');
});

it('Write 2', () => {
const buf = new BufWrapper();
buf.writeVarint(300);
buf.writeVarInt(300);
assert.equal(buf.buffer.toString('hex'), 'ac02');
});

it('Read 1', () => {
const buf = new BufWrapper(Buffer.from([0x2a]));
assert.equal(buf.readVarint(), 42);
assert.equal(buf.readVarInt(), 42);
});

it('Read 2', () => {
const buf = new BufWrapper(Buffer.from([0xac, 0x02]));
assert.equal(buf.readVarint(), 300);
assert.equal(buf.readVarInt(), 300);
});
});

0 comments on commit 3e02aa5

Please sign in to comment.