This commit is contained in:
Holly Stubbs 2023-05-12 14:14:28 +01:00
parent a2e59b28df
commit 7989e10647
19 changed files with 664 additions and 3 deletions

3
.gitignore vendored
View file

@ -1,6 +1,3 @@
# Custom
lib/
# Logs
logs
*.log

9
lib/index.d.ts vendored Normal file
View file

@ -0,0 +1,9 @@
/// <reference types="node" />
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;

30
lib/index.js Normal file
View file

@ -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;

19
lib/readers/IReader.d.ts vendored Normal file
View file

@ -0,0 +1,19 @@
/// <reference types="node" />
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;
}

2
lib/readers/IReader.js Normal file
View file

@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

15
lib/readers/ReaderBE.d.ts vendored Normal file
View file

@ -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;
}

71
lib/readers/ReaderBE.js Normal file
View file

@ -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;

11
lib/readers/ReaderBase.d.ts vendored Normal file
View file

@ -0,0 +1,11 @@
/// <reference types="node" />
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;
}

38
lib/readers/ReaderBase.js Normal file
View file

@ -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;

15
lib/readers/ReaderLE.d.ts vendored Normal file
View file

@ -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;
}

71
lib/readers/ReaderLE.js Normal file
View file

@ -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;

21
lib/writers/IWriter.d.ts vendored Normal file
View file

@ -0,0 +1,21 @@
/// <reference types="node" />
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;
}

2
lib/writers/IWriter.js Normal file
View file

@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

15
lib/writers/WriterBE.d.ts vendored Normal file
View file

@ -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;
}

130
lib/writers/WriterBE.js Normal file
View file

@ -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;

14
lib/writers/WriterBase.d.ts vendored Normal file
View file

@ -0,0 +1,14 @@
/// <reference types="node" />
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;
}

56
lib/writers/WriterBase.js Normal file
View file

@ -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;

15
lib/writers/WriterLE.d.ts vendored Normal file
View file

@ -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;
}

130
lib/writers/WriterLE.js Normal file
View file

@ -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;