@lcap/nasl
Version:
NetEase Application Specific Language
189 lines • 7.75 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.internalWaitOqlQueryComponentChildrenFinish = exports.requestEmbeddedTSBatch = exports.naslOQLCacheStore = void 0;
const nasl_utils_1 = require("@lcap/nasl-utils");
const localforage_1 = __importDefault(require("localforage"));
const utils_1 = require("./utils");
// import { getGzipData } from '../../generator/release-body/utils';
var StoreName;
(function (StoreName) {
StoreName["OQLToTypeScript"] = "OQLToTypeScript";
})(StoreName || (StoreName = {}));
const disabledOqlCache = nasl_utils_1.isNode || nasl_utils_1.isAutoTestEnv;
exports.naslOQLCacheStore = localforage_1.default.createInstance({
description: 'OQL to TypeScript接口缓存',
storeName: StoreName.OQLToTypeScript,
});
const api = {
typescriptBatchV2: {
path: '/api/v1/querydebug/generate/typescriptBatch/v2',
method: 'post',
},
typescriptBatchEncodeV2: {
path: '/api/v1/querydebug/generate/typescriptBatchEncode/v2',
method: 'post',
},
};
function preparePayloadAndCacheKey(oql) {
if (oql.code) {
const payload = {
oqlIdentifier: 'myoqlIdentifier',
datasourceName: oql.dataSource,
oql: oql.code,
typeScriptStartLine: 1,
typeScriptStartLineColumn: 1,
};
return {
cacheKey: JSON.stringify(payload),
payload: payload,
};
}
return {
cacheKey: '',
payload: null,
};
}
function callTypescriptBatchV2(http, oqlGenList, extraParams) {
const body = Object.assign({ oqlGenList }, extraParams);
// const body = { gzipBody: getGzipData(Object.assign({ oqlGenList }, extraParams)) };
return http.post(api.typescriptBatchV2.path, body).then((result) => {
const { data } = result;
if (data.code !== 200 || !data.success || !data.result) {
throw result;
}
return data.result;
});
}
async function makeCachedBatchRequest(http, payloadWithCacheKeyList, extraParams) {
const itemsWithIndex = payloadWithCacheKeyList.map((item, index) => {
return { item, index };
});
const cacheTest = await Promise.all(itemsWithIndex.map(async ({ item, index }) => {
const cached = disabledOqlCache
? null
: await exports.naslOQLCacheStore.getItem(item.cacheKey);
return { item, index, cached };
}));
const notCachedList = cacheTest.filter((x) => !x.cached);
const cachedList = cacheTest.filter((x) => x.cached);
const resultContainer = Array.from({
length: payloadWithCacheKeyList.length,
}).fill(null);
for (const { cached, index } of cachedList) {
resultContainer[index] = cached;
}
const oqlGenList = notCachedList.map((x) => x.item.payload);
const tempResultList = (oqlGenList.length > 0)
? await (0, utils_1.batchApiCall)(http, oqlGenList, callTypescriptBatchV2, extraParams, 50)
.then(async (resultList = []) => {
await Promise.all(resultList.map((res, index) => {
const payload = notCachedList[index];
resultContainer[payload.index] = res;
if (!disabledOqlCache) {
return exports.naslOQLCacheStore.setItem(payload.item.cacheKey, res);
}
}));
return resultList;
})
: [];
if (tempResultList.length !== oqlGenList.length) {
throw new Error('返回的数据长度不一致');
}
return resultContainer;
}
// 从服务端获取 SQL -> TS 的翻译和 sourceMap
async function requestEmbeddedTSBatch(http, oqlList, extraParams) {
const paramList = oqlList
.map((oql) => ({ oql, ...preparePayloadAndCacheKey(oql) }))
.filter((oql) => oql.payload);
return makeCachedBatchRequest(http, paramList, extraParams)
.then((data) => {
if (data.length !== paramList.length) {
throw new Error('返回的数据长度不一致');
}
data.forEach((item, index) => {
const targetOql = paramList[index].oql;
if (item) {
// 截止 20250919 后端目前还有并发 bug,会导致 appId 不正确。暂时先覆写之
item.appId = extraParams.appId;
}
targetOql.codeSourceMap = item ?? { typescript: '', lexicalErrorCode: 1001, appId: extraParams.appId };
});
})
.catch((err) => {
if (err instanceof Error) {
throw new Error(`OQL 请求失败 (${err.message})`);
}
else {
throw new Error(`OQL 请求失败 (${err?.data?.msg ?? '未知错误'})`);
}
});
}
exports.requestEmbeddedTSBatch = requestEmbeddedTSBatch;
async function internalWaitOqlQueryComponentChildrenFinish(node) {
const oqlList = [];
node.traverseStrictChildren((el) => {
if (el.concept === 'OqlQueryComponent') {
if (!el.codeSourceMap) {
oqlList.push(el);
}
}
});
if (oqlList.length > 0) {
const app = node.rootNode ?? node;
const http = app?.naslServer.http;
if (http) {
// 314 开始默认使用批量接口
const datasourceInfoList = [];
const completeNameList = [];
oqlList.forEach((oql) => {
if (oql.code) {
const completeName = oql.dataSource;
const dataSourceNasl = app.findNodeByCompleteName(completeName);
if (dataSourceNasl && !completeNameList.includes(completeName)) {
completeNameList.push(completeName);
datasourceInfoList.push({
datasourceName: completeName,
datasource: {
concept: 'DataSource',
entities: dataSourceNasl.entities?.map((entity) => {
return {
name: entity.name,
concept: entity.concept,
tableName: entity.tableName,
properties: entity.properties?.map((prop) => {
return {
name: prop.name,
concept: prop.concept,
columnName: prop.columnName,
};
}),
};
}),
},
});
}
}
});
if (datasourceInfoList.length === 0)
return;
const extraParams = {
datasourceInfoList,
appId: app.id,
ideVersion: app.ideVersion,
};
return requestEmbeddedTSBatch(http, oqlList, extraParams).catch(err => {
console.error(`OQL 接口请求失败 (${err?.message}),重试一次`);
return requestEmbeddedTSBatch(http, oqlList, extraParams);
});
}
else {
throw new Error('http client 不存在');
}
}
}
exports.internalWaitOqlQueryComponentChildrenFinish = internalWaitOqlQueryComponentChildrenFinish;
//# sourceMappingURL=oql-cache.js.map