1
- const fs = require ( 'fs' ) ;
1
+ import { existsSync , readFileSync } from 'fs' ;
2
2
3
- const defaultSettings = require ( '../../proxy.config.json' ) ;
4
- const userSettingsPath = require ( './file' ) . configFile ;
3
+ import defaultSettings from '../../proxy.config.json' ;
4
+ import { configFile } from './file' ;
5
+ import { Authentication , AuthorisedRepo , Database , TempPasswordConfig , UserSettings } from './types' ;
5
6
6
- let _userSettings = null ;
7
- if ( fs . existsSync ( userSettingsPath ) ) {
8
- _userSettings = JSON . parse ( fs . readFileSync ( userSettingsPath ) ) ;
7
+
8
+ let _userSettings : UserSettings | null = null ;
9
+ if ( existsSync ( configFile ) ) {
10
+ _userSettings = JSON . parse ( readFileSync ( configFile , 'utf-8' ) ) ;
9
11
}
10
- let _authorisedList = defaultSettings . authorisedList ;
11
- let _database = defaultSettings . sink ;
12
- let _authentication = defaultSettings . authentication ;
13
- let _tempPassword = defaultSettings . tempPassword ;
12
+ let _authorisedList : AuthorisedRepo [ ] = defaultSettings . authorisedList ;
13
+ let _database : Database [ ] = defaultSettings . sink ;
14
+ let _authentication : Authentication [ ] = defaultSettings . authentication ;
15
+ let _tempPassword : TempPasswordConfig = defaultSettings . tempPassword ;
14
16
let _proxyUrl = defaultSettings . proxyUrl ;
15
- let _api = defaultSettings . api ;
16
- let _cookieSecret = defaultSettings . cookieSecret ;
17
- let _sessionMaxAgeHours = defaultSettings . sessionMaxAgeHours ;
18
- let _sslKeyPath = defaultSettings . sslKeyPemPath ;
19
- let _sslCertPath = defaultSettings . sslCertPemPath ;
20
- let _plugins = defaultSettings . plugins ;
21
- let _commitConfig = defaultSettings . commitConfig ;
22
- let _attestationConfig = defaultSettings . attestationConfig ;
23
- let _privateOrganizations = defaultSettings . privateOrganizations ;
24
- let _urlShortener = defaultSettings . urlShortener ;
25
- let _contactEmail = defaultSettings . contactEmail ;
26
- let _csrfProtection = defaultSettings . csrfProtection ;
27
- let _domains = defaultSettings . domains ;
17
+ let _api : Record < string , unknown > = defaultSettings . api ;
18
+ let _cookieSecret : string = defaultSettings . cookieSecret ;
19
+ let _sessionMaxAgeHours : number = defaultSettings . sessionMaxAgeHours ;
20
+ let _plugins : any [ ] = defaultSettings . plugins ;
21
+ let _commitConfig : Record < string , unknown > = defaultSettings . commitConfig ;
22
+ let _attestationConfig : Record < string , unknown > = defaultSettings . attestationConfig ;
23
+ let _privateOrganizations : string [ ] = defaultSettings . privateOrganizations ;
24
+ let _urlShortener : string = defaultSettings . urlShortener ;
25
+ let _contactEmail : string = defaultSettings . contactEmail ;
26
+ let _csrfProtection : boolean = defaultSettings . csrfProtection ;
27
+ let _domains : Record < string , unknown > = defaultSettings . domains ;
28
+ // These are not always present in the default config file, so casting is required
29
+ let _sslKeyPath : string = ( defaultSettings as any ) . sslKeyPemPath ;
30
+ let _sslCertPath : string = ( defaultSettings as any ) . sslCertPemPath ;
28
31
29
32
// Get configured proxy URL
30
- const getProxyUrl = ( ) => {
33
+ export const getProxyUrl = ( ) => {
31
34
if ( _userSettings !== null && _userSettings . proxyUrl ) {
32
35
_proxyUrl = _userSettings . proxyUrl ;
33
36
}
@@ -36,25 +39,24 @@ const getProxyUrl = () => {
36
39
} ;
37
40
38
41
// Gets a list of authorised repositories
39
- const getAuthorisedList = ( ) => {
42
+ export const getAuthorisedList = ( ) => {
40
43
if ( _userSettings !== null && _userSettings . authorisedList ) {
41
44
_authorisedList = _userSettings . authorisedList ;
42
45
}
43
-
44
46
return _authorisedList ;
45
47
} ;
46
48
47
49
// Gets a list of authorised repositories
48
- const getTempPasswordConfig = ( ) => {
50
+ export const getTempPasswordConfig = ( ) => {
49
51
if ( _userSettings !== null && _userSettings . tempPassword ) {
50
52
_tempPassword = _userSettings . tempPassword ;
51
53
}
52
54
53
55
return _tempPassword ;
54
56
} ;
55
57
56
- // Gets the configuared data sink, defaults to filesystem
57
- const getDatabase = ( ) => {
58
+ // Gets the configured data sink, defaults to filesystem
59
+ export const getDatabase = ( ) => {
58
60
if ( _userSettings !== null && _userSettings . sink ) {
59
61
_database = _userSettings . sink ;
60
62
}
@@ -70,8 +72,8 @@ const getDatabase = () => {
70
72
throw Error ( 'No database cofigured!' ) ;
71
73
} ;
72
74
73
- // Gets the configuared data sink , defaults to filesystem
74
- const getAuthentication = ( ) => {
75
+ // Gets the configured authentication method , defaults to local
76
+ export const getAuthentication = ( ) => {
75
77
if ( _userSettings !== null && _userSettings . authentication ) {
76
78
_authentication = _userSettings . authentication ;
77
79
}
@@ -87,90 +89,90 @@ const getAuthentication = () => {
87
89
} ;
88
90
89
91
// Log configuration to console
90
- const logConfiguration = ( ) => {
92
+ export const logConfiguration = ( ) => {
91
93
console . log ( `authorisedList = ${ JSON . stringify ( getAuthorisedList ( ) ) } ` ) ;
92
94
console . log ( `data sink = ${ JSON . stringify ( getDatabase ( ) ) } ` ) ;
93
95
console . log ( `authentication = ${ JSON . stringify ( getAuthentication ( ) ) } ` ) ;
94
96
} ;
95
97
96
- const getAPIs = ( ) => {
98
+ export const getAPIs = ( ) => {
97
99
if ( _userSettings && _userSettings . api ) {
98
100
_api = _userSettings . api ;
99
101
}
100
102
return _api ;
101
103
} ;
102
104
103
- const getCookieSecret = ( ) => {
105
+ export const getCookieSecret = ( ) => {
104
106
if ( _userSettings && _userSettings . cookieSecret ) {
105
107
_cookieSecret = _userSettings . cookieSecret ;
106
108
}
107
109
return _cookieSecret ;
108
110
} ;
109
111
110
- const getSessionMaxAgeHours = ( ) => {
112
+ export const getSessionMaxAgeHours = ( ) => {
111
113
if ( _userSettings && _userSettings . sessionMaxAgeHours ) {
112
114
_sessionMaxAgeHours = _userSettings . sessionMaxAgeHours ;
113
115
}
114
116
return _sessionMaxAgeHours ;
115
117
} ;
116
118
117
119
// Get commit related configuration
118
- const getCommitConfig = ( ) => {
120
+ export const getCommitConfig = ( ) => {
119
121
if ( _userSettings && _userSettings . commitConfig ) {
120
122
_commitConfig = _userSettings . commitConfig ;
121
123
}
122
124
return _commitConfig ;
123
125
} ;
124
126
125
127
// Get attestation related configuration
126
- const getAttestationConfig = ( ) => {
128
+ export const getAttestationConfig = ( ) => {
127
129
if ( _userSettings && _userSettings . attestationConfig ) {
128
130
_attestationConfig = _userSettings . attestationConfig ;
129
131
}
130
132
return _attestationConfig ;
131
133
} ;
132
134
133
135
// Get private organizations related configuration
134
- const getPrivateOrganizations = ( ) => {
136
+ export const getPrivateOrganizations = ( ) => {
135
137
if ( _userSettings && _userSettings . privateOrganizations ) {
136
138
_privateOrganizations = _userSettings . privateOrganizations ;
137
139
}
138
140
return _privateOrganizations ;
139
141
} ;
140
142
141
143
// Get URL shortener
142
- const getURLShortener = ( ) => {
144
+ export const getURLShortener = ( ) => {
143
145
if ( _userSettings && _userSettings . urlShortener ) {
144
146
_urlShortener = _userSettings . urlShortener ;
145
147
}
146
148
return _urlShortener ;
147
149
} ;
148
150
149
151
// Get contact e-mail address
150
- const getContactEmail = ( ) => {
152
+ export const getContactEmail = ( ) => {
151
153
if ( _userSettings && _userSettings . contactEmail ) {
152
154
_contactEmail = _userSettings . contactEmail ;
153
155
}
154
156
return _contactEmail ;
155
157
} ;
156
158
157
159
// Get CSRF protection flag
158
- const getCSRFProtection = ( ) => {
160
+ export const getCSRFProtection = ( ) => {
159
161
if ( _userSettings && _userSettings . csrfProtection ) {
160
162
_csrfProtection = _userSettings . csrfProtection ;
161
163
}
162
164
return _csrfProtection ;
163
165
} ;
164
166
165
167
// Get loadable push plugins
166
- const getPlugins = ( ) => {
168
+ export const getPlugins = ( ) => {
167
169
if ( _userSettings && _userSettings . plugins ) {
168
170
_plugins = _userSettings . plugins ;
169
171
}
170
172
return _plugins ;
171
173
}
172
174
173
- const getSSLKeyPath = ( ) => {
175
+ export const getSSLKeyPath = ( ) => {
174
176
if ( _userSettings && _userSettings . sslKeyPemPath ) {
175
177
_sslKeyPath = _userSettings . sslKeyPemPath ;
176
178
}
@@ -180,7 +182,7 @@ const getSSLKeyPath = () => {
180
182
return _sslKeyPath ;
181
183
} ;
182
184
183
- const getSSLCertPath = ( ) => {
185
+ export const getSSLCertPath = ( ) => {
184
186
if ( _userSettings && _userSettings . sslCertPemPath ) {
185
187
_sslCertPath = _userSettings . sslCertPemPath ;
186
188
}
@@ -190,29 +192,9 @@ const getSSLCertPath = () => {
190
192
return _sslCertPath ;
191
193
} ;
192
194
193
- const getDomains = ( ) => {
195
+ export const getDomains = ( ) => {
194
196
if ( _userSettings && _userSettings . domains ) {
195
197
_domains = _userSettings . domains ;
196
198
}
197
199
return _domains ;
198
200
} ;
199
-
200
- exports . getAPIs = getAPIs ;
201
- exports . getProxyUrl = getProxyUrl ;
202
- exports . getAuthorisedList = getAuthorisedList ;
203
- exports . getDatabase = getDatabase ;
204
- exports . logConfiguration = logConfiguration ;
205
- exports . getAuthentication = getAuthentication ;
206
- exports . getTempPasswordConfig = getTempPasswordConfig ;
207
- exports . getCookieSecret = getCookieSecret ;
208
- exports . getSessionMaxAgeHours = getSessionMaxAgeHours ;
209
- exports . getCommitConfig = getCommitConfig ;
210
- exports . getAttestationConfig = getAttestationConfig ;
211
- exports . getPrivateOrganizations = getPrivateOrganizations ;
212
- exports . getURLShortener = getURLShortener ;
213
- exports . getContactEmail = getContactEmail ;
214
- exports . getCSRFProtection = getCSRFProtection ;
215
- exports . getPlugins = getPlugins ;
216
- exports . getSSLKeyPath = getSSLKeyPath ;
217
- exports . getSSLCertPath = getSSLCertPath ;
218
- exports . getDomains = getDomains ;
0 commit comments