Compare commits

..

1 Commits
2.x ... i18n

Author SHA1 Message Date
fengmk2
43a4440886 start i18n 2015-05-18 02:18:07 +08:00
124 changed files with 1780 additions and 3677 deletions

View File

@@ -2,6 +2,3 @@ node_modules/
coverage/
.tmp/
.git/
tools/
public/
test/

View File

@@ -1,11 +1,8 @@
sudo: false
language: node_js
node_js:
- '4'
- '6'
- '7'
- 'iojs-1.2'
- '0.12'
addons:
- postgresql: '9.3'
script: 'make test-travis-all'
after_script:
- 'npm i codecov && codecov'
after_script: 'npm install coveralls@2 && cat ./coverage/lcov.info | coveralls'

View File

@@ -1,12 +1,7 @@
# Ordered by date of first contribution.
fengmk2 <fengmk2@gmail.com> (https://github.com/fengmk2)
fengmk2 <m@fengmk2.com> (https://github.com/fengmk2)
dead-horse <dead_horse@qq.com> (https://github.com/dead-horse)
alsotang <alsotang@gmail.com> (https://github.com/alsotang)
4simple <wondger@qq.com> (https://github.com/4simple)
afc163 <afc163@gmail.com> (https://github.com/afc163)
Yuwei Ba <i@xiaoba.me> (https://github.com/ibigbug)
dickeylth <dickeylth@gmail.com> (https://github.com/dickeylth)
wenbing <wenbing@users.noreply.github.com> (https://github.com/wenbing)
21paradox <1036339815@qq.com> (https://github.com/21paradox)

View File

@@ -1,364 +1,4 @@
2.19.4 / 2017-03-26
==================
* feat: need to sync sourceNpmRegistry also (#1153)
* docs: change user.json to utf8mb4
2.19.3 / 2017-02-22
==================
* fix: should get package from orginal registry when package is unpublished (#1130)
2.19.2 / 2017-01-05
==================
* fix: should auto sync un-deprecate message (#1105)
2.19.1 / 2016-12-29
==================
* fix: try to use the best repository url (#1102)
2.19.0 / 2016-12-21
==================
* feat: keyword search with limit to support keywords > 100 (#1097)
2.18.0 / 2016-12-05
==================
* fix: support downloads total on scope package (#1088)
* fix: try to sync from official replicate (#1076)
* feat: add change password script (#1070)
* test: skip always fail tests
* test: add node v7
* feat: show more sync info
2.17.2 / 2016-11-13
==================
* fix: ignore long package name on unpublished sync (#1067)
2.17.1 / 2016-11-08
==================
* fix: add publish_time for private packages (#1061)
2.17.0 / 2016-11-03
==================
* feat: make snyk.io url configable (#1058)
2.16.2 / 2016-09-27
==================
* fix: try to use config.registryHost first on setDownloadURL (#1044)
2.16.1 / 2016-08-22
==================
* refactor: refine publishable's code (#1022)
2.16.0 / 2016-08-22
==================
* feat: admin can do everything (#1021)
2.15.0 / 2016-08-22
==================
* feat: return dist-tag on package registry ([#1020](https://github.com/cnpm/cnpmjs.org/issues/1020))
* chore(package): update supertest to version 2.0.0 ([#1004](https://github.com/cnpm/cnpmjs.org/issues/1004))
2.14.0 / 2016-08-04
==================
* feat: password may contains ":" ([#999](https://github.com/cnpm/cnpmjs.org/issues/999))
* fix: limit sync fails email notice ([#1006](https://github.com/cnpm/cnpmjs.org/issues/1006))
2.13.0 / 2016-07-26
==================
* feat: enable maxrequests middleware ([#1003](https://github.com/cnpm/cnpmjs.org/issues/1003))
2.12.2 / 2016-07-11
==================
* fix: getModuleByRange don't list all packages ([#990](https://github.com/cnpm/cnpmjs.org/issues/990))
* fix: should show new version package count ([#984](https://github.com/cnpm/cnpmjs.org/issues/984))
2.12.1 / 2016-07-01
==================
* fix: make sure chagnes stream destroy ([#982](https://github.com/cnpm/cnpmjs.org/issues/982))
* chore(package): update semver to version 5.2.0 ([#978](https://github.com/cnpm/cnpmjs.org/issues/978))
* deps: use ^ instead of ~ ([#976](https://github.com/cnpm/cnpmjs.org/issues/976))
* chore(package): update mini-logger to version 1.1.1 ([#973](https://github.com/cnpm/cnpmjs.org/issues/973))
2.12.0 / 2016-06-26
==================
* fix: logger seperator should be one EOL ([#972](https://github.com/cnpm/cnpmjs.org/issues/972))
* feat: add security check badge for public package ([#971](https://github.com/cnpm/cnpmjs.org/issues/971))
2.11.0 / 2016-06-25
==================
* feat: add changes stream syncer ([#970](https://github.com/cnpm/cnpmjs.org/issues/970))
* chore(package): update pg to version 5.1.0 ([#953](https://github.com/cnpm/cnpmjs.org/issues/953))
2.10.1 / 2016-06-05
==================
* fix: should sync missing public scoped package on install ([#946](https://github.com/cnpm/cnpmjs.org/issues/946))
* chore(package): update bytes to version 2.4.0 ([#943](https://github.com/cnpm/cnpmjs.org/issues/943))
* userService ([#926](https://github.com/cnpm/cnpmjs.org/issues/926))
* chore(package): update should to version 8.4.0 ([#928](https://github.com/cnpm/cnpmjs.org/issues/928))
* chore(package): update humanize-ms to version 1.2.0 ([#927](https://github.com/cnpm/cnpmjs.org/issues/927))
* chore(package): update kcors to version 1.2.1 ([#918](https://github.com/cnpm/cnpmjs.org/issues/918))
* chore(package): update urllib to version 2.9.0 ([#898](https://github.com/cnpm/cnpmjs.org/issues/898))
2.10.0 / 2016-04-15
==================
* feat: show tarball url on package page ([#894](https://github.com/cnpm/cnpmjs.org/issues/894))
* chore(package): update koa-mock to version 1.6.1 ([#891](https://github.com/cnpm/cnpmjs.org/issues/891))
2.9.5 / 2016-04-12
==================
* fix: change logo url to a better https source
* fix: http://cnpmjs.org/package/fms pre style ([#739](https://github.com/cnpm/cnpmjs.org/issues/739))
2.9.4 / 2016-04-09
==================
* fix: don't sync constructor package on exists mode ([#883](https://github.com/cnpm/cnpmjs.org/issues/883))
* Update utility to version 1.7.0 🚀
* chore: update sponsor link
2.9.3 / 2016-04-05
==================
* fix: use better diff time to check sync status
* Update sequelize to version 3.21.0 🚀
* chore(package): update agentkeepalive to version 2.1.0
* chore(package): update pg to version 4.5.2
2.9.2 / 2016-03-29
==================
* fix: override antd for ul & ol list number & icon.
2.9.1 / 2016-03-29
==================
* refactor: add more ua info on syncer
2.9.0 / 2016-03-26
==================
* feat: only admin can unpublish
* chore(package): update gravatar to version 1.5.0
* chore(package): update sequelize to version 3.20.0
* fix: fix save download count unqiue constraint error
* chore(package): update moment to version 2.12.0
2.8.1 / 2016-03-07
==================
* fix: only send warning email if no any sync data after 24h
* chore(package): update kcors to version 1.1.0
* chore(package): update koa to version 1.2.0
* chore(package): update urllib to version 2.8.0
2.8.0 / 2016-02-23
==================
* fix: convert `*` to latest tag
* deps: upgrade deps and remove node 2.0.0 support
* doc: update sponsors on readme
* fix: update copyright year
* doc: fix disturl typo
* deps: sequelize@3.19.0
2.7.1 / 2016-02-01
==================
* fix(semver): when have invalid version([#817](https://github.com/cnpm/cnpmjs.org/issues/817))
2.7.0 / 2016-02-01
==================
* test: fix all test cases
* test: fix unpublish
* test: add complex range test case
* feat: support semver([#816](https://github.com/cnpm/cnpmjs.org/issues/816))
2.6.2 / 2016-01-19
==================
* feat: list & show support jsonp
* chore(package): update urllib to version 2.7.0
* Delete install.md
2.6.1 / 2016-01-12
==================
* fix: source registry is not cnpm, ignore check status
2.6.0 / 2016-01-12
==================
* feat(sync): monitor sync status
* chore(package): update agentkeepalive to version 2.0.3
* fix SequelizeDatabaseError: ER_NO_SUCH_TABLE: Table 'qnpm.total' doesn't exist\nreproduce this bug:\nthe first startup of cnpmjs.org
* chore(package): update moment to version 2.11.0
* chore(package): update xss to version 0.2.10
* chore(package): update pg-hstore to version 2.3.2
* chore(package): update mini-logger to version 1.1.0
* chore(package): update urllib to version 2.6.0
* fix: row.package will json parse error
* remove bluebird
* chore(package): update utility to version 1.6.0
2.5.1 / 2015-12-02
==================
* chore(package): update bluebird to version 3.0.6
* fix: SequelizeDatabaseError
* fix(dist_tag): disable delete latest tag
* feat: count total private pkgs
* fix: use isoweek. a week start from monday
* chore(package): update xss to version 0.2.8
* chore(package): update semver to version 5.1.0
2.5.0 / 2015-11-17
==================
* test: add node v5
* feat(sync): sync deleted user
* Update show.js
* chore(package): update bytes to version 2.2.0
* do not sync inner username
* gzip static file
* chore(package): update bytes to version 2.1.0
* chore(package): update is-type-of to version 1.0.0
* Update static.js
* chore(package): update cfork to version 1.4.0
* chore(package): update bluebird to version 3.0.5
2.4.1 / 2015-10-27
==================
* fix: improve registry index page performance with cache
* Configable badge URL prefix.
* chore(package): update koa-mock to version 1.5.0
* chore(package): update urllib to version 2.5.0
* chore(package): update co to version 4.6.0
* chore(package): update semver to version 5.0.3
2.4.0 / 2015-10-21
==================
* feat(registry): add package's dependents api
* fix: show package's dependents
* chore(package): update utility to version 1.5.0
* chore(package): update bluebird to version 2.10.2
2.3.1 / 2015-10-15
==================
* refactor: remove gnode
* deps: upgrade giturl
* Some fixes
2.3.0 / 2015-10-15
==================
* Add dev dependencies.
* Package page fix.
* refactor: add more sync log
* Add mock data.
* refactor: add more sync log
* Fix sidebar overflow.
* Merge pull request [#680](https://github.com/cnpm/cnpmjs.org/issues/680) from ibigbug/ant-design
* Clean code.
* Indent.
* chore(package): update debug to version 2.2.0
* chore(package): update should to version 7.1.0
* chore(package): update koa to version 1.1.0
* Remove default adBanner.
* Package pages.
* Common styles.
* search page.
* Sync page.
* Profile page antd style.
* Unpublished pkg page style.
* Add page title for unpubed pkg.
* Index page style use antd.
* chore(package): update pg to version 4.4.2
* chore(package): update cfork to version 1.3.1
* chore(package): update moment to version 2.10.6
* feat(badge): Use qiniu cdn
2.2.1 / 2015-09-30
==================
* test: use istanbul
* pref: move out try/catch block
* fix: support nfs.url is generator
2.2.0 / 2015-09-29
==================
* feat: list packages by username
* test: use codecov
* feat(badge): support custom subject
* fix(sync): add recover logic
* feat(sync): add sync scripts
2.1.5 / 2015-09-05
==================
* fix: only sync update packages
2.1.4 / 2015-09-05
==================
* fix: support new array and old map format both
* fix: /-/all/since had been redirect to /-/all/static/today.json
* fix(list): let koa-etag to caculate the etag
2.1.3 / 2015-08-18
==================
* fix: sync public scope package download url is wrong
* fix: default registry change to taobao registry
2.1.2 / 2015-08-09
==================
* fix(syncer): sync worker pkg null bug
* feat(web): add downloads badge
2.1.1 / 2015-07-27
==================
* fix: fix private scope package detect
* fix: dont sync if upstream is npm registry
* fix(sync): support sync public scope package
* test: fix fails tests
* fix: ignore 503 server error
* fix: ignore sync 503 server error
2.1.0 / 2015-07-08
==================
* feat(web): search support jsonp
* fix function name
2.0.0 / 2015-05-11
==================
@@ -380,10 +20,10 @@
* feat: support always-auth
* fix mysql select args = [] bug
* fix [#597](https://github.com/cnpm/cnpmjs.org/issues/597) sequelize raw query.
* fix #597 sequelize raw query.
* fix(markdown): hotfix markdown-it cpu problem
* feat: upgrade to co4
* use kcors fixes [#594](https://github.com/cnpm/cnpmjs.org/issues/594)
* use kcors fixes #594
2.0.0-rc.13 / 2015-02-04
==================

View File

@@ -1,6 +1,6 @@
This software is licensed under the MIT License.
Copyright(c) cnpm and other contributors.
Copyright(c) 2014 - 2015 cnpm and other contributors.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -3,12 +3,26 @@ REPORTER = spec
TIMEOUT = 30000
MOCHA_OPTS =
DB = sqlite
DISTURL = http://npm.taobao.org/mirrors/iojs
BIN = iojs
jshint:
@node_modules/.bin/jshint .
ifeq ($(findstring io.js, $(shell which node)),)
BIN = node
DISTURL = http://npm.taobao.org/mirrors/node
endif
install:
@npm install --build-from-source --registry=http://registry.npm.taobao.org \
--disturl=$(DISTURL)
install-production production:
@NODE_ENV=production $(MAKE) install
jshint: install
@-node_modules/.bin/jshint ./
init-database:
@node test/init_db.js
@$(BIN) --harmony test/init_db.js
init-mysql:
@mysql -uroot -e 'DROP DATABASE IF EXISTS cnpmjs_test;'
@@ -18,12 +32,14 @@ init-pg:
@psql -c 'DROP DATABASE IF EXISTS cnpmjs_test;'
@psql -c 'CREATE DATABASE cnpmjs_test;'
test: init-database
test: install init-database
@NODE_ENV=test DB=${DB} node_modules/.bin/mocha \
--harmony \
--reporter $(REPORTER) \
--timeout $(TIMEOUT) \
--require should \
--require thunk-mocha \
--require should-http \
--require co-mocha \
--require ./test/init.js \
$(MOCHA_OPTS) \
$(TESTS)
@@ -39,15 +55,16 @@ test-pg: init-pg
test-all: test-sqlite test-mysql
test-cov cov: init-database
@NODE_ENV=test DB=${DB} node \
node_modules/.bin/istanbul cover \
test-cov cov: install init-database
@NODE_ENV=test DB=${DB} $(BIN) --harmony \
node_modules/.bin/istanbul cover --preserve-comments \
node_modules/.bin/_mocha \
-- -u exports \
--reporter $(REPORTER) \
--timeout $(TIMEOUT) \
--require should \
--require thunk-mocha \
--require should-http \
--require co-mocha \
--require ./test/init.js \
$(MOCHA_OPTS) \
$(TESTS)
@@ -58,16 +75,18 @@ test-cov-sqlite:
test-cov-mysql: init-mysql
@$(MAKE) test-cov DB=mysql
test-travis: init-database
@NODE_ENV=test DB=${DB} CNPM_SOURCE_NPM=https://registry.npmjs.com CNPM_SOURCE_NPM_ISCNPM=false \
node \
node_modules/.bin/istanbul cover \
test-travis: install init-database
@NODE_ENV=test DB=${DB} CNPM_SOURCE_NPM=http://registry.npmjs.com CNPM_SOURCE_NPM_ISCNPM=false \
$(BIN) --harmony \
node_modules/.bin/istanbul cover --preserve-comments \
node_modules/.bin/_mocha \
--report lcovonly \
-- -u exports \
--reporter dot \
--timeout $(TIMEOUT) \
--require should \
--require thunk-mocha \
--require should-http \
--require co-mocha \
--require ./test/init.js \
$(MOCHA_OPTS) \
$(TESTS)
@@ -83,19 +102,20 @@ test-travis-pg:
@psql -c 'CREATE DATABASE cnpmjs_test;' -U postgres
@DB_PORT=5432 DB_USER=postgres $(MAKE) test-travis DB=postgres
test-travis-all: jshint test-travis-sqlite test-travis-mysql test-travis-pg
test-travis-all: test-travis-sqlite test-travis-mysql test-travis-pg
dev:
@NODE_ENV=development node node_modules/.bin/node-dev dispatch.js
@NODE_ENV=development $(BIN) node_modules/.bin/node-dev --harmony dispatch.js
contributors:
contributors: install
@node_modules/.bin/contributors -f plain -o AUTHORS
autod:
autod: install
@node_modules/.bin/autod -w \
--prefix "~" \
--exclude public,view,docs,backup,coverage \
--dep mysql \
--keep should,supertest,chunkstream,mm,pedding
--dep bluebird,mysql \
--keep should,supertest,should-http,chunkstream,mm,pedding
@$(MAKE) install
.PHONY: test

View File

@@ -3,23 +3,29 @@ cnpmjs.org
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![Gittip][gittip-image]][gittip-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![node version][node-image]][node-url]
[![npm download][download-image]][download-url]
[![gitter][gitter-image]][gitter-url]
[npm-image]: http://cnpmjs.org/badge/v/cnpmjs.org.svg?style=flat-square
[npm-url]: http://cnpmjs.org/package/cnpmjs.org
[travis-image]: https://img.shields.io/travis/cnpm/cnpmjs.org.svg?style=flat-square
[travis-url]: https://travis-ci.org/cnpm/cnpmjs.org
[codecov-image]: https://codecov.io/gh/cnpm/cnpmjs.org/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/cnpm/cnpmjs.org
[coveralls-image]: https://img.shields.io/coveralls/cnpm/cnpmjs.org.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/cnpm/cnpmjs.org?branch=master
[gittip-image]: https://img.shields.io/gittip/fengmk2.svg?style=flat-square
[gittip-url]: https://www.gittip.com/fengmk2/
[david-image]: https://img.shields.io/david/cnpm/cnpmjs.org.svg?style=flat-square
[david-url]: https://david-dm.org/cnpm/cnpmjs.org
[snyk-image]: https://snyk.io/test/npm/cnpmjs.org/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/cnpmjs.org
[node-image]: https://img.shields.io/badge/node.js-%3E=_0.11-red.svg?style=flat-square
[node-url]: http://nodejs.org/download/
[download-image]: https://img.shields.io/npm/dm/cnpmjs.org.svg?style=flat-square
[download-url]: https://npmjs.org/package/cnpmjs.org
[gitter-image]: https://badges.gitter.im/Join%20Chat.svg
[gitter-url]: https://gitter.im/cnpm/cnpmjs.org?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge
![logo](https://raw.github.com/cnpm/cnpmjs.org/master/logo.png)
@@ -74,7 +80,7 @@ as well as [New features in 2.x](https://github.com/cnpm/cnpmjs.org/wiki/New-fea
### Dependencies
* [node](http://nodejs.org) >= 4.3.1
* [node](http://nodejs.org) >=0.11.12, use `--harmony`
* Databases: only required one type
* [sqlite3](https://npm.taobao.org/package/sqlite3) >= 3.0.2, we use `sqlite3` by default
* [MySQL](http://dev.mysql.com/downloads/) >= 0.5.0, include `mysqld` and `mysql cli`. I test on `mysql@5.6.16`.
@@ -114,9 +120,8 @@ Tips: make sure your code is following the [node-style-guide](https://github.com
## Sponsors
- [![阿里云](https://static.aliyun.com/images/www-summerwind/logo.gif)](http://click.aliyun.com/m/4288/) (2016.2 - now)
- [![UCloud云计算](https://www.ucloud.cn/static/style/images/about/logo.png)](http://www.ucloud.cn?sem=sdk-CNPMJS) (2015.3 - 2016.3)
- [![UCloud云计算](https://www.ucloud.cn/www/img/www/logo.png)](http://www.ucloud.cn/sdk?sem=sdk-CNPMJS)
## License
[MIT](LICENSE.txt)
MIT

View File

@@ -1,24 +0,0 @@
"use strict";
// Only support for ./services/DefaultUserService. If you use custom user service, ignore this file.
// call with:
// $ node ./bin/change_password.js 'username' 'new_password'
var UserModel = require('../models').User;
var co = require('co');
var utility = require('utility');
var username = process.argv[2];
var newPassword = process.argv[3];
co(function * () {
var user = yield UserModel.find({where: {name: username}});
var salt = user.salt;
console.log(`user original password_sha: ${user.password_sha}`);
var newPasswordSha = utility.sha1(newPassword + salt);
user.password_sha = newPasswordSha;
user = yield user.save();
console.log(`change user password successful!! user new password_sha: ${user.password_sha}`);
process.exit(0);
}).catch(function (e) {
console.log(e);
});

View File

@@ -1,7 +1,9 @@
#!/usr/bin/env node
/**!
* Copyright(c) cnpm and other contributors.
* cnpmjs.org - bin/cli.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
@@ -14,6 +16,7 @@
* Module dependencies.
*/
require('gnode');
var debug = require('debug')('cnpmjs.org:cli');
var program = require('commander');
var path = require('path');
@@ -83,10 +86,9 @@ function start(options) {
debug('save config %s to %s', configJSON, configfile);
// if sqlite db file not exists, init first
initDatabase(function() {
require('../dispatch');
});
initDatabase();
require('../dispatch');
fs.writeFileSync(path.join(dataDir, 'pid'), process.pid + '');
}
@@ -106,7 +108,7 @@ function stop(options) {
}
}
function initDatabase(callback) {
function initDatabase() {
var models = require('../models');
models.sequelize.sync({ force: false })
@@ -118,7 +120,6 @@ function initDatabase(callback) {
throw err;
} else {
console.log('[models/init_script.js] `sqlite` sequelize sync and init success');
callback();
}
});
})

View File

@@ -7,7 +7,7 @@ export NODE_ENV='production'
ulimit -c unlimited
cd `dirname $0`/..
NODEJS='node'
NODEJS='node --harmony'
BASE_HOME=`pwd`
PROJECT_NAME=`basename ${BASE_HOME}`
STDOUT_LOG=`$NODEJS -e "console.log(require('path').join(require('$BASE_HOME/config').logdir, 'nodejs_stdout.log'));\

View File

@@ -1,56 +1,62 @@
/**!
* cnpmjs.org - common/logger.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
const debug = require('debug')('cnpmjs.org:logger');
const formater = require('error-formater');
const Logger = require('mini-logger');
const utility = require('utility');
const util = require('util');
const os = require('os');
const config = require('../config');
const mail = require('./mail');
/**
* Module dependencies.
*/
const isTEST = process.env.NODE_ENV === 'test';
const categories = ['sync_info', 'sync_error'];
var formater = require('error-formater');
var Logger = require('mini-logger');
var utility = require('utility');
var util = require('util');
var config = require('../config');
var mail = require('./mail');
const logger = module.exports = Logger({
var isTEST = process.env.NODE_ENV === 'test';
var categories = ['sync_info', 'sync_error'];
var logger = module.exports = Logger({
categories: categories,
dir: config.logdir,
duration: '1d',
format: '[{category}.]YYYY-MM-DD[.log]',
stdout: config.debug && !isTEST,
errorFormater: errorFormater,
seperator: os.EOL,
errorFormater: errorFormater
});
const to = [];
var to = [];
for (var user in config.admins) {
to.push(config.admins[user]);
}
function errorFormater(err) {
const msg = formater.both(err);
var msg = formater.both(err);
mail.error(to, msg.json.name, msg.text);
return msg.text;
}
logger.syncInfo = function () {
const args = [].slice.call(arguments);
var args = [].slice.call(arguments);
if (typeof args[0] === 'string') {
args[0] = util.format('[%s][%s] ', utility.logDate(), process.pid) + args[0];
}
if (debug.enabled) {
debug.apply(debug, args);
}
logger.sync_info.apply(logger, args);
};
logger.syncError =function () {
const args = [].slice.call(arguments);
var args = [].slice.call(arguments);
if (typeof args[0] === 'string') {
args[0] = util.format('[%s][%s] ', utility.logDate(), process.pid) + args[0];
}
if (debug.enabled) {
debug.apply(debug, args);
}
logger.sync_error.apply(logger, arguments);
};

View File

@@ -1,9 +1,11 @@
/*!
* cnpmjs.org - common/nfs.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';

View File

@@ -1,5 +1,20 @@
/**!
* cnpmjs.org - config/index.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com>
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/
'use strict';
/**
* Module dependencies.
*/
var mkdirp = require('mkdirp');
var copy = require('copy-to');
var path = require('path');
@@ -13,7 +28,6 @@ var dataDir = path.join(process.env.HOME || root, '.cnpmjs.org');
var config = {
version: version,
dataDir: dataDir,
/**
* Cluster mode
@@ -81,7 +95,7 @@ var config = {
}
},
logoURL: 'https://os.alipayobjects.com/rmsportal/oygxuIUkkrRccUz.jpg', // cnpm logo image url
logoURL: '//ww4.sinaimg.cn/large/69c1d4acgw1ebfly5kjlij208202oglr.jpg', // cnpm logo image url
adBanner: '',
customReadmeFile: '', // you can use your custom readme file instead the cnpm one
customFooter: '', // you can add copyright and site total script html here
@@ -147,7 +161,7 @@ var config = {
enablePrivate: false,
// registry scopes, if don't set, means do not support scopes
scopes: [ '@cnpm', '@cnpmtest', '@cnpm-test' ],
scopes: [ '@cnpm', '@cnpmtest' ],
// some registry already have some private packages in global scope
// but we want to treat them as scoped private packages,
@@ -162,13 +176,12 @@ var config = {
// cnpm wont directly sync from this one
// but sometimes will request it for some package infomations
// please don't change it if not necessary
officialNpmRegistry: 'https://registry.npmjs.com',
officialNpmReplicate: 'https://replicate.npmjs.com',
officialNpmRegistry: 'https://registry.npmjs.org',
// sync source, upstream registry
// If you want to directly sync from official npm's registry
// please drop them an email first
sourceNpmRegistry: 'https://registry.npm.taobao.org',
sourceNpmRegistry: 'http://registry.npm.taobao.org',
// upstream registry is base on cnpm/cnpmjs.org or not
// if your upstream is official npm registry, please turn it off
@@ -198,17 +211,10 @@ var config = {
// sync devDependencies or not, default is false
syncDevDependencies: false,
// changes streaming sync
syncChangesStream: false,
handleSyncRegistry: 'http://127.0.0.1:7001',
// badge subject on http://shields.io/
badgePrefixURL: 'https://img.shields.io/badge',
badgeSubject: 'cnpm',
// custom user service, @see https://github.com/cnpm/cnpmjs.org/wiki/Use-Your-Own-User-Authorization
// when you not intend to ingegrate with your company's user system, then use null, it would
// use the default cnpm user system
userService: null,
// always-auth https://docs.npmjs.com/misc/config#always-auth
@@ -218,9 +224,6 @@ var config = {
// if you're behind firewall, need to request through http proxy, please set this
// e.g.: `httpProxy: 'http://proxy.mycompany.com:8080'`
httpProxy: null,
// snyk.io root url
snykUrl: 'https://snyk.io',
};
if (process.env.NODE_ENV !== 'test') {

View File

@@ -1,5 +1,19 @@
/**!
* cnpmjs.org - controllers/registry/package/dist_tag.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
/**
* Module dependencies.
*/
var packageService = require('../../../services/package');
function ok() {
@@ -11,7 +25,7 @@ function ok() {
// GET /-/package/:pkg/dist-tags -- returns the package's dist-tags
exports.index = function* () {
var name = this.params.name || this.params[0];
var rows = yield packageService.listModuleTags(name);
var rows = yield* packageService.listModuleTags(name);
var tags = {};
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
@@ -65,14 +79,6 @@ exports.set = function* () {
exports.destroy = function* () {
var name = this.params.name || this.params[0];
var tag = this.params.tag || this.params[1];
if (tag === 'latest') {
this.status = 400;
this.body = {
error: 'dist_tag_error',
reason: 'Can\'t not delete latest tag',
};
return;
}
yield packageService.removeModuleTagsByNames(name, tag);
this.body = ok();
};

View File

@@ -1,9 +1,11 @@
/**!
* Copyright(c) cnpm and other contributors.
* cnpmjs.org - controllers/registry/package/download.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
@@ -16,7 +18,6 @@ var debug = require('debug')('cnpmjs.org:controllers:registry:download');
var mime = require('mime');
var utility = require('utility');
var defer = require('co-defer');
var is = require('is-type-of');
var nfs = require('../../../common/nfs');
var logger = require('../../../common/logger');
var common = require('../../../lib/common');
@@ -31,16 +32,12 @@ module.exports = function* download(next) {
var name = this.params.name || this.params[0];
var filename = this.params.filename || this.params[1];
var version = filename.slice(name.length + 1, -4);
var row = yield packageService.getModule(name, version);
var row = yield* packageService.getModule(name, version);
// can not get dist
var url = null;
if (typeof nfs.url === 'function') {
if (is.generatorFunction(nfs.url)) {
url = yield nfs.url(common.getCDNKey(name, filename));
} else {
url = nfs.url(common.getCDNKey(name, filename));
}
url = nfs.url(common.getCDNKey(name, filename));
}
debug('download %s %s %s %s', name, filename, version, url);
@@ -81,7 +78,7 @@ module.exports = function* download(next) {
this.attachment(filename);
this.etag = dist.shasum;
this.body = yield downloadAsReadStream(dist.key);
this.body = yield* downloadAsReadStream(dist.key);
};
defer.setInterval(function* () {
@@ -107,12 +104,10 @@ defer.setInterval(function* () {
try {
yield* downloadTotalService.plusModuleTotal({ name: name, date: date, count: count });
} catch (err) {
if (err.name !== 'SequelizeUniqueConstraintError') {
err.message += '; name: ' + name + ', count: ' + count + ', date: ' + date;
logger.error(err);
}
err.message += '; name: ' + name + ', count: ' + count + ', date: ' + date;
logger.error(err);
// save back to _downloads, try again next time
_downloads[name] = (_downloads[name] || 0) + count;
}
}
}, 5000 + Math.ceil(Math.random() * 1000));
}, 5000);

View File

@@ -1,11 +1,25 @@
/*!
* cnpmjs.org - controllers/registry/package/download_total.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (https://github.com/dead-horse)
*/
'use strict';
/**
* Module dependencies.
*/
var DownloadTotal = require('../../../services/download_total');
var DATE_REG = /^\d{4}-\d{2}-\d{2}$/;
module.exports = function* downloadTotal() {
var range = this.params.range || this.params[0] || '';
var name = this.params.name || this.params[1];
module.exports = function* downloadTotal () {
var range = this.params.range;
var name = this.params.name;
range = range.split(':');
if (range.length !== 2

View File

@@ -1,3 +1,13 @@
/**!
* cnpmjs.org - controllers/registry/package/list.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
/**
@@ -35,6 +45,9 @@ module.exports = function* list() {
}
}
// use modifiedTime as etag
this.set('ETag', '"' + modifiedTime.getTime() + '"');
// must set status first
this.status = 200;
if (this.fresh) {
@@ -91,7 +104,7 @@ module.exports = function* list() {
this.status = 404;
this.body = {
error: 'not_found',
reason: 'document not found',
reason: 'document not found'
};
return;
}
@@ -120,14 +133,8 @@ module.exports = function* list() {
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
var pkg = row.package;
// pkg is string ... ignore it
if (typeof pkg === 'string') {
continue;
}
common.setDownloadURL(pkg, this);
pkg._cnpm_publish_time = row.publish_time;
pkg.publish_time = pkg.publish_time || row.publish_time;
versions[pkg.version] = pkg;
var t = times[pkg.version] = row.publish_time ? new Date(row.publish_time) : row.gmt_modified;
@@ -192,5 +199,5 @@ module.exports = function* list() {
info.license = pkg.license;
debug('show module %s: %s, latest: %s', orginalName, rev, latestMod.version);
this.jsonp = info;
this.body = info;
};

View File

@@ -1,29 +0,0 @@
/**!
* list packages by username
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/
'use strict';
/**
* Module dependencies.
*/
const packageService = require('../../../services/package');
module.exports = function*() {
const username = this.params.user;
const packages = yield packageService.listModulesByUser(username);
this.body = {
user: {
name: username,
},
packages: packages,
};
};

View File

@@ -1,26 +0,0 @@
/**!
* list package's dependents
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/
'use strict';
/**
* Module dependencies.
*/
const packageService = require('../../../services/package');
module.exports = function*() {
const name = this.params.name || this.params[0];
const dependents = yield packageService.listDependents(name);
this.body = {
dependents: dependents,
};
};

View File

@@ -1,9 +1,11 @@
/**!
* Copyright(c) cnpm and other contributors.
* cnpmjs.org - controllers/registry/package/list_shorts.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
@@ -17,5 +19,5 @@ var packageService = require('../../../services/package');
// GET /-/short
// List all packages names only
module.exports = function* () {
this.body = yield packageService.listAllPublicModuleNames();
this.body = yield* packageService.listAllPublicModuleNames();
};

View File

@@ -1,5 +1,19 @@
/**!
* cnpmjs.org - controllers/registry/package/list_since.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
/**
* Module dependencies.
*/
var packageService = require('../../../services/package');
var A_WEEK_MS = 3600000 * 24 * 7;
@@ -7,7 +21,7 @@ var A_WEEK_MS = 3600000 * 24 * 7;
// GET /-/all/since?stale=update_after&startkey={key}
// List packages names since startkey
// https://github.com/npm/npm-registry-client/blob/master/lib/get.js#L89
module.exports = function* listSince() {
module.exports = function* () {
var query = this.query;
if (query.stale !== 'update_after') {
this.status = 400;
@@ -35,7 +49,7 @@ module.exports = function* listSince() {
Date(), query, this.ip);
}
var names = yield packageService.listPublicModuleNamesSince(startkey);
var names = yield* packageService.listPublicModuleNamesSince(startkey);
var result = { _updated: updated };
names.forEach(function (name) {
result[name] = true;

View File

@@ -1,5 +1,19 @@
/**!
* cnpmjs.js - controllers/registry/package/show.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
/**
* Module dependencies.
*/
var debug = require('debug')('cnpmjs.org:controllers:registry:package:show');
var semver = require('semver');
var packageService = require('../../../services/package');
@@ -18,48 +32,28 @@ var config = require('../../../config');
module.exports = function* show() {
var name = this.params.name || this.params[0];
var tag = this.params.version || this.params[1];
if (tag === '*') {
tag = 'latest';
}
var version = semver.valid(tag);
var range = semver.validRange(tag);
var mod;
if (version) {
mod = yield packageService.getModule(name, version);
} else if (range) {
mod = yield packageService.getModuleByRange(name, range);
mod = yield* packageService.getModule(name, version);
} else {
mod = yield packageService.getModuleByTag(name, tag);
mod = yield* packageService.getModuleByTag(name, tag);
}
if (mod) {
setDownloadURL(mod.package, this);
mod.package._cnpm_publish_time = mod.publish_time;
mod.package.publish_time = mod.package.publish_time || mod.publish_time;
var rs = yield [
packageService.listMaintainers(name),
packageService.listModuleTags(name),
];
var maintainers = rs[0];
var maintainers = yield* packageService.listMaintainers(name);
if (maintainers.length > 0) {
mod.package.maintainers = maintainers;
}
var tags = rs[1];
var distTags = {};
for (var i = 0; i < tags.length; i++) {
var t = tags[i];
distTags[t.tag] = t.version;
}
// show tags for npminstall faster download
mod.package['dist-tags'] = distTags;
this.jsonp = mod.package;
this.body = mod.package;
return;
}
// if not fond, sync from source registry
if (!this.allowSync) {
this.status = 404;
this.jsonp = {
this.body = {
error: 'not exist',
reason: 'version not found: ' + version
};
@@ -67,7 +61,7 @@ module.exports = function* show() {
}
// start sync
var logId = yield SyncModuleWorker.sync(name, 'sync-by-install');
var logId = yield* SyncModuleWorker.sync(name, 'sync-by-install');
debug('start sync %s, get log id %s', name, logId);
this.redirect(config.officialNpmRegistry + this.url);

View File

@@ -21,7 +21,7 @@ var config = require('../config');
exports.sync = function* () {
var username = this.user.name || 'anonymous';
var name = this.params.name || this.params[0];
var name = this.params.name;
var type = 'package';
if (name.indexOf(':') > 0) {
// user:fengmk2
@@ -60,13 +60,8 @@ exports.sync = function* () {
};
exports.getSyncLog = function* (next) {
var logId = Number(this.params.id || this.params[1]);
var logId = this.params.id;
var offset = Number(this.query.offset) || 0;
if (!logId) { // NaN
this.status = 404;
return;
}
var row = yield* Log.get(logId);
if (!row) {
return yield* next;

View File

@@ -1,6 +1,21 @@
/**!
* cnpmjs.org - controllers/sync_module_worker.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/
'use strict';
var debug = require('debug')('cnpmjs.org:sync_module_worker');
/**
* Module dependencies.
*/
var debug = require('debug')('cnpmjs.org:proxy:sync_module_worker');
var co = require('co');
var gather = require('co-gather');
var defer = require('co-defer');
@@ -25,11 +40,7 @@ var User = require('../models').User;
var os = require('os');
var USER_AGENT = 'sync.cnpmjs.org/' + config.version +
' hostname/' + os.hostname() +
' syncModel/' + config.syncModel +
' syncInterval/' + config.syncInterval +
' syncConcurrency/' + config.syncConcurrency +
' ' + urllib.USER_AGENT;
' hostname/' + os.hostname() + ' ' + urllib.USER_AGENT;
function SyncModuleWorker(options) {
EventEmitter.call(this);
@@ -59,7 +70,6 @@ function SyncModuleWorker(options) {
this.successes = [];
this.fails = [];
this.updates = [];
}
util.inherits(SyncModuleWorker, EventEmitter);
@@ -128,7 +138,7 @@ SyncModuleWorker.prototype.start = function () {
// sync upstream
if (that.syncUpstreamFirst) {
try {
yield that.syncUpstream(that.startName);
yield* that.syncUpstream(that.startName);
} catch (err) {
logger.error(err);
}
@@ -181,18 +191,11 @@ SyncModuleWorker.prototype._doneOne = function* (concurrencyId, name, success) {
var that = this;
// relase the stack: https://github.com/cnpm/cnpmjs.org/issues/328
defer.setImmediate(function* () {
yield that.next(concurrencyId);
yield* that.next(concurrencyId);
});
};
SyncModuleWorker.prototype.syncUpstream = function* (name) {
if (config.sourceNpmRegistry.indexOf('registry.npmjs.org') >= 0 ||
config.sourceNpmRegistry.indexOf('registry.npmjs.com') >= 0 ||
config.sourceNpmRegistry.indexOf('replicate.npmjs.com') >= 0) {
this.log('----------------- upstream is npm registry: %s, ignore it -------------------',
config.sourceNpmRegistry);
return;
}
var syncname = name;
if (this.type === 'user') {
syncname = this.type + ':' + syncname;
@@ -292,19 +295,6 @@ SyncModuleWorker.prototype.next = function* (concurrencyId) {
return setImmediate(this.finish.bind(this));
}
// try to sync from official replicate when source npm registry is not cnpmjs.org
const registry = config.sourceNpmRegistryIsCNpm ? config.sourceNpmRegistry : config.officialNpmReplicate;
const versions = yield this.syncByName(concurrencyId, name, registry);
if (versions && versions.length === 0 && registry === config.officialNpmReplicate) {
// need to sync sourceNpmRegistry also
// make sure package data be update event replicate down.
// https://github.com/npm/registry/issues/129
yield this.syncByName(concurrencyId, name, config.officialNpmRegistry);
}
};
SyncModuleWorker.prototype.syncByName = function* (concurrencyId, name, registry) {
var that = this;
that.syncingNames[name] = true;
var pkg = null;
@@ -312,113 +302,76 @@ SyncModuleWorker.prototype.syncByName = function* (concurrencyId, name, registry
this.log('----------------- Syncing %s -------------------', name);
// ignore private scoped package
if (common.isPrivateScopedPackage(name)) {
this.log('[c#%d] [%s] ignore sync private scoped %j package',
concurrencyId, name, config.scopes);
yield this._doneOne(concurrencyId, name, true);
// ignore scoped package
if (name[0] === '@') {
this.log('[c#%d] [%s] ignore sync scoped package',
concurrencyId, name);
yield* this._doneOne(concurrencyId, name, true);
return;
}
// get from npm
const packageUrl = '/' + name.replace('/', '%2f');
try {
var result = yield npmSerivce.request(packageUrl, { registry: registry });
var result = yield* npmSerivce.request('/' + name);
pkg = result.data;
status = result.status;
} catch (err) {
// if 404
if (!err.res || err.res.statusCode !== 404) {
var errMessage = err.name + ': ' + err.message;
that.log('[c#%s] [error] [%s] get package(%s%s) error: %s, status: %s',
concurrencyId, name, registry, packageUrl, errMessage, status);
yield that._doneOne(concurrencyId, name, false);
that.log('[c#%s] [error] [%s] get package error: %s, status: %s',
concurrencyId, name, errMessage, status);
yield *that._doneOne(concurrencyId, name, false);
return;
}
}
if (status === 404 && pkg && pkg.reason === 'deleted' && registry === config.officialNpmReplicate) {
// unpublished package on replicate.npmjs.com
// 404 { error: 'not_found', reason: 'deleted' }
// try to read from registry.npmjs.com and get the whole unpublished info
try {
var result = yield npmSerivce.request(packageUrl, { registry: config.sourceNpmRegistry });
pkg = result.data;
status = result.status;
} catch (err) {
// if 404
if (!err.res || err.res.statusCode !== 404) {
var errMessage = err.name + ': ' + err.message;
that.log('[c#%s] [error] [%s] get package(%s%s) error: %s, status: %s',
concurrencyId, name, config.sourceNpmRegistry, packageUrl, errMessage, status);
yield that._doneOne(concurrencyId, name, false);
return;
}
}
}
var unpublishedInfo = null;
if (status === 404) {
// check if it's unpublished
// ignore too long package name, see https://github.com/cnpm/cnpmjs.org/issues/1066
if (name.length < 80 && pkg && pkg.time && pkg.time.unpublished && pkg.time.unpublished.time) {
if (pkg.time && pkg.time.unpublished && pkg.time.unpublished.time) {
unpublishedInfo = pkg.time.unpublished;
} else {
pkg = null;
}
} else {
// unpublished package status become to 200
if (name.length < 80 && pkg && pkg.time && pkg.time.unpublished && pkg.time.unpublished.time) {
unpublishedInfo = pkg.time.unpublished;
}
}
if (!pkg) {
that.log('[c#%s] [error] [%s] get package(%s%s) error: package not exists, status: %s',
concurrencyId, name, registry, packageUrl, status);
yield that._doneOne(concurrencyId, name, true);
that.log('[c#%s] [error] [%s] get package error: package not exists, status: %s',
concurrencyId, name, status);
yield* that._doneOne(concurrencyId, name, true);
return;
}
that.log('[c#%d] [%s] package(%s%s) status: %s, dist-tags: %j, time.modified: %s, unpublished: %j, start...',
concurrencyId, name, registry, packageUrl, status,
pkg['dist-tags'], pkg.time && pkg.time.modified,
unpublishedInfo);
that.log('[c#%d] [%s] pkg status: %d, start...', concurrencyId, name, status);
if (unpublishedInfo) {
try {
yield that._unpublished(name, unpublishedInfo);
yield* that._unpublished(name, unpublishedInfo);
} catch (err) {
that.log('[c#%s] [error] [%s] sync error: %s', concurrencyId, name, err.stack);
yield that._doneOne(concurrencyId, name, false);
yield* that._doneOne(concurrencyId, name, false);
return;
}
return yield that._doneOne(concurrencyId, name, true);
return yield* that._doneOne(concurrencyId, name, true);
}
var versions;
try {
versions = yield that._sync(name, pkg);
versions = yield* that._sync(name, pkg);
} catch (err) {
that.log('[c#%s] [error] [%s] sync error: %s', concurrencyId, name, err.stack);
yield that._doneOne(concurrencyId, name, false);
yield* that._doneOne(concurrencyId, name, false);
return;
}
// has new version
if (versions.length > 0) {
that.updates.push(name);
}
this.log('[c#%d] [%s] synced success, %d versions: %s',
concurrencyId, name, versions.length, versions.join(', '));
yield this._doneOne(concurrencyId, name, true);
return versions;
yield* this._doneOne(concurrencyId, name, true);
};
function* _listStarUsers(modName) {
var users = yield packageService.listStarUserNames(modName);
var users = yield* packageService.listStarUserNames(modName);
var userMap = {};
users.forEach(function (user) {
userMap[user] = true;
@@ -427,21 +380,11 @@ function* _listStarUsers(modName) {
}
function* _saveNpmUser(username) {
var user = yield npmSerivce.getUser(username);
var user = yield* npmSerivce.getUser(username);
if (!user) {
var existsUser = yield User.findByName(username);
if (existsUser && existsUser.isNpmUser) {
// delete it
yield User.destroy({
where: {
name: username,
}
});
return { exists: true, deleted: true, isNpmUser: true };
}
return { exists: false };
return;
}
yield User.saveNpmUser(user);
yield* User.saveNpmUser(user);
return user;
}
@@ -664,13 +607,6 @@ SyncModuleWorker.prototype._sync = function* (name, pkg) {
deprecated: version.deprecated
});
}
if (exists.package.deprecated && !version.deprecated) {
// remove deprecated info
missingDeprecateds.push({
id: exists.id,
deprecated: undefined,
});
}
continue;
}
}
@@ -723,7 +659,7 @@ SyncModuleWorker.prototype._sync = function* (name, pkg) {
continue;
}
try {
yield that._syncOneVersion(index, syncModule);
yield* that._syncOneVersion(index, syncModule);
syncedVersionNames.push(syncModule.version);
} catch (err) {
that.log(' [%s:%d] sync error, version: %s, %s: %s',
@@ -731,13 +667,14 @@ SyncModuleWorker.prototype._sync = function* (name, pkg) {
}
}
if (deletedVersionNames.length === 0) {
that.log(' [%s] no versions need to deleted', name);
} else {
that.log(' [%s] %d versions: %j need to deleted',
name, deletedVersionNames.length, deletedVersionNames);
try {
yield packageService.removeModulesByNameAndVersions(name, deletedVersionNames);
yield* packageService.removeModulesByNameAndVersions(name, deletedVersionNames);
} catch (err) {
that.log(' [%s] delete error, %s: %s', name, err.name, err.message);
}
@@ -836,7 +773,7 @@ SyncModuleWorker.prototype._sync = function* (name, pkg) {
if (r.error) {
that.log(' save error, id: %s, error: %s', item.id, r.error.message);
} else {
that.log(' saved, id: %s, deprecated: %j', item.id, item.deprecated);
that.log(' saved, id: %s', item.id);
}
}
}
@@ -932,10 +869,6 @@ SyncModuleWorker.prototype._sync = function* (name, pkg) {
};
SyncModuleWorker.prototype._syncOneVersion = function *(versionIndex, sourcePackage) {
var delay = Date.now() - sourcePackage.publish_time;
logger.syncInfo('[sync_module_worker] delay: %s ms, publish_time: %s, start sync %s@%s',
delay, utility.logDate(new Date(sourcePackage.publish_time)),
sourcePackage.name, sourcePackage.version);
var that = this;
var username = this.username;
var downurl = sourcePackage.dist.tarball;
@@ -959,11 +892,9 @@ SyncModuleWorker.prototype._syncOneVersion = function *(versionIndex, sourcePack
devDependencies = Object.keys(sourcePackage.devDependencies || {});
}
that.log(' [%s:%d] syncing, delay: %s ms, version: %s, dist: %j, no deps: %s, ' +
that.log(' [%s:%d] syncing, version: %s, dist: %j, no deps: %s, ' +
'publish on cnpm: %s, dependencies: %d, devDependencies: %d, syncDevDependencies: %s',
sourcePackage.name, versionIndex,
delay,
sourcePackage.version,
sourcePackage.name, versionIndex, sourcePackage.version,
sourcePackage.dist, that.noDep, that._publish,
dependencies.length,
devDependencies.length, this.syncDevDependencies);
@@ -987,22 +918,14 @@ SyncModuleWorker.prototype._syncOneVersion = function *(versionIndex, sourcePack
}
// add module dependence
yield packageService.addDependencies(sourcePackage.name, dependencies);
yield* packageService.addDependencies(sourcePackage.name, dependencies);
var shasum = crypto.createHash('sha1');
var dataSize = 0;
try {
// get tarball
logger.syncInfo('[sync_module_worker] downloading %j to %j', downurl, filepath);
var r;
try {
r = yield urllib.request(downurl, options);
} catch (err) {
logger.syncInfo('[sync_module_worker] download %j to %j error: %s', downurl, filepath, err);
throw err;
}
var r = yield urllib.request(downurl, options);
var statusCode = r.status || -1;
// https://github.com/cnpm/cnpmjs.org/issues/325
// if (statusCode === 404) {
@@ -1016,7 +939,6 @@ SyncModuleWorker.prototype._syncOneVersion = function *(versionIndex, sourcePack
var err = new Error('Download ' + downurl + ' fail, status: ' + statusCode);
err.name = 'DownloadTarballError';
err.data = sourcePackage;
logger.syncInfo('[sync_module_worker] %s', err.message);
throw err;
}
@@ -1033,7 +955,6 @@ SyncModuleWorker.prototype._syncOneVersion = function *(versionIndex, sourcePack
var err = new Error('Download ' + downurl + ' file size is zero');
err.name = 'DownloadTarballSizeZeroError';
err.data = sourcePackage;
logger.syncInfo('[sync_module_worker] %s', err.message);
throw err;
}
@@ -1044,7 +965,6 @@ SyncModuleWorker.prototype._syncOneVersion = function *(versionIndex, sourcePack
' not match ' + sourcePackage.dist.shasum);
err.name = 'DownloadTarballShasumError';
err.data = sourcePackage;
logger.syncInfo('[sync_module_worker] %s', err.message);
throw err;
}
@@ -1055,18 +975,8 @@ SyncModuleWorker.prototype._syncOneVersion = function *(versionIndex, sourcePack
};
// upload to NFS
logger.syncInfo('[sync_module_worker] uploading %j to nfs', options);
var result;
try {
result = yield nfs.upload(filepath, options);
} catch (err) {
logger.syncInfo('[sync_module_worker] upload %j to nfs error: %s', err);
throw err;
}
logger.syncInfo('[sync_module_worker] uploaded, saving %j to database', result);
var r = yield afterUpload(result);
logger.syncInfo('[sync_module_worker] sync %s@%s done!',
sourcePackage.name, sourcePackage.version);
return r;
var result = yield nfs.upload(filepath, options);
return yield *afterUpload(result);
} finally {
// remove tmp file whatever
fs.unlink(filepath, utility.noop);
@@ -1109,7 +1019,7 @@ SyncModuleWorker.prototype._syncOneVersion = function *(versionIndex, sourcePack
}
mod.package.dist = dist;
var r = yield packageService.saveModule(mod);
var r = yield* packageService.saveModule(mod);
that.log(' [%s:%s] done, insertId: %s, author: %s, version: %s, '
+ 'size: %d, publish_time: %j, publish on cnpm: %s',
@@ -1125,7 +1035,7 @@ SyncModuleWorker.prototype._syncOneVersion = function *(versionIndex, sourcePack
SyncModuleWorker.sync = function* (name, username, options) {
options = options || {};
var result = yield logService.create({name: name, username: username});
var result = yield* logService.create({name: name, username: username});
var worker = new SyncModuleWorker({
logId: result.id,
type: options.type,

View File

@@ -1,23 +1,31 @@
/**!
* cnpmjs.org - controllers/total.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/
'use strict';
const Total = require('../services/total');
const version = require('../package.json').version;
const config = require('../config');
const getDownloadTotal = require('./utils').getDownloadTotal;
/**
* Module dependencies.
*/
const startTime = '' + Date.now();
let cache = null;
var Total = require('../services/total');
var version = require('../package.json').version;
var config = require('../config');
var getDownloadTotal = require('./utils').getDownloadTotal;
var startTime = '' + Date.now();
module.exports = function* showTotal() {
if (cache && Date.now() - cache.cache_time < 10000) {
// cache 10 seconds
this.body = cache;
return;
}
const r = yield [Total.get(), getDownloadTotal()];
const total = r[0];
const download = r[1];
var r = yield [Total.get(), getDownloadTotal()];
var total = r[0];
var download = r[1];
total.download = download;
total.db_name = 'registry';
@@ -27,8 +35,5 @@ module.exports = function* showTotal() {
total.donate = 'https://www.gittip.com/fengmk2';
total.sync_model = config.syncModel;
cache = total;
total.cache_time = Date.now();
this.body = total;
};

View File

@@ -56,11 +56,11 @@ exports.getDownloadTotal = function* (name) {
var end = moment();
var start = end.clone().subtract(1, 'months').startOf('month');
var lastday = end.clone().subtract(1, 'days').format('YYYY-MM-DD');
var lastweekStart = end.clone().subtract(1, 'weeks').startOf('isoweek');
var lastweekEnd = lastweekStart.clone().endOf('isoweek').format('YYYY-MM-DD');
var lastweekStart = end.clone().subtract(1, 'weeks').startOf('week');
var lastweekEnd = lastweekStart.clone().endOf('week').format('YYYY-MM-DD');
var lastmonthEnd = start.clone().endOf('month').format('YYYY-MM-DD');
var thismonthStart = end.clone().startOf('month').format('YYYY-MM-DD');
var thisweekStart = end.clone().startOf('isoweek').format('YYYY-MM-DD');
var thisweekStart = end.clone().startOf('week').format('YYYY-MM-DD');
start = start.format('YYYY-MM-DD');
end = end.format('YYYY-MM-DD');
lastweekStart = lastweekStart.format('YYYY-MM-DD');

View File

@@ -5,7 +5,7 @@
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
@@ -18,7 +18,6 @@ var utility = require('utility');
var util = require('util');
var config = require('../../config');
var packageService = require('../../services/package');
var DownloadTotal = require('../../services/download_total');
exports.version = function* () {
var color = 'lightgrey';
@@ -41,22 +40,9 @@ exports.version = function* () {
}
var subject = config.badgeSubject.replace(/\-/g, '--');
if (this.query.subject) {
subject = this.query.subject.replace(/\-/g, '--');
}
version = version.replace(/\-/g, '--');
var style = this.query.style || 'flat-square';
var url = util.format(config.badgePrefixURL + '/%s-%s-%s.svg?style=%s',
utility.encodeURIComponent(subject), version, color, utility.encodeURIComponent(style));
this.redirect(url);
};
exports.downloads = function* () {
// https://dn-img-shields-io.qbox.me/badge/downloads-100k/month-brightgreen.svg?style=flat-square
var name = this.params[0];
var count = yield DownloadTotal.getTotalByName(name);
var style = this.query.style || 'flat-square';
var url = util.format(config.badgePrefixURL + '/downloads-%s-brightgreen.svg?style=%s',
count, utility.encodeURIComponent(style));
var url = util.format('https://img.shields.io/badge/%s-%s-%s.svg?style=%s',
subject, version, color, utility.encodeURIComponent(style));
this.redirect(url);
};

21
controllers/web/home.js Normal file
View File

@@ -0,0 +1,21 @@
/**!
* cnpmjs.org - controllers/web/home.js
*
* Copyright(c) cnpm and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/
'use strict';
/**
* Module dependencies.
*/
module.exports = function* () {
yield this.render('home', {
title: 'cnpmjs.org: Private npm registry and web for Company'
});
};

View File

@@ -6,7 +6,7 @@
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
@@ -21,16 +21,8 @@ var packageService = require('../../../services/package');
module.exports = function* search() {
var params = this.params;
var word = params.word || params[0];
var limit = Number(this.query.limit) || 100;
if (limit > 10000) {
limit = 10000;
}
debug('search %j', word);
var result = yield* packageService.search(word, {
limit: limit
});
var result = yield* packageService.search(word);
var match = null;
for (var i = 0; i < result.searchMatchs.length; i++) {
@@ -43,12 +35,13 @@ module.exports = function* search() {
// return a json result
if (this.query && this.query.type === 'json') {
this.jsonp = {
this.body = {
keyword: word,
match: match,
packages: result.searchMatchs,
keywords: result.keywordMatchs,
};
this.type = 'application/json; charset=utf-8';
return;
}
yield this.render('search', {

View File

@@ -1,5 +1,20 @@
/**!
* cnpmjs.org - controllers/web/package/show.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
/**
* Module dependencies.
*/
var debug = require('debug')('cnpmjs.org:controllers:web:package:show');
var bytes = require('bytes');
var giturl = require('giturl');
@@ -53,8 +68,7 @@ module.exports = function* show(next) {
}
}
yield this.render('package_unpublished', {
package: data,
title: 'Package - ' + name
package: data
});
return;
}
@@ -110,7 +124,7 @@ module.exports = function* show(next) {
pkg.repository = null;
}
if (pkg.repository && pkg.repository.url) {
pkg.repository.weburl = /^https?:\/\//.test(pkg.repository.url) ? pkg.repository.url : (giturl.parse(pkg.repository.url) || pkg.repository.url);
pkg.repository.weburl = giturl.parse(pkg.repository.url) || pkg.repository.url;
}
if (!pkg.bugs) {
pkg.bugs = {};
@@ -164,23 +178,12 @@ module.exports = function* show(next) {
pkg.engines[k] = {
version: engine,
title: k + ': ' + engine,
badgeURL: config.badgePrefixURL + '/' + encodeURIComponent(k) +
badgeURL: 'https://img.shields.io/badge/' + encodeURIComponent(k) +
'-' + encodeURIComponent(engine) + '-' + color + '.svg?style=flat-square',
};
}
}
if (pkg._publish_on_cnpm) {
pkg.isPrivate = true;
} else {
pkg.isPrivate = false;
// add security check badge
pkg.snyk = {
badge: `${config.snykUrl}/test/npm/${pkg.name}/badge.svg?style=flat-square`,
url: `${config.snykUrl}/test/npm/${pkg.name}`,
};
}
yield this.render('package', {
title: 'Package - ' + pkg.name,
package: pkg,

View File

@@ -37,8 +37,7 @@ module.exports = function* showUser(next) {
var data = {
name: name,
email: user.email,
json: user.json || {},
isNpmUser: user.isNpmUser,
json: user.json || {}
};
if (data.json.login) {

View File

@@ -1,5 +1,20 @@
/**!
* cnpmjs.org - dispatch.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com>
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
/**
* Module dependencies.
*/
var childProcess = require('child_process');
var path = require('path');
var util = require('util');

View File

@@ -18,7 +18,6 @@ CREATE TABLE IF NOT EXISTS `user` (
-- ALTER TABLE `user`
-- ADD `json` longtext CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT 'json details',
-- ADD `npm_user` tinyint(1) DEFAULT '0' COMMENT 'user sync from npm or not, 1: true, other: false';
-- ALTER TABLE `user` CHANGE `json` `json` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT 'json details';
CREATE TABLE IF NOT EXISTS `module_keyword` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',

Binary file not shown.

Before

Width:  |  Height:  |  Size: 123 KiB

View File

@@ -1,98 +0,0 @@
@startuml
title cnpmjs.org, npm.taobao.org Network
node "China User" {
[cnpm cli]
}
node "OSS: aliyuncs.com" {
[tnpm-hz.oss-cn-hangzhou]
}
node "SLB: 114.55.80.225 Hangzhou" {
[npm.taobao.org]
[registry.npm.taobao.org]
}
node "npmtaobao3: 121.41.*" {
[nginx 5]
[registry 5]
[web 5]
[syncer 5]
}
node "npmtaobao4: 120.26.*" {
[nginx 6]
[registry 6]
[web 6]
}
node "cnpm6: 47.88.189.193 Singapore" {
[cnpmjs.org]
[r.cnpmjs.org]
[nginx 7]
[registry 7]
[web 7]
[syncer 7]
}
node "Aliyun CDN" {
[cdn.npm.taobao.org]
}
node "qiniu CDN" {
[cnpmjs.up.qiniu.com]
[dn-cnpm.qbox.me]
}
database "taobaonpm DB" {
[rds2860*.mysql.rds.aliyuncs.com]
}
database "cnpm DB" {
[rdsqiz*.mysql.rds.aliyuncs.com]
}
node "npmjs.com" {
[registry.npmjs.com]
[www.npmjs.com]
}
[cnpm cli] -down-> [registry.npm.taobao.org]: Read, Write
[cnpm cli] -down-> [cdn.npm.taobao.org]: Read tgz
[cnpm cli] -down-> [npm.taobao.org]: "Read /mirrors"
[registry.npm.taobao.org] -down-> [nginx 5]
[nginx 5] -down-> [registry 5]
[npm.taobao.org] -down-> [nginx 5]
[nginx 5] -down-> [web 5]
[registry.npm.taobao.org] -down-> [nginx 6]
[nginx 6] -down-> [registry 6]
[npm.taobao.org] -down-> [nginx 6]
[nginx 6] -down-> [web 6]
[registry 5] -down-> [rds2860*.mysql.rds.aliyuncs.com]: Read, Write
[web 5] -down-> [rds2860*.mysql.rds.aliyuncs.com]: Read
[syncer 5] -down-> [rds2860*.mysql.rds.aliyuncs.com]: Read, Write
[syncer 5] -down-> [tnpm-hz.oss-cn-hangzhou]: Write
[syncer 5] -> [r.cnpmjs.org]: Read
[syncer 5] -> [dn-cnpm.qbox.me]: Read tgz
[registry 6] -down-> [rds2860*.mysql.rds.aliyuncs.com]: Read, Write
[web 6] -down-> [rds2860*.mysql.rds.aliyuncs.com]: Read
[cnpmjs.org] -down-> [nginx 7]
[nginx 7] -down-> [registry 7]
[r.cnpmjs.org] -down-> [nginx 7]
[nginx 7] -down-> [web 7]
[registry 7] -down-> [rdsqiz*.mysql.rds.aliyuncs.com]: Read, Write
[web 7] -down-> [rdsqiz*.mysql.rds.aliyuncs.com]: Read
[syncer 7] -down-> [rdsqiz*.mysql.rds.aliyuncs.com]: Read, Write
[syncer 7] -down-> [cnpmjs.up.qiniu.com]: Write
[syncer 7] -down-> [registry.npmjs.com]: Read
[cdn.npm.taobao.org] -down-> [tnpm-hz.oss-cn-hangzhou]: Read
[dn-cnpm.qbox.me] -down-> [cnpmjs.up.qiniu.com]: Read
@enduml

View File

@@ -9,8 +9,6 @@
* [User](/docs/registry-api.md#user)
* [Search](/docs/registry-api.md#search)
[![Run in Postman](https://run.pstmn.io/button.svg)](https://app.getpostman.com/run-collection/f6c8cb46358039bcd689#?env%5BRegistry%5D=W3sia2V5IjoicmVnaXN0cnkiLCJ0eXBlIjoidGV4dCIsInZhbHVlIjoiaHR0cHM6Ly9yZWdpc3RyeS5ucG0udGFvYmFvLm9yZyIsImVuYWJsZWQiOnRydWV9LHsia2V5IjoicGFja2FnZSIsInZhbHVlIjoiY25wbSIsInR5cGUiOiJ0ZXh0IiwiZW5hYmxlZCI6dHJ1ZX1d)
## Schema
All API access is over HTTPS or HTTP,

115
docs/web/install.md Normal file
View File

@@ -0,0 +1,115 @@
# Install & Get Started
## Deps
* MySQL Server: http://db4free.net/
* qiniu CDN: http://www.qiniu.com/
* redis session: https://garantiadata.com Support 24MB free spaces.
* node: >=0.10.21
## Clone
```bash
$ git clone git://github.com/fengmk2/cnpmjs.org.git $HOME/cnpmjs.org
$ cd $HOME/cnpmjs.org
```
## Create your `config.js`
```bash
$ vim config/config.js
```
`config.js` content sample:
```js
module.exports = {
debug: false,
enableCluster: true, // enable cluster mode
logdir: 'your application log dir',
mysqlServers: [
{
host: 'your mysql host',
port: 3306,
user: 'yourname',
password: 'your password'
}
],
mysqlDatabase: 'cnpmjs',
redis: {
host: 'your redist host',
port: 6379,
},
qn: {
accessKey: "your qiniu appkey",
secretKey: "your secret key",
bucket: "foobucket",
domain: "http://foobucket.u.qiniudn.com"
},
nfs: null, // you can set a nfs to replace qiniu cdn
enablePrivate: true, // enable private mode, only admin can publish, other use just can sync package from source npm
admins: {
admin: 'admin@cnpmjs.org',
},
syncModel: 'exist', //`all` sync all packages, `exist` only update exist packages, `none` do nothing
};
```
## Create MySQL Database and Tables
```bash
$ mysql -u yourname -p
mysql> use cnpmjs;
mysql> source docs/db.sql
```
## Use your own CDN
If you wan to use your own CDN instead of qiniu. Just look at `common/qnfs.js` and implement the interface like it, then pass it by set `config.nfs`.
## npm install
```bash
$ npm install
```
## start
```bash
$ ./bin/nodejsctl start
Starting cnpmjs.org ...
Start nodejs success. PID=27175
```
## open registry and web
```bash
# registry
$ open http://localhost:7001
# web
$ open http://localhost:7002
```
## use cnpm cli with your own registry
You do not need to write another command line tool with your own registry,
just alias [cnpm](http://github.com/fengmk2/cnpm), then you can get a npm client for you own registry.
```
# install cnpm first
npm install -g cnpm
# then alias lnpm to cnpm, but change config to your own registry
alias lnpm='cnpm --registry=http://localhost:7001\
--registryweb=http://localhost:7002\
--cache=$HOME/.npm/.cache/lnpm\
--disturl=http://cnpmjs.org/dist\
--userconfig=$HOME/.lnpmrc'
#or put this in .zshrc or .bashrc
echo "#lnpm alias\nalias lnpm='cnpm --registry=http://localhost:7001\
--registryweb=http://localhost:7002\
--cache=$HOME/.npm/.cache/lnpm\
--disturl=http://cnpmjs.org/dist\
--userconfig=$HOME/.lnpmrc'" >> $HOME/.zshrc && source $HOME/.zshrc
```

View File

@@ -1,155 +0,0 @@
# cnpmjs.org: Private npm registry and web for Company
So `cnpm` is meaning: **Company npm**.
## Registry
- Our public registry: [r.cnpmjs.org](https://r.cnpmjs.org), syncing from [registry.npmjs.com](https://registry.npmjs.com)
- [cnpmjs.org](/) version: <span id="app-version"></span>
- [Node.js](https://nodejs.org) version: <span id="node-version"></span>
- For developers in China, please visit [the China mirror](https://npm.taobao.org). 中国用户请访问[国内镜像站点](https://npm.taobao.org)。
<div class="ant-table">
<table class="downloads">
<tbody>
<tr>
<td class="count" id="total-packages"></td><td>total packages</td>
<td class="count" id="total-versions"></td><td>total package versions</td>
<td class="count" id="total-deletes"></td><td>total delete packages</td>
</tr>
<tr>
<td class="count"></td><td> downloads today</td>
<td class="count"></td><td> downloads in this week</td>
<td class="count"></td><td> downloads in this month</td>
</tr>
<tr>
<td class="count"></td><td> downloads in the last day</td>
<td class="count"></td><td> downloads in the last week</td>
<td class="count"></td><td> downloads in the last month</td>
</tr>
</tbody>
</table>
</div>
<div class="sync" style="display:none;">
<h3>Sync Status</h3>
<p id="sync-model"></p>
<p>Last sync time is <span id="last-sync-time"></span>. </p>
<div class="ant-alert ant-alert-info syncing">
<span class="anticon ant-alert-icon anticon-info-circle"></span>
<span class="ant-alert-description">The sync worker is working in the backend now. </span>
</div>
<div class="ant-table">
<table class="sync-status">
<tbody>
<tr>
<td><span id="need-sync"></span> packages need to be sync</td>
<td class="syncing"><span id="left-sync"></span> packages and dependencies waiting for sync</td>
<td><span id="percent-sync"></span>% progress</td>
</tr>
<tr>
<td><span id="success-sync"></span> packages and dependencies sync successed</td>
<td><span id="fail-sync"></span> packages and dependencies sync failed</td>
<td>last success: <span id="last-success-name"></span></td>
</tr>
</tbody>
</table>
</div>
</div>
<script src="/js/readme.js"></script>
## Badges
Default style is `flat-square`.
### Version
Badge URL: `https://cnpmjs.org/badge/v/cnpmjs.org.svg` ![cnpmjs.org-version-badge](//cnpmjs.org/badge/v/cnpmjs.org.svg)
* `<0.1.0 & >=0.0.0`: ![red-badge](https://img.shields.io/badge/cnpm-0.0.1-red.svg?style=flat-square)
* `<1.0.0 & >=0.1.0`: ![red-badge](https://img.shields.io/badge/cnpm-0.1.0-green.svg?style=flat-square)
* `>=1.0.0`: ![red-badge](https://img.shields.io/badge/cnpm-1.0.0-blue.svg?style=flat-square)
### Downloads
Badge URL: `https://cnpmjs.org/badge/d/cnpmjs.org.svg` ![cnpmjs.org-download-badge](//cnpmjs.org/badge/d/cnpmjs.org.svg)
## Usage
use our npm client [cnpm](https://github.com/cnpm/cnpm)(More suitable with cnpmjs.org and gzip support), you can get our client through npm:
```bash
$ npm install -g cnpm --registry=https://registry.npm.taobao.org
```
Or you can alias NPM to use it:
```bash
alias cnpm="npm --registry=https://registry.npm.taobao.org \
--cache=$HOME/.npm/.cache/cnpm \
--disturl=https://npm.taobao.org/mirrors/node \
--userconfig=$HOME/.cnpmrc"
#Or alias it in .bashrc or .zshrc
$ echo '\n#alias for cnpm\nalias cnpm="npm --registry=https://registry.npm.taobao.org \
--cache=$HOME/.npm/.cache/cnpm \
--disturl=https://npm.taobao.org/mirrors/node \
--userconfig=$HOME/.cnpmrc"' >> ~/.zshrc && source ~/.zshrc
```
### install
Install package from [r.cnpmjs.org](//r.cnpmjs.org). When installing a package or version does not exist, it will try to install from the official registry([registry.npmjs.org](https://registry.npmjs.org)), and sync this package to cnpm in the backend.
```bash
$ cnpm install [name]
```
### sync
Only `cnpm` cli has this command. Meaning sync package from source npm.
```bash
$ cnpm sync connect
```
sync package on web: [sync/connect](/sync/connect)
```bash
$ open http://registry.npm.taobao.org/sync/connect
```
### publish / unpublish
Only `admin` user can publish / unpublish package to private registry.
```bash
$ cnpm publish [name]
$ cnpm unpublish [name]
```
### Other commands
Support all the other npm commands. e.g.:
```bash
$ cnpm info cnpm
```
## TODO list
@see Github [Issues](https://github.com/cnpm/cnpmjs.org/issues)
## Histories
Release [History](/history).
## npmjs.org, cnpmjs.org and npm.taobao.org relation
![npm&cnpm](https://cloud.githubusercontent.com/assets/543405/21505401/fd0b6220-cca1-11e6-86ed-599cc81bb03b.png)
## Sponsors
- [![阿里云](https://static.aliyun.com/images/www-summerwind/logo.gif)](http://click.aliyun.com/m/4288/) (2016.2 - now)
- [![UCloud云计算](https://www.ucloud.cn/static/style/images/about/logo.png)](http://www.ucloud.cn?sem=sdk-CNPMJS) (2015.3 - 2016.3)

View File

@@ -1,4 +1,6 @@
/**!
* cnpmjs.org - index.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*

View File

@@ -27,17 +27,12 @@ exports.getTarballFilepath = function (filename) {
};
exports.getCDNKey = function (name, filename) {
// if name is scope package name, need to auto fix filename as a scope package file name
// e.g.: @scope/foo, filename: foo-1.0.0.tgz => filename: @scope/foo-1.0.0.tgz
if (name[0] === '@' && filename[0] !== '@') {
filename = name.split('/')[0] + '/' + filename;
}
return '/' + name + '/-/' + filename;
};
exports.setDownloadURL = function (pkg, ctx, host) {
if (pkg.dist) {
host = host || config.registryHost || ctx.host;
host = host || ctx.host;
pkg.dist.tarball = util.format('%s://%s/%s/download/%s-%s.tgz',
ctx.protocol,
host, pkg.name, pkg.name, pkg.version);
@@ -71,10 +66,3 @@ exports.isLocalModule = function (mods) {
}
return false;
};
exports.isPrivateScopedPackage = function (name) {
if (name[0] !== '@') {
return false;
}
return config.scopes.indexOf(name.split('/')[0]) >= 0;
};

3
locales/en_US.json Normal file
View File

@@ -0,0 +1,3 @@
{
"cnpmjs.org: Private npm registry and web for Company": "cnpmjs.org: Private npm registry and web for Company"
}

3
locales/zh_CN.json Normal file
View File

@@ -0,0 +1,3 @@
{
"cnpmjs.org: Private npm registry and web for Company": "cnpmjs.org: 企业级私有 npm 仓库"
}

View File

@@ -34,14 +34,13 @@ module.exports = function () {
return yield* unauthorized.call(this, next);
}
authorization = new Buffer(authorization, 'base64').toString();
var pos = authorization.indexOf(':');
if (pos === -1) {
return yield* unauthorized.call(this, next);
authorization = new Buffer(authorization, 'base64').toString().split(':');
if (authorization.length !== 2) {
return yield* unauthorized.call(this, next);
}
var username = authorization.slice(0, pos);
var password = authorization.slice(pos + 1);
var username = authorization[0];
var password = authorization[1];
var row;
try {

View File

@@ -1,9 +1,11 @@
/**!
* cnpmjs.org - middleware/exists_package.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';

26
middleware/i18n.js Normal file
View File

@@ -0,0 +1,26 @@
/**!
* cnpmjs.org - middleware/i18n.js
*
* Copyright(c) cnpm and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/
'use strict';
/**
* Module dependencies.
*/
var locales = require('koa-locales');
module.exports = function (options, app) {
locales(app, options);
return function* i18n(next) {
this.state.__ = this.__.bind(this);
yield* next;
};
};

View File

@@ -22,7 +22,7 @@ var template = '<?xml version="1.0" encoding="UTF-8"?>\
<Url method="get" type="text/html" template="http://${host}/browse/keyword/{searchTerms}"/>\
</OpenSearchDescription>';
module.exports = function *opensearch(next) {
module.exports = function *publishable(next) {
if (this.path === '/opensearch.xml') {
this.type = 'text/xml';
this.charset = 'utf-8';

View File

@@ -1,3 +1,12 @@
/**!
* cnpmjs.org - middleware/proxy_to_npm.js
*
* Copyright(c) Alibaba Group Holding Limited.
*
* Authors:
* 苏千 <suqian.yf@alipay.com> (http://fengmk2.com)
*/
'use strict';
/**
@@ -24,11 +33,11 @@ module.exports = function (options) {
}
return function* proxyToNpm(next) {
if (config.syncModel !== 'none') {
return yield next;
return yield* next;
}
// only proxy read requests
if (this.method !== 'GET' && this.method !== 'HEAD') {
return yield next;
return yield* next;
}
var pathname = this.path;
@@ -40,7 +49,7 @@ module.exports = function (options) {
}
}
if (!match) {
return yield next;
return yield* next;
}
var url = redirectUrl + this.url;

View File

@@ -19,13 +19,8 @@ var config = require('../config');
var debug = require('debug')('cnpmjs.org:middlewares/publishable');
module.exports = function *publishable(next) {
// admins always can publish and unpublish
if (this.user.isAdmin) {
return yield next;
}
// private mode, normal user can't publish and unpublish
if (config.enablePrivate) {
// private mode, only admin user can publish
if (config.enablePrivate && !this.user.isAdmin) {
this.status = 403;
this.body = {
error: 'no_perms',
@@ -34,26 +29,28 @@ module.exports = function *publishable(next) {
return;
}
// public mode, normal user have permission to publish `scoped package`
// 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];
// check if is private package list in config
if (config.privatePackages && config.privatePackages.indexOf(name) !== -1) {
return yield next;
return yield* next;
}
// scoped module
// scope
if (name[0] === '@') {
if (checkScope(name, this)) {
return yield next;
return yield* next;
}
return;
}
// none-scope
assertNoneScope(name, this);
if (checkNoneScope(name, this)) {
return yield* next;
}
};
/**
@@ -84,7 +81,12 @@ function checkScope(name, ctx) {
* check if user have permission to publish without scope
*/
function assertNoneScope(name, ctx) {
function checkNoneScope(name, ctx) {
// admins unpublished everything
if (ctx.user.isAdmin && ctx.method === 'DELETE') {
return true;
}
ctx.status = 403;
if (ctx.user.scopes.length === 0) {
ctx.body = {

View File

@@ -23,8 +23,7 @@ var staticDir = path.join(path.dirname(__dirname), 'public');
module.exports = middlewares.staticCache(staticDir, {
buffer: config.debug ? false : true,
maxAge: config.debug ? 0 : 60 * 60 * 24 * 7,
alias: {
alas: {
'/favicon.ico': '/favicon.png'
},
gzip: config.enableCompress,
}
});

View File

@@ -1,5 +1,7 @@
/**
* Copyright(c) cnpm and other contributors.
/**!
* cnpmjs.org - middleware/sync_by_install.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
@@ -15,36 +17,33 @@
var config = require('../config');
/**
* {Boolean} this.allowSync - allow sync triggle by cnpm install
* this.allowSync - allow sync triggle by cnpm install
*/
module.exports = function* syncByInstall(next) {
this.allowSync = false;
if (!config.syncByInstall) {
// only config.enablePrivate should enable sync on install
return yield next;
return yield* next;
}
// request not by node, consider it request from web, don't sync
// request not by node, consider it request from web, dont sync
var ua = this.get('user-agent');
if (!ua || ua.indexOf('node') < 0) {
return yield next;
return yield* next;
}
// if request with `/xxx?write=true`, meaning the read request using for write, don't sync
// if request with `/xxx?write=true`, meaning the read request using for write, dont sync
if (this.query.write) {
return yield next;
return yield* next;
}
var name = this.params.name || this.params[0];
// private scoped package don't sync
// scoped package dont sync
if (name && name[0] === '@') {
var scope = name.split('/')[0];
if (config.scopes.indexOf(scope) >= 0) {
return yield next;
}
return yield* next;
}
this.allowSync = true;
yield next;
yield* next;
};

View File

@@ -1,24 +0,0 @@
/**!
* cnpmjs.org - middleware/unpublishable.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com>
*/
'use strict';
module.exports = function *unpublishable(next) {
// only admin user can unpublish
if (!this.user.isAdmin) {
this.status = 403;
this.body = {
error: 'no_perms',
reason: 'Only administrators can unpublish module',
};
return;
}
yield next;
};

View File

@@ -1,9 +1,11 @@
/**!
* cnpmjs.org - models/index.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
@@ -13,6 +15,7 @@
*/
var path = require('path');
var Sequelize = require('sequelize');
var sequelize = require('../common/sequelize');
function load(name) {
@@ -37,7 +40,7 @@ module.exports = {
query: function* (sql, args) {
var options = { replacements: args };
var data = yield this.sequelize.query(sql, options);
var data = yield this.sequelize.query(sql, options).spread();
if (/select /i.test(sql)) {
return data[0];
}

View File

@@ -35,10 +35,7 @@ if (username) {
var models = require('./');
models.sequelize.sync({
force: force,
logging: console.log,
})
models.sequelize.sync({ force: force })
.then(function () {
models.Total.init(function (err) {
if (err) {

View File

@@ -70,7 +70,7 @@ module.exports = function (sequelize, DataTypes) {
});
if (row) {
row.package = pkg;
if (row.changed()) {
if (row.isDirty) {
row = yield row.save(['package']);
}
return row;

View File

@@ -155,7 +155,7 @@ module.exports = function (sequelize, DataTypes) {
user.json = data;
user.email = data.email || '';
user.rev = data._rev || '';
if (user.changed()) {
if (user.isDirty) {
user = yield user.save();
}
return user;
@@ -182,7 +182,7 @@ module.exports = function (sequelize, DataTypes) {
user.rev = rev;
user.salt = salt;
user.password_sha = passwordSha;
if (user.changed()) {
if (user.isDirty) {
user = yield user.save();
}
return user;

View File

@@ -1,11 +1,10 @@
{
"name": "cnpmjs.org",
"version": "2.19.4",
"version": "2.0.0",
"description": "Private npm registry and web for Enterprise, base on MySQL and Simple Store Service",
"main": "index.js",
"scripts": {
"dev": "DEBUG=cnpm* node dispatch.js",
"test": "make jshint && make test",
"test": "make install && make jshint && make test",
"start": "./bin/nodejsctl start && cp History.md docs/web/history.md",
"status": "./bin/nodejsctl status",
"stop": "./bin/nodejsctl stop"
@@ -14,68 +13,65 @@
"cnpmjs.org": "bin/cli.js"
},
"dependencies": {
"agentkeepalive": "^2.2.0",
"await-event": "^1.0.0",
"bytes": "^2.4.0",
"cfork": "^1.5.1",
"changes-stream": "^1.1.0",
"co": "^4.6.0",
"co-defer": "^1.0.0",
"co-gather": "^0.0.1",
"co-sleep": "^0.0.1",
"commander": "^2.9.0",
"copy-to": "^2.0.1",
"debug": "^2.2.0",
"error-formater": "^1.0.3",
"fs-cnpm": "^1.2.0",
"giturl": "^1.0.0",
"graceful": "^1.0.0",
"gravatar": "^1.5.0",
"humanize-ms": "^1.2.0",
"humanize-number": "^0.0.2",
"is-type-of": "^1.0.0",
"kcors": "^1.2.1",
"koa": "^1.2.0",
"koa-limit": "^1.0.2",
"koa-markdown": "^2.0.1",
"koa-maxrequests": "^1.0.0",
"koa-middlewares": "^2.1.0",
"koa-mock": "^1.6.1",
"koa-safe-jsonp": "^0.3.1",
"markdown-it": "^3.0.6",
"mime": "^1.3.4",
"mini-logger": "^1.1.1",
"mkdirp": "^0.5.0",
"moment": "^2.12.0",
"mysql": "^2.10.2",
"mz": "^2.4.0",
"nodemailer": "^1.3.0",
"normalize-registry-metadata": "^1.1.2",
"semver": "^5.2.0",
"sequelize": "^3.23.4",
"thunkify-wrap": "^1.0.4",
"treekill": "^1.0.0",
"agentkeepalive": "~1.2.0",
"bluebird": "~2.9.7",
"bytes": "~1.0.0",
"cfork": "~1.2.2",
"co": "~4.3.1",
"co-defer": "~1.0.0",
"co-gather": "~0.0.1",
"co-sleep": "~0.0.1",
"commander": "~2.6.0",
"copy-to": "~2.0.1",
"debug": "~2.1.1",
"error-formater": "~1.0.3",
"fs-cnpm": "~1.2.0",
"giturl": "~0.0.3",
"gnode": "~0.1.1",
"graceful": "~1.0.0",
"gravatar": "~1.1.0",
"humanize-ms": "~1.0.1",
"humanize-number": "~0.0.2",
"kcors": "~1.0.1",
"koa": "~0.17.0",
"koa-limit": "~1.0.2",
"koa-locales": "~1.0.2",
"koa-markdown": "~2.0.1",
"koa-middlewares": "~3.1.0",
"koa-mock": "~1.1.4",
"markdown-it": "~3.0.6",
"mime": "~1.3.4",
"mini-logger": "~1.0.0",
"mkdirp": "~0.5.0",
"moment": "~2.9.0",
"mysql": "~2.5.4",
"nodemailer": "~1.3.0",
"semver": "~4.2.0",
"sequelize": "~3.0.0",
"thunkify-wrap": "~1.0.4",
"treekill": "~1.0.0",
"tunnel-agent": "^0.4.0",
"urllib": "^2.11.0",
"utility": "^1.8.0",
"xss": "^0.2.13"
"urllib": "~2.2.2",
"utility": "~1.3.0",
"xss": "~0.1.20"
},
"devDependencies": {
"autod": "*",
"chunkstream": "*",
"co-mocha": "*",
"contributors": "*",
"istanbul": "*",
"istanbul-harmony": "*",
"jshint": "*",
"mm": "*",
"mocha": "*",
"node-dev": "*",
"pedding": "*",
"pg": "5",
"pg-hstore": "2",
"should": "8",
"pg": "~4.2.0",
"pg-hstore": "~2.3.1",
"should": "~4.0.4",
"should-http": "*",
"sqlite3": "*",
"supertest": "2",
"thunk-mocha": "1"
"supertest": "*"
},
"homepage": "https://github.com/cnpm/cnpmjs.org",
"repository": {
@@ -85,7 +81,7 @@
},
"bugs": {
"url": "https://github.com/cnpm/cnpmjs.org/issues",
"email": "fengmk2@gmail.com"
"email": "m@fengmk2.com"
},
"keywords": [
"cnpmjs.org",
@@ -95,10 +91,10 @@
"registry"
],
"engines": {
"node": ">= 4.3.1"
"node": ">=2.0.2"
},
"author": [
"fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)",
"fengmk2 <m@fengmk2.com> (http://fengmk2.com)",
"dead_horse <dead_horse@qq.com> (http://deadhorse.me)"
],
"license": "MIT"

File diff suppressed because one or more lines are too long

View File

@@ -453,7 +453,6 @@
font-size: 85%;
line-height: 1.45;
background-color: #f7f7f7;
color:#333;
border-radius: 3px;
}

View File

@@ -1,140 +1,28 @@
body {
font-size: 16px;
color: #333333;
}
.mb15 {
margin-bottom: 15px !important;
header {
max-width: 1140px;
min-width: 960px;
margin: 30px auto 30px auto;
}
.container {
min-width: 960px;;
padding-right: 15px;
padding-left: 15px;
margin-right: auto;
margin-left: auto
}
@media (min-width: 768px) {
.container {
width:750px
}
}
@media (min-width: 992px) {
.container {
width:970px
}
}
@media (min-width: 1200px) {
.container {
width:1170px
}
}
hr {
margin-top: 20px;
margin-bottom: 20px;
border: 0;
border-top: 1px solid #eee;
height: 0;
}
#header {
padding: 30px 0;
height: 156px;
}
#header .logo {
header .logo {
display: inline-block;
}
#header .logo a {
display: inline-block;
vertical-align: top;
}
#search-input {
header #search-input {
display: inline-block;
width: 400px;
height: 50px;
font-size: 20px;
font-size: 30px;
float: right;
margin-top: 22px;
border-radius: 8px;
padding: 10px 20px;
transition: width .3s ease-in-out;
}
@media (max-width: 1024px) {
#search-input {
width: 200px;
}
.pack-overview-background {
min-width: 960px;
}
}
#search .package.match {
font-weight: bold;
}
.bottom {
text-align: center;
font-size: 14px;
padding-bottom: 15px;
}
code {
padding: 2px 4px;
font-size: 95%;
color: #c7254e;
background-color: #f9f2f4;
border-radius: 4px;
}
img {
vertical-align: middle;
}
pre {
margin-bottom: 20px;
background: #333;
color: #fff;
border-radius: 6px;
margin-top: 30px;
padding: 10px 20px;
word-wrap: break-word;
}
pre code {
padding: 0;
font-size: inherit;
color: inherit;
white-space: pre-wrap;
background-color: transparent;
border-radius: 0;
}
.ant-table {
font-size: 14px;
}
.ant-alert {
font-size: 14px !important;
padding: 15px;
}
.pack-overview {
min-width: 960px;
color: #f1f1f1;
height: 300px;
width: 100%;
margin-bottom: 50px;
position: relative;
}
.pack-overview-background {
height: 300px;
@@ -146,18 +34,13 @@ pre code {
}
.pack-overview a {
color: #f1f1f1;
text-decoration: none;
font-size: 16px;
transition: all .2s ease-in-out;
padding-bottom: 2px;
border-bottom: 1px solid;
text-decoration: underline;
}
.pack-overview a:hover {
color: #ffffff;
opacity: .7;
}
.pack-info {
position: relative;
max-width: 1140px;
margin: 0 auto;
padding: 30px 0;
@@ -170,134 +53,133 @@ pre code {
line-height: 48px;
color: #fff;
}
.pack-description {
font-family: "-apple-system", sans-serif;
margin-top: 20px;
display: block; /* Fallback for non-webkit */
display: -webkit-box;
width: 600px;
font-size: 20px;
line-height: 26px;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
line-height: 28px;
text-overflow: ellipsis;
max-height: 100px;
color: #fff;
width: 600px;
white-space:nowrap;
text-overflow: ellipsis;
overflow:hidden;
}
.deprecated {
text-decoration: line-through;
color: #BDBDBD;
}
.deprecated-content {
color: #555;
color: #FF8A65;
width: 600px;
white-space:nowrap;
text-overflow: ellipsis;
overflow:hidden;
font-style: italic;
}
.pack-ver {
font-size: 28px;
line-height: 48px;
}
.pack-ver a.badge-link {
font-size: inherit;
border-bottom: 0px;
}
.pack-lastup {
font-size: 14px;
display: inline-block;
}
.pack-repo {
position: absolute;
bottom: 10px;
text-align: right;
width: 100%;
margin-top: 50px;
}
.pack-repo a {
border: none;
}
.pack-repo a:hover {
border-bottom: 1px solid;
}
.pack-install {
position: absolute;
top: 75px;
top: 85px;
right: 0px;
max-width: 450px;
}
.pack-install pre {
border: none;
background-color: #666666;
color: #f1f1f1;
font-size: 16px;
width: 480px;
}
.pack-details {
max-width: 1140px;
min-width: 960px;
margin: 40px auto;
}
.pack-details a {
color: #333333;
}
.pack-details a:hover {
border-bottom: 1px solid;
opacity: .8;
.cols-box {
display: flex;
-webkit-box-pack: center;
}
.cols-box ul {
list-style: none;
margin: 0;
padding: 0;
}
.cols-box-item {
margin-bottom: 15px;
flex: 1;
height: 250px;
margin-right: 30px;
}
.cols-box-item:last-child {
margin-right: 0;
}
.cols-box-title {
font-size: 20px;
color: #428bca;
margin-bottom: 10px;
font-weight: bold;
overflow: hidden;
border-bottom: 1px solid #ccc;
padding-bottom: 3px;
}
.cols-box-item .more-item {
font-size: 16px;
line-height: 28px;
color: #428bca;
margin-top: 15px;
text-decoration: underline;
float: right;
}
.pack-dep-ver, .pack-download-num {
float: right;
}
.pack-lastup, .pack-sync {
float: left;
margin-top: 10px;
}
.pack-sync a {
font-size: 23px;
}
.maintainers .row {
display: -moz-box;
display: -webkit-box;
display: box;
-webkit-box-pack: center;
margin-right: 0;
margin-left: 0;
}
.maintainer {
-moz-box-flex: 1;
-webkit-box-flex: 1;
box-flex: 1;
margin-bottom: 10px;
line-height: 30px;
width: 50%;
-webkit-box-orient: vertical;
-webkit-box-pack: center;
line-height: 50px;
overflow: hidden;
}
.maintainer a:hover {
font-style: italic;
border: none;
}
.maintainer .avatar {
-webkit-border-radius: 50px;
-moz-border-radius: 50px;
border-radius: 30px;
width: 30px;
height: 30px;
border-radius: 50px;
margin-right: 10px;
}
ul, ol{
list-style: initial;
.downloads .row {
margin-left: 0;
margin-right: 50px;
line-height: 28px;
border-bottom: 1px solid #f2f2f2;
}
.pack-readme {
max-width: 1140px;
min-width: 960px;
margin: 10px auto;
border-top: 1px solid #dddddd;
}
.pack-readme .title {
color: #dddddd;
font-weight: bolder;
margin-bottom: 20px;
text-align: right;
}

View File

@@ -1,147 +0,0 @@
/**
* yue.css
*
* yue.css is designed for readable content.
*
* Copyright (c) 2013 - 2014 by Hsiaoming Yang.
*/
.yue h1,
.yue h2,
.yue h3,
.yue h4,
.yue h5,
.yue h6 {
font-family: "-apple-system-headline", "Georgia", "Xin Gothic", "Hiragino Sans GB", "Droid Sans Fallback", "Microsoft YaHei", "SimSun", sans-serif;
color: #222223;
}
.yue h1 {
font-size: 1.8em;
margin: 0.67em 0;
}
.yue > h1 {
margin-top: 0;
font-size: 2em;
}
.yue h2 {
font-size: 1.5em;
margin: 0.83em 0;
}
.yue h3 {
font-size: 1.17em;
margin: 1em 0;
}
.yue h4,
.yue h5,
.yue h6 {
font-size: 1em;
margin: 1.6em 0 1em 0;
}
.yue h6 {
font-weight: 500;
}
.yue p {
margin-top: 0;
margin-bottom: 1.46em;
}
.yue strong,
.yue b {
font-weight: 700;
color: #222223;
}
.yue em,
.yue i {
font-style: italic;
color: #222223;
}
.yue hr {
display: block;
width: 14%;
margin: 40px auto 34px;
border: 0 none;
border-top: 3px solid #dededc;
}
.yue blockquote {
margin: 0 0 1.64em 0;
border-left: 3px solid #dadada;
padding-left: 12px;
color: #666664;
}
.yue blockquote a {
color: #666664;
}
.yue ul,
.yue ol {
margin: 0 0 24px 6px;
padding-left: 16px;
}
.yue ul {
list-style-type: square;
}
.yue ol {
list-style-type: decimal;
}
.yue li {
margin-bottom: 0.2em;
}
.yue li ul,
.yue li ol {
margin-top: 0;
margin-bottom: 0;
margin-left: 14px;
}
.yue li ul {
list-style-type: disc;
}
.yue li ul ul {
list-style-type: circle;
}
.yue li p {
margin: 0.4em 0 0.6em;
}
.yue .unstyled {
list-style-type: none;
margin: 0;
padding: 0;
}
@media (min-width: 1100px) {
.yue blockquote {
margin-left: -24px;
padding-left: 20px;
border-width: 4px;
}
.yue blockquote blockquote {
margin-left: 0;
}
}

View File

@@ -41,7 +41,7 @@ $(function () {
data.last_sync_module + '</a>');
if (!data.sync_status) {
$('.syncing').remove();
$('.syncing').html('');
}
$('.sync').show();

View File

@@ -1,4 +1,19 @@
'use strict';
/**!
* cnpmjs.org - routes/registry.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com>
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
"use strict";
/**
* Module dependencies.
*/
var limit = require('../middleware/limit');
var login = require('../middleware/login');
@@ -6,7 +21,6 @@ var publishable = require('../middleware/publishable');
var syncByInstall = require('../middleware/sync_by_install');
var editable = require('../middleware/editable');
var existsPackage = require('../middleware/exists_package');
var unpublishable = require('../middleware/unpublishable');
var showTotal = require('../controllers/total');
@@ -14,7 +28,6 @@ var listAll = require('../controllers/registry/package/list_all');
var listShorts = require('../controllers/registry/package/list_shorts');
var listSince = require('../controllers/registry/package/list_since');
var listAllVersions = require('../controllers/registry/package/list');
var listDependents = require('../controllers/registry/package/list_dependents');
var getOneVersion = require('../controllers/registry/package/show');
var savePackage = require('../controllers/registry/package/save');
var tag = require('../controllers/registry/package/tag');
@@ -23,7 +36,6 @@ var removeOneVersion = require('../controllers/registry/package/remove_version')
var updatePackage = require('../controllers/registry/package/update');
var downloadPackage = require('../controllers/registry/package/download');
var downloadTotal = require('../controllers/registry/package/download_total');
var listPackagesByUser = require('../controllers/registry/package/list_by_user');
var addUser = require('../controllers/registry/user/add');
var showUser = require('../controllers/registry/user/show');
@@ -53,9 +65,9 @@ function routes(app) {
// module
// scope package: params: [$name]
app.get(/^\/(@[\w\-\.]+\/[^\/]+)$/, syncByInstall, listAllVersions);
app.get(/^\/(@[\w\-\.]+\/[\w\-\.]+)$/, syncByInstall, listAllVersions);
// scope package: params: [$name, $version]
app.get(/^\/(@[\w\-\.]+\/[\w\-\.]+)\/([^\/]+)$/, syncByInstall, getOneVersion);
app.get(/^\/(@[\w\-\.]+\/[\w\-\.]+)\/([\w\.\-]+)$/, syncByInstall, getOneVersion);
app.get('/:name', syncByInstall, listAllVersions);
app.get('/:name/:version', syncByInstall, getOneVersion);
@@ -65,9 +77,7 @@ function routes(app) {
app.put('/:name', login, publishable, savePackage);
// sync from source npm
app.put(/^\/(@[\w\-\.]+\/[\w\-\.]+)\/sync$/, sync.sync);
app.put('/:name/sync', sync.sync);
app.get(/^\/(@[\w\-\.]+\/[\w\-\.]+)\/sync\/log\/(\d+)$/, sync.getSyncLog);
app.get('/:name/sync/log/:id', sync.getSyncLog);
// add tag
@@ -82,16 +92,16 @@ function routes(app) {
// delete tarball and remove one version
app.delete(/^\/(@[\w\-\.]+\/[\w\-\.]+)\/download\/(@[\w\-\.]+\/[\w\-\.]+)\/\-rev\/([\w\-\.]+)$/,
login, unpublishable, removeOneVersion);
app.delete('/:name/download/:filename/-rev/:rev', login, unpublishable, removeOneVersion);
login, publishable, editable, removeOneVersion);
app.delete('/:name/download/:filename/-rev/:rev', login, publishable, editable, removeOneVersion);
// update module, unpublish will PUT this
app.put(/^\/(@[\w\-\.]+\/[\w\-\.]+)\/\-rev\/([\w\-\.]+)$/, login, publishable, editable, updatePackage);
app.put('/:name/-rev/:rev', login, publishable, editable, updatePackage);
// remove all versions
app.delete(/^\/(@[\w\-\.]+\/[\w\-\.]+)\/\-rev\/([\w\-\.]+)$/, login, unpublishable, removePackage);
app.delete('/:name/-rev/:rev', login, unpublishable, removePackage);
app.delete(/^\/(@[\w\-\.]+\/[\w\-\.]+)\/\-rev\/([\w\-\.]+)$/, login, publishable, editable, removePackage);
app.delete('/:name/-rev/:rev', login, publishable, editable, removePackage);
// try to create a new user
// https://registry.npmjs.org/-/user/org.couchdb.user:fengmk2
@@ -101,17 +111,11 @@ function routes(app) {
// list all packages of user
app.get('/-/by-user/:user', userPackage.list);
app.get('/-/users/:user/packages', listPackagesByUser);
// download times
app.get('/downloads/range/:range/:name', downloadTotal);
app.get(/^\/downloads\/range\/([^\/]+)\/(@[\w\-\.]+\/[\w\-\.]+)$/, downloadTotal);
app.get('/downloads/range/:range', downloadTotal);
// GET /-/package/:pkg/dependents
app.get('/-/package/:name/dependents', existsPackage, listDependents);
app.get(/^\/\-\/package\/(@[\w\-\.]+\/[\w\-\.]+)\/dependents$/, existsPackage, listDependents);
// GET /-/package/:pkg/dist-tags -- returns the package's dist-tags
app.get('/-/package/:name/dist-tags', existsPackage, tags.index);
app.get(/^\/\-\/package\/(@[\w\-\.]+\/[\w\-\.]+)\/dist\-tags$/, existsPackage, tags.index);

View File

@@ -1,4 +1,19 @@
'use strict';
/**!
* cnpmjs.org - routes/web.js
*
* Copyright(c) cnpm and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com>
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/
"use strict";
/**
* Module dependencies.
*/
var showPackage = require('../controllers/web/package/show');
var searchPackage = require('../controllers/web/package/search');
@@ -9,8 +24,10 @@ var showUser = require('../controllers/web/user/show');
var sync = require('../controllers/sync');
var showTotal = require('../controllers/total');
var badge = require('../controllers/web/badge');
var home = require('../controllers/web/home');
function routes(app) {
app.get('/', home);
app.get('/total', showTotal);
// scope package without version
@@ -27,19 +44,15 @@ function routes(app) {
app.get('/~:name', showUser);
app.get(/\/sync\/(@[\w\-\.]+\/[\w\-\.]+)$/, showSync);
app.get('/sync/:name', showSync);
app.get('/sync', showSync);
app.put(/\/sync\/(@[\w\-\.]+\/[\w\-\.]+)$/, sync.sync);
app.put('/sync/:name', sync.sync);
app.get(/\/sync\/(@[\w\-\.]+\/[\w\-\.]+)\/log\/(\d+)$/, sync.getSyncLog);
app.get('/sync/:name/log/:id', sync.getSyncLog);
app.get('/_list/search/search', searchRange);
app.get(/^\/badge\/v\/([@\w\-\.\/]+)\.svg$/, badge.version);
app.get(/^\/badge\/d\/([@\w\-\.\/]+)\.svg$/, badge.downloads);
}
module.exports = routes;

View File

@@ -1,5 +1,21 @@
'use strict';
/**!
* cnpmjs.org - servers/registry.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com>
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
"use strict";
/**
* Module dependencies.
*/
global.Promise = require('bluebird');
var koa = require('koa');
var app = module.exports = koa();
var http = require('http');
@@ -13,27 +29,25 @@ var staticCache = require('../middleware/static');
var notFound = require('../middleware/registry_not_found');
var cors = require('kcors');
var proxyToNpm = require('../middleware/proxy_to_npm');
var maxrequests = require('koa-maxrequests');
app.use(maxrequests());
app.use(block());
middlewares.jsonp(app);
app.use(middlewares.rt({ headerName: 'X-ReadTime' }));
app.use(middlewares.rt({headerName: 'X-ReadTime'}));
app.use(middlewares.rewrite('/favicon.ico', '/favicon.png'));
app.use(staticCache);
app.keys = ['todokey', config.sessionSecret];
app.proxy = true;
app.use(middlewares.bodyParser({ jsonLimit: config.jsonLimit }));
app.use(middlewares.bodyParser({jsonLimit: config.jsonLimit}));
app.use(cors({
allowMethods: 'GET,HEAD',
allowMethods: 'GET,HEAD'
}));
app.use(auth());
app.use(proxyToNpm());
app.use(notFound);
if (config.enableCompress) {
app.use(middlewares.compress({ threshold: 150 }));
app.use(middlewares.compress({threshold: 150}));
}
app.use(middlewares.conditional());
app.use(middlewares.etag());

View File

@@ -1,5 +1,21 @@
'use strict';
/**!
* cnpmjs.org - servers/web.js
*
* Copyright(c) cnpm and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com>
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/
"use strict";
/**
* Module dependencies.
*/
global.Promise = require('bluebird');
var opensearch = require('../middleware/opensearch');
var notFound = require('../middleware/web_not_found');
var staticCache = require('../middleware/static');
@@ -10,22 +26,18 @@ var logger = require('../common/logger');
var renderMarkdown = require('../common/markdown').render;
var auth = require('../middleware/auth');
var proxyToNpm = require('../middleware/proxy_to_npm');
var i18n = require('../middleware/i18n');
var routes = require('../routes/web');
var config = require('../config');
var jsonp = require('koa-safe-jsonp');
var path = require('path');
var http = require('http');
var koa = require('koa');
var fs = require('fs');
var maxrequests = require('koa-maxrequests');
var app = koa();
jsonp(app);
var rootdir = path.dirname(__dirname);
app.use(maxrequests());
app.use(block());
app.use(middlewares.rt({headerName: 'X-ReadTime'}));
app.use(middlewares.rewrite('/favicon.ico', '/favicon.png'));
@@ -66,14 +78,11 @@ var layout = fs.readFileSync(path.join(viewDir, 'layout.html'), 'utf8')
fs.writeFileSync(layoutFile, layout);
// custom web readme home page support
var readmeFile = path.join(docDir, '_readme.md');
var readmeContent;
if (config.customReadmeFile) {
readmeContent = fs.readFileSync(config.customReadmeFile, 'utf8');
} else {
readmeContent = fs.readFileSync(path.join(docDir, 'readme.md'), 'utf8');
var readmeFile = path.join(docDir, '_readme.md');
var readmeContent = fs.readFileSync(config.customReadmeFile, 'utf8');
fs.writeFileSync(readmeFile, readmeContent);
}
fs.writeFileSync(readmeFile, readmeContent);
app.use(markdownMiddleware({
baseUrl: '/',
@@ -88,7 +97,12 @@ app.use(markdownMiddleware({
},
}));
var locals = {
app.use(i18n({
dir: path.join(__dirname, '..', 'locales'),
defaultLocale: 'en_US'
}, app));
var globalLocals = {
config: config
};
@@ -98,7 +112,7 @@ middlewares.ejs(app, {
layout: '_layout',
cache: config.viewCache,
debug: config.debug,
locals: locals
locals: globalLocals
});
/**

View File

@@ -15,12 +15,11 @@
*/
var config = require('../config');
var isPrivateScopedPackage = require('../lib/common').isPrivateScopedPackage;
config.privatePackages = config.privatePackages || [];
exports.isPrivatePackage = function (name) {
if (isPrivateScopedPackage(name)) {
exports.isPrivatePackage = function* (name) {
if (name[0] === '@') {
return true;
}
if (config.privatePackages.indexOf(name) >= 0) {

View File

@@ -1,9 +1,11 @@
/**!
* Copyright(c) cnpm and other contributors.
* cnpmjs.org - services/download_total.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
@@ -30,27 +32,6 @@ exports.getModuleTotal = function* (name, start, end) {
return formatRows(rows, start, end);
};
exports.getTotalByName = function* (name) {
var rows = yield DownloadTotal.findAll({
where: {
name: name
}
});
var count = 0;
rows.forEach(function (row) {
for (var i = 1; i <= 31; i++) {
var day = i < 10 ? '0' + i : String(i);
var field = 'd' + day;
var val = row[field];
if (typeof val === 'string') {
val = utility.toSafeNumber(val);
}
count += val;
}
});
return count;
};
exports.plusModuleTotal = function* (data) {
var yearMonth = parseYearMonth(data.date);
// all module download total
@@ -72,7 +53,7 @@ exports.plusModuleTotal = function* (data) {
row[field] = utility.toSafeNumber(row[field]);
}
row[field] += data.count;
if (row.changed()) {
if (row.isDirty) {
yield row.save();
}
@@ -94,14 +75,14 @@ exports.plusModuleTotal = function* (data) {
row[field] = utility.toSafeNumber(row[field]);
}
row[field] += data.count;
if (row.changed()) {
if (row.isDirty) {
return yield row.save();
}
return row;
};
exports.getTotal = function* (start, end) {
return yield exports.getModuleTotal('__all__', start, end);
return yield* exports.getModuleTotal('__all__', start, end);
};
function parseYearMonth(date) {

View File

@@ -50,5 +50,5 @@ exports.append = function* (id, log) {
};
exports.get = function* (id) {
return yield ModuleLog.findById(id);
return yield ModuleLog.find(Number(id));
};

View File

@@ -1,7 +1,20 @@
/**!
* cnpmjs.org - services/npm.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/
'use strict';
var ms = require('humanize-ms');
var cleanNpmMetadata = require('normalize-registry-metadata');
/**
* Module dependencies.
*/
var urllib = require('../common/urllib');
var config = require('../config');
@@ -20,18 +33,13 @@ function* request(url, options) {
url = registry + url;
var r;
try {
r = yield urllib.request(url, options);
// https://github.com/npm/registry/issues/87#issuecomment-261450090
if (options.dataType === 'json' && r.data && config.officialNpmReplicate === registry) {
cleanNpmMetadata(r.data);
}
r = yield urllib.requestThunk(url, options);
} catch (err) {
var statusCode = err.status || -1;
var data = err.data || '[empty]';
if (err.name === 'JSONResponseFormatError' && statusCode >= 500) {
err.name = 'NPMServerError';
err.status = statusCode;
err.message = 'Url: ' + url + ', Status ' + statusCode + ', ' + data.toString();
err.message = 'Status ' + statusCode + ', ' + data.toString();
}
throw err;
}
@@ -60,83 +68,6 @@ exports.get = function* (name) {
return data;
};
exports.fetchUpdatesSince = function* (lastSyncTime, timeout) {
var lastModified = lastSyncTime - ms('10m');
var data = yield exports.getAllSince(lastModified, timeout);
var result = {
lastModified: lastSyncTime,
names: [],
};
if (!data) {
return result;
}
if (Array.isArray(data)) {
// support https://registry.npmjs.org/-/all/static/today.json
var maxModified;
data.forEach(function (pkg) {
if (pkg.time && pkg.time.modified) {
var modified = Date.parse(pkg.time.modified);
if (modified >= lastModified) {
result.names.push(pkg.name);
}
if (!maxModified || modified > maxModified) {
maxModified = modified;
}
} else {
result.names.push(pkg.name);
}
});
if (maxModified) {
result.lastModified = maxModified;
}
} else {
// /-/all/since
if (data._updated) {
result.lastModified = data._updated;
delete data._updated;
}
result.names = Object.keys(data);
}
return result;
};
exports.fetchAllPackagesSince = function* (timestamp) {
var r = yield request('/-/all/static/all.json', {
registry: 'http://registry.npmjs.org',
timeout: 600000
});
// {"_updated":1441520402174,"0":{"name":"0","dist-tags
// "time":{"modified":"2014-06-17T06:38:43.495Z"}
var data = r.data;
var result = {
lastModified: timestamp,
lastModifiedName: null,
names: [],
};
var maxModified;
for (var key in data) {
if (key === '_updated') {
continue;
}
var pkg = data[key];
if (!pkg.time || !pkg.time.modified) {
continue;
}
var modified = Date.parse(pkg.time.modified);
if (modified >= timestamp) {
result.names.push(pkg.name);
}
if (!maxModified || modified > maxModified) {
maxModified = modified;
result.lastModifiedName = pkg.name;
}
}
if (maxModified) {
result.lastModified = maxModified;
}
return result;
};
exports.getAllSince = function* (startkey, timeout) {
var r = yield* request('/-/all/since?stale=update_after&startkey=' + startkey, {
timeout: timeout || 300000
@@ -144,14 +75,6 @@ exports.getAllSince = function* (startkey, timeout) {
return r.data;
};
exports.getAllToday = function* (timeout) {
var r = yield* request('/-/all/static/today.json', {
timeout: timeout || 300000
});
// data is array: see https://registry.npmjs.org/-/all/static/today.json
return r.data;
};
exports.getShort = function* (timeout) {
var r = yield* request('/-/short', {
timeout: timeout || 300000,

View File

@@ -1,6 +1,19 @@
/**!
* cnpmjs.org - services/package.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
var semver = require('semver');
/**
* Module dependencies.
*/
var models = require('../models');
var common = require('./common');
var Tag = models.Tag;
@@ -14,23 +27,20 @@ var ModuleUnpublished = models.ModuleUnpublished;
var NpmModuleMaintainer = models.NpmModuleMaintainer;
// module
var _parseRow = function (row) {
if (row.package.indexOf('%7B%22') === 0) {
// now store package will encodeURIComponent() after JSON.stringify
row.package = decodeURIComponent(row.package);
}
row.package = JSON.parse(row.package);
if (typeof row.publish_time === 'string') {
// pg bigint is string
row.publish_time = Number(row.publish_time);
}
};
// module:read
function parseRow(row) {
if (row && row.package) {
try {
_parseRow(row);
if (row.package.indexOf('%7B%22') === 0) {
// now store package will encodeURIComponent() after JSON.stringify
row.package = decodeURIComponent(row.package);
}
row.package = JSON.parse(row.package);
if (typeof row.publish_time === 'string') {
// pg bigint is string
row.publish_time = Number(row.publish_time);
}
} catch (e) {
console.warn('parse package error: %s, id: %s version: %s, error: %s', row.name, row.id, row.version, e);
}
@@ -43,7 +53,7 @@ function stringifyPackage(pkg) {
}
exports.getModuleById = function* (id) {
var row = yield Module.findById(Number(id));
var row = yield Module.find(Number(id));
parseRow(row);
return row;
};
@@ -62,25 +72,6 @@ exports.getModuleByTag = function* (name, tag) {
return yield* exports.getModule(tag.name, tag.version);
};
exports.getModuleByRange = function* (name, range) {
var rows = yield exports.listModulesByName(name, [ 'id', 'version' ]);
var versionMap = {};
var versions = rows.map(function(row) {
versionMap[row.version] = row;
return row.version;
}).filter(function(version) {
return semver.valid(version);
});
var version = semver.maxSatisfying(versions, range);
if (!versionMap[version]) {
return null;
}
var id = versionMap[version].id;
return yield exports.getModuleById(id);
};
exports.getLatestModule = function* (name) {
return yield* exports.getModuleByTag(name, 'latest');
};
@@ -137,7 +128,7 @@ exports.listModules = function* (names) {
id: ids
},
attributes: [
'name', 'description', 'version',
'name', 'description'
]
});
return rows;
@@ -205,7 +196,7 @@ exports.listPublicModuleNamesByUser = function* (username) {
};
// start must be a date or timestamp
exports.listPublicModuleNamesSince = function* listPublicModuleNamesSince(start) {
exports.listPublicModuleNamesSince = function* (start) {
if (!(start instanceof Date)) {
start = new Date(Number(start));
}
@@ -228,25 +219,23 @@ exports.listAllPublicModuleNames = function* () {
var sql = 'SELECT DISTINCT(name) AS name FROM tag ORDER BY name';
var rows = yield models.query(sql);
return rows.filter(function (row) {
return !common.isPrivatePackage(row.name);
return row.name[0] !== '@';
}).map(function (row) {
return row.name;
});
};
exports.listModulesByName = function* (moduleName, attributes) {
exports.listModulesByName = function* (moduleName) {
var mods = yield Module.findAll({
where: {
name: moduleName
},
order: [ ['id', 'DESC'] ],
attributes,
order: [ ['id', 'DESC'] ]
});
for (var mod of mods) {
return mods.map(function (mod) {
parseRow(mod);
}
return mods;
return mod;
});
};
exports.getModuleLastModified = function* (name) {
@@ -291,7 +280,7 @@ exports.saveModule = function* (mod) {
item.dist_size = dist.size;
item.description = description;
if (item.changed()) {
if (item.isDirty) {
item = yield item.save();
}
var result = {
@@ -323,7 +312,7 @@ exports.saveModule = function* (mod) {
};
exports.updateModulePackage = function* (id, pkg) {
var mod = yield Module.findById(Number(id));
var mod = yield Module.find(Number(id));
if (!mod) {
// not exists
return null;
@@ -333,7 +322,7 @@ exports.updateModulePackage = function* (id, pkg) {
};
exports.updateModulePackageFields = function* (id, fields) {
var mod = yield exports.getModuleById(id);
var mod = yield* exports.getModuleById(id);
if (!mod) {
return null;
}
@@ -341,7 +330,7 @@ exports.updateModulePackageFields = function* (id, fields) {
for (var k in fields) {
pkg[k] = fields[k];
}
return yield exports.updateModulePackage(id, pkg);
return yield* exports.updateModulePackage(id, pkg);
};
exports.updateModuleReadme = function* (id, readme) {
@@ -376,9 +365,8 @@ exports.updateModuleLastModified = function* (name) {
if (!row) {
return null;
}
// gmt_modified is readonly, we must use setDataValue
row.setDataValue('gmt_modified', new Date());
return yield row.save();
row.gmt_modified = new Date();
return yield row.save(['gmt_modified']);
};
exports.removeModulesByName = function* (name) {
@@ -415,7 +403,7 @@ exports.addModuleTag = function* (name, tag, version) {
}
row.module_id = mod.id;
row.version = version;
if (row.changed()) {
if (row.isDirty) {
return yield row.save();
}
return row;
@@ -512,7 +500,8 @@ exports.updatePrivateModuleMaintainers = function* (name, usernames) {
};
function* getMaintainerModel(name) {
return common.isPrivatePackage(name) ? PrivateModuleMaintainer : NpmModuleMaintainer;
var isPrivatePackage = yield* common.isPrivatePackage(name);
return isPrivatePackage ? PrivateModuleMaintainer : NpmModuleMaintainer;
}
exports.listMaintainers = function* (name) {
@@ -591,7 +580,7 @@ exports.addKeyword = function* (data) {
item = ModuleKeyword.build(data);
}
item.description = data.description;
if (item.changed()) {
if (item.isDirty) {
// make sure object will change, otherwise will cause empty sql error
// @see https://github.com/cnpm/cnpmjs.org/issues/533
return yield item.save();

View File

@@ -1,9 +1,11 @@
/**
* Copyright(c) cnpm and other contributors.
/**!
* cnpmjs.org - services/total.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';

View File

@@ -1,85 +0,0 @@
'use strict';
const ChangesStream = require('changes-stream');
const path = require('path');
const fs = require('mz/fs');
const urllib = require('urllib');
const streamAwait = require('await-event');
const logger = require('../common/logger');
const config = require('../config');
const db = config.officialNpmReplicate;
const lastSeqFile = path.join(config.dataDir, '.cnpmjs.org.last_seq.txt');
let _STREAM_ID = 0;
module.exports = function* sync() {
const since = yield getLastSequence();
const streamId = _STREAM_ID++;
let changesCount = 0;
logger.syncInfo('start changes stream#%d, since: %s', streamId, since);
const changes = new ChangesStream({
db,
since,
include_docs: false,
});
changes.await = streamAwait;
changes.on('data', change => {
changesCount++;
logger.syncInfo('stream#%d get change#%d: %j', streamId, changesCount, change);
syncPackage(change);
});
try {
yield changes.await('error');
} catch (err) {
// make sure changes steam is destroy
changes.destroy();
err.message += `, stream#${streamId}, changesCount#${changesCount}`;
throw err;
}
};
function syncPackage(change) {
const url = `${config.handleSyncRegistry}/${change.id}/sync`;
urllib.request(url, {
method: 'PUT',
dataType: 'json',
timeout: 10000,
}, (err, data) => {
if (err) {
logger.syncInfo('%s:%s PUT %s error: %s, retry after 5s',
change.seq, change.id, url, err);
logger.syncError(err);
syncPackage(change);
setTimeout(() => syncPackage(change), 5000);
} else {
saveLastSequence(change.seq);
logger.syncInfo('%s:%s sync request sent, log: %s/log/%s',
change.seq, change.id, url, data.logId);
}
});
}
function* getLastSequence() {
let lastSeq;
if (yield fs.exists(lastSeqFile)) {
lastSeq = yield fs.readFile(lastSeqFile, 'utf8');
lastSeq = Number(lastSeq);
}
if (!lastSeq) {
lastSeq = 2649694;
}
// const r = yield urllib.request(db, {
// dataType: 'json',
// timeout: 15000,
// });
// logger.syncInfo('get registry info: %j', r.data);
// if (lastSeq < r.data.update_seq) {
// lastSeq = r.data.update_seq;
// }
return lastSeq;
}
function saveLastSequence(seq) {
fs.writeFile(lastSeqFile, String(seq), () => {});
}

View File

@@ -1,23 +1,38 @@
/**!
* cnpmjs.org - sync/index.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/
'use strict';
const debug = require('debug')('cnpmjs.org:sync:index');
const co = require('co');
const ms = require('humanize-ms');
const util = require('util');
const config = require('../config');
const mail = require('../common/mail');
const logger = require('../common/logger');
const totalService = require('../services/total');
/**
* Module dependencies.
*/
let sync = null;
global.Promise = require('bluebird');
var debug = require('debug')('cnpmjs.org:sync:index');
var co = require('co');
var ms = require('humanize-ms');
var util = require('util');
var config = require('../config');
var mail = require('../common/mail');
var logger = require('../common/logger');
var totalService = require('../services/total');
var sync = null;
switch (config.syncModel) {
case 'all':
sync = require('./sync_all');
break;
case 'exist':
sync = require('./sync_exist');
break;
case 'all':
sync = require('./sync_all');
break;
case 'exist':
sync = require('./sync_exist');
break;
}
if (!sync && config.enableCluster) {
@@ -25,135 +40,108 @@ if (!sync && config.enableCluster) {
process.exit(0);
}
console.log('[%s] [sync_worker:%s] syncing with %s mode, changesStreamingSync: %s',
Date(), process.pid, config.syncModel, config.changesStreamingSync);
console.log('[%s] [sync_worker:%s] syncing with %s mode',
Date(), process.pid, config.syncModel);
function onerror(err) {
logger.error(err);
}
// set sync_status = 0 at first
//set sync_status = 0 at first
co(function* () {
yield totalService.updateSyncStatus(0);
yield checkSyncStatus();
yield* totalService.updateSyncStatus(0);
}).catch(onerror);
let syncInterval = ms(config.syncInterval);
const minSyncInterval = ms('5m');
var syncInterval = ms(config.syncInterval);
var minSyncInterval = ms('5m');
if (!syncInterval || syncInterval < minSyncInterval) {
syncInterval = minSyncInterval;
}
if (sync) {
// the same time only sync once
let syncing = false;
const syncFn = co.wrap(function*() {
debug('mode: %s, syncing: %s', config.syncModel, syncing);
if (!syncing) {
syncing = true;
debug('start syncing');
let data;
let error;
try {
data = yield sync();
} catch (err) {
error = err;
error.message += ' (sync package error)';
logger.syncError(error);
}
data && logger.syncInfo(data);
if (!config.debug) {
sendMailToAdmin(error, data, new Date());
}
syncing = false;
// the same time only sync once
var syncing = false;
var syncFn = co.wrap(function* () {
debug('mode: %s, syncing: %s', config.syncModel, syncing);
if (!syncing) {
syncing = true;
debug('start syncing');
var data;
var error;
try {
data = yield* sync();
} catch (err) {
error = err;
error.message += ' (sync package error)';
logger.syncError(error);
}
data && logger.syncInfo(data);
if (!config.debug) {
sendMailToAdmin(error, data, new Date());
}
syncing = false;
}
});
// check last_sync_time and last_exist_sync_time
yield checkSyncStatus();
});
if (sync) {
syncFn().catch(onerror);
setInterval(() => syncFn().catch(onerror), syncInterval);
setInterval(function () {
syncFn().catch(onerror);
}, syncInterval);
}
/**
* sync popular modules
*/
var startSyncPopular = require('./sync_popular');
var syncingPopular = false;
var syncPopularFn = co.wrap(function* syncPopular() {
if (syncingPopular) {
return;
}
syncingPopular = true;
logger.syncInfo('Start syncing popular modules...');
var data;
var error;
try {
data = yield* startSyncPopular();
} catch (err) {
error = err;
error.message += ' (sync package error)';
logger.syncError(error);
}
if (data) {
logger.syncInfo(data);
}
if (!config.debug) {
sendMailToAdmin(error, data, new Date());
}
syncingPopular = false;
});
if (config.syncPopular) {
const sync = require('./sync_popular');
let syncing = false;
const syncFn = co.wrap(function*() {
if (syncing) {
return;
}
syncing = true;
logger.syncInfo('Start syncing popular modules...');
let data;
let error;
try {
data = yield sync();
} catch (err) {
error = err;
error.message += ' (sync package error)';
logger.syncError(error);
}
if (data) {
logger.syncInfo(data);
}
if (!config.debug) {
sendMailToAdmin(error, data, new Date());
}
syncing = false;
});
syncFn().catch(onerror);
setInterval(() => syncFn().catch(onerror), ms(config.syncPopularInterval));
}
if (config.syncChangesStream) {
const sync = require('./changes_stream_syncer');
let syncing = false;
const syncFn = co.wrap(function*() {
if (syncing) {
return;
}
syncing = true;
logger.syncInfo('Start changes stream syncing...');
try {
yield sync();
} catch (err) {
err.message += ' (sync changes stream error)';
logger.syncError(err);
}
syncing = false;
});
syncFn().catch(onerror);
setInterval(() => syncFn().catch(onerror), ms('1m'));
syncPopularFn().catch(onerror);
setInterval(function () {
syncPopularFn().catch(onerror);
}, ms(config.syncPopularInterval));
} else {
logger.syncInfo('sync popular module disable');
}
function sendMailToAdmin(err, result, syncTime) {
result = result || {};
const to = [];
var to = [];
for (var name in config.admins) {
to.push(config.admins[name]);
}
debug('Send email to all admins: %j, with err message: %s, result: %j, start sync time: %s.',
to, err ? err.message : '', result, syncTime);
let subject;
let type;
let html;
var subject;
var type;
var html;
if (err) {
// ignore 503, 504 error
// 504: Gateway Time-out
if (err.status === 503 || err.status === 504) {
return;
}
if (err.name === 'JSONResponseFormatError') {
return;
}
subject = 'Sync Error';
type = 'error';
html = util.format('Sync packages from official registry failed.\n' +
@@ -165,10 +153,6 @@ function sendMailToAdmin(err, result, syncTime) {
'Start sync time is %s.\n %d packges sync failed: %j ...\n %d packages sync successes :%j ...',
syncTime, result.fails.length, result.fails.slice(0, 10),
result.successes.length, result.successes.slice(0, 10));
// skip email notice when fails items small then 3
if (result.fails.length < 3) {
type = 'log';
}
} else if (result.successes && result.successes.length) {
subject = 'Sync Finished';
type = 'log';
@@ -179,37 +163,10 @@ function sendMailToAdmin(err, result, syncTime) {
debug('send email with type: %s, subject: %s, html: %s', type, subject, html);
logger.syncInfo('send email with type: %s, subject: %s, html: %s', type, subject, html);
if (type && type !== 'log') {
mail[type](to, subject, html, err => {
mail[type](to, subject, html, function (err) {
if (err) {
logger.error(err);
}
});
}
}
function* checkSyncStatus() {
// if source registry not cnpm, ignore it. e.g.: cnpmjs.org source registry is npmjs.org
if (!config.sourceNpmRegistryIsCNpm) {
return;
}
const total = yield totalService.getTotalInfo();
let lastSyncTime;
if (config.syncModel === 'all') {
lastSyncTime = total.last_sync_time;
} else if (config.syncModel === 'exist') {
lastSyncTime = total.last_exist_sync_time;
}
debug('checkSyncStatus start, lastSyncTime: %s, syncInterval: %s', lastSyncTime, syncInterval);
if (!lastSyncTime) {
return;
}
const diff = Date.now() - lastSyncTime;
const oneDay = 3600000 * 24;
const maxTime = Math.max(oneDay, syncInterval * 2);
if (diff > maxTime) {
const err = new Error('Last sync time is expired in ' + diff + ' ms, lastSyncTime: ' +
new Date(lastSyncTime) + ', maxTime: ' + maxTime + ' ms');
err.name = 'SyncExpiredError';
sendMailToAdmin(err, null, new Date());
}
}

View File

@@ -1,5 +1,19 @@
/*!
* cnpmjs.org - sync/status.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/
'use strict';
/**
* Module dependencies.
*/
var debug = require('debug')('cnpmjs.org:sync:status');
var co = require('co');
var Total = require('../services/total');
@@ -22,7 +36,7 @@ Status.prototype.log = function (syncDone) {
lastSyncModule: this.lastSyncModule,
};
co(function* () {
yield Total.updateSyncNum(params);
yield* Total.updateSyncNum(params);
}).catch(function () {});
};

View File

@@ -1,5 +1,20 @@
/*!
* cnpmjs.org - sync/sync_all.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/
'use strict';
/**
* Module dependencies.
*/
var ms = require('humanize-ms');
var thunkify = require('thunkify-wrap');
var config = require('../config');
var Status = require('./status');
@@ -25,6 +40,19 @@ function* getFirstSyncPackages(lastSyncModule) {
}
}
/**
* get all the packages that update time > lastSyncTime
* @param {Number} lastSyncTime
*/
function* getCommonSyncPackages(lastSyncTime) {
var data = yield* npmService.getAllSince(lastSyncTime);
if (!data) {
return [];
}
delete data._updated;
return Object.keys(data);
}
module.exports = function* sync() {
var syncTime = Date.now();
var info = yield* totalService.getTotalInfo();
@@ -38,9 +66,7 @@ module.exports = function* sync() {
logger.syncInfo('First time sync all packages from official registry');
packages = yield* getFirstSyncPackages(info.last_sync_module);
} else {
var result = yield npmService.fetchUpdatesSince(info.last_sync_time);
syncTime = result.lastModified;
packages = result.names;
packages = yield* getCommonSyncPackages(info.last_sync_time - ms('10m'));
}
packages = packages || [];
@@ -57,22 +83,19 @@ module.exports = function* sync() {
concurrency: config.syncConcurrency,
syncUpstreamFirst: false,
});
Status.init({
need: packages.length,
}, worker);
Status.init({need: packages.length}, worker);
worker.start();
var end = thunkify.event(worker);
yield end();
logger.syncInfo('All packages sync done, successes %d, fails %d, updates %d',
worker.successes.length, worker.fails.length, worker.updates.length);
logger.syncInfo('All packages sync done, successes %d, fails %d',
worker.successes.length, worker.fails.length);
//only when all succss, set last sync time
if (!worker.fails.length) {
yield totalService.setLastSyncTime(syncTime);
yield* totalService.setLastSyncTime(syncTime);
}
return {
successes: worker.successes,
fails: worker.fails,
updates: worker.updates,
fails: worker.fails
};
};

View File

@@ -1,7 +1,22 @@
/*!
* cnpmjs.org - sync/sync_exist.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/
'use strict';
/**
* Module dependencies.
*/
var debug = require('debug')('cnpmjs.org:sync:sync_exist');
var Status = require('./status');
var ms = require('humanize-ms');
var thunkify = require('thunkify-wrap');
var config = require('../config');
var npmService = require('../services/npm');
@@ -19,7 +34,7 @@ function intersection(arrOne, arrTwo) {
map[name] = true;
});
arrTwo.forEach(function (name) {
map[name] === true && results.push(name);
map[name] && results.push(name);
});
return results;
}
@@ -36,7 +51,7 @@ module.exports = function* sync() {
var allPackages;
if (!info.last_exist_sync_time) {
var pkgs = yield npmService.getShort();
var pkgs = yield* npmService.getShort();
debug('First time sync all packages from official registry, got %d packages', pkgs.length);
if (info.last_sync_module) {
// start from last success
@@ -48,10 +63,16 @@ module.exports = function* sync() {
}
allPackages = pkgs;
} else {
debug('sync new module from last exist sync time: %s', info.last_exist_sync_time);
var result = yield npmService.fetchUpdatesSince(info.last_exist_sync_time);
allPackages = result.names;
syncTime = result.lastModified;
debug('sync new module from last exist sync time: %s', info.last_sync_time);
var data = yield* npmService.getAllSince(info.last_exist_sync_time - ms('10m'));
if (!data) {
allPackages = [];
}
if (data._updated) {
syncTime = data._updated;
delete data._updated;
}
allPackages = Object.keys(data);
}
var packages = intersection(existPackages, allPackages);
@@ -62,7 +83,7 @@ module.exports = function* sync() {
fails: []
};
}
debug('Total %d packages to sync, top 10: %j', packages.length, packages.slice(0, 10));
debug('Total %d packages to sync', packages.length);
var worker = new SyncModuleWorker({
username: 'admin',

View File

@@ -1,5 +1,19 @@
/*!
* cnpmjs.org - sync/sync_popular.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/
'use strict';
/**
* Module dependencies.
*/
var debug = require('debug')('cnpmjs.org:sync:sync_popular');
var thunkify = require('thunkify-wrap');
var config = require('../config');

View File

@@ -1,52 +0,0 @@
'use strict';
const thunkify = require('thunkify-wrap');
const co = require('co');
const ms = require('humanize-ms');
const npmService = require('../services/npm');
const SyncModuleWorker = require('../controllers/sync_module_worker');
function* sync(sinceTimestamp) {
console.log('Fetching packages since: %s', new Date(sinceTimestamp));
var result = yield npmService.fetchAllPackagesSince(sinceTimestamp);
var packages = result.names;
packages = packages || [];
if (!packages.length) {
console.log('no packages need be sync');
process.exit(0);
}
// var news = [];
// for (var i = 0; i < packages.length; i++) {
// if (packages[i] === 'elwms') {
// news = packages.slice(i);
// break;
// }
// }
// packages = news;
console.log('lastModified: %s, lastModified package: %s, total %d packages to sync: %j',
new Date(result.lastModified), result.lastModifiedName, packages.length, packages);
var worker = new SyncModuleWorker({
username: 'sync_since',
name: packages,
noDep: true,
concurrency: 1,
syncUpstreamFirst: false,
});
worker.start();
var end = thunkify.event(worker);
yield end();
console.log('All packages sync done, successes %d, fails %d',
worker.successes.length, worker.fails.length);
process.exit(0);
}
co(function* () {
var timestamp = Date.now() - ms(process.argv[2] || '30d');
yield sync(timestamp);
}).catch(function (err) {
console.error(err.stack);
process.exit(1);
});

View File

@@ -1,3 +1,13 @@
/**!
* cnpmjs.org - test/controllers/registry/module/scope_package.test.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
/**
@@ -11,7 +21,7 @@ var config = require('../../../../config');
var app = require('../../../../servers/registry');
var utils = require('../../../utils');
describe('test/controllers/registry/module/scope_package.test.js', function () {
describe('controllers/registry/module/scope_package.test.js', function () {
var pkgname = '@cnpm/test-scope-package';
var pkgURL = '/@' + encodeURIComponent(pkgname.substring(1));
before(function (done) {
@@ -42,17 +52,16 @@ describe('test/controllers/registry/module/scope_package.test.js', function () {
afterEach(mm.restore);
it('should get 302 when do not support scope', function (done) {
it('should get 404 when do not support scope', function (done) {
mm(config, 'scopes', []);
request(app)
.get('/@invalid/test')
.expect('Location', 'https://registry.npmjs.com/@invalid/test')
.expect(302, done);
.expect(404, done);
});
it('should get 404 when scope is private', function (done) {
it('should get 400 when scope not match', function (done) {
request(app)
.get('/@cnpmtest/test')
.get('/@invalid/test')
.expect(404, done);
});

View File

@@ -170,18 +170,6 @@ describe('controllers/registry/package/dist_tag.test.js', function () {
});
});
it('should not destroy latest tag', function(done) {
request(app.listen())
.delete('/-/package/@cnpmtest/dist_tag_test_module_destroy/dist-tags/latest')
.set('authorization', utils.otherUserAuth)
.set('content-type', 'application/json')
.expect({
error: 'dist_tag_error',
reason: 'Can\'t not delete latest tag',
})
.expect(400, done);
});
it('should 404 destroy not exists tag', function (done) {
request(app.listen())
.delete('/-/package/@cnpmtest/dist_tag_test_module_destroy/dist-tags/not-exists')

View File

@@ -1,9 +1,11 @@
/**!
* Copyright(c) cnpm and other contributors.
* cnpmjs.org - test/controllers/registry/package/download.test.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
@@ -13,66 +15,36 @@
*/
var request = require('supertest');
var mm = require('mm');
var config = require('../../../../config');
var app = require('../../../../servers/registry');
var utils = require('../../../utils');
describe('test/controllers/registry/package/download.test.js', function () {
describe('controllers/registry/package/download.test.js', function () {
before(function (done) {
var pkg = utils.getPackage('@cnpmtest/download-test-module', '1.0.0', utils.admin);
request(app)
request(app.listen())
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
.expect(201, done);
});
afterEach(mm.restore);
describe('GET /:name/download/:filename', function () {
it('should download a file with 200', function (done) {
request(app)
request(app.listen())
.get('/@cnpmtest/download-test-module/download/@cnpmtest/download-test-module-1.0.0.tgz')
.expect(200, done);
});
it('should alias /:name/-/:filename to /:name/download/:filename', function (done) {
request(app)
request(app.listen())
.get('/@cnpmtest/download-test-module/-/@cnpmtest/download-test-module-1.0.0.tgz')
.expect(200, done);
});
it('should 404 when package not exists', function (done) {
request(app)
request(app.listen())
.get('/@cnpmtest/download-test-module-not-exists/download/@cnpmtest/download-test-module-not-exists-1.0.0.tgz')
.expect(404, done);
});
describe('nfs.url is function', function() {
it('should work with nfs.url is generatorFunction', function(done) {
mm(config.nfs, 'url', function*(key) {
return 'http://foo.test.com' + key;
});
mm(config, 'downloadRedirectToNFS', true);
request(app)
.get('/@cnpmtest/download-test-module/-/@cnpmtest/download-test-module-1.0.0.tgz')
.expect('Location', 'http://foo.test.com/@cnpmtest/download-test-module/-/@cnpmtest/download-test-module-1.0.0.tgz')
.expect(302, done);
});
it('should work with nfs.url is function', function(done) {
mm(config.nfs, 'url', function(key) {
return 'http://foo.test.com' + key;
});
mm(config, 'downloadRedirectToNFS', true);
request(app)
.get('/@cnpmtest/download-test-module/-/@cnpmtest/download-test-module-1.0.0.tgz')
.expect('Location', 'http://foo.test.com/@cnpmtest/download-test-module/-/@cnpmtest/download-test-module-1.0.0.tgz')
.expect(302, done);
});
});
});
});

View File

@@ -1,34 +1,38 @@
/**!
* cnpmjs.org - test/controllers/registry/package/download_total.test.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (https://github.com/dead-horse)
*/
'use strict';
const request = require('supertest');
const mm = require('mm');
const DownloadTotal = require('../../../../services/download_total');
const app = require('../../../../servers/registry');
const utils = require('../../../utils');
/**
* Module dependencies.
*/
describe('test/controllers/registry/package/download_total.test.js', () => {
var request = require('supertest');
var mm = require('mm');
var DownloadTotal = require('../../../../services/download_total');
var app = require('../../../../servers/registry');
describe('controllers/registry/package/download_total.test.js', function () {
afterEach(mm.restore);
before(() => {
const pkg2 = utils.getPackage('@cnpmtest/download_total_test_module', '1.0.1', utils.otherUser);
return request(app.listen())
.put('/' + pkg2.name)
.set('authorization', utils.otherUserAuth)
.send(pkg2)
.expect(201);
});
it('should error when range error', () => {
return request(app.listen())
it('should error when range error', function (done) {
request(app.listen())
.get('/downloads/range/2014-10-10:xxxx/koa')
.expect(400)
.expect({
error: 'range_error',
reason: 'range must be YYYY-MM-DD:YYYY-MM-DD style'
});
}, done);
});
it('should get package downloads ok', () => {
it('should get package downloads ok', function (done) {
mm.data(DownloadTotal, 'getModuleTotal', [{
id: 1,
count: 10,
@@ -46,7 +50,7 @@ describe('test/controllers/registry/package/download_total.test.js', () => {
name: 'koa'
}]);
return request(app.listen())
request(app.listen())
.get('/downloads/range/2014-12-01:2014-12-03/koa')
.expect(200)
.expect({
@@ -63,10 +67,10 @@ describe('test/controllers/registry/package/download_total.test.js', () => {
day: '2014-12-03',
downloads: 10
}]
});
}, done);
});
it('should get total downloads ok', () => {
it('should get total downloads ok', function (done) {
mm.data(DownloadTotal, 'getTotal', [{
count: 20,
date: '2014-12-03',
@@ -78,7 +82,7 @@ describe('test/controllers/registry/package/download_total.test.js', () => {
date: '2014-12-02',
}]);
return request(app.listen())
request(app.listen())
.get('/downloads/range/2014-12-01:2014-12-03')
.expect(200)
.expect({
@@ -94,44 +98,6 @@ describe('test/controllers/registry/package/download_total.test.js', () => {
day: '2014-12-03',
downloads: 20
}]
});
});
it('should get scope package downloads ok', () => {
mm.data(DownloadTotal, 'getModuleTotal', [{
id: 1,
count: 10,
date: '2014-12-03',
name: '@cnpmtest/download_total_test_module'
}, {
id: 1,
count: 8,
date: '2014-12-01',
name: '@cnpmtest/download_total_test_module'
}, {
id: 1,
count: 5,
date: '2014-12-02',
name: '@cnpmtest/download_total_test_module'
}]);
return request(app.listen())
.get('/downloads/range/2014-12-01:2014-12-03/@cnpmtest/download_total_test_module')
.expect(200)
.expect({
start: '2014-12-01',
end: '2014-12-03',
package: '@cnpmtest/download_total_test_module',
downloads: [{
day: '2014-12-01',
downloads: 8
}, {
day: '2014-12-02',
downloads: 5
}, {
day: '2014-12-03',
downloads: 10
}]
});
}, done);
});
});

View File

@@ -1,5 +1,19 @@
/**!
* cnpmjs.org - test/controllers/registry/package/list.test.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
/**
* Module dependencies.
*/
var should = require('should');
var request = require('supertest');
var mm = require('mm');
@@ -9,7 +23,7 @@ var app = require('../../../../servers/registry');
var utils = require('../../../utils');
var config = require('../../../../config');
describe('test/controllers/registry/package/list.test.js', () => {
describe('controllers/registry/package/list.test.js', function () {
afterEach(mm.restore);
before(function (done) {
@@ -71,18 +85,10 @@ describe('test/controllers/registry/package/list.test.js', () => {
fengmk2: true,
foouser: true
});
data.versions['0.0.1'].publish_time.should.equal(data.versions['0.0.1']._cnpm_publish_time);
done();
});
});
it('should support jsonp', function (done) {
request(app.listen())
.get('/@cnpmtest/testmodule-list-1?callback=jsonp')
.expect(/jsonp\(\{/)
.expect(200, done);
});
it('should 404 when package not exists', function (done) {
request(app.listen())
.get('/@cnpmtest/not-exists-package')
@@ -122,20 +128,20 @@ describe('test/controllers/registry/package/list.test.js', () => {
}, done);
});
describe.skip('unpublished', () => {
before(done => {
mm(config, 'syncModel', 'all');
utils.sync('moduletest1', done);
describe('unpublished', function () {
before(function (done) {
utils.sync('tfs', done);
});
it('should show unpublished info', done => {
it('should show unpublished info', function (done) {
mm(config, 'syncModel', 'all');
request(app.listen())
.get('/moduletest1')
.get('/tfs')
.expect(404, function (err, res) {
should.not.exist(err);
var data = res.body;
data.time.unpublished.name.should.equal('dead_horse');
data.time.unpublished.name.should.equal('fengmk2');
data.time.unpublished.description.should.equal('tfs');
done();
});
});

View File

@@ -1,87 +0,0 @@
/**!
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
*/
'use strict';
/**
* Module dependencies.
*/
var request = require('supertest');
var mm = require('mm');
var should = require('should');
var app = require('../../../../servers/registry');
var utils = require('../../../utils');
describe('test/controllers/registry/package/list_by_user.test.js', function () {
var user = 'cnpmjstest_list_by_user';
var userauth = 'Basic ' + new Buffer(user + ':' + user).toString('base64');
afterEach(mm.restore);
before(function (done) {
var pkg = utils.getPackage('@cnpmtest/list_by_user_module1', '1.0.1', user);
request(app)
.put('/' + pkg.name)
.set('authorization', userauth)
.send(pkg)
.expect(201, function (err) {
should.not.exist(err);
var pkg2 = utils.getPackage('@cnpmtest/list_by_user_module2', '2.0.0', user);
request(app)
.put('/' + pkg2.name)
.set('authorization', userauth)
.send(pkg2)
.expect(201, done);
});
});
describe('GET /-/users/:user/packages', function () {
it('should get 200', function (done) {
var url = '/-/users/' + user + '/packages';
request(app)
.get(url)
.expect(function(res) {
var data = res.body;
data.user.name.should.equal(user);
var map = {};
data.packages.forEach(function(pkg) {
map[pkg.name] = pkg;
});
map['@cnpmtest/list_by_user_module1'].should.be.an.Object;
map['@cnpmtest/list_by_user_module1'].should.eql({
name: '@cnpmtest/list_by_user_module1',
description: '',
version: '1.0.1',
});
map['@cnpmtest/list_by_user_module2'].should.be.an.Object;
map['@cnpmtest/list_by_user_module2'].should.eql({
name: '@cnpmtest/list_by_user_module2',
description: '',
version: '2.0.0',
});
})
.expect(200, done);
});
it('should get empty packages list when user not exists', function (done) {
var url = '/-/users/not-exist-username/packages';
request(app)
.get(url)
.expect({
user: {
name: 'not-exist-username',
},
packages: [],
})
.expect(200, done);
});
});
});

View File

@@ -1,54 +0,0 @@
/**!
* Copyright(c) cnpm and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
*/
'use strict';
/**
* Module dependencies.
*/
const pedding = require('pedding');
const request = require('supertest');
const mm = require('mm');
const app = require('../../../../servers/registry');
const utils = require('../../../utils');
describe('test/controllers/registry/package/list_dependents.test.js', function () {
afterEach(mm.restore);
before(function (done) {
done = pedding(2, done);
const pkg = utils.getPackage('@cnpmtest/testmodule-list-dependents1', '1.0.0', utils.admin);
pkg.versions['1.0.0'].dependencies = {
'@cnpmtest/testmodule-list-dependents2': '~1.0.0',
};
request(app.listen())
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
.expect(201, done);
const pkg2 = utils.getPackage('@cnpmtest/testmodule-list-dependents2', '1.0.0', utils.admin);
request(app.listen())
.put('/' + pkg2.name)
.set('authorization', utils.adminAuth)
.send(pkg2)
.expect(201, done);
});
it('should list package dependents', function (done) {
request(app)
.get('/-/package/@cnpmtest/testmodule-list-dependents2/dependents')
.expect({
dependents: [
'@cnpmtest/testmodule-list-dependents1',
],
})
.expect(200, done);
});
});

View File

@@ -5,7 +5,7 @@
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
@@ -23,14 +23,14 @@ var config = require('../../../../config');
var packageService = require('../../../../services/package');
var nfs = require('../../../../common/nfs');
describe('test/controllers/registry/package/remove.test.js', function () {
describe('controllers/registry/package/remove.test.js', function () {
afterEach(mm.restore);
before(function (done) {
var pkg = utils.getPackage('@cnpmtest/testmodule-remove-1', '1.0.0', utils.otherUser);
var pkg = utils.getPackage('@cnpmtest/testmodule-remove-1', '1.0.0', utils.admin);
request(app.listen())
.put('/' + pkg.name)
.set('authorization', utils.otherUserAuth)
.set('authorization', utils.adminAuth)
.send(pkg)
.expect(201, done);
});
@@ -56,13 +56,47 @@ describe('test/controllers/registry/package/remove.test.js', function () {
.expect(404, done);
});
it('should delete 403 when user is not admin', function (done) {
it('should delete 403 when user is not admin on config.enablePrivate = true', function (done) {
mm(config, 'enablePrivate', true);
request(app)
.del('/@cnpmtest/testmodule-remove-1/-rev/1')
.set('authorization', utils.otherUserAuth)
.expect({
error: 'no_perms',
reason: 'Only administrators can unpublish module'
reason: 'Private mode enable, only admin can publish this module'
})
.expect(403, done);
});
it('should 400 when scope not exists', function (done) {
request(app)
.del('/@cnpm-not-exists/testmodule-remove-1/-rev/1')
.set('authorization', utils.otherUserAuth)
.expect({
error: 'invalid scope',
reason: 'scope @cnpm-not-exists not match legal scopes: @cnpm, @cnpmtest'
})
.expect(400, done);
});
it('should 403 when delete non scoped package', function (done) {
request(app)
.del('/testmodule-remove-1/-rev/1')
.set('authorization', utils.otherUserAuth)
.expect({
error: 'no_perms',
reason: 'only allow publish with @cnpm, @cnpmtest scope(s)'
})
.expect(403, done);
});
it('should 403 when user not maintainer', function (done) {
request(app)
.del('/@cnpmtest/testmodule-remove-1/-rev/1')
.set('authorization', utils.otherUserAuth)
.expect({
error: 'forbidden user',
reason: 'cnpmjstest101 not authorized to modify @cnpmtest/testmodule-remove-1'
})
.expect(403, done);
});

View File

@@ -27,10 +27,10 @@ describe('controllers/registry/package/remove_version.test.js', function () {
var lastRev;
before(function (done) {
var pkg = utils.getPackage('@cnpmtest/testmodule-remove_version-1', '0.0.1', utils.otherUser);
var pkg = utils.getPackage('@cnpmtest/testmodule-remove_version-1', '0.0.1', utils.admin);
request(app.listen())
.put('/' + pkg.name)
.set('authorization', utils.otherUserAuth)
.set('authorization', utils.adminAuth)
.send(pkg)
.expect(201, function (err, res) {
should.not.exist(err);
@@ -78,7 +78,7 @@ describe('controllers/registry/package/remove_version.test.js', function () {
.expect(401, done);
});
it('should 403 when not admin', function (done) {
it('should 403 when auth error', function (done) {
request(app)
.del('/@cnpmtest/testmodule-remove_version-1/download/@cnpmtest/testmodule-remove_version-1-0.0.1.tgz/-rev/' + lastRev)
.set('authorization', utils.otherUserAuth)
@@ -94,10 +94,10 @@ describe('controllers/registry/package/remove_version.test.js', function () {
describe('mock error', function () {
before(function (done) {
var pkg = utils.getPackage('@cnpmtest/testmodule-remove_version-1', '0.0.2', utils.otherUser);
var pkg = utils.getPackage('@cnpmtest/testmodule-remove_version-1', '0.0.2', utils.admin);
request(app.listen())
.put('/' + pkg.name)
.set('authorization', utils.otherUserAuth)
.set('authorization', utils.adminAuth)
.send(pkg)
.expect(201, done);
});

View File

@@ -1,9 +1,11 @@
/**!
* cnpmjs.org - test/controllers/registry/package/save.test.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
@@ -20,7 +22,7 @@ var app = require('../../../../servers/registry');
var config = require('../../../../config');
var utils = require('../../../utils');
describe('test/controllers/registry/package/save.test.js', function () {
describe('controllers/registry/package/save.test.js', function () {
afterEach(mm.restore);
describe('no @scoped package', function () {
@@ -286,11 +288,11 @@ describe('test/controllers/registry/package/save.test.js', function () {
});
it('should save dependents', function* () {
var names = yield packageService.listDependents('bytetest2');
var names = yield* packageService.listDependents('bytetest2');
names.should.length(2);
names.should.eql(['@cnpmtest/testmodule-new-1', '@cnpmtest/testmodule-new-2']);
names = yield packageService.listDependents('@cnpmtest/testmodule-new-1');
names = yield* packageService.listDependents('@cnpmtest/testmodule-new-1');
names.should.length(0);
});
});

View File

@@ -1,37 +1,34 @@
/**!
* cnpmjs.org - test/controllers/registry/package/show.test.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
/**
* Module dependencies.
*/
var should = require('should');
var request = require('supertest');
var mm = require('mm');
var app = require('../../../../servers/registry');
var utils = require('../../../utils');
describe('test/controllers/registry/package/show.test.js', function () {
describe('controllers/registry/package/show.test.js', function () {
afterEach(mm.restore);
before(function(done) {
before(function (done) {
var pkg = utils.getPackage('@cnpmtest/testmodule-show', '0.0.1', utils.admin);
request(app.listen())
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
.expect(201, function(err) {
should.not.exist(err);
pkg = utils.getPackage('@cnpmtest/testmodule-show', '1.1.0', utils.admin);
request(app.listen())
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
.expect(201, done);
});
});
before(function(done) {
var pkg = utils.getPackage('@cnpmtest/testmodule-only-beta', '1.0.0-beta.1', utils.admin);
request(app.listen())
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
.expect(201, done);
});
@@ -43,86 +40,11 @@ describe('test/controllers/registry/package/show.test.js', function () {
var data = res.body;
data.name.should.equal('@cnpmtest/testmodule-show');
data.version.should.equal('0.0.1');
data['dist-tags'].should.eql({
latest: '1.1.0',
});
data.dist.tarball.should.containEql('/@cnpmtest/testmodule-show/download/@cnpmtest/testmodule-show-0.0.1.tgz');
data._cnpm_publish_time.should.equal(data.publish_time);
done();
});
});
it('should return max satisfied package with semver range', function (done) {
request(app.listen())
.get('/@cnpmtest/testmodule-show/^1.0.0')
.expect(200, function (err, res) {
should.not.exist(err);
var data = res.body;
data.name.should.equal('@cnpmtest/testmodule-show');
data.version.should.equal('1.1.0');
data['dist-tags'].should.eql({
latest: '1.1.0',
});
data.dist.tarball.should.containEql('/@cnpmtest/testmodule-show/download/@cnpmtest/testmodule-show-1.1.0.tgz');
done();
});
});
it('should return max satisfied package with complex semver range', function (done) {
request(app.listen())
.get('/@cnpmtest/testmodule-show/>1.2.0 <=2 || 0.0.1')
.expect(200, function (err, res) {
should.not.exist(err);
var data = res.body;
data.name.should.equal('@cnpmtest/testmodule-show');
data.version.should.equal('0.0.1');
data['dist-tags'].should.eql({
latest: '1.1.0',
});
data.dist.tarball.should.containEql('/@cnpmtest/testmodule-show/download/@cnpmtest/testmodule-show-0.0.1.tgz');
done();
});
});
it('should return max satisfied package with *', function (done) {
request(app.listen())
.get('/@cnpmtest/testmodule-show/*')
.expect(200, function (err, res) {
should.not.exist(err);
var data = res.body;
data.name.should.equal('@cnpmtest/testmodule-show');
data.version.should.equal('1.1.0');
data['dist-tags'].should.eql({
latest: '1.1.0',
});
data.dist.tarball.should.containEql('/@cnpmtest/testmodule-show/download/@cnpmtest/testmodule-show-1.1.0.tgz');
done();
});
});
it('should return the only beta version', function (done) {
request(app.listen())
.get('/@cnpmtest/testmodule-only-beta/*')
.expect(200, function (err, res) {
should.not.exist(err);
var data = res.body;
data.name.should.equal('@cnpmtest/testmodule-only-beta');
data.version.should.equal('1.0.0-beta.1');
data['dist-tags'].should.eql({
latest: '1.0.0-beta.1',
});
data.dist.tarball.should.containEql('/@cnpmtest/testmodule-only-beta/download/@cnpmtest/testmodule-only-beta-1.0.0-beta.1.tgz');
done();
});
});
it('should support jsonp', function (done) {
request(app.listen())
.get('/@cnpmtest/testmodule-show/0.0.1?callback=jsonp')
.expect(/jsonp\(\{/)
.expect(200, done);
});
it('should return latest tag', function (done) {
request(app.listen())
.get('/@cnpmtest/testmodule-show/latest')
@@ -130,10 +52,7 @@ describe('test/controllers/registry/package/show.test.js', function () {
should.not.exist(err);
var data = res.body;
data.name.should.equal('@cnpmtest/testmodule-show');
data.version.should.equal('1.1.0');
data['dist-tags'].should.eql({
latest: '1.1.0',
});
data.version.should.equal('0.0.1');
done();
});
});
@@ -152,9 +71,6 @@ describe('test/controllers/registry/package/show.test.js', function () {
var data = res.body;
data.name.should.equal('@cnpmtest/testmodule-show');
data.version.should.equal('0.0.1');
data['dist-tags'].should.eql({
latest: '1.1.0',
});
done();
});
});

View File

@@ -51,7 +51,7 @@ describe('controllers/registry/user/show.test.js, GET /-/user/org.couchdb.user:n
.expect(200, function (err, res) {
should.not.exist(err);
res.body.name.should.equal('fengmk2');
// res.body.github.should.equal('fengmk2');
res.body.github.should.equal('fengmk2');
res.body._cnpm_meta.should.have.keys('id', 'npm_user', 'custom_user',
'gmt_modified', 'gmt_create');
res.body._cnpm_meta.npm_user.should.equal(true);

View File

@@ -123,19 +123,13 @@ describe('controllers/sync.test.js', function () {
.get('/sync/pedding/log/123123123')
.expect(404, done);
});
it('should 404 when log id not number', function (done) {
request(webApp.listen())
.get('/sync/pedding/log/info.php')
.expect(404, done);
});
});
describe('scope package', function () {
it('should sync scope package not found', function (done) {
request(webApp.listen())
.put('/sync/@cnpm/not-exists-package')
.expect(201, done);
.expect(404, done);
});
});
});

View File

@@ -1,19 +1,30 @@
/**!
* cnpmjs.org - test/controllers/sync_module_worker.test.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
var should = require('should');
/**
* Module dependencies.
*/
var mm = require('mm');
var thunkify = require('thunkify-wrap');
var request = require('supertest');
var urllib = require('urllib');
var config = require('../../config');
var SyncModuleWorker = require('../../controllers/sync_module_worker');
var logService = require('../../services/module_log');
var packageService = require('../../services/package');
var utils = require('../utils');
var app = require('../../servers/registry');
var User = require('../../models').User;
describe('test/controllers/sync_module_worker.test.js', function () {
describe('controllers/sync_module_worker.test.js', function () {
afterEach(mm.restore);
beforeEach(function () {
@@ -42,9 +53,9 @@ describe('test/controllers/sync_module_worker.test.js', function () {
yield end();
});
it('should not sync private scoped package', function* () {
it('should not sync scoped package', function* () {
var worker = new SyncModuleWorker({
name: '@cnpmtest/google',
name: '@scoped/google',
username: 'fengmk2',
});
worker.start();
@@ -52,47 +63,6 @@ describe('test/controllers/sync_module_worker.test.js', function () {
yield end();
});
it('should sync public scoped package', function* () {
mm(config, 'registryHost', '');
mm(config, 'sourceNpmRegistry', 'https://registry.npmjs.org');
var worker = new SyncModuleWorker({
name: '@sindresorhus/df',
username: 'fengmk2',
noDep: true,
});
worker.start();
var end = thunkify.event(worker, 'end');
yield end();
// sync again
var worker = new SyncModuleWorker({
name: '@sindresorhus/df',
username: 'fengmk2',
});
worker.start();
var end = thunkify.event(worker, 'end');
yield end();
var tgzUrl;
function checkResult() {
return function (done) {
request(app.listen())
.get('/@sindresorhus/df')
.expect(function (res) {
var latest = res.body.versions[res.body['dist-tags']['latest']];
tgzUrl = latest.dist.tarball;
})
.expect(200, done);
};
}
yield checkResult();
var r = yield urllib.request(tgzUrl);
console.log(r.status, r.headers);
r.status.should.equal(200);
});
it('should start a sync worker and dont sync deps', function* () {
var log = yield* logService.create({
name: 'byte',
@@ -181,7 +151,7 @@ describe('test/controllers/sync_module_worker.test.js', function () {
it('should sync unpublished info', function (done) {
var worker = new SyncModuleWorker({
name: ['afp'],
name: ['tnpm'],
username: 'fengmk2'
});
@@ -189,7 +159,7 @@ describe('test/controllers/sync_module_worker.test.js', function () {
worker.on('end', function () {
var names = worker.successes.concat(worker.fails);
names.sort();
names.should.eql([ 'afp' ]);
names.should.eql(['tnpm']);
done();
});
});
@@ -279,44 +249,6 @@ describe('test/controllers/sync_module_worker.test.js', function () {
});
});
describe('sync deprecated info', () => {
before(function* () {
mm(config, 'syncModel', 'all');
const worker = new SyncModuleWorker({
name: 'ms',
username: 'fengmk2',
});
worker.start();
const end = thunkify.event(worker, 'end');
yield end();
});
it('should sync support un-deprecate action', function* () {
const listModulesByName = packageService.listModulesByName;
mm(packageService, 'listModulesByName', function* (name) {
const mods = yield listModulesByName.call(packageService, name);
mods.forEach(function (mod) {
mod.package.deprecated = 'mock deprecated';
});
return mods;
});
var worker = new SyncModuleWorker({
name: 'ms',
username: 'fengmk2',
});
worker.start();
const end = thunkify.event(worker, 'end');
yield end();
mm.restore();
// check deprecated
const mods = yield packageService.listModulesByName('ms');
for (const mod of mods) {
should.ok(mod.package.deprecated === undefined);
}
});
});
describe('sync user', function () {
it('should sync fengmk2', function* () {
var worker = new SyncModuleWorker({
@@ -328,64 +260,5 @@ describe('test/controllers/sync_module_worker.test.js', function () {
var end = thunkify.event(worker, 'end');
yield end();
});
describe('sync deleted user', function() {
before(function*() {
var user = {
name: 'notexistsuserscnpmtest',
email: 'notexistsuserscnpmtest@gmail.com',
};
yield User.saveNpmUser(user);
var user = {
name: 'existsuserscnpmtest',
email: 'existsuserscnpmtest@gmail.com',
password_sha: '0',
salt: '0',
ip: '127.0.0.1',
};
yield User.add(user);
});
it('should not delete when cnpm user exists', function*() {
var worker = new SyncModuleWorker({
type: 'user',
name: 'existsuserscnpmtest',
username: 'fengmk2',
});
worker.start();
var end = thunkify.event(worker, 'end');
yield end();
var user = yield User.findByName('existsuserscnpmtest');
should.exists(user);
user.name.should.equal('existsuserscnpmtest');
});
it('should delete when user exists', function*() {
var worker = new SyncModuleWorker({
type: 'user',
name: 'notexistsuserscnpmtest',
username: 'fengmk2',
});
worker.start();
var end = thunkify.event(worker, 'end');
yield end();
var user = yield User.findByName('notexistsuserscnpmtest');
should.not.exists(user);
});
it('should not delete when user not exists', function*() {
var worker = new SyncModuleWorker({
type: 'user',
name: 'notexistsuserscnpmtest',
username: 'fengmk2',
});
worker.start();
var end = thunkify.event(worker, 'end');
yield end();
var user = yield User.findByName('notexistsuserscnpmtest');
should.not.exists(user);
});
});
});
});

View File

@@ -19,7 +19,7 @@ var should = require('should');
var request = require('supertest');
var app = require('../../servers/registry');
describe('test/controllers/total.test.js', function () {
describe('controllers/total.test.js', function () {
describe('GET / in registry', function () {
it('should return total info', function (done) {
request(app.listen())
@@ -29,14 +29,7 @@ describe('test/controllers/total.test.js', function () {
res.body.db_name.should.equal('registry');
res.body.store_engine.should.be.a.String;
res.body.node_version.should.equal(process.version);
res.body.cache_time.should.be.a.Number;
// request again should get cache total info
request(app.listen())
.get('/')
.expect(function(res2) {
res2.body.cache_time.should.equal(res.body.cache_time);
})
.expect(200, done);
done();
});
});

View File

@@ -5,7 +5,7 @@
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
@@ -20,7 +20,6 @@ var mm = require('mm');
var app = require('../../../servers/web');
var registry = require('../../../servers/registry');
var utils = require('../../utils');
var config = require('../../../config')
describe('controllers/web/badge.test.js', function () {
afterEach(mm.restore);
@@ -36,7 +35,7 @@ describe('controllers/web/badge.test.js', function () {
should.not.exists(err);
request(app)
.get('/badge/v/@cnpmtest/badge-test-module.svg?style=flat-square')
.expect('Location', config.badgePrefixURL + '/cnpm-1.0.1-blue.svg?style=flat-square')
.expect('Location', 'https://img.shields.io/badge/cnpm-1.0.1-blue.svg?style=flat-square')
.expect(302, done);
});
});
@@ -51,22 +50,7 @@ describe('controllers/web/badge.test.js', function () {
should.not.exists(err);
request(app)
.get('/badge/v/@cnpmtest/badge-test-module.svg?style=flat-square&tag=v2')
.expect('Location', config.badgePrefixURL + '/cnpm-2.0.1-blue.svg?style=flat-square')
.expect(302, done);
});
});
it('should support custom subject', function (done) {
var pkg = utils.getPackage('@cnpmtest/badge-test-module', '3.0.1', utils.admin, 'v3');
request(registry)
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
.end(function (err) {
should.not.exists(err);
request(app)
.get('/badge/v/@cnpmtest/badge-test-module.svg?style=flat-square&tag=v3&subject=ant-design')
.expect('Location', config.badgePrefixURL + '/ant--design-3.0.1-blue.svg?style=flat-square')
.expect('Location', 'https://img.shields.io/badge/cnpm-2.0.1-blue.svg?style=flat-square')
.expect(302, done);
});
});
@@ -81,7 +65,7 @@ describe('controllers/web/badge.test.js', function () {
should.not.exists(err);
request(app)
.get('/badge/v/@cnpmtest/badge-test-module.svg?style=flat-square')
.expect('Location', config.badgePrefixURL + '/cnpm-1.0.0--beta1-blue.svg?style=flat-square')
.expect('Location', 'https://img.shields.io/badge/cnpm-1.0.0--beta1-blue.svg?style=flat-square')
.expect(302, done);
});
});
@@ -96,7 +80,7 @@ describe('controllers/web/badge.test.js', function () {
should.not.exists(err);
request(app)
.get('/badge/v/@cnpmtest/badge-test-module.svg?style=flat-square')
.expect('Location', config.badgePrefixURL + '/cnpm-0.1.0-green.svg?style=flat-square')
.expect('Location', 'https://img.shields.io/badge/cnpm-0.1.0-green.svg?style=flat-square')
.expect(302, done);
});
});
@@ -111,7 +95,7 @@ describe('controllers/web/badge.test.js', function () {
should.not.exists(err);
request(app)
.get('/badge/v/@cnpmtest/badge-test-module.svg?style=flat-square')
.expect('Location', config.badgePrefixURL + '/cnpm-0.0.0-red.svg?style=flat-square')
.expect('Location', 'https://img.shields.io/badge/cnpm-0.0.0-red.svg?style=flat-square')
.expect(302, done);
});
});
@@ -119,25 +103,8 @@ describe('controllers/web/badge.test.js', function () {
it('should show invalid when package not exists', function (done) {
request(app)
.get('/badge/v/@cnpmtest/badge-test-module-not-exists.svg?style=flat')
.expect('Location', config.badgePrefixURL + '/cnpm-invalid-lightgrey.svg?style=flat')
.expect('Location', 'https://img.shields.io/badge/cnpm-invalid-lightgrey.svg?style=flat')
.expect(302, done);
});
});
describe('GET /badge/d/:name.svg', function () {
it('should show downloads count', function (done) {
var pkg = utils.getPackage('@cnpmtest/badge-download-module', '1.0.1', utils.admin);
request(registry)
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
.end(function (err) {
should.not.exists(err);
request(app)
.get('/badge/d/@cnpmtest/badge-download-module.svg?style=flat-square')
.expect('Location', config.badgePrefixURL + '/downloads-0-brightgreen.svg?style=flat-square')
.expect(302, done);
});
});
});
});

View File

@@ -6,7 +6,6 @@
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/
'use strict';
@@ -22,9 +21,6 @@ var registry = require('../../../../servers/registry');
var utils = require('../../../utils');
describe('controllers/web/package/search.test.js', function () {
var app_reg;
before(function (done) {
var pkg = utils.getPackage('@cnpmtest/testmodule-web-search', '0.0.1', utils.admin);
pkg.versions['0.0.1'].dependencies = {
@@ -32,10 +28,7 @@ describe('controllers/web/package/search.test.js', function () {
mocha: '~1.0.0',
'testmodule-web-show': '0.0.1'
};
app_reg = registry.listen();
request(app_reg)
request(registry.listen())
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
@@ -56,74 +49,14 @@ describe('controllers/web/package/search.test.js', function () {
request(app)
.get('/browse/keyword/@cnpmtest/testmodule-web-search?type=json')
.expect(200)
.expect({
keyword: '@cnpmtest/testmodule-web-search',
match: { name: '@cnpmtest/testmodule-web-search', description: '' },
packages: [ { name: '@cnpmtest/testmodule-web-search', description: '' } ],
keywords: []
})
.expect('content-type', 'application/json; charset=utf-8', done);
});
it('should search with jsonp work', function (done) {
request(app)
.get('/browse/keyword/@cnpmtest/testmodule-web-search?type=json&callback=foo')
.expect(200)
.expect('/**/ typeof foo === \'function\' && foo({"keyword":"@cnpmtest/testmodule-web-search","match":{"name":"@cnpmtest/testmodule-web-search","description":""},"packages":[{"name":"@cnpmtest/testmodule-web-search","description":""}],"keywords":[]});')
.expect('content-type', 'application/javascript; charset=utf-8', done);
});
it('should list no match ok', function (done) {
request(app)
.get('/browse/keyword/notexistpackage')
.expect(200)
.expect(/Can not found package match notexistpackage/, done);
});
describe('GET /browse/keyword/:word searchlist', function () {
before(function (done) {
var pkg = utils.getPackage('@cnpmtest/testmodule-web-search_a', '0.0.1', utils.admin);
pkg.versions['0.0.1'].dependencies = {
bytetest: '~0.0.1',
mocha: '~1.0.0',
'testmodule-web-show': '0.0.1'
};
request(app_reg)
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
.expect(201, done);
});
it('should list by keyword with json(default limit=100)', function (done) {
request(app)
.get('/browse/keyword/@cnpmtest/testmodule-web-searc?type=json')
.expect(200)
.expect({
keyword: '@cnpmtest/testmodule-web-searc',
match: null,
packages: [ { name: '@cnpmtest/testmodule-web-search', description: '' },
{ name: '@cnpmtest/testmodule-web-search_a', description: '' }],
keywords: []
})
.expect('content-type', 'application/json; charset=utf-8', done);
});
it('should list by keyword with json(use limit)', function (done) {
request(app)
.get('/browse/keyword/@cnpmtest/testmodule-web-searc?type=json&limit=1')
.expect(200)
.expect({
keyword: '@cnpmtest/testmodule-web-searc',
match: null,
packages: [ { name: '@cnpmtest/testmodule-web-search', description: '' }],
keywords: []
})
.expect('content-type', 'application/json; charset=utf-8', done);
});
});
});
});

View File

@@ -1,5 +1,19 @@
/*!
* cnpmjs.org - test/controllers/web/package/show.test.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/
'use strict';
/**
* Module dependencies.
*/
var should = require('should');
var request = require('supertest');
var mm = require('mm');
@@ -8,7 +22,7 @@ var app = require('../../../../servers/web');
var registry = require('../../../../servers/registry');
var utils = require('../../../utils');
describe('test/controllers/web/package/show.test.js', () => {
describe('controllers/web/package/show.test.js', function () {
before(function (done) {
var pkg = utils.getPackage('@cnpmtest/testmodule-web-show', '0.0.1', utils.admin);
pkg.versions['0.0.1'].dependencies = {
@@ -36,7 +50,7 @@ describe('test/controllers/web/package/show.test.js', () => {
.expect(/Dependencies/)
.expect(/Downloads/, function (err, res) {
should.not.exist(err);
should.exist(res.headers.etag);
res.should.have.header('etag');
res.text.should.containEql('<meta charset="utf-8">');
done();
});
@@ -52,7 +66,7 @@ describe('test/controllers/web/package/show.test.js', () => {
.expect(/Dependencies/)
.expect(/Downloads/, function (err, res) {
should.not.exist(err);
should.exist(res.headers.etag);
res.should.have.header('etag');
res.text.should.containEql('<meta charset="utf-8">');
done();
});
@@ -99,25 +113,23 @@ describe('test/controllers/web/package/show.test.js', () => {
});
});
describe.skip('unpublished package', () => {
before(done => {
mm(config, 'syncModel', 'all');
utils.sync('mk2testmodule', done);
describe('unpublished package', function () {
before(function (done) {
utils.sync('tnpm', done);
});
it('should display unpublished info', () => {
it('should display unpublished info', function (done) {
mm(config, 'syncModel', 'all');
return request(app)
.get('/package/mk2testmodule')
.expect(200)
.expect(/This package has been unpublished\./);
request(app)
.get('/package/tnpm')
.expect(200)
.expect(/This package has been unpublished\./, done);
});
});
describe('xss filter', function () {
before(function (done) {
var pkg = utils.getPackage('@cnpmtest/xss-test-ut', '0.0.1', utils.admin,
null, '[xss link](javascript:alert(2)) \n\nfoo<script>alert(1)</script>/xss\'"&#');
var pkg = utils.getPackage('@cnpmtest/xss-test-ut', '0.0.1', utils.admin, null, '[xss link](javascript:alert(2)) \n\nfoo<script>alert(1)</script>/xss\'"&#');
request(registry.listen())
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
@@ -137,214 +149,20 @@ describe('test/controllers/web/package/show.test.js', () => {
});
});
describe('show npm package', () => {
before(done => {
describe('show npm package', function () {
before(function (done) {
mm(config, 'syncModel', 'exists');
utils.sync('pedding', done);
});
it('should show pedding package info and contributors', () => {
it('should show pedding package info and contributors', function (done) {
mm(config, 'syncModel', 'exists');
return request(app)
.get('/package/pedding')
.expect(200)
// https://github.com/cnpm/cnpmjs.org/issues/497
.expect(/by <a href="\/\~fengmk2">fengmk2<\/a>/)
// snyk link
.expect(/<a class="badge-link" href="https:\/\/snyk\.io\/test\/npm\/pedding" target="_blank"><img title="Known Vulnerabilities" src="https:\/\/snyk\.io\/test\/npm\/pedding\/badge\.svg\?style=flat-square"><\/a>/)
.expect(/pedding/);
});
});
describe('show repository url in git syntax', () => {
before(function (done) {
var pkg = utils.getPackage('@cnpmtest/testmodule-repo-git', '0.0.1', utils.admin);
pkg.versions['0.0.1'].repository = {
type: 'git',
url: 'git://github.com/cnpm/cnpmjs.org.git'
}
request(registry.listen())
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
.expect(201, done);
});
it('should get 200', function (done) {
request(app.listen())
.get('/package/@cnpmtest/testmodule-repo-git')
request(app)
.get('/package/pedding')
.expect(200)
.expect('content-type', 'text/html; charset=utf-8')
.expect(/testmodule-repo-git/)
.expect(/Maintainers/)
.expect(/Dependencies/)
.expect(/https:\/\/github\.com\/cnpm\/cnpmjs\.org/)
.expect(/Downloads/, function (err, res) {
should.not.exist(err);
should.exist(res.headers.etag);
res.text.should.containEql('<meta charset="utf-8">');
done();
});
});
});
describe('show repository url in ssh syntax', () => {
before(function (done) {
var pkg = utils.getPackage('@cnpmtest/testmodule-repo-ssh', '0.0.1', utils.admin);
pkg.versions['0.0.1'].repository = {
type: 'git',
url: 'git@github.com:cnpm/cnpmjs.org.git'
}
request(registry.listen())
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
.expect(201, done);
});
it('should get 200', function (done) {
request(app.listen())
.get('/package/@cnpmtest/testmodule-repo-ssh')
.expect(200)
.expect('content-type', 'text/html; charset=utf-8')
.expect(/testmodule-repo-ssh/)
.expect(/Maintainers/)
.expect(/Dependencies/)
.expect(/https:\/\/github\.com\/cnpm\/cnpmjs\.org/)
.expect(/Downloads/, function (err, res) {
should.not.exist(err);
should.exist(res.headers.etag);
res.text.should.containEql('<meta charset="utf-8">');
done();
});
});
});
describe('show repository url in raw ssh syntax', () => {
before(function (done) {
var pkg = utils.getPackage('@cnpmtest/testmodule-repo-raw-ssh', '0.0.1', utils.admin);
pkg.versions['0.0.1'].repository = {
type: 'git',
url: 'ssh://git@github.com/cnpm/cnpmjs.org.git'
}
request(registry.listen())
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
.expect(201, done);
});
it('should get 200', function (done) {
request(app.listen())
.get('/package/@cnpmtest/testmodule-repo-raw-ssh')
.expect(200)
.expect('content-type', 'text/html; charset=utf-8')
.expect(/testmodule-repo-raw-ssh/)
.expect(/Maintainers/)
.expect(/Dependencies/)
.expect(/https:\/\/github\.com\/cnpm\/cnpmjs\.org/)
.expect(/Downloads/, function (err, res) {
should.not.exist(err);
should.exist(res.headers.etag);
res.text.should.containEql('<meta charset="utf-8">');
done();
});
});
});
describe('show repository url in https syntax', () => {
before(function (done) {
var pkg = utils.getPackage('@cnpmtest/testmodule-repo-https', '0.0.1', utils.admin);
pkg.versions['0.0.1'].repository = {
type: 'git',
url: 'https://github.com/cnpm/cnpmjs.org.git'
}
request(registry.listen())
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
.expect(201, done);
});
it('should get 200', function (done) {
request(app.listen())
.get('/package/@cnpmtest/testmodule-repo-https')
.expect(200)
.expect('content-type', 'text/html; charset=utf-8')
.expect(/testmodule-repo-https/)
.expect(/Maintainers/)
.expect(/Dependencies/)
.expect(/https:\/\/github\.com\/cnpm\/cnpmjs\.org\.git/)
.expect(/Downloads/, function (err, res) {
should.not.exist(err);
should.exist(res.headers.etag);
res.text.should.containEql('<meta charset="utf-8">');
done();
});
});
});
describe('show repository url in short https syntax', () => {
before(function (done) {
var pkg = utils.getPackage('@cnpmtest/testmodule-repo-short-https', '0.0.1', utils.admin);
pkg.versions['0.0.1'].repository = {
type: 'git',
url: 'https://github.com/cnpm/cnpmjs.org'
}
request(registry.listen())
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
.expect(201, done);
});
it('should get 200', function (done) {
request(app.listen())
.get('/package/@cnpmtest/testmodule-repo-short-https')
.expect(200)
.expect('content-type', 'text/html; charset=utf-8')
.expect(/testmodule-repo-short-https/)
.expect(/Maintainers/)
.expect(/Dependencies/)
.expect(/https:\/\/github\.com\/cnpm\/cnpmjs\.org/)
.expect(/Downloads/, function (err, res) {
should.not.exist(err);
should.exist(res.headers.etag);
res.text.should.containEql('<meta charset="utf-8">');
done();
});
});
});
describe('show repository url in short http syntax', () => {
before(function (done) {
var pkg = utils.getPackage('@cnpmtest/testmodule-repo-short-http', '0.0.1', utils.admin);
pkg.versions['0.0.1'].repository = {
type: 'git',
url: 'http://github.com/cnpm/cnpmjs.org.git'
}
request(registry.listen())
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
.expect(201, done);
});
it('should get 200', function (done) {
request(app.listen())
.get('/package/@cnpmtest/testmodule-repo-short-http')
.expect(200)
.expect('content-type', 'text/html; charset=utf-8')
.expect(/testmodule-repo-short-http/)
.expect(/Maintainers/)
.expect(/Dependencies/)
.expect(/http:\/\/github\.com\/cnpm\/cnpmjs\.org/)
.expect(/Downloads/, function (err, res) {
should.not.exist(err);
should.exist(res.headers.etag);
res.text.should.containEql('<meta charset="utf-8">');
done();
});
// https://github.com/cnpm/cnpmjs.org/issues/497
.expect(/by <a href="\/\~fengmk2">fengmk2<\/a>/)
.expect(/pedding/, done);
});
});
});

View File

@@ -1,3 +1,13 @@
/**!
* cnpmjs.org - test/init.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
/**

View File

@@ -1,3 +1,13 @@
/**!
* cnpmjs.org - test/init_db.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
/**
@@ -12,7 +22,7 @@ var config = require('../config');
// init db first
var initscript = path.join(__dirname, '..', 'models', 'init_script.js');
var cmd = ['node', initscript, 'true',
var cmd = ['node', '--harmony', initscript, 'true',
config.database.dialect, config.database.port, config.database.username].join(' ');
console.log('$ %s', cmd);
var stdout = childProcess.execSync(cmd);
@@ -25,22 +35,13 @@ var usernames = [
'cnpmjstest101',
'cnpmjstest102',
'cnpmjstest103',
['cnpmjstest104', 'cnpmjs:test104'],
'cnpmjstest10', // admin
'cnpmjstestAdmin2', // other admin
'cnpmjstestAdmin3', // other admin
'cnpmjstest_list_by_user',
];
var count = usernames.length;
usernames.forEach(function (name) {
var pass;
if (Array.isArray(name)) {
name = name[0];
pass = name[1];
} else {
pass = name;
}
var user = User.build({
name: name,
email: 'fengmk2@gmail.com',
@@ -48,7 +49,7 @@ usernames.forEach(function (name) {
rev: '1',
});
user.salt = crypto.randomBytes(30).toString('hex');
user.password_sha = User.createPasswordSha(pass, user.salt);
user.password_sha = User.createPasswordSha(name, user.salt);
user.save().then(function () {
count--;
if (count === 0) {

View File

@@ -6,7 +6,7 @@
*
* Authors:
* dead_horse <dead_horse@qq.com>
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
@@ -17,7 +17,7 @@
var common = require('../../lib/common');
describe('test/lib/common.test.js', function () {
describe('lib/common.test.js', function () {
describe('isAdmin()', function () {
it('should admin is admin', function () {
common.isAdmin('admin').should.equal(true);
@@ -26,13 +26,4 @@ describe('test/lib/common.test.js', function () {
common.isAdmin('toString').should.equal(false);
});
});
describe('getCDNKey()', function () {
it('should auto fix scope filename', function () {
common.getCDNKey('foo', 'foo-1.0.0.tgz').should.equal('/foo/-/foo-1.0.0.tgz');
common.getCDNKey('@bar/foo', 'foo-1.0.0.tgz').should.equal('/@bar/foo/-/@bar/foo-1.0.0.tgz');
common.getCDNKey('@bar/foo', '@bar/foo-1.0.0.tgz').should.equal('/@bar/foo/-/@bar/foo-1.0.0.tgz');
common.getCDNKey('@bar/foo', '@bar1/foo-1.0.0.tgz').should.equal('/@bar/foo/-/@bar1/foo-1.0.0.tgz');
});
});
});

View File

@@ -53,13 +53,6 @@ describe('middleware/auth.test.js', function () {
.expect(200, done);
});
it('should pass with authorization (password contains ":") and check ok', function (done) {
request(app)
.get('/-/user/org.couchdb.user:cnpmjstest104')
.set('authorization', 'basic ' + new Buffer('cnpmjstest104:cnpmjs:test104').toString('base64'))
.expect(200, done);
});
describe('config.customUserService = true', function () {
beforeEach(function () {
mm(config, 'customUserService', true);

Some files were not shown because too many files have changed in this diff Show More