Compare commits
306 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
20f1ab3af8 | ||
|
|
a932b6487d | ||
|
|
8f05a6e8b9 | ||
|
|
4a91795288 | ||
|
|
de72396cd2 | ||
|
|
7f48d45e0d | ||
|
|
553c1daf85 | ||
|
|
084b83e8d7 | ||
|
|
05e5ac86ba | ||
|
|
2a530a3d6e | ||
|
|
bde9de9f90 | ||
|
|
d538658be9 | ||
|
|
81a010bc53 | ||
|
|
7f2c5725e5 | ||
|
|
7f89b3be92 | ||
|
|
1c6eb50e05 | ||
|
|
aa8d4ac850 | ||
|
|
e0eeefa43e | ||
|
|
662b91ea9a | ||
|
|
cc70f9e6b1 | ||
|
|
7312d761ca | ||
|
|
d5c0e07c1c | ||
|
|
e108b4804d | ||
|
|
92c218d755 | ||
|
|
df2a974b2c | ||
|
|
7fc1a8411c | ||
|
|
0759328140 | ||
|
|
3fc2ea7f25 | ||
|
|
2f2794785f | ||
|
|
ea1f253624 | ||
|
|
b25eaaf73e | ||
|
|
0b0164cbf4 | ||
|
|
8a727c351d | ||
|
|
2c6a08908d | ||
|
|
9a7e94f7a4 | ||
|
|
9a2fba42c6 | ||
|
|
ab8d6bfd19 | ||
|
|
619f4310e8 | ||
|
|
41eea6747d | ||
|
|
4e3a50f3bb | ||
|
|
e5a9a22f86 | ||
|
|
eb66d8958f | ||
|
|
8a0dcb5063 | ||
|
|
cba9d5dbbb | ||
|
|
233b736401 | ||
|
|
58d72bde22 | ||
|
|
38683a82ef | ||
|
|
71cc54fb3b | ||
|
|
b0eb611dc4 | ||
|
|
7e810dba48 | ||
|
|
f4dc41ede5 | ||
|
|
5574301909 | ||
|
|
261c97f1e8 | ||
|
|
af8ae63eab | ||
|
|
89e7ae3ba2 | ||
|
|
46d65a1878 | ||
|
|
77074e3ae3 | ||
|
|
34fa887aa8 | ||
|
|
6dc779a9b4 | ||
|
|
24d6831a68 | ||
|
|
d83c52b32f | ||
|
|
25334e5d95 | ||
|
|
e27af73ccc | ||
|
|
f85da0a384 | ||
|
|
1b6159132d | ||
|
|
2130041f7f | ||
|
|
b5646e12a6 | ||
|
|
d70c284b83 | ||
|
|
ddbb0b4fed | ||
|
|
35c1a956f7 | ||
|
|
416e2650cc | ||
|
|
964cfd5242 | ||
|
|
dd5ac02876 | ||
|
|
54a11ebb17 | ||
|
|
9131dcb105 | ||
|
|
6e276c58c2 | ||
|
|
7b1929b717 | ||
|
|
ec4a74f439 | ||
|
|
027d2bb46b | ||
|
|
2b6bd06bec | ||
|
|
ff523d7374 | ||
|
|
931afd7f4b | ||
|
|
f2724d959f | ||
|
|
ca884d92ca | ||
|
|
44c8e05563 | ||
|
|
8c0f9057ac | ||
|
|
fde319a788 | ||
|
|
c6704782a7 | ||
|
|
491b91ac0e | ||
|
|
f7819186e7 | ||
|
|
4652eca74e | ||
|
|
ebb7cbcf68 | ||
|
|
20b98b1227 | ||
|
|
7e288a442c | ||
|
|
17d1527551 | ||
|
|
4de50e2bf8 | ||
|
|
80bf9d2d73 | ||
|
|
7d8f6ec92f | ||
|
|
1bbf4d7ebe | ||
|
|
88f8cb8fa9 | ||
|
|
7c59fa5e37 | ||
|
|
ccb24025b5 | ||
|
|
6f24c10cad | ||
|
|
2579b62e3a | ||
|
|
38ed0a91b2 | ||
|
|
57a025d96e | ||
|
|
eef7467321 | ||
|
|
4c89b8a4c1 | ||
|
|
353177deee | ||
|
|
603fdeea06 | ||
|
|
1850d33b83 | ||
|
|
8827ca26a7 | ||
|
|
9e34566a19 | ||
|
|
8e4fd68cca | ||
|
|
a8fbeb7be5 | ||
|
|
cd0249dfb9 | ||
|
|
e34d1948fb | ||
|
|
4cd46b1857 | ||
|
|
c8a7343d45 | ||
|
|
0689cc93ce | ||
|
|
6c98c1f07f | ||
|
|
94021b0553 | ||
|
|
9ed3b3ad7a | ||
|
|
d651c2ee6f | ||
|
|
3653daf261 | ||
|
|
01a722190f | ||
|
|
4aec25d4ab | ||
|
|
2049978a68 | ||
|
|
93b98bcb7e | ||
|
|
4838efc141 | ||
|
|
1e48c47010 | ||
|
|
81d742d210 | ||
|
|
665d72ed32 | ||
|
|
c70888d7ac | ||
|
|
5acda5ee57 | ||
|
|
84587bfa86 | ||
|
|
263910c13c | ||
|
|
e6667cceb8 | ||
|
|
b5bfef5160 | ||
|
|
0cbf8e377f | ||
|
|
0e1fdb1c05 | ||
|
|
b582f73e7d | ||
|
|
9b52820a8d | ||
|
|
f08c9dd147 | ||
|
|
990a980af1 | ||
|
|
948d1b652b | ||
|
|
7bb782179b | ||
|
|
1bf7eb7324 | ||
|
|
da69df9bb0 | ||
|
|
b475b231df | ||
|
|
15cb3e66da | ||
|
|
9ee511b96c | ||
|
|
cad590deb4 | ||
|
|
c253994edc | ||
|
|
2d042af2c6 | ||
|
|
ad0dd602e0 | ||
|
|
d95221dd7f | ||
|
|
acbf75514d | ||
|
|
87ad2002a7 | ||
|
|
1183d24d26 | ||
|
|
207707feff | ||
|
|
445db415c9 | ||
|
|
b11693c6bc | ||
|
|
a28ede9e32 | ||
|
|
45fba36a7d | ||
|
|
4e02b2d211 | ||
|
|
e11679f933 | ||
|
|
49d43999e4 | ||
|
|
3b71d81e4a | ||
|
|
b92c239325 | ||
|
|
25c998db33 | ||
|
|
fc3ad15c53 | ||
|
|
4fda3d0375 | ||
|
|
dc5c3e24e2 | ||
|
|
140f5b302e | ||
|
|
aa9d6479de | ||
|
|
651d4b207e | ||
|
|
c8d66f6d78 | ||
|
|
c02e66fea5 | ||
|
|
ff51d7febf | ||
|
|
2dc92530f5 | ||
|
|
d835526eda | ||
|
|
6f29f4cc3b | ||
|
|
a5baac4f7f | ||
|
|
8f1f919d2e | ||
|
|
8e2f0052dc | ||
|
|
6e77a171fd | ||
|
|
3b08a7fd21 | ||
|
|
e3f4d8d158 | ||
|
|
5b3d92ee43 | ||
|
|
c1e35cabe3 | ||
|
|
63d604d9bb | ||
|
|
b402f04fc9 | ||
|
|
a4920fdcee | ||
|
|
798f357132 | ||
|
|
9ce8482a13 | ||
|
|
23067fa1a2 | ||
|
|
abdbf69f25 | ||
|
|
a4d7331f52 | ||
|
|
fac45324b9 | ||
|
|
98c750df1a | ||
|
|
22335db1de | ||
|
|
47ad267df5 | ||
|
|
49b27d562d | ||
|
|
f49ce17457 | ||
|
|
128383d032 | ||
|
|
f47615501c | ||
|
|
adae52cef3 | ||
|
|
30407dee07 | ||
|
|
1ba6f08f4b | ||
|
|
083104e2e1 | ||
|
|
12b09ab559 | ||
|
|
cf910e1d8c | ||
|
|
eda52b0a3e | ||
|
|
cd2fd4a0b5 | ||
|
|
8cfa7a4d8b | ||
|
|
716efab4ea | ||
|
|
a0ec1c8521 | ||
|
|
be1a966ce1 | ||
|
|
c5b5064e40 | ||
|
|
de7bef05c2 | ||
|
|
c03be9c69d | ||
|
|
dd092b356d | ||
|
|
d3e93f3990 | ||
|
|
07accd62e4 | ||
|
|
8c280bd789 | ||
|
|
3d0674fea3 | ||
|
|
393bd42938 | ||
|
|
1ac1cf82d8 | ||
|
|
206d595f02 | ||
|
|
aa135d2a30 | ||
|
|
d2949c9450 | ||
|
|
7e583b2edf | ||
|
|
9b13c184fe | ||
|
|
1830301ae6 | ||
|
|
7774a5c6d9 | ||
|
|
05c6d7b4f6 | ||
|
|
d30260f301 | ||
|
|
c8990083a7 | ||
|
|
ef246c2ec4 | ||
|
|
2a8bd139d0 | ||
|
|
ea322c3310 | ||
|
|
48d8cb81fc | ||
|
|
dd9f4a7f2e | ||
|
|
56eb1b1640 | ||
|
|
4b7a191b73 | ||
|
|
d48cf497cf | ||
|
|
abaac6089a | ||
|
|
e8ba8eadb3 | ||
|
|
20093741eb | ||
|
|
51ddf2cf02 | ||
|
|
26efecd2fc | ||
|
|
678c85102e | ||
|
|
439b6663f3 | ||
|
|
3069179718 | ||
|
|
f2613edcd5 | ||
|
|
6e9ceaf844 | ||
|
|
188dab4ed8 | ||
|
|
5d0d742e85 | ||
|
|
92a828c04d | ||
|
|
3c804758e7 | ||
|
|
683b2e3640 | ||
|
|
35d445cd11 | ||
|
|
45a9e0ff97 | ||
|
|
dda2f0785c | ||
|
|
ce02d2f48e | ||
|
|
c36523da17 | ||
|
|
93f33a98cf | ||
|
|
c8372f06a8 | ||
|
|
234e8bf27b | ||
|
|
dfe56b6222 | ||
|
|
8548104ee6 | ||
|
|
ceeb617f0d | ||
|
|
91ee61c997 | ||
|
|
1d94ba4e11 | ||
|
|
5b09d974fb | ||
|
|
763224bbbb | ||
|
|
7b6c757b83 | ||
|
|
fd915e0c5d | ||
|
|
fb39eb8c2a | ||
|
|
f139cccad0 | ||
|
|
4bb2e6a5d8 | ||
|
|
4d3c257474 | ||
|
|
b990bd9cf1 | ||
|
|
917b407d1e | ||
|
|
774fd44b12 | ||
|
|
a6b67f5453 | ||
|
|
1e56c77deb | ||
|
|
e1e6b6b674 | ||
|
|
615d16165c | ||
|
|
8986503005 | ||
|
|
4b5a49b626 | ||
|
|
fce0876295 | ||
|
|
9df81764a9 | ||
|
|
4801f15768 | ||
|
|
d24422fdba | ||
|
|
835f898eb3 | ||
|
|
2a3a0436e7 | ||
|
|
0a782ea014 | ||
|
|
3813827d77 | ||
|
|
264544c780 | ||
|
|
416afb2e9a | ||
|
|
fe15eb78e9 | ||
|
|
64d2facef3 | ||
|
|
d677e804b5 | ||
|
|
f4dae42589 |
17
.editorconfig
Normal file
17
.editorconfig
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# http://editorconfig.org
|
||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 2
|
||||||
|
end_of_line = lf
|
||||||
|
charset = utf-8
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
insert_final_newline = true
|
||||||
|
|
||||||
|
[*.md]
|
||||||
|
trim_trailing_whitespace = false
|
||||||
|
|
||||||
|
[Makefile]
|
||||||
|
indent_style = tab
|
||||||
|
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -28,3 +28,4 @@ bin/test.sql
|
|||||||
coverage/
|
coverage/
|
||||||
config/web_readme.md
|
config/web_readme.md
|
||||||
.tmp/
|
.tmp/
|
||||||
|
*.sqlite
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
// "single" : require single quotes
|
// "single" : require single quotes
|
||||||
// "double" : require double quotes
|
// "double" : require double quotes
|
||||||
"undef" : true, // true: Require all non-global variables to be declared (prevents global leaks)
|
"undef" : true, // true: Require all non-global variables to be declared (prevents global leaks)
|
||||||
"unused" : false, // true: Require all defined variables be used
|
"unused" : true, // true: Require all defined variables be used
|
||||||
"strict" : true, // true: Requires all functions run in ES5 Strict Mode
|
"strict" : true, // true: Requires all functions run in ES5 Strict Mode
|
||||||
"trailing" : false, // true: Prohibit trailing whitespaces
|
"trailing" : false, // true: Prohibit trailing whitespaces
|
||||||
"maxparams" : false, // {int} Max number of formal params allowed per function
|
"maxparams" : false, // {int} Max number of formal params allowed per function
|
||||||
|
|||||||
3
.node-dev.json
Normal file
3
.node-dev.json
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"notify": false
|
||||||
|
}
|
||||||
@@ -20,3 +20,6 @@ coverage/
|
|||||||
config/web_readme.md
|
config/web_readme.md
|
||||||
.dist/
|
.dist/
|
||||||
config/config.js
|
config/config.js
|
||||||
|
*.sqlite
|
||||||
|
.node-dev.json
|
||||||
|
nohup.out
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
|
sudo: false
|
||||||
language: node_js
|
language: node_js
|
||||||
node_js:
|
node_js:
|
||||||
- '0.11'
|
- 'iojs-2'
|
||||||
script: "make test-travis"
|
addons:
|
||||||
after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
|
- postgresql: '9.3'
|
||||||
|
script: 'make test-travis-all'
|
||||||
|
after_script: 'npm install coveralls@2 && cat ./coverage/lcov.info | coveralls'
|
||||||
|
|||||||
3
AUTHORS
3
AUTHORS
@@ -1,8 +1,7 @@
|
|||||||
# Ordered by date of first contribution.
|
# Ordered by date of first contribution.
|
||||||
# Auto-generated by 'contributors' on Mon, 03 Mar 2014 13:01:28 GMT.
|
|
||||||
# https://github.com/xingrz/node-contributors
|
|
||||||
|
|
||||||
fengmk2 <fengmk2@gmail.com> (https://github.com/fengmk2)
|
fengmk2 <fengmk2@gmail.com> (https://github.com/fengmk2)
|
||||||
dead-horse <dead_horse@qq.com> (https://github.com/dead-horse)
|
dead-horse <dead_horse@qq.com> (https://github.com/dead-horse)
|
||||||
alsotang <alsotang@gmail.com> (https://github.com/alsotang)
|
alsotang <alsotang@gmail.com> (https://github.com/alsotang)
|
||||||
4simple <wondger@qq.com> (https://github.com/4simple)
|
4simple <wondger@qq.com> (https://github.com/4simple)
|
||||||
|
afc163 <afc163@gmail.com> (https://github.com/afc163)
|
||||||
|
|||||||
307
History.md
307
History.md
@@ -1,8 +1,313 @@
|
|||||||
|
|
||||||
|
2.1.3 / 2015-08-18
|
||||||
|
==================
|
||||||
|
|
||||||
|
* fix: sync public scope package download url is wrong
|
||||||
|
* fix: default registry change to taobao registry
|
||||||
|
|
||||||
|
2.1.2 / 2015-08-09
|
||||||
|
==================
|
||||||
|
|
||||||
|
* fix(syncer): sync worker pkg null bug
|
||||||
|
* feat(web): add downloads badge
|
||||||
|
|
||||||
|
2.1.1 / 2015-07-27
|
||||||
|
==================
|
||||||
|
|
||||||
|
* fix: fix private scope package detect
|
||||||
|
* fix: dont sync if upstream is npm registry
|
||||||
|
* fix(sync): support sync public scope package
|
||||||
|
* test: fix fails tests
|
||||||
|
* fix: ignore 503 server error
|
||||||
|
* fix: ignore sync 503 server error
|
||||||
|
|
||||||
|
2.1.0 / 2015-07-08
|
||||||
|
==================
|
||||||
|
|
||||||
|
* feat(web): search support jsonp
|
||||||
|
* fix function name
|
||||||
|
|
||||||
|
2.0.0 / 2015-05-11
|
||||||
|
==================
|
||||||
|
|
||||||
|
* fix: real download as stream
|
||||||
|
* add custom ad banner config
|
||||||
|
* add sponsors: ucloud.cn
|
||||||
|
* fix small typo
|
||||||
|
* feat(urllib): support http_proxy
|
||||||
|
* force using https links
|
||||||
|
|
||||||
|
2.0.0-rc.15 / 2015-02-15
|
||||||
|
==================
|
||||||
|
|
||||||
|
* fix(markdown): filter xss after markdown render
|
||||||
|
* feat(database): support PostgreSQL
|
||||||
|
|
||||||
|
2.0.0-rc.14 / 2015-02-14
|
||||||
|
==================
|
||||||
|
|
||||||
|
* feat: support always-auth
|
||||||
|
* fix mysql select args = [] bug
|
||||||
|
* fix #597 sequelize raw query.
|
||||||
|
* fix(markdown): hotfix markdown-it cpu problem
|
||||||
|
* feat: upgrade to co4
|
||||||
|
* use kcors fixes #594
|
||||||
|
|
||||||
|
2.0.0-rc.13 / 2015-02-04
|
||||||
|
==================
|
||||||
|
|
||||||
|
* docs: Deploy a private npm registry in 5 minutes
|
||||||
|
* refactor(config): move application data to ~/.cnpmjs.org/
|
||||||
|
* fix(sync): make get popular pakcage faster
|
||||||
|
* feat(sync): web page also redirect to npm www
|
||||||
|
* refactor(config): make syncModel to none by default
|
||||||
|
* test: fix admin can not publish non-scoped package test cases
|
||||||
|
* docs: add chinese mirror link
|
||||||
|
* fix: admin can not publish non scoped package on "none" sync model
|
||||||
|
* feat(sync): enable none syncModel proxy all public packages
|
||||||
|
* fix: ignore username start with " or '
|
||||||
|
* fix(bin): fix stop not work on iojs
|
||||||
|
|
||||||
|
2.0.0-rc.12 / 2015-02-01
|
||||||
|
==================
|
||||||
|
|
||||||
|
* feat(syncer): add hostname ua
|
||||||
|
* fix(web): remove pkg.contributors logic
|
||||||
|
|
||||||
|
2.0.0-rc.11 / 2015-02-01
|
||||||
|
==================
|
||||||
|
|
||||||
|
* fix xss tests
|
||||||
|
* fix(markdown): revert marky-markdown
|
||||||
|
|
||||||
|
2.0.0-rc.10 / 2015-01-31
|
||||||
|
==================
|
||||||
|
|
||||||
|
* feat(middleware): CORS headers for GET and HEAD requests
|
||||||
|
* fix(readme): fix index page markdown
|
||||||
|
* feat(markdown): use npm same markdown parser
|
||||||
|
* feat(download): support download redirect to nfs
|
||||||
|
* feat(syncer): request npm registry with gzip
|
||||||
|
* change(sync): remove dist syncer
|
||||||
|
* feat(registry): add dist tag api
|
||||||
|
* refactor(common): remove redis store
|
||||||
|
|
||||||
|
2.0.0-rc.9 / 2015-01-22
|
||||||
|
==================
|
||||||
|
|
||||||
|
* hotfix reame render error, pin xss
|
||||||
|
* fix registry user auth api
|
||||||
|
|
||||||
|
2.0.0-rc.8 / 2015-01-10
|
||||||
|
==================
|
||||||
|
|
||||||
|
* fix(markdown): readme.md allow scripts
|
||||||
|
* fix(style) flexbox compatibility for both chrome and firefox (@afc163)
|
||||||
|
* feat(sync): default sync exist packages
|
||||||
|
|
||||||
|
2.0.0-rc.7 / 2015-01-07
|
||||||
|
==================
|
||||||
|
|
||||||
|
* install sync dont check `enablePrivate`
|
||||||
|
* fix(markdown): filter xss readme before markdown render
|
||||||
|
|
||||||
|
2.0.0-rc.6 / 2015-01-05
|
||||||
|
==================
|
||||||
|
|
||||||
|
* fix(markdown): use markdown-it
|
||||||
|
* add userService options on config
|
||||||
|
* add upload to nfs sync info log
|
||||||
|
|
||||||
|
2.0.0-rc.5 / 2015-01-03
|
||||||
|
==================
|
||||||
|
|
||||||
|
* fix(markdown): use marked instead of remarkable
|
||||||
|
* fix(package): pkg.readme is not a string, dont remarkable it
|
||||||
|
* feat(sync): sync user profile
|
||||||
|
|
||||||
|
2.0.0-rc.4 / 2014-12-25
|
||||||
|
==================
|
||||||
|
|
||||||
|
* refactor(download): try to use nsf.url() first
|
||||||
|
* use __all__ for full downloads
|
||||||
|
* refactor(download_total): optimize download total
|
||||||
|
* fix sqlite raw sql return datetime is string format
|
||||||
|
* fix(download_total): change column date to DateTime
|
||||||
|
* fix(services/download_total): fix download_total slow sql on `date >= $start and date <= $end`
|
||||||
|
* fix(markdown): replace marked use remarkable
|
||||||
|
|
||||||
|
2.0.0-rc.3 / 2014-12-14
|
||||||
|
==================
|
||||||
|
|
||||||
|
* fix(services): need to detect instance isDirty or not before save()
|
||||||
|
|
||||||
|
2.0.0-rc.2 / 2014-12-11
|
||||||
|
==================
|
||||||
|
|
||||||
|
* add download API, closes [#529](https://github.com/cnpm/cnpmjs.org/issues/529)
|
||||||
|
* fix missing home page title (@rockdai)
|
||||||
|
* Fix typo in view/web/package.html (@LoicMahieu)
|
||||||
|
|
||||||
|
2.0.0-rc.1 / 2014-12-09
|
||||||
|
==================
|
||||||
|
|
||||||
|
* fix xss on title
|
||||||
|
* feat(badge): support badge image url with tag
|
||||||
|
|
||||||
|
2.0.0-beta5 / 2014-12-05
|
||||||
|
==================
|
||||||
|
|
||||||
|
* hotfix package.html typo. Closes [#521](https://github.com/cnpm/cnpmjs.org/issues/521)
|
||||||
|
* Add editorconfig
|
||||||
|
* fix(web/package): package name to long cause style problem fix
|
||||||
|
* fix(css): use github-markdown-css for markdown body
|
||||||
|
* feat(mock): use koa-mock for front end dev
|
||||||
|
|
||||||
|
2.0.0-beta4 / 2014-11-21
|
||||||
|
==================
|
||||||
|
|
||||||
|
* fix(registry): add missing /-/short api
|
||||||
|
* zoom sync link
|
||||||
|
* new design for package page
|
||||||
|
* image max width, fixed [#505](https://github.com/cnpm/cnpmjs.org/issues/505)
|
||||||
|
* feat(middleware): block Ruby user-agent
|
||||||
|
|
||||||
|
2.0.0-beta3 / 2014-11-12
|
||||||
|
==================
|
||||||
|
|
||||||
|
* fix(sync): should not sync package when maintainers sort change
|
||||||
|
* fix(package): detect package is private or not
|
||||||
|
* fix(maintainer): fix missing maintainers
|
||||||
|
|
||||||
|
2.0.0-beta2 / 2014-11-09
|
||||||
|
==================
|
||||||
|
|
||||||
|
* fix(sync): add missing syncUpstreamFirst argument
|
||||||
|
|
||||||
|
2.0.0-beta1 / 2014-11-07
|
||||||
|
==================
|
||||||
|
|
||||||
|
* refactor(sync_worker): only sync request need to sync upstream first
|
||||||
|
* fix(sync_worker): make sure end event will emit
|
||||||
|
* fix: mv readme.md script to public/js/readme.js
|
||||||
|
* fix(sync): hotfix co uncaughtException
|
||||||
|
* feat(sync): sync python dist
|
||||||
|
* pin autod@1
|
||||||
|
* remove useless comment
|
||||||
|
* refactor models/_module_maintainer_class_methods.js
|
||||||
|
|
||||||
|
2.0.0-beta0 / 2014-11-02
|
||||||
|
==================
|
||||||
|
|
||||||
|
* ungrade koa-markdown to use remarkable, close [#482](https://github.com/cnpm/cnpmjs.org/issues/482)
|
||||||
|
* fix(module_log): limit module sync log size to 1MB
|
||||||
|
* refactor(config): remove adaptScope config key
|
||||||
|
* chore(Makefile): $ make install-production
|
||||||
|
* fix(sequelize): show warnning message when using old config.js
|
||||||
|
* docs(readme): Migrating from 1.x to 2.x
|
||||||
|
* feat(sync): add min sync interval time detect
|
||||||
|
* refactor(dispatch): remove unused codes
|
||||||
|
* use sequelize to connect database
|
||||||
|
|
||||||
|
1.7.1 / 2014-10-15
|
||||||
|
==================
|
||||||
|
|
||||||
|
* fix typo in sync popular, fix [#477](https://github.com/cnpm/cnpmjs.org/issues/477)
|
||||||
|
|
||||||
|
1.7.0 / 2014-10-15
|
||||||
|
==================
|
||||||
|
|
||||||
|
* Merge pull request [#475](https://github.com/cnpm/cnpmjs.org/issues/475) from KidkArolis/configurable-short-registry-url
|
||||||
|
* If sourceNpmRegistry is cnpm - use it in getShort
|
||||||
|
* Merge pull request [#472](https://github.com/cnpm/cnpmjs.org/issues/472) from cnpm/issue468-upon
|
||||||
|
* fix test label
|
||||||
|
* add sync popular modules, close [#468](https://github.com/cnpm/cnpmjs.org/issues/468)
|
||||||
|
* add sendmail test script
|
||||||
|
|
||||||
|
1.6.1 / 2014-10-09
|
||||||
|
==================
|
||||||
|
|
||||||
|
* make test on travis faster
|
||||||
|
* ensure not sync user also has his own package names
|
||||||
|
* add [v1.6.x-upgrade.sql](https://github.com/cnpm/cnpmjs.org/blob/master/docs/update_sqls/v1.6.x-upgrade.sql)
|
||||||
|
* save npm original package maintainers to npm_module_maintainer table. fixed [#464](https://github.com/cnpm/cnpmjs.org/issues/464)
|
||||||
|
* use simple 404
|
||||||
|
|
||||||
|
1.6.0 / 2014-10-08
|
||||||
|
==================
|
||||||
|
|
||||||
|
* list user all packages api. fixed [#462](https://github.com/cnpm/cnpmjs.org/issues/462)
|
||||||
|
* add node-dev: $ make dev
|
||||||
|
* always start sync worker
|
||||||
|
* update node mailer
|
||||||
|
* update autod
|
||||||
|
|
||||||
|
1.5.5 / 2014-09-25
|
||||||
|
==================
|
||||||
|
|
||||||
|
* fix sync in web
|
||||||
|
* sync upstream only the first package. make sync devDependencies optionsal, default is false
|
||||||
|
* add some comment, default sourceNpmRegistryIsCNpm to true
|
||||||
|
|
||||||
|
1.5.4 / 2014-09-24
|
||||||
|
==================
|
||||||
|
|
||||||
|
* format sync log
|
||||||
|
|
||||||
|
1.5.3 / 2014-09-24
|
||||||
|
==================
|
||||||
|
|
||||||
|
* support sync upstream first. fixed [#451](https://github.com/cnpm/cnpmjs.org/issues/451)
|
||||||
|
|
||||||
|
1.5.2 / 2014-09-24
|
||||||
|
==================
|
||||||
|
|
||||||
|
* support im url on user profile page; update bootstrap to 3.2.0
|
||||||
|
|
||||||
|
1.5.1 / 2014-09-23
|
||||||
|
==================
|
||||||
|
|
||||||
|
* search support case insensitive, close [#450](https://github.com/cnpm/cnpmjs.org/issues/450)
|
||||||
|
* add config._syncInWeb, close [#448](https://github.com/cnpm/cnpmjs.org/issues/448)
|
||||||
|
* show maintainers when publish 403. fixed [#430](https://github.com/cnpm/cnpmjs.org/issues/430)
|
||||||
|
* no attachment for html
|
||||||
|
|
||||||
|
1.5.0 / 2014-09-15
|
||||||
|
==================
|
||||||
|
|
||||||
|
* dist sync document too. fixed [#420](https://github.com/cnpm/cnpmjs.org/issues/420)
|
||||||
|
|
||||||
|
1.4.4 / 2014-09-12
|
||||||
|
==================
|
||||||
|
|
||||||
|
* badge version support 1.0.0-beta1. fixed [#440](https://github.com/cnpm/cnpmjs.org/issues/440)
|
||||||
|
|
||||||
|
1.4.3 / 2014-09-09
|
||||||
|
==================
|
||||||
|
|
||||||
|
* alias /:name/-/:file to /:name/download/:file. fixed [#439](https://github.com/cnpm/cnpmjs.org/issues/439)
|
||||||
|
|
||||||
|
1.4.2 / 2014-09-03
|
||||||
|
==================
|
||||||
|
|
||||||
|
* change default source registry to taobao's registry
|
||||||
|
* Merge pull request [#435](https://github.com/cnpm/cnpmjs.org/issues/435) from cnpm/bluebird
|
||||||
|
* add bluebird
|
||||||
|
* bump fs-cnpm
|
||||||
|
* Merge pull request [#434](https://github.com/cnpm/cnpmjs.org/issues/434) from cnpm/agent-stat
|
||||||
|
* show agent sockets stat. fixed [#433](https://github.com/cnpm/cnpmjs.org/issues/433)
|
||||||
|
* update readme
|
||||||
|
* remove pic in readme
|
||||||
|
|
||||||
|
1.4.1 / 2014-08-20
|
||||||
|
==================
|
||||||
|
|
||||||
|
* fix login error status
|
||||||
|
|
||||||
1.4.0 / 2014-08-20
|
1.4.0 / 2014-08-20
|
||||||
==================
|
==================
|
||||||
|
|
||||||
* different version, different color badge, add version badge. fixed #427
|
* different version, different color badge, add version badge. fixed [#427](https://github.com/cnpm/cnpmjs.org/issues/427)
|
||||||
* add download and node version badge
|
* add download and node version badge
|
||||||
|
|
||||||
1.3.2 / 2014-08-18
|
1.3.2 / 2014-08-18
|
||||||
|
|||||||
91
Makefile
91
Makefile
@@ -2,24 +2,38 @@ TESTS = $(shell ls -S `find test -type f -name "*.test.js" -print`)
|
|||||||
REPORTER = spec
|
REPORTER = spec
|
||||||
TIMEOUT = 30000
|
TIMEOUT = 30000
|
||||||
MOCHA_OPTS =
|
MOCHA_OPTS =
|
||||||
REGISTRY = --registry=https://registry.npm.taobao.org
|
DB = sqlite
|
||||||
|
DISTURL = http://npm.taobao.org/mirrors/iojs
|
||||||
|
BIN = iojs
|
||||||
|
|
||||||
|
ifeq ($(findstring io.js, $(shell which node)),)
|
||||||
|
BIN = node
|
||||||
|
DISTURL = http://npm.taobao.org/mirrors/node
|
||||||
|
endif
|
||||||
|
|
||||||
install:
|
install:
|
||||||
@npm install $(REGISTRY) \
|
@npm install --build-from-source --registry=http://registry.npm.taobao.org \
|
||||||
--disturl=https://npm.taobao.org/dist
|
--disturl=$(DISTURL)
|
||||||
|
|
||||||
|
install-production production:
|
||||||
|
@NODE_ENV=production $(MAKE) install
|
||||||
|
|
||||||
jshint: install
|
jshint: install
|
||||||
@-./node_modules/.bin/jshint ./
|
@-node_modules/.bin/jshint ./
|
||||||
|
|
||||||
pretest:
|
init-database:
|
||||||
|
@$(BIN) --harmony test/init_db.js
|
||||||
|
|
||||||
|
init-mysql:
|
||||||
@mysql -uroot -e 'DROP DATABASE IF EXISTS cnpmjs_test;'
|
@mysql -uroot -e 'DROP DATABASE IF EXISTS cnpmjs_test;'
|
||||||
@mysql -uroot -e 'CREATE DATABASE cnpmjs_test;'
|
@mysql -uroot -e 'CREATE DATABASE cnpmjs_test;'
|
||||||
@mysql -uroot 'cnpmjs_test' < ./docs/db.sql
|
|
||||||
@mysql -uroot 'cnpmjs_test' -e 'show tables;'
|
|
||||||
@rm -rf .tmp/dist
|
|
||||||
|
|
||||||
test: install pretest
|
init-pg:
|
||||||
@NODE_ENV=test ./node_modules/.bin/mocha \
|
@psql -c 'DROP DATABASE IF EXISTS cnpmjs_test;'
|
||||||
|
@psql -c 'CREATE DATABASE cnpmjs_test;'
|
||||||
|
|
||||||
|
test: install init-database
|
||||||
|
@NODE_ENV=test DB=${DB} node_modules/.bin/mocha \
|
||||||
--harmony \
|
--harmony \
|
||||||
--reporter $(REPORTER) \
|
--reporter $(REPORTER) \
|
||||||
--timeout $(TIMEOUT) \
|
--timeout $(TIMEOUT) \
|
||||||
@@ -30,10 +44,21 @@ test: install pretest
|
|||||||
$(MOCHA_OPTS) \
|
$(MOCHA_OPTS) \
|
||||||
$(TESTS)
|
$(TESTS)
|
||||||
|
|
||||||
test-cov cov: install pretest
|
test-sqlite:
|
||||||
@NODE_ENV=test node --harmony \
|
@$(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: install init-database
|
||||||
|
@NODE_ENV=test DB=${DB} $(BIN) --harmony \
|
||||||
node_modules/.bin/istanbul cover --preserve-comments \
|
node_modules/.bin/istanbul cover --preserve-comments \
|
||||||
./node_modules/.bin/_mocha \
|
node_modules/.bin/_mocha \
|
||||||
-- -u exports \
|
-- -u exports \
|
||||||
--reporter $(REPORTER) \
|
--reporter $(REPORTER) \
|
||||||
--timeout $(TIMEOUT) \
|
--timeout $(TIMEOUT) \
|
||||||
@@ -43,14 +68,20 @@ test-cov cov: install pretest
|
|||||||
--require ./test/init.js \
|
--require ./test/init.js \
|
||||||
$(MOCHA_OPTS) \
|
$(MOCHA_OPTS) \
|
||||||
$(TESTS)
|
$(TESTS)
|
||||||
@./node_modules/.bin/cov coverage
|
|
||||||
|
|
||||||
test-travis: install pretest
|
test-cov-sqlite:
|
||||||
@NODE_ENV=test node --harmony \
|
@$(MAKE) test-cov DB=sqlite
|
||||||
|
|
||||||
|
test-cov-mysql: init-mysql
|
||||||
|
@$(MAKE) test-cov DB=mysql
|
||||||
|
|
||||||
|
test-travis: install init-database
|
||||||
|
@NODE_ENV=test DB=${DB} CNPM_SOURCE_NPM=http://registry.npmjs.com CNPM_SOURCE_NPM_ISCNPM=false \
|
||||||
|
$(BIN) --harmony \
|
||||||
node_modules/.bin/istanbul cover --preserve-comments \
|
node_modules/.bin/istanbul cover --preserve-comments \
|
||||||
./node_modules/.bin/_mocha \
|
node_modules/.bin/_mocha \
|
||||||
--report lcovonly \
|
--report lcovonly \
|
||||||
-- \
|
-- -u exports \
|
||||||
--reporter dot \
|
--reporter dot \
|
||||||
--timeout $(TIMEOUT) \
|
--timeout $(TIMEOUT) \
|
||||||
--require should \
|
--require should \
|
||||||
@@ -60,11 +91,31 @@ test-travis: install pretest
|
|||||||
$(MOCHA_OPTS) \
|
$(MOCHA_OPTS) \
|
||||||
$(TESTS)
|
$(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: test-travis-sqlite test-travis-mysql test-travis-pg
|
||||||
|
|
||||||
|
dev:
|
||||||
|
@NODE_ENV=development $(BIN) node_modules/.bin/node-dev --harmony dispatch.js
|
||||||
|
|
||||||
contributors: install
|
contributors: install
|
||||||
@./node_modules/.bin/contributors -f plain -o AUTHORS
|
@node_modules/.bin/contributors -f plain -o AUTHORS
|
||||||
|
|
||||||
autod: install
|
autod: install
|
||||||
@./node_modules/.bin/autod -w -e public,view,docs,backup,coverage -k nodemailer --prefix "~"
|
@node_modules/.bin/autod -w \
|
||||||
|
--prefix "~" \
|
||||||
|
--exclude public,view,docs,backup,coverage \
|
||||||
|
--dep bluebird,mysql \
|
||||||
|
--keep should,supertest,should-http,chunkstream,mm,pedding
|
||||||
@$(MAKE) install
|
@$(MAKE) install
|
||||||
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
|
|||||||
66
README.md
66
README.md
@@ -8,6 +8,7 @@ cnpmjs.org
|
|||||||
[![David deps][david-image]][david-url]
|
[![David deps][david-image]][david-url]
|
||||||
[![node version][node-image]][node-url]
|
[![node version][node-image]][node-url]
|
||||||
[![npm download][download-image]][download-url]
|
[![npm download][download-image]][download-url]
|
||||||
|
[![gitter][gitter-image]][gitter-url]
|
||||||
|
|
||||||
[npm-image]: http://cnpmjs.org/badge/v/cnpmjs.org.svg?style=flat-square
|
[npm-image]: http://cnpmjs.org/badge/v/cnpmjs.org.svg?style=flat-square
|
||||||
[npm-url]: http://cnpmjs.org/package/cnpmjs.org
|
[npm-url]: http://cnpmjs.org/package/cnpmjs.org
|
||||||
@@ -23,6 +24,8 @@ cnpmjs.org
|
|||||||
[node-url]: http://nodejs.org/download/
|
[node-url]: http://nodejs.org/download/
|
||||||
[download-image]: https://img.shields.io/npm/dm/cnpmjs.org.svg?style=flat-square
|
[download-image]: https://img.shields.io/npm/dm/cnpmjs.org.svg?style=flat-square
|
||||||
[download-url]: https://npmjs.org/package/cnpmjs.org
|
[download-url]: https://npmjs.org/package/cnpmjs.org
|
||||||
|
[gitter-image]: https://badges.gitter.im/Join%20Chat.svg
|
||||||
|
[gitter-url]: https://gitter.im/cnpm/cnpmjs.org?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
@@ -39,12 +42,14 @@ Our goal is to provide a low cost maintenance and easy to use solution for priva
|
|||||||
* Build a mirror NPM. (we use it to build a mirror in China: [cnpmjs.org](http://cnpmjs.org/))
|
* 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.
|
* Build a completely independent NPM registry to store whatever you like.
|
||||||
|
|
||||||
### Features
|
## Features
|
||||||
|
|
||||||
* **Support "scoped" packages**: [npm/npm#5239](https://github.com/npm/npm/issues/5239)
|
* **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).
|
* **Simple to deploy**: only need `mysql` and a [simple store system](https://github.com/cnpm/cnpmjs.org/wiki/NFS-Guide).
|
||||||
You can get the source code through `npm` or `git`.
|
You can get the source code through `npm` or `git`.
|
||||||
* **Low cost and easy maintenance**: `package.json` info store in MySQL, tarball(tgz file) store in CDN or other store systems.
|
* **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:
|
* **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).
|
- Sync all modules from a specified registry, like [npm registry](http://registry.npmjs.org).
|
||||||
- Only sync the modules that exists in your own registry.
|
- Only sync the modules that exists in your own registry.
|
||||||
@@ -55,30 +60,32 @@ 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`,
|
* **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).
|
only need to change the registry in config. Even include manual synchronization (through `install` command).
|
||||||
* **Version badge**: base on [shields.io](http://shields.io/) 
|
* **Version badge**: base on [shields.io](http://shields.io/) 
|
||||||
|
* **Support http_proxy**: if you're behind firewall, need to request through http proxy
|
||||||
|
|
||||||
|
**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).
|
||||||
|
|
||||||
## Getting Start
|
## 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)
|
* @[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)
|
* install and deploy cnpmjs.org through npm: [examples](https://github.com/cnpm/custom-cnpm-example)
|
||||||
* Mirror NPM in China: [cnpmjs.org](http://cnpmjs.org)
|
* Mirror NPM in China: [cnpmjs.org](http://cnpmjs.org)
|
||||||
* cnpm client: [cnpm](https://github.com/cnpm/cnpm), `npm install -g cnpm`
|
* 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)
|
* [How to deploy cnpmjs.org](https://github.com/cnpm/cnpmjs.org/wiki/Deploy)
|
||||||
* [NFS guide](https://github.com/cnpm/cnpmjs.org/wiki/NFS-Guide)
|
* [Sync packages through `http_proxy`](https://github.com/cnpm/cnpmjs.org/wiki/Sync-packages-through-http_proxy)
|
||||||
|
* [wiki](https://github.com/cnpm/cnpmjs.org/wiki)
|
||||||

|
|
||||||
|
|
||||||
## Develop on your local machine
|
## Develop on your local machine
|
||||||
|
|
||||||
### Dependencies
|
### Dependencies
|
||||||
|
|
||||||
* [node](http://nodejs.org) =0.11.12
|
* [node](http://nodejs.org) >=0.11.12, use `--harmony`
|
||||||
* [mysql](http://dev.mysql.com/downloads/) >= 0.5.0, include `mysqld` and `mysql cli`. I test on `mysql@5.6.16`.
|
* Databases: only required one type
|
||||||
|
* [sqlite3](https://npm.taobao.org/package/sqlite3) >= 3.0.2, we use `sqlite3` by default
|
||||||
### Start MySQL
|
* [MySQL](http://dev.mysql.com/downloads/) >= 0.5.0, include `mysqld` and `mysql cli`. I test on `mysql@5.6.16`.
|
||||||
|
* MariaDB
|
||||||
```bash
|
* PostgreSQL
|
||||||
$ nohup mysqld &
|
|
||||||
```
|
|
||||||
|
|
||||||
### Clone codes and run test
|
### Clone codes and run test
|
||||||
|
|
||||||
@@ -95,11 +102,11 @@ $ make test
|
|||||||
# coverage
|
# coverage
|
||||||
$ make test-cov
|
$ make test-cov
|
||||||
|
|
||||||
# udpate dependencies
|
# update dependencies
|
||||||
$ make autod
|
$ make autod
|
||||||
|
|
||||||
# start server
|
# start server with development mode
|
||||||
$ node --harmony_generators dispatch.js
|
$ make dev
|
||||||
```
|
```
|
||||||
|
|
||||||
## How to contribute
|
## How to contribute
|
||||||
@@ -111,27 +118,10 @@ $ node --harmony_generators dispatch.js
|
|||||||
|
|
||||||
Tips: make sure your code is following the [node-style-guide](https://github.com/felixge/node-style-guide).
|
Tips: make sure your code is following the [node-style-guide](https://github.com/felixge/node-style-guide).
|
||||||
|
|
||||||
|
## Sponsors
|
||||||
|
|
||||||
|
- [](http://www.ucloud.cn/sdk?sem=sdk-CNPMJS)
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
(The MIT License)
|
MIT
|
||||||
|
|
||||||
Copyright(c) cnpmjs.org and other contributors.
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining
|
|
||||||
a copy of this software and associated documentation files (the
|
|
||||||
'Software'), to deal in the Software without restriction, including
|
|
||||||
without limitation the rights to use, copy, modify, merge, publish,
|
|
||||||
distribute, sublicense, and/or sell copies of the Software, and to
|
|
||||||
permit persons to whom the Software is furnished to do so, subject to
|
|
||||||
the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be
|
|
||||||
included in all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
||||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
||||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
||||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
||||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|||||||
142
backup/dump.js
142
backup/dump.js
@@ -1,142 +0,0 @@
|
|||||||
/**!
|
|
||||||
* cnpmjs.org - backup/dump.js
|
|
||||||
*
|
|
||||||
* Copyright(c) cnpmjs.org and other contributors.
|
|
||||||
* MIT Licensed
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 1. dump module
|
|
||||||
* 2. dump tag
|
|
||||||
* 3. dump user
|
|
||||||
* 4. total
|
|
||||||
* 5. download_total
|
|
||||||
*/
|
|
||||||
|
|
||||||
var path = require('path');
|
|
||||||
var fs = require('fs');
|
|
||||||
var moment = require('moment');
|
|
||||||
var eventproxy = require('eventproxy');
|
|
||||||
var util = require('util');
|
|
||||||
var zlib = require('zlib');
|
|
||||||
var mysql = require('../common/mysql');
|
|
||||||
var nfs = require('../common/nfs');
|
|
||||||
var config = require('../config');
|
|
||||||
|
|
||||||
function dumpTable(name, lastRow, callback) {
|
|
||||||
var sql = 'SELECT * from ' + name + ' WHERE gmt_modified >=? ORDER BY gmt_modified ASC LIMIT 10000;';
|
|
||||||
mysql.query(sql, [lastRow.gmt_modified], function (err, rows) {
|
|
||||||
if (err || rows.length === 0) {
|
|
||||||
return callback(err, rows);
|
|
||||||
}
|
|
||||||
if (rows[0].id === lastRow.id) {
|
|
||||||
rows = rows.slice(1);
|
|
||||||
}
|
|
||||||
callback(null, rows);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function log() {
|
|
||||||
var str = '[' + moment().format('YYYY-MM-DD HH:mm:ss') + '] ' + util.format.apply(util, arguments);
|
|
||||||
console.log(str);
|
|
||||||
}
|
|
||||||
|
|
||||||
function syncTable(name, callback) {
|
|
||||||
var datadir = __dirname;
|
|
||||||
var dataFile = path.join(datadir, moment().format('YYYY-MM-DD-HH') + '_' + name + '.json');
|
|
||||||
var lastRowFile = path.join(datadir, name + '_lastdate.json');
|
|
||||||
var lastRow = null;
|
|
||||||
if (fs.existsSync(lastRowFile)) {
|
|
||||||
lastRow = require(lastRowFile);
|
|
||||||
lastRow.gmt_modified = new Date(Date.parse(lastRow.gmt_modified));
|
|
||||||
} else {
|
|
||||||
lastRow = {
|
|
||||||
gmt_modified: new Date('2011-11-11'),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
log('getting "%s" since %j', name, lastRow);
|
|
||||||
dumpTable(name, lastRow, function (err, rows) {
|
|
||||||
console.log('[%s] got %d rows', Date(), rows && rows.length || 0);
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!rows || rows.length === 0) {
|
|
||||||
return callback();
|
|
||||||
}
|
|
||||||
|
|
||||||
var writeStream = fs.createWriteStream(dataFile, {flags: 'a'});
|
|
||||||
writeStream.once('error', callback);
|
|
||||||
for (var i = 0; i < rows.length; i++) {
|
|
||||||
writeStream.write(JSON.stringify(rows[i]) + '\n');
|
|
||||||
}
|
|
||||||
writeStream.end();
|
|
||||||
writeStream.on('finish', function () {
|
|
||||||
log('append %d rows to %s', rows.length, dataFile);
|
|
||||||
var gzfile = dataFile + '.gz';
|
|
||||||
var gzip = zlib.createGzip();
|
|
||||||
var inp = fs.createReadStream(dataFile);
|
|
||||||
var out = fs.createWriteStream(gzfile);
|
|
||||||
inp.pipe(gzip).pipe(out);
|
|
||||||
out.once('error', callback);
|
|
||||||
out.on('finish', function () {
|
|
||||||
var key = path.join(config.backupFilePrefix, path.basename(gzfile));
|
|
||||||
log('saving %s to %s ...', gzfile, key);
|
|
||||||
nfs.upload(gzfile, {key: key}, function (err, result) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
lastRow = rows[rows.length - 1];
|
|
||||||
lastRow = {gmt_modified: lastRow.gmt_modified, id: lastRow.id};
|
|
||||||
fs.writeFileSync(lastRowFile, JSON.stringify(lastRow));
|
|
||||||
log('save %s data file to %j, lastrow: %j', name, result, lastRow);
|
|
||||||
callback();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
var ep = eventproxy.create();
|
|
||||||
ep.fail(function (err) {
|
|
||||||
log('error: %s', err.stack);
|
|
||||||
process.exit(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
syncTable('module', ep.done('module'));
|
|
||||||
|
|
||||||
ep.on('module', function () {
|
|
||||||
syncTable('tag', ep.done('tag'));
|
|
||||||
});
|
|
||||||
|
|
||||||
ep.on('tag', function () {
|
|
||||||
syncTable('user', ep.done('user'));
|
|
||||||
});
|
|
||||||
|
|
||||||
ep.on('user', function () {
|
|
||||||
syncTable('total', ep.done('total'));
|
|
||||||
});
|
|
||||||
|
|
||||||
ep.on('total', function () {
|
|
||||||
syncTable('download_total', ep.done('download_total'));
|
|
||||||
});
|
|
||||||
|
|
||||||
ep.on('download_total', function () {
|
|
||||||
ep.emit('finish');
|
|
||||||
});
|
|
||||||
|
|
||||||
ep.on('finish', function () {
|
|
||||||
log('finish, %d process exit', process.pid);
|
|
||||||
process.exit(0);
|
|
||||||
});
|
|
||||||
131
bin/cli.js
Executable file
131
bin/cli.js
Executable file
@@ -0,0 +1,131 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
/**!
|
||||||
|
* cnpmjs.org - bin/cli.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
require('gnode');
|
||||||
|
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;
|
||||||
|
|
||||||
|
function list(val) {
|
||||||
|
return val.split(',');
|
||||||
|
}
|
||||||
|
|
||||||
|
program
|
||||||
|
.version(version);
|
||||||
|
|
||||||
|
program
|
||||||
|
.command('start')
|
||||||
|
.description('start cnpmjs.org server')
|
||||||
|
.option('--admins <admins>', 'set admins', list)
|
||||||
|
.option('--scopes <scopes>', 'set scopes', list)
|
||||||
|
// .option('--cluster', 'enable cluster mode')
|
||||||
|
.option('--dataDir <dataDir>', 'cnpmjs.org data dir, default is `$HOME/.cnpmjs.org`')
|
||||||
|
.action(start);
|
||||||
|
|
||||||
|
program
|
||||||
|
.command('stop')
|
||||||
|
.description('stop cnpmjs.org server')
|
||||||
|
.option('--dataDir <dataDir>', 'cnpmjs.org data dir, default is `$HOME/.cnpmjs.org`')
|
||||||
|
.action(stop);
|
||||||
|
|
||||||
|
program.parse(process.argv);
|
||||||
|
|
||||||
|
|
||||||
|
function start(options) {
|
||||||
|
stop(options);
|
||||||
|
var 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 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();
|
||||||
|
|
||||||
|
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');
|
||||||
|
if (fs.existsSync(pidfile)) {
|
||||||
|
var 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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function initDatabase() {
|
||||||
|
var models = require('../models');
|
||||||
|
|
||||||
|
models.sequelize.sync({ force: false })
|
||||||
|
.then(function () {
|
||||||
|
models.Total.init(function (err) {
|
||||||
|
if (err) {
|
||||||
|
console.error('[models/init_script.js] sequelize init fail');
|
||||||
|
console.error(err);
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
console.log('[models/init_script.js] `sqlite` sequelize sync and init success');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch(function (err) {
|
||||||
|
console.error('[models/init_script.js] sequelize sync fail');
|
||||||
|
console.error(err);
|
||||||
|
throw err;
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -52,9 +52,9 @@ stop()
|
|||||||
kill -15 $PID
|
kill -15 $PID
|
||||||
sleep 2
|
sleep 2
|
||||||
|
|
||||||
node_num=`ps -ef |grep node|grep ${PROJECT_NAME}|grep -v grep|wc -l`
|
node_num=`ps -ef | grep ${PROJECT_NAME} | grep -v grep | wc -l`
|
||||||
if [ $node_num != 0 ]; then
|
if [ $node_num != 0 ]; then
|
||||||
ps -ef |grep node | grep ${PROJECT_NAME} |grep -v grep|awk '{print $2}'|xargs kill -9
|
ps -ef | grep ${PROJECT_NAME} |grep -v grep|awk '{print $2}'|xargs kill -9
|
||||||
ipcs -s | grep 0x | awk '{print $2}' | xargs -n1 ipcrm -s > /dev/null 2>&1
|
ipcs -s | grep 0x | awk '{print $2}' | xargs -n1 ipcrm -s > /dev/null 2>&1
|
||||||
ipcs -m | grep 0x | awk '{print $2}' | xargs -n1 ipcrm -m > /dev/null 2>&1
|
ipcs -m | grep 0x | awk '{print $2}' | xargs -n1 ipcrm -m > /dev/null 2>&1
|
||||||
fi
|
fi
|
||||||
|
|||||||
@@ -1,74 +0,0 @@
|
|||||||
/**!
|
|
||||||
* cnpmjs.org - bin/restore_module_deps.js
|
|
||||||
*
|
|
||||||
* Copyright(c) 2014
|
|
||||||
* MIT Licensed
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
|
||||||
*/
|
|
||||||
|
|
||||||
"use strict";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var mysql = require('../common/mysql');
|
|
||||||
var Module = require('../proxy/module');
|
|
||||||
var ModuleDeps = require('../proxy/module_deps');
|
|
||||||
|
|
||||||
var addCount = 0;
|
|
||||||
|
|
||||||
function restore(id, callback) {
|
|
||||||
var sql = 'SELECT id, name, package FROM module WHERE id > ? ORDER BY id ASC LIMIT 1000';
|
|
||||||
mysql.query(sql, [id], function (err, rows) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
if (rows.length === 0) {
|
|
||||||
return callback(null, []);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('[%s] got %d rows', id, rows.length);
|
|
||||||
|
|
||||||
rows.forEach(function (r) {
|
|
||||||
Module.parseRow(r);
|
|
||||||
if (!r.package) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var deps = Object.keys(r.package.dependencies || {});
|
|
||||||
if (!Array.isArray(deps) || !deps.length) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
deps.forEach(function (dep) {
|
|
||||||
ModuleDeps.add(dep, r.name, function (err) {
|
|
||||||
// console.log('[%s] add %s <= %s, error: %s', id, dep, r.name, err);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
addCount += deps.length;
|
|
||||||
});
|
|
||||||
setTimeout(function () {
|
|
||||||
console.log('[%s] add %d relations', id, addCount);
|
|
||||||
callback(null, rows);
|
|
||||||
}, 1000);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
var id = 0;
|
|
||||||
function run() {
|
|
||||||
restore(id, function (err, rows) {
|
|
||||||
if (err) {
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
if (rows.length === 0) {
|
|
||||||
console.log('finished, last id: %s, exit.', id);
|
|
||||||
process.exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
id = rows[rows.length - 1].id;
|
|
||||||
run();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
run();
|
|
||||||
@@ -17,10 +17,10 @@
|
|||||||
|
|
||||||
var formater = require('error-formater');
|
var formater = require('error-formater');
|
||||||
var Logger = require('mini-logger');
|
var Logger = require('mini-logger');
|
||||||
var config = require('../config');
|
|
||||||
var utility = require('utility');
|
var utility = require('utility');
|
||||||
var mail = require('./mail');
|
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
|
var config = require('../config');
|
||||||
|
var mail = require('./mail');
|
||||||
|
|
||||||
var isTEST = process.env.NODE_ENV === 'test';
|
var isTEST = process.env.NODE_ENV === 'test';
|
||||||
var categories = ['sync_info', 'sync_error'];
|
var categories = ['sync_info', 'sync_error'];
|
||||||
|
|||||||
@@ -14,11 +14,32 @@
|
|||||||
* Module dependencies.
|
* Module dependencies.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var utility = require('utility');
|
|
||||||
var nodemailer = require('nodemailer');
|
var nodemailer = require('nodemailer');
|
||||||
|
var utility = require('utility');
|
||||||
var os = require('os');
|
var os = require('os');
|
||||||
var mailConfig = require('../config').mail;
|
var mailConfig = require('../config').mail;
|
||||||
|
|
||||||
|
var smtpConfig;
|
||||||
|
if (mailConfig.auth) {
|
||||||
|
// new style
|
||||||
|
smtpConfig = mailConfig;
|
||||||
|
} else {
|
||||||
|
smtpConfig = {
|
||||||
|
enable: mailConfig.enable,
|
||||||
|
// backward compat
|
||||||
|
host: mailConfig.host,
|
||||||
|
port: mailConfig.port,
|
||||||
|
secure: mailConfig.secure || mailConfig.ssl,
|
||||||
|
debug: mailConfig.debug,
|
||||||
|
auth: {
|
||||||
|
user: mailConfig.user,
|
||||||
|
pass: mailConfig.pass
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
var transport;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send notice email with mail level and appname.
|
* Send notice email with mail level and appname.
|
||||||
*
|
*
|
||||||
@@ -51,26 +72,23 @@ LEVELS.forEach(function (level) {
|
|||||||
exports.send = function (to, subject, html, callback) {
|
exports.send = function (to, subject, html, callback) {
|
||||||
callback = callback || utility.noop;
|
callback = callback || utility.noop;
|
||||||
|
|
||||||
var transport = nodemailer.createTransport("SMTP", {
|
if (mailConfig.enable === false) {
|
||||||
host: mailConfig.host,
|
console.log('[send mail debug] [%s] to: %s, subject: %s\n%s', Date(), to, subject, html);
|
||||||
port: mailConfig.port,
|
return callback();
|
||||||
secureConnection: mailConfig.ssl,
|
}
|
||||||
debug: mailConfig.debug,
|
|
||||||
auth: {
|
if (!transport) {
|
||||||
user: mailConfig.user,
|
transport = nodemailer.createTransport(smtpConfig);
|
||||||
pass: mailConfig.pass,
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
var message = {
|
var message = {
|
||||||
sender: mailConfig.sender,
|
from: mailConfig.from || mailConfig.sender,
|
||||||
to: to,
|
to: to,
|
||||||
subject: subject,
|
subject: subject,
|
||||||
html: html,
|
html: html,
|
||||||
};
|
};
|
||||||
|
|
||||||
transport.sendMail(message, function (err, result) {
|
transport.sendMail(message, function (err, result) {
|
||||||
transport.close();
|
|
||||||
callback(err, result);
|
callback(err, result);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
34
common/markdown.js
Normal file
34
common/markdown.js
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - common/markdown.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var xss = require('xss');
|
||||||
|
var MarkdownIt = require('markdown-it');
|
||||||
|
|
||||||
|
// allow class attr on code
|
||||||
|
xss.whiteList.code = ['class'];
|
||||||
|
|
||||||
|
var md = new MarkdownIt({
|
||||||
|
html: true,
|
||||||
|
linkify: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
exports.render = function (content, filterXss) {
|
||||||
|
var html = md.render(content);
|
||||||
|
if (filterXss !== false) {
|
||||||
|
html = xss(html);
|
||||||
|
}
|
||||||
|
return html;
|
||||||
|
};
|
||||||
@@ -1,80 +0,0 @@
|
|||||||
/*!
|
|
||||||
* cnpmjs.org - common/mysql.js
|
|
||||||
*
|
|
||||||
* Copyright(c) cnpmjs.org and other contributors.
|
|
||||||
* MIT Licensed
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* dead_horse <dead_horse@qq.com>
|
|
||||||
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var thunkify = require('thunkify-wrap');
|
|
||||||
var ready = require('ready');
|
|
||||||
var mysql = require('mysql');
|
|
||||||
var config = require('../config');
|
|
||||||
|
|
||||||
var server = config.mysqlServers[0];
|
|
||||||
|
|
||||||
// TODO: query timeout
|
|
||||||
var pool = mysql.createPool({
|
|
||||||
host: server.host,
|
|
||||||
port: server.port,
|
|
||||||
user: server.user,
|
|
||||||
password: server.password,
|
|
||||||
database: config.mysqlDatabase,
|
|
||||||
connectionLimit: config.mysqlMaxConnections,
|
|
||||||
multipleStatements: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
exports.pool = pool;
|
|
||||||
|
|
||||||
exports.query = function (sql, values, cb) {
|
|
||||||
if (typeof values === 'function') {
|
|
||||||
cb = values;
|
|
||||||
values = null;
|
|
||||||
}
|
|
||||||
pool.query(sql, values, function (err, rows) {
|
|
||||||
cb(err, rows);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.queryOne = function (sql, values, cb) {
|
|
||||||
if (typeof values === 'function') {
|
|
||||||
cb = values;
|
|
||||||
values = null;
|
|
||||||
}
|
|
||||||
exports.query(sql, values, function (err, rows) {
|
|
||||||
if (rows) {
|
|
||||||
rows = rows[0];
|
|
||||||
}
|
|
||||||
cb(err, rows);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.escape = function (val) {
|
|
||||||
return pool.escape(val);
|
|
||||||
};
|
|
||||||
|
|
||||||
ready(exports);
|
|
||||||
|
|
||||||
thunkify(exports);
|
|
||||||
|
|
||||||
function init() {
|
|
||||||
exports.query('show tables', function (err, rows) {
|
|
||||||
if (err) {
|
|
||||||
console.error('[%s] [worker:%s] mysql init error: %s', Date(), process.pid, err);
|
|
||||||
setTimeout(init, 1000);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
console.log('[%s] [worker:%s] mysql ready, got %d tables', Date(), process.pid, rows.length);
|
|
||||||
exports.ready(true);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
init();
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
/**!
|
|
||||||
* cnpmjs.org - common/redis.js
|
|
||||||
*
|
|
||||||
* Copyright(c) cnpmjs.org and other contributors.
|
|
||||||
* MIT Licensed
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var config = require('../config');
|
|
||||||
|
|
||||||
// close redis by set config.redis to `null` or `{}`
|
|
||||||
if (config.redis && config.redis.host && config.redis.port) {
|
|
||||||
|
|
||||||
var redis = require('redis');
|
|
||||||
var wrapper = require('co-redis');
|
|
||||||
var logger = require('./logger');
|
|
||||||
var _client = redis.createClient(config.redis.port, config.redis.host);
|
|
||||||
|
|
||||||
_client.on('error', function (err) {
|
|
||||||
logger.error(err);
|
|
||||||
});
|
|
||||||
|
|
||||||
module.exports = wrapper(_client);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
console.warn('[%s] [worker:%s:common/redis.js] Redis config can not found',
|
|
||||||
Date(), process.pid);
|
|
||||||
module.exports = null;
|
|
||||||
}
|
|
||||||
75
common/sequelize.js
Normal file
75
common/sequelize.js
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - common/sequelize.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var Sequelize = require('sequelize');
|
||||||
|
var DataTypes = require('sequelize/lib/data-types');
|
||||||
|
var config = require('../config');
|
||||||
|
|
||||||
|
if (config.mysqlServers && config.database.dialect === 'sqlite') {
|
||||||
|
// https://github.com/cnpm/cnpmjs.org/wiki/Migrating-from-1.x-to-2.x
|
||||||
|
// forward compat with old style on 1.x
|
||||||
|
// mysqlServers: [
|
||||||
|
// {
|
||||||
|
// host: '127.0.0.1',
|
||||||
|
// port: 3306,
|
||||||
|
// user: 'root',
|
||||||
|
// password: ''
|
||||||
|
// }
|
||||||
|
// ],
|
||||||
|
// mysqlDatabase: 'cnpmjs_test',
|
||||||
|
// mysqlMaxConnections: 4,
|
||||||
|
// 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];
|
||||||
|
config.database = {
|
||||||
|
db: config.mysqlDatabase,
|
||||||
|
username: server.user,
|
||||||
|
password: server.password,
|
||||||
|
dialect: 'mysql',
|
||||||
|
host: server.host,
|
||||||
|
port: server.port,
|
||||||
|
pool: {
|
||||||
|
maxConnections: config.mysqlMaxConnections || 10,
|
||||||
|
minConnections: 0,
|
||||||
|
maxIdleTime: 30000
|
||||||
|
},
|
||||||
|
logging: !!process.env.SQL_DEBUG,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
var database = config.database;
|
||||||
|
|
||||||
|
// sync database before app start, defaul is false
|
||||||
|
database.syncFirst = false;
|
||||||
|
|
||||||
|
// add longtext for mysql
|
||||||
|
Sequelize.LONGTEXT = DataTypes.LONGTEXT = DataTypes.TEXT;
|
||||||
|
if (config.dialect === 'mysql') {
|
||||||
|
Sequelize.LONGTEXT = DataTypes.LONGTEXT = 'LONGTEXT';
|
||||||
|
}
|
||||||
|
|
||||||
|
database.define = {
|
||||||
|
timestamps: true,
|
||||||
|
createdAt: 'gmt_create',
|
||||||
|
updatedAt: 'gmt_modified',
|
||||||
|
charset: 'utf8',
|
||||||
|
collate: 'utf8_general_ci',
|
||||||
|
};
|
||||||
|
|
||||||
|
var sequelize = new Sequelize(database.db, database.username, database.password, database);
|
||||||
|
|
||||||
|
module.exports = sequelize;
|
||||||
75
common/urllib.js
Normal file
75
common/urllib.js
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - common/urllib.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var urlparse = require('url').parse;
|
||||||
|
var urllib = require('urllib');
|
||||||
|
var HttpAgent = require('agentkeepalive');
|
||||||
|
var HttpsAgent = require('agentkeepalive').HttpsAgent;
|
||||||
|
var config = require('../config');
|
||||||
|
|
||||||
|
var httpAgent;
|
||||||
|
var httpsAgent;
|
||||||
|
|
||||||
|
if (config.httpProxy) {
|
||||||
|
var tunnel = require('tunnel-agent');
|
||||||
|
var urlinfo = urlparse(config.httpProxy);
|
||||||
|
if (urlinfo.protocol === 'http:') {
|
||||||
|
httpAgent = tunnel.httpOverHttp({
|
||||||
|
proxy: {
|
||||||
|
host: urlinfo.hostname,
|
||||||
|
port: urlinfo.port
|
||||||
|
}
|
||||||
|
});
|
||||||
|
httpsAgent = tunnel.httpsOverHttp({
|
||||||
|
proxy: {
|
||||||
|
host: urlinfo.hostname,
|
||||||
|
port: urlinfo.port
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else if (urlinfo.protocol === 'https:') {
|
||||||
|
httpAgent = tunnel.httpOverHttps({
|
||||||
|
proxy: {
|
||||||
|
host: urlinfo.hostname,
|
||||||
|
port: urlinfo.port
|
||||||
|
}
|
||||||
|
});
|
||||||
|
httpsAgent = tunnel.httpsOverHttps({
|
||||||
|
proxy: {
|
||||||
|
host: urlinfo.hostname,
|
||||||
|
port: urlinfo.port
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
throw new TypeError('httpProxy format error: ' + config.httpProxy);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
httpAgent = new HttpAgent({
|
||||||
|
timeout: 0,
|
||||||
|
keepAliveTimeout: 15000
|
||||||
|
});
|
||||||
|
httpsAgent = new HttpsAgent({
|
||||||
|
timeout: 0,
|
||||||
|
keepAliveTimeout: 15000
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
var client = urllib.create({
|
||||||
|
agent: httpAgent,
|
||||||
|
httpsAgent: httpsAgent
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = client;
|
||||||
|
module.exports.USER_AGENT = urllib.USER_AGENT;
|
||||||
168
config/index.js
168
config/index.js
@@ -6,7 +6,7 @@
|
|||||||
*
|
*
|
||||||
* Authors:
|
* Authors:
|
||||||
* dead_horse <dead_horse@qq.com>
|
* dead_horse <dead_horse@qq.com>
|
||||||
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
@@ -15,19 +15,18 @@
|
|||||||
* Module dependencies.
|
* Module dependencies.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
var mkdirp = require('mkdirp');
|
||||||
|
var copy = require('copy-to');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var os = require('os');
|
var os = require('os');
|
||||||
var mkdirp = require('mkdirp');
|
|
||||||
var copy = require('copy-to');
|
|
||||||
|
|
||||||
fs.existsSync = fs.existsSync || path.existsSync;
|
|
||||||
var version = require('../package.json').version;
|
var version = require('../package.json').version;
|
||||||
|
|
||||||
var root = path.dirname(__dirname);
|
var root = path.dirname(__dirname);
|
||||||
|
var dataDir = path.join(process.env.HOME || root, '.cnpmjs.org');
|
||||||
|
|
||||||
var config = {
|
var config = {
|
||||||
|
|
||||||
version: version,
|
version: version,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,6 +38,7 @@ var config = {
|
|||||||
/*
|
/*
|
||||||
* server configure
|
* server configure
|
||||||
*/
|
*/
|
||||||
|
|
||||||
registryPort: 7001,
|
registryPort: 7001,
|
||||||
webPort: 7002,
|
webPort: 7002,
|
||||||
bindingHost: '127.0.0.1', // only binding on 127.0.0.1 for local access
|
bindingHost: '127.0.0.1', // only binding on 127.0.0.1 for local access
|
||||||
@@ -46,15 +46,17 @@ var config = {
|
|||||||
// debug mode
|
// debug mode
|
||||||
// if in debug mode, some middleware like limit wont load
|
// if in debug mode, some middleware like limit wont load
|
||||||
// logger module will print to stdout
|
// logger module will print to stdout
|
||||||
debug: true,
|
debug: process.env.NODE_ENV === 'development',
|
||||||
|
// page mode, enable on development env
|
||||||
|
pagemock: process.env.NODE_ENV === 'development',
|
||||||
// session secret
|
// session secret
|
||||||
sessionSecret: 'cnpmjs.org test session secret',
|
sessionSecret: 'cnpmjs.org test session secret',
|
||||||
// max request json body size
|
// max request json body size
|
||||||
jsonLimit: '10mb',
|
jsonLimit: '10mb',
|
||||||
// log dir name
|
// log dir name
|
||||||
logdir: path.join(root, '.tmp', 'logs'),
|
logdir: path.join(dataDir, 'logs'),
|
||||||
// update file template dir
|
// update file template dir
|
||||||
uploadDir: path.join(root, '.dist'),
|
uploadDir: path.join(dataDir, 'downloads'),
|
||||||
// web page viewCache
|
// web page viewCache
|
||||||
viewCache: false,
|
viewCache: false,
|
||||||
|
|
||||||
@@ -78,23 +80,23 @@ var config = {
|
|||||||
fengmk2: 'fengmk2@gmail.com',
|
fengmk2: 'fengmk2@gmail.com',
|
||||||
admin: 'admin@cnpmjs.org',
|
admin: 'admin@cnpmjs.org',
|
||||||
dead_horse: 'dead_horse@qq.com',
|
dead_horse: 'dead_horse@qq.com',
|
||||||
cnpmjstest10: 'cnpmjstest10@cnpmjs.org',
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// email notification for errors
|
// email notification for errors
|
||||||
|
// check https://github.com/andris9/Nodemailer for more informations
|
||||||
mail: {
|
mail: {
|
||||||
|
enable: false,
|
||||||
appname: 'cnpmjs.org',
|
appname: 'cnpmjs.org',
|
||||||
sender: 'cnpmjs.org mail sender <adderss@gmail.com>',
|
from: 'cnpmjs.org mail sender <adderss@gmail.com>',
|
||||||
host: 'smtp.gmail.com',
|
service: 'gmail',
|
||||||
port: 465,
|
auth: {
|
||||||
user: 'address@gmail.com',
|
user: 'address@gmail.com',
|
||||||
pass: 'your password',
|
pass: 'your password'
|
||||||
ssl: true,
|
}
|
||||||
debug: false
|
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
logoURL: '//ww4.sinaimg.cn/large/69c1d4acgw1ebfly5kjlij208202oglr.jpg', // cnpm logo image url
|
logoURL: '//ww4.sinaimg.cn/large/69c1d4acgw1ebfly5kjlij208202oglr.jpg', // cnpm logo image url
|
||||||
|
adBanner: '',
|
||||||
customReadmeFile: '', // you can use your custom readme file instead the cnpm one
|
customReadmeFile: '', // you can use your custom readme file instead the cnpm one
|
||||||
customFooter: '', // you can add copyright and site total script html here
|
customFooter: '', // you can add copyright and site total script html here
|
||||||
npmClientName: 'cnpm', // use `${name} install package`
|
npmClientName: 'cnpm', // use `${name} install package`
|
||||||
@@ -106,79 +108,90 @@ var config = {
|
|||||||
backupFilePrefix: '/cnpm/backup/',
|
backupFilePrefix: '/cnpm/backup/',
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* mysql config
|
* database config
|
||||||
*/
|
*/
|
||||||
|
|
||||||
mysqlServers: [
|
database: {
|
||||||
{
|
db: 'cnpmjs_test',
|
||||||
host: '127.0.0.1',
|
username: 'root',
|
||||||
port: 3306,
|
password: '',
|
||||||
user: 'root',
|
|
||||||
password: ''
|
|
||||||
}
|
|
||||||
],
|
|
||||||
mysqlDatabase: 'cnpmjs_test',
|
|
||||||
mysqlMaxConnections: 4,
|
|
||||||
mysqlQueryTimeout: 5000,
|
|
||||||
|
|
||||||
|
// the sql dialect of the database
|
||||||
|
// - currently supported: 'mysql', 'sqlite', 'postgres', 'mariadb'
|
||||||
|
dialect: 'sqlite',
|
||||||
|
|
||||||
// redis config
|
// custom host; default: 127.0.0.1
|
||||||
// use for koa-limit module as storage
|
host: '127.0.0.1',
|
||||||
redis: null,
|
|
||||||
|
// 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
|
// package tarball store in local filesystem by default
|
||||||
nfs: require('fs-cnpm')({
|
nfs: require('fs-cnpm')({
|
||||||
dir: path.join(root, '.tmp', 'dist')
|
dir: path.join(dataDir, 'nfs')
|
||||||
}),
|
}),
|
||||||
|
// if set true, will 302 redirect to `nfs.url(dist.key)`
|
||||||
|
downloadRedirectToNFS: false,
|
||||||
|
|
||||||
// registry url name
|
// registry url name
|
||||||
registryHost: 'r.cnpmjs.org',
|
registryHost: 'r.cnpmjs.org',
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* registry mode config
|
* registry mode config
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// enable private mode, only admin can publish, other use just can sync package from source npm
|
// enable private mode or not
|
||||||
enablePrivate: true,
|
// 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
|
// registry scopes, if don't set, means do not support scopes
|
||||||
scopes: [
|
scopes: [ '@cnpm', '@cnpmtest', '@cnpm-test' ],
|
||||||
'@cnpm',
|
|
||||||
'@cnpmtest'
|
|
||||||
],
|
|
||||||
|
|
||||||
// redirect @cnpm/private-package => private-package
|
|
||||||
// forward compatbility for update from lower version cnpmjs.org
|
|
||||||
adaptScope: true,
|
|
||||||
|
|
||||||
// force user publish with scope
|
|
||||||
// but admins still can publish without scope
|
|
||||||
forcePublishWithScope: true,
|
|
||||||
|
|
||||||
// some registry already have some private packages in global scope
|
// some registry already have some private packages in global scope
|
||||||
// but we want to treat them as scoped private packages,
|
// but we want to treat them as scoped private packages,
|
||||||
// so you can use this white list.
|
// so you can use this white list.
|
||||||
privatePackages: ['private-package'],
|
privatePackages: [],
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sync configs
|
* sync configs
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// sync dist config
|
// the official npm registry
|
||||||
// sync node.js dist from nodejs.org
|
// cnpm wont directly sync from this one
|
||||||
noticeSyncDistError: true,
|
// but sometimes will request it for some package infomations
|
||||||
disturl: 'http://nodejs.org/dist',
|
// please don't change it if not necessary
|
||||||
syncDist: false,
|
officialNpmRegistry: 'https://registry.npmjs.org',
|
||||||
|
|
||||||
// sync source
|
// sync source, upstream registry
|
||||||
sourceNpmRegistry: 'http://registry.npmjs.org',
|
// If you want to directly sync from official npm's registry
|
||||||
|
// please drop them an email first
|
||||||
|
sourceNpmRegistry: 'http://registry.npm.taobao.org',
|
||||||
|
|
||||||
|
// upstream registry is base on cnpm/cnpmjs.org or not
|
||||||
|
// if your upstream is official npm registry, please turn it off
|
||||||
|
sourceNpmRegistryIsCNpm: true,
|
||||||
|
|
||||||
// if install return 404, try to sync from source registry
|
// if install return 404, try to sync from source registry
|
||||||
syncByInstall: true,
|
syncByInstall: true,
|
||||||
|
|
||||||
// sync mode select
|
// sync mode select
|
||||||
// none: do not sync any module
|
// none: do not sync any module, proxy all public modules from sourceNpmRegistry
|
||||||
// exist: only sync exist modules
|
// exist: only sync exist modules
|
||||||
// all: sync all modules
|
// all: sync all modules
|
||||||
syncModel: 'none', // 'none', 'all', 'exist'
|
syncModel: 'none', // 'none', 'all', 'exist'
|
||||||
@@ -187,14 +200,47 @@ var config = {
|
|||||||
// sync interval, default is 10 minutes
|
// sync interval, default is 10 minutes
|
||||||
syncInterval: '10m',
|
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,
|
||||||
|
|
||||||
// badge subject on http://shields.io/
|
// badge subject on http://shields.io/
|
||||||
badgeSubject: 'cnpm',
|
badgeSubject: 'cnpm',
|
||||||
|
|
||||||
|
// custom user service, @see https://github.com/cnpm/cnpmjs.org/wiki/Use-Your-Own-User-Authorization
|
||||||
|
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,
|
||||||
};
|
};
|
||||||
|
|
||||||
// load config/config.js, everything in config.js will cover the same key in index.js
|
if (process.env.NODE_ENV !== 'test') {
|
||||||
var customConfig = path.join(root, 'config/config.js');
|
var customConfig;
|
||||||
if (fs.existsSync(customConfig)) {
|
if (process.env.NODE_ENV === 'development') {
|
||||||
copy(require(customConfig)).override(config);
|
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.logdir);
|
||||||
|
|||||||
@@ -1,83 +0,0 @@
|
|||||||
/**!
|
|
||||||
* cnpmjs.org - controllers/download.js
|
|
||||||
*
|
|
||||||
* Copyright(c) cnpmjs.org and other contributors.
|
|
||||||
* MIT Licensed
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var thunkify = require('thunkify-wrap');
|
|
||||||
var moment = require('moment');
|
|
||||||
var DownloadTotal = require('../proxy/download');
|
|
||||||
|
|
||||||
exports.total = function (name, callback) {
|
|
||||||
if (typeof name === 'function') {
|
|
||||||
callback = name;
|
|
||||||
name = null;
|
|
||||||
}
|
|
||||||
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('week');
|
|
||||||
var lastweekEnd = lastweekStart.clone().endOf('week').format('YYYY-MM-DD');
|
|
||||||
var lastmonthEnd = start.clone().endOf('month').format('YYYY-MM-DD');
|
|
||||||
var thismonthStart = end.clone().startOf('month').format('YYYY-MM-DD');
|
|
||||||
var thisweekStart = end.clone().startOf('week').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];
|
|
||||||
if (name) {
|
|
||||||
args.unshift(name);
|
|
||||||
}
|
|
||||||
args.push(function (err, rows) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
var download = {
|
|
||||||
today: 0,
|
|
||||||
thisweek: 0,
|
|
||||||
thismonth: 0,
|
|
||||||
lastday: 0,
|
|
||||||
lastweek: 0,
|
|
||||||
lastmonth: 0,
|
|
||||||
};
|
|
||||||
for (var i = 0; i < rows.length; i++) {
|
|
||||||
var r = rows[i];
|
|
||||||
if (r.date === end) {
|
|
||||||
download.today += r.count;
|
|
||||||
}
|
|
||||||
if (r.date >= thismonthStart) {
|
|
||||||
download.thismonth += r.count;
|
|
||||||
}
|
|
||||||
if (r.date >= thisweekStart) {
|
|
||||||
download.thisweek += r.count;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (r.date === lastday) {
|
|
||||||
download.lastday += r.count;
|
|
||||||
}
|
|
||||||
if (r.date >= lastweekStart && r.date <= lastweekEnd) {
|
|
||||||
download.lastweek += r.count;
|
|
||||||
}
|
|
||||||
if (r.date >= start && r.date <= lastmonthEnd) {
|
|
||||||
download.lastmonth += r.count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
callback(null, download);
|
|
||||||
});
|
|
||||||
|
|
||||||
DownloadTotal[method].apply(DownloadTotal, args);
|
|
||||||
};
|
|
||||||
|
|
||||||
thunkify(exports);
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
|||||||
/**!
|
/**!
|
||||||
* cnpmjs.org - controllers/registry/deprecate.js
|
* cnpmjs.org - controllers/registry/package/deprecate.js
|
||||||
*
|
*
|
||||||
* Copyright(c) fengmk2 and other contributors.
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
* MIT Licensed
|
* MIT Licensed
|
||||||
@@ -14,20 +14,20 @@
|
|||||||
* Module dependencies.
|
* Module dependencies.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var Module = require('../../proxy/module');
|
var packageService = require('../../../services/package');
|
||||||
|
|
||||||
module.exports = deprecateVersions;
|
module.exports = deprecateVersions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see https://github.com/cnpm/cnpmjs.org/issues/415
|
* @see https://github.com/cnpm/cnpmjs.org/issues/415
|
||||||
*/
|
*/
|
||||||
function* deprecateVersions(next) {
|
function* deprecateVersions() {
|
||||||
var body = this.request.body;
|
var body = this.request.body;
|
||||||
var name = this.params.name || this.params[0];
|
var name = this.params.name || this.params[0];
|
||||||
|
|
||||||
var tasks = [];
|
var tasks = [];
|
||||||
for (var version in body.versions) {
|
for (var version in body.versions) {
|
||||||
tasks.push(Module.get(name, version));
|
tasks.push(packageService.getModule(name, version));
|
||||||
}
|
}
|
||||||
var rs = yield tasks;
|
var rs = yield tasks;
|
||||||
|
|
||||||
@@ -46,12 +46,12 @@ function* deprecateVersions(next) {
|
|||||||
var data = body.versions[row.package.version];
|
var data = body.versions[row.package.version];
|
||||||
if (typeof data.deprecated === 'string') {
|
if (typeof data.deprecated === 'string') {
|
||||||
row.package.deprecated = data.deprecated;
|
row.package.deprecated = data.deprecated;
|
||||||
updateTasks.push(Module.updatePackage(row.id, row.package));
|
updateTasks.push(packageService.updateModulePackage(row.id, row.package));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
yield updateTasks;
|
yield updateTasks;
|
||||||
// update last modified
|
// update last modified
|
||||||
yield* Module.updateLastModified(name);
|
yield* packageService.updateModuleLastModified(name);
|
||||||
|
|
||||||
this.status = 201;
|
this.status = 201;
|
||||||
this.body = {
|
this.body = {
|
||||||
84
controllers/registry/package/dist_tag.js
Normal file
84
controllers/registry/package/dist_tag.js
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - controllers/registry/package/dist_tag.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var packageService = require('../../../services/package');
|
||||||
|
|
||||||
|
function ok() {
|
||||||
|
return {
|
||||||
|
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];
|
||||||
|
tags[row.tag] = row.version;
|
||||||
|
}
|
||||||
|
this.body = tags;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 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];
|
||||||
|
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];
|
||||||
|
yield packageService.addModuleTag(name, tag, version);
|
||||||
|
}
|
||||||
|
this.status = 201;
|
||||||
|
this.body = ok();
|
||||||
|
};
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
// make sure version exists
|
||||||
|
var pkg = yield packageService.getModule(name, version);
|
||||||
|
if (!pkg) {
|
||||||
|
this.status = 400;
|
||||||
|
this.body = {
|
||||||
|
error: 'version_error',
|
||||||
|
reason: name + '@' + version + ' not exists'
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
yield packageService.addModuleTag(name, tag, version);
|
||||||
|
this.status = 201;
|
||||||
|
this.body = ok();
|
||||||
|
};
|
||||||
|
|
||||||
|
// 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];
|
||||||
|
yield packageService.removeModuleTagsByNames(name, tag);
|
||||||
|
this.body = ok();
|
||||||
|
};
|
||||||
113
controllers/registry/package/download.js
Normal file
113
controllers/registry/package/download.js
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - controllers/registry/package/download.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var debug = require('debug')('cnpmjs.org:controllers:registry:download');
|
||||||
|
var mime = require('mime');
|
||||||
|
var utility = require('utility');
|
||||||
|
var defer = require('co-defer');
|
||||||
|
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');
|
||||||
|
|
||||||
|
var _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);
|
||||||
|
var row = yield* packageService.getModule(name, version);
|
||||||
|
// can not get dist
|
||||||
|
var url = null;
|
||||||
|
|
||||||
|
if (typeof nfs.url === 'function') {
|
||||||
|
url = nfs.url(common.getCDNKey(name, filename));
|
||||||
|
}
|
||||||
|
|
||||||
|
debug('download %s %s %s %s', name, filename, version, url);
|
||||||
|
|
||||||
|
if (!row || !row.package || !row.package.dist) {
|
||||||
|
if (!url) {
|
||||||
|
return yield* next;
|
||||||
|
}
|
||||||
|
this.status = 302;
|
||||||
|
this.set('Location', url);
|
||||||
|
_downloads[name] = (_downloads[name] || 0) + 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_downloads[name] = (_downloads[name] || 0) + 1;
|
||||||
|
|
||||||
|
if (config.downloadRedirectToNFS && url) {
|
||||||
|
this.status = 302;
|
||||||
|
this.set('Location', url);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var dist = row.package.dist;
|
||||||
|
if (!dist.key) {
|
||||||
|
// try to use nsf.url() first
|
||||||
|
url = url || dist.tarball;
|
||||||
|
debug('get tarball by 302, url: %s', url);
|
||||||
|
this.status = 302;
|
||||||
|
this.set('Location', url);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// else use `dist.key` to get tarball from nfs
|
||||||
|
if (typeof dist.size === 'number' && dist.size > 0) {
|
||||||
|
this.length = dist.size;
|
||||||
|
}
|
||||||
|
this.type = mime.lookup(dist.key);
|
||||||
|
this.attachment(filename);
|
||||||
|
this.etag = dist.shasum;
|
||||||
|
|
||||||
|
this.body = yield* downloadAsReadStream(dist.key);
|
||||||
|
};
|
||||||
|
|
||||||
|
defer.setInterval(function* () {
|
||||||
|
// save download count
|
||||||
|
var totals = [];
|
||||||
|
for (var name in _downloads) {
|
||||||
|
var count = _downloads[name];
|
||||||
|
totals.push([name, count]);
|
||||||
|
}
|
||||||
|
_downloads = {};
|
||||||
|
|
||||||
|
if (totals.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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];
|
||||||
|
try {
|
||||||
|
yield* downloadTotalService.plusModuleTotal({ name: name, date: date, count: count });
|
||||||
|
} catch (err) {
|
||||||
|
err.message += '; name: ' + name + ', count: ' + count + ', date: ' + date;
|
||||||
|
logger.error(err);
|
||||||
|
// save back to _downloads, try again next time
|
||||||
|
_downloads[name] = (_downloads[name] || 0) + count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 5000);
|
||||||
80
controllers/registry/package/download_total.js
Normal file
80
controllers/registry/package/download_total.js
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
/*!
|
||||||
|
* cnpmjs.org - controllers/registry/package/download_total.js
|
||||||
|
*
|
||||||
|
* Copyright(c) cnpmjs.org and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* dead_horse <dead_horse@qq.com> (https://github.com/dead-horse)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var DownloadTotal = require('../../../services/download_total');
|
||||||
|
var DATE_REG = /^\d{4}-\d{2}-\d{2}$/;
|
||||||
|
|
||||||
|
module.exports = function* downloadTotal () {
|
||||||
|
var range = this.params.range;
|
||||||
|
var name = this.params.name;
|
||||||
|
|
||||||
|
range = range.split(':');
|
||||||
|
if (range.length !== 2
|
||||||
|
|| !range[0].match(DATE_REG)
|
||||||
|
|| !range[1].match(DATE_REG)) {
|
||||||
|
this.status = 400;
|
||||||
|
this.body = {
|
||||||
|
error: 'range_error',
|
||||||
|
reason: 'range must be YYYY-MM-DD:YYYY-MM-DD style'
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.body = name
|
||||||
|
? yield getPackageTotal(name, range[0], range[1])
|
||||||
|
: yield getTotal(range[0], range[1]);
|
||||||
|
};
|
||||||
|
|
||||||
|
function* getPackageTotal(name, start, end) {
|
||||||
|
var res = yield DownloadTotal.getModuleTotal(name, start, end);
|
||||||
|
var downloads = res.map(function (row) {
|
||||||
|
return {
|
||||||
|
day: row.date,
|
||||||
|
downloads: row.count
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
downloads.sort(function (a, b) {
|
||||||
|
return a.day > b.day ? 1 : -1;
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
downloads: downloads,
|
||||||
|
package: name,
|
||||||
|
start: start,
|
||||||
|
end: end
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function* getTotal(start, end) {
|
||||||
|
var res = yield DownloadTotal.getTotal(start, end);
|
||||||
|
var downloads = res.map(function (row) {
|
||||||
|
return {
|
||||||
|
day: row.date,
|
||||||
|
downloads: row.count
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
downloads.sort(function (a, b) {
|
||||||
|
return a.day > b.day ? 1 : -1;
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
downloads: downloads,
|
||||||
|
start: start,
|
||||||
|
end: end
|
||||||
|
};
|
||||||
|
}
|
||||||
203
controllers/registry/package/list.js
Normal file
203
controllers/registry/package/list.js
Normal file
@@ -0,0 +1,203 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - controllers/registry/package/list.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var debug = require('debug')('cnpmjs.org:controllers:registry:package:list');
|
||||||
|
var packageService = require('../../../services/package');
|
||||||
|
var common = require('../../../lib/common');
|
||||||
|
var SyncModuleWorker = require('../../sync_module_worker');
|
||||||
|
var config = require('../../../config');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* list all version of a module
|
||||||
|
* GET /:name
|
||||||
|
*/
|
||||||
|
module.exports = function* list() {
|
||||||
|
var orginalName = this.params.name || this.params[0];
|
||||||
|
var name = orginalName;
|
||||||
|
var rs = yield [
|
||||||
|
packageService.getModuleLastModified(name),
|
||||||
|
packageService.listModuleTags(name)
|
||||||
|
];
|
||||||
|
var modifiedTime = rs[0];
|
||||||
|
var tags = rs[1];
|
||||||
|
|
||||||
|
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];
|
||||||
|
if (tag.gmt_modified > modifiedTime) {
|
||||||
|
modifiedTime = tag.gmt_modified;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// use modifiedTime as etag
|
||||||
|
this.set('ETag', '"' + modifiedTime.getTime() + '"');
|
||||||
|
|
||||||
|
// must set status first
|
||||||
|
this.status = 200;
|
||||||
|
if (this.fresh) {
|
||||||
|
debug('%s not change at %s, 304 return', name, modifiedTime);
|
||||||
|
this.status = 304;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var r = yield [
|
||||||
|
packageService.listModulesByName(name),
|
||||||
|
packageService.listStarUserNames(name),
|
||||||
|
packageService.listMaintainers(name),
|
||||||
|
];
|
||||||
|
var rows = r[0];
|
||||||
|
var starUsers = r[1];
|
||||||
|
var 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];
|
||||||
|
if (starUser[0] !== '"' && starUser[0] !== "'") {
|
||||||
|
starUserMap[starUser] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
starUsers = starUserMap;
|
||||||
|
|
||||||
|
if (rows.length === 0) {
|
||||||
|
// check if unpublished
|
||||||
|
var unpublishedInfo = yield* packageService.getUnpublishedModule(name);
|
||||||
|
debug('show unpublished %j', unpublishedInfo);
|
||||||
|
if (unpublishedInfo) {
|
||||||
|
this.status = 404;
|
||||||
|
this.body = {
|
||||||
|
_id: orginalName,
|
||||||
|
name: orginalName,
|
||||||
|
time: {
|
||||||
|
modified: unpublishedInfo.package.time,
|
||||||
|
unpublished: unpublishedInfo.package,
|
||||||
|
},
|
||||||
|
_attachments: {}
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if module not exist in this registry,
|
||||||
|
// sync the module backend and return package info from official registry
|
||||||
|
if (rows.length === 0) {
|
||||||
|
if (!this.allowSync) {
|
||||||
|
this.status = 404;
|
||||||
|
this.body = {
|
||||||
|
error: 'not_found',
|
||||||
|
reason: 'document not found'
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// start sync
|
||||||
|
var 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;
|
||||||
|
// 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 times = {};
|
||||||
|
var attachments = {};
|
||||||
|
var createdTime = null;
|
||||||
|
for (var i = 0; i < rows.length; i++) {
|
||||||
|
var row = rows[i];
|
||||||
|
var pkg = row.package;
|
||||||
|
common.setDownloadURL(pkg, this);
|
||||||
|
pkg._cnpm_publish_time = row.publish_time;
|
||||||
|
versions[pkg.version] = pkg;
|
||||||
|
|
||||||
|
var t = times[pkg.version] = row.publish_time ? new Date(row.publish_time) : row.gmt_modified;
|
||||||
|
if ((!distTags.latest && !latestMod) || distTags.latest === pkg.version) {
|
||||||
|
latestMod = row;
|
||||||
|
readme = pkg.readme;
|
||||||
|
}
|
||||||
|
|
||||||
|
delete pkg.readme;
|
||||||
|
if (maintainers.length > 0) {
|
||||||
|
pkg.maintainers = maintainers;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!createdTime || t < createdTime) {
|
||||||
|
createdTime = t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (modifiedTime && createdTime) {
|
||||||
|
var ts = {
|
||||||
|
modified: modifiedTime,
|
||||||
|
created: createdTime,
|
||||||
|
};
|
||||||
|
for (var t in times) {
|
||||||
|
ts[t] = times[t];
|
||||||
|
}
|
||||||
|
times = ts;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!latestMod) {
|
||||||
|
latestMod = rows[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
var rev = String(latestMod.id);
|
||||||
|
var pkg = latestMod.package;
|
||||||
|
|
||||||
|
if (tags.length === 0) {
|
||||||
|
// some sync error reason, will cause tags missing
|
||||||
|
// set latest tag at least
|
||||||
|
distTags.latest = pkg.version;
|
||||||
|
}
|
||||||
|
|
||||||
|
var info = {
|
||||||
|
_id: orginalName,
|
||||||
|
_rev: rev,
|
||||||
|
name: orginalName,
|
||||||
|
description: pkg.description,
|
||||||
|
"dist-tags": distTags,
|
||||||
|
maintainers: pkg.maintainers,
|
||||||
|
time: times,
|
||||||
|
users: starUsers,
|
||||||
|
author: pkg.author,
|
||||||
|
repository: pkg.repository,
|
||||||
|
versions: versions,
|
||||||
|
readme: readme,
|
||||||
|
_attachments: attachments,
|
||||||
|
};
|
||||||
|
|
||||||
|
info.readmeFilename = pkg.readmeFilename;
|
||||||
|
info.homepage = pkg.homepage;
|
||||||
|
info.bugs = pkg.bugs;
|
||||||
|
info.license = pkg.license;
|
||||||
|
|
||||||
|
debug('show module %s: %s, latest: %s', orginalName, rev, latestMod.version);
|
||||||
|
this.body = info;
|
||||||
|
};
|
||||||
30
controllers/registry/package/list_all.js
Normal file
30
controllers/registry/package/list_all.js
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - controllers/registry/package/list_all.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var packageService = require('../../../services/package');
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
result[name] = true;
|
||||||
|
});
|
||||||
|
this.body = result;
|
||||||
|
};
|
||||||
23
controllers/registry/package/list_shorts.js
Normal file
23
controllers/registry/package/list_shorts.js
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - controllers/registry/package/list_shorts.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var packageService = require('../../../services/package');
|
||||||
|
|
||||||
|
// GET /-/short
|
||||||
|
// List all packages names only
|
||||||
|
module.exports = function* () {
|
||||||
|
this.body = yield* packageService.listAllPublicModuleNames();
|
||||||
|
};
|
||||||
59
controllers/registry/package/list_since.js
Normal file
59
controllers/registry/package/list_since.js
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - controllers/registry/package/list_since.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var packageService = require('../../../services/package');
|
||||||
|
|
||||||
|
var A_WEEK_MS = 3600000 * 24 * 7;
|
||||||
|
|
||||||
|
// 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* () {
|
||||||
|
var query = this.query;
|
||||||
|
if (query.stale !== 'update_after') {
|
||||||
|
this.status = 400;
|
||||||
|
this.body = {
|
||||||
|
error: 'query_parse_error',
|
||||||
|
reason: 'Invalid value for `stale`.'
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var startkey = Number(query.startkey);
|
||||||
|
if (!startkey) {
|
||||||
|
this.status = 400;
|
||||||
|
this.body = {
|
||||||
|
error: 'query_parse_error',
|
||||||
|
reason: 'Invalid value for `startkey`.'
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var updated = Date.now();
|
||||||
|
if (updated - startkey > A_WEEK_MS) {
|
||||||
|
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) {
|
||||||
|
result[name] = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
this.body = result;
|
||||||
|
};
|
||||||
67
controllers/registry/package/remove.js
Normal file
67
controllers/registry/package/remove.js
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
/**!
|
||||||
|
* 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';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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');
|
||||||
|
|
||||||
|
// 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];
|
||||||
|
debug('remove all the module with name: %s, id: %s', name, rev);
|
||||||
|
|
||||||
|
var mods = yield* packageService.listModulesByName(name);
|
||||||
|
debug('removeAll module %s: %d', name, mods.length);
|
||||||
|
var mod = mods[0];
|
||||||
|
if (!mod) {
|
||||||
|
return yield* next;
|
||||||
|
}
|
||||||
|
|
||||||
|
yield [
|
||||||
|
packageService.removeModulesByName(name),
|
||||||
|
packageService.removeModuleTags(name),
|
||||||
|
totalService.plusDeleteModule(),
|
||||||
|
];
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
yield keys.map(function (key) {
|
||||||
|
return nfs.remove(key);
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove the maintainers
|
||||||
|
yield* packageService.removeAllMaintainers(name);
|
||||||
|
|
||||||
|
this.body = { ok: true };
|
||||||
|
};
|
||||||
74
controllers/registry/package/remove_version.js
Normal file
74
controllers/registry/package/remove_version.js
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
/**!
|
||||||
|
* 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';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
// 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]);
|
||||||
|
// cnpmjs.org-2.0.0.tgz
|
||||||
|
var version = filename.split(name + '-')[1];
|
||||||
|
if (version) {
|
||||||
|
// 2.0.0.tgz
|
||||||
|
version = version.substring(0, version.lastIndexOf('.tgz'));
|
||||||
|
}
|
||||||
|
if (!version) {
|
||||||
|
return yield* next;
|
||||||
|
}
|
||||||
|
|
||||||
|
debug('remove tarball with filename: %s, version: %s, revert to => rev id: %s', filename, version, id);
|
||||||
|
|
||||||
|
if (isNaN(id)) {
|
||||||
|
return yield* next;
|
||||||
|
}
|
||||||
|
|
||||||
|
var rs = yield [
|
||||||
|
packageService.getModuleById(id),
|
||||||
|
packageService.getModule(name, version),
|
||||||
|
];
|
||||||
|
var revertTo = rs[0];
|
||||||
|
var mod = rs[1]; // module need to delete
|
||||||
|
if (!mod || mod.name !== name) {
|
||||||
|
return yield* next;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
// remove version from table
|
||||||
|
yield* packageService.removeModulesByNameAndVersions(name, [version]);
|
||||||
|
debug('removed %s@%s', name, version);
|
||||||
|
this.body = { ok: true };
|
||||||
|
};
|
||||||
237
controllers/registry/package/save.js
Normal file
237
controllers/registry/package/save.js
Normal file
@@ -0,0 +1,237 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - controllers/registry/package/save.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var debug = require('debug')('cnpmjs.org:controllers:registry:package: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');
|
||||||
|
|
||||||
|
// old flows:
|
||||||
|
// 1. add()
|
||||||
|
// 2. upload()
|
||||||
|
// 3. updateLatest()
|
||||||
|
//
|
||||||
|
// new flows: only one request
|
||||||
|
// PUT /:name
|
||||||
|
// https://github.com/npm/npm-registry-client/blob/master/lib/publish.js#L84
|
||||||
|
module.exports = function* save(next) {
|
||||||
|
// 'dist-tags': { latest: '0.0.2' },
|
||||||
|
// _attachments:
|
||||||
|
// { 'nae-sandbox-0.0.2.tgz':
|
||||||
|
// { 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];
|
||||||
|
if (!version) {
|
||||||
|
this.status = 400;
|
||||||
|
this.body = {
|
||||||
|
error: 'version_error',
|
||||||
|
reason: 'package.versions is empty'
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check maintainers
|
||||||
|
var result = yield* packageService.authMaintainer(name, username);
|
||||||
|
if (!result.isMaintainer) {
|
||||||
|
this.status = 403;
|
||||||
|
this.body = {
|
||||||
|
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];
|
||||||
|
if (typeof row.deprecated === 'string') {
|
||||||
|
hasDeprecated = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (hasDeprecated) {
|
||||||
|
return yield* deprecateVersions.call(this, next);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.status = 400;
|
||||||
|
this.body = {
|
||||||
|
error: 'attachment_error',
|
||||||
|
reason: 'package._attachments is empty'
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var attachment = pkg._attachments[filename];
|
||||||
|
var versionPackage = pkg.versions[version];
|
||||||
|
var maintainers = versionPackage.maintainers;
|
||||||
|
|
||||||
|
// should never happened in normal request
|
||||||
|
if (!maintainers) {
|
||||||
|
this.status = 400;
|
||||||
|
this.body = {
|
||||||
|
error: 'maintainers error',
|
||||||
|
reason: 'request body need maintainers'
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// notice that admins can not publish to all modules
|
||||||
|
// (but admins can add self to maintainers first)
|
||||||
|
|
||||||
|
// make sure user in auth is in maintainers
|
||||||
|
// should never happened in normal request
|
||||||
|
var m = maintainers.filter(function (maintainer) {
|
||||||
|
return maintainer.name === username;
|
||||||
|
});
|
||||||
|
if (m.length === 0) {
|
||||||
|
this.status = 403;
|
||||||
|
this.body = {
|
||||||
|
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]]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tags.length === 0) {
|
||||||
|
this.status = 400;
|
||||||
|
this.body = {
|
||||||
|
error: 'invalid',
|
||||||
|
reason: 'dist-tags should not be empty'
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
if (exists) {
|
||||||
|
this.status = 403;
|
||||||
|
this.body = {
|
||||||
|
error: 'forbidden',
|
||||||
|
reason: 'cannot modify pre-existing version: ' + version
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// upload attachment
|
||||||
|
var tarballBuffer;
|
||||||
|
tarballBuffer = new Buffer(attachment.data, 'base64');
|
||||||
|
|
||||||
|
if (tarballBuffer.length !== attachment.length) {
|
||||||
|
this.status = 403;
|
||||||
|
this.body = {
|
||||||
|
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');
|
||||||
|
if (!latest) {
|
||||||
|
// auto add latest
|
||||||
|
tags.push(['latest', tags[0][1]]);
|
||||||
|
debug('auto add latest tag: %j', tags);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
shasum = crypto.createHash('sha1');
|
||||||
|
shasum.update(tarballBuffer);
|
||||||
|
shasum = shasum.digest('hex');
|
||||||
|
|
||||||
|
var options = {
|
||||||
|
key: common.getCDNKey(name, filename),
|
||||||
|
shasum: shasum
|
||||||
|
};
|
||||||
|
var uploadResult = yield nfs.uploadBuffer(tarballBuffer, options);
|
||||||
|
debug('upload %j', uploadResult);
|
||||||
|
|
||||||
|
var dist = {
|
||||||
|
shasum: shasum,
|
||||||
|
size: attachment.length
|
||||||
|
};
|
||||||
|
|
||||||
|
// if nfs upload return a key, record it
|
||||||
|
if (uploadResult.url) {
|
||||||
|
dist.tarball = uploadResult.url;
|
||||||
|
} else if (uploadResult.key) {
|
||||||
|
dist.key = uploadResult.key;
|
||||||
|
dist.tarball = uploadResult.key;
|
||||||
|
}
|
||||||
|
|
||||||
|
var mod = {
|
||||||
|
name: name,
|
||||||
|
version: version,
|
||||||
|
author: username,
|
||||||
|
package: versionPackage
|
||||||
|
};
|
||||||
|
|
||||||
|
mod.package.dist = dist;
|
||||||
|
yield* addDepsRelations(mod.package);
|
||||||
|
|
||||||
|
var 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) {
|
||||||
|
// tag: [tagName, version]
|
||||||
|
return packageService.addModuleTag(name, tag[0], tag[1]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensure maintainers exists
|
||||||
|
var maintainerNames = maintainers.map(function (item) {
|
||||||
|
return item.name;
|
||||||
|
});
|
||||||
|
yield* packageService.addPrivateModuleMaintainers(name, maintainerNames);
|
||||||
|
|
||||||
|
this.status = 201;
|
||||||
|
this.body = {
|
||||||
|
ok: true,
|
||||||
|
rev: String(addResult.id)
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
function* addDepsRelations(pkg) {
|
||||||
|
var dependencies = Object.keys(pkg.dependencies || {});
|
||||||
|
if (dependencies.length > config.maxDependencies) {
|
||||||
|
dependencies = dependencies.slice(0, config.maxDependencies);
|
||||||
|
}
|
||||||
|
yield packageService.addDependencies(pkg.name, dependencies);
|
||||||
|
}
|
||||||
68
controllers/registry/package/show.js
Normal file
68
controllers/registry/package/show.js
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.js - controllers/registry/package/show.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var debug = require('debug')('cnpmjs.org:controllers:registry:package:show');
|
||||||
|
var semver = require('semver');
|
||||||
|
var packageService = require('../../../services/package');
|
||||||
|
var setDownloadURL = require('../../../lib/common').setDownloadURL;
|
||||||
|
var SyncModuleWorker = require('../../sync_module_worker');
|
||||||
|
var config = require('../../../config');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [deprecate] api
|
||||||
|
*
|
||||||
|
* get the special version or tag of a module
|
||||||
|
*
|
||||||
|
* GET /:name/:version
|
||||||
|
* GET /:name/:tag
|
||||||
|
*/
|
||||||
|
module.exports = function* show() {
|
||||||
|
var name = this.params.name || this.params[0];
|
||||||
|
var tag = this.params.version || this.params[1];
|
||||||
|
var version = semver.valid(tag);
|
||||||
|
var mod;
|
||||||
|
if (version) {
|
||||||
|
mod = yield* packageService.getModule(name, version);
|
||||||
|
} else {
|
||||||
|
mod = yield* packageService.getModuleByTag(name, tag);
|
||||||
|
}
|
||||||
|
if (mod) {
|
||||||
|
setDownloadURL(mod.package, this);
|
||||||
|
mod.package._cnpm_publish_time = mod.publish_time;
|
||||||
|
var maintainers = yield* packageService.listMaintainers(name);
|
||||||
|
if (maintainers.length > 0) {
|
||||||
|
mod.package.maintainers = maintainers;
|
||||||
|
}
|
||||||
|
this.body = mod.package;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if not fond, sync from source registry
|
||||||
|
if (!this.allowSync) {
|
||||||
|
this.status = 404;
|
||||||
|
this.body = {
|
||||||
|
error: 'not exist',
|
||||||
|
reason: 'version not found: ' + version
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// start sync
|
||||||
|
var logId = yield* SyncModuleWorker.sync(name, 'sync-by-install');
|
||||||
|
debug('start sync %s, get log id %s', name, logId);
|
||||||
|
|
||||||
|
this.redirect(config.officialNpmRegistry + this.url);
|
||||||
|
};
|
||||||
69
controllers/registry/package/tag.js
Normal file
69
controllers/registry/package/tag.js
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - controllers/registry/package/tag.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var debug = require('debug')('cnpmjs.org:controllers:registry:package:tag');
|
||||||
|
var semver = require('semver');
|
||||||
|
var util = require('util');
|
||||||
|
var 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];
|
||||||
|
debug('tag %j to %s/%s', version, name, tag);
|
||||||
|
|
||||||
|
if (!version) {
|
||||||
|
this.status = 400;
|
||||||
|
this.body = {
|
||||||
|
error: 'version_missed',
|
||||||
|
reason: 'version not found'
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!semver.valid(version)) {
|
||||||
|
this.status = 403;
|
||||||
|
var reason = util.format('setting tag %s to invalid version: %s: %s/%s',
|
||||||
|
tag, version, name, tag);
|
||||||
|
this.body = {
|
||||||
|
error: 'forbidden',
|
||||||
|
reason: reason
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var mod = yield* packageService.getModule(name, version);
|
||||||
|
if (!mod) {
|
||||||
|
this.status = 403;
|
||||||
|
var reason = util.format('setting tag %s to unknown version: %s: %s/%s',
|
||||||
|
tag, version, name, tag);
|
||||||
|
this.body = {
|
||||||
|
error: 'forbidden',
|
||||||
|
reason: reason
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var row = yield* packageService.addModuleTag(name, tag, version);
|
||||||
|
this.status = 201;
|
||||||
|
this.body = {
|
||||||
|
ok: true,
|
||||||
|
modified: row.gmt_modified
|
||||||
|
};
|
||||||
|
};
|
||||||
180
controllers/registry/package/update.js
Normal file
180
controllers/registry/package/update.js
Normal file
@@ -0,0 +1,180 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - controllers/registry/package/update.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var debug = require('debug')('cnpmjs.org:controllers:registry:package:update');
|
||||||
|
var packageService = require('../../../services/package');
|
||||||
|
var userService = require('../../../services/user');
|
||||||
|
var 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];
|
||||||
|
debug('update module %s, %s, %j', this.url, name, this.request.body);
|
||||||
|
|
||||||
|
var body = this.request.body;
|
||||||
|
if (body.versions) {
|
||||||
|
yield* updateVersions.call(this, next);
|
||||||
|
} else if (body.maintainers) {
|
||||||
|
yield* updateMaintainers.call(this, next);
|
||||||
|
} else {
|
||||||
|
yield* 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];
|
||||||
|
// left versions
|
||||||
|
var versions = this.request.body.versions;
|
||||||
|
|
||||||
|
// step1: list all the versions
|
||||||
|
var 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) {
|
||||||
|
return yield* next;
|
||||||
|
}
|
||||||
|
|
||||||
|
// step3: calculate which versions need to remove and
|
||||||
|
// which versions need to remain
|
||||||
|
var removeVersions = [];
|
||||||
|
var removeVersionMaps = {};
|
||||||
|
var remainVersions = [];
|
||||||
|
|
||||||
|
for (var i = 0; i < mods.length; i++) {
|
||||||
|
var v = mods[i].version;
|
||||||
|
if (!versions[v]) {
|
||||||
|
removeVersions.push(v);
|
||||||
|
removeVersionMaps[v] = true;
|
||||||
|
} else {
|
||||||
|
remainVersions.push(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!removeVersions.length) {
|
||||||
|
debug('no versions need to remove');
|
||||||
|
this.status = 201;
|
||||||
|
this.body = { ok: true };
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
debug('remove versions: %j, remain versions: %j', removeVersions, remainVersions);
|
||||||
|
|
||||||
|
// step 4: remove all the versions which need to remove
|
||||||
|
// let removeTar do remove versions from module table
|
||||||
|
var tags = yield* packageService.listModuleTags(name);
|
||||||
|
|
||||||
|
var removeTags = [];
|
||||||
|
var latestRemoved = false;
|
||||||
|
tags.forEach(function (tag) {
|
||||||
|
// this tag need be removed
|
||||||
|
if (removeVersionMaps[tag.version]) {
|
||||||
|
removeTags.push(tag.id);
|
||||||
|
if (tag.tag === 'latest') {
|
||||||
|
latestRemoved = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
debug('remove tags: %j', removeTags);
|
||||||
|
if (removeTags.length) {
|
||||||
|
// step 5: remove all the tags
|
||||||
|
yield* packageService.removeModuleTagsByIds(removeTags);
|
||||||
|
if (latestRemoved && remainVersions[0]) {
|
||||||
|
debug('latest tags removed, generate a new latest tag with new version: %s',
|
||||||
|
remainVersions[0]);
|
||||||
|
// step 6: insert new latest tag
|
||||||
|
yield* packageService.addModuleTag(name, 'latest', remainVersions[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// step 7: update last modified, make sure etag change
|
||||||
|
yield* packageService.updateModuleLastModified(name);
|
||||||
|
|
||||||
|
this.status = 201;
|
||||||
|
this.body = { ok: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
function* updateMaintainers() {
|
||||||
|
var name = this.params.name || this.params[0];
|
||||||
|
var body = this.request.body;
|
||||||
|
debug('updateMaintainers module %s, %j', name, body);
|
||||||
|
|
||||||
|
var usernames = body.maintainers.map(function (user) {
|
||||||
|
return user.name;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (usernames.length === 0) {
|
||||||
|
this.status = 403;
|
||||||
|
this.body = {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
for (var i = 0; i < usernames.length; i++) {
|
||||||
|
var username = usernames[i];
|
||||||
|
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];
|
||||||
|
map[user.login] = 1;
|
||||||
|
}
|
||||||
|
var invailds = [];
|
||||||
|
for (var i = 0; i < newNames.length; i++) {
|
||||||
|
var username = newNames[i];
|
||||||
|
if (map[username] !== 1) {
|
||||||
|
invailds.push(username);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (invailds.length > 0) {
|
||||||
|
this.status = 403;
|
||||||
|
this.body = {
|
||||||
|
error: 'invalid user name',
|
||||||
|
reason: 'User: `' + invailds.join(', ') + '` not exists'
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var r = yield* packageService.updatePrivateModuleMaintainers(name, usernames);
|
||||||
|
debug('result: %j', r);
|
||||||
|
|
||||||
|
this.status = 201;
|
||||||
|
this.body = {
|
||||||
|
ok: true,
|
||||||
|
id: name,
|
||||||
|
rev: this.params.rev || this.params[1],
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,291 +0,0 @@
|
|||||||
/**!
|
|
||||||
* cnpmjs.org - controllers/registry/user.js
|
|
||||||
*
|
|
||||||
* Copyright(c) cnpmjs.org and other contributors.
|
|
||||||
* MIT Licensed
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
|
|
||||||
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var debug = require('debug')('cnpmjs.org:controllers:registry:user');
|
|
||||||
var utility = require('utility');
|
|
||||||
var crypto = require('crypto');
|
|
||||||
var UserService = require('../../services/user');
|
|
||||||
var User = require('../../proxy/user');
|
|
||||||
var config = require('../../config');
|
|
||||||
var common = require('../../lib/common');
|
|
||||||
|
|
||||||
exports.show = function* (next) {
|
|
||||||
var name = this.params.name;
|
|
||||||
var isAdmin = common.isAdmin(name);
|
|
||||||
var scopes = config.scopes || [];
|
|
||||||
if (config.customUserService) {
|
|
||||||
var customUser = yield* UserService.get(name);
|
|
||||||
if (customUser) {
|
|
||||||
isAdmin = !!customUser.site_admin;
|
|
||||||
scopes = customUser.scopes;
|
|
||||||
|
|
||||||
var data = {
|
|
||||||
user: customUser
|
|
||||||
};
|
|
||||||
yield* User.saveCustomUser(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var user = yield* User.get(name);
|
|
||||||
if (!user) {
|
|
||||||
return yield* next;
|
|
||||||
}
|
|
||||||
|
|
||||||
var data = user.json;
|
|
||||||
if (!data) {
|
|
||||||
data = {
|
|
||||||
_id: 'org.couchdb.user:' + user.name,
|
|
||||||
_rev: user.rev,
|
|
||||||
name: user.name,
|
|
||||||
email: user.email,
|
|
||||||
type: 'user',
|
|
||||||
roles: [],
|
|
||||||
date: user.gmt_modified,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.login) {
|
|
||||||
// custom user format
|
|
||||||
// convert to npm user format
|
|
||||||
data = {
|
|
||||||
_id: 'org.couchdb.user:' + user.name,
|
|
||||||
_rev: user.rev,
|
|
||||||
name: user.name,
|
|
||||||
email: user.email,
|
|
||||||
type: 'user',
|
|
||||||
roles: [],
|
|
||||||
date: user.gmt_modified,
|
|
||||||
avatar: data.avatar_url,
|
|
||||||
fullname: data.name || data.login,
|
|
||||||
homepage: data.html_url,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
data._cnpm_meta = {
|
|
||||||
id: user.id,
|
|
||||||
npm_user: user.npm_user === 1,
|
|
||||||
custom_user: user.npm_user === 2,
|
|
||||||
gmt_create: user.gmt_create,
|
|
||||||
gmt_modified: user.gmt_modified,
|
|
||||||
admin: isAdmin,
|
|
||||||
scopes: scopes,
|
|
||||||
};
|
|
||||||
|
|
||||||
this.body = data;
|
|
||||||
};
|
|
||||||
|
|
||||||
function ensurePasswordSalt(user, body) {
|
|
||||||
if (!user.password_sha && body.password) {
|
|
||||||
// create password_sha on server
|
|
||||||
user.salt = crypto.randomBytes(30).toString('hex');
|
|
||||||
user.password_sha = utility.sha1(body.password + user.salt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// npm 1.4.4
|
|
||||||
// add new user first
|
|
||||||
// @see https://github.com/npm/npm-registry-client/commit/effb4bc88d443f764f2c2e8b4dd583cc72cf6084
|
|
||||||
// PUT /-/user/org.couchdb.user:mk2 { accept: 'application/json',
|
|
||||||
// 'accept-encoding': 'gzip',
|
|
||||||
// 'user-agent': 'node/v0.11.12 darwin x64',
|
|
||||||
// host: '127.0.0.1:7001',
|
|
||||||
// 'content-type': 'application/json',
|
|
||||||
// 'content-length': '150',
|
|
||||||
// connection: 'close' } { name: 'mk2',
|
|
||||||
// password: '123456',
|
|
||||||
// email: 'fengmk2@gmail.com',
|
|
||||||
// _id: 'org.couchdb.user:mk2',
|
|
||||||
// type: 'user',
|
|
||||||
// roles: [],
|
|
||||||
// date: '2014-03-15T02:33:19.465Z' }
|
|
||||||
|
|
||||||
// old npm flow
|
|
||||||
// json:
|
|
||||||
// PUT /-/user/org.couchdb.user:mk2 { accept: 'application/json',
|
|
||||||
// 'user-agent': 'node/v0.8.26 darwin x64',
|
|
||||||
// host: '127.0.0.1:7001',
|
|
||||||
// 'content-type': 'application/json',
|
|
||||||
// 'content-length': '258',
|
|
||||||
// connection: 'keep-alive' }
|
|
||||||
// { name: 'mk2',
|
|
||||||
// salt: '12351936478446a5466d4fb1633b80f3838b4caaa03649a885ac722cd6',
|
|
||||||
// password_sha: '123408912a6db1d96b132a90856d99db029cef3d',
|
|
||||||
// email: 'fengmk2@gmail.com',
|
|
||||||
// _id: 'org.couchdb.user:mk2',
|
|
||||||
// type: 'user',
|
|
||||||
// roles: [],
|
|
||||||
// date: '2014-03-15T02:39:25.696Z' }
|
|
||||||
exports.add = function* () {
|
|
||||||
var name = this.params.name;
|
|
||||||
var body = this.request.body || {};
|
|
||||||
var user = {
|
|
||||||
name: body.name,
|
|
||||||
// salt: body.salt,
|
|
||||||
// password_sha: body.password_sha,
|
|
||||||
email: body.email,
|
|
||||||
ip: this.ip || '0.0.0.0',
|
|
||||||
// roles: body.roles || [],
|
|
||||||
};
|
|
||||||
|
|
||||||
ensurePasswordSalt(user, body);
|
|
||||||
|
|
||||||
if (!body.password || !user.name || !user.salt || !user.password_sha || !user.email) {
|
|
||||||
this.status = 422;
|
|
||||||
this.body = {
|
|
||||||
error: 'paramError',
|
|
||||||
reason: 'params missing, name, email or password missing.'
|
|
||||||
};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
debug('add user: %j', body);
|
|
||||||
|
|
||||||
var loginedUser;
|
|
||||||
try {
|
|
||||||
loginedUser = yield UserService.auth(body.name, body.password);
|
|
||||||
} catch (err) {
|
|
||||||
this.status = err.status || 500;
|
|
||||||
this.body = {
|
|
||||||
error: err.name,
|
|
||||||
reason: err.message
|
|
||||||
};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (loginedUser) {
|
|
||||||
var rev = Date.now() + '-' + loginedUser.login;
|
|
||||||
if (config.customUserService) {
|
|
||||||
// make sure sync user meta to cnpm database
|
|
||||||
var data = user;
|
|
||||||
data.rev = rev;
|
|
||||||
data.user = loginedUser;
|
|
||||||
yield* User.saveCustomUser(data);
|
|
||||||
}
|
|
||||||
this.status = 201;
|
|
||||||
this.body = {
|
|
||||||
ok: true,
|
|
||||||
id: 'org.couchdb.user:' + loginedUser.login,
|
|
||||||
rev: rev,
|
|
||||||
};
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
var existUser = yield User.get(name);
|
|
||||||
if (existUser) {
|
|
||||||
this.status = 409;
|
|
||||||
this.body = {
|
|
||||||
error: 'conflict',
|
|
||||||
reason: 'User ' + name + ' already exists.'
|
|
||||||
};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = yield User.add(user);
|
|
||||||
this.etag = '"' + result.rev + '"';
|
|
||||||
this.status = 201;
|
|
||||||
this.body = {
|
|
||||||
ok: true,
|
|
||||||
id: 'org.couchdb.user:' + name,
|
|
||||||
rev: result.rev
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
// logined before update, no need to auth user again
|
|
||||||
// { name: 'admin',
|
|
||||||
// password: '123123',
|
|
||||||
// email: 'fengmk2@gmail.com',
|
|
||||||
// _id: 'org.couchdb.user:admin',
|
|
||||||
// type: 'user',
|
|
||||||
// roles: [],
|
|
||||||
// date: '2014-08-05T16:08:22.645Z',
|
|
||||||
// _rev: '1-1a18c3d73ba42e863523a399ff3304d8',
|
|
||||||
// _cnpm_meta:
|
|
||||||
// { id: 14,
|
|
||||||
// npm_user: false,
|
|
||||||
// custom_user: false,
|
|
||||||
// gmt_create: '2014-08-05T15:46:58.000Z',
|
|
||||||
// gmt_modified: '2014-08-05T15:46:58.000Z',
|
|
||||||
// admin: true,
|
|
||||||
// scopes: [ '@cnpm', '@cnpmtest' ] } }
|
|
||||||
exports.update = function *(next) {
|
|
||||||
var name = this.params.name;
|
|
||||||
var rev = this.params.rev;
|
|
||||||
if (!name || !rev) {
|
|
||||||
return yield* next;
|
|
||||||
}
|
|
||||||
debug('update: %s, rev: %s, user.name: %s', name, rev, this.user.name);
|
|
||||||
|
|
||||||
if (name !== this.user.name) {
|
|
||||||
// must auth user first
|
|
||||||
this.status = 401;
|
|
||||||
this.body = {
|
|
||||||
error: 'unauthorized',
|
|
||||||
reason: 'Name is incorrect.'
|
|
||||||
};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var body = this.request.body || {};
|
|
||||||
var user = {
|
|
||||||
name: body.name,
|
|
||||||
// salt: body.salt,
|
|
||||||
// password_sha: body.password_sha,
|
|
||||||
email: body.email,
|
|
||||||
ip: this.ip || '0.0.0.0',
|
|
||||||
rev: body.rev || body._rev,
|
|
||||||
// roles: body.roles || [],
|
|
||||||
};
|
|
||||||
|
|
||||||
debug('update user %j', body);
|
|
||||||
|
|
||||||
ensurePasswordSalt(user, body);
|
|
||||||
|
|
||||||
if (!body.password || !user.name || !user.salt || !user.password_sha || !user.email) {
|
|
||||||
this.status = 422;
|
|
||||||
this.body = {
|
|
||||||
error: 'paramError',
|
|
||||||
reason: 'params missing, name, email or password missing.'
|
|
||||||
};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = yield User.update(user);
|
|
||||||
if (!result) {
|
|
||||||
this.status = 409;
|
|
||||||
this.body = {
|
|
||||||
error: 'conflict',
|
|
||||||
reason: 'Document update conflict.'
|
|
||||||
};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.status = 201;
|
|
||||||
this.body = {
|
|
||||||
ok: true,
|
|
||||||
id: 'org.couchdb.user:' + user.name,
|
|
||||||
rev: result.rev
|
|
||||||
};
|
|
||||||
};
|
|
||||||
128
controllers/registry/user/add.js
Normal file
128
controllers/registry/user/add.js
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
/**!
|
||||||
|
* 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';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var ensurePasswordSalt = require('./common').ensurePasswordSalt;
|
||||||
|
var userService = require('../../../services/user');
|
||||||
|
var config = require('../../../config');
|
||||||
|
|
||||||
|
// npm 1.4.4
|
||||||
|
// add new user first
|
||||||
|
// @see https://github.com/npm/npm-registry-client/commit/effb4bc88d443f764f2c2e8b4dd583cc72cf6084
|
||||||
|
// PUT /-/user/org.couchdb.user:mk2 { accept: 'application/json',
|
||||||
|
// 'accept-encoding': 'gzip',
|
||||||
|
// 'user-agent': 'node/v0.11.12 darwin x64',
|
||||||
|
// host: '127.0.0.1:7001',
|
||||||
|
// 'content-type': 'application/json',
|
||||||
|
// 'content-length': '150',
|
||||||
|
// connection: 'close' } { name: 'mk2',
|
||||||
|
// password: '123456',
|
||||||
|
// email: 'fengmk2@gmail.com',
|
||||||
|
// _id: 'org.couchdb.user:mk2',
|
||||||
|
// type: 'user',
|
||||||
|
// roles: [],
|
||||||
|
// date: '2014-03-15T02:33:19.465Z' }
|
||||||
|
|
||||||
|
// old npm flow
|
||||||
|
// json:
|
||||||
|
// PUT /-/user/org.couchdb.user:mk2 { accept: 'application/json',
|
||||||
|
// 'user-agent': 'node/v0.8.26 darwin x64',
|
||||||
|
// host: '127.0.0.1:7001',
|
||||||
|
// 'content-type': 'application/json',
|
||||||
|
// 'content-length': '258',
|
||||||
|
// connection: 'keep-alive' }
|
||||||
|
// { name: 'mk2',
|
||||||
|
// salt: '12351936478446a5466d4fb1633b80f3838b4caaa03649a885ac722cd6',
|
||||||
|
// password_sha: '123408912a6db1d96b132a90856d99db029cef3d',
|
||||||
|
// email: 'fengmk2@gmail.com',
|
||||||
|
// _id: 'org.couchdb.user:mk2',
|
||||||
|
// type: 'user',
|
||||||
|
// roles: [],
|
||||||
|
// date: '2014-03-15T02:39:25.696Z' }
|
||||||
|
module.exports = function* addUser() {
|
||||||
|
var name = this.params.name;
|
||||||
|
var body = this.request.body || {};
|
||||||
|
var user = {
|
||||||
|
name: body.name,
|
||||||
|
// salt: body.salt,
|
||||||
|
// password_sha: body.password_sha,
|
||||||
|
email: body.email,
|
||||||
|
ip: this.ip || '0.0.0.0',
|
||||||
|
// roles: body.roles || [],
|
||||||
|
};
|
||||||
|
|
||||||
|
ensurePasswordSalt(user, body);
|
||||||
|
|
||||||
|
if (!body.password || !user.name || !user.salt || !user.password_sha || !user.email) {
|
||||||
|
this.status = 422;
|
||||||
|
this.body = {
|
||||||
|
error: 'paramError',
|
||||||
|
reason: 'params missing, name, email or password missing.'
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
var existUser = yield* userService.get(name);
|
||||||
|
if (existUser) {
|
||||||
|
this.status = 409;
|
||||||
|
this.body = {
|
||||||
|
error: 'conflict',
|
||||||
|
reason: 'User ' + name + ' already exists.'
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// add new user
|
||||||
|
var result = yield* userService.add(user);
|
||||||
|
this.etag = '"' + result.rev + '"';
|
||||||
|
this.status = 201;
|
||||||
|
this.body = {
|
||||||
|
ok: true,
|
||||||
|
id: 'org.couchdb.user:' + name,
|
||||||
|
rev: result.rev
|
||||||
|
};
|
||||||
|
};
|
||||||
26
controllers/registry/user/common.js
Normal file
26
controllers/registry/user/common.js
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - controllers/registry/user/common.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var crypto = require('crypto');
|
||||||
|
var utility = require('utility');
|
||||||
|
|
||||||
|
exports.ensurePasswordSalt = function (user, body) {
|
||||||
|
if (!user.password_sha && body.password) {
|
||||||
|
// create password_sha on server
|
||||||
|
user.salt = crypto.randomBytes(30).toString('hex');
|
||||||
|
user.password_sha = utility.sha1(body.password + user.salt);
|
||||||
|
}
|
||||||
|
};
|
||||||
69
controllers/registry/user/show.js
Normal file
69
controllers/registry/user/show.js
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
/**!
|
||||||
|
* 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';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var 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);
|
||||||
|
if (!user) {
|
||||||
|
return yield* next;
|
||||||
|
}
|
||||||
|
|
||||||
|
var data = user.json;
|
||||||
|
if (!data) {
|
||||||
|
data = {
|
||||||
|
_id: 'org.couchdb.user:' + user.name,
|
||||||
|
_rev: user.rev,
|
||||||
|
name: user.name,
|
||||||
|
email: user.email,
|
||||||
|
type: 'user',
|
||||||
|
roles: [],
|
||||||
|
date: user.gmt_modified,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.login) {
|
||||||
|
// custom user format
|
||||||
|
// convert to npm user format
|
||||||
|
data = {
|
||||||
|
_id: 'org.couchdb.user:' + user.name,
|
||||||
|
_rev: user.rev,
|
||||||
|
name: user.name,
|
||||||
|
email: user.email,
|
||||||
|
type: 'user',
|
||||||
|
roles: [],
|
||||||
|
date: user.gmt_modified,
|
||||||
|
avatar: data.avatar_url,
|
||||||
|
fullname: data.name || data.login,
|
||||||
|
homepage: data.html_url,
|
||||||
|
scopes: data.scopes,
|
||||||
|
site_admin: data.site_admin
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
data._cnpm_meta = {
|
||||||
|
id: user.id,
|
||||||
|
npm_user: user.isNpmUser,
|
||||||
|
custom_user: !!data.login,
|
||||||
|
gmt_create: user.gmt_create,
|
||||||
|
gmt_modified: user.gmt_modified,
|
||||||
|
};
|
||||||
|
|
||||||
|
this.body = data;
|
||||||
|
};
|
||||||
97
controllers/registry/user/update.js
Normal file
97
controllers/registry/user/update.js
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
/**!
|
||||||
|
* 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';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var debug = require('debug')('cnpmjs.org:controllers:registry:user:update');
|
||||||
|
var ensurePasswordSalt = require('./common').ensurePasswordSalt;
|
||||||
|
var userService = require('../../../services/user');
|
||||||
|
|
||||||
|
// logined before update, no need to auth user again
|
||||||
|
// { name: 'admin',
|
||||||
|
// password: '123123',
|
||||||
|
// email: 'fengmk2@gmail.com',
|
||||||
|
// _id: 'org.couchdb.user:admin',
|
||||||
|
// type: 'user',
|
||||||
|
// roles: [],
|
||||||
|
// date: '2014-08-05T16:08:22.645Z',
|
||||||
|
// _rev: '1-1a18c3d73ba42e863523a399ff3304d8',
|
||||||
|
// _cnpm_meta:
|
||||||
|
// { id: 14,
|
||||||
|
// npm_user: false,
|
||||||
|
// custom_user: false,
|
||||||
|
// gmt_create: '2014-08-05T15:46:58.000Z',
|
||||||
|
// gmt_modified: '2014-08-05T15:46:58.000Z',
|
||||||
|
// admin: true,
|
||||||
|
// scopes: [ '@cnpm', '@cnpmtest' ] } }
|
||||||
|
module.exports = function* updateUser(next) {
|
||||||
|
var name = this.params.name;
|
||||||
|
var rev = this.params.rev;
|
||||||
|
if (!name || !rev) {
|
||||||
|
return yield* next;
|
||||||
|
}
|
||||||
|
debug('update: %s, rev: %s, user.name: %s', name, rev, this.user.name);
|
||||||
|
|
||||||
|
if (name !== this.user.name) {
|
||||||
|
// must auth user first
|
||||||
|
this.status = 401;
|
||||||
|
this.body = {
|
||||||
|
error: 'unauthorized',
|
||||||
|
reason: 'Name is incorrect.'
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var body = this.request.body || {};
|
||||||
|
var user = {
|
||||||
|
name: body.name,
|
||||||
|
// salt: body.salt,
|
||||||
|
// password_sha: body.password_sha,
|
||||||
|
email: body.email,
|
||||||
|
ip: this.ip || '0.0.0.0',
|
||||||
|
rev: body.rev || body._rev,
|
||||||
|
// roles: body.roles || [],
|
||||||
|
};
|
||||||
|
|
||||||
|
debug('update user %j', body);
|
||||||
|
|
||||||
|
ensurePasswordSalt(user, body);
|
||||||
|
|
||||||
|
if (!body.password || !user.name || !user.salt || !user.password_sha || !user.email) {
|
||||||
|
this.status = 422;
|
||||||
|
this.body = {
|
||||||
|
error: 'paramError',
|
||||||
|
reason: 'params missing, name, email or password missing.'
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = yield* userService.update(user);
|
||||||
|
if (!result) {
|
||||||
|
this.status = 409;
|
||||||
|
this.body = {
|
||||||
|
error: 'conflict',
|
||||||
|
reason: 'Document update conflict.'
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.status = 201;
|
||||||
|
this.body = {
|
||||||
|
ok: true,
|
||||||
|
id: 'org.couchdb.user:' + user.name,
|
||||||
|
rev: result.rev
|
||||||
|
};
|
||||||
|
};
|
||||||
51
controllers/registry/user_package.js
Normal file
51
controllers/registry/user_package.js
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - controllers/registry/user_package.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var packageService = require('../../services/package');
|
||||||
|
|
||||||
|
// GET /-/by-user/:user
|
||||||
|
exports.list = function* () {
|
||||||
|
var users = this.params.user.split('|');
|
||||||
|
if (users.length > 20) {
|
||||||
|
this.status = 400;
|
||||||
|
this.body = {
|
||||||
|
error: 'bad_request',
|
||||||
|
reason: 'reach max user names limit, must <= 20 user names'
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var firstUser = users[0];
|
||||||
|
if (!firstUser) {
|
||||||
|
// params.user = '|'
|
||||||
|
this.body = {};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var tasks = {};
|
||||||
|
for (var i = 0; i < users.length; i++) {
|
||||||
|
var username = users[i];
|
||||||
|
tasks[username] = packageService.listPublicModuleNamesByUser(username);
|
||||||
|
}
|
||||||
|
|
||||||
|
var data = yield tasks;
|
||||||
|
for (var k in data) {
|
||||||
|
if (data[k].length === 0) {
|
||||||
|
data[k] = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.body = data;
|
||||||
|
};
|
||||||
@@ -15,16 +15,25 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
var debug = require('debug')('cnpmjs.org:controllers:sync');
|
var debug = require('debug')('cnpmjs.org:controllers:sync');
|
||||||
var Log = require('../proxy/module_log');
|
var Log = require('../services/module_log');
|
||||||
var SyncModuleWorker = require('../proxy/sync_module_worker');
|
var SyncModuleWorker = require('./sync_module_worker');
|
||||||
|
var config = require('../config');
|
||||||
|
|
||||||
exports.sync = function* () {
|
exports.sync = function* () {
|
||||||
var username = this.user.name || 'anonymous';
|
var username = this.user.name || 'anonymous';
|
||||||
var name = this.params.name || this.params[0];
|
var name = this.params.name || this.params[0];
|
||||||
|
var type = 'package';
|
||||||
|
if (name.indexOf(':') > 0) {
|
||||||
|
// user:fengmk2
|
||||||
|
// package:pedding
|
||||||
|
var splits = name.split(':');
|
||||||
|
type = splits[0];
|
||||||
|
name = splits[1];
|
||||||
|
}
|
||||||
var publish = this.query.publish === 'true';
|
var publish = this.query.publish === 'true';
|
||||||
var noDep = this.query.nodeps === 'true';
|
var noDep = this.query.nodeps === 'true';
|
||||||
debug('sync %s with query: %j', name, this.query);
|
debug('sync %s with query: %j', name, this.query);
|
||||||
if (publish && !this.user.isAdmin) {
|
if (type === 'package' && publish && !this.user.isAdmin) {
|
||||||
this.status = 403;
|
this.status = 403;
|
||||||
this.body = {
|
this.body = {
|
||||||
error: 'no_perms',
|
error: 'no_perms',
|
||||||
@@ -34,39 +43,26 @@ exports.sync = function* () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var options = {
|
var options = {
|
||||||
|
type: type,
|
||||||
publish: publish,
|
publish: publish,
|
||||||
noDep: noDep,
|
noDep: noDep,
|
||||||
|
syncUpstreamFirst: config.sourceNpmRegistryIsCNpm,
|
||||||
};
|
};
|
||||||
|
|
||||||
var result = yield SyncModuleWorker.sync(name, username, options);
|
var logId = yield* SyncModuleWorker.sync(name, username, options);
|
||||||
debug('sync %s got %j', name, result);
|
debug('sync %s got log id %j', name, logId);
|
||||||
|
|
||||||
// friendly 404 reason info
|
|
||||||
if (result.statusCode === 404) {
|
|
||||||
this.status = 404;
|
|
||||||
this.body = {
|
|
||||||
ok: false,
|
|
||||||
reason: 'can not found ' + name + ' in the source registry'
|
|
||||||
};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!result.ok) {
|
|
||||||
this.status = result.statusCode || 500;
|
|
||||||
this.body = result.pkg;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.status = 201;
|
this.status = 201;
|
||||||
this.body = {
|
this.body = {
|
||||||
ok: true,
|
ok: true,
|
||||||
logId: result.logId
|
logId: logId
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.getSyncLog = function* (next) {
|
exports.getSyncLog = function* (next) {
|
||||||
// params: [$name, $id] on scope package
|
|
||||||
var logId = this.params.id || this.params[1];
|
var logId = this.params.id || this.params[1];
|
||||||
var offset = Number(this.query.offset) || 0;
|
var offset = Number(this.query.offset) || 0;
|
||||||
var row = yield Log.get(logId);
|
var row = yield* Log.get(logId);
|
||||||
if (!row) {
|
if (!row) {
|
||||||
return yield* next;
|
return yield* next;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
/**!
|
/**!
|
||||||
* cnpmjs.org - proxy/sync_module_worker.js
|
* cnpmjs.org - controllers/sync_module_worker.js
|
||||||
*
|
*
|
||||||
* Copyright(c) cnpmjs.org and other contributors.
|
* Copyright(c) cnpmjs.org and other contributors.
|
||||||
* MIT Licensed
|
* MIT Licensed
|
||||||
*
|
*
|
||||||
* Authors:
|
* Authors:
|
||||||
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
|
||||||
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
|
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -15,53 +15,58 @@
|
|||||||
* Module dependencies.
|
* Module dependencies.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
var debug = require('debug')('cnpmjs.org:proxy:sync_module_worker');
|
||||||
var co = require('co');
|
var co = require('co');
|
||||||
var gather = require('co-gather');
|
var gather = require('co-gather');
|
||||||
var defer = require('co-defer');
|
var defer = require('co-defer');
|
||||||
var thunkify = require('thunkify-wrap');
|
var thunkify = require('thunkify-wrap');
|
||||||
var debug = require('debug')('cnpmjs.org:proxy:sync_module_worker');
|
|
||||||
var EventEmitter = require('events').EventEmitter;
|
var EventEmitter = require('events').EventEmitter;
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var crypto = require('crypto');
|
var crypto = require('crypto');
|
||||||
var urllib = require('co-urllib');
|
var sleep = require('co-sleep');
|
||||||
var utility = require('utility');
|
var utility = require('utility');
|
||||||
var ms = require('ms');
|
|
||||||
var urlparse = require('url').parse;
|
var urlparse = require('url').parse;
|
||||||
var nfs = require('../common/nfs');
|
var urllib = require('../common/urllib');
|
||||||
var npm = require('./npm');
|
|
||||||
var common = require('../lib/common');
|
|
||||||
var Module = require('./module');
|
|
||||||
var ModuleDeps = require('./module_deps');
|
|
||||||
var Log = require('./module_log');
|
|
||||||
var config = require('../config');
|
var config = require('../config');
|
||||||
var ModuleStar = require('./module_star');
|
var nfs = require('../common/nfs');
|
||||||
var User = require('./user');
|
var logger = require('../common/logger');
|
||||||
var ModuleUnpublished = require('./module_unpublished');
|
var common = require('../lib/common');
|
||||||
|
var npmSerivce = require('../services/npm');
|
||||||
|
var packageService = require('../services/package');
|
||||||
|
var logService = require('../services/module_log');
|
||||||
|
var User = require('../models').User;
|
||||||
|
var os = require('os');
|
||||||
|
|
||||||
var USER_AGENT = 'sync.cnpmjs.org/' + config.version + ' ' + urllib.USER_AGENT;
|
var USER_AGENT = 'sync.cnpmjs.org/' + config.version +
|
||||||
|
' hostname/' + os.hostname() + ' ' + urllib.USER_AGENT;
|
||||||
|
|
||||||
function SyncModuleWorker(options) {
|
function SyncModuleWorker(options) {
|
||||||
EventEmitter.call(this);
|
EventEmitter.call(this);
|
||||||
this._logId = options.logId;
|
this._logId = options.logId;
|
||||||
|
this._log = '';
|
||||||
|
this._loging = false;
|
||||||
if (!Array.isArray(options.name)) {
|
if (!Array.isArray(options.name)) {
|
||||||
options.name = [options.name];
|
options.name = [options.name];
|
||||||
}
|
}
|
||||||
|
|
||||||
this.names = options.name || [];
|
this.type = options.type || 'package';
|
||||||
|
this.names = options.name;
|
||||||
this.startName = this.names[0];
|
this.startName = this.names[0];
|
||||||
|
|
||||||
this.username = options.username;
|
this.username = options.username;
|
||||||
this.concurrency = options.concurrency || 1;
|
this.concurrency = options.concurrency || 1;
|
||||||
this._publish = options.publish; // _publish_on_cnpm
|
this._publish = options.publish === true; // _publish_on_cnpm
|
||||||
|
this.syncUpstreamFirst = options.syncUpstreamFirst;
|
||||||
|
|
||||||
this.syncingNames = {};
|
this.syncingNames = {};
|
||||||
this.nameMap = {};
|
this.nameMap = {};
|
||||||
this.names.forEach(function (name) {
|
this.names.forEach(function (name) {
|
||||||
this.nameMap[name] = true;
|
this.nameMap[name] = true;
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
this.noDep = options.noDep; //do not sync dependences
|
this.noDep = options.noDep === true; // do not sync dependences
|
||||||
|
this.syncDevDependencies = config.syncDevDependencies;
|
||||||
|
|
||||||
this.successes = [];
|
this.successes = [];
|
||||||
this.fails = [];
|
this.fails = [];
|
||||||
@@ -77,8 +82,9 @@ SyncModuleWorker.prototype.finish = function () {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this._finished = true;
|
this._finished = true;
|
||||||
this.log('[done] Sync %s module finished, %d success, %d fail\nSuccess: [ %s ]\nFail: [ %s ]',
|
this.log('[done] Sync %s %s finished, %d success, %d fail\nSuccess: [ %s ]\nFail: [ %s ]',
|
||||||
this.startName,
|
this.startName,
|
||||||
|
this.type,
|
||||||
this.successes.length, this.fails.length,
|
this.successes.length, this.fails.length,
|
||||||
this.successes.join(', '), this.fails.join(', '));
|
this.successes.join(', '), this.fails.join(', '));
|
||||||
this.emit('end');
|
this.emit('end');
|
||||||
@@ -86,23 +92,71 @@ SyncModuleWorker.prototype.finish = function () {
|
|||||||
this.removeAllListeners();
|
this.removeAllListeners();
|
||||||
};
|
};
|
||||||
|
|
||||||
SyncModuleWorker.prototype.log = function (format, arg1, arg2) {
|
// log(format, arg1, arg2, ...)
|
||||||
|
SyncModuleWorker.prototype.log = function () {
|
||||||
var str = '[' + utility.YYYYMMDDHHmmss() + '] ' + util.format.apply(util, arguments);
|
var str = '[' + utility.YYYYMMDDHHmmss() + '] ' + util.format.apply(util, arguments);
|
||||||
debug(str);
|
debug(str);
|
||||||
this._logId && Log.append(this._logId, str, utility.noop);
|
var logId = this._logId;
|
||||||
|
if (logId) {
|
||||||
|
if (this._log) {
|
||||||
|
this._log += '\n';
|
||||||
|
}
|
||||||
|
this._log += str;
|
||||||
|
this._saveLog();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
SyncModuleWorker.prototype._saveLog = function () {
|
||||||
|
var that = this;
|
||||||
|
if (that._loging) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
that._loging = true;
|
||||||
|
var logstr = that._log;
|
||||||
|
that._log = '';
|
||||||
|
co(function* () {
|
||||||
|
yield* logService.append(that._logId, logstr);
|
||||||
|
}).then(function () {
|
||||||
|
that._loging = false;
|
||||||
|
if (that._log) {
|
||||||
|
that._saveLog();
|
||||||
|
}
|
||||||
|
}).catch(function (err) {
|
||||||
|
logger.error(err);
|
||||||
|
that._loging = false;
|
||||||
|
if (that._log) {
|
||||||
|
that._saveLog();
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
SyncModuleWorker.prototype.start = function () {
|
SyncModuleWorker.prototype.start = function () {
|
||||||
this.log('user: %s, sync %s worker start, %d concurrency, nodeps: %s, publish: %s',
|
this.log('user: %s, sync %s worker start, %d concurrency, nodeps: %s, publish: %s',
|
||||||
this.username, this.names[0], this.concurrency, this.noDep, this._publish);
|
this.username, this.names[0], this.concurrency, this.noDep, this._publish);
|
||||||
var self = this;
|
var that = this;
|
||||||
co(function *() {
|
co(function* () {
|
||||||
|
// sync upstream
|
||||||
|
if (that.syncUpstreamFirst) {
|
||||||
|
try {
|
||||||
|
yield* that.syncUpstream(that.startName);
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (that.type === 'user') {
|
||||||
|
yield that.syncUser();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var arr = [];
|
var arr = [];
|
||||||
for (var i = 0; i < self.concurrency; i++) {
|
for (var i = 0; i < that.concurrency; i++) {
|
||||||
arr.push(self.next(i));
|
arr.push(that.next(i));
|
||||||
}
|
}
|
||||||
yield arr;
|
yield arr;
|
||||||
})();
|
}).catch(function (err) {
|
||||||
|
logger.error(err);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
SyncModuleWorker.prototype.pushSuccess = function (name) {
|
SyncModuleWorker.prototype.pushSuccess = function (name) {
|
||||||
@@ -126,6 +180,8 @@ SyncModuleWorker.prototype.add = function (name) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
SyncModuleWorker.prototype._doneOne = function* (concurrencyId, name, success) {
|
SyncModuleWorker.prototype._doneOne = function* (concurrencyId, name, success) {
|
||||||
|
this.log('----------------- Synced %s %s -------------------',
|
||||||
|
name, success ? 'success' : 'fail');
|
||||||
if (success) {
|
if (success) {
|
||||||
this.pushSuccess(name);
|
this.pushSuccess(name);
|
||||||
} else {
|
} else {
|
||||||
@@ -134,13 +190,111 @@ SyncModuleWorker.prototype._doneOne = function* (concurrencyId, name, success) {
|
|||||||
delete this.syncingNames[name];
|
delete this.syncingNames[name];
|
||||||
var that = this;
|
var that = this;
|
||||||
// relase the stack: https://github.com/cnpm/cnpmjs.org/issues/328
|
// relase the stack: https://github.com/cnpm/cnpmjs.org/issues/328
|
||||||
defer.setImmediate(function *() {
|
defer.setImmediate(function* () {
|
||||||
that.log('[c#%s] setImmediate after, %s done, start next...', concurrencyId, name);
|
yield* that.next(concurrencyId);
|
||||||
yield *that.next(concurrencyId);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
SyncModuleWorker.prototype.next = function *(concurrencyId) {
|
SyncModuleWorker.prototype.syncUpstream = function* (name) {
|
||||||
|
if (config.sourceNpmRegistry.indexOf('registry.npmjs.org') >= 0 ||
|
||||||
|
config.sourceNpmRegistry.indexOf('registry.npmjs.com') >= 0) {
|
||||||
|
this.log('----------------- upstream is npm registry: %s, ignore it -------------------', config.sourceNpmRegistry);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var syncname = name;
|
||||||
|
if (this.type === 'user') {
|
||||||
|
syncname = this.type + ':' + syncname;
|
||||||
|
}
|
||||||
|
var url = config.sourceNpmRegistry + '/' + syncname + '/sync';
|
||||||
|
if (this.noDep) {
|
||||||
|
url += '?nodeps=true';
|
||||||
|
}
|
||||||
|
var r = yield urllib.request(url, {
|
||||||
|
method: 'put',
|
||||||
|
timeout: 20000,
|
||||||
|
headers: {
|
||||||
|
'content-length': 0
|
||||||
|
},
|
||||||
|
dataType: 'json',
|
||||||
|
gzip: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (r.status !== 201 || !r.data.ok) {
|
||||||
|
return this.log('sync upstream %s error, status: %s, response: %j',
|
||||||
|
url, r.status, r.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
var logURL = config.sourceNpmRegistry + '/' + name + '/sync/log/' + r.data.logId;
|
||||||
|
var offset = 0;
|
||||||
|
this.log('----------------- Syncing upstream %s -------------------', logURL);
|
||||||
|
|
||||||
|
var count = 0;
|
||||||
|
while (true) {
|
||||||
|
count++;
|
||||||
|
var synclogURL = logURL + '?offset=' + offset;
|
||||||
|
var rs = yield urllib.request(synclogURL, {
|
||||||
|
timeout: 20000,
|
||||||
|
dataType: 'json',
|
||||||
|
gzip: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (rs.status !== 200 || !rs.data.ok) {
|
||||||
|
this.log('sync upstream %s error, status: %s, response: %j',
|
||||||
|
synclogURL, rs.status, rs.data);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var data = rs.data;
|
||||||
|
var syncDone = false;
|
||||||
|
if (data.log && data.log.indexOf('[done] Sync') >= 0) {
|
||||||
|
syncDone = true;
|
||||||
|
data.log = data.log.replace('[done] Sync', '[Upstream done] Sync');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.log) {
|
||||||
|
this.log(data.log);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (syncDone) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count >= 30) {
|
||||||
|
this.log('sync upstream %s fail, give up', logURL);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.log) {
|
||||||
|
offset += data.log.split('\n').length;
|
||||||
|
}
|
||||||
|
|
||||||
|
yield sleep(2000);
|
||||||
|
}
|
||||||
|
this.log('----------------- Synced upstream %s -------------------', logURL);
|
||||||
|
};
|
||||||
|
|
||||||
|
SyncModuleWorker.prototype.syncUser = function* () {
|
||||||
|
for (var i = 0; i < this.names.length; i++) {
|
||||||
|
var username = this.names[i];
|
||||||
|
try {
|
||||||
|
var user = yield _saveNpmUser(username);
|
||||||
|
this.pushSuccess(username);
|
||||||
|
this.log('[c#%s] [%s] sync success: %j', 0, username, user);
|
||||||
|
} catch (err) {
|
||||||
|
this.pushFail(username);
|
||||||
|
this.log('[c#%s] [error] [%s] sync error: %s', 0, username, err.stack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.finish();
|
||||||
|
};
|
||||||
|
|
||||||
|
SyncModuleWorker.prototype.next = function* (concurrencyId) {
|
||||||
|
if (config.syncModel === 'none') {
|
||||||
|
this.log('[c#%d] [%s] syncModel is none, ignore',
|
||||||
|
concurrencyId, name);
|
||||||
|
return this.finish();
|
||||||
|
}
|
||||||
|
|
||||||
var name = this.names.shift();
|
var name = this.names.shift();
|
||||||
if (!name) {
|
if (!name) {
|
||||||
return setImmediate(this.finish.bind(this));
|
return setImmediate(this.finish.bind(this));
|
||||||
@@ -150,9 +304,20 @@ SyncModuleWorker.prototype.next = function *(concurrencyId) {
|
|||||||
that.syncingNames[name] = true;
|
that.syncingNames[name] = true;
|
||||||
var pkg = null;
|
var pkg = null;
|
||||||
var status = 0;
|
var status = 0;
|
||||||
|
|
||||||
|
this.log('----------------- Syncing %s -------------------', name);
|
||||||
|
|
||||||
|
// ignore private scoped package
|
||||||
|
if (common.isPrivateScopedPackage(name)) {
|
||||||
|
this.log('[c#%d] [%s] ignore sync private scoped %j package',
|
||||||
|
concurrencyId, name, config.scopes);
|
||||||
|
yield* this._doneOne(concurrencyId, name, true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// get from npm
|
// get from npm
|
||||||
try {
|
try {
|
||||||
var result = yield npm.request('/' + name);
|
var result = yield* npmSerivce.request('/' + name.replace('/', '%2f'));
|
||||||
pkg = result.data;
|
pkg = result.data;
|
||||||
status = result.status;
|
status = result.status;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -169,7 +334,7 @@ SyncModuleWorker.prototype.next = function *(concurrencyId) {
|
|||||||
var unpublishedInfo = null;
|
var unpublishedInfo = null;
|
||||||
if (status === 404) {
|
if (status === 404) {
|
||||||
// check if it's unpublished
|
// check if it's unpublished
|
||||||
if (pkg.time && pkg.time.unpublished && pkg.time.unpublished.time) {
|
if (pkg && pkg.time && pkg.time.unpublished && pkg.time.unpublished.time) {
|
||||||
unpublishedInfo = pkg.time.unpublished;
|
unpublishedInfo = pkg.time.unpublished;
|
||||||
} else {
|
} else {
|
||||||
pkg = null;
|
pkg = null;
|
||||||
@@ -205,13 +370,13 @@ SyncModuleWorker.prototype.next = function *(concurrencyId) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
that.log('[c#%d] [%s] synced success, %d versions: %s',
|
this.log('[c#%d] [%s] synced success, %d versions: %s',
|
||||||
concurrencyId, name, versions.length, versions.join(', '));
|
concurrencyId, name, versions.length, versions.join(', '));
|
||||||
yield* that._doneOne(concurrencyId, name, true);
|
yield* this._doneOne(concurrencyId, name, true);
|
||||||
};
|
};
|
||||||
|
|
||||||
function *_listStarUsers(modName) {
|
function* _listStarUsers(modName) {
|
||||||
var users = yield ModuleStar.listUsers(modName);
|
var users = yield* packageService.listStarUserNames(modName);
|
||||||
var userMap = {};
|
var userMap = {};
|
||||||
users.forEach(function (user) {
|
users.forEach(function (user) {
|
||||||
userMap[user] = true;
|
userMap[user] = true;
|
||||||
@@ -219,35 +384,40 @@ function *_listStarUsers(modName) {
|
|||||||
return userMap;
|
return userMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
function *_addStar(modName, username) {
|
function* _saveNpmUser(username) {
|
||||||
yield ModuleStar.add(modName, username);
|
var user = yield* npmSerivce.getUser(username);
|
||||||
}
|
|
||||||
|
|
||||||
function *_saveNpmUser(username) {
|
|
||||||
var user = yield *npm.getUser(username);
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
yield User.saveNpmUser(user);
|
yield* User.saveNpmUser(user);
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
function* _saveMaintainer(modName, username, action) {
|
||||||
|
if (action === 'add') {
|
||||||
|
yield* packageService.addPublicModuleMaintainer(modName, username);
|
||||||
|
} else if (action === 'remove') {
|
||||||
|
yield* packageService.removePublicModuleMaintainer(modName, username);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SyncModuleWorker.prototype._unpublished = function* (name, unpublishedInfo) {
|
SyncModuleWorker.prototype._unpublished = function* (name, unpublishedInfo) {
|
||||||
var mods = yield Module.listByName(name);
|
var mods = yield* packageService.listModulesByName(name);
|
||||||
this.log(' [%s] start unpublished %d versions from local cnpm registry',
|
this.log(' [%s] start unpublished %d versions from local cnpm registry',
|
||||||
name, mods.length);
|
name, mods.length);
|
||||||
if (this._isLocalModule(mods)) {
|
if (common.isLocalModule(mods)) {
|
||||||
// publish on cnpm, dont sync this version package
|
// publish on cnpm, dont sync this version package
|
||||||
this.log(' [%s] publish on local cnpm registry, don\'t sync', name);
|
this.log(' [%s] publish on local cnpm registry, don\'t sync', name);
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
var r = yield* ModuleUnpublished.add(name, unpublishedInfo);
|
var r = yield* packageService.saveUnpublishedModule(name, unpublishedInfo);
|
||||||
this.log(' [%s] save unpublished info: %j to row#%s',
|
this.log(' [%s] save unpublished info: %j to row#%s',
|
||||||
name, unpublishedInfo, r.insertId);
|
name, unpublishedInfo, r.id);
|
||||||
if (mods.length === 0) {
|
if (mods.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
yield [Module.removeByName(name), Module.removeTags(name)];
|
yield [packageService.removeModulesByName(name), packageService.removeModuleTags(name)];
|
||||||
var keys = [];
|
var keys = [];
|
||||||
for (var i = 0; i < mods.length; i++) {
|
for (var i = 0; i < mods.length; i++) {
|
||||||
var row = mods[i];
|
var row = mods[i];
|
||||||
@@ -259,47 +429,35 @@ SyncModuleWorker.prototype._unpublished = function* (name, unpublishedInfo) {
|
|||||||
key && keys.push(key);
|
key && keys.push(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (keys.length === 0) {
|
if (keys.length > 0) {
|
||||||
return;
|
try {
|
||||||
}
|
yield keys.map(function (key) {
|
||||||
|
return nfs.remove(key);
|
||||||
try {
|
});
|
||||||
yield keys.map(function (key) {
|
} catch (err) {
|
||||||
return nfs.remove(key);
|
// ignore error here
|
||||||
});
|
this.log(' [%s] delete nfs files: %j error: %s: %s',
|
||||||
} catch (err) {
|
name, keys, err.name, err.message);
|
||||||
// ignore error here
|
}
|
||||||
this.log(' [%s] delete nfs files: %j error: %s: %s',
|
|
||||||
name, keys, err.name, err.message);
|
|
||||||
}
|
}
|
||||||
this.log(' [%s] delete nfs files: %j success', name, keys);
|
this.log(' [%s] delete nfs files: %j success', name, keys);
|
||||||
};
|
};
|
||||||
|
|
||||||
SyncModuleWorker.prototype._isLocalModule = function (mods) {
|
|
||||||
for (var i = 0; i < mods.length; i++) {
|
|
||||||
var r = mods[i];
|
|
||||||
if (r.package && r.package._publish_on_cnpm) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
SyncModuleWorker.prototype._sync = function* (name, pkg) {
|
SyncModuleWorker.prototype._sync = function* (name, pkg) {
|
||||||
var username = this.username;
|
|
||||||
var that = this;
|
var that = this;
|
||||||
|
|
||||||
var hasModules = false;
|
var hasModules = false;
|
||||||
var result = yield [
|
var result = yield [
|
||||||
Module.listByName(name),
|
packageService.listModulesByName(name),
|
||||||
Module.listTags(name),
|
packageService.listModuleTags(name),
|
||||||
_listStarUsers(name)
|
_listStarUsers(name),
|
||||||
|
packageService.listPublicModuleMaintainers(name),
|
||||||
];
|
];
|
||||||
var moduleRows = result[0];
|
var moduleRows = result[0];
|
||||||
var tagRows = result[1];
|
var tagRows = result[1];
|
||||||
var existsStarUsers = result[2];
|
var existsStarUsers = result[2];
|
||||||
|
var existsNpmMaintainers = result[3];
|
||||||
|
|
||||||
if (that._isLocalModule(moduleRows)) {
|
if (common.isLocalModule(moduleRows)) {
|
||||||
// publish on cnpm, dont sync this version package
|
// publish on cnpm, dont sync this version package
|
||||||
that.log(' [%s] publish on local cnpm registry, don\'t sync', name);
|
that.log(' [%s] publish on local cnpm registry, don\'t sync', name);
|
||||||
return [];
|
return [];
|
||||||
@@ -314,10 +472,6 @@ SyncModuleWorker.prototype._sync = function* (name, pkg) {
|
|||||||
// package json parse error
|
// package json parse error
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (r.version === 'next') {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!map.latest) {
|
if (!map.latest) {
|
||||||
map.latest = r;
|
map.latest = r;
|
||||||
}
|
}
|
||||||
@@ -342,25 +496,58 @@ SyncModuleWorker.prototype._sync = function* (name, pkg) {
|
|||||||
var missingStarUsers = [];
|
var missingStarUsers = [];
|
||||||
var npmUsernames = {};
|
var npmUsernames = {};
|
||||||
var missingDeprecateds = [];
|
var missingDeprecateds = [];
|
||||||
|
// [[user, 'add or remove'], ...]
|
||||||
|
var diffNpmMaintainers = [];
|
||||||
|
|
||||||
|
// find out new maintainers
|
||||||
|
var pkgMaintainers = pkg.maintainers || [];
|
||||||
|
if (Array.isArray(pkgMaintainers)) {
|
||||||
|
var existsMap = {};
|
||||||
|
var originalMap = {};
|
||||||
|
for (var i = 0; i < existsNpmMaintainers.length; i++) {
|
||||||
|
var user = existsNpmMaintainers[i];
|
||||||
|
existsMap[user] = true;
|
||||||
|
}
|
||||||
|
for (var i = 0; i < pkgMaintainers.length; i++) {
|
||||||
|
var item = pkgMaintainers[i];
|
||||||
|
originalMap[item.name] = item;
|
||||||
|
npmUsernames[item.name.toLowerCase()] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// find add users
|
||||||
|
for (var i = 0; i < pkgMaintainers.length; i++) {
|
||||||
|
var item = pkgMaintainers[i];
|
||||||
|
if (!existsMap[item.name]) {
|
||||||
|
diffNpmMaintainers.push([item.name, 'add']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// find remove users
|
||||||
|
for (var i = 0; i < existsNpmMaintainers.length; i++) {
|
||||||
|
var user = existsNpmMaintainers[i];
|
||||||
|
if (!originalMap[user]) {
|
||||||
|
diffNpmMaintainers.push([user, 'remove']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// find out all user names
|
// find out all user names
|
||||||
for (var v in pkg.versions) {
|
for (var v in pkg.versions) {
|
||||||
var p = pkg.versions[v];
|
var p = pkg.versions[v];
|
||||||
|
|
||||||
var maintainers = p.maintainers || [];
|
var maintainers = p.maintainers || [];
|
||||||
if (maintainers && !Array.isArray(maintainers)) {
|
if (!Array.isArray(maintainers)) {
|
||||||
// http://r.cnpmjs.org/jasmine-node
|
// http://r.cnpmjs.org/jasmine-node
|
||||||
// TODO: "maintainers": "Martin H膫陇ger <martin.haeger@gmail.com>",
|
// TODO: "maintainers": "Martin H膫陇ger <martin.haeger@gmail.com>",
|
||||||
maintainers = [maintainers];
|
maintainers = [maintainers];
|
||||||
}
|
}
|
||||||
|
for (var i = 0; i < maintainers.length; i++) {
|
||||||
maintainers.forEach(pushName);
|
var m = maintainers[i];
|
||||||
}
|
if (m.name) {
|
||||||
function pushName(m) {
|
npmUsernames[m.name.toLowerCase()] = 1;
|
||||||
if (m.name) {
|
}
|
||||||
npmUsernames[m.name.toLowerCase()] = 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the missing star users
|
// get the missing star users
|
||||||
var starUsers = pkg.users || {};
|
var starUsers = pkg.users || {};
|
||||||
for (var k in starUsers) {
|
for (var k in starUsers) {
|
||||||
@@ -374,40 +561,6 @@ SyncModuleWorker.prototype._sync = function* (name, pkg) {
|
|||||||
var times = pkg.time || {};
|
var times = pkg.time || {};
|
||||||
pkg.versions = pkg.versions || {};
|
pkg.versions = pkg.versions || {};
|
||||||
var remoteVersionNames = Object.keys(pkg.versions);
|
var remoteVersionNames = Object.keys(pkg.versions);
|
||||||
if (remoteVersionNames.length === 0) {
|
|
||||||
that.log(' [%s] no times and no versions, hasModules: %s', name, hasModules);
|
|
||||||
if (!hasModules) {
|
|
||||||
// save a next module
|
|
||||||
var maintainer = pkg.maintainers && pkg.maintainers[0];
|
|
||||||
if (maintainer && maintainer.name) {
|
|
||||||
maintainer = maintainer.name;
|
|
||||||
}
|
|
||||||
if (!maintainer) {
|
|
||||||
maintainer = '-';
|
|
||||||
}
|
|
||||||
var nextMod = {
|
|
||||||
name: name,
|
|
||||||
version: 'next',
|
|
||||||
author: maintainer,
|
|
||||||
package: {
|
|
||||||
name: name,
|
|
||||||
version: 'next',
|
|
||||||
description: pkg.description || '',
|
|
||||||
readme: pkg.readme || '',
|
|
||||||
maintainers: pkg.maintainers || {
|
|
||||||
name: maintainer
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
try {
|
|
||||||
var result = yield Module.add(nextMod);
|
|
||||||
that.log(' [%s] save next module, %j', name, result);
|
|
||||||
} catch (err) {
|
|
||||||
that.log(' [%s] save next module error %s', err.message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var remoteVersionNameMap = {};
|
var remoteVersionNameMap = {};
|
||||||
|
|
||||||
// find out missing versions
|
// find out missing versions
|
||||||
@@ -428,13 +581,8 @@ SyncModuleWorker.prototype._sync = function* (name, pkg) {
|
|||||||
if (!version.maintainers || !version.maintainers[0]) {
|
if (!version.maintainers || !version.maintainers[0]) {
|
||||||
version.maintainers = pkg.maintainers;
|
version.maintainers = pkg.maintainers;
|
||||||
}
|
}
|
||||||
var sourceAuthor = version.maintainers && version.maintainers[0] &&
|
|
||||||
version.maintainers[0].name || exists.author;
|
|
||||||
|
|
||||||
if (exists.package &&
|
if (exists.package &&
|
||||||
exists.package.dist.shasum === version.dist.shasum &&
|
exists.package.dist.shasum === version.dist.shasum) {
|
||||||
exists.author === sourceAuthor) {
|
|
||||||
// * author make sure equal
|
|
||||||
// * shasum make sure equal
|
// * shasum make sure equal
|
||||||
if ((version.publish_time === exists.publish_time) ||
|
if ((version.publish_time === exists.publish_time) ||
|
||||||
(!version.publish_time && exists.publish_time)) {
|
(!version.publish_time && exists.publish_time)) {
|
||||||
@@ -516,11 +664,11 @@ SyncModuleWorker.prototype._sync = function* (name, pkg) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
var result = yield that._syncOneVersion(index, syncModule);
|
yield* that._syncOneVersion(index, syncModule);
|
||||||
syncedVersionNames.push(syncModule.version);
|
syncedVersionNames.push(syncModule.version);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
that.log(' [%s:%d] sync error, version: %s, %s: %s',
|
that.log(' [%s:%d] sync error, version: %s, %s: %s',
|
||||||
syncModule.name, index, syncModule.version, err.name, err.message);
|
syncModule.name, index, syncModule.version, err.name, err.stack);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -530,22 +678,21 @@ SyncModuleWorker.prototype._sync = function* (name, pkg) {
|
|||||||
} else {
|
} else {
|
||||||
that.log(' [%s] %d versions: %j need to deleted',
|
that.log(' [%s] %d versions: %j need to deleted',
|
||||||
name, deletedVersionNames.length, deletedVersionNames);
|
name, deletedVersionNames.length, deletedVersionNames);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
yield Module.removeByNameAndVersions(name, deletedVersionNames);
|
yield* packageService.removeModulesByNameAndVersions(name, deletedVersionNames);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
that.log(' [%s] delete error, %s: %s', name, err.name, err.message);
|
that.log(' [%s] delete error, %s: %s', name, err.name, err.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// sync missing descriptions
|
// sync missing descriptions
|
||||||
function *syncDes() {
|
function* syncDes() {
|
||||||
if (missingDescriptions.length === 0) {
|
if (missingDescriptions.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
that.log(' [%s] saving %d descriptions', name, missingDescriptions.length);
|
that.log(' [%s] saving %d descriptions', name, missingDescriptions.length);
|
||||||
var res = yield gather(missingDescriptions.map(function (item) {
|
var res = yield gather(missingDescriptions.map(function (item) {
|
||||||
return Module.updateDescription(item.id, item.description);
|
return packageService.updateModuleDescription(item.id, item.description);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
for (var i = 0; i < res.length; i++) {
|
for (var i = 0; i < res.length; i++) {
|
||||||
@@ -564,7 +711,7 @@ SyncModuleWorker.prototype._sync = function* (name, pkg) {
|
|||||||
// sync missing tags
|
// sync missing tags
|
||||||
function* syncTag() {
|
function* syncTag() {
|
||||||
if (deletedTags.length > 0) {
|
if (deletedTags.length > 0) {
|
||||||
yield* Module.removeTagsByNames(name, deletedTags);
|
yield* packageService.removeModuleTagsByNames(name, deletedTags);
|
||||||
that.log(' [%s] deleted %d tags: %j',
|
that.log(' [%s] deleted %d tags: %j',
|
||||||
name, deletedTags.length, deletedTags);
|
name, deletedTags.length, deletedTags);
|
||||||
}
|
}
|
||||||
@@ -574,9 +721,8 @@ SyncModuleWorker.prototype._sync = function* (name, pkg) {
|
|||||||
}
|
}
|
||||||
that.log(' [%s] adding %d tags', name, missingTags.length);
|
that.log(' [%s] adding %d tags', name, missingTags.length);
|
||||||
// sync tags
|
// sync tags
|
||||||
|
|
||||||
var res = yield gather(missingTags.map(function (item) {
|
var res = yield gather(missingTags.map(function (item) {
|
||||||
return Module.addTag(name, item[0], item[1]);
|
return packageService.addModuleTag(name, item[0], item[1]);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
for (var i = 0; i < res.length; i++) {
|
for (var i = 0; i < res.length; i++) {
|
||||||
@@ -593,14 +739,14 @@ SyncModuleWorker.prototype._sync = function* (name, pkg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// sycn missing readme
|
// sycn missing readme
|
||||||
function *syncReadme() {
|
function* syncReadme() {
|
||||||
if (missingReadmes.length === 0) {
|
if (missingReadmes.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
that.log(' [%s] saving %d readmes', name, missingReadmes.length);
|
that.log(' [%s] saving %d readmes', name, missingReadmes.length);
|
||||||
|
|
||||||
var res = yield gather(missingReadmes.map(function (item) {
|
var res = yield gather(missingReadmes.map(function (item) {
|
||||||
return Module.updateReadme(item.id, item.readme);
|
return packageService.updateModuleReadme(item.id, item.readme);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
for (var i = 0; i < res.length; i++) {
|
for (var i = 0; i < res.length; i++) {
|
||||||
@@ -621,7 +767,7 @@ SyncModuleWorker.prototype._sync = function* (name, pkg) {
|
|||||||
that.log(' [%s] saving %d Deprecated fields', name, missingDeprecateds.length);
|
that.log(' [%s] saving %d Deprecated fields', name, missingDeprecateds.length);
|
||||||
|
|
||||||
var res = yield gather(missingDeprecateds.map(function (item) {
|
var res = yield gather(missingDeprecateds.map(function (item) {
|
||||||
return Module.updatePackageFields(item.id, {
|
return packageService.updateModulePackageFields(item.id, {
|
||||||
deprecated: item.deprecated
|
deprecated: item.deprecated
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
@@ -637,13 +783,13 @@ SyncModuleWorker.prototype._sync = function* (name, pkg) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function *syncMissingUsers() {
|
function* syncMissingUsers() {
|
||||||
var missingUsers = [];
|
var missingUsers = [];
|
||||||
var names = Object.keys(npmUsernames);
|
var names = Object.keys(npmUsernames);
|
||||||
if (names.length === 0) {
|
if (names.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var rows = yield *User.listByNames(names);
|
var rows = yield* User.listByNames(names);
|
||||||
var map = {};
|
var map = {};
|
||||||
rows.forEach(function (r) {
|
rows.forEach(function (r) {
|
||||||
map[r.name] = r;
|
map[r.name] = r;
|
||||||
@@ -651,7 +797,9 @@ SyncModuleWorker.prototype._sync = function* (name, pkg) {
|
|||||||
names.forEach(function (username) {
|
names.forEach(function (username) {
|
||||||
var r = map[username];
|
var r = map[username];
|
||||||
if (!r || !r.json) {
|
if (!r || !r.json) {
|
||||||
missingUsers.push(username);
|
if (username[0] !== '"' && username[0] !== "'") {
|
||||||
|
missingUsers.push(username);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -675,27 +823,53 @@ SyncModuleWorker.prototype._sync = function* (name, pkg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// sync missing star users
|
// sync missing star users
|
||||||
function *syncMissingStarUsers() {
|
function* syncMissingStarUsers() {
|
||||||
if (missingStarUsers.length === 0) {
|
if (missingStarUsers.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
that.log(' [%s] saving %d star users', name, missingStarUsers.length);
|
that.log(' [%s] saving %d star users', name, missingStarUsers.length);
|
||||||
var res = yield gather(missingStarUsers.map(function (username) {
|
var res = yield gather(missingStarUsers.map(function (username) {
|
||||||
return _addStar(name, username);
|
return packageService.addStar(name, username);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
for (var i = 0; i < res.length; i++) {
|
for (var i = 0; i < res.length; i++) {
|
||||||
var r = res[i];
|
var r = res[i];
|
||||||
if (r.error) {
|
if (r.error) {
|
||||||
that.log(' add star user error, %s', r.error.message);
|
that.log(' add star user error, %s', r.error.stack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sync diff npm package maintainers
|
||||||
|
function* syncNpmPackageMaintainers() {
|
||||||
|
if (diffNpmMaintainers.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
that.log(' [%s] syncing %d diff package maintainers: %j',
|
||||||
|
name, diffNpmMaintainers.length, diffNpmMaintainers);
|
||||||
|
var res = yield gather(diffNpmMaintainers.map(function (item) {
|
||||||
|
return _saveMaintainer(name, item[0], item[1]);
|
||||||
|
}));
|
||||||
|
|
||||||
|
for (var i = 0; i < res.length; i++) {
|
||||||
|
var r = res[i];
|
||||||
|
if (r.error) {
|
||||||
|
that.log(' save package maintainer error, %s', r.error.stack);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
yield [
|
yield [
|
||||||
syncDes(), syncTag(), syncReadme(), syncDeprecateds(),
|
syncDes(),
|
||||||
syncMissingStarUsers(), syncMissingUsers()];
|
syncTag(),
|
||||||
|
syncReadme(),
|
||||||
|
syncDeprecateds(),
|
||||||
|
syncMissingStarUsers(),
|
||||||
|
syncMissingUsers(),
|
||||||
|
syncNpmPackageMaintainers(),
|
||||||
|
];
|
||||||
return syncedVersionNames;
|
return syncedVersionNames;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -713,17 +887,22 @@ SyncModuleWorker.prototype._syncOneVersion = function *(versionIndex, sourcePack
|
|||||||
timeout: 600000, // 10 minutes download
|
timeout: 600000, // 10 minutes download
|
||||||
headers: {
|
headers: {
|
||||||
'user-agent': USER_AGENT
|
'user-agent': USER_AGENT
|
||||||
}
|
},
|
||||||
|
gzip: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
var dependencies = Object.keys(sourcePackage.dependencies || {});
|
var dependencies = Object.keys(sourcePackage.dependencies || {});
|
||||||
var devDependencies = Object.keys(sourcePackage.devDependencies || {});
|
var devDependencies = [];
|
||||||
|
if (this.syncDevDependencies) {
|
||||||
|
devDependencies = Object.keys(sourcePackage.devDependencies || {});
|
||||||
|
}
|
||||||
|
|
||||||
that.log(' [%s:%d] syncing, version: %s, dist: %j, no deps: %s, ' +
|
that.log(' [%s:%d] syncing, version: %s, dist: %j, no deps: %s, ' +
|
||||||
'publish on cnpm: %s, dependencies: %d, devDependencies: %d',
|
'publish on cnpm: %s, dependencies: %d, devDependencies: %d, syncDevDependencies: %s',
|
||||||
sourcePackage.name, versionIndex, sourcePackage.version,
|
sourcePackage.name, versionIndex, sourcePackage.version,
|
||||||
sourcePackage.dist, that.noDep, that._publish,
|
sourcePackage.dist, that.noDep, that._publish,
|
||||||
dependencies.length, devDependencies.length);
|
dependencies.length,
|
||||||
|
devDependencies.length, this.syncDevDependencies);
|
||||||
|
|
||||||
if (dependencies.length > config.maxDependencies) {
|
if (dependencies.length > config.maxDependencies) {
|
||||||
dependencies = dependencies.slice(0, config.maxDependencies);
|
dependencies = dependencies.slice(0, config.maxDependencies);
|
||||||
@@ -744,20 +923,14 @@ SyncModuleWorker.prototype._syncOneVersion = function *(versionIndex, sourcePack
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add module dependence
|
// add module dependence
|
||||||
try {
|
yield* packageService.addDependencies(sourcePackage.name, dependencies);
|
||||||
yield dependencies.map(function (depName) {
|
|
||||||
return ModuleDeps.add(depName, sourcePackage.name);
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
|
|
||||||
var shasum = crypto.createHash('sha1');
|
var shasum = crypto.createHash('sha1');
|
||||||
var dataSize = 0;
|
var dataSize = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// get tarball
|
// get tarball
|
||||||
var r = yield *urllib.request(downurl, options);
|
var r = yield urllib.request(downurl, options);
|
||||||
var statusCode = r.status || -1;
|
var statusCode = r.status || -1;
|
||||||
// https://github.com/cnpm/cnpmjs.org/issues/325
|
// https://github.com/cnpm/cnpmjs.org/issues/325
|
||||||
// if (statusCode === 404) {
|
// if (statusCode === 404) {
|
||||||
@@ -806,6 +979,7 @@ SyncModuleWorker.prototype._syncOneVersion = function *(versionIndex, sourcePack
|
|||||||
shasum: shasum
|
shasum: shasum
|
||||||
};
|
};
|
||||||
// upload to NFS
|
// upload to NFS
|
||||||
|
logger.syncInfo('[sync_module_worker] uploading %j to nfs', options);
|
||||||
var result = yield nfs.upload(filepath, options);
|
var result = yield nfs.upload(filepath, options);
|
||||||
return yield *afterUpload(result);
|
return yield *afterUpload(result);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -850,7 +1024,7 @@ SyncModuleWorker.prototype._syncOneVersion = function *(versionIndex, sourcePack
|
|||||||
}
|
}
|
||||||
|
|
||||||
mod.package.dist = dist;
|
mod.package.dist = dist;
|
||||||
var r = yield Module.add(mod);
|
var r = yield* packageService.saveModule(mod);
|
||||||
|
|
||||||
that.log(' [%s:%s] done, insertId: %s, author: %s, version: %s, '
|
that.log(' [%s:%s] done, insertId: %s, author: %s, version: %s, '
|
||||||
+ 'size: %d, publish_time: %j, publish on cnpm: %s',
|
+ 'size: %d, publish_time: %j, publish on cnpm: %s',
|
||||||
@@ -866,33 +1040,16 @@ SyncModuleWorker.prototype._syncOneVersion = function *(versionIndex, sourcePack
|
|||||||
|
|
||||||
SyncModuleWorker.sync = function* (name, username, options) {
|
SyncModuleWorker.sync = function* (name, username, options) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
var result = yield npm.request('/' + name);
|
var result = yield* logService.create({name: name, username: username});
|
||||||
var pkg = result.data;
|
|
||||||
if (result.status === 404 &&
|
|
||||||
(!pkg.time || !pkg.time.unpublished || !pkg.time.unpublished.time)) {
|
|
||||||
pkg = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!pkg || !pkg._id) {
|
|
||||||
return {
|
|
||||||
ok: false,
|
|
||||||
pkg: pkg,
|
|
||||||
statusCode: 404
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = yield Log.create({name: name, username: username});
|
|
||||||
var worker = new SyncModuleWorker({
|
var worker = new SyncModuleWorker({
|
||||||
logId: result.id,
|
logId: result.id,
|
||||||
|
type: options.type,
|
||||||
name: name,
|
name: name,
|
||||||
username: username,
|
username: username,
|
||||||
noDep: options.noDep,
|
noDep: options.noDep,
|
||||||
publish: options.publish,
|
publish: options.publish,
|
||||||
|
syncUpstreamFirst: options.syncUpstreamFirst,
|
||||||
});
|
});
|
||||||
worker.start();
|
worker.start();
|
||||||
return {
|
return result.id;
|
||||||
ok: true,
|
|
||||||
logId: result.id,
|
|
||||||
pkg: pkg
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
@@ -15,15 +15,15 @@
|
|||||||
* Module dependencies.
|
* Module dependencies.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var Total = require('../proxy/total');
|
var Total = require('../services/total');
|
||||||
var Download = require('./download');
|
|
||||||
var version = require('../package.json').version;
|
var version = require('../package.json').version;
|
||||||
var config = require('../config');
|
var config = require('../config');
|
||||||
|
var getDownloadTotal = require('./utils').getDownloadTotal;
|
||||||
|
|
||||||
var startTime = '' + Date.now();
|
var startTime = '' + Date.now();
|
||||||
|
|
||||||
exports.show = function *() {
|
module.exports = function* showTotal() {
|
||||||
var r = yield [Total.get(), Download.total()];
|
var r = yield [Total.get(), getDownloadTotal()];
|
||||||
var total = r[0];
|
var total = r[0];
|
||||||
var download = r[1];
|
var download = r[1];
|
||||||
|
|
||||||
|
|||||||
@@ -18,13 +18,20 @@ var debug = require('debug')('cnpmjs.org:controllers:utils');
|
|||||||
var path = require('path');
|
var path = require('path');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var utility = require('utility');
|
var utility = require('utility');
|
||||||
var ms = require('ms');
|
var ms = require('humanize-ms');
|
||||||
|
var moment = require('moment');
|
||||||
|
var downloadTotalService = require('../services/download_total');
|
||||||
var nfs = require('../common/nfs');
|
var nfs = require('../common/nfs');
|
||||||
var config = require('../config');
|
var config = require('../config');
|
||||||
|
|
||||||
var DOWNLOAD_TIMEOUT = ms('10m');
|
var DOWNLOAD_TIMEOUT = ms('10m');
|
||||||
|
|
||||||
exports.downloadAsReadStream = function* (key) {
|
exports.downloadAsReadStream = function* (key) {
|
||||||
|
var options = { timeout: DOWNLOAD_TIMEOUT };
|
||||||
|
if (nfs.createDownloadStream) {
|
||||||
|
return yield nfs.createDownloadStream(key, options);
|
||||||
|
}
|
||||||
|
|
||||||
var tmpPath = path.join(config.uploadDir,
|
var tmpPath = path.join(config.uploadDir,
|
||||||
utility.randomString() + key.replace(/\//g, '-'));
|
utility.randomString() + key.replace(/\//g, '-'));
|
||||||
function cleanup() {
|
function cleanup() {
|
||||||
@@ -33,7 +40,7 @@ exports.downloadAsReadStream = function* (key) {
|
|||||||
}
|
}
|
||||||
debug('downloadAsReadStream() %s to %s', key, tmpPath);
|
debug('downloadAsReadStream() %s to %s', key, tmpPath);
|
||||||
try {
|
try {
|
||||||
yield nfs.download(key, tmpPath, {timeout: DOWNLOAD_TIMEOUT});
|
yield nfs.download(key, tmpPath, options);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
debug('downloadAsReadStream() %s to %s error: %s', key, tmpPath, err.stack);
|
debug('downloadAsReadStream() %s to %s error: %s', key, tmpPath, err.stack);
|
||||||
cleanup();
|
cleanup();
|
||||||
@@ -44,3 +51,116 @@ exports.downloadAsReadStream = function* (key) {
|
|||||||
tarball.once('end', cleanup);
|
tarball.once('end', cleanup);
|
||||||
return tarball;
|
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('week');
|
||||||
|
var lastweekEnd = lastweekStart.clone().endOf('week').format('YYYY-MM-DD');
|
||||||
|
var lastmonthEnd = start.clone().endOf('month').format('YYYY-MM-DD');
|
||||||
|
var thismonthStart = end.clone().startOf('month').format('YYYY-MM-DD');
|
||||||
|
var thisweekStart = end.clone().startOf('week').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];
|
||||||
|
if (name) {
|
||||||
|
args.unshift(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
var rows = yield* downloadTotalService[method].apply(downloadTotalService, args);
|
||||||
|
var download = {
|
||||||
|
today: 0,
|
||||||
|
thisweek: 0,
|
||||||
|
thismonth: 0,
|
||||||
|
lastday: 0,
|
||||||
|
lastweek: 0,
|
||||||
|
lastmonth: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
for (var i = 0; i < rows.length; i++) {
|
||||||
|
var r = rows[i];
|
||||||
|
if (r.date === end) {
|
||||||
|
download.today += r.count;
|
||||||
|
}
|
||||||
|
if (r.date >= thismonthStart) {
|
||||||
|
download.thismonth += r.count;
|
||||||
|
}
|
||||||
|
if (r.date >= thisweekStart) {
|
||||||
|
download.thisweek += r.count;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (r.date === lastday) {
|
||||||
|
download.lastday += r.count;
|
||||||
|
}
|
||||||
|
if (r.date >= lastweekStart && r.date <= lastweekEnd) {
|
||||||
|
download.lastweek += r.count;
|
||||||
|
}
|
||||||
|
if (r.date >= start && r.date <= lastmonthEnd) {
|
||||||
|
download.lastmonth += r.count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return download;
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.setLicense = function (pkg) {
|
||||||
|
var license;
|
||||||
|
license = pkg.license || pkg.licenses || pkg.licence || pkg.licences;
|
||||||
|
if (!license) {
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(license)) {
|
||||||
|
license = license[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof license === 'object') {
|
||||||
|
pkg.license = {
|
||||||
|
name: license.name || license.type,
|
||||||
|
url: license.url
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof license === 'string') {
|
||||||
|
if (license.match(/(http|https)(:\/\/)/ig)) {
|
||||||
|
pkg.license = {
|
||||||
|
name: license,
|
||||||
|
url: license
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
pkg.license = {
|
||||||
|
url: exports.getOssLicenseUrlFromName(license),
|
||||||
|
name: license
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.getOssLicenseUrlFromName = function (name) {
|
||||||
|
var base = 'http://opensource.org/licenses/';
|
||||||
|
|
||||||
|
var licenseMap = {
|
||||||
|
'bsd': 'BSD-2-Clause',
|
||||||
|
'mit': 'MIT',
|
||||||
|
'x11': 'MIT',
|
||||||
|
'mit/x11': 'MIT',
|
||||||
|
'apache 2.0': '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',
|
||||||
|
'lgplv2.1': 'LGPL-2.1',
|
||||||
|
'lgplv2': 'LGPL-2.1'
|
||||||
|
};
|
||||||
|
|
||||||
|
return licenseMap[name.toLowerCase()] ?
|
||||||
|
base + licenseMap[name.toLowerCase()] : base + name;
|
||||||
|
};
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
* MIT Licensed
|
* MIT Licensed
|
||||||
*
|
*
|
||||||
* Authors:
|
* Authors:
|
||||||
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
@@ -14,16 +14,20 @@
|
|||||||
* Module dependencies.
|
* Module dependencies.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
var utility = require('utility');
|
||||||
|
var util = require('util');
|
||||||
var config = require('../../config');
|
var config = require('../../config');
|
||||||
var Module = require('../../proxy/module');
|
var packageService = require('../../services/package');
|
||||||
|
var DownloadTotal = require('../../services/download_total');
|
||||||
|
|
||||||
exports.version = function* (next) {
|
exports.version = function* () {
|
||||||
var color = 'lightgrey';
|
var color = 'lightgrey';
|
||||||
var version = 'invalid';
|
var version = 'invalid';
|
||||||
var name = this.params[0];
|
var name = this.params[0];
|
||||||
var latestTag = yield* Module.getTag(name, 'latest');
|
var tag = this.query.tag || 'latest';
|
||||||
if (latestTag) {
|
var info = yield* packageService.getModuleByTag(name, tag);
|
||||||
version = latestTag.version;
|
if (info) {
|
||||||
|
version = info.version;
|
||||||
if (/^0\.0\./.test(version)) {
|
if (/^0\.0\./.test(version)) {
|
||||||
// <0.1.0 & >=0.0.0
|
// <0.1.0 & >=0.0.0
|
||||||
color = 'red';
|
color = 'red';
|
||||||
@@ -36,12 +40,20 @@ exports.version = function* (next) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var url = 'https://img.shields.io/badge/' + config.badgeSubject + '-' + version + '-' + color + '.svg';
|
var subject = config.badgeSubject.replace(/\-/g, '--');
|
||||||
if (this.querystring) {
|
version = version.replace(/\-/g, '--');
|
||||||
url += '?' + this.querystring;
|
var style = this.query.style || 'flat-square';
|
||||||
} else {
|
var url = util.format('https://img.shields.io/badge/%s-%s-%s.svg?style=%s',
|
||||||
url += '?style=flat-square';
|
subject, version, color, utility.encodeURIComponent(style));
|
||||||
}
|
this.redirect(url);
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.downloads = function* () {
|
||||||
|
// https://img.shields.io/badge/downloads-100k/month-brightgreen.svg?style=flat-square
|
||||||
|
var name = this.params[0];
|
||||||
|
var count = yield DownloadTotal.getTotalByName(name);
|
||||||
|
var style = this.query.style || 'flat-square';
|
||||||
|
var url = util.format('https://img.shields.io/badge/downloads-%s-brightgreen.svg?style=%s',
|
||||||
|
count, utility.encodeURIComponent(style));
|
||||||
this.redirect(url);
|
this.redirect(url);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,85 +0,0 @@
|
|||||||
/**!
|
|
||||||
* cnpmjs.org - controllers/web/dist.js
|
|
||||||
*
|
|
||||||
* Copyright(c) cnpmjs.org and other contributors.
|
|
||||||
* MIT Licensed
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
|
||||||
*/
|
|
||||||
|
|
||||||
"use strict";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var debug = require('debug')('cnpmjs.org:controllers:web:dist');
|
|
||||||
var mime = require('mime');
|
|
||||||
var Dist = require('../../proxy/dist');
|
|
||||||
var config = require('../../config');
|
|
||||||
var downloadAsReadStream = require('../utils').downloadAsReadStream;
|
|
||||||
|
|
||||||
function padding(max, current, pad) {
|
|
||||||
pad = pad || ' ';
|
|
||||||
var left = max - current;
|
|
||||||
var str = '';
|
|
||||||
for (var i = 0; i < left; i++) {
|
|
||||||
str += pad;
|
|
||||||
}
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.list = function* (next) {
|
|
||||||
var params = this.params;
|
|
||||||
var url = params[0];
|
|
||||||
if (!url) {
|
|
||||||
// GET /dist => /dist/
|
|
||||||
return this.redirect('/dist/');
|
|
||||||
}
|
|
||||||
|
|
||||||
var isDir = url[url.length - 1] === '/';
|
|
||||||
if (!isDir) {
|
|
||||||
return yield* download.call(this, next);
|
|
||||||
}
|
|
||||||
|
|
||||||
var items = yield* Dist.listdir(url);
|
|
||||||
if (url === '/') {
|
|
||||||
// phantomjs/
|
|
||||||
items.push({
|
|
||||||
name: 'phantomjs/',
|
|
||||||
date: '',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
yield this.render('dist', {
|
|
||||||
title: 'Mirror index of ' + config.disturl + url,
|
|
||||||
disturl: config.disturl,
|
|
||||||
dirname: url,
|
|
||||||
items: items,
|
|
||||||
padding: padding
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
function* download(next) {
|
|
||||||
var fullname = this.params[0];
|
|
||||||
var info = yield* Dist.getfile(fullname);
|
|
||||||
debug('download %s got %j', fullname, info);
|
|
||||||
if (!info || !info.url) {
|
|
||||||
return yield* next;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (info.url.indexOf('http') === 0) {
|
|
||||||
return this.redirect(info.url);
|
|
||||||
}
|
|
||||||
|
|
||||||
// download it from nfs
|
|
||||||
if (typeof info.size === 'number') {
|
|
||||||
this.length = info.size;
|
|
||||||
}
|
|
||||||
this.type = mime.lookup(info.url);
|
|
||||||
this.attachment = info.name;
|
|
||||||
this.etag = info.sha1;
|
|
||||||
|
|
||||||
this.body = yield* downloadAsReadStream(info.url);
|
|
||||||
}
|
|
||||||
@@ -1,347 +0,0 @@
|
|||||||
/*!
|
|
||||||
* cnpmjs.org - controllers/web/package.js
|
|
||||||
*
|
|
||||||
* Copyright(c) cnpmjs.org and other contributors.
|
|
||||||
* MIT Licensed
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var debug = require('debug')('cnpmjs.org:controllers:web:package');
|
|
||||||
var bytes = require('bytes');
|
|
||||||
var giturl = require('giturl');
|
|
||||||
var moment = require('moment');
|
|
||||||
var eventproxy = require('eventproxy');
|
|
||||||
var semver = require('semver');
|
|
||||||
var marked = require('marked');
|
|
||||||
var gravatar = require('gravatar');
|
|
||||||
var humanize = require('humanize-number');
|
|
||||||
var config = require('../../config');
|
|
||||||
var Module = require('../../proxy/module');
|
|
||||||
var down = require('../download');
|
|
||||||
var sync = require('../sync');
|
|
||||||
var Log = require('../../proxy/module_log');
|
|
||||||
var ModuleDeps = require('../../proxy/module_deps');
|
|
||||||
var setDownloadURL = require('../../lib/common').setDownloadURL;
|
|
||||||
var ModuleStar = require('../../proxy/module_star');
|
|
||||||
var packageService = require('../../services/package');
|
|
||||||
var ModuleUnpublished = require('../../proxy/module_unpublished');
|
|
||||||
|
|
||||||
exports.display = function* (next) {
|
|
||||||
var 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];
|
|
||||||
debug('display %s with %j', name, params);
|
|
||||||
|
|
||||||
var getPackageMethod;
|
|
||||||
var getPackageArgs;
|
|
||||||
var version = semver.valid(tag || '');
|
|
||||||
if (version) {
|
|
||||||
getPackageMethod = 'get';
|
|
||||||
getPackageArgs = [name, version];
|
|
||||||
} else {
|
|
||||||
getPackageMethod = 'getByTag';
|
|
||||||
getPackageArgs = [name, tag || 'latest'];
|
|
||||||
}
|
|
||||||
|
|
||||||
var pkg = yield Module[getPackageMethod].apply(Module, getPackageArgs);
|
|
||||||
if (!pkg) {
|
|
||||||
var adaptName = yield* Module.getAdaptName(name);
|
|
||||||
if (adaptName) {
|
|
||||||
name = adaptName;
|
|
||||||
pkg = yield Module[getPackageMethod].apply(Module, [name, getPackageArgs[1]]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!pkg || !pkg.package) {
|
|
||||||
// check if unpublished
|
|
||||||
var unpublishedInfo = yield* ModuleUnpublished.get(name);
|
|
||||||
debug('show unpublished %j', unpublishedInfo);
|
|
||||||
if (unpublishedInfo) {
|
|
||||||
var data = {
|
|
||||||
name: 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];
|
|
||||||
if (maintainer.email) {
|
|
||||||
maintainer.gravatar = gravatar.url(maintainer.email, {s: '50', d: 'retro'}, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
yield this.render('package_unpublished', {
|
|
||||||
package: data
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
return yield* next;
|
|
||||||
}
|
|
||||||
|
|
||||||
var r = yield [
|
|
||||||
down.total(name),
|
|
||||||
ModuleDeps.list(name),
|
|
||||||
ModuleStar.listUsers(name),
|
|
||||||
packageService.listMaintainers(name)
|
|
||||||
];
|
|
||||||
var download = r[0];
|
|
||||||
var dependents = (r[1] || []).map(function (item) {
|
|
||||||
return item.deps;
|
|
||||||
});
|
|
||||||
var users = r[2];
|
|
||||||
var maintainers = r[3];
|
|
||||||
|
|
||||||
pkg.package.fromNow = moment(pkg.publish_time).fromNow();
|
|
||||||
pkg = pkg.package;
|
|
||||||
pkg.users = users;
|
|
||||||
pkg.readme = marked(pkg.readme || '');
|
|
||||||
if (!pkg.readme) {
|
|
||||||
pkg.readme = pkg.description || '';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (maintainers.length > 0) {
|
|
||||||
pkg.maintainers = maintainers;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pkg.maintainers) {
|
|
||||||
for (var i = 0; i < pkg.maintainers.length; i++) {
|
|
||||||
var maintainer = pkg.maintainers[i];
|
|
||||||
if (maintainer.email) {
|
|
||||||
maintainer.gravatar = gravatar.url(maintainer.email, {s: '50', d: 'retro'}, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pkg.contributors) {
|
|
||||||
// registry.cnpmjs.org/compressible
|
|
||||||
if (!Array.isArray(pkg.contributors)) {
|
|
||||||
pkg.contributors = [pkg.contributors];
|
|
||||||
}
|
|
||||||
for (var i = 0; i < pkg.contributors.length; i++) {
|
|
||||||
var contributor = pkg.contributors[i];
|
|
||||||
if (contributor.email) {
|
|
||||||
contributor.gravatar = gravatar.url(contributor.email, {s: '50', d: 'retro'}, true);
|
|
||||||
}
|
|
||||||
if (config.packagePageContributorSearch || !contributor.url) {
|
|
||||||
contributor.url = '/~' + encodeURIComponent(contributor.name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pkg.repository && pkg.repository.url) {
|
|
||||||
pkg.repository.weburl = giturl.parse(pkg.repository.url) || pkg.repository.url;
|
|
||||||
}
|
|
||||||
|
|
||||||
setLicense(pkg);
|
|
||||||
|
|
||||||
for (var k in download) {
|
|
||||||
download[k] = humanize(download[k]);
|
|
||||||
}
|
|
||||||
setDownloadURL(pkg, this, config.registryHost);
|
|
||||||
|
|
||||||
pkg.dependents = dependents;
|
|
||||||
|
|
||||||
if (pkg.dist) {
|
|
||||||
pkg.dist.size = bytes(pkg.dist.size || 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pkg.name !== orginalName) {
|
|
||||||
pkg.name = orginalName;
|
|
||||||
}
|
|
||||||
|
|
||||||
// pkg.engines = {
|
|
||||||
// "python": ">= 0.11.9",
|
|
||||||
// "node": ">= 0.11.9",
|
|
||||||
// "node1": ">= 0.8.9",
|
|
||||||
// "node2": ">= 0.10.9",
|
|
||||||
// "node3": ">= 0.6.9",
|
|
||||||
// };
|
|
||||||
if (pkg.engines) {
|
|
||||||
for (var k in pkg.engines) {
|
|
||||||
var engine = String(pkg.engines[k] || '').trim();
|
|
||||||
var color = 'blue';
|
|
||||||
if (k.indexOf('node') === 0) {
|
|
||||||
color = 'yellowgreen';
|
|
||||||
var 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';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pkg.engines[k] = {
|
|
||||||
version: engine,
|
|
||||||
title: k + ': ' + engine,
|
|
||||||
badgeURL: 'https://img.shields.io/badge/' + encodeURIComponent(k) +
|
|
||||||
'-' + encodeURIComponent(engine) + '-' + color + '.svg?style=flat-square',
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
yield this.render('package', {
|
|
||||||
title: 'Package - ' + pkg.name,
|
|
||||||
package: pkg,
|
|
||||||
download: download
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.search = function *(next) {
|
|
||||||
var params = this.params;
|
|
||||||
var word = params.word || params[0];
|
|
||||||
debug('search %j', word);
|
|
||||||
var result = yield Module.search(word);
|
|
||||||
|
|
||||||
var match = null;
|
|
||||||
for (var i = 0; i < result.searchMatchs.length; i++) {
|
|
||||||
var p = result.searchMatchs[i];
|
|
||||||
if (p.name === word) {
|
|
||||||
match = p;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// return a json result
|
|
||||||
if (this.query && this.query.type === 'json') {
|
|
||||||
this.body = {
|
|
||||||
keyword: word,
|
|
||||||
match: match,
|
|
||||||
packages: result.searchMatchs,
|
|
||||||
keywords: result.keywordMatchs,
|
|
||||||
};
|
|
||||||
this.type = 'application/json; charset=utf-8';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
yield this.render('search', {
|
|
||||||
title: 'Keyword - ' + word,
|
|
||||||
keyword: word,
|
|
||||||
match: match,
|
|
||||||
packages: result.searchMatchs,
|
|
||||||
keywords: result.keywordMatchs,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.rangeSearch = function *(next) {
|
|
||||||
var 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 Module.search(startKey, {limit: limit});
|
|
||||||
|
|
||||||
var packages = result.searchMatchs.concat(result.keywordMatchs);
|
|
||||||
|
|
||||||
var rows = [];
|
|
||||||
for (var i = 0; i < packages.length; i++) {
|
|
||||||
var p = packages[i];
|
|
||||||
var row = {
|
|
||||||
key: p.name,
|
|
||||||
count: 1,
|
|
||||||
value: {
|
|
||||||
name: p.name,
|
|
||||||
description: p.description,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
rows.push(row);
|
|
||||||
}
|
|
||||||
this.body = {
|
|
||||||
rows: rows
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.displaySync = function* (next) {
|
|
||||||
var name = this.params.name || this.params[0] || this.query.name;
|
|
||||||
yield this.render('sync', {
|
|
||||||
name: name,
|
|
||||||
title: 'Sync - ' + name
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.listPrivates = function* () {
|
|
||||||
var packages = yield Module.listPrivates();
|
|
||||||
yield this.render('private', {
|
|
||||||
title: 'private packages',
|
|
||||||
packages: packages
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
function setLicense(pkg) {
|
|
||||||
var license;
|
|
||||||
license = pkg.license || pkg.licenses || pkg.licence || pkg.licences;
|
|
||||||
if (!license) {
|
|
||||||
return ;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Array.isArray(license)) {
|
|
||||||
license = license[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof license === 'object') {
|
|
||||||
pkg.license = {
|
|
||||||
name: license.name || license.type,
|
|
||||||
url: license.url
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof license === 'string') {
|
|
||||||
if (license.match(/(http|https)(:\/\/)/ig)) {
|
|
||||||
pkg.license = {
|
|
||||||
name: license,
|
|
||||||
url: license
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
pkg.license = {
|
|
||||||
url: getOssLicenseUrlFromName(license),
|
|
||||||
name: license
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exports.setLicense = setLicense;
|
|
||||||
|
|
||||||
function getOssLicenseUrlFromName(name) {
|
|
||||||
var base = 'http://opensource.org/licenses/';
|
|
||||||
|
|
||||||
var licenseMap = {
|
|
||||||
'bsd': 'BSD-2-Clause',
|
|
||||||
'mit': 'MIT',
|
|
||||||
'x11': 'MIT',
|
|
||||||
'mit/x11': 'MIT',
|
|
||||||
'apache 2.0': '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',
|
|
||||||
'lgplv2.1': 'LGPL-2.1',
|
|
||||||
'lgplv2': 'LGPL-2.1'
|
|
||||||
};
|
|
||||||
|
|
||||||
return licenseMap[name.toLowerCase()] ?
|
|
||||||
base + licenseMap[name.toLowerCase()] : base + name;
|
|
||||||
}
|
|
||||||
37
controllers/web/package/list_privates.js
Normal file
37
controllers/web/package/list_privates.js
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
/**!
|
||||||
|
* 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';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var packageService = require('../../../services/package');
|
||||||
|
var config = require('../../../config');
|
||||||
|
|
||||||
|
module.exports = function* listPrivates() {
|
||||||
|
var tasks = {};
|
||||||
|
for (var i = 0; i < config.scopes.length; i++) {
|
||||||
|
var scope = config.scopes[i];
|
||||||
|
tasks[scope] = packageService.listPrivateModulesByScope(scope);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.privatePackages && config.privatePackages.length > 0) {
|
||||||
|
tasks['no scoped'] = packageService.listModules(config.privatePackages);
|
||||||
|
}
|
||||||
|
|
||||||
|
var scopes = yield tasks;
|
||||||
|
yield this.render('private', {
|
||||||
|
title: 'private packages',
|
||||||
|
scopes: scopes
|
||||||
|
});
|
||||||
|
};
|
||||||
53
controllers/web/package/search.js
Normal file
53
controllers/web/package/search.js
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
/**!
|
||||||
|
* 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';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var debug = require('debug')('cnpmjs.org:controllers:web:package:search');
|
||||||
|
var packageService = require('../../../services/package');
|
||||||
|
|
||||||
|
module.exports = function* search() {
|
||||||
|
var params = this.params;
|
||||||
|
var word = params.word || params[0];
|
||||||
|
debug('search %j', word);
|
||||||
|
var result = yield* packageService.search(word);
|
||||||
|
|
||||||
|
var match = null;
|
||||||
|
for (var i = 0; i < result.searchMatchs.length; i++) {
|
||||||
|
var p = result.searchMatchs[i];
|
||||||
|
if (p.name === word) {
|
||||||
|
match = p;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// return a json result
|
||||||
|
if (this.query && this.query.type === 'json') {
|
||||||
|
this.jsonp = {
|
||||||
|
keyword: word,
|
||||||
|
match: match,
|
||||||
|
packages: result.searchMatchs,
|
||||||
|
keywords: result.keywordMatchs,
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
yield this.render('search', {
|
||||||
|
title: 'Keyword - ' + word,
|
||||||
|
keyword: word,
|
||||||
|
match: match,
|
||||||
|
packages: result.searchMatchs,
|
||||||
|
keywords: result.keywordMatchs,
|
||||||
|
});
|
||||||
|
};
|
||||||
49
controllers/web/package/search_range.js
Normal file
49
controllers/web/package/search_range.js
Normal file
@@ -0,0 +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';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var packageService = require('../../../services/package');
|
||||||
|
|
||||||
|
module.exports = function* searchRange() {
|
||||||
|
var 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});
|
||||||
|
|
||||||
|
var packages = result.searchMatchs.concat(result.keywordMatchs);
|
||||||
|
|
||||||
|
var rows = [];
|
||||||
|
for (var i = 0; i < packages.length; i++) {
|
||||||
|
var p = packages[i];
|
||||||
|
var row = {
|
||||||
|
key: p.name,
|
||||||
|
count: 1,
|
||||||
|
value: {
|
||||||
|
name: p.name,
|
||||||
|
description: p.description,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
rows.push(row);
|
||||||
|
}
|
||||||
|
this.body = {
|
||||||
|
rows: rows
|
||||||
|
};
|
||||||
|
};
|
||||||
192
controllers/web/package/show.js
Normal file
192
controllers/web/package/show.js
Normal file
@@ -0,0 +1,192 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - controllers/web/package/show.js
|
||||||
|
*
|
||||||
|
* Copyright(c) cnpmjs.org and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var debug = require('debug')('cnpmjs.org:controllers:web:package:show');
|
||||||
|
var bytes = require('bytes');
|
||||||
|
var giturl = require('giturl');
|
||||||
|
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');
|
||||||
|
|
||||||
|
module.exports = function* show(next) {
|
||||||
|
var 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];
|
||||||
|
debug('display %s with %j', name, params);
|
||||||
|
|
||||||
|
var getPackageMethod;
|
||||||
|
var getPackageArgs;
|
||||||
|
var version = semver.valid(tag || '');
|
||||||
|
if (version) {
|
||||||
|
getPackageMethod = 'getModule';
|
||||||
|
getPackageArgs = [name, version];
|
||||||
|
} else {
|
||||||
|
getPackageMethod = 'getModuleByTag';
|
||||||
|
getPackageArgs = [name, tag || 'latest'];
|
||||||
|
}
|
||||||
|
|
||||||
|
var pkg = yield packageService[getPackageMethod].apply(packageService, getPackageArgs);
|
||||||
|
if (!pkg || !pkg.package) {
|
||||||
|
// check if unpublished
|
||||||
|
var unpublishedInfo = yield* packageService.getUnpublishedModule(name);
|
||||||
|
debug('show unpublished %j', unpublishedInfo);
|
||||||
|
if (unpublishedInfo) {
|
||||||
|
var data = {
|
||||||
|
name: 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];
|
||||||
|
if (maintainer.email) {
|
||||||
|
maintainer.gravatar = gravatar.url(maintainer.email, {s: '50', d: 'retro'}, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
yield this.render('package_unpublished', {
|
||||||
|
package: data
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return yield* next;
|
||||||
|
}
|
||||||
|
|
||||||
|
var r = yield [
|
||||||
|
utils.getDownloadTotal(name),
|
||||||
|
packageService.listDependents(name),
|
||||||
|
packageService.listStarUserNames(name),
|
||||||
|
packageService.listMaintainers(name)
|
||||||
|
];
|
||||||
|
var download = r[0];
|
||||||
|
var dependents = r[1];
|
||||||
|
var users = r[2];
|
||||||
|
var maintainers = r[3];
|
||||||
|
|
||||||
|
pkg.package.fromNow = moment(pkg.publish_time).fromNow();
|
||||||
|
pkg = pkg.package;
|
||||||
|
pkg.users = users;
|
||||||
|
if (pkg.readme && typeof pkg.readme !== 'string') {
|
||||||
|
pkg.readme = 'readme is not string: ' + JSON.stringify(pkg.readme);
|
||||||
|
} else {
|
||||||
|
pkg.readme = renderMarkdown(pkg.readme || '');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pkg.readme) {
|
||||||
|
pkg.readme = pkg.description || '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (maintainers.length > 0) {
|
||||||
|
pkg.maintainers = maintainers;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pkg.maintainers) {
|
||||||
|
for (var i = 0; i < pkg.maintainers.length; i++) {
|
||||||
|
var maintainer = pkg.maintainers[i];
|
||||||
|
if (maintainer.email) {
|
||||||
|
maintainer.gravatar = gravatar.url(maintainer.email, {s: '50', d: 'retro'}, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pkg._npmUser) {
|
||||||
|
pkg.lastPublishedUser = pkg._npmUser;
|
||||||
|
if (pkg.lastPublishedUser.email) {
|
||||||
|
pkg.lastPublishedUser.gravatar = gravatar.url(pkg.lastPublishedUser.email, {s: '50', d: 'retro'}, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pkg.repository === 'undefined') {
|
||||||
|
pkg.repository = null;
|
||||||
|
}
|
||||||
|
if (pkg.repository && pkg.repository.url) {
|
||||||
|
pkg.repository.weburl = giturl.parse(pkg.repository.url) || pkg.repository.url;
|
||||||
|
}
|
||||||
|
if (!pkg.bugs) {
|
||||||
|
pkg.bugs = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
utils.setLicense(pkg);
|
||||||
|
|
||||||
|
for (var k in download) {
|
||||||
|
download[k] = humanize(download[k]);
|
||||||
|
}
|
||||||
|
setDownloadURL(pkg, this, config.registryHost);
|
||||||
|
|
||||||
|
pkg.dependents = dependents;
|
||||||
|
|
||||||
|
if (pkg.dist) {
|
||||||
|
pkg.dist.size = bytes(pkg.dist.size || 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pkg.name !== orginalName) {
|
||||||
|
pkg.name = orginalName;
|
||||||
|
}
|
||||||
|
|
||||||
|
pkg.registryUrl = '//' + config.registryHost + '/' + pkg.name;
|
||||||
|
|
||||||
|
// pkg.engines = {
|
||||||
|
// "python": ">= 0.11.9",
|
||||||
|
// "node": ">= 0.11.9",
|
||||||
|
// "node1": ">= 0.8.9",
|
||||||
|
// "node2": ">= 0.10.9",
|
||||||
|
// "node3": ">= 0.6.9",
|
||||||
|
// };
|
||||||
|
if (pkg.engines) {
|
||||||
|
for (var k in pkg.engines) {
|
||||||
|
var engine = String(pkg.engines[k] || '').trim();
|
||||||
|
var color = 'blue';
|
||||||
|
if (k.indexOf('node') === 0) {
|
||||||
|
color = 'yellowgreen';
|
||||||
|
var 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';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pkg.engines[k] = {
|
||||||
|
version: engine,
|
||||||
|
title: k + ': ' + engine,
|
||||||
|
badgeURL: 'https://img.shields.io/badge/' + encodeURIComponent(k) +
|
||||||
|
'-' + encodeURIComponent(engine) + '-' + color + '.svg?style=flat-square',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
yield this.render('package', {
|
||||||
|
title: 'Package - ' + pkg.name,
|
||||||
|
package: pkg,
|
||||||
|
download: download
|
||||||
|
});
|
||||||
|
};
|
||||||
34
controllers/web/show_sync.js
Normal file
34
controllers/web/show_sync.js
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - controllers/web/show_sync.js
|
||||||
|
*
|
||||||
|
* Copyright(c) cnpmjs.org and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
module.exports = function* showSync() {
|
||||||
|
var name = this.params.name || this.params[0] || this.query.name;
|
||||||
|
if (!name) {
|
||||||
|
return this.redirect('/');
|
||||||
|
}
|
||||||
|
var type = 'package';
|
||||||
|
if (name.indexOf(':') > 0) {
|
||||||
|
var splits = name.split(':');
|
||||||
|
name = splits[1];
|
||||||
|
type = splits[0];
|
||||||
|
}
|
||||||
|
yield this.render('sync', {
|
||||||
|
type: type,
|
||||||
|
name: name,
|
||||||
|
title: 'Sync ' + type + ' - ' + name,
|
||||||
|
});
|
||||||
|
};
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/**!
|
/**!
|
||||||
* cnpmjs.org - controllers/web/package.js
|
* cnpmjs.org - controllers/web/user/show.js
|
||||||
*
|
*
|
||||||
* Copyright(c) cnpmjs.org and other contributors.
|
* Copyright(c) cnpmjs.org and other contributors.
|
||||||
* MIT Licensed
|
* MIT Licensed
|
||||||
@@ -15,30 +15,18 @@
|
|||||||
* Module dependencies.
|
* Module dependencies.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var config = require('../../config');
|
var config = require('../../../config');
|
||||||
var Module = require('../../proxy/module');
|
var packageService = require('../../../services/package');
|
||||||
var User = require('../../proxy/user');
|
var userService = require('../../../services/user');
|
||||||
var UserService = require('../../services/user');
|
var common = require('../../../lib/common');
|
||||||
var common = require('../../lib/common');
|
|
||||||
|
|
||||||
exports.display = function* (next) {
|
module.exports = function* showUser(next) {
|
||||||
var name = this.params.name;
|
var name = this.params.name;
|
||||||
var isAdmin = common.isAdmin(name);
|
var isAdmin = common.isAdmin(name);
|
||||||
var scopes = config.scopes || [];
|
var scopes = config.scopes || [];
|
||||||
if (config.customUserService) {
|
var user;
|
||||||
var customUser = yield* UserService.get(name);
|
var r = yield [packageService.listModulesByUser(name), userService.getAndSave(name)];
|
||||||
if (customUser) {
|
var packages = r[0];
|
||||||
isAdmin = !!customUser.site_admin;
|
|
||||||
scopes = customUser.scopes;
|
|
||||||
var data = {
|
|
||||||
user: customUser
|
|
||||||
};
|
|
||||||
yield* User.saveCustomUser(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var r = yield [Module.listByAuthor(name), User.get(name)];
|
|
||||||
var packages = r[0] || [];
|
|
||||||
var user = r[1];
|
var user = r[1];
|
||||||
if (!user && !packages.length) {
|
if (!user && !packages.length) {
|
||||||
return yield* next;
|
return yield* next;
|
||||||
@@ -67,6 +55,7 @@ exports.display = function* (next) {
|
|||||||
avatar: json.avatar_url,
|
avatar: json.avatar_url,
|
||||||
fullname: json.name || json.login,
|
fullname: json.name || json.login,
|
||||||
homepage: json.html_url,
|
homepage: json.html_url,
|
||||||
|
im: json.im_url
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +63,7 @@ exports.display = function* (next) {
|
|||||||
title: 'User - ' + name,
|
title: 'User - ' + name,
|
||||||
packages: packages,
|
packages: packages,
|
||||||
user: data,
|
user: data,
|
||||||
lastModified: user && user.gmt_modified,
|
lastModified: user.gmt_modified,
|
||||||
isAdmin: isAdmin,
|
isAdmin: isAdmin,
|
||||||
scopes: scopes
|
scopes: scopes
|
||||||
});
|
});
|
||||||
@@ -15,15 +15,17 @@
|
|||||||
* Module dependencies.
|
* Module dependencies.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
var childProcess = require('child_process');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
var cluster = require('cluster');
|
|
||||||
var cfork = require('cfork');
|
var cfork = require('cfork');
|
||||||
var config = require('./config');
|
var config = require('./config');
|
||||||
var workerPath = path.join(__dirname, 'worker.js');
|
var workerPath = path.join(__dirname, 'worker.js');
|
||||||
var childProcess = require('child_process');
|
|
||||||
var syncPath = path.join(__dirname, 'sync');
|
var 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);
|
||||||
|
|
||||||
if (config.enableCluster) {
|
if (config.enableCluster) {
|
||||||
forkWorker();
|
forkWorker();
|
||||||
if (config.syncModel !== 'none') {
|
if (config.syncModel !== 'none') {
|
||||||
|
|||||||
143
docs/Migrating-from-1.x-to-2.x.md
Normal file
143
docs/Migrating-from-1.x-to-2.x.md
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
# Migrating from 1.x to 2.x
|
||||||
|
|
||||||
|
2.x using [Sequelize] ORM to supports MySQL, MariaDB, SQLite or PostgreSQL databases.
|
||||||
|
|
||||||
|
## New download total table structure
|
||||||
|
|
||||||
|
### Create `downloads` table SQL
|
||||||
|
|
||||||
|
You should create `downloads` table first:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
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(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',
|
||||||
|
`d03` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '03 download count',
|
||||||
|
`d04` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '04 download count',
|
||||||
|
`d05` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '05 download count',
|
||||||
|
`d06` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '06 download count',
|
||||||
|
`d07` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '07 download count',
|
||||||
|
`d08` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '08 download count',
|
||||||
|
`d09` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '09 download count',
|
||||||
|
`d10` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '10 download count',
|
||||||
|
`d11` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '11 download count',
|
||||||
|
`d12` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '12 download count',
|
||||||
|
`d13` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '13 download count',
|
||||||
|
`d14` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '14 download count',
|
||||||
|
`d15` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '15 download count',
|
||||||
|
`d16` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '16 download count',
|
||||||
|
`d17` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '17 download count',
|
||||||
|
`d18` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '18 download count',
|
||||||
|
`d19` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '19 download count',
|
||||||
|
`d20` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '20 download count',
|
||||||
|
`d21` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '21 download count',
|
||||||
|
`d22` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '22 download count',
|
||||||
|
`d23` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '23 download count',
|
||||||
|
`d24` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '24 download count',
|
||||||
|
`d25` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '25 download count',
|
||||||
|
`d26` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '26 download count',
|
||||||
|
`d27` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '27 download count',
|
||||||
|
`d28` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '28 download count',
|
||||||
|
`d29` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '29 download count',
|
||||||
|
`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 `name_date` (`name`, `date`),
|
||||||
|
KEY `date` (`date`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module download total info';
|
||||||
|
```
|
||||||
|
|
||||||
|
### Sync `download_total` to `downloads`
|
||||||
|
|
||||||
|
Then use [sync_download_total.js](../tools/sync_download_total.js) scrpt to sync datas from `download_total`:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ node --harmony tools/sync_download_total.js
|
||||||
|
```
|
||||||
|
|
||||||
|
# `config.js` changes in 2.x
|
||||||
|
|
||||||
|
## New database config
|
||||||
|
|
||||||
|
```js
|
||||||
|
/**
|
||||||
|
* database config
|
||||||
|
*/
|
||||||
|
|
||||||
|
database: {
|
||||||
|
db: 'cnpmjs_test',
|
||||||
|
username: 'root',
|
||||||
|
password: '',
|
||||||
|
|
||||||
|
// the sql dialect of the database
|
||||||
|
// - currently supported: 'mysql', 'sqlite', 'postgres', 'mariadb'
|
||||||
|
dialect: 'sqlite',
|
||||||
|
|
||||||
|
// custom host; default: 127.0.0.1
|
||||||
|
host: '127.0.0.1',
|
||||||
|
|
||||||
|
// 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.sqlite
|
||||||
|
storage: path.join(process.env.HOME || root, 'cnpmjs.org.sqlite'),
|
||||||
|
|
||||||
|
logging: !!process.env.SQL_DEBUG,
|
||||||
|
},
|
||||||
|
```
|
||||||
|
|
||||||
|
If you're still using MySQL and old config.js `mysqlServers: []` from 1.x:
|
||||||
|
|
||||||
|
```js
|
||||||
|
mysqlServers: [
|
||||||
|
{
|
||||||
|
host: '127.0.0.1',
|
||||||
|
port: 3306,
|
||||||
|
user: 'root',
|
||||||
|
password: ''
|
||||||
|
}
|
||||||
|
],
|
||||||
|
mysqlDatabase: 'cnpmjs_test',
|
||||||
|
mysqlMaxConnections: 4,
|
||||||
|
mysqlQueryTimeout: 5000,
|
||||||
|
```
|
||||||
|
|
||||||
|
We will do forward compat, and auto change old style config.js to:
|
||||||
|
|
||||||
|
```js
|
||||||
|
database: {
|
||||||
|
db: 'cnpmjs_test',
|
||||||
|
username: 'root',
|
||||||
|
password: '',
|
||||||
|
dialect: 'mysql',
|
||||||
|
host: '127.0.0.1',
|
||||||
|
port: 3306,
|
||||||
|
pool: {
|
||||||
|
maxConnections: 10,
|
||||||
|
minConnections: 0,
|
||||||
|
maxIdleTime: 30000
|
||||||
|
},
|
||||||
|
logging: !!process.env.SQL_DEBUG,
|
||||||
|
},
|
||||||
|
```
|
||||||
|
|
||||||
|
## remove `adaptScope`
|
||||||
|
|
||||||
|
`adaptScope: true | false` feature was removed.
|
||||||
|
|
||||||
|
|
||||||
|
[Sequelize]: http://sequelizejs.com/
|
||||||
109
docs/db.sql
109
docs/db.sql
@@ -1,4 +1,4 @@
|
|||||||
CREATE TABLE `user` (
|
CREATE TABLE IF NOT EXISTS `user` (
|
||||||
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
||||||
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
||||||
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
|
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
|
||||||
@@ -19,7 +19,7 @@ CREATE TABLE `user` (
|
|||||||
-- ADD `json` longtext CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT 'json details',
|
-- 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';
|
-- ADD `npm_user` tinyint(1) DEFAULT '0' COMMENT 'user sync from npm or not, 1: true, other: false';
|
||||||
|
|
||||||
CREATE TABLE `module_keyword` (
|
CREATE TABLE IF NOT EXISTS `module_keyword` (
|
||||||
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
||||||
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
||||||
`keyword` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'keyword',
|
`keyword` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'keyword',
|
||||||
@@ -30,7 +30,7 @@ CREATE TABLE `module_keyword` (
|
|||||||
KEY `name` (`name`)
|
KEY `name` (`name`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module keyword';
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module keyword';
|
||||||
|
|
||||||
CREATE TABLE `module_star` (
|
CREATE TABLE IF NOT EXISTS `module_star` (
|
||||||
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
||||||
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
||||||
`user` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'user name',
|
`user` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'user name',
|
||||||
@@ -40,7 +40,7 @@ CREATE TABLE `module_star` (
|
|||||||
KEY `name` (`name`)
|
KEY `name` (`name`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module star';
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module star';
|
||||||
|
|
||||||
CREATE TABLE `module_maintainer` (
|
CREATE TABLE IF NOT EXISTS `module_maintainer` (
|
||||||
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
||||||
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
||||||
`user` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'user name',
|
`user` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'user name',
|
||||||
@@ -48,9 +48,19 @@ CREATE TABLE `module_maintainer` (
|
|||||||
PRIMARY KEY (`id`),
|
PRIMARY KEY (`id`),
|
||||||
UNIQUE KEY `user_module_name` (`user`,`name`),
|
UNIQUE KEY `user_module_name` (`user`,`name`),
|
||||||
KEY `name` (`name`)
|
KEY `name` (`name`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module maintainers';
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='private module maintainers';
|
||||||
|
|
||||||
CREATE TABLE `module` (
|
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(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
UNIQUE KEY `user_module_name` (`user`,`name`),
|
||||||
|
KEY `name` (`name`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='npm original module maintainers';
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS `module` (
|
||||||
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
||||||
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
||||||
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
|
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
|
||||||
@@ -76,7 +86,7 @@ CREATE TABLE `module` (
|
|||||||
-- show create table module\G
|
-- 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 `name` `name` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name';
|
||||||
|
|
||||||
CREATE TABLE `module_log` (
|
CREATE TABLE IF NOT EXISTS `module_log` (
|
||||||
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
||||||
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
||||||
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
|
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
|
||||||
@@ -88,7 +98,7 @@ CREATE TABLE `module_log` (
|
|||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module sync log';
|
) 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 `name` `name` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name';
|
||||||
|
|
||||||
CREATE TABLE `tag` (
|
CREATE TABLE IF NOT EXISTS `tag` (
|
||||||
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
||||||
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
||||||
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
|
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
|
||||||
@@ -104,7 +114,7 @@ CREATE TABLE `tag` (
|
|||||||
-- ALTER TABLE `tag` CHANGE `name` `name` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name';
|
-- 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` ADD KEY `gmt_modified` (`gmt_modified`);
|
||||||
|
|
||||||
CREATE TABLE `module_unpublished` (
|
CREATE TABLE IF NOT EXISTS `module_unpublished` (
|
||||||
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
||||||
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
||||||
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
|
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
|
||||||
@@ -115,7 +125,7 @@ CREATE TABLE `module_unpublished` (
|
|||||||
KEY `gmt_modified` (`gmt_modified`)
|
KEY `gmt_modified` (`gmt_modified`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module unpublished info';
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module unpublished info';
|
||||||
|
|
||||||
CREATE TABLE `total` (
|
CREATE TABLE IF NOT EXISTS `total` (
|
||||||
`name` varchar(100) NOT NULL COMMENT 'total name',
|
`name` varchar(100) NOT NULL COMMENT 'total name',
|
||||||
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
|
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
|
||||||
`module_delete` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'module delete count',
|
`module_delete` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'module delete count',
|
||||||
@@ -129,7 +139,9 @@ CREATE TABLE `total` (
|
|||||||
`last_sync_module` varchar(100) COMMENT 'last sync success module name',
|
`last_sync_module` varchar(100) COMMENT 'last sync success module name',
|
||||||
PRIMARY KEY (`name`)
|
PRIMARY KEY (`name`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='total info';
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='total info';
|
||||||
INSERT INTO total(name, gmt_modified) VALUES('total', now());
|
-- init `total` count
|
||||||
|
INSERT INTO total(name, gmt_modified) VALUES('total', now())
|
||||||
|
ON DUPLICATE KEY UPDATE gmt_modified=now();
|
||||||
-- ALTER TABLE `total` ADD `last_sync_time` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'last timestamp sync from official registry'
|
-- ALTER TABLE `total` ADD `last_sync_time` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'last timestamp sync from official registry'
|
||||||
-- ALTER TABLE `total` ADD `last_exist_sync_time` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'last timestamp sync exist packages from official registry'
|
-- ALTER TABLE `total` ADD `last_exist_sync_time` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'last timestamp sync exist packages from official registry'
|
||||||
-- ALTER TABLE `total` ADD `sync_status` tinyint unsigned NOT NULL DEFAULT '0' COMMENT 'system sync from official registry status'
|
-- ALTER TABLE `total` ADD `sync_status` tinyint unsigned NOT NULL DEFAULT '0' COMMENT 'system sync from official registry status'
|
||||||
@@ -139,19 +151,62 @@ INSERT INTO total(name, gmt_modified) VALUES('total', now());
|
|||||||
-- 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 `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` ADD `last_sync_module` varchar(100) NOT NULL COMMENT 'last sync success module name';
|
||||||
|
|
||||||
CREATE TABLE `download_total` (
|
-- CREATE TABLE IF NOT EXISTS `download_total` (
|
||||||
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
-- `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
||||||
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
-- `gmt_create` datetime NOT NULL COMMENT 'create time',
|
||||||
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
|
-- `gmt_modified` datetime NOT NULL COMMENT 'modified time',
|
||||||
`date` varchar(10) NOT NULL COMMENT 'YYYY-MM-DD format',
|
-- `date` datetime NOT NULL COMMENT 'YYYY-MM-DD format',
|
||||||
`name` varchar(100) 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',
|
||||||
`count` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'download count',
|
-- `count` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'download count',
|
||||||
PRIMARY KEY (`id`),
|
-- PRIMARY KEY (`id`),
|
||||||
UNIQUE KEY `date_name` (`date`, `name`)
|
-- UNIQUE KEY `date_name` (`date`, `name`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module download total info';
|
-- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module download total info';
|
||||||
-- ALTER TABLE `download_total` CHANGE `name` `name` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name';
|
-- ALTER TABLE `download_total` CHANGE `name` `name` VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name';
|
||||||
|
-- ALTER TABLE `download_total` CHANGE `date` `date` datetime NOT NULL COMMENT 'YYYY-MM-DD format';
|
||||||
|
|
||||||
CREATE TABLE `module_deps` (
|
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(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',
|
||||||
|
`d03` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '03 download count',
|
||||||
|
`d04` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '04 download count',
|
||||||
|
`d05` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '05 download count',
|
||||||
|
`d06` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '06 download count',
|
||||||
|
`d07` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '07 download count',
|
||||||
|
`d08` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '08 download count',
|
||||||
|
`d09` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '09 download count',
|
||||||
|
`d10` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '10 download count',
|
||||||
|
`d11` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '11 download count',
|
||||||
|
`d12` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '12 download count',
|
||||||
|
`d13` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '13 download count',
|
||||||
|
`d14` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '14 download count',
|
||||||
|
`d15` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '15 download count',
|
||||||
|
`d16` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '16 download count',
|
||||||
|
`d17` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '17 download count',
|
||||||
|
`d18` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '18 download count',
|
||||||
|
`d19` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '19 download count',
|
||||||
|
`d20` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '20 download count',
|
||||||
|
`d21` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '21 download count',
|
||||||
|
`d22` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '22 download count',
|
||||||
|
`d23` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '23 download count',
|
||||||
|
`d24` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '24 download count',
|
||||||
|
`d25` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '25 download count',
|
||||||
|
`d26` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '26 download count',
|
||||||
|
`d27` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '27 download count',
|
||||||
|
`d28` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '28 download count',
|
||||||
|
`d29` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '29 download count',
|
||||||
|
`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 `name_date` (`name`, `date`),
|
||||||
|
KEY `date` (`date`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module download total info';
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS `module_deps` (
|
||||||
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
||||||
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
||||||
`name` varchar(100) 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',
|
||||||
@@ -161,11 +216,11 @@ CREATE TABLE `module_deps` (
|
|||||||
KEY `name` (`name`)
|
KEY `name` (`name`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module deps';
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module deps';
|
||||||
|
|
||||||
CREATE TABLE `dist_dir` (
|
CREATE TABLE IF NOT EXISTS `dist_dir` (
|
||||||
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
||||||
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
||||||
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
|
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
|
||||||
`name` varchar(200) NOT NULL COMMENT 'user name',
|
`name` varchar(200) NOT NULL COMMENT 'dir name',
|
||||||
`parent` varchar(200) NOT NULL COMMENT 'parent dir' DEFAULT '/',
|
`parent` varchar(200) NOT NULL COMMENT 'parent dir' DEFAULT '/',
|
||||||
`date` varchar(20) COMMENT '02-May-2014 01:06',
|
`date` varchar(20) COMMENT '02-May-2014 01:06',
|
||||||
PRIMARY KEY (`id`),
|
PRIMARY KEY (`id`),
|
||||||
@@ -173,11 +228,11 @@ CREATE TABLE `dist_dir` (
|
|||||||
KEY `gmt_modified` (`gmt_modified`)
|
KEY `gmt_modified` (`gmt_modified`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='dist dir info';
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='dist dir info';
|
||||||
|
|
||||||
CREATE TABLE `dist_file` (
|
CREATE TABLE IF NOT EXISTS `dist_file` (
|
||||||
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
||||||
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
||||||
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
|
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
|
||||||
`name` varchar(100) NOT NULL COMMENT 'user name',
|
`name` varchar(100) NOT NULL COMMENT 'file name',
|
||||||
`parent` varchar(200) NOT NULL COMMENT 'parent dir' DEFAULT '/',
|
`parent` varchar(200) NOT NULL COMMENT 'parent dir' DEFAULT '/',
|
||||||
`date` varchar(20) COMMENT '02-May-2014 01:06',
|
`date` varchar(20) COMMENT '02-May-2014 01:06',
|
||||||
`size` int(10) unsigned NOT NULL COMMENT 'file size' DEFAULT '0',
|
`size` int(10) unsigned NOT NULL COMMENT 'file size' DEFAULT '0',
|
||||||
|
|||||||
@@ -19,22 +19,20 @@ All data is sent and received as JSON.
|
|||||||
$ curl -i https://registry.npmjs.org
|
$ curl -i https://registry.npmjs.org
|
||||||
|
|
||||||
HTTP/1.1 200 OK
|
HTTP/1.1 200 OK
|
||||||
Date: Tue, 05 Aug 2014 10:53:24 GMT
|
|
||||||
Server: CouchDB/1.5.0 (Erlang OTP/R16B03)
|
|
||||||
Content-Type: text/plain; charset=utf-8
|
|
||||||
Cache-Control: max-age=60
|
|
||||||
Content-Length: 258
|
|
||||||
Accept-Ranges: bytes
|
|
||||||
Via: 1.1 varnish
|
|
||||||
Age: 11
|
|
||||||
X-Served-By: cache-ty67-TYO
|
|
||||||
X-Cache: HIT
|
|
||||||
X-Cache-Hits: 1
|
|
||||||
X-Timer: S1407236004.867906,VS0,VE0
|
|
||||||
|
|
||||||
{"db_name":"registry","doc_count":90789,"doc_del_count":381,"update_seq":137250,"purge_seq":0,
|
{
|
||||||
"compact_running":false,"disk_size":436228219,"data_size":332875061,
|
"db_name": "registry",
|
||||||
"instance_start_time":"1405721973718703","disk_format_version":6,"committed_update_seq":137250}
|
"doc_count": 123772,
|
||||||
|
"doc_del_count": 377,
|
||||||
|
"update_seq": 685591,
|
||||||
|
"purge_seq": 0,
|
||||||
|
"compact_running": false,
|
||||||
|
"disk_size": 634187899,
|
||||||
|
"data_size": 445454185,
|
||||||
|
"instance_start_time": "1420670152481614",
|
||||||
|
"disk_format_version": 6,
|
||||||
|
"committed_update_seq": 685591
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Client Errors
|
## Client Errors
|
||||||
@@ -61,22 +59,10 @@ $ curl -u "username:password" https://registry.npmjs.org
|
|||||||
## Failed login limit
|
## Failed login limit
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ curl -i -X PUT -u foo:pwd \
|
$ curl -i -X "GET" -u "foo:pwd" \
|
||||||
-d '{"name":"foo","email":"foo@bar.com","type":"user","roles":[]}' \
|
"https://registry.npmjs.com/-/user/org.couchdb.user:npm-user-service-testuser?write=true"
|
||||||
https://registry.npmjs.org/-/user/org.couchdb.user:foo/-rev/11-d226c6afa9286ab5b9eb858c429bdabf
|
|
||||||
|
|
||||||
HTTP/1.1 401 Unauthorized
|
HTTP/1.1 401 Unauthorized
|
||||||
Date: Tue, 05 Aug 2014 15:33:25 GMT
|
|
||||||
Server: CouchDB/1.5.0 (Erlang OTP/R14B04)
|
|
||||||
Content-Type: text/plain; charset=utf-8
|
|
||||||
Cache-Control: max-age=60
|
|
||||||
Content-Length: 67
|
|
||||||
Accept-Ranges: bytes
|
|
||||||
Via: 1.1 varnish
|
|
||||||
X-Served-By: cache-ty66-TYO
|
|
||||||
X-Cache: MISS
|
|
||||||
X-Cache-Hits: 0
|
|
||||||
X-Timer: S1407252805.261390,VS0,VE434
|
|
||||||
|
|
||||||
{"error":"unauthorized","reason":"Name or password is incorrect."}
|
{"error":"unauthorized","reason":"Name or password is incorrect."}
|
||||||
```
|
```
|
||||||
@@ -87,6 +73,7 @@ X-Timer: S1407252805.261390,VS0,VE434
|
|||||||
* [Get a single package](/docs/registry-api.md#get-a-single-package)
|
* [Get a single package](/docs/registry-api.md#get-a-single-package)
|
||||||
* [Get a special version or tag package](/docs/registry-api.md#get-a-special-version-or-tag-package)
|
* [Get a special version or tag package](/docs/registry-api.md#get-a-special-version-or-tag-package)
|
||||||
* [List packages since from a update time](/docs/registry-api.md#list-packages-since-from-a-update-time)
|
* [List packages since from a update time](/docs/registry-api.md#list-packages-since-from-a-update-time)
|
||||||
|
* [List package names by users](/docs/registry-api.md#list-package-names-by-users)
|
||||||
* Write
|
* Write
|
||||||
* [Publish a new package](/docs/registry-api.md#publish-a-new-package)
|
* [Publish a new package](/docs/registry-api.md#publish-a-new-package)
|
||||||
* [Update a package's tag](/docs/registry-api.md#update-a-packages-tag)
|
* [Update a package's tag](/docs/registry-api.md#update-a-packages-tag)
|
||||||
@@ -100,14 +87,11 @@ X-Timer: S1407252805.261390,VS0,VE434
|
|||||||
GET /:package
|
GET /:package
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Response
|
#### Response 200
|
||||||
|
|
||||||
```json
|
```json
|
||||||
HTTP/1.1 200 OK
|
HTTP/1.1 200 OK
|
||||||
Etag: "8UDCP753LFXOG42NMX88JAN40"
|
Etag: "8UDCP753LFXOG42NMX88JAN40"
|
||||||
Content-Type: application/json
|
|
||||||
Cache-Control: max-age=60
|
|
||||||
Content-Length: 2243
|
|
||||||
|
|
||||||
{
|
{
|
||||||
"_id": "pedding",
|
"_id": "pedding",
|
||||||
@@ -245,6 +229,17 @@ Content-Length: 2243
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Response 404
|
||||||
|
|
||||||
|
```json
|
||||||
|
HTTP/1.1 404 Object Not Found
|
||||||
|
|
||||||
|
{
|
||||||
|
"error": "not_found",
|
||||||
|
"reason": "document not found"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### ~~Get a special version or tag package~~
|
### ~~Get a special version or tag package~~
|
||||||
|
|
||||||
__deprecated__
|
__deprecated__
|
||||||
@@ -253,7 +248,7 @@ __deprecated__
|
|||||||
GET /:package/:tag_or_version
|
GET /:package/:tag_or_version
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Reponse
|
#### Reponse 200
|
||||||
|
|
||||||
```json
|
```json
|
||||||
HTTP/1.1 200 OK
|
HTTP/1.1 200 OK
|
||||||
@@ -705,11 +700,91 @@ HTTP/1.1 200 OK
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### List package names by users
|
||||||
|
|
||||||
|
```bash
|
||||||
|
GET /-/by-user/$username[|$another1[|$another2...]]
|
||||||
|
```
|
||||||
|
|
||||||
|
* `username` user name like `fengmk2`
|
||||||
|
* also support multi users by `name1|name2|name3`
|
||||||
|
|
||||||
|
#### Response
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ curl -i "https://registry.npmjs.org/-/by-user/czy88840616"
|
||||||
|
$ curl -i "https://registry.npmjs.org/-/by-user/czy88840616|fengmk2|dead-horse"
|
||||||
|
```
|
||||||
|
|
||||||
|
```json
|
||||||
|
HTTP/1.1 200 OK
|
||||||
|
|
||||||
|
{
|
||||||
|
"czy88840616": [
|
||||||
|
"easyconf",
|
||||||
|
"egg",
|
||||||
|
"flag",
|
||||||
|
"gdp",
|
||||||
|
"generator-webx-vm",
|
||||||
|
"magic-cube",
|
||||||
|
"rim",
|
||||||
|
"tbuild",
|
||||||
|
"test-publish",
|
||||||
|
"velocity-parser",
|
||||||
|
"vmarket",
|
||||||
|
"wi"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## User
|
## User
|
||||||
|
|
||||||
* [Get a single user](/docs/registry-api.md#get-a-single-user)
|
- [Auth user](/docs/registry-api.md#auth-user)
|
||||||
* [Add a new user](/docs/registry-api.md#add-a-new-user)
|
- [Get a single user](/docs/registry-api.md#get-a-single-user)
|
||||||
* [Update a exists user](/docs/registry-api.md#update-a-exists-user)
|
- [Add a new user](/docs/registry-api.md#add-a-new-user)
|
||||||
|
- [Update a exists user](/docs/registry-api.md#update-a-exists-user)
|
||||||
|
|
||||||
|
### Auth user
|
||||||
|
|
||||||
|
* Authentication required.
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /-/user/org.couchdb.user::username?write=true
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Response 200
|
||||||
|
|
||||||
|
```json
|
||||||
|
HTTP/1.1 200 OK
|
||||||
|
ETag: "5-a31b61ba3c50b50f7fcaf185e079e17a"
|
||||||
|
|
||||||
|
{
|
||||||
|
"_id": "org.couchdb.user:npm-user-service-testuser",
|
||||||
|
"_rev": "5-a31b61ba3c50b50f7fcaf185e079e17a",
|
||||||
|
"password_scheme": "pbkdf2",
|
||||||
|
"iterations": 10,
|
||||||
|
"name": "npm-user-service-testuser",
|
||||||
|
"email": "fengmk2@gmail.com",
|
||||||
|
"type": "user",
|
||||||
|
"roles": [],
|
||||||
|
"date": "2015-01-04T08:28:51.378Z",
|
||||||
|
"password_scheme": "pbkdf2",
|
||||||
|
"iterations": 10,
|
||||||
|
"derived_key": "644157c126b93356e6eba2c59fdf1b7ec644ebf2",
|
||||||
|
"salt": "5d13874c0aa10751e35743bacd6eedd5"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Response 401
|
||||||
|
|
||||||
|
```json
|
||||||
|
HTTP/1.1 401 Unauthorized
|
||||||
|
|
||||||
|
{
|
||||||
|
"error": "unauthorized",
|
||||||
|
"reason": "Name or password is incorrect."
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Get a single user
|
### Get a single user
|
||||||
|
|
||||||
@@ -717,7 +792,7 @@ HTTP/1.1 200 OK
|
|||||||
GET /-/user/org.couchdb.user::username
|
GET /-/user/org.couchdb.user::username
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Response
|
#### Response 200
|
||||||
|
|
||||||
```json
|
```json
|
||||||
HTTP/1.1 200 OK
|
HTTP/1.1 200 OK
|
||||||
@@ -787,6 +862,17 @@ ETag: "32-984ee97e01aea166dcab6d1517c730e3"
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Response 404
|
||||||
|
|
||||||
|
```json
|
||||||
|
HTTP/1.1 404 Object Not Found
|
||||||
|
|
||||||
|
{
|
||||||
|
"error": "not_found",
|
||||||
|
"reason": "missing"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Add a new user
|
### Add a new user
|
||||||
|
|
||||||
```
|
```
|
||||||
@@ -807,7 +893,7 @@ PUT /-/user/org.couchdb.user::username
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Response
|
#### Response 201
|
||||||
|
|
||||||
```json
|
```json
|
||||||
Status: 201 Created
|
Status: 201 Created
|
||||||
@@ -819,6 +905,19 @@ Status: 201 Created
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Response 409
|
||||||
|
|
||||||
|
User already exists
|
||||||
|
|
||||||
|
```json
|
||||||
|
HTTP/1.1 409 Conflict
|
||||||
|
|
||||||
|
{
|
||||||
|
"error": "conflict",
|
||||||
|
"reason": "Document update conflict."
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Update a exists user
|
### Update a exists user
|
||||||
|
|
||||||
* Authentication required.
|
* Authentication required.
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
CREATE TABLE `module_maintainer` (
|
CREATE TABLE IF NOT EXISTS `npm_module_maintainer` (
|
||||||
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
||||||
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
||||||
`user` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'user name',
|
`user` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'user name',
|
||||||
@@ -6,4 +6,4 @@ CREATE TABLE `module_maintainer` (
|
|||||||
PRIMARY KEY (`id`),
|
PRIMARY KEY (`id`),
|
||||||
UNIQUE KEY `user_module_name` (`user`,`name`),
|
UNIQUE KEY `user_module_name` (`user`,`name`),
|
||||||
KEY `name` (`name`)
|
KEY `name` (`name`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module maintainers';
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='npm original module maintainers';
|
||||||
@@ -4,10 +4,10 @@ So `cnpm` is meaning: **Company npm**.
|
|||||||
|
|
||||||
## Registry
|
## Registry
|
||||||
|
|
||||||
* Our public registry: [r.cnpmjs.org](http://r.cnpmjs.org), syncing from [registry.npmjs.org](http://registry.npmjs.org)
|
- Our public registry: [r.cnpmjs.org](//r.cnpmjs.org), syncing from [registry.npmjs.org](//registry.npmjs.org)
|
||||||
* Current [cnpmjs.org](/) version: <span id="app-version"></span>
|
- [cnpmjs.org](/) version: <span id="app-version"></span>
|
||||||
* Mirror of [nodejs.org/dist](http://nodejs.org/dist): [/dist mirror](/dist)
|
- [Node.js](https://nodejs.org) version: <span id="node-version"></span>
|
||||||
* Mirror of [phantomjs downloads](https://bitbucket.org/ariya/phantomjs/downloads): [phantomjs mirror](/dist/phantomjs/)
|
- For developers behind the GFW, please visit [the Chinese mirror](https://npm.taobao.org). 中国用户请访问[国内镜像站点](https://npm.taobao.org/)。
|
||||||
|
|
||||||
<table class="downloads">
|
<table class="downloads">
|
||||||
<tbody>
|
<tbody>
|
||||||
@@ -50,98 +50,52 @@ So `cnpm` is meaning: **Company npm**.
|
|||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
Running on [Node.js](http://nodejs.org), version <span id="node-version"></span>.
|
<script src="/js/readme.js"></script>
|
||||||
|
|
||||||
<script>
|
## Badges
|
||||||
$(function () {
|
|
||||||
function humanize(n, options) {
|
|
||||||
options = options || {};
|
|
||||||
var d = options.delimiter || ',';
|
|
||||||
var s = options.separator || '.';
|
|
||||||
n = n.toString().split('.');
|
|
||||||
n[0] = n[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1' + d);
|
|
||||||
return n.join(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
$.getJSON('/total', function (data) {
|
|
||||||
$('#total-packages').html(humanize(data.doc_count));
|
|
||||||
$('#total-versions').html(humanize(data.doc_version_count));
|
|
||||||
$('#total-deletes').html(humanize(data.doc_del_count));
|
|
||||||
|
|
||||||
var downloads = $('table.downloads');
|
|
||||||
downloads.find('td.count:eq(3)').html(humanize(data.download.today));
|
|
||||||
downloads.find('td.count:eq(4)').html(humanize(data.download.thisweek));
|
|
||||||
downloads.find('td.count:eq(5)').html(humanize(data.download.thismonth));
|
|
||||||
downloads.find('td.count:eq(6)').html(humanize(data.download.lastday));
|
|
||||||
downloads.find('td.count:eq(7)').html(humanize(data.download.lastweek));
|
|
||||||
downloads.find('td.count:eq(8)').html(humanize(data.download.lastmonth));
|
|
||||||
|
|
||||||
$('#node-version').html(data.node_version || 'v0.10.22');
|
|
||||||
$('#app-version').html(data.app_version || '0.0.0');
|
|
||||||
|
|
||||||
if (data.sync_model === 'all') {
|
|
||||||
$('#sync-model').html('This registry will sync all packages from official registry.');
|
|
||||||
$('#last-sync-time').html(new Date(data.last_sync_time));
|
|
||||||
} else if (data.sync_model === 'exist') {
|
|
||||||
$('#sync-model').html('This registry will only update exist packages from official registry.');
|
|
||||||
$('#last-sync-time').html(new Date(data.last_exist_sync_time));
|
|
||||||
}
|
|
||||||
|
|
||||||
$('#need-sync').html(data.need_sync_num);
|
|
||||||
$('#success-sync').html(data.success_sync_num);
|
|
||||||
$('#fail-sync').html(data.fail_sync_num);
|
|
||||||
$('#left-sync').html(data.left_sync_num);
|
|
||||||
$('#percent-sync').html(Math.floor(data.success_sync_num / data.need_sync_num * 100));
|
|
||||||
$('#last-success-name').html('<a target="_blank" href="/package/' + data.last_sync_module + '">' +
|
|
||||||
data.last_sync_module + '</a>');
|
|
||||||
|
|
||||||
if (!data.sync_status) {
|
|
||||||
$('.syncing').html('');
|
|
||||||
}
|
|
||||||
|
|
||||||
$('.sync').show();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
## Version Badge
|
|
||||||
|
|
||||||
Default style is `flat-square`.
|
Default style is `flat-square`.
|
||||||
|
|
||||||
Badge URL: `http://cnpmjs.org/badge/v/cnpmjs.org.svg` 
|
### Version
|
||||||
|
|
||||||
|
Badge URL: `http://cnpmjs.org/badge/v/cnpmjs.org.svg` 
|
||||||
|
|
||||||
* `<0.1.0 & >=0.0.0`: 
|
* `<0.1.0 & >=0.0.0`: 
|
||||||
* `<1.0.0 & >=0.1.0`: 
|
* `<1.0.0 & >=0.1.0`: 
|
||||||
* `>=1.0.0`: 
|
* `>=1.0.0`: 
|
||||||
|
|
||||||
|
### Downloads
|
||||||
|
|
||||||
|
Badge URL: `http://cnpmjs.org/badge/d/cnpmjs.org.svg` 
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
use our npm client [cnpm](https://github.com/cnpm/cnpm)(More suitable with cnpmjs.org and gzip support), you can get our client through npm:
|
use our npm client [cnpm](https://github.com/cnpm/cnpm)(More suitable with cnpmjs.org and gzip support), you can get our client through npm:
|
||||||
|
|
||||||
```
|
```bash
|
||||||
npm install -g cnpm --registry=http://r.cnpmjs.org
|
$ npm install -g cnpm --registry=http://registry.npm.taobao.org
|
||||||
```
|
```
|
||||||
|
|
||||||
Or you can alias NPM to use it:
|
Or you can alias NPM to use it:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
alias cnpm="npm --registry=http://r.cnpmjs.org \
|
alias cnpm="npm --registry=http://registry.npm.taobao.org \
|
||||||
--cache=$HOME/.npm/.cache/cnpm \
|
--cache=$HOME/.npm/.cache/cnpm \
|
||||||
--disturl=http://cnpmjs.org/dist \
|
--disturl=http://registry.npm.taobao.org/mirrors/node \
|
||||||
--userconfig=$HOME/.cnpmrc"
|
--userconfig=$HOME/.cnpmrc"
|
||||||
|
|
||||||
#Or alias it in .bashrc or .zshrc
|
#Or alias it in .bashrc or .zshrc
|
||||||
$ echo '\n#alias for cnpm\nalias cnpm="npm --registry=http://r.cnpmjs.org \
|
$ echo '\n#alias for cnpm\nalias cnpm="npm --registry=http://registry.npm.taobao.org \
|
||||||
--cache=$HOME/.npm/.cache/cnpm \
|
--cache=$HOME/.npm/.cache/cnpm \
|
||||||
--disturl=http://cnpmjs.org/dist \
|
--disturl=http://registry.npm.taobao.org/mirrors/node \
|
||||||
--userconfig=$HOME/.cnpmrc"' >> ~/.zshrc && source ~/.zshrc
|
--userconfig=$HOME/.cnpmrc"' >> ~/.zshrc && source ~/.zshrc
|
||||||
```
|
```
|
||||||
|
|
||||||
### install
|
### install
|
||||||
|
|
||||||
Install package from [r.cnpmjs.org](http://r.cnpmjs.org). When installing a package or version does not exist, it will try to install from the official registry([registry.npmjs.org](http://registry.npmjs.org)), and sync this package to cnpm in the backend.
|
Install package from [r.cnpmjs.org](//r.cnpmjs.org). When installing a package or version does not exist, it will try to install from the official registry([registry.npmjs.org](//registry.npmjs.org)), and sync this package to cnpm in the backend.
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ cnpm install [name]
|
$ cnpm install [name]
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -153,10 +107,10 @@ Only `cnpm` cli has this command. Meaning sync package from source npm.
|
|||||||
$ cnpm sync connect
|
$ cnpm sync connect
|
||||||
```
|
```
|
||||||
|
|
||||||
sync package on web: [cnpmjs.org/sync/connect](http://cnpmjs.org/sync/connect)
|
sync package on web: [sync/connect](/sync/connect)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ open http://cnpmjs.org/sync/connect
|
$ open http://registry.npm.taobao.org/sync/connect
|
||||||
```
|
```
|
||||||
|
|
||||||
### publish / unpublish
|
### publish / unpublish
|
||||||
@@ -186,4 +140,8 @@ Release [History](/history).
|
|||||||
|
|
||||||
## npm and cnpm relation
|
## npm and cnpm relation
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
## Sponsors
|
||||||
|
|
||||||
|
- [](http://www.ucloud.cn/sdk?sem=sdk-CNPMJS)
|
||||||
|
|||||||
@@ -27,6 +27,11 @@ exports.getTarballFilepath = function (filename) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
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] !== '@') {
|
||||||
|
filename = name.split('/')[0] + '/' + filename;
|
||||||
|
}
|
||||||
return '/' + name + '/-/' + filename;
|
return '/' + name + '/-/' + filename;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -56,3 +61,20 @@ exports.isMaintainer = function (user, maintainers) {
|
|||||||
|
|
||||||
return match.length > 0;
|
return match.length > 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.isLocalModule = function (mods) {
|
||||||
|
for (var i = 0; i < mods.length; i++) {
|
||||||
|
var r = mods[i];
|
||||||
|
if (r.package && r.package._publish_on_cnpm) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.isPrivateScopedPackage = function (name) {
|
||||||
|
if (name[0] !== '@') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return config.scopes.indexOf(name.split('/')[0]) >= 0;
|
||||||
|
};
|
||||||
|
|||||||
@@ -16,13 +16,14 @@
|
|||||||
|
|
||||||
var debug = require('debug')('cnpmjs.org:middleware:auth');
|
var debug = require('debug')('cnpmjs.org:middleware:auth');
|
||||||
var UserService = require('../services/user');
|
var UserService = require('../services/user');
|
||||||
|
var config = require('../config');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse the request authorization
|
* Parse the request authorization
|
||||||
* get the real user
|
* get the real user
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = function (options) {
|
module.exports = function () {
|
||||||
return function* auth(next) {
|
return function* auth(next) {
|
||||||
this.user = {};
|
this.user = {};
|
||||||
|
|
||||||
@@ -30,12 +31,12 @@ module.exports = function (options) {
|
|||||||
authorization = authorization.trim();
|
authorization = authorization.trim();
|
||||||
debug('%s %s with %j', this.method, this.url, authorization);
|
debug('%s %s with %j', this.method, this.url, authorization);
|
||||||
if (!authorization) {
|
if (!authorization) {
|
||||||
return yield* next;
|
return yield* unauthorized.call(this, next);
|
||||||
}
|
}
|
||||||
|
|
||||||
authorization = new Buffer(authorization, 'base64').toString().split(':');
|
authorization = new Buffer(authorization, 'base64').toString().split(':');
|
||||||
if (authorization.length !== 2) {
|
if (authorization.length !== 2) {
|
||||||
return yield* next;
|
return yield* unauthorized.call(this, next);
|
||||||
}
|
}
|
||||||
|
|
||||||
var username = authorization[0];
|
var username = authorization[0];
|
||||||
@@ -52,7 +53,7 @@ module.exports = function (options) {
|
|||||||
|
|
||||||
if (!row) {
|
if (!row) {
|
||||||
debug('auth fail user: %j, headers: %j', row, this.header);
|
debug('auth fail user: %j, headers: %j', row, this.header);
|
||||||
return yield* next;
|
return yield* unauthorized.call(this, next);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.user.name = row.login;
|
this.user.name = row.login;
|
||||||
@@ -62,3 +63,19 @@ module.exports = function (options) {
|
|||||||
yield* next;
|
yield* next;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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') {
|
||||||
|
this.body = {
|
||||||
|
error: 'unauthorized',
|
||||||
|
reason: 'login first'
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
this.body = 'login first';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
28
middleware/block.js
Normal file
28
middleware/block.js
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - middleware/block.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
module.exports = function () {
|
||||||
|
return function* block(next) {
|
||||||
|
var ua = String(this.get('user-agent')).toLowerCase();
|
||||||
|
if (ua === 'ruby') {
|
||||||
|
this.status = 403;
|
||||||
|
return this.body = {
|
||||||
|
message: 'forbidden Ruby user-agent, ip: ' + this.ip
|
||||||
|
};
|
||||||
|
}
|
||||||
|
yield* next;
|
||||||
|
};
|
||||||
|
};
|
||||||
42
middleware/editable.js
Normal file
42
middleware/editable.js
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - middleware/editable.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var packageService = require('../services/package');
|
||||||
|
|
||||||
|
// 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];
|
||||||
|
if (username && moduleName) {
|
||||||
|
if (this.user.isAdmin) {
|
||||||
|
return yield* next;
|
||||||
|
}
|
||||||
|
var isMaintainer = yield* packageService.isMaintainer(moduleName, username);
|
||||||
|
if (isMaintainer) {
|
||||||
|
return yield* next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.status = 403;
|
||||||
|
var message = 'not authorized to modify ' + moduleName;
|
||||||
|
if (username) {
|
||||||
|
message = username + ' ' + message;
|
||||||
|
}
|
||||||
|
this.body = {
|
||||||
|
error: 'forbidden user',
|
||||||
|
reason: message
|
||||||
|
};
|
||||||
|
};
|
||||||
30
middleware/exists_package.js
Normal file
30
middleware/exists_package.js
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - middleware/exists_package.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var packageService = require('../services/package');
|
||||||
|
|
||||||
|
module.exports = function* (next) {
|
||||||
|
var name = this.params.name || this.params[0];
|
||||||
|
var pkg = yield packageService.getLatestModule(name);
|
||||||
|
if (pkg) {
|
||||||
|
return yield* next;
|
||||||
|
}
|
||||||
|
this.status = 404;
|
||||||
|
this.body = {
|
||||||
|
error: 'not_found',
|
||||||
|
reason: 'document not found'
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
var config = require('../config');
|
var config = require('../config');
|
||||||
var limit = require('koa-limit');
|
var limit = require('koa-limit');
|
||||||
var store = require('../common/redis');
|
|
||||||
|
|
||||||
var limitConfig = config.limit;
|
var limitConfig = config.limit;
|
||||||
|
|
||||||
@@ -25,10 +24,5 @@ if (!limitConfig.enable) {
|
|||||||
yield *next;
|
yield *next;
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if (!config.debug) {
|
|
||||||
limitConfig.store = store;
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = limit(limitConfig);
|
module.exports = limit(limitConfig);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,9 +14,15 @@
|
|||||||
* Module dependencies.
|
* Module dependencies.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
var http = require('http');
|
||||||
|
|
||||||
module.exports = function *login(next) {
|
module.exports = function *login(next) {
|
||||||
if (this.user.error) {
|
if (this.user.error) {
|
||||||
this.status = this.user.error.status || 500;
|
var status = this.user.error.status;
|
||||||
|
this.status = http.STATUS_CODES[status]
|
||||||
|
? status
|
||||||
|
: 500;
|
||||||
|
|
||||||
this.body = {
|
this.body = {
|
||||||
error: this.user.error.name,
|
error: this.user.error.name,
|
||||||
reason: this.user.error.message
|
reason: this.user.error.message
|
||||||
@@ -25,11 +31,10 @@ module.exports = function *login(next) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!this.user.name) {
|
if (!this.user.name) {
|
||||||
|
|
||||||
this.status = 401;
|
this.status = 401;
|
||||||
this.body = {
|
this.body = {
|
||||||
error: 'unauthorized',
|
error: 'unauthorized',
|
||||||
reason: 'Login first.'
|
reason: 'Login first'
|
||||||
};
|
};
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,9 +22,7 @@ var template = '<?xml version="1.0" encoding="UTF-8"?>\
|
|||||||
<Url method="get" type="text/html" template="http://${host}/browse/keyword/{searchTerms}"/>\
|
<Url method="get" type="text/html" template="http://${host}/browse/keyword/{searchTerms}"/>\
|
||||||
</OpenSearchDescription>';
|
</OpenSearchDescription>';
|
||||||
|
|
||||||
var lastModifyDate = new Date();
|
module.exports = function *opensearch(next) {
|
||||||
|
|
||||||
module.exports = function *publishable(next) {
|
|
||||||
if (this.path === '/opensearch.xml') {
|
if (this.path === '/opensearch.xml') {
|
||||||
this.type = 'text/xml';
|
this.type = 'text/xml';
|
||||||
this.charset = 'utf-8';
|
this.charset = 'utf-8';
|
||||||
|
|||||||
59
middleware/proxy_to_npm.js
Normal file
59
middleware/proxy_to_npm.js
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - middleware/proxy_to_npm.js
|
||||||
|
*
|
||||||
|
* Copyright(c) Alibaba Group Holding Limited.
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* 苏千 <suqian.yf@alipay.com> (http://fengmk2.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var debug = require('debug')('cnpmjs.org:middleware:proxy_to_npm');
|
||||||
|
var config = require('../config');
|
||||||
|
|
||||||
|
module.exports = function (options) {
|
||||||
|
var redirectUrl = config.sourceNpmRegistry;
|
||||||
|
var proxyUrls = [
|
||||||
|
// /:pkg, dont contains scoped package
|
||||||
|
/^\/[\w\-\.]+$/,
|
||||||
|
// /-/package/:pkg/dist-tags
|
||||||
|
/^\/\-\/package\/[\w\-\.]+\/dist-tags/,
|
||||||
|
];
|
||||||
|
if (options && options.isWeb) {
|
||||||
|
redirectUrl = redirectUrl.replace('//registry.', '//');
|
||||||
|
proxyUrls = [
|
||||||
|
// /package/:pkg
|
||||||
|
/^\/package\/[\w\-\.]+$/,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
return function* proxyToNpm(next) {
|
||||||
|
if (config.syncModel !== 'none') {
|
||||||
|
return yield* next;
|
||||||
|
}
|
||||||
|
// only proxy read requests
|
||||||
|
if (this.method !== 'GET' && this.method !== 'HEAD') {
|
||||||
|
return yield* next;
|
||||||
|
}
|
||||||
|
|
||||||
|
var pathname = this.path;
|
||||||
|
var match;
|
||||||
|
for (var i = 0; i < proxyUrls.length; i++) {
|
||||||
|
match = proxyUrls[i].test(pathname);
|
||||||
|
if (match) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!match) {
|
||||||
|
return yield* next;
|
||||||
|
}
|
||||||
|
|
||||||
|
var url = redirectUrl + this.url;
|
||||||
|
debug('proxy to %s', url);
|
||||||
|
this.redirect(url);
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -21,7 +21,6 @@ var debug = require('debug')('cnpmjs.org:middlewares/publishable');
|
|||||||
module.exports = function *publishable(next) {
|
module.exports = function *publishable(next) {
|
||||||
// private mode, only admin user can publish
|
// private mode, only admin user can publish
|
||||||
if (config.enablePrivate && !this.user.isAdmin) {
|
if (config.enablePrivate && !this.user.isAdmin) {
|
||||||
|
|
||||||
this.status = 403;
|
this.status = 403;
|
||||||
this.body = {
|
this.body = {
|
||||||
error: 'no_perms',
|
error: 'no_perms',
|
||||||
@@ -30,9 +29,8 @@ module.exports = function *publishable(next) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// public mode, all user have permission to publish
|
// public mode, all user have permission to publish `scoped package`
|
||||||
// but if `config.scopes` exist, only can publish with scopes in `config.scope`
|
// and only can publish with scopes in `ctx.user.scopes`, default is `config.scopes`
|
||||||
// if `config.forcePublishWithScope` set to true, only admins can publish without scope
|
|
||||||
|
|
||||||
var name = this.params.name || this.params[0];
|
var name = this.params.name || this.params[0];
|
||||||
|
|
||||||
@@ -50,7 +48,7 @@ module.exports = function *publishable(next) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// none-scope
|
// none-scope
|
||||||
if (checkNoneScope(this)) {
|
if (checkNoneScope(name, this)) {
|
||||||
return yield* next;
|
return yield* next;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -71,7 +69,7 @@ function checkScope(name, ctx) {
|
|||||||
ctx.status = 400;
|
ctx.status = 400;
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
error: 'invalid scope',
|
error: 'invalid scope',
|
||||||
reason: util.format('scope %s not match legal scopes %j', scope, ctx.user.scopes)
|
reason: util.format('scope %s not match legal scopes: %s', scope, ctx.user.scopes.join(', '))
|
||||||
};
|
};
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -83,21 +81,23 @@ function checkScope(name, ctx) {
|
|||||||
* check if user have permission to publish without scope
|
* check if user have permission to publish without scope
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function checkNoneScope(ctx) {
|
function checkNoneScope(name, ctx) {
|
||||||
if (!config.scopes
|
// admins unpublished everything
|
||||||
|| !config.scopes.length
|
if (ctx.user.isAdmin && ctx.method === 'DELETE') {
|
||||||
|| !config.forcePublishWithScope) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// only admins can publish or unpublish non-scope modules
|
|
||||||
if (ctx.user.isAdmin) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.status = 403;
|
ctx.status = 403;
|
||||||
|
if (ctx.user.scopes.length === 0) {
|
||||||
|
ctx.body = {
|
||||||
|
error: 'no_perms',
|
||||||
|
reason: 'can\'t publish non-scoped package, please set `config.scopes`'
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
error: 'no_perms',
|
error: 'no_perms',
|
||||||
reason: 'only allow publish with ' + ctx.user.scopes.join(',') + ' scope(s)'
|
reason: 'only allow publish with ' + ctx.user.scopes.join(', ') + ' scope(s)'
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,8 +14,8 @@
|
|||||||
* Module dependencies.
|
* Module dependencies.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = function *notFound(next) {
|
module.exports = function* notFound(next) {
|
||||||
yield *next;
|
yield* next;
|
||||||
|
|
||||||
if (this.status && this.status !== 404) {
|
if (this.status && this.status !== 404) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -21,21 +21,29 @@ var config = require('../config');
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = function* syncByInstall(next) {
|
module.exports = function* syncByInstall(next) {
|
||||||
if (!config.syncByInstall || !config.enablePrivate) {
|
this.allowSync = false;
|
||||||
|
if (!config.syncByInstall) {
|
||||||
// only config.enablePrivate should enable sync on install
|
// only config.enablePrivate should enable sync on install
|
||||||
return yield* next;
|
return yield* next;
|
||||||
}
|
}
|
||||||
// request not by node, consider it request from web
|
// request not by node, consider it request from web, dont sync
|
||||||
var ua = this.get('user-agent');
|
var ua = this.get('user-agent');
|
||||||
if (!ua || ua.indexOf('node') < 0) {
|
if (!ua || ua.indexOf('node') < 0) {
|
||||||
return yield* next;
|
return yield* next;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if request with `/xxx?write=true`, meaning the read request using for write
|
// if request with `/xxx?write=true`, meaning the read request using for write, dont sync
|
||||||
if (this.query.write) {
|
if (this.query.write) {
|
||||||
return yield* next;
|
return yield* next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var name = this.params.name || this.params[0];
|
||||||
|
|
||||||
|
// scoped package dont sync
|
||||||
|
if (name && name[0] === '@') {
|
||||||
|
return yield* next;
|
||||||
|
}
|
||||||
|
|
||||||
this.allowSync = true;
|
this.allowSync = true;
|
||||||
yield* next;
|
yield* next;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -16,8 +16,8 @@
|
|||||||
|
|
||||||
var debug = require('debug')('cnpmjs.org:middleware:web_not_found');
|
var debug = require('debug')('cnpmjs.org:middleware:web_not_found');
|
||||||
|
|
||||||
module.exports = function *notFound(next) {
|
module.exports = function* notFound(next) {
|
||||||
yield *next;
|
yield* next;
|
||||||
|
|
||||||
if (this.status && this.status !== 404) {
|
if (this.status && this.status !== 404) {
|
||||||
return;
|
return;
|
||||||
@@ -38,16 +38,16 @@ module.exports = function *notFound(next) {
|
|||||||
|
|
||||||
// package not found
|
// package not found
|
||||||
m = /\/package\/([\w\-\_\.]+)\/?$/.exec(this.url);
|
m = /\/package\/([\w\-\_\.]+)\/?$/.exec(this.url);
|
||||||
|
var name = null;
|
||||||
|
var title = '404: Page Not Found';
|
||||||
if (m) {
|
if (m) {
|
||||||
var name = m[1];
|
name = m[1];
|
||||||
this.status = 404;
|
title = name + ' Not Found';
|
||||||
yield* this.render('404', {
|
|
||||||
title: 'Package - ' + name,
|
|
||||||
name: name
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.status = 404;
|
this.status = 404;
|
||||||
this.body = 'Cannot GET ' + this.path;
|
yield* this.render('404', {
|
||||||
|
title: title,
|
||||||
|
name: name
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
159
models/_module_maintainer_class_methods.js
Normal file
159
models/_module_maintainer_class_methods.js
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - models/_module_maintainer_class_methods.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* list all module names by user
|
||||||
|
* @param {String} user
|
||||||
|
*/
|
||||||
|
|
||||||
|
exports.listModuleNamesByUser = function* (user) {
|
||||||
|
var rows = yield this.findAll({
|
||||||
|
attributrs: ['name'],
|
||||||
|
where: {
|
||||||
|
user: user
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return rows.map(function (row) {
|
||||||
|
return row.name;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* list all maintainers of module `name`
|
||||||
|
* @param {String} name
|
||||||
|
*/
|
||||||
|
|
||||||
|
exports.listMaintainers = function* (name) {
|
||||||
|
var rows = yield this.findAll({
|
||||||
|
attributrs: ['user'],
|
||||||
|
where: {
|
||||||
|
name: name
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return rows.map(function (row) {
|
||||||
|
return row.user;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* add a maintainer for module `name`
|
||||||
|
* @param {String} name
|
||||||
|
* @param {String} user
|
||||||
|
*/
|
||||||
|
|
||||||
|
exports.addMaintainer = function* (name, user) {
|
||||||
|
var row = yield this.find({
|
||||||
|
where: {
|
||||||
|
user: user,
|
||||||
|
name: name
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (!row) {
|
||||||
|
row = yield this.build({
|
||||||
|
user: user,
|
||||||
|
name: name
|
||||||
|
}).save();
|
||||||
|
}
|
||||||
|
return row;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* add maintainers for module `name`
|
||||||
|
* @param {String} name
|
||||||
|
* @param {Array} users
|
||||||
|
*/
|
||||||
|
|
||||||
|
exports.addMaintainers = function* (name, users) {
|
||||||
|
return yield users.map(function (user) {
|
||||||
|
return this.addMaintainer(name, user);
|
||||||
|
}.bind(this));
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* remove maintainers for module `name`
|
||||||
|
* @param {String} name
|
||||||
|
* @param {Array} users
|
||||||
|
*/
|
||||||
|
|
||||||
|
exports.removeMaintainers = function* (name, users) {
|
||||||
|
// removeMaintainers(name, oneUserName)
|
||||||
|
if (typeof users === 'string') {
|
||||||
|
users = [users];
|
||||||
|
}
|
||||||
|
if (users.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
yield this.destroy({
|
||||||
|
where: {
|
||||||
|
name: name,
|
||||||
|
user: users,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* remove all maintainers for module `name`
|
||||||
|
* @param {String} name
|
||||||
|
*/
|
||||||
|
|
||||||
|
exports.removeAllMaintainers = function* (name) {
|
||||||
|
yield this.destroy({
|
||||||
|
where: {
|
||||||
|
name: name
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* add maintainers to module
|
||||||
|
* @param {String} name
|
||||||
|
* @param {Array} users
|
||||||
|
*/
|
||||||
|
|
||||||
|
exports.updateMaintainers = function* (name, users) {
|
||||||
|
// maintainers should be [username1, username2, ...] format
|
||||||
|
// find out the exists maintainers
|
||||||
|
// then remove all the users not present and add all the left
|
||||||
|
|
||||||
|
if (users.length === 0) {
|
||||||
|
return {
|
||||||
|
add: [],
|
||||||
|
remove: []
|
||||||
|
};
|
||||||
|
}
|
||||||
|
var exists = yield* this.listMaintainers(name);
|
||||||
|
|
||||||
|
var 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) {
|
||||||
|
// remove user which in `exists` by not in `users`
|
||||||
|
return users.indexOf(username) === -1;
|
||||||
|
});
|
||||||
|
|
||||||
|
yield [
|
||||||
|
this.addMaintainers(name, addUsers),
|
||||||
|
this.removeMaintainers(name, removeUsers),
|
||||||
|
];
|
||||||
|
|
||||||
|
return {
|
||||||
|
add: addUsers,
|
||||||
|
remove: removeUsers
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
273
models/download_total.js
Normal file
273
models/download_total.js
Normal file
@@ -0,0 +1,273 @@
|
|||||||
|
/**!
|
||||||
|
* 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';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// 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(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',
|
||||||
|
// `d03` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '03 download count',
|
||||||
|
// `d04` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '04 download count',
|
||||||
|
// `d05` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '05 download count',
|
||||||
|
// `d06` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '06 download count',
|
||||||
|
// `d07` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '07 download count',
|
||||||
|
// `d08` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '08 download count',
|
||||||
|
// `d09` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '09 download count',
|
||||||
|
// `d10` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '10 download count',
|
||||||
|
// `d11` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '11 download count',
|
||||||
|
// `d12` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '12 download count',
|
||||||
|
// `d13` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '13 download count',
|
||||||
|
// `d14` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '14 download count',
|
||||||
|
// `d15` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '15 download count',
|
||||||
|
// `d16` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '16 download count',
|
||||||
|
// `d17` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '17 download count',
|
||||||
|
// `d18` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '18 download count',
|
||||||
|
// `d19` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '19 download count',
|
||||||
|
// `d20` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '20 download count',
|
||||||
|
// `d21` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '21 download count',
|
||||||
|
// `d22` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '22 download count',
|
||||||
|
// `d23` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '23 download count',
|
||||||
|
// `d24` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '24 download count',
|
||||||
|
// `d25` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '25 download count',
|
||||||
|
// `d26` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '26 download count',
|
||||||
|
// `d27` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '27 download count',
|
||||||
|
// `d28` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '28 download count',
|
||||||
|
// `d29` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '29 download count',
|
||||||
|
// `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 `name_date` (`name`, `date`)
|
||||||
|
// KEY `date` (`date`)
|
||||||
|
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module download total info';
|
||||||
|
|
||||||
|
module.exports = function (sequelize, DataTypes) {
|
||||||
|
return sequelize.define('DownloadTotal', {
|
||||||
|
name: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'module name',
|
||||||
|
},
|
||||||
|
date: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'YYYYMM format',
|
||||||
|
},
|
||||||
|
d01: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '01 download count',
|
||||||
|
},
|
||||||
|
d02: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '02 download count',
|
||||||
|
},
|
||||||
|
d03: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '03 download count',
|
||||||
|
},
|
||||||
|
d04: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '04 download count',
|
||||||
|
},
|
||||||
|
d05: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '05 download count',
|
||||||
|
},
|
||||||
|
d06: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '06 download count',
|
||||||
|
},
|
||||||
|
d07: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '07 download count',
|
||||||
|
},
|
||||||
|
d08: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '08 download count',
|
||||||
|
},
|
||||||
|
d09: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '09 download count',
|
||||||
|
},
|
||||||
|
d10: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '10 download count',
|
||||||
|
},
|
||||||
|
d11: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '11 download count',
|
||||||
|
},
|
||||||
|
d12: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '12 download count',
|
||||||
|
},
|
||||||
|
d13: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '13 download count',
|
||||||
|
},
|
||||||
|
d14: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '14 download count',
|
||||||
|
},
|
||||||
|
d15: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '15 download count',
|
||||||
|
},
|
||||||
|
d16: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '16 download count',
|
||||||
|
},
|
||||||
|
d17: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '17 download count',
|
||||||
|
},
|
||||||
|
d18: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '18 download count',
|
||||||
|
},
|
||||||
|
d19: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '19 download count',
|
||||||
|
},
|
||||||
|
d20: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '20 download count',
|
||||||
|
},
|
||||||
|
d21: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '21 download count',
|
||||||
|
},
|
||||||
|
d22: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '22 download count',
|
||||||
|
},
|
||||||
|
d23: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '23 download count',
|
||||||
|
},
|
||||||
|
d24: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '24 download count',
|
||||||
|
},
|
||||||
|
d25: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '25 download count',
|
||||||
|
},
|
||||||
|
d26: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '26 download count',
|
||||||
|
},
|
||||||
|
d27: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '27 download count',
|
||||||
|
},
|
||||||
|
d28: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '28 download count',
|
||||||
|
},
|
||||||
|
d29: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '29 download count',
|
||||||
|
},
|
||||||
|
d30: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '30 download count',
|
||||||
|
},
|
||||||
|
d31: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: '31 download count',
|
||||||
|
},
|
||||||
|
}, {
|
||||||
|
tableName: 'downloads',
|
||||||
|
comment: 'module download total info',
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
unique: true,
|
||||||
|
fields: ['name', 'date']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fields: ['date']
|
||||||
|
}
|
||||||
|
],
|
||||||
|
classMethods: {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
53
models/index.js
Normal file
53
models/index.js
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - models/index.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var path = require('path');
|
||||||
|
var Sequelize = require('sequelize');
|
||||||
|
var sequelize = require('../common/sequelize');
|
||||||
|
|
||||||
|
function load(name) {
|
||||||
|
return sequelize.import(path.join(__dirname, name));
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
sequelize: sequelize,
|
||||||
|
Module: load('module'),
|
||||||
|
ModuleLog: load('module_log'),
|
||||||
|
ModuleStar: load('module_star'),
|
||||||
|
ModuleKeyword: load('module_keyword'),
|
||||||
|
ModuleDependency: load('module_deps'),
|
||||||
|
ModuleMaintainer: load('module_maintainer'),
|
||||||
|
ModuleUnpublished: load('module_unpublished'),
|
||||||
|
NpmModuleMaintainer: load('npm_module_maintainer'),
|
||||||
|
|
||||||
|
Tag: load('tag'),
|
||||||
|
User: load('user'),
|
||||||
|
Total: load('total'),
|
||||||
|
DownloadTotal: load('download_total'),
|
||||||
|
|
||||||
|
query: function* (sql, args) {
|
||||||
|
var options = { replacements: args };
|
||||||
|
var data = yield this.sequelize.query(sql, options).spread();
|
||||||
|
if (/select /i.test(sql)) {
|
||||||
|
return data[0];
|
||||||
|
}
|
||||||
|
return data[1];
|
||||||
|
},
|
||||||
|
queryOne: function* (sql, args) {
|
||||||
|
var rows = yield* this.query(sql, args);
|
||||||
|
return rows && rows[0];
|
||||||
|
}
|
||||||
|
};
|
||||||
56
models/init_script.js
Normal file
56
models/init_script.js
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
/**!
|
||||||
|
* 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';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var 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];
|
||||||
|
if (dialect) {
|
||||||
|
config.database.dialect = dialect;
|
||||||
|
}
|
||||||
|
var port = process.argv[4];
|
||||||
|
if (port) {
|
||||||
|
config.database.port = parseInt(port);
|
||||||
|
}
|
||||||
|
var username = process.argv[5];
|
||||||
|
if (username) {
|
||||||
|
config.database.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
var models = require('./');
|
||||||
|
|
||||||
|
models.sequelize.sync({ force: force })
|
||||||
|
.then(function () {
|
||||||
|
models.Total.init(function (err) {
|
||||||
|
if (err) {
|
||||||
|
console.error('[models/init_script.js] sequelize init fail');
|
||||||
|
console.error(err);
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
console.log('[models/init_script.js] `%s` sequelize sync and init success',
|
||||||
|
config.database.dialect);
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch(function (err) {
|
||||||
|
console.error('[models/init_script.js] sequelize sync fail');
|
||||||
|
console.error(err);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
108
models/module.js
Normal file
108
models/module.js
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
/**!
|
||||||
|
* 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` 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) {
|
||||||
|
return sequelize.define('Module', {
|
||||||
|
author: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'first maintainer name'
|
||||||
|
},
|
||||||
|
name: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'module name'
|
||||||
|
},
|
||||||
|
version: {
|
||||||
|
type: DataTypes.STRING(30),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'module version'
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
type: DataTypes.LONGTEXT,
|
||||||
|
},
|
||||||
|
package: {
|
||||||
|
type: DataTypes.LONGTEXT,
|
||||||
|
comment: 'package.json',
|
||||||
|
},
|
||||||
|
dist_shasum: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
allowNull: true,
|
||||||
|
},
|
||||||
|
dist_tarball: {
|
||||||
|
type: DataTypes.STRING(2048),
|
||||||
|
allowNull: true,
|
||||||
|
},
|
||||||
|
dist_size: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
},
|
||||||
|
publish_time: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: true,
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
tableName: 'module',
|
||||||
|
comment: 'module info',
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
unique: true,
|
||||||
|
fields: ['name', 'version']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fields: ['gmt_modified']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fields: ['publish_time']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fields: ['author']
|
||||||
|
}
|
||||||
|
],
|
||||||
|
classMethods: {
|
||||||
|
findByNameAndVersion: function* (name, version) {
|
||||||
|
return yield this.find({
|
||||||
|
where: { name: name, version: version }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
58
models/module_deps.js
Normal file
58
models/module_deps.js
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - models/module_deps.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_deps` (
|
||||||
|
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
|
||||||
|
`gmt_create` datetime NOT NULL COMMENT 'create time',
|
||||||
|
`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 `module_deps_name_deps` (`name`,`deps`),
|
||||||
|
KEY `deps` (`module_deps_deps`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module deps';
|
||||||
|
*/
|
||||||
|
|
||||||
|
module.exports = function (sequelize, DataTypes) {
|
||||||
|
return sequelize.define('ModuleDependency', {
|
||||||
|
name: {
|
||||||
|
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`'
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
tableName: 'module_deps',
|
||||||
|
comment: 'module deps',
|
||||||
|
// no need update timestamp
|
||||||
|
updatedAt: false,
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
unique: true,
|
||||||
|
fields: ['name', 'deps']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fields: ['deps']
|
||||||
|
}
|
||||||
|
],
|
||||||
|
classMethods: {
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
69
models/module_keyword.js
Normal file
69
models/module_keyword.js
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - models/module_keyword.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_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(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
|
||||||
|
`description` longtext,
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
UNIQUE KEY `keyword_module_name` (`keyword`,`name`),
|
||||||
|
KEY `name` (`name`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module keyword';
|
||||||
|
*/
|
||||||
|
|
||||||
|
module.exports = function (sequelize, DataTypes) {
|
||||||
|
return sequelize.define('ModuleKeyword', {
|
||||||
|
keyword: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
allowNull: false,
|
||||||
|
},
|
||||||
|
name: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'module name',
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
type: DataTypes.LONGTEXT,
|
||||||
|
allowNull: true,
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
tableName: 'module_keyword',
|
||||||
|
comment: 'module keyword',
|
||||||
|
updatedAt: false,
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
unique: true,
|
||||||
|
fields: ['keyword', 'name']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fields: ['name']
|
||||||
|
}
|
||||||
|
],
|
||||||
|
classMethods: {
|
||||||
|
findByKeywordAndName: function* (keyword, name) {
|
||||||
|
return yield this.find({
|
||||||
|
where: {
|
||||||
|
keyword: keyword,
|
||||||
|
name: name
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
56
models/module_log.js
Normal file
56
models/module_log.js
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - models/module_log.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_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,
|
||||||
|
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
|
||||||
|
`log` longtext,
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
KEY `module_log_name` (`name`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module sync log';
|
||||||
|
*/
|
||||||
|
|
||||||
|
module.exports = function (sequelize, DataTypes) {
|
||||||
|
return sequelize.define('ModuleLog', {
|
||||||
|
username: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'user name'
|
||||||
|
},
|
||||||
|
name: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'module name',
|
||||||
|
},
|
||||||
|
log: {
|
||||||
|
type: DataTypes.LONGTEXT
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
tableName: 'module_log',
|
||||||
|
comment: 'module sync log',
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
fields: ['name']
|
||||||
|
}
|
||||||
|
],
|
||||||
|
classMethods: {
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
56
models/module_maintainer.js
Normal file
56
models/module_maintainer.js
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - models/module_maintainer.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_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(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
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) {
|
||||||
|
return sequelize.define('ModuleMaintainer', {
|
||||||
|
user: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'user name'
|
||||||
|
},
|
||||||
|
name: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'module name',
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
tableName: 'module_maintainer',
|
||||||
|
comment: 'private module maintainers',
|
||||||
|
updatedAt: false,
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
unique: true,
|
||||||
|
fields: ['user', 'name']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fields: ['name']
|
||||||
|
}
|
||||||
|
],
|
||||||
|
classMethods: require('./_module_maintainer_class_methods'),
|
||||||
|
});
|
||||||
|
};
|
||||||
57
models/module_star.js
Normal file
57
models/module_star.js
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - models/module_star.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_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(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
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) {
|
||||||
|
return sequelize.define('ModuleStar', {
|
||||||
|
user: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'user name'
|
||||||
|
},
|
||||||
|
name: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'module name',
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
tableName: 'module_star',
|
||||||
|
comment: 'module star',
|
||||||
|
updatedAt: false,
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
unique: true,
|
||||||
|
fields: ['user', 'name']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fields: ['name']
|
||||||
|
}
|
||||||
|
],
|
||||||
|
classMethods: {
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
87
models/module_unpublished.js
Normal file
87
models/module_unpublished.js
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - models/module_unpublished.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var 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(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 `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) {
|
||||||
|
return sequelize.define('ModuleUnpublished', {
|
||||||
|
name: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'module name',
|
||||||
|
},
|
||||||
|
package: {
|
||||||
|
type: DataTypes.LONGTEXT,
|
||||||
|
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: ['gmt_modified']
|
||||||
|
}
|
||||||
|
],
|
||||||
|
classMethods: {
|
||||||
|
findByName: function* (name) {
|
||||||
|
return yield this.find({
|
||||||
|
where: {
|
||||||
|
name: name
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
save: function* (name, pkg) {
|
||||||
|
var row = yield this.find({
|
||||||
|
where: {
|
||||||
|
name: name
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (row) {
|
||||||
|
row.package = pkg;
|
||||||
|
if (row.isDirty) {
|
||||||
|
row = yield row.save(['package']);
|
||||||
|
}
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
|
||||||
|
row = this.build({
|
||||||
|
name: name,
|
||||||
|
package: pkg,
|
||||||
|
});
|
||||||
|
return yield row.save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
56
models/npm_module_maintainer.js
Normal file
56
models/npm_module_maintainer.js
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - models/npm_module_maintainer.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 `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(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
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) {
|
||||||
|
return sequelize.define('NpmModuleMaintainer', {
|
||||||
|
user: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'user name'
|
||||||
|
},
|
||||||
|
name: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'module name',
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
tableName: 'npm_module_maintainer',
|
||||||
|
comment: 'npm original module maintainers',
|
||||||
|
updatedAt: false,
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
unique: true,
|
||||||
|
fields: ['user', 'name']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fields: ['name']
|
||||||
|
}
|
||||||
|
],
|
||||||
|
classMethods: require('./_module_maintainer_class_methods'),
|
||||||
|
});
|
||||||
|
};
|
||||||
72
models/tag.js
Normal file
72
models/tag.js
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - models/tag.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 `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(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 `tag_name_tag` (`name`, `tag`),
|
||||||
|
KEY `tag_gmt_modified` (`gmt_modified`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module tag';
|
||||||
|
*/
|
||||||
|
|
||||||
|
module.exports = function (sequelize, DataTypes) {
|
||||||
|
return sequelize.define('Tag', {
|
||||||
|
name: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'module name',
|
||||||
|
},
|
||||||
|
tag: {
|
||||||
|
type: DataTypes.STRING(30),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'tag name',
|
||||||
|
},
|
||||||
|
version: {
|
||||||
|
type: DataTypes.STRING(30),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'module version',
|
||||||
|
},
|
||||||
|
module_id: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'module id'
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
tableName: 'tag',
|
||||||
|
comment: 'module tag',
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
unique: true,
|
||||||
|
fields: ['name', 'tag']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fields: ['gmt_modified']
|
||||||
|
}
|
||||||
|
],
|
||||||
|
classMethods: {
|
||||||
|
findByNameAndTag: function* (name, tag) {
|
||||||
|
return yield this.find({ where: { name: name, tag: tag } });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
118
models/total.js
Normal file
118
models/total.js
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
/**!
|
||||||
|
* 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(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',
|
||||||
|
// `last_exist_sync_time` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'last timestamp sync exist packages from official registry',
|
||||||
|
// `sync_status` tinyint unsigned NOT NULL DEFAULT '0' COMMENT 'system sync from official registry status',
|
||||||
|
// `need_sync_num` int unsigned NOT NULL DEFAULT '0' COMMENT 'how many packages need to be sync',
|
||||||
|
// `success_sync_num` int unsigned NOT NULL DEFAULT '0' COMMENT 'how many packages sync success at this time',
|
||||||
|
// `fail_sync_num` int unsigned NOT NULL DEFAULT '0' COMMENT 'how many packages sync fail at this time',
|
||||||
|
// `left_sync_num` int unsigned NOT NULL DEFAULT '0' COMMENT 'how many packages left to be sync',
|
||||||
|
// `last_sync_module` varchar(100) COMMENT 'last sync success module name',
|
||||||
|
// PRIMARY KEY (`name`)
|
||||||
|
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='total info';
|
||||||
|
// -- init `total` count
|
||||||
|
// INSERT INTO total(name, gmt_modified) VALUES('total', now())
|
||||||
|
// ON DUPLICATE KEY UPDATE gmt_modified=now();
|
||||||
|
|
||||||
|
module.exports = function (sequelize, DataTypes) {
|
||||||
|
return sequelize.define('Total', {
|
||||||
|
name: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
primaryKey: true,
|
||||||
|
comment: 'total name'
|
||||||
|
},
|
||||||
|
module_delete: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: 'module delete count',
|
||||||
|
},
|
||||||
|
last_sync_time: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: 'last timestamp sync from official registry',
|
||||||
|
},
|
||||||
|
last_exist_sync_time: {
|
||||||
|
type: DataTypes.BIGINT(20),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: 'last timestamp sync exist packages from official registry',
|
||||||
|
},
|
||||||
|
sync_status: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: 'system sync from official registry status',
|
||||||
|
},
|
||||||
|
need_sync_num: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: 'how many packages need to be sync',
|
||||||
|
},
|
||||||
|
success_sync_num: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: 'how many packages sync success at this time',
|
||||||
|
},
|
||||||
|
fail_sync_num: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: 'how many packages sync fail at this time',
|
||||||
|
},
|
||||||
|
left_sync_num: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: 'how many packages left to be sync',
|
||||||
|
},
|
||||||
|
last_sync_module: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
allowNull: true,
|
||||||
|
comment: 'last sync success module name',
|
||||||
|
},
|
||||||
|
}, {
|
||||||
|
tableName: 'total',
|
||||||
|
comment: 'total info',
|
||||||
|
createdAt: false,
|
||||||
|
classMethods: {
|
||||||
|
init: function (callback) {
|
||||||
|
var that = this;
|
||||||
|
that.find({
|
||||||
|
where: { name: 'total' }
|
||||||
|
}).then(function (row) {
|
||||||
|
if (!row) {
|
||||||
|
that.build({name: 'total'}).save()
|
||||||
|
.then(function () {
|
||||||
|
callback();
|
||||||
|
})
|
||||||
|
.catch(callback);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
callback();
|
||||||
|
}).catch(callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
250
models/user.js
Normal file
250
models/user.js
Normal file
@@ -0,0 +1,250 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - models/user.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var utility = require('utility');
|
||||||
|
var utils = require('./utils');
|
||||||
|
|
||||||
|
/*
|
||||||
|
CREATE TABLE IF NOT EXISTS `user` (
|
||||||
|
`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(100) NOT NULL COMMENT 'user name',
|
||||||
|
`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 '[]',
|
||||||
|
`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 `user_name` (`name`),
|
||||||
|
KEY `user_gmt_modified` (`gmt_modified`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='user base info';
|
||||||
|
*/
|
||||||
|
|
||||||
|
module.exports = function (sequelize, DataTypes) {
|
||||||
|
return sequelize.define('User', {
|
||||||
|
name: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'user name',
|
||||||
|
},
|
||||||
|
salt: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
allowNull: false,
|
||||||
|
},
|
||||||
|
password_sha: {
|
||||||
|
type: DataTypes.STRING(100),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'user password hash',
|
||||||
|
},
|
||||||
|
ip: {
|
||||||
|
type: DataTypes.STRING(64),
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'user last request ip',
|
||||||
|
},
|
||||||
|
roles: {
|
||||||
|
type: DataTypes.STRING(200),
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: '[]',
|
||||||
|
},
|
||||||
|
rev: {
|
||||||
|
type: DataTypes.STRING(40),
|
||||||
|
allowNull: false,
|
||||||
|
},
|
||||||
|
email: {
|
||||||
|
type: DataTypes.STRING(400),
|
||||||
|
allowNull: false,
|
||||||
|
},
|
||||||
|
json: {
|
||||||
|
type: DataTypes.LONGTEXT,
|
||||||
|
allowNull: true,
|
||||||
|
get: utils.JSONGetter('json'),
|
||||||
|
set: utils.JSONSetter('json'),
|
||||||
|
},
|
||||||
|
isNpmUser: {
|
||||||
|
field: 'npm_user',
|
||||||
|
type: DataTypes.BOOLEAN,
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: false,
|
||||||
|
comment: 'user sync from npm or not, 1: true, other: false',
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
tableName: 'user',
|
||||||
|
comment: 'user base info',
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
unique: true,
|
||||||
|
fields: ['name']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fields: ['gmt_modified']
|
||||||
|
}
|
||||||
|
],
|
||||||
|
classMethods: {
|
||||||
|
// utils
|
||||||
|
createPasswordSha: function (password, salt) {
|
||||||
|
return utility.sha1(password + salt);
|
||||||
|
},
|
||||||
|
|
||||||
|
// read
|
||||||
|
auth: function* (name, password) {
|
||||||
|
var user = yield* this.findByName(name);
|
||||||
|
if (user) {
|
||||||
|
var sha = this.createPasswordSha(password, user.salt);
|
||||||
|
if (user.password_sha !== sha) {
|
||||||
|
user = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return user;
|
||||||
|
},
|
||||||
|
findByName: function* (name) {
|
||||||
|
return yield this.find({ where: { name: name } });
|
||||||
|
},
|
||||||
|
listByNames: function* (names) {
|
||||||
|
if (!names || names.length === 0) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
return yield this.findAll({
|
||||||
|
where: {
|
||||||
|
name: {
|
||||||
|
in: names
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
search: function* (query, options) {
|
||||||
|
return yield this.findAll({
|
||||||
|
where: {
|
||||||
|
name: {
|
||||||
|
like: query + '%'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
limit: options.limit
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// write
|
||||||
|
saveNpmUser: function* (data) {
|
||||||
|
var user = yield* this.findByName(data.name);
|
||||||
|
if (!user) {
|
||||||
|
user = this.build({
|
||||||
|
isNpmUser: true,
|
||||||
|
name: data.name,
|
||||||
|
salt: '0',
|
||||||
|
password_sha: '0',
|
||||||
|
ip: '0',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
user.isNpmUser = true;
|
||||||
|
user.json = data;
|
||||||
|
user.email = data.email || '';
|
||||||
|
user.rev = data._rev || '';
|
||||||
|
if (user.isDirty) {
|
||||||
|
user = yield user.save();
|
||||||
|
}
|
||||||
|
return user;
|
||||||
|
},
|
||||||
|
saveCustomUser: function* (data) {
|
||||||
|
var name = data.user.login;
|
||||||
|
var user = yield* this.findByName(name);
|
||||||
|
if (!user) {
|
||||||
|
user = this.build({
|
||||||
|
isNpmUser: false,
|
||||||
|
name: name,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
var rev = '1-' + data.user.login;
|
||||||
|
var salt = data.salt || '0';
|
||||||
|
var passwordSha = data.password_sha || '0';
|
||||||
|
var ip = data.ip || '0';
|
||||||
|
|
||||||
|
user.isNpmUser = false;
|
||||||
|
user.email = data.user.email;
|
||||||
|
user.ip = ip;
|
||||||
|
user.json = data.user;
|
||||||
|
user.rev = rev;
|
||||||
|
user.salt = salt;
|
||||||
|
user.password_sha = passwordSha;
|
||||||
|
if (user.isDirty) {
|
||||||
|
user = yield user.save();
|
||||||
|
}
|
||||||
|
return user;
|
||||||
|
},
|
||||||
|
|
||||||
|
// add cnpm user
|
||||||
|
add: function* (user) {
|
||||||
|
var roles = user.roles || [];
|
||||||
|
try {
|
||||||
|
roles = JSON.stringify(roles);
|
||||||
|
} catch (e) {
|
||||||
|
roles = '[]';
|
||||||
|
}
|
||||||
|
var rev = '1-' + utility.md5(JSON.stringify(user));
|
||||||
|
|
||||||
|
var row = this.build({
|
||||||
|
rev: rev,
|
||||||
|
name: user.name,
|
||||||
|
email: user.email,
|
||||||
|
salt: user.salt,
|
||||||
|
password_sha: user.password_sha,
|
||||||
|
ip: user.ip,
|
||||||
|
roles: roles,
|
||||||
|
isNpmUser: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
return yield row.save();
|
||||||
|
},
|
||||||
|
|
||||||
|
update: function* (user) {
|
||||||
|
var rev = user.rev || user._rev;
|
||||||
|
var revNo = Number(rev.split('-', 1));
|
||||||
|
if (!revNo) {
|
||||||
|
var err = new Error(rev + ' format error');
|
||||||
|
err.name = 'RevFormatError';
|
||||||
|
err.data = {user: user};
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
revNo++;
|
||||||
|
var newRev = revNo + '-' + utility.md5(JSON.stringify(user));
|
||||||
|
var roles = user.roles || [];
|
||||||
|
try {
|
||||||
|
roles = JSON.stringify(roles);
|
||||||
|
} catch (e) {
|
||||||
|
roles = '[]';
|
||||||
|
}
|
||||||
|
|
||||||
|
var row = yield* this.findByName(user.name);
|
||||||
|
if (!row) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
row.rev = newRev;
|
||||||
|
row.email = user.email;
|
||||||
|
row.salt = user.salt;
|
||||||
|
row.password_sha = user.password_sha;
|
||||||
|
row.ip = user.ip;
|
||||||
|
row.roles = roles;
|
||||||
|
row.isNpmUser = false;
|
||||||
|
|
||||||
|
return yield row.save(['rev', 'email', 'salt', 'password_sha', 'ip', 'roles', 'isNpmUser']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
34
models/utils.js
Normal file
34
models/utils.js
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
/**!
|
||||||
|
* cnpmjs.org - models/utils.js
|
||||||
|
*
|
||||||
|
* Copyright(c) fengmk2 and other contributors.
|
||||||
|
* MIT Licensed
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
exports.JSONGetter = function (propertyName) {
|
||||||
|
return function JSONGetter() {
|
||||||
|
var value = this.getDataValue(propertyName);
|
||||||
|
if (value && typeof value === 'string') {
|
||||||
|
value = JSON.parse(value);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.JSONSetter = function (propertyName) {
|
||||||
|
return function JSONSetter(value) {
|
||||||
|
if (typeof value !== 'string') {
|
||||||
|
value = JSON.stringify(value);
|
||||||
|
}
|
||||||
|
this.setDataValue(propertyName, value);
|
||||||
|
};
|
||||||
|
};
|
||||||
90
package.json
90
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "cnpmjs.org",
|
"name": "cnpmjs.org",
|
||||||
"version": "1.4.0",
|
"version": "2.1.3",
|
||||||
"description": "Private npm registry and web for Enterprise, base on MySQL and Simple Store Service",
|
"description": "Private npm registry and web for Enterprise, base on MySQL and Simple Store Service",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -9,59 +9,69 @@
|
|||||||
"status": "./bin/nodejsctl status",
|
"status": "./bin/nodejsctl status",
|
||||||
"stop": "./bin/nodejsctl stop"
|
"stop": "./bin/nodejsctl stop"
|
||||||
},
|
},
|
||||||
|
"bin": {
|
||||||
|
"cnpmjs.org": "bin/cli.js"
|
||||||
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"agentkeepalive": "~1.2.0",
|
||||||
|
"bluebird": "~2.9.7",
|
||||||
"bytes": "~1.0.0",
|
"bytes": "~1.0.0",
|
||||||
"cfork": "~1.1.0",
|
"cfork": "~1.2.2",
|
||||||
"cheerio": "~0.17.0",
|
"co": "~4.3.1",
|
||||||
"co": "~3.1.0",
|
"co-defer": "~1.0.0",
|
||||||
"co-defer": "~0.1.0",
|
|
||||||
"co-gather": "~0.0.1",
|
"co-gather": "~0.0.1",
|
||||||
"co-read": "~0.1.0",
|
"co-sleep": "~0.0.1",
|
||||||
"co-redis": "~1.1.0",
|
"commander": "~2.6.0",
|
||||||
"co-urllib": "~0.2.3",
|
"copy-to": "~2.0.1",
|
||||||
"co-write": "~0.3.0",
|
"debug": "~2.1.1",
|
||||||
"copy-to": "~1.0.1",
|
|
||||||
"debug": "~1.0.4",
|
|
||||||
"error-formater": "~1.0.3",
|
"error-formater": "~1.0.3",
|
||||||
"eventproxy": "~0.3.1",
|
"fs-cnpm": "~1.2.0",
|
||||||
"fs-cnpm": "~1.0.2",
|
|
||||||
"giturl": "~0.0.3",
|
"giturl": "~0.0.3",
|
||||||
"graceful": "~0.1.0",
|
"gnode": "~0.1.1",
|
||||||
"gravatar": "~1.0.6",
|
"graceful": "~1.0.0",
|
||||||
|
"gravatar": "~1.1.0",
|
||||||
|
"humanize-ms": "~1.0.1",
|
||||||
"humanize-number": "~0.0.2",
|
"humanize-number": "~0.0.2",
|
||||||
"koa": "~0.10.0",
|
"kcors": "~1.0.1",
|
||||||
|
"koa": "~0.17.0",
|
||||||
"koa-limit": "~1.0.2",
|
"koa-limit": "~1.0.2",
|
||||||
"koa-markdown": "~0.0.3",
|
"koa-markdown": "~2.0.1",
|
||||||
"koa-middlewares": "~1.2.0",
|
"koa-middlewares": "~2.1.0",
|
||||||
"marked": "~0.3.2",
|
"koa-mock": "~1.1.4",
|
||||||
"mime": "~1.2.11",
|
"koa-safe-jsonp": "~0.3.0",
|
||||||
"mini-logger": "~0.3.0",
|
"markdown-it": "~3.0.6",
|
||||||
|
"mime": "~1.3.4",
|
||||||
|
"mini-logger": "~1.0.0",
|
||||||
"mkdirp": "~0.5.0",
|
"mkdirp": "~0.5.0",
|
||||||
"moment": "~2.8.1",
|
"moment": "~2.9.0",
|
||||||
"ms": "~0.6.2",
|
"mysql": "~2.5.4",
|
||||||
"multiline": "~1.0.0",
|
"nodemailer": "~1.3.0",
|
||||||
"mysql": "~2.4.2",
|
"semver": "~4.2.0",
|
||||||
"nodemailer": "0.7.1",
|
"sequelize": "~2.0.1",
|
||||||
"ready": "~0.1.1",
|
"thunkify-wrap": "~1.0.4",
|
||||||
"redis": "~0.12.1",
|
"treekill": "~1.0.0",
|
||||||
"semver": "~3.0.1",
|
"tunnel-agent": "^0.4.0",
|
||||||
"thunkify-wrap": "~1.0.2",
|
"urllib": "~2.2.2",
|
||||||
"utility": "~1.0.0"
|
"utility": "~1.3.0",
|
||||||
|
"xss": "~0.1.20"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"autod": "~0.2.0",
|
"autod": "*",
|
||||||
"chunkstream": "~0.0.1",
|
"chunkstream": "*",
|
||||||
"co-mocha": "0.0.2",
|
"co-mocha": "*",
|
||||||
"contributors": "*",
|
"contributors": "*",
|
||||||
"cov": "*",
|
|
||||||
"istanbul-harmony": "*",
|
"istanbul-harmony": "*",
|
||||||
"jshint": "*",
|
"jshint": "*",
|
||||||
"mm": "~0.2.1",
|
"mm": "*",
|
||||||
"mocha": "*",
|
"mocha": "*",
|
||||||
"pedding": "~1.0.0",
|
"node-dev": "*",
|
||||||
|
"pedding": "*",
|
||||||
|
"pg": "~4.2.0",
|
||||||
|
"pg-hstore": "~2.3.1",
|
||||||
"should": "~4.0.4",
|
"should": "~4.0.4",
|
||||||
"should-http": "0.0.1",
|
"should-http": "*",
|
||||||
"supertest": "~0.13.0"
|
"sqlite3": "*",
|
||||||
|
"supertest": "*"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/cnpm/cnpmjs.org",
|
"homepage": "https://github.com/cnpm/cnpmjs.org",
|
||||||
"repository": {
|
"repository": {
|
||||||
@@ -81,7 +91,7 @@
|
|||||||
"registry"
|
"registry"
|
||||||
],
|
],
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">= 0.11.9"
|
"node": ">= 0.11.14"
|
||||||
},
|
},
|
||||||
"author": [
|
"author": [
|
||||||
"fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)",
|
"fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)",
|
||||||
|
|||||||
@@ -1,86 +0,0 @@
|
|||||||
/**!
|
|
||||||
* cnpmjs.org - proxy/dist.js
|
|
||||||
*
|
|
||||||
* Copyright(c) fengmk2 and other contributors.
|
|
||||||
* MIT Licensed
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
/* jshint -W032 */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var path = require('path');
|
|
||||||
var multiline = require('multiline');
|
|
||||||
var mysql = require('../common/mysql');
|
|
||||||
|
|
||||||
var SAVE_FILE_SQL = multiline(function () {;/*
|
|
||||||
INSERT INTO
|
|
||||||
dist_file(gmt_create, gmt_modified, name, parent, date, size, url, sha1)
|
|
||||||
VALUES
|
|
||||||
(now(), now(), ?, ?, ?, ?, ?, ?)
|
|
||||||
ON DUPLICATE KEY UPDATE
|
|
||||||
name=VALUES(name),
|
|
||||||
parent=VALUES(parent),
|
|
||||||
date=VALUES(date),
|
|
||||||
size=VALUES(size),
|
|
||||||
url=VALUES(url),
|
|
||||||
sha1=VALUES(sha1);
|
|
||||||
*/});
|
|
||||||
|
|
||||||
exports.savefile = function* (info) {
|
|
||||||
return yield mysql.query(SAVE_FILE_SQL, [
|
|
||||||
info.name, info.parent, info.date, info.size, info.url, info.sha1
|
|
||||||
]);
|
|
||||||
};
|
|
||||||
|
|
||||||
var SAVE_DIR_SQL = multiline(function () {;/*
|
|
||||||
INSERT INTO
|
|
||||||
dist_dir(gmt_create, gmt_modified, name, parent, date)
|
|
||||||
VALUES
|
|
||||||
(now(), now(), ?, ?, ?)
|
|
||||||
ON DUPLICATE KEY UPDATE
|
|
||||||
name=VALUES(name),
|
|
||||||
parent=VALUES(parent),
|
|
||||||
date=VALUES(date);
|
|
||||||
*/});
|
|
||||||
|
|
||||||
exports.savedir = function* (info) {
|
|
||||||
return yield mysql.query(SAVE_DIR_SQL, [
|
|
||||||
info.name, info.parent, info.date
|
|
||||||
]);
|
|
||||||
};
|
|
||||||
|
|
||||||
var LIST_DIRS_SQL = multiline(function () {;/*
|
|
||||||
SELECT name, parent, date FROM dist_dir WHERE parent = ?;
|
|
||||||
*/});
|
|
||||||
var LIST_FILES_SQL = multiline(function () {;/*
|
|
||||||
SELECT name, parent, date, size, url, sha1
|
|
||||||
FROM dist_file WHERE parent = ?;
|
|
||||||
*/});
|
|
||||||
|
|
||||||
exports.listdir = function* (name) {
|
|
||||||
var rs = yield [
|
|
||||||
mysql.query(LIST_DIRS_SQL, [name]),
|
|
||||||
mysql.query(LIST_FILES_SQL, [name]),
|
|
||||||
];
|
|
||||||
return rs[0].concat(rs[1]);
|
|
||||||
};
|
|
||||||
|
|
||||||
var GET_FILE_SQL = multiline(function () {;/*
|
|
||||||
SELECT name, parent, date, url, size, sha1 FROM dist_file WHERE parent = ? AND name = ?;
|
|
||||||
*/});
|
|
||||||
|
|
||||||
exports.getfile = function* (fullname) {
|
|
||||||
var name = path.basename(fullname);
|
|
||||||
var parent = path.dirname(fullname);
|
|
||||||
if (parent !== '/') {
|
|
||||||
parent += '/';
|
|
||||||
}
|
|
||||||
return yield mysql.queryOne(GET_FILE_SQL, [parent, name]);
|
|
||||||
};
|
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
/**!
|
|
||||||
* cnpmjs.org - proxy/download.js
|
|
||||||
*
|
|
||||||
* Copyright(c) cnpmjs.org and other contributors.
|
|
||||||
* MIT Licensed
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
/* jshint -W032 */
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var thunkify = require('thunkify-wrap');
|
|
||||||
var config = require('../config');
|
|
||||||
var mysql = require('../common/mysql');
|
|
||||||
var multiline = require('multiline');
|
|
||||||
|
|
||||||
var PLUS_SQL = multiline(function () {;/*
|
|
||||||
INSERT INTO
|
|
||||||
download_total(gmt_create, gmt_modified, date, name, count)
|
|
||||||
VALUES
|
|
||||||
(now(), now(), ?, ?, ?)
|
|
||||||
ON DUPLICATE KEY UPDATE
|
|
||||||
count=count + VALUES(count),
|
|
||||||
name=VALUES(name),
|
|
||||||
date=VALUES(date);
|
|
||||||
*/});
|
|
||||||
exports.plusTotal = function (data, callback) {
|
|
||||||
mysql.query(PLUS_SQL, [data.date, data.name, data.count], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
var SELECT_ONE_TOTAL_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
date, count
|
|
||||||
FROM
|
|
||||||
download_total
|
|
||||||
WHERE
|
|
||||||
date>=? AND date<=? AND name=?;
|
|
||||||
*/});
|
|
||||||
exports.getModuleTotal = function (name, start, end, callback) {
|
|
||||||
mysql.query(SELECT_ONE_TOTAL_SQL, [start, end, name], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
var SELECT_ALL_TOTAL_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
date, sum(count) AS count
|
|
||||||
FROM
|
|
||||||
download_total
|
|
||||||
WHERE
|
|
||||||
date>=? AND date<=?
|
|
||||||
GROUP BY
|
|
||||||
date;
|
|
||||||
*/});
|
|
||||||
exports.getTotal = function (start, end, callback) {
|
|
||||||
mysql.query(SELECT_ALL_TOTAL_SQL, [start, end], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
thunkify(exports);
|
|
||||||
768
proxy/module.js
768
proxy/module.js
@@ -1,768 +0,0 @@
|
|||||||
/**!
|
|
||||||
* cnpmjs.org - proxy/module.js
|
|
||||||
*
|
|
||||||
* Copyright(c) cnpmjs.org and other contributors.
|
|
||||||
* MIT Licensed
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
/* jshint -W032 */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var thunkify = require('thunkify-wrap');
|
|
||||||
var utility = require('utility');
|
|
||||||
var eventproxy = require('eventproxy');
|
|
||||||
var config = require('../config');
|
|
||||||
var mysql = require('../common/mysql');
|
|
||||||
var multiline = require('multiline');
|
|
||||||
|
|
||||||
var INSERT_MODULE_SQL = multiline(function () {;/*
|
|
||||||
INSERT INTO
|
|
||||||
module(gmt_create, gmt_modified, publish_time, author, name, version,
|
|
||||||
package, dist_tarball, dist_shasum, dist_size, description)
|
|
||||||
VALUES
|
|
||||||
(now(), now(), ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
||||||
ON DUPLICATE KEY UPDATE
|
|
||||||
gmt_modified=now(),
|
|
||||||
publish_time=VALUES(publish_time),
|
|
||||||
description=VALUES(description),
|
|
||||||
author=VALUES(author),
|
|
||||||
name=VALUES(name),
|
|
||||||
version=VALUES(version),
|
|
||||||
package=VALUES(package),
|
|
||||||
dist_tarball=VALUES(dist_tarball),
|
|
||||||
dist_shasum=VALUES(dist_shasum),
|
|
||||||
dist_size=VALUES(dist_size);
|
|
||||||
*/});
|
|
||||||
|
|
||||||
exports.add = function (mod, callback) {
|
|
||||||
var keywords = mod.package.keywords;
|
|
||||||
var pkg;
|
|
||||||
try {
|
|
||||||
pkg = stringifyPackage(mod.package);
|
|
||||||
} catch (e) {
|
|
||||||
return callback(e);
|
|
||||||
}
|
|
||||||
var description = mod.package && mod.package.description || '';
|
|
||||||
|
|
||||||
var dist = mod.package.dist || {};
|
|
||||||
dist.tarball = '';
|
|
||||||
dist.shasum = '';
|
|
||||||
dist.size = 0;
|
|
||||||
var publish_time = mod.publish_time || Date.now();
|
|
||||||
var values = [
|
|
||||||
publish_time, mod.author, mod.name, mod.version, pkg,
|
|
||||||
dist.tarball, dist.shasum, dist.size, description
|
|
||||||
];
|
|
||||||
mysql.query(INSERT_MODULE_SQL, values, function (err, result) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
callback(null, {id: result.insertId, gmt_modified: new Date()});
|
|
||||||
|
|
||||||
if (typeof keywords === 'string') {
|
|
||||||
keywords = [keywords];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Array.isArray(keywords)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var words = [];
|
|
||||||
for (var i = 0; i < keywords.length; i++) {
|
|
||||||
var w = keywords[i];
|
|
||||||
if (typeof w === 'string') {
|
|
||||||
w = w.trim();
|
|
||||||
if (w) {
|
|
||||||
words.push(w);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (words.length === 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// add keywords
|
|
||||||
exports.addKeywords(mod, description, words, utility.noop);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var GET_KEYWORD_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
keyword
|
|
||||||
FROM
|
|
||||||
module_keyword
|
|
||||||
WHERE
|
|
||||||
name = ?
|
|
||||||
ORDER BY
|
|
||||||
keyword;
|
|
||||||
*/});
|
|
||||||
|
|
||||||
exports.getKeywords = function (name, callback) {
|
|
||||||
mysql.query(GET_KEYWORD_SQL, [name], function (err, rows) {
|
|
||||||
var keywords = [];
|
|
||||||
if (rows && rows.length) {
|
|
||||||
keywords = rows.map(function (r) {
|
|
||||||
return r.keyword;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
callback(err, keywords);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var ADD_KEYWORD_SQL = multiline(function () {;/*
|
|
||||||
INSERT INTO
|
|
||||||
module_keyword(gmt_create, keyword, name, description)
|
|
||||||
VALUES
|
|
||||||
(now(), ?, ?, ?)
|
|
||||||
ON DUPLICATE KEY UPDATE
|
|
||||||
description=VALUES(description);
|
|
||||||
*/});
|
|
||||||
|
|
||||||
exports.addKeywords = function (name, description, keywords, callback) {
|
|
||||||
var sql = '';
|
|
||||||
var values = [];
|
|
||||||
for (var i = 0; i < keywords.length; i++) {
|
|
||||||
sql += ADD_KEYWORD_SQL;
|
|
||||||
values.push(keywords[i]);
|
|
||||||
values.push(name);
|
|
||||||
values.push(description);
|
|
||||||
}
|
|
||||||
mysql.query(sql, values, function (err, results) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
var ids = [];
|
|
||||||
for (var i = 0; i < results.length; i++) {
|
|
||||||
var r = results[i];
|
|
||||||
if (r.insertId) {
|
|
||||||
ids.push(r.insertId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
callback(null, ids);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var UPDATE_DESC_SQL = multiline(function () {;/*
|
|
||||||
UPDATE
|
|
||||||
module
|
|
||||||
SET
|
|
||||||
description=?
|
|
||||||
WHERE
|
|
||||||
id=?;
|
|
||||||
*/});
|
|
||||||
exports.updateDescription = function (id, description, callback) {
|
|
||||||
mysql.query(UPDATE_DESC_SQL, [description, id], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
var UPDATE_DIST_SQL = 'UPDATE module SET ? WHERE id=?';
|
|
||||||
exports.update = function (mod, callback) {
|
|
||||||
var pkg;
|
|
||||||
try {
|
|
||||||
pkg = stringifyPackage(mod.package);
|
|
||||||
} catch (e) {
|
|
||||||
return callback(e);
|
|
||||||
}
|
|
||||||
var dist = mod.package.dist;
|
|
||||||
|
|
||||||
var arg = {
|
|
||||||
publish_time: mod.publish_time,
|
|
||||||
version: mod.version,
|
|
||||||
package: pkg,
|
|
||||||
dist_tarball: dist.tarball,
|
|
||||||
dist_shasum: dist.shasum,
|
|
||||||
dist_size: dist.size
|
|
||||||
};
|
|
||||||
|
|
||||||
mysql.query(UPDATE_DIST_SQL,
|
|
||||||
[arg, mod.id],
|
|
||||||
function (err, result) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
callback(null, {id: mod.id, gmt_modified: new Date()});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
function parseRow(row) {
|
|
||||||
if (row && row.package) {
|
|
||||||
try {
|
|
||||||
if (row.package.indexOf('%7B%22') === 0) {
|
|
||||||
// now store package will encodeURIComponent() after JSON.stringify
|
|
||||||
row.package = decodeURIComponent(row.package);
|
|
||||||
}
|
|
||||||
row.package = JSON.parse(row.package);
|
|
||||||
} catch (e) {
|
|
||||||
console.warn('parse package error: %s, id: %s version: %s, error: %s', row.name, row.id, row.version, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exports.parseRow = parseRow;
|
|
||||||
|
|
||||||
function stringifyPackage(pkg) {
|
|
||||||
return encodeURIComponent(JSON.stringify(pkg));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
var SELECT_MODULE_BY_ID_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
id, publish_time, gmt_create, gmt_modified, author, name,
|
|
||||||
version, description, package, dist_tarball, dist_shasum, dist_size
|
|
||||||
FROM
|
|
||||||
module
|
|
||||||
WHERE
|
|
||||||
id=?;
|
|
||||||
*/});
|
|
||||||
exports.getById = function (id, callback) {
|
|
||||||
id = Number(id);
|
|
||||||
mysql.queryOne(SELECT_MODULE_BY_ID_SQL, [id], function (err, row) {
|
|
||||||
if (err || !row) {
|
|
||||||
return callback(err, row);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
parseRow(row);
|
|
||||||
} catch (e) {
|
|
||||||
e.data = row;
|
|
||||||
return callback(e);
|
|
||||||
}
|
|
||||||
callback(null, row);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var SELECT_MODULE_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
id, publish_time, gmt_create, gmt_modified, author, name,
|
|
||||||
version, description, package, dist_tarball, dist_shasum, dist_size
|
|
||||||
FROM
|
|
||||||
module
|
|
||||||
WHERE
|
|
||||||
name=? AND version=?;
|
|
||||||
*/});
|
|
||||||
exports.get = function (name, version, callback) {
|
|
||||||
mysql.queryOne(SELECT_MODULE_SQL, [name, version], function (err, row) {
|
|
||||||
if (err || !row) {
|
|
||||||
return callback(err, row);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
parseRow(row);
|
|
||||||
} catch (e) {
|
|
||||||
e.data = row;
|
|
||||||
return callback(e);
|
|
||||||
}
|
|
||||||
callback(null, row);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var SELECT_MODULE_ID_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
id
|
|
||||||
FROM
|
|
||||||
module
|
|
||||||
WHERE
|
|
||||||
name=? AND version=?;
|
|
||||||
*/});
|
|
||||||
var INSERT_TAG_SQL = multiline(function () {;/*
|
|
||||||
INSERT INTO
|
|
||||||
tag(gmt_create, gmt_modified, name, tag, version, module_id)
|
|
||||||
VALUES
|
|
||||||
(now(), now(), ?, ?, ?, ?)
|
|
||||||
ON DUPLICATE KEY UPDATE
|
|
||||||
gmt_modified=now(),
|
|
||||||
module_id=VALUES(module_id),
|
|
||||||
name=VALUES(name),
|
|
||||||
tag=VALUES(tag),
|
|
||||||
version=VALUES(version);
|
|
||||||
*/});
|
|
||||||
exports.addTag = function (name, tag, version, callback) {
|
|
||||||
mysql.queryOne(SELECT_MODULE_ID_SQL, [name, version], function (err, row) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
var module_id = row && row.id || 0;
|
|
||||||
mysql.query(INSERT_TAG_SQL, [name, tag, version, module_id], function (err, result) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
callback(null, {id: result.insertId, gmt_modified: new Date(), module_id: module_id});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var SELECT_TAG_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
tag, version, gmt_modified, module_id
|
|
||||||
FROM
|
|
||||||
tag
|
|
||||||
WHERE
|
|
||||||
name=? AND tag=?;
|
|
||||||
*/});
|
|
||||||
exports.getByTag = function (name, tag, callback) {
|
|
||||||
mysql.queryOne(SELECT_TAG_SQL, [name, tag], function (err, row) {
|
|
||||||
if (err || !row) {
|
|
||||||
return callback(err, row);
|
|
||||||
}
|
|
||||||
exports.get(name, row.version, callback);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var DELETE_TAGS_SQL = multiline(function () {;/*
|
|
||||||
DELETE FROM
|
|
||||||
tag
|
|
||||||
WHERE
|
|
||||||
name=?;
|
|
||||||
*/});
|
|
||||||
exports.removeTags = function (name, callback) {
|
|
||||||
mysql.query(DELETE_TAGS_SQL, [name], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
var DELETE_TAGS_BY_IDS_SQL = multiline(function () {;/*
|
|
||||||
DELETE FROM
|
|
||||||
tag
|
|
||||||
WHERE
|
|
||||||
id IN (?);
|
|
||||||
*/});
|
|
||||||
exports.removeTagsByIds = function (ids, callback) {
|
|
||||||
mysql.query(DELETE_TAGS_BY_IDS_SQL, [ids], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
var SELECT_ALL_TAGS_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
id, tag, version, gmt_modified, module_id
|
|
||||||
FROM
|
|
||||||
tag
|
|
||||||
WHERE
|
|
||||||
name=?;
|
|
||||||
*/});
|
|
||||||
exports.listTags = function (name, callback) {
|
|
||||||
mysql.query(SELECT_ALL_TAGS_SQL, [name], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
var SELECT_LATEST_MODULE_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
id, publish_time, gmt_create, gmt_modified, author, name,
|
|
||||||
version, description, package, dist_tarball, dist_shasum, dist_size
|
|
||||||
FROM
|
|
||||||
module
|
|
||||||
WHERE
|
|
||||||
name=? AND version <> "next"
|
|
||||||
ORDER BY
|
|
||||||
publish_time DESC
|
|
||||||
LIMIT
|
|
||||||
1;
|
|
||||||
*/});
|
|
||||||
exports.getLatest = function (name, callback) {
|
|
||||||
exports.getByTag(name, 'latest', function (err, row) {
|
|
||||||
if (err || row) {
|
|
||||||
return callback(err, row);
|
|
||||||
}
|
|
||||||
|
|
||||||
// get latest order by id
|
|
||||||
mysql.queryOne(SELECT_LATEST_MODULE_SQL, [name], function (err, row) {
|
|
||||||
if (err || !row) {
|
|
||||||
return callback(err, row);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
parseRow(row);
|
|
||||||
} catch (e) {
|
|
||||||
e.data = row;
|
|
||||||
return callback(e);
|
|
||||||
}
|
|
||||||
callback(null, row);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var LIST_MODULE_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
id, publish_time, gmt_create, gmt_modified, author, name,
|
|
||||||
version, description, package, dist_tarball, dist_shasum, dist_size
|
|
||||||
FROM
|
|
||||||
module
|
|
||||||
WHERE
|
|
||||||
name=?
|
|
||||||
ORDER BY
|
|
||||||
id DESC;
|
|
||||||
*/});
|
|
||||||
exports.listByName = function (name, callback) {
|
|
||||||
mysql.query(LIST_MODULE_SQL, [name], function (err, rows) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
rows = rows || [];
|
|
||||||
try {
|
|
||||||
for (var i = 0; i < rows.length; i++) {
|
|
||||||
parseRow(rows[i]);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
err = e;
|
|
||||||
err.data = rows;
|
|
||||||
}
|
|
||||||
callback(err, rows);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var LIST_SINCE_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
distinct(name)
|
|
||||||
FROM
|
|
||||||
tag
|
|
||||||
WHERE
|
|
||||||
gmt_modified > ?;
|
|
||||||
*/});
|
|
||||||
exports.listSince = function (start, callback) {
|
|
||||||
mysql.query(LIST_SINCE_SQL, [new Date(start)], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
var LIST_ALL_NAME_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
distinct(name)
|
|
||||||
FROM
|
|
||||||
module;
|
|
||||||
*/});
|
|
||||||
exports.listAllNames = function (callback) {
|
|
||||||
mysql.query(LIST_ALL_NAME_SQL, [], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
var LIST_SHORT_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
distinct(name)
|
|
||||||
FROM
|
|
||||||
tag
|
|
||||||
ORDER BY
|
|
||||||
name;
|
|
||||||
*/});
|
|
||||||
exports.listShort = function (callback) {
|
|
||||||
mysql.query(LIST_SHORT_SQL, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
var LIST_ALL_MODULE_NAMES_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
distinct(name)
|
|
||||||
FROM
|
|
||||||
module
|
|
||||||
ORDER BY
|
|
||||||
name;
|
|
||||||
*/});
|
|
||||||
exports.listAllModuleNames = function (callback) {
|
|
||||||
mysql.query(LIST_ALL_MODULE_NAMES_SQL, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
var DELETE_MODULE_BY_NAME_SQL = multiline(function () {;/*
|
|
||||||
DELETE FROM
|
|
||||||
module
|
|
||||||
WHERE
|
|
||||||
name=?;
|
|
||||||
*/});
|
|
||||||
exports.removeByName = function (name, callback) {
|
|
||||||
mysql.query(DELETE_MODULE_BY_NAME_SQL, [name], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
var DELETE_MODULE_BY_NAME_AND_VERSIONS_SQL = multiline(function () {;/*
|
|
||||||
DELETE FROM
|
|
||||||
module
|
|
||||||
WHERE
|
|
||||||
name=? AND version in(?);
|
|
||||||
*/});
|
|
||||||
exports.removeByNameAndVersions = function (name, versions, callback) {
|
|
||||||
mysql.query(DELETE_MODULE_BY_NAME_AND_VERSIONS_SQL, [name, versions], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
var SEARCH_MODULES_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
module_id
|
|
||||||
FROM
|
|
||||||
tag
|
|
||||||
WHERE
|
|
||||||
name LIKE ? AND tag="latest"
|
|
||||||
ORDER BY
|
|
||||||
name
|
|
||||||
LIMIT
|
|
||||||
?;
|
|
||||||
*/});
|
|
||||||
var SEARCH_MODULES_BY_KEYWORD_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
name, description
|
|
||||||
FROM
|
|
||||||
module_keyword
|
|
||||||
WHERE
|
|
||||||
keyword=?
|
|
||||||
ORDER BY
|
|
||||||
id DESC
|
|
||||||
LIMIT
|
|
||||||
?;
|
|
||||||
*/});
|
|
||||||
var QUERY_MODULES_BY_ID_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
name, description
|
|
||||||
FROM
|
|
||||||
module
|
|
||||||
WHERE
|
|
||||||
id IN (?)
|
|
||||||
ORDER BY
|
|
||||||
name;
|
|
||||||
*/});
|
|
||||||
exports.search = function (word, options, callback) {
|
|
||||||
if (typeof options === 'function') {
|
|
||||||
callback = options;
|
|
||||||
options = null;
|
|
||||||
}
|
|
||||||
options = options || {};
|
|
||||||
var limit = options.limit || 100;
|
|
||||||
word = word.replace(/^%/, ''); //ignore prefix %
|
|
||||||
var ep = eventproxy.create();
|
|
||||||
ep.fail(callback);
|
|
||||||
|
|
||||||
// search flows:
|
|
||||||
// 1. prefix search by name
|
|
||||||
// 2. like search by name
|
|
||||||
// 3. keyword equal search
|
|
||||||
var ids = {};
|
|
||||||
|
|
||||||
mysql.query(SEARCH_MODULES_SQL, [word + '%', limit ], ep.done(function (rows) {
|
|
||||||
rows = rows || [];
|
|
||||||
if (rows.length > 0) {
|
|
||||||
for (var i = 0; i < rows.length; i++) {
|
|
||||||
ids[rows[i].module_id] = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (rows.length >= 20) {
|
|
||||||
return ep.emit('ids', Object.keys(ids));
|
|
||||||
}
|
|
||||||
|
|
||||||
mysql.query(SEARCH_MODULES_SQL, [ '%' + word + '%', limit ], ep.done('likeSearch'));
|
|
||||||
}));
|
|
||||||
|
|
||||||
mysql.query(SEARCH_MODULES_BY_KEYWORD_SQL, [ word, limit ], ep.done('keywordRows'));
|
|
||||||
|
|
||||||
ep.on('likeSearch', function (rows) {
|
|
||||||
rows = rows || [];
|
|
||||||
if (rows.length > 0) {
|
|
||||||
for (var i = 0; i < rows.length; i++) {
|
|
||||||
ids[rows[i].module_id] = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ep.emit('ids', Object.keys(ids));
|
|
||||||
});
|
|
||||||
|
|
||||||
ep.all('ids', 'keywordRows', function (ids, keywordRows) {
|
|
||||||
keywordRows = keywordRows || [];
|
|
||||||
var data = {
|
|
||||||
keywordMatchs: keywordRows,
|
|
||||||
searchMatchs: []
|
|
||||||
};
|
|
||||||
if (ids.length === 0) {
|
|
||||||
return callback(null, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
mysql.query(QUERY_MODULES_BY_ID_SQL, [ids], ep.done(function (modules) {
|
|
||||||
data.searchMatchs = modules;
|
|
||||||
callback(null, data);
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
thunkify(exports);
|
|
||||||
|
|
||||||
var GET_LAST_MODIFIED_MODULE_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
id, gmt_modified
|
|
||||||
FROM
|
|
||||||
module
|
|
||||||
WHERE
|
|
||||||
name=?
|
|
||||||
ORDER BY
|
|
||||||
gmt_modified DESC
|
|
||||||
LIMIT 1;
|
|
||||||
*/});
|
|
||||||
exports.getLastModified = function* (name) {
|
|
||||||
var row = yield mysql.queryOne(GET_LAST_MODIFIED_MODULE_SQL, [name]);
|
|
||||||
return row && row.gmt_modified;
|
|
||||||
};
|
|
||||||
|
|
||||||
var UPDATE_LAST_MODIFIED_SQL = 'UPDATE module SET gmt_modified=now() WHERE id=?;';
|
|
||||||
exports.updateLastModified = function* (name) {
|
|
||||||
var row = yield mysql.queryOne(GET_LAST_MODIFIED_MODULE_SQL, [name]);
|
|
||||||
if (row) {
|
|
||||||
yield mysql.query(UPDATE_LAST_MODIFIED_SQL, [row.id]);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var DELETE_TAGS_BY_NAMES_SQL = 'DELETE FROM tag WHERE name=? AND tag IN (?);';
|
|
||||||
exports.removeTagsByNames = function* (moduleName, tagNames) {
|
|
||||||
return yield mysql.query(DELETE_TAGS_BY_NAMES_SQL, [moduleName, tagNames]);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* forward compatbility for update from lower version cnpmjs.org
|
|
||||||
* redirect @scope/name => name
|
|
||||||
*/
|
|
||||||
exports.getAdaptName = function* (name) {
|
|
||||||
if (!config.scopes
|
|
||||||
|| !config.scopes.length
|
|
||||||
|| !config.adaptScope) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var tmp = name.split('/');
|
|
||||||
var scope = tmp[0];
|
|
||||||
name = tmp[1];
|
|
||||||
|
|
||||||
if (config.scopes.indexOf(scope) === -1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var pkg = yield exports.getByTag(name, 'latest');
|
|
||||||
// only private module can adapt
|
|
||||||
if (pkg && pkg.package._publish_on_cnpm) {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.listPrivates = function* () {
|
|
||||||
var scopes = config.scopes;
|
|
||||||
if (!scopes || !scopes.length) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
var privatePackages = config.privatePackages || [];
|
|
||||||
|
|
||||||
var args = [];
|
|
||||||
var sql = 'SELECT module_id AS id FROM tag WHERE tag="latest" AND (';
|
|
||||||
var wheres = [];
|
|
||||||
|
|
||||||
scopes.forEach(function (scope) {
|
|
||||||
wheres.push('name LIKE ?');
|
|
||||||
args.push(scope + '%');
|
|
||||||
});
|
|
||||||
|
|
||||||
if (privatePackages.length) {
|
|
||||||
wheres.push('name in (?)');
|
|
||||||
args.push(privatePackages);
|
|
||||||
}
|
|
||||||
|
|
||||||
sql = sql + wheres.join(' OR ') + ')';
|
|
||||||
|
|
||||||
var ids = yield mysql.query(sql, args);
|
|
||||||
ids = ids.map(function (row) {
|
|
||||||
return row.id;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!ids.length) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
return yield mysql.query(QUERY_MODULES_BY_ID_SQL, [ids]);
|
|
||||||
};
|
|
||||||
|
|
||||||
var LIST_BY_AUTH_SQLS = [];
|
|
||||||
LIST_BY_AUTH_SQLS.push(multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
distinct(name) AS name
|
|
||||||
FROM
|
|
||||||
module
|
|
||||||
WHERE
|
|
||||||
author=?
|
|
||||||
ORDER BY
|
|
||||||
publish_time DESC
|
|
||||||
LIMIT
|
|
||||||
100;
|
|
||||||
*/}));
|
|
||||||
LIST_BY_AUTH_SQLS.push(multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
name
|
|
||||||
FROM
|
|
||||||
module_maintainer
|
|
||||||
WHERE
|
|
||||||
user = ?
|
|
||||||
*/}));
|
|
||||||
LIST_BY_AUTH_SQLS.push(multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
module_id
|
|
||||||
FROM
|
|
||||||
tag
|
|
||||||
WHERE
|
|
||||||
tag="latest" AND name IN (?);
|
|
||||||
*/}));
|
|
||||||
LIST_BY_AUTH_SQLS.push(multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
name, description
|
|
||||||
FROM
|
|
||||||
module
|
|
||||||
WHERE
|
|
||||||
id IN (?)
|
|
||||||
ORDER BY
|
|
||||||
publish_time DESC;
|
|
||||||
*/}));
|
|
||||||
exports.listByAuthor = function* (author, callback) {
|
|
||||||
var names = yield [
|
|
||||||
mysql.query(LIST_BY_AUTH_SQLS[0], [author]),
|
|
||||||
mysql.query(LIST_BY_AUTH_SQLS[1], [author])
|
|
||||||
];
|
|
||||||
|
|
||||||
names = names[0].concat(names[1]).map(function (n) {
|
|
||||||
return n.name;
|
|
||||||
}).sort();
|
|
||||||
|
|
||||||
if (!names.length) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
var ids = yield mysql.query(LIST_BY_AUTH_SQLS[2], [names]);
|
|
||||||
if (!ids.length) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
ids = ids.map(function (i) {
|
|
||||||
return i.module_id;
|
|
||||||
});
|
|
||||||
return yield mysql.query(LIST_BY_AUTH_SQLS[3], [ids]);
|
|
||||||
};
|
|
||||||
|
|
||||||
var UPDATE_PACKAGE_SQL = multiline(function () {;/*
|
|
||||||
UPDATE
|
|
||||||
module
|
|
||||||
SET
|
|
||||||
package=?
|
|
||||||
WHERE
|
|
||||||
id=?;
|
|
||||||
*/});
|
|
||||||
|
|
||||||
exports.updatePackage = function* (id, pkg) {
|
|
||||||
pkg = stringifyPackage(pkg);
|
|
||||||
return yield mysql.query(UPDATE_PACKAGE_SQL, [pkg, id]);
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.updatePackageFields = function* (id, fields) {
|
|
||||||
var data = yield exports.getById(id);
|
|
||||||
if (!data) {
|
|
||||||
throw new Error('module#' + id + ' not exists');
|
|
||||||
}
|
|
||||||
data.package = data.package || {};
|
|
||||||
for (var k in fields) {
|
|
||||||
data.package[k] = fields[k];
|
|
||||||
}
|
|
||||||
return yield* exports.updatePackage(id, data.package);
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.updateReadme = function* (id, readme) {
|
|
||||||
var data = yield exports.getById(id);
|
|
||||||
if (!data) {
|
|
||||||
throw new Error('module#' + id + ' not exists');
|
|
||||||
}
|
|
||||||
data.package = data.package || {};
|
|
||||||
data.package.readme = readme;
|
|
||||||
return yield* exports.updatePackage(id, data.package);
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.getTag = function* (name, tag) {
|
|
||||||
return yield mysql.queryOne(SELECT_TAG_SQL, [name, tag]);
|
|
||||||
};
|
|
||||||
@@ -1,59 +0,0 @@
|
|||||||
/**!
|
|
||||||
* cnpmjs.org - proxy/module_deps.js
|
|
||||||
*
|
|
||||||
* Copyright(c) 2014
|
|
||||||
* MIT Licensed
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
|
||||||
*/
|
|
||||||
|
|
||||||
"use strict";
|
|
||||||
/* jshint -W032 */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var thunkify = require('thunkify-wrap');
|
|
||||||
var mysql = require('../common/mysql');
|
|
||||||
var multiline = require('multiline');
|
|
||||||
|
|
||||||
var LIST_DEPS_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
deps
|
|
||||||
FROM
|
|
||||||
module_deps
|
|
||||||
WHERE
|
|
||||||
name=?;
|
|
||||||
*/});
|
|
||||||
exports.list = function (name, callback) {
|
|
||||||
mysql.query(LIST_DEPS_SQL, [name], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
var INSERT_DEPS_SQL = multiline(function () {;/*
|
|
||||||
INSERT INTO
|
|
||||||
module_deps(gmt_create, name, deps)
|
|
||||||
VALUES
|
|
||||||
(now(), ?, ?);
|
|
||||||
*/});
|
|
||||||
exports.add = function (name, deps, callback) {
|
|
||||||
mysql.query(INSERT_DEPS_SQL, [name, deps], function (err, result) {
|
|
||||||
if (err && err.code === 'ER_DUP_ENTRY') {
|
|
||||||
err = null;
|
|
||||||
}
|
|
||||||
callback(err);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var DELETE_DEPS_SQL = multiline(function () {;/*
|
|
||||||
DELETE FROM
|
|
||||||
module_deps
|
|
||||||
WHERE
|
|
||||||
name=? AND deps=?;
|
|
||||||
*/});
|
|
||||||
exports.remove = function (name, deps, callback) {
|
|
||||||
mysql.query(DELETE_DEPS_SQL, [name, deps], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
thunkify(exports);
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
/**!
|
|
||||||
* cnpmjs.org - proxy/module_log.js
|
|
||||||
*
|
|
||||||
* Copyright(c) cnpmjs.org and other contributors.
|
|
||||||
* MIT Licensed
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
/* jshint -W032 */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var thunkify = require('thunkify-wrap');
|
|
||||||
var mysql = require('../common/mysql');
|
|
||||||
|
|
||||||
var INSERT_LOG_SQL = 'INSERT INTO module_log SET ?';
|
|
||||||
exports.create = function (data, callback) {
|
|
||||||
var now = new Date();
|
|
||||||
var args = {
|
|
||||||
gmt_create: now,
|
|
||||||
gmt_modified: now,
|
|
||||||
name: data.name,
|
|
||||||
username: data.username,
|
|
||||||
log: ''
|
|
||||||
};
|
|
||||||
mysql.query(INSERT_LOG_SQL, [args], function (err, result) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
callback(null, {id: result.insertId, gmt_modified: new Date()});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var APPEND_SQL = 'UPDATE module_log SET log=CONCAT(log, ?), gmt_modified=now() WHERE id=?';
|
|
||||||
exports.append = function (id, log, callback) {
|
|
||||||
log = '\n' + log;
|
|
||||||
mysql.query(APPEND_SQL, [log, id], function (err) {
|
|
||||||
callback(null, {id: id, gmt_modified: new Date()});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var SELECT_SQL = 'SELECT * FROM module_log WHERE id=?';
|
|
||||||
exports.get = function (id, callback) {
|
|
||||||
mysql.queryOne(SELECT_SQL, [id], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
thunkify(exports);
|
|
||||||
@@ -1,92 +0,0 @@
|
|||||||
/**!
|
|
||||||
* cnpmjs.org - proxy/module_maintainer.js
|
|
||||||
*
|
|
||||||
* Copyright(c) fengmk2 and other contributors.
|
|
||||||
* MIT Licensed
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var debug = require('debug')('cnpmjs.org:proxy:module_maintainer');
|
|
||||||
var mysql = require('../common/mysql');
|
|
||||||
var Module = require('./module');
|
|
||||||
|
|
||||||
var GET_MAINTANINERS_SQL = 'SELECT user FROM module_maintainer WHERE name = ?;';
|
|
||||||
|
|
||||||
exports.get = function* (name) {
|
|
||||||
var users = yield mysql.query(GET_MAINTANINERS_SQL, [name]);
|
|
||||||
return users.map(function (row) {
|
|
||||||
return row.user;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var ADD_SQL = 'INSERT INTO module_maintainer(name, user, gmt_create) \
|
|
||||||
VALUES (?, ?, now());';
|
|
||||||
function* add(name, username) {
|
|
||||||
try {
|
|
||||||
yield mysql.query(ADD_SQL, [name, username]);
|
|
||||||
} catch (err) {
|
|
||||||
if (err.code !== 'ER_DUP_ENTRY') {
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var REMOVE_SQL = 'DELETE FROM module_maintainer WHERE name = ? AND user IN (?);';
|
|
||||||
function* remove(name, usernames) {
|
|
||||||
return yield mysql.query(REMOVE_SQL, [name, usernames]);
|
|
||||||
}
|
|
||||||
|
|
||||||
var REMOVE_ALL_SQL = 'DELETE FROM module_maintainer WHERE name = ?';
|
|
||||||
|
|
||||||
exports.removeAll = function* (name) {
|
|
||||||
return yield mysql.query(REMOVE_ALL_SQL, [name]);
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.addMulti = function* (name, usernames) {
|
|
||||||
var tasks = [];
|
|
||||||
for (var i = 0; i < usernames.length; i++) {
|
|
||||||
tasks.push(add(name, usernames[i]));
|
|
||||||
}
|
|
||||||
return yield tasks;
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.update = function* (name, maintainers) {
|
|
||||||
// maintainers should be [name1, name2, ...] format
|
|
||||||
// find out the exists maintainers then remove the deletes and add the left
|
|
||||||
if (maintainers.length === 0) {
|
|
||||||
return {
|
|
||||||
add: [],
|
|
||||||
remove: []
|
|
||||||
};
|
|
||||||
}
|
|
||||||
var exists = yield* exports.get(name);
|
|
||||||
var addUsers = maintainers;
|
|
||||||
var removeUsers = [];
|
|
||||||
if (exists.length > 0) {
|
|
||||||
for (var i = 0; i < exists.length; i++) {
|
|
||||||
var username = exists[i];
|
|
||||||
if (addUsers.indexOf(username) === -1) {
|
|
||||||
removeUsers.push(username);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
yield* exports.addMulti(name, addUsers);
|
|
||||||
// make sure all add users success then remove users
|
|
||||||
if (removeUsers.length > 0) {
|
|
||||||
yield* remove(name, removeUsers);
|
|
||||||
}
|
|
||||||
debug('add %d users, remove %d users', addUsers.length, removeUsers.length);
|
|
||||||
return {
|
|
||||||
add: addUsers,
|
|
||||||
remove: removeUsers
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
/**!
|
|
||||||
* cnpmjs.org - proxy/module_star.js
|
|
||||||
*
|
|
||||||
* Copyright(c) cnpmjs.org and other contributors.
|
|
||||||
* MIT Licensed
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
/* jshint -W032 */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var mysql = require('../common/mysql');
|
|
||||||
var multiline = require('multiline');
|
|
||||||
|
|
||||||
var ADD_SQL = multiline(function () {;/*
|
|
||||||
INSERT INTO
|
|
||||||
module_star(name, user, gmt_create)
|
|
||||||
VALUES
|
|
||||||
(?, ?, now());
|
|
||||||
*/});
|
|
||||||
exports.add = function *add(name, user) {
|
|
||||||
try {
|
|
||||||
yield mysql.query(ADD_SQL, [name, user]);
|
|
||||||
} catch (err) {
|
|
||||||
if (err.code !== 'ER_DUP_ENTRY') {
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var REMOVE_SQL = multiline(function () {;/*
|
|
||||||
DELETE FROM
|
|
||||||
module_star
|
|
||||||
WHERE
|
|
||||||
name = ? AND user = ?;
|
|
||||||
*/});
|
|
||||||
exports.remove = function *(name, user) {
|
|
||||||
return yield mysql.query(REMOVE_SQL, [name, user]);
|
|
||||||
};
|
|
||||||
|
|
||||||
var LIST_USERS_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
user
|
|
||||||
FROM
|
|
||||||
module_star
|
|
||||||
WHERE
|
|
||||||
name = ?;
|
|
||||||
*/});
|
|
||||||
exports.listUsers = function *(name) {
|
|
||||||
var rows = yield mysql.query(LIST_USERS_SQL, [name]);
|
|
||||||
return rows.map(function (r) {
|
|
||||||
return r.user;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var LIST_USER_MODULES_SQL = multiline(function () {;/*
|
|
||||||
SELECT
|
|
||||||
name
|
|
||||||
FROM
|
|
||||||
module_star
|
|
||||||
WHERE
|
|
||||||
user = ?;
|
|
||||||
*/});
|
|
||||||
exports.listUserModules = function *(user) {
|
|
||||||
return (yield mysql.query(LIST_USER_MODULES_SQL, [user])).map(function (r) {
|
|
||||||
return r.name;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user