UnderlyingSource.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/VM.h>
  7. #include <LibWeb/Streams/AbstractOperations.h>
  8. #include <LibWeb/Streams/UnderlyingSource.h>
  9. #include <LibWeb/WebIDL/CallbackType.h>
  10. namespace Web::Streams {
  11. JS::ThrowCompletionOr<UnderlyingSource> UnderlyingSource::from_value(JS::VM& vm, JS::Value value)
  12. {
  13. if (!value.is_object())
  14. return UnderlyingSource {};
  15. auto& object = value.as_object();
  16. UnderlyingSource underlying_source {
  17. .start = TRY(property_to_callback(vm, value, "start", WebIDL::OperationReturnsPromise::No)),
  18. .pull = TRY(property_to_callback(vm, value, "pull", WebIDL::OperationReturnsPromise::Yes)),
  19. .cancel = TRY(property_to_callback(vm, value, "cancel", WebIDL::OperationReturnsPromise::Yes)),
  20. .type = {},
  21. .auto_allocate_chunk_size = {},
  22. };
  23. if (TRY(object.has_property("type"))) {
  24. auto type_value = TRY(TRY(object.get("type")).to_string(vm));
  25. if (type_value == "bytes"sv) {
  26. underlying_source.type = ReadableStreamType::Bytes;
  27. } else {
  28. return vm.throw_completion<JS::TypeError>(DeprecatedString::formatted("Unknown stream type '{}'", type_value));
  29. }
  30. }
  31. if (TRY(object.has_property("autoAllocateChunkSize")))
  32. underlying_source.auto_allocate_chunk_size = TRY(TRY(object.get("autoAllocateChunkSize")).to_bigint_int64(vm));
  33. return underlying_source;
  34. }
  35. }