amberjs 1.16.1

JavaScript/TypeScript runtime built with Rust and V8
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
// FormData API implementation per Web standard
//
// The FormData interface provides a way to construct a set of key/value pairs
/// representing form fields and their values, which can be sent using fetch().
use anyhow::Result;
use rusty_v8 as v8;
use std::collections::HashMap;
use std::sync::{Mutex, OnceLock};

/// Thread-safe FormData storage
static FORMDATA_CACHE: OnceLock<Mutex<HashMap<usize, Vec<FormDataEntry>>>> = OnceLock::new();

/// Get the FormData cache mutex
fn get_formdata_cache() -> &'static Mutex<HashMap<usize, Vec<FormDataEntry>>> {
    FORMDATA_CACHE.get_or_init(|| Mutex::new(HashMap::new()))
}

/// FormData entry - can be simple value or file
#[derive(Debug, Clone)]
pub struct FormDataEntry {
    pub name: String,
    pub value: String,
    pub body: Vec<u8>,
    pub filename: Option<String>,
    pub content_type: String,
}

fn value_to_optional_string(
    scope: &mut v8::PinScope,
    value: v8::Local<v8::Value>,
) -> Option<String> {
    if value.is_undefined() || value.is_null() {
        return None;
    }

    value
        .to_string(scope)
        .map(|value| value.to_rust_string_lossy(scope))
}

fn object_string_property(
    scope: &mut v8::PinScope,
    object: v8::Local<v8::Object>,
    key: &str,
) -> Option<String> {
    let key = v8::String::new(scope, key)?.into();
    object
        .get(scope, key)
        .and_then(|value| value_to_optional_string(scope, value))
}

fn object_bool_property(
    scope: &mut v8::PinScope,
    object: v8::Local<v8::Object>,
    key: &str,
) -> bool {
    let Some(key) = v8::String::new(scope, key) else {
        return false;
    };
    object
        .get(scope, key.into())
        .is_some_and(|value| value.is_boolean() && value.boolean_value(scope))
}

fn object_has_property(scope: &mut v8::PinScope, object: v8::Local<v8::Object>, key: &str) -> bool {
    let Some(key) = v8::String::new(scope, key) else {
        return false;
    };
    object.has(scope, key.into()).unwrap_or(false)
}

fn bytes_from_array_buffer_value(value: v8::Local<v8::Value>) -> Option<Vec<u8>> {
    let buffer = v8::Local::<v8::ArrayBuffer>::try_from(value).ok()?;
    let len = buffer.byte_length();
    if len == 0 {
        return Some(Vec::new());
    }
    let backing_store = buffer.get_backing_store();
    let ptr = backing_store
        .data()
        .map(|p| p.as_ptr() as *const u8)
        .unwrap_or(std::ptr::null());
    if ptr.is_null() {
        return None;
    }
    Some(unsafe { std::slice::from_raw_parts(ptr, len).to_vec() })
}

fn object_blob_bytes(scope: &mut v8::PinScope, object: v8::Local<v8::Object>) -> Option<Vec<u8>> {
    let key = v8::String::new(scope, "blobBytes")?;
    object
        .get(scope, key.into())
        .and_then(bytes_from_array_buffer_value)
}

fn form_data_value_entry(
    scope: &mut v8::PinScope,
    value: v8::Local<v8::Value>,
    explicit_filename: Option<String>,
) -> Option<(String, Vec<u8>, Option<String>, String)> {
    if value.is_object() {
        let object = value.to_object(scope)?;
        let is_blob_like = object_has_property(scope, object, "arrayBuffer")
            || object_has_property(scope, object, "blobData");

        if is_blob_like {
            let body = object_blob_bytes(scope, object).unwrap_or_else(|| {
                object_string_property(scope, object, "blobData")
                    .unwrap_or_default()
                    .into_bytes()
            });
            let body_text = object_string_property(scope, object, "blobData")
                .unwrap_or_else(|| String::from_utf8_lossy(&body).into_owned());
            let content_type = object_string_property(scope, object, "type")
                .filter(|value| !value.is_empty())
                .unwrap_or_else(|| "application/octet-stream".to_string());
            let filename = explicit_filename.or_else(|| {
                if object_bool_property(scope, object, "_isFile") {
                    object_string_property(scope, object, "name").filter(|name| !name.is_empty())
                } else {
                    Some("blob".to_string())
                }
            });

            return Some((body_text, body, filename, content_type));
        }
    }

    let text = value_to_optional_string(scope, value)?;
    let body = text.as_bytes().to_vec();
    Some((text, body, explicit_filename, "text/plain".to_string()))
}

