This commit is contained in:
William Floyd 2026-03-16 01:57:07 +08:00 committed by GitHub
commit 41789e473d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 40 additions and 7 deletions

7
package-lock.json generated
View file

@ -13,6 +13,7 @@
"@alexaltea/capstone-js": "^3.0.5",
"@astronautlabs/amf": "^0.0.6",
"@blu3r4y/lzma": "^2.3.3",
"@michaelhomer/jqjs": "^1.5.0",
"@wavesenterprise/crypto-gost-js": "^2.1.0-RC1",
"@xmldom/xmldom": "^0.8.11",
"argon2-browser": "^1.18.0",
@ -3590,6 +3591,12 @@
"dev": true,
"license": "MIT"
},
"node_modules/@michaelhomer/jqjs": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/@michaelhomer/jqjs/-/jqjs-1.5.0.tgz",
"integrity": "sha512-GzKHQkeoVGsGDl5tDbQvmrl6ai03Gu1/Tm+/qvaExAcYoslhVx+OMVMZAVdB3sSvOMlo3vUiQlgBjAFk0FdC7w==",
"license": "MIT"
},
"node_modules/@napi-rs/nice": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@napi-rs/nice/-/nice-1.0.1.tgz",

View file

@ -96,6 +96,7 @@
"@alexaltea/capstone-js": "^3.0.5",
"@astronautlabs/amf": "^0.0.6",
"@blu3r4y/lzma": "^2.3.3",
"@michaelhomer/jqjs": "^1.5.0",
"@wavesenterprise/crypto-gost-js": "^2.1.0-RC1",
"@xmldom/xmldom": "^0.8.11",
"argon2-browser": "^1.18.0",

View file

@ -7,6 +7,7 @@
import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
import * as jq from "jq-wasm";
import * as jqjs from "@michaelhomer/jqjs";
/**
* jq operation
@ -30,7 +31,12 @@ class Jq extends Operation {
name: "Query",
type: "string",
value: ""
}
},
{
name: "Implementation",
type: "option",
value: ["WASM", "Native JS"]
},
];
}
@ -41,12 +47,31 @@ class Jq extends Operation {
*/
run(input, args) {
return (async () => {
const [query] = args;
try {
const result = await jq.json(input, query);
return JSON.stringify(result);
} catch (err) {
throw new OperationError(`Invalid jq expression: ${err.message}`);
const query = args[0];
const implementation = args[1].toLowerCase();
switch (implementation) {
case "wasm":
try {
const result = await jq.json(input, query);
return JSON.stringify(result);
} catch (err) {
throw new OperationError(`Invalid jq expression: ${err.message}`);
}
case "native js":
let result = '';
let filter = jqjs.compile(query)
for (let i of filter(input)) {
if (typeof i == 'undefined') {
result += 'undefined (runtime error)\n'
} else {
result += jqjs.prettyPrint(i) + '\n'
}
}
return result;
default:
throw new OperationError(`Invalid jq implementation: ${implementation}`);
break;
}
})();
}