-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathconfig.default.ts
More file actions
284 lines (263 loc) · 8.43 KB
/
config.default.ts
File metadata and controls
284 lines (263 loc) · 8.43 KB
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
import assert from 'node:assert/strict';
import { randomUUID } from 'node:crypto';
import { join } from 'node:path';
import type { Context, EggAppConfig, PartialEggConfig } from 'egg';
import OSSClient from 'oss-cnpm';
import S3Client from 's3-cnpmcore';
import { env } from 'read-env-value';
import { patchAjv } from '../app/port/typebox.ts';
import {
ChangesStreamMode,
NOT_IMPLEMENTED_PATH,
SyncDeleteMode,
SyncMode,
} from '../app/common/constants.ts';
import type { CnpmcoreConfig } from '../app/port/config.ts';
import { database } from './database.ts';
export const cnpmcoreConfig: CnpmcoreConfig = {
name: 'cnpm',
hookEnable: false,
hooksLimit: 20,
sourceRegistry: env(
'CNPMCORE_CONFIG_SOURCE_REGISTRY',
'string',
'https://registry.npmjs.org'
),
sourceRegistryIsCNpm: env(
'CNPMCORE_CONFIG_SOURCE_REGISTRY_IS_CNPM',
'boolean',
false
),
syncUpstreamFirst: false,
sourceRegistrySyncTimeout: 180_000,
taskQueueHighWaterSize: 100,
syncMode: SyncMode.none,
syncDeleteMode: SyncDeleteMode.delete,
syncPackageWorkerMaxConcurrentTasks: 10,
triggerHookWorkerMaxConcurrentTasks: 10,
createTriggerHookWorkerMaxConcurrentTasks: 10,
syncPackageBlockList: [],
enableCheckRecentlyUpdated: true,
enableSyncBinary: false,
syncBinaryFromAPISource: '',
enableSyncDownloadData: false,
syncDownloadDataSourceRegistry: '',
syncDownloadDataMaxDate: '',
enableChangesStream: false,
checkChangesStreamInterval: 500,
changesStreamRegistry: 'https://replicate.npmjs.com/registry',
changesStreamRegistryMode: ChangesStreamMode.streaming,
registry: env('CNPMCORE_CONFIG_REGISTRY', 'string', 'http://localhost:7001'),
alwaysAuth: false,
allowScopes: ['@cnpm', '@cnpmcore', '@example'],
allowPublishNonScopePackage: false,
allowPublicRegistration: false,
admins: {
cnpmcore_admin: 'admin@cnpmjs.org',
},
enableWebAuthn: env('CNPMCORE_CONFIG_ENABLE_WEB_AUTHN', 'boolean', false),
enableCDN: false,
cdnCacheControlHeader: 'public, max-age=300',
cdnVaryHeader: 'Accept, Accept-Encoding',
enableStoreFullPackageVersionManifestsToDatabase: false,
enableNpmClientAndVersionCheck: true,
syncNotFound: false,
redirectNotFound: true,
enableUnpkg: true,
enableSyncUnpkgFiles: true,
enableSyncUnpkgFilesWhiteList: false,
strictSyncSpecivicVersion: false,
enableElasticsearch: env('CNPMCORE_CONFIG_ENABLE_ES', 'boolean', false),
elasticsearchIndex: 'cnpmcore_packages',
strictValidateTarballPkg: false,
strictValidatePackageDeps: false,
searchFilterDeprecated: env(
'CNPMCORE_CONFIG_SEARCH_FILTER_DEPRECATED',
'boolean',
true
),
searchMinPublishTime: env(
'CNPMCORE_CONFIG_SEARCH_MIN_PUBLISH_TIME',
'string',
''
),
database: {
type: database.type,
},
};
export interface NFSConfig {
client: unknown;
dir: string;
removeBeforeUpload: boolean;
}
export type Config = PartialEggConfig & { nfs: NFSConfig };
export default function startConfig(appInfo: EggAppConfig): Config {
const config = {} as Config;
config.keys = env('CNPMCORE_EGG_KEYS', 'string', randomUUID());
config.cnpmcore = cnpmcoreConfig;
// override config from framework / plugin
config.dataDir = env(
'CNPMCORE_DATA_DIR',
'string',
join(appInfo.root, '.cnpmcore')
);
config.orm = {
...database,
database: database.name ?? 'cnpmcore',
charset: 'utf8mb4',
logger: {
// https://github.com/cyjake/leoric/blob/master/docs/zh/logging.md#logqueryerror
// ignore query error
logQueryError() {
// do nothing
},
// logQueryError(...args: any[]) {
// console.log(args);
// },
},
};
config.redis = {
client: {
port: env('CNPMCORE_REDIS_PORT', 'number', 6379),
host: env('CNPMCORE_REDIS_HOST', 'string', '127.0.0.1'),
password: env('CNPMCORE_REDIS_PASSWORD', 'string', ''),
db: env('CNPMCORE_REDIS_DB', 'number', 0),
},
};
config.security = {
csrf: {
enable: false,
},
};
config.cors = {
// allow all domains
origin: (ctx: Context): string => {
return ctx.get('Origin');
},
credentials: true,
// https://github.com/koajs/cors/blob/master/index.js#L10C57-L10C64
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS',
};
config.nfs = {
client: null,
dir: env('CNPMCORE_NFS_DIR', 'string', join(config.dataDir, 'nfs')),
removeBeforeUpload: env(
'CNPMCORE_NFS_REMOVE_BEFORE_UPLOAD',
'boolean',
false
),
};
/* c8 ignore next 17 */
// enable oss nfs store by env values
const nfsType = env('CNPMCORE_NFS_TYPE', 'string', '');
if (nfsType === 'oss') {
const ossConfig = {
cdnBaseUrl: env('CNPMCORE_NFS_OSS_CDN', 'string', ''),
endpoint: env('CNPMCORE_NFS_OSS_ENDPOINT', 'string', ''),
bucket: env('CNPMCORE_NFS_OSS_BUCKET', 'string', ''),
accessKeyId: env('CNPMCORE_NFS_OSS_ID', 'string', ''),
accessKeySecret: env('CNPMCORE_NFS_OSS_SECRET', 'string', ''),
defaultHeaders: {
'Cache-Control': 'max-age=0, s-maxage=60',
},
};
assert.ok(ossConfig.bucket, 'require env CNPMCORE_NFS_OSS_BUCKET');
assert.ok(ossConfig.endpoint, 'require env CNPMCORE_NFS_OSS_ENDPOINT');
assert.ok(ossConfig.accessKeyId, 'require env CNPMCORE_NFS_OSS_ID');
assert.ok(ossConfig.accessKeySecret, 'require env CNPMCORE_NFS_OSS_SECRET');
config.nfs.client = new OSSClient(ossConfig);
} else if (nfsType === 's3') {
const s3Config = {
region: env('CNPMCORE_NFS_S3_CLIENT_REGION', 'string', 'default'),
endpoint: env('CNPMCORE_NFS_S3_CLIENT_ENDPOINT', 'string', ''),
credentials: {
accessKeyId: env('CNPMCORE_NFS_S3_CLIENT_ID', 'string', ''),
secretAccessKey: env('CNPMCORE_NFS_S3_CLIENT_SECRET', 'string', ''),
},
bucket: env('CNPMCORE_NFS_S3_CLIENT_BUCKET', 'string', ''),
forcePathStyle: env(
'CNPMCORE_NFS_S3_CLIENT_FORCE_PATH_STYLE',
'boolean',
false
),
disableURL: env('CNPMCORE_NFS_S3_CLIENT_DISABLE_URL', 'boolean', false),
};
assert.ok(s3Config.endpoint, 'require env CNPMCORE_NFS_S3_CLIENT_ENDPOINT');
assert.ok(
s3Config.credentials.accessKeyId,
'require env CNPMCORE_NFS_S3_CLIENT_ID'
);
assert.ok(
s3Config.credentials.secretAccessKey,
'require env CNPMCORE_NFS_S3_CLIENT_SECRET'
);
assert.ok(s3Config.bucket, 'require env CNPMCORE_NFS_S3_CLIENT_BUCKET');
// @ts-expect-error has no construct signatures
config.nfs.client = new S3Client(s3Config);
}
config.logger = {
enablePerformanceTimer: true,
enableFastContextLogger: true,
appLogName: env(
'CNPMCORE_APP_LOG_NAME',
'string',
`${appInfo.name}-web.log`
),
coreLogName: env('CNPMCORE_CORE_LOG_NAME', 'string', 'egg-web.log'),
agentLogName: env('CNPMCORE_AGENT_LOG_NAME', 'string', 'egg-agent.log'),
errorLogName: env('CNPMCORE_ERROR_LOG_NAME', 'string', 'common-error.log'),
outputJSON: env('CNPMCORE_LOG_JSON_OUTPUT', 'boolean', false),
};
const logDir = env('CNPMCORE_LOG_DIR', 'string', '');
if (logDir) {
config.logger.dir = logDir;
}
config.logrotator = {
// only keep 1 days log files
maxDays: 1,
};
config.bodyParser = {
// saveTag will send version string in JSON format
strict: false,
// set default limit to 10mb, see https://github.com/npm/npm/issues/12750
jsonLimit: '10mb',
// https://github.com/cnpm/cnpmcore/issues/551
ignore: NOT_IMPLEMENTED_PATH,
};
// https://github.com/xiekw2010/egg-typebox-validate#%E5%A6%82%E4%BD%95%E5%86%99%E8%87%AA%E5%AE%9A%E4%B9%89%E6%A0%A1%E9%AA%8C%E8%A7%84%E5%88%99
config.typeboxValidate = { patchAjv };
config.httpclient = {
useHttpClientNext: true,
allowH2: true,
};
config.view = {
root: join(appInfo.baseDir, 'app/port'),
defaultViewEngine: 'nunjucks',
};
config.customLogger = {
sqlLogger: {
file: 'sql.log',
},
};
// more options: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html
if (config.cnpmcore.enableElasticsearch) {
config.elasticsearch = {
client: {
node: env('CNPMCORE_CONFIG_ES_CLIENT_NODE', 'string', ''),
auth: {
username: env(
'CNPMCORE_CONFIG_ES_CLIENT_AUTH_USERNAME',
'string',
''
),
password: env(
'CNPMCORE_CONFIG_ES_CLIENT_AUTH_PASSWORD',
'string',
''
),
},
},
};
}
return config;
}