@afterpack/wasm 0.2.5 → 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 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) {
@@ -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 callEngine(call) {
37
- bind();
37
+ export function guardEngineCall(call, memory, drop) {
38
38
  try {
39
39
  return call();
40
40
  }
41
41
  catch (cause) {
42
- dropInstance();
43
- throw cause;
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@afterpack/wasm",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "The AfterPack JavaScript obfuscator as WebAssembly, for Cloudflare Workers and Node",
5
5
  "type": "module",
6
6
  "main": "./dist/node.js",
package/readme.md CHANGED
@@ -76,7 +76,7 @@ Without a key, all of Free runs inside your Worker or Node process, with every p
76
76
  | Measure | Bytes | |
77
77
  |---|---:|---|
78
78
  | Raw `.wasm` | 4,569,613 | 4.36 MiB |
79
- | Gzipped | 1,306,547 | 1.25 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