diff --git a/bin/couch-url-rewrite-proxy.js b/bin/couch-url-rewrite-proxy.js new file mode 100755 index 0000000..302b792 --- /dev/null +++ b/bin/couch-url-rewrite-proxy.js @@ -0,0 +1,33 @@ +#!/usr/bin/env node +const proxy = require('../server') + +require('yargs') + .command( + 'start', + 'start the registry URL rewrite proxy', + function (yargs) { + return yargs + .option('port', { + alias: 'p', + default: 8080, + describe: 'what port should the proxy run on' + }) + .option('upstream', { + alias: 'u', + default: 'http://127.0.0.1:9999', + describe: 'what upstream should the proxy connect to' + }) + .option('front-door-host', { + alias: 'f', + default: process.env.FRONT_DOOR_HOST || 'https://registry.example.com', + describe: 'what external URL should tarball URLs be rewritten to' + }) + }, + function (argv) { + proxy(argv) + } + ) + .help() + .alias('help', 'h') + .demand(1, 'please provide a command to run') + .argv diff --git a/package.json b/package.json index 89700fb..d1cbf05 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "1.0.0", "description": "a proxy that rewrites URLs on their way out of couch, making registry migrations easier", "main": "server.js", + "bin": "bin/couch-url-rewrite-proxy.js", "scripts": { "coverage": "nyc report --reporter=text-lcov | coveralls", "pretest": "standard", @@ -37,6 +38,7 @@ "dependencies": { "@npm/npm-urls": "^1.0.1", "express": "^4.14.0", - "request": "^2.72.0" + "request": "^2.72.0", + "yargs": "^4.7.1" } } diff --git a/server.js b/server.js index f16a4d0..5b88114 100644 --- a/server.js +++ b/server.js @@ -9,9 +9,11 @@ function CouchUrlRewriteProxy (opts) { function proxy (req, res, next) { var payload = { method: req.method, - url: url.resolve(opts.frontDoorHost, req.path), + url: url.resolve(opts.upstream, req.path), headers: req.headers, - qs: req.query + qs: req.query, + gzip: true, + strictSSL: false } req.headers.host = 'registry.npmjs.org' @@ -58,7 +60,10 @@ function rewriteUrls (res, status, body, frontDoorHost) { } module.exports = function (opts, cb) { + cb = cb || function () {} CouchUrlRewriteProxy(opts) + console.info('routing', opts.port, 'to', opts.upstream) + console.info('rewriting to FRONT_DOOR_HOST =', opts.frontDoorHost) var server = app.listen(opts.port, function () { console.info('listening on ', opts.port) return cb(undefined, server) diff --git a/test/couch-url-rewrite-proxy.js b/test/couch-url-rewrite-proxy.js index dee861e..0a6ecd0 100644 --- a/test/couch-url-rewrite-proxy.js +++ b/test/couch-url-rewrite-proxy.js @@ -17,6 +17,7 @@ describe('couch-url-rewrite-proxy', function () { before(function (done) { proxy({ port: 9999, + upstream: 'http://www.example.com', frontDoorHost: 'http://www.example.com' }, function (err, _server) { if (err) return done(err)