Skip to content

Commit

Permalink
proxy package (#8)
Browse files Browse the repository at this point in the history
* proxy package

* aborted
  • Loading branch information
MARCROCK22 authored Nov 20, 2024
1 parent 3cb6192 commit c009705
Show file tree
Hide file tree
Showing 22 changed files with 417 additions and 195 deletions.
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://biomejs.dev/schemas/1.9.1/schema.json",
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"vcs": {
"enabled": true,
"clientKind": "git",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"check": "turbo run --concurrency 32 lint format build test checkb"
},
"devDependencies": {
"@biomejs/biome": "1.9.1",
"@biomejs/biome": "1.9.4",
"turbo": "^2.3.0",
"vitest": "^2.1.5"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/chartjs/biome.json
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"]
}
2 changes: 1 addition & 1 deletion packages/chartjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"typescript": "^5.6.3"
},
"dependencies": {
"@napi-rs/canvas": "^0.1.61",
"@napi-rs/canvas": "^0.1.62",
"chart.js": "^4.4.6"
}
}
2 changes: 1 addition & 1 deletion packages/cooldown/biome.json
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"]
}
2 changes: 1 addition & 1 deletion packages/cooldown/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
"typescript": "^5.6.3"
},
"dependencies": {
"seyfert": "2.1.1-dev-11882952397.0"
"seyfert": "2.1.1-dev-11916234323.0"
}
}
2 changes: 1 addition & 1 deletion packages/generic-adapter/biome.json
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"]
}
2 changes: 1 addition & 1 deletion packages/generic-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"checkb": "biome check --write --no-errors-on-unmatched ./src"
},
"dependencies": {
"seyfert": "2.1.1-dev-11882952397.0",
"seyfert": "2.1.1-dev-11916234323.0",
"tweetnacl": "^1.0.3"
},
"devDependencies": {
Expand Down
11 changes: 11 additions & 0 deletions packages/proxy/README.md
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?...
});
```
4 changes: 4 additions & 0 deletions packages/proxy/biome.json
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"]
}
30 changes: 30 additions & 0 deletions packages/proxy/package.json
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"
}
}
126 changes: 126 additions & 0 deletions packages/proxy/src/adapter.ts
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);
}
1 change: 1 addition & 0 deletions packages/proxy/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { createProxy } from './adapter';
34 changes: 34 additions & 0 deletions packages/proxy/tsconfig.json
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"
],
}
2 changes: 1 addition & 1 deletion packages/redis-adapter/biome.json
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"]
}
2 changes: 1 addition & 1 deletion packages/redis-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"dependencies": {
"@redis/client": "^1.6.0",
"seyfert": "2.1.1-dev-11882952397.0"
"seyfert": "2.1.1-dev-11916234323.0"
},
"devDependencies": {
"@types/node": "^22.9.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/uws-adapter/biome.json
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"]
}
4 changes: 2 additions & 2 deletions packages/uws-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"checkb": "biome check --write --no-errors-on-unmatched ./src"
},
"dependencies": {
"seyfert": "2.1.1-dev-11882952397.0",
"seyfert": "2.1.1-dev-11916234323.0",
"tweetnacl": "^1.0.3",
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.47.0"
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.49.0"
},
"devDependencies": {
"@types/node": "^22.9.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/watcher/biome.json
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"]
}
2 changes: 1 addition & 1 deletion packages/watcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"dependencies": {
"chokidar": "^4.0.1",
"seyfert": "2.1.1-dev-11882952397.0",
"seyfert": "2.1.1-dev-11916234323.0",
"tweetnacl": "^1.0.3"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/webhooks/biome.json
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"]
}
Loading

0 comments on commit c009705

Please sign in to comment.