ts-coder
v0.0.7
Published
simple ts file encoding/decoding library
Downloads
6
Readme
simple ts encoding/decoding library
▶ About
ts-coder
is a simple ts(transport streaming) file encoding and decoding library.
➕ Installation
ts-coder
can be installed using npm.
yarn add ts-coder # or npm install ts-coder
💡 How to use
data
▶ ts packets
import { Encoder } from 'ts-coder'
const encoder = new Encoder({
pid: 0x30, // packet identifier
})
const packets = encoder.encode(Buffer.from('hello world')) // encode "hello world" to ts packets
console.log(packets)
data
◀ ts packets
import { Encoder, Decoder } from 'ts-coder'
// Encode part.
const encoder = new Encoder({
pid: 0x30,
headSize: 4,
preMap(buffer, index, buffers) {
let status = 0x00
if (index === buffers.length - 1) {
status = 0x01
}
return Buffer.concat([Buffer.from([status, 0x00, 0x00, 0x00]), buffer])
},
})
const packets = encoder.encode(Buffer.from("hello world"))
// Decode part.
const decoder = new Decoder({
eadSize: 4,
isEnd(head) {
return head[0] === 0x01
},
})
decoder.onData((buffer) => {
console.log(buffer) // buffer with "hello world" string
})
for (const packet of packets) {
decoder.push(packet)
}