Skip to content

Commit 42b2b6e

Browse files
committed
chore: add constants for file size and replace throughout app
1 parent bf920f8 commit 42b2b6e

File tree

7 files changed

+80
-69
lines changed

7 files changed

+80
-69
lines changed

src/config/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { GitProxyConfig, Convert } from './generated/config';
55
import { ConfigLoader, Configuration } from './ConfigLoader';
66
import { serverConfig } from './env';
77
import { configFile } from './file';
8+
import { GIGABYTE } from '../constants';
89

910
// Cache for current configuration
1011
let _currentConfig: GitProxyConfig | null = null;
@@ -299,7 +300,7 @@ export const getRateLimit = () => {
299300
export const getMaxPackSizeBytes = (): number => {
300301
const config = loadFullConfiguration();
301302
const configuredValue = config.limits?.maxPackSizeBytes;
302-
const fallback = 1024 * 1024 * 1024; // 1 GiB default
303+
const fallback = 1 * GIGABYTE; // 1 GiB default
303304

304305
if (
305306
typeof configuredValue === 'number' &&

src/constants/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const KILOBYTE = 1024;
2+
const MEGABYTE = KILOBYTE * 1024;
3+
const GIGABYTE = MEGABYTE * 1024;
4+
5+
export { KILOBYTE, MEGABYTE, GIGABYTE };

src/proxy/routes/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { executeChain } from '../chain';
66
import { processUrlPath, validGitRequest, getAllProxiedHosts } from './helper';
77
import { ProxyOptions } from 'express-http-proxy';
88
import { getMaxPackSizeBytes } from '../../config';
9+
import { MEGABYTE } from '../../constants';
910

1011
enum ActionType {
1112
ALLOWED = 'Allowed',
@@ -151,10 +152,10 @@ const extractRawBody = async (req: Request, res: Response, next: NextFunction) =
151152
}
152153

153154
const proxyStream = new PassThrough({
154-
highWaterMark: 4 * 1024 * 1024,
155+
highWaterMark: 4 * MEGABYTE,
155156
});
156157
const pluginStream = new PassThrough({
157-
highWaterMark: 4 * 1024 * 1024,
158+
highWaterMark: 4 * MEGABYTE,
158159
});
159160

160161
req.pipe(proxyStream);

src/proxy/ssh/server.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as db from '../../db';
88
import { Action } from '../actions';
99
import { SSHAgent } from '../../security/SSHAgent';
1010
import { SSHKeyManager } from '../../security/SSHKeyManager';
11+
import { KILOBYTE, MEGABYTE } from '../../constants';
1112

1213
interface SSHUser {
1314
username: string;
@@ -795,8 +796,8 @@ export class SSHServer {
795796
readyTimeout: 30000,
796797
keepaliveInterval: 15000,
797798
keepaliveCountMax: 5,
798-
windowSize: 1024 * 1024,
799-
packetSize: 32768,
799+
windowSize: 1 * MEGABYTE,
800+
packetSize: 32 * KILOBYTE,
800801
privateKey: usingUserKey ? (userPrivateKey as Buffer) : proxyPrivateKey,
801802
debug: (msg: string) => {
802803
console.debug('[GitHub SSH Debug]', msg);
@@ -950,8 +951,8 @@ export class SSHServer {
950951
readyTimeout: 30000,
951952
keepaliveInterval: 15000, // 15 seconds between keepalives (recommended for SSH connections is 15-30 seconds)
952953
keepaliveCountMax: 5, // Recommended for SSH connections is 3-5 attempts
953-
windowSize: 1024 * 1024, // 1MB window size
954-
packetSize: 32768, // 32KB packet size
954+
windowSize: 1 * MEGABYTE, // 1MB window size
955+
packetSize: 32 * KILOBYTE, // 32KB packet size
955956
privateKey: fs.readFileSync(sshConfig.hostKey.privateKeyPath),
956957
debug: (msg: string) => {
957958
console.debug('[GitHub SSH Debug]', msg);

test/proxy/performance.test.js

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
const chai = require('chai');
2+
const { KILOBYTE, MEGABYTE } = require('../../src/constants');
23
const expect = chai.expect;
34

45
describe('HTTP/HTTPS Performance Tests', () => {
56
describe('Memory Usage Tests', () => {
67
it('should handle small POST requests efficiently', async () => {
7-
const smallData = Buffer.alloc(1024); // 1KB
8+
const smallData = Buffer.alloc(1 * KILOBYTE);
89
const startMemory = process.memoryUsage().heapUsed;
910

1011
// Simulate request processing
@@ -20,12 +21,12 @@ describe('HTTP/HTTPS Performance Tests', () => {
2021
const endMemory = process.memoryUsage().heapUsed;
2122
const memoryIncrease = endMemory - startMemory;
2223

23-
expect(memoryIncrease).to.be.lessThan(1024 * 5); // Should use less than 5KB
24-
expect(req.body.length).to.equal(1024);
24+
expect(memoryIncrease).to.be.lessThan(KILOBYTE * 5); // Should use less than 5KB
25+
expect(req.body.length).to.equal(KILOBYTE);
2526
});
2627

2728
it('should handle medium POST requests within reasonable limits', async () => {
28-
const mediumData = Buffer.alloc(10 * 1024 * 1024); // 10MB
29+
const mediumData = Buffer.alloc(10 * MEGABYTE);
2930
const startMemory = process.memoryUsage().heapUsed;
3031

3132
// Simulate request processing
@@ -41,12 +42,12 @@ describe('HTTP/HTTPS Performance Tests', () => {
4142
const endMemory = process.memoryUsage().heapUsed;
4243
const memoryIncrease = endMemory - startMemory;
4344

44-
expect(memoryIncrease).to.be.lessThan(15 * 1024 * 1024); // Should use less than 15MB
45-
expect(req.body.length).to.equal(10 * 1024 * 1024);
45+
expect(memoryIncrease).to.be.lessThan(15 * MEGABYTE); // Should use less than 15MB
46+
expect(req.body.length).to.equal(10 * MEGABYTE);
4647
});
4748

4849
it('should handle large POST requests up to size limit', async () => {
49-
const largeData = Buffer.alloc(100 * 1024 * 1024); // 100MB
50+
const largeData = Buffer.alloc(100 * MEGABYTE);
5051
const startMemory = process.memoryUsage().heapUsed;
5152

5253
// Simulate request processing
@@ -62,25 +63,25 @@ describe('HTTP/HTTPS Performance Tests', () => {
6263
const endMemory = process.memoryUsage().heapUsed;
6364
const memoryIncrease = endMemory - startMemory;
6465

65-
expect(memoryIncrease).to.be.lessThan(120 * 1024 * 1024); // Should use less than 120MB
66-
expect(req.body.length).to.equal(100 * 1024 * 1024);
66+
expect(memoryIncrease).to.be.lessThan(120 * MEGABYTE); // Should use less than 120MB
67+
expect(req.body.length).to.equal(100 * MEGABYTE);
6768
});
6869

6970
it('should reject requests exceeding size limit', async () => {
70-
const oversizedData = Buffer.alloc(1200 * 1024 * 1024); // 1.2GB (exceeds 1GB limit)
71+
const oversizedData = Buffer.alloc(1200 * MEGABYTE); // 1.2GB (exceeds 1GB limit)
7172

7273
// Simulate size check
73-
const maxPackSize = 1024 * 1024 * 1024;
74+
const maxPackSize = 1 * GIGABYTE;
7475
const requestSize = oversizedData.length;
7576

7677
expect(requestSize).to.be.greaterThan(maxPackSize);
77-
expect(requestSize).to.equal(1200 * 1024 * 1024);
78+
expect(requestSize).to.equal(1200 * MEGABYTE);
7879
});
7980
});
8081

8182
describe('Processing Time Tests', () => {
8283
it('should process small requests quickly', async () => {
83-
const smallData = Buffer.alloc(1024); // 1KB
84+
const smallData = Buffer.alloc(1 * KILOBYTE);
8485
const startTime = Date.now();
8586

8687
// Simulate processing
@@ -96,11 +97,11 @@ describe('HTTP/HTTPS Performance Tests', () => {
9697
const processingTime = Date.now() - startTime;
9798

9899
expect(processingTime).to.be.lessThan(100); // Should complete in less than 100ms
99-
expect(req.body.length).to.equal(1024);
100+
expect(req.body.length).to.equal(1 * KILOBYTE);
100101
});
101102

102103
it('should process medium requests within acceptable time', async () => {
103-
const mediumData = Buffer.alloc(10 * 1024 * 1024); // 10MB
104+
const mediumData = Buffer.alloc(10 * MEGABYTE);
104105
const startTime = Date.now();
105106

106107
// Simulate processing
@@ -116,11 +117,11 @@ describe('HTTP/HTTPS Performance Tests', () => {
116117
const processingTime = Date.now() - startTime;
117118

118119
expect(processingTime).to.be.lessThan(1000); // Should complete in less than 1 second
119-
expect(req.body.length).to.equal(10 * 1024 * 1024);
120+
expect(req.body.length).to.equal(10 * MEGABYTE);
120121
});
121122

122123
it('should process large requests within reasonable time', async () => {
123-
const largeData = Buffer.alloc(100 * 1024 * 1024); // 100MB
124+
const largeData = Buffer.alloc(100 * MEGABYTE);
124125
const startTime = Date.now();
125126

126127
// Simulate processing
@@ -136,7 +137,7 @@ describe('HTTP/HTTPS Performance Tests', () => {
136137
const processingTime = Date.now() - startTime;
137138

138139
expect(processingTime).to.be.lessThan(5000); // Should complete in less than 5 seconds
139-
expect(req.body.length).to.equal(100 * 1024 * 1024);
140+
expect(req.body.length).to.equal(100 * MEGABYTE);
140141
});
141142
});
142143

@@ -148,7 +149,7 @@ describe('HTTP/HTTPS Performance Tests', () => {
148149
// Simulate 10 concurrent small requests
149150
for (let i = 0; i < 10; i++) {
150151
const request = new Promise((resolve) => {
151-
const smallData = Buffer.alloc(1024);
152+
const smallData = Buffer.alloc(1 * KILOBYTE);
152153
const req = {
153154
method: 'POST',
154155
url: '/github.com/test/test-repo.git/git-receive-pack',
@@ -168,7 +169,7 @@ describe('HTTP/HTTPS Performance Tests', () => {
168169
expect(results).to.have.length(10);
169170
expect(totalTime).to.be.lessThan(1000); // Should complete all in less than 1 second
170171
results.forEach((result) => {
171-
expect(result.body.length).to.equal(1024);
172+
expect(result.body.length).to.equal(1 * KILOBYTE);
172173
});
173174
});
174175

@@ -177,7 +178,7 @@ describe('HTTP/HTTPS Performance Tests', () => {
177178
const startTime = Date.now();
178179

179180
// Simulate mixed operations
180-
const sizes = [1024, 1024 * 1024, 10 * 1024 * 1024]; // 1KB, 1MB, 10MB
181+
const sizes = [1 * KILOBYTE, 1 * MEGABYTE, 10 * MEGABYTE];
181182

182183
for (let i = 0; i < 9; i++) {
183184
const request = new Promise((resolve) => {
@@ -226,7 +227,7 @@ describe('HTTP/HTTPS Performance Tests', () => {
226227
const processingTime = endTime - startTime;
227228

228229
expect(processingTime).to.be.lessThan(100); // Should handle errors quickly
229-
expect(memoryIncrease).to.be.lessThan(2048); // Should not leak memory (allow for GC timing)
230+
expect(memoryIncrease).to.be.lessThan(2 * KILOBYTE); // Should not leak memory (allow for GC timing)
230231
});
231232

232233
it('should handle malformed requests efficiently', async () => {
@@ -239,7 +240,7 @@ describe('HTTP/HTTPS Performance Tests', () => {
239240
headers: {
240241
'content-type': 'application/x-git-receive-pack-request',
241242
},
242-
body: Buffer.alloc(1024),
243+
body: Buffer.alloc(1 * KILOBYTE),
243244
};
244245

245246
// Simulate validation
@@ -256,24 +257,24 @@ describe('HTTP/HTTPS Performance Tests', () => {
256257
const startMemory = process.memoryUsage().heapUsed;
257258

258259
// Simulate processing with cleanup
259-
const data = Buffer.alloc(10 * 1024 * 1024); // 10MB
260+
const data = Buffer.alloc(10 * MEGABYTE);
260261
const _processedData = Buffer.concat([data]);
261262

262263
// Simulate cleanup
263264
data.fill(0); // Clear buffer
264265
const cleanedMemory = process.memoryUsage().heapUsed;
265266

266-
expect(_processedData.length).to.equal(10 * 1024 * 1024);
267+
expect(_processedData.length).to.equal(10 * MEGABYTE);
267268
// Memory should be similar to start (allowing for GC timing)
268-
expect(cleanedMemory - startMemory).to.be.lessThan(5 * 1024 * 1024);
269+
expect(cleanedMemory - startMemory).to.be.lessThan(5 * MEGABYTE);
269270
});
270271

271272
it('should handle multiple cleanup cycles without memory growth', async () => {
272273
const initialMemory = process.memoryUsage().heapUsed;
273274

274275
// Simulate multiple processing cycles
275276
for (let i = 0; i < 5; i++) {
276-
const data = Buffer.alloc(5 * 1024 * 1024); // 5MB
277+
const data = Buffer.alloc(5 * MEGABYTE);
277278
const _processedData = Buffer.concat([data]);
278279
data.fill(0); // Cleanup
279280

@@ -287,7 +288,7 @@ describe('HTTP/HTTPS Performance Tests', () => {
287288
const memoryGrowth = finalMemory - initialMemory;
288289

289290
// Memory growth should be minimal
290-
expect(memoryGrowth).to.be.lessThan(10 * 1024 * 1024); // Less than 10MB growth
291+
expect(memoryGrowth).to.be.lessThan(10 * MEGABYTE); // Less than 10MB growth
291292
});
292293
});
293294

@@ -298,7 +299,7 @@ describe('HTTP/HTTPS Performance Tests', () => {
298299
// Simulate config loading
299300
const testConfig = {
300301
proxy: { port: 8000, host: 'localhost' },
301-
limits: { maxPackSizeBytes: 1024 * 1024 * 1024 },
302+
limits: { maxPackSizeBytes: 1 * GIGABYTE },
302303
};
303304

304305
const endTime = Date.now();
@@ -315,7 +316,7 @@ describe('HTTP/HTTPS Performance Tests', () => {
315316
// Simulate config validation
316317
const testConfig = {
317318
proxy: { port: 8000 },
318-
limits: { maxPackSizeBytes: 1024 * 1024 * 1024 },
319+
limits: { maxPackSizeBytes: 1 * GIGABYTE },
319320
};
320321
const isValid = testConfig.proxy.port > 0 && testConfig.limits.maxPackSizeBytes > 0;
321322

test/ssh/integration.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const ssh2 = require('ssh2');
66
const config = require('../../src/config');
77
const db = require('../../src/db');
88
const chain = require('../../src/proxy/chain');
9+
const { MEGABYTE } = require('../../src/constants');
910
const SSHServer = require('../../src/proxy/ssh/server').default;
1011

1112
describe('SSH Pack Data Capture Integration Tests', () => {
@@ -63,7 +64,7 @@ describe('SSH Pack Data Capture Integration Tests', () => {
6364
// Stub dependencies
6465
sinon.stub(config, 'getSSHConfig').callsFake(mockConfig.getSSHConfig);
6566
sinon.stub(config, 'getProxyUrl').callsFake(mockConfig.getProxyUrl);
66-
sinon.stub(config, 'getMaxPackSizeBytes').returns(500 * 1024 * 1024);
67+
sinon.stub(config, 'getMaxPackSizeBytes').returns(500 * MEGABYTE);
6768
sinon.stub(db, 'findUserBySSHKey').callsFake(mockDb.findUserBySSHKey);
6869
sinon.stub(db, 'findUser').callsFake(mockDb.findUser);
6970
sinon.stub(chain.default, 'executeChain').callsFake(mockChain.executeChain);
@@ -147,7 +148,7 @@ describe('SSH Pack Data Capture Integration Tests', () => {
147148
// Simulate large but acceptable pack data (100MB)
148149
const dataHandler = mockStream.on.withArgs('data').firstCall?.args[1];
149150
if (dataHandler) {
150-
const largePack = Buffer.alloc(100 * 1024 * 1024, 'pack-data');
151+
const largePack = Buffer.alloc(100 * MEGABYTE, 'pack-data');
151152
dataHandler(largePack);
152153
}
153154

@@ -164,7 +165,7 @@ describe('SSH Pack Data Capture Integration Tests', () => {
164165
// Simulate oversized pack data (600MB)
165166
const dataHandler = mockStream.on.withArgs('data').firstCall?.args[1];
166167
if (dataHandler) {
167-
const oversizedPack = Buffer.alloc(600 * 1024 * 1024, 'oversized-pack');
168+
const oversizedPack = Buffer.alloc(600 * MEGABYTE, 'oversized-pack');
168169
dataHandler(oversizedPack);
169170
}
170171

0 commit comments

Comments
 (0)