UNPKG

youzanyun-devtool-worker

Version:

- web - ws - proxy

65 lines (64 loc) 2.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const axios_1 = tslib_1.__importDefault(require("axios")); const lodash_1 = require("lodash"); const spring4js_nodejs_1 = require("spring4js-nodejs"); let ActionBypassService = class ActionBypassService { async start() { this.proxy = this.configService.isUseProxy(); this.host = this.configService.getProxyHost(); this.port = this.configService.getProxyPort(); } async processRequest(ctx, next) { let hasBody = false; switch (lodash_1.toLower(ctx.method)) { case 'post': case 'patch': case 'put': hasBody = true; break; default: hasBody = false; } let axiosRes; let config = { method: ctx.method, baseURL: ctx.origin, url: ctx.url, headers: Object.assign(ctx.headers, {}), data: hasBody ? (ctx.isJsonHeader ? ctx.request.body : ctx.req) : null, responseType: "stream", validateStatus: function (status) { return true; }, maxRedirects: 0, }; if (this.proxy) { config.proxy = { host: this.host, port: this.port, protocol: 'http:' }; } try { axiosRes = await axios_1.default.request(config); ctx.status = axiosRes.status; let header = JSON.parse(JSON.stringify(axiosRes.headers)); delete header["content-length"]; ctx.set(header); ctx.body = axiosRes.data; } catch (err) { console.log('err', err); ctx.body = ''; } return true; } }; tslib_1.__decorate([ spring4js_nodejs_1.Resource() ], ActionBypassService.prototype, "configService", void 0); ActionBypassService = tslib_1.__decorate([ spring4js_nodejs_1.Service() ], ActionBypassService); exports.default = ActionBypassService;