npm-cache-proxy/readme.md
2019-04-22 18:56:32 +03:00

3.6 KiB

npm-cache-proxy

Docker Cloud Build Status

Quick start

Release binaries for different platforms can be downloaded on the Releases page. Also, Docker Image is provided.

docker run -e REDIS_ADDRESS=host.docker.internal:6379 -p 8080:8080 -it emeralt/npm-cache-proxy

Deployment

NCP can be deployed using Kubernetes, Docker Compose or any other container orchestration platform. NCP supports running indefinite amount of instances simultaneously.

Usage

ncp

Start proxy server.

Options Env Default Description
--listen <address> LISTEN_ADDRESS locahost:8080 Address to listen
--upstream <address> UPSTREAM_ADDRESS https://registry.npmjs.org Upstream registry address
--cache-limit <count> CACHE_LIMIT - Cached packages count limit
--cache-ttl <timeout> CACHE_TTL 3600 Cache expiration timeout in seconds
--redis-address <address> REDIS_ADDRESS http://localhost:6379 Redis address
--redis-database <database> REDIS_DATABASE 0 Redis database
--redis-password <password> REDIS_PASSWORD - Redis password
--redis-prefix <prefix> REDIS_PREFIX ncp- Redis keys prefix

ncp list

List cached packages.

ncp purge

Purge cached packages.

Programmatic usage

Along with the CLI, go package is provided. Documentation is available on godoc.org.

go get -u github.com/emeralt/npm-cache-proxy/proxy

Example

package main

import (
	"net/http"
	"time"

	npmproxy "github.com/emeralt/npm-cache-proxy/proxy"
	redis "github.com/go-redis/redis"
)

func main() {
	proxy := npmproxy.Proxy{
		Database: npmproxy.DatabaseRedis{
			Client: redis.NewClient(&redis.Options{
				Addr:     "localhost:6379",
			}),
		},
		HttpClient: &http.Client{},
		GetOptions: func() (npmproxy.Options, error) {
			return npmproxy.Options{
				DatabasePrefix:     "ncp-",
				DatabaseExpiration: 1 * time.Hour,
				UpstreamAddress:    "https://registry.npmjs.org",
			}, nil
		},
	}

	proxy.Server(npmproxy.ServerOptions{
		ListenAddress: "localhost:8080",
	}).ListenAndServe()
}

Benchmark

# GOMAXPROCS=1 GIN_MODE=release ncp --listen localhost:8080

$ go-wrk -c 100 -d 5 http://localhost:8080/ascii
Running 5s test @ http://localhost:8080/ascii
  100 goroutine(s) running concurrently
33321 requests in 5.00537759s, 212.69MB read
Requests/sec:		6657.04
Transfer/sec:		42.49MB
Avg Req Time:		15.02169ms
Fastest Request:	230.514µs
Slowest Request:	571.420487ms
Number of Errors:	0

License

MIT