This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: route tarballs directly through to the client
- Loading branch information
Benjamin Coe
committed
Jun 30, 2016
1 parent
c529426
commit 8eac6e1
Showing
7 changed files
with
167 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 not shown.