npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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)