From 130c3a4ccd6e0e3daea2d28d1d154c766501f4fd Mon Sep 17 00:00:00 2001 From: Shigma Date: Tue, 7 Nov 2023 02:47:46 +0800 Subject: [PATCH] feat(proxy): support @satorijs/proxy --- packages/proxy/.npmignore | 2 ++ packages/proxy/package.json | 33 +++++++++++++++++++++++++++ packages/proxy/src/index.ts | 43 ++++++++++++++++++++++++++++++++++++ packages/proxy/tsconfig.json | 12 ++++++++++ packages/router/package.json | 8 ------- 5 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 packages/proxy/.npmignore create mode 100644 packages/proxy/package.json create mode 100644 packages/proxy/src/index.ts create mode 100644 packages/proxy/tsconfig.json diff --git a/packages/proxy/.npmignore b/packages/proxy/.npmignore new file mode 100644 index 00000000..7e5fcbc1 --- /dev/null +++ b/packages/proxy/.npmignore @@ -0,0 +1,2 @@ +.DS_Store +tsconfig.tsbuildinfo diff --git a/packages/proxy/package.json b/packages/proxy/package.json new file mode 100644 index 00000000..57546b0d --- /dev/null +++ b/packages/proxy/package.json @@ -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 ", + "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" + } +} diff --git a/packages/proxy/src/index.ts b/packages/proxy/src/index.ts new file mode 100644 index 00000000..76df1ffb --- /dev/null +++ b/packages/proxy/src/index.ts @@ -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(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 = Schema.object({ + path: Schema.string().default('/proxy'), + }) +} + +export default ProxyService diff --git a/packages/proxy/tsconfig.json b/packages/proxy/tsconfig.json new file mode 100644 index 00000000..6f11f324 --- /dev/null +++ b/packages/proxy/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "../../tsconfig.base", + "compilerOptions": { + "rootDir": "src", + "outDir": "lib", + "strict": true, + "noImplicitAny": false, + }, + "include": [ + "src", + ], +} diff --git a/packages/router/package.json b/packages/router/package.json index 9ebd538d..c7845ad4 100644 --- a/packages/router/package.json +++ b/packages/router/package.json @@ -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"