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

Commit

Permalink
feat(sync): web page also redirect to npm www
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Feb 3, 2015
1 parent 46d65a1 commit 89e7ae3
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 77 deletions.
4 changes: 2 additions & 2 deletions controllers/registry/package/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var debug = require('debug')('cnpmjs.org:controllers:registry:package:list');
var packageService = require('../../../services/package');
var common = require('../../../lib/common');
var SyncModuleWorker = require('../../sync_module_worker');
var utils = require('../../utils');
var config = require('../../../config');

/**
* list all version of a module
Expand Down Expand Up @@ -113,7 +113,7 @@ module.exports = function* list() {
var logId = yield* SyncModuleWorker.sync(name, 'sync-by-install');
debug('start sync %s, get log id %s', name, logId);

return yield utils.proxyToNpmRegistry(this);
return this.redirect(config.officialNpmRegistry + this.url);
}

var latestMod = null;
Expand Down
18 changes: 1 addition & 17 deletions controllers/registry/package/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
var debug = require('debug')('cnpmjs.org:controllers:registry:package:show');
var semver = require('semver');
var packageService = require('../../../services/package');
var npmService = require('../../../services/npm');
var setDownloadURL = require('../../../lib/common').setDownloadURL;
var SyncModuleWorker = require('../../sync_module_worker');
var config = require('../../../config');
Expand Down Expand Up @@ -65,20 +64,5 @@ module.exports = function* show() {
var logId = yield* SyncModuleWorker.sync(name, 'sync-by-install');
debug('start sync %s, get log id %s', name, logId);

// rty to get package from official registry
var r = yield npmService.request('/' + name + '/' + tag, {
registry: config.officialNpmRegistry
});

if (r.status !== 200) {
debug('requet from officialNpmRegistry response %s', r.status);
this.status = 404;
this.body = {
error: 'not exist',
reason: 'tag or version not found: ' + tag
};
return;
}

this.body = r.data;
this.redirect(config.officialNpmRegistry + this.url);
};
17 changes: 0 additions & 17 deletions controllers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ var utility = require('utility');
var ms = require('humanize-ms');
var moment = require('moment');
var downloadTotalService = require('../services/download_total');
var npmService = require('../services/npm');
var nfs = require('../common/nfs');
var config = require('../config');

Expand Down Expand Up @@ -160,19 +159,3 @@ exports.getOssLicenseUrlFromName = function (name) {
return licenseMap[name.toLowerCase()] ?
base + licenseMap[name.toLowerCase()] : base + name;
};

exports.proxyToNpmRegistry = function* (ctx) {
var r = yield* npmService.request(ctx.url);
if (!r.status && r.status < 200) {
r.status = 502;
r.body = {
error: 'remote_npm_registry_error',
reason: 'status: ' + r.status + ' body: ' + (r.data ? r.data.toString() : '[empty]'),
};
return;
}

ctx.status = r.status;
ctx.body = r.data;
return;
};
63 changes: 36 additions & 27 deletions middleware/proxy_to_npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,44 @@
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;
module.exports = function (options) {
var redirectUrl = config.sourceNpmRegistry;
var proxyUrls = [
// /:pkg, dont contains scoped package
/^\/[\w\-\.]+$/,
// /-/package/:pkg/dist-tags
/^\/\-\/package\/[\w\-\.]+\/dist-tags/,
];
if (options && options.isWeb) {
redirectUrl = redirectUrl.replace('//registry.', '//');
proxyUrls = [
// /package/:pkg
/^\/package\/[\w\-\.]+$/,
];
}
return 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;
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;
}
}
if (!match) {
return yield* next;
}

var url = config.sourceNpmRegistry + this.url;
debug('proxy to %s', url);
this.redirect(url);
var url = redirectUrl + this.url;
debug('proxy to %s', url);
this.redirect(url);
};
};
25 changes: 13 additions & 12 deletions middleware/publishable.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ module.exports = function *publishable(next) {
return;
}

// public mode, all user have permission to publish
// but if `config.scopes` exist, only can publish with scopes in `config.scope`
// if `config.forcePublishWithScope` set to true, only admins can publish without scope
// public mode, all user have permission to publish `scoped package`
// and only can publish with scopes in `ctx.user.scopes`, default is `config.scopes`

var name = this.params.name || this.params[0];

Expand All @@ -49,7 +48,7 @@ module.exports = function *publishable(next) {
}

// none-scope
if (checkNoneScope(this)) {
if (checkNoneScope(name, this)) {
return yield* next;
}
};
Expand Down Expand Up @@ -82,19 +81,21 @@ function checkScope(name, ctx) {
* check if user have permission to publish without scope
*/

function checkNoneScope(ctx) {
if (!config.scopes
|| !config.scopes.length
|| !config.forcePublishWithScope) {
function checkNoneScope(name, ctx) {
// admins unpublished everything
if (ctx.user.isAdmin && ctx.method === 'DELETE') {
return true;
}

// only admins can publish or unpublish non-scope modules
if (ctx.user.isAdmin && config.syncModel !== 'none') {
return true;
ctx.status = 403;
if (ctx.user.scopes.length === 0) {
ctx.body = {
error: 'no_perms',
reason: 'can\'t publish non-scoped package, please set `config.scopes`'
};
return;
}

ctx.status = 403;
ctx.body = {
error: 'no_perms',
reason: 'only allow publish with ' + ctx.user.scopes.join(', ') + ' scope(s)'
Expand Down
2 changes: 1 addition & 1 deletion servers/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ app.use(staticCache);

app.keys = ['todokey', config.sessionSecret];
app.proxy = true;
app.use(proxyToNpm);
app.use(proxyToNpm());
app.use(middlewares.bodyParser({jsonLimit: config.jsonLimit}));
app.use(cors({
methods: 'GET,HEAD'
Expand Down
4 changes: 4 additions & 0 deletions servers/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var block = require('../middleware/block');
var logger = require('../common/logger');
var renderMarkdown = require('../common/markdown').render;
var auth = require('../middleware/auth');
var proxyToNpm = require('../middleware/proxy_to_npm');
var routes = require('../routes/web');
var config = require('../config');
var path = require('path');
Expand All @@ -49,6 +50,9 @@ if (config.pagemock) {
app.use(opensearch);
app.keys = ['todokey', config.sessionSecret];
app.proxy = true;
app.use(proxyToNpm({
isWeb: true
}));
app.use(middlewares.bodyParser());
app.use(auth());
app.use(notFound);
Expand Down
3 changes: 2 additions & 1 deletion sync/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ if (sync) {
* sync popular modules
*/

var startSyncPopular = require('./sync_popular');
var syncingPopular = false;
var syncPopular = co(function* syncPopular() {
if (syncingPopular) {
Expand All @@ -100,7 +101,7 @@ var syncPopular = co(function* syncPopular() {
var data;
var error;
try {
data = yield* require('./sync_popular');
data = yield* startSyncPopular();
} catch (err) {
error = err;
error.message += ' (sync package error)';
Expand Down

0 comments on commit 89e7ae3

Please sign in to comment.