@sailfish-ai/recorder 1.6.0 → 1.6.1

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.
@@ -1,19 +1,43 @@
1
+ import { Mutex } from "async-mutex";
2
+ const MAX_MESSAGE_SIZE_MB = 50;
3
+ const MAX_MESSAGE_SIZE_BYTES = MAX_MESSAGE_SIZE_MB * 1024 * 1024;
1
4
  let eventCache = [];
5
+ const mutex = new Mutex();
2
6
  export function cacheEvents(event) {
3
- eventCache.push(event);
7
+ mutex.runExclusive(() => {
8
+ eventCache.push(event);
9
+ });
4
10
  }
5
11
  export function sendRecordingEvents(webSocket) {
6
- if (webSocket && webSocket.readyState === WebSocket.OPEN) {
7
- if (eventCache.length > 0) {
8
- const message = {
9
- type: "events",
10
- events: eventCache,
11
- };
12
- webSocket.send(JSON.stringify(message));
13
- eventCache = [];
12
+ mutex.runExclusive(() => {
13
+ if (webSocket && webSocket.readyState === WebSocket.OPEN) {
14
+ if (eventCache.length > 0) {
15
+ let batch = [];
16
+ let batchSize = 0;
17
+ const sendBatch = () => {
18
+ if (batch.length > 0) {
19
+ const message = JSON.stringify({ type: "events", events: batch });
20
+ webSocket.send(message);
21
+ batch = [];
22
+ batchSize = 0;
23
+ }
24
+ };
25
+ for (const event of eventCache) {
26
+ const eventSize = new Blob([JSON.stringify(event)]).size;
27
+ if (eventSize > MAX_MESSAGE_SIZE_BYTES) {
28
+ console.error(`Event Type: ${event.type || "unknown"} exceeds ${MAX_MESSAGE_SIZE_MB}MB limit! It may be rejected by the backend.`);
29
+ webSocket.send(JSON.stringify({ type: "events", events: [event] }));
30
+ continue;
31
+ }
32
+ if (batchSize + eventSize > MAX_MESSAGE_SIZE_BYTES) {
33
+ sendBatch();
34
+ }
35
+ batch.push(event);
36
+ batchSize += eventSize;
37
+ }
38
+ sendBatch();
39
+ eventCache = [];
40
+ }
14
41
  }
15
- }
16
- else {
17
- eventCache.push(...eventCache);
18
- }
42
+ });
19
43
  }