luvit-zed
v0.0.8-h
Published
Util-library for Luvit.io
Downloads
28
Readme
luvit-zed
Zed is a library for all your luvit needs and is aimed to simplify many tasks including:
- clienthttp.lua: Simplified http client
- fs.lua: File system tasks (for example deleting a directory with content)
- httperror.lua: Http error codes
- json.lua: Less buggy JSON library ( use ZD.json:encode(), ZD.json:decode() )
- math.lua: Math compatibility functions
- simplehttp.lua: Simplified http server
- string.lua: Extended String formatting
- table.lua: Extended table functionality
- utils.lua: Debug Table; URL parsing; Extended console output; Hashing tables
- zedsocket.lua: Event-based Server-Browser communication in realtime using WebSocket RFC 6455 or AJAX.
How to install
luvit-zed is available using npm. to install zed just use
npm install luvit-zed
Initialization
luvit-zed has 3 initialization methods. The first one just returns the default zed as a local variable:
local ZD = require('luvit-zed')()
The second one does the same, but also extends the given tables with more functionality:
local string = require('string')
local math = require('math')
local fs = require('fs')
local table = require('table')
local ZD = require('luvit-zed')({
string = string,
math = math,
fs = fs,
table = table
})
-- the given tables now have more functions than before, see below for more information.
And the third one does the same as the other two, but puts every given table, including Zed, into the global variable.
do
local string = require('string')
require('luvit-zed')({global = {string = string}})
end
string.split("This code will work, even though string is not defined in this scope", " ")
Examples/API:
clienthttp.lua:
local ZD = require('luvit-zed')()
ZD.requestGET("example.com", 80, "/", function(html) -- Gets html code from the given website
print(html)
end)
fs.lua:
local fs = require('fs')
local ZD = require('luvit-zed')({fs = fs})
fs.rmsubdir("./delete/this/folder/with/content", function() -- Deletes the given folder including it's content
print("Done")
end)
httperror.lua:
local ZD = require('luvit-zed')()
local text = ZD.httpError(404) -- returns "Not found"
local html = ZD.htmlError(404) -- returns the 404 page of ZED
markdown.lua:
local ZD = require('luvit-zed')()
local html = ZD.markdown([[
Test
------
]])
math.lua:
local math = require('math')
local ZD = require('luvit-zed')({math = math})
local v = math.mod(7, 2) -- equals 7 % 2
md5.lua:
local ZD = require('luvit-zed')()
ZD.md5.sumhexa("Test") -- returns the md5 hash of "Test"
-- not made by me, check the source file for credits
simplehttp.lua:
local fs = require('fs')
local ZD = require('luvit-zed')()
local sH = ZD.simpleHTTP(80, function(out) -- out(data, [type, [code]]), eg: out("Test", "text/plain")
out(nil, nil, 404) -- default request handler, gets executed when no datatype has been found
end)
sH:addDataType("html", "text/html") -- datatypes to handle like normal fileservers
sH:addDataType("jpg", "image/jpeg")
sH:addDataType("lua", function(url, req, cb) -- script interpreter for lua files
if fs.existsSync("." .. url.path) then
local script = require("." .. url.path) or {} -- loads lua file serverside on request
if script.onRequest then
script.onRequest(url, cb) -- calls the onRequest function of that script
end
else
cb(nil, nil, 404) -- return 404 error
end
end)
string.lua:
local string = require('string')
local ZD = require('luvit-zed')({string = string})
local tbl = string.split("Hello/World", "/") -- simply splits the first string by the second string and returns it as an indexed table
table.lua:
local table = require('table')
local ZD = require('luvit-zed')({table = table})
local k = table.find(t, v) -- returns the key to the given value
local t = table.reverse(t2) -- reverses indexed table
local t = table.invert(t2) -- inverts table ( t2[v] = k )
utils.lua:
local ZD = require('luvit-zed')()
ZD.debugTable(tbl, 0, print) -- iterates through and prints out the give table in a readable form
local hash = ZD.hashTable(t) -- hashes the given table using md5
local url = ZD.parseURL("/index.lua?id=1234") -- parses the given url using the following syntax:
-- {path = "/index.lua", ending = "lua", get={id="1234"}}
-- allows easy access to get variables, for example: print(url.get.id)
zedsocket.lua:
local ZD = require("luvit-zed")()
local simpleHTTP = ZD.simpleHTTP(80, function(req, out) -- creates http server to return the clientside code
out([[
<script type="text/javascript" src='/node_modules/luvit-zed/js/zedsocket.js'></script> // run the zedsocket.js
<script>
var socket = new ZedSocket("localhost", 1734, false); // create new zedsocket, third argument equals the second argument from the server
socket.on("Foo", function(data){ // add listener for "Foo"
console.log(data.msg); // print out the received msg
});
socket.onConnection(function(){
socket.send("Foo", {msg: "Bar"}); // send "Foo" packet with the msg "Bar"
});
</script>
]], "text/html")
end)
simpleHTTP:addDataType("js", "text/javascript") -- add DataType for js, so the clientside code can be retrieved
local socket = ZD.ZedSocket(1734, false) -- AJAX Server will be running on this port, Websocket Server on this port + 1, setting the second arguments to true will only use Websocket on the give port
-- Be sure that in this case both ports must be open, 1734 and 1735, as WebSocket will be using the given port + 1
socket:onConnection(function(client) -- listen for connections
print("Client connected.")
client:on("Foo", function(data) -- listen for "Foo" packets
print(data.msg) -- print out the received msg
client:send("Foo", data) -- send the packet pack using the same name
end)
client:on("disconnect", function(data) -- listen for disconnections
print("Client disconnected.")
end)
end)