-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
102 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "@cordisjs/plugin-server-proxy", | ||
"description": "Proxy plugin for cordis", | ||
"version": "0.2.0", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"files": [ | ||
"lib", | ||
"src" | ||
], | ||
"author": "Shigma <[email protected]>", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/cordiverse/server.git", | ||
"directory": "packages/proxy" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/cordiverse/server/issues" | ||
}, | ||
"homepage": "https://github.com/cordiverse/server/tree/main/packages/proxy", | ||
"keywords": [ | ||
"cordis", | ||
"router", | ||
"http", | ||
"proxy", | ||
"server", | ||
"service", | ||
"plugin" | ||
] | ||
} |
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,56 @@ | ||
import { Context, Service } from 'cordis' | ||
import {} from '@cordisjs/plugin-http' | ||
import {} from '@cordisjs/plugin-server' | ||
import { Readable } from 'node:stream' | ||
import { sanitize } from 'cosmokit' | ||
import Schema from 'schemastery' | ||
|
||
declare module 'cordis' { | ||
interface Context { | ||
'server.proxy': ProxyServer | ||
} | ||
|
||
namespace Context { | ||
interface Server { | ||
proxy: ProxyServer | ||
} | ||
} | ||
} | ||
|
||
class ProxyServer extends Service { | ||
static inject = ['server', 'http'] | ||
|
||
public path: string | ||
|
||
constructor(protected ctx: Context, public config: ProxyServer.Config) { | ||
super(ctx, 'server.proxy', true) | ||
|
||
const logger = ctx.logger('proxy') | ||
|
||
this.path = sanitize(config.path) | ||
|
||
ctx.server.get(this.path + '/:url(.*)', async (koa) => { | ||
logger.debug(koa.params.url) | ||
koa.header['Access-Control-Allow-Origin'] = ctx.server.config.selfUrl || '*' | ||
try { | ||
koa.body = Readable.fromWeb(await ctx.http.get(koa.params.url, { responseType: 'stream' })) | ||
} catch (error) { | ||
if (!ctx.http.isError(error) || !error.response) throw error | ||
koa.status = error.response.status | ||
koa.body = error.response.data | ||
} | ||
}) | ||
} | ||
} | ||
|
||
namespace ProxyServer { | ||
export interface Config { | ||
path: string | ||
} | ||
|
||
export const Config: Schema<Config> = Schema.object({ | ||
path: Schema.string().default('/proxy'), | ||
}) | ||
} | ||
|
||
export default ProxyServer |
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,12 @@ | ||
{ | ||
"extends": "../../tsconfig.base", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "lib", | ||
"strict": true, | ||
"noImplicitAny": false, | ||
}, | ||
"include": [ | ||
"src", | ||
], | ||
} |