Skip to content

Commit 33c27a1

Browse files
committed
refactor(ts): change config module to use TS and ESM
1 parent 91e8a4f commit 33c27a1

File tree

6 files changed

+139
-100
lines changed

6 files changed

+139
-100
lines changed

src/config/env.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/config/env.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export type ServerConfig = {
2+
GIT_PROXY_SERVER_PORT: string | number;
3+
GIT_PROXY_HTTPS_SERVER_PORT: string | number;
4+
GIT_PROXY_UI_HOST: string;
5+
GIT_PROXY_UI_PORT: string | number;
6+
};
7+
8+
const {
9+
GIT_PROXY_SERVER_PORT = 8000,
10+
GIT_PROXY_HTTPS_SERVER_PORT = 8443,
11+
GIT_PROXY_UI_HOST = 'http://localhost',
12+
GIT_PROXY_UI_PORT = 8080
13+
} = process.env;
14+
15+
export const serverConfig: ServerConfig = {
16+
GIT_PROXY_SERVER_PORT,
17+
GIT_PROXY_HTTPS_SERVER_PORT,
18+
GIT_PROXY_UI_HOST,
19+
GIT_PROXY_UI_PORT
20+
};

src/config/file.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/config/file.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { readFileSync } from 'fs';
2+
import { join } from 'path';
3+
import { validate as jsonSchemaValidate } from 'jsonschema';
4+
5+
export let configFile: string = join(process.cwd(), 'proxy.config.json');
6+
7+
/**
8+
* Set the config file path.
9+
* @param {string} file - The path to the config file.
10+
*/
11+
export function setConfigFile(file: string) {
12+
configFile = file;
13+
}
14+
15+
/**
16+
* Validate config file.
17+
* @param {string} configFilePath - The path to the config file.
18+
* @return {boolean} - Returns true if validation is successful.
19+
* @throws Will throw an error if the validation fails.
20+
*/
21+
export function validate(configFilePath: string = configFile!): boolean {
22+
const config = JSON.parse(readFileSync(configFilePath, 'utf-8'));
23+
const schemaPath = join(process.cwd(), 'config.schema.json');
24+
const schema = JSON.parse(readFileSync(schemaPath, 'utf-8'));
25+
jsonSchemaValidate(config, schema, { required: true, throwError: true });
26+
return true;
27+
}
Lines changed: 47 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
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;
1416
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;
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;

src/config/types.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
export interface UserSettings {
2+
authorisedList: AuthorisedRepo[];
3+
sink: Database[];
4+
authentication: Authentication[];
5+
tempPassword?: TempPasswordConfig;
6+
proxyUrl: string;
7+
api: Record<string, any>;
8+
cookieSecret: string;
9+
sessionMaxAgeHours: number;
10+
sslKeyPemPath?: string; // Optional (not in config.schema.json)
11+
sslCertPemPath?: string; // Optional (not in config.schema.json)
12+
plugins: any[];
13+
commitConfig: Record<string, unknown>;
14+
attestationConfig: Record<string, unknown>;
15+
privateOrganizations: any[];
16+
urlShortener: string;
17+
contactEmail: string;
18+
csrfProtection: boolean;
19+
domains: Record<string, unknown>;
20+
}
21+
22+
export interface AuthorisedRepo {
23+
project: string;
24+
name: string;
25+
url: string;
26+
}
27+
28+
export interface Database {
29+
type: string;
30+
enabled: boolean;
31+
connectionString?: string;
32+
params?: Record<string, unknown>;
33+
options?: Record<string, unknown>;
34+
}
35+
36+
export interface Authentication {
37+
type: string;
38+
enabled: boolean;
39+
options?: Record<string, unknown>;
40+
}
41+
42+
export interface TempPasswordConfig {
43+
sendEmail: boolean;
44+
emailConfig: Record<string, unknown>;
45+
}

0 commit comments

Comments
 (0)