@botim/mp-debug-sdk 0.2.0 → 0.3.1

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
@@ -107,8 +107,7 @@ var Transport = class {
107
107
  Authorization: `Bearer ${this.opts.deviceToken}`
108
108
  },
109
109
  body: JSON.stringify(batch),
110
- signal: this.inflightUpload.signal,
111
- keepalive: true
110
+ signal: this.inflightUpload.signal
112
111
  });
113
112
  if (!res.ok) {
114
113
  throw new Error(`ingest http ${res.status}`);
@@ -200,6 +199,12 @@ var Transport = class {
200
199
  }
201
200
  if (this.opts.buffer.size() > 0) {
202
201
  const events = this.opts.buffer.drain(this.opts.buffer.size());
202
+ const body = JSON.stringify({
203
+ sessionToken: this.opts.deviceToken,
204
+ events
205
+ });
206
+ const KEEPALIVE_BODY_LIMIT = 60 * 1024;
207
+ const useKeepalive = body.length <= KEEPALIVE_BODY_LIMIT;
203
208
  try {
204
209
  await this.internalFetch(this.opts.ingestUrl, {
205
210
  method: "POST",
@@ -207,11 +212,8 @@ var Transport = class {
207
212
  "Content-Type": "application/json",
208
213
  Authorization: `Bearer ${this.opts.deviceToken}`
209
214
  },
210
- body: JSON.stringify({
211
- sessionToken: this.opts.deviceToken,
212
- events
213
- }),
214
- keepalive: true
215
+ body,
216
+ ...useKeepalive ? { keepalive: true } : {}
215
217
  });
216
218
  } catch (err) {
217
219
  this.opts.onError?.(err);
@@ -774,32 +776,95 @@ async function defaultDomScreenshot() {
774
776
  return await captureViaHtmlSnapshot();
775
777
  }
776
778
  }
777
- async function captureViaSvgForeignObject() {
778
- const dpr = Math.min(window.devicePixelRatio || 1, 2);
779
- const w = Math.max(1, Math.min(window.innerWidth || document.documentElement.clientWidth || 1024, 2400));
780
- const h = Math.max(1, Math.min(document.documentElement.scrollHeight, 4e3));
781
- const inlinedCss = readInlineableStyles();
782
- const clone = document.documentElement.cloneNode(true);
783
- clone.querySelectorAll('link[rel="stylesheet"]').forEach((el) => el.remove());
784
- clone.querySelectorAll("script,noscript,template").forEach((el) => el.remove());
785
- clone.querySelectorAll("canvas").forEach((el) => el.remove());
786
- clone.querySelectorAll("img").forEach((img) => {
787
- const src = img.getAttribute("src") || "";
788
- if (src && /^(https?:|\/\/)/i.test(src)) {
779
+ var VOID_ELEMENTS = /* @__PURE__ */ new Set([
780
+ "area",
781
+ "base",
782
+ "br",
783
+ "col",
784
+ "embed",
785
+ "hr",
786
+ "img",
787
+ "input",
788
+ "link",
789
+ "meta",
790
+ "source",
791
+ "track",
792
+ "wbr"
793
+ ]);
794
+ var SKIP_TAGS = /* @__PURE__ */ new Set(["script", "noscript", "template"]);
795
+ var DEVTOOL_OVERLAY_PREFIXES = ["vite-", "astro-dev-", "next-route-"];
796
+ function isDevtoolOverlay(tag) {
797
+ return DEVTOOL_OVERLAY_PREFIXES.some((p) => tag.startsWith(p));
798
+ }
799
+ function escapeText(s) {
800
+ return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
801
+ }
802
+ function escapeAttr(s) {
803
+ return s.replace(/&/g, "&amp;").replace(/"/g, "&quot;");
804
+ }
805
+ function serializeComposed(node, opts, depth) {
806
+ if (depth > 256) return "";
807
+ if (node.nodeType === Node.TEXT_NODE) {
808
+ return escapeText(node.textContent ?? "");
809
+ }
810
+ if (node.nodeType === Node.COMMENT_NODE) return "";
811
+ if (node.nodeType !== Node.ELEMENT_NODE) return "";
812
+ const el = node;
813
+ const tag = el.tagName.toLowerCase();
814
+ if (SKIP_TAGS.has(tag)) return "";
815
+ if (isDevtoolOverlay(tag)) return "";
816
+ if (tag === "slot") {
817
+ const slot = el;
818
+ let assigned = [];
819
+ try {
820
+ assigned = slot.assignedNodes({ flatten: true });
821
+ } catch {
822
+ }
823
+ const source = assigned.length > 0 ? assigned : Array.from(el.childNodes);
824
+ return source.map((c) => serializeComposed(c, opts, depth + 1)).join("");
825
+ }
826
+ if (tag === "link" && (el.getAttribute("rel") || "").toLowerCase() === "stylesheet") {
827
+ return "";
828
+ }
829
+ if (tag === "style") {
830
+ return `<style>${el.textContent ?? ""}</style>`;
831
+ }
832
+ if (tag === "img" && opts.stripCrossOriginImages) {
833
+ const src = el.getAttribute("src") || "";
834
+ if (/^(https?:|\/\/)/i.test(src)) {
789
835
  try {
790
836
  const u = new URL(src, location.href);
791
- if (u.origin !== location.origin) img.remove();
837
+ if (u.origin !== location.origin) return "";
792
838
  } catch {
793
- img.remove();
839
+ return "";
794
840
  }
795
841
  }
796
- });
797
- clone.querySelectorAll("*").forEach((el) => {
798
- if (el.tagName.includes("-")) el.remove();
799
- });
800
- clone.setAttribute("xmlns", "http://www.w3.org/1999/xhtml");
801
- const xs = new XMLSerializer();
802
- const htmlStr = xs.serializeToString(clone);
842
+ }
843
+ if (tag === "canvas") return `<canvas></canvas>`;
844
+ const attrs = Array.from(el.attributes).filter((a) => !a.name.startsWith("on")).map((a) => ` ${a.name}="${escapeAttr(a.value)}"`).join("");
845
+ if (VOID_ELEMENTS.has(tag)) return `<${tag}${attrs}>`;
846
+ const childSource = el.shadowRoot ?? el;
847
+ const inner = Array.from(childSource.childNodes).map((c) => serializeComposed(c, opts, depth + 1)).join("");
848
+ return `<${tag}${attrs}>${inner}</${tag}>`;
849
+ }
850
+ function readInlineableStyles() {
851
+ const chunks = [];
852
+ for (const sheet of Array.from(document.styleSheets)) {
853
+ try {
854
+ const rules = sheet.cssRules;
855
+ for (const rule of Array.from(rules)) chunks.push(rule.cssText);
856
+ } catch {
857
+ }
858
+ }
859
+ return chunks.join("\n");
860
+ }
861
+ async function captureViaSvgForeignObject() {
862
+ const dpr = Math.min(window.devicePixelRatio || 1, 2);
863
+ const w = Math.max(1, Math.min(window.innerWidth || document.documentElement.clientWidth || 1024, 2400));
864
+ const h = Math.max(1, Math.min(document.documentElement.scrollHeight, 4e3));
865
+ const inlinedCss = readInlineableStyles();
866
+ const bodyHtml = serializeComposed(document.body, { stripCrossOriginImages: true }, 0);
867
+ const htmlStr = `<html xmlns="http://www.w3.org/1999/xhtml"><body>${bodyHtml}</body></html>`;
803
868
  const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}"><foreignObject width="100%" height="100%">` + (inlinedCss ? `<style xmlns="http://www.w3.org/1999/xhtml">${escapeForXml(inlinedCss)}</style>` : "") + htmlStr + `</foreignObject></svg>`;
804
869
  const blobUrl = URL.createObjectURL(
805
870
  new Blob([svg], { type: "image/svg+xml;charset=utf-8" })
@@ -833,30 +898,19 @@ function loadImage(src) {
833
898
  function escapeForXml(s) {
834
899
  return s.replace(/]]>/g, "]]&gt;").replace(/<\//g, "&lt;/");
835
900
  }
836
- function readInlineableStyles() {
837
- const chunks = [];
838
- for (const sheet of Array.from(document.styleSheets)) {
839
- try {
840
- const rules = sheet.cssRules;
841
- for (const rule of Array.from(rules)) chunks.push(rule.cssText);
842
- } catch {
843
- }
844
- }
845
- return chunks.join("\n");
846
- }
847
901
  async function captureViaHtmlSnapshot() {
848
902
  const inlinedCss = readInlineableStyles();
849
- const clone = document.documentElement.cloneNode(true);
850
- clone.querySelectorAll('link[rel="stylesheet"]').forEach((el) => el.remove());
851
- clone.querySelectorAll("script").forEach((el) => el.remove());
852
- const head = clone.querySelector("head");
853
- if (head && inlinedCss) {
854
- const styleEl = document.createElement("style");
855
- styleEl.setAttribute("data-botim-snapshot", "1");
856
- styleEl.textContent = inlinedCss;
857
- head.insertBefore(styleEl, head.firstChild);
858
- }
859
- const html = "<!DOCTYPE html>\n" + clone.outerHTML;
903
+ const bodyHtml = serializeComposed(document.body, { stripCrossOriginImages: false }, 0);
904
+ const html = `<!DOCTYPE html>
905
+ <html lang="en">
906
+ <head>
907
+ <meta charset="utf-8">
908
+ <meta name="viewport" content="width=device-width,initial-scale=1">
909
+ <base href="${escapeAttr(location.href)}">
910
+ ` + (inlinedCss ? `<style data-botim-snapshot="1">${inlinedCss}</style>
911
+ ` : "") + `</head>
912
+ <body>${bodyHtml}</body>
913
+ </html>`;
860
914
  return {
861
915
  data: JSON.stringify({
862
916
  html,