Skip to content

Commit 2bcb475

Browse files
committed
refactor: convert SSH files from JavaScript to TypeScript
- Convert SSH server (src/proxy/ssh/server.js -> server.ts) - Convert SSH CLI tool (src/cli/ssh-key.js -> ssh-key.ts) - Add proper TypeScript types and interfaces - Install @types/ssh2 for SSH2 library types - Fix TypeScript compilation errors with type assertions - Update imports to use TypeScript files - Remove @ts-expect-error comment as no longer needed
1 parent bc0b2f6 commit 2bcb475

File tree

6 files changed

+473
-709
lines changed

6 files changed

+473
-709
lines changed

package-lock.json

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@
100100
"@types/node": "^22.18.0",
101101
"@types/react-dom": "^17.0.26",
102102
"@types/react-html-parser": "^2.0.7",
103-
"@types/validator": "^13.15.2",
104103
"@types/sinon": "^17.0.4",
104+
"@types/ssh2": "^1.15.5",
105+
"@types/validator": "^13.15.2",
105106
"@types/yargs": "^17.0.33",
106107
"@typescript-eslint/eslint-plugin": "^8.41.0",
107108
"@typescript-eslint/parser": "^8.41.0",

src/cli/ssh-key.js renamed to src/cli/ssh-key.ts

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
#!/usr/bin/env node
22

3-
const fs = require('fs');
4-
const path = require('path');
5-
const axios = require('axios');
3+
import * as fs from 'fs';
4+
import * as path from 'path';
5+
import axios from 'axios';
66

77
const API_BASE_URL = process.env.GIT_PROXY_API_URL || 'http://localhost:3000';
88
const GIT_PROXY_COOKIE_FILE = path.join(
9-
process.env.HOME || process.env.USERPROFILE,
9+
process.env.HOME || process.env.USERPROFILE || '',
1010
'.git-proxy-cookies.json',
1111
);
1212

13-
async function addSSHKey(username, keyPath) {
13+
interface ApiErrorResponse {
14+
error: string;
15+
}
16+
17+
interface ErrorWithResponse {
18+
response?: {
19+
data: ApiErrorResponse;
20+
status: number;
21+
};
22+
code?: string;
23+
message: string;
24+
}
25+
26+
async function addSSHKey(username: string, keyPath: string): Promise<void> {
1427
try {
1528
// Check for authentication
1629
if (!fs.existsSync(GIT_PROXY_COOKIE_FILE)) {
@@ -32,6 +45,7 @@ async function addSSHKey(username, keyPath) {
3245
}
3346

3447
console.log('Making API request to:', `${API_BASE_URL}/api/v1/user/${username}/ssh-keys`);
48+
3549
// Make the API request
3650
await axios.post(
3751
`${API_BASE_URL}/api/v1/user/${username}/ssh-keys`,
@@ -47,20 +61,22 @@ async function addSSHKey(username, keyPath) {
4761

4862
console.log('SSH key added successfully!');
4963
} catch (error) {
64+
const axiosError = error as ErrorWithResponse;
5065
console.error('Full error:', error);
51-
if (error.response) {
52-
console.error('Response error:', error.response.data);
53-
console.error('Response status:', error.response.status);
54-
} else if (error.code === 'ENOENT') {
66+
67+
if (axiosError.response) {
68+
console.error('Response error:', axiosError.response.data);
69+
console.error('Response status:', axiosError.response.status);
70+
} else if (axiosError.code === 'ENOENT') {
5571
console.error(`Error: Could not find SSH key file at ${keyPath}`);
5672
} else {
57-
console.error('Error:', error.message);
73+
console.error('Error:', axiosError.message);
5874
}
5975
process.exit(1);
6076
}
6177
}
6278

63-
async function removeSSHKey(username, keyPath) {
79+
async function removeSSHKey(username: string, keyPath: string): Promise<void> {
6480
try {
6581
// Check for authentication
6682
if (!fs.existsSync(GIT_PROXY_COOKIE_FILE)) {
@@ -86,12 +102,14 @@ async function removeSSHKey(username, keyPath) {
86102

87103
console.log('SSH key removed successfully!');
88104
} catch (error) {
89-
if (error.response) {
90-
console.error('Error:', error.response.data.error);
91-
} else if (error.code === 'ENOENT') {
105+
const axiosError = error as ErrorWithResponse;
106+
107+
if (axiosError.response) {
108+
console.error('Error:', axiosError.response.data.error);
109+
} else if (axiosError.code === 'ENOENT') {
92110
console.error(`Error: Could not find SSH key file at ${keyPath}`);
93111
} else {
94-
console.error('Error:', error.message);
112+
console.error('Error:', axiosError.message);
95113
}
96114
process.exit(1);
97115
}
@@ -106,8 +124,8 @@ const keyPath = args[2];
106124
if (!command || !username || !keyPath) {
107125
console.log(`
108126
Usage:
109-
Add SSH key: node ssh-key.js add <username> <path-to-public-key>
110-
Remove SSH key: node ssh-key.js remove <username> <path-to-public-key>
127+
Add SSH key: npx tsx src/cli/ssh-key.ts add <username> <path-to-public-key>
128+
Remove SSH key: npx tsx src/cli/ssh-key.ts remove <username> <path-to-public-key>
111129
`);
112130
process.exit(1);
113131
}

src/proxy/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { addUserCanAuthorise, addUserCanPush, createRepo, getRepos } from '../db
1515
import { PluginLoader } from '../plugin';
1616
import chain from './chain';
1717
import { Repo } from '../db/types';
18-
// @ts-expect-error - SSH server is a JavaScript file
1918
import SSHServer from './ssh/server';
2019

2120
const { GIT_PROXY_SERVER_PORT: proxyHttpPort, GIT_PROXY_HTTPS_SERVER_PORT: proxyHttpsPort } =

0 commit comments

Comments
 (0)