1
0
mirror of https://github.com/falk-werner/webfuse synced 2026-10-18 09:23:54 +00:00

add basic infrastructure of javascript example

This commit is contained in:
Falk Werner
2023-02-05 00:11:02 +01:00
parent 7783b294cc
commit 17d6275d96
10 changed files with 669 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
class MessageReader {
constructor(data) {
// console.log(new Uint8Array(data));
this.raw = data;
this.data = new DataView(data);
this.pos = 0;
this.decoder = new TextDecoder('utf-8');
}
read_u8() {
const result = this.data.getUint8(this.pos);
this.pos++;
return result;
}
read_bool() {
return this.read_u8() == 1;
}
read_u32() {
const result = this.data.getUint32(this.pos);
this.pos += 4;
return result;
}
read_u64() {
const result = this.data.getBigUint64(this.pos);
this.pos += 8;
return Number(result);
}
read_str() {
const length = this.read_u32();
if (length > 0) {
const view = new Uint8Array(this.raw, this.pos, length);
this.pos += length;
return this.decoder.decode(view);
}
else {
return "";
}
}
read_bytes() {
const length = this.read_u32();
if (length > 0) {
const view = new Uint8Array(this.raw, this.pos, length);
this.pos += length;
return view;
}
else {
return [];
}
}
}
export { MessageReader }