Simple but versatile class to make reading, writing and parsing binary data easy
version 1.0.0 in progress
Example
conststream=newByteStream();(asyncfunctionwriter(){// can write to byte streamconstwritestream=newByteStream();writestream.writeInt(-512,'int16','BE');writestream.writeInt(262,'int16','BE');writestream.writeUtf8(String.fromCodePoint(0x1f303));returnwritestream.bytes();})().then(function(data){functionreader(){if(reader.pos>=data.length)returnnull;constpos=reader.pos,offset=2;reader.pos+=offset;returndata.slice(pos,pos+offset);// simulate data coming in progressively}reader.pos=0;setTimeout(functionread(){constchunk=reader();if(!chunk){// if no more data, signal to the stream that we finishedstream.finish();}else{// as long as reader has new data, add them to the streamstream.add(chunk);setTimeout(read,100);}},100);});(asyncfunctiontest(){// read from byte stream asynchronously, while new data arriveconsole.log(awaitstream.readInt('int16','BE'),-512);console.log(awaitstream.readInt('int16','BE'),262);console.log(awaitstream.readUtf8(1),String.fromCodePoint(0x1f303));})().then(()=>{console.log('--EOF--')});