@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,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,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.pluginEstrogen = pluginEstrogen;
|
|
4
|
+
const obfuscate_js_1 = require("./obfuscate.js");
|
|
5
|
+
/**
|
|
6
|
+
* Rsbuild plugin that runs estrogen over the resolved dist directory after the
|
|
7
|
+
* build finishes.
|
|
8
|
+
*
|
|
9
|
+
* It reads the dist path from the Rsbuild context (`api.context.distPath`),
|
|
10
|
+
* which is the absolute output directory Rsbuild resolved for the build, and
|
|
11
|
+
* runs estrogen in `onAfterBuild`, once the files are written. An estrogen failure is
|
|
12
|
+
* rethrown so the build fails.
|
|
13
|
+
*
|
|
14
|
+
* estrogen rewrites that directory in place and raises the cost of reading it. It
|
|
15
|
+
* is not secrecy: a decoder key ships in each file.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { defineConfig } from '@rsbuild/core';
|
|
20
|
+
* import { pluginEstrogen } from '@sysce/estrogen/rsbuild';
|
|
21
|
+
*
|
|
22
|
+
* export default defineConfig({
|
|
23
|
+
* plugins: [pluginEstrogen({ canary: 'canary-list.json' })],
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @param options estrogen options. `dirs` is ignored; the dist path comes from the
|
|
28
|
+
* Rsbuild context.
|
|
29
|
+
*/
|
|
30
|
+
function pluginEstrogen(options = {}) {
|
|
31
|
+
return {
|
|
32
|
+
name: '@sysce/estrogen',
|
|
33
|
+
setup(api) {
|
|
34
|
+
api.onAfterBuild(async () => {
|
|
35
|
+
const distPath = api.context.distPath;
|
|
36
|
+
if (!distPath) {
|
|
37
|
+
throw new Error('estrogen could not find the Rsbuild dist path. ' +
|
|
38
|
+
'Set output.distPath.root so estrogen knows what to rewrite.');
|
|
39
|
+
}
|
|
40
|
+
await (0, obfuscate_js_1.obfuscate)({ ...options, dirs: distPath });
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
exports.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,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.stageVendor = stageVendor;
|
|
4
|
+
const promises_1 = require("node:fs/promises");
|
|
5
|
+
const node_os_1 = require("node:os");
|
|
6
|
+
const node_path_1 = require("node:path");
|
|
7
|
+
/** Characters that make a `from` a glob, as the binary reads it. */
|
|
8
|
+
const GLOB = /[*?[\]{}]/;
|
|
9
|
+
/** Every file under `dir`, as forward-slash paths relative to it, sorted. */
|
|
10
|
+
async function walk(dir, base = dir) {
|
|
11
|
+
const out = [];
|
|
12
|
+
const entries = await (0, promises_1.readdir)(dir, { withFileTypes: true });
|
|
13
|
+
entries.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
14
|
+
for (const entry of entries) {
|
|
15
|
+
const full = (0, node_path_1.join)(dir, entry.name);
|
|
16
|
+
if (entry.isDirectory())
|
|
17
|
+
out.push(...(await walk(full, base)));
|
|
18
|
+
else if (entry.isFile())
|
|
19
|
+
out.push((0, node_path_1.relative)(base, full).split(node_path_1.sep).join('/'));
|
|
20
|
+
}
|
|
21
|
+
return out;
|
|
22
|
+
}
|
|
23
|
+
/** A destination from `rename` must stay inside `to`. */
|
|
24
|
+
function checkDestination(input, path, dest) {
|
|
25
|
+
const parts = dest.split('/');
|
|
26
|
+
if (dest === '' || dest.startsWith('/') || parts.includes('..'))
|
|
27
|
+
throw new Error(`estrogen: vendor rename for ${input.from}: ${path} -> ${JSON.stringify(dest)} leaves \`to\``);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Write the vendor inputs to a JSON file for `--vendor-file`. An input with a
|
|
31
|
+
* `rename` or `transform` hook is staged first: each kept file is written,
|
|
32
|
+
* renamed and transformed, into a temporary directory, and the input points
|
|
33
|
+
* there instead. An input without hooks passes through with `from` resolved
|
|
34
|
+
* against `cwd`.
|
|
35
|
+
*/
|
|
36
|
+
async function stageVendor(inputs, cwd) {
|
|
37
|
+
const root = await (0, promises_1.mkdtemp)((0, node_path_1.join)((0, node_os_1.tmpdir)(), 'estrogen-vendor-'));
|
|
38
|
+
const cleanup = () => (0, promises_1.rm)(root, { recursive: true, force: true });
|
|
39
|
+
try {
|
|
40
|
+
const tables = [];
|
|
41
|
+
for (const [index, input] of inputs.entries()) {
|
|
42
|
+
const from = (0, node_path_1.resolve)(cwd, input.from);
|
|
43
|
+
const table = {
|
|
44
|
+
from,
|
|
45
|
+
to: input.to,
|
|
46
|
+
mode: input.mode ?? 'verbatim',
|
|
47
|
+
stamp: input.stamp ?? false,
|
|
48
|
+
module: input.module ?? false,
|
|
49
|
+
rewrite: input.rewrite ?? [],
|
|
50
|
+
};
|
|
51
|
+
if (input.rename || input.transform) {
|
|
52
|
+
if (GLOB.test(input.from))
|
|
53
|
+
throw new Error(`estrogen: vendor ${input.from}: rename and transform need a file or a directory, not a glob`);
|
|
54
|
+
const staged = (0, node_path_1.join)(root, String(index));
|
|
55
|
+
const isDir = (await (0, promises_1.stat)(from)).isDirectory();
|
|
56
|
+
const files = isDir ? await walk(from) : [from.split(node_path_1.sep).pop()];
|
|
57
|
+
let kept = 0;
|
|
58
|
+
let last = staged;
|
|
59
|
+
for (const path of files) {
|
|
60
|
+
const info = {
|
|
61
|
+
path,
|
|
62
|
+
file: isDir ? (0, node_path_1.join)(from, ...path.split('/')) : from,
|
|
63
|
+
};
|
|
64
|
+
const renamed = input.rename?.(path, info);
|
|
65
|
+
if (renamed === null)
|
|
66
|
+
continue;
|
|
67
|
+
const dest = renamed ?? path;
|
|
68
|
+
checkDestination(input, path, dest);
|
|
69
|
+
const contents = await (0, promises_1.readFile)(info.file);
|
|
70
|
+
const changed = await input.transform?.(contents, info);
|
|
71
|
+
const target = (0, node_path_1.join)(staged, ...dest.split('/'));
|
|
72
|
+
await (0, promises_1.mkdir)((0, node_path_1.dirname)(target), { recursive: true });
|
|
73
|
+
await (0, promises_1.writeFile)(target, changed ?? contents);
|
|
74
|
+
last = target;
|
|
75
|
+
kept += 1;
|
|
76
|
+
}
|
|
77
|
+
// A hook that drops every file would hand the binary an empty
|
|
78
|
+
// input; say so here, where the cause is.
|
|
79
|
+
if (kept === 0)
|
|
80
|
+
throw new Error(`estrogen: vendor ${input.from}: rename left no files`);
|
|
81
|
+
// A single file stays a single file, so `to` means what it does
|
|
82
|
+
// without hooks: the file's name, or a directory that exists.
|
|
83
|
+
table.from = isDir ? staged : last;
|
|
84
|
+
}
|
|
85
|
+
tables.push(table);
|
|
86
|
+
}
|
|
87
|
+
const vendorFile = (0, node_path_1.join)(root, 'vendor.json');
|
|
88
|
+
await (0, promises_1.writeFile)(vendorFile, JSON.stringify(tables));
|
|
89
|
+
return { vendorFile, cleanup };
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
await cleanup();
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -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;
|
package/dist/cjs/vite.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.pluginEstrogen = pluginEstrogen;
|
|
4
|
+
const node_path_1 = require("node:path");
|
|
5
|
+
const obfuscate_js_1 = require("./obfuscate.js");
|
|
6
|
+
/**
|
|
7
|
+
* Vite plugin that runs estrogen over the build output directory once Vite has
|
|
8
|
+
* written it.
|
|
9
|
+
*
|
|
10
|
+
* It reads the output directory from the resolved config (`build.outDir`,
|
|
11
|
+
* resolved against `root`) in `configResolved`, and runs estrogen in
|
|
12
|
+
* `closeBundle`, after the bundle is on disk. `apply: 'build'` with
|
|
13
|
+
* `enforce: 'post'` keeps it out of `vite serve` and after every other plugin,
|
|
14
|
+
* so it rewrites the finished build. An estrogen failure is thrown so the build
|
|
15
|
+
* fails.
|
|
16
|
+
*
|
|
17
|
+
* estrogen rewrites that directory in place and raises the cost of reading it.
|
|
18
|
+
* It is not secrecy: a decoder key ships in each file.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { defineConfig } from 'vite';
|
|
23
|
+
* import { pluginEstrogen } from '@sysce/estrogen/vite';
|
|
24
|
+
*
|
|
25
|
+
* export default defineConfig({
|
|
26
|
+
* plugins: [pluginEstrogen({ canary: 'canary-list.json' })],
|
|
27
|
+
* });
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @param options estrogen options. `dirs` is ignored; the output directory comes
|
|
31
|
+
* from the Vite config.
|
|
32
|
+
*/
|
|
33
|
+
function pluginEstrogen(options = {}) {
|
|
34
|
+
let outDir = null;
|
|
35
|
+
return {
|
|
36
|
+
name: '@sysce/estrogen',
|
|
37
|
+
apply: 'build',
|
|
38
|
+
enforce: 'post',
|
|
39
|
+
configResolved(config) {
|
|
40
|
+
outDir = (0, node_path_1.resolve)(config.root, config.build.outDir);
|
|
41
|
+
},
|
|
42
|
+
async closeBundle() {
|
|
43
|
+
if (outDir === null)
|
|
44
|
+
return;
|
|
45
|
+
await (0, obfuscate_js_1.obfuscate)({ ...options, dirs: outDir });
|
|
46
|
+
},
|
|
47
|
+
// Watcher seam: `estrogen --watch` and dev-server HMR land as a companion
|
|
48
|
+
// serve-mode plugin (configureServer + handleHotUpdate) that re-runs the
|
|
49
|
+
// vendor and obfuscate step on change. This build plugin stays build-only;
|
|
50
|
+
// the dev integration is tracked separately and not wired here.
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
exports.default = pluginEstrogen;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { AstroIntegration } from 'astro';
|
|
2
|
+
import type { PluginOptions } from './options.js';
|
|
3
|
+
/**
|
|
4
|
+
* Astro integration that runs estrogen over the build output directory once Astro
|
|
5
|
+
* has written it.
|
|
6
|
+
*
|
|
7
|
+
* It hooks `astro:build:done`, which runs after the build is on disk, and reads
|
|
8
|
+
* the final output directory from that hook's `dir` URL through
|
|
9
|
+
* `fileURLToPath`. For a static (SSG) build this is the directory Astro serves;
|
|
10
|
+
* for an SSR build it is the client output directory, so the integration
|
|
11
|
+
* rewrites the client assets. An estrogen failure is thrown so the build fails.
|
|
12
|
+
*
|
|
13
|
+
* estrogen rewrites that directory in place and raises the cost of reading it.
|
|
14
|
+
* It is not secrecy: a decoder key ships in each file, and anyone with devtools
|
|
15
|
+
* can run the code.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { defineConfig } from 'astro/config';
|
|
20
|
+
* import estrogen from '@sysce/estrogen/astro';
|
|
21
|
+
*
|
|
22
|
+
* export default defineConfig({
|
|
23
|
+
* integrations: [estrogen({ canary: 'canary-list.json' })],
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @param options estrogen options. `dirs` is ignored; the output directory comes
|
|
28
|
+
* from the Astro build hook.
|
|
29
|
+
*/
|
|
30
|
+
export declare function estrogen(options?: PluginOptions): AstroIntegration;
|
|
31
|
+
export default estrogen;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { fileURLToPath } from 'node:url';
|
|
2
|
+
import { obfuscate } from './obfuscate.js';
|
|
3
|
+
/**
|
|
4
|
+
* Astro integration that runs estrogen over the build output directory once Astro
|
|
5
|
+
* has written it.
|
|
6
|
+
*
|
|
7
|
+
* It hooks `astro:build:done`, which runs after the build is on disk, and reads
|
|
8
|
+
* the final output directory from that hook's `dir` URL through
|
|
9
|
+
* `fileURLToPath`. For a static (SSG) build this is the directory Astro serves;
|
|
10
|
+
* for an SSR build it is the client output directory, so the integration
|
|
11
|
+
* rewrites the client assets. An estrogen failure is thrown so the build fails.
|
|
12
|
+
*
|
|
13
|
+
* estrogen rewrites that directory in place and raises the cost of reading it.
|
|
14
|
+
* It is not secrecy: a decoder key ships in each file, and anyone with devtools
|
|
15
|
+
* can run the code.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { defineConfig } from 'astro/config';
|
|
20
|
+
* import estrogen from '@sysce/estrogen/astro';
|
|
21
|
+
*
|
|
22
|
+
* export default defineConfig({
|
|
23
|
+
* integrations: [estrogen({ canary: 'canary-list.json' })],
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @param options estrogen options. `dirs` is ignored; the output directory comes
|
|
28
|
+
* from the Astro build hook.
|
|
29
|
+
*/
|
|
30
|
+
export function estrogen(options = {}) {
|
|
31
|
+
return {
|
|
32
|
+
name: '@sysce/estrogen',
|
|
33
|
+
hooks: {
|
|
34
|
+
'astro:build:done': async ({ dir, logger }) => {
|
|
35
|
+
const outDir = fileURLToPath(dir);
|
|
36
|
+
logger.info(`running estrogen over ${outDir}`);
|
|
37
|
+
await obfuscate({ ...options, dirs: outDir });
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export default estrogen;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
---
|
|
2
|
+
import { linkElementProps, type CloakLinkProps } from '../props.js';
|
|
3
|
+
import HolyText from './HolyText.astro';
|
|
4
|
+
|
|
5
|
+
type Props = CloakLinkProps & { plain?: boolean; spanHost?: boolean; text?: string; labelCiphertext?: string; [attribute: string]: unknown };
|
|
6
|
+
const { plain: plainProp, spanHost = false, href, hrefCipher, target, text, labelCiphertext, ...rest } = Astro.props as Props;
|
|
7
|
+
const plain = plainProp ?? (Astro.locals as { isMainWebsite?: boolean }).isMainWebsite ?? false;
|
|
8
|
+
// An HTML href must not ship in the cloak branch. Match website2's
|
|
9
|
+
// request-local encryption when the link was given only a plaintext URL.
|
|
10
|
+
let encryptedHref = hrefCipher;
|
|
11
|
+
if (!plain && href !== undefined && !encryptedHref) {
|
|
12
|
+
const encryptText = (Astro.locals as { encryptText?: (text: string) => Promise<string> }).encryptText;
|
|
13
|
+
if (!encryptText) {
|
|
14
|
+
throw new Error('cloak: HolyLink needs Astro.locals.encryptText or an explicit hrefCipher');
|
|
15
|
+
}
|
|
16
|
+
encryptedHref = await encryptText(href);
|
|
17
|
+
}
|
|
18
|
+
const attributes = linkElementProps({ href, hrefCipher: encryptedHref, target });
|
|
19
|
+
---
|
|
20
|
+
{plain ? (
|
|
21
|
+
<a {...rest} href={href} target={target}>{typeof text === 'string' ? text : <slot />}</a>
|
|
22
|
+
) : spanHost ? (
|
|
23
|
+
<span {...rest} data-eclink={attributes['href-cipher']} data-target={target}>
|
|
24
|
+
{typeof text === 'string' ? <HolyText spanHost text={text} ciphertext={labelCiphertext} /> : <slot />}
|
|
25
|
+
</span>
|
|
26
|
+
) : (
|
|
27
|
+
<holy-link {...rest} href={attributes.href} href-cipher={attributes['href-cipher']} target={attributes.target}>
|
|
28
|
+
{typeof text === 'string' ? <HolyText text={text} ciphertext={labelCiphertext} /> : <slot />}
|
|
29
|
+
</holy-link>
|
|
30
|
+
)}
|
|
31
|
+
<script>
|
|
32
|
+
import '../elements.js';
|
|
33
|
+
import { navigate } from 'astro:transitions/client';
|
|
34
|
+
import { setupSpanCloak } from '../span.js';
|
|
35
|
+
setupSpanCloak(navigate);
|
|
36
|
+
</script>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AstroComponentFactory } from 'astro/runtime/server/index.js';
|
|
2
|
+
import type { CloakLinkProps } from '../props.js';
|
|
3
|
+
|
|
4
|
+
declare const HolyLink: AstroComponentFactory &
|
|
5
|
+
((
|
|
6
|
+
props: CloakLinkProps & {
|
|
7
|
+
plain?: boolean;
|
|
8
|
+
spanHost?: boolean;
|
|
9
|
+
text?: string;
|
|
10
|
+
labelCiphertext?: string;
|
|
11
|
+
[attribute: string]: unknown;
|
|
12
|
+
},
|
|
13
|
+
) => unknown);
|
|
14
|
+
export default HolyLink;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
---
|
|
2
|
+
import { textElementProps, type CloakTextProps } from '../props.js';
|
|
3
|
+
|
|
4
|
+
type Props = CloakTextProps & { plain?: boolean; spanHost?: boolean; [attribute: string]: unknown };
|
|
5
|
+
const { plain: plainProp, spanHost = false, text, ciphertext, ellipsis, revealForBots, ...rest } = Astro.props as Props;
|
|
6
|
+
const plain = plainProp ?? (Astro.locals as { isMainWebsite?: boolean }).isMainWebsite ?? false;
|
|
7
|
+
// website2's HolyText encrypts its text on each SSR request. Keep that
|
|
8
|
+
// contract in the shared component, rather than emitting an unencrypted
|
|
9
|
+
// fallback when a caller supplies text but not a precomputed ciphertext.
|
|
10
|
+
let encrypted = ciphertext;
|
|
11
|
+
if (!plain && text !== undefined && !encrypted) {
|
|
12
|
+
const encryptText = (Astro.locals as { encryptText?: (text: string) => Promise<string> }).encryptText;
|
|
13
|
+
if (!encryptText) {
|
|
14
|
+
throw new Error('cloak: HolyText needs Astro.locals.encryptText or an explicit ciphertext');
|
|
15
|
+
}
|
|
16
|
+
encrypted = await encryptText(text);
|
|
17
|
+
}
|
|
18
|
+
const { attributes, placeholder } = textElementProps({ text, ciphertext: encrypted, ellipsis, revealForBots });
|
|
19
|
+
---
|
|
20
|
+
{plain ? (
|
|
21
|
+
<span {...rest} set:text={text ?? ''} />
|
|
22
|
+
) : spanHost ? (
|
|
23
|
+
<span {...rest} data-ectext={attributes.ciphertext}>{placeholder}</span>
|
|
24
|
+
) : (
|
|
25
|
+
<holy-text {...rest} ciphertext={attributes.ciphertext} ellipsis={attributes.ellipsis} reveal-for-bots={attributes['reveal-for-bots']}>{placeholder}</holy-text>
|
|
26
|
+
)}
|
|
27
|
+
<script>
|
|
28
|
+
import '../elements.js';
|
|
29
|
+
import { setupSpanCloak } from '../span.js';
|
|
30
|
+
setupSpanCloak();
|
|
31
|
+
</script>
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { AstroComponentFactory } from 'astro/runtime/server/index.js';
|
|
2
|
+
import type { CloakTextProps } from '../props.js';
|
|
3
|
+
|
|
4
|
+
declare const HolyText: AstroComponentFactory &
|
|
5
|
+
((
|
|
6
|
+
props: CloakTextProps & {
|
|
7
|
+
plain?: boolean;
|
|
8
|
+
spanHost?: boolean;
|
|
9
|
+
[attribute: string]: unknown;
|
|
10
|
+
},
|
|
11
|
+
) => unknown);
|
|
12
|
+
export default HolyText;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Base64 helpers that work in a browser, on Node and on edge runtimes without
|
|
2
|
+
// Buffer. The cloak key and ciphertext move as base64 strings, so both the
|
|
3
|
+
// server (encrypt) and client (decrypt) sides share this one implementation.
|
|
4
|
+
export function toBase64(bytes) {
|
|
5
|
+
let binary = '';
|
|
6
|
+
for (let i = 0; i < bytes.length; i++)
|
|
7
|
+
binary += String.fromCharCode(bytes[i]);
|
|
8
|
+
return btoa(binary);
|
|
9
|
+
}
|
|
10
|
+
export function fromBase64(str) {
|
|
11
|
+
const binary = atob(str.trim());
|
|
12
|
+
const bytes = new Uint8Array(binary.length);
|
|
13
|
+
for (let i = 0; i < binary.length; i++)
|
|
14
|
+
bytes[i] = binary.charCodeAt(i);
|
|
15
|
+
return bytes;
|
|
16
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface EncryptionSession {
|
|
2
|
+
/** 32-byte AES key. */
|
|
3
|
+
key: Uint8Array;
|
|
4
|
+
/** 16-byte IV. */
|
|
5
|
+
iv: Uint8Array;
|
|
6
|
+
/** base64(iv ++ key), the value that ships in the #eckey element. */
|
|
7
|
+
combinedKeyBase64: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Make a fresh random key+IV. Call this once per request (or per build) so the
|
|
11
|
+
* same source text encrypts to different ciphertext every time. Publish
|
|
12
|
+
* `combinedKeyBase64` into the page's #eckey element.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createEncryptionSession(randomFill?: (buffer: Uint8Array) => Uint8Array): EncryptionSession;
|
|
15
|
+
/** Encrypt one string under a session. Returns base64 AES-256-CBC ciphertext. */
|
|
16
|
+
export declare function encryptText(text: string, session: EncryptionSession): string;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Server/build side of the cloak: make a per-request key and encrypt text with
|
|
2
|
+
// it. AES-256-CBC via @noble/ciphers and web getRandomValues, so this runs the
|
|
3
|
+
// same on Node, on an edge runtime (Cloudflare Pages, Deno) and in a build
|
|
4
|
+
// step. The wire format matches what the client decryptor expects: a 48-byte
|
|
5
|
+
// combined key, base64(iv(16) ++ key(32)), and base64 AES-CBC ciphertext.
|
|
6
|
+
import { cbc } from '@noble/ciphers/aes.js';
|
|
7
|
+
import { toBase64 } from './base64.js';
|
|
8
|
+
function randomBytes(length, randomFill) {
|
|
9
|
+
const out = new Uint8Array(length);
|
|
10
|
+
if (randomFill)
|
|
11
|
+
return randomFill(out);
|
|
12
|
+
if (typeof globalThis.crypto?.getRandomValues !== 'function') {
|
|
13
|
+
throw new Error('cloak: globalThis.crypto.getRandomValues is unavailable; need Node >=19, an edge runtime or a browser');
|
|
14
|
+
}
|
|
15
|
+
globalThis.crypto.getRandomValues(out);
|
|
16
|
+
return out;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Make a fresh random key+IV. Call this once per request (or per build) so the
|
|
20
|
+
* same source text encrypts to different ciphertext every time. Publish
|
|
21
|
+
* `combinedKeyBase64` into the page's #eckey element.
|
|
22
|
+
*/
|
|
23
|
+
export function createEncryptionSession(randomFill) {
|
|
24
|
+
const key = randomBytes(32, randomFill);
|
|
25
|
+
const iv = randomBytes(16, randomFill);
|
|
26
|
+
const combined = new Uint8Array(48);
|
|
27
|
+
combined.set(iv, 0);
|
|
28
|
+
combined.set(key, 16);
|
|
29
|
+
return { key, iv, combinedKeyBase64: toBase64(combined) };
|
|
30
|
+
}
|
|
31
|
+
const encoder = new TextEncoder();
|
|
32
|
+
/** Encrypt one string under a session. Returns base64 AES-256-CBC ciphertext. */
|
|
33
|
+
export function encryptText(text, session) {
|
|
34
|
+
const ciphertext = cbc(session.key, session.iv).encrypt(encoder.encode(text));
|
|
35
|
+
return toBase64(ciphertext);
|
|
36
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Read the key from a host application's existing runtime, without copying globals. */
|
|
2
|
+
export declare function setCloakKeyProvider(provider: (() => string | null | undefined) | undefined): void;
|
|
3
|
+
/**
|
|
4
|
+
* Tell the decryptor where to look for the #eckey element first, e.g. a decoy
|
|
5
|
+
* overlay's shadow root. The document is always checked as a fallback.
|
|
6
|
+
*/
|
|
7
|
+
export declare function setCloakRoot(resolver: (() => Document | ShadowRoot | null | undefined) | undefined): void;
|
|
8
|
+
/** Load the combined key from the DOM (or the window global). Safe to re-call. */
|
|
9
|
+
export declare function loadDecryptionKey(): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Decrypt one base64 ciphertext string to plaintext. Returns "" on a missing
|
|
12
|
+
* key or a decrypt error, so a caller can keep its dash placeholder rather than
|
|
13
|
+
* blanking the text.
|
|
14
|
+
*/
|
|
15
|
+
export declare function holyDecrypt(ciphertextB64: string): string;
|
|
16
|
+
/** Parse a JSON payload encrypted with `encryptJson` on the server. */
|
|
17
|
+
export declare function holyDecryptJson<T = unknown>(ciphertextB64: string): T | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* A tripwire, not a security control: flags a likely headless/automated browser
|
|
20
|
+
* so the reveal step can stay on the dash placeholder. Must never block a real
|
|
21
|
+
* user, so it only reads navigator.webdriver.
|
|
22
|
+
*/
|
|
23
|
+
export declare function isLikelyBot(): boolean;
|