Compare commits

...

11 Commits

12 changed files with 136 additions and 2879 deletions

View File

@ -1,6 +1,6 @@
MIT License MIT License
Copyright (c) 2023 Holly Stubbs (tgpholly) Copyright (c) 2021-2024 Holly Stubbs (tgpholly)
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@ -22,12 +22,13 @@ I tried to keep it as simple as possible to use, for example if you want to writ
writer.writeByte(<number>); writer.writeByte(<number>);
``` ```
You can find a list of all of the methods for [Writers](https://github.com/tgpholly/bufferStuff/blob/master/writers/IWriter.ts) and [Readers](https://github.com/tgpholly/bufferStuff/blob/master/readers/IReader.ts) in their interface files. You can find a list of all of the methods for [Writers](https://git.eusv.net/tgpholly/bufferStuff/src/branch/master/writers/IWriter.ts) and [Readers](https://git.eusv.net/tgpholly/bufferStuff/src/branch/master/readers/IReader.ts) in their interface files.
## Projects using bufferStuff ## Projects using bufferStuff
If your project uses bufferStuff feel free to make a PR to add it to this list! If your project uses bufferStuff feel free to make a PR to add it to this list!
### [tgpholly/mc-beta-server](https://github.com/tgpholly/mc-beta-server) ### [tgpholly/mc-beta-server](https://git.eusv.net/tgpholly/mc-beta-server)
### [tgpholly/ultrakillMP_server](https://github.com/tgpholly/ultrakillMP_server) ### [tgpholly/ultrakillMP_server](https://github.com/tgpholly/ultrakillMP_server)
### [tgpholly/t00-multiuser](https://git.eusv.net/tgpholly/t00-multiuser)
## Projects similar to bufferStuff ## Projects similar to bufferStuff
### [tgpholly/csharp-extensions/BinaryTools](https://github.com/tgpholly/csharp-extensions/tree/master/BinaryTools) - Basically bufferStuff but for C# ### [tgpholly/csharp-extensions/BinaryTools](https://github.com/tgpholly/csharp-extensions/tree/master/BinaryTools) - Basically bufferStuff but for C#

View File

@ -14,6 +14,10 @@ export class ReaderBase {
return this.offset; return this.offset;
} }
public get length() {
return this.buffer.length;
}
public readBuffer(bytes:number) { public readBuffer(bytes:number) {
const value = this.buffer.subarray(this.offset, this.offset + bytes); const value = this.buffer.subarray(this.offset, this.offset + bytes);
this.offset += bytes; this.offset += bytes;
@ -46,4 +50,25 @@ export class ReaderBase {
public readBool() { public readBool() {
return Boolean(this.readUByte()); return Boolean(this.readUByte());
} }
public readShortString() {
const length = this.readUByte();
let text = "";
for (let i = 0; i < length; i++) {
text += String.fromCharCode(this.readUByte());
}
return text;
}
public readBytesAsString(bytesToRead:number) {
let text = "";
for (let i = 0; i < bytesToRead; i++) {
text += String.fromCharCode(this.readUByte());
}
return text;
}
} }

View File

@ -14,6 +14,14 @@ export class WriterBase {
this.resizable = size === 0; this.resizable = size === 0;
} }
public get writeOffset() {
return this.offset;
}
public get length() {
return this.buffer.length;
}
public toBuffer() { public toBuffer() {
return this.buffer; return this.buffer;
} }
@ -68,4 +76,19 @@ export class WriterBase {
return this; return this;
} }
public writeStringAsBytes(text:string) {
let buffer;
if (this.resizable) {
buffer = getBufferClass().alloc(text.length);
} else {
buffer = this.buffer;
}
for (let i = 0; i < text.length; i++) {
buffer.writeUInt8(text.charCodeAt(i), i);
}
return this;
}
} }

2857
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "bufferstuff", "name": "bufferstuff",
"version": "1.4.0", "version": "1.5.1",
"description": "A set of utility classes for reading and writing binary data in NodeJS and the browser", "description": "A set of utility classes for reading and writing binary data in NodeJS and the browser",
"main": "./lib/index.js", "main": "./lib/index.js",
"types": "./lib/index.d.ts", "types": "./lib/index.d.ts",
@ -20,21 +20,20 @@
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/tgpholly/bufferStuff.git" "url": "https://git.eusv.net/tgpholly/bufferStuff.git"
}, },
"keywords": [], "keywords": [],
"author": "tgpholly", "author": "tgpholly",
"license": "MIT", "license": "MIT",
"bugs": { "bugs": {
"url": "https://github.com/tgpholly/bufferStuff/issues" "url": "https://git.eusv.net/tgpholly/bufferStuff/issues"
}, },
"homepage": "https://github.com/tgpholly/bufferStuff#readme", "homepage": "https://git.eusv.net/tgpholly/bufferStuff#readme",
"devDependencies": { "devDependencies": {
"check-outdated": "^2.12.0", "check-outdated": "^2.12.0",
"npm-run-all": "^4.1.5", "npm-run-all": "^4.1.5",
"terser": "^5.24.0", "terser": "^5.27.0",
"ts-loader": "^9.5.0", "ts-node": "^10.9.2",
"ts-node": "^10.9.1", "typescript": "^5.3.3"
"typescript": "^5.2.2"
} }
} }

