@kenkaiiii/gg-pixel 4.3.87 → 4.3.89

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
@@ -344,8 +344,20 @@ var NodeHttpSink = class extends HttpSink {
344
344
  // src/core/sinks/local-sqlite.ts
345
345
  import { homedir } from "os";
346
346
  import { mkdirSync } from "fs";
347
+ import { createRequire } from "module";
347
348
  import { dirname, join } from "path";
348
- import Database from "better-sqlite3";
349
+ var requireBSQ = createRequire(import.meta.url);
350
+ function loadBetterSqlite3() {
351
+ try {
352
+ const mod = requireBSQ("better-sqlite3");
353
+ return typeof mod === "function" ? mod : mod.default;
354
+ } catch (err) {
355
+ throw new Error(
356
+ `@kenkaiiii/gg-pixel: \`kind: "local"\` requires the optional peer dependency \`better-sqlite3\`. Install it with \`npm install better-sqlite3\` (or your package manager's equivalent). Underlying error: ${err.message}`,
357
+ { cause: err }
358
+ );
359
+ }
360
+ }
349
361
  var SCHEMA = `
350
362
  CREATE TABLE IF NOT EXISTS events (
351
363
  id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -369,6 +381,7 @@ var LocalSqliteSink = class {
369
381
  db;
370
382
  insert;
371
383
  constructor(path) {
384
+ const Database = loadBetterSqlite3();
372
385
  const resolved = path ?? join(homedir(), ".gg", "errors.db");
373
386
  mkdirSync(dirname(resolved), { recursive: true });
374
387
  this.db = new Database(resolved);
@@ -1058,13 +1071,12 @@ function wireElectron({ projectRoot, pkg, projectKey, ingestUrl }) {
1058
1071
  "Could not copy gg-pixel browser IIFE bundle \u2014 install @kenkaiiii/gg-pixel and re-run."
1059
1072
  );
1060
1073
  }
1061
- let patchedAny = false;
1074
+ let wiredAny = false;
1062
1075
  for (const html of htmlFiles) {
1063
- if (patchRendererHtml(html, rendererInitPath, projectKey, ingestUrl) === "patched") {
1064
- patchedAny = true;
1065
- }
1076
+ const r = patchRendererHtml(html, rendererInitPath, projectKey, ingestUrl);
1077
+ if (r === "patched" || r === "already") wiredAny = true;
1066
1078
  }
1067
- if (!patchedAny) {
1079
+ if (!wiredAny) {
1068
1080
  warnings.push(
1069
1081
  `Found HTML files in ${rendererDir} but couldn't patch any \u2014 they may have unusual CSP or no <head>.`
1070
1082
  );
@@ -1183,6 +1195,7 @@ function copyIifeBundle(projectRoot, dest) {
1183
1195
  }
1184
1196
  return false;
1185
1197
  }
1198
+ var PIXEL_HTML_MARKER = "<!-- gg-pixel: auto-wired by ggcoder pixel install -->";
1186
1199
  function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
1187
1200
  let content;
1188
1201
  try {
@@ -1190,7 +1203,20 @@ function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
1190
1203
  } catch {
1191
1204
  return "not-applicable";
1192
1205
  }
1193
- if (content.includes("gg-pixel.browser.iife")) return "already";
1206
+ const original = content;
1207
+ const markerIdx = content.indexOf(PIXEL_HTML_MARKER);
1208
+ if (markerIdx !== -1) {
1209
+ const firstScriptEnd = content.indexOf("</script>", markerIdx);
1210
+ const secondScriptEnd = firstScriptEnd !== -1 ? content.indexOf("</script>", firstScriptEnd + "</script>".length) : -1;
1211
+ if (secondScriptEnd !== -1) {
1212
+ let stripEnd = secondScriptEnd + "</script>".length;
1213
+ while (stripEnd < content.length && /\s/.test(content[stripEnd])) stripEnd++;
1214
+ let stripStart = markerIdx;
1215
+ while (stripStart > 0 && /[ \t]/.test(content[stripStart - 1])) stripStart--;
1216
+ if (stripStart > 0 && content[stripStart - 1] === "\n") stripStart--;
1217
+ content = content.slice(0, stripStart) + content.slice(stripEnd);
1218
+ }
1219
+ }
1194
1220
  const ingestOrigin = new URL(ingestUrl).origin;
1195
1221
  content = content.replace(
1196
1222
  /(<meta[^>]+http-equiv=["']?content-security-policy["']?[^>]*content=)("([^"]+)"|'([^']+)')/i,
@@ -1210,7 +1236,7 @@ function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
1210
1236
  );
1211
1237
  const relScript = relative(dirname2(htmlPath), iifePath).split(sep).join("/");
1212
1238
  const inject = `
1213
- <!-- gg-pixel: auto-wired by ggcoder pixel install -->
1239
+ ${PIXEL_HTML_MARKER}
1214
1240
  <script src="${relScript}"></script>
1215
1241
  <script>
1216
1242
  if (window.GGPixel) GGPixel.initPixel({ projectKey: ${JSON.stringify(projectKey)}, ingestUrl: ${JSON.stringify(ingestUrl)} });
@@ -1223,6 +1249,7 @@ function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
1223
1249
  } else {
1224
1250
  return "not-applicable";
1225
1251
  }
1252
+ if (content === original) return "already";
1226
1253
  writeFileSync(htmlPath, content, "utf8");
1227
1254
  return "patched";
1228
1255
  }