@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.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,11 +790,12 @@ function wireNextjs({ projectRoot, projectKey, ingestUrl }) {
787
790
  }
788
791
  function writeNextInstrumentation(path, ingestUrl, projectKey) {
789
792
  const existing = existsSync2(path) ? readFileSync2(path, "utf8") : "";
790
- if (existing.includes("@kenkaiiii/gg-pixel")) return;
791
- const newContent = existing ? existing + "\n" + nextInstrumentationAppend(ingestUrl, projectKey) : nextInstrumentationStandalone(ingestUrl, projectKey);
792
- writeFileSync(path, newContent, "utf8");
793
+ const cleaned = stripLegacyPixelContent(existing);
794
+ const block = nextInstrumentationBlock(ingestUrl, projectKey);
795
+ const next = upsertPixelBlock(cleaned, block);
796
+ if (next !== existing) writeFileSync(path, next, "utf8");
793
797
  }
794
- function nextInstrumentationStandalone(ingestUrl, projectKey) {
798
+ function nextInstrumentationBlock(ingestUrl, projectKey) {
795
799
  const fallback = projectKey ? ` ?? ${JSON.stringify(projectKey)}` : "";
796
800
  return `// Next.js auto-loads this file on server start. Pixel hooks the
797
801
  // uncaughtExceptionMonitor + unhandledRejection events for API routes,
@@ -804,20 +808,7 @@ export async function register() {
804
808
  sink: { kind: "http", ingestUrl: ${JSON.stringify(`${ingestUrl}/ingest`)} },
805
809
  });
806
810
  }
807
- }
808
- `;
809
- }
810
- function nextInstrumentationAppend(ingestUrl, projectKey) {
811
- const fallback = projectKey ? ` ?? ${JSON.stringify(projectKey)}` : "";
812
- return `// gg-pixel: server-side error tracking
813
- import { initPixel } from "@kenkaiiii/gg-pixel";
814
- if (typeof process !== "undefined" && process.env.NEXT_RUNTIME === "nodejs") {
815
- initPixel({
816
- projectKey: process.env.GG_PIXEL_KEY${fallback},
817
- sink: { kind: "http", ingestUrl: ${JSON.stringify(`${ingestUrl}/ingest`)} },
818
- });
819
- }
820
- `;
811
+ }`;
821
812
  }
822
813
  function findNextLayout(projectRoot) {
823
814
  const candidates = [
@@ -950,25 +941,21 @@ function wireSveltekit({ projectRoot, projectKey, ingestUrl }) {
950
941
  const serverPath = join2(projectRoot, "src/hooks.server.ts");
951
942
  const clientPath = join2(projectRoot, "src/hooks.client.ts");
952
943
  if (!existsSync2(dirname2(serverPath))) mkdirSync2(dirname2(serverPath), { recursive: true });
953
- appendOrCreate(
944
+ upsertPixelBlockInFile(
954
945
  serverPath,
955
946
  `import { initPixel } from "@kenkaiiii/gg-pixel";
956
947
  initPixel({
957
948
  projectKey: process.env.GG_PIXEL_KEY ?? ${JSON.stringify(projectKey)},
958
949
  sink: { kind: "http", ingestUrl: ${JSON.stringify(`${ingestUrl}/ingest`)} },
959
- });
960
- `,
961
- "@kenkaiiii/gg-pixel"
950
+ });`
962
951
  );