fn form_data_index_from_object(
    scope: &mut v8::PinScope,
    form_data_obj: v8::Local<v8::Object>,
) -> Option<usize> {
    form_data_obj
        .get_internal_field(scope, 0)
        .and_then(|data| v8::Local::<v8::Value>::try_from(data).ok())
        .and_then(|value| value.to_integer(scope))
        .map(|index| index.value() as usize)
}

fn form_data_entries_for_object(
    scope: &mut v8::PinScope,
    form_data_obj: v8::Local<v8::Object>,
) -> Vec<FormDataEntry> {
    form_data_index_from_object(scope, form_data_obj)
        .and_then(|index| get_formdata_cache().lock().unwrap().get(&index).cloned())
        .unwrap_or_default()
}

fn form_data_entries_array<'a>(
    scope: &mut v8::PinScope<'a, '_>,
    entries: &[FormDataEntry],
) -> v8::Local<'a, v8::Array> {
    let array = v8::Array::new(scope, entries.len() as i32);
    for (index, entry) in entries.iter().enumerate() {
        let pair = v8::Array::new(scope, 2);
        let name = v8::String::new(scope, &entry.name).unwrap().into();
        let value = v8::String::new(scope, &entry.value).unwrap().into();
        pair.set_index(scope, 0, name);
        pair.set_index(scope, 1, value);
        array.set_index(scope, index as u32, pair.into());
    }
    array
}

fn form_data_keys_array<'a>(
    scope: &mut v8::PinScope<'a, '_>,
    entries: &[FormDataEntry],
) -> v8::Local<'a, v8::Array> {
    let array = v8::Array::new(scope, entries.len() as i32);
    for (index, entry) in entries.iter().enumerate() {
        let name = v8::String::new(scope, &entry.name).unwrap().into();
        array.set_index(scope, index as u32, name);
    }
    array
}

fn form_data_values_array<'a>(
    scope: &mut v8::PinScope<'a, '_>,
    entries: &[FormDataEntry],
) -> v8::Local<'a, v8::Array> {
    let array = v8::Array::new(scope, entries.len() as i32);
    for (index, entry) in entries.iter().enumerate() {
        let value = v8::String::new(scope, &entry.value).unwrap().into();
        array.set_index(scope, index as u32, value);
    }
    array
}

fn symbol_iterator_value<'a>(scope: &mut v8::PinScope<'a, '_>) -> Option<v8::Local<'a, v8::Value>> {
    let context = scope.get_current_context();
    let global = context.global(scope);
    let symbol_key: v8::Local<v8::Value> = v8::String::new(scope, "Symbol")?.into();
    let symbol_value = global.get(scope, symbol_key)?;
    let symbol_object = symbol_value.to_object(scope)?;
    let iterator_key: v8::Local<v8::Value> = v8::String::new(scope, "iterator")?.into();
    symbol_object.get(scope, iterator_key)
}

fn iterator_from_array<'a>(
    scope: &mut v8::PinScope<'a, '_>,
    array: v8::Local<'a, v8::Array>,
) -> v8::Local<'a, v8::Value> {
    let Some(iterator_key) = symbol_iterator_value(scope) else {
        return array.into();
    };
    let Some(array_iterator) = array.get(scope, iterator_key) else {
        return array.into();
    };
    let Ok(array_iterator_func) = v8::Local::<v8::Function>::try_from(array_iterator) else {
        return array.into();
    };

    array_iterator_func
        .call(scope, array.into(), &[])
        .unwrap_or_else(|| array.into())
}

