Skip to content
forked from mah0x211/lua-resp

RESP (REdis Serialization Protocol) parser for lua

Notifications You must be signed in to change notification settings

lalawue/lua-resp

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lua-resp

RESP (REdis Serialization Protocol) parser module.

NOTE: this module is under heavy development.

install from LuaRocks

$ luarocks install lua-resp

resp module

local resp = require('resp')

Status Constants

  • resp.EAGAIN: Not enough data available.
  • resp.EILSEQ: Found illegal byte sequence.

Message Type Constants

  • resp.ARR: arrays.
  • resp.BLK: bulkstrings.
  • resp.ERR: errors.
  • resp.INT: integers.
  • resp.STR: simplestrings.

Functions

msg, err = resp.encode( ... )

encode messages.

Parameters

  • ...: messages of the following data types;
    • nil
    • string
    • number
    • boolean
    • table (non-sparse array only)

Returns

  • msg:string: serialized message.
  • err:string: error message.

msg, err = resp.encode2array( ... )

encode messages as array.
usually, this api use to encode a command message.

Parameters and Returns

same as resp.encode API

consumed, msg, typ = resp.decode( str [, head] )

decode serialized message strings.

Parameters

  • str: serialized message string.
  • head: decode start position. (default 0)

Returns


Example

local inspect = require'util'.inspect
local resp = require("resp")

local msg = resp.encode( 'HMSET', 'myhash', 'hello', '"world"' )
-- encoded to following string;
--  *4\r\n$5\r\nHMSET\r\n$6\r\nmyhash\r\n$5\r\nhello\r\n$7\r\n"world"\r\n


local consumed, data, typ = resp.decode( msg )
-- consumed equal to 51
-- typ equal to resp.ARR
-- decoded to following table;
--  { [1] = "HMSET",
--    [2] = "myhash",
--    [3] = "hello",
--    [4] = "\"world\"",
--    len = 4 }


-- decode multiple-message
local mmsg = table.concat({msg, msg, msg})

consumed, data = resp.decode( mmsg )
while consumed > 0 do
    mmsg = string.sub( mmsg, consumed + 1 )
    consumed, data = resp.decode( mmsg )
end


-- use with head optional argument
mmsg = table.concat({msg, msg, msg})

consumed, data = resp.decode( mmsg )
while consumed > 0 do
    consumed, data = resp.decode( mmsg, consumed )
end

About

RESP (REdis Serialization Protocol) parser for lua

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C 97.8%
  • Lua 2.2%