We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see ourdocumentation.
一个缓冲可以一边写一边根据需要读取特定大小的数据
该库旨在解决js需要缓存内存块来解决接收不定长度的数据,取出不定长度的数据的问题。例如tcp的粘包问题。
接收一个远程请求,不断接收到不定长度的数据,然后根据协议解析到长度字段,然后读取一段长度来解包,此时可能缓存不够,需要等待继续接收数据,如果缓存够了,则读取长度后,需要修改缓存为剩余的数据
用核酸筛查来比喻,陆续有人来排队,需要排满10人为一组进行采样。
$ npm install oput
分成生产端和消费端,生产端负责向OPut中写入数据(TypedArray或者ArrayBuffer)。
importOPutfrom'oput'constoput=newOPut(reader)oput.write(newUint32Array([1,2,3]))
importOPutfrom'oput'constoput=newOPut(reader)readableStream.pipeTo(newWritableStream(oput))
消费端负责从OPut中读取数据。根据实际需要可以选择两种读取模式。
读取出来的是Uint8Array对象,要及时处理,后续读取出的数据会覆盖前面的数据。
function*reader(){letb=yield5;//读取5个字节console.log(b[0])}
可以自行选择是否复用TypedArray对象
function*reader(){letb=newUint8Array(5);yieldb;//填充到b中console.log(b[0])b=newUint32Array(5);yieldb;//填充到b中,又读取了20个字节console.log(b[0])}
constoput=newOPut();oput.write(newUint32Array([1,2]));oput.write(newUint32Array([1,2]));oput.read(1).then(value=>{expect(value[0]).toBe(1)returnoput.read(4)}).then(value=>{expect(value[3]).toBe(2)})