@afterpack/wasm 0.2.4 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.js +30 -11
- package/dist/contract.d.ts +6 -2
- package/dist/crash.d.ts +18 -0
- package/dist/crash.js +45 -0
- package/dist/engine.d.ts +1 -0
- package/dist/engine.js +9 -4
- package/engine/afterpack_engine_bg.wasm +0 -0
- package/package.json +1 -1
- package/readme.md +3 -3
package/dist/api.js
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
1
|
import { cloudApiBase } from "./cloud-base.js";
|
|
2
2
|
import { ObfuscationError } from "./contract.js";
|
|
3
|
+
import { engineCrash } from "./crash.js";
|
|
3
4
|
import { engineVersion, processFile, randomSeed } from "./engine.js";
|
|
4
5
|
export * from "./contract.js";
|
|
5
|
-
function engineCrash(cause) {
|
|
6
|
-
const error = new ObfuscationError("AfterPack engine hit an internal limit while processing this input in this environment " +
|
|
7
|
-
"(the build was not completed)", []);
|
|
8
|
-
error.cause = cause;
|
|
9
|
-
return error;
|
|
10
|
-
}
|
|
11
6
|
const decoder = new TextDecoder();
|
|
12
7
|
const encoder = new TextEncoder();
|
|
13
8
|
function sourceToString(source) {
|
|
@@ -21,6 +16,22 @@ function enginePath(file, index, total) {
|
|
|
21
16
|
return file.path;
|
|
22
17
|
return total === 1 ? "input.js" : `input-${index}.js`;
|
|
23
18
|
}
|
|
19
|
+
const SOURCE_TYPES = ["auto", "module", "script"];
|
|
20
|
+
function fileSourceTypes(files) {
|
|
21
|
+
return files.map((file, i) => {
|
|
22
|
+
const kind = file.sourceType;
|
|
23
|
+
if (kind === undefined || kind === null)
|
|
24
|
+
return undefined;
|
|
25
|
+
if (typeof kind === "string" && SOURCE_TYPES.includes(kind))
|
|
26
|
+
return kind;
|
|
27
|
+
const key = `files[${i}].sourceType`;
|
|
28
|
+
throw new ObfuscationError(`AfterPack refused this build: invalid engine configuration at \`${key}\` -- expected ` +
|
|
29
|
+
'"auto", "module" or "script". No output was emitted. AfterPack never falls back to ' +
|
|
30
|
+
"default settings on a bad config: a build that silently ignored your configuration " +
|
|
31
|
+
`would ship far weaker protection than you asked for. Fix or remove \`${key}\` where ` +
|
|
32
|
+
"you configure AfterPack (your plugin options or CLI flags) and rebuild.", []);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
24
35
|
function blockingMessage(diagnostics) {
|
|
25
36
|
const messages = diagnostics
|
|
26
37
|
.filter((d) => d.severity === "error" || d.severity === "critical")
|
|
@@ -58,11 +69,14 @@ function engineConfigWithoutKey(config) {
|
|
|
58
69
|
const { key: _key, ...rest } = config;
|
|
59
70
|
return rest;
|
|
60
71
|
}
|
|
61
|
-
function buildCloudForm(files, paths, sources, engineConfig) {
|
|
72
|
+
function buildCloudForm(files, paths, sources, kinds, engineConfig) {
|
|
62
73
|
const manifestFiles = files.map((file, i) => {
|
|
63
74
|
const entry = { filePath: paths[i], partName: `file-${i}` };
|
|
64
75
|
if (file.sourceMap !== undefined)
|
|
65
76
|
entry.sourceMapPartName = `map-${i}`;
|
|
77
|
+
const kind = kinds[i];
|
|
78
|
+
if (kind !== undefined)
|
|
79
|
+
entry.config = { sourceType: kind };
|
|
66
80
|
return entry;
|
|
67
81
|
});
|
|
68
82
|
const form = new FormData();
|
|
@@ -310,8 +324,8 @@ function inRequestOrder(paths, results) {
|
|
|
310
324
|
return results[at];
|
|
311
325
|
});
|
|
312
326
|
}
|
|
313
|
-
async function obfuscateAllCloud(files, paths, sources, key, config) {
|
|
314
|
-
const form = buildCloudForm(files, paths, sources, engineConfigWithoutKey(config));
|
|
327
|
+
async function obfuscateAllCloud(files, paths, sources, kinds, key, config) {
|
|
328
|
+
const form = buildCloudForm(files, paths, sources, kinds, engineConfigWithoutKey(config));
|
|
315
329
|
const response = await requestCloudBatch(key, form);
|
|
316
330
|
const boundary = extractBoundary(response.headers.get("content-type"));
|
|
317
331
|
if (boundary === null) {
|
|
@@ -333,13 +347,18 @@ async function obfuscateAllCloud(files, paths, sources, key, config) {
|
|
|
333
347
|
export async function obfuscateAll(files, config) {
|
|
334
348
|
const paths = files.map((file, i) => enginePath(file, i, files.length));
|
|
335
349
|
const sources = files.map((file) => sourceToString(file.source));
|
|
350
|
+
const kinds = fileSourceTypes(files);
|
|
336
351
|
const build = config?.seed === undefined ? { ...config, seed: randomSeed() } : config;
|
|
337
352
|
if (build.key !== undefined) {
|
|
338
|
-
return obfuscateAllCloud(files, paths, sources, build.key, build);
|
|
353
|
+
return obfuscateAllCloud(files, paths, sources, kinds, build.key, build);
|
|
339
354
|
}
|
|
340
355
|
let results;
|
|
341
356
|
try {
|
|
342
|
-
results = files.map((file, i) => processFile(sources[i], file.sourceMap ?? "", JSON.stringify({
|
|
357
|
+
results = files.map((file, i) => processFile(sources[i], file.sourceMap ?? "", JSON.stringify({
|
|
358
|
+
...build,
|
|
359
|
+
filePath: paths[i],
|
|
360
|
+
...(kinds[i] === undefined ? {} : { sourceType: kinds[i] }),
|
|
361
|
+
})));
|
|
343
362
|
}
|
|
344
363
|
catch (cause) {
|
|
345
364
|
throw engineCrash(cause);
|
package/dist/contract.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/** A named protection level. Sets the `complexity` target; output size follows from it. */
|
|
2
2
|
export type Preset = "minify" | "light" | "medium" | "hard" | "extreme";
|
|
3
|
+
/** Whether an input is an ES module or a classic script. `auto` decides from the source, treating an `.mjs` path as a module. */
|
|
4
|
+
export type SourceType = "auto" | "module" | "script";
|
|
3
5
|
/** A kind of transformation that a region can allow or deny. */
|
|
4
6
|
export type TransformKind = ConfigurableTransform | "stringEncoding";
|
|
5
7
|
type ConfigurableTransform = "controlFlowFlatten" | "opaquePredicate" | "mixedBooleanArithmetic" | "integerBytecode" | "crossDependency" | "scopeDeepen" | "comparisonHardening" | "selfIntegrity" | "objectConstruction";
|
|
@@ -53,8 +55,8 @@ export interface AfterpackConfig {
|
|
|
53
55
|
key?: string;
|
|
54
56
|
/** A named protection level. Defaults to `light`. */
|
|
55
57
|
preset?: Preset;
|
|
56
|
-
/** Whether the input is an ES module or a classic script. `auto` (the default) decides from the source, treating an `.mjs` path as a module. Set `module` for module output that has no `import`/`export`. */
|
|
57
|
-
sourceType?:
|
|
58
|
+
/** Whether the input is an ES module or a classic script. `auto` (the default) decides from the source, treating an `.mjs` path as a module. Set `module` for module output that has no `import`/`export`. A file's own `sourceType` overrides it. */
|
|
59
|
+
sourceType?: SourceType;
|
|
58
60
|
/** How much protection to apply. `0` applies none; higher values apply more and grow the output. */
|
|
59
61
|
complexity?: number;
|
|
60
62
|
/** Runtime reflection your code relies on. A detected pattern that is not listed fails the build. */
|
|
@@ -122,6 +124,8 @@ export interface FileInput {
|
|
|
122
124
|
source: string | Uint8Array;
|
|
123
125
|
/** The file's existing source map, so the output map points back to your original source. */
|
|
124
126
|
sourceMap?: string;
|
|
127
|
+
/** This file's kind, overriding the build's `sourceType`, for a build that mixes ES modules and classic scripts. */
|
|
128
|
+
sourceType?: SourceType;
|
|
125
129
|
}
|
|
126
130
|
/** `info` never blocks a build; `error` and `critical` do. */
|
|
127
131
|
export type Severity = "info" | "error" | "critical";
|
package/dist/crash.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ObfuscationError } from "./contract.js";
|
|
2
|
+
export declare const DIAG_OUT_OF_MEMORY = "DIAG_OUT_OF_MEMORY";
|
|
3
|
+
/** The engine declares no memory maximum, so wasm32's 65,536 pages are its ceiling. */
|
|
4
|
+
export declare const WASM32_MAX_MEMORY_BYTES: number;
|
|
5
|
+
/**
|
|
6
|
+
* A request that fails with less than this committed must itself be larger than
|
|
7
|
+
* this, so an `unreachable` trap above `WASM32_MAX_MEMORY_BYTES - OOM_HEADROOM_BYTES`
|
|
8
|
+
* is classified as exhaustion, and one below it is missed only if a single
|
|
9
|
+
* allocation asked for more than 2 GiB. The engine's measured working sets are
|
|
10
|
+
* hundreds of MiB, so a panic up there is not the expected story.
|
|
11
|
+
*/
|
|
12
|
+
export declare const OOM_HEADROOM_BYTES: number;
|
|
13
|
+
/**
|
|
14
|
+
* Whether an engine trap is memory exhaustion. `memoryBytes` is the instance's
|
|
15
|
+
* committed linear memory at the moment of the trap, when the caller has it.
|
|
16
|
+
*/
|
|
17
|
+
export declare function isOutOfMemory(cause: unknown, memoryBytes?: number): boolean;
|
|
18
|
+
export declare function engineCrash(cause: unknown, memoryBytes?: number): ObfuscationError;
|
package/dist/crash.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { ObfuscationError } from "./contract.js";
|
|
2
|
+
export const DIAG_OUT_OF_MEMORY = "DIAG_OUT_OF_MEMORY";
|
|
3
|
+
export const WASM32_MAX_MEMORY_BYTES = 65_536 * 65_536;
|
|
4
|
+
export const OOM_HEADROOM_BYTES = 2 * 1024 * 1024 * 1024;
|
|
5
|
+
function isHostAllocationError(cause) {
|
|
6
|
+
const text = `${cause.name} ${cause.message}`.toLowerCase();
|
|
7
|
+
return (text.includes("out of memory") ||
|
|
8
|
+
text.includes("memory size exceeded") ||
|
|
9
|
+
text.includes("array buffer allocation failed") ||
|
|
10
|
+
text.includes("cannot allocate") ||
|
|
11
|
+
text.includes("failed to allocate"));
|
|
12
|
+
}
|
|
13
|
+
export function isOutOfMemory(cause, memoryBytes) {
|
|
14
|
+
if (!(cause instanceof Error))
|
|
15
|
+
return false;
|
|
16
|
+
if (isHostAllocationError(cause))
|
|
17
|
+
return true;
|
|
18
|
+
return (cause instanceof WebAssembly.RuntimeError &&
|
|
19
|
+
/unreachable/i.test(cause.message) &&
|
|
20
|
+
memoryBytes !== undefined &&
|
|
21
|
+
memoryBytes >= WASM32_MAX_MEMORY_BYTES - OOM_HEADROOM_BYTES);
|
|
22
|
+
}
|
|
23
|
+
export function engineCrash(cause, memoryBytes) {
|
|
24
|
+
if (cause instanceof ObfuscationError)
|
|
25
|
+
return cause;
|
|
26
|
+
if (isOutOfMemory(cause, memoryBytes)) {
|
|
27
|
+
const message = "AfterPack ran out of memory obfuscating this input in this environment, so the build was " +
|
|
28
|
+
"not completed. Lower `complexity` or `inflation.max`, split the bundle, or exclude the " +
|
|
29
|
+
"largest file, then rebuild.";
|
|
30
|
+
const diagnostic = {
|
|
31
|
+
severity: "error",
|
|
32
|
+
code: DIAG_OUT_OF_MEMORY,
|
|
33
|
+
message,
|
|
34
|
+
file: null,
|
|
35
|
+
span: null,
|
|
36
|
+
};
|
|
37
|
+
const error = new ObfuscationError(message, [diagnostic]);
|
|
38
|
+
error.cause = cause;
|
|
39
|
+
return error;
|
|
40
|
+
}
|
|
41
|
+
const error = new ObfuscationError("AfterPack engine hit an internal limit while processing this input in this environment " +
|
|
42
|
+
"(the build was not completed)", []);
|
|
43
|
+
error.cause = cause;
|
|
44
|
+
return error;
|
|
45
|
+
}
|
package/dist/engine.d.ts
CHANGED
|
@@ -10,5 +10,6 @@ export declare function useModule(source: ModuleSource): void;
|
|
|
10
10
|
export type RandomFill = (words: Uint32Array) => void;
|
|
11
11
|
export declare function useRandom(fill: RandomFill): void;
|
|
12
12
|
export declare function randomSeed(): number;
|
|
13
|
+
export declare function guardEngineCall<T>(call: () => T, memory: () => WebAssembly.Memory | null, drop: () => void): T;
|
|
13
14
|
export declare function engineVersion(): string;
|
|
14
15
|
export declare function processFile(source: string, inputSourceMap: string, configJson: string): EngineOutput;
|
package/dist/engine.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as glue from "../engine/afterpack_engine_bg.js";
|
|
2
|
+
import { engineCrash } from "./crash.js";
|
|
2
3
|
let loadModule = null;
|
|
3
4
|
let bound = false;
|
|
4
5
|
let boundMemory = null;
|
|
@@ -33,16 +34,20 @@ function dropInstance() {
|
|
|
33
34
|
boundMemory?.grow(0);
|
|
34
35
|
boundMemory = null;
|
|
35
36
|
}
|
|
36
|
-
function
|
|
37
|
-
bind();
|
|
37
|
+
export function guardEngineCall(call, memory, drop) {
|
|
38
38
|
try {
|
|
39
39
|
return call();
|
|
40
40
|
}
|
|
41
41
|
catch (cause) {
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
const memoryBytes = memory()?.buffer.byteLength;
|
|
43
|
+
drop();
|
|
44
|
+
throw engineCrash(cause, memoryBytes);
|
|
44
45
|
}
|
|
45
46
|
}
|
|
47
|
+
function callEngine(call) {
|
|
48
|
+
bind();
|
|
49
|
+
return guardEngineCall(call, () => boundMemory, dropInstance);
|
|
50
|
+
}
|
|
46
51
|
export function engineVersion() {
|
|
47
52
|
return callEngine(() => glue.version());
|
|
48
53
|
}
|
|
Binary file
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -50,7 +50,7 @@ const results = await obfuscateAll([
|
|
|
50
50
|
await version(); // "0.2.1"
|
|
51
51
|
```
|
|
52
52
|
|
|
53
|
-
`obfuscate` accepts a string, a `Uint8Array`, or a file object `{ path, source, sourceMap }`, where `sourceMap` is the file's existing map to chain. Each result has `code`, `bytes`, `sourceMap` and `diagnostics`. The second argument is the config. See the [configuration reference](https://www.afterpack.dev/docs/config) and the [presets](https://www.afterpack.dev/docs/presets).
|
|
53
|
+
`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](https://www.afterpack.dev/docs/config) and the [presets](https://www.afterpack.dev/docs/presets).
|
|
54
54
|
|
|
55
55
|
## Errors
|
|
56
56
|
|
|
@@ -75,8 +75,8 @@ Without a key, all of Free runs inside your Worker or Node process, with every p
|
|
|
75
75
|
|
|
76
76
|
| Measure | Bytes | |
|
|
77
77
|
|---|---:|---|
|
|
78
|
-
| Raw `.wasm` | 4,
|
|
79
|
-
| Gzipped | 1,306,
|
|
78
|
+
| Raw `.wasm` | 4,569,613 | 4.36 MiB |
|
|
79
|
+
| Gzipped | 1,306,553 | 1.25 MiB |
|
|
80
80
|
|
|
81
81
|
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.
|
|
82
82
|
|