byte-rw
Reading and writing at the byte level is easy with byte-rw
. It exposes two interfaces like the DataView object but is adaptable for streams or other implementations.
const littleEndian = true
const writer_inMemory = new DataViewByteWriterChunkedDynamic(littleEndian)
writeData(writer_inMemory)
const data = writer_inMemory.combineChunks() // ArrayBuffer
console.log(data)
const stream: WritableStream = ...
const writer_stream = new StreamByteWriter(stream)
writeData(writer_stream)
function writeData(writer: ByteWriter) {
const endianness = writer.littleEndian
writer.writeUint16(0xABCD)
writer.writeUint16(0x1234)
writer.littleEndian = false
writer.writeUint32(0xCC0011FF)
writer.littleEndian = endianness
}
See also the ByteBuffer for another buffer interface. It must have well more performance and is more mature though appears limited to working with in-memory buffers; this package may have more flexibility and can work with streams.
Appreciation
Mocha + typescript + ES modules: Henry Ruhs
Function annotations largely adapted from type declarations in lib.es5.d.ts.