@kenkaiiii/gg-pixel 4.3.85 → 4.3.87

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,11 +824,12 @@ 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") : "";
824
- if (existing.includes("@kenkaiiii/gg-pixel")) return;
825
- const newContent = existing ? existing + "\n" + nextInstrumentationAppend(ingestUrl, projectKey) : nextInstrumentationStandalone(ingestUrl, projectKey);
826
- (0, import_node_fs3.writeFileSync)(path, newContent, "utf8");
827
+ const cleaned = stripLegacyPixelContent(existing);
828
+ const block = nextInstrumentationBlock(ingestUrl, projectKey);
829
+ const next = upsertPixelBlock(cleaned, block);
830
+ if (next !== existing) (0, import_node_fs3.writeFileSync)(path, next, "utf8");
827
831
  }
828
- function nextInstrumentationStandalone(ingestUrl, projectKey) {
832
+ function nextInstrumentationBlock(ingestUrl, projectKey) {
829
833
  const fallback = projectKey ? ` ?? ${JSON.stringify(projectKey)}` : "";
830
834
  return `// Next.js auto-loads this file on server start. Pixel hooks the
831
835
  // uncaughtExceptionMonitor + unhandledRejection events for API routes,
@@ -838,20 +842,7 @@ export async function register() {
838
842
  sink: { kind: "http", ingestUrl: ${JSON.stringify(`${ingestUrl}/ingest`)} },
839
843
  });
840
844
  }
841
- }
842
- `;
843
- }
844
- function nextInstrumentationAppend(ingestUrl, projectKey) {
845
- const fallback = projectKey ? ` ?? ${JSON.stringify(projectKey)}` : "";
846
- return `// gg-pixel: server-side error tracking
847
- import { initPixel } from "@kenkaiiii/gg-pixel";
848
- if (typeof process !== "undefined" && process.env.NEXT_RUNTIME === "nodejs") {
849
- initPixel({
850
- projectKey: process.env.GG_PIXEL_KEY${fallback},
851
- sink: { kind: "http", ingestUrl: ${JSON.stringify(`${ingestUrl}/ingest`)} },
852
- });
853
- }
854
- `;
845
+ }`;
855
846
  }
856
847
  function findNextLayout(projectRoot) {
857
848
  const candidates = [
@@ -984,25 +975,21 @@ function wireSveltekit({ projectRoot, projectKey, ingestUrl }) {
984
975
  const serverPath = (0, import_node_path2.join)(projectRoot, "src/hooks.server.ts");
985
976
  const clientPath = (0, import_node_path2.join)(projectRoot, "src/hooks.client.ts");
986
977
  if (!(0, import_node_fs3.existsSync)((0, import_node_path2.dirname)(serverPath))) (0, import_node_fs3.mkdirSync)((0, import_node_path2.dirname)(serverPath), { recursive: true });
987
- appendOrCreate(
978
+ upsertPixelBlockInFile(
988
979
  serverPath,
989
980
  `import { initPixel } from "@kenkaiiii/gg-pixel";
990
981
  initPixel({
991
982
  projectKey: process.env.GG_PIXEL_KEY ?? ${JSON.stringify(projectKey)},
992
983
  sink: { kind: "http", ingestUrl: ${JSON.stringify(`${ingestUrl}/ingest`)} },
993
- });
994
- `,
995
- "@kenkaiiii/gg-pixel"
984
+ });`
996
985
  );
