Skip to content

Commit 957fd6b

Browse files
committed
feat: initial commit
0 parents  commit 957fd6b

File tree

11 files changed

+378
-0
lines changed

11 files changed

+378
-0
lines changed

.github/workflows/docker.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Docker Build
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
workflow_dispatch:
8+
9+
permissions:
10+
packages: write
11+
contents: write
12+
13+
env:
14+
IMAGE_TAG: ghcr.io/yeecord/api-proxy:latest
15+
BUN_VERSION: 1.1.38
16+
17+
jobs:
18+
docker:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- uses: actions/checkout@v4
22+
23+
- name: Log in to ghcr
24+
uses: docker/login-action@v3
25+
with:
26+
registry: ghcr.io
27+
username: ${{ github.actor }}
28+
password: ${{ secrets.GITHUB_TOKEN }}
29+
30+
- uses: docker/setup-buildx-action@v3
31+
32+
- name: Build and push Docker image
33+
uses: docker/build-push-action@v6
34+
with:
35+
context: .
36+
push: true
37+
tags: ${{ env.IMAGE_TAG }}
38+
cache-from: type=gha
39+
cache-to: type=gha,mode=max
40+
file: action.Dockerfile
41+
build-args: |
42+
BUN_VERSION=${{ env.BUN_VERSION }}
43+
secrets: |
44+
"NPM_TOKEN=${{ secrets.GITHUB_TOKEN }}"

.gitignore

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
2+
3+
# Logs
4+
5+
logs
6+
_.log
7+
npm-debug.log_
8+
yarn-debug.log*
9+
yarn-error.log*
10+
lerna-debug.log*
11+
.pnpm-debug.log*
12+
13+
# Caches
14+
15+
.cache
16+
17+
# Diagnostic reports (https://nodejs.org/api/report.html)
18+
19+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
20+
21+
# Runtime data
22+
23+
pids
24+
_.pid
25+
_.seed
26+
*.pid.lock
27+
28+
# Directory for instrumented libs generated by jscoverage/JSCover
29+
30+
lib-cov
31+
32+
# Coverage directory used by tools like istanbul
33+
34+
coverage
35+
*.lcov
36+
37+
# nyc test coverage
38+
39+
.nyc_output
40+
41+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
42+
43+
.grunt
44+
45+
# Bower dependency directory (https://bower.io/)
46+
47+
bower_components
48+
49+
# node-waf configuration
50+
51+
.lock-wscript
52+
53+
# Compiled binary addons (https://nodejs.org/api/addons.html)
54+
55+
build/Release
56+
57+
# Dependency directories
58+
59+
node_modules/
60+
jspm_packages/
61+
62+
# Snowpack dependency directory (https://snowpack.dev/)
63+
64+
web_modules/
65+
66+
# TypeScript cache
67+
68+
*.tsbuildinfo
69+
70+
# Optional npm cache directory
71+
72+
.npm
73+
74+
# Optional eslint cache
75+
76+
.eslintcache
77+
78+
# Optional stylelint cache
79+
80+
.stylelintcache
81+
82+
# Microbundle cache
83+
84+
.rpt2_cache/
85+
.rts2_cache_cjs/
86+
.rts2_cache_es/
87+
.rts2_cache_umd/
88+
89+
# Optional REPL history
90+
91+
.node_repl_history
92+
93+
# Output of 'npm pack'
94+
95+
*.tgz
96+
97+
# Yarn Integrity file
98+
99+
.yarn-integrity
100+
101+
# dotenv environment variable files
102+
103+
.env
104+
.env.development.local
105+
.env.test.local
106+
.env.production.local
107+
.env.local
108+
109+
# parcel-bundler cache (https://parceljs.org/)
110+
111+
.parcel-cache
112+
113+
# Next.js build output
114+
115+
.next
116+
out
117+
118+
# Nuxt.js build / generate output
119+
120+
.nuxt
121+
dist
122+
123+
# Gatsby files
124+
125+
# Comment in the public line in if your project uses Gatsby and not Next.js
126+
127+
# https://nextjs.org/blog/next-9-1#public-directory-support
128+
129+
# public
130+
131+
# vuepress build output
132+
133+
.vuepress/dist
134+
135+
# vuepress v2.x temp and cache directory
136+
137+
.temp
138+
139+
# Docusaurus cache and generated files
140+
141+
.docusaurus
142+
143+
# Serverless directories
144+
145+
.serverless/
146+
147+
# FuseBox cache
148+
149+
.fusebox/
150+
151+
# DynamoDB Local files
152+
153+
.dynamodb/
154+
155+
# TernJS port file
156+
157+
.tern-port
158+
159+
# Stores VSCode versions used for testing VSCode extensions
160+
161+
.vscode-test
162+
163+
# yarn v2
164+
165+
.yarn/cache
166+
.yarn/unplugged
167+
.yarn/build-state.yml
168+
.yarn/install-state.gz
169+
.pnp.*
170+
171+
# IntelliJ based IDEs
172+
.idea
173+
174+
# Finder (MacOS) folder config
175+
.DS_Store

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.tsdk": "node_modules/typescript/lib"
3+
}

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# api-proxy
2+
3+
To install dependencies:
4+
5+
```bash
6+
bun install
7+
```
8+
9+
To run:
10+
11+
```bash
12+
bun run src/index.ts
13+
```
14+
15+
This project was created using `bun init` in bun v1.1.38. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.

