Charonless is a simple, lightweight, and easy-to-use library for creating HTTP proxy server in serverless environments. It use basic Fetch API to proxy requests and responses.
Charonless provides a single function charon to create a fetch handler. It is a
high-level function that takes a default configuration object for handling invalid requests, and a list of testers for proxying requests. The tester is a function that takes a request and returns a proxy configuration object.
Example (serverless env with Bun.js):
import {charon} from 'charonless';
Bun.serve({
fetch(req) {
return charon(
{
host: '192.168.1.1',
port: 2000,
},
(req) => {
if (req.location.host === 'a.example.com') {
return {
host: '192.168.1.2',
port: 2001,
}
}
},
() => {
if (req.location.host === 'b.example.com') {
return {
host: '192.168.1.3',
port: 2002,
}
}
}
)(req);
}
})
The proxy behavior is defined through the BackendConfig interface, which allows fine-grained control over request forwarding and response handling. Below are the configuration options available:
type BackendConfig = {
host: string;
port?: number;
ssl?: boolean;
followRedirects?: boolean;
request?: {
path?: (p: string[]) => string[];
headers?: (o: Record<string, string>) => Record<string, string>;
query?: (o: Array<[string, string]>) => Array<[string, string]>;
json?: (o: any) => Object | undefined;
};
response?: {
headers?: (o: Record<string, string>) => Record<string, string>;
status?: number;
body?: (b?: ReadableStream<Uint8Array>) => ReadableStream<Uint8Array> | string;
map?: (r?: Response) => Response;
};
};
Key Features Explanation:
host and port define the target backend server location
ssl if true then use https or else use http (default: false)
Use request object methods to:
The response object allows:
This structure provides both basic routing and advanced middleware capabilities for proxy operations.