@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.cjs CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
 
30
20
  // src/index.ts
@@ -385,8 +375,21 @@ var NodeHttpSink = class extends HttpSink {
385
375
  // src/core/sinks/local-sqlite.ts
386
376
  var import_node_os = require("os");
387
377
  var import_node_fs2 = require("fs");
378
+ var import_node_module = require("module");
388
379
  var import_node_path = require("path");
389
- var import_better_sqlite3 = __toESM(require("better-sqlite3"), 1);
380
+ var import_meta = {};
381
+ var requireBSQ = (0, import_node_module.createRequire)(import_meta.url);
382
+ function loadBetterSqlite3() {
383
+ try {
384
+ const mod = requireBSQ("better-sqlite3");
385
+ return typeof mod === "function" ? mod : mod.default;
386
+ } catch (err) {
387
+ throw new Error(
388
+ `@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}`,
389
+ { cause: err }
390
+ );
391
+ }
392
+ }
390
393
  var SCHEMA = `
391
394
  CREATE TABLE IF NOT EXISTS events (
392
395
  id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -410,9 +413,10 @@ var LocalSqliteSink = class {
410
413
  db;
411
414
  insert;
412
415
  constructor(path) {
416
+ const Database = loadBetterSqlite3();
413
417
  const resolved = path ?? (0, import_node_path.join)((0, import_node_os.homedir)(), ".gg", "errors.db");
414
418
  (0, import_node_fs2.mkdirSync)((0, import_node_path.dirname)(resolved), { recursive: true });
415
- this.db = new import_better_sqlite3.default(resolved);
419
+ this.db = new Database(resolved);
416
420
  this.db.pragma("journal_mode = WAL");
417
421
  this.db.exec(SCHEMA);
418
422
  this.insert = this.db.prepare(`
@@ -1092,13 +1096,12 @@ function wireElectron({ projectRoot, pkg, projectKey, ingestUrl }) {
1092
1096
  "Could not copy gg-pixel browser IIFE bundle \u2014 install @kenkaiiii/gg-pixel and re-run."
1093
1097
  );
1094
1098
  }
1095
- let patchedAny = false;
1099
+ let wiredAny = false;
1096
1100
  for (const html of htmlFiles) {
1097
- if (patchRendererHtml(html, rendererInitPath, projectKey, ingestUrl) === "patched") {
1098
- patchedAny = true;
1099
- }
1101
+ const r = patchRendererHtml(html, rendererInitPath, projectKey, ingestUrl);
1102
+ if (r === "patched" || r === "already") wiredAny = true;
1100
1103
  }
1101
- if (!patchedAny) {
1104
+ if (!wiredAny) {
1102
1105
  warnings.push(
1103
1106
  `Found HTML files in ${rendererDir} but couldn't patch any \u2014 they may have unusual CSP or no <head>.`
1104
1107
  );
@@ -1217,6 +1220,7 @@ function copyIifeBundle(projectRoot, dest) {
1217
1220
  }
1218
1221
  return false;
1219
1222
  }
1223
+ var PIXEL_HTML_MARKER = "<!-- gg-pixel: auto-wired by ggcoder pixel install -->";
1220
1224
  function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
1221
1225
  let content;
1222
1226
  try {
@@ -1224,7 +1228,20 @@ function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
1224
1228
  } catch {
1225
1229
  return "not-applicable";
1226
1230
  }
1227
- if (content.includes("gg-pixel.browser.iife")) return "already";
1231
+ const original = content;
1232
+ const markerIdx = content.indexOf(PIXEL_HTML_MARKER);
1233
+ if (markerIdx !== -1) {
1234
+ const firstScriptEnd = content.indexOf("</script>", markerIdx);
1235
+ const secondScriptEnd = firstScriptEnd !== -1 ? content.indexOf("</script>", firstScriptEnd + "</script>".length) : -1;
1236
+ if (secondScriptEnd !== -1) {
1237
+ let stripEnd = secondScriptEnd + "</script>".length;
1238
+ while (stripEnd < content.length && /\s/.test(content[stripEnd])) stripEnd++;
1239
+ let stripStart = markerIdx;
1240
+ while (stripStart > 0 && /[ \t]/.test(content[stripStart - 1])) stripStart--;
1241
+ if (stripStart > 0 && content[stripStart - 1] === "\n") stripStart--;
1242
+ content = content.slice(0, stripStart) + content.slice(stripEnd);
1243
+ }
1244
+ }
1228
1245
  const ingestOrigin = new URL(ingestUrl).origin;
1229
1246
  content = content.replace(
1230
1247
  /(<meta[^>]+http-equiv=["']?content-security-policy["']?[^>]*content=)("([^"]+)"|'([^']+)')/i,
@@ -1244,7 +1261,7 @@ function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
1244
1261
  );
1245
1262
  const relScript = (0, import_node_path2.relative)((0, import_node_path2.dirname)(htmlPath), iifePath).split(import_node_path2.sep).join("/");
1246
1263
  const inject = `
1247
- <!-- gg-pixel: auto-wired by ggcoder pixel install -->
1264
+ ${PIXEL_HTML_MARKER}
1248
1265
  <script src="${relScript}"></script>
1249
1266
  <script>
1250
1267
  if (window.GGPixel) GGPixel.initPixel({ projectKey: ${JSON.stringify(projectKey)}, ingestUrl: ${JSON.stringify(ingestUrl)} });
@@ -1257,6 +1274,7 @@ function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
1257
1274
  } else {
1258
1275
  return "not-applicable";
1259
1276
  }
1277
+ if (content === original) return "already";
1260
1278
  (0, import_node_fs3.writeFileSync)(htmlPath, content, "utf8");
1261
1279
  return "patched";
1262
1280
  }