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