@sysce/estrogen 0.0.0-stage → 0.5.4

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 +207 -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 +148 -0
  57. package/dist/cjs/options.d.ts +217 -0
  58. package/dist/cjs/options.js +89 -0
  59. package/dist/cjs/package.json +3 -0
  60. package/dist/cjs/report.d.ts +64 -0
  61. package/dist/cjs/report.js +55 -0
  62. package/dist/cjs/resolve-binary.d.ts +44 -0
  63. package/dist/cjs/resolve-binary.js +109 -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 +145 -0
  125. package/dist/esm/options.d.ts +217 -0
  126. package/dist/esm/options.js +85 -0
  127. package/dist/esm/package.json +3 -0
  128. package/dist/esm/report.d.ts +64 -0
  129. package/dist/esm/report.js +49 -0
  130. package/dist/esm/resolve-binary.d.ts +44 -0
  131. package/dist/esm/resolve-binary.js +101 -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 +282 -5
@@ -0,0 +1,85 @@
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.names !== undefined)
31
+ args.push('--names', options.names);
32
+ if (options.sourcemap !== undefined)
33
+ args.push('--sourcemap', options.sourcemap);
34
+ if (options.seed !== undefined)
35
+ args.push('--seed', options.seed);
36
+ if (options.jobs !== undefined)
37
+ args.push('--jobs', String(options.jobs));
38
+ if (options.check)
39
+ args.push('--check');
40
+ if (options.optimizeSvg === false)
41
+ args.push('--no-optimize-svg');
42
+ if (options.minifyHtml === false)
43
+ args.push('--no-minify-html');
44
+ if (options.minifyCss === false)
45
+ args.push('--no-minify-css');
46
+ if (options.moduleGlobs)
47
+ for (const pattern of options.moduleGlobs)
48
+ args.push('--module-glob', pattern);
49
+ if (options.noVmGlobs)
50
+ for (const pattern of options.noVmGlobs)
51
+ args.push('--no-vm-glob', pattern);
52
+ if (options.manifest === false)
53
+ args.push('--no-manifest');
54
+ else if (options.manifest !== undefined)
55
+ args.push('--manifest', options.manifest);
56
+ if (options.sharedRuntime)
57
+ args.push('--shared-runtime');
58
+ if (options.functionSource === false)
59
+ args.push('--no-function-source');
60
+ if (options.functionSourceWarning === false)
61
+ args.push('--no-function-source-warning');
62
+ if (options.injector) {
63
+ args.push('--cloak-target', options.injector.target ?? 'index.html');
64
+ const inputs = Array.isArray(options.injector.cloaks)
65
+ ? options.injector.cloaks
66
+ : [options.injector.cloaks];
67
+ if (inputs.length === 0)
68
+ throw new Error('estrogen: injector.cloaks must contain a frontend');
69
+ for (const input of inputs) {
70
+ args.push('--cloaks', input instanceof URL ? fileURLToPath(input) : input);
71
+ }
72
+ if (options.injector.shortcut)
73
+ args.push('--cloak-shortcut', options.injector.shortcut);
74
+ if (options.injector.mount)
75
+ args.push('--cloak-mount', options.injector.mount);
76
+ }
77
+ if (options.quiet)
78
+ args.push('--quiet');
79
+ if (options.verbose)
80
+ args.push('--verbose');
81
+ if (options.extraArgs)
82
+ args.push(...options.extraArgs);
83
+ args.push(...normalizeDirs(options.dirs));
84
+ return args;
85
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,64 @@
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
+ /** The process exit code. 0 on success. */
10
+ exitCode: number;
11
+ /** The directories estrogen was asked to rewrite. */
12
+ dirs: string[];
13
+ /** The binary that ran. */
14
+ binaryPath: string;
15
+ /** The argument vector passed to the binary. */
16
+ args: string[];
17
+ /** Captured standard output. */
18
+ stdout: string;
19
+ /** Captured standard error. */
20
+ stderr: string;
21
+ /** Wall-clock run time in milliseconds. */
22
+ durationMs: number;
23
+ /**
24
+ * The function source warning from stderr, when the build turns a VM
25
+ * function into a string. {@link obfuscate} also writes it to
26
+ * `process.stderr`. See {@link Options.functionSourceWarning}.
27
+ */
28
+ warning?: string;
29
+ /**
30
+ * With {@link Options.vendor} set, each dir's vendor manifest, read from
31
+ * {@link Options.manifest} (default `estrogen-vendor-manifest.json`), in
32
+ * `dirs` order: the build stamp and the map from each vendored file's public
33
+ * path to its served path. An entry is undefined when that dir has no
34
+ * manifest there. Absent when `manifest` is `false`.
35
+ */
36
+ manifests?: unknown[];
37
+ /**
38
+ * Structured detail, when the binary prints a JSON object on stdout. The
39
+ * shape is the binary's own and may grow; read it defensively.
40
+ */
41
+ details?: unknown;
42
+ }
43
+ /**
44
+ * Thrown when estrogen exits nonzero or fails to launch. Carries the partial
45
+ * {@link Report} so a caller can read the exit code, stderr, and the command
46
+ * that ran.
47
+ */
48
+ export declare class EstrogenError extends Error {
49
+ /** The report captured up to the failure. */
50
+ readonly report: Report;
51
+ constructor(message: string, report: Report);
52
+ }
53
+ /**
54
+ * Parse a structured detail object out of estrogen's stdout. estrogen may print a
55
+ * JSON object as its last non-empty line. Return it when present, otherwise
56
+ * undefined. A line that is not JSON is ignored, not an error.
57
+ */
58
+ export declare function parseDetails(stdout: string): unknown;
59
+ /**
60
+ * Pull the function source warning out of estrogen's stderr: the block from
61
+ * its `⚠ estrogen:` headline through the line that says how to hide it.
62
+ * Returns undefined when stderr has no such block.
63
+ */
64
+ 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,44 @@
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 and the
34
+ * bin launcher's own override use this).
35
+ * 2. Otherwise the matching `@sysce/estrogen-<os>-<cpu>` package is resolved
36
+ * through `req.resolve` and the binary path inside it is returned.
37
+ *
38
+ * `req` is a `require` function anchored in the launcher package, so resolution
39
+ * finds the optional dependency whether npm installed it beside the launcher or
40
+ * hoisted it to a parent `node_modules`. Throws {@link MissingBinaryError} when
41
+ * the platform is unsupported, the package is not installed, or the package
42
+ * has no binary in it.
43
+ */
44
+ export declare function resolveBinaryPath(req: NodeRequire, override?: string): string;
@@ -0,0 +1,101 @@
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 for the estrogen ' +
56
+ 'command, or pass the 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 and the
62
+ * bin launcher's own override use this).
63
+ * 2. Otherwise the matching `@sysce/estrogen-<os>-<cpu>` package is resolved
64
+ * through `req.resolve` and the binary path inside it is returned.
65
+ *
66
+ * `req` is a `require` function anchored in the launcher package, so resolution
67
+ * finds the optional dependency whether npm installed it beside the launcher or
68
+ * hoisted it to a parent `node_modules`. Throws {@link MissingBinaryError} when
69
+ * the platform is unsupported, the package is not installed, or the package
70
+ * has no binary in it.
71
+ */
72
+ export function resolveBinaryPath(req, override) {
73
+ if (override !== undefined && override !== '')
74
+ return override;
75
+ const key = targetKey();
76
+ const pkg = platformPackageName();
77
+ if (key === null || pkg === null) {
78
+ const supported = Object.keys(PLATFORM_PACKAGES).join(', ');
79
+ throw new MissingBinaryError(`estrogen has no prebuilt binary for ${processPlatform}-${processArch}. ` +
80
+ `Supported targets: ${supported}. ${OVERRIDE_HINT}`);
81
+ }
82
+ let pkgDir;
83
+ try {
84
+ pkgDir = dirname(req.resolve(`${pkg}/package.json`));
85
+ }
86
+ catch {
87
+ throw new MissingBinaryError(`estrogen could not find its binary package ${pkg} for ${key}. ` +
88
+ `npm installs it as an optional dependency of @sysce/estrogen, so ` +
89
+ `it is missing after an install with --omit=optional or ` +
90
+ `--no-optional, or when the lockfile was written on another ` +
91
+ `platform. Run "npm install ${pkg}" with the same version as ` +
92
+ `@sysce/estrogen, or reinstall without --omit=optional. ` +
93
+ OVERRIDE_HINT);
94
+ }
95
+ const binary = join(pkgDir, binSubpath());
96
+ if (!existsSync(binary)) {
97
+ throw new MissingBinaryError(`estrogen found ${pkg} at ${pkgDir}, but it has no binary at ` +
98
+ `${binary}. Reinstall ${pkg}. ${OVERRIDE_HINT}`);
99
+ }
100
+ return binary;
101
+ }
@@ -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;
@@ -0,0 +1,54 @@
1
+ import { dirname, resolve as resolvePath } from 'node:path';
2
+ import { obfuscate } from './obfuscate.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 function pluginEstrogen(options = {}) {
30
+ return {
31
+ name: '@sysce/estrogen',
32
+ async writeBundle(output) {
33
+ const outDir = resolveOutputDir(output);
34
+ if (outDir === null) {
35
+ throw new Error('estrogen could not find the Rollup output directory. ' +
36
+ 'Set output.dir or output.file so estrogen knows what to rewrite.');
37
+ }
38
+ await obfuscate({ ...options, dirs: outDir });
39
+ },
40
+ };
41
+ }
42
+ /**
43
+ * Resolve the absolute output directory from a Rollup write. Prefers `dir`,
44
+ * falls back to the directory of `file`, and resolves relative paths against the
45
+ * working directory. Returns null when neither is set.
46
+ */
47
+ function resolveOutputDir(output) {
48
+ if (output.dir !== undefined)
49
+ return resolvePath(process.cwd(), output.dir);
50
+ if (output.file !== undefined)
51
+ return resolvePath(process.cwd(), dirname(output.file));
52
+ return null;
53
+ }
54
+ export default pluginEstrogen;
@@ -0,0 +1,29 @@
1
+ import type { RsbuildPlugin } from '@rsbuild/core';
2
+ import type { PluginOptions } from './options.js';
3
+ /**
4
+ * Rsbuild plugin that runs estrogen over the resolved dist directory after the
5
+ * build finishes.
6
+ *
7
+ * It reads the dist path from the Rsbuild context (`api.context.distPath`),
8
+ * which is the absolute output directory Rsbuild resolved for the build, and
9
+ * runs estrogen in `onAfterBuild`, once the files are written. An estrogen failure is
10
+ * rethrown so the build fails.
11
+ *
12
+ * estrogen rewrites that directory in place and raises the cost of reading it. It
13
+ * is not secrecy: a decoder key ships in each file.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { defineConfig } from '@rsbuild/core';
18
+ * import { pluginEstrogen } from '@sysce/estrogen/rsbuild';
19
+ *
20
+ * export default defineConfig({
21
+ * plugins: [pluginEstrogen({ canary: 'canary-list.json' })],
22
+ * });
23
+ * ```
24
+ *
25
+ * @param options estrogen options. `dirs` is ignored; the dist path comes from the
26
+ * Rsbuild context.
27
+ */
28
+ export declare function pluginEstrogen(options?: PluginOptions): RsbuildPlugin;
29
+ export default pluginEstrogen;
@@ -0,0 +1,42 @@
1
+ import { obfuscate } from './obfuscate.js';
2
+ /**
3
+ * Rsbuild plugin that runs estrogen over the resolved dist directory after the
4
+ * build finishes.
5
+ *
6
+ * It reads the dist path from the Rsbuild context (`api.context.distPath`),
7
+ * which is the absolute output directory Rsbuild resolved for the build, and
8
+ * runs estrogen in `onAfterBuild`, once the files are written. An estrogen failure is
9
+ * rethrown so the build fails.
10
+ *
11
+ * estrogen rewrites that directory in place and raises the cost of reading it. It
12
+ * is not secrecy: a decoder key ships in each file.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * import { defineConfig } from '@rsbuild/core';
17
+ * import { pluginEstrogen } from '@sysce/estrogen/rsbuild';
18
+ *
19
+ * export default defineConfig({
20
+ * plugins: [pluginEstrogen({ canary: 'canary-list.json' })],
21
+ * });
22
+ * ```
23
+ *
24
+ * @param options estrogen options. `dirs` is ignored; the dist path comes from the
25
+ * Rsbuild context.
26
+ */
27
+ export function pluginEstrogen(options = {}) {
28
+ return {
29
+ name: '@sysce/estrogen',
30
+ setup(api) {
31
+ api.onAfterBuild(async () => {
32
+ const distPath = api.context.distPath;
33
+ if (!distPath) {
34
+ throw new Error('estrogen could not find the Rsbuild dist path. ' +
35
+ 'Set output.distPath.root so estrogen knows what to rewrite.');
36
+ }
37
+ await obfuscate({ ...options, dirs: distPath });
38
+ });
39
+ },
40
+ };
41
+ }
42
+ export default pluginEstrogen;
@@ -0,0 +1,16 @@
1
+ import type { VendorInput } from './options.js';
2
+ /** The vendor inputs written for `--vendor-file`, and how to remove them. */
3
+ export interface StagedVendor {
4
+ /** The JSON file to pass as `--vendor-file`. */
5
+ vendorFile: string;
6
+ /** Remove the staging directory and everything in it. */
7
+ cleanup: () => Promise<void>;
8
+ }
9
+ /**
10
+ * Write the vendor inputs to a JSON file for `--vendor-file`. An input with a
11
+ * `rename` or `transform` hook is staged first: each kept file is written,
12
+ * renamed and transformed, into a temporary directory, and the input points
13
+ * there instead. An input without hooks passes through with `from` resolved
14
+ * against `cwd`.
15
+ */
16
+ export declare function stageVendor(inputs: VendorInput[], cwd: string): Promise<StagedVendor>;
@@ -0,0 +1,92 @@
1
+ import { mkdir, mkdtemp, readdir, readFile, rm, stat, writeFile, } from 'node:fs/promises';
2
+ import { tmpdir } from 'node:os';
3
+ import { dirname, join, relative, resolve, sep } from 'node:path';
4
+ /** Characters that make a `from` a glob, as the binary reads it. */
5
+ const GLOB = /[*?[\]{}]/;
6
+ /** Every file under `dir`, as forward-slash paths relative to it, sorted. */
7
+ async function walk(dir, base = dir) {
8
+ const out = [];
9
+ const entries = await readdir(dir, { withFileTypes: true });
10
+ entries.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
11
+ for (const entry of entries) {
12
+ const full = join(dir, entry.name);
13
+ if (entry.isDirectory())
14
+ out.push(...(await walk(full, base)));
15
+ else if (entry.isFile())
16
+ out.push(relative(base, full).split(sep).join('/'));
17
+ }
18
+ return out;
19
+ }
20
+ /** A destination from `rename` must stay inside `to`. */
21
+ function checkDestination(input, path, dest) {
22
+ const parts = dest.split('/');
23
+ if (dest === '' || dest.startsWith('/') || parts.includes('..'))
24
+ throw new Error(`estrogen: vendor rename for ${input.from}: ${path} -> ${JSON.stringify(dest)} leaves \`to\``);
25
+ }
26
+ /**
27
+ * Write the vendor inputs to a JSON file for `--vendor-file`. An input with a
28
+ * `rename` or `transform` hook is staged first: each kept file is written,
29
+ * renamed and transformed, into a temporary directory, and the input points
30
+ * there instead. An input without hooks passes through with `from` resolved
31
+ * against `cwd`.
32
+ */
33
+ export async function stageVendor(inputs, cwd) {
34
+ const root = await mkdtemp(join(tmpdir(), 'estrogen-vendor-'));
35
+ const cleanup = () => rm(root, { recursive: true, force: true });
36
+ try {
37
+ const tables = [];
38
+ for (const [index, input] of inputs.entries()) {
39
+ const from = resolve(cwd, input.from);
40
+ const table = {
41
+ from,
42
+ to: input.to,
43
+ mode: input.mode ?? 'verbatim',
44
+ stamp: input.stamp ?? false,
45
+ module: input.module ?? false,
46
+ rewrite: input.rewrite ?? [],
47
+ };
48
+ if (input.rename || input.transform) {
49
+ if (GLOB.test(input.from))
50
+ throw new Error(`estrogen: vendor ${input.from}: rename and transform need a file or a directory, not a glob`);
51
+ const staged = join(root, String(index));
52
+ const isDir = (await stat(from)).isDirectory();
53
+ const files = isDir ? await walk(from) : [from.split(sep).pop()];
54
+ let kept = 0;
55
+ let last = staged;
56
+ for (const path of files) {
57
+ const info = {
58
+ path,
59
+ file: isDir ? join(from, ...path.split('/')) : from,
60
+ };
61
+ const renamed = input.rename?.(path, info);
62
+ if (renamed === null)
63
+ continue;
64
+ const dest = renamed ?? path;
65
+ checkDestination(input, path, dest);
66
+ const contents = await readFile(info.file);
67
+ const changed = await input.transform?.(contents, info);
68
+ const target = join(staged, ...dest.split('/'));
69
+ await mkdir(dirname(target), { recursive: true });
70
+ await writeFile(target, changed ?? contents);
71
+ last = target;
72
+ kept += 1;
73
+ }
74
+ // A hook that drops every file would hand the binary an empty
75
+ // input; say so here, where the cause is.
76
+ if (kept === 0)
77
+ throw new Error(`estrogen: vendor ${input.from}: rename left no files`);
78
+ // A single file stays a single file, so `to` means what it does
79
+ // without hooks: the file's name, or a directory that exists.
80
+ table.from = isDir ? staged : last;
81
+ }
82
+ tables.push(table);
83
+ }
84
+ const vendorFile = join(root, 'vendor.json');
85
+ await writeFile(vendorFile, JSON.stringify(tables));
86
+ return { vendorFile, cleanup };
87
+ }
88
+ catch (error) {
89
+ await cleanup();
90
+ throw error;
91
+ }
92
+ }
@@ -0,0 +1,31 @@
1
+ import type { Plugin } from 'vite';
2
+ import type { PluginOptions } from './options.js';
3
+ /**
4
+ * Vite plugin that runs estrogen over the build output directory once Vite has
5
+ * written it.
6
+ *
7
+ * It reads the output directory from the resolved config (`build.outDir`,
8
+ * resolved against `root`) in `configResolved`, and runs estrogen in
9
+ * `closeBundle`, after the bundle is on disk. `apply: 'build'` with
10
+ * `enforce: 'post'` keeps it out of `vite serve` and after every other plugin,
11
+ * so it rewrites the finished build. An estrogen failure is thrown so the build
12
+ * fails.
13
+ *
14
+ * estrogen rewrites that directory in place and raises the cost of reading it.
15
+ * It is not secrecy: a decoder key ships in each file.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { defineConfig } from 'vite';
20
+ * import { pluginEstrogen } from '@sysce/estrogen/vite';
21
+ *
22
+ * export default defineConfig({
23
+ * plugins: [pluginEstrogen({ canary: 'canary-list.json' })],
24
+ * });
25
+ * ```
26
+ *
27
+ * @param options estrogen options. `dirs` is ignored; the output directory comes
28
+ * from the Vite config.
29
+ */
30
+ export declare function pluginEstrogen(options?: PluginOptions): Plugin;
31
+ export default pluginEstrogen;