From c142f7fcdd38cb3f41715ad5c42b8ab3545b157d Mon Sep 17 00:00:00 2001 From: William Floyd Date: Mon, 9 Mar 2026 17:07:38 -0500 Subject: [PATCH] feat: Add option for native js implementation of jq --- package-lock.json | 7 +++++++ package.json | 1 + src/core/operations/Jq.mjs | 39 +++++++++++++++++++++++++++++++------- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6ab7c7fe3..59bb51044 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index d3519e0fc..d7b4c7c28 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/core/operations/Jq.mjs b/src/core/operations/Jq.mjs index 4584d1a98..ffb61bdac 100644 --- a/src/core/operations/Jq.mjs +++ b/src/core/operations/Jq.mjs @@ -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; } })(); }