@kenkaiiii/gg-pixel 4.3.86 → 4.3.88

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/dist/index.cjs CHANGED
@@ -535,10 +535,13 @@ function findMappingByPath(projectsJsonPath, projectRoot) {
535
535
  } catch {
536
536
  return null;
537
537
  }
538
+ let fallback = null;
538
539
  for (const [id, entry] of Object.entries(map)) {
539
- if (entry.path === projectRoot) return { id, ...entry };
540
+ if (entry.path !== projectRoot) continue;
541
+ if (entry.secret) return { id, ...entry };
542
+ if (!fallback) fallback = { id, ...entry };
540
543
  }
541
- return null;
544
+ return fallback;
542
545
  }
543
546
  function readEnvKey(envPath, key) {
544
547
  if (!(0, import_node_fs3.existsSync)(envPath)) return null;
@@ -821,8 +824,9 @@ function wireNextjs({ projectRoot, projectKey, ingestUrl }) {
821
824
  }
822
825
  function writeNextInstrumentation(path, ingestUrl, projectKey) {
823
826
  const existing = (0, import_node_fs3.existsSync)(path) ? (0, import_node_fs3.readFileSync)(path, "utf8") : "";
827
+ const cleaned = stripLegacyPixelContent(existing);
824
828
  const block = nextInstrumentationBlock(ingestUrl, projectKey);
825
- const next = upsertPixelBlock(existing, block);
829
+ const next = upsertPixelBlock(cleaned, block);
826
830
  if (next !== existing) (0, import_node_fs3.writeFileSync)(path, next, "utf8");
827
831
  }
828
832
  function nextInstrumentationBlock(ingestUrl, projectKey) {
@@ -1088,13 +1092,12 @@ function wireElectron({ projectRoot, pkg, projectKey, ingestUrl }) {
1088
1092
  "Could not copy gg-pixel browser IIFE bundle \u2014 install @kenkaiiii/gg-pixel and re-run."
1089
1093
  );
1090
1094
  }
1091
- let patchedAny = false;
1095
+ let wiredAny = false;
1092
1096
  for (const html of htmlFiles) {
1093
- if (patchRendererHtml(html, rendererInitPath, projectKey, ingestUrl) === "patched") {
1094
- patchedAny = true;
1095
- }
1097
+ const r = patchRendererHtml(html, rendererInitPath, projectKey, ingestUrl);
1098
+ if (r === "patched" || r === "already") wiredAny = true;
1096
1099
  }
1097
- if (!patchedAny) {
1100
+ if (!wiredAny) {
1098
1101
  warnings.push(
1099
1102
  `Found HTML files in ${rendererDir} but couldn't patch any \u2014 they may have unusual CSP or no <head>.`
1100
1103
  );
@@ -1213,6 +1216,7 @@ function copyIifeBundle(projectRoot, dest) {
1213
1216
  }
1214
1217
  return false;
1215
1218
  }
1219
+ var PIXEL_HTML_MARKER = "<!-- gg-pixel: auto-wired by ggcoder pixel install -->";
1216
1220
  function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
1217
1221
  let content;
1218
1222
  try {
@@ -1220,7 +1224,20 @@ function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
1220
1224
  } catch {
1221
1225
  return "not-applicable";
1222
1226
  }
1223
- if (content.includes("gg-pixel.browser.iife")) return "already";
1227
+ const original = content;
1228
+ const markerIdx = content.indexOf(PIXEL_HTML_MARKER);
1229
+ if (markerIdx !== -1) {
1230
+ const firstScriptEnd = content.indexOf("</script>", markerIdx);
1231
+ const secondScriptEnd = firstScriptEnd !== -1 ? content.indexOf("</script>", firstScriptEnd + "</script>".length) : -1;
1232
+ if (secondScriptEnd !== -1) {
1233
+ let stripEnd = secondScriptEnd + "</script>".length;
1234
+ while (stripEnd < content.length && /\s/.test(content[stripEnd])) stripEnd++;
1235
+ let stripStart = markerIdx;
1236
+ while (stripStart > 0 && /[ \t]/.test(content[stripStart - 1])) stripStart--;
1237
+ if (stripStart > 0 && content[stripStart - 1] === "\n") stripStart--;
1238
+ content = content.slice(0, stripStart) + content.slice(stripEnd);
1239
+ }
1240
+ }
1224
1241
  const ingestOrigin = new URL(ingestUrl).origin;
1225
1242
  content = content.replace(
1226
1243
  /(<meta[^>]+http-equiv=["']?content-security-policy["']?[^>]*content=)("([^"]+)"|'([^']+)')/i,
@@ -1240,7 +1257,7 @@ function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
1240
1257
  );
1241
1258
  const relScript = (0, import_node_path2.relative)((0, import_node_path2.dirname)(htmlPath), iifePath).split(import_node_path2.sep).join("/");
1242
1259
  const inject = `
1243
- <!-- gg-pixel: auto-wired by ggcoder pixel install -->
1260
+ ${PIXEL_HTML_MARKER}
1244
1261
  <script src="${relScript}"></script>
1245
1262
  <script>
1246
1263
  if (window.GGPixel) GGPixel.initPixel({ projectKey: ${JSON.stringify(projectKey)}, ingestUrl: ${JSON.stringify(ingestUrl)} });
@@ -1253,6 +1270,7 @@ function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
1253
1270
  } else {
1254
1271
  return "not-applicable";
1255
1272
  }
1273
+ if (content === original) return "already";
1256
1274
  (0, import_node_fs3.writeFileSync)(htmlPath, content, "utf8");
1257
1275
  return "patched";
1258
1276
  }
@@ -1352,9 +1370,64 @@ function upsertPixelBlock(existing, block) {
1352
1370
  }
1353
1371
  function upsertPixelBlockInFile(filePath, block) {
1354
1372
  const existing = (0, import_node_fs3.existsSync)(filePath) ? (0, import_node_fs3.readFileSync)(filePath, "utf8") : "";
1355
- const next = upsertPixelBlock(existing, block);
1373
+ const cleaned = stripLegacyPixelContent(existing);
1374
+ const next = upsertPixelBlock(cleaned, block);
1356
1375
  if (next !== existing) (0, import_node_fs3.writeFileSync)(filePath, next, "utf8");
1357
1376
  }
1377
+ function stripLegacyPixelContent(content) {
1378
+ if (!content.includes("@kenkaiiii/gg-pixel")) return content;
1379
+ const beginIdx = content.indexOf(PIXEL_MARK_BEGIN);
1380
+ const endIdx = beginIdx === -1 ? -1 : content.indexOf(PIXEL_MARK_END, beginIdx);
1381
+ const insideMarkers = (start, end) => beginIdx !== -1 && endIdx !== -1 && start >= beginIdx && end <= endIdx + PIXEL_MARK_END.length;
1382
+ const ranges = [];
1383
+ const registerRe = /(?:^|\n)((?:[ \t]*\/\/[^\n]*\n)*)[ \t]*export\s+async\s+function\s+register\s*\(\s*\)\s*\{/g;
1384
+ let m;
1385
+ while ((m = registerRe.exec(content)) !== null) {
1386
+ const blockStart = m.index + (content[m.index] === "\n" ? 1 : 0);
1387
+ const openBraceIdx = m.index + m[0].length - 1;
1388
+ let depth = 1;
1389
+ let i = openBraceIdx + 1;
1390
+ while (i < content.length && depth > 0) {
1391
+ const ch = content[i];
1392
+ if (ch === "{") depth++;
1393
+ else if (ch === "}") depth--;
1394
+ i++;
1395
+ }
1396
+ if (depth !== 0) continue;
1397
+ const blockEnd = i;
1398
+ const blockText = content.slice(blockStart, blockEnd);
1399
+ if (!blockText.includes("@kenkaiiii/gg-pixel")) continue;
1400
+ if (insideMarkers(blockStart, blockEnd)) continue;
1401
+ const trailingNL = content[blockEnd] === "\n" ? 1 : 0;
1402
+ ranges.push({ start: blockStart, end: blockEnd + trailingNL });
1403
+ }
1404
+ const importRe = /(?:^|\n)((?:[ \t]*\/\/[^\n]*\n)*)[ \t]*import\s*\{\s*initPixel[^}]*\}\s*from\s*"@kenkaiiii\/gg-pixel(?:\/[\w-]+)?"\s*;?\s*\n/g;
1405
+ while ((m = importRe.exec(content)) !== null) {
1406
+ const blockStart = m.index + (content[m.index] === "\n" ? 1 : 0);
1407
+ const callIdx = content.indexOf("initPixel(", importRe.lastIndex);
1408
+ if (callIdx === -1 || callIdx - importRe.lastIndex > 2048) continue;
1409
+ const openParen = content.indexOf("(", callIdx);
1410
+ let depth = 1;
1411
+ let i = openParen + 1;
1412
+ while (i < content.length && depth > 0) {
1413
+ const ch = content[i];
1414
+ if (ch === "(" || ch === "{") depth++;
1415
+ else if (ch === ")" || ch === "}") depth--;
1416
+ i++;
1417
+ }
1418
+ if (depth !== 0) continue;
1419
+ while (i < content.length && (content[i] === ";" || content[i] === " ")) i++;
1420
+ const trailingNL = content[i] === "\n" ? 1 : 0;
1421
+ const blockEnd = i + trailingNL;
1422
+ if (insideMarkers(blockStart, blockEnd)) continue;
1423
+ ranges.push({ start: blockStart, end: blockEnd });
1424
+ }
1425
+ if (ranges.length === 0) return content;
1426
+ ranges.sort((a, b) => b.start - a.start);
1427
+ let out = content;
1428
+ for (const r of ranges) out = out.slice(0, r.start) + out.slice(r.end);
1429
+ return out.replace(/\n{3,}/g, "\n\n");
1430
+ }
1358
1431
  function injectImport(entryPath, initFilePath) {
1359
1432
  let content;
1360
1433
  try {