@brett_lamy/ui 3.0.0 → 3.1.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.
@@ -0,0 +1,280 @@
1
+ "use client";
2
+ import { storage as e } from "./append-stream.js";
3
+ import { useSyncExternalStore as t } from "react";
4
+ //#region src/lib/durable-streams.ts
5
+ var n = class {
6
+ cfg;
7
+ constructor(e) {
8
+ this.cfg = e;
9
+ }
10
+ url(e, t = "") {
11
+ return `${this.cfg.url.replace(/\/+$/, "")}/v1/stream/${encodeURIComponent((this.cfg.prefix ?? "") + e)}${t}`;
12
+ }
13
+ request(e, t = {}) {
14
+ return (this.cfg.fetch ?? fetch)(e, {
15
+ ...t,
16
+ headers: {
17
+ ...this.cfg.headers,
18
+ ...t.headers
19
+ }
20
+ });
21
+ }
22
+ async create(e) {
23
+ let t = await this.request(this.url(e), {
24
+ method: "PUT",
25
+ headers: { "Content-Type": "application/json" }
26
+ });
27
+ if (!t.ok && t.status !== 409) throw Error(`create ${e}: ${t.status}`);
28
+ }
29
+ async append(e, t, n) {
30
+ let r = () => this.request(this.url(e), {
31
+ method: "POST",
32
+ headers: {
33
+ "Content-Type": "application/json",
34
+ ...n ? {
35
+ "Producer-Id": n.id,
36
+ "Producer-Epoch": String(n.epoch),
37
+ "Producer-Seq": String(n.seq)
38
+ } : null
39
+ },
40
+ body: JSON.stringify(t)
41
+ }), i = await r();
42
+ if (i.status === 404 && (await this.create(e), i = await r()), i.ok) return;
43
+ let a = Number(i.headers.get("Producer-Expected-Seq"));
44
+ if (!(i.status === 409 && n && Number.isFinite(a) && i.headers.has("Producer-Expected-Seq") && a > n.seq)) throw Error(`append ${e}: ${i.status}`);
45
+ }
46
+ async read(e, t = "-1") {
47
+ let n = [], r = t;
48
+ for (;;) {
49
+ let t = await this.request(this.url(e, `?offset=${encodeURIComponent(r)}`));
50
+ if (t.status === 404) return {
51
+ items: n,
52
+ next: r
53
+ };
54
+ if (!t.ok) throw Error(`read ${e}: ${t.status}`);
55
+ let i = t.status === 204 ? "" : await t.text();
56
+ i && n.push(...JSON.parse(i));
57
+ let a = t.headers.get("Stream-Next-Offset");
58
+ if (t.headers.get("Stream-Up-To-Date") === "true" || !a || a === r) return {
59
+ items: n,
60
+ next: a ?? r
61
+ };
62
+ r = a;
63
+ }
64
+ }
65
+ async tail(e, t, n, r) {
66
+ let i = t;
67
+ for (; !r.aborted;) {
68
+ let t = await this.request(this.url(e, `?offset=${encodeURIComponent(i)}&live=long-poll`), { signal: r });
69
+ if (t.status === 404) {
70
+ await new Promise((e) => setTimeout(e, 3e3));
71
+ continue;
72
+ }
73
+ if (!t.ok) throw Error(`tail ${e}: ${t.status}`);
74
+ let a = t.status === 204 ? "" : await t.text();
75
+ i = t.headers.get("Stream-Next-Offset") ?? i, a && n(JSON.parse(a), i);
76
+ }
77
+ }
78
+ }, r = "bl-cloud:settings", i = "bl-cloud:cursor:", a = "bl-cloud:device", o = 400, s = {
79
+ enabled: !1,
80
+ url: "",
81
+ token: "",
82
+ prefix: ""
83
+ };
84
+ function c() {
85
+ try {
86
+ return {
87
+ ...s,
88
+ ...JSON.parse(e()?.getItem(r) ?? "{}")
89
+ };
90
+ } catch {
91
+ return s;
92
+ }
93
+ }
94
+ function l() {
95
+ let t = e(), n = t?.getItem(a);
96
+ if (!n) {
97
+ n = `d${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`;
98
+ try {
99
+ t?.setItem(a, n);
100
+ } catch {}
101
+ }
102
+ return n;
103
+ }
104
+ var u = class {
105
+ local;
106
+ pull;
107
+ transport;
108
+ settings = c();
109
+ status = {
110
+ state: "off",
111
+ pending: 0
112
+ };
113
+ listeners = /* @__PURE__ */ new Set();
114
+ stopLocal;
115
+ timer;
116
+ running = !1;
117
+ failures = 0;
118
+ abort;
119
+ pulled = /* @__PURE__ */ new Map();
120
+ constructor(e, t = [], n = {}) {
121
+ this.local = e, this.pull = t, this.transport = n;
122
+ }
123
+ subscribe = (e) => (this.listeners.add(e), () => {
124
+ this.listeners.delete(e);
125
+ });
126
+ getStatus = () => this.status;
127
+ getSettings = () => this.settings;
128
+ set(e) {
129
+ this.status = {
130
+ ...this.status,
131
+ ...e
132
+ }, this.listeners.forEach((e) => e());
133
+ }
134
+ client() {
135
+ let e = this.settings;
136
+ return !e.enabled || !e.url.trim() ? null : new n({
137
+ url: e.url.trim(),
138
+ prefix: e.prefix.trim() ? `${e.prefix.trim().replace(/\/+$/, "")}.` : void 0,
139
+ headers: e.token.trim() ? { Authorization: `Bearer ${e.token.trim()}` } : void 0,
140
+ fetch: this.transport.fetch
141
+ });
142
+ }
143
+ cursorKey(e) {
144
+ return `${i}${encodeURIComponent(`${this.settings.url}|${this.settings.prefix}`)}:${encodeURIComponent(e)}`;
145
+ }
146
+ cursor(t) {
147
+ try {
148
+ return {
149
+ records: 0,
150
+ batches: 0,
151
+ ...JSON.parse(e()?.getItem(this.cursorKey(t)) ?? "{}")
152
+ };
153
+ } catch {
154
+ return {
155
+ records: 0,
156
+ batches: 0
157
+ };
158
+ }
159
+ }
160
+ saveCursor(t, n) {
161
+ try {
162
+ e()?.setItem(this.cursorKey(t), JSON.stringify(n));
163
+ } catch {}
164
+ }
165
+ configure(t) {
166
+ this.settings = {
167
+ ...this.settings,
168
+ ...t
169
+ };
170
+ try {
171
+ e()?.setItem(r, JSON.stringify(this.settings));
172
+ } catch {}
173
+ this.stop(), this.start(), this.listeners.forEach((e) => e());
174
+ }
175
+ start() {
176
+ if (this.stopLocal || !this.client()) {
177
+ this.client() || this.set({
178
+ state: "off",
179
+ pending: 0,
180
+ error: void 0
181
+ });
182
+ return;
183
+ }
184
+ this.stopLocal = this.local.subscribe(() => this.schedule());
185
+ let e = () => this.schedule(0), t = () => this.set({ state: "offline" });
186
+ addEventListener("online", e), addEventListener("offline", t);
187
+ let n = this.stopLocal;
188
+ this.stopLocal = () => {
189
+ n(), removeEventListener("online", e), removeEventListener("offline", t);
190
+ }, this.set({
191
+ state: navigator.onLine === !1 ? "offline" : "idle",
192
+ error: void 0
193
+ }), this.schedule(0), this.followPulled();
194
+ }
195
+ stop() {
196
+ this.stopLocal?.(), this.stopLocal = void 0, clearTimeout(this.timer), this.abort?.abort(), this.abort = void 0, this.pulled.clear(), this.set({
197
+ state: "off",
198
+ pending: 0,
199
+ error: void 0
200
+ });
201
+ }
202
+ pulledRecords(e) {
203
+ return this.pulled.get(e) ?? [];
204
+ }
205
+ async readRemote(e) {
206
+ let t = this.client();
207
+ return t ? (await t.read(e)).items : [];
208
+ }
209
+ schedule(e = 500) {
210
+ this.client() && (clearTimeout(this.timer), this.timer = setTimeout(() => void this.run(), e));
211
+ }
212
+ async run() {
213
+ let e = this.client();
214
+ if (!(!e || this.running)) {
215
+ if (navigator.onLine === !1) {
216
+ this.set({ state: "offline" });
217
+ return;
218
+ }
219
+ this.running = !0, this.set({ state: "syncing" });
220
+ try {
221
+ let t = l();
222
+ for (let n of this.local.names()) {
223
+ let r = this.cursor(n), i = this.local.length(n);
224
+ if (!(r.records >= i)) for (r.records === 0 && r.batches === 0 && await e.create(n); r.records < i;) {
225
+ let i = await this.local.read(n, r.records, r.records + o);
226
+ if (!i.length) break;
227
+ await e.append(n, i, {
228
+ id: t,
229
+ epoch: 0,
230
+ seq: r.batches
231
+ }), r.records += i.length, r.batches += 1, this.saveCursor(n, r), this.set({ pending: this.pending() });
232
+ }
233
+ }
234
+ this.failures = 0, this.set({
235
+ state: "idle",
236
+ pending: this.pending(),
237
+ lastSyncAt: Date.now(),
238
+ error: void 0
239
+ });
240
+ } catch (e) {
241
+ this.failures++, this.set({
242
+ state: "error",
243
+ pending: this.pending(),
244
+ error: e instanceof Error ? e.message : String(e)
245
+ }), this.schedule(Math.min(6e4, 1e3 * 2 ** this.failures));
246
+ } finally {
247
+ this.running = !1;
248
+ }
249
+ }
250
+ }
251
+ pending() {
252
+ return this.local.names().reduce((e, t) => e + Math.max(0, this.local.length(t) - this.cursor(t).records), 0);
253
+ }
254
+ followPulled() {
255
+ let e = this.client();
256
+ if (!e || !this.pull.length) return;
257
+ let t = this.abort = new AbortController();
258
+ for (let n of this.pull) (async () => {
259
+ try {
260
+ let { items: r, next: i } = await e.read(n);
261
+ this.pulled.set(n, r), this.listeners.forEach((e) => e()), await e.tail(n, i, (e) => {
262
+ this.pulled.set(n, [...this.pulled.get(n) ?? [], ...e]), this.listeners.forEach((e) => e());
263
+ }, t.signal);
264
+ } catch (e) {
265
+ t.signal.aborted || this.set({
266
+ state: "error",
267
+ error: e instanceof Error ? e.message : String(e)
268
+ });
269
+ }
270
+ })();
271
+ }
272
+ };
273
+ function d(e) {
274
+ return {
275
+ status: t(e.subscribe, e.getStatus, e.getStatus),
276
+ settings: t(e.subscribe, e.getSettings, e.getSettings)
277
+ };
278
+ }
279
+ //#endregion
280
+ export { u as CloudSync, n as DurableStreamsClient, l as deviceId, c as loadCloudSettings, d as useCloud };
@@ -0,0 +1,87 @@
1
+ import { eventWithTime } from 'rrweb';
2
+ import { CloudSync, CloudSettings, CloudStatus } from './durable-streams';
3
+ export type RecordedEvent = eventWithTime;
4
+ /** The records of the `sessions` index stream. */
5
+ export type IndexRecord = {
6
+ t: 'start';
7
+ id: string;
8
+ at: number;
9
+ url: string;
10
+ title: string;
11
+ width: number;
12
+ height: number;
13
+ device: string;
14
+ } | {
15
+ t: 'end';
16
+ id: string;
17
+ at: number;
18
+ events: number;
19
+ truncated?: boolean;
20
+ } | {
21
+ t: 'delete';
22
+ id: string;
23
+ at: number;
24
+ };
25
+ export interface SessionInfo {
26
+ id: string;
27
+ startedAt: number;
28
+ /** When it ended, if it ended cleanly. */
29
+ endedAt?: number;
30
+ /** Events recorded so far (here) or in total (from the `end` record, for a session held only in the cloud). */
31
+ events?: number;
32
+ url: string;
33
+ title: string;
34
+ width: number;
35
+ height: number;
36
+ /** Which browser recorded it. */
37
+ device: string;
38
+ /** Being recorded right now, by this tab. */
39
+ live: boolean;
40
+ /** Its events are in this browser. */
41
+ local: boolean;
42
+ /** Storage ran out and the recording stopped early. */
43
+ truncated?: boolean;
44
+ }
45
+ export interface SessionRecordingOptions {
46
+ /** Elements with this class are recorded as empty placeholders. Default `rr-block`. */
47
+ blockClass?: string;
48
+ /** Elements with this class don't record input events. Default `rr-ignore`. */
49
+ ignoreClass?: string;
50
+ /** Replace typed text with asterisks. Default true. */
51
+ maskAllInputs?: boolean;
52
+ /** How many sessions' events to keep in this browser; the oldest go first. Default 30. */
53
+ maxSessions?: number;
54
+ /** How often buffered events are written, in ms. Default 1500. */
55
+ flushMs?: number;
56
+ }
57
+ /** Mirrors the session streams to a Durable Streams server (Rivet) when enabled; see `CloudSync`. */
58
+ export declare const sessionCloud: CloudSync;
59
+ /** Every session, newest first, kept current while one records or another device syncs. */
60
+ export declare function useSessions(): SessionInfo[];
61
+ /** The session this tab is recording, if any. */
62
+ export declare function currentSessionId(): string | null;
63
+ /** A session's events, in order. A session still recording reads as far as it has got. */
64
+ export declare function readSession(id: string): Promise<RecordedEvent[]>;
65
+ /** Delete a session: its events leave this browser, and a `delete` record joins the index so every device agrees. */
66
+ export declare function deleteSession(id: string): Promise<void>;
67
+ /** Delete every session but the one recording now. */
68
+ export declare function clearSessions(): Promise<void>;
69
+ /** What this browser's streams occupy, in characters, against a typical 5M-character localStorage quota. */
70
+ export declare function storageUsage(): {
71
+ used: number;
72
+ quota: number;
73
+ };
74
+ /** The cloud's status and settings, live. */
75
+ export declare function useSessionCloud(): {
76
+ status: CloudStatus;
77
+ settings: CloudSettings;
78
+ configure: (s: Partial<CloudSettings>) => void;
79
+ };
80
+ /** Start recording this page as a new session; returns the function that ends it. Calls share one recording. */
81
+ export declare function startSessionRecording(options?: SessionRecordingOptions): () => void;
82
+ /** Record this page for as long as the component is mounted: one new session per mount (not per re-render). The cloud,
83
+ if switched on, syncs meanwhile. */
84
+ export declare function useSessionRecording({ enabled, ...options }?: SessionRecordingOptions & {
85
+ enabled?: boolean;
86
+ }): void;
87
+ //# sourceMappingURL=session-recorder.d.ts.map
@@ -0,0 +1 @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EAAE,GAAG,OAAO,EAAE,GAAE,uBAAuB,GAAG;IAAE,OAAO,CAAC,EAAE,OAAO,CAAA;CAAO,QAQvH"}
@@ -0,0 +1,257 @@
1
+ "use client";
2
+ import { LocalStreams as e, StreamFullError as t } from "./append-stream.js";
3
+ import { CloudSync as n, deviceId as r, useCloud as i } from "./durable-streams.js";
4
+ import { useEffect as a, useSyncExternalStore as o } from "react";
5
+ //#region src/lib/session-recorder.ts
6
+ var s = "sessions", c = (e) => `session/${e}`, l = 5e6, u = [], d = null, f = new e({ evict(e) {
7
+ let t = [...u].reverse().find((t) => t.local && t.id !== d && c(t.id) !== e);
8
+ return t ? (f.remove(c(t.id)), !0) : !1;
9
+ } }), p = new n(f, [s]);
10
+ async function m() {
11
+ let e = await f.read(s), t = p.pulledRecords(s), n = /* @__PURE__ */ new Set(), r = [...e, ...t].filter((e) => {
12
+ let t = `${e.t}:${e.id}`;
13
+ return !n.has(t) && (n.add(t), !0);
14
+ }), i = new Set(r.filter((e) => e.t === "delete").map((e) => e.id)), a = [];
15
+ for (let e of r) {
16
+ if (e.t !== "start" || i.has(e.id)) continue;
17
+ let t = r.find((t) => t.t === "end" && t.id === e.id), n = f.length(c(e.id)), o = p.getSettings().enabled;
18
+ !n && !o || a.push({
19
+ id: e.id,
20
+ startedAt: e.at,
21
+ endedAt: t?.at,
22
+ events: n || t?.events,
23
+ url: e.url,
24
+ title: e.title,
25
+ width: e.width,
26
+ height: e.height,
27
+ device: e.device,
28
+ live: e.id === d,
29
+ local: n > 0,
30
+ truncated: t?.truncated
31
+ });
32
+ }
33
+ return a.sort((e, t) => t.startedAt - e.startedAt);
34
+ }
35
+ var h = /* @__PURE__ */ new Set(), g = !1, _ = !1;
36
+ async function v() {
37
+ if (g) {
38
+ _ = !0;
39
+ return;
40
+ }
41
+ g = !0;
42
+ try {
43
+ do
44
+ _ = !1, u = await m(), h.forEach((e) => e());
45
+ while (_);
46
+ } finally {
47
+ g = !1;
48
+ }
49
+ }
50
+ function y(e) {
51
+ h.add(e);
52
+ let t = [f.subscribe(() => void v()), p.subscribe(() => void v())];
53
+ return v(), () => {
54
+ h.delete(e), t.forEach((e) => e());
55
+ };
56
+ }
57
+ function b() {
58
+ return o(y, () => u, () => u);
59
+ }
60
+ function x() {
61
+ return d;
62
+ }
63
+ async function S(e) {
64
+ e === d && await f.flush();
65
+ let t = c(e);
66
+ return f.length(t) > 0 ? f.read(t) : p.readRemote(t);
67
+ }
68
+ async function C(e) {
69
+ e !== d && (f.remove(c(e)), await f.append(s, [{
70
+ t: "delete",
71
+ id: e,
72
+ at: Date.now()
73
+ }]));
74
+ }
75
+ async function w() {
76
+ for (let e of u) await C(e.id);
77
+ }
78
+ function T() {
79
+ return {
80
+ used: f.size(),
81
+ quota: l
82
+ };
83
+ }
84
+ function E() {
85
+ let { status: e, settings: t } = i(p);
86
+ return {
87
+ status: e,
88
+ settings: t,
89
+ configure: (e) => p.configure(e)
90
+ };
91
+ }
92
+ var D = class {
93
+ opts;
94
+ id = `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`;
95
+ buffer = [];
96
+ events = 0;
97
+ started = !1;
98
+ last = Date.now();
99
+ info;
100
+ timer;
101
+ stopRecord;
102
+ dead = !1;
103
+ constructor(e, t) {
104
+ this.opts = e, this.info = {
105
+ t: "start",
106
+ id: this.id,
107
+ at: Date.now(),
108
+ url: location.href,
109
+ title: document.title,
110
+ width: innerWidth,
111
+ height: innerHeight,
112
+ device: t
113
+ };
114
+ }
115
+ start(e) {
116
+ this.stopRecord = e({
117
+ emit: (e) => this.push(e),
118
+ blockClass: this.opts.blockClass,
119
+ ignoreClass: this.opts.ignoreClass,
120
+ maskAllInputs: this.opts.maskAllInputs,
121
+ sampling: {
122
+ mousemove: 60,
123
+ scroll: 150,
124
+ input: "last"
125
+ },
126
+ slimDOMOptions: {
127
+ script: !0,
128
+ comment: !0,
129
+ headFavicon: !0,
130
+ headMetaSocial: !0,
131
+ headMetaRobots: !0,
132
+ headMetaHttpEquiv: !0,
133
+ headMetaVerification: !0
134
+ }
135
+ }), this.timer = setInterval(() => this.flush(), this.opts.flushMs), document.addEventListener("visibilitychange", this.onHide), addEventListener("pagehide", this.onLeave);
136
+ }
137
+ onHide = () => {
138
+ document.visibilityState === "hidden" && this.flush(!0);
139
+ };
140
+ onLeave = () => {
141
+ this.flush(!0);
142
+ };
143
+ push(e) {
144
+ if (!this.dead) {
145
+ if (e.type === 4 && !this.events) {
146
+ let t = e.data;
147
+ this.info = {
148
+ ...this.info,
149
+ at: e.timestamp,
150
+ url: t.href ?? this.info.url,
151
+ width: t.width ?? this.info.width,
152
+ height: t.height ?? this.info.height
153
+ };
154
+ }
155
+ this.buffer.push(e), this.events++, this.last = Math.max(this.last, e.timestamp), this.buffer.length >= 400 && this.flush();
156
+ }
157
+ }
158
+ flush(e = !1) {
159
+ if (!this.buffer.length || this.dead) return;
160
+ let t = this.buffer;
161
+ this.buffer = [];
162
+ let n = c(this.id), r = () => (this.started || (this.started = !0, d = this.id, f.appendSync(s, [this.info])), e ? Promise.resolve(f.appendSync(n, t)) : f.append(n, t));
163
+ try {
164
+ r().catch((e) => this.fail(e));
165
+ } catch (e) {
166
+ this.fail(e);
167
+ }
168
+ }
169
+ fail(e) {
170
+ if (!(e instanceof t)) throw e;
171
+ this.dead = !0, this.stopRecord?.(), clearInterval(this.timer);
172
+ try {
173
+ f.appendSync(s, [{
174
+ t: "end",
175
+ id: this.id,
176
+ at: this.last,
177
+ events: this.events,
178
+ truncated: !0
179
+ }]);
180
+ } catch {}
181
+ }
182
+ stop() {
183
+ if (!this.dead) {
184
+ if (this.stopRecord?.(), clearInterval(this.timer), document.removeEventListener("visibilitychange", this.onHide), removeEventListener("pagehide", this.onLeave), !this.started && this.events < 4) {
185
+ this.dead = !0;
186
+ return;
187
+ }
188
+ this.flush(!0), this.dead = !0;
189
+ try {
190
+ f.appendSync(s, [{
191
+ t: "end",
192
+ id: this.id,
193
+ at: this.last,
194
+ events: this.events
195
+ }]);
196
+ } catch {}
197
+ d === this.id && (d = null);
198
+ }
199
+ }
200
+ }, O = 0, k = null, A = null, j = {
201
+ blockClass: "rr-block",
202
+ ignoreClass: "rr-ignore",
203
+ maskAllInputs: !0,
204
+ maxSessions: 30,
205
+ flushMs: 1500
206
+ };
207
+ async function M(e) {
208
+ let { record: t } = await import("rrweb");
209
+ if (!(O === 0 || k)) {
210
+ await v();
211
+ for (let t of u.filter((e) => e.local).slice(Math.max(0, e.maxSessions - 1))) f.remove(c(t.id));
212
+ k = new D(e, r()), k.start(t);
213
+ }
214
+ }
215
+ async function N() {
216
+ if (await A, O > 0 || !k) return;
217
+ let e = k;
218
+ k = null, e.stop(), v();
219
+ }
220
+ function P(e = {}) {
221
+ O++;
222
+ let t = Object.fromEntries(Object.entries(e).filter(([, e]) => e !== void 0));
223
+ (O === 1 || !k) && (A = M({
224
+ ...j,
225
+ ...t
226
+ }));
227
+ let n = !1;
228
+ return () => {
229
+ n || (n = !0, --O === 0 && N());
230
+ };
231
+ }
232
+ function F({ enabled: e = !0, ...t } = {}) {
233
+ let { blockClass: n, ignoreClass: r, maskAllInputs: i, maxSessions: o, flushMs: s } = t;
234
+ a(() => {
235
+ if (!e) return;
236
+ p.start();
237
+ let t = P({
238
+ blockClass: n,
239
+ ignoreClass: r,
240
+ maskAllInputs: i,
241
+ maxSessions: o,
242
+ flushMs: s
243
+ });
244
+ return () => {
245
+ t();
246
+ };
247
+ }, [
248
+ e,
249
+ n,
250
+ r,
251
+ i,
252
+ o,
253
+ s
254
+ ]);
255
+ }
256
+ //#endregion
257
+ export { w as clearSessions, x as currentSessionId, C as deleteSession, S as readSession, p as sessionCloud, P as startSessionRecording, T as storageUsage, E as useSessionCloud, F as useSessionRecording, b as useSessions };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brett_lamy/ui",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -41,6 +41,7 @@
41
41
  "perfect-freehand": "^1.2.2",
42
42
  "react-aria": "^3.51.0",
43
43
  "react-aria-components": "^1",
44
+ "rrweb": "2.0.0-alpha.20",
44
45
  "tailwind-merge": "^3"
45
46
  },
46
47
  "peerDependencies": {