generated from tiramisulabs/monorepo
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
22 changed files
with
417 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.9.1/schema.json", | ||
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json", | ||
"extends": ["../../biome.json"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.9.1/schema.json", | ||
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json", | ||
"extends": ["../../biome.json"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.9.1/schema.json", | ||
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json", | ||
"extends": ["../../biome.json"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Rest proxy using [uWebSockets.js](https://github.com/uNetworking/uWebSockets.js) | ||
|
||
```ts | ||
const { app } = await createProxy({ | ||
port: 4444, | ||
token: "MzUxNDMzMTk0MDAxNjYxOTUy........", | ||
baseUrl?... | ||
app?... | ||
rest?... | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json", | ||
"extends": ["../../biome.json"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "@slipher/proxy", | ||
"version": "0.0.1", | ||
"private": false, | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"files": [ | ||
"lib/**" | ||
], | ||
"main": "./lib/index.js", | ||
"module": "./lib/index.js", | ||
"types": "./lib/index.d.ts", | ||
"scripts": { | ||
"dev": "tsc --watch", | ||
"build": "tsc", | ||
"lint": "biome lint --write ./src", | ||
"format": "biome format --write ./src", | ||
"checkb": "biome check --write --no-errors-on-unmatched ./src" | ||
}, | ||
"dependencies": { | ||
"seyfert": "2.1.1-dev-11916234323.0", | ||
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.49.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^22.9.0", | ||
"typescript": "^5.6.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { ApiHandler, type HttpMethods, type RawFile } from 'seyfert'; | ||
import { | ||
App, | ||
type HttpRequest, | ||
type HttpResponse, | ||
type TemplatedApp, | ||
getParts, | ||
type us_listen_socket, | ||
} from 'uWebSockets.js'; | ||
export function createProxy(options: { | ||
token: string; | ||
port: number; | ||
baseUrl?: `/${string}`; | ||
app?: TemplatedApp; | ||
rest?: ApiHandler; | ||
}) { | ||
const rest = | ||
options.rest ?? | ||
new ApiHandler({ | ||
token: options.token, | ||
}); | ||
const app = options.app ?? App(); | ||
const authKey = `Bot ${options.token}`; | ||
const sliceLength = options.baseUrl ? options.baseUrl.length + 1 : 8; | ||
|
||
app.any('/*', async (res, req) => { | ||
res.onAborted(() => { | ||
res.aborted = true; | ||
}); | ||
if (res.aborted) { | ||
res.writeStatus('401'); | ||
return; | ||
} | ||
|
||
const auth = req.getHeader('authorization'); | ||
if (auth !== authKey) { | ||
res.writeStatus('401').end(''); | ||
return; | ||
} | ||
let body: undefined | Record<string, unknown>; | ||
const files: RawFile[] = []; | ||
const method = <HttpMethods>req.getMethod().toUpperCase(); | ||
const query = new URLSearchParams(req.getQuery()); | ||
const path = <`/${string}`>req.getUrl().slice(sliceLength); | ||
if (method !== 'GET' && method !== 'DELETE') { | ||
const contentType = req.getHeader('content-type'); | ||
if (contentType.includes('multipart/form-data')) { | ||
const form = await readBody(res, req); | ||
if (form) { | ||
for (let i = 0; i < form.length; i++) { | ||
const field = form[i]; | ||
if (field.name === 'payload_json') { | ||
body = JSON.parse(Buffer.from(field.data).toString()); | ||
} else { | ||
files.push({ | ||
filename: field.filename!, | ||
data: field.data, | ||
}); | ||
} | ||
} | ||
} | ||
} else body = await readJson(res); | ||
} | ||
try { | ||
const reason = req.getHeader('x-audit-log-reason'); | ||
const result = await rest.request(method, path, { | ||
body, | ||
files, | ||
query, | ||
reason, | ||
}); | ||
if (!res.aborted) | ||
res.cork(() => { | ||
res.writeHeader('content-type', 'application/json').end(JSON.stringify(result)); | ||
}); | ||
} catch (e) { | ||
const message = typeof e === 'object' && e && 'message' in e ? (e.message as string) : String(e); | ||
if (!res.aborted) | ||
res.cork(() => { | ||
res | ||
.writeStatus(message.match(/\[[0-9]{1,3}/g)?.[0].slice(1) ?? '500') | ||
.writeHeader('content-type', 'application/json') | ||
.end(JSON.stringify({ message })); | ||
}); | ||
} | ||
}); | ||
|
||
return new Promise<{ result: us_listen_socket | false; app: TemplatedApp }>(r => { | ||
app.listen(options.port, result => { | ||
r({ result, app }); | ||
}); | ||
}); | ||
} | ||
|
||
export function readBuffer(res: HttpResponse) { | ||
return new Promise<Buffer>((ok, rej) => { | ||
const buffers: Buffer[] = []; | ||
res.onData((ab, isLast) => { | ||
const chunk = Buffer.from(ab); | ||
if (isLast) { | ||
try { | ||
buffers.push(chunk); | ||
ok(Buffer.concat(buffers)); | ||
} catch (e) { | ||
res.close(); | ||
return; | ||
} | ||
} else { | ||
buffers.push(chunk); | ||
} | ||
}); | ||
|
||
res.onAborted(rej); | ||
}); | ||
} | ||
|
||
export async function readJson<T extends Record<string, any>>(res: HttpResponse): Promise<T> { | ||
const buffer = await readBuffer(res); | ||
return JSON.parse(buffer.toString()); | ||
} | ||
|
||
export async function readBody(res: HttpResponse, req: HttpRequest) { | ||
const contentType = req.getHeader('content-type'); | ||
const buffer = await readBuffer(res); | ||
return getParts(buffer, contentType); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { createProxy } from './adapter'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "CommonJS", | ||
"target": "ESNext", | ||
"lib": [ | ||
"ESNext" | ||
], | ||
"moduleResolution": "node", | ||
"declaration": true, | ||
"sourceMap": false, | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"experimentalDecorators": true, | ||
"emitDecoratorMetadata": true, | ||
"preserveConstEnums": true, | ||
/* Type Checking */ | ||
"noImplicitAny": true, | ||
"strictNullChecks": true, | ||
"strictFunctionTypes": true, | ||
"noImplicitThis": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"skipLibCheck": true, | ||
"noErrorTruncation": true, | ||
"outDir": "./lib", | ||
"stripInternal": true, | ||
}, | ||
"exclude": [ | ||
"**/lib", | ||
"**/test" | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.9.1/schema.json", | ||
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json", | ||
"extends": ["../../biome.json"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.9.1/schema.json", | ||
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json", | ||
"extends": ["../../biome.json"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.9.1/schema.json", | ||
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json", | ||
"extends": ["../../biome.json"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.9.1/schema.json", | ||
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json", | ||
"extends": ["../../biome.json"] | ||
} |
Oops, something went wrong.