avocado-old/packages/net/packet/index.js

45 lines
717 B
JavaScript
Raw Normal View History

2019-04-25 00:09:03 -05:00
import schemapack from 'schemapack';
2019-04-11 15:26:13 -05:00
export class Packet {
constructor(data) {
this.data = data;
}
2019-04-25 00:09:03 -05:00
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],
})
}
2019-04-11 15:26:13 -05:00
static get schema() {
return {
_id: 'uint8',
};
}
2019-04-25 00:09:03 -05:00
static unpack(packet) {
const {data} = this.builder.decode(packet);
return data;
}
2019-04-11 15:26:13 -05:00
}
export {
allPackets,
idFromPacket,
packetFromId,
registerPacket,
} from './registry';
import * as SocketIoParser from './socket.io-parser';
export {SocketIoParser};