Skip to content

Commit c5a5a44

Browse files
committed
nginx: initial support for static preseeding
Read input from STDIN on startup. If it is a tgz file, then assume it is a node package as produced by `npm pack`. Read that tarball in and extract the metadata from the 'package/package.json' contained in it. Use this extracted metadata to fetch the upstream registry's metadata for the given package and then augment the version-specific metadata for version given in the tarball such that it looks like it was recently published. Use shasum to calculate the digest and then pre-seed the paths where nginx will look for the package.json and package-X.Y.Z.tgz so that it doesn't even bother looking upstream for them.
1 parent ef7c520 commit c5a5a44

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
FROM openresty/openresty:alpine
22
MAINTAINER Ryan Graham <[email protected]>
33

4-
RUN apk --no-cache add dnsmasq
4+
RUN apk --no-cache add dnsmasq jq perl curl
55

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

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

entrypoint.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
# [ ] NPM_USER
1111
# [ ] NPM_PASSWORD
1212

13+
source preseed.sh
14+
1315
export npm_config_registry=${npm_config_registry:-https://registry.npmjs.org}
1416
export MAXAGE=${MAXAGE:-5m}
1517

@@ -20,6 +22,9 @@ dnsmasq --listen-address=127.0.0.1 --user=root
2022
mkdir -p /tmp/npm/store
2123
mkdir -p /tmp/npm/temp
2224
mkdir -p /tmp/npm/cache
25+
26+
maybePreseed
27+
2328
# nginx runs as uid nobody and it needs write access
2429
chown -R nobody /tmp/npm
2530

preseed.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/sh
2+
3+
function preSeed() {
4+
mkdir /tmp/seed
5+
tgz=$1
6+
tar -xzf $1 -C /tmp/seed
7+
shasum=$(shasum $tgz | cut -d' ' -f1)
8+
seedJson=/tmp/seed/package/package.json
9+
upstreamJson=/tmp/seed/upstream.json
10+
pkgName=$(cat $seedJson | jq -r .name)
11+
pkgVersion=$(cat $seedJson | jq -r .version)
12+
mkdir -p /tmp/npm/store/$pkgName
13+
14+
cat <<-EOF
15+
Preseeding with:
16+
name: "$pkgName"
17+
version: "$pkgVersion"
18+
shasum: "$shasum"
19+
tag: "latest"
20+
EOF
21+
22+
cat > /tmp/seed/seed.jq <<-EOJQ
23+
.versions["$pkgVersion"] |= \$seedJson[0]
24+
| .versions["$pkgVersion"].dist.shasum = \$shasum
25+
| .versions["$pkgVersion"].dist.tarball = "$npm_config_registry/$pkgName/-/$pkgName-$pkgVersion.tgz"
26+
| .["dist-tags"].latest = "$pkgVersion"
27+
EOJQ
28+
29+
curl -sL $npm_config_registry/$pkgName > $upstreamJson
30+
jq --from-file /tmp/seed/seed.jq \
31+
--slurpfile seedJson $seedJson \
32+
--arg shasum $shasum \
33+
--arg pkgName $pkgName \
34+
--arg pkgVersion $pkgVersion \
35+
$upstreamJson \
36+
> /tmp/npm/store/$pkgName/package.json
37+
cp $tgz /tmp/npm/store/$pkgName/$pkgName-$pkgVersion.tgz
38+
}
39+
40+
function maybePreseed() {
41+
echo "checking if given a tarball..."
42+
cat - > /tmp/maybe.tgz
43+
echo "done reading input."
44+
if tar -tzf /tmp/maybe.tgz > /dev/null; then
45+
preSeed /tmp/maybe.tgz
46+
else
47+
echo 'no pre-seed package.'
48+
fi
49+
}

0 commit comments

Comments
 (0)