satoru-render 1.0.11 → 1.0.12

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.
@@ -547,6 +547,7 @@ var SatoruBase = class {
547
547
  this.currentFontMap = options.fontMap ?? DEFAULT_FONT_MAP;
548
548
  const instancePtr = mod.create_instance();
549
549
  mod.set_font_map(instancePtr, this.currentFontMap);
550
+ mod.set_collect_profile_enabled(instancePtr, profileEnabled);
550
551
  try {
551
552
  const defaultResolver = (r) => this.resolveDefaultResource(r, baseUrl, options.userAgent);
552
553
  const resolver = options.resolveResource ? async (r) => {
@@ -590,6 +591,8 @@ var SatoruBase = class {
590
591
  };
591
592
  const inputHtmls = Array.isArray(value) ? value : [value];
592
593
  const processedHtmls = [];
594
+ const utf8Decoder = new TextDecoder();
595
+ const utf8Encoder = new TextEncoder();
593
596
  const resolvedResources = /* @__PURE__ */ new Set();
594
597
  for (const rawHtml of inputHtmls) {
595
598
  let processedHtml = rawHtml;
@@ -597,7 +600,16 @@ var SatoruBase = class {
597
600
  addProfile("collectResourcesCount", 1);
598
601
  const collectStart = now();
599
602
  mod.collect_resources(instancePtr, processedHtml, width, height, options.mediaType === "print" ? 1 : 0);
600
- addProfile("collectResources", now() - collectStart);
603
+ const collectElapsed = now() - collectStart;
604
+ addProfile("collectResources", collectElapsed);
605
+ addProfile(`collectResourcesRound${i + 1}`, collectElapsed);
606
+ if (profileEnabled) try {
607
+ const collectProfile = JSON.parse(mod.get_collect_profile(instancePtr));
608
+ for (const [key, value] of Object.entries(collectProfile)) {
609
+ addProfile(key, value);
610
+ addProfile(`${key}Round${i + 1}`, value);
611
+ }
612
+ } catch {}
601
613
  const pendingStart = now();
602
614
  const binary = mod.get_pending_resources(instancePtr);
603
615
  addProfile("getPendingResources", now() - pendingStart);
@@ -613,15 +625,15 @@ var SatoruBase = class {
613
625
  const redraw_on_ready = view.getUint8(offset++) !== 0;
614
626
  const urlLen = view.getUint32(offset, true);
615
627
  offset += 4;
616
- const url = new TextDecoder().decode(new Uint8Array(binary.buffer, binary.byteOffset + offset, urlLen));
628
+ const url = utf8Decoder.decode(new Uint8Array(binary.buffer, binary.byteOffset + offset, urlLen));
617
629
  offset += urlLen;
618
630
  const nameLen = view.getUint32(offset, true);
619
631
  offset += 4;
620
- const name = new TextDecoder().decode(new Uint8Array(binary.buffer, binary.byteOffset + offset, nameLen));
632
+ const name = utf8Decoder.decode(new Uint8Array(binary.buffer, binary.byteOffset + offset, nameLen));
621
633
  offset += nameLen;
622
634
  const charsLen = view.getUint32(offset, true);
623
635
  offset += 4;
624
- const characters = new TextDecoder().decode(new Uint8Array(binary.buffer, binary.byteOffset + offset, charsLen));
636
+ const characters = utf8Decoder.decode(new Uint8Array(binary.buffer, binary.byteOffset + offset, charsLen));
625
637
  offset += charsLen;
626
638
  let type = "font";
627
639
  if (typeInt === 2) type = "image";
@@ -636,16 +648,31 @@ var SatoruBase = class {
636
648
  }
637
649
  const pending = resources.filter((r) => {
638
650
  const key = `${r.type}:${r.url}:${r.characters ?? ""}`;
651
+ if (r.type === "font" && resolvedResources.has(`font:${r.url}:`)) return false;
639
652
  return !resolvedResources.has(key);
640
653
  });
654
+ addProfile("pendingResourcesCount", pending.length);
655
+ addProfile(`pendingResourcesRound${i + 1}Count`, pending.length);
656
+ for (const r of pending) if (r.type === "font") {
657
+ addProfile("pendingFontResourcesCount", 1);
658
+ addProfile(`pendingFontResourcesRound${i + 1}Count`, 1);
659
+ } else if (r.type === "css") {
660
+ addProfile("pendingCssResourcesCount", 1);
661
+ addProfile(`pendingCssResourcesRound${i + 1}Count`, 1);
662
+ } else if (r.type === "image") {
663
+ addProfile("pendingImageResourcesCount", 1);
664
+ addProfile(`pendingImageResourcesRound${i + 1}Count`, 1);
665
+ }
641
666
  addProfile("parsePendingResources", now() - parseStart);
642
667
  if (pending.length === 0) break;
668
+ addProfile("pendingResourceRoundsCount", 1);
643
669
  const loadStart = now();
644
670
  await Promise.all(pending.map(async (r) => {
645
671
  try {
646
672
  if (r.url.startsWith("data:")) return;
647
673
  const key = `${r.type}:${r.url}:${r.characters ?? ""}`;
648
674
  resolvedResources.add(key);
675
+ if (r.type === "font") resolvedResources.add(`font:${r.url}:`);
649
676
  const data = await cachedResolver({ ...r });
650
677
  if (!data) return;
651
678
  if (typeof data === "object" && "css" in data && "fonts" in data) {
@@ -661,7 +688,7 @@ var SatoruBase = class {
661
688
  if (data instanceof Uint8Array || ArrayBuffer.isView(data)) {
662
689
  let finalUint8 = data instanceof Uint8Array ? data : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
663
690
  if (r.type === "css") {
664
- const cssText = new TextDecoder().decode(finalUint8);
691
+ const cssText = utf8Decoder.decode(finalUint8);
665
692
  const isAbsolute = /^[a-z][a-z0-9+.-]*:/i.test(r.url) || r.url.startsWith("data:");
666
693
  let cssBaseUrl = r.url;
667
694
  if (!isAbsolute && baseUrl) try {
@@ -677,7 +704,7 @@ var SatoruBase = class {
677
704
  return match;
678
705
  }
679
706
  });
680
- finalUint8 = new TextEncoder().encode(rewrittenCss);
707
+ finalUint8 = utf8Encoder.encode(rewrittenCss);
681
708
  }
682
709
  }
683
710
  await loadResourceData(r, finalUint8);
@@ -744,7 +771,7 @@ var SatoruBase = class {
744
771
  }
745
772
  if (format === "svg") {
746
773
  const decodeStart = now();
747
- const svg = new TextDecoder().decode(result);
774
+ const svg = utf8Decoder.decode(result);
748
775
  addProfile("decodeResult", now() - decodeStart);
749
776
  options.onProfile?.(profile);
750
777
  return svg;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "satoru-render",
3
- "version": "1.0.11",
3
+ "version": "1.0.12",
4
4
  "description": "High-fidelity HTML/CSS to SVG/PNG/PDF converter running in WebAssembly",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -8,10 +8,6 @@
8
8
  "bin": {
9
9
  "satoru-render": "./dist/cli.js"
10
10
  },
11
- "scripts": {
12
- "build": "tsc -b && rolldown -c rolldown.config.js",
13
- "deploy": "npm publish"
14
- },
15
11
  "exports": {
16
12
  ".": {
17
13
  "workerd": {
@@ -164,5 +160,9 @@
164
160
  "@unocss/preset-wind4": {
165
161
  "optional": true
166
162
  }
163
+ },
164
+ "scripts": {
165
+ "build": "tsc -b && rolldown -c rolldown.config.js",
166
+ "deploy": "npm publish"
167
167
  }
168
- }
168
+ }