Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Make Node API work with CJS and ESM applications #600

Merged
merged 3 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ script:
- grunt lint
- grunt test
- grunt docs
- npm run node-prod
- grunt prod --msg="$COMPILE_MSG"
- xvfb-run --server-args="-screen 0 1200x800x24" grunt testui
- grunt testnodeconsumer
before_deploy:
- grunt exec:sitemap
- grunt copy:ghPages
Expand All @@ -34,7 +34,7 @@ deploy:
file_glob: true
file:
- build/prod/*.zip
- build/node/CyberChef.js
- src/node/cjs.js
on:
repo: gchq/CyberChef
tags: true
Expand Down
91 changes: 45 additions & 46 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const NodeExternals = require("webpack-node-externals");
const glob = require("glob");
const path = require("path");

Expand All @@ -15,7 +14,6 @@ const path = require("path");
* @license Apache-2.0
*/

const NODE_PROD = process.env.NODE_ENV === "production";

module.exports = function (grunt) {
grunt.file.defaultEncoding = "utf8";
Expand All @@ -36,8 +34,7 @@ module.exports = function (grunt) {
grunt.registerTask("node",
"Compiles CyberChef into a single NodeJS module.",
[
"clean:node", "clean:config", "clean:nodeConfig", "exec:generateConfig",
"exec:generateNodeIndex", "webpack:node", "webpack:nodeRepl", "chmod:build"
"clean:node", "clean:config", "clean:nodeConfig", "exec:generateConfig", "exec:generateNodeIndex"
]);

grunt.registerTask("test",
Expand All @@ -51,6 +48,10 @@ module.exports = function (grunt) {
"A task which runs all the UI tests in the tests directory. The prod task must already have been run.",
["connect:prod", "exec:browserTests"]);

grunt.registerTask("testnodeconsumer",
"A task which checks whether consuming CJS and ESM apps work with the CyberChef build",
["exec:setupNodeConsumers", "exec:testCJSNodeConsumer", "exec:testESMNodeConsumer", "exec:testESMDeepImportNodeConsumer", "exec:teardownNodeConsumers"]);

grunt.registerTask("docs",
"Compiles documentation in the /docs directory.",
["clean:docs", "jsdoc", "chmod:docs"]);
Expand Down Expand Up @@ -90,7 +91,8 @@ module.exports = function (grunt) {
COMPILE_MSG: JSON.stringify(grunt.option("compile-msg") || grunt.option("msg") || ""),
PKG_VERSION: JSON.stringify(pkg.version),
},
moduleEntryPoints = listEntryModules();
moduleEntryPoints = listEntryModules(),
nodeConsumerTestPath = "~/tmp-cyberchef";


/**
Expand Down Expand Up @@ -201,46 +203,6 @@ module.exports = function (grunt) {
]
};
},
node: {
mode: NODE_PROD ? "production" : "development",
target: "node",
entry: "./src/node/index.mjs",
externals: [NodeExternals({
whitelist: ["crypto-api/src/crypto-api"]
})],
output: {
filename: "CyberChef.js",
path: __dirname + "/build/node",
library: "CyberChef",
libraryTarget: "commonjs2"
},
plugins: [
new webpack.DefinePlugin(BUILD_CONSTANTS),
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1
})
],
},
nodeRepl: {
mode: NODE_PROD ? "production" : "development",
target: "node",
entry: "./src/node/repl-index.mjs",
externals: [NodeExternals({
whitelist: ["crypto-api/src/crypto-api"]
})],
output: {
filename: "CyberChef-repl.js",
path: __dirname + "/build/node",
library: "CyberChef",
libraryTarget: "commonjs2"
},
plugins: [
new webpack.DefinePlugin(BUILD_CONSTANTS),
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1
})
],
}
},
"webpack-dev-server": {
options: {
Expand Down Expand Up @@ -428,7 +390,44 @@ module.exports = function (grunt) {
},
nodeTests: {
command: "node --experimental-modules --no-warnings --no-deprecation tests/node/index.mjs"
}
},
setupNodeConsumers: {
command: [
"echo '\n--- Testing node conumers ---'",
"npm link",
`mkdir ${nodeConsumerTestPath}`,
`cp tests/node/consumers/* ${nodeConsumerTestPath}`,
`cd ${nodeConsumerTestPath}`,
"npm link cyberchef"
].join(";"),
},
teardownNodeConsumers: {
command: [
`rm -rf ${nodeConsumerTestPath}`,
"echo '\n--- Node consumer tests complete ---'"
].join(";"),
},
testCJSNodeConsumer: {
command: [
`cd ${nodeConsumerTestPath}`,
"node --no-warnings cjs-consumer.js",
].join(";"),
stdout: false,
},
testESMNodeConsumer: {
command: [
`cd ${nodeConsumerTestPath}`,
"node --no-warnings --experimental-modules esm-consumer.mjs",
].join(";"),
stdout: false,
},
testESMDeepImportNodeConsumer: {
command: [
`cd ${nodeConsumerTestPath}`,
"node --no-warnings --experimental-modules esm-deep-import-consumer.mjs",
].join(";"),
stdout: false,
},
},
});
};
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ CyberChef is built to support
- Mozilla Firefox 35+
- Microsoft Edge 14+

## Node.js support

CyberChef is built to fully support Node.js `v10` and partially supports `v12`. Named imports using a deep import specifier does not work in `v12`. For more information, see the Node API page in the project [wiki pages](https://github.com/gchq/CyberChef/wiki)


## Contributing

Expand Down
89 changes: 47 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"type": "git",
"url": "https://github.com/gchq/CyberChef/"
},
"main": "build/node/CyberChef.js",
"main": "src/node/cjs.js",
"module": "src/node/index.mjs",
"bugs": "https://github.com/gchq/CyberChef/issues",
"browserslist": [
Expand Down Expand Up @@ -108,6 +108,7 @@
"diff": "^4.0.1",
"es6-promisify": "^6.0.1",
"escodegen": "^1.11.1",
"esm": "^3.2.25",
"esmangle": "^1.0.1",
"esprima": "^4.0.1",
"exif-parser": "^0.1.12",
Expand All @@ -126,7 +127,7 @@
"kbpgp": "2.1.2",
"libbzip2-wasm": "0.0.4",
"libyara-wasm": "0.0.12",
"lodash": "^4.17.11",
"lodash": "^4.17.15",
"loglevel": "^1.6.3",
"loglevel-message-prefix": "^3.0.0",
"moment": "^2.24.0",
Expand Down Expand Up @@ -156,11 +157,9 @@
"scripts": {
"start": "grunt dev",
"build": "grunt prod",
"node": "NODE_ENV=development grunt node",
"node-prod": "NODE_ENV=production grunt node",
"repl": "grunt node && node build/node/CyberChef-repl.js",
"repl": "node src/node/repl.js",
"test": "grunt test",
"test-node": "grunt test-node",
"test-node-consumer": "grunt testnodeconsumer",
"testui": "grunt testui",
"docs": "grunt docs",
"lint": "grunt lint",
Expand Down
13 changes: 13 additions & 0 deletions src/node/cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Export the main ESM module as CommonJS
*
*
* @author d98762656 [d98762625@gmail.com]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/

/*eslint no-global-assign: ["off"] */
require = require("esm")(module);
module.exports = require("./index.mjs");
module.exports.File = require("./File.mjs");
Loading