youzanyun-devtool-worker
Version:
153 lines (152 loc) • 6.17 kB
JavaScript
;
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 url_1 = tslib_1.__importDefault(require("url"));
const spring4js_nodejs_1 = require("spring4js-nodejs");
let ActionH5PreviewService = class ActionH5PreviewService {
async start() {
const useProxy = this.configService.isUseProxy();
const host = this.configService.getProxyHost();
const port = this.configService.getProxyPort();
this.webPort = this.configService.getWebPort();
if (useProxy) {
this.proxyConfig = {
host,
port,
protocol: 'http:'
};
}
}
async processRequest(ctx, next) {
const { _yzy_preview_type_ } = ctx.query;
if (ctx.path === '/html/h5-comp-preview' && _yzy_preview_type_ === 'h5-comp-preview') {
await this.pageModify(ctx, next);
return true;
}
return false;
}
async getPreviewComps(params) {
try {
const result = await axios_1.default.request({
method: 'GET',
url: `http://127.0.0.1:${this.webPort}/api/h5-extension/getPreviewComps`,
params,
});
return result.data.data || [];
}
catch (err) {
return {};
}
}
async pageModify(ctx, next) {
const { pageName, nodeName, compName, projectId, workbenchId } = ctx.query;
let requestHeader = JSON.parse(JSON.stringify(ctx.headers));
requestHeader['accept-encoding'] = 'gzip, deflate';
let axiosRes = await axios_1.default.request({
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 (axiosRes.status < 200 || axiosRes.status >= 300) {
await (0, axios_1.default)({
method: "post",
url: `http://localhost:${this.webPort}/api/h5-extension/previewComps/broadcast`,
data: {
projectId,
workbenchId,
previewComps: []
}
});
ctx.status = axiosRes.status;
let header = JSON.parse(JSON.stringify(axiosRes.headers));
delete header["content-length"];
ctx.set(header);
ctx.body = axiosRes.data;
return;
}
let design = "";
const { localComps = [], localPageFile = '' } = await this.getPreviewComps({
projectId, nodeCode: nodeName, pageName
});
let previewComps = localComps;
if (compName) {
previewComps = previewComps.filter((item) => item.type === compName);
}
const filteredComps = previewComps.filter((item) => !item.disabled);
design = JSON.stringify(filteredComps);
const fileUrl = url_1.default.parse(localPageFile, true);
const filePath = fileUrl.query.filePath;
const isFileExist = await fs_extra_1.default.pathExists(filePath);
let mallCloudJsRepalce = '';
if (isFileExist) {
mallCloudJsRepalce = `<script src="${localPageFile}"></script>`;
}
else {
mallCloudJsRepalce = `<div>
<div>本地代码未打包,请确认。</div>
<div>安装依赖:npm install</div>
<div>前端打包:npm run dev 或 npm run build</div>
</div>`;
}
let designUpdate = `
<script id="customScript">
if (!window._global) {
window._global = { design: []};
}
window._global.design = ${design}
var xhr = new XMLHttpRequest();
var url = '/preview?type=savePreview';
xhr.open('POST', url, true);
var params = {
projectId: ${projectId},
workbenchId: ${workbenchId},
previewComps: ${JSON.stringify(previewComps)} || []
}
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(params));
xhr.onreadystatechange = function () {
}
</script>
`;
const content = axiosRes.data;
let newContentWithMallCloud = axiosRes.data;
if (mallCloudJsRepalce) {
const mallCloudJsReg = /<script src="https:\/\/file.yzcdn.cn\/mall-cloud\/\S*?"><\/script>/g;
let mallCloudJsIndex = -1;
let mallCloudJsMatches;
let matchMallCloudJsStr = '';
while ((mallCloudJsMatches = mallCloudJsReg.exec(content)) !== null) {
mallCloudJsIndex = mallCloudJsMatches.index;
matchMallCloudJsStr = mallCloudJsMatches[0];
}
const matchStrLength = matchMallCloudJsStr.length;
newContentWithMallCloud = content.substring(0, mallCloudJsIndex) + mallCloudJsRepalce + content.substring(mallCloudJsIndex + matchStrLength);
}
const wscJsReg = /<script src="\S*?" charset="utf-8" onerror="_cdnFallback\(this\)[\s\S]*?><\/script>/g;
let wscJsIndex = -1;
let wscMatches;
while ((wscMatches = wscJsReg.exec(newContentWithMallCloud)) !== null) {
wscJsIndex = wscMatches.index;
}
const finalContent = newContentWithMallCloud.substring(0, wscJsIndex) + designUpdate + newContentWithMallCloud.substring(wscJsIndex);
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)()
], ActionH5PreviewService.prototype, "configService", void 0);
ActionH5PreviewService = tslib_1.__decorate([
(0, spring4js_nodejs_1.Service)()
], ActionH5PreviewService);
exports.default = ActionH5PreviewService;