/// Setup FormData API in V8 context
pub fn setup_form_data_api(
    scope: &mut v8::ContextScope<v8::HandleScope>,
    context: &v8::Local<v8::Context>,
) -> Result<()> {
    // FormData constructor
    let form_data_template: _ = v8::FunctionTemplate::new(scope, form_data_constructor);
    let form_data_constructor: _ = form_data_template.get_function(scope).unwrap();
    // Register FormData constructor
    let global: _ = context.global(scope);
    let form_data_key: _ = v8::String::new(scope, "FormData").unwrap();
    global.set(scope, form_data_key.into(), form_data_constructor.into());
    Ok(())
}
/// FormData constructor callback
fn form_data_constructor(
    scope: &mut v8::PinScope,
    _args: v8::FunctionCallbackArguments,
    mut retval: v8::ReturnValue,
) {
    // Create ObjectTemplate with internal field for storing FormData index
    let form_data_template = v8::ObjectTemplate::new(scope);
    form_data_template.set_internal_field_count(1);

    let form_data_obj: v8::Local<v8::Object> = match form_data_template.new_instance(scope) {
        Some(obj) => obj,
        None => {
            retval.set(v8::null(scope).into());
            return;
        }
    };

    // Get next available index for this FormData instance
    static FORMDATA_INDEX_COUNTER: OnceLock<Mutex<usize>> = OnceLock::new();
    let index_counter = FORMDATA_INDEX_COUNTER.get_or_init(|| Mutex::new(0));
    let mut counter = index_counter.lock().unwrap();
    let index = *counter;
    *counter += 1;
    drop(counter);

    // Store index in internal field 0
    let index_val: v8::Local<v8::Value> = v8::Integer::new(scope, index as i32).into();
    form_data_obj.set_internal_field(0, index_val.into());

    // Initialize FormData entries for this index
    let mut cache = get_formdata_cache().lock().unwrap();
    cache.insert(index, Vec::new());
    drop(cache);

    // Add append method
    let append_key = v8::String::new(scope, "append").unwrap().into();
    let append_template = v8::FunctionTemplate::new(scope, form_data_append);
    let append_func = append_template.get_function(scope).unwrap();
    form_data_obj.set(scope, append_key, append_func.into());

    // Add delete method
    let delete_key = v8::String::new(scope, "delete").unwrap().into();
    let delete_template = v8::FunctionTemplate::new(scope, form_data_delete);
    let delete_func = delete_template.get_function(scope).unwrap();
    form_data_obj.set(scope, delete_key, delete_func.into());

    // Add get method
    let get_key = v8::String::new(scope, "get").unwrap().into();
    let get_template = v8::FunctionTemplate::new(scope, form_data_get);
    let get_func = get_template.get_function(scope).unwrap();
    form_data_obj.set(scope, get_key, get_func.into());

    // Add getAll method
    let get_all_key = v8::String::new(scope, "getAll").unwrap().into();
    let get_all_template = v8::FunctionTemplate::new(scope, form_data_get_all);
    let get_all_func = get_all_template.get_function(scope).unwrap();
    form_data_obj.set(scope, get_all_key, get_all_func.into());

    // Add has method
    let has_key = v8::String::new(scope, "has").unwrap().into();
    let has_template = v8::FunctionTemplate::new(scope, form_data_has);
    let has_func = has_template.get_function(scope).unwrap();
    form_data_obj.set(scope, has_key, has_func.into());

    // Add set method
    let set_key = v8::String::new(scope, "set").unwrap().into();
    let set_template = v8::FunctionTemplate::new(scope, form_data_set);
    let set_func = set_template.get_function(scope).unwrap();
    form_data_obj.set(scope, set_key, set_func.into());

    // Add entries method
    let entries_key = v8::String::new(scope, "entries").unwrap().into();
    let entries_template = v8::FunctionTemplate::new(scope, form_data_entries);
    let entries_func = entries_template.get_function(scope).unwrap();
    form_data_obj.set(scope, entries_key, entries_func.into());
    if let Some(iterator_key) = symbol_iterator_value(scope) {
        form_data_obj.set(scope, iterator_key, entries_func.into());
    }

    // Add keys method
    let keys_key = v8::String::new(scope, "keys").unwrap().into();
    let keys_template = v8::FunctionTemplate::new(scope, form_data_keys);
    let keys_func = keys_template.get_function(scope).unwrap();
    form_data_obj.set(scope, keys_key, keys_func.into());

    // Add values method
    let values_key = v8::String::new(scope, "values").unwrap().into();
    let values_template = v8::FunctionTemplate::new(scope, form_data_values);
    let values_func = values_template.get_function(scope).unwrap();
    form_data_obj.set(scope, values_key, values_func.into());

    // Add forEach method
    let for_each_key = v8::String::new(scope, "forEach").unwrap().into();
    let for_each_template = v8::FunctionTemplate::new(scope, form_data_for_each);
    let for_each_func = for_each_template.get_function(scope).unwrap();
    form_data_obj.set(scope, for_each_key, for_each_func.into());

    retval.set(form_data_obj.into());
}

