Compare commits

..

No commits in common. "master" and "1.3.3" have entirely different histories.

12 changed files with 2885 additions and 167 deletions

View file

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

View file

@ -22,13 +22,12 @@ I tried to keep it as simple as possible to use, for example if you want to writ
writer.writeByte(<number>);
```
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.
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.
## Projects using bufferStuff
If your project uses bufferStuff feel free to make a PR to add it to this list!
### [tgpholly/mc-beta-server](https://git.eusv.net/tgpholly/mc-beta-server)
### [tgpholly/mc-beta-server](https://github.com/tgpholly/mc-beta-server)
### [tgpholly/ultrakillMP_server](https://github.com/tgpholly/ultrakillMP_server)
### [tgpholly/t00-multiuser](https://git.eusv.net/tgpholly/t00-multiuser)
## Projects similar to bufferStuff
### [tgpholly/csharp-extensions/BinaryTools](https://github.com/tgpholly/csharp-extensions/tree/master/BinaryTools) - Basically bufferStuff but for C#

View file

@ -10,14 +10,6 @@ export class ReaderBase {
this.offset = 0;
}
public get readOffset() {
return this.offset;
}
public get length() {
return this.buffer.length;
}
public readBuffer(bytes:number) {
const value = this.buffer.subarray(this.offset, this.offset + bytes);
this.offset += bytes;
@ -50,25 +42,4 @@ export class ReaderBase {
public readBool() {
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,14 +14,6 @@ export class WriterBase {
this.resizable = size === 0;
}
public get writeOffset() {
return this.offset;
}
public get length() {
return this.buffer.length;
}
public toBuffer() {
return this.buffer;
}
@ -31,20 +23,20 @@ export class WriterBase {
}
public writeBuffer(buffer:Buffer) {
this.buffer = getBufferClass().concat([this.buffer, buffer], this.buffer.length + buffer.length);
this.buffer = Buffer.concat([this.buffer, buffer], this.buffer.length + buffer.length);
return this;
}
public writeUint8Array(array:Uint8Array) {
this.writeBuffer(getBufferClass().from(array));
this.writeBuffer(Buffer.from(array));
return this;
}
public writeByte(value:number) {
if (this.resizable) {
const buffer = getBufferClass().alloc(1);
const buffer = Buffer.alloc(1);
buffer.writeInt8(value);
this.writeBuffer(buffer);
} else {
@ -57,7 +49,7 @@ export class WriterBase {
public writeUByte(value:number) {
if (this.resizable) {
const buffer = getBufferClass().alloc(1);
const buffer = Buffer.alloc(1);
buffer.writeUInt8(value);
this.writeBuffer(buffer);
} else {
@ -76,19 +68,4 @@ export class WriterBase {
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;
}
}

2846
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

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

View file

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

View file

@ -53,9 +53,20 @@ export class ReaderBE extends ReaderBase implements IReader {
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 = "";
let text:string = "";
for (let i = 0; i < length; i++) {
text += String.fromCharCode(this.readUByte());
@ -66,7 +77,7 @@ export class ReaderBE extends ReaderBase implements IReader {
public readString16() {
const length = this.readUShort();
let text = "";
let text:string = "";
for (let i = 0; i < length; i++) {
text += String.fromCharCode(this.readUShort());
@ -74,14 +85,4 @@ export class ReaderBE extends ReaderBase implements IReader {
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,6 +53,17 @@ export class ReaderLE extends ReaderBase implements IReader {
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 = "";
@ -74,14 +85,4 @@ export class ReaderLE extends ReaderBase implements IReader {
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,8 +2,6 @@
// Check LICENSE in repository root for more information.
export interface IWriter {
writeOffset: number,
length: number,
toBuffer(): Buffer,
toString(): string,
writeBuffer(buffer:Buffer): IWriter,
@ -11,17 +9,15 @@ export interface IWriter {
writeByte(value:number): IWriter,
writeUByte(value:number): IWriter,
writeBool(value:boolean|number): IWriter,
writeStringAsBytes(text:string): 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|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,
writeStringAsShorts(text:string): IWriter
}

View file

@ -3,12 +3,11 @@
import { IWriter } from "./IWriter";
import { WriterBase } from "../base/WriterBase";
import { getBufferClass } from "../base/BufferShim";
export class WriterBE extends WriterBase implements IWriter {
public writeShort(value:number) {
if (this.resizable) {
const buffer = getBufferClass().alloc(2);
const buffer = Buffer.alloc(2);
buffer.writeInt16BE(value);
this.writeBuffer(buffer);
} else {
@ -21,7 +20,7 @@ export class WriterBE extends WriterBase implements IWriter {
public writeUShort(value:number) {
if (this.resizable) {
const buffer = getBufferClass().alloc(2);
const buffer = Buffer.alloc(2);
buffer.writeUInt16BE(value);
this.writeBuffer(buffer);
} else {
@ -34,7 +33,7 @@ export class WriterBE extends WriterBase implements IWriter {
public writeInt(value:number) {
if (this.resizable) {
const buffer = getBufferClass().alloc(4);
const buffer = Buffer.alloc(4);
buffer.writeInt32BE(value);
this.writeBuffer(buffer);
} else {
@ -47,7 +46,7 @@ export class WriterBE extends WriterBase implements IWriter {
public writeUInt(value:number) {
if (this.resizable) {
const buffer = getBufferClass().alloc(4);
const buffer = Buffer.alloc(4);
buffer.writeUInt32BE(value);
this.writeBuffer(buffer);
} else {
@ -64,7 +63,7 @@ export class WriterBE extends WriterBase implements IWriter {
}
if (this.resizable) {
const buffer = getBufferClass().alloc(8);
const buffer = Buffer.alloc(8);
buffer.writeBigInt64BE(value);
this.writeBuffer(buffer);
} else {
@ -81,7 +80,7 @@ export class WriterBE extends WriterBase implements IWriter {
}
if (this.resizable) {
const buffer = getBufferClass().alloc(8);
const buffer = Buffer.alloc(8);
buffer.writeBigUint64BE(value);
this.writeBuffer(buffer);
} else {
@ -94,7 +93,7 @@ export class WriterBE extends WriterBase implements IWriter {
public writeFloat(value:number) {
if (this.resizable) {
const buffer = getBufferClass().alloc(4);
const buffer = Buffer.alloc(4);
buffer.writeFloatBE(value);
this.writeBuffer(buffer);
} else {
@ -107,7 +106,7 @@ export class WriterBE extends WriterBase implements IWriter {
public writeDouble(value:number) {
if (this.resizable) {
const buffer = getBufferClass().alloc(8);
const buffer = Buffer.alloc(8);
buffer.writeDoubleBE(value);
this.writeBuffer(buffer);
} else {
@ -147,19 +146,4 @@ export class WriterBE extends WriterBase implements IWriter {
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

@ -3,12 +3,11 @@
import { IWriter } from "./IWriter";
import { WriterBase } from "../base/WriterBase";
import { getBufferClass } from "../base/BufferShim";
export class WriterLE extends WriterBase implements IWriter {
public writeShort(value:number) {
if (this.resizable) {
const buffer = getBufferClass().alloc(2);
const buffer = Buffer.alloc(2);
buffer.writeInt16LE(value);
this.writeBuffer(buffer);
} else {
@ -21,7 +20,7 @@ export class WriterLE extends WriterBase implements IWriter {
public writeUShort(value:number) {
if (this.resizable) {
const buffer = getBufferClass().alloc(2);
const buffer = Buffer.alloc(2);
buffer.writeUInt16LE(value);
this.writeBuffer(buffer);
} else {
@ -34,7 +33,7 @@ export class WriterLE extends WriterBase implements IWriter {
public writeInt(value:number) {
if (this.resizable) {
const buffer = getBufferClass().alloc(4);
const buffer = Buffer.alloc(4);
buffer.writeInt32LE(value);
this.writeBuffer(buffer);
} else {
@ -46,7 +45,7 @@ export class WriterLE extends WriterBase implements IWriter {
}
public writeUInt(value:number) {
if (this.resizable) {
const buffer = getBufferClass().alloc(4);
const buffer = Buffer.alloc(4);
buffer.writeUInt32LE(value);
this.writeBuffer(buffer);
} else {
@ -63,7 +62,7 @@ export class WriterLE extends WriterBase implements IWriter {
}
if (this.resizable) {
const buffer = getBufferClass().alloc(8);
const buffer = Buffer.alloc(8);
buffer.writeBigInt64LE(value);
this.writeBuffer(buffer);
} else {
@ -80,7 +79,7 @@ export class WriterLE extends WriterBase implements IWriter {
}
if (this.resizable) {
const buffer = getBufferClass().alloc(8);
const buffer = Buffer.alloc(8);
buffer.writeBigUint64LE(value);
this.writeBuffer(buffer);
} else {
@ -93,7 +92,7 @@ export class WriterLE extends WriterBase implements IWriter {
public writeFloat(value:number) {
if (this.resizable) {
const buffer = getBufferClass().alloc(4);
const buffer = Buffer.alloc(4);
buffer.writeFloatLE(value);
this.writeBuffer(buffer);
} else {
@ -106,7 +105,7 @@ export class WriterLE extends WriterBase implements IWriter {
public writeDouble(value:number) {
if (this.resizable) {
const buffer = getBufferClass().alloc(8);
const buffer = Buffer.alloc(8);
buffer.writeDoubleLE(value);
this.writeBuffer(buffer);
} else {
@ -146,19 +145,4 @@ export class WriterLE extends WriterBase implements IWriter {
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;
}
}