You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
falk-werner_webfuse/example/provider/javascript/js/webfuse/messagereader.js

58 lines
1.2 KiB

class MessageReader {
constructor(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 }