View File

@ -2,11 +2,15 @@
// Check LICENSE in repository root for more information. // Check LICENSE in repository root for more information.
export interface IReader { export interface IReader {
readOffset: number,
length: number,
readBuffer(bytes:number): Buffer, readBuffer(bytes:number): Buffer,
readUint8Array(bytes:number): Uint8Array, readUint8Array(bytes:number): Uint8Array,
readByte(): number, readByte(): number,
readUByte(): number, readUByte(): number,
readBool(): boolean, readBool(): boolean,
readShortString(): string,
readBytesAsString(bytesToRead:number): string,
readShort(): number, readShort(): number,
readUShort(): number, readUShort(): number,
readInt(): number, readInt(): number,
@ -15,7 +19,7 @@ export interface IReader {
readULong(): bigint, readULong(): bigint,
readFloat(): number, readFloat(): number,
readDouble(): number, readDouble(): number,
readShortString(): string,
readString(): string, readString(): string,
readShortsAsString(shortsToRead:number): string,
readString16(): string, readString16(): string,
} }

View File

@ -53,20 +53,9 @@ export class ReaderBE extends ReaderBase implements IReader {
return value; 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() { public readString() {
const length = this.readUShort(); const length = this.readUShort();
let text:string = ""; let text = "";
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
text += String.fromCharCode(this.readUByte()); text += String.fromCharCode(this.readUByte());
@ -77,7 +66,7 @@ export class ReaderBE extends ReaderBase implements IReader {
public readString16() { public readString16() {
const length = this.readUShort(); const length = this.readUShort();
let text:string = ""; let text = "";
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
text += String.fromCharCode(this.readUShort()); text += String.fromCharCode(this.readUShort());
@ -85,4 +74,14 @@ export class ReaderBE extends ReaderBase implements IReader {
return text; return text;
} }
public readShortsAsString(shortsToRead:number) {
let text = "";
for (let i = 0; i < shortsToRead; i++) {
text += String.fromCharCode(this.readUShort());
}
return text;
}
} }

View File

@ -53,17 +53,6 @@ export class ReaderLE extends ReaderBase implements IReader {
return value; 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() { public readString() {
const length = this.readUShort(); const length = this.readUShort();
let text:string = ""; let text:string = "";
@ -85,4 +74,14 @@ export class ReaderLE extends ReaderBase implements IReader {
return text; return text;
} }
public readShortsAsString(shortsToRead:number) {
let text = "";
for (let i = 0; i < shortsToRead; i++) {
text += String.fromCharCode(this.readUShort());
}
return text;
}
} }

View File

@ -2,6 +2,8 @@
// Check LICENSE in repository root for more information. // Check LICENSE in repository root for more information.
export interface IWriter { export interface IWriter {
writeOffset: number,
length: number,
toBuffer(): Buffer, toBuffer(): Buffer,
toString(): string, toString(): string,
writeBuffer(buffer:Buffer): IWriter, writeBuffer(buffer:Buffer): IWriter,
@ -9,15 +11,17 @@ export interface IWriter {
writeByte(value:number): IWriter, writeByte(value:number): IWriter,
writeUByte(value:number): IWriter, writeUByte(value:number): IWriter,
writeBool(value:boolean|number): IWriter, writeBool(value:boolean|number): IWriter,
writeStringAsBytes(text:string): IWriter,
writeShort(value:number): IWriter, writeShort(value:number): IWriter,
writeUShort(value:number): IWriter, writeUShort(value:number): IWriter,
writeInt(value:number): IWriter, writeInt(value:number): IWriter,
writeUInt(value:number): IWriter, writeUInt(value:number): IWriter,
writeLong(value:number|bigint): IWriter, writeLong(value:number|bigint): IWriter,
writeULong(value:number): IWriter, writeULong(value:number|bigint): IWriter,
writeFloat(value:number): IWriter, writeFloat(value:number): IWriter,
writeDouble(value:number): IWriter, writeDouble(value:number): IWriter,
writeShortString(text:string): IWriter, writeShortString(text:string): IWriter,
writeString(text:string): IWriter, writeString(text:string): IWriter,
writeString16(text:string): IWriter, writeString16(text:string): IWriter,
writeStringAsShorts(text:string): IWriter
} }

View File

@ -147,4 +147,19 @@ export class WriterBE extends WriterBase implements IWriter {
return this; return this;
} }
public writeStringAsShorts(text:string) {
let buffer:Buffer;
if (this.resizable) {
buffer = getBufferClass().alloc(text.length * 2);
} else {
buffer = this.buffer;
}
for (let i = 0; i < text.length; i++) {
buffer.writeUint16BE(text.charCodeAt(i), i);
}
return this;
}
} }

View File

@ -146,4 +146,19 @@ export class WriterLE extends WriterBase implements IWriter {
return this; return this;
} }
public writeStringAsShorts(text:string) {
let buffer:Buffer;
if (this.resizable) {
buffer = getBufferClass().alloc(text.length * 2);
} else {
buffer = this.buffer;
}
for (let i = 0; i < text.length; i++) {
buffer.writeUint16LE(text.charCodeAt(i), i);
}
return this;
}
} }