@sysce/estrogen 0.0.0-stage → 0.5.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.
Files changed (140) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +235 -2
  3. package/bin/estrogen.js +38 -0
  4. package/dist/cjs/astro.d.ts +31 -0
  5. package/dist/cjs/astro.js +45 -0
  6. package/dist/cjs/cloak/astro/HolyLink.astro +36 -0
  7. package/dist/cjs/cloak/astro/HolyLink.astro.d.ts +14 -0
  8. package/dist/cjs/cloak/astro/HolyText.astro +31 -0
  9. package/dist/cjs/cloak/astro/HolyText.astro.d.ts +12 -0
  10. package/dist/cjs/cloak/astro.d.ts +2 -0
  11. package/dist/cjs/cloak/astro.js +10 -0
  12. package/dist/cjs/cloak/base64.d.ts +2 -0
  13. package/dist/cjs/cloak/base64.js +20 -0
  14. package/dist/cjs/cloak/crypto.d.ts +16 -0
  15. package/dist/cjs/cloak/crypto.js +40 -0
  16. package/dist/cjs/cloak/decrypt.d.ts +23 -0
  17. package/dist/cjs/cloak/decrypt.js +144 -0
  18. package/dist/cjs/cloak/elements.d.ts +45 -0
  19. package/dist/cjs/cloak/elements.js +195 -0
  20. package/dist/cjs/cloak/index.d.ts +10 -0
  21. package/dist/cjs/cloak/index.js +41 -0
  22. package/dist/cjs/cloak/preact.d.ts +10 -0
  23. package/dist/cjs/cloak/preact.js +19 -0
  24. package/dist/cjs/cloak/props.d.ts +18 -0
  25. package/dist/cjs/cloak/props.js +27 -0
  26. package/dist/cjs/cloak/react-extra.d.ts +327 -0
  27. package/dist/cjs/cloak/react-extra.js +169 -0
  28. package/dist/cjs/cloak/react.d.ts +28 -0
  29. package/dist/cjs/cloak/react.js +159 -0
  30. package/dist/cjs/cloak/render.d.ts +37 -0
  31. package/dist/cjs/cloak/render.js +54 -0
  32. package/dist/cjs/cloak/server.d.ts +35 -0
  33. package/dist/cjs/cloak/server.js +87 -0
  34. package/dist/cjs/cloak/server.node.d.ts +15 -0
  35. package/dist/cjs/cloak/server.node.js +36 -0
  36. package/dist/cjs/cloak/solid.d.ts +13 -0
  37. package/dist/cjs/cloak/solid.js +55 -0
  38. package/dist/cjs/cloak/span.d.ts +8 -0
  39. package/dist/cjs/cloak/span.js +123 -0
  40. package/dist/cjs/cloak/strings.d.ts +18 -0
  41. package/dist/cjs/cloak/strings.js +53 -0
  42. package/dist/cjs/cloak/svelte/HolyLink.svelte +12 -0
  43. package/dist/cjs/cloak/svelte/HolyLink.svelte.d.ts +5 -0
  44. package/dist/cjs/cloak/svelte/HolyText.svelte +13 -0
  45. package/dist/cjs/cloak/svelte/HolyText.svelte.d.ts +5 -0
  46. package/dist/cjs/cloak/svelte.d.ts +2 -0
  47. package/dist/cjs/cloak/svelte.js +10 -0
  48. package/dist/cjs/cloak/vue.d.ts +36 -0
  49. package/dist/cjs/cloak/vue.js +34 -0
  50. package/dist/cjs/esbuild.d.ts +33 -0
  51. package/dist/cjs/esbuild.js +79 -0
  52. package/dist/cjs/index.d.ts +24 -0
  53. package/dist/cjs/index.js +35 -0
  54. package/dist/cjs/node-require.js +7 -0
  55. package/dist/cjs/obfuscate.d.ts +23 -0
  56. package/dist/cjs/obfuscate.js +154 -0
  57. package/dist/cjs/options.d.ts +264 -0
  58. package/dist/cjs/options.js +105 -0
  59. package/dist/cjs/package.json +3 -0
  60. package/dist/cjs/report.d.ts +74 -0
  61. package/dist/cjs/report.js +55 -0
  62. package/dist/cjs/resolve-binary.d.ts +46 -0
  63. package/dist/cjs/resolve-binary.js +114 -0
  64. package/dist/cjs/rollup.d.ts +30 -0
  65. package/dist/cjs/rollup.js +57 -0
  66. package/dist/cjs/rsbuild.d.ts +29 -0
  67. package/dist/cjs/rsbuild.js +45 -0
  68. package/dist/cjs/vendor.d.ts +16 -0
  69. package/dist/cjs/vendor.js +95 -0
  70. package/dist/cjs/vite.d.ts +31 -0
  71. package/dist/cjs/vite.js +53 -0
  72. package/dist/esm/astro.d.ts +31 -0
  73. package/dist/esm/astro.js +42 -0
  74. package/dist/esm/cloak/astro/HolyLink.astro +36 -0
  75. package/dist/esm/cloak/astro/HolyLink.astro.d.ts +14 -0
  76. package/dist/esm/cloak/astro/HolyText.astro +31 -0
  77. package/dist/esm/cloak/astro/HolyText.astro.d.ts +12 -0
  78. package/dist/esm/cloak/astro.d.ts +2 -0
  79. package/dist/esm/cloak/astro.js +2 -0
  80. package/dist/esm/cloak/base64.d.ts +2 -0
  81. package/dist/esm/cloak/base64.js +16 -0
  82. package/dist/esm/cloak/crypto.d.ts +16 -0
  83. package/dist/esm/cloak/crypto.js +36 -0
  84. package/dist/esm/cloak/decrypt.d.ts +23 -0
  85. package/dist/esm/cloak/decrypt.js +136 -0
  86. package/dist/esm/cloak/elements.d.ts +45 -0
  87. package/dist/esm/cloak/elements.js +189 -0
  88. package/dist/esm/cloak/index.d.ts +10 -0
  89. package/dist/esm/cloak/index.js +13 -0
  90. package/dist/esm/cloak/preact.d.ts +10 -0
  91. package/dist/esm/cloak/preact.js +15 -0
  92. package/dist/esm/cloak/props.d.ts +18 -0
  93. package/dist/esm/cloak/props.js +23 -0
  94. package/dist/esm/cloak/react-extra.d.ts +327 -0
  95. package/dist/esm/cloak/react-extra.js +161 -0
  96. package/dist/esm/cloak/react.d.ts +28 -0
  97. package/dist/esm/cloak/react.js +147 -0
  98. package/dist/esm/cloak/render.d.ts +37 -0
  99. package/dist/esm/cloak/render.js +48 -0
  100. package/dist/esm/cloak/server.d.ts +35 -0
  101. package/dist/esm/cloak/server.js +75 -0
  102. package/dist/esm/cloak/server.node.d.ts +15 -0
  103. package/dist/esm/cloak/server.node.js +22 -0
  104. package/dist/esm/cloak/solid.d.ts +13 -0
  105. package/dist/esm/cloak/solid.js +51 -0
  106. package/dist/esm/cloak/span.d.ts +8 -0
  107. package/dist/esm/cloak/span.js +117 -0
  108. package/dist/esm/cloak/strings.d.ts +18 -0
  109. package/dist/esm/cloak/strings.js +47 -0
  110. package/dist/esm/cloak/svelte/HolyLink.svelte +12 -0
  111. package/dist/esm/cloak/svelte/HolyLink.svelte.d.ts +5 -0
  112. package/dist/esm/cloak/svelte/HolyText.svelte +13 -0
  113. package/dist/esm/cloak/svelte/HolyText.svelte.d.ts +5 -0
  114. package/dist/esm/cloak/svelte.d.ts +2 -0
  115. package/dist/esm/cloak/svelte.js +2 -0
  116. package/dist/esm/cloak/vue.d.ts +36 -0
  117. package/dist/esm/cloak/vue.js +31 -0
  118. package/dist/esm/esbuild.d.ts +33 -0
  119. package/dist/esm/esbuild.js +76 -0
  120. package/dist/esm/index.d.ts +24 -0
  121. package/dist/esm/index.js +22 -0
  122. package/dist/esm/node-require.js +8 -0
  123. package/dist/esm/obfuscate.d.ts +23 -0
  124. package/dist/esm/obfuscate.js +151 -0
  125. package/dist/esm/options.d.ts +264 -0
  126. package/dist/esm/options.js +101 -0
  127. package/dist/esm/package.json +3 -0
  128. package/dist/esm/report.d.ts +74 -0
  129. package/dist/esm/report.js +49 -0
  130. package/dist/esm/resolve-binary.d.ts +46 -0
  131. package/dist/esm/resolve-binary.js +106 -0
  132. package/dist/esm/rollup.d.ts +30 -0
  133. package/dist/esm/rollup.js +54 -0
  134. package/dist/esm/rsbuild.d.ts +29 -0
  135. package/dist/esm/rsbuild.js +42 -0
  136. package/dist/esm/vendor.d.ts +16 -0
  137. package/dist/esm/vendor.js +92 -0
  138. package/dist/esm/vite.d.ts +31 -0
  139. package/dist/esm/vite.js +50 -0
  140. package/package.json +283 -5
