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

Commit

Permalink
feat: route tarballs directly through to the client
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Coe committed Jun 30, 2016
1 parent c529426 commit 8eac6e1
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 35 deletions.
11 changes: 0 additions & 11 deletions index.js

This file was deleted.

12 changes: 9 additions & 3 deletions package.json
Expand Up @@ -2,11 +2,11 @@
"name": "@npm/couch-url-rewrite-proxy",
"version": "1.0.0",
"description": "a proxy that rewrites URLs on their way out of couch, making registry migrations easier",
"main": "index.js",
"main": "server.js",
"scripts": {
"coverage": "nyc report --reporter=text-lcov | coveralls",
"pretest": "standard",
"test": "nyc mocha test.js",
"test": "nyc mocha test/*.js",
"release": "standard-version"
},
"repository": {
Expand All @@ -28,8 +28,14 @@
"chai": "^3.5.0",
"coveralls": "^2.11.9",
"mocha": "^2.5.3",
"nock": "^8.0.0",
"nyc": "^6.6.1",
"standard": "^7.1.2",
"standard-version": "^2.3.1"
"standard-version": "^2.3.1",
"tar.gz": "^1.0.5"
},
"dependencies": {
"express": "^4.14.0",
"request": "^2.72.0"
}
}
38 changes: 38 additions & 0 deletions server.js
@@ -0,0 +1,38 @@
var express = require('express')

var app = express()
var request = require('request')
var url = require('url')

function CouchUrlRewriteProxy (opts) {
function proxy (req, res, next) {
var payload = {
method: req.method,
url: url.resolve(opts.frontDoorHost, req.path),
headers: req.headers,
qs: req.query
}
req.headers.host = 'registry.npmjs.org'

if (~['PUT', 'POST', 'DELETE'].indexOf(req.method)) payload.body = req.body

request(payload, function (err, response, body) {
var status = 500
if (response && response.status) status = response.status
if (err) res.status(status).send(body)
})
.pipe(res)
}

['put', 'post', 'delete', 'get', 'head'].forEach(function (method) {
app[method](/.*/, proxy)
})
}

module.exports = function (opts, cb) {
CouchUrlRewriteProxy(opts)
var server = app.listen(opts.port, function () {
console.info('listening on ', opts.port)
return cb(undefined, server)
})
}
21 changes: 0 additions & 21 deletions test.js

This file was deleted.

56 changes: 56 additions & 0 deletions test/couch-url-rewrite-proxy.js
@@ -0,0 +1,56 @@
/* global describe, it, before, after */

var fs = require('fs')
var nock = require('nock')
var proxy = require('../server')
var request = require('request')
var targz = require('tar.gz')

require('chai').should()

console.info = function () {}

describe('couch-url-rewrite-proxy', function () {
var server = null

before(function (done) {
proxy({
port: 9999,
frontDoorHost: 'http://www.example.com'
}, function (err, _server) {
if (err) return done(err)
server = _server
return done()
})
})

it('serves a .tgz URL directly', function (done) {
var tarPath = '/tiny-tarball/-/tiny-tarball-1.0.0.tgz'
var entries = []
var expected = [
'package/package.json',
'package/README.md',
'package/index.js',
'package/test'
]
var tarball = nock('http://www.example.com')
.get(tarPath)
.reply(200, fs.readFileSync('./test/fixtures/tiny-tarball.tgz'))
var parse = targz().createParseStream()
var read = request.get('http://localhost:9999' + tarPath)

parse.on('entry', function (entry) {
entries.push(entry.path)
})
parse.on('end', function () {
entries.should.deep.equal(expected)
tarball.done()
return done()
})
read.pipe(parse)
})

after(function () {
server.close()
})
})
64 changes: 64 additions & 0 deletions test/fixtures/tiny-tarball.json
@@ -0,0 +1,64 @@
{
"_id": "tiny-tarball",
"_rev": "3-085759e977d42299e64a35aedc17d250",
"name": "tiny-tarball",
"description": "tiny tarball used for health checks",
"dist-tags": {
"latest": "1.0.0"
},
"versions": {
"1.0.0": {
"name": "tiny-tarball",
"version": "1.0.0",
"description": "tiny tarball used for health checks",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": {
"name": "Ben Coe",
"email": "ben@npmjs.com"
},
"license": "ISC",
"_id": "tiny-tarball@1.0.0",
"_shasum": "bbf102d5ae73afe2c553295e0fb02230216f65b1",
"_from": ".",
"_npmVersion": "2.7.0",
"_nodeVersion": "1.5.0",
"_npmUser": {
"name": "bcoe",
"email": "bencoe@gmail.com"
},
"maintainers": [
{
"name": "bcoe",
"email": "bencoe@gmail.com"
}
],
"dist": {
"shasum": "bbf102d5ae73afe2c553295e0fb02230216f65b1",
"tarball": "https://registry.npmjs.org/tiny-tarball/-/tiny-tarball-1.0.0.tgz"
},
"directories": {}
}
},
"readme": "# TinyTarball\n\ntiny-tarball used for health checks\n\n**don't unpublish me!**\n",
"maintainers": [
{
"name": "bcoe",
"email": "ben@npmjs.com"
}
],
"time": {
"modified": "2015-05-16T22:27:54.741Z",
"created": "2015-03-24T00:12:24.039Z",
"1.0.0": "2015-03-24T00:12:24.039Z"
},
"author": {
"name": "Ben Coe",
"email": "ben@npmjs.com"
},
"license": "ISC",
"readmeFilename": "README.md",
"_attachments": {}
}
Binary file added test/fixtures/tiny-tarball.tgz
Binary file not shown.

0 comments on commit 8eac6e1

Please sign in to comment.