Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | import { serve } from '@hono/node-server';
import { Hono } from 'hono';
import { logger } from 'hono/logger';
import { cors } from 'hono/cors';
import { prettyJSON } from 'hono/pretty-json';
import httpProxy from 'http-proxy';
import { Context } from 'hono';
import { openApiStore } from './store/openApiStore.js';
import { IncomingMessage, ServerResponse, createServer, Server } from 'node:http';
import { Agent } from 'node:https';
import chalk from 'chalk';
export interface ServerOptions {
target: string;
proxyPort: number;
docsPort: number;
apiKey?: string;
verbose?: boolean;
}
export async function startServers(options: ServerOptions): Promise<{ proxyServer: Server; docsServer: Server }> {
// Set the target URL in the OpenAPI store
openApiStore.setTargetUrl(options.target);
// Create two separate Hono apps
const proxyApp = new Hono();
const docsApp = new Hono();
// Create proxy server
const proxy = httpProxy.createProxyServer({
changeOrigin: true,
secure: false,
selfHandleResponse: true,
target: options.target,
headers: {
'Host': new URL(options.target).host
},
agent: new Agent({
rejectUnauthorized: false
})
});
// Set up error handlers
proxy.on('error', (err) => {
console.error('Proxy error:', err);
});
proxy.on('proxyReq', (proxyReq, req, res) => {
// Ensure we're using the correct protocol
proxyReq.protocol = new URL(options.target).protocol;
});
// Middleware for both apps
if (options.verbose) {
proxyApp.use('*', logger());
docsApp.use('*', logger());
}
proxyApp.use('*', cors());
proxyApp.use('*', prettyJSON());
docsApp.use('*', cors());
docsApp.use('*', prettyJSON());
// Documentation endpoints
docsApp.get('/docs', async (c: Context) => {
const spec = openApiStore.getOpenAPISpec();
return c.html(`
<!DOCTYPE html>
<html>
<head>
<title>API Documentation</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<script
id="api-reference"
data-url="/openapi.json"
data-proxy-url="https://proxy.scalar.com"></script>
<script>
var configuration = {
theme: 'light',
title: 'API Documentation'
}
document.getElementById('api-reference').dataset.configuration =
JSON.stringify(configuration)
</script>
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
</body>
</html>
`);
});
docsApp.get('/openapi.json', (c: Context) => {
return c.json(openApiStore.getOpenAPISpec());
});
docsApp.get('/openapi.yaml', (c: Context) => {
return c.text(openApiStore.getOpenAPISpecAsYAML());
});
docsApp.get('/har', (c: Context) => {
return c.json(openApiStore.generateHAR());
});
// Proxy all requests
proxyApp.all('*', async (c: Context) => {
let requestBody: any;
let responseBody: any;
// Get request body if present
if (c.req.method !== 'GET' && c.req.method !== 'HEAD') {
try {
requestBody = await c.req.json();
} catch (e) {
// Body might not be JSON
requestBody = await c.req.text();
}
}
try {
// Create a new request object with the target URL
const targetUrl = new URL(c.req.path, options.target);
// Copy query parameters
const originalUrl = new URL(c.req.url);
originalUrl.searchParams.forEach((value, key) => {
targetUrl.searchParams.append(key, value);
});
const proxyReq = new Request(targetUrl.toString(), {
method: c.req.method,
headers: new Headers({
'content-type': c.req.header('content-type') || 'application/json',
'accept': c.req.header('accept') || 'application/json',
...Object.fromEntries(
Object.entries(c.req.header())
.filter(([key]) => !['content-type', 'accept'].includes(key.toLowerCase()))
),
}),
body: c.req.method !== 'GET' && c.req.method !== 'HEAD' ? requestBody : undefined,
});
// Forward the request to the target server
const proxyRes = await fetch(proxyReq);
// Get response body
const contentType = proxyRes.headers.get('content-type') || '';
if (contentType.includes('application/json')) {
responseBody = await proxyRes.json();
} else {
responseBody = await proxyRes.text();
}
// Record the API call in OpenAPI format
openApiStore.recordEndpoint(
c.req.path,
c.req.method.toLowerCase(),
{
query: Object.fromEntries(new URL(c.req.url).searchParams),
body: requestBody,
contentType: c.req.header('content-type') || 'application/json',
headers: Object.fromEntries(Object.entries(c.req.header()))
},
{
status: proxyRes.status,
body: responseBody,
contentType: proxyRes.headers.get('content-type') || 'application/json',
headers: Object.fromEntries(proxyRes.headers.entries())
}
);
// Create a new response with the correct content type and body
return new Response(JSON.stringify(responseBody), {
status: proxyRes.status,
headers: Object.fromEntries(proxyRes.headers.entries())
});
} catch (error: any) {
console.error('Proxy request failed:', error);
return c.json({ error: 'Proxy error', details: error.message }, 500);
}
});
// Add API key if provided
if (options.apiKey) {
proxy.on('proxyReq', (proxyReq) => {
proxyReq.setHeader('Authorization', `Bearer ${options.apiKey}`);
});
}
// Function to check if a port is available
async function isPortAvailable(port: number): Promise<boolean> {
return new Promise((resolve) => {
const server = createServer()
.once('error', () => {
resolve(false);
})
.once('listening', () => {
server.close();
resolve(true);
})
.listen(port);
});
}
// Function to find an available port
async function findAvailablePort(startPort: number): Promise<number> {
let port = startPort;
while (!(await isPortAvailable(port))) {
port++;
}
return port;
}
// Start servers
const availableProxyPort = await findAvailablePort(options.proxyPort);
const availableDocsPort = await findAvailablePort(options.docsPort);
if (availableProxyPort !== options.proxyPort) {
console.log(chalk.yellow(`Port ${options.proxyPort} is in use, using port ${availableProxyPort} instead`));
}
if (availableDocsPort !== options.docsPort) {
console.log(chalk.yellow(`Port ${options.docsPort} is in use, using port ${availableDocsPort} instead`));
}
console.log(chalk.blue(`Starting proxy server on port ${availableProxyPort}...`));
console.log(chalk.gray(`Proxying requests to: ${options.target}`));
console.log(chalk.blue(`Starting documentation server on port ${availableDocsPort}...`));
const proxyServer = createServer(async (req, res) => {
try {
const url = new URL(req.url || '/', `http://localhost:${availableProxyPort}`);
const request = new Request(url.toString(), {
method: req.method || 'GET',
headers: req.headers as Record<string, string>,
body: req.method !== 'GET' && req.method !== 'HEAD' ? req : undefined,
});
const response = await proxyApp.fetch(request);
res.statusCode = response.status;
res.statusMessage = response.statusText;
// Copy all headers from the response
for (const [key, value] of response.headers.entries()) {
res.setHeader(key, value);
}
// Stream the response body
if (response.body) {
const reader = response.body.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
res.write(value);
}
res.end();
} else {
res.end();
}
} catch (error: any) {
console.error('Proxy request failed:', error);
res.statusCode = 500;
res.end(JSON.stringify({ error: 'Proxy error', details: error.message }));
}
});
const docsServer = createServer(async (req, res) => {
try {
const url = new URL(req.url || '/', `http://localhost:${availableDocsPort}`);
const request = new Request(url.toString(), {
method: req.method || 'GET',
headers: req.headers as Record<string, string>,
body: req.method !== 'GET' && req.method !== 'HEAD' ? req : undefined,
});
const response = await docsApp.fetch(request);
res.statusCode = response.status;
res.statusMessage = response.statusText;
for (const [key, value] of response.headers.entries()) {
res.setHeader(key, value);
}
if (response.body) {
const reader = response.body.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
res.write(value);
}
}
res.end();
} catch (error: any) {
console.error('Documentation request failed:', error);
res.statusCode = 500;
res.end(JSON.stringify({ error: 'Documentation error', details: error.message }));
}
});
await new Promise<void>((resolve, reject) => {
proxyServer.once('error', reject);
proxyServer.listen(availableProxyPort, '0.0.0.0', () => {
console.log(chalk.green(`✓ Proxy server running on port ${availableProxyPort}`));
resolve();
});
});
await new Promise<void>((resolve, reject) => {
docsServer.once('error', reject);
docsServer.listen(availableDocsPort, '0.0.0.0', () => {
console.log(chalk.green(`✓ Documentation server running on port ${availableDocsPort}`));
resolve();
});
});
// Print startup message
console.log('\n' + chalk.green('Arbiter is running! 🚀'));
console.log('\n' + chalk.bold('Proxy Server:'));
console.log(chalk.cyan(` URL: http://localhost:${availableProxyPort}`));
console.log(chalk.gray(` Target: ${options.target}`));
console.log('\n' + chalk.bold('Documentation:'));
console.log(chalk.cyan(` API Reference: http://localhost:${availableDocsPort}/docs`));
console.log('\n' + chalk.bold('Exports:'));
console.log(chalk.cyan(` HAR Export: http://localhost:${availableDocsPort}/har`));
console.log(chalk.cyan(` OpenAPI JSON: http://localhost:${availableDocsPort}/openapi.json`));
console.log(chalk.cyan(` OpenAPI YAML: http://localhost:${availableDocsPort}/openapi.yaml`));
console.log('\n' + chalk.yellow('Press Ctrl+C to stop'));
return { proxyServer, docsServer };
} |