@buoy-gg/events 7.0.8 → 7.0.10

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@buoy-gg/events",
3
- "version": "7.0.8",
3
+ "version": "7.0.10",
4
4
  "description": "One timeline across every Buoy devtool, with LLM-ready export presets. Part of Buoy devtools.",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",
@@ -27,8 +27,8 @@
27
27
  ],
28
28
  "sideEffects": false,
29
29
  "dependencies": {
30
- "@buoy-gg/floating-tools-core": "7.0.8",
31
- "@buoy-gg/shared-ui": "7.0.8"
30
+ "@buoy-gg/floating-tools-core": "^7.0.10",
31
+ "@buoy-gg/shared-ui": "^7.0.10"
32
32
  },
33
33
  "peerDependencies": {
34
34
  "react": "*",
@@ -37,11 +37,80 @@ interface TruncatedOriginalEvent {
37
37
  note: string;
38
38
  }
39
39
 
40
+ /**
41
+ * Early-aborting size estimate. The previous guard measured with
42
+ * JSON.stringify(payload).length — an O(payload) cost to enforce a 16KB cap,
43
+ * which meant the protection itself blocked the JS thread for seconds (and
44
+ * could OOM) on enterprise-scale redux state trees. This walk accumulates
45
+ * approximate byte counts and bails as soon as the limit is exceeded, so the
46
+ * cost is O(limit) no matter how large the payload is. `cyclic` is reported
47
+ * because cyclic payloads must never reach the socket serializer.
48
+ */
49
+ function approxSizeOver(
50
+ value: unknown,
51
+ limit: number
52
+ ): { over: boolean; approxBytes: number; cyclic: boolean } {
53
+ let bytes = 0;
54
+ let cyclic = false;
55
+ const stack: unknown[] = [value];
56
+ const seen = new Set<object>();
57
+ while (stack.length > 0) {
58
+ if (bytes > limit) return { over: true, approxBytes: bytes, cyclic };
59
+ const v = stack.pop();
60
+ if (v === null || v === undefined) {
61
+ bytes += 4;
62
+ } else if (typeof v === "string") {
63
+ bytes += v.length + 2; // O(1): length read, no scan
64
+ } else if (typeof v === "number") {
65
+ bytes += 8;
66
+ } else if (typeof v === "boolean") {
67
+ bytes += 5;
68
+ } else if (typeof v === "object") {
69
+ if (seen.has(v as object)) {
70
+ cyclic = true;
71
+ continue;
72
+ }
73
+ seen.add(v as object);
74
+ if (Array.isArray(v)) {
75
+ bytes += 2 + v.length;
76
+ for (let i = 0; i < v.length; i++) stack.push(v[i]);
77
+ } else {
78
+ bytes += 2;
79
+ for (const k in v as Record<string, unknown>) {
80
+ bytes += k.length + 4;
81
+ stack.push((v as Record<string, unknown>)[k]);
82
+ }
83
+ }
84
+ } else {
85
+ bytes += 4; // function/symbol/bigint — JSON drops or errors; count nominally
86
+ }
87
+ }
88
+ return { over: bytes > limit, approxBytes: bytes, cyclic };
89
+ }
90
+
91
+ function truncatedMarker(approxBytes: number, note?: string): TruncatedOriginalEvent {
92
+ return {
93
+ __buoyTruncated: true,
94
+ approxBytes,
95
+ note:
96
+ note ??
97
+ `Payload (≥${(approxBytes / 1024).toFixed(0)}KB approx) exceeds the ${MAX_SYNC_ORIGINAL_EVENT_BYTES / 1024}KB sync limit and stayed on the device. Open the tool on-device (or use the source tool's detail action) for the full payload.`,
98
+ };
99
+ }
100
+
101
+ /** Marker replacing per-event redux state trees at the sync boundary. */
102
+ const STATE_TREE_OMITTED = {
103
+ __buoyOmitted: "state-tree",
104
+ note: "prevState/nextState stay on the device (use get_redux_state / the on-device tool).",
105
+ } as const;
106
+
40
107
  // Events are immutable once created, so compaction is computed once per event
41
108
  // object and drops out of the cache when the store evicts the event.
42
109
  const compactionCache = new WeakMap<object, unknown>();
43
110
 
44
- function compactEventForSync<T extends { originalEvent: unknown }>(event: T): T {
111
+ function compactEventForSync<T extends { source?: string; originalEvent: unknown }>(
112
+ event: T
113
+ ): T {
45
114
  const original = event.originalEvent;
46
115
  if (typeof original !== "object" || original === null) return event;
47
116
 
@@ -49,27 +118,38 @@ function compactEventForSync<T extends { originalEvent: unknown }>(event: T): T
49
118
  if (cached !== undefined) return cached as T;
50
119
 
51
120
  let result: T = event;
52
- try {
53
- const size = JSON.stringify(original)?.length ?? 0;
54
- if (size > MAX_SYNC_ORIGINAL_EVENT_BYTES) {
55
- const truncated: TruncatedOriginalEvent = {
56
- __buoyTruncated: true,
57
- approxBytes: size,
58
- note: `Payload (${(size / 1024).toFixed(0)}KB) exceeds the ${MAX_SYNC_ORIGINAL_EVENT_BYTES / 1024}KB sync limit and stayed on the device. Open the tool on-device (or use the source tool's detail action) for the full payload.`,
59
- };
60
- result = { ...event, originalEvent: truncated };
61
- }
62
- } catch {
63
- // Unserializable payloads would break the socket emit anyway — replace.
121
+
122
+ // Redux events carry the whole ReduxAction, whose prevState/nextState are
123
+ // FULL state trees — per event. Never ship (or even size-walk) those:
124
+ // drop them unconditionally before estimating, so the rest of the action
125
+ // (type/payload/meta/diff) still makes it onto the wire when it fits.
126
+ let candidate = original as Record<string, unknown>;
127
+ if (
128
+ event.source === "redux" &&
129
+ ("prevState" in candidate || "nextState" in candidate)
130
+ ) {
131
+ candidate = {
132
+ ...candidate,
133
+ prevState: STATE_TREE_OMITTED,
134
+ nextState: STATE_TREE_OMITTED,
135
+ };
136
+ result = { ...event, originalEvent: candidate };
137
+ }
138
+
139
+ const { over, approxBytes, cyclic } = approxSizeOver(
140
+ candidate,
141
+ MAX_SYNC_ORIGINAL_EVENT_BYTES
142
+ );
143
+ if (over) {
144
+ result = { ...event, originalEvent: truncatedMarker(approxBytes) };
145
+ } else if (cyclic) {
146
+ // Cyclic payloads would crash the socket serializer — never ship them.
64
147
  result = {
65
148
  ...event,
66
- originalEvent: {
67
- __buoyTruncated: true,
68
- approxBytes: -1,
69
- note: "Payload is not JSON-serializable and stayed on the device.",
70
- } satisfies TruncatedOriginalEvent,
149
+ originalEvent: truncatedMarker(-1, "Payload is not JSON-serializable (cyclic) and stayed on the device."),
71
150
  };
72
151
  }
152
+
73
153
  compactionCache.set(event as object, result);
74
154
  return result;
75
155
  }
@@ -682,6 +682,44 @@ function formatTimestamp(
682
682
  }
683
683
  }
684
684
 
685
+ /**
686
+ * Early-aborting JSON-size probe: approximate byte count, bailing as soon as
687
+ * `limit` is exceeded — O(limit), never O(payload). Measuring with
688
+ * JSON.stringify(data).length meant enforcing a 5KB cap could serialize a
689
+ * 190MB state tree first, blocking the JS thread for seconds per event.
690
+ */
691
+ function approxJsonBytesOver(
692
+ value: unknown,
693
+ limit: number
694
+ ): { over: boolean; approxBytes: number } {
695
+ let bytes = 0;
696
+ const stack: unknown[] = [value];
697
+ const seen = new Set<object>();
698
+ while (stack.length > 0) {
699
+ if (bytes > limit) return { over: true, approxBytes: bytes };
700
+ const v = stack.pop();
701
+ if (v === null || v === undefined) bytes += 4;
702
+ else if (typeof v === "string") bytes += v.length + 2;
703
+ else if (typeof v === "number") bytes += 8;
704
+ else if (typeof v === "boolean") bytes += 5;
705
+ else if (typeof v === "object") {
706
+ if (seen.has(v as object)) continue;
707
+ seen.add(v as object);
708
+ if (Array.isArray(v)) {
709
+ bytes += 2 + v.length;
710
+ for (let i = 0; i < v.length; i++) stack.push(v[i]);
711
+ } else {
712
+ bytes += 2;
713
+ for (const k in v as Record<string, unknown>) {
714
+ bytes += k.length + 4;
715
+ stack.push((v as Record<string, unknown>)[k]);
716
+ }
717
+ }
718
+ } else bytes += 4;
719
+ }
720
+ return { over: bytes > limit, approxBytes: bytes };
721
+ }
722
+
685
723
  /**
686
724
  * Truncate data based on size threshold
687
725
  */
@@ -693,18 +731,20 @@ function truncateData(
693
731
  return { data, truncated: false };
694
732
  }
695
733
 
696
- try {
697
- const json = JSON.stringify(data);
698
- const sizeKB = json.length / 1024;
699
-
700
- if (sizeKB <= thresholdKB) {
701
- return { data, truncated: false };
702
- }
703
-
734
+ // Cheap probe first — never serialize a payload just to learn it's huge.
735
+ const { over, approxBytes } = approxJsonBytesOver(data, thresholdKB * 1024);
736
+ if (over) {
704
737
  return {
705
- data: `[Data truncated - ${sizeKB.toFixed(1)}KB exceeds ${thresholdKB}KB limit]`,
738
+ data: `[Data truncated - ≥${(approxBytes / 1024).toFixed(1)}KB exceeds ${thresholdKB}KB limit]`,
706
739
  truncated: true,
707
740
  };
741
+ }
742
+
743
+ try {
744
+ // Under the cap: a real stringify is bounded by the threshold and also
745
+ // validates serializability (cycles etc.).
746
+ JSON.stringify(data);
747
+ return { data, truncated: false };
708
748
  } catch {
709
749
  return { data: "[Unable to serialize data]", truncated: true };
710
750
  }
@@ -729,18 +769,15 @@ export function estimateExportSize(
729
769
  if (settings.includeDuration) size += 15;
730
770
  size += 30; // Timestamp
731
771
 
732
- // Event data
772
+ // Event data — probe with an early-abort cap instead of fully
773
+ // serializing (an estimate never justifies an O(payload) stringify).
733
774
  if (settings.includeEventData && event.originalEvent) {
734
- try {
735
- const dataSize = JSON.stringify(event.originalEvent).length;
736
- if (settings.dataSizeThreshold === -1) {
737
- size += dataSize;
738
- } else {
739
- size += Math.min(dataSize, settings.dataSizeThreshold * 1024);
740
- }
741
- } catch {
742
- size += 50;
743
- }
775
+ const cap =
776
+ settings.dataSizeThreshold === -1
777
+ ? 1024 * 1024 // no threshold: cap the ESTIMATE at 1MB per event
778
+ : settings.dataSizeThreshold * 1024;
779
+ const { approxBytes } = approxJsonBytesOver(event.originalEvent, cap);
780
+ size += Math.min(approxBytes, cap);
744
781
  }
745
782
  }
746
783
 
@@ -1337,14 +1374,11 @@ function formatBytes(bytes: number): string {
1337
1374
  }
1338
1375
 
1339
1376
  /**
1340
- * Estimate size of data in bytes
1377
+ * Estimate size of data in bytes (early-abort probe, capped at 1MB — a size
1378
+ * label never justifies an O(payload) stringify).
1341
1379
  */
1342
1380
  function estimateDataSize(data: unknown): number {
1343
- try {
1344
- return JSON.stringify(data).length;
1345
- } catch {
1346
- return 0;
1347
- }
1381
+ return approxJsonBytesOver(data, 1024 * 1024).approxBytes;
1348
1382
  }
1349
1383
 
1350
1384
  /**