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.
feat(sync): enable none syncModel proxy all public packages
- Loading branch information
Showing
13 changed files
with
181 additions
and
53 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
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/**! | ||
* cnpmjs.org - middleware/proxy_to_npm.js | ||
* | ||
* Copyright(c) Alibaba Group Holding Limited. | ||
* | ||
* Authors: | ||
* 苏千 <[email protected]> (http://fengmk2.com) | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var debug = require('debug')('cnpmjs.org:middleware:proxy_to_npm'); | ||
var config = require('../config'); | ||
|
||
var proxyUrls = [ | ||
// /:pkg, dont contains scoped package | ||
/^\/[\w\-\.]+$/, | ||
// /-/package/:pkg/dist-tags | ||
/^\/\-\/package\/[\w\-\.]+\/dist-tags/, | ||
]; | ||
|
||
module.exports = function* proxyToNpm(next) { | ||
if (config.syncModel !== 'none') { | ||
return yield* next; | ||
} | ||
// 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; | ||
} | ||
} | ||
if (!match) { | ||
return yield* next; | ||
} | ||
|
||
var url = config.sourceNpmRegistry + this.url; | ||
debug('proxy to %s', url); | ||
this.redirect(url); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/**! | ||
* cnpmjs.org - test/middleware/proxy_to_npm.test.js | ||
* | ||
* Copyright(c) cnpmjs.org 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 app = require('../../servers/registry'); | ||
var config = require('../../config'); | ||
|
||
describe('middleware/proxy_to_npm.test.js', function () { | ||
beforeEach(function () { | ||
mm(config, 'syncModel', 'none'); | ||
}); | ||
|
||
afterEach(mm.restore); | ||
|
||
describe('package', function () { | ||
it('should proxy to source registry when package not exists', function (done) { | ||
request(app.listen()) | ||
.get('/ms') | ||
.expect('location', config.sourceNpmRegistry + '/ms') | ||
.expect(302, done); | ||
}); | ||
|
||
it('should proxy to source registry when package is not local', function (done) { | ||
request(app.listen()) | ||
.get('/baidu?write=true') | ||
.expect('location', config.sourceNpmRegistry + '/baidu?write=true') | ||
.expect(302, done); | ||
}); | ||
|
||
it('should not proxy to source registry when package is scoped', function (done) { | ||
request(app.listen()) | ||
.get('/@scoped/test-package-name') | ||
.expect(404, done); | ||
}); | ||
}); | ||
|
||
describe('dist-tags', function () { | ||
it('should proxy to source registry when package not exists', function (done) { | ||
request(app.listen()) | ||
.get('/-/package/ms/dist-tags') | ||
.expect('location', config.sourceNpmRegistry + '/-/package/ms/dist-tags') | ||
.expect(302, done); | ||
}); | ||
|
||
it('should dont proxy scoped package', function (done) { | ||
request(app.listen()) | ||
.get('/-/package/@scoped/ms/dist-tags') | ||
.expect(404, done); | ||
}); | ||
}); | ||
}); |