@afterpack/wasm
v1.2.2
Published
The AfterPack JavaScript obfuscator as WebAssembly, for Cloudflare Workers and Node
Maintainers
Readme
@afterpack/wasm
The AfterPack JavaScript obfuscator as WebAssembly. Use it where a native Node addon can't load, for example to obfuscate JavaScript in a Cloudflare Worker. It also runs in Node.
For a normal Node build, use @afterpack/core. It has the same obfuscate and obfuscateAll API, runs natively and keeps the work off the main thread. Want to try AfterPack first? The playground obfuscates JavaScript in your browser.
Install
npm install @afterpack/wasmIn a Cloudflare Worker
No init step, no nodejs_compat flag and no extra wrangler rules.
import { obfuscate } from "@afterpack/wasm";
export default {
async fetch(request) {
const source = await request.text();
const result = await obfuscate({ path: "app.js", source }, { preset: "hard" });
return new Response(result.bytes, {
headers: { "content-type": "application/javascript" },
});
},
};Pro from a Worker
Add your Pro key to the same call. The build then runs on AfterPack's cloud with Pro features, and files too big for a Worker move to containers automatically. The Pro build never ships into your Worker.
export default {
async fetch(request, env) {
const source = await request.text();
const result = await obfuscate(
{ path: "app.js", source },
{ preset: "hard", key: env.AFTERPACK_KEY }, // your key, stored as a Worker secret
);
return new Response(result.bytes, {
headers: { "content-type": "application/javascript" },
});
},
};Tip: each Pro call is a metered build, so cache the result for a rotation window. Key the Cache API by the asset and the window, and each asset is built once per window instead of on every request.
The full guide: obfuscate JavaScript in a Cloudflare Worker.
In Node
Same import. The package picks its Node entry automatically.
import { obfuscate, obfuscateAll, version } from "@afterpack/wasm";
const result = await obfuscate('console.log("hello")', { preset: "hard" });
result.code; // the obfuscated source
result.bytes; // the same output as bytes, ready to write
// Several files at once. Results come back in input order.
const results = await obfuscateAll([
{ path: "dist/app.js", source: appSource },
{ path: "dist/vendor.js", source: vendorSource, sourceMap: vendorMap },
]);
await version(); // "0.2.1"obfuscate accepts a string, a Uint8Array, or a file object { path, source, sourceMap, sourceType }, where sourceMap is the file's existing map to chain and sourceType ("auto", "module" or "script") overrides the build's for that file. Each result has code, bytes, sourceMap and diagnostics. The second argument is the config. See the configuration reference and the presets.
A tool that wraps AfterPack can name itself to the cloud in an optional third argument, obfuscate(source, config, { client: "my-tool/1.0.0" }). It never changes the output.
Errors
If a build can't ship safely, the call throws an ObfuscationError with the engine's diagnostics. That covers an invalid config, a blocking diagnostic, or output that would go out unprotected.
import { obfuscate, ObfuscationError } from "@afterpack/wasm";
try {
await obfuscate(source);
} catch (err) {
if (err instanceof ObfuscationError) console.error(err.diagnostics);
throw err;
}Pro
Without a key, all of Free runs inside your Worker or Node process, with every preset. Add a Pro key ({ key: env.AFTERPACK_KEY }) and the same call runs the build in AfterPack's cloud instead. Pro adds per-file and per-region direction through directives, stores each build's Protection Map, and shows your builds in the dashboard. It also offers two opt-in hardening settings, transforms.selfIntegrity and transforms.comparisonHardening, both off by default (details). It does not make any transform stronger, and none of it is part of this package. See deployment modes, AfterPack Pro and pricing.
Size
| Measure | Bytes | |
|---|---:|---|
| Raw .wasm | 4,642,918 | 4.43 MiB |
| Gzipped | 1,301,444 | 1.24 MiB |
This fits Cloudflare's Worker size limit on both the free and paid plans. CPU time is the tighter limit: the free plan allows 10 ms per request, and obfuscating a 185 KB bundle at complexity: 8 takes about 263 ms on an Apple M-series laptop. Plan on Workers Paid for real bundles.
Limits
- No browser entry. It runs in Workers and Node.
- Each file is one synchronous engine call that holds the isolate until it finishes.
obfuscateAllprocesses files one at a time.
License
Free for any use, including commercial and CI, with no limits, and the output is yours. You can't redistribute the binary on its own, build a competing obfuscation product or service on it, or reverse engineer it. See the AfterPack Engine License.
Questions and feedback: get in touch. Bug reports: GitHub.
