Skip to content

Commit 992a041

Browse files
committed
nginx: consolidate per-request Lua usage
Instead of invoking Lua at multiple parts of the request lifecycle, consolidate to as few phases as possible. As a bonus, this also simplifies the code.
1 parent 230673a commit 992a041

File tree

2 files changed

+2
-17
lines changed

2 files changed

+2
-17
lines changed

ephemeral-npm.lua

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function _M.getPackage()
2222
local uri = ngx.var.uri
2323
local meta = ngx.shared.npmMeta
2424
local body = meta:get(uri)
25+
local base = ngx.var.scheme .. '://' .. ngx.var.http_host
2526
-- yep, our own shared memory cache implementation :-/
2627
if body == nil then
2728
ngx.var.ephemeralCacheStatus = 'MISS'
@@ -39,17 +40,9 @@ function _M.getPackage()
3940
else
4041
ngx.var.ephemeralCacheStatus = 'HIT'
4142
end
43+
body = string.gsub(body, _M.hostPattern, base)
4244
ngx.header["Content-Length"] = #body
4345
ngx.print(body)
4446
end
4547

46-
function _M.filterPackageBody()
47-
local npmConfig = ngx.shared.npmConfig
48-
local upstream = npmConfig:get('npm_upstream_pattern')
49-
-- need to construct URL because we may be proxying http<->https
50-
local base = ngx.var.scheme .. '://' .. ngx.var.http_host
51-
-- ngx.log(ngx.ERR, "Modifying JSON of " .. ngx.var.uri .. " to replace '" .. upstream .. "' with '" .. base .. "'")
52-
ngx.arg[1] = string.gsub(ngx.arg[1], upstream, base)
53-
end
54-
5548
return _M

nginx.conf

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,6 @@ http {
6363
proxy_buffers 32 1m;
6464
# need to disable encoding in order to be able to process it locally
6565
proxy_set_header Accept-Encoding "";
66-
# modifying the response body will change the length
67-
header_filter_by_lua_block {
68-
ngx.header.content_length = nil
69-
}
70-
body_filter_by_lua_block {
71-
local ephemeralNPM = require "ephemeral-npm"
72-
ephemeralNPM.filterPackageBody()
73-
}
7466
}
7567
location @fetch-tgz {
7668
internal;

0 commit comments

Comments
 (0)