diff --git a/Dockerfile b/Dockerfile index d8e0b61..2314450 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,8 @@ FROM openresty/openresty:alpine MAINTAINER Ryan Graham -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 / diff --git a/ephemeral-npm.lua b/ephemeral-npm.lua index 0e37207..ae803e9 100644 --- a/ephemeral-npm.lua +++ b/ephemeral-npm.lua @@ -1,4 +1,5 @@ local cjson = require "cjson.safe" +local http = require "resty.http" local utils = require "ephemeral-utils" local _M = {} @@ -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 @@ -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()