@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.js CHANGED
@@ -501,10 +501,13 @@ function findMappingByPath(projectsJsonPath, projectRoot) {
501
501
  } catch {
502
502
  return null;
503
503
  }
504
+ let fallback = null;
504
505
  for (const [id, entry] of Object.entries(map)) {
505
- if (entry.path === projectRoot) return { id, ...entry };
506
+ if (entry.path !== projectRoot) continue;
507
+ if (entry.secret) return { id, ...entry };
508
+ if (!fallback) fallback = { id, ...entry };
506
509
  }
507
- return null;
510
+ return fallback;
508
511
  }
509
512
  function readEnvKey(envPath, key) {
510
513
  if (!existsSync2(envPath)) return null;
@@ -787,8 +790,9 @@ function wireNextjs({ projectRoot, projectKey, ingestUrl }) {
787
790
  }
788
791
  function writeNextInstrumentation(path, ingestUrl, projectKey) {
789
792
  const existing = existsSync2(path) ? readFileSync2(path, "utf8") : "";
793
+ const cleaned = stripLegacyPixelContent(existing);
790
794
  const block = nextInstrumentationBlock(ingestUrl, projectKey);
791
- const next = upsertPixelBlock(existing, block);
795
+ const next = upsertPixelBlock(cleaned, block);
792
796
  if (next !== existing) writeFileSync(path, next, "utf8");
793
797
  }
794
798
  function nextInstrumentationBlock(ingestUrl, projectKey) {
@@ -1054,13 +1058,12 @@ function wireElectron({ projectRoot, pkg, projectKey, ingestUrl }) {
1054
1058
  "Could not copy gg-pixel browser IIFE bundle \u2014 install @kenkaiiii/gg-pixel and re-run."
1055
1059
  );
1056
1060
  }
1057
- let patchedAny = false;
1061
+ let wiredAny = false;
1058
1062
  for (const html of htmlFiles) {
1059
- if (patchRendererHtml(html, rendererInitPath, projectKey, ingestUrl) === "patched") {
1060
- patchedAny = true;
1061
- }
1063
+ const r = patchRendererHtml(html, rendererInitPath, projectKey, ingestUrl);
1064
+ if (r === "patched" || r === "already") wiredAny = true;
1062
1065
  }
1063
- if (!patchedAny) {
1066
+ if (!wiredAny) {
1064
1067
  warnings.push(
1065
1068
  `Found HTML files in ${rendererDir} but couldn't patch any \u2014 they may have unusual CSP or no <head>.`
1066
1069
  );
@@ -1179,6 +1182,7 @@ function copyIifeBundle(projectRoot, dest) {
1179
1182
  }
1180
1183
  return false;
1181
1184
  }
1185
+ var PIXEL_HTML_MARKER = "<!-- gg-pixel: auto-wired by ggcoder pixel install -->";
1182
1186
  function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
1183
1187
  let content;
1184
1188
  try {
@@ -1186,7 +1190,20 @@ function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
1186
1190
  } catch {
1187
1191
  return "not-applicable";
1188
1192
  }
1189
- if (content.includes("gg-pixel.browser.iife")) return "already";
1193
+ const original = content;
1194
+ const markerIdx = content.indexOf(PIXEL_HTML_MARKER);
1195
+ if (markerIdx !== -1) {
1196
+ const firstScriptEnd = content.indexOf("</script>", markerIdx);
1197
+ const secondScriptEnd = firstScriptEnd !== -1 ? content.indexOf("</script>", firstScriptEnd + "</script>".length) : -1;
1198
+ if (secondScriptEnd !== -1) {
1199
+ let stripEnd = secondScriptEnd + "</script>".length;
1200
+ while (stripEnd < content.length && /\s/.test(content[stripEnd])) stripEnd++;
1201
+ let stripStart = markerIdx;
1202
+ while (stripStart > 0 && /[ \t]/.test(content[stripStart - 1])) stripStart--;
1203
+ if (stripStart > 0 && content[stripStart - 1] === "\n") stripStart--;
1204
+ content = content.slice(0, stripStart) + content.slice(stripEnd);
1205
+ }
1206
+ }
1190
1207
  const ingestOrigin = new URL(ingestUrl).origin;
1191
1208
  content = content.replace(
1192
1209
  /(<meta[^>]+http-equiv=["']?content-security-policy["']?[^>]*content=)("([^"]+)"|'([^']+)')/i,
@@ -1206,7 +1223,7 @@ function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
1206
1223
  );
1207
1224
  const relScript = relative(dirname2(htmlPath), iifePath).split(sep).join("/");
1208
1225
  const inject = `
1209
- <!-- gg-pixel: auto-wired by ggcoder pixel install -->
1226
+ ${PIXEL_HTML_MARKER}
1210
1227
  <script src="${relScript}"></script>
1211
1228
  <script>
1212
1229
  if (window.GGPixel) GGPixel.initPixel({ projectKey: ${JSON.stringify(projectKey)}, ingestUrl: ${JSON.stringify(ingestUrl)} });
@@ -1219,6 +1236,7 @@ function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
1219
1236
  } else {
1220
1237
  return "not-applicable";
1221
1238
  }
1239
+ if (content === original) return "already";
1222
1240
  writeFileSync(htmlPath, content, "utf8");
1223
1241
  return "patched";
1224
1242
  }
@@ -1318,9 +1336,64 @@ function upsertPixelBlock(existing, block) {
1318
1336
  }
1319
1337
  function upsertPixelBlockInFile(filePath, block) {
1320
1338
  const existing = existsSync2(filePath) ? readFileSync2(filePath, "utf8") : "";
1321
- const next = upsertPixelBlock(existing, block);
1339
+ const cleaned = stripLegacyPixelContent(existing);
1340
+ const next = upsertPixelBlock(cleaned, block);
1322
1341
  if (next !== existing) writeFileSync(filePath, next, "utf8");
1323
1342
  }
1343
+ function stripLegacyPixelContent(content) {
1344
+ if (!content.includes("@kenkaiiii/gg-pixel")) return content;
1345
+ const beginIdx = content.indexOf(PIXEL_MARK_BEGIN);
1346
+ const endIdx = beginIdx === -1 ? -1 : content.indexOf(PIXEL_MARK_END, beginIdx);
1347
+ const insideMarkers = (start, end) => beginIdx !== -1 && endIdx !== -1 && start >= beginIdx && end <= endIdx + PIXEL_MARK_END.length;
1348
+ const ranges = [];
1349
+ const registerRe = /(?:^|\n)((?:[ \t]*\/\/[^\n]*\n)*)[ \t]*export\s+async\s+function\s+register\s*\(\s*\)\s*\{/g;
1350
+ let m;
1351
+ while ((m = registerRe.exec(content)) !== null) {
1352
+ const blockStart = m.index + (content[m.index] === "\n" ? 1 : 0);
1353
+ const openBraceIdx = m.index + m[0].length - 1;
1354
+ let depth = 1;
1355
+ let i = openBraceIdx + 1;
1356
+ while (i < content.length && depth > 0) {
1357
+ const ch = content[i];
1358
+ if (ch === "{") depth++;
1359
+ else if (ch === "}") depth--;
1360
+ i++;
1361
+ }
1362
+ if (depth !== 0) continue;
1363
+ const blockEnd = i;
1364
+ const blockText = content.slice(blockStart, blockEnd);
1365
+ if (!blockText.includes("@kenkaiiii/gg-pixel")) continue;
1366
+ if (insideMarkers(blockStart, blockEnd)) continue;
1367
+ const trailingNL = content[blockEnd] === "\n" ? 1 : 0;
1368
+ ranges.push({ start: blockStart, end: blockEnd + trailingNL });
1369
+ }
1370
+ const importRe = /(?:^|\n)((?:[ \t]*\/\/[^\n]*\n)*)[ \t]*import\s*\{\s*initPixel[^}]*\}\s*from\s*"@kenkaiiii\/gg-pixel(?:\/[\w-]+)?"\s*;?\s*\n/g;
1371
+ while ((m = importRe.exec(content)) !== null) {
1372
+ const blockStart = m.index + (content[m.index] === "\n" ? 1 : 0);
1373
+ const callIdx = content.indexOf("initPixel(", importRe.lastIndex);
1374
+ if (callIdx === -1 || callIdx - importRe.lastIndex > 2048) continue;
1375
+ const openParen = content.indexOf("(", callIdx);
1376
+ let depth = 1;
1377
+ let i = openParen + 1;
1378
+ while (i < content.length && depth > 0) {
1379
+ const ch = content[i];
1380
+ if (ch === "(" || ch === "{") depth++;
1381
+ else if (ch === ")" || ch === "}") depth--;
1382
+ i++;
1383
+ }
1384
+ if (depth !== 0) continue;
1385
+ while (i < content.length && (content[i] === ";" || content[i] === " ")) i++;
1386
+ const trailingNL = content[i] === "\n" ? 1 : 0;
1387
+ const blockEnd = i + trailingNL;
1388
+ if (insideMarkers(blockStart, blockEnd)) continue;
1389
+ ranges.push({ start: blockStart, end: blockEnd });
1390
+ }
1391
+ if (ranges.length === 0) return content;
1392
+ ranges.sort((a, b) => b.start - a.start);
1393
+ let out = content;
1394
+ for (const r of ranges) out = out.slice(0, r.start) + out.slice(r.end);
1395
+ return out.replace(/\n{3,}/g, "\n\n");
1396
+ }
1324
1397
  function injectImport(entryPath, initFilePath) {
1325
1398
  let content;
1326
1399
  try {