Skip to content

Instantly share code, notes, and snippets.

@weswigham
Last active December 24, 2015 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weswigham/6799855 to your computer and use it in GitHub Desktop.
Save weswigham/6799855 to your computer and use it in GitHub Desktop.
Pull Lua dependencies straight from the web! Now adds a searcher to require, instead of other things!
local socket = require("socket")
local http = require("socket.http")
--Safety is overrated. Pull scripts form the web!
local function webclude(target, ...)
local body, status, res_head, res_stat = http.request(target, ...)
if body then
return body
else
local str = "Could not include from web file: "..target.."\n"
.."Status: "..status.."\n"
.."Response Headers: "..tostring(res_head).."\n"
.."Response Status: "..res_stat.."\n"
return nil, str
end
end
local function webclude_req(modulename)
-- Find source
local src, err = webclude(modulename)
if not src then
-- Return a partial message that require will aggregate
return "\n\tNo module '"..modulename.."' did not resolve; " ..err
else
-- Compile and return the module
local load = load
if loadstring then --5.1
load = loadstring
end
return assert(load(src))
end
end
table.insert(package.loaders, webclude_req)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment