feat: Add option for native js implementation of jq

This commit is contained in:
William Floyd 2026-03-09 17:07:38 -05:00
parent a19261d5f7
commit c142f7fcdd
No known key found for this signature in database
GPG Key ID: B3EEEDD81893CAF9
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", "@alexaltea/capstone-js": "^3.0.5",
"@astronautlabs/amf": "^0.0.6", "@astronautlabs/amf": "^0.0.6",
"@blu3r4y/lzma": "^2.3.3", "@blu3r4y/lzma": "^2.3.3",
"@michaelhomer/jqjs": "^1.5.0",
"@wavesenterprise/crypto-gost-js": "^2.1.0-RC1", "@wavesenterprise/crypto-gost-js": "^2.1.0-RC1",
"@xmldom/xmldom": "^0.8.11", "@xmldom/xmldom": "^0.8.11",
"argon2-browser": "^1.18.0", "argon2-browser": "^1.18.0",
@ -3590,6 +3591,12 @@
"dev": true, "dev": true,
"license": "MIT" "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": { "node_modules/@napi-rs/nice": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/@napi-rs/nice/-/nice-1.0.1.tgz", "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", "@alexaltea/capstone-js": "^3.0.5",
"@astronautlabs/amf": "^0.0.6", "@astronautlabs/amf": "^0.0.6",
"@blu3r4y/lzma": "^2.3.3", "@blu3r4y/lzma": "^2.3.3",
"@michaelhomer/jqjs": "^1.5.0",
"@wavesenterprise/crypto-gost-js": "^2.1.0-RC1", "@wavesenterprise/crypto-gost-js": "^2.1.0-RC1",
"@xmldom/xmldom": "^0.8.11", "@xmldom/xmldom": "^0.8.11",
"argon2-browser": "^1.18.0", "argon2-browser": "^1.18.0",

View File

@ -7,6 +7,7 @@
import Operation from "../Operation.mjs"; import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs"; import OperationError from "../errors/OperationError.mjs";
import * as jq from "jq-wasm"; import * as jq from "jq-wasm";
import * as jqjs from "@michaelhomer/jqjs";
/** /**
* jq operation * jq operation
@ -30,7 +31,12 @@ class Jq extends Operation {
name: "Query", name: "Query",
type: "string", type: "string",
value: "" value: ""
} },
{
name: "Implementation",
type: "option",
value: ["WASM", "Native JS"]
},
]; ];
} }
@ -41,12 +47,31 @@ class Jq extends Operation {
*/ */
run(input, args) { run(input, args) {
return (async () => { return (async () => {
const [query] = args; const query = args[0];
try { const implementation = args[1].toLowerCase();
const result = await jq.json(input, query);
return JSON.stringify(result); switch (implementation) {
} catch (err) { case "wasm":
throw new OperationError(`Invalid jq expression: ${err.message}`); 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;
} }
})(); })();
} }