Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pre-cache related packages when caching #4

Open
wants to merge 1 commit into
base: nginx
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
FROM openresty/openresty:alpine
MAINTAINER Ryan Graham <[email protected]>

RUN apk --no-cache add dnsmasq jq perl curl
RUN apk --no-cache add dnsmasq jq perl curl \
&& opm get pintsized/lua-resty-http

ADD entrypoint.sh nginx.conf ephemeral-npm.lua ephemeral-utils.lua preseed.sh /

Expand Down
46 changes: 45 additions & 1 deletion ephemeral-npm.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local cjson = require "cjson.safe"
local http = require "resty.http"
local utils = require "ephemeral-utils"

local _M = {}
Expand All @@ -14,6 +15,45 @@ function _M.init()
npmConfig:set('MAXAGE', _M.MAXAGE)
end

function _M.prefetchRelatedPackages(premature, selfHost, pkg)
local httpc = http.new()
local meta = ngx.shared.npmMeta
httpc:connect('127.0.0.1', 4873)
local distTags = pkg['dist-tags'] or {}
local versions = pkg.versions or {}
local latestVersion = distTags.latest
local latest = versions[latestVersion] or {}
local deps = latest.dependencies or {}
local reqs = {}
-- find any deps that we haven't already seen and queue them for fetching
for k, v in pairs(deps) do
if meta:get('/' .. k) == nil then
table.insert(reqs, {
path = '/' .. k,
method = 'GET',
headers = {
["Host"] = selfHost,
},
})
end
end
-- extract all the tarball URLs and fetch them to force them to be cached
for v,p in pairs(versions) do
local scheme, host, port, path, query = unpack(httpc:parse_uri(p.dist.tarball))
table.insert(reqs, {
path = path,
method = 'GET',
})
end
local responses, err = httpc:request_pipeline(reqs)
for i,r in ipairs(responses) do
if r.status then
r:read_body() -- to oblivion!
end
end
httpc:close()
end

function _M.getPackage()
local uri = ngx.var.uri
local meta = ngx.shared.npmMeta
Expand All @@ -34,10 +74,14 @@ function _M.getPackage()
return ngx.redirect(uri, ngx.HTTP_MOVED_TEMPORARILY)
end
meta:set(uri, body, _M.MAXAGE)
-- We rewrite the URLs AFTER caching so that we can be accessed by
-- any hostname that is pointed at us.
body = string.gsub(body, _M.hostPattern, base)
ngx.timer.at(0.1, _M.prefetchRelatedPackages, ngx.var.http_host, pkgJSON)
else
body = string.gsub(body, _M.hostPattern, base)
ngx.var.ephemeralCacheStatus = 'HIT'
end
body = string.gsub(body, _M.hostPattern, base)
ngx.header["Content-Length"] = #body
ngx.print(body)
ngx.eof()
Expand Down