Skip to content

Commit

Permalink
feat(proxy): add plugin proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 15, 2024
1 parent 1dd0847 commit 8ce351e
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ declare module 'cordis' {

namespace Context {
const Server: unique symbol
// https://github.com/typescript-eslint/typescript-eslint/issues/6720
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Server<C extends Context> {}
interface Server<C extends Context = Context> {}
}
}

Expand Down Expand Up @@ -121,7 +122,7 @@ export class Server extends KoaRouter {

ctx.on('dispose', () => {
if (config.port) {
this.ctx.logger.info('http server closing')
this.ctx.logger.info('server closing')
}
this._ws?.close()
this._http?.close()
Expand Down
31 changes: 31 additions & 0 deletions packages/proxy/package.json
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"
]
}
56 changes: 56 additions & 0 deletions packages/proxy/src/index.ts
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
12 changes: 12 additions & 0 deletions packages/proxy/tsconfig.json
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",
],
}

0 comments on commit 8ce351e

Please sign in to comment.