Compare commits

...

35 Commits

Author SHA1 Message Date
Holly Stubbs ceffc3eb87 Update README.md 2024-05-01 11:42:22 +01:00
Holly Stubbs c097cc5480 Update dev depends 2024-01-19 10:38:56 +00:00
Holly Stubbs 71a717ae80 fix repo url in package.json 2024-01-19 10:37:13 +00:00
Holly Stubbs d02bf4693b
update LICENSE 2024-01-01 22:35:13 +00:00
Holly Stubbs 2e6ec04512
also update readme links 2024-01-01 22:33:42 +00:00
Holly Stubbs 33e8466e9d
update repo links 2024-01-01 22:30:57 +00:00
Holly Stubbs 77d74ae657
1.5.0 2024-01-01 22:23:47 +00:00
Holly Stubbs 961df8cd72
Add functions for reading/writing strings without lengths 2024-01-01 22:23:16 +00:00
Holly Stubbs 4cacdbf113
Update depends 2024-01-01 22:22:12 +00:00
Holly Stubbs b7a482d1f2
actually add the new public methods to the interfaces 2023-11-07 00:33:28 +00:00
Holly Stubbs c1e43e2937
allow getting the writer offset and the length of both readers and writers 2023-11-07 00:14:03 +00:00
Holly Stubbs 63a3e5c73b
Allow getting of the read offset (needed for mc-beta-server) 2023-11-07 00:01:33 +00:00
Holly Stubbs 3bb2f542ef
update package-lock.json 2023-10-27 00:34:28 +01:00
Holly Stubbs 24f26db2c1 found some more cases of direct Buffer usages 2023-10-24 17:05:20 +01:00
Holly Stubbs 242b3f512b 1.3.4 2023-10-24 12:45:12 +01:00
Holly Stubbs dd46ec825b Remove all direct usages of Buffer so that it can be decided based on platform. 2023-10-24 12:44:11 +01:00
Holly Stubbs a157685fcc Remove debug messages and bump version for release 2023-10-24 12:23:25 +01:00
Holly Stubbs 8367213725 npmignore the base folder 2023-10-24 12:15:45 +01:00
Holly Stubbs d12d971ef5 Add browser js support 2023-10-24 12:04:32 +01:00
Holly Stubbs dd3eb43809 Bump to es2020 2023-10-24 12:04:16 +01:00
Holly Stubbs aabf5cdbb3 Add license header everywhere 2023-10-24 12:04:07 +01:00
Holly Stubbs 558c3c2025 use the generic "build" script to run all scripts 2023-07-28 10:45:22 +01:00
Holly Stubbs ff46656809 Clean up leftovers from debugging CI failures 2023-07-28 10:28:32 +01:00
Holly Stubbs 3a75bec4bf Update Depends 2023-07-28 10:25:20 +01:00
Holly Stubbs f4ac03725c Fix CI 2023-07-28 10:25:08 +01:00
Holly Stubbs a43fa8ef66
Attempt to fix CI 2023-07-28 10:03:08 +01:00
Holly Stubbs 2d7815cff8
Another CI fix attempt 2023-07-28 10:01:47 +01:00
Holly Stubbs 530b468dfb
Investigation continues 2023-07-28 09:59:34 +01:00
Holly Stubbs 0c253b1c95
CI Fix attempt 2023-07-28 09:54:32 +01:00
Holly Stubbs 842bcca1f4
Investigation 2023-07-28 09:48:08 +01:00
Holly Stubbs 61cc7e027e
Update fileSmasher.ts 2023-07-28 09:44:59 +01:00
Holly Stubbs 5498168d94
this is never re-assigned 2023-07-13 16:33:48 +01:00
Holly Stubbs 45479e2481 Merge branch 'master' of https://github.com/tgpholly/bufferStuff 2023-06-22 11:58:16 +01:00
Holly Stubbs 0980693807 remove unused imports 2023-06-22 11:58:12 +01:00
Holly Stubbs 23ccce4ca9
Attempt to fix the workflow 2023-06-22 11:47:51 +01:00
21 changed files with 675 additions and 2929 deletions

