UNPKG

shardy

Version:

Framework for online games and applications on Node.js

40 lines 1.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Block = exports.BlockType = exports.BLOCK_HEAD = void 0; exports.BLOCK_HEAD = 4; var BlockType; (function (BlockType) { BlockType[BlockType["Handshake"] = 0] = "Handshake"; BlockType[BlockType["HandshakeAcknowledgement"] = 1] = "HandshakeAcknowledgement"; BlockType[BlockType["Heartbeat"] = 2] = "Heartbeat"; BlockType[BlockType["Data"] = 3] = "Data"; BlockType[BlockType["Kick"] = 4] = "Kick"; })(BlockType || (exports.BlockType = BlockType = {})); class Block { static encode(type, body) { const data = Buffer.from(body); const length = body ? body.length : 0; const buffer = Buffer.alloc(exports.BLOCK_HEAD + length); let index = 0; buffer[index++] = type & 0xff; buffer[index++] = (length >> 16) & 0xff; buffer[index++] = (length >> 8) & 0xff; buffer[index++] = length & 0xff; data.copy(buffer, index, 0, length); return buffer; } static decode(data) { const bytes = Buffer.from(data); const type = bytes[0]; let index = 1; const length = ((bytes[index++] << 16) | (bytes[index++] << 8) | bytes[index++]) >>> 0; const body = length ? Buffer.alloc(length) : Buffer.alloc(0); bytes.copy(body, 0, exports.BLOCK_HEAD, exports.BLOCK_HEAD + length); return { type, body }; } static check(type) { return Object.values(BlockType).includes(type); } } exports.Block = Block; //# sourceMappingURL=Block.js.map