fix(test-server): update reliable error

This commit is contained in:
scarqin 2023-03-16 00:34:17 +08:00
parent 45939bb0e9
commit d11f3eadd0
6 changed files with 36 additions and 63 deletions

View File

@ -2,7 +2,7 @@ import { spawn } from 'child_process';
const installExtension = (extension, version = 'latest') => {
return new Promise(resolve => {
const ls = spawn('npm', ['i', '--no-save', `${extension}@${version}`]);
const ls = spawn('npm', ['i', `${extension}@${version}`]);
ls.on('close', function (code) {
console.log('child process exited with code ' + code);
return resolve(true);
@ -39,7 +39,6 @@ const loadExtension = async ({ name, version }) => {
extension: extension.default,
packageJson: extPkg
};
console.log(cache);
extensionMap.set(`${name}:${extPkg.version}`, cache);
}
return [cache, null];

View File

@ -1 +1 @@
package-lock-lock.json
package-lock.json

View File

@ -20,6 +20,7 @@
"@grpc/grpc-js": "1.7.3",
"@grpc/proto-loader": "0.7.3",
"@koa/cors": "3.3.0",
"axios": "1.3.4",
"content-disposition": "^0.5.4",
"crypto-js": "4.1.1",
"form-data": "^4.0.0",
@ -32,7 +33,6 @@
"postman-sandbox": "^4.2.3",
"socket.io": "4.5.3",
"ws": "8.8.1",
"xml2js": "0.4.23",
"axios":"1.3.4"
"xml2js": "0.4.23"
}
}

View File

@ -501,13 +501,14 @@ privateFun.parseBeforeCode = async function (scritEngines = 'pm', inputData, inp
}
default: {
//Get extension function
const [{ extension, packageJson }, err] = await loadExtension({
const [extensionCache, err] = await loadExtension({
name: inputOpts.authInfo.authType
});
if (err) {
console.error(`install auth extension code error: ${err}}`);
break;
}
const { extension, packageJson } = extensionCache;
if (!packageJson?.features?.authAPI) break;
//Prepare auth info,such as replace global variable
let tmpEnvGlobals = Object.assign({}, global.eoTestGlobals || {}, tmpEnviroments || {});
@ -524,6 +525,7 @@ privateFun.parseBeforeCode = async function (scritEngines = 'pm', inputData, inp
const config = (authInfo || []).reduce((acc, cur) => ({ [cur.key]: cur.value, ...acc }), {});
//Execute at runtime
const code = await func(config);
console.log(code);
const [authPmRes, err] = await pmRuntime.executeSync(ctx, code, {
context: pmRes
});

View File

@ -3,10 +3,12 @@ const extensionMap = new Map();
const path = require('path');
const isElectron = !!process.versions['electron'];
const axios = require('axios');
const installExtension = (name, version = 'latest') => {
const operateExtension = (name, version = 'latest', operate = 'install') => {
const args = operate === 'install' ? ['i', '--no-save', `${name}@${version}`] : ['uninstall', '--no-save', name];
return new Promise(resolve => {
//TODO install locally at user home dir
const ls = spawn('npm', ['i', '--no-save', `${name}@${version}`]);
const ls = spawn('npm', args);
ls.on('close', function (code) {
// console.log(`child process exited with code :${code}`);
return resolve(true);
@ -21,21 +23,27 @@ const installExtension = (name, version = 'latest') => {
});
});
};
let time = 0;
const loadExtension = async ({ name, version = 'latest' }) => {
// * Is extension in Map cache ?
// * If true, then get the function.
// * If false, then install the extension and save to map cache then get the function.
//* Chek latest version
const allExtensionRes = await axios.get('https://extensions.postcat.com/list').catch(error => {});
const extensionPkgInfo = allExtensionRes?.data?.data.find(val => val.name === name);
version = extensionPkgInfo.version || version;
// * Is extension in Map cache ?
//TODO save version at extensionMap
const hasIt = extensionMap.has(`${name}:${version}`);
// * If true, then get the function.
if (hasIt) {
return [extensionMap.get(`${name}:${version}`, null)];
}
let cache = {};
// * If false, then install the extension and save to map cache then get the function.
if (!hasIt) {
//TODO Remote/Local Node Union
//Install Locally in Electron
//* Install Locally in Electron
if (isElectron) {
const extPkg = require(path.join(`${global['__HOME_DIR']}/node_modules/${name}`, 'package.json'));
const extension = require(`${global['__HOME_DIR']}/node_modules/${name}`);
@ -50,26 +58,27 @@ const loadExtension = async ({ name, version = 'latest' }) => {
return [cache, null];
}
//Install in Remote Node
const isOk = await installExtension(name, version);
//* Install in Remote Node
const isOk = await operateExtension(name, version);
if (!isOk) {
return [null, `Install Extension #${name} Failed`];
}
try {
const extPkg = await axios.get(`https://unpkg.com/${name}@${version}/package.json`).catch(error => {});
if (!extPkg) {
return [null, `Request Extension #${name} Failed`];
}
const extension = await import(name);
//! Delete require cache
delete require.cache[require.resolve(name)];
//* Get the extension pkg instance
const extPkg = require(`${path.dirname(require.resolve(name))}/package.json`);
const extension = require(name);
cache = {
extension: extension.default,
packageJson: extPkg.data
extension: extension,
packageJson: extPkg
};
extensionMap.set(`${name}:${version}`, cache);
return [cache, null];
} catch (e) {
return [null, `Import Extension #${name} Failed`];
return [null, `Import Extension #${name} Failed: ${e}`];
}
}
return [cache, null];
};
module.exports = { loadExtension };

View File

@ -5936,7 +5936,7 @@ debug@^3.1.0, debug@^3.2.6, debug@^3.2.7:
dependencies:
ms "^2.1.1"
debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.npmmirror.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha512-syBZ+rnAK3EgMsH2aYEOLUW7mZSY9Gb+0wUMCFsZvcmiz+HigA0LOcq/HoQqVuGG+EKykunc7QG2bzrponfaSw==
@ -8966,7 +8966,7 @@ import-lazy@^4.0.0:
resolved "https://registry.npmmirror.com/import-lazy/-/import-lazy-4.0.0.tgz#e8eb627483a0a43da3c03f3e35548be5cb0cc153"
integrity sha512-rKtvo6a868b5Hu3heneU+L4yEQ4jYKLtjpnPeUdK7h0yzXGmyBTypknlkCvHFBqfX9YlorEiMM6Dnq/5atfHkw==
imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.npmmirror.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha512-JmXMZ6wuvDmLiHEml9ykzqO6lwFbof0GG4IkcGaENdCRDDmMVnny7s5HsIgHCbaq0w2MyPhDqkhTUgS2LU2PHA==
@ -10551,11 +10551,6 @@ lodash-es@4.17.21:
resolved "https://registry.npmmirror.com/lodash-es/-/lodash-es-4.17.21.tgz#43e626c46e6591b7750beb2b50117390c609e3ee"
integrity sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==
lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.npmmirror.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha512-bSYo8Pc/f0qAkr8fPJydpJjtrHiSynYfYBjtANIgXv5xEf1WlTC63dIDlgu0s9dmTvzRu1+JJTxcIAHe+sH0FQ==
lodash._baseuniq@~4.6.0:
version "4.6.0"
resolved "https://registry.npmmirror.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
@ -10564,33 +10559,11 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"
lodash._bindcallback@*:
version "3.0.1"
resolved "https://registry.npmmirror.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha512-2wlI0JRAGX8WEf4Gm1p/mv/SZ+jLijpj0jyaE/AXeuQphzCgD8ZQW4oSpoN8JAopujOFGU3KMuq7qfHBWlGpjQ==
lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.npmmirror.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha512-S8dUjWr7SUT/X6TBIQ/OYoCHo1Stu1ZRy6uMUSKqzFnZp5G5RyQizSm6kvxD2Ewyy6AVfMg4AToeZzKfF99T5w==
lodash._createcache@*:
version "3.1.2"
resolved "https://registry.npmmirror.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha512-ev5SP+iFpZOugyab/DEUQxUeZP5qyciVTlgQ1f4Vlw7VUcCD8fVnyIqVUEIaoFH9zjAqdgi69KiofzvVmda/ZQ==
dependencies:
lodash._getnative "^3.0.0"
lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.npmmirror.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha512-GTkC6YMprrJZCYU3zcqZj+jkXkrXzq3IPBcF/fIPpNEAB4hZEtXU8zp/RwKOvZl43NUmwDbyRk3+ZTbeRdEBXA==
lodash._getnative@*, lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.npmmirror.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha512-RrL9VxMEPyDMHOd9uFbvMe8X55X16/cGM5IgOKgRElQZutpX89iS6vwl64duTV1/16w5JY7tuFNXqoekmh1EmA==
lodash._root@~3.0.0:
version "3.0.1"
resolved "https://registry.npmmirror.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
@ -10651,11 +10624,6 @@ lodash.mergewith@^4.6.2:
resolved "https://registry.npmmirror.com/lodash.mergewith/-/lodash.mergewith-4.6.2.tgz#617121f89ac55f59047c7aec1ccd6654c6590f55"
integrity sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ==
lodash.restparam@*:
version "3.6.1"
resolved "https://registry.npmmirror.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha512-L4/arjjuq4noiUJpt3yS6KIKDtJwNe2fIYgMqyYYKoeIfV1iEqvPwhCx23o+R9dzouGihDAPN1dTIRWa7zk8tw==
lodash.snakecase@^4.1.1:
version "4.1.1"
resolved "https://registry.npmmirror.com/lodash.snakecase/-/lodash.snakecase-4.1.1.tgz#39d714a35357147837aefd64b5dcbb16becd8f8d"
@ -12963,11 +12931,6 @@ posix-character-classes@^0.1.0:
resolved "https://registry.npmmirror.com/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab"
integrity sha512-xTgYBc3fuo7Yt7JbiuFxSYGToMoz8fLoE6TC9Wx1P/u+LfeThMOAqmuyECnlBaaJb+u1m9hHiXUEtwW4OzfUJg==
postcat-basic-auth@0.0.6:
version "0.0.6"
resolved "https://registry.npmmirror.com/postcat-basic-auth/-/postcat-basic-auth-0.0.6.tgz#190f2d90bdf4acd162f2310ed0b3c262490e0e76"
integrity sha512-c5umzVTBRuiyV1UEXoiyyXB4kG93C2qRY1E9YWnOPseY9IFUG/tzacPaIlC9Gsvpj1DW1uNJrNpUKYYlPLhkOg==
postcss-html@1.5.0:
version "1.5.0"
resolved "https://registry.npmmirror.com/postcss-html/-/postcss-html-1.5.0.tgz#57a43bc9e336f516ecc448a37d2e8c2290170a6f"