youzanyun-devtool-worker
Version:
47 lines (46 loc) • 1.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const spring4js_nodejs_1 = require("spring4js-nodejs");
const axios_1 = tslib_1.__importDefault(require("axios"));
let BuiltInAppActionService = class BuiltInAppActionService {
async start() {
this.webPort = this.configService.getWebPort();
}
async processRequest(ctx, next) {
const { request: { href }, headers, } = ctx;
if (/^https:\/\/www\.youzan\.com\/v4\/apps\/app-container/.test(href)) {
try {
const res = await axios_1.default.get(href, {
headers: Object.assign(Object.assign({}, headers), { "accept-encoding": "gzip, deflate" }),
responseType: "text",
});
ctx.status = res.status;
const scriptText = "<script>window._global.isEmbed = true;</script></head>";
const result = res.data.replace("</head>", scriptText);
ctx.set(res.headers);
ctx.body = result;
}
catch (e) {
console.log("err", e);
ctx.body = "";
}
return true;
}
if (/^https:\/\/.*\.isv\.youzan\.com/.test(href)) {
const { data: { data: { target }, }, } = await axios_1.default.get(`http://127.0.0.1:${this.webPort}/api/built-in-app/get-target`);
if (!/^https:\/\/.*\.isv\.youzan\.com/.test(target)) {
ctx.redirect(target);
return true;
}
}
return false;
}
};
tslib_1.__decorate([
(0, spring4js_nodejs_1.Resource)()
], BuiltInAppActionService.prototype, "configService", void 0);
BuiltInAppActionService = tslib_1.__decorate([
(0, spring4js_nodejs_1.Service)()
], BuiltInAppActionService);
exports.default = BuiltInAppActionService;