36 lines
544 B
JavaScript
36 lines
544 B
JavaScript
|
import schemapack from 'schemapack';
|
||
|
|
||
|
export class Packet {
|
||
|
|
||
|
constructor(data) {
|
||
|
this.data = data;
|
||
|
}
|
||
|
|
||
|
static get builder() {
|
||
|
if (!this._builder) {
|
||
|
this._builder = schemapack.build(this.schema);
|
||
|
}
|
||
|
return this._builder;
|
||
|
}
|
||
|
|
||
|
static pack(packet) {
|
||
|
return this.builder.encode({
|
||
|
_id: packet.data[0],
|
||
|
data: packet.data[1],
|
||
|
});
|
||
|
}
|
||
|
|
||
|
static get schema() {
|
||
|
return {
|
||
|
_id: 'uint8',
|
||
|
};
|
||
|
}
|
||
|
|
||
|
static unpack(packet) {
|
||
|
const {data} = this.builder.decode(packet);
|
||
|
return data;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|