unpeek 2.0.0 → 4.0.0
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/README.md +57 -21
- package/README.tr.md +65 -29
- package/dist/adapters/esbuild.cjs +13 -0
- package/dist/adapters/esbuild.d.cts +13 -0
- package/dist/adapters/esbuild.d.ts +11 -0
- package/dist/adapters/esbuild.js +13 -0
- package/dist/adapters/next/adapter.cjs +43 -0
- package/dist/{next → adapters/next}/adapter.d.cts +4 -4
- package/dist/{next → adapters/next}/adapter.d.ts +4 -4
- package/dist/adapters/next/adapter.js +43 -0
- package/dist/adapters/next/index.cjs +16 -0
- package/dist/adapters/next/index.d.cts +53 -0
- package/dist/adapters/next/index.d.ts +53 -0
- package/dist/adapters/next/index.js +16 -0
- package/dist/adapters/rollup.cjs +13 -0
- package/dist/adapters/rollup.d.cts +13 -0
- package/dist/adapters/rollup.d.ts +11 -0
- package/dist/adapters/rollup.js +13 -0
- package/dist/adapters/rspack.cjs +13 -0
- package/dist/adapters/rspack.d.cts +13 -0
- package/dist/adapters/rspack.d.ts +11 -0
- package/dist/adapters/rspack.js +13 -0
- package/dist/adapters/vite.cjs +13 -0
- package/dist/adapters/vite.d.cts +13 -0
- package/dist/adapters/vite.d.ts +11 -0
- package/dist/adapters/vite.js +13 -0
- package/dist/adapters/webpack.cjs +13 -0
- package/dist/adapters/webpack.d.cts +13 -0
- package/dist/adapters/webpack.d.ts +11 -0
- package/dist/adapters/webpack.js +13 -0
- package/dist/browser.cjs +8 -5
- package/dist/browser.d.cts +44 -3
- package/dist/browser.d.ts +44 -3
- package/dist/browser.js +10 -7
- package/dist/{chunk-5QD6FZPB.js → chunk-2JU3V7HW.js} +75 -43
- package/dist/chunk-3LWXEJU6.cjs +1054 -0
- package/dist/chunk-4FNZNIBW.js +7391 -0
- package/dist/chunk-7HXLHLLQ.cjs +503 -0
- package/dist/chunk-ADKRENAP.js +1054 -0
- package/dist/chunk-EN6QVR6P.js +89 -0
- package/dist/chunk-HHC7BUCK.js +503 -0
- package/dist/chunk-KLYFNMKS.cjs +7391 -0
- package/dist/chunk-L6BOM5VS.cjs +40 -0
- package/dist/{chunk-OOXSZTRV.js → chunk-LA6J3BT3.js} +26 -1
- package/dist/chunk-URPPSTHZ.cjs +89 -0
- package/dist/chunk-VG4Y26ZN.cjs +429 -0
- package/dist/{chunk-GZB7PFLY.js → chunk-W77DAC3H.js} +46 -64
- package/dist/chunk-X6EETCO6.cjs +352 -0
- package/dist/chunk-YCRMTXOE.js +429 -0
- package/dist/{chunk-GAVI2C44.cjs → chunk-ZG7BIAUI.cjs} +80 -48
- package/dist/cli/index.js +9861 -32
- package/dist/{types-BBSRUEab.d.cts → config-Y6JwVGwX.d.cts} +58 -86
- package/dist/{types-BBSRUEab.d.ts → config-Y6JwVGwX.d.ts} +58 -86
- package/dist/config.cjs +4 -16
- package/dist/config.d.cts +1 -12
- package/dist/config.d.ts +1 -12
- package/dist/config.js +5 -17
- package/dist/index-0O1WSfVP.d.ts +52 -0
- package/dist/index-COVHErnx.d.cts +52 -0
- package/dist/index.cjs +437 -9
- package/dist/index.d.cts +9 -17
- package/dist/index.d.ts +9 -17
- package/dist/index.js +438 -10
- package/dist/plugins/unplugin.cjs +21 -0
- package/dist/plugins/unplugin.d.cts +17 -0
- package/dist/plugins/unplugin.d.ts +15 -0
- package/dist/{bundler → plugins}/unplugin.js +6 -5
- package/dist/types-CMtZnV8C.d.ts +112 -0
- package/dist/types-D93PuOOl.d.cts +112 -0
- package/package.json +48 -52
- package/dist/ast/index.cjs +0 -17
- package/dist/ast/index.d.cts +0 -18
- package/dist/ast/index.d.ts +0 -18
- package/dist/ast/index.js +0 -17
- package/dist/bundler/unplugin.cjs +0 -20
- package/dist/bundler/unplugin.d.cts +0 -16
- package/dist/bundler/unplugin.d.ts +0 -14
- package/dist/chunk-24K5NDXH.cjs +0 -370
- package/dist/chunk-27HR647Q.js +0 -190
- package/dist/chunk-4TDLLEEU.cjs +0 -15
- package/dist/chunk-5LCMI2TL.js +0 -575
- package/dist/chunk-5M6Z7ZFT.cjs +0 -83
- package/dist/chunk-6N6S6WTQ.cjs +0 -423
- package/dist/chunk-7YVEHJVM.cjs +0 -575
- package/dist/chunk-J5CGNZDM.cjs +0 -190
- package/dist/chunk-KQWNFKT2.js +0 -423
- package/dist/chunk-LXP73HJC.cjs +0 -4532
- package/dist/chunk-R6EBIR3T.cjs +0 -14
- package/dist/chunk-S4S2WXGC.js +0 -14
- package/dist/chunk-UMPUYLLZ.js +0 -4532
- package/dist/chunk-X5MKMFMX.js +0 -83
- package/dist/cli/index.cjs +0 -324
- package/dist/cli/index.d.cts +0 -22
- package/dist/cli/index.d.ts +0 -22
- package/dist/esbuild.cjs +0 -15
- package/dist/esbuild.d.cts +0 -11
- package/dist/esbuild.d.ts +0 -11
- package/dist/esbuild.js +0 -13
- package/dist/next/adapter.cjs +0 -31
- package/dist/next/adapter.js +0 -31
- package/dist/next/index.cjs +0 -18
- package/dist/next/index.d.cts +0 -42
- package/dist/next/index.d.ts +0 -42
- package/dist/next/index.js +0 -18
- package/dist/rollup.cjs +0 -15
- package/dist/rollup.d.cts +0 -11
- package/dist/rollup.d.ts +0 -11
- package/dist/rollup.js +0 -13
- package/dist/runtime/index.cjs +0 -17
- package/dist/runtime/index.d.cts +0 -36
- package/dist/runtime/index.d.ts +0 -36
- package/dist/runtime/index.js +0 -17
- package/dist/vite.cjs +0 -15
- package/dist/vite.d.cts +0 -11
- package/dist/vite.d.ts +0 -11
- package/dist/vite.js +0 -13
- package/dist/webpack.cjs +0 -15
- package/dist/webpack.d.cts +0 -11
- package/dist/webpack.d.ts +0 -11
- package/dist/webpack.js +0 -13
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// src/presets.ts
|
|
2
|
+
var PRESETS = {
|
|
3
|
+
minimal: {
|
|
4
|
+
profile: "minimal",
|
|
5
|
+
tier: "Tier 1",
|
|
6
|
+
target: "browser",
|
|
7
|
+
coldFlow: false,
|
|
8
|
+
vault: {
|
|
9
|
+
minStringLength: Number.MAX_SAFE_INTEGER,
|
|
10
|
+
polymorphicDecoys: false
|
|
11
|
+
},
|
|
12
|
+
mangler: {
|
|
13
|
+
minLength: 1
|
|
14
|
+
},
|
|
15
|
+
shield: {
|
|
16
|
+
detectHeadless: false,
|
|
17
|
+
deterrentMode: "passive"
|
|
18
|
+
},
|
|
19
|
+
sourcemaps: {
|
|
20
|
+
keepPrivate: true,
|
|
21
|
+
vaultDir: ".unpeek-maps",
|
|
22
|
+
stripComments: true
|
|
23
|
+
},
|
|
24
|
+
scrubPaths: false
|
|
25
|
+
},
|
|
26
|
+
recommended: {
|
|
27
|
+
profile: "recommended",
|
|
28
|
+
tier: "Tier 2",
|
|
29
|
+
target: "browser",
|
|
30
|
+
coldFlow: true,
|
|
31
|
+
vault: {
|
|
32
|
+
minStringLength: 4,
|
|
33
|
+
cssFramework: "tailwind",
|
|
34
|
+
semanticAnchors: {
|
|
35
|
+
excludeUiClasses: true
|
|
36
|
+
},
|
|
37
|
+
polymorphicDecoys: true
|
|
38
|
+
},
|
|
39
|
+
mangler: {
|
|
40
|
+
minLength: 1
|
|
41
|
+
},
|
|
42
|
+
shield: {
|
|
43
|
+
detectHeadless: false,
|
|
44
|
+
deterrentMode: "passive"
|
|
45
|
+
},
|
|
46
|
+
sourcemaps: {
|
|
47
|
+
keepPrivate: true,
|
|
48
|
+
vaultDir: ".unpeek-maps",
|
|
49
|
+
stripComments: true
|
|
50
|
+
},
|
|
51
|
+
scrubPaths: false
|
|
52
|
+
},
|
|
53
|
+
strict: {
|
|
54
|
+
profile: "strict",
|
|
55
|
+
tier: "Tier 3",
|
|
56
|
+
target: "browser",
|
|
57
|
+
coldFlow: true,
|
|
58
|
+
critical: {
|
|
59
|
+
annotation: "@unpeek-critical"
|
|
60
|
+
},
|
|
61
|
+
vault: {
|
|
62
|
+
minStringLength: 4,
|
|
63
|
+
cssFramework: "tailwind",
|
|
64
|
+
semanticAnchors: {
|
|
65
|
+
excludeUiClasses: true
|
|
66
|
+
},
|
|
67
|
+
polymorphicDecoys: true,
|
|
68
|
+
fragmented: true,
|
|
69
|
+
fragmentCount: 4
|
|
70
|
+
},
|
|
71
|
+
mangler: {
|
|
72
|
+
minLength: 1
|
|
73
|
+
},
|
|
74
|
+
shield: {
|
|
75
|
+
detectHeadless: true,
|
|
76
|
+
deterrentMode: "gradual"
|
|
77
|
+
},
|
|
78
|
+
sourcemaps: {
|
|
79
|
+
keepPrivate: true,
|
|
80
|
+
vaultDir: ".unpeek-maps",
|
|
81
|
+
stripComments: true
|
|
82
|
+
},
|
|
83
|
+
scrubPaths: true
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export {
|
|
88
|
+
PRESETS
|
|
89
|
+
};
|
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
import {
|
|
2
|
+
loadUnpeekConfig
|
|
3
|
+
} from "./chunk-W77DAC3H.js";
|
|
4
|
+
import {
|
|
5
|
+
chainSourceMaps,
|
|
6
|
+
computeDeterministicDebugId,
|
|
7
|
+
detectTargetFramework,
|
|
8
|
+
extractDebugId,
|
|
9
|
+
flattenSourceMapSections,
|
|
10
|
+
isWorkerOrServiceWorkerFile,
|
|
11
|
+
relocateSourceMaps,
|
|
12
|
+
replaceOrAppendDebugId,
|
|
13
|
+
safeDecodeURIComponent,
|
|
14
|
+
scanClientBundles,
|
|
15
|
+
shiftSourceMapLines,
|
|
16
|
+
stripDebugIdComments,
|
|
17
|
+
stripSourceMapComments
|
|
18
|
+
} from "./chunk-ADKRENAP.js";
|
|
19
|
+
import {
|
|
20
|
+
computeFnv1aHash,
|
|
21
|
+
deriveWindowKeySymbol,
|
|
22
|
+
generateRuntimeShield,
|
|
23
|
+
transformJavaScript
|
|
24
|
+
} from "./chunk-4FNZNIBW.js";
|
|
25
|
+
import {
|
|
26
|
+
UnpeekError
|
|
27
|
+
} from "./chunk-LA6J3BT3.js";
|
|
28
|
+
|
|
29
|
+
// src/internal/next.ts
|
|
30
|
+
function isNext16OrAbove(versionStr) {
|
|
31
|
+
if (!versionStr) return false;
|
|
32
|
+
const clean = versionStr.replace(/^[^\d]*/, "");
|
|
33
|
+
const match = clean.match(/^(\d+)\.(\d+)/);
|
|
34
|
+
if (!match) return false;
|
|
35
|
+
const major = parseInt(match[1], 10);
|
|
36
|
+
const minor = parseInt(match[2], 10);
|
|
37
|
+
if (major > 16) return true;
|
|
38
|
+
if (major === 16 && minor >= 2) return true;
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
function isSerializedRegExp(val) {
|
|
42
|
+
if (typeof val !== "object" || val === null) return false;
|
|
43
|
+
const record = val;
|
|
44
|
+
return record.__unpeekRegExp === true && typeof record.source === "string" && typeof record.flags === "string";
|
|
45
|
+
}
|
|
46
|
+
function serializeNextOptions(options) {
|
|
47
|
+
return JSON.stringify(options, (_key, value) => {
|
|
48
|
+
if (value instanceof RegExp) {
|
|
49
|
+
return {
|
|
50
|
+
__unpeekRegExp: true,
|
|
51
|
+
source: value.source,
|
|
52
|
+
flags: value.flags
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return value;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
function parseNextOptions(raw) {
|
|
59
|
+
return JSON.parse(raw, (_key, value) => {
|
|
60
|
+
if (isSerializedRegExp(value)) {
|
|
61
|
+
try {
|
|
62
|
+
return new RegExp(value.source, value.flags);
|
|
63
|
+
} catch {
|
|
64
|
+
return void 0;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return value;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
function readUnpeekNextOptionsFromEnv() {
|
|
71
|
+
const raw = process.env.UNPEEK_NEXT_OPTIONS;
|
|
72
|
+
if (!raw) {
|
|
73
|
+
return void 0;
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
const parsed = parseNextOptions(raw);
|
|
77
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
78
|
+
return parsed;
|
|
79
|
+
}
|
|
80
|
+
} catch {
|
|
81
|
+
}
|
|
82
|
+
return void 0;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// src/core/protect.ts
|
|
86
|
+
import fs from "fs";
|
|
87
|
+
import path from "path";
|
|
88
|
+
import zlib from "zlib";
|
|
89
|
+
var ENTRY_PATTERN = /^(?:webpack|main|index|runtime|turbopack|pages\/_app|pages\/index)(?:[-._/]|$)/i;
|
|
90
|
+
function normalizeManifestRel(fileRef) {
|
|
91
|
+
const clean = fileRef.replace(/\\/g, "/").split("?")[0].split("#")[0].replace(/^\/+/, "");
|
|
92
|
+
const stripped = clean.replace(
|
|
93
|
+
/^(?:_next\/static\/chunks\/|_next\/static\/|_next\/|static\/chunks\/|static\/|assets\/)/,
|
|
94
|
+
""
|
|
95
|
+
);
|
|
96
|
+
return stripped && stripped !== clean ? [clean, stripped] : [clean];
|
|
97
|
+
}
|
|
98
|
+
function addManifestEntryCandidates(outDir, manifestRootDir, fileRef, target) {
|
|
99
|
+
const clean = fileRef.replace(/\\/g, "/").split("?")[0].split("#")[0].replace(/^\/+/, "");
|
|
100
|
+
if (!clean) return;
|
|
101
|
+
const absFromRoot = path.resolve(manifestRootDir, clean);
|
|
102
|
+
const relFromOut = path.relative(outDir, absFromRoot).replace(/\\/g, "/");
|
|
103
|
+
if (relFromOut && !relFromOut.startsWith("..") && !path.isAbsolute(relFromOut)) {
|
|
104
|
+
target.add(relFromOut);
|
|
105
|
+
}
|
|
106
|
+
for (const variant of normalizeManifestRel(clean)) {
|
|
107
|
+
target.add(variant);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function collectManifestEntryFiles(outDir, cwd, config) {
|
|
111
|
+
const entries = /* @__PURE__ */ new Set();
|
|
112
|
+
const nextDist = config?.next?.distDir ?? ".next";
|
|
113
|
+
const viteOut = config?.vite?.outDir ?? "dist";
|
|
114
|
+
const nextManifestCandidates = Array.from(
|
|
115
|
+
/* @__PURE__ */ new Set([
|
|
116
|
+
path.join(outDir, "build-manifest.json"),
|
|
117
|
+
path.join(outDir, "..", "build-manifest.json"),
|
|
118
|
+
path.join(outDir, "..", "..", "build-manifest.json"),
|
|
119
|
+
path.join(cwd, nextDist, "build-manifest.json")
|
|
120
|
+
])
|
|
121
|
+
);
|
|
122
|
+
for (const manifestPath of nextManifestCandidates) {
|
|
123
|
+
if (!fs.existsSync(manifestPath)) continue;
|
|
124
|
+
try {
|
|
125
|
+
const parsed = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
|
|
126
|
+
const manifestDir = path.dirname(manifestPath);
|
|
127
|
+
const hasRootMain = Array.isArray(parsed.rootMainFiles) && parsed.rootMainFiles.length > 0;
|
|
128
|
+
if (Array.isArray(parsed.rootMainFiles)) {
|
|
129
|
+
for (const item of parsed.rootMainFiles) {
|
|
130
|
+
if (typeof item === "string") {
|
|
131
|
+
addManifestEntryCandidates(outDir, manifestDir, item, entries);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (parsed.pages && typeof parsed.pages === "object") {
|
|
136
|
+
const priorityKeys = ["/_app", "/", "/index"].filter(
|
|
137
|
+
(k) => Array.isArray(parsed.pages?.[k])
|
|
138
|
+
);
|
|
139
|
+
const pageLists = priorityKeys.length > 0 ? priorityKeys.map((k) => parsed.pages[k]) : !hasRootMain ? Object.values(parsed.pages) : [];
|
|
140
|
+
for (const list of pageLists) {
|
|
141
|
+
if (!Array.isArray(list)) continue;
|
|
142
|
+
for (const item of list) {
|
|
143
|
+
if (typeof item === "string") {
|
|
144
|
+
addManifestEntryCandidates(outDir, manifestDir, item, entries);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
break;
|
|
150
|
+
} catch {
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const viteManifestCandidates = Array.from(
|
|
154
|
+
/* @__PURE__ */ new Set([
|
|
155
|
+
path.join(outDir, ".vite", "manifest.json"),
|
|
156
|
+
path.join(outDir, "..", ".vite", "manifest.json"),
|
|
157
|
+
path.join(cwd, viteOut, ".vite", "manifest.json"),
|
|
158
|
+
path.join(outDir, "manifest.json"),
|
|
159
|
+
path.join(outDir, "..", "manifest.json")
|
|
160
|
+
])
|
|
161
|
+
);
|
|
162
|
+
for (const manifestPath of viteManifestCandidates) {
|
|
163
|
+
if (!fs.existsSync(manifestPath)) continue;
|
|
164
|
+
try {
|
|
165
|
+
const parsed = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
|
|
166
|
+
if (!parsed || typeof parsed !== "object") continue;
|
|
167
|
+
const manifestDir = path.basename(path.dirname(manifestPath)) === ".vite" ? path.dirname(path.dirname(manifestPath)) : path.dirname(manifestPath);
|
|
168
|
+
let matchedViteShape = false;
|
|
169
|
+
for (const val of Object.values(parsed)) {
|
|
170
|
+
if (val && typeof val === "object" && "file" in val && typeof val.file === "string") {
|
|
171
|
+
matchedViteShape = true;
|
|
172
|
+
if (val.isEntry) {
|
|
173
|
+
addManifestEntryCandidates(
|
|
174
|
+
outDir,
|
|
175
|
+
manifestDir,
|
|
176
|
+
val.file,
|
|
177
|
+
entries
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (matchedViteShape) break;
|
|
183
|
+
} catch {
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return entries;
|
|
187
|
+
}
|
|
188
|
+
function matchesManifestEntry(relPath, manifestEntries) {
|
|
189
|
+
if (manifestEntries.size === 0) return false;
|
|
190
|
+
for (const candidate of normalizeManifestRel(relPath)) {
|
|
191
|
+
if (manifestEntries.has(candidate)) {
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
function isEntryChunk(relPath, index, allRelPaths, manifestEntries, isWorkerTarget) {
|
|
198
|
+
const norm = relPath.replace(/\\/g, "/");
|
|
199
|
+
const base = path.basename(norm);
|
|
200
|
+
if (isWorkerTarget && isWorkerOrServiceWorkerFile(norm)) {
|
|
201
|
+
return true;
|
|
202
|
+
}
|
|
203
|
+
if (matchesManifestEntry(norm, manifestEntries)) {
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
if (ENTRY_PATTERN.test(norm) || ENTRY_PATTERN.test(base)) {
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
const hasAnyKnownEntry = allRelPaths.some((p) => {
|
|
210
|
+
const n = p.replace(/\\/g, "/");
|
|
211
|
+
return matchesManifestEntry(n, manifestEntries) || ENTRY_PATTERN.test(n) || ENTRY_PATTERN.test(path.basename(n));
|
|
212
|
+
});
|
|
213
|
+
if (!hasAnyKnownEntry && index === 0) {
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
function readOriginalSourceMapForFile(filePath, relPath, vaultDir, deferVaultWrites) {
|
|
219
|
+
const candidates = [];
|
|
220
|
+
if (deferVaultWrites) {
|
|
221
|
+
candidates.push(`${filePath}.map`);
|
|
222
|
+
}
|
|
223
|
+
candidates.push(path.join(vaultDir, `${relPath}.map`));
|
|
224
|
+
const decodedRel = safeDecodeURIComponent(relPath);
|
|
225
|
+
if (decodedRel !== relPath) {
|
|
226
|
+
candidates.push(path.join(vaultDir, `${decodedRel}.map`));
|
|
227
|
+
}
|
|
228
|
+
for (const cand of candidates) {
|
|
229
|
+
if (!fs.existsSync(cand)) continue;
|
|
230
|
+
try {
|
|
231
|
+
const parsed = JSON.parse(fs.readFileSync(cand, "utf8"));
|
|
232
|
+
if (parsed && typeof parsed === "object") {
|
|
233
|
+
return parsed;
|
|
234
|
+
}
|
|
235
|
+
} catch {
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return void 0;
|
|
239
|
+
}
|
|
240
|
+
async function protectProject(options = {}) {
|
|
241
|
+
const startTime = performance.now();
|
|
242
|
+
const cwd = path.resolve(options.cwd ?? process.cwd());
|
|
243
|
+
const effectiveConfigInput = options.profile ? { ...options.config ?? {}, profile: options.profile } : options.config;
|
|
244
|
+
const config = await loadUnpeekConfig(cwd, effectiveConfigInput);
|
|
245
|
+
const effectiveProfile = options.profile ?? config.profile ?? "recommended";
|
|
246
|
+
const { framework, outDir } = detectTargetFramework(cwd, options.outDir, config);
|
|
247
|
+
const seed = typeof options.seed === "number" ? options.seed : Math.floor(Math.random() * 2147483647) || 1597463007;
|
|
248
|
+
const dryRun = Boolean(options.dryRun);
|
|
249
|
+
const keepPrivateMaps = options.keepPrivateMaps ?? config.sourcemaps?.keepPrivate ?? true;
|
|
250
|
+
const privateMapsDir = options.privateMapsDir ?? config.sourcemaps?.vaultDir ?? ".unpeek-maps";
|
|
251
|
+
const deterrentMode = options.deterrentMode ?? config.shield?.deterrentMode ?? "passive";
|
|
252
|
+
const detectHeadless = options.detectHeadless ?? config.shield?.detectHeadless ?? false;
|
|
253
|
+
const injectRuntimeShield = options.injectRuntimeShield ?? (effectiveProfile === "minimal" ? false : true);
|
|
254
|
+
const minStringLength = options.minStringLength ?? config.vault?.minStringLength ?? 4;
|
|
255
|
+
const aggressive = options.aggressive ?? (effectiveProfile === "strict" ? true : false);
|
|
256
|
+
const scrubPaths = options.scrubPaths ?? config.scrubPaths ?? (effectiveProfile === "strict" ? true : false);
|
|
257
|
+
const masterSym = deriveWindowKeySymbol(seed);
|
|
258
|
+
const masterShield = injectRuntimeShield ? generateRuntimeShield({
|
|
259
|
+
seed,
|
|
260
|
+
deterrentMode,
|
|
261
|
+
detectHeadless,
|
|
262
|
+
windowKeySymbol: masterSym
|
|
263
|
+
}) : void 0;
|
|
264
|
+
const deferVaultWrites = dryRun || typeof options.maxGzipDelta === "number";
|
|
265
|
+
const vaultResult = relocateSourceMaps(outDir, cwd, keepPrivateMaps, privateMapsDir, false, config, {
|
|
266
|
+
dryRun: deferVaultWrites
|
|
267
|
+
});
|
|
268
|
+
const clientFiles = scanClientBundles(outDir, config);
|
|
269
|
+
const allRelPaths = clientFiles.map((f) => path.relative(outDir, f).replace(/\\/g, "/"));
|
|
270
|
+
const manifestEntries = collectManifestEntryFiles(outDir, cwd, config);
|
|
271
|
+
const isWorkerTarget = config.target === "worker";
|
|
272
|
+
let originalBytes = 0;
|
|
273
|
+
let protectedBytes = 0;
|
|
274
|
+
let totalHotFunctionsSkipped = 0;
|
|
275
|
+
let totalColdFunctionsProtected = 0;
|
|
276
|
+
let totalEncryptedStrings = 0;
|
|
277
|
+
let totalMangledSymbols = 0;
|
|
278
|
+
let processedFiles = 0;
|
|
279
|
+
let debugIdsPreserved = 0;
|
|
280
|
+
const diagnostics = [];
|
|
281
|
+
if (vaultResult.dockerIgnoreMissing) {
|
|
282
|
+
diagnostics.push({
|
|
283
|
+
code: "DOCKERIGNORE_MISSING",
|
|
284
|
+
severity: "warning",
|
|
285
|
+
message: ".dockerignore is missing .unpeek-maps/. Private maps may leak into Docker image during 'COPY . .'."
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
const allOrigBufs = [];
|
|
289
|
+
const allProtBufs = [];
|
|
290
|
+
const stagedFiles = [];
|
|
291
|
+
const pendingVaultMaps = [];
|
|
292
|
+
let originalGzipBytes = 0;
|
|
293
|
+
let protectedGzipBytes = 0;
|
|
294
|
+
let gzipDeltaPercent = 0;
|
|
295
|
+
let budgetExceeded = false;
|
|
296
|
+
try {
|
|
297
|
+
for (let i = 0; i < clientFiles.length; i++) {
|
|
298
|
+
const filePath = clientFiles[i];
|
|
299
|
+
let codeToTransform = fs.readFileSync(filePath, "utf8");
|
|
300
|
+
const fileDebugId = extractDebugId(codeToTransform);
|
|
301
|
+
if (codeToTransform.includes("sourceMappingURL=")) {
|
|
302
|
+
const strippedRes = stripSourceMapComments(codeToTransform);
|
|
303
|
+
codeToTransform = strippedRes.code;
|
|
304
|
+
if (strippedRes.stripped) {
|
|
305
|
+
vaultResult.strippedFilesCount++;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
const origBuf = Buffer.from(codeToTransform, "utf8");
|
|
309
|
+
originalBytes += origBuf.byteLength;
|
|
310
|
+
allOrigBufs.push(origBuf);
|
|
311
|
+
const relPath = allRelPaths[i];
|
|
312
|
+
const isEntry = isEntryChunk(relPath, i, allRelPaths, manifestEntries, isWorkerTarget);
|
|
313
|
+
const shouldInjectShield = injectRuntimeShield && isEntry;
|
|
314
|
+
const fileSeed = computeFnv1aHash(`${relPath}:${i}`, seed);
|
|
315
|
+
const transformOptions = {
|
|
316
|
+
seed: fileSeed,
|
|
317
|
+
minStringLength,
|
|
318
|
+
filename: path.basename(filePath),
|
|
319
|
+
injectRuntimeShield: shouldInjectShield,
|
|
320
|
+
skipShieldPreamble: true,
|
|
321
|
+
shieldCoupled: injectRuntimeShield,
|
|
322
|
+
windowKeySymbol: masterSym,
|
|
323
|
+
expectedDelta: masterShield?.expectedDelta,
|
|
324
|
+
deterrentMode,
|
|
325
|
+
detectHeadless,
|
|
326
|
+
aggressive,
|
|
327
|
+
scrubPaths,
|
|
328
|
+
config
|
|
329
|
+
};
|
|
330
|
+
transformOptions.generateMap = keepPrivateMaps;
|
|
331
|
+
transformOptions.rootDir = cwd;
|
|
332
|
+
const result = transformJavaScript(codeToTransform, transformOptions);
|
|
333
|
+
if (result.skippedReason === "parse-error") {
|
|
334
|
+
diagnostics.push({
|
|
335
|
+
code: "PARSE_FALLBACK",
|
|
336
|
+
severity: "warning",
|
|
337
|
+
message: `Skipped AST transform due to parse error in "${relPath}".`,
|
|
338
|
+
file: relPath
|
|
339
|
+
});
|
|
340
|
+
} else if (result.skippedReason === "already-protected") {
|
|
341
|
+
diagnostics.push({
|
|
342
|
+
code: "ALREADY_PROTECTED",
|
|
343
|
+
severity: "info",
|
|
344
|
+
message: `File "${relPath}" is already protected.`,
|
|
345
|
+
file: relPath
|
|
346
|
+
});
|
|
347
|
+
} else if (result.skippedReason === "empty") {
|
|
348
|
+
diagnostics.push({
|
|
349
|
+
code: "EMPTY_INPUT",
|
|
350
|
+
severity: "info",
|
|
351
|
+
message: `File "${relPath}" is empty.`,
|
|
352
|
+
file: relPath
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
let finalCode = result.code;
|
|
356
|
+
let preambleLineOffset = 0;
|
|
357
|
+
if (shouldInjectShield && masterShield?.preambleCode && !finalCode.includes(masterShield.integrityCheckFnName) && !finalCode.startsWith(masterShield.preambleCode)) {
|
|
358
|
+
const prefix = `${masterShield.preambleCode}
|
|
359
|
+
`;
|
|
360
|
+
preambleLineOffset = (prefix.match(/\n/g) ?? []).length;
|
|
361
|
+
finalCode = `${prefix}${finalCode}`;
|
|
362
|
+
}
|
|
363
|
+
const originalMap = keepPrivateMaps ? readOriginalSourceMapForFile(filePath, relPath, vaultResult.vaultDir, deferVaultWrites) : void 0;
|
|
364
|
+
const rawTransformMap = result.sourceMap;
|
|
365
|
+
const shiftedTransformMap = rawTransformMap ? shiftSourceMapLines(rawTransformMap, preambleLineOffset) : void 0;
|
|
366
|
+
let composedMap;
|
|
367
|
+
if (shiftedTransformMap && originalMap) {
|
|
368
|
+
composedMap = chainSourceMaps(shiftedTransformMap, originalMap);
|
|
369
|
+
} else if (originalMap) {
|
|
370
|
+
composedMap = flattenSourceMapSections(originalMap);
|
|
371
|
+
} else if (shiftedTransformMap) {
|
|
372
|
+
composedMap = shiftedTransformMap;
|
|
373
|
+
}
|
|
374
|
+
const originalMapDebugId = originalMap?.debugId ?? originalMap?.debug_id;
|
|
375
|
+
const hadDebugId = Boolean(fileDebugId || originalMapDebugId || result.debugId);
|
|
376
|
+
let mintedDebugId;
|
|
377
|
+
if (hadDebugId) {
|
|
378
|
+
const codeWithoutDebugId = stripDebugIdComments(finalCode);
|
|
379
|
+
mintedDebugId = computeDeterministicDebugId(
|
|
380
|
+
codeWithoutDebugId,
|
|
381
|
+
composedMap?.mappings ?? ""
|
|
382
|
+
);
|
|
383
|
+
finalCode = replaceOrAppendDebugId(codeWithoutDebugId, mintedDebugId);
|
|
384
|
+
if (composedMap) {
|
|
385
|
+
composedMap.debugId = mintedDebugId;
|
|
386
|
+
delete composedMap.debug_id;
|
|
387
|
+
}
|
|
388
|
+
debugIdsPreserved++;
|
|
389
|
+
}
|
|
390
|
+
if (!dryRun && keepPrivateMaps && (composedMap || mintedDebugId)) {
|
|
391
|
+
pendingVaultMaps.push({ relPath, map: composedMap, debugId: mintedDebugId });
|
|
392
|
+
}
|
|
393
|
+
if (!dryRun) {
|
|
394
|
+
const tmpPath = `${filePath}.unpeek.tmp`;
|
|
395
|
+
fs.writeFileSync(tmpPath, finalCode, "utf8");
|
|
396
|
+
stagedFiles.push({ tmpPath, finalPath: filePath });
|
|
397
|
+
}
|
|
398
|
+
const protBuf = Buffer.from(finalCode, "utf8");
|
|
399
|
+
protectedBytes += protBuf.byteLength;
|
|
400
|
+
allProtBufs.push(protBuf);
|
|
401
|
+
totalHotFunctionsSkipped += result.hotPathReport.hotFunctionsSkipped.length;
|
|
402
|
+
totalColdFunctionsProtected += result.hotPathReport.coldFunctionsProtected.length;
|
|
403
|
+
totalEncryptedStrings += result.encryptedStringsCount;
|
|
404
|
+
totalMangledSymbols += result.mangledSymbolsCount;
|
|
405
|
+
processedFiles++;
|
|
406
|
+
}
|
|
407
|
+
originalGzipBytes = allOrigBufs.length > 0 ? zlib.gzipSync(Buffer.concat(allOrigBufs)).byteLength : 0;
|
|
408
|
+
protectedGzipBytes = allProtBufs.length > 0 ? zlib.gzipSync(Buffer.concat(allProtBufs)).byteLength : 0;
|
|
409
|
+
gzipDeltaPercent = originalGzipBytes > 0 ? Number(((protectedGzipBytes - originalGzipBytes) / originalGzipBytes * 100).toFixed(2)) : 0;
|
|
410
|
+
budgetExceeded = typeof options.maxGzipDelta === "number" && gzipDeltaPercent > options.maxGzipDelta;
|
|
411
|
+
if (budgetExceeded && !dryRun) {
|
|
412
|
+
throw new UnpeekError(
|
|
413
|
+
"ERR_BUDGET_EXCEEDED",
|
|
414
|
+
`Protected bundle Gzip delta (${gzipDeltaPercent}%) exceeded maxGzipDelta (${options.maxGzipDelta}%).`
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
if (!dryRun) {
|
|
418
|
+
if (deferVaultWrites) {
|
|
419
|
+
const finalVaultResult = relocateSourceMaps(
|
|
420
|
+
outDir,
|
|
421
|
+
cwd,
|
|
422
|
+
keepPrivateMaps,
|
|
423
|
+
privateMapsDir,
|
|
424
|
+
false,
|
|
425
|
+
config,
|
|
426
|
+
{ dryRun: false }
|
|
427
|
+
);
|
|
428
|
+
vaultResult.movedMapsCount = finalVaultResult.movedMapsCount;
|
|
429
|
+
vaultResult.vaultDir = finalVaultResult.vaultDir;
|
|
430
|
+
vaultResult.movedFiles = finalVaultResult.movedFiles;
|
|
431
|
+
vaultResult.dockerIgnoreMissing = finalVaultResult.dockerIgnoreMissing;
|
|
432
|
+
}
|
|
433
|
+
for (const { relPath, map, debugId } of pendingVaultMaps) {
|
|
434
|
+
const vaultedMapPath = path.join(vaultResult.vaultDir, `${relPath}.map`);
|
|
435
|
+
const decodedMapPath = path.join(
|
|
436
|
+
vaultResult.vaultDir,
|
|
437
|
+
`${safeDecodeURIComponent(relPath)}.map`
|
|
438
|
+
);
|
|
439
|
+
const targetMapPath = fs.existsSync(vaultedMapPath) ? vaultedMapPath : fs.existsSync(decodedMapPath) ? decodedMapPath : vaultedMapPath;
|
|
440
|
+
if (map && fs.existsSync(targetMapPath)) {
|
|
441
|
+
try {
|
|
442
|
+
fs.writeFileSync(targetMapPath, JSON.stringify(map), "utf8");
|
|
443
|
+
} catch {
|
|
444
|
+
}
|
|
445
|
+
} else if (debugId && fs.existsSync(targetMapPath)) {
|
|
446
|
+
try {
|
|
447
|
+
const mapJson = JSON.parse(fs.readFileSync(targetMapPath, "utf8"));
|
|
448
|
+
mapJson.debugId = debugId;
|
|
449
|
+
delete mapJson.debug_id;
|
|
450
|
+
fs.writeFileSync(targetMapPath, JSON.stringify(mapJson), "utf8");
|
|
451
|
+
} catch {
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
for (const { tmpPath, finalPath } of stagedFiles) {
|
|
456
|
+
fs.renameSync(tmpPath, finalPath);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
} catch (error) {
|
|
460
|
+
for (const { tmpPath } of stagedFiles) {
|
|
461
|
+
if (fs.existsSync(tmpPath)) {
|
|
462
|
+
try {
|
|
463
|
+
fs.unlinkSync(tmpPath);
|
|
464
|
+
} catch {
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
throw error;
|
|
469
|
+
}
|
|
470
|
+
const elapsedMs = Math.max(1, Math.round(performance.now() - startTime));
|
|
471
|
+
return {
|
|
472
|
+
framework,
|
|
473
|
+
outDir,
|
|
474
|
+
processedFiles,
|
|
475
|
+
movedSourceMaps: vaultResult.movedMapsCount,
|
|
476
|
+
originalBytes,
|
|
477
|
+
protectedBytes,
|
|
478
|
+
originalGzipBytes,
|
|
479
|
+
protectedGzipBytes,
|
|
480
|
+
gzipDeltaPercent,
|
|
481
|
+
totalHotFunctionsSkipped,
|
|
482
|
+
totalColdFunctionsProtected,
|
|
483
|
+
totalEncryptedStrings,
|
|
484
|
+
totalMangledSymbols,
|
|
485
|
+
elapsedMs,
|
|
486
|
+
seed,
|
|
487
|
+
tier: aggressive ? "Tier 2" : "Tier 1",
|
|
488
|
+
aggressive,
|
|
489
|
+
dockerIgnoreMissing: vaultResult.dockerIgnoreMissing,
|
|
490
|
+
dryRun,
|
|
491
|
+
profile: effectiveProfile,
|
|
492
|
+
budgetExceeded,
|
|
493
|
+
debugIdsPreserved,
|
|
494
|
+
diagnostics
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
export {
|
|
499
|
+
isNext16OrAbove,
|
|
500
|
+
serializeNextOptions,
|
|
501
|
+
readUnpeekNextOptionsFromEnv,
|
|
502
|
+
protectProject
|
|
503
|
+
};
|