The Code™️

This commit is contained in:
Holly Stubbs 2023-04-28 16:47:02 +01:00 committed by Holly
parent 7817565b07
commit 577a0823a4
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
14 changed files with 2902 additions and 0 deletions

3
.gitignore vendored
View file

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

4
Endian.ts Normal file
View file

@ -0,0 +1,4 @@
export enum Endian {
LE,
BE
}

23
index.ts Normal file
View file

@ -0,0 +1,23 @@
import { Endian } from "./Endian";
import { IReader } from "./readers/IReader";
import { IWriter } from "./writers/IWriter";
import { ReaderBE } from "./readers/ReaderBE";
import { ReaderLE } from "./readers/ReaderLE";
import { WriterBE } from "./writers/WriterBE";
import { WriterLE } from "./writers/WriterLE";
export function createReader(endianness:Endian, buffer:Buffer) : IReader {
if (endianness === Endian.LE) {
return new ReaderLE(buffer);
} else {
return new ReaderBE(buffer);
}
}
export function createWriter(endianness:Endian, size?:number) : IWriter {
if (endianness === Endian.LE) {
return new WriterLE(size);
} else {
return new WriterBE(size);
}
}

2268
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

29
package.json Normal file
View file

@ -0,0 +1,29 @@
{
"name": "bufferstuff",
"version": "1.0.0",
"description": "A set of utility classes for reading and writing binary data in NodeJS and the browser",
"main": "index.ts",
"scripts": {
"updateCheck": "check-outdated",
"pack": "webpack",
"build": "tsc --build",
"_clean": "tsc --build --clean"
},
"repository": {
"type": "git",
"url": "git+https://github.com/tgpholly/bufferStuff.git"
},
"keywords": [],
"author": "",
"license": "MIT",
"bugs": {
"url": "https://github.com/tgpholly/bufferStuff/issues"
},
"homepage": "https://github.com/tgpholly/bufferStuff#readme",
"devDependencies": {
"check-outdated": "^2.11.0",
"ts-loader": "^9.4.2",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
}
}

17
readers/IReader.ts Normal file
View file

@ -0,0 +1,17 @@
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,
}

74
readers/ReaderBE.ts Normal file
View file

@ -0,0 +1,74 @@
import { IReader } from "./IReader";
import { ReaderBase } from "./ReaderBase";
export class ReaderBE extends ReaderBase implements IReader {
public readShort() {
const value = this.buffer.readInt16BE(this.offset);
this.offset += 2
return value;
}
public readUShort() {
const value = this.buffer.readUInt16BE(this.offset);
this.offset += 2
return value;
}
public readInt() {
const value = this.buffer.readInt32BE(this.offset);
this.offset += 4;
return value;
}
public readUInt() {
const value = this.buffer.readUInt32BE(this.offset);
this.offset += 4;
return value;
}
public readLong() {
const value = this.buffer.readBigInt64BE(this.offset);
this.offset += 8;
return value;
}
public readULong() {
const value = this.buffer.readBigUint64BE(this.offset);
this.offset += 8;
return value;
}
public readFloat() {
const value = this.buffer.readFloatBE(this.offset);
this.offset += 4;
return value;
}
public readDouble() {
const value = this.buffer.readDoubleBE(this.offset);
this.offset += 8;
return value;
}
public readShortString() {
const length = this.readUByte();
let text:string = "";
for (let i = 0; i < length; i++) {
text += String.fromCharCode(this.readUByte());
}
return text;
}
public readString() {
const length = this.readUShort();
let text:string = "";
for (let i = 0; i < length; i++) {
text += String.fromCharCode(this.readUByte());
}
return text;
}
}

42
readers/ReaderBase.ts Normal file
View file

