mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 19:49:50 +02:00
feat: write stream
This commit is contained in:
1 parent
93e405c370
commit
2951ccdad5
3 files changed
+76
-5
No files matched your search
Vendored
+1
@@ -127,6 +127,7 @@
|
|||||||
"webp",
|
"webp",
|
||||||
"winrate",
|
"winrate",
|
||||||
"wotblitz",
|
"wotblitz",
|
||||||
|
"xffn",
|
||||||
"xkdkw",
|
"xkdkw",
|
||||||
"XMLDVPL",
|
"XMLDVPL",
|
||||||
"YAMLDVPL",
|
"YAMLDVPL",
|
||||||
|
|||||||
+2
-5
@@ -1,6 +1,6 @@
|
|||||||
# Blitzkrieg Object Notation
|
# Blitzkrieg Object Notation
|
||||||
|
|
||||||
A compact binary representation of JSON objects. This documentation uses a frankenstein version of C++ structs that I came up with. Deal with it.
|
A compact binary representation similar to JSON objects. This documentation uses some funky (yet concise) syntax I developed (don't worry, my C++ isn't that bad!).
|
||||||
|
|
||||||
## Primitives
|
## Primitives
|
||||||
|
|
||||||
@@ -70,10 +70,7 @@ enum ValueType {
|
|||||||
Object,
|
Object,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Value {
|
struct Value = `Value${ValueType}`
|
||||||
uint8 type;
|
|
||||||
// what comes next depends on type
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ValueNull {
|
struct ValueNull {
|
||||||
ValueType.Null type;
|
ValueType.Null type;
|
||||||
|
|||||||
@@ -71,3 +71,76 @@ export class ReadStream {
|
|||||||
return this.buffer.readUInt8(this.increment(1)) === 1;
|
return this.buffer.readUInt8(this.increment(1)) === 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class WriteStream {
|
||||||
|
private array: number[] = [];
|
||||||
|
|
||||||
|
get buffer() {
|
||||||
|
return Buffer.from(this.array);
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(buffer?: Buffer) {
|
||||||
|
if (buffer) this.array = Array.from(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
byte(value: Uint8Array) {
|
||||||
|
this.array.push(...value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
ascii(value: string) {
|
||||||
|
for (let index = 0; index < value.length; index++) {
|
||||||
|
this.array.push(value.charCodeAt(index));
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private uintN(bytes: number, value: number) {
|
||||||
|
for (let index = 0; index < bytes; index++) {
|
||||||
|
this.array.push((value >>> (8 * index)) & 0xff);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
uint8(value: number) {
|
||||||
|
this.uintN(1, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
uint16(value: number) {
|
||||||
|
this.uintN(2, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
uint32(value: number) {
|
||||||
|
this.uintN(4, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
uint64(value: bigint) {
|
||||||
|
for (let index = 0n; index < 8n; index++) {
|
||||||
|
this.uint8(Number((value >> (8n * index)) & 0xffn));
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
private intN(bytes: number, value: number) {
|
||||||
|
for (let index = 0; index < bytes; index++) {
|
||||||
|
this.array.push((value >> (8 * index)) & 0xff);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
int8(value: number) {
|
||||||
|
this.intN(1, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
int16(value: number) {
|
||||||
|
this.intN(2, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
int32(value: number) {
|
||||||
|
this.intN(4, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
int64(value: bigint) {
|
||||||
|
for (let index = 0n; index < 8n; index++) {
|
||||||
|
this.int8(Number((value >> (8n * index)) & 0xffn));
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user