/// FormData.append() method - adds a new value to an existing key
/// or adds the key if it doesn't exist
fn form_data_append(
    scope: &mut v8::PinScope,
    args: v8::FunctionCallbackArguments,
    _retval: v8::ReturnValue,
) {
    let this_obj: v8::Local<v8::Object> = args.this();

    // Get index from internal field
    let index = this_obj
        .get_internal_field(scope, 0)
        .and_then(|data| v8::Local::<v8::Value>::try_from(data).ok())
        .and_then(|v| v.to_integer(scope))
        .map(|i| i.value() as usize)
        .unwrap_or(usize::MAX);

    // Get name
    let name = if let Some(name_val) = args.get(0).to_string(scope) {
        name_val.to_rust_string_lossy(scope)
    } else {
        return;
    };

    // Get optional filename (third argument)
    let explicit_filename = value_to_optional_string(scope, args.get(2));

    // Get value (can be string or Blob/File)
    let Some((value, body, filename, content_type)) =
        form_data_value_entry(scope, args.get(1), explicit_filename)
    else {
        return;
    };

    // Store in cache
    let mut cache = get_formdata_cache().lock().unwrap();
    if let Some(entries) = cache.get_mut(&index) {
        entries.push(FormDataEntry {
            name,
            value,
            body,
            filename,
            content_type,
        });
    }
}

/// FormData.delete() method - removes all values associated with a key
fn form_data_delete(
    scope: &mut v8::PinScope,
    args: v8::FunctionCallbackArguments,
    _retval: v8::ReturnValue,
) {
    let this_obj: v8::Local<v8::Object> = args.this();

    let index = this_obj
        .get_internal_field(scope, 0)
        .and_then(|data| v8::Local::<v8::Value>::try_from(data).ok())
        .and_then(|v| v.to_integer(scope))
        .map(|i| i.value() as usize)
        .unwrap_or(usize::MAX);

    let name = if let Some(name_val) = args.get(0).to_string(scope) {
        name_val.to_rust_string_lossy(scope)
    } else {
        return;
    };

    let mut cache = get_formdata_cache().lock().unwrap();
    if let Some(entries) = cache.get_mut(&index) {
        entries.retain(|entry| entry.name != name);
    }
}

/// FormData.get() method - returns the first value associated with a key
fn form_data_get(
    scope: &mut v8::PinScope,
    args: v8::FunctionCallbackArguments,
    mut retval: v8::ReturnValue,
) {
    let this_obj: v8::Local<v8::Object> = args.this();

    let index = this_obj
        .get_internal_field(scope, 0)
        .and_then(|data| v8::Local::<v8::Value>::try_from(data).ok())
        .and_then(|v| v.to_integer(scope))
        .map(|i| i.value() as usize)
        .unwrap_or(usize::MAX);

    let name = if let Some(name_val) = args.get(0).to_string(scope) {
        name_val.to_rust_string_lossy(scope)
    } else {
        retval.set(v8::null(scope).into());
        return;
    };

    let cache = get_formdata_cache().lock().unwrap();
    if let Some(entries) = cache.get(&index) {
        for entry in entries {
            if entry.name == name {
                retval.set(v8::String::new(scope, &entry.value).unwrap().into());
                return;
            }
        }
    }
    retval.set(v8::null(scope).into());
}

