This repository has been archived by the owner on Jun 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 744
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: proxy public package from source registry (#1375)
when syncMode = 'none' closes #1374
- Loading branch information
Showing
46 changed files
with
353 additions
and
642 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
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
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 |
---|---|---|
@@ -1,50 +1,84 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var debug = require('debug')('cnpmjs.org:middleware:proxy_to_npm'); | ||
var config = require('../config'); | ||
|
||
module.exports = function (options) { | ||
var redirectUrl = config.sourceNpmRegistry; | ||
var proxyUrls = [ | ||
// /:pkg, dont contains scoped package | ||
/^\/[\w\-\.]+$/, | ||
// /:pkg/:versionOrTag | ||
/^\/[\w\-\.]+(?:\/[\w\-\.]+)?$/, | ||
// /-/package/:pkg/dist-tags | ||
/^\/\-\/package\/[\w\-\.]+\/dist-tags/, | ||
]; | ||
var scopedUrls = [ | ||
// scoped package | ||
/^\/(@[\w\-\.]+)\/[\w\-\.]+(?:\/[\w\-\.]+)?$/, | ||
/^\/\-\/package\/(@[\w\-\.]+)\/[\w\-\.]+\/dist\-tags/, | ||
]; | ||
if (options && options.isWeb) { | ||
redirectUrl = redirectUrl.replace('//registry.', '//'); | ||
redirectUrl = config.sourceNpmWeb || redirectUrl.replace('//registry.', '//'); | ||
proxyUrls = [ | ||
// /package/:pkg | ||
/^\/package\/[\w\-\.]+$/, | ||
/^\/package\/[\w\-\.]+/, | ||
]; | ||
scopedUrls = [ | ||
// scoped package | ||
/^\/package\/(@[\w\-\.]+)\/[\w\-\.]+/, | ||
]; | ||
} | ||
|
||
return function* proxyToNpm(next) { | ||
if (config.syncModel !== 'none') { | ||
return yield next; | ||
} | ||
|
||
// syncModel === none | ||
// only proxy read requests | ||
if (this.method !== 'GET' && this.method !== 'HEAD') { | ||
return yield next; | ||
} | ||
|
||
var pathname = this.path; | ||
var match; | ||
for (var i = 0; i < proxyUrls.length; i++) { | ||
match = proxyUrls[i].test(pathname); | ||
if (match) { | ||
break; | ||
|
||
var isScoped = false; | ||
var isPublichScoped = false; | ||
// check scoped name | ||
if (config.scopes && config.scopes.length > 0) { | ||
for (var i = 0; i < scopedUrls.length; i++) { | ||
const m = scopedUrls[i].exec(pathname); | ||
if (m) { | ||
isScoped = true; | ||
if (config.scopes.indexOf(m[1]) !== -1) { | ||
// internal scoped | ||
isPublichScoped = false; | ||
} else { | ||
isPublichScoped = true; | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
if (!match) { | ||
return yield next; | ||
|
||
var isPublich = false; | ||
if (!isScoped) { | ||
for (var i = 0; i < proxyUrls.length; i++) { | ||
isPublich = proxyUrls[i].test(pathname); | ||
if (isPublich) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (isPublich || isPublichScoped) { | ||
var url = redirectUrl + this.url; | ||
debug('proxy isPublich: %s, isPublichScoped: %s, package to %s', | ||
isPublich, isPublichScoped, url); | ||
this.redirect(url); | ||
return; | ||
} | ||
|
||
var url = redirectUrl + this.url; | ||
debug('proxy to %s', url); | ||
this.redirect(url); | ||
yield next; | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1,39 +1,23 @@ | ||
/**! | ||
* cnpmjs.org - test/controllers/registry/module/maintainer.test.js | ||
* | ||
* Copyright(c) fengmk2 and other contributors. | ||
* MIT Licensed | ||
* | ||
* Authors: | ||
* fengmk2 <[email protected]> (http://fengmk2.github.com) | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var request = require('supertest'); | ||
var mm = require('mm'); | ||
var config = require('../../../../config'); | ||
var app = require('../../../../servers/registry'); | ||
var utils = require('../../../utils'); | ||
|
||
describe('controllers/registry/module/maintainer.test.js', function () { | ||
describe('test/controllers/registry/module/maintainer.test.js', function () { | ||
var pkgname = '@cnpm/test-package-maintainer'; | ||
var pkgURL = '/@' + encodeURIComponent(pkgname.substring(1)); | ||
before(function (done) { | ||
app = app.listen(0, function () { | ||
// add scope package | ||
var pkg = utils.getPackage(pkgname, '0.0.1', utils.admin); | ||
// add scope package | ||
var pkg = utils.getPackage(pkgname, '0.0.1', utils.admin); | ||
|
||
request(app) | ||
.put(pkgURL) | ||
.set('authorization', utils.adminAuth) | ||
.send(pkg) | ||
.expect(201, done); | ||
}); | ||
request(app) | ||
.put(pkgURL) | ||
.set('authorization', utils.adminAuth) | ||
.send(pkg) | ||
.expect(201, done); | ||
}); | ||
|
||
beforeEach(function () { | ||
|
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 |
---|---|---|
|
@@ -8,27 +8,27 @@ var pedding = require('pedding'); | |
var app = require('../../../../servers/registry'); | ||
var utils = require('../../../utils'); | ||
|
||
describe('controllers/registry/package/deprecate.test.js', function () { | ||
describe('test/controllers/registry/package/deprecate.test.js', function () { | ||
var pkgname = '@cnpmtest/testmodule-deprecate'; | ||
before(function (done) { | ||
var pkg = utils.getPackage(pkgname, '0.0.1', utils.admin); | ||
|
||
request(app.listen()) | ||
request(app) | ||
.put('/' + pkgname) | ||
.set('authorization', utils.adminAuth) | ||
.send(pkg) | ||
.expect(201, function (err) { | ||
should.not.exist(err); | ||
pkg = utils.getPackage(pkgname, '0.0.2', utils.admin); | ||
// publish 0.0.2 | ||
request(app.listen()) | ||
request(app) | ||
.put('/' + pkgname) | ||
.set('authorization', utils.adminAuth) | ||
.send(pkg) | ||
.expect(201, function (err) { | ||
should.not.exist(err); | ||
pkg = utils.getPackage(pkgname, '1.0.0', utils.admin); | ||
request(app.listen()) | ||
request(app) | ||
.put('/' + pkgname) | ||
.set('authorization', utils.adminAuth) | ||
.send(pkg) | ||
|
@@ -41,7 +41,7 @@ describe('controllers/registry/package/deprecate.test.js', function () { | |
|
||
describe('PUT /:name', function () { | ||
it('should deprecate [email protected]', function (done) { | ||
request(app.listen()) | ||
request(app) | ||
.put('/' + pkgname) | ||
.set('authorization', utils.adminAuth) | ||
.send({ | ||
|
@@ -57,14 +57,14 @@ describe('controllers/registry/package/deprecate.test.js', function () { | |
}) | ||
.expect(201, function (err) { | ||
should.not.exist(err); | ||
request(app.listen()) | ||
request(app) | ||
.get('/' + pkgname + '/1.0.0') | ||
.expect(200, function (err, res) { | ||
should.not.exist(err); | ||
res.body.deprecated.should.equal('mock test deprecated message 1.0.0'); | ||
|
||
// undeprecated | ||
request(app.listen()) | ||
request(app) | ||
.put('/' + pkgname) | ||
.set('authorization', utils.adminAuth) | ||
.send({ | ||
|
@@ -80,7 +80,7 @@ describe('controllers/registry/package/deprecate.test.js', function () { | |
}) | ||
.expect(201, function (err) { | ||
should.not.exist(err); | ||
request(app.listen()) | ||
request(app) | ||
.get('/' + pkgname + '/1.0.0') | ||
.expect(200, function (err, res) { | ||
should.not.exist(err); | ||
|
@@ -93,7 +93,7 @@ describe('controllers/registry/package/deprecate.test.js', function () { | |
}); | ||
|
||
it('should deprecate version@<1.0.0', function* () { | ||
yield request(app.listen()) | ||
yield request(app) | ||
.put('/' + pkgname) | ||
.set('authorization', utils.adminAuth) | ||
.send({ | ||
|
@@ -115,30 +115,30 @@ describe('controllers/registry/package/deprecate.test.js', function () { | |
}) | ||
.expect(201); | ||
|
||
yield request(app.listen()) | ||
yield request(app) | ||
.get('/' + pkgname + '/0.0.1') | ||
.expect(200) | ||
.expect(res => { | ||
res.body.deprecated.should.equal('mock test deprecated message 0.0.1'); | ||
}); | ||
|
||
yield request(app.listen()) | ||
yield request(app) | ||
.get('/' + pkgname + '/0.0.2') | ||
.expect(200) | ||
.expect(res => { | ||
res.body.deprecated.should.equal('mock test deprecated message 0.0.2'); | ||
}); | ||
|
||
// not change 1.0.0 | ||
yield request(app.listen()) | ||
yield request(app) | ||
.get('/' + pkgname + '/1.0.0') | ||
.expect(200) | ||
.expect(res => { | ||
assert(!res.body.deprecated); | ||
}); | ||
|
||
// show deprecated info on abbreviatedMeta request | ||
yield request(app.listen()) | ||
yield request(app) | ||
.get('/' + pkgname) | ||
.set('accept', 'application/vnd.npm.install-v1+json') | ||
.expect(200) | ||
|
@@ -150,7 +150,7 @@ describe('controllers/registry/package/deprecate.test.js', function () { | |
}); | ||
|
||
it('should 404 deprecate not exists version', function (done) { | ||
request(app.listen()) | ||
request(app) | ||
.put('/' + pkgname) | ||
.set('authorization', utils.adminAuth) | ||
.send({ | ||
|
@@ -172,7 +172,7 @@ describe('controllers/registry/package/deprecate.test.js', function () { | |
}); | ||
|
||
it('should 403 can not modified when use is not maintainer', function (done) { | ||
request(app.listen()) | ||
request(app) | ||
.put('/' + pkgname) | ||
.set('authorization', utils.otherUserAuth) | ||
.send({ | ||
|
Oops, something went wrong.