@full-self-browsing/phantom-stream 0.1.0 → 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.
Files changed (64) hide show
  1. package/README.md +48 -2
  2. package/dist/types/adapters/extension.d.ts.map +1 -1
  3. package/dist/types/adapters/playwright.d.ts.map +1 -1
  4. package/dist/types/capture/index.d.ts +150 -0
  5. package/dist/types/capture/index.d.ts.map +1 -1
  6. package/dist/types/index.d.ts +2 -0
  7. package/dist/types/player/index.d.ts +136 -0
  8. package/dist/types/player/index.d.ts.map +1 -0
  9. package/dist/types/protocol/constants.d.ts +22 -0
  10. package/dist/types/protocol/constants.d.ts.map +1 -1
  11. package/dist/types/protocol/index.d.ts +1 -0
  12. package/dist/types/protocol/media-reconcile.d.ts +53 -0
  13. package/dist/types/protocol/media-reconcile.d.ts.map +1 -0
  14. package/dist/types/protocol/messages.d.ts +326 -4
  15. package/dist/types/protocol/messages.d.ts.map +1 -1
  16. package/dist/types/recorder/index.d.ts +207 -0
  17. package/dist/types/recorder/index.d.ts.map +1 -0
  18. package/dist/types/renderer/asset-policy.d.ts +72 -0
  19. package/dist/types/renderer/asset-policy.d.ts.map +1 -0
  20. package/dist/types/renderer/diff.d.ts.map +1 -1
  21. package/dist/types/renderer/index.d.ts +61 -8
  22. package/dist/types/renderer/index.d.ts.map +1 -1
  23. package/dist/types/renderer/media-player.d.ts +114 -0
  24. package/dist/types/renderer/media-player.d.ts.map +1 -0
  25. package/dist/types/renderer/overlays.d.ts +12 -0
  26. package/dist/types/renderer/overlays.d.ts.map +1 -1
  27. package/dist/types/renderer/parser-safe.d.ts +26 -0
  28. package/dist/types/renderer/parser-safe.d.ts.map +1 -0
  29. package/dist/types/renderer/sanitize.d.ts +22 -0
  30. package/dist/types/renderer/sanitize.d.ts.map +1 -1
  31. package/dist/types/renderer/snapshot.d.ts +38 -0
  32. package/dist/types/renderer/snapshot.d.ts.map +1 -1
  33. package/dist/types/transport/websocket.d.ts.map +1 -1
  34. package/docs/ARCHITECTURE.md +83 -4
  35. package/docs/QUICKSTARTS.md +64 -0
  36. package/docs/SECURITY.md +279 -2
  37. package/docs/paper/OUTLINE.md +24 -91
  38. package/examples/bookmarklet-demo/viewer.html +12 -3
  39. package/examples/extension-mv3/viewer.html +10 -3
  40. package/package.json +25 -1
  41. package/src/adapters/extension.js +202 -7
  42. package/src/adapters/playwright-inject.js +645 -28
  43. package/src/adapters/playwright.js +176 -2
  44. package/src/capture/README.md +96 -1
  45. package/src/capture/index.js +1672 -40
  46. package/src/index.js +2 -0
  47. package/src/player/README.md +175 -0
  48. package/src/player/index.js +627 -0
  49. package/src/protocol/constants.js +24 -0
  50. package/src/protocol/index.js +1 -0
  51. package/src/protocol/media-reconcile.js +188 -0
  52. package/src/protocol/messages.js +195 -4
  53. package/src/recorder/README.md +225 -0
  54. package/src/recorder/index.js +487 -0
  55. package/src/renderer/README.md +111 -8
  56. package/src/renderer/asset-policy.js +172 -0
  57. package/src/renderer/diff.js +158 -15
  58. package/src/renderer/index.js +978 -21
  59. package/src/renderer/media-player.js +367 -0
  60. package/src/renderer/overlays.js +324 -0
  61. package/src/renderer/parser-safe.js +111 -0
  62. package/src/renderer/sanitize.js +25 -3
  63. package/src/renderer/snapshot.js +534 -3
  64. package/src/transport/websocket.js +36 -0
package/src/index.js CHANGED
@@ -3,3 +3,5 @@ export * from './capture/index.js';
3
3
  export * from './renderer/index.js';
4
4
  export * from './relay/index.js';
5
5
  export * from './transport/websocket.js';
