Skip to content

Commit 5c9be86

Browse files
committed
Added handling for the size constraints.
When the size reaches 80% of the 200KB size for the message, executes the "onTimeout" function and returns the PROGRESS event.
1 parent 10bc037 commit 5c9be86

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

src/common/event-size-monitor.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { EventData } from '../types/extraction';
2+
import { ErrorRecord } from '../types/common';
3+
4+
const MAX_EVENT_SIZE = 200_000;
5+
const SIZE_LIMIT_THRESHOLD = Math.floor(MAX_EVENT_SIZE * 0.8); // 160_000 bytes
6+
7+
/**
8+
* Get the JSON serialized size of event data in bytes
9+
*/
10+
export function getEventDataSize(data: EventData | undefined): number {
11+
if (!data) return 0;
12+
return JSON.stringify(data).length;
13+
}
14+
15+
/**
16+
* Check if event data exceeds the 80% threshold (160KB)
17+
*/
18+
export function shouldTriggerSizeLimit(data: EventData | undefined): boolean {
19+
return getEventDataSize(data) > SIZE_LIMIT_THRESHOLD;
20+
}
21+
22+
/**
23+
* Truncate error message to max length (default 1000 chars)
24+
*/
25+
export function truncateErrorMessage(
26+
error: ErrorRecord | undefined,
27+
maxLength: number = 1000
28+
): ErrorRecord | undefined {
29+
if (!error) return undefined;
30+
31+
return {
32+
message: error.message.substring(0, maxLength)
33+
};
34+
}
35+
36+
/**
37+
* Prune event data by truncating error messages
38+
* Always applied before serialization
39+
*/
40+
export function pruneEventData(data: EventData | undefined): EventData | undefined {
41+
if (!data) return data;
42+
43+
return {
44+
...data,
45+
error: truncateErrorMessage(data.error),
46+
};
47+
}
48+
49+
/**
50+
* Log detailed warning when size limit is detected
51+
*/
52+
export function logSizeLimitWarning(size: number, triggerType: 'onUpload' | 'onEmit'): void {
53+
const percentage = (size / MAX_EVENT_SIZE) * 100;
54+
const detailsString = triggerType === 'onUpload'
55+
? 'during data collection. Emitting progress event and stopping further processing.'
56+
: 'during emit. Error messages truncated.';
57+
58+
console.warn(
59+
`[SIZE_LIMIT] Event data size ${size} bytes (${percentage.toFixed(1)}% of ${MAX_EVENT_SIZE} limit) detected ${detailsString}`
60+
);
61+
}
62+
63+
export { MAX_EVENT_SIZE, SIZE_LIMIT_THRESHOLD };

src/workers/process-task.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ export function processTask<ConnectorState>({
5858
})()
5959
);
6060
await task({ adapter });
61+
62+
// If size limit was triggered during task, call onTimeout for cleanup
63+
if (adapter.isTimeout) {
64+
console.log(
65+
'[SIZE_LIMIT] Size limit detected during data collection. Executing onTimeout function for cleanup.'
66+
);
67+
await onTimeout({ adapter });
68+
}
69+
6170
process.exit(0);
6271
}
6372
} catch (error) {

src/workers/worker-adapter.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ import {
4747
} from '../types/workers';
4848
import { Uploader } from '../uploader/uploader';
4949
import { Artifact, SsorAttachment } from '../uploader/uploader.interfaces';
50+
import {
51+
pruneEventData,
52+
logSizeLimitWarning,
53+
SIZE_LIMIT_THRESHOLD,
54+
} from '../common/event-size-monitor';
5055

5156
const MAX_MESSAGE_LENGTH: number = 200_000;
5257

@@ -163,10 +168,17 @@ export class WorkerAdapter<ConnectorState> {
163168
);
164169
}
165170

166-
if(this.currentLength + newLength > MAX_MESSAGE_LENGTH) {
167-
// TODO: We need to call the adapter's `onTimeout` here and `emit` the Progress event.
168-
// We might have to run the `uploadAllRepos` as well.
171+
this.currentLength += newLength;
172+
173+
// Check for size limit (80% of 200KB = 160KB threshold)
174+
if (this.currentLength > SIZE_LIMIT_THRESHOLD && !this.hasWorkerEmitted) {
175+
logSizeLimitWarning(this.currentLength, 'onUpload');
176+
177+
// Set timeout flag to trigger onTimeout cleanup after task completes
169178
this.handleTimeout();
179+
180+
// Emit progress event to save state and continue on next iteration
181+
void this.emit(ExtractorEventType.ExtractionDataProgress);
170182
}
171183
},
172184
options: this.options,
@@ -259,11 +271,14 @@ export class WorkerAdapter<ConnectorState> {
259271
}
260272

261273
try {
274+
// Always prune error messages to 1000 chars before emit
275+
const prunedData = pruneEventData(data);
276+
262277
await emit({
263278
eventType: newEventType,
264279
event: this.event,
265280
data: {
266-
...data,
281+
...prunedData,
267282
...(ALLOWED_EXTRACTION_EVENT_TYPES.includes(
268283
this.event.payload.event_type
269284
)

0 commit comments

Comments
 (0)