@voila.dev/replay 0.3.0 → 0.4.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.
package/README.md CHANGED
@@ -31,6 +31,7 @@ player.play();
31
31
  - **A page view has a budget.** Past 8 MB of tape (`budget`), `END 2` goes on the next tape and recording stops. A module that throws is cut alone: `ERR` and its name on the tape, said again in each snapshot, and the rest goes on.
32
32
  - **Back from the back/forward cache, a new snapshot is due.** The beacon on the way out may not have held all the tape: `needsSnap` turns true on `pageshow` with `persisted`, and the page view goes on from a new snapshot.
33
33
  - **Seeking back does not replay from the start.** Every 10 s of tape, the player keeps a checkpoint: a copy of the rebuilt page in a document of no window, its node numbers, and what a copy does not carry (constructed sheets and who adopts them, canvas pixels, modal dialogs and popovers, scroll positions, frames' documents). A seek starts from the nearest checkpoint or snapshot before it. Past 64 checkpoints, one in two goes.
34
+ - **Pictures load from their copies.** With `asset(url)`, every address of a picture or a font goes to the copy the host kept. Of a `srcset`, only the candidates with a copy stay, so the viewer's screen never picks one from the site as it is today; all of them when none was kept. With `sources`, the copy loads pictures, fonts and media only from those origins (and `data:`, `blob:`): an address left uncopied is refused, not fetched.
34
35
  - **The copy cannot act.** It plays in a sandboxed iframe without scripts; event handlers, `javascript:` URLs, `<script>`, `<base>` and `target` are dropped.
35
36
 
36
37
  ## Browsers
@@ -11,6 +11,12 @@ export interface PlayerOptions {
11
11
  readonly every?: number;
12
12
  /** Where to load a picture or a font from, given its address on the page (see `MirrorHooks`). */
13
13
  readonly asset?: (url: string) => string;
14
+ /**
15
+ * The only origins the copy may load pictures, fonts and media from (`https://cdn.example`),
16
+ * besides `data:` and `blob:`: the address of a picture left uncopied is then refused rather
17
+ * than fetched from the recorded site. Anything loads when left out.
18
+ */
19
+ readonly sources?: ReadonlyArray<string>;
14
20
  }
15
21
  export interface Gap {
16
22
  /** Ms from the start of the recording. */
package/dist/play.js CHANGED
@@ -49,6 +49,16 @@ function mirror(doc, hooks = {}) {
49
49
  const kept = (url) => hooks.asset ? hooks.asset(url) : url;
50
50
  const css = (text, from = base) => text.replace(CSS_URL, (_m, _q, url) => `url("${kept(absolute(url, from))}")`);
51
51
  const local = (el) => localName.call(el);
52
+ function candidates(srcset) {
53
+ const parts = srcset.split(",").map((part) => part.trim()).filter(Boolean).map((part) => {
54
+ const [url = "", ...rest] = part.split(/\s+/);
55
+ const from = absolute(url);
56
+ const to = kept(from);
57
+ return { text: [to, ...rest].join(" "), held: to !== from };
58
+ });
59
+ const held = hooks.asset ? parts.filter((part) => part.held) : [];
60
+ return (held.length > 0 ? held : parts).map((part) => part.text).join(", ");
61
+ }
52
62
  function add(n) {
53
63
  const top = stack[stack.length - 1];
54
64
  if (!top) return;
@@ -65,15 +75,12 @@ function mirror(doc, hooks = {}) {
65
75
  if (/^on/i.test(name) || name.toLowerCase() === "http-equiv") return;
66
76
  const tag = local(el);
67
77
  if (tag === "iframe" && (name === "src" || name === "srcdoc")) return;
68
- if (ADDRESS.test(name)) {
78
+ if (ADDRESS.test(name) && value.trim().startsWith("#")) value = value.trim();
79
+ else if (ADDRESS.test(name)) {
69
80
  value = absolute(value);
70
81
  if (!SAFE_URL.test(value)) value = "about:blank";
71
82
  else if (tag !== "link") value = kept(value);
72
- } else if (name === "srcset")
73
- value = value.split(",").map((part) => {
74
- const [url, ...rest] = part.trim().split(/\s+/);
75
- return [kept(absolute(url || "")), ...rest].join(" ");
76
- }).join(", ");
83
+ } else if (name === "srcset") value = candidates(value);
77
84
  else if (name === "style") value = css(value);
78
85
  else if (tag === "a" && name === "target") return;
79
86
  try {
@@ -712,6 +719,16 @@ function createPlayer(options) {
712
719
  stage.append(iframe, cursor);
713
720
  root.append(stage);
714
721
  const doc = iframe.contentDocument;
722
+ if (options.sources) {
723
+ const from = ["data:", "blob:", ...options.sources].join(" ");
724
+ const policy = doc.createElement("meta");
725
+ policy.setAttribute("http-equiv", "Content-Security-Policy");
726
+ policy.setAttribute(
727
+ "content",
728
+ `default-src 'none'; img-src ${from}; font-src ${from}; media-src ${from}; style-src 'unsafe-inline' ${from}`
729
+ );
730
+ (doc.head ?? doc.documentElement ?? doc).appendChild(policy);
731
+ }
715
732
  const listeners = {};
716
733
  function emit(event, payload) {
717
734
  const set = listeners[event];
package/dist/testing.js CHANGED
@@ -123,6 +123,21 @@ var CASES = [
123
123
  d.getElementById("b").textContent = "apr\xE8s";
124
124
  }
125
125
  },
126
+ {
127
+ name: 'symbole SVG repris par <use href="#\u2026">, et lien vers une ancre',
128
+ group: "parseur",
129
+ html: D(
130
+ '<svg width="0" height="0"><symbol id="star" viewBox="0 0 10 10"><path d="M5 0L10 10H0z"/></symbol></svg><a href="#b">aller</a><div id="a"></div><div id="b">avant</div>'
131
+ ),
132
+ run: (d) => {
133
+ const svg = d.createElementNS("http://www.w3.org/2000/svg", "svg");
134
+ const use = d.createElementNS("http://www.w3.org/2000/svg", "use");
135
+ use.setAttribute("href", "#star");
136
+ svg.append(use);
137
+ d.getElementById("a")?.append(svg);
138
+ d.getElementById("b").textContent = "apr\xE8s";
139
+ }
140
+ },
126
141
  {
127
142
  name: "les cinq pi\xE8ges, pr\xE9sents d\xE8s le d\xE9part",
128
143
  group: "parseur",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@voila.dev/replay",
3
- "version": "0.3.0",
3
+ "version": "0.4.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",
package/src/play/index.ts CHANGED
@@ -18,6 +18,12 @@ export interface PlayerOptions {
18
18
  readonly every?: number;
19
19
  /** Where to load a picture or a font from, given its address on the page (see `MirrorHooks`). */
20
20
  readonly asset?: (url: string) => string;
21
+ /**
22
+ * The only origins the copy may load pictures, fonts and media from (`https://cdn.example`),
23
+ * besides `data:` and `blob:`: the address of a picture left uncopied is then refused rather
24
+ * than fetched from the recorded site. Anything loads when left out.
25
+ */
26
+ readonly sources?: ReadonlyArray<string>;
21
27
  }
22
28
 
23
29
  export interface Gap {
@@ -87,6 +93,18 @@ export function createPlayer(options: PlayerOptions): Player {
87
93
  stage.append(iframe, cursor);
88
94
  root.append(stage);
89
95
  const doc = iframe.contentDocument as Document;
96
+ // Set before the first node is built; it holds for the document's life, whatever the
97
+ // snapshots later put in its place.
98
+ if (options.sources) {
99
+ const from = ["data:", "blob:", ...options.sources].join(" ");
100
+ const policy = doc.createElement("meta");
101
+ policy.setAttribute("http-equiv", "Content-Security-Policy");
102
+ policy.setAttribute(
103
+ "content",
104
+ `default-src 'none'; img-src ${from}; font-src ${from}; media-src ${from}; style-src 'unsafe-inline' ${from}`,
105
+ );
106
+ (doc.head ?? doc.documentElement ?? doc).appendChild(policy);
107
+ }
90
108
 
91
109
  const listeners: { [K in keyof Events]?: Set<(p: Events[K]) => void> } = {};
92
110
  function emit<K extends keyof Events>(event: K, payload: Events[K]) {
@@ -110,6 +110,23 @@ export function mirror(doc: Document, hooks: MirrorHooks = {}): Mirror {
110
110
  const css = (text: string, from = base) =>
111
111
  text.replace(CSS_URL, (_m, _q, url: string) => `url("${kept(absolute(url, from))}")`);
112
112
  const local = (el: Element) => localName.call(el);
113
+ // The viewer's screen picks among them, not the visitor's: a candidate with no copy kept
114
+ // would load from the site as it is today. Only the kept ones stay; all of them when none
115
+ // was kept, for a recording older than its copies.
116
+ function candidates(srcset: string): string {
117
+ const parts = srcset
118
+ .split(",")
119
+ .map((part) => part.trim())
120
+ .filter(Boolean)
121
+ .map((part) => {
122
+ const [url = "", ...rest] = part.split(/\s+/);
123
+ const from = absolute(url);
124
+ const to = kept(from);
125
+ return { text: [to, ...rest].join(" "), held: to !== from };
126
+ });
127
+ const held = hooks.asset ? parts.filter((part) => part.held) : [];
128
+ return (held.length > 0 ? held : parts).map((part) => part.text).join(", ");
129
+ }
113
130
 
114
131
  function add(n: Node) {
115
132
  const top = stack[stack.length - 1];
@@ -129,19 +146,15 @@ export function mirror(doc: Document, hooks: MirrorHooks = {}): Mirror {
129
146
  if (/^on/i.test(name) || name.toLowerCase() === "http-equiv") return;
130
147
  const tag = local(el);
131
148
  if (tag === "iframe" && (name === "src" || name === "srcdoc")) return;
132
- if (ADDRESS.test(name)) {
149
+ // A reference inside the page (`<use href="#icon">`, `<a href="#main">`) stays one: made
150
+ // absolute, it would point at the recorded site, which the copy may not load.
151
+ if (ADDRESS.test(name) && value.trim().startsWith("#")) value = value.trim();
152
+ else if (ADDRESS.test(name)) {
133
153
  value = absolute(value);
134
154
  if (!SAFE_URL.test(value)) value = "about:blank";
135
155
  // A stylesheet's own address stays: its rules came on the tape, read from there.
136
156
  else if (tag !== "link") value = kept(value);
137
- } else if (name === "srcset")
138
- value = value
139
- .split(",")
140
- .map((part) => {
141
- const [url, ...rest] = part.trim().split(/\s+/);
142
- return [kept(absolute(url || "")), ...rest].join(" ");
143
- })
144
- .join(", ");
157
+ } else if (name === "srcset") value = candidates(value);
145
158
  else if (name === "style") value = css(value);
146
159
  else if (tag === "a" && name === "target") return;
147
160
  try {
@@ -150,6 +150,21 @@ export const CASES: ReadonlyArray<Case> = [
150
150
  (d.getElementById("b") as Element).textContent = "après";
151
151
  },
152
152
  },
153
+ {
154
+ name: "symbole SVG repris par <use href=\"#…\">, et lien vers une ancre",
155
+ group: "parseur",
156
+ html: D(
157
+ '<svg width="0" height="0"><symbol id="star" viewBox="0 0 10 10"><path d="M5 0L10 10H0z"/></symbol></svg><a href="#b">aller</a><div id="a"></div><div id="b">avant</div>',
158
+ ),
159
+ run: (d) => {
160
+ const svg = d.createElementNS("http://www.w3.org/2000/svg", "svg");
161
+ const use = d.createElementNS("http://www.w3.org/2000/svg", "use");
162
+ use.setAttribute("href", "#star");
163
+ svg.append(use);
164
+ d.getElementById("a")?.append(svg);
165
+ (d.getElementById("b") as Element).textContent = "après";
166
+ },
167
+ },
153
168
  {
154
169
  name: "les cinq pièges, présents dès le départ",
155
170
  group: "parseur",