963
- appendOrCreate(
952
+ upsertPixelBlockInFile(
964
953
  clientPath,
965
954
  `import { initPixel } from "@kenkaiiii/gg-pixel/browser";
966
955
  initPixel({
967
956
  projectKey: ${JSON.stringify(projectKey)},
968
957
  ingestUrl: ${JSON.stringify(ingestUrl)},
969
- });
970
- `,
971
- "@kenkaiiii/gg-pixel/browser"
958
+ });`
972
959
  );
973
960
  return {
974
961
  primaryInitPath: clientPath,
@@ -1310,14 +1297,88 @@ function pickPath(root, candidates) {
1310
1297
  }
1311
1298
  return null;
1312
1299
  }
1313
- function appendOrCreate(filePath, snippet, marker) {
1314
- if (existsSync2(filePath)) {
1315
- const existing = readFileSync2(filePath, "utf8");
1316
- if (existing.includes(marker)) return;
1317
- writeFileSync(filePath, existing + "\n" + snippet, "utf8");
1318
- return;
1300
+ var PIXEL_MARK_BEGIN = "// >>> gg-pixel auto-generated \u2014 do not edit between these markers <<<";
1301
+ var PIXEL_MARK_END = "// >>> /gg-pixel <<<";
1302
+ function wrapPixelBlock(content) {
1303
+ return `${PIXEL_MARK_BEGIN}
1304
+ ${content.replace(/\s+$/, "")}
1305
+ ${PIXEL_MARK_END}
1306
+ `;
1307
+ }
1308
+ function upsertPixelBlock(existing, block) {
1309
+ const wrapped = wrapPixelBlock(block);
1310
+ const beginIdx = existing.indexOf(PIXEL_MARK_BEGIN);
1311
+ if (beginIdx !== -1) {
1312
+ const endIdx = existing.indexOf(PIXEL_MARK_END, beginIdx);
1313
+ if (endIdx !== -1) {
1314
+ const after = endIdx + PIXEL_MARK_END.length;
1315
+ const trailNL = existing[after] === "\n" ? 1 : 0;
1316
+ return existing.slice(0, beginIdx) + wrapped + existing.slice(after + trailNL);
1317
+ }
1319
1318
  }
1320
- writeFileSync(filePath, snippet, "utf8");
1319
+ if (existing.length === 0) return wrapped;
1320
+ const sep2 = existing.endsWith("\n") ? "" : "\n";
1321
+ return existing + sep2 + "\n" + wrapped;
1322
+ }
1323
+ function upsertPixelBlockInFile(filePath, block) {
1324
+ const existing = existsSync2(filePath) ? readFileSync2(filePath, "utf8") : "";
1325
+ const cleaned = stripLegacyPixelContent(existing);
1326
+ const next = upsertPixelBlock(cleaned, block);
1327
+ if (next !== existing) writeFileSync(filePath, next, "utf8");
1328
+ }
1329
+ function stripLegacyPixelContent(content) {
1330
+ if (!content.includes("@kenkaiiii/gg-pixel")) return content;
1331
+ const beginIdx = content.indexOf(PIXEL_MARK_BEGIN);
1332
+ const endIdx = beginIdx === -1 ? -1 : content.indexOf(PIXEL_MARK_END, beginIdx);
1333
+ const insideMarkers = (start, end) => beginIdx !== -1 && endIdx !== -1 && start >= beginIdx && end <= endIdx + PIXEL_MARK_END.length;
1334
+ const ranges = [];
1335
+ const registerRe = /(?:^|\n)((?:[ \t]*\/\/[^\n]*\n)*)[ \t]*export\s+async\s+function\s+register\s*\(\s*\)\s*\{/g;
1336
+ let m;
1337
+ while ((m = registerRe.exec(content)) !== null) {
1338
+ const blockStart = m.index + (content[m.index] === "\n" ? 1 : 0);
1339
+ const openBraceIdx = m.index + m[0].length - 1;
1340
+ let depth = 1;
1341
+ let i = openBraceIdx + 1;
1342
+ while (i < content.length && depth > 0) {
1343
+ const ch = content[i];
1344
+ if (ch === "{") depth++;
1345
+ else if (ch === "}") depth--;
1346
+ i++;
1347
+ }
1348
+ if (depth !== 0) continue;
1349
+ const blockEnd = i;
1350
+ const blockText = content.slice(blockStart, blockEnd);
1351
+ if (!blockText.includes("@kenkaiiii/gg-pixel")) continue;
1352
+ if (insideMarkers(blockStart, blockEnd)) continue;
1353
+ const trailingNL = content[blockEnd] === "\n" ? 1 : 0;
1354
+ ranges.push({ start: blockStart, end: blockEnd + trailingNL });
1355
+ }
1356
+ const importRe = /(?:^|\n)((?:[ \t]*\/\/[^\n]*\n)*)[ \t]*import\s*\{\s*initPixel[^}]*\}\s*from\s*"@kenkaiiii\/gg-pixel(?:\/[\w-]+)?"\s*;?\s*\n/g;
1357
+ while ((m = importRe.exec(content)) !== null) {
1358
+ const blockStart = m.index + (content[m.index] === "\n" ? 1 : 0);
1359
+ const callIdx = content.indexOf("initPixel(", importRe.lastIndex);
1360
+ if (callIdx === -1 || callIdx - importRe.lastIndex > 2048) continue;
1361
+ const openParen = content.indexOf("(", callIdx);
1362
+ let depth = 1;
1363
+ let i = openParen + 1;
1364
+ while (i < content.length && depth > 0) {
1365
+ const ch = content[i];
1366
+ if (ch === "(" || ch === "{") depth++;
1367
+ else if (ch === ")" || ch === "}") depth--;
1368
+ i++;
1369
+ }
1370
+ if (depth !== 0) continue;
1371
+ while (i < content.length && (content[i] === ";" || content[i] === " ")) i++;
1372
+ const trailingNL = content[i] === "\n" ? 1 : 0;
1373
+ const blockEnd = i + trailingNL;
1374
+ if (insideMarkers(blockStart, blockEnd)) continue;
1375
+ ranges.push({ start: blockStart, end: blockEnd });
1376
+ }
1377
+ if (ranges.length === 0) return content;
1378
+ ranges.sort((a, b) => b.start - a.start);
1379
+ let out = content;
1380
+ for (const r of ranges) out = out.slice(0, r.start) + out.slice(r.end);
1381
+ return out.replace(/\n{3,}/g, "\n\n");
1321
1382
  }
1322
1383
  function injectImport(entryPath, initFilePath) {
1323
1384
  let content;