docs: improve README with comprehensive examples and protocol support info

This commit is contained in:
MythEclipse
2026-05-08 19:12:18 +07:00
parent 694f14a316
commit 124dab98a2
+89 -55
View File
@@ -1,85 +1,119 @@
# Edge Relay # 🚀 Edge Relay (Proxy Bun)
HTTP proxy untuk Vercel Edge Runtime. High-performance HTTP Proxy & Relay handler optimized for Vercel Edge Runtime, Cloudflare Workers, and Bun.
## Live Deployment ## 🌐 Live Deployment
- **Docs/Tester**: `https://proxy-bun.vercel.app/docs` | Provider | Endpoint |
- **Utama**: `https://proxy-bun.vercel.app` |----------|----------|
- **Alternatif**: `https://vercel-relay-alpha-umber.vercel.app` | **Primary (Vercel)** | `https://proxy-bun.vercel.app` |
- **Alternatif**: `https://proxy-bun-mytheclipse8647-orfq73fe.apn.leapcell.dev` | **Secondary (CF Workers)** | `https://opennext-app.superaseph.workers.dev` |
- **Alternatif**: `https://opennext-app.superaseph.workers.dev` | **Leapcell** | `https://proxy-bun-mytheclipse8647-orfq73fe.apn.leapcell.dev` |
| **Interactive Docs** | `https://proxy-bun.vercel.app/docs` |
## Cara Pakai ---
### Header yang Dibutuhkan ## 🛠 Cara Pakai
Proxy ini bekerja dengan menangkap request ke endpoint relay dan meneruskannya ke target yang ditentukan via headers.
### Required Headers
| Header | Required | Default | Deskripsi | | Header | Required | Default | Deskripsi |
|--------|----------|---------|-----------| |--------|----------|---------|-----------|
| `x-relay-target` | Yes | - | URL target yang ingin di-proxy | | `x-relay-target` | **Yes** | - | Base URL target (e.g. `https://api.openai.com`) |
| `x-relay-path` | No | `/` | Path yang ditambahkan ke target | | `x-relay-path` | No | `/` | Path tambahan (e.g. `/v1/chat/completions`) |
### Contoh ---
## 📖 Contoh Penggunaan
### 1. Simple GET Request
Mengambil data dari JSONPlaceholder.
```bash ```bash
curl -H "x-relay-target: https://jsonplaceholder.typicode.com/posts/1" https://proxy-bun.vercel.app/ curl -H "x-relay-target: https://jsonplaceholder.typicode.com/posts/1" \
```
```bash
curl -H "x-relay-target: https://api.example.com" \
-H "x-relay-path: /v1/users" \
https://proxy-bun.vercel.app/ https://proxy-bun.vercel.app/
``` ```
### HTTP Methods ### 2. POST with Body & Headers
Meneruskan API Key dan data JSON ke target.
```bash
curl -X POST \
-H "x-relay-target: https://api.example.com" \
-H "x-relay-path: /v1/data" \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{"key": "value"}' \
https://proxy-bun.vercel.app/
```
Mendukung semua HTTP methods: ### 3. Binary Data / Upload
- `GET`, `HEAD` - tanpa body Mendukung upload file via `POST`/`PUT` (streaming).
- `POST`, `PUT`, `PATCH`, `DELETE` - dengan body ```bash
curl -X PUT \
-H "x-relay-target: https://storage.com" \
-H "x-relay-path: /upload/image.png" \
--data-binary "@/path/to/image.png" \
https://proxy-bun.vercel.app/
```
### Header Handling ---
Relay headers yang di-strip sebelum forwarded: ## ⚙️ Fitur & Spesifikasi
### ⚡ Protocol Support
- **HTTP/1.1 & HTTP/2** - Full support.
- **Methods** - `GET`, `POST`, `PUT`, `PATCH`, `DELETE`, `HEAD`, `OPTIONS`.
- **Streaming** - Mendukung response streaming (Server-Sent Events / SSE) secara native.
- **CORS** - Otomatis menambahkan header `Access-Control-Allow-*` agar bisa diakses dari browser.
### 🛡️ Security & Header Handling
Relay ini bersifat transparan kecuali untuk header berikut yang di-**strip** sebelum diteruskan ke target:
- `host` (diganti dengan host target)
- `x-relay-target` - `x-relay-target`
- `x-relay-path` - `x-relay-path`
- `host`
Headers lain tetap di-pass. Semua header lain (seperti `Authorization`, `User-Agent`, `Cookie`, dsb) akan diteruskan apa adanya.
### Error Handling ### 🧪 Error Codes
| Status | Deskripsi |
|--------|-----------|
| `400` | Missing `x-relay-target` header. |
| `403` | Target domain tidak valid (jika whitelist aktif). |
| `502` | Target gagal dihubungi / Bad Gateway. |
```json ---
{
"error": "Missing x-relay-target header"
}
```
HTTP 400 jika `x-relay-target` tidak ada.
## Struktur Kode ## 📂 Struktur Project
``` ```text
proxy-bun/ src/
├── src/ ├── app/
│ ├── app/ │ ├── route.ts # Entry point proxy (Edge Handler)
│ ├── docs/page.tsx # UI / Documentation ── docs/ # UI Interactive Docs & Tester
│ │ └── route.ts # Edge API handler └── lib/
── lib/ ── relay-utils.ts # Logic filter header & request builder
── relay-utils.ts # Pure functions untuk relay logic ── utils.ts # Helper UI
│ └── relay-utils.test.ts # Unit tests
``` ```
### relay-utils.ts ## 🏗 Development
| Function | Deskripsi | Gunakan [Bun](https://bun.sh) untuk performa terbaik.
|----------|-----------|
| `normalizeTargetUrl(target, path)` | Gabung target + path, hapus trailing slash |
| `filterHeaders(headers)` | Filter relay & security headers |
| `shouldSendBody(method)` | Cek apakah method butuh body |
| `buildRelayRequest(req, url, headers)` | Bangun RequestInit untuk fetch |
| `createRelayResponse(response)` | Buat Response dari fetch result |
## Development
```bash ```bash
bun test # Run tests # Install dependencies
bun run dev # Start local Next.js server bun install
# Run dev server
bun dev
# Run unit tests
bun test
``` ```
## 🚀 Deployment (GitHub Actions)
Project ini otomatis dideploy ke Cloudflare Workers setiap ada push ke `master`.
Konfigurasi workflow ada di `.github/workflows/deploy.yml`.
---
🤖 **Powered by Bun + Next.js Edge Runtime**