@@ -30,56 +30,17 @@ static JavaScriptSDK *js_sdk = nullptr;
3030extern " C" {
3131
3232static void before_send_wasm_callback (int32_t *p_ids, int32_t p_len) {
33- ERR_FAIL_COND (p_len != 2 );
34- ERR_FAIL_NULL (js_sdk);
33+ ERR_FAIL_COND (p_len != 1 );
3534
3635 JSObjectPtr event_obj = JSObject::from_id (p_ids[0 ]);
37- JSObjectPtr out_attachments = JSObject::from_id (p_ids[1 ]);
3836 ERR_FAIL_COND (!event_obj);
39- ERR_FAIL_COND (!out_attachments);
4037
4138 Ref<JavaScriptEvent> event = memnew (JavaScriptEvent (event_obj));
4239 Ref<JavaScriptEvent> processed = sentry::process_event (event);
4340
4441 // NOTE: We cannot return a value from a callback, so we use the same
4542 // event object to communicate the result back.
46- if (unlikely (processed.is_null ())) {
47- // Discard event.
48- event_obj->set (" shouldDiscard" , true );
49- } else {
50- event_obj->set (" shouldDiscard" , false );
51-
52- // Read file-based attachments and include them with the event.
53- for (const Ref<SentryAttachment> &att : js_sdk->get_file_attachments ()) {
54- if (att->get_path ().is_empty ()) {
55- // Skip attachments with empty path.
56- // NOTE: Byte attachments are not processed here - they are added immediately.
57- continue ;
58- }
59-
60- Ref<FileAccess> file = FileAccess::open (att->get_path (), FileAccess::READ );
61- if (file.is_null ()) {
62- // NOTE: Some attachments may legitimately be missing (e.g. screenshots not created on non-main threads).
63- sentry::logging::print_debug (" Skipping attachment - file not found: " + att->get_path ());
64- continue ;
65- }
66-
67- PackedByteArray bytes = file->get_buffer (file->get_length ());
68- if (bytes.is_empty ()) {
69- sentry::logging::print_debug (" Skipping attachment - empty file: " + att->get_path ());
70- continue ;
71- }
72-
73- sentry::logging::print_debug (" Adding attachment: " + att->get_path ());
74-
75- js_bridge ()->call (" pushAttachmentData" ,
76- out_attachments,
77- bytes,
78- att->get_effective_filename ().utf8 (),
79- att->get_content_type ().utf8 (),
80- att->get_attachment_type ().utf8 ());
81- }
82- }
43+ event_obj->set (" shouldDiscard" , processed.is_null ());
8344}
8445
8546// Reads the file a scope attachment refers to, leaving the bytes unset if it cannot be read.
@@ -269,10 +230,13 @@ void JavaScriptSDK::add_attachment(const Ref<SentryAttachment> &p_attachment) {
269230 ERR_FAIL_COND_MSG (p_attachment.is_null (), " Sentry: Can't add null attachment." );
270231
271232 if (!p_attachment->get_path ().is_empty ()) {
272- // File attachment - add to list for on-demand loading during event processing.
273- file_attachments.push_back (p_attachment);
233+ // The file is read when an event is captured, so it doesn't have to exist yet.
234+ js_bridge ()->call (" addFileAttachment" ,
235+ p_attachment->get_path ().utf8 (),
236+ p_attachment->get_effective_filename ().utf8 (),
237+ p_attachment->get_content_type ().utf8 (),
238+ p_attachment->get_attachment_type ().utf8 ());
274239 } else {
275- // Bytes attachment - added immediately
276240 ERR_FAIL_COND_MSG (p_attachment->get_filename ().is_empty (), " Sentry: Can't add bytes attachment without filename." );
277241
278242 js_bridge ()->call (" addBytesAttachment" ,
@@ -285,9 +249,14 @@ void JavaScriptSDK::add_attachment(const Ref<SentryAttachment> &p_attachment) {
285249void JavaScriptSDK::clear_attachments () {
286250 ERR_FAIL_COND (!js_bridge ());
287251
288- // Reset file attachments from options (Vector uses copy-on-write).
289- file_attachments = SENTRY_OPTIONS ()->get_default_attachments ();
290252 js_bridge ()->call (" clearAttachments" );
253+ _add_default_attachments ();
254+ }
255+
256+ void JavaScriptSDK::_add_default_attachments () {
257+ for (const Ref<SentryAttachment> &att : SENTRY_OPTIONS ()->get_default_attachments ()) {
258+ add_attachment (att);
259+ }
291260}
292261
293262void JavaScriptSDK::metrics_add_count (const Ref<SentryScope> &p_scope, const String &p_name, int64_t p_value, const Dictionary &p_attributes) {
@@ -348,8 +317,6 @@ void JavaScriptSDK::set_trace(const String &p_trace_id, const String &p_parent_s
348317void JavaScriptSDK::init () {
349318 ERR_FAIL_COND (!js_bridge ());
350319
351- file_attachments = SENTRY_OPTIONS ()->get_default_attachments ();
352-
353320 JSObjectPtr before_send_callback = JSObject::create_callback (before_send_wasm_callback);
354321 JSObjectPtr read_attachment_callback = JSObject::create_callback (read_attachment_wasm_callback);
355322
@@ -383,6 +350,7 @@ void JavaScriptSDK::init() {
383350
384351 if (is_enabled ()) {
385352 set_user (SentryUser::create_default ());
353+ _add_default_attachments ();
386354 } else {
387355 ERR_PRINT (" Sentry: Failed to initialize JavaScript SDK." );
388356 }
@@ -392,7 +360,6 @@ void JavaScriptSDK::close() {
392360 ERR_FAIL_COND (!js_bridge ());
393361
394362 js_bridge ()->call (" close" , SENTRY_OPTIONS ()->get_shutdown_timeout_ms ());
395- file_attachments.clear ();
396363}
397364
398365bool JavaScriptSDK::is_enabled () const {
0 commit comments