/// FormData.getAll() method - returns all values associated with a key
fn form_data_get_all(
    scope: &mut v8::PinScope,
    args: v8::FunctionCallbackArguments,
    mut retval: v8::ReturnValue,
) {
    let this_obj: v8::Local<v8::Object> = args.this();

    let index = this_obj
        .get_internal_field(scope, 0)
        .and_then(|data| v8::Local::<v8::Value>::try_from(data).ok())
        .and_then(|v| v.to_integer(scope))
        .map(|i| i.value() as usize)
        .unwrap_or(usize::MAX);

    let name = if let Some(name_val) = args.get(0).to_string(scope) {
        name_val.to_rust_string_lossy(scope)
    } else {
        retval.set(v8::Array::new(scope, 0).into());
        return;
    };

    let cache = get_formdata_cache().lock().unwrap();
    if let Some(entries) = cache.get(&index) {
        let values: Vec<_> = entries
            .iter()
            .filter(|entry| entry.name == name)
            .map(|entry| v8::String::new(scope, &entry.value).unwrap().into())
            .collect();
        let array = v8::Array::new_with_elements(scope, &values);
        retval.set(array.into());
    } else {
        retval.set(v8::Array::new(scope, 0).into());
    }
}

/// FormData.has() method - returns whether a key exists
fn form_data_has(
    scope: &mut v8::PinScope,
    args: v8::FunctionCallbackArguments,
    mut retval: v8::ReturnValue,
) {
    let this_obj: v8::Local<v8::Object> = args.this();

    let index = this_obj
        .get_internal_field(scope, 0)
        .and_then(|data| v8::Local::<v8::Value>::try_from(data).ok())
        .and_then(|v| v.to_integer(scope))
        .map(|i| i.value() as usize)
        .unwrap_or(usize::MAX);

    let name = if let Some(name_val) = args.get(0).to_string(scope) {
        name_val.to_rust_string_lossy(scope)
    } else {
        retval.set(v8::Boolean::new(scope, false).into());
        return;
    };

    let cache = get_formdata_cache().lock().unwrap();
    let has_key = cache
        .get(&index)
        .map(|entries| entries.iter().any(|e| e.name == name))
        .unwrap_or(false);

    retval.set(v8::Boolean::new(scope, has_key).into());
}

/// FormData.set() method - sets a new value for a key, replacing existing values
fn form_data_set(
    scope: &mut v8::PinScope,
    args: v8::FunctionCallbackArguments,
    _retval: v8::ReturnValue,
) {
    let this_obj: v8::Local<v8::Object> = args.this();

    let index = this_obj
        .get_internal_field(scope, 0)
        .and_then(|data| v8::Local::<v8::Value>::try_from(data).ok())
        .and_then(|v| v.to_integer(scope))
        .map(|i| i.value() as usize)
        .unwrap_or(usize::MAX);

    let name = if let Some(name_val) = args.get(0).to_string(scope) {
        name_val.to_rust_string_lossy(scope)
    } else {
        return;
    };

    let explicit_filename = value_to_optional_string(scope, args.get(2));
    let Some((value, body, filename, content_type)) =
        form_data_value_entry(scope, args.get(1), explicit_filename)
    else {
        return;
    };

    let mut cache = get_formdata_cache().lock().unwrap();
    if let Some(entries) = cache.get_mut(&index) {
        // Remove all existing entries with this name
        entries.retain(|entry| entry.name != name);
        // Add new entry
        entries.push(FormDataEntry {
            name,
            value,
            body,
            filename,
            content_type,
        });
    }
}

/// FormData.entries() method - returns an iterator of key/value pairs
fn form_data_entries(
    scope: &mut v8::PinScope,
    args: v8::FunctionCallbackArguments,
    mut retval: v8::ReturnValue,
) {
    let entries = form_data_entries_for_object(scope, args.this());
    let array = form_data_entries_array(scope, &entries);
    retval.set(iterator_from_array(scope, array));
}

/// FormData.keys() method - returns an iterator of keys
fn form_data_keys(
    scope: &mut v8::PinScope,
    args: v8::FunctionCallbackArguments,
    mut retval: v8::ReturnValue,
) {
    let entries = form_data_entries_for_object(scope, args.this());
    let array = form_data_keys_array(scope, &entries);
    retval.set(iterator_from_array(scope, array));
}

/// FormData.values() method - returns an iterator of values
fn form_data_values(
    scope: &mut v8::PinScope,
    args: v8::FunctionCallbackArguments,
    mut retval: v8::ReturnValue,
) {
    let entries = form_data_entries_for_object(scope, args.this());
    let array = form_data_values_array(scope, &entries);
    retval.set(iterator_from_array(scope, array));
}

