@voila.dev/replay 0.1.1 → 0.3.0

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.
@@ -59,7 +59,9 @@ export declare enum Op {
59
59
  /** framed: id, a canvas as a data URL */
60
60
  SHOT = 128,
61
61
  /** framed: the name of a module cut off after it threw */
62
- ERR = 129
62
+ ERR = 129,
63
+ /** framed: id, the address of the picture an image shows, picked from its `srcset` or its `<picture>` */
64
+ SHOWN = 130
63
65
  }
64
66
  export declare enum End {
65
67
  Consent = 1,
package/dist/format.js CHANGED
@@ -14,7 +14,7 @@ import {
14
14
  resolve,
15
15
  span,
16
16
  validate
17
- } from "./shared/chunk-6US53Q3A.js";
17
+ } from "./shared/chunk-3B3R4DHO.js";
18
18
  export {
19
19
  End,
20
20
  FNV_START,
package/dist/modules.js CHANGED
@@ -1,4 +1,4 @@
1
- import "./shared/chunk-6US53Q3A.js";
1
+ import "./shared/chunk-3B3R4DHO.js";
2
2
 
3
3
  // src/modules/index.ts
4
4
  function shadow() {
@@ -9,6 +9,8 @@ export interface PlayerOptions {
9
9
  readonly skipIdle?: boolean;
10
10
  /** Ms of recording between two checkpoints; a seek starts from the nearest one. */
11
11
  readonly every?: number;
12
+ /** Where to load a picture or a font from, given its address on the page (see `MirrorHooks`). */
13
+ readonly asset?: (url: string) => string;
12
14
  }
13
15
  export interface Gap {
14
16
  /** Ms from the start of the recording. */
@@ -5,6 +5,11 @@ export interface MirrorHooks {
5
5
  readonly size?: (width: number, height: number) => void;
6
6
  readonly focus?: (el: Element | null) => void;
7
7
  readonly print?: (ok: boolean, at: number, detail: string) => void;
8
+ /**
9
+ * Where to load a picture or a font from, given its address on the page: a copy kept when
10
+ * the page was recorded outlives the file the site later renamed or replaced.
11
+ */
12
+ readonly asset?: (url: string) => string;
8
13
  }
9
14
  export interface Mirror {
10
15
  /** Runs the tape from `from`, up to the tick that would pass `until`. Returns where it stopped. */
package/dist/play.js CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  fnv,
4
4
  join,
5
5
  length
6
- } from "./shared/chunk-6US53Q3A.js";
6
+ } from "./shared/chunk-3B3R4DHO.js";
7
7
 
8
8
  // src/play/mirror.ts
9
9
  var SVG = "http://www.w3.org/2000/svg";
@@ -46,7 +46,8 @@ function mirror(doc, hooks = {}) {
46
46
  return url;
47
47
  }
48
48
  };
49
- const css = (text, from = base) => text.replace(CSS_URL, (_m, _q, url) => `url("${absolute(url, from)}")`);
49
+ const kept = (url) => hooks.asset ? hooks.asset(url) : url;
50
+ const css = (text, from = base) => text.replace(CSS_URL, (_m, _q, url) => `url("${kept(absolute(url, from))}")`);
50
51
  const local = (el) => localName.call(el);
51
52
  function add(n) {
52
53
  const top = stack[stack.length - 1];
@@ -67,10 +68,11 @@ function mirror(doc, hooks = {}) {
67
68
  if (ADDRESS.test(name)) {
68
69
  value = absolute(value);
69
70
  if (!SAFE_URL.test(value)) value = "about:blank";
71
+ else if (tag !== "link") value = kept(value);
70
72
  } else if (name === "srcset")
71
73
  value = value.split(",").map((part) => {
72
74
  const [url, ...rest] = part.trim().split(/\s+/);
73
- return [absolute(url || ""), ...rest].join(" ");
75
+ return [kept(absolute(url || "")), ...rest].join(" ");
74
76
  }).join(", ");
75
77
  else if (name === "style") value = css(value);
76
78
  else if (tag === "a" && name === "target") return;
@@ -407,6 +409,18 @@ function mirror(doc, hooks = {}) {
407
409
  image.src = shot;
408
410
  break;
409
411
  }
412
+ case 130 /* SHOWN */: {
413
+ const el = nodes[a(2)];
414
+ const src = a(3);
415
+ if (!el || local(el) !== "img" || typeof src !== "string") break;
416
+ set(el, "srcset", src);
417
+ const picture = el.parentNode;
418
+ if (picture && local(picture) === "picture") {
419
+ for (const s of Array.from(picture.children))
420
+ if (local(s) === "source") set(s, "srcset", src);
421
+ }
422
+ break;
423
+ }
410
424
  }
411
425
  }
412
426
  return i;
@@ -713,6 +727,7 @@ function createPlayer(options) {
713
727
  let m = make();
714
728
  function make() {
715
729
  return mirror(doc, {
730
+ asset: options.asset,
716
731
  pointer(x, y) {
717
732
  cursor.style.display = "block";
718
733
  cursor.style.transform = `translate(${x}px,${y}px)`;
package/dist/record.js CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  FNV_START,
3
3
  VERSION,
4
4
  fnv
5
- } from "./shared/chunk-6US53Q3A.js";
5
+ } from "./shared/chunk-3B3R4DHO.js";
6
6
 
7
7
  // src/record/natives.ts
8
8
  var NATIVE = /\{\s*\[native code\]\s*\}\s*$/;
@@ -130,10 +130,13 @@ function record(w, o = {}) {
130
130
  const owner = /* @__PURE__ */ new WeakMap();
131
131
  const fields = /* @__PURE__ */ new Set();
132
132
  const written = /* @__PURE__ */ new WeakMap();
133
+ const pictures = /* @__PURE__ */ new Set();
134
+ let shown = /* @__PURE__ */ new WeakMap();
133
135
  const recordedIn = /* @__PURE__ */ new WeakMap();
134
136
  const stars = (s) => s.replace(/\S/g, "*");
135
137
  const isEl = (n) => n.nodeType === 1;
136
138
  const has = (el, name) => N.has(el, name);
139
+ const isPicture = (n) => !!n && isEl(n) && N.local(n) === "picture";
137
140
  function up(n) {
138
141
  const p = N.parent(n);
139
142
  if (p) return p;
@@ -329,6 +332,9 @@ function record(w, o = {}) {
329
332
  } else if (tag === "dialog" || has(el, "popover")) {
330
333
  fields.add(el);
331
334
  written.set(el, tag === "dialog" && has(el, "open") ? 1 : 0);
335
+ } else if (tag === "img" && (has(el, "srcset") || isPicture(N.parent(el)))) {
336
+ pictures.add(el);
337
+ shown.delete(el);
332
338
  } else if (tag === "style" || tag === "link" && /stylesheet/i.test(N.get(el, "rel") || ""))
333
339
  sheetQueue.push(el);
334
340
  }
@@ -491,6 +497,10 @@ function record(w, o = {}) {
491
497
  stamp();
492
498
  tape.push(53 /* FOCUS */, 0);
493
499
  });
500
+ on("load", (e) => {
501
+ const t = target(e);
502
+ if (t && pictures.has(t)) kick();
503
+ });
494
504
  const typed = () => kick();
495
505
  on("input", typed);
496
506
  on("change", typed);
@@ -668,6 +678,14 @@ function record(w, o = {}) {
668
678
  written.set(el, v);
669
679
  tape.push(op, id, v);
670
680
  });
681
+ pictures.forEach((img) => {
682
+ const id = ids.get(img);
683
+ if (!id || !img.isConnected) return void pictures.delete(img);
684
+ const src = img.currentSrc;
685
+ if (!src || src === img.src || shown.get(img) === src) return;
686
+ shown.set(img, src);
687
+ tape.push(130 /* SHOWN */, 2, id, src);
688
+ });
671
689
  readSheets();
672
690
  for (const m of modules) if (m.onSync) guard(m, () => m.onSync?.());
673
691
  if (cut2) resume();
@@ -810,6 +828,8 @@ function record(w, o = {}) {
810
828
  left.clear();
811
829
  gone.clear();
812
830
  fields.clear();
831
+ pictures.clear();
832
+ shown = /* @__PURE__ */ new WeakMap();
813
833
  observer.takeRecords();
814
834
  last = now();
815
835
  t0 = Date.now() - last;
@@ -31,6 +31,7 @@ var Op = /* @__PURE__ */ ((Op2) => {
31
31
  Op2[Op2["END"] = 98] = "END";
32
32
  Op2[Op2["SHOT"] = 128] = "SHOT";
33
33
  Op2[Op2["ERR"] = 129] = "ERR";
34
+ Op2[Op2["SHOWN"] = 130] = "SHOWN";
34
35
  return Op2;
35
36
  })(Op || {});
36
37
  var End = /* @__PURE__ */ ((End2) => {
package/dist/testing.js CHANGED
@@ -764,6 +764,11 @@ function compare(live, copy, o = {}) {
764
764
  if (tag === "link" && name === "href" && /stylesheet/i.test(getA(el, "rel") || "") && (side === "copy" ? v.startsWith("blob:") : true))
765
765
  continue;
766
766
  if (side === "live" && masked && !PLAIN.test(name)) v = stars(v);
767
+ if (side === "live" && name === "srcset") {
768
+ const parent = el.parentNode;
769
+ const img = tag === "img" ? el : tag === "source" && parent && tagOf(parent) === "picture" ? Array.from(parent.children).find((c) => tagOf(c) === "img") : void 0;
770
+ if (img?.currentSrc && img.currentSrc !== img.src) v = img.currentSrc;
771
+ }
767
772
  if (URL_ATTR.test(name)) {
768
773
  v = abs(v);
769
774
  if (side === "live" && !SAFE_URL.test(v)) v = "about:blank";
@@ -791,7 +796,7 @@ function compare(live, copy, o = {}) {
791
796
  const want = shown(L.value, fieldHidden(L, masked, clear));
792
797
  if (want !== R.value) add(p, "textarea.value", want, R.value);
793
798
  } else if (tag === "select") {
794
- const picked = (s) => Array.from(s.options, (x, i) => x.selected ? i : -1).filter((i) => i >= 0).join(",");
799
+ const picked = (s) => s.multiple || s.size > 1 ? Array.from(s.options, (x, i) => x.selected ? i : -1).filter((i) => i >= 0).join(",") : s.selectedIndex < 0 ? "" : String(s.selectedIndex);
795
800
  let want = picked(L);
796
801
  if (fieldHidden(L, masked, clear)) {
797
802
  const options = Array.from(L.options);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@voila.dev/replay",
3
- "version": "0.1.1",
3
+ "version": "0.3.0",
4
4
  "description": "Session recording and replay for the web: a small recorder, a flat tape format, a DOM-built player.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -74,6 +74,8 @@ export enum Op {
74
74
  SHOT = 128,
75
75
  /** framed: the name of a module cut off after it threw */
76
76
  ERR = 129,
77
+ /** framed: id, the address of the picture an image shows, picked from its `srcset` or its `<picture>` */
78
+ SHOWN = 130,
77
79
  }
78
80
 
79
81
  export enum End {
package/src/play/index.ts CHANGED
@@ -16,6 +16,8 @@ export interface PlayerOptions {
16
16
  readonly skipIdle?: boolean;
17
17
  /** Ms of recording between two checkpoints; a seek starts from the nearest one. */
18
18
  readonly every?: number;
19
+ /** Where to load a picture or a font from, given its address on the page (see `MirrorHooks`). */
20
+ readonly asset?: (url: string) => string;
19
21
  }
20
22
 
21
23
  export interface Gap {
@@ -101,6 +103,7 @@ export function createPlayer(options: PlayerOptions): Player {
101
103
  let m: Mirror = make();
102
104
  function make() {
103
105
  return mirror(doc, {
106
+ asset: options.asset,
104
107
  pointer(x, y) {
105
108
  cursor.style.display = "block";
106
109
  cursor.style.transform = `translate(${x}px,${y}px)`;
@@ -12,6 +12,11 @@ export interface MirrorHooks {
12
12
  readonly size?: (width: number, height: number) => void;
13
13
  readonly focus?: (el: Element | null) => void;
14
14
  readonly print?: (ok: boolean, at: number, detail: string) => void;
15
+ /**
16
+ * Where to load a picture or a font from, given its address on the page: a copy kept when
17
+ * the page was recorded outlives the file the site later renamed or replaced.
18
+ */
19
+ readonly asset?: (url: string) => string;
15
20
  }
16
21
 
17
22
  export interface Mirror {
@@ -101,8 +106,9 @@ export function mirror(doc: Document, hooks: MirrorHooks = {}): Mirror {
101
106
  return url;
102
107
  }
103
108
  };
109
+ const kept = (url: string) => (hooks.asset ? hooks.asset(url) : url);
104
110
  const css = (text: string, from = base) =>
105
- text.replace(CSS_URL, (_m, _q, url: string) => `url("${absolute(url, from)}")`);
111
+ text.replace(CSS_URL, (_m, _q, url: string) => `url("${kept(absolute(url, from))}")`);
106
112
  const local = (el: Element) => localName.call(el);
107
113
 
108
114
  function add(n: Node) {
@@ -126,12 +132,14 @@ export function mirror(doc: Document, hooks: MirrorHooks = {}): Mirror {
126
132
  if (ADDRESS.test(name)) {
127
133
  value = absolute(value);
128
134
  if (!SAFE_URL.test(value)) value = "about:blank";
135
+ // A stylesheet's own address stays: its rules came on the tape, read from there.
136
+ else if (tag !== "link") value = kept(value);
129
137
  } else if (name === "srcset")
130
138
  value = value
131
139
  .split(",")
132
140
  .map((part) => {
133
141
  const [url, ...rest] = part.trim().split(/\s+/);
134
- return [absolute(url || ""), ...rest].join(" ");
142
+ return [kept(absolute(url || "")), ...rest].join(" ");
135
143
  })
136
144
  .join(", ");
137
145
  else if (name === "style") value = css(value);
@@ -473,6 +481,18 @@ export function mirror(doc: Document, hooks: MirrorHooks = {}): Mirror {
473
481
  image.src = shot;
474
482
  break;
475
483
  }
484
+ case Op.SHOWN: {
485
+ // The picture the visitor's browser picked, whatever the viewer's screen would.
486
+ const el = nodes[a(2)] as Element | undefined;
487
+ const src: string = a(3);
488
+ if (!el || local(el) !== "img" || typeof src !== "string") break;
489
+ set(el, "srcset", src);
490
+ const picture = el.parentNode as Element | null;
491
+ if (picture && local(picture) === "picture")
492
+ for (const s of Array.from(picture.children))
493
+ if (local(s) === "source") set(s, "srcset", src);
494
+ break;
495
+ }
476
496
  // HIDE, END, ERR and the framed ops this player does not know: nothing to build.
477
497
  }
478
498
  }
@@ -175,12 +175,16 @@ export function record(w: Window, o: Options = {}): Recorder | null {
175
175
  /** Form controls, dialogs and popovers, and the value last written for each. */
176
176
  const fields = new Set<Element>();
177
177
  const written = new WeakMap<Element, string | number>();
178
+ /** Images that pick their picture, and the address last written for each. */
179
+ const pictures = new Set<HTMLImageElement>();
180
+ let shown = new WeakMap<Element, string>();
178
181
  /** The zone each node was recorded under: a node moved to another zone is recorded again. */
179
182
  const recordedIn = new WeakMap<Node, Zone>();
180
183
 
181
184
  const stars = (s: string) => s.replace(/\S/g, "*");
182
185
  const isEl = (n: Node): n is Element => n.nodeType === 1;
183
186
  const has = (el: Element, name: string) => N.has(el, name);
187
+ const isPicture = (n: Node | null) => !!n && isEl(n) && N.local(n) === "picture";
184
188
 
185
189
  // --- Zones and masking -------------------------------------------------------------
186
190
 
@@ -419,6 +423,12 @@ export function record(w: Window, o: Options = {}): Recorder | null {
419
423
  } else if (tag === "dialog" || has(el, "popover")) {
420
424
  fields.add(el);
421
425
  written.set(el, tag === "dialog" && has(el, "open") ? 1 : 0);
426
+ } else if (
427
+ tag === "img" &&
428
+ (has(el, "srcset") || isPicture(N.parent(el)))
429
+ ) {
430
+ pictures.add(el as HTMLImageElement);
431
+ shown.delete(el);
422
432
  } else if (
423
433
  tag === "style" ||
424
434
  (tag === "link" && /stylesheet/i.test(N.get(el, "rel") || ""))
@@ -602,6 +612,10 @@ export function record(w: Window, o: Options = {}): Recorder | null {
602
612
  stamp();
603
613
  tape.push(Op.FOCUS, 0);
604
614
  });
615
+ on("load", (e: Event) => {
616
+ const t = target(e);
617
+ if (t && pictures.has(t as HTMLImageElement)) kick();
618
+ });
605
619
  const typed = () => kick();
606
620
  on("input", typed);
607
621
  on("change", typed);
@@ -815,6 +829,16 @@ export function record(w: Window, o: Options = {}): Recorder | null {
815
829
  tape.push(op, id, v);
816
830
  });
817
831
 
832
+ // The picture the browser picked, never in the copy's choice: the viewer's screen is another.
833
+ pictures.forEach((img) => {
834
+ const id = ids.get(img);
835
+ if (!id || !img.isConnected) return void pictures.delete(img);
836
+ const src = img.currentSrc;
837
+ if (!src || src === img.src || shown.get(img) === src) return;
838
+ shown.set(img, src);
839
+ tape.push(Op.SHOWN, 2, id, src);
840
+ });
841
+
818
842
  readSheets();
819
843
  for (const m of modules) if (m.onSync) guard(m, () => m.onSync?.());
820
844
 
@@ -973,6 +997,8 @@ export function record(w: Window, o: Options = {}): Recorder | null {
973
997
  left.clear();
974
998
  gone.clear();
975
999
  fields.clear();
1000
+ pictures.clear();
1001
+ shown = new WeakMap();
976
1002
  observer.takeRecords();
977
1003
  last = now();
978
1004
  t0 = Date.now() - last;
@@ -174,6 +174,18 @@ export function compare(live: Document, copy: Document, o: OracleOptions = {}):
174
174
  )
175
175
  continue;
176
176
  if (side === "live" && masked && !PLAIN.test(name)) v = stars(v);
177
+ // The copy shows the picture the visitor's browser picked, whatever its own would.
178
+ if (side === "live" && name === "srcset") {
179
+ const parent = el.parentNode as Element | null;
180
+ const img = (
181
+ tag === "img"
182
+ ? el
183
+ : tag === "source" && parent && tagOf(parent) === "picture"
184
+ ? Array.from(parent.children).find((c) => tagOf(c) === "img")
185
+ : undefined
186
+ ) as HTMLImageElement | undefined;
187
+ if (img?.currentSrc && img.currentSrc !== img.src) v = img.currentSrc;
188
+ }
177
189
  if (URL_ATTR.test(name)) {
178
190
  v = abs(v);
179
191
  if (side === "live" && !SAFE_URL.test(v)) v = "about:blank";
@@ -206,13 +218,18 @@ export function compare(live: Document, copy: Document, o: OracleOptions = {}):
206
218
  const want = shown(L.value, fieldHidden(L, masked, clear));
207
219
  if (want !== R.value) add(p, "textarea.value", want, R.value);
208
220
  } else if (tag === "select") {
221
+ // A single select shows its `selectedIndex`: Safari 16 may also report as chosen the
222
+ // option given `selected` by script, which it does not show.
209
223
  const picked = (s: HTMLSelectElement) =>
210
- Array.from(s.options, (x, i) => (x.selected ? i : -1))
211
- .filter((i) => i >= 0)
212
- .join(",");
224
+ s.multiple || s.size > 1
225
+ ? Array.from(s.options, (x, i) => (x.selected ? i : -1))
226
+ .filter((i) => i >= 0)
227
+ .join(",")
228
+ : s.selectedIndex < 0
229
+ ? ""
230
+ : String(s.selectedIndex);
213
231
  let want = picked(L);
214
232
  if (fieldHidden(L, masked, clear)) {
215
- // A hidden choice: the copy shows what the markup alone selects.
216
233
  // A hidden choice: the copy shows what the markup alone selects.
217
234
  const options = Array.from(L.options as ArrayLike<HTMLOptionElement>);
218
235
  if (L.multiple || L.size > 1) want = options.map((x, i) => (hasA(x, "selected") ? i : -1)).filter((i) => i >= 0).join(",");