@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.
- package/LICENSE +15 -0
- package/README.md +207 -2
- package/bin/estrogen.js +38 -0
- package/dist/cjs/astro.d.ts +31 -0
- package/dist/cjs/astro.js +45 -0
- package/dist/cjs/cloak/astro/HolyLink.astro +36 -0
- package/dist/cjs/cloak/astro/HolyLink.astro.d.ts +14 -0
- package/dist/cjs/cloak/astro/HolyText.astro +31 -0
- package/dist/cjs/cloak/astro/HolyText.astro.d.ts +12 -0
- package/dist/cjs/cloak/astro.d.ts +2 -0
- package/dist/cjs/cloak/astro.js +10 -0
- package/dist/cjs/cloak/base64.d.ts +2 -0
- package/dist/cjs/cloak/base64.js +20 -0
- package/dist/cjs/cloak/crypto.d.ts +16 -0
- package/dist/cjs/cloak/crypto.js +40 -0
- package/dist/cjs/cloak/decrypt.d.ts +23 -0
- package/dist/cjs/cloak/decrypt.js +144 -0
- package/dist/cjs/cloak/elements.d.ts +45 -0
- package/dist/cjs/cloak/elements.js +195 -0
- package/dist/cjs/cloak/index.d.ts +10 -0
- package/dist/cjs/cloak/index.js +41 -0
- package/dist/cjs/cloak/preact.d.ts +10 -0
- package/dist/cjs/cloak/preact.js +19 -0
- package/dist/cjs/cloak/props.d.ts +18 -0
- package/dist/cjs/cloak/props.js +27 -0
- package/dist/cjs/cloak/react-extra.d.ts +327 -0
- package/dist/cjs/cloak/react-extra.js +169 -0
- package/dist/cjs/cloak/react.d.ts +28 -0
- package/dist/cjs/cloak/react.js +159 -0
- package/dist/cjs/cloak/render.d.ts +37 -0
- package/dist/cjs/cloak/render.js +54 -0
- package/dist/cjs/cloak/server.d.ts +35 -0
- package/dist/cjs/cloak/server.js +87 -0
- package/dist/cjs/cloak/server.node.d.ts +15 -0
- package/dist/cjs/cloak/server.node.js +36 -0
- package/dist/cjs/cloak/solid.d.ts +13 -0
- package/dist/cjs/cloak/solid.js +55 -0
- package/dist/cjs/cloak/span.d.ts +8 -0
- package/dist/cjs/cloak/span.js +123 -0
- package/dist/cjs/cloak/strings.d.ts +18 -0
- package/dist/cjs/cloak/strings.js +53 -0
- package/dist/cjs/cloak/svelte/HolyLink.svelte +12 -0
- package/dist/cjs/cloak/svelte/HolyLink.svelte.d.ts +5 -0
- package/dist/cjs/cloak/svelte/HolyText.svelte +13 -0
- package/dist/cjs/cloak/svelte/HolyText.svelte.d.ts +5 -0
- package/dist/cjs/cloak/svelte.d.ts +2 -0
- package/dist/cjs/cloak/svelte.js +10 -0
- package/dist/cjs/cloak/vue.d.ts +36 -0
- package/dist/cjs/cloak/vue.js +34 -0
- package/dist/cjs/esbuild.d.ts +33 -0
- package/dist/cjs/esbuild.js +79 -0
- package/dist/cjs/index.d.ts +24 -0
- package/dist/cjs/index.js +35 -0
- package/dist/cjs/node-require.js +7 -0
- package/dist/cjs/obfuscate.d.ts +23 -0
- package/dist/cjs/obfuscate.js +148 -0
- package/dist/cjs/options.d.ts +217 -0
- package/dist/cjs/options.js +89 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/report.d.ts +64 -0
- package/dist/cjs/report.js +55 -0
- package/dist/cjs/resolve-binary.d.ts +44 -0
- package/dist/cjs/resolve-binary.js +109 -0
- package/dist/cjs/rollup.d.ts +30 -0
- package/dist/cjs/rollup.js +57 -0
- package/dist/cjs/rsbuild.d.ts +29 -0
- package/dist/cjs/rsbuild.js +45 -0
- package/dist/cjs/vendor.d.ts +16 -0
- package/dist/cjs/vendor.js +95 -0
- package/dist/cjs/vite.d.ts +31 -0
- package/dist/cjs/vite.js +53 -0
- package/dist/esm/astro.d.ts +31 -0
- package/dist/esm/astro.js +42 -0
- package/dist/esm/cloak/astro/HolyLink.astro +36 -0
- package/dist/esm/cloak/astro/HolyLink.astro.d.ts +14 -0
- package/dist/esm/cloak/astro/HolyText.astro +31 -0
- package/dist/esm/cloak/astro/HolyText.astro.d.ts +12 -0
- package/dist/esm/cloak/astro.d.ts +2 -0
- package/dist/esm/cloak/astro.js +2 -0
- package/dist/esm/cloak/base64.d.ts +2 -0
- package/dist/esm/cloak/base64.js +16 -0
- package/dist/esm/cloak/crypto.d.ts +16 -0
- package/dist/esm/cloak/crypto.js +36 -0
- package/dist/esm/cloak/decrypt.d.ts +23 -0
- package/dist/esm/cloak/decrypt.js +136 -0
- package/dist/esm/cloak/elements.d.ts +45 -0
- package/dist/esm/cloak/elements.js +189 -0
- package/dist/esm/cloak/index.d.ts +10 -0
- package/dist/esm/cloak/index.js +13 -0
- package/dist/esm/cloak/preact.d.ts +10 -0
- package/dist/esm/cloak/preact.js +15 -0
- package/dist/esm/cloak/props.d.ts +18 -0
- package/dist/esm/cloak/props.js +23 -0
- package/dist/esm/cloak/react-extra.d.ts +327 -0
- package/dist/esm/cloak/react-extra.js +161 -0
- package/dist/esm/cloak/react.d.ts +28 -0
- package/dist/esm/cloak/react.js +147 -0
- package/dist/esm/cloak/render.d.ts +37 -0
- package/dist/esm/cloak/render.js +48 -0
- package/dist/esm/cloak/server.d.ts +35 -0
- package/dist/esm/cloak/server.js +75 -0
- package/dist/esm/cloak/server.node.d.ts +15 -0
- package/dist/esm/cloak/server.node.js +22 -0
- package/dist/esm/cloak/solid.d.ts +13 -0
- package/dist/esm/cloak/solid.js +51 -0
- package/dist/esm/cloak/span.d.ts +8 -0
- package/dist/esm/cloak/span.js +117 -0
- package/dist/esm/cloak/strings.d.ts +18 -0
- package/dist/esm/cloak/strings.js +47 -0
- package/dist/esm/cloak/svelte/HolyLink.svelte +12 -0
- package/dist/esm/cloak/svelte/HolyLink.svelte.d.ts +5 -0
- package/dist/esm/cloak/svelte/HolyText.svelte +13 -0
- package/dist/esm/cloak/svelte/HolyText.svelte.d.ts +5 -0
- package/dist/esm/cloak/svelte.d.ts +2 -0
- package/dist/esm/cloak/svelte.js +2 -0
- package/dist/esm/cloak/vue.d.ts +36 -0
- package/dist/esm/cloak/vue.js +31 -0
- package/dist/esm/esbuild.d.ts +33 -0
- package/dist/esm/esbuild.js +76 -0
- package/dist/esm/index.d.ts +24 -0
- package/dist/esm/index.js +22 -0
- package/dist/esm/node-require.js +8 -0
- package/dist/esm/obfuscate.d.ts +23 -0
- package/dist/esm/obfuscate.js +145 -0
- package/dist/esm/options.d.ts +217 -0
- package/dist/esm/options.js +85 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/report.d.ts +64 -0
- package/dist/esm/report.js +49 -0
- package/dist/esm/resolve-binary.d.ts +44 -0
- package/dist/esm/resolve-binary.js +101 -0
- package/dist/esm/rollup.d.ts +30 -0
- package/dist/esm/rollup.js +54 -0
- package/dist/esm/rsbuild.d.ts +29 -0
- package/dist/esm/rsbuild.js +42 -0
- package/dist/esm/vendor.d.ts +16 -0
- package/dist/esm/vendor.js +92 -0
- package/dist/esm/vite.d.ts +31 -0
- package/dist/esm/vite.js +50 -0
- package/package.json +282 -5
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import '../elements.js';
|
|
3
|
+
import { textElementProps, type CloakTextProps } from '../props.js';
|
|
4
|
+
|
|
5
|
+
let { text = '', ciphertext, ellipsis = false, revealForBots = false }: CloakTextProps = $props();
|
|
6
|
+
const result = $derived(textElementProps({ text, ciphertext, ellipsis, revealForBots }));
|
|
7
|
+
</script>
|
|
8
|
+
|
|
9
|
+
<holy-text
|
|
10
|
+
ciphertext={result.attributes.ciphertext}
|
|
11
|
+
ellipsis={result.attributes.ellipsis}
|
|
12
|
+
reveal-for-bots={result.attributes['reveal-for-bots']}
|
|
13
|
+
>{result.placeholder}</holy-text>
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type PropType } from 'vue';
|
|
2
|
+
import './elements.js';
|
|
3
|
+
export declare const HolyText: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
|
|
4
|
+
text: {
|
|
5
|
+
type: StringConstructor;
|
|
6
|
+
default: string;
|
|
7
|
+
};
|
|
8
|
+
ciphertext: StringConstructor;
|
|
9
|
+
ellipsis: BooleanConstructor;
|
|
10
|
+
revealForBots: BooleanConstructor;
|
|
11
|
+
}>, () => import("vue").VNode<import("vue").RendererNode, import("vue").RendererElement, {
|
|
12
|
+
[key: string]: any;
|
|
13
|
+
}>, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
|
|
14
|
+
text: {
|
|
15
|
+
type: StringConstructor;
|
|
16
|
+
default: string;
|
|
17
|
+
};
|
|
18
|
+
ciphertext: StringConstructor;
|
|
19
|
+
ellipsis: BooleanConstructor;
|
|
20
|
+
revealForBots: BooleanConstructor;
|
|
21
|
+
}>> & Readonly<{}>, {
|
|
22
|
+
ellipsis: boolean;
|
|
23
|
+
text: string;
|
|
24
|
+
revealForBots: boolean;
|
|
25
|
+
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
26
|
+
export declare const HolyLink: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
|
|
27
|
+
href: StringConstructor;
|
|
28
|
+
hrefCipher: StringConstructor;
|
|
29
|
+
target: PropType<string>;
|
|
30
|
+
}>, () => import("vue").VNode<import("vue").RendererNode, import("vue").RendererElement, {
|
|
31
|
+
[key: string]: any;
|
|
32
|
+
}>, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
|
|
33
|
+
href: StringConstructor;
|
|
34
|
+
hrefCipher: StringConstructor;
|
|
35
|
+
target: PropType<string>;
|
|
36
|
+
}>> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { defineComponent, h } from 'vue';
|
|
2
|
+
import './elements.js';
|
|
3
|
+
import { linkElementProps, textElementProps } from './props.js';
|
|
4
|
+
export const HolyText = defineComponent({
|
|
5
|
+
name: 'HolyText',
|
|
6
|
+
inheritAttrs: false,
|
|
7
|
+
props: {
|
|
8
|
+
text: { type: String, default: '' },
|
|
9
|
+
ciphertext: String,
|
|
10
|
+
ellipsis: Boolean,
|
|
11
|
+
revealForBots: Boolean,
|
|
12
|
+
},
|
|
13
|
+
setup(props, { attrs }) {
|
|
14
|
+
return () => {
|
|
15
|
+
const { attributes, placeholder } = textElementProps(props);
|
|
16
|
+
return h('holy-text', { ...attrs, ...attributes }, placeholder);
|
|
17
|
+
};
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
export const HolyLink = defineComponent({
|
|
21
|
+
name: 'HolyLink',
|
|
22
|
+
inheritAttrs: false,
|
|
23
|
+
props: {
|
|
24
|
+
href: String,
|
|
25
|
+
hrefCipher: String,
|
|
26
|
+
target: String,
|
|
27
|
+
},
|
|
28
|
+
setup(props, { attrs, slots }) {
|
|
29
|
+
return () => h('holy-link', { ...attrs, ...linkElementProps(props) }, slots.default?.());
|
|
30
|
+
},
|
|
31
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Plugin } from 'esbuild';
|
|
2
|
+
import type { PluginOptions } from './options.js';
|
|
3
|
+
/**
|
|
4
|
+
* esbuild plugin that runs estrogen over the build's output directory after esbuild
|
|
5
|
+
* writes it.
|
|
6
|
+
*
|
|
7
|
+
* It finds the directory from `initialOptions.outdir`, or from the directory of
|
|
8
|
+
* `initialOptions.outfile` when a single file is built, resolved against the
|
|
9
|
+
* build's `absWorkingDir` or the current working directory. It runs in `onEnd`,
|
|
10
|
+
* once the bundle is on disk, and skips when the build produced errors. An estrogen
|
|
11
|
+
* failure is turned into an esbuild error so the build fails.
|
|
12
|
+
*
|
|
13
|
+
* estrogen rewrites that directory in place and raises the cost of reading it. It
|
|
14
|
+
* is not secrecy: a decoder key ships in each file.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { build } from 'esbuild';
|
|
19
|
+
* import estrogen from '@sysce/estrogen/esbuild';
|
|
20
|
+
*
|
|
21
|
+
* await build({
|
|
22
|
+
* entryPoints: ['src/app.ts'],
|
|
23
|
+
* bundle: true,
|
|
24
|
+
* outdir: 'dist',
|
|
25
|
+
* plugins: [estrogen({ canary: 'canary-list.json' })],
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @param options estrogen options. `dirs` is ignored; the output directory is
|
|
30
|
+
* taken from the esbuild config.
|
|
31
|
+
*/
|
|
32
|
+
export declare function estrogen(options?: PluginOptions): Plugin;
|
|
33
|
+
export default estrogen;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { dirname, resolve as resolvePath } from 'node:path';
|
|
2
|
+
import { obfuscate } from './obfuscate.js';
|
|
3
|
+
/**
|
|
4
|
+
* esbuild plugin that runs estrogen over the build's output directory after esbuild
|
|
5
|
+
* writes it.
|
|
6
|
+
*
|
|
7
|
+
* It finds the directory from `initialOptions.outdir`, or from the directory of
|
|
8
|
+
* `initialOptions.outfile` when a single file is built, resolved against the
|
|
9
|
+
* build's `absWorkingDir` or the current working directory. It runs in `onEnd`,
|
|
10
|
+
* once the bundle is on disk, and skips when the build produced errors. An estrogen
|
|
11
|
+
* failure is turned into an esbuild error so the build fails.
|
|
12
|
+
*
|
|
13
|
+
* estrogen rewrites that directory in place and raises the cost of reading it. It
|
|
14
|
+
* is not secrecy: a decoder key ships in each file.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { build } from 'esbuild';
|
|
19
|
+
* import estrogen from '@sysce/estrogen/esbuild';
|
|
20
|
+
*
|
|
21
|
+
* await build({
|
|
22
|
+
* entryPoints: ['src/app.ts'],
|
|
23
|
+
* bundle: true,
|
|
24
|
+
* outdir: 'dist',
|
|
25
|
+
* plugins: [estrogen({ canary: 'canary-list.json' })],
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @param options estrogen options. `dirs` is ignored; the output directory is
|
|
30
|
+
* taken from the esbuild config.
|
|
31
|
+
*/
|
|
32
|
+
export function estrogen(options = {}) {
|
|
33
|
+
return {
|
|
34
|
+
name: '@sysce/estrogen',
|
|
35
|
+
setup(build) {
|
|
36
|
+
build.onEnd(async (result) => {
|
|
37
|
+
if (result.errors.length > 0)
|
|
38
|
+
return;
|
|
39
|
+
const outDir = resolveOutputDir(build);
|
|
40
|
+
if (outDir === null) {
|
|
41
|
+
return {
|
|
42
|
+
errors: [
|
|
43
|
+
{
|
|
44
|
+
text: 'estrogen could not find the esbuild output directory. ' +
|
|
45
|
+
'Set outdir or outfile so estrogen knows what to rewrite.',
|
|
46
|
+
},
|
|
47
|
+
],
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
await obfuscate({ ...options, dirs: outDir });
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
56
|
+
return { errors: [{ text: `estrogen failed: ${message}` }] };
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Resolve the absolute output directory from an esbuild build. Prefers `outdir`,
|
|
64
|
+
* falls back to the directory of `outfile`, and resolves relative paths against
|
|
65
|
+
* `absWorkingDir`. Returns null when neither is set.
|
|
66
|
+
*/
|
|
67
|
+
function resolveOutputDir(build) {
|
|
68
|
+
const { outdir, outfile, absWorkingDir } = build.initialOptions;
|
|
69
|
+
const base = absWorkingDir ?? process.cwd();
|
|
70
|
+
if (outdir !== undefined)
|
|
71
|
+
return resolvePath(base, outdir);
|
|
72
|
+
if (outfile !== undefined)
|
|
73
|
+
return resolvePath(base, dirname(outfile));
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
export default estrogen;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@sysce/estrogen`: run the estrogen obfuscator from Node.
|
|
3
|
+
*
|
|
4
|
+
* estrogen rewrites an already built output directory, in place: string and
|
|
5
|
+
* template literals into an encrypted per-file pool, proxy and store names into
|
|
6
|
+
* per-build substitutes, and the embedded wasm names to match. It raises the
|
|
7
|
+
* cost of reading the build. It is not secrecy: a decoder key ships in each
|
|
8
|
+
* file, and anyone with devtools can run the code.
|
|
9
|
+
*
|
|
10
|
+
* - {@link obfuscate} spawns the binary over one or more directories.
|
|
11
|
+
* - The esbuild plugin lives at `@sysce/estrogen/esbuild`.
|
|
12
|
+
* - The Rsbuild plugin lives at `@sysce/estrogen/rsbuild`.
|
|
13
|
+
* - The Vite plugin lives at `@sysce/estrogen/vite`.
|
|
14
|
+
* - The Rollup plugin lives at `@sysce/estrogen/rollup`.
|
|
15
|
+
* - The Astro integration lives at `@sysce/estrogen/astro`.
|
|
16
|
+
*
|
|
17
|
+
* @packageDocumentation
|
|
18
|
+
*/
|
|
19
|
+
export { obfuscate } from './obfuscate.js';
|
|
20
|
+
export type { CloakInput, LiveInjectorOptions, Options, PluginOptions, VendorFileInfo, VendorInput, VendorRewrite, } from './options.js';
|
|
21
|
+
export { normalizeDirs, buildArgs } from './options.js';
|
|
22
|
+
export { EstrogenError } from './report.js';
|
|
23
|
+
export type { Report } from './report.js';
|
|
24
|
+
export { PLATFORM_PACKAGES, MissingBinaryError, platformPackageName, targetKey, binSubpath, resolveBinaryPath, } from './resolve-binary.js';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@sysce/estrogen`: run the estrogen obfuscator from Node.
|
|
3
|
+
*
|
|
4
|
+
* estrogen rewrites an already built output directory, in place: string and
|
|
5
|
+
* template literals into an encrypted per-file pool, proxy and store names into
|
|
6
|
+
* per-build substitutes, and the embedded wasm names to match. It raises the
|
|
7
|
+
* cost of reading the build. It is not secrecy: a decoder key ships in each
|
|
8
|
+
* file, and anyone with devtools can run the code.
|
|
9
|
+
*
|
|
10
|
+
* - {@link obfuscate} spawns the binary over one or more directories.
|
|
11
|
+
* - The esbuild plugin lives at `@sysce/estrogen/esbuild`.
|
|
12
|
+
* - The Rsbuild plugin lives at `@sysce/estrogen/rsbuild`.
|
|
13
|
+
* - The Vite plugin lives at `@sysce/estrogen/vite`.
|
|
14
|
+
* - The Rollup plugin lives at `@sysce/estrogen/rollup`.
|
|
15
|
+
* - The Astro integration lives at `@sysce/estrogen/astro`.
|
|
16
|
+
*
|
|
17
|
+
* @packageDocumentation
|
|
18
|
+
*/
|
|
19
|
+
export { obfuscate } from './obfuscate.js';
|
|
20
|
+
export { normalizeDirs, buildArgs } from './options.js';
|
|
21
|
+
export { EstrogenError } from './report.js';
|
|
22
|
+
export { PLATFORM_PACKAGES, MissingBinaryError, platformPackageName, targetKey, binSubpath, resolveBinaryPath, } from './resolve-binary.js';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// ESM runtime for getRequire. Copied to dist/esm/node-require.js by the build.
|
|
2
|
+
// import.meta.url anchors the require in this file's package so it resolves the
|
|
3
|
+
// optional binary packages correctly.
|
|
4
|
+
import { createRequire } from 'node:module';
|
|
5
|
+
|
|
6
|
+
export function getRequire() {
|
|
7
|
+
return createRequire(import.meta.url);
|
|
8
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Options } from './options.js';
|
|
2
|
+
import { type Report } from './report.js';
|
|
3
|
+
/**
|
|
4
|
+
* Run estrogen over one or more built directories.
|
|
5
|
+
*
|
|
6
|
+
* Resolves the per-platform binary (or uses {@link Options.binaryPath}), spawns
|
|
7
|
+
* it once with the flags and directories from `options`, and collects its
|
|
8
|
+
* output. Resolves with a {@link Report} when estrogen exits 0. Rejects with a
|
|
9
|
+
* {@link EstrogenError} when it exits nonzero or fails to launch; the error carries
|
|
10
|
+
* the partial report.
|
|
11
|
+
*
|
|
12
|
+
* estrogen rewrites the directories in place and raises the cost of reading the
|
|
13
|
+
* build. It is not secrecy: a decoder key ships in each file.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { obfuscate } from '@sysce/estrogen';
|
|
18
|
+
*
|
|
19
|
+
* const report = await obfuscate({ dirs: 'dist', canary: 'canary-list.json' });
|
|
20
|
+
* console.log(report.stdout);
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function obfuscate(options: Options): Promise<Report>;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { readFile } from 'node:fs/promises';
|
|
3
|
+
import { resolve as resolvePath } from 'node:path';
|
|
4
|
+
import { getRequire } from './node-require.js';
|
|
5
|
+
import { buildArgs, normalizeDirs } from './options.js';
|
|
6
|
+
import { EstrogenError, functionSourceWarning, parseDetails, } from './report.js';
|
|
7
|
+
import { resolveBinaryPath } from './resolve-binary.js';
|
|
8
|
+
import { stageVendor } from './vendor.js';
|
|
9
|
+
/**
|
|
10
|
+
* Run estrogen over one or more built directories.
|
|
11
|
+
*
|
|
12
|
+
* Resolves the per-platform binary (or uses {@link Options.binaryPath}), spawns
|
|
13
|
+
* it once with the flags and directories from `options`, and collects its
|
|
14
|
+
* output. Resolves with a {@link Report} when estrogen exits 0. Rejects with a
|
|
15
|
+
* {@link EstrogenError} when it exits nonzero or fails to launch; the error carries
|
|
16
|
+
* the partial report.
|
|
17
|
+
*
|
|
18
|
+
* estrogen rewrites the directories in place and raises the cost of reading the
|
|
19
|
+
* build. It is not secrecy: a decoder key ships in each file.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { obfuscate } from '@sysce/estrogen';
|
|
24
|
+
*
|
|
25
|
+
* const report = await obfuscate({ dirs: 'dist', canary: 'canary-list.json' });
|
|
26
|
+
* console.log(report.stdout);
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function obfuscate(options) {
|
|
30
|
+
const dirs = normalizeDirs(options.dirs);
|
|
31
|
+
if (dirs.length === 0) {
|
|
32
|
+
return Promise.reject(new EstrogenError('estrogen needs at least one directory to rewrite; set options.dirs.', {
|
|
33
|
+
ok: false,
|
|
34
|
+
exitCode: -1,
|
|
35
|
+
dirs,
|
|
36
|
+
binaryPath: options.binaryPath ?? '',
|
|
37
|
+
args: [],
|
|
38
|
+
stdout: '',
|
|
39
|
+
stderr: '',
|
|
40
|
+
durationMs: 0,
|
|
41
|
+
}));
|
|
42
|
+
}
|
|
43
|
+
let binaryPath;
|
|
44
|
+
try {
|
|
45
|
+
binaryPath = resolveBinaryPath(getRequire(), options.binaryPath);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
return Promise.reject(error);
|
|
49
|
+
}
|
|
50
|
+
if (!options.vendor?.length)
|
|
51
|
+
return run(binaryPath, buildArgs(options), dirs, options);
|
|
52
|
+
return vendorRun(binaryPath, dirs, options);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Stage the vendor inputs, run `estrogen build` with them, and read each dir's
|
|
56
|
+
* vendor manifest into the report. The staging directory is removed after the
|
|
57
|
+
* run, whether it passed or not.
|
|
58
|
+
*/
|
|
59
|
+
async function vendorRun(binaryPath, dirs, options) {
|
|
60
|
+
const cwd = options.cwd ?? process.cwd();
|
|
61
|
+
const staged = await stageVendor(options.vendor ?? [], cwd);
|
|
62
|
+
try {
|
|
63
|
+
const args = [
|
|
64
|
+
'build',
|
|
65
|
+
'--vendor-file',
|
|
66
|
+
staged.vendorFile,
|
|
67
|
+
...buildArgs(options),
|
|
68
|
+
];
|
|
69
|
+
const report = await run(binaryPath, args, dirs, options);
|
|
70
|
+
const manifest = options.manifest;
|
|
71
|
+
if (manifest === false)
|
|
72
|
+
return report;
|
|
73
|
+
report.manifests = await Promise.all(dirs.map((dir) => readFile(resolvePath(resolvePath(cwd, dir), manifest ?? 'estrogen-vendor-manifest.json'), 'utf8')
|
|
74
|
+
.then((text) => JSON.parse(text))
|
|
75
|
+
.catch(() => undefined)));
|
|
76
|
+
return report;
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
await staged.cleanup();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/** Spawn the binary once with `args` and collect its output. */
|
|
83
|
+
function run(binaryPath, args, dirs, options) {
|
|
84
|
+
const started = Date.now();
|
|
85
|
+
return new Promise((resolve, reject) => {
|
|
86
|
+
// The binary sees a pipe, so ask it for color when this process
|
|
87
|
+
// writes its warning to a terminal.
|
|
88
|
+
const color = process.stderr.isTTY &&
|
|
89
|
+
process.env.NO_COLOR === undefined &&
|
|
90
|
+
process.env.FORCE_COLOR === undefined;
|
|
91
|
+
const child = spawn(binaryPath, args, {
|
|
92
|
+
cwd: options.cwd ?? process.cwd(),
|
|
93
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
94
|
+
env: color ? { ...process.env, FORCE_COLOR: '1' } : process.env,
|
|
95
|
+
});
|
|
96
|
+
let stdout = '';
|
|
97
|
+
let stderr = '';
|
|
98
|
+
child.stdout.setEncoding('utf8');
|
|
99
|
+
child.stderr.setEncoding('utf8');
|
|
100
|
+
child.stdout.on('data', (chunk) => {
|
|
101
|
+
stdout += chunk;
|
|
102
|
+
});
|
|
103
|
+
child.stderr.on('data', (chunk) => {
|
|
104
|
+
stderr += chunk;
|
|
105
|
+
});
|
|
106
|
+
child.on('error', (error) => {
|
|
107
|
+
const report = {
|
|
108
|
+
ok: false,
|
|
109
|
+
exitCode: -1,
|
|
110
|
+
dirs,
|
|
111
|
+
binaryPath,
|
|
112
|
+
args,
|
|
113
|
+
stdout,
|
|
114
|
+
stderr,
|
|
115
|
+
durationMs: Date.now() - started,
|
|
116
|
+
};
|
|
117
|
+
reject(new EstrogenError(`estrogen failed to start: ${error.message}`, report));
|
|
118
|
+
});
|
|
119
|
+
child.on('close', (code, signal) => {
|
|
120
|
+
const exitCode = code ?? -1;
|
|
121
|
+
// A plugin never prints stderr, so pass the warning through here.
|
|
122
|
+
const warning = functionSourceWarning(stderr);
|
|
123
|
+
if (warning !== undefined)
|
|
124
|
+
process.stderr.write(warning);
|
|
125
|
+
const report = {
|
|
126
|
+
ok: exitCode === 0,
|
|
127
|
+
exitCode,
|
|
128
|
+
dirs,
|
|
129
|
+
binaryPath,
|
|
130
|
+
args,
|
|
131
|
+
stdout,
|
|
132
|
+
stderr,
|
|
133
|
+
durationMs: Date.now() - started,
|
|
134
|
+
details: parseDetails(stdout),
|
|
135
|
+
warning,
|
|
136
|
+
};
|
|
137
|
+
if (report.ok) {
|
|
138
|
+
resolve(report);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const reason = signal ? `signal ${signal}` : `exit code ${exitCode}`;
|
|
142
|
+
reject(new EstrogenError(`estrogen exited with ${reason}.`, report));
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
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
|
+
/** One reference rewrite on a vendored file (`[[vendor.rewrite]]`). */
|
|
15
|
+
export interface VendorRewrite {
|
|
16
|
+
/** The text to find. */
|
|
17
|
+
find: string;
|
|
18
|
+
/** Its replacement. `{stamp}` expands to the per-build stamp. */
|
|
19
|
+
to: string;
|
|
20
|
+
/** Fail the build when `find` matches nothing across the input. Default `true`. */
|
|
21
|
+
required?: boolean;
|
|
22
|
+
/** Fail the build unless `find` matches exactly this many times. */
|
|
23
|
+
count?: number;
|
|
24
|
+
}
|
|
25
|
+
/** What a {@link VendorInput} hook learns about one file. */
|
|
26
|
+
export interface VendorFileInfo {
|
|
27
|
+
/** The file's path relative to `from`, with forward slashes. */
|
|
28
|
+
path: string;
|
|
29
|
+
/** The absolute source file. */
|
|
30
|
+
file: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* One vendored input, the same as a `[[vendor]]` table in the config, plus two
|
|
34
|
+
* optional hooks that run in Node before estrogen sees the file. With either
|
|
35
|
+
* hook set, `from` must be a file or a directory, not a glob.
|
|
36
|
+
*/
|
|
37
|
+
export interface VendorInput {
|
|
38
|
+
/** A file, a directory, or (without hooks) a glob, relative to `cwd`. */
|
|
39
|
+
from: string;
|
|
40
|
+
/** The destination under the dist root. */
|
|
41
|
+
to: string;
|
|
42
|
+
/** `'verbatim'` (the default) copies the file; `'obfuscate'` runs it through estrogen. */
|
|
43
|
+
mode?: 'verbatim' | 'obfuscate';
|
|
44
|
+
/** Insert the per-build stamp before each file's extension. Default `false`. */
|
|
45
|
+
stamp?: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Compile each obfuscated JS file as an ES module. Default `false`: a
|
|
48
|
+
* vendored file's kind comes from its own syntax, and `moduleGlobs` never
|
|
49
|
+
* applies to it.
|
|
50
|
+
*/
|
|
51
|
+
module?: boolean;
|
|
52
|
+
/** Reference rewrites with required and count assertions. */
|
|
53
|
+
rewrite?: VendorRewrite[];
|
|
54
|
+
/**
|
|
55
|
+
* Choose each file's destination, relative to `to`. Return a path to place
|
|
56
|
+
* it there, `undefined` to keep its own path, or `null` to leave it out.
|
|
57
|
+
*/
|
|
58
|
+
rename?: (path: string, info: VendorFileInfo) => string | null | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Change a file's contents before estrogen copies it. Return the new
|
|
61
|
+
* contents, or `undefined` to keep them. Runs after `rename`, only for files
|
|
62
|
+
* that are kept.
|
|
63
|
+
*/
|
|
64
|
+
transform?: (contents: Buffer, info: VendorFileInfo) => string | Uint8Array | undefined | Promise<string | Uint8Array | undefined>;
|
|
65
|
+
}
|
|
66
|
+
export interface Options {
|
|
67
|
+
/**
|
|
68
|
+
* Directories to rewrite in place. Pass one string or a list. The CLI accepts
|
|
69
|
+
* `estrogen <dir>...`, so a list runs one process over every directory.
|
|
70
|
+
*
|
|
71
|
+
* The plugins fill this in from the build's output directory, so plugin
|
|
72
|
+
* callers leave it unset. A direct {@link obfuscate} call must set it.
|
|
73
|
+
*/
|
|
74
|
+
dirs?: string | string[];
|
|
75
|
+
/**
|
|
76
|
+
* Config file for estrogen (`-c`/`--config <file>`). Per-site settings that do
|
|
77
|
+
* not fit a flag live here.
|
|
78
|
+
*/
|
|
79
|
+
config?: string;
|
|
80
|
+
/**
|
|
81
|
+
* Canary list file (`--canary <file>`). The list of literals that must not
|
|
82
|
+
* survive verbatim in the output. estrogen fails nonzero if one does.
|
|
83
|
+
*/
|
|
84
|
+
canary?: string;
|
|
85
|
+
/**
|
|
86
|
+
* Names file (`--names <file>`). The proxy and store names to rename for this
|
|
87
|
+
* site.
|
|
88
|
+
*/
|
|
89
|
+
names?: string;
|
|
90
|
+
/**
|
|
91
|
+
* Source map output directory (`--sourcemap <dir>`). Off by default. When set,
|
|
92
|
+
* estrogen writes maps here, outside the served tree, for private debugging. A
|
|
93
|
+
* map traces the output back to its source, so keep this directory private
|
|
94
|
+
* and never ship it.
|
|
95
|
+
*/
|
|
96
|
+
sourcemap?: string;
|
|
97
|
+
/**
|
|
98
|
+
* Seed for the per-file and per-build randomness (`--seed <hex>`). Fixes the
|
|
99
|
+
* output so two runs on one input match. Use it for tests and parity harnesses
|
|
100
|
+
* only. Production builds leave it unset and use real randomness.
|
|
101
|
+
*/
|
|
102
|
+
seed?: string;
|
|
103
|
+
/**
|
|
104
|
+
* Worker count (`-j`/`--jobs <n>`). estrogen processes files in parallel; this
|
|
105
|
+
* caps the workers. Unset lets the binary pick.
|
|
106
|
+
*/
|
|
107
|
+
jobs?: number;
|
|
108
|
+
/**
|
|
109
|
+
* Check mode (`--check`). Report what estrogen would do and run the canary check
|
|
110
|
+
* without writing changes. Useful in CI to gate a build.
|
|
111
|
+
*/
|
|
112
|
+
check?: boolean;
|
|
113
|
+
/**
|
|
114
|
+
* Optimize `.svg` asset files (`--no-optimize-svg` turns it off). On by
|
|
115
|
+
* default: estrogen shrinks each plain image SVG with the SVGO default job set
|
|
116
|
+
* (drops metadata and comments, collapses whitespace, rounds path numbers,
|
|
117
|
+
* removes default attributes) and writes the smaller result. This replaces a
|
|
118
|
+
* site's separate `svgmin` step. A cloak SVG (one with an inline `<script>`)
|
|
119
|
+
* is left for the markup pass. Set this to `false` to keep the SVGs as the
|
|
120
|
+
* bundler wrote them.
|
|
121
|
+
*/
|
|
122
|
+
optimizeSvg?: boolean;
|
|
123
|
+
/**
|
|
124
|
+
* Minify `.html` shells (`--no-minify-html` turns it off). On by default:
|
|
125
|
+
* estrogen strips comments and collapses insignificant whitespace before the
|
|
126
|
+
* cloak pass, leaving `<pre>`, `<textarea>`, `<script>` and `<style>` content
|
|
127
|
+
* intact. Set this to `false` to keep the HTML as the bundler wrote it.
|
|
128
|
+
*/
|
|
129
|
+
minifyHtml?: boolean;
|
|
130
|
+
/**
|
|
131
|
+
* Minify `.css` stylesheets (`--no-minify-css` turns it off). On by default:
|
|
132
|
+
* estrogen minifies each stylesheet with lightningcss. CSS is not an
|
|
133
|
+
* obfuscation target, so this only shrinks it. Set this to `false` to keep the
|
|
134
|
+
* CSS as the bundler wrote it.
|
|
135
|
+
*/
|
|
136
|
+
minifyCss?: boolean;
|
|
137
|
+
/** Inject a selected frontend in a closed shadow overlay after obfuscation. Off by default. */
|
|
138
|
+
injector?: LiveInjectorOptions;
|
|
139
|
+
/**
|
|
140
|
+
* Glob patterns relative to the dist root that identify ES module scripts
|
|
141
|
+
* without import/export syntax. Only set these for files actually loaded as
|
|
142
|
+
* modules. Classic scripts expose top-level var/function on globalThis.
|
|
143
|
+
*/
|
|
144
|
+
moduleGlobs?: string[];
|
|
145
|
+
/**
|
|
146
|
+
* Glob patterns relative to the dist root for JS files that stay out of the
|
|
147
|
+
* bytecode VM (`--no-vm-glob`). They take the literal-pool and name passes.
|
|
148
|
+
* Use for code that rebuilds functions from `fn.toString()`, since a VM
|
|
149
|
+
* function's source text is the VM call wrapper.
|
|
150
|
+
*/
|
|
151
|
+
noVmGlobs?: string[];
|
|
152
|
+
/**
|
|
153
|
+
* Keep the source text of a VM function the code turns into a string, such
|
|
154
|
+
* as `f.toString()`, `String(f)` or `f + ''`, so code that rebuilds it from
|
|
155
|
+
* its text still runs. On by default. `false` passes `--no-function-source`,
|
|
156
|
+
* and such a function reads as `[native code]`.
|
|
157
|
+
*/
|
|
158
|
+
functionSource?: boolean;
|
|
159
|
+
/**
|
|
160
|
+
* Print the warning that lists functions the code turns into strings. On by
|
|
161
|
+
* default. `false` passes `--no-function-source-warning`.
|
|
162
|
+
*/
|
|
163
|
+
functionSourceWarning?: boolean;
|
|
164
|
+
/**
|
|
165
|
+
* Files to copy into the dist before obfuscating, as `[[vendor]]` tables in
|
|
166
|
+
* the config are. Setting this runs `estrogen build`, which also vendors the
|
|
167
|
+
* config's own `[[vendor]]` inputs, and adds these after them. See
|
|
168
|
+
* {@link VendorInput} for the `rename` and `transform` hooks.
|
|
169
|
+
*/
|
|
170
|
+
vendor?: VendorInput[];
|
|
171
|
+
/**
|
|
172
|
+
* Where `estrogen build` writes the vendor manifest (`--manifest`): relative
|
|
173
|
+
* to each dir, or absolute. `false` writes none (`--no-manifest`), for a
|
|
174
|
+
* dist served as is where the manifest would be public. The default is
|
|
175
|
+
* `estrogen-vendor-manifest.json` under each dir.
|
|
176
|
+
*/
|
|
177
|
+
manifest?: string | false;
|
|
178
|
+
/**
|
|
179
|
+
* Emit the VM runtime once as a shared module that each ES module VM chunk
|
|
180
|
+
* imports (`--shared-runtime`), instead of inlining a copy in every chunk.
|
|
181
|
+
* A classic script and a vendored file keep the inline copy.
|
|
182
|
+
*/
|
|
183
|
+
sharedRuntime?: boolean;
|
|
184
|
+
/** Quiet output (`--quiet`). Suppresses progress logging. */
|
|
185
|
+
quiet?: boolean;
|
|
186
|
+
/** Verbose output (`-v`/`--verbose`). Logs per-file detail. */
|
|
187
|
+
verbose?: boolean;
|
|
188
|
+
/**
|
|
189
|
+
* Path to the estrogen binary. Overrides the per-platform package resolution.
|
|
190
|
+
* Set it to run a locally built binary or a stub in tests. Normally leave it
|
|
191
|
+
* unset and let the matching `@sysce/estrogen-<os>-<cpu>` package provide it.
|
|
192
|
+
*/
|
|
193
|
+
binaryPath?: string;
|
|
194
|
+
/** Working directory for the spawned process. Defaults to `process.cwd()`. */
|
|
195
|
+
cwd?: string;
|
|
196
|
+
/**
|
|
197
|
+
* Extra arguments appended after the flags and before the directories. An
|
|
198
|
+
* escape hatch for flags this type does not model yet. Prefer a typed field
|
|
199
|
+
* when one exists.
|
|
200
|
+
*/
|
|
201
|
+
extraArgs?: string[];
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Plugin options: {@link Options} without `dirs`, which a plugin takes from the
|
|
205
|
+
* build's output directory. Everything else carries through to estrogen.
|
|
206
|
+
*/
|
|
207
|
+
export type PluginOptions = Omit<Options, 'dirs'>;
|
|
208
|
+
/**
|
|
209
|
+
* Normalize {@link Options.dirs} to a list. Returns an empty list when unset.
|
|
210
|
+
*/
|
|
211
|
+
export declare function normalizeDirs(dirs: Options['dirs']): string[];
|
|
212
|
+
/**
|
|
213
|
+
* Turn {@link Options} into the estrogen CLI argument vector. The order is flags
|
|
214
|
+
* first, then `extraArgs`, then the directories, matching `estrogen [options]
|
|
215
|
+
* <dir>...`.
|
|
216
|
+
*/
|
|
217
|
+
export declare function buildArgs(options: Options): string[];
|