Compare commits

..

4 Commits
next ... egg

Author SHA1 Message Date
fengmk2
d64ea76647 f 2017-02-10 00:13:55 +08:00
fengmk2
c9f958d194 f 2017-02-09 21:44:40 +08:00
fengmk2
889a9c7ee5 test: use npm scripts 2017-02-09 21:22:03 +08:00
fengmk2
29a7cced3a feat: use eslint 2017-02-09 20:45:23 +08:00
220 changed files with 5382 additions and 9264 deletions

35
.autod.conf.js Normal file
View File

@@ -0,0 +1,35 @@
'ues strict';
module.exports = {
write: true,
prefix: '^',
devprefix: '^',
registry: 'https://r.cnpmjs.org',
exclude: [
'test/fixtures',
'examples',
'docs',
'public',
],
dep: [
'mysql',
'egg',
'koa-router',
],
devdep: [
'autod',
'autod-egg',
'eslint',
'eslint-config-egg',
'egg-bin',
'egg-mock',
],
keep: [
],
semver: [
'changes-stream@1',
'nodemailer@1',
'koa-router@3',
'supertest@2',
],
};

View File

@@ -1,2 +0,0 @@
node_modules
npm-debug.log

6
.eslintignore Normal file
View File

@@ -0,0 +1,6 @@
test/fixtures
test/mocks
examples/**/app/public
logs
run
public/

3
.eslintrc Normal file
View File

@@ -0,0 +1,3 @@
{
"extends": "eslint-config-egg"
}

1
.gitignore vendored
View File

@@ -29,4 +29,3 @@ coverage/
config/web_readme.md
.tmp/
*.sqlite

View File

@@ -1,7 +0,0 @@
node_modules/
coverage/
.tmp/
.git/
tools/
public/
test/

View File

@@ -1,95 +0,0 @@
{
// JSHint Default Configuration File (as on JSHint website)
// See http://jshint.com/docs/ for more details
"maxerr" : 50, // {int} Maximum error before stopping
// Enforcing
"bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.)
"camelcase" : false, // true: Identifiers must be in camelCase
"curly" : true, // true: Require {} for every new block or scope
"eqeqeq" : true, // true: Require triple equals (===) for comparison
"forin" : false, // true: Require filtering for..in loops with obj.hasOwnProperty()
"immed" : false, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());`
"indent" : false, // {int} Number of spaces to use for indentation
"latedef" : false, // true: Require variables/functions to be defined before being used
"newcap" : false, // true: Require capitalization of all constructor functions e.g. `new F()`
"noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee`
"noempty" : true, // true: Prohibit use of empty blocks
"nonew" : false, // true: Prohibit use of constructors for side-effects (without assignment)
"plusplus" : false, // true: Prohibit use of `++` & `--`
"quotmark" : false, // Quotation mark consistency:
// false : do nothing (default)
// true : ensure whatever is used is consistent
// "single" : require single quotes
// "double" : require double quotes
"undef" : true, // true: Require all non-global variables to be declared (prevents global leaks)
"unused" : true, // true: Require all defined variables be used
"strict" : true, // true: Requires all functions run in ES5 Strict Mode
"trailing" : false, // true: Prohibit trailing whitespaces
"maxparams" : false, // {int} Max number of formal params allowed per function
"maxdepth" : false, // {int} Max depth of nested blocks (within functions)
"maxstatements" : false, // {int} Max number statements per function
"maxcomplexity" : false, // {int} Max cyclomatic complexity per function
"maxlen" : false, // {int} Max number of characters per line
// Relaxing
"asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons)
"boss" : true, // true: Tolerate assignments where comparisons would be expected
"debug" : false, // true: Allow debugger statements e.g. browser breakpoints.
"eqnull" : false, // true: Tolerate use of `== null`
"es5" : false, // true: Allow ES5 syntax (ex: getters and setters)
"esnext" : true, // true: Allow ES.next (ES6) syntax (ex: `const`)
"moz" : false, // true: Allow Mozilla specific syntax (extends and overrides esnext features)
// (ex: `for each`, multiple try/catch, function expression…)
"evil" : false, // true: Tolerate use of `eval` and `new Function()`
"expr" : true, // true: Tolerate `ExpressionStatement` as Programs
"funcscope" : false, // true: Tolerate defining variables inside control statements"
"globalstrict" : false, // true: Allow global "use strict" (also enables 'strict')
"iterator" : false, // true: Tolerate using the `__iterator__` property
"lastsemic" : false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block
"laxbreak" : true, // true: Tolerate possibly unsafe line breakings
"laxcomma" : false, // true: Tolerate comma-first style coding
"loopfunc" : false, // true: Tolerate functions being defined in loops
"multistr" : true, // true: Tolerate multi-line strings
"proto" : false, // true: Tolerate using the `__proto__` property
"scripturl" : false, // true: Tolerate script-targeted URLs
"smarttabs" : false, // true: Tolerate mixed tabs/spaces when used for alignment
"shadow" : true, // true: Allows re-define variables later in code e.g. `var x=1; x=2;`
"sub" : false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation
"supernew" : false, // true: Tolerate `new function () { ... };` and `new Object;`
"validthis" : true, // true: Tolerate using this in a non-constructor function
// Environments
"browser" : true, // Web Browser (window, document, etc)
"couch" : false, // CouchDB
"devel" : true, // Development/debugging (alert, confirm, etc)
"dojo" : false, // Dojo Toolkit
"jquery" : false, // jQuery
"mootools" : false, // MooTools
"node" : true, // Node.js
"nonstandard" : false, // Widely adopted globals (escape, unescape, etc)
"prototypejs" : false, // Prototype and Scriptaculous
"rhino" : false, // Rhino
"worker" : false, // Web Workers
"wsh" : false, // Windows Scripting Host
"yui" : false, // Yahoo User Interface
"noyield" : true, // allow generators without a yield
// Legacy
"nomen" : false, // true: Prohibit dangling `_` in variables
"onevar" : false, // true: Allow only one `var` statement per function
"passfail" : false, // true: Stop on first error
"white" : false, // true: Check against strict whitespace and indentation rules
// Custom Globals
"globals" : { // additional predefined global variables
// mocha
"describe": true,
"it": true,
"before": true,
"afterEach": true,
"beforeEach": true,
"after": true
}
}

View File

@@ -1,3 +0,0 @@
{
"notify": false
}

View File

@@ -1,11 +1,11 @@
sudo: false
language: node_js
node_js:
- '8'
- '10'
services:
- mysql
- postgresql
script: 'make test-travis-all'
- '6'
- '7'
addons:
- postgresql: '9.3'
script:
- TEST_TIMEOUT=60000 CNPM_SOURCE_NPM=https://registry.npmjs.org CNPM_SOURCE_NPM_ISCNPM=false npm run test-all
after_script:
- 'npm i codecov && codecov'
- npm i codecov && codecov

View File

@@ -1,28 +0,0 @@
FROM node:12
MAINTAINER Bono Lv <lvscar {aT} gmail.com>
# Working enviroment
ENV \
CNPM_DIR="/var/app/cnpmjs.org" \
CNPM_DATA_DIR="/var/data/cnpm_data"
RUN mkdir -p ${CNPM_DIR}
WORKDIR ${CNPM_DIR}
COPY package.json ${CNPM_DIR}
RUN npm set registry https://registry.npm.taobao.org
RUN npm install --production
COPY . ${CNPM_DIR}
COPY docs/dockerize/config.js ${CNPM_DIR}/config/
EXPOSE 7001/tcp 7002/tcp
VOLUME ["/var/data/cnpm_data"]
# Entrypoint
CMD ["node", "dispatch.js"]

View File

