1- const fs = require ( 'fs' ) ;
1+ import { existsSync , readFileSync } from 'fs' ;
22
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' ;
56
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' ) ) ;
911}
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 ;
1416let _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 ;
2831
2932// Get configured proxy URL
30- const getProxyUrl = ( ) => {
33+ export const getProxyUrl = ( ) => {
3134 if ( _userSettings !== null && _userSettings . proxyUrl ) {
3235 _proxyUrl = _userSettings . proxyUrl ;
3336 }
@@ -36,25 +39,24 @@ const getProxyUrl = () => {
3639} ;
3740
3841// Gets a list of authorised repositories
39- const getAuthorisedList = ( ) => {
42+ export const getAuthorisedList = ( ) => {
4043 if ( _userSettings !== null && _userSettings . authorisedList ) {
4144 _authorisedList = _userSettings . authorisedList ;
4245 }
43-
4446 return _authorisedList ;
4547} ;
4648
4749// Gets a list of authorised repositories
48- const getTempPasswordConfig = ( ) => {
50+ export const getTempPasswordConfig = ( ) => {
4951 if ( _userSettings !== null && _userSettings . tempPassword ) {
5052 _tempPassword = _userSettings . tempPassword ;
5153 }
5254
5355 return _tempPassword ;
5456} ;
5557
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 = ( ) => {
5860 if ( _userSettings !== null && _userSettings . sink ) {
5961 _database = _userSettings . sink ;
6062 }
@@ -70,8 +72,8 @@ const getDatabase = () => {
7072 throw Error ( 'No database cofigured!' ) ;
7173} ;
7274
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 = ( ) => {
7577 if ( _userSettings !== null && _userSettings . authentication ) {
7678 _authentication = _userSettings . authentication ;
7779 }
@@ -87,90 +89,90 @@ const getAuthentication = () => {
8789} ;
8890
8991// Log configuration to console
90- const logConfiguration = ( ) => {
92+ export const logConfiguration = ( ) => {
9193 console . log ( `authorisedList = ${ JSON . stringify ( getAuthorisedList ( ) ) } ` ) ;
9294 console . log ( `data sink = ${ JSON . stringify ( getDatabase ( ) ) } ` ) ;
9395 console . log ( `authentication = ${ JSON . stringify ( getAuthentication ( ) ) } ` ) ;
9496} ;
9597
96- const getAPIs = ( ) => {
98+ export const getAPIs = ( ) => {
9799 if ( _userSettings && _userSettings . api ) {
98100 _api = _userSettings . api ;
99101 }
100102 return _api ;
101103} ;
102104
103- const getCookieSecret = ( ) => {
105+ export const getCookieSecret = ( ) => {
104106 if ( _userSettings && _userSettings . cookieSecret ) {
105107 _cookieSecret = _userSettings . cookieSecret ;
106108 }
107109 return _cookieSecret ;
108110} ;
109111
110- const getSessionMaxAgeHours = ( ) => {
112+ export const getSessionMaxAgeHours = ( ) => {
111113 if ( _userSettings && _userSettings . sessionMaxAgeHours ) {
112114 _sessionMaxAgeHours = _userSettings . sessionMaxAgeHours ;
113115 }
114116 return _sessionMaxAgeHours ;
115117} ;
116118
117119// Get commit related configuration
118- const getCommitConfig = ( ) => {
120+ export const getCommitConfig = ( ) => {
119121 if ( _userSettings && _userSettings . commitConfig ) {
120122 _commitConfig = _userSettings . commitConfig ;
121123 }
122124 return _commitConfig ;
123125} ;
124126
125127// Get attestation related configuration
126- const getAttestationConfig = ( ) => {
128+ export const getAttestationConfig = ( ) => {
127129 if ( _userSettings && _userSettings . attestationConfig ) {
128130 _attestationConfig = _userSettings . attestationConfig ;
129131 }
130132 return _attestationConfig ;
131133} ;
132134
133135// Get private organizations related configuration
134- const getPrivateOrganizations = ( ) => {
136+ export const getPrivateOrganizations = ( ) => {
135137 if ( _userSettings && _userSettings . privateOrganizations ) {
136138 _privateOrganizations = _userSettings . privateOrganizations ;
137139 }
138140 return _privateOrganizations ;
139141} ;
140142
141143// Get URL shortener
142- const getURLShortener = ( ) => {
144+ export const getURLShortener = ( ) => {
143145 if ( _userSettings && _userSettings . urlShortener ) {
144146 _urlShortener = _userSettings . urlShortener ;
145147 }
146148 return _urlShortener ;
147149} ;
148150
149151// Get contact e-mail address
150- const getContactEmail = ( ) => {
152+ export const getContactEmail = ( ) => {
151153 if ( _userSettings && _userSettings . contactEmail ) {
152154 _contactEmail = _userSettings . contactEmail ;
153155 }
154156 return _contactEmail ;
155157} ;
156158
157159// Get CSRF protection flag
158- const getCSRFProtection = ( ) => {
160+ export const getCSRFProtection = ( ) => {
159161 if ( _userSettings && _userSettings . csrfProtection ) {
160162 _csrfProtection = _userSettings . csrfProtection ;
161163 }
162164 return _csrfProtection ;
163165} ;
164166
165167// Get loadable push plugins
166- const getPlugins = ( ) => {
168+ export const getPlugins = ( ) => {
167169 if ( _userSettings && _userSettings . plugins ) {
168170 _plugins = _userSettings . plugins ;
169171 }
170172 return _plugins ;
171173}
172174
173- const getSSLKeyPath = ( ) => {
175+ export const getSSLKeyPath = ( ) => {
174176 if ( _userSettings && _userSettings . sslKeyPemPath ) {
175177 _sslKeyPath = _userSettings . sslKeyPemPath ;
176178 }
@@ -180,7 +182,7 @@ const getSSLKeyPath = () => {
180182 return _sslKeyPath ;
181183} ;
182184
183- const getSSLCertPath = ( ) => {
185+ export const getSSLCertPath = ( ) => {
184186 if ( _userSettings && _userSettings . sslCertPemPath ) {
185187 _sslCertPath = _userSettings . sslCertPemPath ;
186188 }
@@ -190,29 +192,9 @@ const getSSLCertPath = () => {
190192 return _sslCertPath ;
191193} ;
192194
193- const getDomains = ( ) => {
195+ export const getDomains = ( ) => {
194196 if ( _userSettings && _userSettings . domains ) {
195197 _domains = _userSettings . domains ;
196198 }
197199 return _domains ;
198200} ;
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