UNPKG

youzanyun-devtool-worker

Version:

140 lines (139 loc) 5.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const axios_1 = tslib_1.__importDefault(require("axios")); const fs_extra_1 = tslib_1.__importDefault(require("fs-extra")); const path_1 = tslib_1.__importDefault(require("path")); const spring4js_nodejs_1 = require("spring4js-nodejs"); let ActionBeautyPreviewService = class ActionBeautyPreviewService { async start() { const useProxy = this.configService.isUseProxy(); const host = this.configService.getProxyHost(); const port = this.configService.getProxyPort(); if (useProxy) { this.proxyConfig = { host, port, protocol: 'http:' }; } } async processRequest(ctx, next) { const { _preview_type_ } = ctx.query; if (_preview_type_ === 'beauty') { await this.pageModify(ctx, next); return true; } return false; } transformPathToUrl(data) { const allowUrlName = ['js', 'css', 'url']; for (let key of allowUrlName) { if (data[key]) { const isArray = data[key] instanceof Array; if (isArray) { data[key] = data[key].map((path) => `/_yzydev_/local/file?filePath=${path}`); } else { data[key] = `/_yzydev_/local/file?filePath=${data[key]}`; } } } } replaceInfoPath(data) { if (!(data instanceof Array)) { return data; } for (let i = 0; i < data.length; i++) { const item = data[i]; if (data instanceof Array) { for (let j = 0; j < item.length; j++) { this.transformPathToUrl(item[j]); } } else { this.transformPathToUrl(item); } } } async getBeautyLocalCustom(proPath) { let infoJson = ''; const distPath = path_1.default.resolve(proPath, "dist"); const infoPath = path_1.default.resolve(distPath, "info.json"); if (fs_extra_1.default.existsSync(infoPath)) { infoJson = await fs_extra_1.default.readJson(infoPath); } try { this.replaceInfoPath(Object.values(infoJson)); } catch (error) { } return infoJson; } async pageModify(ctx, next) { const { projectId, workbenchId, proPath } = ctx.query; let requestHeader = JSON.parse(JSON.stringify(ctx.headers)); requestHeader['accept-encoding'] = 'gzip, deflate'; const config = { method: ctx.method, url: ctx.href, headers: requestHeader, data: ctx.isJsonHeader ? ctx.request.body : ctx.req, responseType: "text", validateStatus: function (status) { return true; }, maxRedirects: 0 }; if (this.proxyConfig) { config.proxy = this.proxyConfig; } let axiosRes = await axios_1.default.request(config); if (axiosRes.status < 200 || axiosRes.status >= 300) { ctx.status = axiosRes.status; let header = JSON.parse(JSON.stringify(axiosRes.headers)); delete header["content-length"]; ctx.set(header); ctx.body = axiosRes.data; return; } const content = axiosRes.data; const localInfo = await this.getBeautyLocalCustom(proPath); if (!localInfo) { let header = JSON.parse(JSON.stringify(axiosRes.headers)); delete header["content-length"]; ctx.set(header); ctx.body = content; return; } const reg1 = /<div id="yun-custom-replace"><\/div>/g; let index = -1; let matches; while ((matches = reg1.exec(axiosRes.data)) !== null) { index = matches.index; } if (index === -1) { const reg2 = /<\/body>/g; while ((matches = reg2.exec(axiosRes.data)) !== null) { index = matches.index; } } const _diyInfo = { customized: true, pageContent: localInfo }; const addStr = `<script>window._diyInfo = ${JSON.stringify(_diyInfo)}</script>`; const finalContent = content.substring(0, index) + addStr + content.substring(index); let header = JSON.parse(JSON.stringify(axiosRes.headers)); delete header["content-length"]; ctx.set(header); ctx.body = finalContent; } }; tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], ActionBeautyPreviewService.prototype, "configService", void 0); ActionBeautyPreviewService = tslib_1.__decorate([ (0, spring4js_nodejs_1.Service)() ], ActionBeautyPreviewService); exports.default = ActionBeautyPreviewService;