Compare commits
41 Commits
Author | SHA1 | Date |
---|---|---|
|
ceffc3eb87 | |
|
c097cc5480 | |
|
71a717ae80 | |
|
d02bf4693b | |
|
2e6ec04512 | |
|
33e8466e9d | |
|
77d74ae657 | |
|
961df8cd72 | |
|
4cacdbf113 | |
|
b7a482d1f2 | |
|
c1e43e2937 | |
|
63a3e5c73b | |
|
3bb2f542ef | |
|
24f26db2c1 | |
|
242b3f512b | |
|
dd46ec825b | |
|
a157685fcc | |
|
8367213725 | |
|
d12d971ef5 | |
|
dd3eb43809 | |
|
aabf5cdbb3 | |
|
558c3c2025 | |
|
ff46656809 | |
|
3a75bec4bf | |
|
f4ac03725c | |
|
a43fa8ef66 | |
|
2d7815cff8 | |
|
530b468dfb | |
|
0c253b1c95 | |
|
842bcca1f4 | |
|
61cc7e027e | |
|
5498168d94 | |
|
45479e2481 | |
|
0980693807 | |
|
23ccce4ca9 | |
|
cc47903e47 | |
|
9aa1ad9a5d | |
|
07fa9c0ecf | |
|
ecaa8f28a6 | |
|
ff90cae559 | |
|
b1596e1c45 |
|
@ -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
|
|
@ -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
|
# 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
|
# 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:
|
on:
|
||||||
push:
|
push:
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
# Build folder
|
||||||
|
lib/
|
||||||
|
combined.ts
|
||||||
|
|
||||||
# Logs
|
# Logs
|
||||||
logs
|
logs
|
||||||
*.log
|
*.log
|
||||||
|
|
|
@ -0,0 +1,140 @@
|
||||||
|
.github/*
|
||||||
|
base/*
|
||||||
|
enums/*
|
||||||
|
index.ts
|
||||||
|
combined.ts
|
||||||
|
readers/*
|
||||||
|
writers/*
|
||||||
|
tooling/*
|
||||||
|
tsconfig.json
|
||||||
|
|
||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
lerna-debug.log*
|
||||||
|
.pnpm-debug.log*
|
||||||
|
|
||||||
|
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||||
|
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||||
|
|
||||||
|
# Runtime data
|
||||||
|
pids
|
||||||
|
*.pid
|
||||||
|
*.seed
|
||||||
|
*.pid.lock
|
||||||
|
|
||||||
|
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||||
|
lib-cov
|
||||||
|
|
||||||
|
# Coverage directory used by tools like istanbul
|
||||||
|
coverage
|
||||||
|
*.lcov
|
||||||
|
|
||||||
|
# nyc test coverage
|
||||||
|
.nyc_output
|
||||||
|
|
||||||
|
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||||
|
.grunt
|
||||||
|
|
||||||
|
# Bower dependency directory (https://bower.io/)
|
||||||
|
bower_components
|
||||||
|
|
||||||
|
# node-waf configuration
|
||||||
|
.lock-wscript
|
||||||
|
|
||||||
|
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||||
|
build/Release
|
||||||
|
|
||||||
|
# Dependency directories
|
||||||
|
node_modules/
|
||||||
|
jspm_packages/
|
||||||
|
|
||||||
|
# Snowpack dependency directory (https://snowpack.dev/)
|
||||||
|
web_modules/
|
||||||
|
|
||||||
|
# TypeScript cache
|
||||||
|
*.tsbuildinfo
|
||||||
|
|
||||||
|
# Optional npm cache directory
|
||||||
|
.npm
|
||||||
|
|
||||||
|
# Optional eslint cache
|
||||||
|
.eslintcache
|
||||||
|
|
||||||
|
# Optional stylelint cache
|
||||||
|
.stylelintcache
|
||||||
|
|
||||||
|
# Microbundle cache
|
||||||
|
.rpt2_cache/
|
||||||
|
.rts2_cache_cjs/
|
||||||
|
.rts2_cache_es/
|
||||||
|
.rts2_cache_umd/
|
||||||
|
|
||||||
|
# Optional REPL history
|
||||||
|
.node_repl_history
|
||||||
|
|
||||||
|
# Output of 'npm pack'
|
||||||
|
*.tgz
|
||||||
|
|
||||||
|
# Yarn Integrity file
|
||||||
|
.yarn-integrity
|
||||||
|
|
||||||
|
# dotenv environment variable files
|
||||||
|
.env
|
||||||
|
.env.development.local
|
||||||
|
.env.test.local
|
||||||
|
.env.production.local
|
||||||
|
.env.local
|
||||||
|
|
||||||
|
# parcel-bundler cache (https://parceljs.org/)
|
||||||
|
.cache
|
||||||
|
.parcel-cache
|
||||||
|
|
||||||
|
# Next.js build output
|
||||||
|
.next
|
||||||
|
out
|
||||||
|
|
||||||
|
# Nuxt.js build / generate output
|
||||||
|
.nuxt
|
||||||
|
dist
|
||||||
|
|
||||||
|
# Gatsby files
|
||||||
|
.cache/
|
||||||
|
# Comment in the public line in if your project uses Gatsby and not Next.js
|
||||||
|
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||||
|
# public
|
||||||
|
|
||||||
|
# vuepress build output
|
||||||
|
.vuepress/dist
|
||||||
|
|
||||||
|
# vuepress v2.x temp and cache directory
|
||||||
|
.temp
|
||||||
|
.cache
|
||||||
|
|
||||||
|
# Docusaurus cache and generated files
|
||||||
|
.docusaurus
|
||||||
|
|
||||||
|
# Serverless directories
|
||||||
|
.serverless/
|
||||||
|
|
||||||
|
# FuseBox cache
|
||||||
|
.fusebox/
|
||||||
|
|
||||||
|
# DynamoDB Local files
|
||||||
|
.dynamodb/
|
||||||
|
|
||||||
|
# TernJS port file
|
||||||
|
.tern-port
|
||||||
|
|
||||||
|
# Stores VSCode versions used for testing VSCode extensions
|
||||||
|
.vscode-test
|
||||||
|
|
||||||
|
# yarn v2
|
||||||
|
.yarn/cache
|
||||||
|
.yarn/unplugged
|
||||||
|
.yarn/build-state.yml
|
||||||
|
.yarn/install-state.gz
|
||||||
|
.pnp.*
|
2
LICENSE
2
LICENSE
|
@ -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
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# bufferStuff [![CodeFactor](https://www.codefactor.io/repository/github/tgpholly/bufferstuff/badge)](https://www.codefactor.io/repository/github/tgpholly/bufferstuff) [![Node.js CI](https://github.com/tgpholly/bufferStuff/actions/workflows/node.js.yml/badge.svg?branch=master)](https://github.com/tgpholly/bufferStuff/actions/workflows/node.js.yml)
|
# bufferStuff [![CodeFactor](https://www.codefactor.io/repository/github/tgpholly/bufferstuff/badge)](https://www.codefactor.io/repository/github/tgpholly/bufferstuff) [![Node.js CI](https://github.com/tgpholly/bufferStuff/actions/workflows/node.js.yml/badge.svg?branch=master)](https://github.com/tgpholly/bufferStuff/actions/workflows/node.js.yml) [![npm](https://img.shields.io/npm/v/bufferstuff)](https://www.npmjs.com/package/bufferstuff)
|
||||||
A set of utility classes for reading and writing binary data in NodeJS.
|
A set of utility classes for reading and writing binary data in NodeJS.
|
||||||
|
|
||||||
## Motivations
|
## Motivations
|
||||||
|
@ -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#
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,6 @@
|
||||||
|
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
|
||||||
|
// Check LICENSE in repository root for more information.
|
||||||
|
|
||||||
export class ReaderBase {
|
export class ReaderBase {
|
||||||
public buffer:Buffer;
|
public buffer:Buffer;
|
||||||
public offset:number;
|
public offset:number;
|
||||||
|
@ -7,6 +10,14 @@ export class ReaderBase {
|
||||||
this.offset = 0;
|
this.offset = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public get readOffset() {
|
||||||
|
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;
|
||||||
|
@ -39,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;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -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 {
|
export class WriterBase {
|
||||||
public buffer:Buffer;
|
public buffer:Buffer;
|
||||||
public offset:number;
|
public offset:number;
|
||||||
public readonly resizable:boolean;
|
public readonly resizable:boolean;
|
||||||
|
|
||||||
public constructor(size:number = 0) {
|
public constructor(size:number = 0) {
|
||||||
this.buffer = Buffer.alloc(size);
|
this.buffer = getBufferClass().alloc(size);
|
||||||
this.offset = 0;
|
this.offset = 0;
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
@ -18,20 +31,20 @@ export class WriterBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public writeBuffer(buffer:Buffer) {
|
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;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public writeUint8Array(array:Uint8Array) {
|
public writeUint8Array(array:Uint8Array) {
|
||||||
this.writeBuffer(Buffer.from(array));
|
this.writeBuffer(getBufferClass().from(array));
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public writeByte(value:number) {
|
public writeByte(value:number) {
|
||||||
if (this.resizable) {
|
if (this.resizable) {
|
||||||
const buffer = Buffer.alloc(1);
|
const buffer = getBufferClass().alloc(1);
|
||||||
buffer.writeInt8(value);
|
buffer.writeInt8(value);
|
||||||
this.writeBuffer(buffer);
|
this.writeBuffer(buffer);
|
||||||
} else {
|
} else {
|
||||||
|
@ -44,7 +57,7 @@ export class WriterBase {
|
||||||
|
|
||||||
public writeUByte(value:number) {
|
public writeUByte(value:number) {
|
||||||
if (this.resizable) {
|
if (this.resizable) {
|
||||||
const buffer = Buffer.alloc(1);
|
const buffer = getBufferClass().alloc(1);
|
||||||
buffer.writeUInt8(value);
|
buffer.writeUInt8(value);
|
||||||
this.writeBuffer(buffer);
|
this.writeBuffer(buffer);
|
||||||
} else {
|
} else {
|
||||||
|
@ -63,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;
|
||||||
|
}
|
||||||
}
|
}
|
3
index.ts
3
index.ts
|
@ -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 { IReader } from "./readers/IReader";
|
||||||
import { IWriter } from "./writers/IWriter";
|
import { IWriter } from "./writers/IWriter";
|
||||||
import { ReaderBE } from "./readers/ReaderBE";
|
import { ReaderBE } from "./readers/ReaderBE";
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
/// <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
30
lib/index.js
|
@ -1,30 +0,0 @@
|
||||||
"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;
|
|
|
@ -1,19 +0,0 @@
|
||||||
/// <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;
|
|
||||||
}
|
|
|
@ -1,2 +0,0 @@
|
||||||
"use strict";
|
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
@ -1,15 +0,0 @@
|
||||||
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;
|
|
||||||
}
|
|
|
@ -1,71 +0,0 @@
|
||||||
"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;
|
|
|
@ -1,11 +0,0 @@
|
||||||
/// <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;
|
|
||||||
}
|
|
|
@ -1,38 +0,0 @@
|
||||||
"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;
|
|
|
@ -1,15 +0,0 @@
|
||||||
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;
|
|
||||||
}
|
|
|
@ -1,71 +0,0 @@
|
||||||
"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;
|
|
|
@ -1,21 +0,0 @@
|
||||||
/// <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;
|
|
||||||
}
|
|
|
@ -1,2 +0,0 @@
|
||||||
"use strict";
|
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
@ -1,15 +0,0 @@
|
||||||
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;
|
|
||||||
}
|
|
|
@ -1,130 +0,0 @@
|
||||||
"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;
|
|
|
@ -1,14 +0,0 @@
|
||||||
/// <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;
|
|
||||||
}
|
|
|
@ -1,56 +0,0 @@
|
||||||
"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;
|
|
|
@ -1,15 +0,0 @@
|
||||||
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;
|
|
||||||
}
|
|
|
@ -1,130 +0,0 @@
|
||||||
"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;
|
|
File diff suppressed because it is too large
Load Diff
32
package.json
32
package.json
|
@ -1,29 +1,39 @@
|
||||||
{
|
{
|
||||||
"name": "bufferstuff",
|
"name": "bufferstuff",
|
||||||
"version": "1.1.2",
|
"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",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"updateCheck": "check-outdated",
|
"updateCheck": "check-outdated",
|
||||||
"build": "tsc --build",
|
"_clean": "tsc --build --clean",
|
||||||
"_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",
|
||||||
|
|
||||||
|
"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": {
|
"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.11.0",
|
"check-outdated": "^2.12.0",
|
||||||
"ts-loader": "^9.4.2",
|
"npm-run-all": "^4.1.5",
|
||||||
"ts-node": "^10.9.1",
|
"terser": "^5.27.0",
|
||||||
"typescript": "^5.0.4"
|
"ts-node": "^10.9.2",
|
||||||
|
"typescript": "^5.3.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,16 @@
|
||||||
|
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
|
||||||
|
// 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,
|
||||||
|
@ -12,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,
|
||||||
}
|
}
|
|
@ -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 { IReader } from "./IReader";
|
||||||
import { ReaderBase } from "./ReaderBase";
|
import { ReaderBase } from "../base/ReaderBase";
|
||||||
|
|
||||||
export class ReaderBE extends ReaderBase implements IReader {
|
export class ReaderBE extends ReaderBase implements IReader {
|
||||||
public readShort() {
|
public readShort() {
|
||||||
|
@ -50,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());
|
||||||
|
@ -74,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());
|
||||||
|
@ -82,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;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -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 { IReader } from "./IReader";
|
||||||
import { ReaderBase } from "./ReaderBase";
|
import { ReaderBase } from "../base/ReaderBase";
|
||||||
|
|
||||||
export class ReaderLE extends ReaderBase implements IReader {
|
export class ReaderLE extends ReaderBase implements IReader {
|
||||||
public readShort() {
|
public readShort() {
|
||||||
|
@ -50,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 = "";
|
||||||
|
@ -82,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;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
// 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");
|
||||||
|
|
||||||
|
for (const file of libFiles) {
|
||||||
|
if (!file.startsWith("combined")) {
|
||||||
|
rmSync(`./lib/${file}`, { recursive: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
renameSync("./lib/combined.js", "./lib/index.js");
|
||||||
|
renameSync("./lib/combined.d.ts", "./lib/index.d.ts");
|
|
@ -0,0 +1,73 @@
|
||||||
|
// 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, lstatSync, readFileSync, writeFileSync } from "fs";
|
||||||
|
|
||||||
|
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 == toolinglessFolderPath && (file.startsWith(".") || file == "tooling" || file == "lib" || file == "node_modules" || file == "combined.ts")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lstatSync(`${nam}/${file}`).isDirectory()) {
|
||||||
|
readDir(`${nam}/${file}`);
|
||||||
|
} else if (file.endsWith(".ts")) {
|
||||||
|
tsFileData.push(readFileSync((`${nam}/${file}`).replace("//", "/")).toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
readDir(toolinglessFolderPath);
|
||||||
|
|
||||||
|
const combinedFiles = tsFileData.join("\n");
|
||||||
|
|
||||||
|
const splitLines = combinedFiles.split("\n");
|
||||||
|
const resultLines:Array<string> = new Array<string>();
|
||||||
|
|
||||||
|
const unExport = [
|
||||||
|
"class:ReaderBase",
|
||||||
|
"class:ReaderLE",
|
||||||
|
"class:ReaderBE",
|
||||||
|
"class:WriterBase",
|
||||||
|
"class:WriterLE",
|
||||||
|
"class:WriterBE",
|
||||||
|
];
|
||||||
|
|
||||||
|
function checkForMatchAndReplace(s:string) {
|
||||||
|
for (const tUExp of unExport) {
|
||||||
|
const spl = tUExp.split(":");
|
||||||
|
const type = spl[0];
|
||||||
|
if (s.startsWith(`export ${type} ${spl[1]}`)) {
|
||||||
|
return s.replace(`export ${type} ${spl[1]}`, `${type} ${spl[1]}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Let's process the file to make it usable
|
||||||
|
for (const line of splitLines) {
|
||||||
|
// Throw away imports as they aren't needed
|
||||||
|
// TODO: Add allow list for npm module imports
|
||||||
|
if (line.startsWith("import")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// Fix up classes, interfaces and such.
|
||||||
|
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"));
|
|
@ -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}`);
|
||||||
|
})();
|
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"module": "commonjs",
|
"module": "CommonJS",
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
"target": "es6",
|
"target": "ES2020",
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
"rootDir": "./",
|
"rootDir": "./",
|
||||||
"outDir": "./lib",
|
"outDir": "./lib/",
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"declaration": true
|
"declaration": true
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,9 @@
|
||||||
|
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
|
||||||
|
// 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,
|
||||||
|
@ -6,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
|
||||||
}
|
}
|
|
@ -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 { IWriter } from "./IWriter";
|
||||||
import { WriterBase } from "./WriterBase";
|
import { WriterBase } from "../base/WriterBase";
|
||||||
|
import { getBufferClass } from "../base/BufferShim";
|
||||||
|
|
||||||
export class WriterBE extends WriterBase implements IWriter {
|
export class WriterBE extends WriterBase implements IWriter {
|
||||||
public writeShort(value:number) {
|
public writeShort(value:number) {
|
||||||
if (this.resizable) {
|
if (this.resizable) {
|
||||||
const buffer = Buffer.alloc(2);
|
const buffer = getBufferClass().alloc(2);
|
||||||
buffer.writeInt16BE(value);
|
buffer.writeInt16BE(value);
|
||||||
this.writeBuffer(buffer);
|
this.writeBuffer(buffer);
|
||||||
} else {
|
} else {
|
||||||
|
@ -17,7 +21,7 @@ export class WriterBE extends WriterBase implements IWriter {
|
||||||
|
|
||||||
public writeUShort(value:number) {
|
public writeUShort(value:number) {
|
||||||
if (this.resizable) {
|
if (this.resizable) {
|
||||||
const buffer = Buffer.alloc(2);
|
const buffer = getBufferClass().alloc(2);
|
||||||
buffer.writeUInt16BE(value);
|
buffer.writeUInt16BE(value);
|
||||||
this.writeBuffer(buffer);
|
this.writeBuffer(buffer);
|
||||||
} else {
|
} else {
|
||||||
|
@ -30,7 +34,7 @@ export class WriterBE extends WriterBase implements IWriter {
|
||||||
|
|
||||||
public writeInt(value:number) {
|
public writeInt(value:number) {
|
||||||
if (this.resizable) {
|
if (this.resizable) {
|
||||||
const buffer = Buffer.alloc(4);
|
const buffer = getBufferClass().alloc(4);
|
||||||
buffer.writeInt32BE(value);
|
buffer.writeInt32BE(value);
|
||||||
this.writeBuffer(buffer);
|
this.writeBuffer(buffer);
|
||||||
} else {
|
} else {
|
||||||
|
@ -43,7 +47,7 @@ export class WriterBE extends WriterBase implements IWriter {
|
||||||
|
|
||||||
public writeUInt(value:number) {
|
public writeUInt(value:number) {
|
||||||
if (this.resizable) {
|
if (this.resizable) {
|
||||||
const buffer = Buffer.alloc(4);
|
const buffer = getBufferClass().alloc(4);
|
||||||
buffer.writeUInt32BE(value);
|
buffer.writeUInt32BE(value);
|
||||||
this.writeBuffer(buffer);
|
this.writeBuffer(buffer);
|
||||||
} else {
|
} else {
|
||||||
|
@ -60,7 +64,7 @@ export class WriterBE extends WriterBase implements IWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.resizable) {
|
if (this.resizable) {
|
||||||
const buffer = Buffer.alloc(8);
|
const buffer = getBufferClass().alloc(8);
|
||||||
buffer.writeBigInt64BE(value);
|
buffer.writeBigInt64BE(value);
|
||||||
this.writeBuffer(buffer);
|
this.writeBuffer(buffer);
|
||||||
} else {
|
} else {
|
||||||
|
@ -77,7 +81,7 @@ export class WriterBE extends WriterBase implements IWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.resizable) {
|
if (this.resizable) {
|
||||||
const buffer = Buffer.alloc(8);
|
const buffer = getBufferClass().alloc(8);
|
||||||
buffer.writeBigUint64BE(value);
|
buffer.writeBigUint64BE(value);
|
||||||
this.writeBuffer(buffer);
|
this.writeBuffer(buffer);
|
||||||
} else {
|
} else {
|
||||||
|
@ -90,7 +94,7 @@ export class WriterBE extends WriterBase implements IWriter {
|
||||||
|
|
||||||
public writeFloat(value:number) {
|
public writeFloat(value:number) {
|
||||||
if (this.resizable) {
|
if (this.resizable) {
|
||||||
const buffer = Buffer.alloc(4);
|
const buffer = getBufferClass().alloc(4);
|
||||||
buffer.writeFloatBE(value);
|
buffer.writeFloatBE(value);
|
||||||
this.writeBuffer(buffer);
|
this.writeBuffer(buffer);
|
||||||
} else {
|
} else {
|
||||||
|
@ -103,7 +107,7 @@ export class WriterBE extends WriterBase implements IWriter {
|
||||||
|
|
||||||
public writeDouble(value:number) {
|
public writeDouble(value:number) {
|
||||||
if (this.resizable) {
|
if (this.resizable) {
|
||||||
const buffer = Buffer.alloc(8);
|
const buffer = getBufferClass().alloc(8);
|
||||||
buffer.writeDoubleBE(value);
|
buffer.writeDoubleBE(value);
|
||||||
this.writeBuffer(buffer);
|
this.writeBuffer(buffer);
|
||||||
} else {
|
} else {
|
||||||
|
@ -143,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;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -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 { IWriter } from "./IWriter";
|
||||||
import { WriterBase } from "./WriterBase";
|
import { WriterBase } from "../base/WriterBase";
|
||||||
|
import { getBufferClass } from "../base/BufferShim";
|
||||||
|
|
||||||
export class WriterLE extends WriterBase implements IWriter {
|
export class WriterLE extends WriterBase implements IWriter {
|
||||||
public writeShort(value:number) {
|
public writeShort(value:number) {
|
||||||
if (this.resizable) {
|
if (this.resizable) {
|
||||||
const buffer = Buffer.alloc(2);
|
const buffer = getBufferClass().alloc(2);
|
||||||
buffer.writeInt16LE(value);
|
buffer.writeInt16LE(value);
|
||||||
this.writeBuffer(buffer);
|
this.writeBuffer(buffer);
|
||||||
} else {
|
} else {
|
||||||
|
@ -17,7 +21,7 @@ export class WriterLE extends WriterBase implements IWriter {
|
||||||
|
|
||||||
public writeUShort(value:number) {
|
public writeUShort(value:number) {
|
||||||
if (this.resizable) {
|
if (this.resizable) {
|
||||||
const buffer = Buffer.alloc(2);
|
const buffer = getBufferClass().alloc(2);
|
||||||
buffer.writeUInt16LE(value);
|
buffer.writeUInt16LE(value);
|
||||||
this.writeBuffer(buffer);
|
this.writeBuffer(buffer);
|
||||||
} else {
|
} else {
|
||||||
|
@ -30,7 +34,7 @@ export class WriterLE extends WriterBase implements IWriter {
|
||||||
|
|
||||||
public writeInt(value:number) {
|
public writeInt(value:number) {
|
||||||
if (this.resizable) {
|
if (this.resizable) {
|
||||||
const buffer = Buffer.alloc(4);
|
const buffer = getBufferClass().alloc(4);
|
||||||
buffer.writeInt32LE(value);
|
buffer.writeInt32LE(value);
|
||||||
this.writeBuffer(buffer);
|
this.writeBuffer(buffer);
|
||||||
} else {
|
} else {
|
||||||
|
@ -42,7 +46,7 @@ export class WriterLE extends WriterBase implements IWriter {
|
||||||
}
|
}
|
||||||
public writeUInt(value:number) {
|
public writeUInt(value:number) {
|
||||||
if (this.resizable) {
|
if (this.resizable) {
|
||||||
const buffer = Buffer.alloc(4);
|
const buffer = getBufferClass().alloc(4);
|
||||||
buffer.writeUInt32LE(value);
|
buffer.writeUInt32LE(value);
|
||||||
this.writeBuffer(buffer);
|
this.writeBuffer(buffer);
|
||||||
} else {
|
} else {
|
||||||
|
@ -59,7 +63,7 @@ export class WriterLE extends WriterBase implements IWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.resizable) {
|
if (this.resizable) {
|
||||||
const buffer = Buffer.alloc(8);
|
const buffer = getBufferClass().alloc(8);
|
||||||
buffer.writeBigInt64LE(value);
|
buffer.writeBigInt64LE(value);
|
||||||
this.writeBuffer(buffer);
|
this.writeBuffer(buffer);
|
||||||
} else {
|
} else {
|
||||||
|
@ -76,7 +80,7 @@ export class WriterLE extends WriterBase implements IWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.resizable) {
|
if (this.resizable) {
|
||||||
const buffer = Buffer.alloc(8);
|
const buffer = getBufferClass().alloc(8);
|
||||||
buffer.writeBigUint64LE(value);
|
buffer.writeBigUint64LE(value);
|
||||||
this.writeBuffer(buffer);
|
this.writeBuffer(buffer);
|
||||||
} else {
|
} else {
|
||||||
|
@ -89,7 +93,7 @@ export class WriterLE extends WriterBase implements IWriter {
|
||||||
|
|
||||||
public writeFloat(value:number) {
|
public writeFloat(value:number) {
|
||||||
if (this.resizable) {
|
if (this.resizable) {
|
||||||
const buffer = Buffer.alloc(4);
|
const buffer = getBufferClass().alloc(4);
|
||||||
buffer.writeFloatLE(value);
|
buffer.writeFloatLE(value);
|
||||||
this.writeBuffer(buffer);
|
this.writeBuffer(buffer);
|
||||||
} else {
|
} else {
|
||||||
|
@ -102,7 +106,7 @@ export class WriterLE extends WriterBase implements IWriter {
|
||||||
|
|
||||||
public writeDouble(value:number) {
|
public writeDouble(value:number) {
|
||||||
if (this.resizable) {
|
if (this.resizable) {
|
||||||
const buffer = Buffer.alloc(8);
|
const buffer = getBufferClass().alloc(8);
|
||||||
buffer.writeDoubleLE(value);
|
buffer.writeDoubleLE(value);
|
||||||
this.writeBuffer(buffer);
|
this.writeBuffer(buffer);
|
||||||
} else {
|
} else {
|
||||||
|
@ -142,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;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue