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,1054 @@
|
|
|
1
|
+
import {
|
|
2
|
+
UnpeekError
|
|
3
|
+
} from "./chunk-LA6J3BT3.js";
|
|
4
|
+
|
|
5
|
+
// src/core/sourcemap-vault.ts
|
|
6
|
+
import fs2 from "fs";
|
|
7
|
+
import path2 from "path";
|
|
8
|
+
|
|
9
|
+
// src/core/detector.ts
|
|
10
|
+
import fs from "fs";
|
|
11
|
+
import path from "path";
|
|
12
|
+
var SUPPORTED_EXTENSIONS = /* @__PURE__ */ new Set([".js", ".mjs", ".cjs"]);
|
|
13
|
+
var EXCLUDED_DIR_NAMES = /* @__PURE__ */ new Set([
|
|
14
|
+
"node_modules",
|
|
15
|
+
".unpeek-maps"
|
|
16
|
+
]);
|
|
17
|
+
var WORKER_OR_SW_BASENAME_REGEX = /^(?:sw|service-worker|workbox-[^/]+)\.(?:js|mjs|cjs)$|\.worker(?:[.-][^/]+)*\.(?:js|mjs|cjs)$/i;
|
|
18
|
+
function isWorkerOrServiceWorkerFile(filePath, rootDir) {
|
|
19
|
+
const normalized = filePath.replace(/\\/g, "/").split("?")[0];
|
|
20
|
+
const baseName = path.posix.basename(normalized);
|
|
21
|
+
if (WORKER_OR_SW_BASENAME_REGEX.test(baseName)) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
if (rootDir) {
|
|
25
|
+
const rel = path.relative(rootDir, filePath).replace(/\\/g, "/");
|
|
26
|
+
const relBase = path.posix.basename(rel);
|
|
27
|
+
if (WORKER_OR_SW_BASENAME_REGEX.test(relBase)) {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
var NEXT_CONFIG_FILES = ["next.config.ts", "next.config.js", "next.config.mjs", "next.config.cjs"];
|
|
34
|
+
var VITE_CONFIG_FILES = ["vite.config.ts", "vite.config.js", "vite.config.mjs", "vite.config.cjs"];
|
|
35
|
+
function escapeRegex(str) {
|
|
36
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
37
|
+
}
|
|
38
|
+
function extractConfigValue(rootDir, filenames, pattern) {
|
|
39
|
+
for (const filename of filenames) {
|
|
40
|
+
const filePath = path.join(rootDir, filename);
|
|
41
|
+
if (fs.existsSync(filePath)) {
|
|
42
|
+
try {
|
|
43
|
+
const content = fs.readFileSync(filePath, "utf8");
|
|
44
|
+
const match = content.match(pattern);
|
|
45
|
+
if (match && match[1]) {
|
|
46
|
+
return match[1].trim();
|
|
47
|
+
}
|
|
48
|
+
} catch {
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return void 0;
|
|
53
|
+
}
|
|
54
|
+
function hasNextServerSegment(fullPath, rootDir, serverDirPrefixes) {
|
|
55
|
+
const normalized = fullPath.replace(/\\/g, "/");
|
|
56
|
+
if (normalized.includes("/node_modules/") || normalized.endsWith("/node_modules") || normalized.includes("/.unpeek-maps/") || normalized.endsWith("/.unpeek-maps")) {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
if (/(?:^|\/)\.next\/(?:server|ssr)(?:\/|$)/i.test(normalized)) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
if (serverDirPrefixes && serverDirPrefixes.length > 0) {
|
|
63
|
+
for (const prefix of serverDirPrefixes) {
|
|
64
|
+
const normPrefix = prefix.replace(/\\/g, "/").replace(/^\/+|\/+$/g, "");
|
|
65
|
+
if (normPrefix) {
|
|
66
|
+
const regex = new RegExp(`(?:^|\\/)${escapeRegex(normPrefix)}(?:\\/|$)`, "i");
|
|
67
|
+
if (regex.test(normalized)) {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (rootDir) {
|
|
74
|
+
const rel = path.relative(rootDir, fullPath).replace(/\\/g, "/");
|
|
75
|
+
const segments = rel.split("/").filter(Boolean);
|
|
76
|
+
if (segments.length > 0 && (segments[0].toLowerCase() === "server" || segments[0].toLowerCase() === "ssr")) {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
function detectTargetFramework(cwd, explicitOutDir, explicitConfig) {
|
|
83
|
+
let config = explicitConfig;
|
|
84
|
+
let outDirOverride;
|
|
85
|
+
if (typeof explicitOutDir === "object" && explicitOutDir !== null) {
|
|
86
|
+
config = explicitOutDir;
|
|
87
|
+
outDirOverride = config.outDir;
|
|
88
|
+
} else if (typeof explicitOutDir === "string") {
|
|
89
|
+
outDirOverride = explicitOutDir;
|
|
90
|
+
}
|
|
91
|
+
const rootDir = path.resolve(cwd);
|
|
92
|
+
let framework = config?.framework;
|
|
93
|
+
const discoveredNextDist = extractConfigValue(
|
|
94
|
+
rootDir,
|
|
95
|
+
NEXT_CONFIG_FILES,
|
|
96
|
+
/(?:["']?distDir["']?\s*:\s*["']([^"']+)["'])/
|
|
97
|
+
);
|
|
98
|
+
const discoveredViteOut = extractConfigValue(
|
|
99
|
+
rootDir,
|
|
100
|
+
VITE_CONFIG_FILES,
|
|
101
|
+
/(?:["']?outDir["']?\s*:\s*["']([^"']+)["'])/
|
|
102
|
+
);
|
|
103
|
+
const discoveredViteAssets = extractConfigValue(
|
|
104
|
+
rootDir,
|
|
105
|
+
VITE_CONFIG_FILES,
|
|
106
|
+
/(?:["']?assetsDir["']?\s*:\s*["']([^"']+)["'])/
|
|
107
|
+
);
|
|
108
|
+
const nextDist = config?.next?.distDir ?? discoveredNextDist ?? ".next";
|
|
109
|
+
const viteOut = config?.vite?.outDir ?? discoveredViteOut ?? "dist";
|
|
110
|
+
const viteAssets = config?.vite?.assetsDir ?? discoveredViteAssets ?? "assets";
|
|
111
|
+
if (!framework) {
|
|
112
|
+
const pkgPath = path.join(rootDir, "package.json");
|
|
113
|
+
if (fs.existsSync(pkgPath)) {
|
|
114
|
+
try {
|
|
115
|
+
const pkgRaw = fs.readFileSync(pkgPath, "utf8");
|
|
116
|
+
const pkg = JSON.parse(pkgRaw);
|
|
117
|
+
const allDeps = {
|
|
118
|
+
...pkg.dependencies ?? {},
|
|
119
|
+
...pkg.devDependencies ?? {}
|
|
120
|
+
};
|
|
121
|
+
if ("next" in allDeps) {
|
|
122
|
+
framework = "nextjs";
|
|
123
|
+
} else if ("vite" in allDeps) {
|
|
124
|
+
framework = "vite";
|
|
125
|
+
} else if ("webpack" in allDeps || "react-scripts" in allDeps || "@rspack/core" in allDeps) {
|
|
126
|
+
framework = "webpack";
|
|
127
|
+
}
|
|
128
|
+
} catch {
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const nextChunksDir = path.join(rootDir, nextDist, "static", "chunks");
|
|
133
|
+
const nextExportChunksDir = path.join(rootDir, "out", "_next", "static", "chunks");
|
|
134
|
+
const nextExportDir = path.join(rootDir, "out");
|
|
135
|
+
const nextDistDir = path.join(rootDir, nextDist);
|
|
136
|
+
const distAssetsDir = path.join(rootDir, viteOut, viteAssets);
|
|
137
|
+
const distDir = path.join(rootDir, viteOut);
|
|
138
|
+
const buildDir = path.join(rootDir, "build");
|
|
139
|
+
const publicDir = path.join(rootDir, "public");
|
|
140
|
+
if (!framework) {
|
|
141
|
+
if (fs.existsSync(nextChunksDir) || fs.existsSync(nextDistDir) || fs.existsSync(nextExportChunksDir) || discoveredNextDist !== void 0) {
|
|
142
|
+
framework = "nextjs";
|
|
143
|
+
} else if (fs.existsSync(distAssetsDir) || discoveredViteOut !== void 0) {
|
|
144
|
+
framework = "vite";
|
|
145
|
+
} else if (fs.existsSync(buildDir)) {
|
|
146
|
+
framework = "webpack";
|
|
147
|
+
} else if (outDirOverride) {
|
|
148
|
+
const normExplicit = outDirOverride.replace(/\\/g, "/");
|
|
149
|
+
if (normExplicit.includes(nextDist) || normExplicit.includes(".next") || normExplicit.includes("out/_next")) {
|
|
150
|
+
framework = "nextjs";
|
|
151
|
+
} else if (normExplicit.includes(`${viteOut}/${viteAssets}`) || normExplicit.includes("dist/assets")) {
|
|
152
|
+
framework = "vite";
|
|
153
|
+
} else if (normExplicit.includes("build")) {
|
|
154
|
+
framework = "webpack";
|
|
155
|
+
} else {
|
|
156
|
+
framework = "vanilla";
|
|
157
|
+
}
|
|
158
|
+
} else {
|
|
159
|
+
framework = "vanilla";
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (outDirOverride) {
|
|
163
|
+
return {
|
|
164
|
+
framework,
|
|
165
|
+
outDir: path.resolve(rootDir, outDirOverride)
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
let outDir;
|
|
169
|
+
if (framework === "nextjs") {
|
|
170
|
+
if (fs.existsSync(nextExportChunksDir)) {
|
|
171
|
+
outDir = nextExportChunksDir;
|
|
172
|
+
} else if (fs.existsSync(nextChunksDir)) {
|
|
173
|
+
outDir = nextChunksDir;
|
|
174
|
+
} else if (fs.existsSync(nextExportDir)) {
|
|
175
|
+
outDir = nextExportDir;
|
|
176
|
+
} else if (fs.existsSync(nextDistDir)) {
|
|
177
|
+
outDir = nextChunksDir;
|
|
178
|
+
} else {
|
|
179
|
+
outDir = nextChunksDir;
|
|
180
|
+
}
|
|
181
|
+
} else if (framework === "vite") {
|
|
182
|
+
outDir = fs.existsSync(distAssetsDir) ? distAssetsDir : distDir;
|
|
183
|
+
} else if (framework === "webpack") {
|
|
184
|
+
if (fs.existsSync(distDir)) {
|
|
185
|
+
outDir = distDir;
|
|
186
|
+
} else if (fs.existsSync(buildDir)) {
|
|
187
|
+
outDir = buildDir;
|
|
188
|
+
} else {
|
|
189
|
+
outDir = distDir;
|
|
190
|
+
}
|
|
191
|
+
} else {
|
|
192
|
+
const candidates = [nextExportChunksDir, nextChunksDir, distAssetsDir, distDir, buildDir, publicDir];
|
|
193
|
+
const existing = candidates.find((candidate) => fs.existsSync(candidate));
|
|
194
|
+
outDir = existing ?? distDir;
|
|
195
|
+
}
|
|
196
|
+
return { framework, outDir };
|
|
197
|
+
}
|
|
198
|
+
function scanClientBundles(outDir, config) {
|
|
199
|
+
const resolvedOutDir = path.resolve(outDir);
|
|
200
|
+
if (!fs.existsSync(resolvedOutDir)) {
|
|
201
|
+
return [];
|
|
202
|
+
}
|
|
203
|
+
const results = [];
|
|
204
|
+
const serverDirPrefixes = [];
|
|
205
|
+
if (config?.next?.distDir) {
|
|
206
|
+
serverDirPrefixes.push(
|
|
207
|
+
path.join(config.next.distDir, "server"),
|
|
208
|
+
path.join(config.next.distDir, "ssr")
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
if (config?.vite?.outDir) {
|
|
212
|
+
serverDirPrefixes.push(path.join(config.vite.outDir, "server"));
|
|
213
|
+
}
|
|
214
|
+
const allowedExtensions = config?.extensions && config.extensions.length > 0 ? new Set(config.extensions.map((e) => e.startsWith(".") ? e.toLowerCase() : `.${e.toLowerCase()}`)) : SUPPORTED_EXTENSIONS;
|
|
215
|
+
function walk(currentDir) {
|
|
216
|
+
if (hasNextServerSegment(currentDir, resolvedOutDir, serverDirPrefixes)) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
let entries;
|
|
220
|
+
try {
|
|
221
|
+
entries = fs.readdirSync(currentDir, { withFileTypes: true });
|
|
222
|
+
} catch {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
for (const entry of entries) {
|
|
226
|
+
const fullPath = path.join(currentDir, entry.name);
|
|
227
|
+
if (entry.isDirectory()) {
|
|
228
|
+
if (EXCLUDED_DIR_NAMES.has(entry.name.toLowerCase())) {
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
if (hasNextServerSegment(fullPath, resolvedOutDir, serverDirPrefixes)) {
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
walk(fullPath);
|
|
235
|
+
} else if (entry.isFile()) {
|
|
236
|
+
if (hasNextServerSegment(fullPath, resolvedOutDir, serverDirPrefixes)) {
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
const ext = path.extname(entry.name).toLowerCase();
|
|
240
|
+
if (allowedExtensions.has(ext)) {
|
|
241
|
+
if (config?.target !== "worker" && isWorkerOrServiceWorkerFile(fullPath, resolvedOutDir)) {
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
results.push(fullPath);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
walk(resolvedOutDir);
|
|
250
|
+
return results.sort();
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// src/core/sourcemap-vault.ts
|
|
254
|
+
var SOURCE_MAP_COMMENT_REGEX = /[ \t]*(?:\/\/[#@]\s*sourceMappingURL=[^\r\n]*|\/\*[#@]\s*sourceMappingURL=[\s\S]*?\*\/)[ \t]*(?:\r?\n)?/g;
|
|
255
|
+
var DEBUG_ID_COMMENT_REGEX = /(?:\/\/[#@]\s*debugId=([^\s*]+)|\/\*[#@]\s*debugId=([^\s*]+)\s*\*\/)/;
|
|
256
|
+
var DEBUG_ID_LINE_REGEX = /[ \t]*(?:\/\/[#@]\s*debugId=[^\r\n]*|\/\*[#@]\s*debugId=[\s\S]*?\*\/)[ \t]*(?:\r?\n)?/g;
|
|
257
|
+
function extractDebugId(code) {
|
|
258
|
+
const match = code.match(DEBUG_ID_COMMENT_REGEX);
|
|
259
|
+
const id = match?.[1] ?? match?.[2];
|
|
260
|
+
return id ? id.trim() : void 0;
|
|
261
|
+
}
|
|
262
|
+
function stripDebugIdComments(code) {
|
|
263
|
+
DEBUG_ID_LINE_REGEX.lastIndex = 0;
|
|
264
|
+
return code.replace(DEBUG_ID_LINE_REGEX, "");
|
|
265
|
+
}
|
|
266
|
+
function replaceOrAppendDebugId(code, debugId) {
|
|
267
|
+
const cleaned = stripDebugIdComments(code);
|
|
268
|
+
const suffix = cleaned.length > 0 && !cleaned.endsWith("\n") ? "\n" : "";
|
|
269
|
+
return `${cleaned}${suffix}//# debugId=${debugId}
|
|
270
|
+
`;
|
|
271
|
+
}
|
|
272
|
+
function stripSourceMapComments(code) {
|
|
273
|
+
const debugId = extractDebugId(code);
|
|
274
|
+
SOURCE_MAP_COMMENT_REGEX.lastIndex = 0;
|
|
275
|
+
let next = code.replace(SOURCE_MAP_COMMENT_REGEX, "");
|
|
276
|
+
const stripped = next !== code;
|
|
277
|
+
if (debugId && !DEBUG_ID_COMMENT_REGEX.test(next)) {
|
|
278
|
+
const suffix = next.length > 0 && !next.endsWith("\n") ? "\n" : "";
|
|
279
|
+
next = `${next}${suffix}//# debugId=${debugId}
|
|
280
|
+
`;
|
|
281
|
+
}
|
|
282
|
+
return { code: next, stripped, debugId };
|
|
283
|
+
}
|
|
284
|
+
function ensureIgnoreEntry(dir, fileName, entryName) {
|
|
285
|
+
const filePath = path2.join(path2.resolve(dir), fileName);
|
|
286
|
+
if (!fs2.existsSync(filePath)) {
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
const content = fs2.readFileSync(filePath, "utf8");
|
|
290
|
+
const normalizedEntry = entryName.endsWith("/") ? entryName : `${entryName}/`;
|
|
291
|
+
const bareEntry = normalizedEntry.slice(0, -1);
|
|
292
|
+
const lines = content.split(/\r?\n/).map((line) => line.trim());
|
|
293
|
+
if (!lines.includes(normalizedEntry) && !lines.includes(bareEntry) && !lines.includes(".unpeek-maps") && !lines.includes(".unpeek-maps/")) {
|
|
294
|
+
const prefix = content.length > 0 && !content.endsWith("\n") ? "\n" : "";
|
|
295
|
+
fs2.writeFileSync(filePath, `${content}${prefix}${normalizedEntry}
|
|
296
|
+
`, "utf8");
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
function isSubdirectoryOrSame(parent, child) {
|
|
300
|
+
const rel = path2.relative(parent, child);
|
|
301
|
+
return rel === "" || !rel.startsWith("..") && !path2.isAbsolute(rel);
|
|
302
|
+
}
|
|
303
|
+
function findMonorepoRoots(cwd) {
|
|
304
|
+
const resolved = path2.resolve(cwd);
|
|
305
|
+
const dirs = [resolved];
|
|
306
|
+
let curr = resolved;
|
|
307
|
+
while (true) {
|
|
308
|
+
const parent = path2.dirname(curr);
|
|
309
|
+
if (!parent || parent === curr) {
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
const hasGit = fs2.existsSync(path2.join(parent, ".git"));
|
|
313
|
+
const hasWorkspace = fs2.existsSync(path2.join(parent, "pnpm-workspace.yaml"));
|
|
314
|
+
const hasGitIgnore = fs2.existsSync(path2.join(parent, ".gitignore"));
|
|
315
|
+
const hasDockerIgnore = fs2.existsSync(path2.join(parent, ".dockerignore"));
|
|
316
|
+
const hasPkgJson = fs2.existsSync(path2.join(parent, "package.json"));
|
|
317
|
+
if (hasGit || hasWorkspace || hasGitIgnore || hasDockerIgnore || hasPkgJson) {
|
|
318
|
+
dirs.push(parent);
|
|
319
|
+
}
|
|
320
|
+
if (hasGit || hasWorkspace) {
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
curr = parent;
|
|
324
|
+
}
|
|
325
|
+
return dirs;
|
|
326
|
+
}
|
|
327
|
+
function checkDockerIgnoreMissing(cwd, ignoreEntry) {
|
|
328
|
+
const candidateDirs = findMonorepoRoots(cwd);
|
|
329
|
+
let foundAnyDockerIgnore = false;
|
|
330
|
+
for (const dir of candidateDirs) {
|
|
331
|
+
const filePath = path2.join(dir, ".dockerignore");
|
|
332
|
+
if (fs2.existsSync(filePath)) {
|
|
333
|
+
foundAnyDockerIgnore = true;
|
|
334
|
+
const content = fs2.readFileSync(filePath, "utf8");
|
|
335
|
+
const normalizedEntry = ignoreEntry.endsWith("/") ? ignoreEntry : `${ignoreEntry}/`;
|
|
336
|
+
const bareEntry = normalizedEntry.slice(0, -1);
|
|
337
|
+
const lines = content.split(/\r?\n/).map((line) => line.trim());
|
|
338
|
+
if (lines.includes(normalizedEntry) || lines.includes(bareEntry) || lines.includes(".unpeek-maps") || lines.includes(".unpeek-maps/")) {
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return true;
|
|
344
|
+
}
|
|
345
|
+
function updateAllIgnoreFiles(cwd, vaultDir) {
|
|
346
|
+
const candidateDirs = findMonorepoRoots(cwd);
|
|
347
|
+
for (const dir of candidateDirs) {
|
|
348
|
+
const relFromDir = path2.relative(dir, vaultDir).replace(/\\/g, "/");
|
|
349
|
+
const entry = relFromDir && !relFromDir.startsWith("..") ? `${relFromDir}/` : ".unpeek-maps/";
|
|
350
|
+
ensureIgnoreEntry(dir, ".gitignore", entry);
|
|
351
|
+
ensureIgnoreEntry(dir, ".dockerignore", entry);
|
|
352
|
+
ensureIgnoreEntry(dir, ".npmignore", entry);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
function relocateSourceMaps(outDir, cwd, keepPrivateMaps = true, privateMapsDir = ".unpeek-maps", stripComments = true, config, options) {
|
|
356
|
+
const resolvedOutDir = path2.resolve(outDir);
|
|
357
|
+
const resolvedCwd = path2.resolve(cwd);
|
|
358
|
+
const effectiveVaultDirName = config?.sourcemaps?.vaultDir ?? privateMapsDir ?? ".unpeek-maps";
|
|
359
|
+
const effectiveKeepPrivate = config?.sourcemaps?.keepPrivate ?? keepPrivateMaps;
|
|
360
|
+
const effectiveStripComments = config?.sourcemaps?.stripComments ?? stripComments;
|
|
361
|
+
const updateIgnoreFiles = typeof options === "boolean" ? options : options?.updateIgnoreFiles ?? true;
|
|
362
|
+
const dryRun = typeof options === "object" && options !== null ? options.dryRun ?? false : false;
|
|
363
|
+
const vaultDir = path2.resolve(resolvedCwd, effectiveVaultDirName);
|
|
364
|
+
const publicDirsToCheck = /* @__PURE__ */ new Set();
|
|
365
|
+
if (config?.publicDirs && config.publicDirs.length > 0) {
|
|
366
|
+
for (const d of config.publicDirs) {
|
|
367
|
+
publicDirsToCheck.add(d);
|
|
368
|
+
}
|
|
369
|
+
} else {
|
|
370
|
+
publicDirsToCheck.add("public");
|
|
371
|
+
publicDirsToCheck.add("static");
|
|
372
|
+
publicDirsToCheck.add("assets");
|
|
373
|
+
}
|
|
374
|
+
if (effectiveKeepPrivate !== false) {
|
|
375
|
+
if (!isSubdirectoryOrSame(resolvedCwd, vaultDir)) {
|
|
376
|
+
throw new UnpeekError(
|
|
377
|
+
"ERR_SECURITY_PATH_TRAVERSAL",
|
|
378
|
+
`[unpeek] Security Error: privateMapsDir (${vaultDir}) cannot traverse outside cwd (${resolvedCwd}).`
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
if (isSubdirectoryOrSame(resolvedOutDir, vaultDir)) {
|
|
382
|
+
throw new Error(
|
|
383
|
+
`[unpeek] Security Error: privateMapsDir cannot be located inside outDir (${resolvedOutDir}). This would expose private source maps to the web server.`
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
for (const pubDir of publicDirsToCheck) {
|
|
387
|
+
const resolvedPublicDir = path2.resolve(resolvedCwd, pubDir);
|
|
388
|
+
if (isSubdirectoryOrSame(resolvedPublicDir, vaultDir)) {
|
|
389
|
+
throw new Error(
|
|
390
|
+
`[unpeek] Security Error: privateMapsDir cannot be located inside publicDir (${resolvedPublicDir}). This would expose private source maps to the web server.`
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
const relVault = path2.relative(resolvedCwd, vaultDir).replace(/\\/g, "/");
|
|
396
|
+
const ignoreEntry = relVault && !relVault.startsWith("..") ? `${relVault}/` : ".unpeek-maps/";
|
|
397
|
+
const dockerIgnoreMissing = checkDockerIgnoreMissing(resolvedCwd, ignoreEntry);
|
|
398
|
+
const movedFiles = [];
|
|
399
|
+
let movedMapsCount = 0;
|
|
400
|
+
let strippedFilesCount = 0;
|
|
401
|
+
if (!fs2.existsSync(resolvedOutDir)) {
|
|
402
|
+
return {
|
|
403
|
+
movedMapsCount: 0,
|
|
404
|
+
strippedFilesCount: 0,
|
|
405
|
+
vaultDir,
|
|
406
|
+
movedFiles: [],
|
|
407
|
+
dockerIgnoreMissing
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
function walkForMaps(currentDir) {
|
|
411
|
+
if (hasNextServerSegment(currentDir, resolvedOutDir)) {
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
let entries;
|
|
415
|
+
try {
|
|
416
|
+
entries = fs2.readdirSync(currentDir, { withFileTypes: true });
|
|
417
|
+
} catch {
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
for (const entry of entries) {
|
|
421
|
+
const fullPath = path2.join(currentDir, entry.name);
|
|
422
|
+
if (entry.isDirectory()) {
|
|
423
|
+
if (entry.name === "node_modules" || entry.name === ".unpeek-maps") {
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
if (hasNextServerSegment(fullPath, resolvedOutDir)) {
|
|
427
|
+
continue;
|
|
428
|
+
}
|
|
429
|
+
walkForMaps(fullPath);
|
|
430
|
+
} else if (entry.isFile() && entry.name.endsWith(".map")) {
|
|
431
|
+
if (effectiveKeepPrivate !== false) {
|
|
432
|
+
const relPath = path2.relative(resolvedOutDir, fullPath);
|
|
433
|
+
const targetPath = path2.join(vaultDir, relPath);
|
|
434
|
+
if (!dryRun) {
|
|
435
|
+
fs2.mkdirSync(path2.dirname(targetPath), { recursive: true });
|
|
436
|
+
fs2.copyFileSync(fullPath, targetPath);
|
|
437
|
+
fs2.unlinkSync(fullPath);
|
|
438
|
+
}
|
|
439
|
+
movedFiles.push(targetPath);
|
|
440
|
+
} else if (!dryRun) {
|
|
441
|
+
fs2.unlinkSync(fullPath);
|
|
442
|
+
}
|
|
443
|
+
movedMapsCount++;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
walkForMaps(resolvedOutDir);
|
|
448
|
+
if (!dryRun && updateIgnoreFiles && effectiveKeepPrivate !== false && movedMapsCount > 0) {
|
|
449
|
+
updateAllIgnoreFiles(resolvedCwd, vaultDir);
|
|
450
|
+
}
|
|
451
|
+
if (effectiveStripComments) {
|
|
452
|
+
const clientFiles = scanClientBundles(resolvedOutDir, config);
|
|
453
|
+
for (const filePath of clientFiles) {
|
|
454
|
+
const original = fs2.readFileSync(filePath, "utf8");
|
|
455
|
+
if (original.includes("sourceMappingURL=")) {
|
|
456
|
+
const { code: strippedCode, stripped } = stripSourceMapComments(original);
|
|
457
|
+
if (stripped) {
|
|
458
|
+
if (!dryRun) {
|
|
459
|
+
fs2.writeFileSync(filePath, strippedCode, "utf8");
|
|
460
|
+
}
|
|
461
|
+
strippedFilesCount++;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
return {
|
|
467
|
+
movedMapsCount,
|
|
468
|
+
strippedFilesCount,
|
|
469
|
+
vaultDir,
|
|
470
|
+
movedFiles,
|
|
471
|
+
dockerIgnoreMissing
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// src/core/vlq.ts
|
|
476
|
+
import crypto from "crypto";
|
|
477
|
+
import fs3 from "fs";
|
|
478
|
+
import path3 from "path";
|
|
479
|
+
var BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
480
|
+
var BASE64_MAP = /* @__PURE__ */ new Map();
|
|
481
|
+
for (let i = 0; i < BASE64_CHARS.length; i++) {
|
|
482
|
+
BASE64_MAP.set(BASE64_CHARS[i], i);
|
|
483
|
+
}
|
|
484
|
+
var VLQ_BASE_SHIFT = 5;
|
|
485
|
+
var VLQ_BASE = 1 << VLQ_BASE_SHIFT;
|
|
486
|
+
var VLQ_BASE_MASK = VLQ_BASE - 1;
|
|
487
|
+
var VLQ_CONTINUATION_BIT = VLQ_BASE;
|
|
488
|
+
function encodeVlq(value) {
|
|
489
|
+
let vlq = value < 0 ? (-value << 1) + 1 : value << 1;
|
|
490
|
+
let encoded = "";
|
|
491
|
+
do {
|
|
492
|
+
let digit = vlq & VLQ_BASE_MASK;
|
|
493
|
+
vlq >>>= VLQ_BASE_SHIFT;
|
|
494
|
+
if (vlq > 0) {
|
|
495
|
+
digit |= VLQ_CONTINUATION_BIT;
|
|
496
|
+
}
|
|
497
|
+
encoded += BASE64_CHARS[digit];
|
|
498
|
+
} while (vlq > 0);
|
|
499
|
+
return encoded;
|
|
500
|
+
}
|
|
501
|
+
function decodeVlq(segment, startIndex) {
|
|
502
|
+
let result = 0;
|
|
503
|
+
let shift = 0;
|
|
504
|
+
let index = startIndex;
|
|
505
|
+
while (index < segment.length) {
|
|
506
|
+
const digit = BASE64_MAP.get(segment[index]);
|
|
507
|
+
if (digit === void 0) {
|
|
508
|
+
return null;
|
|
509
|
+
}
|
|
510
|
+
index++;
|
|
511
|
+
const continuation = (digit & VLQ_CONTINUATION_BIT) !== 0;
|
|
512
|
+
result += (digit & VLQ_BASE_MASK) << shift;
|
|
513
|
+
shift += VLQ_BASE_SHIFT;
|
|
514
|
+
if (!continuation) {
|
|
515
|
+
const isNegative = (result & 1) === 1;
|
|
516
|
+
const shifted = result >> 1;
|
|
517
|
+
return {
|
|
518
|
+
value: isNegative ? -shifted : shifted,
|
|
519
|
+
nextIndex: index
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
if (shift > 30) {
|
|
523
|
+
return null;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return null;
|
|
527
|
+
}
|
|
528
|
+
function decodeMappingsString(mappingsStr) {
|
|
529
|
+
const mappings = [];
|
|
530
|
+
let malformedCount = 0;
|
|
531
|
+
if (!mappingsStr) {
|
|
532
|
+
return { mappings, malformedCount };
|
|
533
|
+
}
|
|
534
|
+
let sourceIndex = 0;
|
|
535
|
+
let originalLine = 0;
|
|
536
|
+
let originalColumn = 0;
|
|
537
|
+
let nameIndex = 0;
|
|
538
|
+
const lines = mappingsStr.split(";");
|
|
539
|
+
for (let genLine = 0; genLine < lines.length; genLine++) {
|
|
540
|
+
const lineStr = lines[genLine];
|
|
541
|
+
if (!lineStr) continue;
|
|
542
|
+
let genCol = 0;
|
|
543
|
+
const segments = lineStr.split(",");
|
|
544
|
+
for (const seg of segments) {
|
|
545
|
+
if (!seg) continue;
|
|
546
|
+
const values = [];
|
|
547
|
+
let idx = 0;
|
|
548
|
+
let valid = true;
|
|
549
|
+
while (idx < seg.length) {
|
|
550
|
+
const decoded = decodeVlq(seg, idx);
|
|
551
|
+
if (!decoded) {
|
|
552
|
+
valid = false;
|
|
553
|
+
break;
|
|
554
|
+
}
|
|
555
|
+
values.push(decoded.value);
|
|
556
|
+
idx = decoded.nextIndex;
|
|
557
|
+
}
|
|
558
|
+
if (!valid || values.length !== 1 && values.length !== 4 && values.length !== 5) {
|
|
559
|
+
malformedCount++;
|
|
560
|
+
continue;
|
|
561
|
+
}
|
|
562
|
+
genCol += values[0];
|
|
563
|
+
if (values.length >= 4) {
|
|
564
|
+
sourceIndex += values[1];
|
|
565
|
+
originalLine += values[2];
|
|
566
|
+
originalColumn += values[3];
|
|
567
|
+
const mapping = {
|
|
568
|
+
generatedLine: genLine,
|
|
569
|
+
generatedColumn: genCol,
|
|
570
|
+
sourceIndex,
|
|
571
|
+
originalLine,
|
|
572
|
+
originalColumn
|
|
573
|
+
};
|
|
574
|
+
if (values.length === 5) {
|
|
575
|
+
nameIndex += values[4];
|
|
576
|
+
mapping.nameIndex = nameIndex;
|
|
577
|
+
}
|
|
578
|
+
mappings.push(mapping);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
return { mappings, malformedCount };
|
|
583
|
+
}
|
|
584
|
+
function resolveSourceWithRoot(rawSource, sourceRoot) {
|
|
585
|
+
if (!sourceRoot || !rawSource || rawSource.startsWith(sourceRoot)) {
|
|
586
|
+
return rawSource;
|
|
587
|
+
}
|
|
588
|
+
return `${sourceRoot.replace(/\/$/, "")}/${rawSource.replace(/^\//, "")}`;
|
|
589
|
+
}
|
|
590
|
+
function flattenSourceMapSectionsWithStats(rawMap) {
|
|
591
|
+
if (!Array.isArray(rawMap.sections)) {
|
|
592
|
+
const normalized = {
|
|
593
|
+
version: rawMap.version ?? 3,
|
|
594
|
+
sources: Array.isArray(rawMap.sources) ? [...rawMap.sources] : [],
|
|
595
|
+
names: Array.isArray(rawMap.names) ? [...rawMap.names] : [],
|
|
596
|
+
mappings: typeof rawMap.mappings === "string" ? rawMap.mappings : ""
|
|
597
|
+
};
|
|
598
|
+
if (rawMap.file !== void 0) normalized.file = rawMap.file;
|
|
599
|
+
if (rawMap.sourceRoot !== void 0) normalized.sourceRoot = rawMap.sourceRoot;
|
|
600
|
+
if (Array.isArray(rawMap.sourcesContent)) {
|
|
601
|
+
normalized.sourcesContent = [...rawMap.sourcesContent];
|
|
602
|
+
}
|
|
603
|
+
const debugId2 = rawMap.debugId ?? rawMap.debug_id;
|
|
604
|
+
if (debugId2) normalized.debugId = debugId2;
|
|
605
|
+
return { map: normalized, malformedCount: 0 };
|
|
606
|
+
}
|
|
607
|
+
const mergedSources = [];
|
|
608
|
+
const mergedSourcesContent = [];
|
|
609
|
+
let hasAnySourcesContent = false;
|
|
610
|
+
const sourceIndexMap = /* @__PURE__ */ new Map();
|
|
611
|
+
const mergedNames = [];
|
|
612
|
+
const nameIndexMap = /* @__PURE__ */ new Map();
|
|
613
|
+
const mergedMappings = [];
|
|
614
|
+
let totalMalformed = 0;
|
|
615
|
+
const internSource = (src, content) => {
|
|
616
|
+
const existing = sourceIndexMap.get(src);
|
|
617
|
+
if (existing !== void 0) {
|
|
618
|
+
if (mergedSourcesContent[existing] === null && content !== void 0 && content !== null) {
|
|
619
|
+
mergedSourcesContent[existing] = content;
|
|
620
|
+
hasAnySourcesContent = true;
|
|
621
|
+
}
|
|
622
|
+
return existing;
|
|
623
|
+
}
|
|
624
|
+
const nextIdx = mergedSources.length;
|
|
625
|
+
mergedSources.push(src);
|
|
626
|
+
if (content !== void 0 && content !== null) {
|
|
627
|
+
hasAnySourcesContent = true;
|
|
628
|
+
}
|
|
629
|
+
mergedSourcesContent.push(content ?? null);
|
|
630
|
+
sourceIndexMap.set(src, nextIdx);
|
|
631
|
+
return nextIdx;
|
|
632
|
+
};
|
|
633
|
+
const internName = (name) => {
|
|
634
|
+
const existing = nameIndexMap.get(name);
|
|
635
|
+
if (existing !== void 0) return existing;
|
|
636
|
+
const nextIdx = mergedNames.length;
|
|
637
|
+
mergedNames.push(name);
|
|
638
|
+
nameIndexMap.set(name, nextIdx);
|
|
639
|
+
return nextIdx;
|
|
640
|
+
};
|
|
641
|
+
for (const section of rawMap.sections) {
|
|
642
|
+
if (!section || !section.map) continue;
|
|
643
|
+
const offLine = (section.offset?.line ?? 0) | 0;
|
|
644
|
+
const offCol = (section.offset?.column ?? 0) | 0;
|
|
645
|
+
const flatSecResult = flattenSourceMapSectionsWithStats(section.map);
|
|
646
|
+
const flatSec = flatSecResult.map;
|
|
647
|
+
totalMalformed += flatSecResult.malformedCount;
|
|
648
|
+
const decoded = decodeMappingsString(flatSec.mappings);
|
|
649
|
+
totalMalformed += decoded.malformedCount;
|
|
650
|
+
const secSources = flatSec.sources ?? [];
|
|
651
|
+
const secContents = flatSec.sourcesContent ?? [];
|
|
652
|
+
const secNames = flatSec.names ?? [];
|
|
653
|
+
const sourceRemap = /* @__PURE__ */ new Map();
|
|
654
|
+
for (let i = 0; i < secSources.length; i++) {
|
|
655
|
+
const fullSource = resolveSourceWithRoot(secSources[i], flatSec.sourceRoot);
|
|
656
|
+
sourceRemap.set(i, internSource(fullSource, secContents[i]));
|
|
657
|
+
}
|
|
658
|
+
const nameRemap = /* @__PURE__ */ new Map();
|
|
659
|
+
for (let i = 0; i < secNames.length; i++) {
|
|
660
|
+
nameRemap.set(i, internName(secNames[i]));
|
|
661
|
+
}
|
|
662
|
+
for (const m of decoded.mappings) {
|
|
663
|
+
const mappedSource = sourceRemap.get(m.sourceIndex);
|
|
664
|
+
if (mappedSource === void 0) continue;
|
|
665
|
+
const shifted = {
|
|
666
|
+
generatedLine: offLine + m.generatedLine,
|
|
667
|
+
generatedColumn: (m.generatedLine === 0 ? offCol : 0) + m.generatedColumn,
|
|
668
|
+
sourceIndex: mappedSource,
|
|
669
|
+
originalLine: m.originalLine,
|
|
670
|
+
originalColumn: m.originalColumn
|
|
671
|
+
};
|
|
672
|
+
if (m.nameIndex !== void 0) {
|
|
673
|
+
const mappedName = nameRemap.get(m.nameIndex);
|
|
674
|
+
if (mappedName !== void 0) {
|
|
675
|
+
shifted.nameIndex = mappedName;
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
mergedMappings.push(shifted);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
mergedMappings.sort(
|
|
682
|
+
(a, b) => a.generatedLine !== b.generatedLine ? a.generatedLine - b.generatedLine : a.generatedColumn - b.generatedColumn
|
|
683
|
+
);
|
|
684
|
+
const flat = {
|
|
685
|
+
version: rawMap.version ?? 3,
|
|
686
|
+
sources: mergedSources,
|
|
687
|
+
names: mergedNames,
|
|
688
|
+
mappings: encodeMappings(mergedMappings)
|
|
689
|
+
};
|
|
690
|
+
if (rawMap.file !== void 0) flat.file = rawMap.file;
|
|
691
|
+
if (rawMap.sourceRoot !== void 0) flat.sourceRoot = rawMap.sourceRoot;
|
|
692
|
+
if (hasAnySourcesContent) flat.sourcesContent = mergedSourcesContent;
|
|
693
|
+
const debugId = rawMap.debugId ?? rawMap.debug_id;
|
|
694
|
+
if (debugId) flat.debugId = debugId;
|
|
695
|
+
return { map: flat, malformedCount: totalMalformed };
|
|
696
|
+
}
|
|
697
|
+
function flattenSourceMapSections(rawMap) {
|
|
698
|
+
return flattenSourceMapSectionsWithStats(rawMap).map;
|
|
699
|
+
}
|
|
700
|
+
function encodeMappings(decoded) {
|
|
701
|
+
if (decoded.length === 0) return "";
|
|
702
|
+
const sorted = [...decoded].sort(
|
|
703
|
+
(a, b) => a.generatedLine !== b.generatedLine ? a.generatedLine - b.generatedLine : a.generatedColumn - b.generatedColumn
|
|
704
|
+
);
|
|
705
|
+
const maxLine = sorted[sorted.length - 1].generatedLine;
|
|
706
|
+
const lineBuckets = Array.from({ length: maxLine + 1 }, () => []);
|
|
707
|
+
let prevSource = 0;
|
|
708
|
+
let prevOrigLine = 0;
|
|
709
|
+
let prevOrigCol = 0;
|
|
710
|
+
let prevName = 0;
|
|
711
|
+
let prevGenLine = -1;
|
|
712
|
+
let prevGenCol = 0;
|
|
713
|
+
for (const m of sorted) {
|
|
714
|
+
if (m.generatedLine !== prevGenLine) {
|
|
715
|
+
prevGenLine = m.generatedLine;
|
|
716
|
+
prevGenCol = 0;
|
|
717
|
+
}
|
|
718
|
+
let seg = encodeVlq(m.generatedColumn - prevGenCol) + encodeVlq(m.sourceIndex - prevSource) + encodeVlq(m.originalLine - prevOrigLine) + encodeVlq(m.originalColumn - prevOrigCol);
|
|
719
|
+
prevGenCol = m.generatedColumn;
|
|
720
|
+
prevSource = m.sourceIndex;
|
|
721
|
+
prevOrigLine = m.originalLine;
|
|
722
|
+
prevOrigCol = m.originalColumn;
|
|
723
|
+
if (m.nameIndex !== void 0) {
|
|
724
|
+
seg += encodeVlq(m.nameIndex - prevName);
|
|
725
|
+
prevName = m.nameIndex;
|
|
726
|
+
}
|
|
727
|
+
lineBuckets[m.generatedLine].push(seg);
|
|
728
|
+
}
|
|
729
|
+
return lineBuckets.map((segs) => segs.join(",")).join(";");
|
|
730
|
+
}
|
|
731
|
+
function ensureSortedMappings(mappings) {
|
|
732
|
+
for (let i = 1; i < mappings.length; i++) {
|
|
733
|
+
const prev = mappings[i - 1];
|
|
734
|
+
const curr = mappings[i];
|
|
735
|
+
if (curr.generatedLine < prev.generatedLine || curr.generatedLine === prev.generatedLine && curr.generatedColumn < prev.generatedColumn) {
|
|
736
|
+
return [...mappings].sort(
|
|
737
|
+
(a, b) => a.generatedLine !== b.generatedLine ? a.generatedLine - b.generatedLine : a.generatedColumn - b.generatedColumn
|
|
738
|
+
);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
return mappings;
|
|
742
|
+
}
|
|
743
|
+
function findMappingAt(mappings, line0, col0) {
|
|
744
|
+
let lo = 0;
|
|
745
|
+
let hi = mappings.length - 1;
|
|
746
|
+
let best;
|
|
747
|
+
while (lo <= hi) {
|
|
748
|
+
const mid = lo + hi >> 1;
|
|
749
|
+
const m = mappings[mid];
|
|
750
|
+
if (m.generatedLine < line0 || m.generatedLine === line0 && m.generatedColumn <= col0) {
|
|
751
|
+
if (m.generatedLine === line0) {
|
|
752
|
+
best = m;
|
|
753
|
+
}
|
|
754
|
+
lo = mid + 1;
|
|
755
|
+
} else {
|
|
756
|
+
hi = mid - 1;
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
return best;
|
|
760
|
+
}
|
|
761
|
+
function shiftSourceMapLines(map, lineOffset) {
|
|
762
|
+
if (lineOffset <= 0 || !map.mappings) {
|
|
763
|
+
return map;
|
|
764
|
+
}
|
|
765
|
+
return {
|
|
766
|
+
...map,
|
|
767
|
+
mappings: `${";".repeat(lineOffset)}${map.mappings}`
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
function computeDeterministicDebugId(code, mappings = "") {
|
|
771
|
+
const hash = crypto.createHash("sha256").update(code, "utf8");
|
|
772
|
+
if (mappings) {
|
|
773
|
+
hash.update("\0", "utf8").update(mappings, "utf8");
|
|
774
|
+
}
|
|
775
|
+
const bytes = hash.digest();
|
|
776
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
777
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
778
|
+
const hex = bytes.subarray(0, 16).toString("hex");
|
|
779
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20, 32)}`;
|
|
780
|
+
}
|
|
781
|
+
function chainSourceMaps(mapB, mapA) {
|
|
782
|
+
const flatB = flattenSourceMapSections(mapB);
|
|
783
|
+
const flatA = flattenSourceMapSections(mapA);
|
|
784
|
+
const decodedB = ensureSortedMappings(decodeMappingsString(flatB.mappings).mappings);
|
|
785
|
+
const decodedA = ensureSortedMappings(decodeMappingsString(flatA.mappings).mappings);
|
|
786
|
+
const chained = [];
|
|
787
|
+
for (const mB of decodedB) {
|
|
788
|
+
const mA = findMappingAt(decodedA, mB.originalLine, mB.originalColumn);
|
|
789
|
+
if (mA) {
|
|
790
|
+
chained.push({
|
|
791
|
+
generatedLine: mB.generatedLine,
|
|
792
|
+
generatedColumn: mB.generatedColumn,
|
|
793
|
+
sourceIndex: mA.sourceIndex,
|
|
794
|
+
originalLine: mA.originalLine,
|
|
795
|
+
originalColumn: mA.originalColumn,
|
|
796
|
+
nameIndex: mA.nameIndex
|
|
797
|
+
});
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
const debugId = flatA.debugId ?? flatA.debug_id ?? flatB.debugId ?? flatB.debug_id;
|
|
801
|
+
const result = {
|
|
802
|
+
version: 3,
|
|
803
|
+
file: flatB.file ?? flatA.file,
|
|
804
|
+
sourceRoot: flatA.sourceRoot,
|
|
805
|
+
sources: [...flatA.sources],
|
|
806
|
+
sourcesContent: flatA.sourcesContent ? [...flatA.sourcesContent] : void 0,
|
|
807
|
+
names: [...flatA.names],
|
|
808
|
+
mappings: encodeMappings(chained)
|
|
809
|
+
};
|
|
810
|
+
if (debugId) {
|
|
811
|
+
result.debugId = debugId;
|
|
812
|
+
}
|
|
813
|
+
return result;
|
|
814
|
+
}
|
|
815
|
+
function resolvePosition(map, line1Based, column1Based) {
|
|
816
|
+
const { map: flatMap, malformedCount: flattenMalformed } = flattenSourceMapSectionsWithStats(map);
|
|
817
|
+
const { mappings, malformedCount: decodeMalformed } = decodeMappingsString(flatMap.mappings);
|
|
818
|
+
const malformedCount = flattenMalformed + decodeMalformed;
|
|
819
|
+
const sortedMappings = ensureSortedMappings(mappings);
|
|
820
|
+
const match = findMappingAt(
|
|
821
|
+
sortedMappings,
|
|
822
|
+
Math.max(0, line1Based - 1),
|
|
823
|
+
Math.max(0, column1Based - 1)
|
|
824
|
+
);
|
|
825
|
+
if (!match) {
|
|
826
|
+
return { malformedCount };
|
|
827
|
+
}
|
|
828
|
+
const rawSource = flatMap.sources[match.sourceIndex];
|
|
829
|
+
const source = resolveSourceWithRoot(rawSource, flatMap.sourceRoot);
|
|
830
|
+
return {
|
|
831
|
+
source,
|
|
832
|
+
line: match.originalLine + 1,
|
|
833
|
+
column: match.originalColumn + 1,
|
|
834
|
+
name: match.nameIndex !== void 0 ? flatMap.names[match.nameIndex] : void 0,
|
|
835
|
+
malformedCount
|
|
836
|
+
};
|
|
837
|
+
}
|
|
838
|
+
function safeDecodeURIComponent(input) {
|
|
839
|
+
try {
|
|
840
|
+
return decodeURIComponent(input);
|
|
841
|
+
} catch {
|
|
842
|
+
return input.replace(/%[0-9A-Fa-f]{2}/g, (seq) => {
|
|
843
|
+
try {
|
|
844
|
+
return decodeURIComponent(seq);
|
|
845
|
+
} catch {
|
|
846
|
+
return seq;
|
|
847
|
+
}
|
|
848
|
+
});
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
function collectMapFiles(mapsDir) {
|
|
852
|
+
if (!fs3.existsSync(mapsDir)) return [];
|
|
853
|
+
const results = [];
|
|
854
|
+
const walk = (dir) => {
|
|
855
|
+
let entries;
|
|
856
|
+
try {
|
|
857
|
+
entries = fs3.readdirSync(dir, { withFileTypes: true });
|
|
858
|
+
} catch {
|
|
859
|
+
return;
|
|
860
|
+
}
|
|
861
|
+
for (const entry of entries) {
|
|
862
|
+
const fullPath = path3.join(dir, entry.name);
|
|
863
|
+
if (entry.isDirectory()) {
|
|
864
|
+
walk(fullPath);
|
|
865
|
+
} else if (entry.isFile() && entry.name.endsWith(".map")) {
|
|
866
|
+
results.push(fullPath);
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
};
|
|
870
|
+
walk(mapsDir);
|
|
871
|
+
return results;
|
|
872
|
+
}
|
|
873
|
+
function normalizeFileRefCandidates(fileRef) {
|
|
874
|
+
let cleanRef = fileRef.trim();
|
|
875
|
+
try {
|
|
876
|
+
if (/^(?:https?|file):\/\//i.test(cleanRef)) {
|
|
877
|
+
cleanRef = new URL(cleanRef).pathname;
|
|
878
|
+
}
|
|
879
|
+
} catch {
|
|
880
|
+
}
|
|
881
|
+
cleanRef = cleanRef.split("?")[0].split("#")[0].replace(/\\/g, "/");
|
|
882
|
+
cleanRef = cleanRef.replace(/^\/+([A-Za-z]:\/)/, "$1").replace(/^\/+/, "");
|
|
883
|
+
const decodedRef = safeDecodeURIComponent(cleanRef).replace(/\\/g, "/").replace(/^\/+/, "");
|
|
884
|
+
const variants = /* @__PURE__ */ new Set();
|
|
885
|
+
for (const raw of [decodedRef, cleanRef]) {
|
|
886
|
+
if (!raw) continue;
|
|
887
|
+
variants.add(raw);
|
|
888
|
+
const withoutDrive = raw.replace(/^[A-Za-z]:\//, "");
|
|
889
|
+
variants.add(withoutDrive);
|
|
890
|
+
for (const v of [raw, withoutDrive]) {
|
|
891
|
+
const nextStripped = v.replace(
|
|
892
|
+
/^(?:.*\/)?(?:_next\/static\/chunks\/|_next\/static\/|_next\/|static\/chunks\/|static\/)/,
|
|
893
|
+
""
|
|
894
|
+
);
|
|
895
|
+
if (nextStripped) variants.add(nextStripped);
|
|
896
|
+
const chunksIdx = v.indexOf("/chunks/");
|
|
897
|
+
if (chunksIdx !== -1) {
|
|
898
|
+
variants.add(v.slice(chunksIdx + "/chunks/".length));
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
return Array.from(variants).filter(Boolean);
|
|
903
|
+
}
|
|
904
|
+
function findMatchingMapFile(fileRef, mapsDir, allMaps) {
|
|
905
|
+
const candidates = normalizeFileRefCandidates(fileRef);
|
|
906
|
+
for (const cand of candidates) {
|
|
907
|
+
const exactCandidate = path3.join(mapsDir, `${cand}.map`);
|
|
908
|
+
if (fs3.existsSync(exactCandidate)) {
|
|
909
|
+
return exactCandidate;
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
for (const mapPath of allMaps) {
|
|
913
|
+
const relRaw = path3.relative(mapsDir, mapPath).replace(/\\/g, "/");
|
|
914
|
+
const relDecoded = safeDecodeURIComponent(relRaw);
|
|
915
|
+
for (const rel of [relDecoded, relRaw]) {
|
|
916
|
+
const relNoMap = rel.slice(0, -4);
|
|
917
|
+
for (const cand of candidates) {
|
|
918
|
+
if (rel === `${cand}.map` || rel.endsWith(`/${cand}.map`) || cand.endsWith(`/${relNoMap}`) || cand === relNoMap) {
|
|
919
|
+
return mapPath;
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
const baseNames = new Set(candidates.map((c) => `${path3.posix.basename(c)}.map`));
|
|
925
|
+
return allMaps.find((m) => {
|
|
926
|
+
const base = path3.basename(m);
|
|
927
|
+
return baseNames.has(base) || baseNames.has(safeDecodeURIComponent(base));
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
var V8_PARENS_FRAME_REGEX = /^\s*at\s+(?:.+?\s+\()(.+):(\d+):(\d+)\)\s*$/;
|
|
931
|
+
var V8_BARE_FRAME_REGEX = /^\s*at\s+(.+?):(\d+):(\d+)\s*$/;
|
|
932
|
+
var FIREFOX_FRAME_REGEX = /^[^@]*@(.+?):(\d+):(\d+)\s*$/;
|
|
933
|
+
var STACK_FRAME_REGEX = /(?:at\s+(?:[^(]+\s+\()?|\@)([^\s()]+?):(\d+):(\d+)\)?/;
|
|
934
|
+
function parseStackFrame(rawLine) {
|
|
935
|
+
const match = rawLine.match(V8_PARENS_FRAME_REGEX) ?? rawLine.match(V8_BARE_FRAME_REGEX) ?? rawLine.match(FIREFOX_FRAME_REGEX) ?? rawLine.match(STACK_FRAME_REGEX);
|
|
936
|
+
if (!match) {
|
|
937
|
+
return null;
|
|
938
|
+
}
|
|
939
|
+
return {
|
|
940
|
+
fileRef: match[1],
|
|
941
|
+
lineNum: Number(match[2]),
|
|
942
|
+
colNum: Number(match[3])
|
|
943
|
+
};
|
|
944
|
+
}
|
|
945
|
+
async function symbolicateStackTrace(options) {
|
|
946
|
+
const cwd = path3.resolve(options.cwd ?? process.cwd());
|
|
947
|
+
const mapsDir = path3.resolve(cwd, options.mapsDir ?? ".unpeek-maps");
|
|
948
|
+
const allMaps = collectMapFiles(mapsDir);
|
|
949
|
+
const warnings = [];
|
|
950
|
+
const mapCache = /* @__PURE__ */ new Map();
|
|
951
|
+
if (allMaps.length === 0) {
|
|
952
|
+
warnings.push(`No .map files found in "${mapsDir}".`);
|
|
953
|
+
}
|
|
954
|
+
const lines = options.stackTrace.split(/\r?\n/);
|
|
955
|
+
const frames = [];
|
|
956
|
+
const outputLines = [];
|
|
957
|
+
for (const rawLine of lines) {
|
|
958
|
+
const parsedFrame = parseStackFrame(rawLine);
|
|
959
|
+
if (!parsedFrame) {
|
|
960
|
+
frames.push({ raw: rawLine, resolved: false });
|
|
961
|
+
outputLines.push(rawLine);
|
|
962
|
+
continue;
|
|
963
|
+
}
|
|
964
|
+
const { fileRef, lineNum, colNum } = parsedFrame;
|
|
965
|
+
const normalizedFrameFile = safeDecodeURIComponent(fileRef).replace(/\\/g, "/");
|
|
966
|
+
const mapPath = findMatchingMapFile(fileRef, mapsDir, allMaps);
|
|
967
|
+
if (!mapPath) {
|
|
968
|
+
frames.push({
|
|
969
|
+
raw: rawLine,
|
|
970
|
+
file: normalizedFrameFile,
|
|
971
|
+
line: lineNum,
|
|
972
|
+
column: colNum,
|
|
973
|
+
resolved: false
|
|
974
|
+
});
|
|
975
|
+
outputLines.push(rawLine);
|
|
976
|
+
continue;
|
|
977
|
+
}
|
|
978
|
+
let parsedMap = mapCache.get(mapPath);
|
|
979
|
+
if (parsedMap === void 0) {
|
|
980
|
+
try {
|
|
981
|
+
const rawJson = JSON.parse(fs3.readFileSync(mapPath, "utf8"));
|
|
982
|
+
parsedMap = Array.isArray(rawJson?.sections) ? flattenSourceMapSections(rawJson) : rawJson;
|
|
983
|
+
} catch {
|
|
984
|
+
parsedMap = null;
|
|
985
|
+
warnings.push(`Failed to parse source map "${mapPath}".`);
|
|
986
|
+
}
|
|
987
|
+
mapCache.set(mapPath, parsedMap);
|
|
988
|
+
}
|
|
989
|
+
if (!parsedMap || typeof parsedMap.mappings !== "string" || !Array.isArray(parsedMap.sources)) {
|
|
990
|
+
frames.push({
|
|
991
|
+
raw: rawLine,
|
|
992
|
+
file: normalizedFrameFile,
|
|
993
|
+
line: lineNum,
|
|
994
|
+
column: colNum,
|
|
995
|
+
resolved: false
|
|
996
|
+
});
|
|
997
|
+
outputLines.push(rawLine);
|
|
998
|
+
continue;
|
|
999
|
+
}
|
|
1000
|
+
const pos = resolvePosition(parsedMap, lineNum, colNum);
|
|
1001
|
+
if (pos.malformedCount > 0) {
|
|
1002
|
+
warnings.push(`Skipped ${pos.malformedCount} malformed VLQ segment(s) in "${path3.basename(mapPath)}".`);
|
|
1003
|
+
}
|
|
1004
|
+
if (!pos.source || pos.line === void 0 || pos.column === void 0) {
|
|
1005
|
+
frames.push({
|
|
1006
|
+
raw: rawLine,
|
|
1007
|
+
file: normalizedFrameFile,
|
|
1008
|
+
line: lineNum,
|
|
1009
|
+
column: colNum,
|
|
1010
|
+
resolved: false
|
|
1011
|
+
});
|
|
1012
|
+
outputLines.push(rawLine);
|
|
1013
|
+
continue;
|
|
1014
|
+
}
|
|
1015
|
+
frames.push({
|
|
1016
|
+
raw: rawLine,
|
|
1017
|
+
file: normalizedFrameFile,
|
|
1018
|
+
line: lineNum,
|
|
1019
|
+
column: colNum,
|
|
1020
|
+
originalSource: pos.source,
|
|
1021
|
+
originalLine: pos.line,
|
|
1022
|
+
originalColumn: pos.column,
|
|
1023
|
+
originalName: pos.name,
|
|
1024
|
+
resolved: true
|
|
1025
|
+
});
|
|
1026
|
+
const fnPrefix = pos.name ? `${pos.name} ` : "";
|
|
1027
|
+
outputLines.push(` at ${fnPrefix}(${pos.source}:${pos.line}:${pos.column})`);
|
|
1028
|
+
}
|
|
1029
|
+
return {
|
|
1030
|
+
stackTrace: outputLines.join("\n"),
|
|
1031
|
+
frames,
|
|
1032
|
+
warnings: Array.from(new Set(warnings))
|
|
1033
|
+
};
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
export {
|
|
1037
|
+
isWorkerOrServiceWorkerFile,
|
|
1038
|
+
hasNextServerSegment,
|
|
1039
|
+
detectTargetFramework,
|
|
1040
|
+
scanClientBundles,
|
|
1041
|
+
SOURCE_MAP_COMMENT_REGEX,
|
|
1042
|
+
extractDebugId,
|
|
1043
|
+
stripDebugIdComments,
|
|
1044
|
+
replaceOrAppendDebugId,
|
|
1045
|
+
stripSourceMapComments,
|
|
1046
|
+
findMonorepoRoots,
|
|
1047
|
+
relocateSourceMaps,
|
|
1048
|
+
flattenSourceMapSections,
|
|
1049
|
+
shiftSourceMapLines,
|
|
1050
|
+
computeDeterministicDebugId,
|
|
1051
|
+
chainSourceMaps,
|
|
1052
|
+
safeDecodeURIComponent,
|
|
1053
|
+
symbolicateStackTrace
|
|
1054
|
+
};
|