Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / fetch / body_init.lucb

#==============================================================================================
#
#   body_init - Fetch::safely_extract_body and extract_body of a byte sequence
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Fetch/BodyInit.cpp (lines 25-155) for the ReadonlyBytes
#       alternative of BodyInitOrReadableBytes, which byte_sequence_as_body (srcdoc documents,
#       generated pages) passes: a new ReadableStream set up with byte reading support, the
#       bytes enqueued as one Uint8Array chunk and the stream closed (region p2s, over the
#       ReadableStream stand-in of DESIGN.md §3.6). The other alternatives (ReadableStream, Blob,
#       BufferSource, FormData, URLSearchParams, a string) come from JS and are phase 3.
#
#       Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
#       Copyright (c) 2022-2024, Kenneth Myhra <kennethmyhra@serenityos.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Extracting a body =======================================================================

## Ported from Libraries/LibWeb/Fetch/BodyInit.cpp:26 Web::Fetch::safely_extract_body (of a byte sequence, the
## ReadonlyBytes alternative of BodyInitOrReadableBytes).
## https://fetch.spec.whatwg.org/#bodyinit-safely-extract
pub func fetch_safely_extract_body_bytes(realm: Realm*, bytes: const u8[]) -> FetchBodyWithType:
    # 1. If object is a ReadableStream object, then:
    #    1. Assert: object is neither disturbed nor locked.
    # (object is a byte sequence.)

    # 2. Return the result of extracting object.
    return fetch_extract_body_bytes(realm, bytes) else trap("MUST: extract_body")

## Ported from Libraries/LibWeb/Fetch/BodyInit.cpp:39 Web::Fetch::extract_body (of a byte sequence, the ReadonlyBytes
## alternative of BodyInitOrReadableBytes; keepalive only matters for a ReadableStream).
## https://fetch.spec.whatwg.org/#concept-bodyinit-extract
pub func fetch_extract_body_bytes(realm: Realm*, bytes: const u8[], keepalive: bool = false) -> FetchBodyWithType!:
    var execution_context: HtmlTemporaryExecutionContext = ---
    html_temporary_execution_context_construct_realm(&execution_context, realm, HtmlTemporaryExecutionContextCallbacksEnabled.yes)
    defer html_temporary_execution_context_destruct(&execution_context)

    let vm = realm_vm(realm)
    _ = keepalive

    # 1. Let stream be null.
    # 2. If object is a ReadableStream object, then set stream to object.
    # 3. Otherwise, if object is a Blob object, set stream to the result of running object’s get stream.
    # 4. Otherwise, set stream to a new ReadableStream object, and set up stream with byte reading support.
    let stream = realm_create_streams_readable_stream(realm, realm)
    streams_readable_stream_set_up_with_byte_reading_support(stream)

    # 5. Assert: stream is a ReadableStream object.

    # 6. Let action be null.
    # 7. Let source be null.
    # 8. Let length be null.
    var length: u64? = none

    # 9. Let type be null.
    let type_: ak.ByteString? = none

    # 10. Switch on object.
    #     -> byte sequence: Set source to object.
    let source = ak.byte_buffer_copy_span_u8(bytes) else trap("MUST: ByteBuffer::copy")

    # 11. If source is a byte sequence, then set action to a step that returns source and length to source’s length.
    # (The action's own copy of the source.)
    let action_source = ak.byte_buffer_copy_span_u8(ak.byte_buffer_bytes_const(&source)) else trap("MUST: ByteBuffer::copy")
    length = (u64)ak.byte_buffer_size(&source)

    # 12. If action is non-null, then run these steps in parallel:
    # AD-HOC: There is a race condition between document population(Ex: load_html_document->fully_read->read_all_bytes->readable_stream_default_reader_read)
    # and stream population. So currently we run stream population synchronously.
    var action_execution_context: HtmlTemporaryExecutionContext = ---
    html_temporary_execution_context_construct_realm(&action_execution_context, realm, HtmlTemporaryExecutionContextCallbacksEnabled.yes)
    defer html_temporary_execution_context_destruct(&action_execution_context)

    # 1. Run action.
    let action_bytes = action_source

    # Whenever one or more bytes are available and stream is not errored, enqueue the result of creating a
    # Uint8Array from the available bytes into stream.
    if not ak.byte_buffer_is_empty(&action_bytes) and not streams_readable_stream_is_errored(stream):
        let stream_realm = streams_readable_stream_realm(stream)
        let array_buffer = js_array_buffer_create_byte_buffer(stream_realm, action_bytes)
        let chunk = js_uint8_array_create(stream_realm, (u32)js_array_buffer_byte_length(array_buffer), array_buffer)

        streams_readable_stream_enqueue(stream, js_value_from_object((JsObject*)chunk)) else trap("FIXME: should propagate errors")

    # When running action is done, close stream.
    streams_readable_stream_close(stream)

    # 13. Let body be a body whose stream is stream, source is source, and length is length.
    let body = fetch_body_create_fetch_body_source_type(vm, stream, .byte_buffer(source), length)

    # 14. Return (body, type).
    return FetchBodyWithType(body = body, type_ = type_)