The Redis RESP protocol consists of just five types, therefore in your filters, the context.packet object will always be one of these five types:
simple strings: basic strings used for small amounts of text
bulk string: arbitrarily long strings (up to 512MB)
error: used to send errors back to the client
integer: used to communicate numbers
array: contains any number of any of the five types, including array
However, for requests, the type is always Array. A Redis command such as:
will be received by request filters as an array of BulkStrings:
All types have a packetType property:
All types implement the following methods:
getPacketType(): returns a string, one of: "SimpleString:, "BulkString", "Integer", "Array" or "Error"
isSimpleString(): returns true if the object is a SimpleString
isBulkString(): returns true if the object is a BulkString
isInteger(): returns true if the object is an Integer
isArray(): returns true if the object is an Array
isError(): returns true if the object is an Error object (used only in responses)
deepCopy(): returns a deep copy of the entire object (useful for Arrays)
canBeMap(): checks whether the object is an Array with an even number of values, and whether all values in even slots (starting at 0) are strings. If this returns true, then the object can be converted to an object by calling toMap(), making it easier to manipulate in code.
A SimpleString object is usually just a string. Its content can be obtained and changed using the string property:
The value cannot contain <CR> <LF> -- use a BulkString if you need that.
The value of strings can be binary -- either encoded strings (e.g. UTF-8) or actual binary data. By default, UTF-8 encoding is assumed. For strings with other encodings, you can use the getString and setString methods:
Note that, if the string uses an encoding other than UTF-8, you should not use the string property, since it assumes UTF-8.
The binary value can be accessed using the bytes property:
An Error object is sent back by the server whenever it needs to indicate that something went wrong.
The Error type is otherwise exactly like the SimpleString type.
The format, by convention, is normally XXX Message where XXX is the type of the error, e.g. ERR or WRONGTYPE, and Message is a string describing the error. Like SimpleString, Error cannot contain <CR> <LF>.
A BulkString object is a string of arbitrary length (up to 512MB), and is most commonly used when returning data.
BulkString objects have an extra property called isNull, indicating a special value indicating the non-existence of a value.
A BulkString is otherwise like a SimpleString, except that its content is unrestricted.
An Integer object is just an integer number. Its value goes from -2^63-1 to 2^63.
The value can be retrieved and modified using the int property:
An Array is an ordered list of objects of any of the 5 supported types. It is therefore possible (and common) to have arrays of arrays, and of other types.
Arrays also have a remove(int) method that removes the object at the specified index.