diff --git a/.gitignore b/.gitignore
index 710ba61..c6bba59 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,3 @@
-# Custom
-lib/
-
# Logs
logs
*.log
diff --git a/lib/index.d.ts b/lib/index.d.ts
new file mode 100644
index 0000000..f1a08b2
--- /dev/null
+++ b/lib/index.d.ts
@@ -0,0 +1,9 @@
+///
+import { IReader } from "./readers/IReader";
+import { IWriter } from "./writers/IWriter";
+export declare enum Endian {
+ LE = 0,
+ BE = 1
+}
+export declare function createReader(endianness: Endian, buffer: Buffer): IReader;
+export declare function createWriter(endianness: Endian, size?: number): IWriter;
diff --git a/lib/index.js b/lib/index.js
new file mode 100644
index 0000000..9714766
--- /dev/null
+++ b/lib/index.js
@@ -0,0 +1,30 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.createWriter = exports.createReader = exports.Endian = void 0;
+const ReaderBE_1 = require("./readers/ReaderBE");
+const ReaderLE_1 = require("./readers/ReaderLE");
+const WriterBE_1 = require("./writers/WriterBE");
+const WriterLE_1 = require("./writers/WriterLE");
+var Endian;
+(function (Endian) {
+ Endian[Endian["LE"] = 0] = "LE";
+ Endian[Endian["BE"] = 1] = "BE";
+})(Endian = exports.Endian || (exports.Endian = {}));
+function createReader(endianness, buffer) {
+ if (endianness === Endian.LE) {
+ return new ReaderLE_1.ReaderLE(buffer);
+ }
+ else {
+ return new ReaderBE_1.ReaderBE(buffer);
+ }
+}
+exports.createReader = createReader;
+function createWriter(endianness, size) {
+ if (endianness === Endian.LE) {
+ return new WriterLE_1.WriterLE(size);
+ }
+ else {
+ return new WriterBE_1.WriterBE(size);
+ }
+}
+exports.createWriter = createWriter;
diff --git a/lib/readers/IReader.d.ts b/lib/readers/IReader.d.ts
new file mode 100644
index 0000000..311c505
--- /dev/null
+++ b/lib/readers/IReader.d.ts
@@ -0,0 +1,19 @@
+///
+export interface IReader {
+ readBuffer(bytes: number): Buffer;
+ readUint8Array(bytes: number): Uint8Array;
+ readByte(): number;
+ readUByte(): number;
+ readBool(): boolean;
+ readShort(): number;
+ readUShort(): number;
+ readInt(): number;
+ readUInt(): number;
+ readLong(): bigint;
+ readULong(): bigint;
+ readFloat(): number;
+ readDouble(): number;
+ readShortString(): string;
+ readString(): string;
+ readString16(): string;
+}
diff --git a/lib/readers/IReader.js b/lib/readers/IReader.js
new file mode 100644
index 0000000..c8ad2e5
--- /dev/null
+++ b/lib/readers/IReader.js
@@ -0,0 +1,2 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
diff --git a/lib/readers/ReaderBE.d.ts b/lib/readers/ReaderBE.d.ts
new file mode 100644
index 0000000..528cc2d
--- /dev/null
+++ b/lib/readers/ReaderBE.d.ts
@@ -0,0 +1,15 @@
+import { IReader } from "./IReader";
+import { ReaderBase } from "./ReaderBase";
+export declare class ReaderBE extends ReaderBase implements IReader {
+ readShort(): number;
+ readUShort(): number;
+ readInt(): number;
+ readUInt(): number;
+ readLong(): bigint;
+ readULong(): bigint;
+ readFloat(): number;
+ readDouble(): number;
+ readShortString(): string;
+ readString(): string;
+ readString16(): string;
+}
diff --git a/lib/readers/ReaderBE.js b/lib/readers/ReaderBE.js
new file mode 100644
index 0000000..4d35167
--- /dev/null
+++ b/lib/readers/ReaderBE.js
@@ -0,0 +1,71 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.ReaderBE = void 0;
+const ReaderBase_1 = require("./ReaderBase");
+class ReaderBE extends ReaderBase_1.ReaderBase {
+ readShort() {
+ const value = this.buffer.readInt16BE(this.offset);
+ this.offset += 2;
+ return value;
+ }
+ readUShort() {
+ const value = this.buffer.readUInt16BE(this.offset);
+ this.offset += 2;
+ return value;
+ }
+ readInt() {
+ const value = this.buffer.readInt32BE(this.offset);
+ this.offset += 4;
+ return value;
+ }
+ readUInt() {
+ const value = this.buffer.readUInt32BE(this.offset);
+ this.offset += 4;
+ return value;
+ }
+ readLong() {
+ const value = this.buffer.readBigInt64BE(this.offset);
+ this.offset += 8;
+ return value;
+ }
+ readULong() {
+ const value = this.buffer.readBigUint64BE(this.offset);
+ this.offset += 8;
+ return value;
+ }
+ readFloat() {
+ const value = this.buffer.readFloatBE(this.offset);
+ this.offset += 4;
+ return value;
+ }
+ readDouble() {
+ const value = this.buffer.readDoubleBE(this.offset);
+ this.offset += 8;
+ return value;
+ }
+ readShortString() {
+ const length = this.readUByte();
+ let text = "";
+ for (let i = 0; i < length; i++) {
+ text += String.fromCharCode(this.readUByte());
+ }
+ return text;
+ }
+ readString() {
+ const length = this.readUShort();
+ let text = "";
+ for (let i = 0; i < length; i++) {
+ text += String.fromCharCode(this.readUByte());
+ }
+ return text;
+ }
+ readString16() {
+ const length = this.readUShort();
+ let text = "";
+ for (let i = 0; i < length; i++) {
+ text += String.fromCharCode(this.readUShort());
+ }
+ return text;
+ }
+}
+exports.ReaderBE = ReaderBE;
diff --git a/lib/readers/ReaderBase.d.ts b/lib/readers/ReaderBase.d.ts
new file mode 100644
index 0000000..aafdc96
--- /dev/null
+++ b/lib/readers/ReaderBase.d.ts
@@ -0,0 +1,11 @@
+///
+export declare class ReaderBase {
+ buffer: Buffer;
+ offset: number;
+ constructor(buffer: Buffer);
+ readBuffer(bytes: number): Buffer;
+ readUint8Array(bytes: number): Uint8Array;
+ readByte(): number;
+ readUByte(): number;
+ readBool(): boolean;
+}
diff --git a/lib/readers/ReaderBase.js b/lib/readers/ReaderBase.js
new file mode 100644
index 0000000..706304a
--- /dev/null
+++ b/lib/readers/ReaderBase.js
@@ -0,0 +1,38 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.ReaderBase = void 0;
+class ReaderBase {
+ constructor(buffer) {
+ this.buffer = buffer;
+ this.offset = 0;
+ }
+ readBuffer(bytes) {
+ const value = this.buffer.subarray(this.offset, this.offset + bytes);
+ this.offset += bytes;
+ return value;
+ }
+ // NOTE: This has to be a copy as the subarray is only cropped & offset
+ // Realistically this is what we want anyway.
+ readUint8Array(bytes) {
+ const croppedBuffer = this.readBuffer(bytes);
+ const newArray = new Uint8Array(croppedBuffer.length);
+ for (let i = 0; i < croppedBuffer.length; i++) {
+ newArray[i] = croppedBuffer[i];
+ }
+ return newArray;
+ }
+ readByte() {
+ const value = this.buffer.readInt8(this.offset);
+ this.offset++;
+ return value;
+ }
+ readUByte() {
+ const value = this.buffer.readUInt8(this.offset);
+ this.offset++;
+ return value;
+ }
+ readBool() {
+ return Boolean(this.readUByte());
+ }
+}
+exports.ReaderBase = ReaderBase;
diff --git a/lib/readers/ReaderLE.d.ts b/lib/readers/ReaderLE.d.ts
new file mode 100644
index 0000000..b59c151
--- /dev/null
+++ b/lib/readers/ReaderLE.d.ts
@@ -0,0 +1,15 @@
+import { IReader } from "./IReader";
+import { ReaderBase } from "./ReaderBase";
+export declare class ReaderLE extends ReaderBase implements IReader {
+ readShort(): number;
+ readUShort(): number;
+ readInt(): number;
+ readUInt(): number;
+ readLong(): bigint;
+ readULong(): bigint;
+ readFloat(): number;
+ readDouble(): number;
+ readShortString(): string;
+ readString(): string;
+ readString16(): string;
+}
diff --git a/lib/readers/ReaderLE.js b/lib/readers/ReaderLE.js
new file mode 100644
index 0000000..cca6625
--- /dev/null
+++ b/lib/readers/ReaderLE.js
@@ -0,0 +1,71 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.ReaderLE = void 0;
+const ReaderBase_1 = require("./ReaderBase");
+class ReaderLE extends ReaderBase_1.ReaderBase {
+ readShort() {
+ const value = this.buffer.readInt16LE(this.offset);
+ this.offset += 2;
+ return value;
+ }
+ readUShort() {
+ const value = this.buffer.readUInt16LE(this.offset);
+ this.offset += 2;
+ return value;
+ }
+ readInt() {
+ const value = this.buffer.readInt32LE(this.offset);
+ this.offset += 4;
+ return value;
+ }
+ readUInt() {
+ const value = this.buffer.readUInt32LE(this.offset);
+ this.offset += 4;
+ return value;
+ }
+ readLong() {
+ const value = this.buffer.readBigInt64LE(this.offset);
+ this.offset += 8;
+ return value;
+ }
+ readULong() {
+ const value = this.buffer.readBigUint64LE(this.offset);
+ this.offset += 8;
+ return value;
+ }
+ readFloat() {
+ const value = this.buffer.readFloatLE(this.offset);
+ this.offset += 4;
+ return value;
+ }
+ readDouble() {
+ const value = this.buffer.readDoubleLE(this.offset);
+ this.offset += 8;
+ return value;
+ }
+ readShortString() {
+ const length = this.readUByte();
+ let text = "";
+ for (let i = 0; i < length; i++) {
+ text += String.fromCharCode(this.readUByte());
+ }
+ return text;
+ }
+ readString() {
+ const length = this.readUShort();
+ let text = "";
+ for (let i = 0; i < length; i++) {
+ text += String.fromCharCode(this.readUByte());
+ }
+ return text;
+ }
+ readString16() {
+ const length = this.readUShort();
+ let text = "";
+ for (let i = 0; i < length; i++) {
+ text += String.fromCharCode(this.readUShort());
+ }
+ return text;
+ }
+}
+exports.ReaderLE = ReaderLE;
diff --git a/lib/writers/IWriter.d.ts b/lib/writers/IWriter.d.ts
new file mode 100644
index 0000000..f9f0f23
--- /dev/null
+++ b/lib/writers/IWriter.d.ts
@@ -0,0 +1,21 @@
+///
+export interface IWriter {
+ toBuffer(): Buffer;
+ toString(): string;
+ writeBuffer(buffer: Buffer): IWriter;
+ writeUint8Array(array: Uint8Array): IWriter;
+ writeByte(value: number): IWriter;
+ writeUByte(value: number): IWriter;
+ writeBool(value: boolean | number): IWriter;
+ writeShort(value: number): IWriter;
+ writeUShort(value: number): IWriter;
+ writeInt(value: number): IWriter;
+ writeUInt(value: number): IWriter;
+ writeLong(value: number | bigint): IWriter;
+ writeULong(value: number): IWriter;
+ writeFloat(value: number): IWriter;
+ writeDouble(value: number): IWriter;
+ writeShortString(text: string): IWriter;
+ writeString(text: string): IWriter;
+ writeString16(text: string): IWriter;
+}
diff --git a/lib/writers/IWriter.js b/lib/writers/IWriter.js
new file mode 100644
index 0000000..c8ad2e5
--- /dev/null
+++ b/lib/writers/IWriter.js
@@ -0,0 +1,2 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
diff --git a/lib/writers/WriterBE.d.ts b/lib/writers/WriterBE.d.ts
new file mode 100644
index 0000000..9b3069b
--- /dev/null
+++ b/lib/writers/WriterBE.d.ts
@@ -0,0 +1,15 @@
+import { IWriter } from "./IWriter";
+import { WriterBase } from "./WriterBase";
+export declare class WriterBE extends WriterBase implements IWriter {
+ writeShort(value: number): this;
+ writeUShort(value: number): this;
+ writeInt(value: number): this;
+ writeUInt(value: number): this;
+ writeLong(value: number | bigint): this;
+ writeULong(value: number | bigint): this;
+ writeFloat(value: number): this;
+ writeDouble(value: number): this;
+ writeShortString(text: string): this;
+ writeString(text: string): this;
+ writeString16(text: string): this;
+}
diff --git a/lib/writers/WriterBE.js b/lib/writers/WriterBE.js
new file mode 100644
index 0000000..a23a44b
--- /dev/null
+++ b/lib/writers/WriterBE.js
@@ -0,0 +1,130 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.WriterBE = void 0;
+const WriterBase_1 = require("./WriterBase");
+class WriterBE extends WriterBase_1.WriterBase {
+ writeShort(value) {
+ if (this.resizable) {
+ const buffer = Buffer.alloc(2);
+ buffer.writeInt16BE(value);
+ this.writeBuffer(buffer);
+ }
+ else {
+ this.buffer.writeInt16BE(value, this.offset);
+ this.offset += 2;
+ }
+ return this;
+ }
+ writeUShort(value) {
+ if (this.resizable) {
+ const buffer = Buffer.alloc(2);
+ buffer.writeUInt16BE(value);
+ this.writeBuffer(buffer);
+ }
+ else {
+ this.buffer.writeUInt16BE(value, this.offset);
+ this.offset += 2;
+ }
+ return this;
+ }
+ writeInt(value) {
+ if (this.resizable) {
+ const buffer = Buffer.alloc(4);
+ buffer.writeInt32BE(value);
+ this.writeBuffer(buffer);
+ }
+ else {
+ this.buffer.writeInt32BE(value, this.offset);
+ this.offset += 4;
+ }
+ return this;
+ }
+ writeUInt(value) {
+ if (this.resizable) {
+ const buffer = Buffer.alloc(4);
+ buffer.writeUInt32BE(value);
+ this.writeBuffer(buffer);
+ }
+ else {
+ this.buffer.writeUInt32BE(value, this.offset);
+ this.offset += 4;
+ }
+ return this;
+ }
+ writeLong(value) {
+ if (typeof (value) !== "bigint") {
+ value = BigInt(value);
+ }
+ if (this.resizable) {
+ const buffer = Buffer.alloc(8);
+ buffer.writeBigInt64BE(value);
+ this.writeBuffer(buffer);
+ }
+ else {
+ this.buffer.writeBigInt64BE(value, this.offset);
+ this.offset += 8;
+ }
+ return this;
+ }
+ writeULong(value) {
+ if (typeof (value) !== "bigint") {
+ value = BigInt(value);
+ }
+ if (this.resizable) {
+ const buffer = Buffer.alloc(8);
+ buffer.writeBigUint64BE(value);
+ this.writeBuffer(buffer);
+ }
+ else {
+ this.buffer.writeBigUint64BE(value, this.offset);
+ this.offset += 8;
+ }
+ return this;
+ }
+ writeFloat(value) {
+ if (this.resizable) {
+ const buffer = Buffer.alloc(4);
+ buffer.writeFloatBE(value);
+ this.writeBuffer(buffer);
+ }
+ else {
+ this.buffer.writeFloatBE(value, this.offset);
+ this.offset += 4;
+ }
+ return this;
+ }
+ writeDouble(value) {
+ if (this.resizable) {
+ const buffer = Buffer.alloc(8);
+ buffer.writeDoubleBE(value);
+ this.writeBuffer(buffer);
+ }
+ else {
+ this.buffer.writeDoubleBE(value, this.offset);
+ this.offset += 8;
+ }
+ return this;
+ }
+ writeShortString(text) {
+ this.writeUByte(text.length);
+ for (let i = 0; i < text.length; i++) {
+ this.writeUByte(text.charCodeAt(i));
+ }
+ return this;
+ }
+ writeString(text) {
+ this.writeUShort(text.length);
+ for (let i = 0; i < text.length; i++) {
+ this.writeUByte(text.charCodeAt(i));
+ }
+ return this;
+ }
+ writeString16(text) {
+ this.writeUShort(text.length);
+ for (let i = 0; i < text.length; i++) {
+ this.writeUShort(text.charCodeAt(i));
+ }
+ return this;
+ }
+}
+exports.WriterBE = WriterBE;
diff --git a/lib/writers/WriterBase.d.ts b/lib/writers/WriterBase.d.ts
new file mode 100644
index 0000000..87599f9
--- /dev/null
+++ b/lib/writers/WriterBase.d.ts
@@ -0,0 +1,14 @@
+///
+export declare class WriterBase {
+ buffer: Buffer;
+ offset: number;
+ readonly resizable: boolean;
+ constructor(size?: number);
+ toBuffer(): Buffer;
+ toString(): string;
+ writeBuffer(buffer: Buffer): this;
+ writeUint8Array(array: Uint8Array): this;
+ writeByte(value: number): this;
+ writeUByte(value: number): this;
+ writeBool(value: boolean | number): this;
+}
diff --git a/lib/writers/WriterBase.js b/lib/writers/WriterBase.js
new file mode 100644
index 0000000..3c80864
--- /dev/null
+++ b/lib/writers/WriterBase.js
@@ -0,0 +1,56 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.WriterBase = void 0;
+class WriterBase {
+ constructor(size = 0) {
+ this.buffer = Buffer.alloc(size);
+ this.offset = 0;
+ this.resizable = size === 0;
+ }
+ toBuffer() {
+ return this.buffer;
+ }
+ toString() {
+ return this.buffer.toString();
+ }
+ writeBuffer(buffer) {
+ this.buffer = Buffer.concat([this.buffer, buffer], this.buffer.length + buffer.length);
+ return this;
+ }
+ writeUint8Array(array) {
+ this.writeBuffer(Buffer.from(array));
+ return this;
+ }
+ writeByte(value) {
+ if (this.resizable) {
+ const buffer = Buffer.alloc(1);
+ buffer.writeInt8(value);
+ this.writeBuffer(buffer);
+ }
+ else {
+ this.buffer.writeInt8(value, this.offset);
+ this.offset++;
+ }
+ return this;
+ }
+ writeUByte(value) {
+ if (this.resizable) {
+ const buffer = Buffer.alloc(1);
+ buffer.writeUInt8(value);
+ this.writeBuffer(buffer);
+ }
+ else {
+ this.buffer.writeUInt8(value, this.offset);
+ this.offset++;
+ }
+ return this;
+ }
+ writeBool(value) {
+ if (typeof (value) === "number") {
+ value = Boolean(value);
+ }
+ this.writeUByte(value ? 1 : 0);
+ return this;
+ }
+}
+exports.WriterBase = WriterBase;
diff --git a/lib/writers/WriterLE.d.ts b/lib/writers/WriterLE.d.ts
new file mode 100644
index 0000000..1f9cd06
--- /dev/null
+++ b/lib/writers/WriterLE.d.ts
@@ -0,0 +1,15 @@
+import { IWriter } from "./IWriter";
+import { WriterBase } from "./WriterBase";
+export declare class WriterLE extends WriterBase implements IWriter {
+ writeShort(value: number): this;
+ writeUShort(value: number): this;
+ writeInt(value: number): this;
+ writeUInt(value: number): this;
+ writeLong(value: number | bigint): this;
+ writeULong(value: number | bigint): this;
+ writeFloat(value: number): this;
+ writeDouble(value: number): this;
+ writeShortString(text: string): this;
+ writeString(text: string): this;
+ writeString16(text: string): this;
+}
diff --git a/lib/writers/WriterLE.js b/lib/writers/WriterLE.js
new file mode 100644
index 0000000..279ea39
--- /dev/null
+++ b/lib/writers/WriterLE.js
@@ -0,0 +1,130 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.WriterLE = void 0;
+const WriterBase_1 = require("./WriterBase");
+class WriterLE extends WriterBase_1.WriterBase {
+ writeShort(value) {
+ if (this.resizable) {
+ const buffer = Buffer.alloc(2);
+ buffer.writeInt16LE(value);
+ this.writeBuffer(buffer);
+ }
+ else {
+ this.buffer.writeInt16LE(value, this.offset);
+ this.offset += 2;
+ }
+ return this;
+ }
+ writeUShort(value) {
+ if (this.resizable) {
+ const buffer = Buffer.alloc(2);
+ buffer.writeUInt16LE(value);
+ this.writeBuffer(buffer);
+ }
+ else {
+ this.buffer.writeUInt16LE(value, this.offset);
+ this.offset += 2;
+ }
+ return this;
+ }
+ writeInt(value) {
+ if (this.resizable) {
+ const buffer = Buffer.alloc(4);
+ buffer.writeInt32LE(value);
+ this.writeBuffer(buffer);
+ }
+ else {
+ this.buffer.writeInt32LE(value, this.offset);
+ this.offset += 4;
+ }
+ return this;
+ }
+ writeUInt(value) {
+ if (this.resizable) {
+ const buffer = Buffer.alloc(4);
+ buffer.writeUInt32LE(value);
+ this.writeBuffer(buffer);
+ }
+ else {
+ this.buffer.writeUInt32LE(value, this.offset);
+ this.offset += 4;
+ }
+ return this;
+ }
+ writeLong(value) {
+ if (typeof (value) !== "bigint") {
+ value = BigInt(value);
+ }
+ if (this.resizable) {
+ const buffer = Buffer.alloc(8);
+ buffer.writeBigInt64LE(value);
+ this.writeBuffer(buffer);
+ }
+ else {
+ this.buffer.writeBigInt64LE(value, this.offset);
+ this.offset += 8;
+ }
+ return this;
+ }
+ writeULong(value) {
+ if (typeof (value) !== "bigint") {
+ value = BigInt(value);
+ }
+ if (this.resizable) {
+ const buffer = Buffer.alloc(8);
+ buffer.writeBigUint64LE(value);
+ this.writeBuffer(buffer);
+ }
+ else {
+ this.buffer.writeBigUint64LE(value, this.offset);
+ this.offset += 8;
+ }
+ return this;
+ }
+ writeFloat(value) {
+ if (this.resizable) {
+ const buffer = Buffer.alloc(4);
+ buffer.writeFloatLE(value);
+ this.writeBuffer(buffer);
+ }
+ else {
+ this.buffer.writeFloatLE(value, this.offset);
+ this.offset += 4;
+ }
+ return this;
+ }
+ writeDouble(value) {
+ if (this.resizable) {
+ const buffer = Buffer.alloc(8);
+ buffer.writeDoubleLE(value);
+ this.writeBuffer(buffer);
+ }
+ else {
+ this.buffer.writeDoubleLE(value, this.offset);
+ this.offset += 8;
+ }
+ return this;
+ }
+ writeShortString(text) {
+ this.writeUByte(text.length);
+ for (let i = 0; i < text.length; i++) {
+ this.writeUByte(text.charCodeAt(i));
+ }
+ return this;
+ }
+ writeString(text) {
+ this.writeUShort(text.length);
+ for (let i = 0; i < text.length; i++) {
+ this.writeUByte(text.charCodeAt(i));
+ }
+ return this;
+ }
+ writeString16(text) {
+ this.writeUShort(text.length);
+ for (let i = 0; i < text.length; i++) {
+ this.writeUShort(text.charCodeAt(i));
+ }
+ return this;
+ }
+}
+exports.WriterLE = WriterLE;