mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-23 00:06:17 -04:00
WIP: generate top level node exports - manually mock lib files
This commit is contained in:
parent
0977e82170
commit
0a0240e520
5 changed files with 125 additions and 38 deletions
99
src/core/config/scripts/generateNodeIndex.mjs
Normal file
99
src/core/config/scripts/generateNodeIndex.mjs
Normal file
|
@ -0,0 +1,99 @@
|
|||
/**
|
||||
* This script generates the exports functionality for the node API.
|
||||
*
|
||||
* it exports chef as default, but all the wrapped operations as
|
||||
* other top level exports.
|
||||
*
|
||||
* @author d98762656 [d98762625@gmail.com]
|
||||
* @copyright Crown Copyright 2018
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
/*eslint no-console: ["off"] */
|
||||
|
||||
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import * as operations from "../../operations/index";
|
||||
import { decapitalise } from "../../../node/apiUtils";
|
||||
|
||||
const dir = path.join(`${process.cwd()}/src/node`);
|
||||
if (!fs.existsSync(dir)) {
|
||||
console.log("\nCWD: " + process.cwd());
|
||||
console.log("Error: generateNodeIndex.mjs should be run from the project root");
|
||||
console.log("Example> node --experimental-modules src/core/config/scripts/generateNodeIndex.mjs");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
let code = `/**
|
||||
* THIS FILE IS AUTOMATICALLY GENERATED BY src/core/config/scripts/generateOpsIndex.mjs
|
||||
*
|
||||
* @author d98762625 [d98762625@gmail.com]
|
||||
* @copyright Crown Copyright ${new Date().getUTCFullYear()}
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import { wrap } from "./apiUtils";
|
||||
import {
|
||||
`;
|
||||
|
||||
Object.keys(operations).forEach((op) => {
|
||||
// prepend with core_ to avoid name collision later.
|
||||
code += ` ${op} as core_${op},\n`;
|
||||
});
|
||||
|
||||
code +=`
|
||||
} from "../node/operations/index";
|
||||
|
||||
// Define global environment functions
|
||||
global.ENVIRONMENT_IS_WORKER = function() {
|
||||
return typeof importScripts === "function";
|
||||
};
|
||||
global.ENVIRONMENT_IS_NODE = function() {
|
||||
return typeof process === "object" && typeof require === "function";
|
||||
};
|
||||
global.ENVIRONMENT_IS_WEB = function() {
|
||||
return typeof window === "object";
|
||||
};
|
||||
|
||||
/**
|
||||
* generateChef
|
||||
*
|
||||
* Creates decapitalised, wrapped ops in chef object for default export.
|
||||
*/
|
||||
function generateChef() {
|
||||
return {
|
||||
`;
|
||||
|
||||
Object.keys(operations).forEach((op) => {
|
||||
code += ` ${decapitalise(op)}: wrap(core_${op}),\n`;
|
||||
});
|
||||
|
||||
code += ` };
|
||||
}
|
||||
|
||||
const chef = generateChef();
|
||||
|
||||
`;
|
||||
|
||||
Object.keys(operations).forEach((op) => {
|
||||
code += `const ${decapitalise(op)} = chef[${decapitalise(op)}];\n`;
|
||||
});
|
||||
|
||||
code +=`
|
||||
export default chef;
|
||||
export {
|
||||
`;
|
||||
|
||||
Object.keys(operations).forEach((op) => {
|
||||
code += ` ${decapitalise(op)},\n`;
|
||||
});
|
||||
|
||||
|
||||
code += "};\n";
|
||||
|
||||
|
||||
fs.writeFileSync(
|
||||
path.join(dir, "/index.mjs"),
|
||||
code
|
||||
);
|
|
@ -163,9 +163,14 @@ export function help(operations, searchTerm) {
|
|||
* @returns {String} decapitalised
|
||||
*/
|
||||
export function decapitalise(name) {
|
||||
// Don't decapitalise names that are purely uppercase
|
||||
if (/^[A-Z0-9]+$/g.test(name)) {
|
||||
// Don't decapitalise names that start with 2+ caps
|
||||
if (/^[A-Z0-9]{2,}/g.test(name)) {
|
||||
return name;
|
||||
}
|
||||
// reserved. Don't change for now.
|
||||
if (name === "Return") {
|
||||
return name;
|
||||
}
|
||||
|
||||
return `${name.charAt(0).toLowerCase()}${name.substr(1)}`;
|
||||
}
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
/**
|
||||
* Node view for CyberChef.
|
||||
*
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2018
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
import "babel-polyfill";
|
||||
|
||||
import {wrap, help, decapitalise, translateTo} from "./apiUtils";
|
||||
import * as operations from "../core/operations/index";
|
||||
|
||||
// Define global environment functions
|
||||
global.ENVIRONMENT_IS_WORKER = function() {
|
||||
return typeof importScripts === "function";
|
||||
};
|
||||
global.ENVIRONMENT_IS_NODE = function() {
|
||||
return typeof process === "object" && typeof require === "function";
|
||||
};
|
||||
global.ENVIRONMENT_IS_WEB = function() {
|
||||
return typeof window === "object";
|
||||
};
|
||||
|
||||
|
||||
const chef = {};
|
||||
|
||||
// Add in wrapped operations with camelCase names
|
||||
Object.keys(operations).forEach(op =>
|
||||
chef[decapitalise(op)] = wrap(operations[op]));
|
||||
|
||||
chef.help = help.bind(null, operations);
|
||||
chef.translateTo = translateTo;
|
||||
|
||||
export default chef;
|
||||
export {chef};
|
Loading…
Add table
Add a link
Reference in a new issue