6
+ export * from './recorder/index.js';
7
+ export * from './player/index.js';
@@ -0,0 +1,175 @@
1
+ # player
2
+
3
+ Seekable, speed-controlled, inert playback of a PhantomStream recording (see
4
+ `src/recorder/`). The player creates a viewer (`createViewer`) in the host
5
+ container over a private transport and feeds it recorded messages, so a
6
+ recording renders exactly as the live stream did, through the same
7
+ sanitizers, srcdoc CSP, sandbox, and asset gate. The player writes no HTML
8
+ itself, and the sandbox stays exactly `allow-same-origin`.
9
+
10
+ The module touches no DOM, timers, or frames at import, so it imports in
11
+ bare Node.
12
+
13
+ ## Factory
14
+
15
+ ```js
16
+ import { createPlayer } from '@full-self-browsing/phantom-stream/player';
17
+
18
+ const player = createPlayer({ container, recording: JSON.parse(text) });
19
+ await player.seek(0); // nothing renders until seek() or play()
20
+ player.play({ speed: 2 });
21
+ // -> { seek, play, pause, setSpeed, currentTime, duration, playing, markers, on, destroy }
22
+ ```
23
+
24
+ | Option | Required | Default | Purpose |
25
+ |--------|----------|---------|---------|
26
+ | `container` | yes | — | Host element the playback viewer is appended into. It must be connected to a document: the mirror only loads in a connected iframe. The factory throws `Error('player-container-required')` when it has no `appendChild`. |
27
+ | `recording` | yes | — | A recording (`{ format, version, startedAt, endedAt, meta, events }`) or a bare array of `{ t, type, payload }` events. Validated with `validateRecording` at creation; failures throw `Error('player-recording-invalid:' + reason)`, for example `player-recording-invalid:recording-no-snapshot`. |
28
+ | `logger` | no | console-backed (`info`/`warn`/`error`) | Shared with the viewer. Host event handler errors log as `'[Player] event handler failed'`. |
29
+ | `document` | no | — | Construction-document fallback, passed to `createViewer`. |
30
+ | `skipIdleMs` | no | `0` | During play, an idle gap never plays for more than this many recording ms. `0` plays every gap in full. Negative or non-finite values throw `Error('player-skip-idle-invalid')`. |
31
+ | `inert` | no | `true` | Make the mirror inert (below). Pass `false` only for trusted recordings. |
32
+ | `mediaMode` | no | `'reference'` | `'reference'`, `'poster'`, or `'off'`, passed to `createViewer` (which throws `Error('viewer-mediamode-invalid')` otherwise). |
33
+ | `assetOriginPolicy` | no | — | Asset origin hook, passed to `createViewer` unchanged. |
34
+ | `allowAssetOrigins` | no | — | Asset host allowlist, passed to `createViewer` unchanged. |
35
+
36
+ Creation is synchronous and deterministic: it validates, builds the viewer,
37
+ applies the inert default, and renders nothing.
38
+
39
+ ## Handle contract
40
+
41
+ - `seek(ms)` — show exactly the recorded state at `ms` (clamped to
42
+ `[0, duration]`, non-finite means `0`). Returns a promise that resolves
43
+ once that state is applied, or once a newer seek supersedes this one.
44
+ Resolves immediately after `destroy()`.
45
+ - `play({ speed }?)` — start the frame clock. A `speed` outside
46
+ `[0.25, 16]` (or not a number) throws `Error('player-speed-invalid')`
47
+ synchronously. From the end of the recording, play restarts at `0`.
48
+ Returns a promise that resolves once the first frame is scheduled.
49
+ - `pause()` — stop the clock at the current frame time.
50
+ - `setSpeed(x)` — change the speed with the same validation as `play`.
51
+ It applies from the next frame.
52
+ - `currentTime`, `duration`, `playing` — read-only getters. Times are ms
53
+ from the first event; `duration` is the later of the last event and the
54
+ recording's `endedAt`, minus the first event's `t`.
55
+ - `markers()` — fresh copies of every host mark as
56
+ `{ ...data, kind, t }` with `t` in ms from the first event.
57
+ - `on(eventName, handler)` — subscribe (below). Returns an unsubscribe
58
+ function. Unknown names, or a handler that is not a function, throw
59
+ `Error('player-event-unsupported')`.
60
+ - `destroy()` — stop playback, resolve any waiting seek, destroy the viewer
61
+ (its root leaves the container), and drop every subscription. Idempotent;
62
+ later `seek()` and `play()` calls resolve without effect, and `markers()`
63
+ keeps working.
64
+
65
+ ## Events
66
+
67
+ | Event | Payload | When | Replayed on subscribe |
68
+ |-------|---------|------|-----------------------|
69
+ | `time` | `{ currentTime, duration }` | Once per frame during play and once per completed seek. | no |
70
+ | `marker` | `{ ...data, kind, t }` | During `play()` only, as the clock passes each host mark. Seeks do not "pass" marks. | no |
71
+ | `ended` | `{ currentTime, duration }` | Once, when play reaches the duration. | no |
72
+ | `state` | `{ state: 'paused' \| 'playing' \| 'ended', currentTime }` | On each playback state change. A seek from `'ended'` returns to `'paused'`. | yes, immediately |
73
+ | `loaded` | `{ streamSessionId, snapshotId }` | Each time the viewer finishes loading a snapshot document (forwarded from the viewer's `loaded` event), before the seek that fed it applies later events. | no |
74
+
75
+ Each handler receives its own payload copy, and a throwing handler never
76
+ stops the others.
77
+
78
+ ## Seek semantics
79
+
80
+ 1. Pick the keyframe: the nearest snapshot at or before the target, or the
81
+ first snapshot when none is that early.
82
+ 2. Feed it to the viewer and wait for its `loaded` event. The viewer drops
83
+ mutations that arrive before a snapshot's document loads, so nothing is
84
+ applied until then.
85
+ 3. Apply every later event with a time at or before the target, in order.
86
+
87
+ When seeks overlap, the last one wins: an older seek stops at its next wait,
88
+ and its promise resolves without applying anything more. At most one
89
+ snapshot load is in flight; a seek that arrives during a load waits for it to
90
+ settle, then plans against its own target.
91
+
92
+ A forward seek inside the keyframe window that is already loaded (same
93
+ keyframe, target not behind the current time) applies only the new events
94
+ and does not re-feed the snapshot. Backward seeks and keyframe changes
95
+ rebuild from the keyframe. Seek cost therefore grows with the distance from
96
+ the last snapshot; see the recorder README for keyframe guidance.
97
+
98
+ ## Play semantics
99
+
100
+ - The clock reads only `requestAnimationFrame` timestamps (with a 16 ms
101
+ timer fallback when the window has none) and advances by frame time
102
+ times speed (`PLAYER_MIN_SPEED` 0.25 to `PLAYER_MAX_SPEED` 16).
103
+ - When the clock reaches a snapshot, it holds at that snapshot's time until
104
+ the document loads, then the next frame rebases instead of jumping ahead,
105
+ so no event is applied to the old document.
106
+ - With `skipIdleMs > 0`, when the next event (or the end) is more than
107
+ `skipIdleMs` ahead, the clock jumps so it is exactly `skipIdleMs` ahead.
108
+ No event is ever skipped.
109
+ - At the duration, play stops, `state` becomes `'ended'`, and `ended`
110
+ fires once. Calling `play()` again restarts at `0`.
111
+
112
+ ## Overlay continuity
113
+
114
+ An anchored overlay (the agent highlight) is positioned when its overlay
115
+ message arrives. The player caches the latest overlay for the loaded
116
+ snapshot identity and re-dispatches it after each load and after each
117
+ mutation batch, so the highlight follows its element and hides once that
118
+ element is removed.
119
+
120
+ ## Media
121
+
122
+ Recorded `STREAM.MEDIA` messages carry the capture clock in `sentAt`. The
123
+ player rewrites `sentAt` onto the playback clock, on a copy (the recording is
124
+ never mutated), so the viewer's reconciler expects exactly the recorded media
125
+ position at the current playback time. `playbackRate` is not scaled, because
126
+ browsers throw outside their supported range: at speeds other than 1x the
127
+ mirrored element plays at its recorded rate and resyncs at each recorded
128
+ media update. Media loads by reference at playback time (see Security).
129
+
130
+ ## Resync policy
131
+
132
+ When the viewer detects drift (stale misses), it asks its transport for a
133
+ fresh snapshot with `CONTROL.START`. The player answers by re-seeking the
134
+ current time with a forced re-feed, at most once per keyframe window; the
135
+ record of resynced windows is cleared on every host `seek()`. A recording
136
+ that keeps drifting therefore costs one extra re-feed, never a loop.
137
+
138
+ ## Ignored controls and events
139
+
140
+ The player ignores every viewer control except `CONTROL.START`: subtree
141
+ requests and remote-control frames have no capture to answer them. It
142
+ never sends `STREAM.READY`, recorded `STREAM.SUBTREE_RESPONSE` messages
143
+ (they answer request ids this viewer never issued), host marks, or
144
+ snapshots without string `html` to the viewer. Everything else is
145
+ dispatched as recorded.
146
+
147
+ ## Inert mirror
148
+
149
+ A sandboxed srcdoc frame runs no script, but it can still follow a link
150
+ click (navigation needs no script) and take keyboard focus. By default the
151
+ player therefore sets the `inert` attribute, `pointer-events: none`, and
152
+ `tabindex="-1"` on the mirror iframe, through DOM attribute and style APIs
153
+ only. Clicks and keys never reach mirrored links or forms, and the mirror
154
+ never navigates away from the recording. `inert: false` leaves the mirror
155
+ interactive; use it only for recordings you trust.
156
+
157
+ ## Environment
158
+
159
+ - The container must be connected to a document; the mirror only loads in a
160
+ connected iframe, so `seek()` never resolves in a detached container.
161
+ - Nothing renders until `seek()` or `play()`.
162
+ - jsdom never parses srcdoc. Tests write the srcdoc into
163
+ `contentDocument` with the close-only glue
164
+ (`cd.open(); cd.write(iframe.getAttribute('srcdoc')); cd.close();`) after
165
+ every snapshot the player feeds; `close()` fires the one load the player
166
+ waits for. Never dispatch an extra load by hand (see
167
+ `tests/player.test.js`).
168
+ - Background tabs throttle `requestAnimationFrame`, so play advances only
169
+ while the page is visible.
170
+
171
+ ## Security
172
+
173
+ Recordings are attacker-influenced input exactly like a live stream, and
174
+ they hold page content at rest. See `docs/SECURITY.md` (Recordings and
175
+ playback).