@@ -1,152 +1,3 @@
3.0.0-rc.39 / 2021-01-14
==================
**features**
* [[`33dd355`](http://github.com/cnpm/cnpmjs.org/commit/33dd3554f5daf13de33f04128be6853ce120636f)] - feat: impl dist-tag hooks (#1612) (killa <<killa123@126.com>>)
3.0.0-rc.38 / 2021-01-12
==================
**features**
* [[`c6040b`](http://github.com/cnpm/cnpmjs.org/commit/1c6040bc95610e23b4756baa09e8119cda2fe01e)] - performance: optimise query pkg latestModified (#1611) (killa <<killa123@126.com>>)
3.0.0-rc.37 / 2020-10-21
==================
**features**
* [[`39c3223`](http://github.com/cnpm/cnpmjs.org/commit/39c322332ffafc512bf56c1679d2904fece2ae07)] - feat: new registry api (#1597) (killa <<killa123@126.com>>)
* [[`45f2f8b`](http://github.com/cnpm/cnpmjs.org/commit/45f2f8b31f095eeadf0f47e234d6eb225e6b197f)] - feat: impl registry token api (#1590) (killa <<killa123@126.com>>)
* [[`e97835f`](http://github.com/cnpm/cnpmjs.org/commit/e97835f7020e945e59fa7a84b14ab58c580add1e)] - feat: support custom web middlewares (#1563) (fengmk2 <<fengmk2@gmail.com>>)
* [[`3cb3fe0`](http://github.com/cnpm/cnpmjs.org/commit/3cb3fe02f01dd669ad4bd3aebca51c44eb9e5938)] - feat: list all package versions by date (#1557) (fengmk2 <<fengmk2@gmail.com>>)
* [[`a8ff647`](http://github.com/cnpm/cnpmjs.org/commit/a8ff647aa0f73076f4625e395e5da8ced9f61680)] - feat: retry sync fail on cnpm registry (#1547) (fengmk2 <<fengmk2@gmail.com>>)
* [[`2c511f2`](http://github.com/cnpm/cnpmjs.org/commit/2c511f2209329e95b0cbe7603fa98a7f93c66474)] - feat: add unpublishRemoveTarball mode (#1536) (Khaidi Chu <<i@2333.moe>>)
* [[`19563f5`](http://github.com/cnpm/cnpmjs.org/commit/19563f58517ffaebed8006630bd467f15b71d9ff)] - feat: allow to disable npm audits proxy (#1430) (fengmk2 <<fengmk2@gmail.com>>)
* [[`8e2367e`](http://github.com/cnpm/cnpmjs.org/commit/8e2367ee1676bd36a4112cf0f6dce2c4f422806e)] - feat: dont check db data on tgz download request (#1477) (fengmk2 <<fengmk2@gmail.com>>)
* [[`be05886`](http://github.com/cnpm/cnpmjs.org/commit/be05886452803d46f614bdde497ccdec8e9ed734)] - feat: add vary header on cdn (fengmk2 <<fengmk2@gmail.com>>)
* [[`ea46399`](http://github.com/cnpm/cnpmjs.org/commit/ea46399265615c70ee33d9cab9ba5d5ce312fb67)] - feat: allow disable search page (fengmk2 <<fengmk2@gmail.com>>)
* [[`581925d`](http://github.com/cnpm/cnpmjs.org/commit/581925db9733d295be02e75f0090db05fd6bae75)] - feat: support cache-control header on registry request (#1468) (fengmk2 <<fengmk2@gmail.com>>)
* [[`7f0c141`](http://github.com/cnpm/cnpmjs.org/commit/7f0c141ac2f7679b5322aadd537c5ff1bef0b032)] - feat: allow config request protocol (fengmk2 <<fengmk2@gmail.com>>)
* [[`807187e`](http://github.com/cnpm/cnpmjs.org/commit/807187ebeb0b266828a59724234e1a99c3238eb3)] - feat: add redis cache to import list all versions api perf (#1441) (fengmk2 <<fengmk2@gmail.com>>)
* [[`99c4c3f`](http://github.com/cnpm/cnpmjs.org/commit/99c4c3fe35a9fde805751ef3d44413413f053f45)] - feat: support customized middlewares (#1436) (Khaidi Chu <<i@2333.moe>>)
* [[`4b57c11`](http://github.com/cnpm/cnpmjs.org/commit/4b57c118a0b044f41b1c98eaf92449221c984c15)] - feat: can override tgz download options (fengmk2 <<fengmk2@gmail.com>>)
* [[`b395c66`](http://github.com/cnpm/cnpmjs.org/commit/b395c666be3ae6b237803239fae8678647f3b70b)] - feat: proxy npm audit request (#1419) (alsotang <<alsotang@gmail.com>>)
* [[`a4a25f9`](http://github.com/cnpm/cnpmjs.org/commit/a4a25f9e381aa20e1ef1e709f320aae41f3ae466)] - feat: use faster etag instead of koa-etag (#1409) (fengmk2 <<fengmk2@gmail.com>>)
* [[`90580a7`](http://github.com/cnpm/cnpmjs.org/commit/90580a72e56c69f8f03bbdb64d79b4b1b139fbbf)] - feat: configurable view directory (#1400) (Khaidi Chu <<i@2333.moe>>)
* [[`ad2d341`](http://github.com/cnpm/cnpmjs.org/commit/ad2d341d2c9317b062a25363f4805aedfef3913b)] - feat: sync downloads total <= 10000 unpublish package (fengmk2 <<fengmk2@gmail.com>>)
* [[`25a9030`](http://github.com/cnpm/cnpmjs.org/commit/25a90300473e6ac437e393de139cebde1e354e8c)] - feat: allow to close mysql trace (fengmk2 <<fengmk2@gmail.com>>)
* [[`017af69`](http://github.com/cnpm/cnpmjs.org/commit/017af69cce23c870694d124f4a865864e5c061cd)] - feat: add badgeService define on config (#1387) (fengmk2 <<fengmk2@gmail.com>>)
* [[`842c031`](http://github.com/cnpm/cnpmjs.org/commit/842c0316ede2b19b76d9c1ca790902de467c82e9)] - feat: show versions list on package page (#1386) (fengmk2 <<fengmk2@gmail.com>>)
* [[`bd87907`](http://github.com/cnpm/cnpmjs.org/commit/bd87907b69d3e65aa544930b5c7f04e75bdbc773)] - feat: auto retry if download tgz error (#1363) (fengmk2 <<fengmk2@gmail.com>>)
* [[`533c27f`](http://github.com/cnpm/cnpmjs.org/commit/533c27fa78323ee50fcd549115034915ea3017ef)] - feat: support nfs.url return multi urls (#1344) (fengmk2 <<fengmk2@gmail.com>>)
* [[`e61c7fa`](http://github.com/cnpm/cnpmjs.org/commit/e61c7fa32bdc54ef4474a071da686c70e512b009)] - feat: support pass through querystring to tgz url (#1334) (fengmk2 <<fengmk2@gmail.com>>)
* [[`34d3a1e`](http://github.com/cnpm/cnpmjs.org/commit/34d3a1eabe927dc5c8c87436e2b644c70a7abc2a)] - feat: auto sync delete packages which deleted in 24 hours (#1315) (fengmk2 <<fengmk2@gmail.com>>)
* [[`4210b7b`](http://github.com/cnpm/cnpmjs.org/commit/4210b7bdf8bfe8dfa2578802fd1d14e7411d4ea6)] - feat: can config to not sync deleted versions (#1282) (fengmk2 <<fengmk2@gmail.com>>)
* [[`56c9457`](http://github.com/cnpm/cnpmjs.org/commit/56c945740f545abe9ba55759f6b1502a3abc453d)] - feat: let opensearch host can be config (#1258) (fengmk2 <<fengmk2@gmail.com>>)
**fixes**
* [[`b7089d3`](http://github.com/cnpm/cnpmjs.org/commit/b7089d33d400f9fd4fc398479d4dac5aab26b633)] - fix: set maintainer to current user if maintainer is undefined (#1592) (killa <<killa123@126.com>>)
* [[`2b74e00`](http://github.com/cnpm/cnpmjs.org/commit/2b74e00cb9ae20e9cf2f06c54ef8dbe6a36b4066)] - fix: release 3.0.0-rc.35 fix npm include functions dir (fengmk2 <<fengmk2@gmail.com>>)
* [[`61549b4`](http://github.com/cnpm/cnpmjs.org/commit/61549b47a2f49c163bef6994f1e0f5f761317975)] - fix: avoid "ENAMETOOLONG: name too long" error (#1583) (fengmk2 <<fengmk2@gmail.com>>)
* [[`e7bafb2`](http://github.com/cnpm/cnpmjs.org/commit/e7bafb2ee9d80ce3ef4087a6b69bc17517f85ec5)] - fix: audit proxy test cases (#1537) (Khaidi Chu <<i@2333.moe>>)
* [[`92b7216`](http://github.com/cnpm/cnpmjs.org/commit/92b72169a89cec333177d1ba65205a31e60ebbb2)] - fix: maintainer permission greater than scope (#1494) (Khaidi Chu <<i@2333.moe>>)
* [[`f084eba`](http://github.com/cnpm/cnpmjs.org/commit/f084ebae2106c8d4435dc0385e493fe18c6cec8a)] - fix: cpu usage 100% in node@6.x (#1470) (Yiman Liu <<413893093@qq.com>>)
* [[`8d57216`](http://github.com/cnpm/cnpmjs.org/commit/8d572169b7293a33035257d3525c66b0abb5b679)] - fix: add cache on total (fengmk2 <<fengmk2@gmail.com>>)
* [[`585f55b`](http://github.com/cnpm/cnpmjs.org/commit/585f55bbcc0ce257bfab2f0f545dd8a89c66ca49)] - fix: download url pathname (fengmk2 <<fengmk2@gmail.com>>)
* [[`da2f964`](http://github.com/cnpm/cnpmjs.org/commit/da2f9640b87f1b110210b7b8caaf26b4b854ede8)] - fix: dont override exists weburl (fengmk2 <<fengmk2@gmail.com>>)
* [[`b094f56`](http://github.com/cnpm/cnpmjs.org/commit/b094f5692f83700f152dd6ea9eb65f67385f6b5f)] - fix: changes stream syncer without deps (fengmk2 <<fengmk2@gmail.com>>)
* [[`65bca46`](http://github.com/cnpm/cnpmjs.org/commit/65bca46f3c275bac5dc7497eb266d84605f6f8f8)] - fix: don't cache npm_service.cnpmjs.org request (fengmk2 <<fengmk2@gmail.com>>)
* [[`f9d4858`](http://github.com/cnpm/cnpmjs.org/commit/f9d4858862a4b70cb989c3c60478c2424ca2c139)] - fix: avoid toString as downloads count key (#1438) (fengmk2 <<fengmk2@gmail.com>>)
* [[`8a2f744`](http://github.com/cnpm/cnpmjs.org/commit/8a2f744749fc9f1297ff298fafe14deacf67efea)] - fix: don't update __all__ downloads every times (#1417) (fengmk2 <<fengmk2@gmail.com>>)
* [[`9bdb695`](http://github.com/cnpm/cnpmjs.org/commit/9bdb695375a800464636d70981f433b7a11dd82d)] - fix: proxy to source registry when package is public scoped with encoded path (#1415) (Albert Zhang <<label4king@163.com>>)
* [[`8bd0a2d`](http://github.com/cnpm/cnpmjs.org/commit/8bd0a2d49195734afa988cce69804d8540bbda19)] - fix: swap compress middleware and notFound position (#1413) (alsotang <<alsotang@gmail.com>>)
* [[`93d5def`](http://github.com/cnpm/cnpmjs.org/commit/93d5def8ac8882edbd526e5a7341e07c99463b25)] - fix: show package when non-semver version of semver tag (#1411) (Khaidi Chu <<i@2333.moe>>)
* [[`6a8434e`](http://github.com/cnpm/cnpmjs.org/commit/6a8434e0cae391981579af1a0b533aff0008904f)] - fix: Don't display sync info when the sync mode is none (#1410) (XingKai Zhang <<jack_zhxk@163.com>>)
* [[`4a3a851`](http://github.com/cnpm/cnpmjs.org/commit/4a3a851256483d438753b154d80d28c12c1d625c)] - fix: use <%- instead of <%= in user profile page (#1404) (Khaidi Chu <<i@2333.moe>>)
* [[`3497bae`](http://github.com/cnpm/cnpmjs.org/commit/3497bae2b94237664716911de965a4b27afc083a)] - fix: Obfuscate email address (#1391) (Ankur Kumar <<ankurk91@users.noreply.github.com>>)
* [[`9b8491b`](http://github.com/cnpm/cnpmjs.org/commit/9b8491b736ebcb98df02d26c41334cf7fce306dc)] - fix: use https://cdn.staticfile.org (fengmk2 <<fengmk2@gmail.com>>)
* [[`fc79930`](http://github.com/cnpm/cnpmjs.org/commit/fc799304d8c6710e71364bdf1d1ed0961b9e8695)] - fix: should return `[done] Sync {name}` string when task finished (#1382) (fengmk2 <<fengmk2@gmail.com>>)
* [[`3c20267`](http://github.com/cnpm/cnpmjs.org/commit/3c20267b22491cd2ac2d751ccc459cf1f4fb0f1f)] - fix: don't retry to save log when db error (#1381) (fengmk2 <<fengmk2@gmail.com>>)
* [[`5149aa5`](http://github.com/cnpm/cnpmjs.org/commit/5149aa5a1eb01dfc17f8de1cb6c6abfecca0ed96)] - fix: proxy public package from source registry (#1375) (fengmk2 <<fengmk2@gmail.com>>)
* [[`fc07a38`](http://github.com/cnpm/cnpmjs.org/commit/fc07a38bde81bd93ef9067f3aacb06ae8e76e12b)] - fix: make sure replicate pkg is the latest pkg (#1347) (fengmk2 <<fengmk2@gmail.com>>)
* [[`17f8b66`](http://github.com/cnpm/cnpmjs.org/commit/17f8b6648b2cf8cb4cf17daef2a2477f74a671e8)] - fix: retry from registry when no_db_file error on replicate (fengmk2 <<fengmk2@gmail.com>>)
* [[`d1fe6ce`](http://github.com/cnpm/cnpmjs.org/commit/d1fe6cede7b5a082eabfe9eb94225c9af9399e62)] - fix: add other_urls on download dist tarball (#1345) (fengmk2 <<fengmk2@gmail.com>>)
* [[`8fbad39`](http://github.com/cnpm/cnpmjs.org/commit/8fbad397f3ab7177c6e6c9b458b4b0bf3d24fbd7)] - fix: use rimraf instead of fs.unlink (#1338) (Yiyu He <<dead_horse@qq.com>>)
* [[`0121de3`](http://github.com/cnpm/cnpmjs.org/commit/0121de31a3b7a8da38e31fca4e10d973c07d79e7)] - fix: no need to resync again (#1336) (fengmk2 <<fengmk2@gmail.com>>)
* [[`84a3037`](http://github.com/cnpm/cnpmjs.org/commit/84a3037d90d4b3a316752eda7440ff5c73b0872f)] - fix: avoid query too frequently (#1329) (fengmk2 <<fengmk2@gmail.com>>)
* [[`1f60a01`](http://github.com/cnpm/cnpmjs.org/commit/1f60a0136c5f2e4a33827d1f36b38c49e1e3dec6)] - fix: replicate request error, try to request from official registry (#1316) (fengmk2 <<fengmk2@gmail.com>>)
* [[`6f656a0`](http://github.com/cnpm/cnpmjs.org/commit/6f656a0736c7d1d8b58288ff97590d7cb1317ecd)] - fix: save sync last time when successes > 1000 (fengmk2 <<fengmk2@gmail.com>>)
* [[`1b30146`](http://github.com/cnpm/cnpmjs.org/commit/1b30146e94e7e72f9e762947b1ecdbd176d64532)] - fix: npm >= v5.5.0 login need not `email` (#1275) (#1304) (wmzy <<1256573276@qq.com>>)
* [[`820ae23`](http://github.com/cnpm/cnpmjs.org/commit/820ae23454f0f9755456681f3ced03e634cb3109)] - fix: control sync frequency (fengmk2 <<fengmk2@gmail.com>>)
* [[`bfb29f8`](http://github.com/cnpm/cnpmjs.org/commit/bfb29f82c967cb68f4de3a314200d95a8c59baff)] - fix: use _npmUser reset the maintainers (fengmk2 <<fengmk2@gmail.com>>)
* [[`95aa035`](http://github.com/cnpm/cnpmjs.org/commit/95aa035a275089b50dfc2590497e3bc7319f4f6b)] - fix: make sure maintainers exists on sync worker (liang feng <<anhulife@gmail.com>>)
* [[`6c69a38`](http://github.com/cnpm/cnpmjs.org/commit/6c69a38a508812f0320866d70b555de02e1fc204)] - fix: if replicate error, retry from official registry (#1230) (fengmk2 <<fengmk2@gmail.com>>)
* [[`43ffa99`](http://github.com/cnpm/cnpmjs.org/commit/43ffa995cb8a724e8cd04224c2f137d407bfe014)] - fix: "start" should wait for "stop" to remove the pid file(using Promise) (#1220) (cloudstone <<baby31529@gmail.com>>)
* [[`6c019de`](http://github.com/cnpm/cnpmjs.org/commit/6c019de514c9f4a62db1a1814ca2359408609074)] - fix: changes_stream_syncer log url should not contain sync_upstream=true (fengmk2 <<fengmk2@gmail.com>>)
**others**
* [[`522ad11`](http://github.com/cnpm/cnpmjs.org/commit/522ad11124f168788b28dd925417ae37eb9d3991)] - update readme for now situation (#1506) (alsotang <<alsotang@gmail.com>>)
* [[`0c59791`](http://github.com/cnpm/cnpmjs.org/commit/0c59791e50ef9d3080d5a2ab3e24b5899bd91446)] - Release Release 3.0.0-rc.19 (fengmk2 <<fengmk2@gmail.com>>)
* [[`79fb163`](http://github.com/cnpm/cnpmjs.org/commit/79fb163a3b12f1b9c4c9eafad7f2041e7c4c4dbf)] - chore: README fix typo ( not to use plural for code ) (#1448) (Paul Verest <<enide.github@gmail.com>>)
* [[`be00b65`](http://github.com/cnpm/cnpmjs.org/commit/be00b6557359d328c851e538827d6c681c2c3416)] - refactor: add detail message to error and keep reason (#1445) (alsotang <<alsotang@gmail.com>>)
* [[`f7e9670`](http://github.com/cnpm/cnpmjs.org/commit/f7e9670025c6e7f09d8aa88c676938a2cf4849b5)] - Release Release 3.0.0-rc.14 (fengmk2 <<fengmk2@gmail.com>>)
* [[`d0c3f1b`](http://github.com/cnpm/cnpmjs.org/commit/d0c3f1b19e46e73ce389e78413304a1542811b5f)] - test: shouldjs change from getter to function call (#1420) (alsotang <<alsotang@gmail.com>>)
* [[`d889eba`](http://github.com/cnpm/cnpmjs.org/commit/d889ebafbd6ff1bc15fbf277fd8e143a57e6cac6)] - deps: use agentkeepalive@4 (fengmk2 <<fengmk2@gmail.com>>)
* [[`938a14d`](http://github.com/cnpm/cnpmjs.org/commit/938a14d0a13b711c7b91d795151a7266b0a43c5a)] - chore: Hall of Fame integration on README (#1388) (Gwenael Pluchon <<gwenael.pluchon+github@gmail.com>>)
* [[`26d7147`](http://github.com/cnpm/cnpmjs.org/commit/26d7147562a1ae21db8bfec26983daf311353d96)] - refactor: normalize database structure (#1376) (Khaidi Chu <<i@2333.moe>>)
* [[`5334375`](http://github.com/cnpm/cnpmjs.org/commit/53343751f7c0a34ea0a346172bff0818d27864dd)] - chore: add latest-3 tag (fengmk2 <<fengmk2@gmail.com>>)
3.0.0-alpha.8 / 2017-06-15
==================
* fix: should remove unpublished version on ModuleAbbreviated too (#1192)
* docs: Dockerized cnpmjs.org configuration with installation guide (#1191)
3.0.0-alpha.7 / 2017-06-01
==================
* fix: add missing publish_time property on package list api (#1185)
3.0.0-alpha.6 / 2017-05-18
==================
* feat: add globalHook on config (#1177)
* fix: TypeError caused by invalid engines property (#1151)
* test: add new test for application/vnd.npm.install-v1+json request
3.0.0-alpha.5 / 2017-04-14
==================
* fix: should auto sync missing deprecated property (#1167)
3.0.0-alpha.4 / 2017-04-12
==================
* fix: add missing deprecated on abbreviated meta (#1165)
3.0.0-alpha.2 / 2017-03-27
==================
* fix: only get from package_readme table
3.0.0-alpha.1 / 2017-03-27
==================
* chore: start 3.x
* fix: ignore sync npm registry status 502
* feat: remove readme from package
* feat: [BREAKING_CHANGE] support abbreviated meta
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
==================

105
Makefile
View File

@@ -1,105 +0,0 @@
TESTS = $(shell ls -S `find test -type f -name "*.test.js" -print`)
REPORTER = spec
TIMEOUT = 600000
MOCHA_OPTS =
DB = sqlite
jshint:
@node_modules/.bin/jshint .
init-database:
@NODE_ENV=test node test/init_db.js
init-mysql:
@mysql -uroot -e 'DROP DATABASE IF EXISTS cnpmjs_test;'
@mysql -uroot -e 'CREATE DATABASE cnpmjs_test;'
init-pg:
@psql -c 'DROP DATABASE IF EXISTS cnpmjs_test;'
@psql -c 'CREATE DATABASE cnpmjs_test;'
test: init-database
@NODE_ENV=test DB=${DB} node_modules/.bin/mocha \
--reporter $(REPORTER) \
--timeout $(TIMEOUT) \
--require intelli-espower-loader \
--require should \
--require thunk-mocha \
--require ./test/init.js \
$(MOCHA_OPTS) \
$(TESTS)
test-sqlite:
@$(MAKE) test DB=sqlite
test-mysql: init-mysql
@$(MAKE) test DB=mysql
test-pg: init-pg
@DB_PORT=5432 DB_USER=$(USER) $(MAKE) test DB=postgres
test-all: test-sqlite test-mysql
test-cov cov: init-database
@NODE_ENV=test DB=${DB} node \
node_modules/.bin/istanbul cover \
node_modules/.bin/_mocha \
-- -u exports \
--reporter $(REPORTER) \
--timeout $(TIMEOUT) \
--require intelli-espower-loader \
--require should \
--require thunk-mocha \
--require ./test/init.js \
$(MOCHA_OPTS) \
$(TESTS)
test-cov-sqlite:
@$(MAKE) test-cov DB=sqlite
test-cov-mysql: init-mysql
@$(MAKE) test-cov DB=mysql
test-travis: init-database
@NODE_ENV=test DB=${DB} \
node \
node_modules/.bin/istanbul cover \
node_modules/.bin/_mocha \
-- -u exports \
--reporter dot \
--timeout $(TIMEOUT) \
--require intelli-espower-loader \
--require should \
--require thunk-mocha \
--require ./test/init.js \
$(MOCHA_OPTS) \
$(TESTS)
test-travis-sqlite:
@$(MAKE) test-travis DB=sqlite
test-travis-mysql: init-mysql
@$(MAKE) test-travis DB=mysql
test-travis-pg:
@psql -c 'DROP DATABASE IF EXISTS cnpmjs_test;' -U postgres
@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
dev:
@NODE_ENV=development node node_modules/.bin/node-dev dispatch.js
contributors:
@node_modules/.bin/contributors -f plain -o AUTHORS
autod:
@node_modules/.bin/autod -w \
--registry https://r.cnpmjs.org \
--prefix "^" \
--exclude public,view,docs,backup,coverage \
--dep mysql \
--keep should,supertest,chunkstream,mm,pedding
.PHONY: test

127
README.md
View File

@@ -1,9 +1,10 @@
cnpmjs.org
=======
[![npm version][npm-image]][npm-url]
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]
@@ -13,6 +14,8 @@ cnpmjs.org
[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
[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
[download-image]: https://img.shields.io/npm/dm/cnpmjs.org.svg?style=flat-square
@@ -20,58 +23,65 @@ cnpmjs.org
![logo](https://raw.github.com/cnpm/cnpmjs.org/master/logo.png)
## Description
## What is this?
Private npm registry and web for Enterprise, base on [koa](http://koajs.com/),
MySQL and [Simple Store Service](https://github.com/cnpm/cnpmjs.org/wiki/NFS-Guide).
Our goal is to provide a low cost maintenance, easy to use, and easy to scale solution for private npm.
Our goal is to provide a low cost maintenance and easy to use solution for private npm.
## What can you do with `cnpmjs.org`?
## What can you do with `cnpmjs.org`
* Build a private npm for your own enterprise. ([alibaba](http://www.alibaba.com/) is using `cnpmjs.org` now)
* Build a npm mirror. (we use it to build a mirror in China: [https://npm.taobao.org/](https://npm.taobao.org/))
* Use the private npm service provided by Alibaba Cloud DevOps which build with cnpm. [https://packages.aliyun.com/](https://packages.aliyun.com/?channel=pd_cnpm_github)
* Build a mirror NPM. (we use it to build a mirror in China: [cnpmjs.org](http://cnpmjs.org/))
* Build a completely independent NPM registry to store whatever you like.
## Features
* **Support "scoped" packages**: [npm/npm#5239](https://github.com/npm/npm/issues/5239)
* **Support [CORS](http://en.wikipedia.org/wiki/Cross-origin_resource_sharing)**
* **Simple to deploy**: only need `mysql` and a [simple store system](https://github.com/cnpm/cnpmjs.org/wiki/NFS-Guide).
* **Low cost and easy maintenance**: `package.json` info can store in MySQL, MariaDB, SQLite or PostgreSQL.
tarball(tgz file) can store in Amazon S3 or other object storage service.
* **Automatic synchronization**: automatically sync from any registry specified. support two sync modes:
- Sync all modules from upstream
- Only sync the modules after first access.
* **Manual synchronization**: automatic synchronization may has little delay. you can sync manually on web page.
You can get the source code through `npm` or `git`.
* **Low cost and easy maintenance**: `package.json` info store in MySQL, MariaDB, SQLite or PostgreSQL databases,
tarball(tgz file) store in CDN or other store systems.
* **Automatic synchronization**: automatic synchronization from any registry specified, support two sync modes:
- Sync all modules from a specified registry, like [npm registry](http://registry.npmjs.org).
- Only sync the modules that exists in your own registry.
* **Manual synchronization**: automatic synchronization may has little delay, but you can syn immediately by manually.
* **Customized client**: we provide a client [cnpm](https://github.com/cnpm/cnpm)
to extend `npm` with more features(`sync` command, [gzip](https://github.com/npm/npm-registry-client/pull/40) support).
And it is easy to wrap for your own registry which build with `cnpmjs.org`.
* **Compatible with npm client**: you can use the official npm client with `cnpmjs.org`.
you only need to change the registry in client config.
* **Support http_proxy**: if you're behind a firewall, you can provide a http proxy for cnpmjs.org.
And it easy to wrap for your own registry which build with `cnpmjs.org`.
* **Compatible with NPM client**: you can use the origin NPM client with `cnpmjs.org`,
only need to change the registry in config. Even include manual synchronization (through `install` command).
* **Version badge**: base on [shields.io](http://shields.io/) ![cnpm-badge](http://cnpmjs.org/badge/v/cnpmjs.org.svg?style=flat-square)
* **Support http_proxy**: if you're behind firewall, need to request through http proxy
## Docs
**PROTIP** Be sure to read [Migrating from 1.x to 2.x](https://github.com/cnpm/cnpmjs.org/wiki/Migrating-from-1.x-to-2.x)
as well as [New features in 2.x](https://github.com/cnpm/cnpmjs.org/wiki/New-features-in-2.x).
* [How to deploy](https://github.com/cnpm/cnpmjs.org/wiki/Deploy)
## Getting Start
* [Deploy a private npm registry in 5 minutes](https://github.com/cnpm/cnpmjs.org/wiki/Deploy-a-private-npm-registry-in-5-minutes)
* @[dead-horse](https://github.com/dead-horse): [What is cnpm?](http://deadhorse.me/slides/cnpmjs.html)
* install and deploy cnpmjs.org through npm: [examples](https://github.com/cnpm/custom-cnpm-example)
* Mirror NPM in China: [cnpmjs.org](http://cnpmjs.org)
* cnpm client: [cnpm](https://github.com/cnpm/cnpm), `npm install -g cnpm`
* [How to deploy cnpmjs.org](https://github.com/cnpm/cnpmjs.org/wiki/Deploy)
* [Sync packages through `http_proxy`](https://github.com/cnpm/cnpmjs.org/wiki/Sync-packages-through-http_proxy)
* [Migrating from 1.x to 2.x](https://github.com/cnpm/cnpmjs.org/wiki/Migrating-from-1.x-to-2.x)
* [New features in 2.x](https://github.com/cnpm/cnpmjs.org/wiki/New-features-in-2.x).
* [wiki](https://github.com/cnpm/cnpmjs.org/wiki)
## Develop on your local machine
### Dependencies
* [node](http://nodejs.org) >= 8.0.0
* [node](http://nodejs.org) >= 4.3.1
* 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/) >= 5.6.16, include `mysqld` and `mysql cli`. I test on `mysql@5.6.16`.
* [MySQL](http://dev.mysql.com/downloads/) >= 0.5.0, include `mysqld` and `mysql cli`. I test on `mysql@5.6.16`.
* MariaDB
* PostgreSQL
### Clone code and run test
### Clone codes and run test
```bash
# clone from git
@@ -93,61 +103,6 @@ $ make autod
$ make dev
```
### Dockerized cnpmjs.org Installation Guide
Cnpmjs.org shipped with a simple but pragmatic Docker Compose configuration.With the configuration, you can set up a MySQL backend cnpmjs.org instance by executing just one command on Docker installed environment.
#### Preparation
* [Install Docker](https://www.docker.com/community-edition)
* [Install Docker Compose](https://docs.docker.com/compose/install/) (Docker for Mac, Docker for Windows include Docker Compose, so most Mac and Windows users do not need to install Docker Compose separately)
* (Optional) Speed up Docker images downloading by setting up [Docker images download accelerator](https://yq.aliyun.com/articles/29941)
#### Dockerized cnpmjs.org control command
Make sure your current working directory is the root of this GitHub repository.
##### Run dockerized cnpmjs.org
```bash
$docker-compose up
```
This command will build a Docker image using the current code of repository. Then set up a dockerized MySQL instance with data initialized. After Docker container running, you can access your cnpmjs.org web portal at http://127.0.0.1:7002 and npm register at http://127.0.0.1:7001.
#### Run cnpmjs.org in the backend
```bash
$docker-compose up -d
```
#### Rebuild cnpmjs.org Docker image
```bash
$docker-compose build
```
#### Remove current dockerized cnpmjs.org instance
The current configuration set 2 named Docker Volume for your persistent data. If you haven't change the repository directory name, them will be "cnpmjsorg_cnpm-files-volume" & "cnpmjsorg_cnpm-db-volume".
Be Careful, the following commands will remove them.
```bash
$docker-compose rm
$docker volume rm cnpmjsorg_cnpm-files-volume
$docker volume rm cnpmjsorg_cnpm-db-volume
```
You can get more information about your data volumes using the below commands:
```bash
$docker volume ls // list all of your Docker volume
$docker volume inspect cnpmjsorg_cnpm-files-volume
$docker volume inspect cnpmjsorg_cnpm-db-volume
```
## How to contribute
* Clone the project
@@ -157,22 +112,10 @@ $docker volume inspect cnpmjsorg_cnpm-db-volume
Tips: make sure your code is following the [node-style-guide](https://github.com/felixge/node-style-guide).
## Top contributors
[![0](https://sourcerer.io/fame/fengmk2/cnpm/cnpmjs.org/images/0)](https://sourcerer.io/fame/fengmk2/cnpm/cnpmjs.org/links/0)
[![1](https://sourcerer.io/fame/fengmk2/cnpm/cnpmjs.org/images/1)](https://sourcerer.io/fame/fengmk2/cnpm/cnpmjs.org/links/1)
[![2](https://sourcerer.io/fame/fengmk2/cnpm/cnpmjs.org/images/2)](https://sourcerer.io/fame/fengmk2/cnpm/cnpmjs.org/links/2)
[![3](https://sourcerer.io/fame/fengmk2/cnpm/cnpmjs.org/images/3)](https://sourcerer.io/fame/fengmk2/cnpm/cnpmjs.org/links/3)
[![4](https://sourcerer.io/fame/fengmk2/cnpm/cnpmjs.org/images/4)](https://sourcerer.io/fame/fengmk2/cnpm/cnpmjs.org/links/4)
[![5](https://sourcerer.io/fame/fengmk2/cnpm/cnpmjs.org/images/5)](https://sourcerer.io/fame/fengmk2/cnpm/cnpmjs.org/links/5)
[![6](https://sourcerer.io/fame/fengmk2/cnpm/cnpmjs.org/images/6)](https://sourcerer.io/fame/fengmk2/cnpm/cnpmjs.org/links/6)
[![7](https://sourcerer.io/fame/fengmk2/cnpm/cnpmjs.org/images/7)](https://sourcerer.io/fame/fengmk2/cnpm/cnpmjs.org/links/7)
## Sponsors
- [![阿里云](https://static.aliyun.com/images/www-summerwind/logo.gif)](http://click.aliyun.com/m/4288/) [![阿里云云效](https://img.alicdn.com/tfs/TB116yt3fb2gK0jSZK9XXaEgFXa-106-20.png)](https://devops.aliyun.com/?channel=pd_cnpm_github) (2016.2 - now)
- [![阿里云](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)
## License

View File

@@ -1,24 +1,24 @@
"use strict";
'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');
const UserModel = require('../models').User;
const co = require('co');
const utility = require('utility');
var username = process.argv[2];
var newPassword = process.argv[3];
const username = process.argv[2];
const newPassword = process.argv[3];
co(function * () {
var user = yield UserModel.find({where: {name: username}});
var salt = user.salt;
co(function* () {
let user = yield UserModel.find({ where: { name: username } });
const salt = user.salt;
console.log(`user original password_sha: ${user.password_sha}`);
var newPasswordSha = utility.sha1(newPassword + salt);
const 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) {
}).catch(function(e) {
console.log(e);
});

View File

@@ -1,26 +1,14 @@
#!/usr/bin/env node
/**!
* Copyright(c) cnpm and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/
'use strict';
/**
* Module dependencies.
*/
var debug = require('debug')('cnpmjs.org:cli');
var program = require('commander');
var path = require('path');
var fs = require('fs');
var mkdirp = require('mkdirp');
var treekill = require('treekill');
var version = require('../package.json').version;
const debug = require('debug')('cnpmjs.org:cli');
const program = require('commander');
const path = require('path');
const fs = require('fs');
const mkdirp = require('mkdirp');
const treekill = require('treekill');
const version = require('../package.json').version;
function list(val) {
return val.split(',');
@@ -48,78 +36,70 @@ program.parse(process.argv);
function start(options) {
stop(options)
// wait for "stop" method to remove the pid file
.then(function () {
var dataDir = options.dataDir || path.join(process.env.HOME, '.cnpmjs.org');
mkdirp.sync(dataDir);
stop(options);
const dataDir = options.dataDir || path.join(process.env.HOME, '.cnpmjs.org');
mkdirp.sync(dataDir);
var configfile = path.join(dataDir, 'config.json');
var config = {};
if (fs.existsSync(configfile)) {
try {
config = require(configfile);
} catch (err) {
console.warn('load old %s error: %s', configfile, err);
}
const configfile = path.join(dataDir, 'config.json');
let config = {};
if (fs.existsSync(configfile)) {
try {
config = require(configfile);
} catch (err) {
console.warn('load old %s error: %s', configfile, err);
}
}
// config.enableCluster = !!options.cluster;
if (options.admins) {
config.admins = {};
for (let i = 0; i < options.admins.length; i++) {
config.admins[options.admins[i]] = options.admins[i] + '@localhost.com';
}
}
if (options.scopes) {
config.scopes = options.scopes.map(function(name) {
if (name[0] !== '@') {
name = '@' + name;
}
// config.enableCluster = !!options.cluster;
if (options.admins) {
config.admins = {};
for (var i = 0; i < options.admins.length; i++) {
config.admins[options.admins[i]] = options.admins[i] + '@localhost.com';
}
}
if (options.scopes) {
config.scopes = options.scopes.map(function (name) {
if (name[0] !== '@') {
name = '@' + name;
}
return name;
});
}
var configJSON = JSON.stringify(config, null, 2);
fs.writeFileSync(configfile, configJSON);
debug('save config %s to %s', configJSON, configfile);
// if sqlite db file not exists, init first
initDatabase(function() {
require('../dispatch');
});
fs.writeFileSync(path.join(dataDir, 'pid'), process.pid + '');
return name;
});
}
const configJSON = JSON.stringify(config, null, 2);
fs.writeFileSync(configfile, configJSON);
debug('save config %s to %s', configJSON, configfile);
// if sqlite db file not exists, init first
initDatabase(function() {
require('../dispatch');
});
fs.writeFileSync(path.join(dataDir, 'pid'), process.pid + '');
}
function stop(options) {
var dataDir = options.dataDir || path.join(process.env.HOME, '.cnpmjs.org');
var pidfile = path.join(dataDir, 'pid');
return new Promise(function (resolve) {
if (!fs.existsSync(pidfile)) {
resolve();
return;
}
var pid = Number(fs.readFileSync(pidfile, 'utf8'));
treekill(pid, function (err) {
const dataDir = options.dataDir || path.join(process.env.HOME, '.cnpmjs.org');
const pidfile = path.join(dataDir, 'pid');
if (fs.existsSync(pidfile)) {
const pid = Number(fs.readFileSync(pidfile, 'utf8'));
treekill(pid, function(err) {
if (err) {
console.log(err);
throw err;
}
console.log('cnpmjs.org server:%d stop', pid);
fs.unlinkSync(pidfile);
resolve();
});
});
}
}
function initDatabase(callback) {
var models = require('../models');
const models = require('../models');
models.sequelize.sync({ force: false })
.then(function () {
models.Total.init(function (err) {
.then(function() {
models.Total.init(function(err) {
if (err) {
console.error('[models/init_script.js] sequelize init fail');
console.error(err);
@@ -130,7 +110,7 @@ function initDatabase(callback) {
}
});
})
.catch(function (err) {
.catch(function(err) {
console.error('[models/init_script.js] sequelize sync fail');
console.error(err);
throw err;

View File

@@ -1,16 +0,0 @@
'use strict';
const debug = require('debug')('cnpmjs.org:cache');
const Redis = require('ioredis');
const config = require('../config');
let client;
if (config.redisCache.enable) {
client = new Redis(config.redisCache.connectOptions);
client.on('ready', () => {
debug('connect ready, getBuiltinCommands: %j', client.getBuiltinCommands());
});
}
module.exports = client;

View File

@@ -10,20 +10,20 @@ const config = require('../config');
const mail = require('./mail');
const isTEST = process.env.NODE_ENV === 'test';
const categories = ['sync_info', 'sync_error'];
const categories = [ 'sync_info', 'sync_error' ];
const logger = module.exports = Logger({
categories: categories,
categories,
dir: config.logdir,
duration: '1d',
format: '[{category}.]YYYY-MM-DD[.log]',
stdout: config.debug && !isTEST,
errorFormater: errorFormater,
errorFormater,
seperator: os.EOL,
});
const to = [];
for (var user in config.admins) {
for (const user in config.admins) {
to.push(config.admins[user]);
}
@@ -33,7 +33,7 @@ function errorFormater(err) {
return msg.text;
}
logger.syncInfo = function () {
logger.syncInfo = function() {
const args = [].slice.call(arguments);
if (typeof args[0] === 'string') {
args[0] = util.format('[%s][%s] ', utility.logDate(), process.pid) + args[0];
@@ -44,7 +44,7 @@ logger.syncInfo = function () {
logger.sync_info.apply(logger, args);
};
logger.syncError =function () {
logger.syncError = function() {
const args = [].slice.call(arguments);
if (typeof args[0] === 'string') {
args[0] = util.format('[%s][%s] ', utility.logDate(), process.pid) + args[0];

View File

@@ -1,11 +1,11 @@
'use strict';
var nodemailer = require('nodemailer');
var utility = require('utility');
var os = require('os');
var mailConfig = require('../config').mail;
const nodemailer = require('nodemailer');
const utility = require('utility');
const os = require('os');
const mailConfig = require('../config').mail;
var smtpConfig;
let smtpConfig;
if (mailConfig.auth) {
// new style
smtpConfig = mailConfig;
@@ -19,12 +19,12 @@ if (mailConfig.auth) {
debug: mailConfig.debug,
auth: {
user: mailConfig.user,
pass: mailConfig.pass
}
pass: mailConfig.pass,
},
};
}
var transport;
let transport;
/**
* Send notice email with mail level and appname.
@@ -35,15 +35,16 @@ var transport;
* @param {String} html
* @param {Function(err, result)} callback
*/
exports.notice = function sendLogMail(to, level, subject, html, callback) {
subject = '[' + mailConfig.appname + '] [' + level + '] [' + os.hostname() + '] ' + subject;
html = String(html);
exports.send(to, subject, html.replace(/\n/g, '<br/>'), callback);
};
var LEVELS = [ 'log', 'warn', 'error' ];
LEVELS.forEach(function (level) {
exports[level] = function (to, subject, html, callback) {
const LEVELS = [ 'log', 'warn', 'error' ];
LEVELS.forEach(function(level) {
exports[level] = function(to, subject, html, callback) {
exports.notice(to, level, subject, html, callback);
};
});
@@ -55,7 +56,8 @@ LEVELS.forEach(function (level) {
* @param {String} html
* @param {Function(err, result)} callback
*/
exports.send = function (to, subject, html, callback) {
exports.send = function(to, subject, html, callback) {
callback = callback || utility.noop;
if (mailConfig.enable === false) {
@@ -67,14 +69,14 @@ exports.send = function (to, subject, html, callback) {
transport = nodemailer.createTransport(smtpConfig);
}
var message = {
const message = {
from: mailConfig.from || mailConfig.sender,
to: to,
subject: subject,
html: html,
to,
subject,
html,
};
transport.sendMail(message, function (err, result) {
transport.sendMail(message, function(err, result) {
callback(err, result);
});
};

View File

@@ -1,18 +1,32 @@
/**!
* cnpmjs.org - common/markdown.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
var xss = require('xss');
var MarkdownIt = require('markdown-it');
/**
* Module dependencies.
*/
const xss = require('xss');
const MarkdownIt = require('markdown-it');
// allow class attr on code
xss.whiteList.code = ['class'];
xss.whiteList.code = [ 'class' ];
var md = new MarkdownIt({
const md = new MarkdownIt({
html: true,
linkify: true,
});
exports.render = function (content, filterXss) {
var html = md.render(content);
exports.render = function(content, filterXss) {
let html = md.render(content);
if (filterXss !== false) {
html = xss(html);
}

View File

@@ -1,4 +1,4 @@
/*!
/* !
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
@@ -12,6 +12,6 @@
* Module dependencies.
*/
var config = require('../config');
const config = require('../config');
module.exports = config.nfs;

View File

@@ -1,8 +1,22 @@
/**!
* cnpmjs.org - common/sequelize.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
var Sequelize = require('sequelize');
var DataTypes = require('sequelize/lib/data-types');
var config = require('../config');
/**
* Module dependencies.
*/
const Sequelize = require('sequelize');
const DataTypes = require('sequelize/lib/data-types');
const config = require('../config');
if (config.mysqlServers && config.database.dialect === 'sqlite') {
// https://github.com/cnpm/cnpmjs.org/wiki/Migrating-from-1.x-to-2.x
@@ -20,8 +34,7 @@ if (config.mysqlServers && config.database.dialect === 'sqlite') {
// mysqlQueryTimeout: 5000,
console.warn('[WARNNING] your config.js was too old, please @see https://github.com/cnpm/cnpmjs.org/wiki/Migrating-from-1.x-to-2.x');
var server = config.mysqlServers[0];
var dialectOptions = config.database && config.database.dialectOptions;
const server = config.mysqlServers[0];
config.database = {
db: config.mysqlDatabase,
username: server.user,
@@ -36,12 +49,9 @@ if (config.mysqlServers && config.database.dialect === 'sqlite') {
},
logging: !!process.env.SQL_DEBUG,
};
if (dialectOptions) {
config.database.dialectOptions = dialectOptions;
}
}
var database = config.database;
const database = config.database;
// sync database before app start, defaul is false
database.syncFirst = false;
@@ -60,6 +70,6 @@ database.define = {
collate: 'utf8_general_ci',
};
var sequelize = new Sequelize(database.db, database.username, database.password, database);
const sequelize = new Sequelize(database.db, database.username, database.password, database);
module.exports = sequelize;

View File

@@ -1,42 +1,56 @@
/**!
* cnpmjs.org - common/urllib.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
var urlparse = require('url').parse;
var urllib = require('urllib');
var HttpAgent = require('agentkeepalive');
var HttpsAgent = require('agentkeepalive').HttpsAgent;
var config = require('../config');
/**
* Module dependencies.
*/
var httpAgent;
var httpsAgent;
const urlparse = require('url').parse;
const urllib = require('urllib');
const HttpAgent = require('agentkeepalive');
const HttpsAgent = require('agentkeepalive').HttpsAgent;
const config = require('../config');
let httpAgent;
let httpsAgent;
if (config.httpProxy) {
var tunnel = require('tunnel-agent');
var urlinfo = urlparse(config.httpProxy);
const tunnel = require('tunnel-agent');
const urlinfo = urlparse(config.httpProxy);
if (urlinfo.protocol === 'http:') {
httpAgent = tunnel.httpOverHttp({
proxy: {
host: urlinfo.hostname,
port: urlinfo.port
}
port: urlinfo.port,
},
});
httpsAgent = tunnel.httpsOverHttp({
proxy: {
host: urlinfo.hostname,
port: urlinfo.port
}
port: urlinfo.port,
},
});
} else if (urlinfo.protocol === 'https:') {
httpAgent = tunnel.httpOverHttps({
proxy: {
host: urlinfo.hostname,
port: urlinfo.port
}
port: urlinfo.port,
},
});
httpsAgent = tunnel.httpsOverHttps({
proxy: {
host: urlinfo.hostname,
port: urlinfo.port
}
port: urlinfo.port,
},
});
} else {
throw new TypeError('httpProxy format error: ' + config.httpProxy);
@@ -44,17 +58,17 @@ if (config.httpProxy) {
} else {
httpAgent = new HttpAgent({
timeout: 0,
keepAliveTimeout: 15000
keepAliveTimeout: 15000,
});
httpsAgent = new HttpsAgent({
timeout: 0,
keepAliveTimeout: 15000
keepAliveTimeout: 15000,
});
}
var client = urllib.create({
const client = urllib.create({
agent: httpAgent,
httpsAgent: httpsAgent
httpsAgent,
});
module.exports = client;

View File

@@ -1,20 +1,19 @@
'use strict';
var mkdirp = require('mkdirp');
var copy = require('copy-to');
var path = require('path');
var fs = require('fs');
var os = require('os');
var utility = require('utility');
const mkdirp = require('mkdirp');
const copy = require('copy-to');
const path = require('path');
const fs = require('fs');
const os = require('os');
var version = require('../package.json').version;
const version = require('../package.json').version;
var root = path.dirname(__dirname);
var dataDir = path.join(process.env.HOME || root, '.cnpmjs.org');
const root = path.dirname(__dirname);
const dataDir = path.join(process.env.HOME || root, '.cnpmjs.org');
var config = {
version: version,
dataDir: dataDir,
const config = {
version,
dataDir,
/**
* Cluster mode
@@ -29,8 +28,6 @@ var config = {
registryPort: 7001,
webPort: 7002,
bindingHost: '127.0.0.1', // only binding on 127.0.0.1 for local access
// default is ctx.protocol
protocol: '',
// debug mode
// if in debug mode, some middleware like limit wont load
@@ -49,21 +46,6 @@ var config = {
// web page viewCache
viewCache: false,
// registry http response cache control header
// if you are using CDN, can set it to 'max-age=0, s-maxage=10, must-revalidate'
// it meaning cache 10s on CDN server and no cache on client side.
registryCacheControlHeader: '',
// if you are using CDN, can set it to 'Accept, Accept-Encoding'
registryVaryHeader: '',
// disable package search
disableSearch: false,
// view files directory
viewDir: path.join(root, 'view', 'web'),
customRegistryMiddlewares: [],
customWebMiddlewares: [],
// config for koa-limit middleware
// for limit download rates
limit: {
@@ -95,13 +77,12 @@ var config = {
service: 'gmail',
auth: {
user: 'address@gmail.com',
pass: 'your password'
}
pass: 'your password',
},
},
logoURL: 'https://os.alipayobjects.com/rmsportal/oygxuIUkkrRccUz.jpg', // cnpm logo image url
adBanner: '',
customHeader: '',
customReadmeFile: '', // you can use your custom readme file instead the cnpm one
customFooter: '', // you can add copyright and site total script html here
npmClientName: 'cnpm', // use `${name} install package`
@@ -136,12 +117,7 @@ var config = {
pool: {
maxConnections: 10,
minConnections: 0,
maxIdleTime: 30000
},
dialectOptions: {
// if your server run on full cpu load, please set trace to false
trace: true,
maxIdleTime: 30000,
},
// the storage engine for 'sqlite'
@@ -151,23 +127,12 @@ var config = {
logging: !!process.env.SQL_DEBUG,
},
// return total modules and versions, default is true
// it will use `SELECT count(DISTINCT name) FROM module` SQL on Database
enableTotalCount: true,
// enable proxy npm audits request or not
enableNpmAuditsProxy: true,
// package tarball store in local filesystem by default
nfs: require('fs-cnpm')({
dir: path.join(dataDir, 'nfs')
dir: path.join(dataDir, 'nfs'),
}),
// if set true, will 302 redirect to `nfs.url(dist.key)`
downloadRedirectToNFS: false,
// don't check database and just download tgz from nfs
downloadTgzDontCheckModule: false,
// remove original tarball when publishing
unpublishRemoveTarball: true,
// registry url name
registryHost: 'r.cnpmjs.org',
@@ -199,13 +164,11 @@ var config = {
// please don't change it if not necessary
officialNpmRegistry: 'https://registry.npmjs.com',
officialNpmReplicate: 'https://replicate.npmjs.com',
cnpmRegistry: 'https://r.cnpmjs.com',
// 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',
sourceNpmWeb: 'https://npm.taobao.org',
// upstream registry is base on cnpm/cnpmjs.org or not
// if your upstream is official npm registry, please turn it off
@@ -234,36 +197,14 @@ var config = {
// sync devDependencies or not, default is false
syncDevDependencies: false,
// try to remove all deleted versions from original registry
syncDeletedVersions: true,
// changes streaming sync
syncChangesStream: false,
syncDownloadOptions: {
// formatRedirectUrl: function (url, location)
},
handleSyncRegistry: 'http://127.0.0.1:7001',
// default badge subject
// badge subject on http://shields.io/
badgePrefixURL: 'https://img.shields.io/badge',
badgeSubject: 'cnpm',
// defautl use https://badgen.net/
badgeService: {
url: function(subject, status, options) {
options = options || {};
let url = `https://badgen.net/badge/${utility.encodeURIComponent(subject)}/${utility.encodeURIComponent(status)}`;
if (options.color) {
url += `/${utility.encodeURIComponent(options.color)}`;
}
if (options.icon) {
url += `?icon=${utility.encodeURIComponent(options.icon)}`;
}
return url;
},
},
packagephobiaURL: 'https://packagephobia.now.sh',
packagephobiaSupportPrivatePackage: false,
packagephobiaMinDownloadCount: 1000,
// 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
@@ -280,45 +221,10 @@ var config = {
// snyk.io root url
snykUrl: 'https://snyk.io',
// https://github.com/cnpm/cnpmjs.org/issues/1149
// if enable this option, must create module_abbreviated and package_readme table in database
enableAbbreviatedMetadata: false,
// global hook function: function* (envelope) {}
// envelope format please see https://github.com/npm/registry/blob/master/docs/hooks/hooks-payload.md#payload
globalHook: null,
opensearch: {
host: '',
},
// redis cache
redisCache: {
enable: false,
connectOptions: null,
},
};
if (process.env.NODE_ENV === 'test') {
config.enableAbbreviatedMetadata = true;
config.customRegistryMiddlewares.push(() => {
return function* (next) {
this.set('x-custom-middleware', 'true');
yield next;
};
});
config.customWebMiddlewares.push(() => {
return function* (next) {
this.set('x-custom-web-middleware', 'true');
yield next;
};
});
}
if (process.env.NODE_ENV !== 'test') {
var customConfig;
let customConfig;
if (process.env.NODE_ENV === 'development') {
customConfig = path.join(root, 'config', 'config.js');
} else {
@@ -339,7 +245,7 @@ mkdirp.sync(config.uploadDir);
module.exports = config;
config.loadConfig = function (customConfig) {
config.loadConfig = function(customConfig) {
if (!customConfig) {
return;
}

View File

@@ -1,6 +1,20 @@
/**!
* cnpmjs.org - controllers/registry/package/deprecate.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
var packageService = require('../../../services/package');
/**
* Module dependencies.
*/
const packageService = require('../../../services/package');
module.exports = deprecateVersions;
@@ -8,29 +22,28 @@ module.exports = deprecateVersions;
* @see https://github.com/cnpm/cnpmjs.org/issues/415
*/
function* deprecateVersions() {
var body = this.request.body;
var name = this.params.name || this.params[0];
const body = this.request.body;
const name = this.params.name || this.params[0];
var tasks = [];
for (var version in body.versions) {
const tasks = [];
for (const version in body.versions) {
tasks.push(packageService.getModule(name, version));
}
var rs = yield tasks;
const rs = yield tasks;
var updateTasks = [];
for (var i = 0; i < rs.length; i++) {
var row = rs[i];
const updateTasks = [];
for (let i = 0; i < rs.length; i++) {
const row = rs[i];
if (!row) {
// some version not exists
this.status = 400;
const error = '[version_error] Some versions: ' + JSON.stringify(Object.keys(body.versions)) + ' not found';
this.body = {
error,
reason: error,
error: 'version_error',
reason: 'Some versions: ' + JSON.stringify(Object.keys(body.versions)) + ' not found',
};
return;
}
var data = body.versions[row.package.version];
const data = body.versions[row.package.version];
if (typeof data.deprecated === 'string') {
row.package.deprecated = data.deprecated;
updateTasks.push(packageService.updateModulePackage(row.id, row.package));

View File

@@ -1,21 +1,20 @@
'use strict';
var packageService = require('../../../services/package');
var hook = require('../../../services/hook');
const packageService = require('../../../services/package');
function ok() {
return {
ok: "dist-tags updated"
ok: 'dist-tags updated',
};
}
// 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 tags = {};
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
const name = this.params.name || this.params[0];
const rows = yield packageService.listModuleTags(name);
const tags = {};
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
tags[row.tag] = row.version;
}
this.body = tags;
@@ -23,31 +22,18 @@ exports.index = function* () {
// PUT /-/package/:pkg/dist-tags -- Set package's dist-tags to provided object body (removing missing)
exports.save = function* () {
var name = this.params.name || this.params[0];
const name = this.params.name || this.params[0];
yield packageService.removeModuleTags(name);
yield exports.update.call(this);
};
// POST /-/package/:pkg/dist-tags -- Add/modify dist-tags from provided object body (merge)
exports.update = function* () {
var name = this.params.name || this.params[0];
var tags = this.request.body;
for (var tag in tags) {
var version = tags[tag];
const name = this.params.name || this.params[0];
const tags = this.request.body;
for (const tag in tags) {
const version = tags[tag];
yield packageService.addModuleTag(name, tag, version);
// hooks
const envelope = {
event: 'package:dist-tag',
name: name,
tag: tag,
type: 'package',
version: version,
hookOwner: null,
payload: null,
change: null,
};
hook.trigger(envelope);
}
this.status = 201;
this.body = ok();
@@ -56,17 +42,16 @@ exports.update = function* () {
// PUT /-/package/:pkg/dist-tags/:tag -- Set package's dist-tags[tag] to provided string body
// POST /-/package/:pkg/dist-tags/:tag -- Same as PUT /-/package/:pkg/dist-tags/:tag
exports.set = function* () {
var name = this.params.name || this.params[0];
var tag = this.params.tag || this.params[1];
var version = this.request.body;
const name = this.params.name || this.params[0];
const tag = this.params.tag || this.params[1];
const version = this.request.body;
// make sure version exists
var pkg = yield packageService.getModule(name, version);
const pkg = yield packageService.getModule(name, version);
if (!pkg) {
this.status = 400;
const error = '[version_error] ' + name + '@' + version + ' not exists';
this.body = {
error,
reason: error,
error: 'version_error',
reason: name + '@' + version + ' not exists',
};
return;
}
@@ -74,44 +59,20 @@ exports.set = function* () {
yield packageService.addModuleTag(name, tag, version);
this.status = 201;
this.body = ok();
// hooks
const envelope = {
event: 'package:dist-tag',
name: name,
tag: tag,
type: 'package',
version: version,
hookOwner: null,
payload: null,
change: null,
};
hook.trigger(envelope);
};
// DELETE /-/package/:pkg/dist-tags/:tag -- Remove tag from dist-tags
exports.destroy = function* () {
var name = this.params.name || this.params[0];
var tag = this.params.tag || this.params[1];
const name = this.params.name || this.params[0];
const tag = this.params.tag || this.params[1];
if (tag === 'latest') {
this.status = 400;
const error = '[dist_tag_error] Can\'t not delete latest tag';
this.body = {
error,
reason: error,
error: 'dist_tag_error',
reason: 'Can\'t not delete latest tag',
};
return;
}
yield packageService.removeModuleTagsByNames(name, tag);
this.body = ok();
// hooks
const envelope = {
event: 'package:dist-tag:rm',
name: name,
tag: tag,
type: 'package',
hookOwner: null,
payload: null,
change: null,
};
hook.trigger(envelope);
};

View File

@@ -1,63 +1,49 @@
'use strict';
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');
var downloadAsReadStream = require('../../utils').downloadAsReadStream;
var packageService = require('../../../services/package');
var downloadTotalService = require('../../../services/download_total');
var config = require('../../../config');
const debug = require('debug')('cnpmjs.org:controllers:registry:download');
const mime = require('mime');
const utility = require('utility');
const defer = require('co-defer');
const is = require('is-type-of');
const nfs = require('../../../common/nfs');
const logger = require('../../../common/logger');
const common = require('../../../lib/common');
const downloadAsReadStream = require('../../utils').downloadAsReadStream;
const packageService = require('../../../services/package');
const downloadTotalService = require('../../../services/download_total');
const config = require('../../../config');
let globalDownloads = new Map();
let _downloads = {};
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);
const name = this.params.name || this.params[0];
const filename = this.params.filename || this.params[1];
const version = filename.slice(name.length + 1, -4);
const row = yield packageService.getModule(name, version);
// can not get dist
var url = null;
var query = this.query || {};
// allow download from specific store bucket
var options = query.bucket ? { bucket: query.bucket } : null;
let url = null;
if (typeof nfs.url === 'function') {
if (is.generatorFunction(nfs.url)) {
url = yield nfs.url(common.getCDNKey(name, filename), options);
url = yield nfs.url(common.getCDNKey(name, filename));
} else {
url = nfs.url(common.getCDNKey(name, filename), options);
url = nfs.url(common.getCDNKey(name, filename));
}
}
debug('download %s %s %s %s', name, filename, version, url);
// don't check database and just download tgz from nfs
if (config.downloadTgzDontCheckModule && url) {
this.status = 302;
this.set('Location', url);
const count = (globalDownloads.get(name) || 0) + 1;
globalDownloads.set(name, count);
return;
}
var row = yield packageService.getModule(name, version);
if (!row || !row.package || !row.package.dist) {
if (!url) {
return yield next;
}
this.status = 302;
this.set('Location', url);
const count = (globalDownloads.get(name) || 0) + 1;
globalDownloads.set(name, count);
_downloads[name] = (_downloads[name] || 0) + 1;
return;
}
const count = (globalDownloads.get(name) || 0) + 1;
globalDownloads.set(name, count);
_downloads[name] = (_downloads[name] || 0) + 1;
if (config.downloadRedirectToNFS && url) {
this.status = 302;
@@ -65,7 +51,7 @@ module.exports = function* download(next) {
return;
}
var dist = row.package.dist;
const dist = row.package.dist;
if (!dist.key) {
// try to use nsf.url() first
url = url || dist.tarball;
@@ -86,47 +72,35 @@ module.exports = function* download(next) {
this.body = yield downloadAsReadStream(dist.key);
};
var saving = false;
defer.setInterval(function* () {
if (saving) {
return;
}
// save download count
var totals = [];
var allCount = 0;
for (const [ name, count ] of globalDownloads) {
if (name !== '__all__') {
totals.push([name, count]);
}
allCount += count;
const totals = [];
for (const name in _downloads) {
const count = _downloads[name];
totals.push([ name, count ]);
}
globalDownloads = new Map();
_downloads = {};
if (allCount === 0) {
if (totals.length === 0) {
return;
}
saving = true;
totals.push([ '__all__', allCount ]);
debug('save download total: %j', totals);
var date = utility.YYYYMMDD();
for (var i = 0; i < totals.length; i++) {
var item = totals[i];
var name = item[0];
var count = item[1];
const date = utility.YYYYMMDD();
for (let i = 0; i < totals.length; i++) {
const item = totals[i];
const name = item[0];
const count = item[1];
try {
yield downloadTotalService.plusModuleTotal({ name: name, date: date, count: count });
yield downloadTotalService.plusModuleTotal({ name, date, count });
} catch (err) {
if (err.name !== 'SequelizeUniqueConstraintError') {
err.message += '; name: ' + name + ', count: ' + count + ', date: ' + date;
logger.error(err);
}
// save back to globalDownloads, try again next time
count = (globalDownloads.get(name) || 0) + count;
globalDownloads.set(name, count);
// save back to _downloads, try again next time
_downloads[name] = (_downloads[name] || 0) + count;
}
}
saving = false;
}, 5000 + Math.ceil(Math.random() * 1000));

View File

@@ -1,21 +1,20 @@
'use strict';
var DownloadTotal = require('../../../services/download_total');
var DATE_REG = /^\d{4}-\d{2}-\d{2}$/;
const DownloadTotal = require('../../../services/download_total');
const 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];
let range = this.params.range || this.params[0] || '';
const name = this.params.name || this.params[1];
range = range.split(':');
if (range.length !== 2
|| !range[0].match(DATE_REG)
|| !range[1].match(DATE_REG)) {
this.status = 400;
const error = '[range_error] range must be YYYY-MM-DD:YYYY-MM-DD style';
this.body = {
error,
reason: error,
error: 'range_error',
reason: 'range must be YYYY-MM-DD:YYYY-MM-DD style',
};
return;
}
@@ -26,42 +25,42 @@ module.exports = function* downloadTotal() {
};
function* getPackageTotal(name, start, end) {
var res = yield DownloadTotal.getModuleTotal(name, start, end);
var downloads = res.map(function (row) {
const res = yield DownloadTotal.getModuleTotal(name, start, end);
const downloads = res.map(function(row) {
return {
day: row.date,
downloads: row.count
downloads: row.count,
};
});
downloads.sort(function (a, b) {
downloads.sort(function(a, b) {
return a.day > b.day ? 1 : -1;
});
return {
downloads: downloads,
downloads,
package: name,
start: start,
end: end
start,
end,
};
}
function* getTotal(start, end) {
var res = yield DownloadTotal.getTotal(start, end);
var downloads = res.map(function (row) {
const res = yield DownloadTotal.getTotal(start, end);
const downloads = res.map(function(row) {
return {
day: row.date,
downloads: row.count
downloads: row.count,
};
});
downloads.sort(function (a, b) {
downloads.sort(function(a, b) {
return a.day > b.day ? 1 : -1;
});
return {
downloads: downloads,
start: start,
end: end
downloads,
start,
end,
};
}

View File

@@ -1,78 +1,31 @@
'use strict';
var debug = require('debug')('cnpmjs.org:controllers:registry:package:list');
var utility = require('utility');
var packageService = require('../../../services/package');
var common = require('../../../lib/common');
var SyncModuleWorker = require('../../sync_module_worker');
var config = require('../../../config');
const cache = require('../../../common/cache');
const logger = require('../../../common/logger');
// https://forum.nginx.org/read.php?2,240120,240120#msg-240120
// should set weak etag avoid nginx remove it
function etag(objs) {
return 'W/"' + utility.md5(JSON.stringify(objs)) + '"';
}
const debug = require('debug')('cnpmjs.org:controllers:registry:package:list');
const packageService = require('../../../services/package');
const common = require('../../../lib/common');
const SyncModuleWorker = require('../../sync_module_worker');
const config = require('../../../config');
/**
* list all version of a module
* GET /:name
*/
module.exports = function* list() {
const name = this.params.name || this.params[0];
// sync request will contain this query params
let noCache = this.query.cache === '0';
if (!noCache) {
const ua = this.headers['user-agent'] || '';
// old sync client will request with these user-agent
if (ua.indexOf('npm_service.cnpmjs.org/') !== -1) {
noCache = true;
}
}
const isJSONPRequest = this.query.callback;
let cacheKey;
let needAbbreviatedMeta = false;
let abbreviatedMetaType = 'application/vnd.npm.install-v1+json';
if (config.enableAbbreviatedMetadata && this.accepts([ 'json', abbreviatedMetaType ]) === abbreviatedMetaType) {
needAbbreviatedMeta = true;
if (cache && !isJSONPRequest) {
cacheKey = `list-${name}-v1`;
}
}
if (cacheKey && !noCache) {
const values = yield cache.hmget(cacheKey, 'etag', 'body');
if (values && values[0] && values[1]) {
this.body = values[1];
this.type = 'json';
this.etag = values[0];
this.set('x-hit-cache', cacheKey);
debug('hmget %s success, etag:%j', cacheKey, values[0]);
if (config.registryCacheControlHeader) {
this.set('cache-control', config.registryCacheControlHeader);
}
if (config.registryVaryHeader) {
this.set('vary', config.registryVaryHeader);
}
return;
}
debug('hmget %s missing, %j', cacheKey, values);
}
var rs = yield [
const orginalName = this.params.name || this.params[0];
const name = orginalName;
const rs = yield [
packageService.getModuleLastModified(name),
packageService.listModuleTags(name),
];
var modifiedTime = rs[0];
var tags = rs[1];
let modifiedTime = rs[0];
const tags = rs[1];
debug('show %s, last modified: %s, tags: %j', name, modifiedTime, tags);
debug('show %s(%s), last modified: %s, tags: %j', name, orginalName, modifiedTime, tags);
if (modifiedTime) {
// find out the latest modfied time
// because update tags only modfied tag, wont change module gmt_modified
for (var i = 0; i < tags.length; i++) {
var tag = tags[i];
for (const tag of tags) {
if (tag.gmt_modified > modifiedTime) {
modifiedTime = tag.gmt_modified;
}
@@ -87,35 +40,20 @@ module.exports = function* list() {
}
}
if (needAbbreviatedMeta) {
var rows = yield packageService.listModuleAbbreviatedsByName(name);
if (rows.length > 0) {
yield handleAbbreviatedMetaRequest(this, name, modifiedTime, tags, rows, cacheKey);
return;
}
var fullRows = yield packageService.listModulesByName(name);
if (fullRows.length > 0) {
// no abbreviated meta rows, use the full meta convert to abbreviated meta
yield handleAbbreviatedMetaRequestWithFullMeta(this, name, modifiedTime, tags, fullRows);
return;
}
}
var r = yield [
const r = yield [
packageService.listModulesByName(name),
packageService.listStarUserNames(name),
packageService.listMaintainers(name),
];
var rows = r[0];
var starUsers = r[1];
var maintainers = r[2];
const rows = r[0];
let starUsers = r[1];
const maintainers = r[2];
debug('show %s got %d rows, %d tags, %d star users, maintainers: %j',
name, rows.length, tags.length, starUsers.length, maintainers);
var starUserMap = {};
for (var i = 0; i < starUsers.length; i++) {
var starUser = starUsers[i];
const starUserMap = {};
for (const starUser of starUsers) {
if (starUser[0] !== '"' && starUser[0] !== "'") {
starUserMap[starUser] = true;
}
@@ -124,13 +62,13 @@ module.exports = function* list() {
if (rows.length === 0) {
// check if unpublished
var unpublishedInfo = yield packageService.getUnpublishedModule(name);
const unpublishedInfo = yield packageService.getUnpublishedModule(name);
debug('show unpublished %j', unpublishedInfo);
if (unpublishedInfo) {
this.status = 404;
this.jsonp = {
_id: name,
name: name,
this.body = {
_id: orginalName,
name: orginalName,
time: {
modified: unpublishedInfo.package.time,
unpublished: unpublishedInfo.package,
@@ -146,39 +84,35 @@ module.exports = function* list() {
if (rows.length === 0) {
if (!this.allowSync) {
this.status = 404;
const error = '[not_found] document not found';
this.jsonp = {
error,
reason: error,
this.body = {
error: 'not_found',
reason: 'document not found',
};
return;
}
// start sync
var logId = yield SyncModuleWorker.sync(name, 'sync-by-install');
const logId = yield SyncModuleWorker.sync(name, 'sync-by-install');
debug('start sync %s, get log id %s', name, logId);
return this.redirect(config.officialNpmRegistry + this.url);
}
var latestMod = null;
var readme = null;
let latestMod = null;
let readme = null;
// set tags
var distTags = {};
for (var i = 0; i < tags.length; i++) {
var t = tags[i];
const distTags = {};
for (const t of tags) {
distTags[t.tag] = t.version;
}
// set versions and times
var versions = {};
var allVersionString = '';
var times = {};
var attachments = {};
var createdTime = null;
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
var pkg = row.package;
const versions = {};
let times = {};
const attachments = {};
let createdTime = null;
for (const row of rows) {
const pkg = row.package;
// pkg is string ... ignore it
if (typeof pkg === 'string') {
continue;
@@ -188,9 +122,8 @@ module.exports = function* list() {
pkg.publish_time = pkg.publish_time || row.publish_time;
versions[pkg.version] = pkg;
allVersionString += pkg.version + ',';
var t = times[pkg.version] = row.publish_time ? new Date(row.publish_time) : row.gmt_modified;
const t = times[pkg.version] = row.publish_time ? new Date(row.publish_time) : row.gmt_modified;
if ((!distTags.latest && !latestMod) || distTags.latest === pkg.version) {
latestMod = row;
readme = pkg.readme;
@@ -207,11 +140,11 @@ module.exports = function* list() {
}
if (modifiedTime && createdTime) {
var ts = {
const ts = {
modified: modifiedTime,
created: createdTime,
};
for (var t in times) {
for (const t in times) {
ts[t] = times[t];
}
times = ts;
@@ -221,8 +154,8 @@ module.exports = function* list() {
latestMod = rows[0];
}
var rev = String(latestMod.id);
var pkg = latestMod.package;
const rev = String(latestMod.id);
const pkg = latestMod.package;
if (tags.length === 0) {
// some sync error reason, will cause tags missing
@@ -230,17 +163,10 @@ module.exports = function* list() {
distTags.latest = pkg.version;
}
if (!readme && config.enableAbbreviatedMetadata) {
var packageReadme = yield packageService.getPackageReadme(name);
if (packageReadme) {
readme = packageReadme.readme;
}
}
var info = {
_id: name,
const info = {
_id: orginalName,
_rev: rev,
name: name,
name: orginalName,
description: pkg.description,
'dist-tags': distTags,
maintainers: pkg.maintainers,
@@ -248,8 +174,8 @@ module.exports = function* list() {
users: starUsers,
author: pkg.author,
repository: pkg.repository,
versions: versions,
readme: readme,
versions,
readme,
_attachments: attachments,
};
@@ -258,182 +184,6 @@ module.exports = function* list() {
info.bugs = pkg.bugs;
info.license = pkg.license;
debug('show module %s: %s, latest: %s', name, rev, latestMod.version);
debug('show module %s: %s, latest: %s', orginalName, rev, latestMod.version);
this.jsonp = info;
// use faster etag
this.etag = etag([
modifiedTime,
distTags,
pkg.maintainers,
allVersionString,
]);
if (config.registryCacheControlHeader) {
this.set('cache-control', config.registryCacheControlHeader);
}
if (config.registryVaryHeader) {
this.set('vary', config.registryVaryHeader);
}
};
function* handleAbbreviatedMetaRequest(ctx, name, modifiedTime, tags, rows, cacheKey) {
debug('show %s got %d rows, %d tags, modifiedTime: %s', name, rows.length, tags.length, modifiedTime);
const isJSONPRequest = ctx.query.callback;
var latestMod = null;
// set tags
var distTags = {};
for (var i = 0; i < tags.length; i++) {
var t = tags[i];
distTags[t.tag] = t.version;
}
// set versions and times
var versions = {};
var allVersionString = '';
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
var pkg = row.package;
common.setDownloadURL(pkg, ctx);
pkg._publish_on_cnpm = undefined;
pkg.publish_time = pkg.publish_time || row.publish_time;
versions[pkg.version] = pkg;
allVersionString += pkg.version + ',';
if ((!distTags.latest && !latestMod) || distTags.latest === pkg.version) {
latestMod = row;
}
}
if (!latestMod) {
latestMod = rows[0];
}
if (tags.length === 0) {
// some sync error reason, will cause tags missing
// set latest tag at least
distTags.latest = latestMod.package.version;
}
var info = {
name: name,
modified: modifiedTime,
'dist-tags': distTags,
versions: versions,
};
debug('show %j', info);
// use faster etag
const resultEtag = etag([
modifiedTime,
distTags,
allVersionString,
]);
if (isJSONPRequest) {
ctx.jsonp = info;
} else {
ctx.body = JSON.stringify(info);
ctx.type = 'json';
// set cache
if (cacheKey) {
// set cache async, dont block the response
cache.pipeline()
.hmset(cacheKey, 'etag', resultEtag, 'body', ctx.body)
// cache 120s
.expire(cacheKey, 120)
.exec()
.catch(err => {
logger.error(err);
});
}
}
ctx.etag = resultEtag;
if (config.registryCacheControlHeader) {
ctx.set('cache-control', config.registryCacheControlHeader);
}
if (config.registryVaryHeader) {
ctx.set('vary', config.registryVaryHeader);
}
}
function* handleAbbreviatedMetaRequestWithFullMeta(ctx, name, modifiedTime, tags, rows) {
debug('show %s got %d rows, %d tags',
name, rows.length, tags.length);
var latestMod = null;
// set tags
var distTags = {};
for (var i = 0; i < tags.length; i++) {
var t = tags[i];
distTags[t.tag] = t.version;
}
// set versions and times
var versions = {};
var allVersionString = '';
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
// pkg is string ... ignore it
if (typeof row.package === 'string') {
continue;
}
// https://github.com/npm/registry/blob/master/docs/responses/package-metadata.md#abbreviated-version-object
var pkg = {
name: row.package.name,
version: row.package.version,
deprecated: row.package.deprecated,
dependencies: row.package.dependencies,
optionalDependencies: row.package.optionalDependencies,
devDependencies: row.package.devDependencies,
bundleDependencies: row.package.bundleDependencies,
peerDependencies: row.package.peerDependencies,
bin: row.package.bin,
directories: row.package.directories,
dist: row.package.dist,
engines: row.package.engines,
_hasShrinkwrap: row.package._hasShrinkwrap,
publish_time: row.package.publish_time || row.publish_time,
};
common.setDownloadURL(pkg, ctx);
versions[pkg.version] = pkg;
allVersionString += pkg.version + ',';
if ((!distTags.latest && !latestMod) || distTags.latest === pkg.version) {
latestMod = row;
}
}
if (!latestMod) {
latestMod = rows[0];
}
if (tags.length === 0) {
// some sync error reason, will cause tags missing
// set latest tag at least
distTags.latest = latestMod.package.version;
}
var info = {
name: name,
modified: modifiedTime,
'dist-tags': distTags,
versions: versions,
};
debug('show %j', info);
ctx.jsonp = info;
// use faster etag
ctx.etag = etag([
modifiedTime,
distTags,
allVersionString,
]);
if (config.registryCacheControlHeader) {
ctx.set('cache-control', config.registryCacheControlHeader);
}
if (config.registryVaryHeader) {
ctx.set('vary', config.registryVaryHeader);
}
}

View File

@@ -1,15 +1,15 @@
'use strict';
var packageService = require('../../../services/package');
const packageService = require('../../../services/package');
// GET /-/all
// List all packages names
// https://github.com/npm/npm-registry-client/blob/master/lib/get.js#L86
module.exports = function* () {
var updated = Date.now();
var names = yield packageService.listAllPublicModuleNames();
var result = { _updated: updated };
names.forEach(function (name) {
const updated = Date.now();
const names = yield packageService.listAllPublicModuleNames();
const result = { _updated: updated };
names.forEach(function(name) {
result[name] = true;
});
this.body = result;

View File

@@ -16,7 +16,7 @@
const packageService = require('../../../services/package');
module.exports = function*() {
module.exports = function* () {
const username = this.params.user;
const packages = yield packageService.listModulesByUser(username);
@@ -24,6 +24,6 @@ module.exports = function*() {
user: {
name: username,
},
packages: packages,
packages,
};
};

View File

@@ -16,11 +16,11 @@
const packageService = require('../../../services/package');
module.exports = function*() {
module.exports = function* () {
const name = this.params.name || this.params[0];
const dependents = yield packageService.listDependents(name);
this.body = {
dependents: dependents,
dependents,
};
};

View File

@@ -1,32 +1,21 @@
/**!
* Copyright(c) cnpm and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
*/
'use strict';
/**
* Module dependencies.
*/
const packageService = require('../../../services/package');
const config = require('../../../config');
// GET /-/short
// List public all packages names only
// List all packages names only
module.exports = function* () {
if (this.query.private_only) {
const tasks = [];
for (let i = 0; i < config.scopes.length; i++) {
const scope = config.scopes[i];
tasks.push(packageService.listPrivateModulesByScope(scope));
}
if (config.privatePackages && config.privatePackages.length > 0) {
tasks.push(packageService.listModules(config.privatePackages));
}
const results = yield tasks;
const names = [];
for (const rows of results) {
for (const row of rows) {
names.push(row.name);
}
}
this.body = names;
return;
}
this.body = yield packageService.listAllPublicModuleNames();
};

View File

@@ -1,46 +1,43 @@
'use strict';
var packageService = require('../../../services/package');
const packageService = require('../../../services/package');
var A_WEEK_MS = 3600000 * 24 * 7;
var TWA_DAYS_MS = 3600000 * 24 * 2;
const 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() {
var query = this.query;
const query = this.query;
if (query.stale !== 'update_after') {
this.status = 400;
const error = '[query_parse_error] Invalid value for `stale`.';
this.body = {
error,
reason: error,
error: 'query_parse_error',
reason: 'Invalid value for `stale`.',
};
return;
}
var startkey = Number(query.startkey);
let startkey = Number(query.startkey);
if (!startkey) {
this.status = 400;
const error = '[query_parse_error] Invalid value for `startkey`.';
this.body = {
error,
reason: error,
error: 'query_parse_error',
reason: 'Invalid value for `startkey`.',
};
return;
}
var updated = Date.now();
const updated = Date.now();
if (updated - startkey > A_WEEK_MS) {
startkey = updated - TWA_DAYS_MS;
console.warn('[%s] list modules since time out of range: query: %j, ip: %s, limit to %s',
Date(), query, this.ip, startkey);
startkey = updated - A_WEEK_MS;
console.warn('[%s] list modules since time out of range: query: %j, ip: %s',
Date(), query, this.ip);
}
var names = yield packageService.listPublicModuleNamesSince(startkey);
var result = { _updated: updated };
names.forEach(function (name) {
const names = yield packageService.listPublicModuleNamesSince(startkey);
const result = { _updated: updated };
names.forEach(function(name) {
result[name] = true;
});

View File

@@ -1,37 +0,0 @@
'use strict';
const moment = require('moment');
const packageService = require('../../../services/package');
// GET /-/allversions?date={2020-02-20}
// List all packages versions sync at date(gmt_modified)
module.exports = function* () {
const query = this.query;
const date = moment(query.date, 'YYYY-MM-DD');
if (!date.isValid()) {
this.status = 400;
const error = '[query_parse_error] Invalid value for `date`, should be `YYYY-MM-DD` format.';
this.body = {
error,
reason: error,
};
return;
}
const today = date.format('YYYY-MM-DD');
const rows = yield packageService.findAllModuleAbbreviateds({
gmt_modified: {
$gte: `${today} 00:00:00`,
$lte: `${today} 23:59:59`,
},
});
this.body = rows.map(row => {
return {
name: row.name,
version: row.version,
publish_time: new Date(row.publish_time),
gmt_modified: row.gmt_modified,
};
});
};

View File

@@ -1,23 +1,36 @@
/**!
* cnpmjs.org - controllers/registry/package/remove.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
var debug = require('debug')('cnpmjs.org:controllers:registry:package:remove');
var urlparse = require('url').parse;
var packageService = require('../../../services/package');
var totalService = require('../../../services/total');
var nfs = require('../../../common/nfs');
var logger = require('../../../common/logger');
var config = require('../../../config');
/**
* Module dependencies.
*/
const debug = require('debug')('cnpmjs.org:controllers:registry:package:remove');
const urlparse = require('url').parse;
const packageService = require('../../../services/package');
const totalService = require('../../../services/total');
const nfs = require('../../../common/nfs');
const logger = require('../../../common/logger');
// DELETE /:name/-rev/:rev
// https://github.com/npm/npm-registry-client/blob/master/lib/unpublish.js#L25
module.exports = function* remove(next) {
var name = this.params.name || this.params[0];
var rev = this.params.rev || this.params[1];
const name = this.params.name || this.params[0];
const rev = this.params.rev || this.params[1];
debug('remove all the module with name: %s, id: %s', name, rev);
var mods = yield packageService.listModulesByName(name);
const mods = yield packageService.listModulesByName(name);
debug('removeAll module %s: %d', name, mods.length);
var mod = mods[0];
const mod = mods[0];
if (!mod) {
return yield next;
}
@@ -28,25 +41,23 @@ module.exports = function* remove(next) {
totalService.plusDeleteModule(),
];
if (config.unpublishRemoveTarball) {
var keys = [];
for (var i = 0; i < mods.length; i++) {
var row = mods[i];
var dist = row.package.dist;
var key = dist.key;
if (!key) {
key = urlparse(dist.tarball).pathname;
}
key && keys.push(key);
const keys = [];
for (let i = 0; i < mods.length; i++) {
const row = mods[i];
const dist = row.package.dist;
let key = dist.key;
if (!key) {
key = urlparse(dist.tarball).pathname;
}
key && keys.push(key);
}
try {
yield keys.map(function (key) {
return nfs.remove(key);
});
} catch (err) {
logger.error(err);
}
try {
yield keys.map(function(key) {
return nfs.remove(key);
});
} catch (err) {
logger.error(err);
}
// remove the maintainers

View File

@@ -1,20 +1,33 @@
/**!
* cnpmjs.org - controllers/registry/package/remove_version.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
var debug = require('debug')('cnpmjs.org:controllers:registry:package:remove_version');
var packageService = require('../../../services/package');
var nfs = require('../../../common/nfs');
var logger = require('../../../common/logger');
var getCDNKey = require('../../../lib/common').getCDNKey;
var config = require('../../../config');
/**
* Module dependencies.
*/
const debug = require('debug')('cnpmjs.org:controllers:registry:package:remove_version');
const packageService = require('../../../services/package');
const nfs = require('../../../common/nfs');
const logger = require('../../../common/logger');
const getCDNKey = require('../../../lib/common').getCDNKey;
// DELETE /:name/download/:filename/-rev/:rev
// https://github.com/npm/npm-registry-client/blob/master/lib/unpublish.js#L97
module.exports = function* removeOneVersion(next) {
var name = this.params.name || this.params[0];
var filename = this.params.filename || this.params[1];
var id = Number(this.params.rev || this.params[2]);
const name = this.params.name || this.params[0];
const filename = this.params.filename || this.params[1];
const id = Number(this.params.rev || this.params[2]);
// cnpmjs.org-2.0.0.tgz
var version = filename.split(name + '-')[1];
let version = filename.split(name + '-')[1];
if (version) {
// 2.0.0.tgz
version = version.substring(0, version.lastIndexOf('.tgz'));
@@ -29,37 +42,33 @@ module.exports = function* removeOneVersion(next) {
return yield next;
}
var rs = yield [
const rs = yield [
packageService.getModuleById(id),
packageService.getModule(name, version),
];
var revertTo = rs[0];
var mod = rs[1]; // module need to delete
const revertTo = rs[0];
const mod = rs[1]; // module need to delete
if (!mod || mod.name !== name) {
return yield next;
}
if (config.unpublishRemoveTarball) {
var key = mod.package && mod.package.dist && mod.package.dist.key;
if (!key) {
key = getCDNKey(mod.name, filename);
}
if (revertTo && revertTo.package) {
debug('removing key: %s from nfs, revert to %s@%s', key, revertTo.name, revertTo.package.version);
} else {
debug('removing key: %s from nfs, no revert mod', key);
}
try {
yield nfs.remove(key);
} catch (err) {
logger.error(err);
}
let key = mod.package && mod.package.dist && mod.package.dist.key;
if (!key) {
key = getCDNKey(mod.name, filename);
}
if (revertTo && revertTo.package) {
debug('removing key: %s from nfs, revert to %s@%s', key, revertTo.name, revertTo.package.version);
} else {
debug('removing key: %s from nfs, no revert mod', key);
}
try {
yield nfs.remove(key);
} catch (err) {
logger.error(err);
}
// remove version from table
yield packageService.removeModulesByNameAndVersions(name, [version]);
yield packageService.removeModulesByNameAndVersions(name, [ version ]);
debug('removed %s@%s', name, version);
this.body = { ok: true };
};

View File

@@ -1,13 +1,12 @@
'use strict';
var debug = require('debug')('cnpmjs.org:controllers:registry:package:save');
var crypto = require('crypto');
var deprecateVersions = require('./deprecate');
var packageService = require('../../../services/package');
var common = require('../../../lib/common');
var nfs = require('../../../common/nfs');
var config = require('../../../config');
var hook = require('../../../services/hook');
const debug = require('debug')('cnpmjs.org:controllers:registry:package:save');
const crypto = require('crypto');
const deprecateVersions = require('./deprecate');
const packageService = require('../../../services/package');
const common = require('../../../lib/common');
const nfs = require('../../../common/nfs');
const config = require('../../../config');
// old flows:
// 1. add()
@@ -24,38 +23,36 @@ module.exports = function* save(next) {
// { content_type: 'application/octet-stream',
// data: 'H4sIAAAAA
// length: 9883
var pkg = this.request.body;
var username = this.user.name;
var name = this.params.name || this.params[0];
var filename = Object.keys(pkg._attachments || {})[0];
var version = Object.keys(pkg.versions || {})[0];
const pkg = this.request.body;
const username = this.user.name;
const name = this.params.name || this.params[0];
const filename = Object.keys(pkg._attachments || {})[0];
const version = Object.keys(pkg.versions || {})[0];
if (!version) {
this.status = 400;
const error = '[version_error] package.versions is empty';
this.body = {
error,
reason: error,
error: 'version_error',
reason: 'package.versions is empty',
};
return;
}
// check maintainers
var result = yield packageService.authMaintainer(name, username);
const result = yield packageService.authMaintainer(name, username);
if (!result.isMaintainer) {
this.status = 403;
const error = '[forbidden] ' + username + ' not authorized to modify ' + name +
', please contact maintainers: ' + result.maintainers.join(', ');
this.body = {
error,
reason: error,
error: 'forbidden user',
reason: username + ' not authorized to modify ' + name +
', please contact maintainers: ' + result.maintainers.join(', '),
};
return;
}
if (!filename) {
var hasDeprecated = false;
for (var v in pkg.versions) {
var row = pkg.versions[v];
let hasDeprecated = false;
for (const v in pkg.versions) {
const row = pkg.versions[v];
if (typeof row.deprecated === 'string') {
hasDeprecated = true;
break;
@@ -66,37 +63,25 @@ module.exports = function* save(next) {
}
this.status = 400;
const error = '[attachment_error] package._attachments is empty';
this.body = {
error,
reason: error,
error: 'attachment_error',
reason: 'package._attachments is empty',
};
return;
}
var attachment = pkg._attachments[filename];
var versionPackage = pkg.versions[version];
var maintainers = versionPackage.maintainers;
const attachment = pkg._attachments[filename];
const versionPackage = pkg.versions[version];
const maintainers = versionPackage.maintainers;
// should never happened in normal request
if (!maintainers) {
var authorizeType = common.getAuthorizeType(this);
if (authorizeType === common.AuthorizeType.BEARER) {
// With the token mode, pub lib with no maintainers
// make the maintainer to be puber
maintainers = [{
name: this.user.name,
email: this.user.email,
}];
} else {
// should never happened in normal request
this.status = 400;
const error = '[maintainers_error] request body need maintainers';
this.body = {
error,
reason: error,
};
return;
}
this.status = 400;
this.body = {
error: 'maintainers error',
reason: 'request body need maintainers',
};
return;
}
// notice that admins can not publish to all modules
@@ -104,33 +89,31 @@ module.exports = function* save(next) {
// make sure user in auth is in maintainers
// should never happened in normal request
var m = maintainers.filter(function (maintainer) {
const m = maintainers.filter(function(maintainer) {
return maintainer.name === username;
});
if (m.length === 0) {
this.status = 403;
const error = '[maintainers_error] ' + username + ' does not in maintainer list';
this.body = {
error,
reason: error,
error: 'maintainers error',
reason: username + ' does not in maintainer list',
};
return;
}
// TODO: add this info into some table
versionPackage._publish_on_cnpm = true;
var distTags = pkg['dist-tags'] || {};
var tags = []; // tag, version
for (var t in distTags) {
tags.push([t, distTags[t]]);
const distTags = pkg['dist-tags'] || {};
const tags = []; // tag, version
for (const t in distTags) {
tags.push([ t, distTags[t] ]);
}
if (tags.length === 0) {
this.status = 400;
const error = '[invalid] dist-tags should not be empty';
this.body = {
error,
reason: error,
error: 'invalid',
reason: 'dist-tags should not be empty',
};
return;
}
@@ -138,39 +121,36 @@ module.exports = function* save(next) {
debug('%s publish new %s:%s, attachment size: %s, maintainers: %j, distTags: %j',
username, name, version, attachment.length, versionPackage.maintainers, distTags);
var exists = yield packageService.getModule(name, version);
var shasum;
const exists = yield packageService.getModule(name, version);
let shasum;
if (exists) {
this.status = 403;
const error = '[forbidden] cannot modify pre-existing version: ' + version;
this.body = {
error,
reason: error,
error: 'forbidden',
reason: 'cannot modify pre-existing version: ' + version,
};
return;
}
// upload attachment
var tarballBuffer;
tarballBuffer = Buffer.from(attachment.data, 'base64');
const tarballBuffer = new Buffer(attachment.data, 'base64');
if (tarballBuffer.length !== attachment.length) {
this.status = 403;
const error = '[size_wrong] Attachment size ' + attachment.length
+ ' not match download size ' + tarballBuffer.length;
this.body = {
error,
reason: error,
error: 'size_wrong',
reason: 'Attachment size ' + attachment.length
+ ' not match download size ' + tarballBuffer.length,
};
return;
}
if (!distTags.latest) {
// need to check if latest tag exists or not
var latest = yield packageService.getModuleByTag(name, 'latest');
const latest = yield packageService.getModuleByTag(name, 'latest');
if (!latest) {
// auto add latest
tags.push(['latest', tags[0][1]]);
tags.push([ 'latest', tags[0][1] ]);
debug('auto add latest tag: %j', tags);
}
}
@@ -179,16 +159,16 @@ module.exports = function* save(next) {
shasum.update(tarballBuffer);
shasum = shasum.digest('hex');
var options = {
const options = {
key: common.getCDNKey(name, filename),
shasum: shasum
shasum,
};
var uploadResult = yield nfs.uploadBuffer(tarballBuffer, options);
const uploadResult = yield nfs.uploadBuffer(tarballBuffer, options);
debug('upload %j', uploadResult);
var dist = {
shasum: shasum,
size: attachment.length
const dist = {
shasum,
size: attachment.length,
};
// if nfs upload return a key, record it
@@ -199,29 +179,29 @@ module.exports = function* save(next) {
dist.tarball = uploadResult.key;
}
var mod = {
name: name,
version: version,
const mod = {
name,
version,
author: username,
package: versionPackage
package: versionPackage,
};
mod.package.dist = dist;
yield addDepsRelations(mod.package);
var addResult = yield packageService.saveModule(mod);
const addResult = yield packageService.saveModule(mod);
debug('%s module: save file to %s, size: %d, sha1: %s, dist: %j, version: %s',
addResult.id, dist.tarball, dist.size, shasum, dist, version);
if (tags.length) {
yield tags.map(function (tag) {
yield tags.map(function(tag) {
// tag: [tagName, version]
return packageService.addModuleTag(name, tag[0], tag[1]);
});
}
// ensure maintainers exists
var maintainerNames = maintainers.map(function (item) {
const maintainerNames = maintainers.map(function(item) {
return item.name;
});
yield packageService.addPrivateModuleMaintainers(name, maintainerNames);
@@ -229,24 +209,12 @@ module.exports = function* save(next) {
this.status = 201;
this.body = {
ok: true,
rev: String(addResult.id)
rev: String(addResult.id),
};
// hooks
const envelope = {
event: 'package:publish',
name: mod.name,
type: 'package',
version: mod.version,
hookOwner: null,
payload: null,
change: null,
};
hook.trigger(envelope);
};
function* addDepsRelations(pkg) {
var dependencies = Object.keys(pkg.dependencies || {});
let dependencies = Object.keys(pkg.dependencies || {});
if (dependencies.length > config.maxDependencies) {
dependencies = dependencies.slice(0, config.maxDependencies);
}

View File

@@ -1,11 +1,11 @@
'use strict';
var debug = require('debug')('cnpmjs.org:controllers:registry:package:show');
var semver = require('semver');
var packageService = require('../../../services/package');
var setDownloadURL = require('../../../lib/common').setDownloadURL;
var SyncModuleWorker = require('../../sync_module_worker');
var config = require('../../../config');
const debug = require('debug')('cnpmjs.org:controllers:registry:package:show');
const semver = require('semver');
const packageService = require('../../../services/package');
const setDownloadURL = require('../../../lib/common').setDownloadURL;
const SyncModuleWorker = require('../../sync_module_worker');
const config = require('../../../config');
/**
* [deprecate] api
@@ -16,14 +16,14 @@ var config = require('../../../config');
* GET /:name/:tag
*/
module.exports = function* show() {
var name = this.params.name || this.params[0];
var tag = this.params.version || this.params[1];
const name = this.params.name || this.params[0];
let tag = this.params.version || this.params[1];
if (tag === '*') {
tag = 'latest';
}
var version = semver.valid(tag);
var range = semver.validRange(tag);
var mod;
const version = semver.valid(tag);
const range = semver.validRange(tag);
let mod;
if (version) {
mod = yield packageService.getModule(name, version);
} else if (range) {
@@ -36,45 +36,38 @@ module.exports = function* show() {
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 [
const rs = yield [
packageService.listMaintainers(name),
packageService.listModuleTags(name),
];
var maintainers = rs[0];
const maintainers = rs[0];
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];
const tags = rs[1];
const distTags = {};
for (let i = 0; i < tags.length; i++) {
const t = tags[i];
distTags[t.tag] = t.version;
}
// show tags for npminstall faster download
mod.package['dist-tags'] = distTags;
this.jsonp = mod.package;
if (config.registryCacheControlHeader) {
this.set('cache-control', config.registryCacheControlHeader);
}
if (config.registryVaryHeader) {
this.set('vary', config.registryVaryHeader);
}
return;
}
// if not fond, sync from source registry
if (!this.allowSync) {
this.status = 404;
const error = '[not_exists] version not found: ' + version;
this.jsonp = {
error,
reason: error,
error: 'not exist',
reason: 'version not found: ' + version,
};
return;
}
// start sync
var logId = yield SyncModuleWorker.sync(name, 'sync-by-install');
const 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

@@ -1,53 +1,52 @@
'use strict';
var debug = require('debug')('cnpmjs.org:controllers:registry:package:tag');
var semver = require('semver');
var util = require('util');
var packageService = require('../../../services/package');
const debug = require('debug')('cnpmjs.org:controllers:registry:package:tag');
const semver = require('semver');
const util = require('util');
const packageService = require('../../../services/package');
// PUT /:name/:tag
// https://github.com/npm/npm-registry-client/blob/master/lib/tag.js#L4
// this.request("PUT", uri+"/"+tagName, { body : JSON.stringify(version) }, cb)
module.exports = function* tag() {
var version = this.request.body;
var name = this.params.name || this.params[0];
var tag = this.params.tag || this.params[1];
const version = this.request.body;
const name = this.params.name || this.params[0];
const tag = this.params.tag || this.params[1];
debug('tag %j to %s/%s', version, name, tag);
if (!version) {
this.status = 400;
const error = '[version_missed] version not found';
this.body = {
error,
reason: error,
error: 'version_missed',
reason: 'version not found',
};
return;
}
if (!semver.valid(version)) {
this.status = 403;
const error = util.format('[forbidden] setting tag %s to invalid version: %s: %s/%s',
const reason = util.format('setting tag %s to invalid version: %s: %s/%s',
tag, version, name, tag);
this.body = {
error,
reason: error,
error: 'forbidden',
reason,
};
return;
}
var mod = yield packageService.getModule(name, version);
const mod = yield packageService.getModule(name, version);
if (!mod) {
this.status = 403;
const error = util.format('[forbidden] setting tag %s to unknown version: %s: %s/%s',
const reason = util.format('setting tag %s to unknown version: %s: %s/%s',
tag, version, name, tag);
this.body = {
error,
reason: error,
error: 'forbidden',
reason,
};
return;
}
var row = yield packageService.addModuleTag(name, tag, version);
const row = yield packageService.addModuleTag(name, tag, version);
this.status = 201;
this.body = {
ok: true,

View File

@@ -1,19 +1,19 @@
'use strict';
var debug = require('debug')('cnpmjs.org:controllers:registry:package:update');
var packageService = require('../../../services/package');
var userService = require('../../../services/user');
var config = require('../../../config');
const debug = require('debug')('cnpmjs.org:controllers:registry:package:update');
const packageService = require('../../../services/package');
const userService = require('../../../services/user');
const config = require('../../../config');
// PUT /:name/-rev/:rev
//
// * remove with versions, then will `DELETE /:name/download/:filename/-rev/:rev`
// * ...
module.exports = function* update(next) {
var name = this.params.name || this.params[0];
const name = this.params.name || this.params[0];
debug('update module %s, %s, %j', this.url, name, this.request.body);
var body = this.request.body;
const body = this.request.body;
if (body.versions) {
yield updateVersions.call(this, next);
} else if (body.maintainers) {
@@ -26,12 +26,12 @@ module.exports = function* update(next) {
// update with versions
// https://github.com/npm/npm-registry-client/blob/master/lib/unpublish.js#L63
function* updateVersions(next) {
var name = this.params.name || this.params[0];
const name = this.params.name || this.params[0];
// left versions
var versions = this.request.body.versions;
const versions = this.request.body.versions;
// step1: list all the versions
var mods = yield packageService.listModulesByName(name);
const mods = yield packageService.listModulesByName(name);
debug('removeWithVersions module %s, left versions %j, %s mods',
name, Object.keys(versions), mods && mods.length);
if (!mods || !mods.length) {
@@ -40,12 +40,12 @@ function* updateVersions(next) {
// step3: calculate which versions need to remove and
// which versions need to remain
var removeVersions = [];
var removeVersionMaps = {};
var remainVersions = [];
const removeVersions = [];
const removeVersionMaps = {};
const remainVersions = [];
for (var i = 0; i < mods.length; i++) {
var v = mods[i].version;
for (let i = 0; i < mods.length; i++) {
const v = mods[i].version;
if (!versions[v]) {
removeVersions.push(v);
removeVersionMaps[v] = true;
@@ -64,11 +64,11 @@ function* updateVersions(next) {
// step 4: remove all the versions which need to remove
// let removeTar do remove versions from module table
var tags = yield packageService.listModuleTags(name);
const tags = yield packageService.listModuleTags(name);
var removeTags = [];
var latestRemoved = false;
tags.forEach(function (tag) {
const removeTags = [];
let latestRemoved = false;
tags.forEach(function(tag) {
// this tag need be removed
if (removeVersionMaps[tag.version]) {
removeTags.push(tag.id);
@@ -98,65 +98,60 @@ function* updateVersions(next) {
}
function* updateMaintainers() {
var name = this.params.name || this.params[0];
var body = this.request.body;
const name = this.params.name || this.params[0];
const body = this.request.body;
debug('updateMaintainers module %s, %j', name, body);
var usernames = body.maintainers.map(function (user) {
const usernames = body.maintainers.map(function(user) {
return user.name;
});
if (usernames.length === 0) {
this.status = 403;
const error = '[invalid_operation] Can not remove all maintainers';
this.body = {
error,
reason: error,
error: 'invalid operation',
reason: 'Can not remove all maintainers',
};
return;
}
if (config.customUserService) {
// ensure new authors are vaild
var maintainers = yield packageService.listMaintainerNamesOnly(name);
var map = {};
var newNames = [];
for (var i = 0; i < maintainers.length; i++) {
map[maintainers[i]] = 1;
const maintainers = yield packageService.listMaintainerNamesOnly(name);
const map = {};
const newNames = [];
for (const maintainer of maintainers) {
map[maintainer] = 1;
}
for (var i = 0; i < usernames.length; i++) {
var username = usernames[i];
for (const username of usernames) {
if (map[username] !== 1) {
newNames.push(username);
}
}
if (newNames.length > 0) {
var users = yield userService.list(newNames);
var map = {};
for (var i = 0; i < users.length; i++) {
var user = users[i];
const users = yield userService.list(newNames);
const map = {};
for (const user of users) {
map[user.login] = 1;
}
var invailds = [];
for (var i = 0; i < newNames.length; i++) {
var username = newNames[i];
const invailds = [];
for (const username of newNames) {
if (map[username] !== 1) {
invailds.push(username);
}
}
if (invailds.length > 0) {
this.status = 403;
const error = '[invalid] User: `' + invailds.join(', ') + '` not exists';
this.body = {
error,
reason: error,
error: 'invalid user name',
reason: 'User: `' + invailds.join(', ') + '` not exists',
};
return;
}
}
}
var r = yield packageService.updatePrivateModuleMaintainers(name, usernames);
const r = yield packageService.updatePrivateModuleMaintainers(name, usernames);
debug('result: %j', r);
this.status = 201;

View File

@@ -1,53 +0,0 @@
'use strict';
var ipRegex = require('ip-regex');
var tokenService = require('../../../services/token');
var userService = require('../../../services/user');
var ipv4 = ipRegex.v4({ exact: true });
module.exports = function* createToken() {
var readonly = this.request.body.readonly;
if (typeof readonly !== 'undefined' && typeof readonly !== 'boolean') {
this.status = 400;
var error = '[bad_request] readonly ' + readonly + ' is not boolean';
this.body = {
error,
reason: error,
};
return;
}
var cidrWhitelist = this.request.body.cidr_whitelist;
if (typeof cidrWhitelist !== 'undefined') {
var isValidateWhiteList = Array.isArray(cidrWhitelist) && cidrWhitelist.every(function (cidr) {
return ipv4.test(cidr);
});
if (!isValidateWhiteList) {
this.status = 400;
var error = '[bad_request] cide white list ' + JSON.stringify(cidrWhitelist) + ' is not validate ip array';
this.body = {
error,
reason: error,
};
return;
}
}
var password = this.request.body.password;
var user = yield userService.auth(this.user.name, password);
if (!user) {
this.status = 401;
var error = '[unauthorized] incorrect or missing password.';
this.body = {
error,
reason: error,
};
return;
}
var token = yield tokenService.createToken(this.user.name, {
readonly: !!readonly,
cidrWhitelist: cidrWhitelist || [],
});
this.status = 201;
this.body = token;
};

View File

@@ -1,8 +0,0 @@
'use strict';
var tokenService = require('../../../services/token');
module.exports = function* deleteToken() {
yield tokenService.deleteToken(this.user.name, this.params.UUID);
this.status = 204;
};

View File

@@ -1,60 +0,0 @@
'use strict';
var tokenService = require('../../../services/token');
var DEFAULT_PER_PAGE = 10;
var MIN_PER_PAGE = 1;
var MAX_PER_PAGE = 9999;
module.exports = function* createToken() {
var perPage = typeof this.query.perPage === 'undefined' ? DEFAULT_PER_PAGE : parseInt(this.query.perPage);
if (Number.isNaN(perPage)) {
this.status = 400;
var error = 'perPage ' + this.query.perPage + ' is not a number';
this.body = {
error,
reason: error,
};
return;
}
if (perPage < MIN_PER_PAGE || perPage > MAX_PER_PAGE) {
this.status = 400;
var error = 'perPage ' + this.query.perPage + ' is out of boundary';
this.body = {
error,
reason: error,
};
return;
}
var page = typeof this.query.page === 'undefined' ? 0 : parseInt(this.query.page);
if (Number.isNaN(page)) {
this.status = 400;
var error = 'page ' + this.query.page + ' is not a number';
this.body = {
error,
reason: error,
};
return;
}
if (page < 0) {
this.status = 400;
var error = 'page ' + this.query.page + ' is invalidate';
this.body = {
error,
reason: error,
};
return;
}
var tokens = yield tokenService.listToken(this.user.name, {
page: page,
perPage: perPage,
});
this.status = 200;
this.body = {
objects: tokens,
urls: {},
};
};

View File

@@ -1,9 +1,23 @@
/**!
* cnpmjs.org - controllers/registry/user/add.js
*
* Copyright(c) fengmk2 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';
var ensurePasswordSalt = require('./common').ensurePasswordSalt;
var userService = require('../../../services/user');
var config = require('../../../config');
var tokenService = require('../../../services/token');
/**
* Module dependencies.
*/
const ensurePasswordSalt = require('./common').ensurePasswordSalt;
const userService = require('../../../services/user');
const config = require('../../../config');
// npm 1.4.4
// add new user first
@@ -39,57 +53,9 @@ var tokenService = require('../../../services/token');
// roles: [],
// date: '2014-03-15T02:39:25.696Z' }
module.exports = function* addUser() {
var name = this.params.name;
var body = this.request.body || {};
if (!body.password || !body.name) {
this.status = 422;
const error = '[param_error] params missing, name, email or password missing';
this.body = {
error,
reason: error,
};
return;
}
var loginedUser;
try {
loginedUser = yield userService.authAndSave(body.name, body.password);
} catch (err) {
this.status = err.status || 500;
this.body = {
error: err.message,
reason: err.message,
};
return;
}
if (loginedUser) {
var token = yield tokenService.createToken(body.name, {
readonly: !!body.readonly,
cidrWhitelist: body.cidr_whitelist || [],
});
this.status = 201;
this.body = {
token: token.token,
ok: true,
id: 'org.couchdb.user:' + loginedUser.login,
rev: Date.now() + '-' + loginedUser.login
};
return;
}
if (config.customUserService) {
// user login fail, not allow to add new user
this.status = 401;
const error = '[unauthorized] Login fail, please check your login name and password';
this.body = {
error,
reason: error,
};
return;
}
var user = {
const name = this.params.name;
const body = this.request.body || {};
const user = {
name: body.name,
// salt: body.salt,
// password_sha: body.password_sha,
@@ -100,41 +66,63 @@ module.exports = function* addUser() {
ensurePasswordSalt(user, body);
if (!user.salt || !user.password_sha || !user.email) {
if (!body.password || !user.name || !user.salt || !user.password_sha || !user.email) {
this.status = 422;
const error = '[param_error] params missing, name, email or password missing';
this.body = {
error,
reason: error,
error: 'paramError',
reason: 'params missing, name, email or password missing.',
};
return;
}
var existUser = yield userService.get(name);
let loginedUser;
try {
loginedUser = yield userService.authAndSave(body.name, body.password);
} catch (err) {
this.status = err.status || 500;
this.body = {
error: err.name,
reason: err.message,
};
return;
}
if (loginedUser) {
this.status = 201;
this.body = {
ok: true,
id: 'org.couchdb.user:' + loginedUser.login,
rev: Date.now() + '-' + loginedUser.login,
};
return;
}
if (config.customUserService) {
// user login fail, not allow to add new user
this.status = 401;
this.body = {
error: 'unauthorized',
reason: 'Login fail, please check your login name and password',
};
return;
}
const existUser = yield userService.get(name);
if (existUser) {
this.status = 409;
const error = '[conflict] User ' + name + ' already exists';
this.body = {
error,
reason: error,
error: 'conflict',
reason: 'User ' + name + ' already exists.',
};
return;
}
// add new user
var result = yield userService.add(user);
const result = yield userService.add(user);
this.etag = '"' + result.rev + '"';
var token = yield tokenService.createToken(body.name, {
readonly: !!body.readonly,
cidrWhitelist: body.cidr_whitelist || [],
});
this.status = 201;
this.body = {
token: token.token,
ok: true,
id: 'org.couchdb.user:' + name,
rev: result.rev
rev: result.rev,
};
};

View File

@@ -14,10 +14,10 @@
* Module dependencies.
*/
var crypto = require('crypto');
var utility = require('utility');
const crypto = require('crypto');
const utility = require('utility');
exports.ensurePasswordSalt = function (user, body) {
exports.ensurePasswordSalt = function(user, body) {
if (!user.password_sha && body.password) {
// create password_sha on server
user.salt = crypto.randomBytes(30).toString('hex');

View File

@@ -1,7 +0,0 @@
'use strict';
// https://docs.npmjs.com/cli/ping
module.exports = function* () {
this.status = 200;
this.body = {};
};

View File

@@ -1,16 +1,31 @@
/**!
* cnpmjs.org - controllers/registry/user/show.js
*
* Copyright(c) fengmk2 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';
var userService = require('../../../services/user');
/**
* Module dependencies.
*/
const userService = require('../../../services/user');
// GET /-/user/org.couchdb.user::name
module.exports = function* show(next) {
var name = this.params.name;
var user = yield userService.getAndSave(name);
const name = this.params.name;
const user = yield userService.getAndSave(name);
if (!user) {
return yield next;
}
var data = user.json;
let data = user.json;
if (!data) {
data = {
_id: 'org.couchdb.user:' + user.name,
@@ -38,7 +53,7 @@ module.exports = function* show(next) {
fullname: data.name || data.login,
homepage: data.html_url,
scopes: data.scopes,
site_admin: data.site_admin
site_admin: data.site_admin,
};
}

View File

@@ -1,8 +1,23 @@
/**!
* cnpmjs.org - controllers/registry/user/update.js
*
* Copyright(c) fengmk2 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';
var debug = require('debug')('cnpmjs.org:controllers:registry:user:update');
var ensurePasswordSalt = require('./common').ensurePasswordSalt;
var userService = require('../../../services/user');
/**
* Module dependencies.
*/
const debug = require('debug')('cnpmjs.org:controllers:registry:user:update');
const ensurePasswordSalt = require('./common').ensurePasswordSalt;
const userService = require('../../../services/user');
// logined before update, no need to auth user again
// { name: 'admin',
@@ -22,8 +37,8 @@ var userService = require('../../../services/user');
// admin: true,
// scopes: [ '@cnpm', '@cnpmtest' ] } }
module.exports = function* updateUser(next) {
var name = this.params.name;
var rev = this.params.rev;
const name = this.params.name;
const rev = this.params.rev;
if (!name || !rev) {
return yield next;
}
@@ -32,16 +47,15 @@ module.exports = function* updateUser(next) {
if (name !== this.user.name) {
// must auth user first
this.status = 401;
const error = '[unauthorized] Name is incorrect';
this.body = {
error,
reason: error,
error: 'unauthorized',
reason: 'Name is incorrect.',
};
return;
}
var body = this.request.body || {};
var user = {
const body = this.request.body || {};
const user = {
name: body.name,
// salt: body.salt,
// password_sha: body.password_sha,
@@ -57,21 +71,19 @@ module.exports = function* updateUser(next) {
if (!body.password || !user.name || !user.salt || !user.password_sha || !user.email) {
this.status = 422;
const error = '[param_error] params missing, name, email or password missing';
this.body = {
error,
reason: error,
error: 'paramError',
reason: 'params missing, name, email or password missing.',
};
return;
}
var result = yield userService.update(user);
const result = yield userService.update(user);
if (!result) {
this.status = 409;
const error = '[conflict] Document update conflict';
this.body = {
error,
reason: error,
error: 'conflict',
reason: 'Document update conflict.',
};
return;
}
@@ -80,6 +92,6 @@ module.exports = function* updateUser(next) {
this.body = {
ok: true,
id: 'org.couchdb.user:' + user.name,
rev: result.rev
rev: result.rev,
};
};

View File

@@ -1,9 +0,0 @@
'use strict';
// https://docs.npmjs.com/cli/whoami
module.exports = function* () {
this.status = 200;
this.body = {
username: this.user.name,
};
};

View File

@@ -14,36 +14,35 @@
* Module dependencies.
*/
var packageService = require('../../services/package');
const packageService = require('../../services/package');
// GET /-/by-user/:user
exports.list = function* () {
var users = this.params.user.split('|');
const users = this.params.user.split('|');
if (users.length > 20) {
this.status = 400;
const error = '[bad_request] reach max user names limit, must <= 20 user names';
this.body = {
error,
reason: error,
error: 'bad_request',
reason: 'reach max user names limit, must <= 20 user names',
};
return;
}
var firstUser = users[0];
const firstUser = users[0];
if (!firstUser) {
// params.user = '|'
this.body = {};
return;
}
var tasks = {};
for (var i = 0; i < users.length; i++) {
var username = users[i];
const tasks = {};
for (let i = 0; i < users.length; i++) {
const username = users[i];
tasks[username] = packageService.listPublicModuleNamesByUser(username);
}
var data = yield tasks;
for (var k in data) {
const data = yield tasks;
for (const k in data) {
if (data[k].length === 0) {
data[k] = undefined;
}

View File

@@ -1,77 +1,80 @@
/**!
* cnpmjs.org - controllers/sync.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/
'use strict';
var debug = require('debug')('cnpmjs.org:controllers:sync');
var Log = require('../services/module_log');
var SyncModuleWorker = require('./sync_module_worker');
var config = require('../config');
/**
* Module dependencies.
*/
const debug = require('debug')('cnpmjs.org:controllers:sync');
const Log = require('../services/module_log');
const SyncModuleWorker = require('./sync_module_worker');
const config = require('../config');
exports.sync = function* () {
var username = this.user.name || 'anonymous';
var name = this.params.name || this.params[0];
var type = 'package';
const username = this.user.name || 'anonymous';
let name = this.params.name || this.params[0];
let type = 'package';
if (name.indexOf(':') > 0) {
// user:fengmk2
// package:pedding
var splits = name.split(':');
const splits = name.split(':');
type = splits[0];
name = splits[1];
}
var publish = this.query.publish === 'true';
var noDep = this.query.nodeps === 'true';
var syncUpstreamFirst = this.query.sync_upstream === 'true';
if (!config.sourceNpmRegistryIsCNpm) {
syncUpstreamFirst = false;
}
debug('sync %s with query: %j, syncUpstreamFirst: %s', name, this.query, syncUpstreamFirst);
const publish = this.query.publish === 'true';
const noDep = this.query.nodeps === 'true';
debug('sync %s with query: %j', name, this.query);
if (type === 'package' && publish && !this.user.isAdmin) {
this.status = 403;
const error = '[no_perms] Only admin can publish';
this.body = {
error,
reason: error,
error: 'no_perms',
reason: 'Only admin can publish',
};
return;
}
var options = {
type: type,
publish: publish,
noDep: noDep,
syncUpstreamFirst: syncUpstreamFirst,
const options = {
type,
publish,
noDep,
syncUpstreamFirst: config.sourceNpmRegistryIsCNpm,
};
var logId = yield SyncModuleWorker.sync(name, username, options);
const logId = yield SyncModuleWorker.sync(name, username, options);
debug('sync %s got log id %j', name, logId);
this.status = 201;
this.body = {
ok: true,
logId: logId
logId,
};
};
exports.getSyncLog = function* (next) {
var logId = Number(this.params.id || this.params[1]);
var offset = Number(this.query.offset) || 0;
const logId = Number(this.params.id || this.params[1]);
const offset = Number(this.query.offset) || 0;
if (!logId) { // NaN
this.status = 404;
return;
}
var row = yield Log.get(logId);
const row = yield Log.get(logId);
if (!row) {
return yield next;
}
var log = row.log.trim();
var syncDone = row.log.indexOf('[done] Sync') >= 0;
let log = row.log.trim();
if (offset > 0) {
log = log.split('\n').slice(offset).join('\n');
if (!log && syncDone) {
// append the last 1k string
// the cnpm client sync need the `[done] Sync {name}` string to detect when sync task finished
log = '... ignore long logs ...\n' + row.log.substring(row.log.length - 1024);
}
}
this.body = { ok: true, syncDone: syncDone, log: log };
this.body = { ok: true, log };
};

File diff suppressed because it is too large Load Diff

View File

@@ -4,33 +4,17 @@ const Total = require('../services/total');
const version = require('../package.json').version;
const config = require('../config');
const getDownloadTotal = require('./utils').getDownloadTotal;
const cacheClient = require('../common/cache');
const logger = require('../common/logger');
const startTime = '' + Date.now();
let cache = null;
module.exports = function* showTotal() {
if (cache && Date.now() - cache.cache_time < 120000) {
// cache 120 seconds
if (cache && Date.now() - cache.cache_time < 10000) {
// cache 10 seconds
this.body = cache;
return;
}
const cacheKey = 'registry_total';
if (cacheClient) {
const result = yield cacheClient.get(cacheKey);
if (result) {
this.body = JSON.parse(result);
return;
}
}
if (cache) {
// set cache_time fisrt, avoid query in next time
cache.cache_time = Date.now();
}
const r = yield [ Total.get(), getDownloadTotal() ];
const total = r[0];
const download = r[1];
@@ -44,17 +28,7 @@ module.exports = function* showTotal() {
total.sync_model = config.syncModel;
cache = total;
cache.cache_time = Date.now();
total.cache_time = Date.now();
this.body = total;
if (cacheClient) {
cacheClient.pipeline()
.set(cacheKey, JSON.stringify(total))
// cache 12h
.expire(cacheKey, 3600 * 12)
.exec()
.catch(err => {
logger.error(err);
});
}
};

View File

@@ -1,33 +1,42 @@
/**!
* cnpmjs.org - controllers/utils.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
var debug = require('debug')('cnpmjs.org:controllers:utils');
var path = require('path');
var fs = require('fs');
var utility = require('utility');
var ms = require('humanize-ms');
var moment = require('moment');
var rimraf = require('rimraf');
var downloadTotalService = require('../services/download_total');
var nfs = require('../common/nfs');
var config = require('../config');
/**
* Module dependencies.
*/
var DOWNLOAD_TIMEOUT = ms('10m');
const debug = require('debug')('cnpmjs.org:controllers:utils');
const path = require('path');
const fs = require('fs');
const utility = require('utility');
const ms = require('humanize-ms');
const moment = require('moment');
const downloadTotalService = require('../services/download_total');
const nfs = require('../common/nfs');
const config = require('../config');
const DOWNLOAD_TIMEOUT = ms('10m');
exports.downloadAsReadStream = function* (key) {
var options = { timeout: DOWNLOAD_TIMEOUT };
const options = { timeout: DOWNLOAD_TIMEOUT };
if (nfs.createDownloadStream) {
return yield nfs.createDownloadStream(key, options);
}
var tmpPath = path.join(config.uploadDir,
const tmpPath = path.join(config.uploadDir,
utility.randomString() + key.replace(/\//g, '-'));
var tarball;
function cleanup() {
debug('cleanup %s', tmpPath);
rimraf(tmpPath, utility.noop);
if (tarball) {
tarball.destroy();
}
fs.unlink(tmpPath, utility.noop);
}
debug('downloadAsReadStream() %s to %s', key, tmpPath);
try {
@@ -37,32 +46,32 @@ exports.downloadAsReadStream = function* (key) {
cleanup();
throw err;
}
tarball = fs.createReadStream(tmpPath);
const tarball = fs.createReadStream(tmpPath);
tarball.once('error', cleanup);
tarball.once('end', cleanup);
return tarball;
};
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 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');
let end = moment();
let start = end.clone().subtract(1, 'months').startOf('month');
const lastday = end.clone().subtract(1, 'days').format('YYYY-MM-DD');
let lastweekStart = end.clone().subtract(1, 'weeks').startOf('isoweek');
const lastweekEnd = lastweekStart.clone().endOf('isoweek').format('YYYY-MM-DD');
const lastmonthEnd = start.clone().endOf('month').format('YYYY-MM-DD');
const thismonthStart = end.clone().startOf('month').format('YYYY-MM-DD');
const thisweekStart = end.clone().startOf('isoweek').format('YYYY-MM-DD');
start = start.format('YYYY-MM-DD');
end = end.format('YYYY-MM-DD');
lastweekStart = lastweekStart.format('YYYY-MM-DD');
var method = name ? 'getModuleTotal' : 'getTotal';
var args = [start, end];
const method = name ? 'getModuleTotal' : 'getTotal';
const args = [ start, end ];
if (name) {
args.unshift(name);
}
var rows = yield downloadTotalService[method].apply(downloadTotalService, args);
var download = {
const rows = yield downloadTotalService[method].apply(downloadTotalService, args);
const download = {
today: 0,
thisweek: 0,
thismonth: 0,
@@ -71,8 +80,8 @@ exports.getDownloadTotal = function* (name) {
lastmonth: 0,
};
for (var i = 0; i < rows.length; i++) {
var r = rows[i];
for (let i = 0; i < rows.length; i++) {
const r = rows[i];
if (r.date === end) {
download.today += r.count;
}
@@ -96,11 +105,11 @@ exports.getDownloadTotal = function* (name) {
return download;
};
exports.setLicense = function (pkg) {
var license;
exports.setLicense = function(pkg) {
let license;
license = pkg.license || pkg.licenses || pkg.licence || pkg.licences;
if (!license) {
return ;
return;
}
if (Array.isArray(license)) {
@@ -110,7 +119,7 @@ exports.setLicense = function (pkg) {
if (typeof license === 'object') {
pkg.license = {
name: license.name || license.type,
url: license.url
url: license.url,
};
}
@@ -118,38 +127,38 @@ exports.setLicense = function (pkg) {
if (license.match(/(http|https)(:\/\/)/ig)) {
pkg.license = {
name: license,
url: license
url: license,
};
} else {
pkg.license = {
url: exports.getOssLicenseUrlFromName(license),
name: license
name: license,
};
}
}
};
exports.getOssLicenseUrlFromName = function (name) {
var base = 'http://opensource.org/licenses/';
exports.getOssLicenseUrlFromName = function(name) {
const base = 'http://opensource.org/licenses/';
var licenseMap = {
'bsd': 'BSD-2-Clause',
'mit': 'MIT',
'x11': 'MIT',
const licenseMap = {
bsd: 'BSD-2-Clause',
mit: 'MIT',
x11: 'MIT',
'mit/x11': 'MIT',
'apache 2.0': 'Apache-2.0',
'apache2': 'Apache-2.0',
apache2: 'Apache-2.0',
'apache 2': 'Apache-2.0',
'apache-2': 'Apache-2.0',
'apache': 'Apache-2.0',
'gpl': 'GPL-3.0',
'gplv3': 'GPL-3.0',
'gplv2': 'GPL-2.0',
'gpl3': 'GPL-3.0',
'gpl2': 'GPL-2.0',
'lgpl': 'LGPL-2.1',
apache: 'Apache-2.0',
gpl: 'GPL-3.0',
gplv3: 'GPL-3.0',
gplv2: 'GPL-2.0',
gpl3: 'GPL-3.0',
gpl2: 'GPL-2.0',
lgpl: 'LGPL-2.1',
'lgplv2.1': 'LGPL-2.1',
'lgplv2': 'LGPL-2.1'
lgplv2: 'LGPL-2.1',
};
return licenseMap[name.toLowerCase()] ?

View File

@@ -1,20 +1,31 @@
/**!
* cnpmjs.org - controllers/web/badge.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
*/
'use strict';
var config = require('../../config');
var packageService = require('../../services/package');
var DownloadTotal = require('../../services/download_total');
/**
* Module dependencies.
*/
const utility = require('utility');
const util = require('util');
const config = require('../../config');
const packageService = require('../../services/package');
const DownloadTotal = require('../../services/download_total');
exports.version = function* () {
var color = 'grey';
var name = this.params[0];
var tag = this.query.tag || 'latest';
var version = this.query.version;
let info;
if (version) {
info = yield packageService.getModule(name, version);
} else {
info = yield packageService.getModuleByTag(name, tag);
}
let color = 'lightgrey';
let version = 'invalid';
const name = this.params[0];
const tag = this.query.tag || 'latest';
const info = yield packageService.getModuleByTag(name, tag);
if (info) {
version = info.version;
if (/^0\.0\./.test(version)) {
@@ -29,23 +40,23 @@ exports.version = function* () {
}
}
var subject = config.badgeSubject;
let subject = config.badgeSubject.replace(/\-/g, '--');
if (this.query.subject) {
subject = this.query.subject;
subject = this.query.subject.replace(/\-/g, '--');
}
if (!version) {
version = 'invalid';
}
var style = this.query.style || 'flat-square';
var url = config.badgeService.url(subject, version, { color, style });
version = version.replace(/\-/g, '--');
const style = this.query.style || 'flat-square';
const 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;
var url = config.badgeService.url('downloads', count, { style });
const name = this.params[0];
const count = yield DownloadTotal.getTotalByName(name);
const style = this.query.style || 'flat-square';
const url = util.format(config.badgePrefixURL + '/downloads-%s-brightgreen.svg?style=%s',
count, utility.encodeURIComponent(style));
this.redirect(url);
};

View File

@@ -1,12 +1,27 @@
/**!
* cnpmjs.org - controllers/web/package/list_privates.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';
var packageService = require('../../../services/package');
var config = require('../../../config');
/**
* Module dependencies.
*/
const packageService = require('../../../services/package');
const config = require('../../../config');
module.exports = function* listPrivates() {
var tasks = {};
for (var i = 0; i < config.scopes.length; i++) {
var scope = config.scopes[i];
const tasks = {};
for (let i = 0; i < config.scopes.length; i++) {
const scope = config.scopes[i];
tasks[scope] = packageService.listPrivateModulesByScope(scope);
}
@@ -14,9 +29,9 @@ module.exports = function* listPrivates() {
tasks['no scoped'] = packageService.listModules(config.privatePackages);
}
var scopes = yield tasks;
const scopes = yield tasks;
yield this.render('private', {
title: 'private packages',
scopes: scopes
scopes,
});
};

View File

@@ -1,30 +1,40 @@
/**!
* cnpmjs.org - controllers/web/package/search.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/
'use strict';
var debug = require('debug')('cnpmjs.org:controllers:web:package:search');
var packageService = require('../../../services/package');
var config = require('../../../config');
/**
* Module dependencies.
*/
const debug = require('debug')('cnpmjs.org:controllers:web:package:search');
const 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;
const params = this.params;
const word = params.word || params[0];
let limit = Number(this.query.limit) || 100;
if (limit > 10000) {
limit = 10000;
}
if (config.disableSearch) {
return this.redirect(`/package/${encodeURIComponent(word)}`);
}
debug('search %j', word);
var result = yield packageService.search(word, {
limit: limit
const result = yield packageService.search(word, {
limit,
});
var match = null;
for (var i = 0; i < result.searchMatchs.length; i++) {
var p = result.searchMatchs[i];
let match = null;
for (let i = 0; i < result.searchMatchs.length; i++) {
const p = result.searchMatchs[i];
if (p.name === word) {
match = p;
break;
@@ -35,7 +45,7 @@ module.exports = function* search() {
if (this.query && this.query.type === 'json') {
this.jsonp = {
keyword: word,
match: match,
match,
packages: result.searchMatchs,
keywords: result.keywordMatchs,
};
@@ -44,7 +54,7 @@ module.exports = function* search() {
yield this.render('search', {
title: 'Keyword - ' + word,
keyword: word,
match: match,
match,
packages: result.searchMatchs,
keywords: result.keywordMatchs,
});

View File

@@ -1,34 +1,49 @@
/**!
* cnpmjs.org - controllers/web/package/search_range.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';
var packageService = require('../../../services/package');
/**
* Module dependencies.
*/
const packageService = require('../../../services/package');
module.exports = function* searchRange() {
var startKey = this.query.startkey || '';
let startKey = this.query.startkey || '';
if (startKey[0] === '"') {
startKey = startKey.substring(1);
}
if (startKey[startKey.length - 1] === '"') {
startKey = startKey.substring(0, startKey.length - 1);
}
var limit = Number(this.query.limit) || 20;
var result = yield packageService.search(startKey, {limit: limit});
const limit = Number(this.query.limit) || 20;
const result = yield packageService.search(startKey, { limit });
var packages = result.searchMatchs.concat(result.keywordMatchs);
const packages = result.searchMatchs.concat(result.keywordMatchs);
var rows = [];
for (var i = 0; i < packages.length; i++) {
var p = packages[i];
var row = {
const rows = [];
for (let i = 0; i < packages.length; i++) {
const p = packages[i];
const row = {
key: p.name,
count: 1,
value: {
name: p.name,
description: p.description,
}
},
};
rows.push(row);
}
this.body = {
rows: rows
rows,
};
};

View File

@@ -1,69 +1,60 @@
'use strict';
var debug = require('debug')('cnpmjs.org:controllers:web:package:show');
var bytes = require('bytes');
var giturl = require('giturl');
var moment = require('moment');
var semver = require('semver');
var gravatar = require('gravatar');
var humanize = require('humanize-number');
var config = require('../../../config');
var utils = require('../../utils');
var setDownloadURL = require('../../../lib/common').setDownloadURL;
var renderMarkdown = require('../../../common/markdown').render;
var packageService = require('../../../services/package');
var downloadTotalService = require('../../../services/download_total');
const debug = require('debug')('cnpmjs.org:controllers:web:package:show');
const bytes = require('bytes');
const giturl = require('giturl');
const moment = require('moment');
const semver = require('semver');
const gravatar = require('gravatar');
const humanize = require('humanize-number');
const config = require('../../../config');
const utils = require('../../utils');
const setDownloadURL = require('../../../lib/common').setDownloadURL;
const renderMarkdown = require('../../../common/markdown').render;
const packageService = require('../../../services/package');
module.exports = function* show(next) {
var params = this.params;
const params = this.params;
// normal: {name: $name, version: $version}
// scope: [$name, $version]
var orginalName = params.name || params[0];
var name = orginalName;
var tag = params.version || params[1];
const orginalName = params.name || params[0];
const name = orginalName;
const tag = params.version || params[1];
debug('display %s with %j', name, params);
var getPackageMethod;
var getPackageArgs;
var version = semver.valid(tag || '');
let getPackageMethod;
let getPackageArgs;
const version = semver.valid(tag || '');
if (version) {
getPackageMethod = 'getModule';
getPackageArgs = [name, version];
getPackageArgs = [ name, version ];
} else {
getPackageMethod = 'getModuleByTag';
getPackageArgs = [name, tag || 'latest'];
getPackageArgs = [ name, tag || 'latest' ];
}
var pkg = yield packageService[getPackageMethod].apply(packageService, getPackageArgs);
if ((!pkg || !pkg.package) && tag) {
// + if we can't find it by tag, it may be a non-semver version, check it then
// + if we can't find it by version, it may be a tag, check it then
getPackageMethod = version ? 'getModuleByTag' : 'getModule';
pkg = yield packageService[getPackageMethod](name, tag);
}
// if it's still not found
let pkg = yield packageService[getPackageMethod].apply(packageService, getPackageArgs);
if (!pkg || !pkg.package) {
// check if unpublished
var unpublishedInfo = yield packageService.getUnpublishedModule(name);
const unpublishedInfo = yield packageService.getUnpublishedModule(name);
debug('show unpublished %j', unpublishedInfo);
if (unpublishedInfo) {
var data = {
name: name,
unpublished: unpublishedInfo.package
const data = {
name,
unpublished: unpublishedInfo.package,
};
data.unpublished.time = new Date(data.unpublished.time);
if (data.unpublished.maintainers) {
for (var i = 0; i < data.unpublished.maintainers.length; i++) {
var maintainer = data.unpublished.maintainers[i];
for (let i = 0; i < data.unpublished.maintainers.length; i++) {
const maintainer = data.unpublished.maintainers[i];
if (maintainer.email) {
maintainer.gravatar = gravatar.url(maintainer.email, {s: '50', d: 'retro'}, true);
maintainer.gravatar = gravatar.url(maintainer.email, { s: '50', d: 'retro' }, true);
}
}
}
yield this.render('package_unpublished', {
package: data,
title: 'Package - ' + name
title: 'Package - ' + name,
});
return;
}
@@ -71,52 +62,20 @@ module.exports = function* show(next) {
return yield next;
}
var r = yield [
const r = yield [
utils.getDownloadTotal(name),
packageService.listDependents(name),
packageService.listStarUserNames(name),
packageService.listMaintainers(name),
packageService.listModulesByName(name),
packageService.listModuleTags(name),
downloadTotalService.getTotalByName(name),
];
var download = r[0];
var dependents = r[1];
var users = r[2];
var maintainers = r[3];
var rows = r[4];
var tags = r[5];
var downloadTotal = r[6];
const versionsMap = {};
const versions = [];
for (const row of rows) {
var versionPkg = row.package;
// pkg is string ... ignore it
if (typeof versionPkg === 'string') {
continue;
}
versionPkg.fromNow = moment(versionPkg.publish_time || row.publish_time).fromNow();
versions.push(versionPkg);
versionsMap[versionPkg.version] = versionPkg;
}
for (const row of tags) {
row.fromNow = versionsMap[row.version] && versionsMap[row.version].fromNow;
}
const download = r[0];
const dependents = r[1];
const users = r[2];
const maintainers = r[3];
pkg.package.fromNow = moment(pkg.publish_time).fromNow();
pkg = pkg.package;
pkg.users = users;
pkg.versions = versions;
pkg.tags = tags;
if (!pkg.readme && config.enableAbbreviatedMetadata) {
var packageReadme = yield packageService.getPackageReadme(name);
if (packageReadme) {
pkg.readme = packageReadme.readme;
}
}
if (pkg.readme && typeof pkg.readme !== 'string') {
pkg.readme = 'readme is not string: ' + JSON.stringify(pkg.readme);
} else {
@@ -132,10 +91,10 @@ module.exports = function* show(next) {
}
if (pkg.maintainers) {
for (var i = 0; i < pkg.maintainers.length; i++) {
var maintainer = pkg.maintainers[i];
for (let i = 0; i < pkg.maintainers.length; i++) {
const maintainer = pkg.maintainers[i];
if (maintainer.email) {
maintainer.gravatar = gravatar.url(maintainer.email, {s: '50', d: 'retro'}, true);
maintainer.gravatar = gravatar.url(maintainer.email, { s: '50', d: 'retro' }, true);
}
}
}
@@ -143,7 +102,7 @@ module.exports = function* show(next) {
if (pkg._npmUser) {
pkg.lastPublishedUser = pkg._npmUser;
if (pkg.lastPublishedUser.email) {
pkg.lastPublishedUser.gravatar = gravatar.url(pkg.lastPublishedUser.email, {s: '50', d: 'retro'}, true);
pkg.lastPublishedUser.gravatar = gravatar.url(pkg.lastPublishedUser.email, { s: '50', d: 'retro' }, true);
}
}
@@ -151,9 +110,7 @@ module.exports = function* show(next) {
pkg.repository = null;
}
if (pkg.repository && pkg.repository.url) {
if (!pkg.repository.weburl) {
pkg.repository.weburl = /^https?:\/\//.test(pkg.repository.url) ? pkg.repository.url : (giturl.parse(pkg.repository.url) || pkg.repository.url);
}
pkg.repository.weburl = /^https?:\/\//.test(pkg.repository.url) ? pkg.repository.url : (giturl.parse(pkg.repository.url) || pkg.repository.url);
}
if (!pkg.bugs) {
pkg.bugs = {};
@@ -161,7 +118,7 @@ module.exports = function* show(next) {
utils.setLicense(pkg);
for (var k in download) {
for (const k in download) {
download[k] = humanize(download[k]);
}
setDownloadURL(pkg, this, config.registryHost);
@@ -177,7 +134,6 @@ module.exports = function* show(next) {
}
pkg.registryUrl = '//' + config.registryHost + '/' + pkg.name;
pkg.registryPackageUrl = '//' + config.registryHost + '/' + pkg.name + '/' + pkg.version;
// pkg.engines = {
// "python": ">= 0.11.9",
@@ -186,58 +142,48 @@ module.exports = function* show(next) {
// "node2": ">= 0.10.9",
// "node3": ">= 0.6.9",
// };
// "engines": "0.10.24",
// invalid engines
if (pkg.engines && typeof pkg.engines !== 'object') {
pkg.engines = {};
}
for (var k in pkg.engines) {
var engine = String(pkg.engines[k] || '').trim();
var color = 'blue';
if (k.indexOf('node') === 0 || k.indexOf('install-') === 0) {
color = 'green';
var version = /(\d+\.)/.exec(engine);
if (version) {
version = version[0];
if (/^[0123]\./.test(version)) {
color = 'red';
if (pkg.engines) {
for (const k in pkg.engines) {
const engine = String(pkg.engines[k] || '').trim();
let color = 'blue';
if (k.indexOf('node') === 0) {
color = 'yellowgreen';
let version = /(\d+\.\d+\.\d+)/.exec(engine);
if (version) {
version = version[0];
if (/^0\.11\.\d+/.test(version)) {
color = 'red';
} else if (/^0\.10\./.test(version) ||
/^0\.12\./.test(version) ||
/^0\.14\./.test(version) ||
/^[^0]+\./.test(version)) {
color = 'brightgreen';
}
}
}
if (engine === '*') {
color = 'red';
}
pkg.engines[k] = {
version: engine,
title: k + ': ' + engine,
badgeURL: config.badgePrefixURL + '/' + encodeURIComponent(k) +
'-' + encodeURIComponent(engine) + '-' + color + '.svg?style=flat-square',
};
}
pkg.engines[k] = {
version: engine,
title: k + ': ' + engine,
badgeURL: config.badgeService.url(k, engine, { color }),
};
}
let packagephobiaSupport = downloadTotal >= config.packagephobiaMinDownloadCount;
if (pkg._publish_on_cnpm) {
pkg.isPrivate = true;
// need download total >= 1000
packagephobiaSupport = downloadTotal >= config.packagephobiaMinDownloadCount && config.packagephobiaSupportPrivatePackage;
} else {
pkg.isPrivate = false;
// add security check badge
pkg.snyk = {
badge: `${config.snykUrl}/test/npm/${pkg.name}/badge.svg`,
badge: `${config.snykUrl}/test/npm/${pkg.name}/badge.svg?style=flat-square`,
url: `${config.snykUrl}/test/npm/${pkg.name}`,
};
}
if (packagephobiaSupport) {
pkg.packagephobia = {
badge: `${config.packagephobiaURL}/badge?p=${pkg.name}@${pkg.version}`,
url: `${config.packagephobiaURL}/result?p=${pkg.name}@${pkg.version}`,
};
}
yield this.render('package', {
title: 'Package - ' + pkg.name,
package: pkg,
download: download,
download,
});
};

View File

@@ -16,19 +16,19 @@
*/
module.exports = function* showSync() {
var name = this.params.name || this.params[0] || this.query.name;
let name = this.params.name || this.params[0] || this.query.name;
if (!name) {
return this.redirect('/');
}
var type = 'package';
let type = 'package';
if (name.indexOf(':') > 0) {
var splits = name.split(':');
const splits = name.split(':');
name = splits[1];
type = splits[0];
}
yield this.render('sync', {
type: type,
name: name,
type,
name,
title: 'Sync ' + type + ' - ' + name,
});
};

View File

@@ -1,28 +1,26 @@
'use strict';
var config = require('../../../config');
var packageService = require('../../../services/package');
var userService = require('../../../services/user');
var common = require('../../../lib/common');
var he = require('he');
const config = require('../../../config');
const packageService = require('../../../services/package');
const userService = require('../../../services/user');
const common = require('../../../lib/common');
module.exports = function* showUser(next) {
var name = this.params.name;
var isAdmin = common.isAdmin(name);
var scopes = config.scopes || [];
var user;
var r = yield [packageService.listModulesByUser(name), userService.getAndSave(name)];
var packages = r[0];
var user = r[1];
const name = this.params.name;
const isAdmin = common.isAdmin(name);
const scopes = config.scopes || [];
const r = yield [ packageService.listModulesByUser(name), userService.getAndSave(name) ];
const packages = r[0];
let user = r[1];
if (!user && !packages.length) {
return yield next;
}
user = user || {};
var data = {
name: name,
email: user.email ? he.encode(user.email, { encodeEverything: true }) : user.email,
const data = {
name,
email: user.email,
json: user.json || {},
isNpmUser: user.isNpmUser,
};
@@ -30,7 +28,7 @@ module.exports = function* showUser(next) {
if (data.json.login) {
// custom user format
// convert to npm user format
var json = data.json;
const json = data.json;
data.json = {
_id: 'org.couchdb.user:' + user.name,
_rev: user.rev,
@@ -42,16 +40,16 @@ module.exports = function* showUser(next) {
avatar: json.avatar_url,
fullname: json.name || json.login,
homepage: json.html_url,
im: json.im_url
im: json.im_url,
};
}
yield this.render('profile', {
title: 'User - ' + name,
packages: packages,
packages,
user: data,
lastModified: user.gmt_modified,
isAdmin: isAdmin,
scopes: scopes
isAdmin,
scopes,
});
};

View File

@@ -1,12 +1,12 @@
'use strict';
var childProcess = require('child_process');
var path = require('path');
var util = require('util');
var cfork = require('cfork');
var config = require('./config');
var workerPath = path.join(__dirname, 'worker.js');
var syncPath = path.join(__dirname, 'sync');
const childProcess = require('child_process');
const path = require('path');
const util = require('util');
const cfork = require('cfork');
const config = require('./config');
const workerPath = path.join(__dirname, 'worker.js');
const syncPath = path.join(__dirname, 'sync');
console.log('Starting cnpmjs.org ...\ncluster: %s\nadmins: %j\nscopes: %j\nsourceNpmRegistry: %s\nsyncModel: %s',
config.enableCluster, config.admins, config.scopes, config.sourceNpmRegistry, config.syncModel);
@@ -27,14 +27,17 @@ function forkWorker() {
cfork({
exec: workerPath,
count: config.numCPUs,
}).on('fork', function (worker) {
})
.on('fork', worker => {
console.log('[%s] [worker:%d] new worker start', Date(), worker.process.pid);
}).on('disconnect', function (worker) {
})
.on('disconnect', worker => {
console.error('[%s] [master:%s] wroker:%s disconnect, suicide: %s, state: %s.',
Date(), process.pid, worker.process.pid, worker.suicide, worker.state);
}).on('exit', function (worker, code, signal) {
var exitCode = worker.process.exitCode;
var err = new Error(util.format('worker %s died (code: %s, signal: %s, suicide: %s, state: %s)',
})
.on('exit', (worker, code, signal) => {
const exitCode = worker.process.exitCode;
const err = new Error(util.format('worker %s died (code: %s, signal: %s, suicide: %s, state: %s)',
worker.process.pid, exitCode, signal, worker.suicide, worker.state));
err.name = 'WorkerDiedError';
console.error('[%s] [master:%s] wroker exit: %s', Date(), process.pid, err.stack);
@@ -42,9 +45,9 @@ function forkWorker() {
}
function forkSyncer() {
var syncer = childProcess.fork(syncPath);
syncer.on('exit', function (code, signal) {
var err = new Error(util.format('syncer %s died (code: %s, signal: %s, stdout: %s, stderr: %s)',
const syncer = childProcess.fork(syncPath);
syncer.on('exit', (code, signal) => {
const err = new Error(util.format('syncer %s died (code: %s, signal: %s, stdout: %s, stderr: %s)',
syncer.pid, code, signal, syncer.stdout, syncer.stderr));
err.name = 'SyncerWorkerDiedError';
console.error('[%s] [master:%s] syncer exit: %s: %s',

View File

@@ -1,30 +0,0 @@
version: '3'
services:
web:
build:
context: .
dockerfile: Dockerfile
image: cnpmjs.org
depends_on:
- mysql-db
volumes:
- cnpm-files-volume:/var/data/cnpm_data
ports:
- "7001:7001"
- "7002:7002"
mysql-db:
image: mysql:5.6
restart: always
environment :
MYSQL_USER: root
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
MYSQL_DATABASE: cnpmjs_test
MYSQL_ROOT_HOST: "%"
volumes:
- ./docs/db.sql:/docker-entrypoint-initdb.d/cnpm-init.sql
- cnpm-db-volume:/var/lib/mysql
volumes:
cnpm-files-volume:
cnpm-db-volume:

View File

@@ -3,97 +3,81 @@ CREATE TABLE IF NOT EXISTS `user` (
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(100) NOT NULL COMMENT 'user name',
`salt` varchar(100) NOT NULL COMMENT 'user salt',
`salt` varchar(100) NOT NULL,
`password_sha` varchar(100) NOT NULL COMMENT 'user password hash',
`ip` varchar(64) NOT NULL COMMENT 'user last request ip',
`roles` varchar(200) NOT NULL DEFAULT '[]' COMMENT 'user roles',
`rev` varchar(40) NOT NULL COMMENT 'user rev',
`email` varchar(400) NOT NULL COMMENT 'user email',
`json` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT 'json details',
`roles` varchar(200) NOT NULL DEFAULT '[]',
`rev` varchar(40) NOT NULL,
`email` varchar(400) NOT NULL,
`json` longtext CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT 'json details',
`npm_user` tinyint(1) DEFAULT '0' COMMENT 'user sync from npm or not, 1: true, other: false',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name` (`name`),
KEY `idx_gmt_modified` (`gmt_modified`)
UNIQUE KEY `name` (`name`),
KEY `gmt_modified` (`gmt_modified`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='user base info';
-- 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';
-- ALTER TABLE `user`
-- CHANGE `salt` `salt` varchar(100) NOT NULL COMMENT 'user salt',
-- CHANGE `roles` `roles` varchar(200) NOT NULL DEFAULT '[]' COMMENT 'user roles',
-- CHANGE `rev` `rev` varchar(40) NOT NULL COMMENT 'user rev',
-- CHANGE `email` `email` varchar(400) NOT NULL COMMENT 'user email';
CREATE TABLE IF NOT EXISTS `module_keyword` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`keyword` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'keyword',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`description` longtext COMMENT 'module description',
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`description` longtext,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_keyword_module_name` (`keyword`,`name`),
KEY `idx_name` (`name`)
UNIQUE KEY `keyword_module_name` (`keyword`,`name`),
KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module keyword';
-- ALTER TABLE `module_keyword`
-- CHANGE `name` `name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
-- CHANGE `description` `description` longtext COMMENT 'module description';
CREATE TABLE IF NOT EXISTS `module_star` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`user` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'user name',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_user_module_name` (`user`,`name`),
KEY `idx_name` (`name`)
UNIQUE KEY `user_module_name` (`user`,`name`),
KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module star';
-- ALTER TABLE `module_star`
-- CHANGE `name` `name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name';
CREATE TABLE IF NOT EXISTS `module_maintainer` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`user` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'user name',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_user_module_name` (`user`,`name`),
KEY `idx_name` (`name`)
UNIQUE KEY `user_module_name` (`user`,`name`),
KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='private module maintainers';
-- ALTER TABLE `module_maintainer`
-- CHANGE `name` `name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name';
CREATE TABLE IF NOT EXISTS `npm_module_maintainer` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`user` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'user name',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_user_module_name` (`user`,`name`),
KEY `idx_name` (`name`)
UNIQUE KEY `user_module_name` (`user`,`name`),
KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='npm original module maintainers';
-- ALTER TABLE `npm_module_maintainer`
-- CHANGE `name` `name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name';
CREATE TABLE IF NOT EXISTS `module` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`author` varchar(100) NOT NULL COMMENT 'module author',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`author` varchar(100) NOT NULL,
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`version` varchar(30) NOT NULL COMMENT 'module version',
`description` longtext COMMENT 'module description',
`description` longtext,
`package` longtext CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT 'package.json',
`dist_shasum` varchar(100) DEFAULT NULL COMMENT 'module dist SHASUM',
`dist_tarball` varchar(2048) DEFAULT NULL COMMENT 'module dist tarball',
`dist_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'module dist size',
`publish_time` bigint(20) unsigned COMMENT 'module publish time',
`dist_shasum` varchar(100) DEFAULT NULL,
`dist_tarball` varchar(2048) DEFAULT NULL,
`dist_size` int(10) unsigned NOT NULL DEFAULT '0',
`publish_time` bigint(20) unsigned,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name` (`name`,`version`),
KEY `idx_gmt_modified` (`gmt_modified`),
KEY `idx_publish_time` (`publish_time`),
KEY `idx_author` (`author`),
KEY `idx_name_gmt_modified` (`name`,`gmt_modified`)
UNIQUE KEY `name` (`name`,`version`),
KEY `gmt_modified` (`gmt_modified`),
KEY `publish_time` (`publish_time`),
KEY `author` (`author`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module info';
-- ALTER TABLE `module` ADD `description` longtext;
-- ALTER TABLE `module` ADD `publish_time` bigint(20) unsigned, ADD KEY `publish_time` (`publish_time`);
@@ -101,97 +85,48 @@ CREATE TABLE IF NOT EXISTS `module` (
-- ALTER TABLE `module` CHANGE `description` `description` LONGTEXT CHARACTER SET utf8 COLLATE utf8_general_ci;
-- show create table module\G
-- ALTER TABLE `module` CHANGE `name` `name` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name';
-- ALTER TABLE `module`
-- CHANGE `author` `author` varchar(100) NOT NULL COMMENT 'module author',
-- CHANGE `name` `name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
-- CHANGE `version` `version` varchar(30) NOT NULL COMMENT 'module version',
-- CHANGE `description` `description` longtext COMMENT 'module description',
-- CHANGE `package` `package` longtext CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT 'package.json',
-- CHANGE `dist_shasum` `dist_shasum` varchar(100) DEFAULT NULL COMMENT 'module dist SHASUM',
-- CHANGE `dist_tarball` `dist_tarball` varchar(2048) DEFAULT NULL COMMENT 'module dist tarball',
-- CHANGE `dist_size` `dist_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'module dist size',
-- CHANGE `publish_time` `publish_time` bigint(20) unsigned COMMENT 'module publish time';
CREATE TABLE IF NOT EXISTS `module_abbreviated` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`version` varchar(30) NOT NULL COMMENT 'module version',
`package` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT 'the abbreviated metadata',
`publish_time` bigint(20) unsigned COMMENT 'the publish time',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name` (`name`,`version`),
KEY `idx_gmt_modified` (`gmt_modified`),
KEY `idx_publish_time` (`publish_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module abbreviated info';
-- ALTER TABLE `module_abbreviated`
-- CHANGE `name` `name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
-- CHANGE `publish_time` `publish_time` bigint(20) unsigned COMMENT 'the publish time';
CREATE TABLE IF NOT EXISTS `package_readme` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`readme` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT 'the latest version readme',
`version` varchar(30) NOT NULL COMMENT 'module version',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name` (`name`),
KEY `idx_gmt_modified` (`gmt_modified`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='package latest readme';
-- ALTER TABLE `package_readme`
-- CHANGE `name` `name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name';
CREATE TABLE IF NOT EXISTS `module_log` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`username` varchar(100) NOT NULL COMMENT 'which username',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`log` longtext COMMENT 'the raw log',
`username` varchar(100) NOT NULL,
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`log` longtext,
PRIMARY KEY (`id`),
KEY `idx_name` (`name`)
KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module sync log';
-- ALTER TABLE `module_log` CHANGE `name` `name` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name';
-- ALTER TABLE `module_log`
-- CHANGE `username` `username` varchar(100) NOT NULL COMMENT 'which username',
-- CHANGE `name` `name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
-- CHANGE `log` `log` longtext COMMENT 'the raw log';
CREATE TABLE IF NOT EXISTS `tag` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`tag` varchar(30) NOT NULL COMMENT 'tag name',
`version` varchar(30) NOT NULL COMMENT 'module version',
`module_id` bigint(20) unsigned NOT NULL COMMENT 'module id',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name` (`name`, `tag`),
KEY `idx_gmt_modified` (`gmt_modified`)
UNIQUE KEY `name` (`name`, `tag`),
KEY `gmt_modified` (`gmt_modified`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module tag';
-- ALTER TABLE `tag` ADD `module_id` BIGINT( 20 ) UNSIGNED NOT NULL;
-- ALTER TABLE `tag` CHANGE `name` `name` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name';
-- ALTER TABLE `tag` ADD KEY `gmt_modified` (`gmt_modified`);
-- ALTER TABLE `tag`
-- CHANGE `name` `name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name';
CREATE TABLE IF NOT EXISTS `module_unpublished` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`package` longtext CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT 'base info: tags, time, maintainers, description, versions',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name` (`name`),
KEY `idx_gmt_modified` (`gmt_modified`)
UNIQUE KEY `name` (`name`),
KEY `gmt_modified` (`gmt_modified`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module unpublished info';
-- ALTER TABLE `module_unpublished`
-- CHANGE `name` `name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name';
CREATE TABLE IF NOT EXISTS `total` (
`name` varchar(214) NOT NULL COMMENT 'total name',
`name` varchar(100) NOT NULL COMMENT 'total name',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`module_delete` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'module delete count',
`last_sync_time` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'last timestamp sync from official registry',
@@ -215,7 +150,6 @@ INSERT INTO total(name, gmt_modified) VALUES('total', now())
-- ALTER TABLE `total` ADD `fail_sync_num` int unsigned NOT NULL DEFAULT '0' COMMENT 'how many packages sync fail at this time'
-- ALTER TABLE `total` ADD `left_sync_num` int unsigned NOT NULL DEFAULT '0' COMMENT 'how many packages left to be sync'
-- ALTER TABLE `total` ADD `last_sync_module` varchar(100) NOT NULL COMMENT 'last sync success module name';
-- ALTER TABLE `total` CHANGE `name` `name` varchar(214) NOT NULL COMMENT 'total name';
-- CREATE TABLE IF NOT EXISTS `download_total` (
-- `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
@@ -234,7 +168,7 @@ CREATE TABLE IF NOT EXISTS `downloads` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`date` int unsigned NOT NULL COMMENT 'YYYYMM format',
`d01` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '01 download count',
`d02` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '02 download count',
@@ -268,68 +202,43 @@ CREATE TABLE IF NOT EXISTS `downloads` (
`d30` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '30 download count',
`d31` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '31 download count',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name_date` (`name`, `date`),
KEY `idx_date` (`date`)
UNIQUE KEY `name_date` (`name`, `date`),
KEY `date` (`date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module download total info';
-- ALTER TABLE `downloads`
-- CHANGE `name` `name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name';
CREATE TABLE IF NOT EXISTS `module_deps` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`deps` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'which module depend on this module',
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`deps` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'which module depend on this module',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name_deps` (`name`,`deps`),
KEY `idx_name` (`name`)
UNIQUE KEY `name_deps` (`name`,`deps`),
KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module deps';
-- ALTER TABLE `module_deps`
-- CHANGE `name` `name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
-- CHANGE `deps` `deps` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'which module depend on this module';
CREATE TABLE IF NOT EXISTS `dist_dir` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(214) NOT NULL COMMENT 'dir name',
`parent` varchar(214) NOT NULL COMMENT 'parent dir' DEFAULT '/',
`name` varchar(200) NOT NULL COMMENT 'dir name',
`parent` varchar(200) NOT NULL COMMENT 'parent dir' DEFAULT '/',
`date` varchar(20) COMMENT '02-May-2014 01:06',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name` (`parent`, `name`),
KEY `idx_gmt_modified` (`gmt_modified`)
UNIQUE KEY `name` (`parent`, `name`),
KEY `gmt_modified` (`gmt_modified`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='dist dir info';
-- ALTER TABLE `dist_dir`
-- CHANGE `name` `name` varchar(214) NOT NULL COMMENT 'dir name',
-- CHANGE `parent` `parent` varchar(214) NOT NULL COMMENT 'parent dir' DEFAULT '/';
CREATE TABLE IF NOT EXISTS `dist_file` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(214) NOT NULL COMMENT 'file name',
`parent` varchar(214) NOT NULL COMMENT 'parent dir' DEFAULT '/',
`name` varchar(100) NOT NULL COMMENT 'file name',
`parent` varchar(200) NOT NULL COMMENT 'parent dir' DEFAULT '/',
`date` varchar(20) COMMENT '02-May-2014 01:06',
`size` int(10) unsigned NOT NULL COMMENT 'file size' DEFAULT '0',
`sha1` varchar(40) COMMENT 'sha1 hex value',
`url` varchar(2048),
PRIMARY KEY (`id`),
UNIQUE KEY `uk_fullname` (`parent`, `name`),
KEY `idx_gmt_modified` (`gmt_modified`)
UNIQUE KEY `fullname` (`parent`, `name`),
KEY `gmt_modified` (`gmt_modified`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='dist file info';
-- ALTER TABLE `dist_file`
-- CHANGE `name` `name` varchar(214) NOT NULL COMMENT 'file name',
-- CHANGE `parent` `parent` varchar(214) NOT NULL COMMENT 'parent dir' DEFAULT '/';
CREATE TABLE IF NOT EXISTS `token` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`token` varchar(100) NOT NULL COMMENT 'token',
`user_id` varchar(100) NOT NULL COMMENT 'user name',
`readonly` tinyint NOT NULL DEFAULT 0 COMMENT 'readonly or not, 1: true, other: false',
`token_key` varchar(200) NOT NULL COMMENT 'token sha512 hash',
`cidr_whitelist` varchar(500) NOT NULL COMMENT 'ip list, ["127.0.0.1"]',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_token` (`token`),
KEY `idx_user` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='token info';

View File

@@ -1,261 +0,0 @@
'use strict';
var mkdirp = require('mkdirp');
var copy = require('copy-to');
var path = require('path');
var fs = require('fs');
var os = require('os');
var version = require('../package.json').version;
var root = path.dirname(__dirname);
var dataDir = process.env.CNPM_DATA_DIR ;
var config = {
version: version,
dataDir: dataDir,
/**
* Cluster mode
*/
enableCluster: false,
numCPUs: os.cpus().length,
/*
* server configure
*/
registryPort: 7001,
webPort: 7002,
bindingHost: '0.0.0.0', // binding on 0.0.0.0 for outside of container access
// debug mode
// if in debug mode, some middleware like limit wont load
// logger module will print to stdout
debug: process.env.NODE_ENV === 'development',
// page mode, enable on development env
pagemock: process.env.NODE_ENV === 'development',
// session secret
sessionSecret: 'cnpmjs.org test session secret',
// max request json body size
jsonLimit: '10mb',
// log dir name
logdir: path.join(dataDir, 'logs'),
// update file template dir
uploadDir: path.join(dataDir, 'downloads'),
// web page viewCache
viewCache: false,
// config for koa-limit middleware
// for limit download rates
limit: {
enable: false,
token: 'koa-limit:download',
limit: 1000,
interval: 1000 * 60 * 60 * 24,
whiteList: [],
blackList: [],
message: 'request frequency limited, any question, please contact fengmk2@gmail.com',
},
enableCompress: false, // enable gzip response or not
// default system admins
admins: {
// name: email
fengmk2: 'fengmk2@gmail.com',
admin: 'admin@cnpmjs.org',
dead_horse: 'dead_horse@qq.com',
},
// email notification for errors
// check https://github.com/andris9/Nodemailer for more informations
mail: {
enable: false,
appname: 'cnpmjs.org',
from: 'cnpmjs.org mail sender <adderss@gmail.com>',
service: 'gmail',
auth: {
user: 'address@gmail.com',
pass: 'your password'
}
},
logoURL: 'https://os.alipayobjects.com/rmsportal/oygxuIUkkrRccUz.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
npmClientName: 'cnpm', // use `${name} install package`
packagePageContributorSearch: true, // package page contributor link to search, default is true
// max handle number of package.json `dependencies` property
maxDependencies: 200,
// backup filepath prefix
backupFilePrefix: '/cnpm/backup/',
/**
* database config
*/
database: {
db: 'cnpmjs_test',
username: 'root',
password: '',
// the sql dialect of the database
// - currently supported: 'mysql', 'sqlite', 'postgres', 'mariadb'
dialect: 'mysql',
// the Docker container network hostname defined at docker-compose.yml
host: 'mysql-db',
// custom port; default: 3306
port: 3306,
// use pooling in order to reduce db connection overload and to increase speed
// currently only for mysql and postgresql (since v1.5.0)
pool: {
maxConnections: 10,
minConnections: 0,
maxIdleTime: 30000
},
// the storage engine for 'sqlite'
// default store into ~/.cnpmjs.org/data.sqlite
//storage: path.join(dataDir, 'data.sqlite'),
logging: !!process.env.SQL_DEBUG,
},
// package tarball store in local filesystem by default
nfs: require('fs-cnpm')({
dir: path.join(dataDir, 'nfs')
}),
// if set true, will 302 redirect to `nfs.url(dist.key)`
downloadRedirectToNFS: false,
// registry url name
registryHost: '127.0.0.1:7001',
/**
* registry mode config
*/
// enable private mode or not
// private mode: only admins can publish, other users just can sync package from source npm
// public mode: all users can publish
enablePrivate: false,
// registry scopes, if don't set, means do not support scopes
scopes: [ '@cnpm', '@cnpmtest', '@cnpm-test' ],
// some registry already have some private packages in global scope
// but we want to treat them as scoped private packages,
// so you can use this white list.
privatePackages: [],
/**
* sync configs
*/
// the official npm registry
// 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',
// 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',
// upstream registry is base on cnpm/cnpmjs.org or not
// if your upstream is official npm registry, please turn it off
sourceNpmRegistryIsCNpm: true,
// if install return 404, try to sync from source registry
syncByInstall: true,
// sync mode select
// none: do not sync any module, proxy all public modules from sourceNpmRegistry
// exist: only sync exist modules
// all: sync all modules
syncModel: 'none', // 'none', 'all', 'exist'
syncConcurrency: 1,
// sync interval, default is 10 minutes
syncInterval: '10m',
// sync polular modules, default to false
// because cnpm can't auto sync tag change for now
// so we want to sync popular modules to ensure their tags
syncPopular: false,
syncPopularInterval: '1h',
// top 100
topPopular: 100,
// 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
// Force npm to always require authentication when accessing the registry, even for GET requests.
alwaysAuth: false,
// 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',
// https://github.com/cnpm/cnpmjs.org/issues/1149
// if enable this option, must create module_abbreviated and package_readme table in database
enableAbbreviatedMetadata: true,
// global hook function: function* (envelope) {}
// envelope format please see https://github.com/npm/registry/blob/master/docs/hooks/hooks-payload.md#payload
globalHook: null,
};
if (process.env.NODE_ENV !== 'test') {
var customConfig;
if (process.env.NODE_ENV === 'development') {
customConfig = path.join(root, 'config', 'config.js');
} else {
// 1. try to load `$dataDir/config.json` first, not exists then goto 2.
// 2. load config/config.js, everything in config.js will cover the same key in index.js
customConfig = path.join(dataDir, 'config.json');
if (!fs.existsSync(customConfig)) {
customConfig = path.join(root, 'config', 'config.js');
}
}
if (fs.existsSync(customConfig)) {
copy(require(customConfig)).override(config);
}
}
mkdirp.sync(config.logdir);
mkdirp.sync(config.uploadDir);
module.exports = config;
config.loadConfig = function (customConfig) {
if (!customConfig) {
return;
}
copy(customConfig).override(config);
};

View File

@@ -50,7 +50,7 @@ Status: 4xx
## Authentication
There are two ways to authenticate through the API.
There is only one way to authenticate through the API.
## Basic Authentication
@@ -58,12 +58,6 @@ There are two ways to authenticate through the API.
$ curl -u "username:password" https://registry.npmjs.org
```
## Bearer Authentication
```bash
$ curl -H "Authorization: Bearer ${UUId}" https://registry.npmjs.org
```
## Failed login limit
```bash
@@ -909,8 +903,7 @@ Status: 201 Created
{
"ok": true,
"id": "org.couchdb.user:fengmk2",
"rev": "32-984ee97e01aea166dcab6d1517c730e3",
"token": "85d32fad-bd43-4dd7-9451-4f7d907313a2"
"rev": "32-984ee97e01aea166dcab6d1517c730e3"
}
```
@@ -963,76 +956,3 @@ Status: 201 Created
```
## Search
## Token
- [Create token](/docs/registry-api.md#create-token)
- [List token](/docs/registry-api.md#list-token)
- [Delete token](/docs/registry-api.md#delete-token)
### Create token
* Authentication required.
```
POST /-/npm/v1/tokens
```
#### Input
```json
{
"password": "123",
"readonly": false,
"cidr_whitelist": [
"127.0.0.1"
]
}
```
#### Response 200
```json
HTTP/1.1 200 OK
{
"token": "85d32fad-bd43-4dd7-9451-4f7d907313a2",
"key": "d06309a210570ef71cd9c7bd4849e7e96eeaa841976e63326436f6fd320dc4bbd452710e4e0fedc2efc2ea4a793b7159e95e9596e85e00dee26adc3f8afbb97f",
"cidr_whitelist": [ "127.0.0.1" ],
"created": "2015-01-04T08:28:51.378Z",
"updated": "2015-01-04T08:28:51.378Z",
"readonly": false
}
```
### List token
* Authentication required.
```
GET /-/npm/v1/tokens
```
### Input
perPage=10&page=0
#### Response 200
```json
{
"objects": [{
"token": "85d32f...7313a2",
"key": "d06309a210570ef71cd9c7bd4849e7e96eeaa841976e63326436f6fd320dc4bbd452710e4e0fedc2efc2ea4a793b7159e95e9596e85e00dee26adc3f8afbb97f",
"cidr_whitelist": [ "127.0.0.1" ],
"created": "2015-01-04T08:28:51.378Z",
"updated": "2015-01-04T08:28:51.378Z",
"readonly": false
}]
}
```
### Delete token
* Authentication required.
```
GET /-/npm/v1/tokens/token/:UUID
```
#### Response 204

View File

@@ -8,7 +8,6 @@ So `cnpm` is meaning: **Company npm**.
- [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)。
- Use the private npm service provided by Alibaba Cloud DevOps which build with cnpm. [https://packages.aliyun.com/](https://packages.aliyun.com/?channel=pd_cnpm_github)
<div class="ant-table">
<table class="downloads">
@@ -152,5 +151,5 @@ Release [History](/history).
## Sponsors
- [![阿里云](https://static.aliyun.com/images/www-summerwind/logo.gif)](http://click.aliyun.com/m/4288/) [![阿里云云效](https://img.alicdn.com/tfs/TB116yt3fb2gK0jSZK9XXaEgFXa-106-20.png)](https://devops.aliyun.com/?channel=pd_cnpm_github) (2016.2 - now)
- [![阿里云](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,28 +1,16 @@
/**!
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/
'use strict';
/**
* Module dependencies.
*/
var config = require('./config');
const config = require('./config');
exports.loadConfig = config.loadConfig;
exports.config = config;
exports.startWorker = function (customConfig) {
exports.startWorker = function(customConfig) {
config.loadConfig(customConfig);
require('./worker');
};
exports.startSync = function (customConfig) {
exports.startSync = function(customConfig) {
config.loadConfig(customConfig);
require('./sync');
};

View File

@@ -1,23 +1,18 @@
'use strict';
var crypto = require('crypto');
var path = require('path');
var utility = require('utility');
var util = require('util');
var config = require('../config');
var BASIC_PREFIX = /basic /i;
var BEARER_PREFIX = /bearer /i;
const crypto = require('crypto');
const path = require('path');
const config = require('../config');
const util = require('util');
exports.getTarballFilepath = function (packageName, packageVersion, filename) {
exports.getTarballFilepath = function(filename) {
// ensure download file path unique
// TODO: not only .tgz, and also other extname
var name = filename.replace(/\.tgz$/, '.' + crypto.randomBytes(16).toString('hex'));
// use filename string md5 instead, fix "ENAMETOOLONG: name too long" error
name = packageName.replace(/\//g, '-').replace(/\@/g, '') + '-' + packageVersion.substring(0, 20) + '.' + utility.md5(name) + '.tgz';
const name = filename.replace(/\.tgz$/, '.' + crypto.randomBytes(16).toString('hex') + '.tgz');
return path.join(config.uploadDir, name);
};
exports.getCDNKey = function (name, 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] !== '@') {
@@ -26,41 +21,36 @@ exports.getCDNKey = function (name, filename) {
return '/' + name + '/-/' + filename;
};
exports.setDownloadURL = function (pkg, ctx, host) {
exports.setDownloadURL = function(pkg, ctx, host) {
if (pkg.dist) {
host = host || config.registryHost || ctx.host;
var protocol = config.protocol || ctx.protocol;
pkg.dist.tarball = util.format('%s://%s/%s/download/%s-%s.tgz',
protocol,
ctx.protocol,
host, pkg.name, pkg.name, pkg.version);
if (ctx.querystring) {
var backupUrl = pkg.dist.tarball;
pkg.dist.tarball += '?' + ctx.querystring + '&other_urls=' + encodeURIComponent(backupUrl);
}
}
};
exports.isAdmin = function (username) {
exports.isAdmin = function(username) {
return typeof config.admins[username] === 'string';
};
exports.isMaintainer = function (user, maintainers) {
exports.isMaintainer = function(user, maintainers) {
if (user.isAdmin) {
return true;
}
var username = user.name;
const username = user.name;
maintainers = maintainers || [];
var match = maintainers.filter(function (item) {
const match = maintainers.filter(function(item) {
return item.name === username;
});
return match.length > 0;
};
exports.isLocalModule = function (mods) {
for (var i = 0; i < mods.length; i++) {
var r = mods[i];
exports.isLocalModule = function(mods) {
for (let i = 0; i < mods.length; i++) {
const r = mods[i];
if (r.package && r.package._publish_on_cnpm) {
return true;
}
@@ -68,27 +58,9 @@ exports.isLocalModule = function (mods) {
return false;
};
exports.isPrivateScopedPackage = function (name) {
if (!name) {
return false;
}
exports.isPrivateScopedPackage = function(name) {
if (name[0] !== '@') {
return false;
}
return config.scopes.indexOf(name.split('/')[0]) >= 0;
};
var AuthorizeType = exports.AuthorizeType = {
BASIC: 'BASIC',
BEARER: 'BEARER',
};
exports.getAuthorizeType = function (ctx) {
var authorization = (ctx.get('authorization') || '').trim();
if (BASIC_PREFIX.test(authorization)) {
return AuthorizeType.BASIC;
} else if (BEARER_PREFIX.test(authorization)) {
return AuthorizeType.BEARER;
}
};

View File

@@ -1,37 +1,37 @@
'use strict';
var debug = require('debug')('cnpmjs.org:middleware:auth');
var UserService = require('../services/user');
var TokenService = require('../services/token');
var config = require('../config');
var common = require('../lib/common');
const debug = require('debug')('cnpmjs.org:middleware:auth');
const UserService = require('../services/user');
const config = require('../config');
/**
* Parse the request authorization
* get the real user
*/
module.exports = function () {
module.exports = function() {
return function* auth(next) {
this.user = {};
var authorization = (this.get('authorization') || '').trim();
let authorization = (this.get('authorization') || '').split(' ')[1] || '';
authorization = authorization.trim();
debug('%s %s with %j', this.method, this.url, authorization);
if (!authorization) {
return yield unauthorized.call(this, next);
}
var row;
try {
var authorizeType = common.getAuthorizeType(this);
authorization = new Buffer(authorization, 'base64').toString();
const pos = authorization.indexOf(':');
if (pos === -1) {
return yield unauthorized.call(this, next);
}
if (authorizeType === common.AuthorizeType.BASIC) {
row = yield basicAuth(authorization);
} else if (authorizeType === common.AuthorizeType.BEARER) {
row = yield bearerAuth(authorization, this.method, this.ip);
} else {
return yield unauthorized.call(this, next);
}
const username = authorization.slice(0, pos);
const password = authorization.slice(pos + 1);
let row;
try {
row = yield UserService.auth(username, password);
} catch (err) {
// do not response error here
// many request do not need login
@@ -51,41 +51,16 @@ module.exports = function () {
};
};
function* basicAuth(authorization) {
authorization = authorization.split(' ')[1];
authorization = Buffer.from(authorization, 'base64').toString();
var pos = authorization.indexOf(':');
if (pos === -1) {
return null;
}
var username = authorization.slice(0, pos);
var password = authorization.slice(pos + 1);
return yield UserService.auth(username, password);
}
function* bearerAuth(authorization, method, ip) {
var token = authorization.split(' ')[1];
var isReadOperation = method === 'HEAD' || method === 'GET';
return yield TokenService.validateToken(token, {
isReadOperation: isReadOperation,
accessIp: ip,
});
}
function* unauthorized(next) {
if (!config.alwaysAuth || this.method !== 'GET') {
return yield next;
}
this.status = 401;
this.set('WWW-Authenticate', 'Basic realm="sample"');
if (this.accepts(['html', 'json']) === 'json') {
const error = '[unauthorized] login first';
if (this.accepts([ 'html', 'json' ]) === 'json') {
this.body = {
error,
reason: error,
error: 'unauthorized',
reason: 'login first',
};
} else {
this.body = 'login first';

View File

@@ -1,13 +1,14 @@
'use strict';
module.exports = function () {
module.exports = () => {
return function* block(next) {
var ua = String(this.get('user-agent')).toLowerCase();
const ua = String(this.get('user-agent')).toLowerCase();
if (ua === 'ruby') {
this.status = 403;
return this.body = {
message: 'forbidden Ruby user-agent, ip: ' + this.ip
this.body = {
message: 'forbidden Ruby user-agent, ip: ' + this.ip,
};
return;
}
yield next;
};

View File

@@ -1,29 +1,28 @@
'use strict';
var packageService = require('../services/package');
const packageService = require('../services/package');
// admin or module's maintainer can modified the module
module.exports = function* editable(next) {
var username = this.user && this.user.name;
var moduleName = this.params.name || this.params[0];
const username = this.user && this.user.name;
const moduleName = this.params.name || this.params[0];
if (username && moduleName) {
if (this.user.isAdmin) {
return yield next;
}
var isMaintainer = yield packageService.isMaintainer(moduleName, username);
const isMaintainer = yield packageService.isMaintainer(moduleName, username);
if (isMaintainer) {
return yield next;
}
}
this.status = 403;
var message = 'not authorized to modify ' + moduleName;
let message = 'not authorized to modify ' + moduleName;
if (username) {
message = username + ' ' + message;
}
message = '[forbidden] ' + message;
this.body = {
error: message,
error: 'forbidden user',
reason: message,
};
};

View File

@@ -1,17 +1,16 @@
'use strict';
var packageService = require('../services/package');
const packageService = require('../services/package');
module.exports = function* (next) {
var name = this.params.name || this.params[0];
var pkg = yield packageService.getLatestModule(name);
const name = this.params.name || this.params[0];
const pkg = yield packageService.getLatestModule(name);
if (pkg) {
return yield next;
}
this.status = 404;
const error = '[not_found] document not found';
this.body = {
error,
reason: error,
error: 'not_found',
reason: 'document not found',
};
};

View File

@@ -1,9 +1,9 @@
'use strict';
var config = require('../config');
var limit = require('koa-limit');
const config = require('../config');
const limit = require('koa-limit');
var limitConfig = config.limit;
const limitConfig = config.limit;
if (!limitConfig.enable) {
module.exports = function* ignoreLimit(next) {

View File

@@ -1,33 +1,26 @@
'use strict';
var http = require('http');
module.exports = function *login(next) {
if (this.path === '/-/ping' && this.query.write !== 'true') {
yield next;
return;
}
const http = require('http');
module.exports = function* login(next) {
if (this.user.error) {
var status = this.user.error.status;
const status = this.user.error.status;
this.status = http.STATUS_CODES[status]
? status
: 500;
const error = `[${this.user.error.name}] ${this.user.error.message}`;
this.body = {
error,
reason: error,
error: this.user.error.name,
reason: this.user.error.message,
};
return;
}
if (!this.user.name) {
this.status = 401;
const error = '[unauthorized] Login first';
this.body = {
error,
reason: error,
error: 'unauthorized',
reason: 'Login first',
};
return;
}

View File

@@ -1,20 +1,19 @@
'use strict';
var template = '<?xml version="1.0" encoding="UTF-8"?>\
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">\
<ShortName>CNPM</ShortName>\
<Description>Search packages in CNPM.</Description>\
<Tags>CNPM</Tags>\
<Url method="get" type="text/html" template="http://${host}/browse/keyword/{searchTerms}"/>\
</OpenSearchDescription>';
var config = require('../config');
const template = `<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>CNPM</ShortName>
<Description>Search packages in CNPM.</Description>
<Tags>CNPM</Tags>
<Url method="get" type="text/html" template="http://{{host}}/browse/keyword/{searchTerms}"/>
</OpenSearchDescription>`;
module.exports = function* opensearch(next) {
if (this.path === '/opensearch.xml') {
this.type = 'text/xml';
this.charset = 'utf-8';
this.body = template.replace('${host}', config.opensearch.host || this.host);
this.body = template.replace('{{host}}', this.host);
return;
}
yield next;
};

View File

@@ -1,84 +1,46 @@
'use strict';
var debug = require('debug')('cnpmjs.org:middleware:proxy_to_npm');
var config = require('../config');
const debug = require('debug')('cnpmjs.org:middleware:proxy_to_npm');
const config = require('../config');
module.exports = function (options) {
var redirectUrl = config.sourceNpmRegistry;
var proxyUrls = [
module.exports = function(options) {
let redirectUrl = config.sourceNpmRegistry;
let proxyUrls = [
// /:pkg, dont contains scoped package
// /:pkg/:versionOrTag
/^\/[\w\-\.]+(?:\/[\w\-\.]+)?$/,
/^\/[\w\-\.]+$/,
// /-/package/:pkg/dist-tags
/^\/\-\/package\/[\w\-\.]+\/dist-tags/,
];
var scopedUrls = [
// scoped package
/^\/(@[\w\-\.]+)\/[\w\-\.]+(?:\/[\w\-\.]+)?$/,
/^\/\-\/package\/(@[\w\-\.]+)\/[\w\-\.]+\/dist\-tags/,
];
if (options && options.isWeb) {
redirectUrl = config.sourceNpmWeb || redirectUrl.replace('//registry.', '//');
redirectUrl = redirectUrl.replace('//registry.', '//');
proxyUrls = [
// /package/:pkg
/^\/package\/[\w\-\.]+/,
];
scopedUrls = [
// scoped package
/^\/package\/(@[\w\-\.]+)\/[\w\-\.]+/,
/^\/package\/[\w\-\.]+$/,
];
}
return function* proxyToNpm(next) {
if (config.syncModel !== 'none') {
return yield next;
}
// syncModel === none
// only proxy read requests
if (this.method !== 'GET' && this.method !== 'HEAD') {
return yield next;
}
var pathname = decodeURIComponent(this.path);
var isScoped = false;
var isPublichScoped = false;
// check scoped name
if (config.scopes && config.scopes.length > 0) {
for (var i = 0; i < scopedUrls.length; i++) {
const m = scopedUrls[i].exec(pathname);
if (m) {
isScoped = true;
if (config.scopes.indexOf(m[1]) !== -1) {
// internal scoped
isPublichScoped = false;
} else {
isPublichScoped = true;
}
break;
}
const pathname = this.path;
let match;
for (let i = 0; i < proxyUrls.length; i++) {
match = proxyUrls[i].test(pathname);
if (match) {
break;
}
}
var isPublich = false;
if (!isScoped) {
for (var i = 0; i < proxyUrls.length; i++) {
isPublich = proxyUrls[i].test(pathname);
if (isPublich) {
break;
}
}
if (!match) {
return yield next;
}
if (isPublich || isPublichScoped) {
var url = redirectUrl + this.url;
debug('proxy isPublich: %s, isPublichScoped: %s, package to %s',
isPublich, isPublichScoped, url);
this.redirect(url);
return;
}
yield next;
const url = redirectUrl + this.url;
debug('proxy to %s', url);
this.redirect(url);
};
};

View File

@@ -1,16 +1,10 @@
'use strict';
/**
* Module dependencies.
*/
const util = require('util');
const config = require('../config');
const debug = require('debug')('cnpmjs.org:middlewares/publishable');
var packageService = require('../services/package');
var util = require('util');
var config = require('../config');
var debug = require('debug')('cnpmjs.org:middlewares/publishable');
module.exports = function *publishable(next) {
module.exports = function* publishable(next) {
// admins always can publish and unpublish
if (this.user.isAdmin) {
return yield next;
@@ -19,10 +13,9 @@ module.exports = function *publishable(next) {
// private mode, normal user can't publish and unpublish
if (config.enablePrivate) {
this.status = 403;
const error = '[no_perms] Private mode enable, only admin can publish this module';
this.body = {
error,
reason: error,
error: 'no_perms',
reason: 'Private mode enable, only admin can publish this module',
};
return;
}
@@ -30,29 +23,13 @@ module.exports = function *publishable(next) {
// public mode, normal 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];
const 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;
}
// check the package is already exists and is maintainer
var result = yield packageService.authMaintainer(name, this.user.name);
if (result.maintainers && result.maintainers.length) {
if (result.isMaintainer) {
return yield next;
}
this.status = 403;
const error = '[forbidden] ' + this.user.name + ' not authorized to modify ' + name +
', please contact maintainers: ' + result.maintainers.join(', ');
this.body = {
error,
reason: error,
};
return;
}
// scoped module
if (name[0] === '@') {
if (checkScope(name, this)) {
@@ -75,14 +52,13 @@ function checkScope(name, ctx) {
return false;
}
var scope = name.split('/')[0];
const scope = name.split('/')[0];
if (ctx.user.scopes.indexOf(scope) === -1) {
debug('assert scope %s error', name);
ctx.status = 400;
const error = util.format('[invalid] scope %s not match legal scopes: %s', scope, ctx.user.scopes.join(', '));
ctx.body = {
error,
reason: error,
error: 'invalid scope',
reason: util.format('scope %s not match legal scopes: %s', scope, ctx.user.scopes.join(', ')),
};
return false;
}
@@ -97,17 +73,15 @@ function checkScope(name, ctx) {
function assertNoneScope(name, ctx) {
ctx.status = 403;
if (ctx.user.scopes.length === 0) {
const error = '[no_perms] can\'t publish non-scoped package, please set `config.scopes`';
ctx.body = {
error,
reason: error,
error: 'no_perms',
reason: 'can\'t publish non-scoped package, please set `config.scopes`',
};
return;
}
const error = '[no_perms] only allow publish with ' + ctx.user.scopes.join(', ') + ' scope(s)';
ctx.body = {
error,
reason: error,
error: 'no_perms',
reason: 'only allow publish with ' + ctx.user.scopes.join(', ') + ' scope(s)',
};
}

View File

@@ -1,5 +1,19 @@
/**!
* cnpmjs.org - middleware/registry_not_found.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/
'use strict';
/**
* Module dependencies.
*/
module.exports = function* notFound(next) {
yield next;
@@ -11,9 +25,8 @@ module.exports = function* notFound(next) {
}
this.status = 404;
const error = '[not_found] document not found';
this.body = {
error,
reason: error,
error: 'not_found',
reason: 'document not found',
};
};

View File

@@ -1,30 +1,16 @@
/**!
* cnpmjs.org - middleware/static.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
/**
* Module dependencies.
*/
const path = require('path');
const middlewares = require('koa-middlewares');
const config = require('../config');
var path = require('path');
var middlewares = require('koa-middlewares');
var config = require('../config');
var staticDir = path.join(path.dirname(__dirname), 'public');
const staticDir = path.join(path.dirname(__dirname), 'public');
module.exports = middlewares.staticCache(staticDir, {
buffer: config.debug ? false : true,
buffer: !config.debug,
maxAge: config.debug ? 0 : 60 * 60 * 24 * 7,
alias: {
'/favicon.ico': '/favicon.png'
'/favicon.ico': '/favicon.png',
},
gzip: config.enableCompress,
});

View File

@@ -1,18 +1,6 @@
/**
* Copyright(c) cnpm and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/
'use strict';
/**
* Module dependencies.
*/
var config = require('../config');
const config = require('../config');
/**
* {Boolean} this.allowSync - allow sync triggle by cnpm install
@@ -25,7 +13,7 @@ module.exports = function* syncByInstall(next) {
return yield next;
}
// request not by node, consider it request from web, don't sync
var ua = this.get('user-agent');
const ua = this.get('user-agent');
if (!ua || ua.indexOf('node') < 0) {
return yield next;
}
@@ -35,11 +23,11 @@ module.exports = function* syncByInstall(next) {
return yield next;
}
var name = this.params.name || this.params[0];
const name = this.params.name || this.params[0];
// private scoped package don't sync
if (name && name[0] === '@') {
var scope = name.split('/')[0];
const scope = name.split('/')[0];
if (config.scopes.indexOf(scope) >= 0) {
return yield next;
}

View File

@@ -1,13 +1,12 @@
'use strict';
module.exports = function *unpublishable(next) {
module.exports = function* unpublishable(next) {
// only admin user can unpublish
if (!this.user.isAdmin) {
this.status = 403;
const error = '[no_perms] Only administrators can unpublish module';
this.body = {
error,
reason: error,
error: 'no_perms',
reason: 'Only administrators can unpublish module',
};
return;
}

View File

@@ -1,6 +1,6 @@
'use strict';
var debug = require('debug')('cnpmjs.org:middleware:web_not_found');
const debug = require('debug')('cnpmjs.org:middleware:web_not_found');
module.exports = function* notFound(next) {
yield next;
@@ -12,7 +12,7 @@ module.exports = function* notFound(next) {
return;
}
var m = /^\/([\w\-\.]+)\/?$/.exec(this.path);
let m = /^\/([\w\-\.]+)\/?$/.exec(this.path);
if (!m) {
// scoped packages
m = /^\/(@[\w\-\.]+\/[\w\-\.]+)$/.exec(this.path);
@@ -24,8 +24,8 @@ module.exports = function* notFound(next) {
// package not found
m = /\/package\/([\w\-\_\.]+)\/?$/.exec(this.url);
var name = null;
var title = '404: Page Not Found';
let name = null;
let title = '404: Page Not Found';
if (m) {
name = m[1];
title = name + ' Not Found';
@@ -33,7 +33,7 @@ module.exports = function* notFound(next) {
this.status = 404;
yield this.render('404', {
title: title,
name: name
title,
name,
});
};

View File

@@ -6,13 +6,13 @@
*/
exports.listModuleNamesByUser = function* (user) {
var rows = yield this.findAll({
attributrs: ['name'],
const rows = yield this.findAll({
attributrs: [ 'name' ],
where: {
user: user
}
user,
},
});
return rows.map(function (row) {
return rows.map(function(row) {
return row.name;
});
};
@@ -23,13 +23,13 @@ exports.listModuleNamesByUser = function* (user) {
*/
exports.listMaintainers = function* (name) {
var rows = yield this.findAll({
attributrs: ['user'],
const rows = yield this.findAll({
attributrs: [ 'user' ],
where: {
name: name
}
name,
},
});
return rows.map(function (row) {
return rows.map(function(row) {
return row.user;
});
};
@@ -41,16 +41,16 @@ exports.listMaintainers = function* (name) {
*/
exports.addMaintainer = function* (name, user) {
var row = yield this.find({
let row = yield this.find({
where: {
user: user,
name: name
}
user,
name,
},
});
if (!row) {
row = yield this.build({
user: user,
name: name
user,
name,
}).save();
}
return row;
@@ -63,7 +63,7 @@ exports.addMaintainer = function* (name, user) {
*/
exports.addMaintainers = function* (name, users) {
return yield users.map(function (user) {
return yield users.map(function(user) {
return this.addMaintainer(name, user);
}.bind(this));
};
@@ -77,16 +77,16 @@ exports.addMaintainers = function* (name, users) {
exports.removeMaintainers = function* (name, users) {
// removeMaintainers(name, oneUserName)
if (typeof users === 'string') {
users = [users];
users = [ users ];
}
if (users.length === 0) {
return;
}
yield this.destroy({
where: {
name: name,
name,
user: users,
}
},
});
};
@@ -98,8 +98,8 @@ exports.removeMaintainers = function* (name, users) {
exports.removeAllMaintainers = function* (name) {
yield this.destroy({
where: {
name: name
}
name,
},
});
};
@@ -117,17 +117,17 @@ exports.updateMaintainers = function* (name, users) {
if (users.length === 0) {
return {
add: [],
remove: []
remove: [],
};
}
var exists = yield this.listMaintainers(name);
const exists = yield this.listMaintainers(name);
var addUsers = users.filter(function (username) {
const addUsers = users.filter(function(username) {
// add user which in `users` but do not in `exists`
return exists.indexOf(username) === -1;
});
var removeUsers = exists.filter(function (username) {
const removeUsers = exists.filter(function(username) {
// remove user which in `exists` by not in `users`
return users.indexOf(username) === -1;
});
@@ -139,6 +139,6 @@ exports.updateMaintainers = function* (name, users) {
return {
add: addUsers,
remove: removeUsers
remove: removeUsers,
};
};

View File

@@ -1,13 +1,3 @@
/**!
* cnpmjs.org - models/download_total.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
/**
@@ -18,7 +8,7 @@
// `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
// `gmt_create` datetime NOT NULL COMMENT 'create time',
// `gmt_modified` datetime NOT NULL COMMENT 'modified time',
// `name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
// `name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
// `date` int unsigned NOT NULL COMMENT 'YYYYMM format',
// `d01` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '01 download count',
// `d02` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '02 download count',
@@ -52,14 +42,14 @@
// `d30` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '30 download count',
// `d31` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '31 download count',
// PRIMARY KEY (`id`),
// UNIQUE KEY `uk_name_date` (`name`, `date`),
// KEY `idx_date` (`date`)
// UNIQUE KEY `name_date` (`name`, `date`)
// KEY `date` (`date`)
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module download total info';
module.exports = function (sequelize, DataTypes) {
module.exports = function(sequelize, DataTypes) {
return sequelize.define('DownloadTotal', {
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(100),
allowNull: false,
comment: 'module name',
},
@@ -260,14 +250,14 @@ module.exports = function (sequelize, DataTypes) {
indexes: [
{
unique: true,
fields: ['name', 'date'],
fields: [ 'name', 'date' ],
},
{
fields: ['date'],
}
fields: [ 'date' ],
},
],
classMethods: {
}
},
});
};

View File

@@ -1,18 +1,26 @@
/**!
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
*/
'use strict';
var path = require('path');
var config = require('../config');
var sequelize = require('../common/sequelize');
/**
* Module dependencies.
*/
const path = require('path');
const sequelize = require('../common/sequelize');
function load(name) {
return sequelize.import(path.join(__dirname, name));
}
var _ModuleAbbreviated = config.enableAbbreviatedMetadata ? load('module_abbreviated') : null;
var _PackageReadme = config.enableAbbreviatedMetadata ? load('package_readme') : null;
module.exports = {
sequelize: sequelize,
sequelize,
Module: load('module'),
ModuleLog: load('module_log'),
ModuleStar: load('module_star'),
@@ -26,38 +34,17 @@ module.exports = {
User: load('user'),
Total: load('total'),
DownloadTotal: load('download_total'),
Token: load('token'),
query: function* (sql, args) {
var options = { replacements: args };
var data = yield this.sequelize.query(sql, options);
* query(sql, args) {
const options = { replacements: args };
const data = yield this.sequelize.query(sql, options);
if (/select /i.test(sql)) {
return data[0];
}
return data[1];
},
queryOne: function* (sql, args) {
var rows = yield this.query(sql, args);
* queryOne(sql, args) {
const rows = yield this.query(sql, args);
return rows && rows[0];
},
get ModuleAbbreviated() {
if (!config.enableAbbreviatedMetadata) {
return null;
}
if (!_ModuleAbbreviated) {
_ModuleAbbreviated = load('module_abbreviated');
}
return _ModuleAbbreviated;
},
get PackageReadme() {
if (!config.enableAbbreviatedMetadata) {
return null;
}
if (!_PackageReadme) {
_PackageReadme = load('package_readme');
}
return _PackageReadme;
},
};

View File

@@ -1,32 +1,46 @@
/**!
* cnpmjs.org - models/init_script.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
var config = require('../config');
/**
* Module dependencies.
*/
const config = require('../config');
config.database.logging = console.log;
// $ node --harmony models/init_script.js <force> <dialect> <port> <username>
var force = process.argv[2] === 'true';
var dialect = process.argv[3];
const force = process.argv[2] === 'true';
const dialect = process.argv[3];
if (dialect) {
config.database.dialect = dialect;
}
var port = process.argv[4];
const port = process.argv[4];
if (port) {
config.database.port = parseInt(port);
}
var username = process.argv[5];
const username = process.argv[5];
if (username) {
config.database.username = username;
}
var models = require('./');
const models = require('./');
models.sequelize.sync({
force: force,
force,
logging: console.log,
})
.then(function () {
models.Total.init(function (err) {
})
.then(function() {
models.Total.init(function(err) {
if (err) {
console.error('[models/init_script.js] sequelize init fail');
console.error(err);
@@ -38,7 +52,7 @@ models.sequelize.sync({
}
});
})
.catch(function (err) {
.catch(function(err) {
console.error('[models/init_script.js] sequelize sync fail');
console.error(err);
process.exit(1);

View File

@@ -1,61 +1,48 @@
/**!
* cnpmjs.org - models/module.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
/**
* Module dependencies.
*/
/*
CREATE TABLE IF NOT EXISTS `module` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`author` varchar(100) NOT NULL COMMENT 'module author',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`version` varchar(30) NOT NULL COMMENT 'module version',
`description` longtext COMMENT 'module description',
`package` longtext CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT 'package.json',
`dist_shasum` varchar(100) DEFAULT NULL COMMENT 'module dist SHASUM',
`dist_tarball` varchar(2048) DEFAULT NULL COMMENT 'module dist tarball',
`dist_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'module dist size',
`publish_time` bigint(20) unsigned COMMENT 'module publish time',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name` (`name`,`version`),
KEY `idx_gmt_modified` (`gmt_modified`),
KEY `idx_publish_time` (`publish_time`),
KEY `idx_author` (`author`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module info';
`id` INTEGER NOT NULL auto_increment ,
`author` VARCHAR(100) NOT NULL,
`name` VARCHAR(100) NOT NULL,
`version` VARCHAR(30) NOT NULL,
`description` LONGTEXT,
`package` LONGTEXT,
`dist_shasum` VARCHAR(100),
`dist_tarball` VARCHAR(2048),
`dist_size` INTEGER UNSIGNED NOT NULL DEFAULT 0,
`publish_time` BIGINT(20) UNSIGNED,
`gmt_create` DATETIME NOT NULL,
`gmt_modified` DATETIME NOT NULL,
PRIMARY KEY (`id`)
)
COMMENT 'module info' ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_general_ci;
CREATE UNIQUE INDEX `module_name_version` ON `module` (`name`, `version`);
CREATE INDEX `module_gmt_modified` ON `module` (`gmt_modified`);
CREATE INDEX `module_publish_time` ON `module` (`publish_time`);
CREATE INDEX `module_author` ON `module` (`author`);
*/
module.exports = function (sequelize, DataTypes) {
module.exports = function(sequelize, DataTypes) {
return sequelize.define('Module', {
author: {
type: DataTypes.STRING(100),
allowNull: false,
comment: 'first maintainer name'
comment: 'first maintainer name',
},
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(100),
allowNull: false,
comment: 'module name'
comment: 'module name',
},
version: {
type: DataTypes.STRING(30),
allowNull: false,
comment: 'module version'
comment: 'module version',
},
description: {
type: DataTypes.LONGTEXT,
comment: 'module description',
},
package: {
type: DataTypes.LONGTEXT,
@@ -64,48 +51,44 @@ module.exports = function (sequelize, DataTypes) {
dist_shasum: {
type: DataTypes.STRING(100),
allowNull: true,
comment: 'module dist SHASUM',
},
dist_tarball: {
type: DataTypes.STRING(2048),
allowNull: true,
comment: 'module dist tarball',
},
dist_size: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0,
comment: 'module dist size',
},
publish_time: {
type: DataTypes.BIGINT(20),
allowNull: true,
comment: 'module publish time',
}
},
}, {
tableName: 'module',
comment: 'module info',
indexes: [
{
unique: true,
fields: ['name', 'version'],
fields: [ 'name', 'version' ],
},
{
fields: ['gmt_modified'],
fields: [ 'gmt_modified' ],
},
{
fields: ['publish_time'],
fields: [ 'publish_time' ],
},
{
fields: ['author'],
}
fields: [ 'author' ],
},
],
classMethods: {
findByNameAndVersion: function* (name, version) {
* findByNameAndVersion(name, version) {
return yield this.find({
where: { name: name, version: version }
where: { name, version },
});
}
}
},
},
});
};

View File

@@ -1,63 +0,0 @@
'use strict';
/*
CREATE TABLE IF NOT EXISTS `module_abbreviated` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`version` varchar(30) NOT NULL COMMENT 'module version',
`package` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT 'the abbreviated metadata',
`publish_time` bigint(20) unsigned COMMENT 'the publish time',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name` (`name`,`version`),
KEY `idx_gmt_modified` (`gmt_modified`),
KEY `idx_publish_time` (`publish_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module abbreviated info';
*/
module.exports = function (sequelize, DataTypes) {
return sequelize.define('ModuleAbbreviated', {
name: {
type: DataTypes.STRING(214),
allowNull: false,
comment: 'module name'
},
version: {
type: DataTypes.STRING(30),
allowNull: false,
comment: 'module version'
},
package: {
type: DataTypes.LONGTEXT,
comment: 'package.json',
},
publish_time: {
type: DataTypes.BIGINT(20),
allowNull: true,
comment: 'the publish time',
}
}, {
tableName: 'module_abbreviated',
comment: 'module abbreviated info',
indexes: [
{
unique: true,
fields: ['name', 'version'],
},
{
fields: ['gmt_modified'],
},
{
fields: ['publish_time'],
},
],
classMethods: {
findByNameAndVersion: function* (name, version) {
return yield this.find({
where: { name: name, version: version }
});
}
}
});
};

View File

@@ -18,26 +18,26 @@
CREATE TABLE IF NOT EXISTS `module_deps` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`deps` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'which module depend on this module',
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`deps` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '`name` is deped by `deps`',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name_deps` (`name`,`deps`),
KEY `idx_name` (`name`)
UNIQUE KEY `module_deps_name_deps` (`name`,`deps`),
KEY `deps` (`module_deps_deps`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module deps';
*/
module.exports = function (sequelize, DataTypes) {
module.exports = function(sequelize, DataTypes) {
return sequelize.define('ModuleDependency', {
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(100),
allowNull: false,
comment: 'module name',
},
dependent: {
field: 'deps',
type: DataTypes.STRING(100),
comment: '`name` is depended by `deps`. `deps` == depend => `name`'
}
comment: '`name` is depended by `deps`. `deps` == depend => `name`',
},
}, {
tableName: 'module_deps',
comment: 'module deps',
@@ -46,13 +46,13 @@ module.exports = function (sequelize, DataTypes) {
indexes: [
{
unique: true,
fields: ['name', 'deps'],
fields: [ 'name', 'deps' ],
},
{
fields: ['deps'],
}
fields: [ 'deps' ],
},
],
classMethods: {
}
},
});
};

View File

@@ -19,31 +19,29 @@ CREATE TABLE IF NOT EXISTS `module_keyword` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`keyword` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'keyword',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`description` longtext COMMENT 'module description',
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`description` longtext,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_keyword_module_name` (`keyword`,`name`),
KEY `idx_name` (`name`)
UNIQUE KEY `keyword_module_name` (`keyword`,`name`),
KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module keyword';
*/
module.exports = function (sequelize, DataTypes) {
module.exports = function(sequelize, DataTypes) {
return sequelize.define('ModuleKeyword', {
keyword: {
type: DataTypes.STRING(100),
allowNull: false,
comment: 'keyword',
},
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(100),
allowNull: false,
comment: 'module name',
},
description: {
type: DataTypes.LONGTEXT,
allowNull: true,
comment: 'module description',
}
},
}, {
tableName: 'module_keyword',
comment: 'module keyword',
@@ -51,21 +49,21 @@ module.exports = function (sequelize, DataTypes) {
indexes: [
{
unique: true,
fields: ['keyword', 'name'],
fields: [ 'keyword', 'name' ],
},
{
fields: ['name'],
}
fields: [ 'name' ],
},
],
classMethods: {
findByKeywordAndName: function* (keyword, name) {
* findByKeywordAndName(keyword, name) {
return yield this.find({
where: {
keyword: keyword,
name: name
}
keyword,
name,
},
});
}
}
},
},
});
};

View File

@@ -15,43 +15,42 @@
*/
/*
CREATE TABLE IF NOT EXISTS `module_log` (
CREATE TABLE IF NOT EXISTS `module_log` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`username` varchar(100) NOT NULL COMMENT 'which username',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`log` longtext COMMENT 'the raw log',
`username` varchar(100) NOT NULL,
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`log` longtext,
PRIMARY KEY (`id`),
KEY `idx_name` (`name`)
KEY `module_log_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module sync log';
*/
module.exports = function (sequelize, DataTypes) {
module.exports = function(sequelize, DataTypes) {
return sequelize.define('ModuleLog', {
username: {
type: DataTypes.STRING(100),
allowNull: false,
comment: 'user name'
comment: 'user name',
},
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(100),
allowNull: false,
comment: 'module name',
},
log: {
type: DataTypes.LONGTEXT,
comment: 'the raw log',
}
},
}, {
tableName: 'module_log',
comment: 'module sync log',
indexes: [
{
fields: ['name'],
}
fields: [ 'name' ],
},
],
classMethods: {
}
},
});
};

View File

@@ -19,25 +19,25 @@ CREATE TABLE IF NOT EXISTS `module_maintainer` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`user` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'user name',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_user_module_name` (`user`,`name`),
KEY `idx_name` (`name`)
UNIQUE KEY `module_maintainer_user_name` (`user`,`name`),
KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='private module maintainers';
*/
module.exports = function (sequelize, DataTypes) {
module.exports = function(sequelize, DataTypes) {
return sequelize.define('ModuleMaintainer', {
user: {
type: DataTypes.STRING(100),
allowNull: false,
comment: 'user name'
comment: 'user name',
},
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(100),
allowNull: false,
comment: 'module name',
}
},
}, {
tableName: 'module_maintainer',
comment: 'private module maintainers',
@@ -45,11 +45,11 @@ module.exports = function (sequelize, DataTypes) {
indexes: [
{
unique: true,
fields: ['user', 'name'],
fields: [ 'user', 'name' ],
},
{
fields: ['name'],
}
fields: [ 'name' ],
},
],
classMethods: require('./_module_maintainer_class_methods'),
});

View File

@@ -19,25 +19,25 @@ CREATE TABLE IF NOT EXISTS `module_star` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`user` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'user name',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_user_module_name` (`user`,`name`),
KEY `idx_name` (`name`)
UNIQUE KEY `module_star_user_name` (`user`,`name`),
KEY `module_star_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module star';
*/
module.exports = function (sequelize, DataTypes) {
module.exports = function(sequelize, DataTypes) {
return sequelize.define('ModuleStar', {
user: {
type: DataTypes.STRING(100),
allowNull: false,
comment: 'user name'
comment: 'user name',
},
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(100),
allowNull: false,
comment: 'module name',
}
},
}, {
tableName: 'module_star',
comment: 'module star',
@@ -45,13 +45,13 @@ module.exports = function (sequelize, DataTypes) {
indexes: [
{
unique: true,
fields: ['user', 'name'],
fields: [ 'user', 'name' ],
},
{
fields: ['name'],
}
fields: [ 'name' ],
},
],
classMethods: {
}
},
});
};

View File

@@ -14,25 +14,25 @@
* Module dependencies.
*/
var utils = require('./utils');
const utils = require('./utils');
/*
CREATE TABLE IF NOT EXISTS `module_unpublished` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`package` longtext CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT 'base info: tags, time, maintainers, description, versions',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name` (`name`),
KEY `idx_gmt_modified` (`gmt_modified`)
UNIQUE KEY `module_unpublished_name` (`name`),
KEY `module_unpublished_gmt_modified` (`gmt_modified`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module unpublished info';
*/
module.exports = function (sequelize, DataTypes) {
module.exports = function(sequelize, DataTypes) {
return sequelize.define('ModuleUnpublished', {
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(100),
allowNull: false,
comment: 'module name',
},
@@ -41,47 +41,47 @@ module.exports = function (sequelize, DataTypes) {
comment: 'base info: tags, time, maintainers, description, versions',
get: utils.JSONGetter('package'),
set: utils.JSONSetter('package'),
}
},
}, {
tableName: 'module_unpublished',
comment: 'module unpublished info',
indexes: [
{
unique: true,
fields: ['name'],
fields: [ 'name' ],
},
{
fields: ['gmt_modified'],
}
fields: [ 'gmt_modified' ],
},
],
classMethods: {
findByName: function* (name) {
* findByName(name) {
return yield this.find({
where: {
name: name
}
name,
},
});
},
save: function* (name, pkg) {
var row = yield this.find({
* save(name, pkg) {
let row = yield this.find({
where: {
name: name
}
name,
},
});
if (row) {
row.package = pkg;
if (row.changed()) {
row = yield row.save(['package']);
row = yield row.save([ 'package' ]);
}
return row;
}
row = this.build({
name: name,
name,
package: pkg,
});
return yield row.save();
}
}
},
},
});
};

View File

@@ -19,25 +19,25 @@ CREATE TABLE IF NOT EXISTS `npm_module_maintainer` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`user` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'user name',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_user_module_name` (`user`,`name`),
KEY `idx_name` (`name`)
UNIQUE KEY `npm_module_maintainer_user_name` (`user`,`name`),
KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='npm original module maintainers';
*/
module.exports = function (sequelize, DataTypes) {
module.exports = function(sequelize, DataTypes) {
return sequelize.define('NpmModuleMaintainer', {
user: {
type: DataTypes.STRING(100),
allowNull: false,
comment: 'user name'
comment: 'user name',
},
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(100),
allowNull: false,
comment: 'module name',
}
},
}, {
tableName: 'npm_module_maintainer',
comment: 'npm original module maintainers',
@@ -45,11 +45,11 @@ module.exports = function (sequelize, DataTypes) {
indexes: [
{
unique: true,
fields: ['user', 'name'],
fields: [ 'user', 'name' ],
},
{
fields: ['name'],
}
fields: [ 'name' ],
},
],
classMethods: require('./_module_maintainer_class_methods'),
});

View File

@@ -1,53 +0,0 @@
'use strict';
/*
CREATE TABLE IF NOT EXISTS `package_readme` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`readme` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT 'the latest version readme',
`version` varchar(30) NOT NULL COMMENT 'module version',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name` (`name`),
KEY `idx_gmt_modified` (`gmt_modified`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='package latest readme';
*/
module.exports = function (sequelize, DataTypes) {
return sequelize.define('PackageReadme', {
name: {
type: DataTypes.STRING(214),
allowNull: false,
comment: 'module name'
},
version: {
type: DataTypes.STRING(30),
allowNull: false,
comment: 'module latest version'
},
readme: {
type: DataTypes.LONGTEXT,
comment: 'latest version readme',
},
}, {
tableName: 'package_readme',
comment: 'package latest readme',
indexes: [
{
unique: true,
fields: ['name'],
},
{
fields: ['gmt_modified'],
},
],
classMethods: {
findByName: function* (name) {
return yield this.find({
where: { name: name },
});
}
}
});
};

View File

@@ -19,20 +19,20 @@ CREATE TABLE IF NOT EXISTS `tag` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`tag` varchar(30) NOT NULL COMMENT 'tag name',
`version` varchar(30) NOT NULL COMMENT 'module version',
`module_id` bigint(20) unsigned NOT NULL COMMENT 'module id',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name` (`name`, `tag`),
KEY `idx_gmt_modified` (`gmt_modified`)
UNIQUE KEY `tag_name_tag` (`name`, `tag`),
KEY `tag_gmt_modified` (`gmt_modified`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module tag';
*/
module.exports = function (sequelize, DataTypes) {
module.exports = function(sequelize, DataTypes) {
return sequelize.define('Tag', {
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(100),
allowNull: false,
comment: 'module name',
},
@@ -49,24 +49,24 @@ module.exports = function (sequelize, DataTypes) {
module_id: {
type: DataTypes.BIGINT(20),
allowNull: false,
comment: 'module id'
}
comment: 'module id',
},
}, {
tableName: 'tag',
comment: 'module tag',
indexes: [
{
unique: true,
fields: ['name', 'tag'],
fields: [ 'name', 'tag' ],
},
{
fields: ['gmt_modified'],
}
fields: [ 'gmt_modified' ],
},
],
classMethods: {
findByNameAndTag: function* (name, tag) {
return yield this.find({ where: { name: name, tag: tag } });
}
}
* findByNameAndTag(name, tag) {
return yield this.find({ where: { name, tag } });
},
},
});
};

View File

@@ -1,117 +0,0 @@
'use strict';
/*
CREATE TABLE IF NOT EXISTS `token` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`token` varchar(100) NOT NULL COMMENT 'token',
`user_id` varchar(100) NOT NULL COMMENT 'user name',
`readonly` tinyint NOT NULL DEFAULT 0 COMMENT 'readonly or not, 1: true, other: false',
`token_key` varchar(200) NOT NULL COMMENT 'token sha512 hash',
`cidr_whitelist` varchar(500) NOT NULL COMMENT 'ip list, ["127.0.0.1"]',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_token` (`token`),
KEY `idx_user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='token info';
*/
module.exports = function (sequelize, DataTypes) {
return sequelize.define('Token', {
token: {
type: DataTypes.STRING(100),
allowNull: false,
comment: 'token',
},
userId: {
field: 'user_id',
type: DataTypes.STRING(100),
allowNull: false,
comment: 'user name'
},
readonly: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
comment: 'readonly or not, 1: true, other: false',
},
key: {
field: 'token_key',
type: DataTypes.STRING(256),
allowNull: false,
comment: 'token sha512 hash',
},
cidrWhitelist: {
field: 'cidr_whitelist',
type: DataTypes.STRING(500),
allowNull: false,
comment: 'ip list, ["127.0.0.1"]',
get: function () {
try {
return JSON.parse(this.getDataValue('cidrWhitelist'));
} catch (_) {
return [];
}
},
set: function (val) {
try {
var stringifyVal = JSON.stringify(val);
this.setDataValue('cidrWhitelist', stringifyVal);
} catch (_) {
// ...
}
}
},
}, {
tableName: 'token',
comment: 'token info',
indexes: [
{
unique: true,
fields: [ 'token' ],
},
{
fields: [ 'user_id' ],
}
],
classMethods: {
findByToken: function* (token) {
return yield this.find({ where: { token: token } });
},
add: function* (tokenObj) {
var row = this.build(tokenObj);
return yield row.save();
},
listByUser: function* (userId, offset, limit) {
return yield this.findAll({
where: {
userId: userId,
},
limit: limit,
offset: offset,
order: 'id asc',
});
},
deleteByKeyOrToken: function* (userId, keyOrToken) {
const self = this;
yield sequelize.transaction(function (t) {
return self.destroy({
where: {
userId: userId,
$or: [
{ key: { like: keyOrToken + '%' } },
{ token: keyOrToken },
],
},
transaction: t,
}).then(function (deleteRows) {
// Key like query should not match more than 1 row
if (deleteRows > 1) {
throw new Error(`Token ID "${keyOrToken}" was ambiguous`);
}
});
});
},
},
});
};

View File

@@ -1,21 +1,7 @@
/**!
* cnpmjs.org - models/total.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
/**
* Module dependencies.
*/
// CREATE TABLE IF NOT EXISTS `total` (
// `name` varchar(214) NOT NULL COMMENT 'total name',
// `name` varchar(100) NOT NULL COMMENT 'total name',
// `gmt_modified` datetime NOT NULL COMMENT 'modified time',
// `module_delete` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'module delete count',
// `last_sync_time` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'last timestamp sync from official registry',
@@ -32,12 +18,12 @@
// INSERT INTO total(name, gmt_modified) VALUES('total', now())
// ON DUPLICATE KEY UPDATE gmt_modified=now();
module.exports = function (sequelize, DataTypes) {
module.exports = function(sequelize, DataTypes) {
return sequelize.define('Total', {
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(100),
primaryKey: true,
comment: 'total name'
comment: 'total name',
},
module_delete: {
type: DataTypes.BIGINT(20),
@@ -97,14 +83,14 @@ module.exports = function (sequelize, DataTypes) {
comment: 'total info',
createdAt: false,
classMethods: {
init: function (callback) {
var that = this;
init(callback) {
const that = this;
that.find({
where: { name: 'total' }
}).then(function (row) {
where: { name: 'total' },
}).then(function(row) {
if (!row) {
that.build({name: 'total'}).save()
.then(function () {
that.build({ name: 'total' }).save()
.then(function() {
callback();
})
.catch(callback);
@@ -112,7 +98,7 @@ module.exports = function (sequelize, DataTypes) {
}
callback();
}).catch(callback);
}
}
},
},
});
};

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