
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
AStyle code formatter compiled to WebAssembly, for Node.js and the browser.
Install via NPM:
$ yarn add wastyle
Firstly, you need to initialize AStyle WASM library with init
function.
Unfortunately this step couldn't be done automatically since you need make Webpack emit the AStyle WASM binary to your dist directory and tell us its URL. Since we support running in browser mainly, so I don't want to write code to detect Node.js to increase your Webpack bundle size. So in both Node.js and Webpack this step must be done.
In Node.js, call init
with a Buffer
from fs.readFile
or fs.readFileSync
:
const fs = require("fs");
const util = require("util");
const wastyle = require("wastyle");
util.promisify(fs.readFile)("wastyle/dist/astyle.wasm")
.then(data => wastyle.init(data))
.then(() => console.log("WAstyle is ready!"))
.catch(err => console.error(err));
In Webpack, I recommend you to use file-loader
to emit the AStyle WASM binary to your dist directory and get its URL. See NeekSandhu/onigasm#2.
Note that Webpack has a very simple built-in WASM loader, which will load a WASM file (detected by its magic header) as a compiled and instantiated WASM instance. But it doesn't support passing imports to WASM module. So it WILL fail since the AStyle WASM binary needs WASI to run. To disable the build-in WASM loader and force file-loader
to produce the file URL, use this rule: (webpack/webpack#6725)
{
test: /\.wasm$/,
loader: "file-loader",
type: "javascript/auto"
}
In your code, initialize WAstyle with:
import { init, format } from "wastyle";
import astyleBinaryUrl from "wastyle/dist/astyle.wasm";
init(astyleBinaryUrl).then(() => console.log("WAstyle is ready!"));
The init
function will compile and instantiate the Astyle WASM binary asynchronously. Will throw an error if failed.
After initialization, call the synchronous function format
to use Astyle:
const [success, result] = format("#include <cstdio>\nint main(){int 🦄,a,*b=a,c=🦄*2,*d=nullptr;return -1;}", "pad-oper style=google");
console.log(result);
// #include <cstdio>
// int main() {
// int 🦄, a, *b = a, c = 🦄 * 2, *d = nullptr;
// return -1;
// }
Besides, you can use wastyle/dist/wastyle-optimize-size.wasm
to reduce your Webpack dist's size.
Tested on Chrome 80.0.3987.122 on an i7-6600U laptop, formatting this C++ file takes 15ms with wastyle/dist/wastyle.wasm
and 40ms with wastyle/dist/wastyle-optimize-size.wasm
in average of 100 samples.
You'll need emcc
to build the Astyle WASM binary. Follow this guide to install it.
$ yarn build # Build TypeSrript
$ yarn build:wasm # Build WASM
This project is licensed under the MIT license.
Astyle code formatter (Artistic Style) is also licensed under the MIT license. You can find its license in the astyle
folder.
FAQs
AStyle code formatter compiled to WebAssembly, for Node.js and the browser.
The npm package wastyle receives a total of 353 weekly downloads. As such, wastyle popularity was classified as not popular.
We found that wastyle demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.