UnderlyingSource.cpp 1.7 KB

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