This repository has been archived by the owner on Jun 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 745
/
Copy pathindex.js
398 lines (331 loc) · 11.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
'use strict';
var mkdirp = require('mkdirp');
var copy = require('copy-to');
var path = require('path');
var fs = require('fs');
var os = require('os');
var utility = require('utility');
var version = require('../package.json').version;
var Nfs = require('fs-cnpm');
var root = path.dirname(__dirname);
var dataDir = path.join(process.env.HOME || root, '.cnpmjs.org');
var config = {
version: version,
dataDir: dataDir,
/**
* Cluster mode
*/
enableCluster: false,
numCPUs: os.cpus().length,
/*
* server configure
*/
registryPort: 7001,
webPort: 7002,
bindingHost: '127.0.0.1', // only binding on 127.0.0.1 for local access
// default is ctx.protocol
protocol: '',
// When sync package, cnpm not know the access protocol.
// So should set manually
backupProtocol: 'http',
// debug mode
// if in debug mode, some middleware like limit wont load
// logger module will print to stdout
debug: process.env.NODE_ENV === 'development',
// page mode, enable on development env
pagemock: process.env.NODE_ENV === 'development',
// session secret
sessionSecret: 'cnpmjs.org test session secret',
// max request json body size
jsonLimit: '10mb',
// log dir name
logdir: path.join(dataDir, 'logs'),
// update file template dir
uploadDir: path.join(dataDir, 'downloads'),
// web page viewCache
viewCache: false,
// registry http response cache control header
// if you are using CDN, can set it to 'max-age=0, s-maxage=10, must-revalidate'
// it meaning cache 10s on CDN server and no cache on client side.
registryCacheControlHeader: '',
// if you are using CDN, can set it to 'Accept, Accept-Encoding'
registryVaryHeader: '',
// disable package search
disableSearch: false,
// view files directory
viewDir: path.join(root, 'view', 'web'),
customRegistryMiddlewares: [],
customWebMiddlewares: [],
// config for koa-limit middleware
// for limit download rates
limit: {
enable: false,
token: 'koa-limit:download',
limit: 1000,
interval: 1000 * 60 * 60 * 24,
whiteList: [],
blackList: [],
message: 'request frequency limited, any question, please contact fengmk2@gmail.com',
},
enableCompress: false, // enable gzip response or not
// default system admins
admins: {
// name: email
fengmk2: 'fengmk2@gmail.com',
admin: 'admin@cnpmjs.org',
dead_horse: 'dead_horse@qq.com',
},
// email notification for errors
// check https://github.com/andris9/Nodemailer for more informations
mail: {
enable: false,
appname: 'cnpmjs.org',
from: 'cnpmjs.org mail sender <adderss@gmail.com>',
service: 'gmail',
auth: {
user: 'address@gmail.com',
pass: 'your password'
}
},
logoURL: 'https://os.alipayobjects.com/rmsportal/oygxuIUkkrRccUz.jpg', // cnpm logo image url
adBanner: '',
customHeader: '',
customReadmeFile: '', // you can use your custom readme file instead the cnpm one
customFooter: '', // you can add copyright and site total script html here
npmClientName: 'cnpm', // use `${name} install package`
// max handle number of package.json `dependencies` property
maxDependencies: 200,
/**
* 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
},
dialectOptions: {
// if your server run on full cpu load, please set trace to false
trace: true,
},
// the storage engine for 'sqlite'
// default store into ~/.cnpmjs.org/data.sqlite
storage: path.join(dataDir, 'data.sqlite'),
logging: !!process.env.SQL_DEBUG,
},
// return total modules and versions, default is true
// it will use `SELECT count(DISTINCT name) FROM module` SQL on Database
enableTotalCount: true,
// enable proxy npm audits request or not
enableNpmAuditsProxy: true,
// package tarball store in local filesystem by default
nfs: new Nfs({
dir: path.join(dataDir, 'nfs')
}),
// if set true, will 302 redirect to `nfs.url(dist.key)`
downloadRedirectToNFS: false,
// don't check database and just download tgz from nfs
downloadTgzDontCheckModule: false,
// remove original tarball when publishing
unpublishRemoveTarball: true,
// registry url name
registryHost: 'r.cnpmjs.org',
/**
* registry mode config
*/
// enable private mode or not
// private mode: only admins can publish, other users just can sync package from source npm
// public mode: all users can publish
enablePrivate: false,
// registry scopes, if don't set, means do not support scopes
scopes: [ '@cnpm', '@cnpmtest', '@cnpm-test' ],
// some registry already have some private packages in global scope
// but we want to treat them as scoped private packages,
// so you can use this white list.
privatePackages: [],
/**
* sync configs
*/
// the official npm registry
// cnpm wont directly sync from this one
// but sometimes will request it for some package infomations
// please don't change it if not necessary
officialNpmRegistry: 'https://registry.npmjs.com',
officialNpmReplicate: 'https://replicate.npmjs.com',
cnpmRegistry: 'https://r.cnpmjs.com',
// /-/all/changes
// since different changes are aggregated through many tables
// prevent changesStream changes collisions
changesDelay: 5000,
// sync source, upstream registry
// If you want to directly sync from official npm's registry
// please drop them an email first
sourceNpmRegistry: 'https://registry.npmmirror.com',
sourceNpmWeb: 'https://npmmirror.com',
// set remote registry to show web page data
enableWebDataRemoteRegistry: false,
webDataRemoteRegistry: '',
// upstream registry is base on cnpm/cnpmjs.org or not
// if your upstream is official npm registry, please turn it off
sourceNpmRegistryIsCNpm: true,
// if install return 404, try to sync from source registry
syncByInstall: true,
// sync mode select
// none: do not sync any module, proxy all public modules from sourceNpmRegistry
// exist: only sync exist modules
// all: sync all modules
syncModel: 'none', // 'none', 'all', 'exist'
// sync package.json/dist-tag.json to sync dir
syncBackupFiles: false,
syncConcurrency: 1,
// sync interval, default is 10 minutes
syncInterval: '10m',
// sync polular modules, default to false
// because cnpm can't auto sync tag change for now
// so we want to sync popular modules to ensure their tags
syncPopular: false,
syncPopularInterval: '1h',
// top 100
topPopular: 100,
// sync devDependencies or not, default is false
syncDevDependencies: false,
// try to remove all deleted versions from original registry
syncDeletedVersions: true,
// changes streaming sync
syncChangesStream: false,
syncDownloadOptions: {
// formatRedirectUrl: function (url, location)
},
// all syncModel cannot sync scope pacakge, you can use this model to sync scope package from any resgitry
syncScope: false,
syncScopeInterval: '12h',
// scope package sync config
/**
* sync scope package from assign registry
* @param {Array<scope>} scopes
* @param {String} scope.scope scope name
* @param {String} scope.sourceCnpmWeb source cnpm registry web url for get scope all packages name
* @param {String} scope.sourceCnpmRegistry source cnpm registry url for sync packages
*/
syncScopeConfig: [],
handleSyncRegistry: 'http://127.0.0.1:7001',
// default badge subject
badgeSubject: 'cnpm',
// defautl use https://badgen.net/
badgeService: {
url: function(subject, status, options) {
options = options || {};
let url = `https://badgen.net/badge/${utility.encodeURIComponent(subject)}/${utility.encodeURIComponent(status)}`;
if (options.color) {
url += `/${utility.encodeURIComponent(options.color)}`;
}
if (options.icon) {
url += `?icon=${utility.encodeURIComponent(options.icon)}`;
}
return url;
},
},
packagephobiaURL: 'https://packagephobia.now.sh',
packagephobiaSupportPrivatePackage: false,
packagephobiaMinDownloadCount: 1000,
// custom user service, @see https://github.com/cnpm/cnpmjs.org/wiki/Use-Your-Own-User-Authorization
// when you not intend to ingegrate with your company's user system, then use null, it would
// use the default cnpm user system
userService: null,
// always-auth https://docs.npmjs.com/misc/config#always-auth
// Force npm to always require authentication when accessing the registry, even for GET requests.
alwaysAuth: false,
// if you're behind firewall, need to request through http proxy, please set this
// e.g.: `httpProxy: 'http://proxy.mycompany.com:8080'`
httpProxy: null,
// snyk.io root url
snykUrl: 'https://snyk.io',
// https://github.com/cnpm/cnpmjs.org/issues/1149
// if enable this option, must create module_abbreviated and package_readme table in database
enableAbbreviatedMetadata: false,
// enable package or package version block list, must create package_version_blocklist table in database
enableBlockPackageVersion: false,
// enable bug version hotfix by https://github.com/cnpm/bug-versions
enableBugVersion: false,
// global hook function: function* (envelope) {}
// envelope format please see https://github.com/npm/registry/blob/master/docs/hooks/hooks-payload.md#payload
globalHook: null,
opensearch: {
host: '',
},
// redis cache
redisCache: {
enable: false,
connectOptions: null,
},
// custom format full package list
// change `GET /:name` request response body
// use on `controllers/registry/list.js`
formatCustomFullPackageInfoAndVersions: (ctx, packageInfo) => {
return packageInfo;
},
// custom format one package version
// change `GET /:name/:version` request response body
// use on `controllers/registry/show.js`
formatCustomOnePackageVersion: (ctx, packageVersion) => {
return packageVersion;
},
// registry download accelerate map
accelerateHostMap: {},
};
if (process.env.NODE_ENV === 'test') {
config.enableAbbreviatedMetadata = true;
config.customRegistryMiddlewares.push((app) => {
return function* (next) {
this.set('x-custom-middleware', 'true');
this.set('x-custom-app-models', typeof app.models.query === 'function' ? 'true' : 'false');
yield next;
};
});
config.customWebMiddlewares.push((app) => {
return function* (next) {
this.set('x-custom-web-middleware', 'true');
this.set('x-custom-web-app-models', typeof app.models.query === 'function' ? 'true' : 'false');
yield next;
};
});
config.enableBlockPackageVersion = true;
config.enableBugVersion = true;
}
if (process.env.NODE_ENV !== 'test') {
var customConfig;
if (process.env.NODE_ENV === 'development') {
customConfig = path.join(root, 'config', 'config.js');
} else {
// 1. try to load `$dataDir/config.json` first, not exists then goto 2.
// 2. load config/config.js, everything in config.js will cover the same key in index.js
customConfig = path.join(dataDir, 'config.json');
if (!fs.existsSync(customConfig)) {
customConfig = path.join(root, 'config', 'config.js');
}
}
if (fs.existsSync(customConfig)) {
copy(require(customConfig)).override(config);
}
}
mkdirp.sync(config.logdir);
mkdirp.sync(config.uploadDir);
module.exports = config;
config.loadConfig = function (customConfig) {
if (!customConfig) {
return;
}
copy(customConfig).override(config);
};