33
.github/workflows/browser.yml vendored Normal file
View File

@ -0,0 +1,33 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
name: Build for Browser
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
# Don't run updateCheck for now
#- run: npm run dev:updateCheck
- run: npm run buildweb
#- run: npm test

View File

@ -1,7 +1,7 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
name: Node.js CI
name: Build for NodeJS
on:
push:

View File

@ -1,9 +1,12 @@
.github/*
base/*
enums/*
index.ts
combined.ts
readers/*
writers/*
tooling/*
tsconfig.json
# Logs
logs

View File

@ -1,6 +1,6 @@
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
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>);
```
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
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/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#

387
base/BufferShim.ts Normal file
View File

@ -0,0 +1,387 @@
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
// Check LICENSE in repository root for more information.
// This is a mostly 1:1 feature complete implementation of node's Buffer class for browsers.
// It's missing some of the static stuff but everything in the object *should* be there.
class BrowserBuffer {
public buffer:Uint8Array;
private dataView:DataView;
public constructor(dataOrSize: Array<number> | ArrayBufferLike | number) {
if (typeof(dataOrSize) === "number") {
this.buffer = new Uint8Array(dataOrSize);
} else if (typeof(dataOrSize) === "object") {
this.buffer = new Uint8Array(dataOrSize); // Convert whatever comes in to a Uint8Array.
} else {
this.buffer = new Uint8Array(0); // Fallback
}
this.dataView = new DataView(this.buffer.buffer);
}
public static allocUnsafe(size:number) { return this.alloc(size); }
public static allocUnsafeSlow(size:number) { return this.alloc(size); }
public static alloc(size:number) {
return new BrowserBuffer(size);
}
public static concat(buffers:Array<BrowserBuffer>, totalLength?:number) {
let joinedArrays:Uint8Array;
if (totalLength !== undefined) {
joinedArrays = new Uint8Array(totalLength);
} else {
let arraysLength = 0;
for (const buffer of buffers) {
arraysLength += buffer.length;
}
joinedArrays = new Uint8Array(arraysLength);
}
let offset = 0;
for (const buffer of buffers) {
joinedArrays.set(buffer.buffer, offset);
offset += buffer.length;
}
return new BrowserBuffer(joinedArrays);
}
public static from(data: Array<number> | ArrayBufferLike | string) {
if (typeof(data) === "string") {
throw new Error("BrowserBuffer does not currently support creating buffers from strings.");
}
return new BrowserBuffer(data);
}
// NOTE: Here to match node buffer, has no use.
public static readonly poolSize:number = 8192;
// TODO: Implement
public static of() {}
public static isBuffer() {}
public static isEncoding() {}
public static byteLength() {}
public static copyBytesFrom() {}
public static compare() {}
public get length() {
return this.buffer.length;
}
private checkRanged(value:number|bigint, valueName:string, lowRange:number|bigint, highRange:number|bigint) {
if (value < lowRange || value > highRange) {
throw new Error(`The value of "${valueName}" is out of range. It must be >= ${lowRange} and <= ${highRange}. Received ${value}`);
}
}
private checkValue(value:number|bigint, low:number|bigint, high:number|bigint) {
this.checkRanged(value, "value", low, high);
}
private checkOffset(offset?:number) {
if (offset) {
this.checkRanged(offset, "offset", 0, this.buffer.length - 1);
}
}
// Writing methods
public writeInt8(value:number, offset:number) {
this.checkValue(value, -128, 127);
this.checkOffset(offset);
this.dataView.setInt8(offset, value);
return this;
}
public writeUInt8(value:number, offset:number) {
this.checkValue(value, 0, 255);
this.checkOffset(offset);
this.dataView.setUint8(offset, value);
return this;
}
// Little Endian
public writeInt16LE(value:number, offset:number) {
this.checkValue(value, -32768, 32767);
this.checkOffset(offset);
this.dataView.setInt16(offset, value, true);
return this;
}
public writeUInt16LE(value:number, offset:number) {
this.checkValue(value, 0, 65535);
this.checkOffset(offset);
this.dataView.setUint16(offset, value, true);
return this;
}
public writeInt32LE(value:number, offset:number) {
this.checkValue(value, -2147483648, 2147483647);
this.checkOffset(offset);
this.dataView.setInt32(offset, value, true);
return this;
}
public writeUInt32LE(value:number, offset:number) {
this.checkValue(value, 0, 4294967295);
this.checkOffset(offset);
this.dataView.setUint32(offset, value, true);
return this;
}
public writeBigInt64LE(value:bigint|number, offset:number) {
if (typeof(value) === "number") {
value = BigInt(value);
}
this.checkValue(value, -(2n ** 63n), 2n ** 63n);
this.checkOffset(offset);
this.dataView.setBigInt64(offset, value, true);
return this;
}
public writeBigUint64LE(value:bigint|number, offset:number) {
if (typeof(value) === "number") {
value = BigInt(value);
}
this.checkValue(value, 0n, 2n ** 64n);
this.checkOffset(offset);
this.dataView.setBigUint64(offset, value, true);
return this;
}
public writeFloatLE(value:number, offset:number) {
this.checkOffset(offset);
this.dataView.setFloat32(offset, value, true);
return this;
}
public writeDoubleLE(value:number, offset:number) {
this.checkOffset(offset);
this.dataView.setFloat64(offset, value, true);
return this;
}
// Big Endian
public writeInt16BE(value:number, offset:number) {
this.checkValue(value, -32768, 32767);
this.checkOffset(offset);
this.dataView.setInt16(offset, value, false);
return this;
}
public writeUInt16BE(value:number, offset:number) {
this.checkValue(value, 0, 65535);
this.checkOffset(offset);
this.dataView.setUint16(offset, value, false);
return this;
}
public writeInt32BE(value:number, offset:number) {
this.checkValue(value, -2147483648, 2147483647);
this.checkOffset(offset);
this.dataView.setInt32(offset, value, false);
return this;
}
public writeUInt32BE(value:number, offset:number) {
this.checkValue(value, 0, 4294967295);
this.checkOffset(offset);
this.dataView.setUint32(offset, value, false);
return this;
}
public writeBigInt64BE(value:bigint|number, offset:number) {
if (typeof(value) === "number") {
value = BigInt(value);
}
this.checkValue(value, -(2n ** 63n), 2n ** 63n);
this.checkOffset(offset);
this.dataView.setBigInt64(offset, value, false);
return this;
}
public writeBigUint64BE(value:bigint|number, offset:number) {
if (typeof(value) === "number") {
value = BigInt(value);
}
this.checkValue(value, 0n, 2n ** 64n);
this.checkOffset(offset);
this.dataView.setBigUint64(offset, value, false);
return this;
}
public writeFloatBE(value:number, offset:number) {
this.checkOffset(offset);
this.dataView.setFloat32(offset, value, false);
return this;
}
public writeDoubleBE(value:number, offset:number) {
this.checkOffset(offset);
this.dataView.setFloat64(offset, value, false);
return this;
}
// Reading methods
public readInt8(offset:number) {
this.checkOffset(offset);
return this.dataView.getInt8(offset);
}
public readUInt8(offset:number) {
this.checkOffset(offset);
return this.dataView.getUint8(offset);
}
// Little Endian
public readInt16LE(offset:number) {
this.checkOffset(offset);
return this.dataView.getInt16(offset, true);
}
public readUInt16LE(offset:number) {
this.checkOffset(offset);
return this.dataView.getUint16(offset, true);
}
public readInt32LE(offset:number) {
this.checkOffset(offset);
return this.dataView.getInt32(offset, true);
}
public readUInt32LE(offset:number) {
this.checkOffset(offset);
return this.dataView.getUint32(offset, true);
}
public readBigInt64LE(offset:number) {
this.checkOffset(offset);
return this.dataView.getBigInt64(offset, true);
}
public readBigUint64LE(offset:number) {
this.checkOffset(offset);
return this.dataView.getBigUint64(offset, true);
}
public readFloatLE(offset:number) {
this.checkOffset(offset);
return this.dataView.getFloat32(offset, true);
}
public readDoubleLE(offset:number) {
this.checkOffset(offset);
return this.dataView.getFloat64(offset, true);
}
// Big Endian
public readInt16BE(offset:number) {
this.checkOffset(offset);
return this.dataView.getInt16(offset, false);
}
public readUInt16BE(offset:number) {
this.checkOffset(offset);
return this.dataView.getUint16(offset, false);
}
public readInt32BE(offset:number) {
this.checkOffset(offset);
return this.dataView.getInt32(offset, false);
}
public readUInt32BE(offset:number) {
this.checkOffset(offset);
return this.dataView.getUint32(offset, false);
}
public readBigInt64BE(offset:number) {
this.checkOffset(offset);
return this.dataView.getBigInt64(offset, false);
}
public readBigUint64BE(offset:number) {
this.checkOffset(offset);
return this.dataView.getBigUint64(offset, false);
}
public readFloatBE(offset:number) {
this.checkOffset(offset);
return this.dataView.getFloat32(offset, false);
}
public readDoubleBE(offset:number) {
this.checkOffset(offset);
return this.dataView.getFloat64(offset, false);
}
}
export function getBufferClass() : BufferConstructor {
if (typeof(Buffer) === "undefined") {
// @ts-ignore
return BrowserBuffer;
} else {
return Buffer;
}
}

View File

@ -1,3 +1,6 @@
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
// Check LICENSE in repository root for more information.
export class ReaderBase {
public buffer:Buffer;
public offset:number;
@ -7,6 +10,14 @@ 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;
@ -39,4 +50,25 @@ 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

@ -1,14 +1,27 @@
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
// Check LICENSE in repository root for more information.
import { getBufferClass } from "./BufferShim";
export class WriterBase {
public buffer:Buffer;
public offset:number;
public readonly resizable:boolean;
public constructor(size:number = 0) {
this.buffer = Buffer.alloc(size);
this.buffer = getBufferClass().alloc(size);
this.offset = 0;
this.resizable = size === 0;
}
public get writeOffset() {
return this.offset;
}
public get length() {
return this.buffer.length;
}
public toBuffer() {
return this.buffer;
}
@ -18,20 +31,20 @@ export class WriterBase {
}
public writeBuffer(buffer:Buffer) {
this.buffer = Buffer.concat([this.buffer, buffer], this.buffer.length + buffer.length);
this.buffer = getBufferClass().concat([this.buffer, buffer], this.buffer.length + buffer.length);
return this;
}
public writeUint8Array(array:Uint8Array) {
this.writeBuffer(Buffer.from(array));
this.writeBuffer(getBufferClass().from(array));
return this;
}
public writeByte(value:number) {
if (this.resizable) {
const buffer = Buffer.alloc(1);
const buffer = getBufferClass().alloc(1);
buffer.writeInt8(value);
this.writeBuffer(buffer);
} else {
@ -44,7 +57,7 @@ export class WriterBase {
public writeUByte(value:number) {
if (this.resizable) {
const buffer = Buffer.alloc(1);
const buffer = getBufferClass().alloc(1);
buffer.writeUInt8(value);
this.writeBuffer(buffer);
} else {
@ -63,4 +76,19 @@ 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;
}
}

View File

@ -1,3 +1,6 @@
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
// Check LICENSE in repository root for more information.
import { IReader } from "./readers/IReader";
import { IWriter } from "./writers/IWriter";
import { ReaderBE } from "./readers/ReaderBE";

2881
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,33 +1,39 @@
{
"name": "bufferstuff",
"version": "1.3.0",
"version": "1.5.1",
"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",
"scripts": {
"updateCheck": "check-outdated",
"_clean": "tsc --build --clean",
"build": "npm-run-all build:*",
"build:smash": "ts-node ./tooling/fileSmasher.ts",
"build:build": "tsc --build",
"build:cleanup": "ts-node ./tooling/cleanup.ts",
"_clean": "tsc --build --clean"
"buildweb": "npm-run-all buildweb:*",
"buildweb:smash": "ts-node ./tooling/fileSmasher.ts forweb",
"buildweb:build": "tsc --build",
"buildweb:cleanup": "ts-node ./tooling/cleanup.ts"
},
"repository": {
"type": "git",
"url": "git+https://github.com/tgpholly/bufferStuff.git"
"url": "https://git.eusv.net/tgpholly/bufferStuff.git"
},
"keywords": [],
"author": "tgpholly",
"license": "MIT",
"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": {
"check-outdated": "^2.11.0",
"ts-loader": "^9.4.3",
"ts-node": "^10.9.1",
"typescript": "^5.1.3",
"npm-run-all": "^4.1.5"
"check-outdated": "^2.12.0",
"npm-run-all": "^4.1.5",
"terser": "^5.27.0",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
}
}

View File

@ -1,9 +1,16 @@
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
// 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,
@ -12,7 +19,7 @@ export interface IReader {
readULong(): bigint,
readFloat(): number,
readDouble(): number,
readShortString(): string,
readString(): string,
readShortsAsString(shortsToRead:number): string,
readString16(): string,
}

View File

@ -1,5 +1,8 @@
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
// Check LICENSE in repository root for more information.
import { IReader } from "./IReader";
import { ReaderBase } from "./ReaderBase";
import { ReaderBase } from "../base/ReaderBase";
export class ReaderBE extends ReaderBase implements IReader {
public readShort() {
@ -50,20 +53,9 @@ 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:string = "";
let text = "";
for (let i = 0; i < length; i++) {
text += String.fromCharCode(this.readUByte());
@ -74,7 +66,7 @@ export class ReaderBE extends ReaderBase implements IReader {
public readString16() {
const length = this.readUShort();
let text:string = "";
let text = "";
for (let i = 0; i < length; i++) {
text += String.fromCharCode(this.readUShort());
@ -82,4 +74,14 @@ 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

@ -1,5 +1,8 @@
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
// Check LICENSE in repository root for more information.
import { IReader } from "./IReader";
import { ReaderBase } from "./ReaderBase";
import { ReaderBase } from "../base/ReaderBase";
export class ReaderLE extends ReaderBase implements IReader {
public readShort() {
@ -50,17 +53,6 @@ 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 = "";
@ -82,4 +74,14 @@ 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

@ -1,3 +1,6 @@
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
// Check LICENSE in repository root for more information.
import { readdirSync, rmSync, renameSync } from "fs";
const libFiles = readdirSync("./lib");

View File

@ -1,21 +1,25 @@
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
// Check LICENSE in repository root for more information.
// fileSmasher ~.~
// for when you're just too lazy to
// do it properly.
import { readdirSync, statSync, readFileSync, writeFileSync, mkdirSync, existsSync } from "fs";
import { readdirSync, lstatSync, readFileSync, writeFileSync } from "fs";
let tsFileData:Array<string> = new Array<string>();
const tsFileData:Array<string> = new Array<string>();
// Github Actions forced my hand
const toolinglessFolderPath = __dirname.replace("/tooling", "/");
function readDir(nam:string) {
const files = readdirSync(nam);
for (const file of files) {
if (nam == "./" && (file.startsWith(".") || file == "tooling" || file == "lib" || file == "node_modules" || file == "combined.ts")) {
if (nam == toolinglessFolderPath && (file.startsWith(".") || file == "tooling" || file == "lib" || file == "node_modules" || file == "combined.ts")) {
continue;
}
// This is a very dumb way of checking for folders
// protip: don't do this.
if (statSync(`${nam}/${file}`).size == 0) {
if (lstatSync(`${nam}/${file}`).isDirectory()) {
readDir(`${nam}/${file}`);
} else if (file.endsWith(".ts")) {
tsFileData.push(readFileSync((`${nam}/${file}`).replace("//", "/")).toString());
@ -23,7 +27,7 @@ function readDir(nam:string) {
}
}
readDir("./");
readDir(toolinglessFolderPath);
const combinedFiles = tsFileData.join("\n");
@ -59,8 +63,11 @@ for (const line of splitLines) {
continue;
}
// Fix up classes, interfaces and such.
resultLines.push(checkForMatchAndReplace(line));
//resultLines.push(line.replace("export class", "class").replace("export interface", "interface").replace("export enum", "enum"));
if (process.argv[2] === "forweb") {
resultLines.push(line.replace("export class", "class").replace("export function", "function").replace("export enum", "enum").replace("export interface", "interface"));
} else {
resultLines.push(checkForMatchAndReplace(line));
}
}
writeFileSync("./combined.ts", resultLines.join("\n"));
writeFileSync("./combined.ts", resultLines.join("\n"));

10
tooling/mangle.ts Normal file
View File

@ -0,0 +1,10 @@
import { readFileSync, writeFileSync } from "fs";
import { minify } from "terser";
(async () => {
const mangled = await minify(readFileSync("./lib/index.js").toString(), {
mangle: true,
toplevel: true,
});
writeFileSync("./lib/index.min.js", `${mangled.code}`);
})();

View File

@ -2,7 +2,7 @@
"compilerOptions": {
"module": "CommonJS",
"moduleResolution": "node",
"target": "es6",
"target": "ES2020",
"esModuleInterop": true,
"resolveJsonModule": true,
"rootDir": "./",

View File

@ -1,4 +1,9 @@
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
// Check LICENSE in repository root for more information.
export interface IWriter {
writeOffset: number,
length: number,
toBuffer(): Buffer,
toString(): string,
writeBuffer(buffer:Buffer): IWriter,
@ -6,15 +11,17 @@ 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): IWriter,
writeULong(value:number|bigint): 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

@ -1,10 +1,14 @@
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
// Check LICENSE in repository root for more information.
import { IWriter } from "./IWriter";
import { WriterBase } from "./WriterBase";
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 = Buffer.alloc(2);
const buffer = getBufferClass().alloc(2);
buffer.writeInt16BE(value);
this.writeBuffer(buffer);
} else {
@ -17,7 +21,7 @@ export class WriterBE extends WriterBase implements IWriter {
public writeUShort(value:number) {
if (this.resizable) {
const buffer = Buffer.alloc(2);
const buffer = getBufferClass().alloc(2);
buffer.writeUInt16BE(value);
this.writeBuffer(buffer);
} else {
@ -30,7 +34,7 @@ export class WriterBE extends WriterBase implements IWriter {
public writeInt(value:number) {
if (this.resizable) {
const buffer = Buffer.alloc(4);
const buffer = getBufferClass().alloc(4);
buffer.writeInt32BE(value);
this.writeBuffer(buffer);
} else {
@ -43,7 +47,7 @@ export class WriterBE extends WriterBase implements IWriter {
public writeUInt(value:number) {
if (this.resizable) {
const buffer = Buffer.alloc(4);
const buffer = getBufferClass().alloc(4);
buffer.writeUInt32BE(value);
this.writeBuffer(buffer);
} else {
@ -60,7 +64,7 @@ export class WriterBE extends WriterBase implements IWriter {
}
if (this.resizable) {
const buffer = Buffer.alloc(8);
const buffer = getBufferClass().alloc(8);
buffer.writeBigInt64BE(value);
this.writeBuffer(buffer);
} else {
@ -77,7 +81,7 @@ export class WriterBE extends WriterBase implements IWriter {
}
if (this.resizable) {
const buffer = Buffer.alloc(8);
const buffer = getBufferClass().alloc(8);
buffer.writeBigUint64BE(value);
this.writeBuffer(buffer);
} else {
@ -90,7 +94,7 @@ export class WriterBE extends WriterBase implements IWriter {
public writeFloat(value:number) {
if (this.resizable) {
const buffer = Buffer.alloc(4);
const buffer = getBufferClass().alloc(4);
buffer.writeFloatBE(value);
this.writeBuffer(buffer);
} else {
@ -103,7 +107,7 @@ export class WriterBE extends WriterBase implements IWriter {
public writeDouble(value:number) {
if (this.resizable) {
const buffer = Buffer.alloc(8);
const buffer = getBufferClass().alloc(8);
buffer.writeDoubleBE(value);
this.writeBuffer(buffer);
} else {
@ -143,4 +147,19 @@ 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

@ -1,10 +1,14 @@
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
// Check LICENSE in repository root for more information.
import { IWriter } from "./IWriter";
import { WriterBase } from "./WriterBase";
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 = Buffer.alloc(2);
const buffer = getBufferClass().alloc(2);
buffer.writeInt16LE(value);
this.writeBuffer(buffer);
} else {
@ -17,7 +21,7 @@ export class WriterLE extends WriterBase implements IWriter {
public writeUShort(value:number) {
if (this.resizable) {
const buffer = Buffer.alloc(2);
const buffer = getBufferClass().alloc(2);
buffer.writeUInt16LE(value);
this.writeBuffer(buffer);
} else {
@ -30,7 +34,7 @@ export class WriterLE extends WriterBase implements IWriter {
public writeInt(value:number) {
if (this.resizable) {
const buffer = Buffer.alloc(4);
const buffer = getBufferClass().alloc(4);
buffer.writeInt32LE(value);
this.writeBuffer(buffer);
} else {
@ -42,7 +46,7 @@ export class WriterLE extends WriterBase implements IWriter {
}
public writeUInt(value:number) {
if (this.resizable) {
const buffer = Buffer.alloc(4);
const buffer = getBufferClass().alloc(4);
buffer.writeUInt32LE(value);
this.writeBuffer(buffer);
} else {
@ -59,7 +63,7 @@ export class WriterLE extends WriterBase implements IWriter {
}
if (this.resizable) {
const buffer = Buffer.alloc(8);
const buffer = getBufferClass().alloc(8);
buffer.writeBigInt64LE(value);
this.writeBuffer(buffer);
} else {
@ -76,7 +80,7 @@ export class WriterLE extends WriterBase implements IWriter {
}
if (this.resizable) {
const buffer = Buffer.alloc(8);
const buffer = getBufferClass().alloc(8);
buffer.writeBigUint64LE(value);
this.writeBuffer(buffer);
} else {
@ -89,7 +93,7 @@ export class WriterLE extends WriterBase implements IWriter {
public writeFloat(value:number) {
if (this.resizable) {
const buffer = Buffer.alloc(4);
const buffer = getBufferClass().alloc(4);
buffer.writeFloatLE(value);
this.writeBuffer(buffer);
} else {
@ -102,7 +106,7 @@ export class WriterLE extends WriterBase implements IWriter {
public writeDouble(value:number) {
if (this.resizable) {
const buffer = Buffer.alloc(8);
const buffer = getBufferClass().alloc(8);
buffer.writeDoubleLE(value);
this.writeBuffer(buffer);
} else {
@ -142,4 +146,19 @@ 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;
}
}