@kenkaiiii/gg-pixel 4.3.79 → 4.3.81

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
@@ -792,7 +792,7 @@ function wireNextjs({ projectRoot, projectKey, ingestUrl }) {
792
792
  const warnings = [];
793
793
  const serverInitPath = pickPath(projectRoot, ["instrumentation.ts", "instrumentation.js"]);
794
794
  const finalServerPath = serverInitPath ?? (0, import_node_path2.join)(projectRoot, "instrumentation.ts");
795
- writeNextInstrumentation(finalServerPath, ingestUrl);
795
+ writeNextInstrumentation(finalServerPath, ingestUrl, projectKey);
796
796
  patchNextConfig(projectRoot);
797
797
  const clientInitPath = (0, import_node_path2.join)(projectRoot, "gg-pixel.client.tsx");
798
798
  (0, import_node_fs3.writeFileSync)(clientInitPath, renderNextClientComponent(ingestUrl, projectKey), "utf8");
@@ -816,13 +816,14 @@ function wireNextjs({ projectRoot, projectKey, ingestUrl }) {
816
816
  warnings
817
817
  };
818
818
  }
819
- function writeNextInstrumentation(path, ingestUrl) {
819
+ function writeNextInstrumentation(path, ingestUrl, projectKey) {
820
820
  const existing = (0, import_node_fs3.existsSync)(path) ? (0, import_node_fs3.readFileSync)(path, "utf8") : "";
821
821
  if (existing.includes("@kenkaiiii/gg-pixel")) return;
822
- const newContent = existing ? existing + "\n" + nextInstrumentationAppend(ingestUrl) : nextInstrumentationStandalone(ingestUrl);
822
+ const newContent = existing ? existing + "\n" + nextInstrumentationAppend(ingestUrl, projectKey) : nextInstrumentationStandalone(ingestUrl, projectKey);
823
823
  (0, import_node_fs3.writeFileSync)(path, newContent, "utf8");
824
824
  }
825
- function nextInstrumentationStandalone(ingestUrl) {
825
+ function nextInstrumentationStandalone(ingestUrl, projectKey) {
826
+ const fallback = projectKey ? ` ?? ${JSON.stringify(projectKey)}` : "";
826
827
  return `// Next.js auto-loads this file on server start. Pixel hooks the
827
828
  // uncaughtExceptionMonitor + unhandledRejection events for API routes,
828
829
  // Server Components, and route handlers.
@@ -830,19 +831,20 @@ export async function register() {
830
831
  if (process.env.NEXT_RUNTIME === "nodejs") {
831
832
  const { initPixel } = await import("@kenkaiiii/gg-pixel");
832
833
  initPixel({
833
- projectKey: process.env.GG_PIXEL_KEY ?? "",
834
+ projectKey: process.env.GG_PIXEL_KEY${fallback},
834
835
  sink: { kind: "http", ingestUrl: ${JSON.stringify(`${ingestUrl}/ingest`)} },
835
836
  });
836
837
  }
837
838
  }
838
839
  `;
839
840
  }
840
- function nextInstrumentationAppend(ingestUrl) {
841
+ function nextInstrumentationAppend(ingestUrl, projectKey) {
842
+ const fallback = projectKey ? ` ?? ${JSON.stringify(projectKey)}` : "";
841
843
  return `// gg-pixel: server-side error tracking
842
844
  import { initPixel } from "@kenkaiiii/gg-pixel";
843
845
  if (typeof process !== "undefined" && process.env.NEXT_RUNTIME === "nodejs") {
844
846
  initPixel({
845
- projectKey: process.env.GG_PIXEL_KEY ?? "",
847
+ projectKey: process.env.GG_PIXEL_KEY${fallback},
846
848
  sink: { kind: "http", ingestUrl: ${JSON.stringify(`${ingestUrl}/ingest`)} },
847
849
  });
848
850
  }
@@ -980,7 +982,7 @@ function wireSveltekit({ projectRoot, projectKey, ingestUrl }) {
980
982
  serverPath,
981
983
  `import { initPixel } from "@kenkaiiii/gg-pixel";
982
984
  initPixel({
983
- projectKey: process.env.GG_PIXEL_KEY ?? "",
985
+ projectKey: process.env.GG_PIXEL_KEY ?? ${JSON.stringify(projectKey)},
984
986
  sink: { kind: "http", ingestUrl: ${JSON.stringify(`${ingestUrl}/ingest`)} },
985
987
  });
986
988
  `,
@@ -1016,7 +1018,7 @@ function wireNuxt({ projectRoot, projectKey, ingestUrl }) {
1016
1018
  `import { initPixel } from "@kenkaiiii/gg-pixel";
1017
1019
  export default defineNuxtPlugin(() => {
1018
1020
  initPixel({
1019
- projectKey: process.env.GG_PIXEL_KEY ?? "",
1021
+ projectKey: process.env.GG_PIXEL_KEY ?? ${JSON.stringify(projectKey)},
1020
1022
  sink: { kind: "http", ingestUrl: ${JSON.stringify(`${ingestUrl}/ingest`)} },
1021
1023
  });
1022
1024
  });
@@ -1074,6 +1076,7 @@ function wireRemix({ projectRoot, projectKey, ingestUrl }) {
1074
1076
  };
1075
1077
  }
1076
1078
  function wireElectron({ projectRoot, pkg, projectKey, ingestUrl }) {
1079
+ const warnings = [];
1077
1080
  const isMainEsm = pkg.type === "module";
1078
1081
  const mainInitPath = isMainEsm ? (0, import_node_path2.join)(projectRoot, "gg-pixel.main.mjs") : (0, import_node_path2.join)(projectRoot, "gg-pixel.main.cjs");
1079
1082
  (0, import_node_fs3.writeFileSync)(
@@ -1081,50 +1084,62 @@ function wireElectron({ projectRoot, pkg, projectKey, ingestUrl }) {
1081
1084
  isMainEsm ? renderInitFile(ingestUrl, projectKey) : renderInitFileCjs(ingestUrl, projectKey),
1082
1085
  "utf8"
1083
1086
  );
1084
- const rendererInitPath = (0, import_node_path2.join)(projectRoot, "gg-pixel.renderer.mjs");
1085
- (0, import_node_fs3.writeFileSync)(rendererInitPath, renderBrowserInitFile(ingestUrl, projectKey), "utf8");
1086
- const mainEntry = pkg.main ? (0, import_node_path2.join)(projectRoot, pkg.main) : pickPath(projectRoot, [
1087
- "main.js",
1088
- "main.ts",
1089
- "src/main.js",
1090
- "src/main.ts",
1091
- "electron/main.js",
1092
- "electron/main.ts"
1093
- ]);
1087
+ const mainEntry = resolveMainEntryFromPkg(projectRoot, pkg);
1094
1088
  let mainWiring = { kind: "no_entry_found" };
1095
1089
  if (mainEntry && (0, import_node_fs3.existsSync)(mainEntry)) {
1096
1090
  mainWiring = injectImport(mainEntry, mainInitPath);
1097
1091
  }
1098
- const rendererEntry = pickPath(projectRoot, [
1099
- "src/renderer/index.ts",
1100
- "src/renderer/index.tsx",
1101
- "src/renderer/index.js",
1102
- "src/renderer/main.ts",
1103
- "src/renderer/main.tsx",
1104
- "src/renderer/main.js",
1105
- "renderer/index.ts",
1106
- "renderer/index.tsx",
1107
- "renderer/index.js",
1108
- "renderer.ts",
1109
- "renderer.tsx",
1110
- "renderer.js",
1111
- "src/index.tsx",
1112
- "src/index.jsx",
1113
- "src/main.tsx",
1114
- "src/main.jsx"
1115
- ]);
1116
- if (rendererEntry) {
1117
- injectImport(rendererEntry, rendererInitPath);
1118
- }
1119
- const warnings = [];
1120
- if (!rendererEntry) {
1121
- warnings.push(
1122
- 'Could not auto-detect the Electron renderer entry. Add `import "./gg-pixel.renderer.mjs";` to the top of your renderer entry file.'
1123
- );
1092
+ const htmlFiles = findRendererHtmlFiles(projectRoot);
1093
+ let rendererInitPath;
1094
+ if (htmlFiles.length > 0) {
1095
+ const rendererDir = (0, import_node_path2.dirname)(htmlFiles[0]);
1096
+ rendererInitPath = (0, import_node_path2.join)(rendererDir, "gg-pixel.browser.iife.js");
1097
+ if (!copyIifeBundle(projectRoot, rendererInitPath)) {
1098
+ warnings.push(
1099
+ "Could not copy gg-pixel browser IIFE bundle \u2014 install @kenkaiiii/gg-pixel and re-run."
1100
+ );
1101
+ }
1102
+ let patchedAny = false;
1103
+ for (const html of htmlFiles) {
1104
+ if (patchRendererHtml(html, rendererInitPath, projectKey, ingestUrl) === "patched") {
1105
+ patchedAny = true;
1106
+ }
1107
+ }
1108
+ if (!patchedAny) {
1109
+ warnings.push(
1110
+ `Found HTML files in ${rendererDir} but couldn't patch any \u2014 they may have unusual CSP or no <head>.`
1111
+ );
1112
+ }
1113
+ } else {
1114
+ rendererInitPath = (0, import_node_path2.join)(projectRoot, "gg-pixel.renderer.mjs");
1115
+ (0, import_node_fs3.writeFileSync)(rendererInitPath, renderBrowserInitFile(ingestUrl, projectKey), "utf8");
1116
+ const rendererEntry = pickPath(projectRoot, [
1117
+ "src/renderer/index.ts",
1118
+ "src/renderer/index.tsx",
1119
+ "src/renderer/index.js",
1120
+ "src/renderer/main.ts",
1121
+ "src/renderer/main.tsx",
1122
+ "src/renderer/main.js",
1123
+ "renderer/index.ts",
1124
+ "renderer/index.tsx",
1125
+ "renderer/index.js",
1126
+ "renderer.ts",
1127
+ "renderer.tsx",
1128
+ "renderer.js",
1129
+ "src/index.tsx",
1130
+ "src/index.jsx",
1131
+ "src/main.tsx",
1132
+ "src/main.jsx"
1133
+ ]);
1134
+ if (rendererEntry) injectImport(rendererEntry, rendererInitPath);
1135
+ else
1136
+ warnings.push(
1137
+ 'Could not auto-detect the Electron renderer entry. Add `import "./gg-pixel.renderer.mjs";` to the top of your renderer entry file.'
1138
+ );
1124
1139
  }
1125
1140
  return {
1126
1141
  primaryInitPath: rendererInitPath,
1127
- entryWiring: rendererEntry ? { kind: "injected", entryPath: rendererEntry } : { kind: "no_entry_found" },
1142
+ entryWiring: { kind: "injected", entryPath: rendererInitPath },
1128
1143
  secondaryInit: {
1129
1144
  path: mainInitPath,
1130
1145
  description: "Electron main-process init" + (mainWiring.kind === "injected" ? ` (wired into ${mainEntry})` : "")
@@ -1132,6 +1147,124 @@ function wireElectron({ projectRoot, pkg, projectKey, ingestUrl }) {
1132
1147
  warnings
1133
1148
  };
1134
1149
  }
1150
+ function resolveMainEntryFromPkg(projectRoot, pkg) {
1151
+ if (pkg.main) {
1152
+ const sourceCandidates = [];
1153
+ const main = pkg.main;
1154
+ if (/^(dist|build|\.next|out)\//.test(main)) {
1155
+ const swap = main.replace(/^(dist|build|\.next|out)\//, "src/");
1156
+ if (swap.endsWith(".js")) {
1157
+ sourceCandidates.push(swap.replace(/\.js$/, ".ts"));
1158
+ sourceCandidates.push(swap.replace(/\.js$/, ".tsx"));
1159
+ }
1160
+ sourceCandidates.push(swap);
1161
+ }
1162
+ if (main.endsWith(".js")) {
1163
+ sourceCandidates.push(main.replace(/\.js$/, ".ts"));
1164
+ sourceCandidates.push(main.replace(/\.js$/, ".tsx"));
1165
+ }
1166
+ for (const c of sourceCandidates) {
1167
+ const p = (0, import_node_path2.join)(projectRoot, c);
1168
+ if ((0, import_node_fs3.existsSync)(p)) return p;
1169
+ }
1170
+ const literal = (0, import_node_path2.join)(projectRoot, main);
1171
+ if ((0, import_node_fs3.existsSync)(literal)) return literal;
1172
+ }
1173
+ return pickPath(projectRoot, [
1174
+ "main.js",
1175
+ "main.ts",
1176
+ "src/main.js",
1177
+ "src/main.ts",
1178
+ "src/main/index.ts",
1179
+ "src/main/index.js",
1180
+ "electron/main.js",
1181
+ "electron/main.ts"
1182
+ ]);
1183
+ }
1184
+ var RENDERER_HTML_DIRS = ["ui", "renderer", "src/renderer", "public", "static"];
1185
+ function findRendererHtmlFiles(projectRoot) {
1186
+ for (const dir of RENDERER_HTML_DIRS) {
1187
+ const root = (0, import_node_path2.join)(projectRoot, dir);
1188
+ if (!(0, import_node_fs3.existsSync)(root)) continue;
1189
+ const html = [];
1190
+ let entries;
1191
+ try {
1192
+ entries = (0, import_node_fs3.readdirSync)(root);
1193
+ } catch {
1194
+ continue;
1195
+ }
1196
+ for (const e of entries) {
1197
+ if (!e.endsWith(".html")) continue;
1198
+ const p = (0, import_node_path2.join)(root, e);
1199
+ try {
1200
+ const c = (0, import_node_fs3.readFileSync)(p, "utf8");
1201
+ if (/<meta[^>]+content-security-policy/i.test(c) || /<script[\s>]/i.test(c)) {
1202
+ html.push(p);
1203
+ }
1204
+ } catch {
1205
+ }
1206
+ }
1207
+ if (html.length > 0) return html;
1208
+ }
1209
+ return [];
1210
+ }
1211
+ function copyIifeBundle(projectRoot, dest) {
1212
+ const candidates = [
1213
+ (0, import_node_path2.join)(projectRoot, "node_modules/@kenkaiiii/gg-pixel/dist/browser.iife.global.js"),
1214
+ (0, import_node_path2.join)(projectRoot, "node_modules/@kenkaiiii/gg-pixel/dist/browser.iife.js")
1215
+ ];
1216
+ for (const c of candidates) {
1217
+ if ((0, import_node_fs3.existsSync)(c)) {
1218
+ try {
1219
+ (0, import_node_fs3.writeFileSync)(dest, (0, import_node_fs3.readFileSync)(c, "utf8"), "utf8");
1220
+ return true;
1221
+ } catch {
1222
+ }
1223
+ }
1224
+ }
1225
+ return false;
1226
+ }
1227
+ function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
1228
+ let content;
1229
+ try {
1230
+ content = (0, import_node_fs3.readFileSync)(htmlPath, "utf8");
1231
+ } catch {
1232
+ return "not-applicable";
1233
+ }
1234
+ if (content.includes("gg-pixel.browser.iife")) return "already";
1235
+ const ingestOrigin = new URL(ingestUrl).origin;
1236
+ content = content.replace(
1237
+ /(<meta[^>]+http-equiv=["']?content-security-policy["']?[^>]+content=["'])([^"']+)(["'])/i,
1238
+ (_match, before, csp, after) => {
1239
+ let updated = csp;
1240
+ if (/connect-src\s/i.test(csp)) {
1241
+ if (!csp.includes(ingestOrigin)) {
1242
+ updated = csp.replace(/(connect-src[^;]*)/i, `$1 ${ingestOrigin}`);
1243
+ }
1244
+ } else {
1245
+ updated = csp.trim().replace(/;?$/, `; connect-src 'self' ${ingestOrigin};`);
1246
+ }
1247
+ return before + updated + after;
1248
+ }
1249
+ );
1250
+ const relScript = (0, import_node_path2.relative)((0, import_node_path2.dirname)(htmlPath), iifePath).split(import_node_path2.sep).join("/");
1251
+ const inject = `
1252
+ <!-- gg-pixel: auto-wired by ggcoder pixel install -->
1253
+ <script src="${relScript}"></script>
1254
+ <script>
1255
+ if (window.GGPixel) GGPixel.initPixel({ projectKey: ${JSON.stringify(projectKey)}, ingestUrl: ${JSON.stringify(ingestUrl)} });
1256
+ </script>
1257
+ `;
1258
+ if (/<head[^>]*>/i.test(content)) {
1259
+ content = content.replace(/(<head[^>]*>)/i, `$1${inject}`);
1260
+ } else if (/<html[^>]*>/i.test(content)) {
1261
+ content = content.replace(/(<html[^>]*>)/i, `$1<head>${inject}</head>`);
1262
+ } else {
1263
+ return "not-applicable";
1264
+ }
1265
+ (0, import_node_fs3.writeFileSync)(htmlPath, content, "utf8");
1266
+ return "patched";
1267
+ }
1135
1268
  function wireTauri({ projectRoot, pkg, projectKey, ingestUrl }) {
1136
1269
  const initPath = (0, import_node_path2.join)(projectRoot, "gg-pixel.init.mjs");
1137
1270
  (0, import_node_fs3.writeFileSync)(initPath, renderBrowserInitFile(ingestUrl, projectKey), "utf8");