Skip to content

Commit

Permalink
feat(proxy): support @satorijs/proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Nov 6, 2023
1 parent d1207d5 commit 130c3a4
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 8 deletions.
2 changes: 2 additions & 0 deletions packages/proxy/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
tsconfig.tsbuildinfo
33 changes: 33 additions & 0 deletions packages/proxy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@satorijs/proxy",
"description": "Proxy plugin for cordis",
"version": "1.0.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/satorijs/satori.git",
"directory": "packages/proxy"
},
"bugs": {
"url": "https://github.com/satorijs/satori/issues"
},
"homepage": "https://github.com/satorijs/satori/tree/master/packages/proxy",
"keywords": [
"cordis",
"router",
"http",
"proxy",
"server",
"service"
],
"peerDependencies": {
"@satorijs/satori": "^3.1.5"
}
}
43 changes: 43 additions & 0 deletions packages/proxy/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Context, Quester, Schema } from '@satorijs/core'
import {} from '@satorijs/router'
import internal from 'stream'

declare module '@satorijs/core' {
interface Context {
'router.proxy': ProxyService
}
}

class ProxyService {
static inject = ['router']

constructor(protected ctx: Context, public config: ProxyService.Config) {
const logger = ctx.logger('proxy')

ctx.router.get(config.path + '/:url(.*)', async (koa) => {
logger.debug(koa.params.url)
koa.header['Access-Control-Allow-Origin'] = ctx.router.config.selfUrl || '*'
try {
koa.body = await ctx.http.get<internal.Readable>(koa.params.url, { responseType: 'stream' })
} catch (error) {
if (!Quester.isAxiosError(error) || !error.response) throw error
koa.status = error.response.status
koa.body = error.response.data
}
})

ctx.root.provide('router.proxy', this)
}
}

namespace ProxyService {
export interface Config {
path?: string
}

export const Config: Schema<Config> = Schema.object({
path: Schema.string().default('/proxy'),
})
}

export default ProxyService
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",
],
}
8 changes: 0 additions & 8 deletions packages/router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@
"version": "1.1.2",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"exports": {
".": {
"node": "./lib/index.js",
"browser": "./lib/index.mjs",
"types": "./lib/index.d.ts"
},
"./package.json": "./package.json"
},
"files": [
"lib",
"src"
Expand Down

0 comments on commit 130c3a4

Please sign in to comment.