@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.
- package/LICENSE +15 -0
- package/README.md +235 -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 +154 -0
- package/dist/cjs/options.d.ts +264 -0
- package/dist/cjs/options.js +105 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/report.d.ts +74 -0
- package/dist/cjs/report.js +55 -0
- package/dist/cjs/resolve-binary.d.ts +46 -0
- package/dist/cjs/resolve-binary.js +114 -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 +151 -0
- package/dist/esm/options.d.ts +264 -0
- package/dist/esm/options.js +101 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/report.d.ts +74 -0
- package/dist/esm/report.js +49 -0
- package/dist/esm/resolve-binary.d.ts +46 -0
- package/dist/esm/resolve-binary.js +106 -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 +283 -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,151 @@
|
|
|
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', async (code, signal) => {
|
|
120
|
+
const exitCode = code ?? -1;
|
|
121
|
+
const run = options.report === undefined
|
|
122
|
+
? undefined
|
|
123
|
+
: await readFile(resolvePath(options.cwd ?? process.cwd(), options.report), 'utf8')
|
|
124
|
+
.then((text) => JSON.parse(text))
|
|
125
|
+
.catch(() => undefined);
|
|
126
|
+
// A plugin never prints stderr, so pass the warning through here.
|
|
127
|
+
const warning = functionSourceWarning(stderr);
|
|
128
|
+
if (warning !== undefined)
|
|
129
|
+
process.stderr.write(warning);
|
|
130
|
+
const report = {
|
|
131
|
+
ok: exitCode === 0,
|
|
132
|
+
exitCode,
|
|
133
|
+
dirs,
|
|
134
|
+
binaryPath,
|
|
135
|
+
args,
|
|
136
|
+
stdout,
|
|
137
|
+
stderr,
|
|
138
|
+
durationMs: Date.now() - started,
|
|
139
|
+
details: parseDetails(stdout),
|
|
140
|
+
warning,
|
|
141
|
+
run,
|
|
142
|
+
};
|
|
143
|
+
if (report.ok) {
|
|
144
|
+
resolve(report);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const reason = signal ? `signal ${signal}` : `exit code ${exitCode}`;
|
|
148
|
+
reject(new EstrogenError(`estrogen exited with ${reason}.`, report));
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
}
|