@voila.dev/replay 0.3.1 → 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;
@@ -70,11 +80,7 @@ function mirror(doc, hooks = {}) {
70
80
  value = absolute(value);
71
81
  if (!SAFE_URL.test(value)) value = "about:blank";
72
82
  else if (tag !== "link") value = kept(value);
73
- } else if (name === "srcset")
74
- value = value.split(",").map((part) => {
75
- const [url, ...rest] = part.trim().split(/\s+/);
76
- return [kept(absolute(url || "")), ...rest].join(" ");
77
- }).join(", ");
83
+ } else if (name === "srcset") value = candidates(value);
78
84
  else if (name === "style") value = css(value);
79
85
  else if (tag === "a" && name === "target") return;
80
86
  try {
@@ -713,6 +719,16 @@ function createPlayer(options) {
713
719
  stage.append(iframe, cursor);
714
720
  root.append(stage);
715
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
+ }
716
732
  const listeners = {};
717
733
  function emit(event, payload) {
718
734
  const set = listeners[event];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@voila.dev/replay",
3
- "version": "0.3.1",
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];
@@ -137,14 +154,7 @@ export function mirror(doc: Document, hooks: MirrorHooks = {}): Mirror {
137
154
  if (!SAFE_URL.test(value)) value = "about:blank";
138
155
  // A stylesheet's own address stays: its rules came on the tape, read from there.
139
156
  else if (tag !== "link") value = kept(value);
140
- } else if (name === "srcset")
141
- value = value
142
- .split(",")
143
- .map((part) => {
144
- const [url, ...rest] = part.trim().split(/\s+/);
145
- return [kept(absolute(url || "")), ...rest].join(" ");
146
- })
147
- .join(", ");
157
+ } else if (name === "srcset") value = candidates(value);
148
158
  else if (name === "style") value = css(value);
149
159
  else if (tag === "a" && name === "target") return;
150
160
  try {