/// FormData.forEach() method - iterates over all key/value pairs
fn form_data_for_each(
    scope: &mut v8::PinScope,
    args: v8::FunctionCallbackArguments,
    _retval: v8::ReturnValue,
) {
    let callback_val = args.get(0);
    let Ok(callback) = v8::Local::<v8::Function>::try_from(callback_val) else {
        return;
    };

    let this_obj = args.this();
    let entries = form_data_entries_for_object(scope, this_obj);
    let this_arg = args.get(1);
    let receiver = if this_arg.is_undefined() {
        v8::undefined(scope).into()
    } else {
        this_arg
    };

    for entry in entries {
        let value_arg: v8::Local<v8::Value> = v8::String::new(scope, &entry.value).unwrap().into();
        let name_arg: v8::Local<v8::Value> = v8::String::new(scope, &entry.name).unwrap().into();
        let owner_arg: v8::Local<v8::Value> = this_obj.into();
        let _ = callback.call(scope, receiver, &[value_arg, name_arg, owner_arg]);
    }
}

/// Export FormData entries for use with fetch
/// Returns the entries as a Vec for serialization
pub fn get_formdata_entries(index: usize) -> Option<Vec<FormDataEntry>> {
    let cache = FORMDATA_CACHE.get_or_init(|| Mutex::new(HashMap::new()));
    let guard = cache.lock().unwrap();
    guard.get(&index).cloned()
}

/// Check if a V8 value is a FormData object and return its internal index
pub fn get_formdata_index(scope: &mut v8::PinScope, value: v8::Local<v8::Value>) -> Option<usize> {
    if !value.is_object() {
        return None;
    }

    let obj = value.to_object(scope)?;

    // Check if object has internal fields (our FormData uses ObjectTemplate with internal field count)
    if obj.internal_field_count() < 1 {
        return None;
    }

    // Check if the object has FormData methods
    let append_key = v8::String::new(scope, "append").unwrap().into();
    if !obj.has(scope, append_key).unwrap_or(false) {
        return None;
    }

    // Get the internal field which stores the FormData index
    obj.get_internal_field(scope, 0)
        .and_then(|data| v8::Local::<v8::Value>::try_from(data).ok())
        .and_then(|v| v.to_integer(scope))
        .map(|i| i.value() as usize)
}

/// Serialize FormData entries to multipart/form-data format
pub fn serialize_formdata_multipart(entries: &[FormDataEntry], boundary: &str) -> Vec<u8> {
    let mut result = Vec::new();

    for entry in entries {
        // Write boundary
        result.extend_from_slice(b"--");
        result.extend_from_slice(boundary.as_bytes());
        result.extend_from_slice(b"\r\n");

        // Write Content-Disposition header
        result.extend_from_slice(b"Content-Disposition: form-data; name=\"");
        result.extend_from_slice(entry.name.as_bytes());
        result.extend_from_slice(b"\"");

        // Add filename if present
        if let Some(filename) = &entry.filename {
            result.extend_from_slice(b"; filename=\"");
            result.extend_from_slice(filename.as_bytes());
            result.extend_from_slice(b"\"");
        }
        result.extend_from_slice(b"\r\n");

        // Write Content-Type header
        if !entry.content_type.is_empty() {
            result.extend_from_slice(b"Content-Type: ");
            result.extend_from_slice(entry.content_type.as_bytes());
            result.extend_from_slice(b"\r\n");
        }

        // Empty line before body
        result.extend_from_slice(b"\r\n");

        // Write body
        result.extend_from_slice(&entry.body);
        result.extend_from_slice(b"\r\n");
    }

    // Write final boundary
    result.extend_from_slice(b"--");
    result.extend_from_slice(boundary.as_bytes());
    result.extend_from_slice(b"--\r\n");

    result
}

/// Generate a random boundary string for multipart/form-data
pub fn generate_boundary() -> String {
    use rand::Rng;
    let mut rng = rand::thread_rng();
    let random: u128 = rng.gen();
    format!("----AmberFormBoundary{}", random)
}