@@ -0,0 +1,264 @@
1
+ /** A frontend input may be a file path or a file URL from import.meta.url. */
2
+ export type CloakInput = string | URL;
3
+ /** Optional live injector. Passing this object enables it for the build. */
4
+ export interface LiveInjectorOptions {
5
+ /** Output HTML page relative to each dist root. Defaults to index.html. */
6
+ target?: string;
7
+ /** One or more frontend build directories or HTML pages. */
8
+ cloaks: CloakInput | CloakInput[];
9
+ /** Shortcut to show or hide the frontend. Defaults to Ctrl+E. */
10
+ shortcut?: string;
11
+ /** CSS selector for the decoy region to cover; leave unset to cover the viewport. */
12
+ mount?: string;
13
+ /**
14
+ * Served directory for the runtime and frontends, relative to each dist
15
+ * root. Defaults to a name derived from the injector config.
16
+ */
17
+ path?: string;
18
+ /**
19
+ * Global name for the `show`, `hide`, `toggle` and `select` API. Unset, the
20
+ * runtime exposes no global.
21
+ */
22
+ api?: string;
23
+ }
24
+ /** One reference rewrite on a vendored file (`[[vendor.rewrite]]`). */
25
+ export interface VendorRewrite {
26
+ /** The text to find. */
27
+ find: string;
28
+ /** Its replacement. `{stamp}` expands to the per-build stamp. */
29
+ to: string;
30
+ /** Fail the build when `find` matches nothing across the input. Default `true`. */
31
+ required?: boolean;
32
+ /** Fail the build unless `find` matches exactly this many times. */
33
+ count?: number;
34
+ }
35
+ /** What a {@link VendorInput} hook learns about one file. */
36
+ export interface VendorFileInfo {
37
+ /** The file's path relative to `from`, with forward slashes. */
38
+ path: string;
39
+ /** The absolute source file. */
40
+ file: string;
41
+ }
42
+ /**
43
+ * One vendored input, the same as a `[[vendor]]` table in the config, plus two
44
+ * optional hooks that run in Node before estrogen sees the file. With either
45
+ * hook set, `from` must be a file or a directory, not a glob.
46
+ */
47
+ export interface VendorInput {
48
+ /** A file, a directory, or (without hooks) a glob, relative to `cwd`. */
49
+ from: string;
50
+ /** The destination under the dist root. */
51
+ to: string;
52
+ /** `'verbatim'` (the default) copies the file; `'obfuscate'` runs it through estrogen. */
53
+ mode?: 'verbatim' | 'obfuscate';
54
+ /** Insert the per-build stamp before each file's extension. Default `false`. */
55
+ stamp?: boolean;
56
+ /**
57
+ * Compile each obfuscated JS file as an ES module. Default `false`: a
58
+ * vendored file's kind comes from its own syntax, and `moduleGlobs` never
59
+ * applies to it.
60
+ */
61
+ module?: boolean;
62
+ /** Reference rewrites with required and count assertions. */
63
+ rewrite?: VendorRewrite[];
64
+ /**
65
+ * Choose each file's destination, relative to `to`. Return a path to place
66
+ * it there, `undefined` to keep its own path, or `null` to leave it out.
67
+ */
68
+ rename?: (path: string, info: VendorFileInfo) => string | null | undefined;
69
+ /**
70
+ * Change a file's contents before estrogen copies it. Return the new
71
+ * contents, or `undefined` to keep them. Runs after `rename`, only for files
72
+ * that are kept.
73
+ */
74
+ transform?: (contents: Buffer, info: VendorFileInfo) => string | Uint8Array | undefined | Promise<string | Uint8Array | undefined>;
75
+ }
76
+ export interface Options {
77
+ /**
78
+ * Directories to rewrite in place. Pass one string or a list. The CLI accepts
79
+ * `estrogen <dir>...`, so a list runs one process over every directory.
80
+ *
81
+ * The plugins fill this in from the build's output directory, so plugin
82
+ * callers leave it unset. A direct {@link obfuscate} call must set it.
83
+ */
84
+ dirs?: string | string[];
85
+ /**
86
+ * Config file for estrogen (`-c`/`--config <file>`). Per-site settings that do
87
+ * not fit a flag live here.
88
+ */
89
+ config?: string;
90
+ /**
91
+ * Canary list file (`--canary <file>`). The list of literals that must not
92
+ * survive verbatim in the output. estrogen fails nonzero if one does.
93
+ */
94
+ canary?: string;
95
+ /**
96
+ * Match canaries without regard to ASCII case (`--canary-ignore-case`), so
97
+ * `scramjet` also matches `Scramjet`. The default is exact case.
98
+ */
99
+ canaryIgnoreCase?: boolean;
100
+ /**
101
+ * Names file (`--names <file>`). The proxy and store names to rename for this
102
+ * site.
103
+ */
104
+ names?: string;
105
+ /**
106
+ * Source map output directory (`--sourcemap <dir>`). Off by default. When set,
107
+ * estrogen writes maps here, outside the served tree, for private debugging. A
108
+ * map traces the output back to its source, so keep this directory private
109
+ * and never ship it.
110
+ */
111
+ sourcemap?: string;
112
+ /**
113
+ * Seed for the per-file and per-build randomness (`--seed <hex>`). Fixes the
114
+ * output so two runs on one input match. Use it for tests and parity harnesses
115
+ * only. Production builds leave it unset and use real randomness.
116
+ */
117
+ seed?: string;
118
+ /**
119
+ * Worker count (`-j`/`--jobs <n>`). estrogen processes files in parallel; this
120
+ * caps the workers. Unset lets the binary pick.
121
+ */
122
+ jobs?: number;
123
+ /**
124
+ * Check mode (`--check`). Report what estrogen would do and run the canary check
125
+ * without writing changes. Useful in CI to gate a build.
126
+ */
127
+ check?: boolean;
128
+ /**
129
+ * Optimize `.svg` asset files (`--no-optimize-svg` turns it off). On by
130
+ * default: estrogen shrinks each plain image SVG with the SVGO default job set
131
+ * (drops metadata and comments, collapses whitespace, rounds path numbers,
132
+ * removes default attributes) and writes the smaller result. This replaces a
133
+ * site's separate `svgmin` step. A cloak SVG (one with an inline `<script>`)
134
+ * is left for the markup pass. Set this to `false` to keep the SVGs as the
135
+ * bundler wrote them.
136
+ */
137
+ optimizeSvg?: boolean;
138
+ /**
139
+ * Minify `.html` shells (`--no-minify-html` turns it off). On by default:
140
+ * estrogen strips comments and collapses insignificant whitespace before the
141
+ * cloak pass, leaving `<pre>`, `<textarea>`, `<script>` and `<style>` content
142
+ * intact. Set this to `false` to keep the HTML as the bundler wrote it.
143
+ */
144
+ minifyHtml?: boolean;
145
+ /**
146
+ * Minify `.css` stylesheets (`--no-minify-css` turns it off). On by default:
147
+ * estrogen minifies each stylesheet with lightningcss. CSS is not an
148
+ * obfuscation target, so this only shrinks it. Set this to `false` to keep the
149
+ * CSS as the bundler wrote it.
150
+ */
151
+ minifyCss?: boolean;
152
+ /**
153
+ * Compile inline `<script>` bodies to the bytecode VM (`--inline-vm`). Off by
154
+ * default: inline scripts take the literal pool, because each inline VM chunk
155
+ * carries its own runtime.
156
+ */
157
+ inlineVm?: boolean;
158
+ /**
159
+ * The header comment for each protected file (`--marker <text>`), in place of
160
+ * `generated by https://estro.help`. An empty string writes no header, and a
161
+ * second run then cannot recognize the output as protected.
162
+ */
163
+ marker?: string;
164
+ /**
165
+ * Write a JSON report of the run to this file (`--report <file>`), relative
166
+ * to {@link Options.cwd}: each file's status and protection (`vm`,
167
+ * `vm-classic`, `vm-shared`, `literals`, `proxy`, `markup`, `minified`), the
168
+ * reason the VM declined a chunk, every canary leak, and each failed gate.
169
+ * {@link obfuscate} reads it back into {@link Report.run}, on success and on
170
+ * failure.
171
+ */
172
+ report?: string;
173
+ /**
174
+ * Fail the run (exit 4) when any JS chunk is protected by something other
175
+ * than the VM (`--require-vm`).
176
+ */
177
+ requireVm?: boolean;
178
+ /**
179
+ * Fail the run (exit 5) when VM chunks turn more than this many functions
180
+ * into strings (`--max-function-source <n>`).
181
+ */
182
+ maxFunctionSource?: number;
183
+ /** Inject a selected frontend in a closed shadow overlay after obfuscation. Off by default. */
184
+ injector?: LiveInjectorOptions;
185
+ /**
186
+ * Glob patterns relative to the dist root that identify ES module scripts
187
+ * without import/export syntax. Only set these for files actually loaded as
188
+ * modules. Classic scripts expose top-level var/function on globalThis.
189
+ */
190
+ moduleGlobs?: string[];
191
+ /**
192
+ * Glob patterns relative to the dist root for JS files that stay out of the
193
+ * bytecode VM (`--no-vm-glob`). They take the literal-pool and name passes.
194
+ * Use for code that rebuilds functions from `fn.toString()`, since a VM
195
+ * function's source text is the VM call wrapper.
196
+ */
197
+ noVmGlobs?: string[];
198
+ /**
199
+ * Keep the source text of a VM function the code turns into a string, such
200
+ * as `f.toString()`, `String(f)` or `f + ''`, so code that rebuilds it from
201
+ * its text still runs. On by default. `false` passes `--no-function-source`,
202
+ * and such a function reads as `[native code]`.
203
+ */
204
+ functionSource?: boolean;
205
+ /**
206
+ * Print the warning that lists functions the code turns into strings. On by
207
+ * default. `false` passes `--no-function-source-warning`.
208
+ */
209
+ functionSourceWarning?: boolean;
210
+ /**
211
+ * Files to copy into the dist before obfuscating, as `[[vendor]]` tables in
212
+ * the config are. Setting this runs `estrogen build`, which also vendors the
213
+ * config's own `[[vendor]]` inputs, and adds these after them. See
214
+ * {@link VendorInput} for the `rename` and `transform` hooks.
215
+ */
216
+ vendor?: VendorInput[];
217
+ /**
218
+ * Where `estrogen build` writes the vendor manifest (`--manifest`): relative
219
+ * to each dir, or absolute. `false` writes none (`--no-manifest`), for a
220
+ * dist served as is where the manifest would be public. The default is
221
+ * `estrogen-vendor-manifest.json` under each dir.
222
+ */
223
+ manifest?: string | false;
224
+ /**
225
+ * Emit the VM runtime once as a shared module that each ES module VM chunk
226
+ * imports (`--shared-runtime`), instead of inlining a copy in every chunk.
227
+ * A classic script and a vendored file keep the inline copy.
228
+ */
229
+ sharedRuntime?: boolean;
230
+ /** Quiet output (`--quiet`). Suppresses progress logging. */
231
+ quiet?: boolean;
232
+ /** Verbose output (`-v`/`--verbose`). Logs per-file detail. */
233
+ verbose?: boolean;
234
+ /**
235
+ * Path to the estrogen binary. Overrides the per-platform package resolution.
236
+ * Set it to run a locally built binary or a stub in tests. When unset, the
237
+ * `ESTROGEN_BINARY` environment variable is used if set, and otherwise the
238
+ * matching `@sysce/estrogen-<os>-<cpu>` package provides it.
239
+ */
240
+ binaryPath?: string;
241
+ /** Working directory for the spawned process. Defaults to `process.cwd()`. */
242
+ cwd?: string;
243
+ /**
244
+ * Extra arguments appended after the flags and before the directories. An
245
+ * escape hatch for flags this type does not model yet. Prefer a typed field
246
+ * when one exists.
247
+ */
248
+ extraArgs?: string[];
249
+ }
250
+ /**
251
+ * Plugin options: {@link Options} without `dirs`, which a plugin takes from the
252
+ * build's output directory. Everything else carries through to estrogen.
253
+ */
254
+ export type PluginOptions = Omit<Options, 'dirs'>;
255
+ /**
256
+ * Normalize {@link Options.dirs} to a list. Returns an empty list when unset.
257
+ */
258
+ export declare function normalizeDirs(dirs: Options['dirs']): string[];
259
+ /**
260
+ * Turn {@link Options} into the estrogen CLI argument vector. The order is flags
261
+ * first, then `extraArgs`, then the directories, matching `estrogen [options]
262
+ * <dir>...`.
263
+ */
264
+ export declare function buildArgs(options: Options): string[];
@@ -0,0 +1,101 @@
1
+ /**
2
+ * Options shared by the programmatic API ({@link obfuscate}) and the bundler
3
+ * plugins. Each field maps to one estrogen CLI flag, so the same object configures
4
+ * a direct spawn and a plugin run.
5
+ *
6
+ * estrogen rewrites a directory that a bundler has already written. It raises the
7
+ * cost of reading that build. It is not secrecy: a decoder key ships in each
8
+ * file, and anyone with devtools can run the code.
9
+ */
10
+ import { fileURLToPath } from 'node:url';
11
+ /**
12
+ * Normalize {@link Options.dirs} to a list. Returns an empty list when unset.
13
+ */
14
+ export function normalizeDirs(dirs) {
15
+ if (dirs === undefined)
16
+ return [];
17
+ return Array.isArray(dirs) ? dirs.slice() : [dirs];
18
+ }
19
+ /**
20
+ * Turn {@link Options} into the estrogen CLI argument vector. The order is flags
21
+ * first, then `extraArgs`, then the directories, matching `estrogen [options]
22
+ * <dir>...`.
23
+ */
24
+ export function buildArgs(options) {
25
+ const args = [];
26
+ if (options.config !== undefined)
27
+ args.push('--config', options.config);
28
+ if (options.canary !== undefined)
29
+ args.push('--canary', options.canary);
30
+ if (options.canaryIgnoreCase)
31
+ args.push('--canary-ignore-case');
32
+ if (options.names !== undefined)
33
+ args.push('--names', options.names);
34
+ if (options.sourcemap !== undefined)
35
+ args.push('--sourcemap', options.sourcemap);
36
+ if (options.seed !== undefined)
37
+ args.push('--seed', options.seed);
38
+ if (options.jobs !== undefined)
39
+ args.push('--jobs', String(options.jobs));
40
+ if (options.check)
41
+ args.push('--check');
42
+ if (options.optimizeSvg === false)
43
+ args.push('--no-optimize-svg');
44
+ if (options.minifyHtml === false)
45
+ args.push('--no-minify-html');
46
+ if (options.minifyCss === false)
47
+ args.push('--no-minify-css');
48
+ if (options.inlineVm)
49
+ args.push('--inline-vm');
50
+ if (options.marker !== undefined)
51
+ args.push('--marker', options.marker);
52
+ if (options.report !== undefined)
53
+ args.push('--report', options.report);
54
+ if (options.requireVm)
55
+ args.push('--require-vm');
56
+ if (options.maxFunctionSource !== undefined)
57
+ args.push('--max-function-source', String(options.maxFunctionSource));
58
+ if (options.moduleGlobs)
59
+ for (const pattern of options.moduleGlobs)
60
+ args.push('--module-glob', pattern);
61
+ if (options.noVmGlobs)
62
+ for (const pattern of options.noVmGlobs)
63
+ args.push('--no-vm-glob', pattern);
64
+ if (options.manifest === false)
65
+ args.push('--no-manifest');
66
+ else if (options.manifest !== undefined)
67
+ args.push('--manifest', options.manifest);
68
+ if (options.sharedRuntime)
69
+ args.push('--shared-runtime');
70
+ if (options.functionSource === false)
71
+ args.push('--no-function-source');
72
+ if (options.functionSourceWarning === false)
73
+ args.push('--no-function-source-warning');
74
+ if (options.injector) {
75
+ args.push('--cloak-target', options.injector.target ?? 'index.html');
76
+ const inputs = Array.isArray(options.injector.cloaks)
77
+ ? options.injector.cloaks
78
+ : [options.injector.cloaks];
79
+ if (inputs.length === 0)
80
+ throw new Error('estrogen: injector.cloaks must contain a frontend');
81
+ for (const input of inputs) {
82
+ args.push('--cloaks', input instanceof URL ? fileURLToPath(input) : input);
83
+ }
84
+ if (options.injector.shortcut)
85
+ args.push('--cloak-shortcut', options.injector.shortcut);
86
+ if (options.injector.mount)
87
+ args.push('--cloak-mount', options.injector.mount);
88
+ if (options.injector.path)
89
+ args.push('--cloak-path', options.injector.path);
90
+ if (options.injector.api)
91
+ args.push('--cloak-api', options.injector.api);
92
+ }
93
+ if (options.quiet)
94
+ args.push('--quiet');
95
+ if (options.verbose)
96
+ args.push('--verbose');
97
+ if (options.extraArgs)
98
+ args.push(...options.extraArgs);
99
+ args.push(...normalizeDirs(options.dirs));
100
+ return args;
101
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,74 @@
1
+ /**
2
+ * The result of an estrogen run, resolved by {@link obfuscate}. estrogen exits 0 on
3
+ * success and nonzero on failure, so a resolved report always has
4
+ * `exitCode === 0`; a failed run rejects with a {@link EstrogenError} instead.
5
+ */
6
+ export interface Report {
7
+ /** True when estrogen exited 0. */
8
+ ok: boolean;
9
+ /**
10
+ * The process exit code. 0 on success, 1 for an error, 2 for an unprotected
11
+ * file under `check`, 3 for a canary in the output, 4 for a non-VM chunk
12
+ * under `requireVm`, and 5 for `maxFunctionSource` exceeded.
13
+ */
14
+ exitCode: number;
15
+ /** The directories estrogen was asked to rewrite. */
16
+ dirs: string[];
17
+ /** The binary that ran. */
18
+ binaryPath: string;
19
+ /** The argument vector passed to the binary. */
20
+ args: string[];
21
+ /** Captured standard output. */
22
+ stdout: string;
23
+ /** Captured standard error. */
24
+ stderr: string;
25
+ /** Wall-clock run time in milliseconds. */
26
+ durationMs: number;
27
+ /**
28
+ * The function source warning from stderr, when the build turns a VM
29
+ * function into a string. {@link obfuscate} also writes it to
30
+ * `process.stderr`. See {@link Options.functionSourceWarning}.
31
+ */
32
+ warning?: string;
33
+ /**
34
+ * With {@link Options.vendor} set, each dir's vendor manifest, read from
35
+ * {@link Options.manifest} (default `estrogen-vendor-manifest.json`), in
36
+ * `dirs` order: the build stamp and the map from each vendored file's public
37
+ * path to its served path. An entry is undefined when that dir has no
38
+ * manifest there. Absent when `manifest` is `false`.
39
+ */
40
+ manifests?: unknown[];
41
+ /**
42
+ * Structured detail, when the binary prints a JSON object on stdout. The
43
+ * shape is the binary's own and may grow; read it defensively.
44
+ */
45
+ details?: unknown;
46
+ /**
47
+ * The JSON run report, read back from {@link Options.report} when it is set
48
+ * and the binary wrote it. Its `version` field names the shape; read it
49
+ * defensively.
50
+ */
51
+ run?: unknown;
52
+ }
53
+ /**
54
+ * Thrown when estrogen exits nonzero or fails to launch. Carries the partial
55
+ * {@link Report} so a caller can read the exit code, stderr, and the command
56
+ * that ran.
57
+ */
58
+ export declare class EstrogenError extends Error {
59
+ /** The report captured up to the failure. */
60
+ readonly report: Report;
61
+ constructor(message: string, report: Report);
62
+ }
63
+ /**
64
+ * Parse a structured detail object out of estrogen's stdout. estrogen may print a
65
+ * JSON object as its last non-empty line. Return it when present, otherwise
66
+ * undefined. A line that is not JSON is ignored, not an error.
67
+ */
68
+ export declare function parseDetails(stdout: string): unknown;
69
+ /**
70
+ * Pull the function source warning out of estrogen's stderr: the block from
71
+ * its `⚠ estrogen:` headline through the line that says how to hide it.
72
+ * Returns undefined when stderr has no such block.
73
+ */
74
+ export declare function functionSourceWarning(stderr: string): string | undefined;
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Thrown when estrogen exits nonzero or fails to launch. Carries the partial
3
+ * {@link Report} so a caller can read the exit code, stderr, and the command
4
+ * that ran.
5
+ */
6
+ export class EstrogenError extends Error {
7
+ /** The report captured up to the failure. */
8
+ report;
9
+ constructor(message, report) {
10
+ super(message);
11
+ this.name = 'EstrogenError';
12
+ this.report = report;
13
+ }
14
+ }
15
+ /**
16
+ * Parse a structured detail object out of estrogen's stdout. estrogen may print a
17
+ * JSON object as its last non-empty line. Return it when present, otherwise
18
+ * undefined. A line that is not JSON is ignored, not an error.
19
+ */
20
+ export function parseDetails(stdout) {
21
+ const lines = stdout.split('\n');
22
+ for (let i = lines.length - 1; i >= 0; i -= 1) {
23
+ const line = lines[i].trim();
24
+ if (line === '')
25
+ continue;
26
+ if (line[0] !== '{')
27
+ return undefined;
28
+ try {
29
+ return JSON.parse(line);
30
+ }
31
+ catch {
32
+ return undefined;
33
+ }
34
+ }
35
+ return undefined;
36
+ }
37
+ /**
38
+ * Pull the function source warning out of estrogen's stderr: the block from
39
+ * its `⚠ estrogen:` headline through the line that says how to hide it.
40
+ * Returns undefined when stderr has no such block.
41
+ */
42
+ export function functionSourceWarning(stderr) {
43
+ const lines = stderr.split(/(?<=\n)/);
44
+ const start = lines.findIndex((line) => line.includes('⚠ estrogen:'));
45
+ if (start === -1)
46
+ return undefined;
47
+ const end = lines.findIndex((line, index) => index > start && line.includes('--no-function-source-warning'));
48
+ return lines.slice(start, end === -1 ? undefined : end + 1).join('');
49
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * The npm name of the per-platform binary package for each supported target.
3
+ * Keyed by `<os>-<cpu>` where os is `process.platform` and cpu is
4
+ * `process.arch`.
5
+ */
6
+ export declare const PLATFORM_PACKAGES: Record<string, string>;
7
+ /**
8
+ * The `<os>-<cpu>` target key for a platform and arch, or null when the pair is
9
+ * not a supported target. Defaults to the running process.
10
+ */
11
+ export declare function targetKey(platform?: string, arch?: string): string | null;
12
+ /**
13
+ * The npm package name that carries the binary for a platform and arch, or null
14
+ * when unsupported. Defaults to the running process.
15
+ */
16
+ export declare function platformPackageName(platform?: string, arch?: string): string | null;
17
+ /**
18
+ * The binary's path inside its package. Windows carries `bin/estrogen.exe`, every
19
+ * other target `bin/estrogen`.
20
+ */
21
+ export declare function binSubpath(platform?: string): string;
22
+ /**
23
+ * Thrown when no per-platform binary package resolves for the running system.
24
+ * The message names the target and the package npm would have installed.
25
+ */
26
+ export declare class MissingBinaryError extends Error {
27
+ constructor(message: string);
28
+ }
29
+ /**
30
+ * Locate the estrogen binary for the running platform.
31
+ *
32
+ * Resolution order:
33
+ * 1. `override`, when given, is returned as is (the `binaryPath` option uses
34
+ * this).
35
+ * 2. Otherwise the `ESTROGEN_BINARY` environment variable, when set, so the
36
+ * `estrogen` command, {@link obfuscate} and every plugin run the same binary.
37
+ * 3. Otherwise the matching `@sysce/estrogen-<os>-<cpu>` package is resolved
38
+ * through `req.resolve` and the binary path inside it is returned.
39
+ *
40
+ * `req` is a `require` function anchored in the launcher package, so resolution
41
+ * finds the optional dependency whether npm installed it beside the launcher or
42
+ * hoisted it to a parent `node_modules`. Throws {@link MissingBinaryError} when
43
+ * the platform is unsupported, the package is not installed, or the package
44
+ * has no binary in it.
45
+ */
46
+ export declare function resolveBinaryPath(req: NodeRequire, override?: string): string;
@@ -0,0 +1,106 @@
1
+ import { existsSync } from 'node:fs';
2
+ import { dirname, join } from 'node:path';
3
+ import { arch as processArch, platform as processPlatform } from 'node:process';
4
+ /**
5
+ * The npm name of the per-platform binary package for each supported target.
6
+ * Keyed by `<os>-<cpu>` where os is `process.platform` and cpu is
7
+ * `process.arch`.
8
+ */
9
+ export const PLATFORM_PACKAGES = {
10
+ 'linux-x64': '@sysce/estrogen-linux-x64',
11
+ 'linux-arm64': '@sysce/estrogen-linux-arm64',
12
+ 'darwin-x64': '@sysce/estrogen-darwin-x64',
13
+ 'darwin-arm64': '@sysce/estrogen-darwin-arm64',
14
+ 'win32-x64': '@sysce/estrogen-win32-x64',
15
+ };
16
+ /**
17
+ * The `<os>-<cpu>` target key for a platform and arch, or null when the pair is
18
+ * not a supported target. Defaults to the running process.
19
+ */
20
+ export function targetKey(platform = processPlatform, arch = processArch) {
21
+ const key = `${platform}-${arch}`;
22
+ return key in PLATFORM_PACKAGES ? key : null;
23
+ }
24
+ /**
25
+ * The npm package name that carries the binary for a platform and arch, or null
26
+ * when unsupported. Defaults to the running process.
27
+ */
28
+ export function platformPackageName(platform = processPlatform, arch = processArch) {
29
+ const key = targetKey(platform, arch);
30
+ return key === null ? null : PLATFORM_PACKAGES[key];
31
+ }
32
+ /**
33
+ * The binary's path inside its package. Windows carries `bin/estrogen.exe`, every
34
+ * other target `bin/estrogen`.
35
+ */
36
+ export function binSubpath(platform = processPlatform) {
37
+ return platform === 'win32'
38
+ ? join('bin', 'estrogen.exe')
39
+ : join('bin', 'estrogen');
40
+ }
41
+ /**
42
+ * Thrown when no per-platform binary package resolves for the running system.
43
+ * The message names the target and the package npm would have installed.
44
+ */
45
+ export class MissingBinaryError extends Error {
46
+ constructor(message) {
47
+ super(message);
48
+ this.name = 'MissingBinaryError';
49
+ }
50
+ }
51
+ /**
52
+ * The tail of every {@link MissingBinaryError} message: the two ways to run a
53
+ * binary that did not come from a per-platform package.
54
+ */
55
+ const OVERRIDE_HINT = 'To use a binary built elsewhere, set ESTROGEN_BINARY, or pass the ' +
56
+ 'binaryPath option to obfuscate() and the plugins.';
57
+ /**
58
+ * Locate the estrogen binary for the running platform.
59
+ *
60
+ * Resolution order:
61
+ * 1. `override`, when given, is returned as is (the `binaryPath` option uses
62
+ * this).
63
+ * 2. Otherwise the `ESTROGEN_BINARY` environment variable, when set, so the
64
+ * `estrogen` command, {@link obfuscate} and every plugin run the same binary.
65
+ * 3. Otherwise the matching `@sysce/estrogen-<os>-<cpu>` package is resolved
66
+ * through `req.resolve` and the binary path inside it is returned.
67
+ *
68
+ * `req` is a `require` function anchored in the launcher package, so resolution
69
+ * finds the optional dependency whether npm installed it beside the launcher or
70
+ * hoisted it to a parent `node_modules`. Throws {@link MissingBinaryError} when
71
+ * the platform is unsupported, the package is not installed, or the package
72
+ * has no binary in it.
73
+ */
74
+ export function resolveBinaryPath(req, override) {
75
+ if (override !== undefined && override !== '')
76
+ return override;
77
+ const fromEnv = process.env.ESTROGEN_BINARY;
78
+ if (fromEnv !== undefined && fromEnv !== '')
79
+ return fromEnv;
80
+ const key = targetKey();
81
+ const pkg = platformPackageName();
82
+ if (key === null || pkg === null) {
83
+ const supported = Object.keys(PLATFORM_PACKAGES).join(', ');
84
+ throw new MissingBinaryError(`estrogen has no prebuilt binary for ${processPlatform}-${processArch}. ` +
85
+ `Supported targets: ${supported}. ${OVERRIDE_HINT}`);
86
+ }
87
+ let pkgDir;
88
+ try {
89
+ pkgDir = dirname(req.resolve(`${pkg}/package.json`));
90
+ }
91
+ catch {
92
+ throw new MissingBinaryError(`estrogen could not find its binary package ${pkg} for ${key}. ` +
93
+ `npm installs it as an optional dependency of @sysce/estrogen, so ` +
94
+ `it is missing after an install with --omit=optional or ` +
95
+ `--no-optional, or when the lockfile was written on another ` +
96
+ `platform. Run "npm install ${pkg}" with the same version as ` +
97
+ `@sysce/estrogen, or reinstall without --omit=optional. ` +
98
+ OVERRIDE_HINT);
99
+ }
100
+ const binary = join(pkgDir, binSubpath());
101
+ if (!existsSync(binary)) {
102
+ throw new MissingBinaryError(`estrogen found ${pkg} at ${pkgDir}, but it has no binary at ` +
103
+ `${binary}. Reinstall ${pkg}. ${OVERRIDE_HINT}`);
104
+ }
105
+ return binary;
106
+ }
@@ -0,0 +1,30 @@
1
+ import type { Plugin } from 'rollup';
2
+ import type { PluginOptions } from './options.js';
3
+ /**
4
+ * Rollup plugin that runs estrogen over the output directory once Rollup has
5
+ * written it.
6
+ *
7
+ * It finds the directory from the write's `output.dir`, or from the directory of
8
+ * `output.file` for a single-file build, resolved against the current working
9
+ * directory. It runs in `writeBundle`, after the files are on disk. An estrogen
10
+ * failure is thrown so the build fails.
11
+ *
12
+ * estrogen rewrites that directory in place and raises the cost of reading it.
13
+ * It is not secrecy: a decoder key ships in each file.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { pluginEstrogen } from '@sysce/estrogen/rollup';
18
+ *
19
+ * export default {
20
+ * input: 'src/app.js',
21
+ * output: { dir: 'dist', format: 'es' },
22
+ * plugins: [pluginEstrogen({ canary: 'canary-list.json' })],
23
+ * };
24
+ * ```
25
+ *
26
+ * @param options estrogen options. `dirs` is ignored; the output directory comes
27
+ * from the Rollup output config.
28
+ */
29
+ export declare function pluginEstrogen(options?: PluginOptions): Plugin;
30
+ export default pluginEstrogen;