@afterpack/wasm 0.2.3 → 0.2.5

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
@@ -2,7 +2,6 @@ import { cloudApiBase } from "./cloud-base.js";
2
2
  import { ObfuscationError } from "./contract.js";
3
3
  import { engineVersion, processFile, randomSeed } from "./engine.js";
4
4
  export * from "./contract.js";
5
- const DIAG_OUTPUT_UNPARSEABLE = "DIAG_OUTPUT_UNPARSEABLE";
6
5
  function engineCrash(cause) {
7
6
  const error = new ObfuscationError("AfterPack engine hit an internal limit while processing this input in this environment " +
8
7
  "(the build was not completed)", []);
@@ -22,6 +21,22 @@ function enginePath(file, index, total) {
22
21
  return file.path;
23
22
  return total === 1 ? "input.js" : `input-${index}.js`;
24
23
  }
24
+ const SOURCE_TYPES = ["auto", "module", "script"];
25
+ function fileSourceTypes(files) {
26
+ return files.map((file, i) => {
27
+ const kind = file.sourceType;
28
+ if (kind === undefined || kind === null)
29
+ return undefined;
30
+ if (typeof kind === "string" && SOURCE_TYPES.includes(kind))
31
+ return kind;
32
+ const key = `files[${i}].sourceType`;
33
+ throw new ObfuscationError(`AfterPack refused this build: invalid engine configuration at \`${key}\` -- expected ` +
34
+ '"auto", "module" or "script". No output was emitted. AfterPack never falls back to ' +
35
+ "default settings on a bad config: a build that silently ignored your configuration " +
36
+ `would ship far weaker protection than you asked for. Fix or remove \`${key}\` where ` +
37
+ "you configure AfterPack (your plugin options or CLI flags) and rebuild.", []);
38
+ });
39
+ }
25
40
  function blockingMessage(diagnostics) {
26
41
  const messages = diagnostics
27
42
  .filter((d) => d.severity === "error" || d.severity === "critical")
@@ -59,11 +74,14 @@ function engineConfigWithoutKey(config) {
59
74
  const { key: _key, ...rest } = config;
60
75
  return rest;
61
76
  }
62
- function buildCloudForm(files, paths, sources, engineConfig) {
77
+ function buildCloudForm(files, paths, sources, kinds, engineConfig) {
63
78
  const manifestFiles = files.map((file, i) => {
64
79
  const entry = { filePath: paths[i], partName: `file-${i}` };
65
80
  if (file.sourceMap !== undefined)
66
81
  entry.sourceMapPartName = `map-${i}`;
82
+ const kind = kinds[i];
83
+ if (kind !== undefined)
84
+ entry.config = { sourceType: kind };
67
85
  return entry;
68
86
  });
69
87
  const form = new FormData();
@@ -311,8 +329,8 @@ function inRequestOrder(paths, results) {
311
329
  return results[at];
312
330
  });
313
331
  }
314
- async function obfuscateAllCloud(files, paths, sources, key, config) {
315
- const form = buildCloudForm(files, paths, sources, engineConfigWithoutKey(config));
332
+ async function obfuscateAllCloud(files, paths, sources, kinds, key, config) {
333
+ const form = buildCloudForm(files, paths, sources, kinds, engineConfigWithoutKey(config));
316
334
  const response = await requestCloudBatch(key, form);
317
335
  const boundary = extractBoundary(response.headers.get("content-type"));
318
336
  if (boundary === null) {
@@ -334,13 +352,18 @@ async function obfuscateAllCloud(files, paths, sources, key, config) {
334
352
  export async function obfuscateAll(files, config) {
335
353
  const paths = files.map((file, i) => enginePath(file, i, files.length));
336
354
  const sources = files.map((file) => sourceToString(file.source));
355
+ const kinds = fileSourceTypes(files);
337
356
  const build = config?.seed === undefined ? { ...config, seed: randomSeed() } : config;
338
357
  if (build.key !== undefined) {
339
- return obfuscateAllCloud(files, paths, sources, build.key, build);
358
+ return obfuscateAllCloud(files, paths, sources, kinds, build.key, build);
340
359
  }
341
360
  let results;
342
361
  try {
343
- results = files.map((file, i) => processFile(sources[i], file.sourceMap ?? "", JSON.stringify({ ...build, filePath: paths[i] })));
362
+ results = files.map((file, i) => processFile(sources[i], file.sourceMap ?? "", JSON.stringify({
363
+ ...build,
364
+ filePath: paths[i],
365
+ ...(kinds[i] === undefined ? {} : { sourceType: kinds[i] }),
366
+ })));
344
367
  }
345
368
  catch (cause) {
346
369
  throw engineCrash(cause);
@@ -353,15 +376,9 @@ export async function obfuscateAll(files, config) {
353
376
  const detail = failed.map((file) => `${file.path}: ${file.error}`).join("; ");
354
377
  throw new ObfuscationError(`AfterPack refused this build — ${detail}`, everyDiagnostic);
355
378
  }
356
- const cleartext = results
357
- .map((result, i) => ({
358
- path: paths[i],
359
- discarded: result.diagnostics.some((d) => d.code === DIAG_OUTPUT_UNPARSEABLE) ||
360
- (result.code === "" && sources[i].trim() !== ""),
361
- }))
362
- .filter((file) => file.discarded);
379
+ const cleartext = paths.filter((_, i) => results[i].code === "" && sources[i].trim() !== "");
363
380
  if (cleartext.length > 0) {
364
- const named = cleartext.map((file) => file.path).join(", ");
381
+ const named = cleartext.join(", ");
365
382
  throw new ObfuscationError(`AfterPack refused this build — obfuscation was discarded and ${named} would ship as cleartext`, everyDiagnostic);
366
383
  }
367
384
  return results.map((result, i) => new ObfuscatedFile(files[i].path ?? null, result.code, result.sourceMap, result.diagnostics, result.protectionMap ?? null));
@@ -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?: "auto" | "module" | "script";
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";
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@afterpack/wasm",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
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
@@ -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,546,077 | 4.34 MiB |
79
- | Gzipped | 1,301,647 | 1.24 MiB |
78
+ | Raw `.wasm` | 4,569,613 | 4.36 MiB |
79
+ | Gzipped | 1,306,547 | 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