@afterpack/angular 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/README.md CHANGED
@@ -77,7 +77,8 @@ instead.
77
77
  Without a key, AfterPack runs on your machine with the full pipeline at any preset. Set
78
78
  [`AFTERPACK_KEY`](https://www.afterpack.dev/docs/config#key) in your environment and the same step
79
79
  builds in AfterPack's cloud instead, which adds two hardening transforms you can turn on:
80
- self-integrity (anti-tamper) and comparison hardening. See [AfterPack
80
+ self-integrity, which detects edits to its own check table only, not edits
81
+ elsewhere in your code, and comparison hardening. See [AfterPack
81
82
  Pro](https://www.afterpack.dev/docs/pro).
82
83
 
83
84
  ## Links
package/dist/index.cjs CHANGED
@@ -25,7 +25,6 @@ const node_path = __toESM(require("node:path"));
25
25
  const __afterpack_core = __toESM(require("@afterpack/core"));
26
26
  const __afterpack_integration_utils = __toESM(require("@afterpack/integration-utils"));
27
27
  const node_fs = __toESM(require("node:fs"));
28
- const node_crypto = __toESM(require("node:crypto"));
29
28
 
30
29
  //#region src/browser-dir.ts
31
30
  function findAngularBrowserDir(distRoot) {
@@ -39,69 +38,6 @@ function findAngularBrowserDir(distRoot) {
39
38
  throw new Error(`@afterpack/angular: could not locate an Angular browser output under ${distRoot}; pass browserDir explicitly`);
40
39
  }
41
40
 
42
- //#endregion
43
- //#region src/integrity.ts
44
- const SRI_ATTR = /\b(src|href)\s*=\s*"([^"]+)"[^>]*\bintegrity\s*=\s*"(sha(?:256|384|512))-[^"]*"/g;
45
- const SRI_ATTR_REVERSED = /\bintegrity\s*=\s*"(sha(?:256|384|512))-[^"]*"[^>]*\b(src|href)\s*=\s*"([^"]+)"/g;
46
- function digest(bytes, algo, enc) {
47
- return (0, node_crypto.createHash)(algo).update(bytes).digest(enc);
48
- }
49
- function fileFor(baseDir, ref) {
50
- if (/^[a-z][a-z0-9+.-]*:/i.test(ref) || ref.startsWith("//")) return null;
51
- const path = ref.split(/[?#]/)[0];
52
- if (path === "") return null;
53
- return (0, node_path.join)(baseDir, path);
54
- }
55
- function recomputeHtmlIntegrity(browserDir) {
56
- for (const html of htmlFilesUnder(browserDir)) {
57
- const text = (0, node_fs.readFileSync)(html, "utf8");
58
- const rewrite = (algo, ref, match) => {
59
- const file = fileFor(browserDir, ref);
60
- if (file === null || !(0, node_fs.existsSync)(file)) return match;
61
- const hash = digest((0, node_fs.readFileSync)(file), algo, "base64");
62
- return match.replace(/integrity\s*=\s*"sha(?:256|384|512)-[^"]*"/, `integrity="${algo}-${hash}"`);
63
- };
64
- let next = text.replace(SRI_ATTR, (match, _attr, ref, algo) => rewrite(algo, ref, match));
65
- next = next.replace(SRI_ATTR_REVERSED, (match, algo, _attr, ref) => rewrite(algo, ref, match));
66
- if (next !== text) (0, node_fs.writeFileSync)(html, next);
67
- }
68
- }
69
- function htmlFilesUnder(dir) {
70
- const out = [];
71
- for (const entry of (0, node_fs.readdirSync)(dir, {
72
- recursive: true,
73
- encoding: "utf8"
74
- })) if (entry.endsWith(".html")) out.push((0, node_path.join)(dir, entry));
75
- return out;
76
- }
77
- function recomputeNgswHashTable(browserDir) {
78
- const ngsw = (0, node_path.join)(browserDir, "ngsw.json");
79
- if (!(0, node_fs.existsSync)(ngsw)) return;
80
- const config = JSON.parse((0, node_fs.readFileSync)(ngsw, "utf8"));
81
- const table = config.hashTable;
82
- if (!table) return;
83
- for (const url of Object.keys(table)) {
84
- const file = (0, node_path.join)(browserDir, url.replace(/^\//, ""));
85
- if (!(0, node_fs.existsSync)(file) || !(0, node_fs.statSync)(file).isFile()) throw new Error(`@afterpack/angular: ngsw.json names ${url}, which is not in the browser output after obfuscation. AfterPack cannot recompute the service worker hash table. Rebuild, or disable the service worker for this build.`);
86
- table[url] = (0, node_crypto.createHash)("sha1").update((0, node_fs.readFileSync)(file)).digest("hex");
87
- }
88
- (0, node_fs.writeFileSync)(ngsw, `${JSON.stringify(config, null, 2)}\n`);
89
- }
90
- function refuseStaleServerManifest(outputRoot) {
91
- const serverDir = (0, node_path.join)(outputRoot, "server");
92
- if (!(0, node_fs.existsSync)(serverDir)) return;
93
- for (const entry of (0, node_fs.readdirSync)(serverDir)) {
94
- if (!/angular-app-(?:engine-)?manifest\.mjs$/.test(entry)) continue;
95
- const text = (0, node_fs.readFileSync)((0, node_path.join)(serverDir, entry), "utf8");
96
- if (/\bhash\s*:\s*['"`]/.test(text) || /\bintegrity\s*:/.test(text)) throw new Error(`@afterpack/angular: ${entry} embeds asset hashes that AfterPack cannot recompute after renaming and obfuscating the browser output, so the SSR ETags would be stale. This Angular server build is not supported — obfuscate only the browser output, or remove @afterpack/angular from the SSR build.`);
97
- }
98
- }
99
- function recomputeAngularIntegrity(outputRoot, browserDir) {
100
- refuseStaleServerManifest(outputRoot);
101
- recomputeHtmlIntegrity(browserDir);
102
- recomputeNgswHashTable(browserDir);
103
- }
104
-
105
41
  //#endregion
106
42
  //#region src/source-type.ts
107
43
  const SCRIPT_TAG_RE = /<script\b([^>]*)>/gi;
@@ -180,6 +116,17 @@ function angularSourceType(kinds) {
180
116
  if (kinds.module.length === 0) return void 0;
181
117
  return kinds.classic.length === 0 && kinds.unclaimed.length === 0 ? "module" : void 0;
182
118
  }
119
+ function angularFileSourceTypes(kinds, files) {
120
+ const module$1 = new Set(kinds.module);
121
+ const classic = new Set(kinds.classic);
122
+ const out = new Map();
123
+ for (const file of files) {
124
+ const path = (0, node_path.resolve)(file);
125
+ if (module$1.has(path)) out.set(file, "module");
126
+ else if (classic.has(path)) out.set(file, "script");
127
+ }
128
+ return out;
129
+ }
183
130
 
184
131
  //#endregion
185
132
  //#region src/index.ts
@@ -190,8 +137,6 @@ const LOCAL_KEYS = [
190
137
  ];
191
138
  const DIRECTIVES_UNSUPPORTED = "the Angular application builder is sealed (no plugin hook), so this postbuild pass only ever sees already-minified output and cannot capture `/* @afterpack ... */` comments. Remove the key; to use directives, build through a bundler AfterPack can hook (@afterpack/vite, @afterpack/webpack, @afterpack/esbuild).";
192
139
  const DIRECTIVES_ALWAYS_DISABLED = false;
193
- const ESBUILD_HASHED_NAME_RE = /-([A-Z0-9]{8})\.(?:js|mjs|css)$/;
194
- const angularHashToken = (relativePath) => ESBUILD_HASHED_NAME_RE.exec((0, node_path.basename)(relativePath))?.[1] ?? null;
195
140
  function applicationOutputRoot(browserDir) {
196
141
  return (0, node_path.basename)(browserDir) === "browser" ? (0, node_path.dirname)(browserDir) : browserDir;
197
142
  }
@@ -214,6 +159,7 @@ async function afterpackAngular(options = {}) {
214
159
  const files = (0, __afterpack_integration_utils.collectJsFiles)(browserDir, { include: settings.paths?.include });
215
160
  if (files.length === 0) throw new Error(`@afterpack/angular: no .js/.mjs/.cjs files found under ${browserDir}`);
216
161
  const outputRoot = applicationOutputRoot(browserDir);
162
+ const kinds = classifyAngularScripts(browserDir, files);
217
163
  await (0, __afterpack_integration_utils.runObfuscationPass)({
218
164
  files,
219
165
  engine: {
@@ -229,15 +175,16 @@ async function afterpackAngular(options = {}) {
229
175
  sourceMapSweepDir: browserDir,
230
176
  hashedNames: {
231
177
  root: outputRoot,
232
- tokenOf: angularHashToken
178
+ tokenOf: __afterpack_integration_utils.angularHashToken
233
179
  },
234
180
  ...(0, __afterpack_integration_utils.passSettings)(resolved),
235
- sourceType: angularSourceType(classifyAngularScripts(browserDir, files)),
181
+ sourceType: angularSourceType(kinds),
182
+ fileSourceTypes: angularFileSourceTypes(kinds, files),
236
183
  directivesEnabled: DIRECTIVES_ALWAYS_DISABLED
237
184
  });
238
- recomputeAngularIntegrity(outputRoot, browserDir);
185
+ (0, __afterpack_integration_utils.recomputeAngularIntegrity)(outputRoot, browserDir);
239
186
  }
240
187
 
241
188
  //#endregion
242
189
  exports.afterpackAngular = afterpackAngular;
243
- exports.angularHashToken = angularHashToken;
190
+ exports.angularHashToken = __afterpack_integration_utils.angularHashToken;
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { AfterpackPluginOptions, HashTokenOf } from "@afterpack/integration-utils";
1
+ import { AfterpackPluginOptions, angularHashToken } from "@afterpack/integration-utils";
2
2
 
3
3
  //#region src/index.d.ts
4
4
  type AfterpackAngularOptions = Omit<AfterpackPluginOptions, "directives"> & {
@@ -6,7 +6,6 @@ type AfterpackAngularOptions = Omit<AfterpackPluginOptions, "directives"> & {
6
6
  distRoot?: string;
7
7
  browserDir?: string;
8
8
  };
9
- declare const angularHashToken: HashTokenOf;
10
9
  declare function afterpackAngular(options?: AfterpackAngularOptions): Promise<void>;
11
10
  //#endregion
12
11
  export { AfterpackAngularOptions, afterpackAngular, angularHashToken };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AfterpackPluginOptions, HashTokenOf } from "@afterpack/integration-utils";
1
+ import { AfterpackPluginOptions, angularHashToken } from "@afterpack/integration-utils";
2
2
 
3
3
  //#region src/index.d.ts
4
4
  type AfterpackAngularOptions = Omit<AfterpackPluginOptions, "directives"> & {
@@ -6,7 +6,6 @@ type AfterpackAngularOptions = Omit<AfterpackPluginOptions, "directives"> & {
6
6
  distRoot?: string;
7
7
  browserDir?: string;
8
8
  };
9
- declare const angularHashToken: HashTokenOf;
10
9
  declare function afterpackAngular(options?: AfterpackAngularOptions): Promise<void>;
11
10
  //#endregion
12
11
  export { AfterpackAngularOptions, afterpackAngular, angularHashToken };
package/dist/index.js CHANGED
@@ -1,8 +1,7 @@
1
1
  import { basename, dirname, join, relative, resolve, sep } from "node:path";
2
2
  import { processBatch, version } from "@afterpack/core";
3
- import { collectJsFiles, createTelemetryReporter, passSettings, resolveClientIdentity, resolvePluginConfig, runObfuscationPass } from "@afterpack/integration-utils";
4
- import { existsSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
5
- import { createHash } from "node:crypto";
3
+ import { angularHashToken, collectJsFiles, createTelemetryReporter, passSettings, recomputeAngularIntegrity, resolveClientIdentity, resolvePluginConfig, runObfuscationPass } from "@afterpack/integration-utils";
4
+ import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
6
5
 
7
6
  //#region src/browser-dir.ts
8
7
  function findAngularBrowserDir(distRoot) {
@@ -16,69 +15,6 @@ function findAngularBrowserDir(distRoot) {
16
15
  throw new Error(`@afterpack/angular: could not locate an Angular browser output under ${distRoot}; pass browserDir explicitly`);
17
16
  }
18
17
 
19
- //#endregion
20
- //#region src/integrity.ts
21
- const SRI_ATTR = /\b(src|href)\s*=\s*"([^"]+)"[^>]*\bintegrity\s*=\s*"(sha(?:256|384|512))-[^"]*"/g;
22
- const SRI_ATTR_REVERSED = /\bintegrity\s*=\s*"(sha(?:256|384|512))-[^"]*"[^>]*\b(src|href)\s*=\s*"([^"]+)"/g;
23
- function digest(bytes, algo, enc) {
24
- return createHash(algo).update(bytes).digest(enc);
25
- }
26
- function fileFor(baseDir, ref) {
27
- if (/^[a-z][a-z0-9+.-]*:/i.test(ref) || ref.startsWith("//")) return null;
28
- const path = ref.split(/[?#]/)[0];
29
- if (path === "") return null;
30
- return join(baseDir, path);
31
- }
32
- function recomputeHtmlIntegrity(browserDir) {
33
- for (const html of htmlFilesUnder(browserDir)) {
34
- const text = readFileSync(html, "utf8");
35
- const rewrite = (algo, ref, match) => {
36
- const file = fileFor(browserDir, ref);
37
- if (file === null || !existsSync(file)) return match;
38
- const hash = digest(readFileSync(file), algo, "base64");
39
- return match.replace(/integrity\s*=\s*"sha(?:256|384|512)-[^"]*"/, `integrity="${algo}-${hash}"`);
40
- };
41
- let next = text.replace(SRI_ATTR, (match, _attr, ref, algo) => rewrite(algo, ref, match));
42
- next = next.replace(SRI_ATTR_REVERSED, (match, algo, _attr, ref) => rewrite(algo, ref, match));
43
- if (next !== text) writeFileSync(html, next);
44
- }
45
- }
46
- function htmlFilesUnder(dir) {
47
- const out = [];
48
- for (const entry of readdirSync(dir, {
49
- recursive: true,
50
- encoding: "utf8"
51
- })) if (entry.endsWith(".html")) out.push(join(dir, entry));
52
- return out;
53
- }
54
- function recomputeNgswHashTable(browserDir) {
55
- const ngsw = join(browserDir, "ngsw.json");
56
- if (!existsSync(ngsw)) return;
57
- const config = JSON.parse(readFileSync(ngsw, "utf8"));
58
- const table = config.hashTable;
59
- if (!table) return;
60
- for (const url of Object.keys(table)) {
61
- const file = join(browserDir, url.replace(/^\//, ""));
62
- if (!existsSync(file) || !statSync(file).isFile()) throw new Error(`@afterpack/angular: ngsw.json names ${url}, which is not in the browser output after obfuscation. AfterPack cannot recompute the service worker hash table. Rebuild, or disable the service worker for this build.`);
63
- table[url] = createHash("sha1").update(readFileSync(file)).digest("hex");
64
- }
65
- writeFileSync(ngsw, `${JSON.stringify(config, null, 2)}\n`);
66
- }
67
- function refuseStaleServerManifest(outputRoot) {
68
- const serverDir = join(outputRoot, "server");
69
- if (!existsSync(serverDir)) return;
70
- for (const entry of readdirSync(serverDir)) {
71
- if (!/angular-app-(?:engine-)?manifest\.mjs$/.test(entry)) continue;
72
- const text = readFileSync(join(serverDir, entry), "utf8");
73
- if (/\bhash\s*:\s*['"`]/.test(text) || /\bintegrity\s*:/.test(text)) throw new Error(`@afterpack/angular: ${entry} embeds asset hashes that AfterPack cannot recompute after renaming and obfuscating the browser output, so the SSR ETags would be stale. This Angular server build is not supported — obfuscate only the browser output, or remove @afterpack/angular from the SSR build.`);
74
- }
75
- }
76
- function recomputeAngularIntegrity(outputRoot, browserDir) {
77
- refuseStaleServerManifest(outputRoot);
78
- recomputeHtmlIntegrity(browserDir);
79
- recomputeNgswHashTable(browserDir);
80
- }
81
-
82
18
  //#endregion
83
19
  //#region src/source-type.ts
84
20
  const SCRIPT_TAG_RE = /<script\b([^>]*)>/gi;
@@ -157,6 +93,17 @@ function angularSourceType(kinds) {
157
93
  if (kinds.module.length === 0) return void 0;
158
94
  return kinds.classic.length === 0 && kinds.unclaimed.length === 0 ? "module" : void 0;
159
95
  }
96
+ function angularFileSourceTypes(kinds, files) {
97
+ const module = new Set(kinds.module);
98
+ const classic = new Set(kinds.classic);
99
+ const out = new Map();
100
+ for (const file of files) {
101
+ const path = resolve(file);
102
+ if (module.has(path)) out.set(file, "module");
103
+ else if (classic.has(path)) out.set(file, "script");
104
+ }
105
+ return out;
106
+ }
160
107
 
161
108
  //#endregion
162
109
  //#region src/index.ts
@@ -167,8 +114,6 @@ const LOCAL_KEYS = [
167
114
  ];
168
115
  const DIRECTIVES_UNSUPPORTED = "the Angular application builder is sealed (no plugin hook), so this postbuild pass only ever sees already-minified output and cannot capture `/* @afterpack ... */` comments. Remove the key; to use directives, build through a bundler AfterPack can hook (@afterpack/vite, @afterpack/webpack, @afterpack/esbuild).";
169
116
  const DIRECTIVES_ALWAYS_DISABLED = false;
170
- const ESBUILD_HASHED_NAME_RE = /-([A-Z0-9]{8})\.(?:js|mjs|css)$/;
171
- const angularHashToken = (relativePath) => ESBUILD_HASHED_NAME_RE.exec(basename(relativePath))?.[1] ?? null;
172
117
  function applicationOutputRoot(browserDir) {
173
118
  return basename(browserDir) === "browser" ? dirname(browserDir) : browserDir;
174
119
  }
@@ -191,6 +136,7 @@ async function afterpackAngular(options = {}) {
191
136
  const files = collectJsFiles(browserDir, { include: settings.paths?.include });
192
137
  if (files.length === 0) throw new Error(`@afterpack/angular: no .js/.mjs/.cjs files found under ${browserDir}`);
193
138
  const outputRoot = applicationOutputRoot(browserDir);
139
+ const kinds = classifyAngularScripts(browserDir, files);
194
140
  await runObfuscationPass({
195
141
  files,
196
142
  engine: {
@@ -209,7 +155,8 @@ async function afterpackAngular(options = {}) {
209
155
  tokenOf: angularHashToken
210
156
  },
211
157
  ...passSettings(resolved),
212
- sourceType: angularSourceType(classifyAngularScripts(browserDir, files)),
158
+ sourceType: angularSourceType(kinds),
159
+ fileSourceTypes: angularFileSourceTypes(kinds, files),
213
160
  directivesEnabled: DIRECTIVES_ALWAYS_DISABLED
214
161
  });
215
162
  recomputeAngularIntegrity(outputRoot, browserDir);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@afterpack/angular",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "description": "Angular 17+ integration for AfterPack: obfuscate the browser bundle after ng build",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -25,8 +25,8 @@
25
25
  "dist"
26
26
  ],
27
27
  "dependencies": {
28
- "@afterpack/core": "~0.2.5",
29
- "@afterpack/integration-utils": "0.2.4"
28
+ "@afterpack/core": "~0.2.7",
29
+ "@afterpack/integration-utils": "0.2.6"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@biomejs/biome": "2.4.4",