997
- appendOrCreate(
986
+ upsertPixelBlockInFile(
998
987
  clientPath,
999
988
  `import { initPixel } from "@kenkaiiii/gg-pixel/browser";
1000
989
  initPixel({
1001
990
  projectKey: ${JSON.stringify(projectKey)},
1002
991
  ingestUrl: ${JSON.stringify(ingestUrl)},
1003
- });
1004
- `,
1005
- "@kenkaiiii/gg-pixel/browser"
992
+ });`
1006
993
  );
1007
994
  return {
1008
995
  primaryInitPath: clientPath,
@@ -1344,14 +1331,88 @@ function pickPath(root, candidates) {
1344
1331
  }
1345
1332
  return null;
1346
1333
  }
1347
- function appendOrCreate(filePath, snippet, marker) {
1348
- if ((0, import_node_fs3.existsSync)(filePath)) {
1349
- const existing = (0, import_node_fs3.readFileSync)(filePath, "utf8");
1350
- if (existing.includes(marker)) return;
1351
- (0, import_node_fs3.writeFileSync)(filePath, existing + "\n" + snippet, "utf8");
1352
- return;
1334
+ var PIXEL_MARK_BEGIN = "// >>> gg-pixel auto-generated \u2014 do not edit between these markers <<<";
1335
+ var PIXEL_MARK_END = "// >>> /gg-pixel <<<";
1336
+ function wrapPixelBlock(content) {
1337
+ return `${PIXEL_MARK_BEGIN}
1338
+ ${content.replace(/\s+$/, "")}
1339
+ ${PIXEL_MARK_END}
1340
+ `;
1341
+ }
1342
+ function upsertPixelBlock(existing, block) {
1343
+ const wrapped = wrapPixelBlock(block);
1344
+ const beginIdx = existing.indexOf(PIXEL_MARK_BEGIN);
1345
+ if (beginIdx !== -1) {
1346
+ const endIdx = existing.indexOf(PIXEL_MARK_END, beginIdx);
1347
+ if (endIdx !== -1) {
1348
+ const after = endIdx + PIXEL_MARK_END.length;
1349
+ const trailNL = existing[after] === "\n" ? 1 : 0;
1350
+ return existing.slice(0, beginIdx) + wrapped + existing.slice(after + trailNL);
1351
+ }
1353
1352
  }
1354
- (0, import_node_fs3.writeFileSync)(filePath, snippet, "utf8");
1353
+ if (existing.length === 0) return wrapped;
1354
+ const sep2 = existing.endsWith("\n") ? "" : "\n";
1355
+ return existing + sep2 + "\n" + wrapped;
1356
+ }
1357
+ function upsertPixelBlockInFile(filePath, block) {
1358
+ const existing = (0, import_node_fs3.existsSync)(filePath) ? (0, import_node_fs3.readFileSync)(filePath, "utf8") : "";
1359
+ const cleaned = stripLegacyPixelContent(existing);
1360
+ const next = upsertPixelBlock(cleaned, block);
1361
+ if (next !== existing) (0, import_node_fs3.writeFileSync)(filePath, next, "utf8");
1362
+ }
1363
+ function stripLegacyPixelContent(content) {
1364
+ if (!content.includes("@kenkaiiii/gg-pixel")) return content;
1365
+ const beginIdx = content.indexOf(PIXEL_MARK_BEGIN);
1366
+ const endIdx = beginIdx === -1 ? -1 : content.indexOf(PIXEL_MARK_END, beginIdx);
1367
+ const insideMarkers = (start, end) => beginIdx !== -1 && endIdx !== -1 && start >= beginIdx && end <= endIdx + PIXEL_MARK_END.length;
1368
+ const ranges = [];
1369
+ const registerRe = /(?:^|\n)((?:[ \t]*\/\/[^\n]*\n)*)[ \t]*export\s+async\s+function\s+register\s*\(\s*\)\s*\{/g;
1370
+ let m;
1371
+ while ((m = registerRe.exec(content)) !== null) {
1372
+ const blockStart = m.index + (content[m.index] === "\n" ? 1 : 0);
1373
+ const openBraceIdx = m.index + m[0].length - 1;
1374
+ let depth = 1;
1375
+ let i = openBraceIdx + 1;
1376
+ while (i < content.length && depth > 0) {
1377
+ const ch = content[i];
1378
+ if (ch === "{") depth++;
1379
+ else if (ch === "}") depth--;
1380
+ i++;
1381
+ }
1382
+ if (depth !== 0) continue;
1383
+ const blockEnd = i;
1384
+ const blockText = content.slice(blockStart, blockEnd);
1385
+ if (!blockText.includes("@kenkaiiii/gg-pixel")) continue;
1386
+ if (insideMarkers(blockStart, blockEnd)) continue;
1387
+ const trailingNL = content[blockEnd] === "\n" ? 1 : 0;
1388
+ ranges.push({ start: blockStart, end: blockEnd + trailingNL });
1389
+ }
1390
+ const importRe = /(?:^|\n)((?:[ \t]*\/\/[^\n]*\n)*)[ \t]*import\s*\{\s*initPixel[^}]*\}\s*from\s*"@kenkaiiii\/gg-pixel(?:\/[\w-]+)?"\s*;?\s*\n/g;
1391
+ while ((m = importRe.exec(content)) !== null) {
1392
+ const blockStart = m.index + (content[m.index] === "\n" ? 1 : 0);
1393
+ const callIdx = content.indexOf("initPixel(", importRe.lastIndex);
1394
+ if (callIdx === -1 || callIdx - importRe.lastIndex > 2048) continue;
1395
+ const openParen = content.indexOf("(", callIdx);
1396
+ let depth = 1;
1397
+ let i = openParen + 1;
1398
+ while (i < content.length && depth > 0) {
1399
+ const ch = content[i];
1400
+ if (ch === "(" || ch === "{") depth++;
1401
+ else if (ch === ")" || ch === "}") depth--;
1402
+ i++;
1403
+ }
1404
+ if (depth !== 0) continue;
1405
+ while (i < content.length && (content[i] === ";" || content[i] === " ")) i++;
1406
+ const trailingNL = content[i] === "\n" ? 1 : 0;
1407
+ const blockEnd = i + trailingNL;
1408
+ if (insideMarkers(blockStart, blockEnd)) continue;
1409
+ ranges.push({ start: blockStart, end: blockEnd });
1410
+ }
1411
+ if (ranges.length === 0) return content;
1412
+ ranges.sort((a, b) => b.start - a.start);
1413
+ let out = content;
1414
+ for (const r of ranges) out = out.slice(0, r.start) + out.slice(r.end);
1415
+ return out.replace(/\n{3,}/g, "\n\n");
1355
1416
  }
1356
1417
  function injectImport(entryPath, initFilePath) {
1357
1418
  let content;