UNPKG

diffusion

Version:

Diffusion JavaScript client

51 lines (47 loc) 1.44 kB
/*eslint valid-jsdoc: "off"*/ var MESSAGE_TYPE_MASK = 0xA7D8C0; var ZLIB_TYPE_FLAGS = 0x80; /** * The "message type flags" allow a subset of MessageEncodings to be encoded * in the upper two bits of the message type byte. Only NO_ENCODING and * ZLIB are supported. * @param {Number} - encodingByte. * @param {Number} - messageTypeFlags. * @return {MessageEncoding} - The MessageEncoding. */ function MessageEncoding(encodingByte, messageTypeFlags) { return { encodingByte : encodingByte, messageTypeFlags : messageTypeFlags }; } /** * Decode the message type from a byte combining the message type and flags. * @return {Number} The message type. */ function extractMessageType(typeAndEncoding) { return typeAndEncoding & ~MESSAGE_TYPE_MASK; } /** * Decode the message encoding from a byte combining the message type and * flags. * @return {MessageEncoding} either ZLIB or NO_ENCODING. */ function extractMessageEncoding(typeAndEncoding) { var encoding = typeAndEncoding & MESSAGE_TYPE_MASK; if (encoding === ZLIB_TYPE_FLAGS) { return EncodingType.ZLIB; } else { return EncodingType.NO_ENCODING; } } var EncodingType = { NO_ENCODING : MessageEncoding(0x00, 0x00), ZLIB : MessageEncoding(0x12, 0x01) }; module.exports = { extractMessageEncoding : extractMessageEncoding, extractMessageType : extractMessageType, EncodingType : EncodingType };