1.1.3 • Published 2 years ago
dataproto v1.1.3
DataReader, DataWriter
Read from and write to a data buffer, with static typing
import { DataReader, DataWriter } from 'dataproto'
const writer = new DataWriter()
writer.int(123)
writer.string('Hello world!')
const buffer = writer.build() // Uint8Array(17) [0, 0, 0, 123, 12, ...]
const reader = new DataReader(buffer)
console.assert(reader.int() == 123)
console.assert(reader.string() == 'Hello World!')Schemas
This is meant for situations where the type to be written cannot be known ahead of time. For performance reasons, don't use this unless you have to.
import { DataReader, DataWriter } from 'dataproto'
const Schema = {a: String, b: [Int, 3]}
const writer = new DataWriter()
writer.write(Schema, {a: 'Hello', b: [1, 2, 3]})
// Save to a file
fs.writeFileSync('./someObject.dat', writer.build())
// Read file again
const reader = new DataReader(fs.readFileSync('./someObject.dat'))
console.log(reader.read(Schema)) // {a: 'Hello', b: [1, 2, 3]}