From 3e02aa542671f8f8e3265d243cd421cbd4603da7 Mon Sep 17 00:00:00 2001 From: RichardDorian Date: Mon, 4 Apr 2022 17:21:25 +0200 Subject: [PATCH] feat: :sparkles: add uuids + rename varint --- README.md | 1 + package.json | 2 +- src/index.ts | 64 ++++++++++++++++++++++++++++++++++++++------- test/uuid.test.js | 42 +++++++++++++++++++++++++++++ test/varint.test.js | 8 +++--- 5 files changed, 102 insertions(+), 15 deletions(-) create mode 100644 test/uuid.test.js diff --git a/README.md b/README.md index 77d5be8..d72fb62 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/package.json b/package.json index 0003726..34629bd 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/index.ts b/src/index.ts index fa0fdc7..fca0306 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,11 +22,11 @@ export default class BufWrapper { * @example * ```javascript * const buf = new BufWrapper(); - * buf.writeVarint(300); + * buf.writeVarInt(300); * console.log(buf.buffer); // * ``` */ - public writeVarint(value: number): void { + public writeVarInt(value: number): void { const encoded = varint.encode(value); this.buffer = Buffer.concat([this.buffer, Buffer.from(encoded)]); } @@ -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; @@ -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)]); } @@ -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, @@ -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)); } @@ -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()); @@ -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)); } @@ -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); // + * ``` + */ + 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; + } } diff --git a/test/uuid.test.js b/test/uuid.test.js new file mode 100644 index 0000000..817aab7 --- /dev/null +++ b/test/uuid.test.js @@ -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'); + }); +}); diff --git a/test/varint.test.js b/test/varint.test.js index f70143a..2dd1ce7 100644 --- a/test/varint.test.js +++ b/test/varint.test.js @@ -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); }); });