@full-self-browsing/phantom-stream 0.2.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.
- package/README.md +48 -2
- package/dist/types/adapters/playwright.d.ts.map +1 -1
- package/dist/types/capture/index.d.ts +38 -0
- package/dist/types/capture/index.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -0
- package/dist/types/player/index.d.ts +136 -0
- package/dist/types/player/index.d.ts.map +1 -0
- package/dist/types/protocol/messages.d.ts +60 -2
- package/dist/types/protocol/messages.d.ts.map +1 -1
- package/dist/types/recorder/index.d.ts +207 -0
- package/dist/types/recorder/index.d.ts.map +1 -0
- package/dist/types/renderer/diff.d.ts.map +1 -1
- package/dist/types/renderer/index.d.ts +48 -8
- package/dist/types/renderer/index.d.ts.map +1 -1
- package/dist/types/renderer/parser-safe.d.ts +26 -0
- package/dist/types/renderer/parser-safe.d.ts.map +1 -0
- package/docs/ARCHITECTURE.md +70 -0
- package/docs/QUICKSTARTS.md +64 -0
- package/docs/SECURITY.md +46 -0
- package/docs/paper/OUTLINE.md +24 -91
- package/package.json +17 -1
- package/src/adapters/playwright-inject.js +596 -29
- package/src/adapters/playwright.js +17 -2
- package/src/capture/README.md +96 -1
- package/src/capture/index.js +602 -30
- package/src/index.js +2 -0
- package/src/player/README.md +175 -0
- package/src/player/index.js +627 -0
- package/src/protocol/messages.js +50 -2
- package/src/recorder/README.md +225 -0
- package/src/recorder/index.js +487 -0
- package/src/renderer/README.md +111 -8
- package/src/renderer/diff.js +57 -18
- package/src/renderer/index.js +129 -12
- package/src/renderer/parser-safe.js +111 -0
package/src/protocol/messages.js
CHANGED
|
@@ -63,7 +63,7 @@ export { REMOTE_CONTROL_STATE_VALUES as REMOTE_CONTROL_STATE };
|
|
|
63
63
|
|
|
64
64
|
/** Diff op codes carried in STREAM.MUTATIONS payloads. */
|
|
65
65
|
export const DIFF_OP = {
|
|
66
|
-
/** { op:'add', parentNid, html, beforeNid|null, nodeIds:string[] } — insert serialized subtree */
|
|
66
|
+
/** { op:'add', parentNid, html, beforeNid|null, nodeIds:string[] } — insert serialized subtree; parentNid is a tracked element nid, or BODY_NID for a direct child of the main <body> */
|
|
67
67
|
ADD: 'add',
|
|
68
68
|
/** { op:'rm', nid } — remove subtree */
|
|
69
69
|
REMOVE: 'rm',
|
|
@@ -89,6 +89,51 @@ export const DIFF_OP = {
|
|
|
89
89
|
*/
|
|
90
90
|
export const NID_ATTR = 'data-fsb-nid';
|
|
91
91
|
|
|
92
|
+
/**
|
|
93
|
+
* Reserved identity for the MAIN document `<body>` (quick task 261127-u5c).
|
|
94
|
+
*
|
|
95
|
+
* The snapshot sidecar starts below `<body>`, so the body itself never gets a
|
|
96
|
+
* counter nid. This value is non-numeric, so it can never collide with the
|
|
97
|
+
* capture counter (which only ever yields `String(n)` for n >= 1), and it
|
|
98
|
+
* consumes no counter value, so nodeIds sidecars keep their exact numbering.
|
|
99
|
+
*
|
|
100
|
+
* - Carried on every main snapshot as SnapshotPayload.bodyNid.
|
|
101
|
+
* - The add-op parentNid for a direct child of the main `<body>` (toasts,
|
|
102
|
+
* modals, portals); beforeNid resolution is unchanged.
|
|
103
|
+
* - The attr-op nid for a `<body>` attribute change other than `style` (the
|
|
104
|
+
* snapshot shell bodyStyle owns the mirror body's style attribute).
|
|
105
|
+
* - Not returned by capture getNodeId, and not a subtree-request target: the
|
|
106
|
+
* body is a structural anchor for body-level diffs, not an addressable
|
|
107
|
+
* element.
|
|
108
|
+
*
|
|
109
|
+
* Same-origin frame documents keep their counter htmlNid/bodyNid
|
|
110
|
+
* (FramePayload); only the main document body uses this value.
|
|
111
|
+
*/
|
|
112
|
+
export const BODY_NID = 'body';
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Parser-safe placeholder element and attribute (quick task 261202-jwf).
|
|
116
|
+
*
|
|
117
|
+
* A DOM built by scripts can hold nesting that HTML text cannot express: a
|
|
118
|
+
* `<button>` inside a `<button>`, an `<a>` inside an `<a>`, a `<div>` inside a
|
|
119
|
+
* `<p>`, a `<tr>` without a `<tbody>`, an `<img>` with children. Re-parsing the
|
|
120
|
+
* serialized HTML rewrites such a tree, which moves content and breaks the
|
|
121
|
+
* positional nodeIds pairing.
|
|
122
|
+
*
|
|
123
|
+
* - Capture renames each such element in its detached wire clone to
|
|
124
|
+
* `<ps-node>` (PARSER_SAFE_TAG), copying every attribute and setting
|
|
125
|
+
* PARSER_SAFE_TAG_ATTR to the lowercase original tag. Element order and
|
|
126
|
+
* count are unchanged, so nodeIds sidecars stay aligned. Capture strips
|
|
127
|
+
* PARSER_SAFE_TAG_ATTR from page-owned `<ps-node>` elements.
|
|
128
|
+
* - Viewers restore only an allowlist of tags (never script, iframe, object,
|
|
129
|
+
* embed, style, link, meta, base, html, head, body or frameset), right
|
|
130
|
+
* after each parse and before the post-parse scrub and identity pairing.
|
|
131
|
+
* - Viewers older than 0.3.0 render the placeholders as generic elements:
|
|
132
|
+
* structure and nid pairing stay intact, only the original tags are lost.
|
|
133
|
+
*/
|
|
134
|
+
export const PARSER_SAFE_TAG = 'ps-node';
|
|
135
|
+
export const PARSER_SAFE_TAG_ATTR = 'data-ps-tag';
|
|
136
|
+
|
|
92
137
|
/**
|
|
93
138
|
* @typedef {Object} StyleScope
|
|
94
139
|
* @property {'document'|'shadow'|'frame'} kind
|
|
@@ -131,7 +176,7 @@ export const NID_ATTR = 'data-fsb-nid';
|
|
|
131
176
|
/**
|
|
132
177
|
* @typedef {Object} AddDiffOp
|
|
133
178
|
* @property {'add'} op
|
|
134
|
-
* @property {string} parentNid Parent node id in the current mirror
|
|
179
|
+
* @property {string} parentNid Parent node id in the current mirror, or BODY_NID for a direct child of the main <body>
|
|
135
180
|
* @property {string} html Serialized subtree HTML, without framework identity attrs
|
|
136
181
|
* @property {string|null} beforeNid Insert before this sibling nid, or append when null
|
|
137
182
|
* @property {string[]} nodeIds Preorder ids for every serialized element in html
|
|
@@ -156,6 +201,8 @@ export const NID_ATTR = 'data-fsb-nid';
|
|
|
156
201
|
* @property {string} kind Frame policy kind, e.g. 'same-origin' or 'cross-origin'
|
|
157
202
|
* @property {string} [html] Serialized frame body HTML for accessible frames
|
|
158
203
|
* @property {string[]} [nodeIds] Preorder ids for serialized frame elements
|
|
204
|
+
* @property {string} [htmlNid] Counter nid of the frame document <html> (empty string when unavailable)
|
|
205
|
+
* @property {string} [bodyNid] Counter nid of the frame document <body> (empty string when unavailable)
|
|
159
206
|
* @property {string[]} [stylesheets] Absolutified stylesheet URLs
|
|
160
207
|
* @property {string[]} [inlineStyles] Sanitized inline style blocks
|
|
161
208
|
* @property {StyleSource[]} [styleSources] Scoped CSSOM sources for this frame document
|
|
@@ -283,6 +330,7 @@ export const NID_ATTR = 'data-fsb-nid';
|
|
|
283
330
|
* @typedef {Object} SnapshotPayload
|
|
284
331
|
* @property {string} html Serialized body innerHTML (style-inlined; framework identity-clean)
|
|
285
332
|
* @property {string[]} nodeIds Preorder ids for every serialized element in html
|
|
333
|
+
* @property {string} [bodyNid] Reserved main <body> identity, always BODY_NID (absent on captures older than this fix)
|
|
286
334
|
* @property {ShadowRootPayload[]} [shadowRoots] Open shadow roots keyed by host nid
|
|
287
335
|
* @property {FramePayload[]} [frames] Frame sidecars keyed by iframe nid
|
|
288
336
|
* @property {boolean} truncated True if the size budget forced subtree drops
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
# recorder
|
|
2
|
+
|
|
3
|
+
Timestamped capture transport and the portable recording format. The
|
|
4
|
+
recorder is a `Transport` that capture sends into directly: every message is
|
|
5
|
+
stamped `{ t, type, payload }` with the recorder clock, so a recording holds
|
|
6
|
+
exactly the post-masking wire messages capture sent. Play recordings back
|
|
7
|
+
with the player (`src/player/`).
|
|
8
|
+
|
|
9
|
+
The module has no DOM access, no timers, and no side effects at import, so it
|
|
10
|
+
imports in bare Node, workers, and pages alike.
|
|
11
|
+
|
|
12
|
+
## Factory
|
|
13
|
+
|
|
14
|
+
```js
|
|
15
|
+
import { createRecorder } from '@full-self-browsing/phantom-stream/recorder';
|
|
16
|
+
|
|
17
|
+
const recorder = createRecorder({
|
|
18
|
+
now: () => Date.now(),
|
|
19
|
+
onChunk: undefined,
|
|
20
|
+
chunkMaxEvents: 200,
|
|
21
|
+
chunkMaxMs: 1000,
|
|
22
|
+
recordReady: false,
|
|
23
|
+
});
|
|
24
|
+
// -> { transport, mark, overlay, identity, flush, toRecording }
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
| Option | Required | Default | Purpose |
|
|
28
|
+
|--------|----------|---------|---------|
|
|
29
|
+
| `now` | no | `() => Date.now()` | Clock in ms. Every event gets `t = max(now(), previous t)`, so a wall-clock step back never produces a decreasing recording. A non-finite reading reuses the previous `t`. The factory throws `Error('recorder-now-invalid')` for a non-function. |
|
|
30
|
+
| `onChunk` | no | none (retained mode) | `(chunk) => void`. Turns on chunk mode (below): events are not retained and finished chunks are handed to this callback. The factory throws `Error('recorder-onchunk-invalid')` for a non-function. |
|
|
31
|
+
| `chunkMaxEvents` | no | `200` | Positive integer. Chunk mode emits a chunk once it holds this many events. Otherwise `Error('recorder-chunk-max-events-invalid')`. |
|
|
32
|
+
| `chunkMaxMs` | no | `1000` | Positive finite ms. Chunk mode emits the open chunk before an event that would make it span more than this. Otherwise `Error('recorder-chunk-max-ms-invalid')`. |
|
|
33
|
+
| `recordReady` | no | `false` | Record `STREAM.READY` too. Capture sends it once at factory creation; it carries no mirror state, so it is dropped by default. |
|
|
34
|
+
|
|
35
|
+
Factory-time validation is the only place the recorder throws.
|
|
36
|
+
|
|
37
|
+
## Handle contract
|
|
38
|
+
|
|
39
|
+
- `transport` — `{ send(type, payload), flush() }`, the capture transport.
|
|
40
|
+
`send` stamps and records one message (keeping the same payload
|
|
41
|
+
reference), drops `STREAM.READY` unless `recordReady`, and tracks the
|
|
42
|
+
identity of every snapshot with string `html`. A message whose type is not
|
|
43
|
+
a non-empty string or whose payload is not an object is not recorded, so
|
|
44
|
+
recorder output always passes the event shape check. `transport.flush` is
|
|
45
|
+
the same function as `recorder.flush`.
|
|
46
|
+
- `mark(kind, data?)` — record a host timeline mark
|
|
47
|
+
`{ t, type: 'host:mark', payload: { kind, ...data } }`. `kind` is stamped
|
|
48
|
+
last, so `data.kind` cannot override it, and the payload is a JSON clone
|
|
49
|
+
(functions and `undefined` values drop). Returns `false` and records
|
|
50
|
+
nothing when `kind` is not a non-empty string or `data` cannot be
|
|
51
|
+
serialized (a cycle or a BigInt). Marks are allowed before the first
|
|
52
|
+
snapshot.
|
|
53
|
+
- `overlay(payload)` — record a `STREAM.OVERLAY` for the current snapshot,
|
|
54
|
+
following capture's overlay rule (E1): `glow` and `progress` default to
|
|
55
|
+
`null`, and the identity keys are stamped last, so an input can never
|
|
56
|
+
spoof `streamSessionId` or `snapshotId`. Returns `false` and records
|
|
57
|
+
nothing before the first snapshot. The input object is never mutated.
|
|
58
|
+
- `identity()` — a fresh copy of `{ streamSessionId, snapshotId }` from the
|
|
59
|
+
latest snapshot, or `null` before the first one.
|
|
60
|
+
- `flush()` — record the end time (`max(now(), last t)`) and, in chunk mode,
|
|
61
|
+
emit the open chunk when it holds any event.
|
|
62
|
+
- `toRecording(meta?)` — the full recording in retained mode, or `null` in
|
|
63
|
+
chunk mode. `startedAt` is the first event's `t`; `endedAt` is the last
|
|
64
|
+
flush time, or `now()` when the recorder was never flushed, and never less
|
|
65
|
+
than the last event's `t`. `meta` is shallow-copied (a non-object becomes
|
|
66
|
+
`{}`).
|
|
67
|
+
|
|
68
|
+
## Wiring with capture
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
import { createCapture } from '@full-self-browsing/phantom-stream/capture';
|
|
72
|
+
import { createRecorder } from '@full-self-browsing/phantom-stream/recorder';
|
|
73
|
+
|
|
74
|
+
const recorder = createRecorder();
|
|
75
|
+
const capture = createCapture({ transport: recorder.transport });
|
|
76
|
+
|
|
77
|
+
capture.start();
|
|
78
|
+
// ... the session runs; the host may annotate it:
|
|
79
|
+
recorder.mark('action', { label: 'Click "Add to cart"' });
|
|
80
|
+
recorder.overlay({ glow: { state: 'active', nid: capture.getNodeId(button) } });
|
|
81
|
+
// ...
|
|
82
|
+
capture.stop(); // flushes pending mutations, then transport.flush() sets endedAt
|
|
83
|
+
|
|
84
|
+
const json = JSON.stringify(recorder.toRecording({ title: 'checkout' }));
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Capture contains transport throws, so a throwing `onChunk` is logged by
|
|
88
|
+
capture and never breaks the capture path.
|
|
89
|
+
|
|
90
|
+
## Recording format
|
|
91
|
+
|
|
92
|
+
```js
|
|
93
|
+
{
|
|
94
|
+
format: 'phantomstream-recording/1', // RECORDING_FORMAT
|
|
95
|
+
version: 1, // RECORDING_VERSION
|
|
96
|
+
startedAt: 1795849257000, // recorder clock ms of the first event
|
|
97
|
+
endedAt: 1795849263000, // recorder clock ms of the end
|
|
98
|
+
meta: { title: 'checkout' }, // host metadata
|
|
99
|
+
events: [
|
|
100
|
+
{ t: 1795849257000, type: 'ext:dom-snapshot', payload: { /* SnapshotPayload */ } },
|
|
101
|
+
{ t: 1795849257400, type: 'host:mark', payload: { kind: 'action', label: 'Click' } },
|
|
102
|
+
{ t: 1795849257520, type: 'ext:dom-mutations', payload: { /* mutations, identity */ } },
|
|
103
|
+
],
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
- Each event is `{ t, type, payload }`. `type` is a `STREAM.*` protocol
|
|
108
|
+
string exactly as capture sent it, or `HOST_MARK_TYPE` (`'host:mark'`) for
|
|
109
|
+
host marks, whose payload always carries a non-empty string `kind`.
|
|
110
|
+
- The time base is the recorder clock in ms (`Date.now()` by default).
|
|
111
|
+
Player time is ms from the first event: `t - events[0].t`.
|
|
112
|
+
- `t` never decreases, and playback needs at least one `STREAM.SNAPSHOT`
|
|
113
|
+
whose `payload.html` is a string.
|
|
114
|
+
- A recording is JSON-safe: capture payloads and host marks are plain data,
|
|
115
|
+
so `JSON.parse(JSON.stringify(recording))` validates and plays the same.
|
|
116
|
+
|
|
117
|
+
`createRecording({ events, meta, startedAt, endedAt })` builds this object.
|
|
118
|
+
`events` is copied; `meta` is shallow-copied (a non-object becomes `{}`);
|
|
119
|
+
`startedAt` and `endedAt` default to the first and last event times (`0` and
|
|
120
|
+
`0` with no events), and explicit finite values are kept. It never
|
|
121
|
+
validates. `validateRecording(rec)` does (below).
|
|
122
|
+
|
|
123
|
+
## Chunk mode
|
|
124
|
+
|
|
125
|
+
Pass `onChunk` to stream a recording out as it is made instead of holding it
|
|
126
|
+
in memory:
|
|
127
|
+
|
|
128
|
+
```js
|
|
129
|
+
const recorder = createRecorder({
|
|
130
|
+
onChunk(chunk) {
|
|
131
|
+
upload(chunk); // { seq, t0, t1, keyframe, events }
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
setInterval(() => recorder.flush(), 5000); // optional timed cut
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
- Chunks are keyframe-aligned: every `STREAM.SNAPSHOT` opens a new chunk, so
|
|
138
|
+
a chunk holds at most one snapshot, and only at index 0. `keyframe` is
|
|
139
|
+
`true` when `events[0]` is a snapshot; playback can start from such a
|
|
140
|
+
chunk without any earlier one.
|
|
141
|
+
- A chunk is emitted when it reaches `chunkMaxEvents`, before an event that
|
|
142
|
+
would make it span more than `chunkMaxMs`, or on `flush()`. An empty open
|
|
143
|
+
chunk emits nothing.
|
|
144
|
+
- There are no timers: a chunk is cut on the next event or on `flush()`.
|
|
145
|
+
Hosts that want timed uploads call `flush()` on their own interval.
|
|
146
|
+
- `onChunk` runs synchronously. The recorder places the event and updates
|
|
147
|
+
its own state (resets the open chunk, increments `seq`) before calling it,
|
|
148
|
+
so a throwing `onChunk` propagates to the caller without corrupting the
|
|
149
|
+
recorder or losing the event.
|
|
150
|
+
- Events are not retained, so `toRecording()` returns `null`. Rebuild a
|
|
151
|
+
recording from the stored chunks, in `seq` order:
|
|
152
|
+
|
|
153
|
+
```js
|
|
154
|
+
import { createRecording, validateRecording } from '@full-self-browsing/phantom-stream/recorder';
|
|
155
|
+
|
|
156
|
+
const recording = createRecording({
|
|
157
|
+
events: chunks.flatMap((c) => c.events),
|
|
158
|
+
meta: { title: 'checkout' },
|
|
159
|
+
});
|
|
160
|
+
validateRecording(recording); // -> { ok: true }
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Recording while also streaming live
|
|
164
|
+
|
|
165
|
+
Capture takes one transport. To record a session that is also mirrored live,
|
|
166
|
+
pass a host-written transport that fans out to both:
|
|
167
|
+
|
|
168
|
+
```js
|
|
169
|
+
const live = transport; // the transport you already stream with
|
|
170
|
+
const recorder = createRecorder();
|
|
171
|
+
|
|
172
|
+
const capture = createCapture({
|
|
173
|
+
transport: {
|
|
174
|
+
send(type, payload) {
|
|
175
|
+
recorder.transport.send(type, payload);
|
|
176
|
+
live.send(type, payload);
|
|
177
|
+
},
|
|
178
|
+
flush() {
|
|
179
|
+
recorder.transport.flush();
|
|
180
|
+
if (typeof live.flush === 'function') return live.flush();
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
The recorder keeps the payload reference it is given and never mutates it.
|
|
187
|
+
|
|
188
|
+
## validateRecording reasons
|
|
189
|
+
|
|
190
|
+
`validateRecording(rec)` returns `{ ok: true }` or
|
|
191
|
+
`{ ok: false, error }`. It never throws, checks structure in this order, and
|
|
192
|
+
reports the first fault only:
|
|
193
|
+
|
|
194
|
+
| `error` | Meaning |
|
|
195
|
+
|---------|---------|
|
|
196
|
+
| `recording-not-object` | Not a non-array object (or reading it threw). |
|
|
197
|
+
| `recording-format-unsupported` | `format` is not `'phantomstream-recording/1'`. |
|
|
198
|
+
| `recording-version-unsupported` | `version` is not `1`. |
|
|
199
|
+
| `recording-bounds-invalid` | `startedAt` or `endedAt` is not finite, or `startedAt` is greater than `endedAt`. |
|
|
200
|
+
| `recording-meta-invalid` | `meta` is not a non-array object. |
|
|
201
|
+
| `recording-events-missing` | `events` is not an array, or is empty. |
|
|
202
|
+
| `recording-event-invalid` | An event lacks a finite `t`, a non-empty string `type`, or an object `payload`, or a `host:mark` payload lacks a non-empty string `kind`. |
|
|
203
|
+
| `recording-time-decreasing` | An event's `t` is below the previous event's `t`. |
|
|
204
|
+
| `recording-no-snapshot` | No `STREAM.SNAPSHOT` has a string `payload.html`. |
|
|
205
|
+
|
|
206
|
+
Validation is structural only. It does not make a recording trustworthy:
|
|
207
|
+
the player still routes every payload through the viewer's sanitizers.
|
|
208
|
+
|
|
209
|
+
## Keyframes and seek cost
|
|
210
|
+
|
|
211
|
+
The player rebuilds any moment from the nearest snapshot at or before it,
|
|
212
|
+
then applies every later event up to that moment. Seek cost therefore grows
|
|
213
|
+
with the distance from the last snapshot. Capture mints a snapshot on every
|
|
214
|
+
`start()`, so calling `capture.start()` at quiet points (for example after
|
|
215
|
+
each agent action settles) adds a fresh keyframe, bounds that cost, and in
|
|
216
|
+
chunk mode starts a new keyframe chunk.
|
|
217
|
+
|
|
218
|
+
## Privacy
|
|
219
|
+
|
|
220
|
+
A recording holds exactly the post-masking wire content: DOM text,
|
|
221
|
+
attribute values, unmasked form values, overlay labels, and mark data.
|
|
222
|
+
Masking happens in capture, before a message reaches the recorder, so
|
|
223
|
+
configure `blockSelector`, `maskTextSelector`, and `maskInputs` before
|
|
224
|
+
recording. Treat stored recordings like the live stream (access control,
|
|
225
|
+
retention, encryption at rest). See `docs/SECURITY.md`.
|