Skip to content

Commit 82ce02f

Browse files
committed
nginx: split lua out of config file
Much easier to work with single-language files.
1 parent 3baf57c commit 82ce02f

File tree

3 files changed

+62
-17
lines changed

3 files changed

+62
-17
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ MAINTAINER Ryan Graham <[email protected]>
33

44
RUN apk --no-cache add dnsmasq
55

6-
ADD entrypoint.sh nginx.conf /
6+
ADD entrypoint.sh nginx.conf ephemeral-npm.lua /
77

88
# Not port 80 because ephemeral-npm stnadardized on couchdb's port already
99
EXPOSE 4873

ephemeral-npm.lua

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
local cjson = require "cjson.safe"
2+
3+
local _M = {}
4+
5+
function _M.init()
6+
local npmConfig = ngx.shared.npmConfig
7+
local registry = os.getenv("npm_config_registry"):gsub("/+$", "")
8+
local pattern = registry:gsub("%.", "%%."):gsub("%-", "%%-")
9+
-- escape . and - which have special meaning in Lua patterns
10+
npmConfig:set('npm_config_registry', registry)
11+
npmConfig:set('npm_upstream_pattern', pattern)
12+
end
13+
14+
function _M.getPackage()
15+
local uri = ngx.var.uri
16+
local meta = ngx.shared.npmMeta
17+
local body = meta:get(uri)
18+
-- yep, our own shared memory cache implementation :-/
19+
if body == nil then
20+
local res = ngx.location.capture('/-@-' .. uri,
21+
{ copy_all_vars = true })
22+
body = res.body
23+
local pkgJSON = cjson.decode(body)
24+
if pkgJSON == nil then
25+
-- somehow the metadata isn't valid JSON.. let's tell
26+
-- the client to try again and hope it works out better
27+
-- next time
28+
return ngx.redirect(uri, ngx.HTTP_MOVED_TEMPORARILY)
29+
end
30+
meta:set(uri, body)
31+
end
32+
ngx.header["Content-Length"] = #body
33+
ngx.print(body)
34+
end
35+
36+
function _M.filterPackageBody()
37+
local npmConfig = ngx.shared.npmConfig
38+
local upstream = npmConfig:get('npm_upstream_pattern')
39+
-- need to construct URL because we may be proxying http<->https
40+
local base = ngx.var.scheme .. '://' .. ngx.var.http_host
41+
-- ngx.log(ngx.ERR, "Modifying JSON of " .. ngx.var.uri .. " to replace '" .. upstream .. "' with '" .. base .. "'")
42+
ngx.arg[1] = string.gsub(ngx.arg[1], upstream, base)
43+
end
44+
45+
return _M

nginx.conf

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ events {
88
worker_connections 1024;
99
}
1010

11-
1211
http {
1312
# We literally only care about 2 types of files/payloads.
1413
types {
@@ -27,15 +26,11 @@ http {
2726
access_log logs/access.log upstreamlog;
2827

2928
lua_shared_dict npmConfig 32k;
29+
lua_shared_dict npmMeta 128M;
30+
3031
init_by_lua_block {
31-
local npmConfig = ngx.shared.npmConfig
32-
local registry = os.getenv("npm_config_registry"):gsub("/+$", "")
33-
local pattern = registry:gsub("%.", "%%."):gsub("%-", "%%-")
34-
-- escape . and - which have special meaning in Lua patterns
35-
npmConfig:set('npm_config_registry', registry)
36-
npmConfig:set('npm_upstream_pattern', pattern)
37-
-- ngx.log(ngx.ERR, 'using upstream base: ', registry)
38-
-- ngx.log(ngx.ERR, 'using upstream pattern: ', pattern)
32+
local ephemeralNPM = require "ephemeral-npm"
33+
ephemeralNPM.init()
3934
}
4035

4136
server {
@@ -49,6 +44,14 @@ http {
4944
try_files $uri @fetch-tgz;
5045
}
5146
location / {
47+
content_by_lua_block {
48+
local ephemeralNPM = require "ephemeral-npm"
49+
ephemeralNPM.getPackage()
50+
}
51+
}
52+
location /-@- {
53+
internal;
54+
rewrite /-@-/(.+) /$1 break;
5255
resolver 127.0.0.1 ipv6=off;
5356
proxy_pass $npm_config_registry;
5457
proxy_buffers 32 1m;
@@ -65,20 +68,17 @@ http {
6568
header_filter_by_lua_block {
6669
ngx.header.content_length = nil
6770
}
68-
# replace all occurances of, eg. https://registry.npmjs.org with http://127.0.0.1:4873
6971
body_filter_by_lua_block {
70-
local npmConfig = ngx.shared.npmConfig
71-
local upstream = npmConfig:get('npm_upstream_pattern')
72-
-- need to construct URL because we may be proxying http<->https
73-
local base = ngx.var.scheme .. '://' .. ngx.var.http_host
74-
-- ngx.log(ngx.ERR, "Modifying JSON of " .. ngx.var.uri .. " to replace '" .. upstream .. "' with '" .. base .. "'")
75-
ngx.arg[1] = string.gsub(ngx.arg[1], upstream, base)
72+
local ephemeralNPM = require "ephemeral-npm"
73+
ephemeralNPM.filterPackageBody()
7674
}
7775
}
7876
location @fetch-tgz {
7977
internal;
8078
resolver 127.0.0.1 ipv6=off;
8179
proxy_pass $npm_config_registry;
80+
proxy_buffering on;
81+
proxy_buffers 512 512k;
8282
proxy_store on;
8383
proxy_store_access user:rw group:rw all:r;
8484
proxy_temp_path /tmp/npm/temp;

0 commit comments

Comments
 (0)