@ -0,0 +1,42 @@
export class ReaderBase {
public buffer:Buffer;
public offset:number;
public constructor(buffer:Buffer) {
this.buffer = buffer;
this.offset = 0;
}
public readBuffer(bytes:number) {
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.
public readUint8Array(bytes:number) {
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;
}
public readByte() {
const value = this.buffer.readInt8(this.offset);
this.offset++;
return value;
}
public readUByte() {
const value = this.buffer.readUInt8(this.offset);
this.offset++;
return value;
}
public readBool() {
return Boolean(this.readUByte());
}
}

74
readers/ReaderLE.ts Normal file
View file

@ -0,0 +1,74 @@
import { IReader } from "./IReader";
import { ReaderBase } from "./ReaderBase";
export class ReaderLE extends ReaderBase implements IReader {
public readShort() {
const value = this.buffer.readInt16LE(this.offset);
this.offset += 2
return value;
}
public readUShort() {
const value = this.buffer.readUInt16LE(this.offset);
this.offset += 2
return value;
}
public readInt() {
const value = this.buffer.readInt32LE(this.offset);
this.offset += 4;
return value;
}
public readUInt() {
const value = this.buffer.readUInt32LE(this.offset);
this.offset += 4;
return value;
}
public readLong() {
const value = this.buffer.readBigInt64LE(this.offset);
this.offset += 8;
return value;
}
public readULong() {
const value = this.buffer.readBigUint64LE(this.offset);
this.offset += 8;
return value;
}
public readFloat() {
const value = this.buffer.readFloatLE(this.offset);
this.offset += 4;
return value;
}
public readDouble() {
const value = this.buffer.readDoubleLE(this.offset);
this.offset += 8;
return value;
}
public readShortString() {
const length = this.readUByte();
let text:string = "";
for (let i = 0; i < length; i++) {
text += String.fromCharCode(this.readUByte());
}
return text;
}
public readString() {
const length = this.readUShort();
let text:string = "";
for (let i = 0; i < length; i++) {
text += String.fromCharCode(this.readUByte());
}
return text;
}
}

12
tsconfig.json Normal file
View file

@ -0,0 +1,12 @@
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "es6",
"esModuleInterop": true,
"resolveJsonModule": true,
"rootDir": "./",
"outDir": "./build",
"strict": true
}
}

19
writers/IWriter.ts Normal file
View file

@ -0,0 +1,19 @@
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): IWriter,
writeULong(value:number): IWriter,
writeFloat(value:number): IWriter,
writeDouble(value:number): IWriter,
writeString(text:string): IWriter,
writeShortString(text:string): IWriter
}

136
writers/WriterBE.ts Normal file
View file

@ -0,0 +1,136 @@
import { IWriter } from "./IWriter";
import { WriterBase } from "./WriterBase";
export class WriterBE extends WriterBase implements IWriter {
public writeShort(value:number) {
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;
}
public writeUShort(value:number) {
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;
}
public writeInt(value:number) {
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;
}
public writeUInt(value:number) {
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;
}
public writeLong(value:number|bigint) {
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;
}
public writeULong(value:number|bigint) {
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;
}
public writeFloat(value:number) {
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;
}
public writeDouble(value:number) {
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;
}
public writeShortString(text:string) {
this.writeUByte(text.length);
for (let i = 0; i < text.length; i++) {
this.writeUByte(text.charCodeAt(i));
}
return this;
}
public writeString(text:string) {
this.writeUShort(text.length);
for (let i = 0; i < text.length; i++) {
this.writeUByte(text.charCodeAt(i));
}
return this;
}
}

66
writers/WriterBase.ts Normal file
View file

@ -0,0 +1,66 @@
export class WriterBase {
public buffer:Buffer;
public offset:number;
public readonly resizable:boolean;
public constructor(size:number = 0) {
this.buffer = Buffer.alloc(size);
this.offset = 0;
this.resizable = size === 0;
}
public toBuffer() {
return this.buffer;
}
public toString() {
return this.buffer.toString();
}
public writeBuffer(buffer:Buffer) {
this.buffer = Buffer.concat([this.buffer, buffer], this.buffer.length + buffer.length);
return this;
}
public writeUint8Array(array:Uint8Array) {
this.writeBuffer(Buffer.from(array));
return this;
}
public writeByte(value:number) {
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;
}
public writeUByte(value:number) {
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;
}
public writeBool(value:boolean|number) {
if (typeof(value) === "number") {
value = Boolean(value);
}
this.writeUByte(value ? 1 : 0);
return this;
}
}

135
writers/WriterLE.ts Normal file
View file

@ -0,0 +1,135 @@
import { IWriter } from "./IWriter";
import { WriterBase } from "./WriterBase";
export class WriterLE extends WriterBase implements IWriter {
public writeShort(value:number) {
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;
}
public writeUShort(value:number) {
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;
}
public writeInt(value:number) {
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;
}
public writeUInt(value:number) {
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;
}
public writeLong(value:number|bigint) {
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;
}
public writeULong(value:number|bigint) {
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;
}
public writeFloat(value:number) {
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;
}
public writeDouble(value:number) {
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;
}
public writeShortString(text:string) {
this.writeUByte(text.length);
for (let i = 0; i < text.length; i++) {
this.writeUByte(text.charCodeAt(i));
}
return this;
}
public writeString(text:string) {
this.writeUShort(text.length);
for (let i = 0; i < text.length; i++) {
this.writeUByte(text.charCodeAt(i));
}
return this;
}
}