action.Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
ARG BUN_VERSION latest
2+
3+
FROM oven/bun:${BUN_VERSION:-1}-alpine AS base
4+
5+
WORKDIR /app
6+
7+
FROM base AS installer
8+
9+
COPY package.json bun.lockb ./
10+
11+
RUN bun install --frozen-lockfile --production
12+
13+
FROM base AS runner
14+
15+
# Install curl for healthcheck
16+
RUN apk update && apk add curl --no-cache
17+
18+
COPY --from=installer /app/node_modules node_modules
19+
COPY . .
20+
21+
ENTRYPOINT ["bun", "run"]
22+
23+
CMD ["start"]

bun.lockb

3.44 KB
Binary file not shown.

package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "api-proxy",
3+
"module": "src/index.ts",
4+
"type": "module",
5+
"devDependencies": {
6+
"@types/bun": "^1.1.14"
7+
},
8+
"scripts": {
9+
"start": "bun run src/index.ts"
10+
},
11+
"peerDependencies": {
12+
"typescript": "^5.7.2"
13+
},
14+
"dependencies": {
15+
"file-system-cache": "^3.0.0-alpha.8"
16+
}
17+
}

src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { handleApiRequest } from "./routes/api";
2+
3+
Bun.serve({
4+
fetch(request) {
5+
const url = new URL(request.url);
6+
7+
if (url.pathname.startsWith("/api/")) {
8+
return handleApiRequest(request, url);
9+
}
10+
11+
return new Response("Not found", { status: 404 });
12+
},
13+
port: 3000,
14+
});

src/routes/api.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { cache, createCacheKey } from "../utils";
2+
3+
interface CachedResponse {
4+
headers: [string, string][];
5+
body: string | null;
6+
}
7+
8+
export async function handleApiRequest(request: Request, url: URL) {
9+
url.hostname = "discord.com";
10+
url.port = "443";
11+
url.protocol = "https:";
12+
13+
request.headers.set("Host", "discord.com");
14+
15+
if (request.method !== "GET") {
16+
return fetch(url.toString(), {
17+
method: request.method,
18+
headers: request.headers,
19+
body: request.body,
20+
});
21+
}
22+
23+
const cacheKey = createCacheKey(request);
24+
25+
const existing = (await cache.get(cacheKey)) as CachedResponse | undefined;
26+
27+
if (existing) {
28+
return new Response(existing.body, {
29+
headers: new Headers(existing.headers),
30+
});
31+
}
32+
33+
const response = await fetch(url.toString(), {
34+
method: request.method,
35+
headers: request.headers,
36+
body: request.body,
37+
});
38+
39+
const content = response.status === 204 ? null : await response.text();
40+
41+
await cache.set(cacheKey, {
42+
headers: [...response.headers.entries()],
43+
body: content,
44+
} satisfies CachedResponse);
45+
46+
return new Response(content, {
47+
headers: response.headers,
48+
});
49+
}

src/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Cache } from "file-system-cache";
2+
3+
export function createCacheKey(request: Request) {
4+
return `${request.method} ${request.url} ${request.headers.get(
5+
"Authorization"
6+
)}`;
7+
}
8+
9+
export const cache = new Cache({
10+
hash: "sha1",
11+
ttl: 60 * 5,
12+
});

tsconfig.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"compilerOptions": {
3+
// Enable latest features
4+
"lib": ["ESNext", "DOM", "DOM.Iterable"],
5+
"target": "ESNext",
6+
"module": "ESNext",
7+
"moduleDetection": "force",
8+
9+
// Bundler mode
10+
"moduleResolution": "bundler",
11+
"allowImportingTsExtensions": true,
12+
"verbatimModuleSyntax": true,
13+
"noEmit": true,
14+
15+
// Best practices
16+
"strict": true,
17+
"skipLibCheck": true,
18+
"noFallthroughCasesInSwitch": true,
19+
"strictNullChecks": true,
20+
21+
// Some stricter flags (disabled by default)
22+
"noUnusedLocals": false,
23+
"noUnusedParameters": false,
24+
"noPropertyAccessFromIndexSignature": false
25+
}
26+
}

0 commit comments

Comments
 (0)