Skip to content
This repository has been archived by the owner on Jan 25, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Add configuration to opt out of tarball rewrites.
  • Loading branch information
totherik committed Oct 13, 2014
1 parent 183ebef commit 0819c3f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 25 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -46,6 +46,8 @@ kappa plugin currently supports the following parameters

- `vhost` - the virtual host associated with the kappa server, e.g. 'npm.mydomain.com'
- `paths` (optional) - any ordered array of npm repositories to use, e.g. Defaults to `['http://localhost:5984/registry/_design/app/_rewrite/', 'https://registry.npmjs.org/']`
- `rewriteTarballs` (optional) - When `true` rewrites the tarball URL in packages to download each resource via kappa. When `false`, tarball URLs
are left untouched, allowing the client to download package tarballs directly from the registry that fulfilled the package request. Defaults to `true`.

For read operations (GET, HEAD, etc) the proxy will first attempt to fetch the module from the first registry.
If the requested module is not found it continues to the next registry, and so on.
Expand Down
2 changes: 1 addition & 1 deletion config/defaults.json
Expand Up @@ -3,5 +3,5 @@
"http://localhost:5984/registry/_design/app/_rewrite/",
"https://registry.npmjs.org/"
],
"logLevel": "info"
"rewriteTarballs": true
}
30 changes: 16 additions & 14 deletions index.js
Expand Up @@ -154,21 +154,23 @@ exports.register = function register(plugin, options, next) {
});


// Rewrite tarball URLs to kappa so that everything comes through kappa.
// This is useful for metrics, logging, white listing, etc.
plugin.ext('onPostHandler', function (request, next) {
var response, rewrite, host, registry;

response = request.response;
if (!response.isBoom && response.variety === 'plain') {
host = util.hostInfo(request);
registry = Url.parse(response.headers['x-registry'] || '');
rewrite = util.rewriter(host, registry);
util.transform(response.source, 'tarball', rewrite);
}
if (settings.rewriteTarballs) {
// Rewrite tarball URLs to kappa so that everything comes through kappa.
// This is useful for metrics, logging, white listing, etc.
plugin.ext('onPostHandler', function (request, next) {
var response, rewrite, host, registry;

response = request.response;
if (!response.isBoom && response.variety === 'plain') {
host = util.hostInfo(request);
registry = Url.parse(response.headers['x-registry'] || '');
rewrite = util.rewriter(host, registry);
util.transform(response.source, 'tarball', rewrite);
}

next();
});
next();
});
}


plugin.ext('onPreResponse', function (request, next) {
Expand Down
74 changes: 64 additions & 10 deletions test/routes.js
Expand Up @@ -46,8 +46,7 @@ test('get', function (t) {
paths: spec.map(function (spec) {
return spec.registry;
}),
vhost: 'npm.mydomain.com',
logLevel: 'error'
vhost: 'npm.mydomain.com'
};

server = new Hapi.Server();
Expand Down Expand Up @@ -241,6 +240,65 @@ test('get', function (t) {
});


test('rewrites', function (t) {
var spec, server;

spec = require('./fixtures/get');
spec.forEach(mock.bind(null, 'get'));


t.on('end', function() {
nock.cleanAll();
});


t.test('server', function (t) {
var settings = {
paths: spec.map(function (spec) {
return spec.registry;
}),
vhost: 'npm.mydomain.com',
rewriteTarballs: false
};

server = new Hapi.Server();
server.pack.register({
plugin: kappa,
options: settings
}, function (err) {
t.error(err);
t.end();
});
});


t.test('disabled rewrites', function (t) {
var req = {
headers: {
host: 'npm.mydomain.com'
},
method: 'get',
url: '/cdb'
};

server.inject(req, function (res) {
var payload;

t.equal(typeof res, 'object');
t.ok(/^application\/json/.test(res.headers['content-type']));
t.strictEqual(res.headers['x-registry'], spec[0].registry);
t.strictEqual(res.statusCode, 200);

payload = JSON.parse(res.payload);
t.equal(typeof payload, 'object');
t.strictEqual(payload.versions['0.0.1'].dist.tarball, 'http://localhost:5984/file.tgz');

t.end();
});
});
});


test('head', function (t) {
var spec, server;

Expand All @@ -258,8 +316,7 @@ test('head', function (t) {
paths: spec.map(function (spec) {
return spec.registry;
}),
vhost: 'npm.mydomain.com',
logLevel: 'error'
vhost: 'npm.mydomain.com'
};


Expand Down Expand Up @@ -392,8 +449,7 @@ test('post', function (t) {
paths: spec.map(function (spec) {
return spec.registry;
}),
vhost: 'npm.mydomain.com',
logLevel: 'error'
vhost: 'npm.mydomain.com'
};

server = new Hapi.Server();
Expand Down Expand Up @@ -455,8 +511,7 @@ test('put', function (t) {
paths: spec.map(function (spec) {
return spec.registry;
}),
vhost: 'npm.mydomain.com',
logLevel: 'error'
vhost: 'npm.mydomain.com'
};

server = new Hapi.Server();
Expand Down Expand Up @@ -518,8 +573,7 @@ test('delete', function (t) {
paths: spec.map(function (spec) {
return spec.registry;
}),
vhost: 'npm.mydomain.com',
logLevel: 'error'
vhost: 'npm.mydomain.com'
};

server = new Hapi.Server();
Expand Down

0 comments on commit 0819c3f

Please sign in to comment.