1
1
import { strict as assert } from 'node:assert' ;
2
2
import { randomUUID } from 'node:crypto' ;
3
3
import { join } from 'node:path' ;
4
- import { EggAppConfig , PowerPartial } from 'egg' ;
4
+ import { EggAppConfig , PowerPartial , Context } from 'egg' ;
5
5
import OSSClient from 'oss-cnpm' ;
6
6
import { patchAjv } from '../app/port/typebox' ;
7
7
import { ChangesStreamMode , NOT_IMPLEMENTED_PATH , SyncDeleteMode , SyncMode } from '../app/common/constants' ;
8
+ import { env } from '../app/common/EnvUtil' ;
8
9
import type { CnpmcoreConfig } from '../app/port/config' ;
9
10
import { database } from './database' ;
10
11
11
12
export const cnpmcoreConfig : CnpmcoreConfig = {
12
13
name : 'cnpm' ,
13
14
hookEnable : false ,
14
15
hooksLimit : 20 ,
15
- sourceRegistry : ' https://registry.npmjs.org',
16
- sourceRegistryIsCNpm : false ,
16
+ sourceRegistry : env ( 'CNPMCORE_CONFIG_SOURCE_REGISTRY' , 'string' , ' https://registry.npmjs.org') ,
17
+ sourceRegistryIsCNpm : env ( 'CNPMCORE_CONFIG_SOURCE_REGISTRY_IS_CNPM' , 'boolean' , false ) ,
17
18
syncUpstreamFirst : false ,
18
19
sourceRegistrySyncTimeout : 180000 ,
19
20
taskQueueHighWaterSize : 100 ,
@@ -33,7 +34,7 @@ export const cnpmcoreConfig: CnpmcoreConfig = {
33
34
checkChangesStreamInterval : 500 ,
34
35
changesStreamRegistry : 'https://replicate.npmjs.com' ,
35
36
changesStreamRegistryMode : ChangesStreamMode . streaming ,
36
- registry : process . env . CNPMCORE_CONFIG_REGISTRY || 'http://localhost:7001' ,
37
+ registry : env ( ' CNPMCORE_CONFIG_REGISTRY' , 'string' , 'http://localhost:7001' ) ,
37
38
alwaysAuth : false ,
38
39
allowScopes : [
39
40
'@cnpm' ,
@@ -45,7 +46,7 @@ export const cnpmcoreConfig: CnpmcoreConfig = {
45
46
admins : {
46
47
cnpmcore_admin :
'[email protected] ' ,
47
48
} ,
48
- enableWebAuthn : ! ! process . env . CNPMCORE_CONFIG_ENABLE_WEB_AUTHN ,
49
+ enableWebAuthn : env ( ' CNPMCORE_CONFIG_ENABLE_WEB_AUTHN' , 'boolean' , false ) ,
49
50
enableCDN : false ,
50
51
cdnCacheControlHeader : 'public, max-age=300' ,
51
52
cdnVaryHeader : 'Accept, Accept-Encoding' ,
@@ -57,7 +58,7 @@ export const cnpmcoreConfig: CnpmcoreConfig = {
57
58
enableSyncUnpkgFiles : true ,
58
59
enableSyncUnpkgFilesWhiteList : false ,
59
60
strictSyncSpecivicVersion : false ,
60
- enableElasticsearch : ! ! process . env . CNPMCORE_CONFIG_ENABLE_ES ,
61
+ enableElasticsearch : env ( ' CNPMCORE_CONFIG_ENABLE_ES' , 'boolean' , false ) ,
61
62
elasticsearchIndex : 'cnpmcore_packages' ,
62
63
strictValidateTarballPkg : false ,
63
64
strictValidatePackageDeps : false ,
@@ -69,14 +70,14 @@ export const cnpmcoreConfig: CnpmcoreConfig = {
69
70
export default ( appInfo : EggAppConfig ) => {
70
71
const config = { } as PowerPartial < EggAppConfig > ;
71
72
72
- config . keys = process . env . CNPMCORE_EGG_KEYS || randomUUID ( ) ;
73
+ config . keys = env ( ' CNPMCORE_EGG_KEYS' , 'string' , randomUUID ( ) ) ;
73
74
config . cnpmcore = cnpmcoreConfig ;
74
75
75
76
// override config from framework / plugin
76
- config . dataDir = process . env . CNPMCORE_DATA_DIR || join ( appInfo . root , '.cnpmcore' ) ;
77
+ config . dataDir = env ( ' CNPMCORE_DATA_DIR' , 'string' , join ( appInfo . root , '.cnpmcore' ) ) ;
77
78
config . orm = {
78
79
...database ,
79
- database : database . name ?? 'cnpmcore' ,
80
+ database : database . name || 'cnpmcore' ,
80
81
charset : 'utf8mb4' ,
81
82
logger : {
82
83
// https://github.com/cyjake/leoric/blob/master/docs/zh/logging.md#logqueryerror
@@ -90,10 +91,10 @@ export default (appInfo: EggAppConfig) => {
90
91
91
92
config . redis = {
92
93
client : {
93
- port : Number ( process . env . CNPMCORE_REDIS_PORT || 6379 ) ,
94
- host : process . env . CNPMCORE_REDIS_HOST || '127.0.0.1' ,
95
- password : process . env . CNPMCORE_REDIS_PASSWORD || '' ,
96
- db : Number ( process . env . CNPMCORE_REDIS_DB || 0 ) ,
94
+ port : env ( ' CNPMCORE_REDIS_PORT' , 'number' , 6379 ) ,
95
+ host : env ( ' CNPMCORE_REDIS_HOST' , 'string' , '127.0.0.1' ) ,
96
+ password : env ( ' CNPMCORE_REDIS_PASSWORD' , 'string' , '' ) ,
97
+ db : env ( ' CNPMCORE_REDIS_DB' , 'number' , 0 ) ,
97
98
} ,
98
99
} ;
99
100
@@ -105,7 +106,7 @@ export default (appInfo: EggAppConfig) => {
105
106
106
107
config . cors = {
107
108
// allow all domains
108
- origin : ( ctx ) : string => {
109
+ origin : ( ctx : Context ) : string => {
109
110
return ctx . get ( 'Origin' ) ;
110
111
} ,
111
112
credentials : true ,
@@ -115,59 +116,61 @@ export default (appInfo: EggAppConfig) => {
115
116
116
117
config . nfs = {
117
118
client : null ,
118
- dir : process . env . CNPMCORE_NFS_DIR || join ( config . dataDir , 'nfs' ) ,
119
+ dir : env ( ' CNPMCORE_NFS_DIR' , 'string' , join ( config . dataDir , 'nfs' ) ) ,
119
120
} ;
120
121
/* c8 ignore next 17 */
121
122
// enable oss nfs store by env values
122
- if ( process . env . CNPMCORE_NFS_TYPE === 'oss' ) {
123
- assert ( process . env . CNPMCORE_NFS_OSS_BUCKET , 'require env CNPMCORE_NFS_OSS_BUCKET' ) ;
124
- assert ( process . env . CNPMCORE_NFS_OSS_ENDPOINT , 'require env CNPMCORE_NFS_OSS_ENDPOINT' ) ;
125
- assert ( process . env . CNPMCORE_NFS_OSS_ID , 'require env CNPMCORE_NFS_OSS_ID' ) ;
126
- assert ( process . env . CNPMCORE_NFS_OSS_SECRET , 'require env CNPMCORE_NFS_OSS_SECRET' ) ;
127
- config . nfs . client = new OSSClient ( {
128
- cdnBaseUrl : process . env . CNPMCORE_NFS_OSS_CDN ,
129
- endpoint : process . env . CNPMCORE_NFS_OSS_ENDPOINT ,
130
- bucket : process . env . CNPMCORE_NFS_OSS_BUCKET ,
131
- accessKeyId : process . env . CNPMCORE_NFS_OSS_ID ,
132
- accessKeySecret : process . env . CNPMCORE_NFS_OSS_SECRET ,
123
+ const nfsType = env ( 'CNPMCORE_NFS_TYPE' , 'string' , '' ) ;
124
+ if ( nfsType === 'oss' ) {
125
+ const ossConfig = {
126
+ cdnBaseUrl : env ( 'CNPMCORE_NFS_OSS_CDN' , 'string' , '' ) ,
127
+ endpoint : env ( 'CNPMCORE_NFS_OSS_ENDPOINT' , 'string' , '' ) ,
128
+ bucket : env ( 'CNPMCORE_NFS_OSS_BUCKET' , 'string' , '' ) ,
129
+ accessKeyId : env ( 'CNPMCORE_NFS_OSS_ID' , 'string' , '' ) ,
130
+ accessKeySecret : env ( 'CNPMCORE_NFS_OSS_SECRET' , 'string' , '' ) ,
133
131
defaultHeaders : {
134
132
'Cache-Control' : 'max-age=0, s-maxage=60' ,
135
133
} ,
136
- } ) ;
137
- } else if ( process . env . CNPMCORE_NFS_TYPE === 's3' ) {
138
- assert ( process . env . CNPMCORE_NFS_S3_CLIENT_ENDPOINT , 'require env CNPMCORE_NFS_S3_CLIENT_ENDPOINT' ) ;
139
- assert ( process . env . CNPMCORE_NFS_S3_CLIENT_ID , 'require env CNPMCORE_NFS_S3_CLIENT_ID' ) ;
140
- assert ( process . env . CNPMCORE_NFS_S3_CLIENT_SECRET , 'require env CNPMCORE_NFS_S3_CLIENT_SECRET' ) ;
141
- assert ( process . env . CNPMCORE_NFS_S3_CLIENT_BUCKET , 'require env CNPMCORE_NFS_S3_CLIENT_BUCKET' ) ;
142
- // eslint-disable-next-line @typescript-eslint/no-var-requires
143
- const S3Client = require ( 's3-cnpmcore' ) ;
144
- config . nfs . client = new S3Client ( {
145
- region : process . env . CNPMCORE_NFS_S3_CLIENT_REGION || 'default' ,
146
- endpoint : process . env . CNPMCORE_NFS_S3_CLIENT_ENDPOINT ,
134
+ } ;
135
+ assert ( ossConfig . cdnBaseUrl , 'require env CNPMCORE_NFS_OSS_BUCKET' ) ;
136
+ assert ( ossConfig . endpoint , 'require env CNPMCORE_NFS_OSS_ENDPOINT' ) ;
137
+ assert ( ossConfig . accessKeyId , 'require env CNPMCORE_NFS_OSS_ID' ) ;
138
+ assert ( ossConfig . accessKeySecret , 'require env CNPMCORE_NFS_OSS_SECRET' ) ;
139
+ config . nfs . client = new OSSClient ( ossConfig ) ;
140
+ } else if ( nfsType === 's3' ) {
141
+ const s3Config = {
142
+ region : env ( 'CNPMCORE_NFS_S3_CLIENT_REGION' , 'string' , 'default' ) ,
143
+ endpoint : env ( 'CNPMCORE_NFS_S3_CLIENT_ENDPOINT' , 'string' , '' ) ,
147
144
credentials : {
148
- accessKeyId : process . env . CNPMCORE_NFS_S3_CLIENT_ID ,
149
- secretAccessKey : process . env . CNPMCORE_NFS_S3_CLIENT_SECRET ,
145
+ accessKeyId : env ( ' CNPMCORE_NFS_S3_CLIENT_ID' , 'string' , '' ) ,
146
+ secretAccessKey : env ( ' CNPMCORE_NFS_S3_CLIENT_SECRET' , 'string' , '' ) ,
150
147
} ,
151
- bucket : process . env . CNPMCORE_NFS_S3_CLIENT_BUCKET ,
152
- forcePathStyle : ! ! process . env . CNPMCORE_NFS_S3_CLIENT_FORCE_PATH_STYLE ,
153
- disableURL : ! ! process . env . CNPMCORE_NFS_S3_CLIENT_DISABLE_URL ,
154
- } ) ;
148
+ bucket : env ( 'CNPMCORE_NFS_S3_CLIENT_BUCKET' , 'string' , '' ) ,
149
+ forcePathStyle : env ( 'CNPMCORE_NFS_S3_CLIENT_FORCE_PATH_STYLE' , 'boolean' , false ) ,
150
+ disableURL : env ( 'CNPMCORE_NFS_S3_CLIENT_DISABLE_URL' , 'boolean' , false ) ,
151
+ } ;
152
+ assert ( s3Config . endpoint , 'require env CNPMCORE_NFS_S3_CLIENT_ENDPOINT' ) ;
153
+ assert ( s3Config . credentials . accessKeyId , 'require env CNPMCORE_NFS_S3_CLIENT_ID' ) ;
154
+ assert ( s3Config . credentials . secretAccessKey , 'require env CNPMCORE_NFS_S3_CLIENT_SECRET' ) ;
155
+ assert ( s3Config . bucket , 'require env CNPMCORE_NFS_S3_CLIENT_BUCKET' ) ;
156
+ // TODO(@fengmk2): should change to use import to support esm
157
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
158
+ const S3Client = require ( 's3-cnpmcore' ) ;
159
+ config . nfs . client = new S3Client ( s3Config ) ;
155
160
}
156
161
157
162
config . logger = {
158
163
enablePerformanceTimer : true ,
159
164
enableFastContextLogger : true ,
160
- appLogName : process . env . CNPMCORE_APP_LOG_NAME || `${ appInfo . name } -web.log` ,
161
- coreLogName : process . env . CNPMCORE_CORE_LOG_NAME || 'egg-web.log' ,
162
- agentLogName : process . env . CNPMCORE_AGENT_LOG_NAME || 'egg-agent.log' ,
163
- errorLogName : process . env . CNPMCORE_ERROR_LOG_NAME || 'common-error.log' ,
164
- outputJSON : Boolean ( process . env . CNPMCORE_LOG_JSON_OUTPUT || false ) ,
165
+ appLogName : env ( ' CNPMCORE_APP_LOG_NAME' , 'string' , `${ appInfo . name } -web.log` ) ,
166
+ coreLogName : env ( ' CNPMCORE_CORE_LOG_NAME' , 'string' , 'egg-web.log' ) ,
167
+ agentLogName : env ( ' CNPMCORE_AGENT_LOG_NAME' , 'string' , 'egg-agent.log' ) ,
168
+ errorLogName : env ( ' CNPMCORE_ERROR_LOG_NAME' , 'string' , 'common-error.log' ) ,
169
+ outputJSON : env ( ' CNPMCORE_LOG_JSON_OUTPUT' , 'boolean' , false ) ,
165
170
} ;
166
- if ( process . env . CNPMCORE_LOG_DIR ) {
167
- config . logger . dir = process . env . CNPMCORE_LOG_DIR ;
168
- }
169
- if ( process . env . CNPMCORE_LOG_JSON_OUTPUT ) {
170
- config . logger . outputJSON = Boolean ( process . env . CNPMCORE_LOG_JSON_OUTPUT ) ;
171
+ const logDir = env ( 'CNPMCORE_LOG_DIR' , 'string' , '' ) ;
172
+ if ( logDir ) {
173
+ config . logger . dir = logDir ;
171
174
}
172
175
173
176
config . logrotator = {
@@ -207,14 +210,15 @@ export default (appInfo: EggAppConfig) => {
207
210
if ( config . cnpmcore . enableElasticsearch ) {
208
211
config . elasticsearch = {
209
212
client : {
210
- node : process . env . CNPMCORE_CONFIG_ES_CLIENT_NODE ,
213
+ node : env ( ' CNPMCORE_CONFIG_ES_CLIENT_NODE' , 'string' , '' ) ,
211
214
auth : {
212
- username : process . env . CNPMCORE_CONFIG_ES_CLIENT_AUTH_USERNAME as string ,
213
- password : process . env . CNPMCORE_CONFIG_ES_CLIENT_AUTH_PASSWORD as string ,
215
+ username : env ( ' CNPMCORE_CONFIG_ES_CLIENT_AUTH_USERNAME' , ' string' , '' ) ,
216
+ password : env ( ' CNPMCORE_CONFIG_ES_CLIENT_AUTH_PASSWORD' , ' string' , '' ) ,
214
217
} ,
215
218
} ,
216
219
} ;
217
220
}
218
221
219
222
return config ;
220
223
} ;
224
+
0 commit comments