Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

create a bin for executing the rewrite proxy #4

Merged
merged 5 commits into from
Jul 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

<a name="1.1.1"></a>
## [1.1.1](https://github.com/npm/couch-url-rewrite-proxy/compare/v1.1.0...v1.1.1) (2016-07-07)


### Bug Fixes

* also add req param parsing ([ee8ae12](https://github.com/npm/couch-url-rewrite-proxy/commit/ee8ae12))
* need body-parser dependency TODO: add test for post ([fd0d821](https://github.com/npm/couch-url-rewrite-proxy/commit/fd0d821))



<a name="1.1.0"></a>
# [1.1.0](https://github.com/npm/couch-url-rewrite-proxy/compare/v1.0.0...v1.1.0) (2016-07-07)


### Features

* add bin for running proxy ([5aa60ca](https://github.com/npm/couch-url-rewrite-proxy/commit/5aa60ca))
* proxy now rewrites package JSON as it is served ([#3](https://github.com/npm/couch-url-rewrite-proxy/issues/3)) ([5870a75](https://github.com/npm/couch-url-rewrite-proxy/commit/5870a75))
* route tarballs directly through to the client ([#2](https://github.com/npm/couch-url-rewrite-proxy/issues/2)) ([a071e6c](https://github.com/npm/couch-url-rewrite-proxy/commit/a071e6c))



<a name="1.0.0"></a>
# 1.0.0 (2016-06-23)

Expand Down
33 changes: 33 additions & 0 deletions bin/couch-url-rewrite-proxy.js
Original file line number Diff line number Diff line change
@@ -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
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "@npm/couch-url-rewrite-proxy",
"version": "1.0.0",
"version": "1.1.1",
"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",
Expand Down Expand Up @@ -36,7 +37,9 @@
},
"dependencies": {
"@npm/npm-urls": "^1.0.1",
"body-parser": "^1.15.2",
"express": "^4.14.0",
"request": "^2.72.0"
"request": "^2.72.0",
"yargs": "^4.7.1"
}
}
19 changes: 13 additions & 6 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
var express = require('express')

var app = express()
var bodyParser = require('body-parser')
var request = require('request')
var npmUrls = require('@npm/npm-urls')
var url = require('url')

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))

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,
json: true,
strictSSL: false
}
req.headers.host = 'registry.npmjs.org'

Expand Down Expand Up @@ -48,17 +54,18 @@ function CouchUrlRewriteProxy (opts) {

function rewriteUrls (res, status, body, frontDoorHost) {
try {
var json = JSON.parse(body)
npmUrls.rewriteOldTarballUrls(frontDoorHost, json)
res.status(status).send(JSON.stringify(json))
npmUrls.rewriteOldTarballUrls(frontDoorHost, body)
} catch (err) {
console.error(err.message)
res.status(status).send(body)
}
res.status(status).send(body)
}

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)
Expand Down
1 change: 1 addition & 0 deletions test/couch-url-rewrite-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down