| 
									
										
										
										
											2023-03-28 18:58:41 -07:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org> | 
					
						
							| 
									
										
										
										
											2023-12-28 11:47:56 +13:00
										 |  |  |  * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org> | 
					
						
							| 
									
										
										
										
											2023-03-28 18:58:41 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/VM.h>
 | 
					
						
							| 
									
										
										
										
											2023-04-02 15:14:21 -07:00
										 |  |  | #include <LibWeb/Streams/AbstractOperations.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-28 18:58:41 -07:00
										 |  |  | #include <LibWeb/Streams/UnderlyingSource.h>
 | 
					
						
							| 
									
										
										
										
											2023-12-28 11:47:56 +13:00
										 |  |  | #include <LibWeb/WebIDL/AbstractOperations.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-28 18:58:41 -07:00
										 |  |  | #include <LibWeb/WebIDL/CallbackType.h>
 | 
					
						
							| 
									
										
										
										
											2023-12-28 11:47:56 +13:00
										 |  |  | #include <LibWeb/WebIDL/Types.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-28 18:58:41 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::Streams { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | JS::ThrowCompletionOr<UnderlyingSource> UnderlyingSource::from_value(JS::VM& vm, JS::Value value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!value.is_object()) | 
					
						
							|  |  |  |         return UnderlyingSource {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto& object = value.as_object(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     UnderlyingSource underlying_source { | 
					
						
							| 
									
										
										
										
											2024-12-08 17:43:24 +13:00
										 |  |  |         .start = TRY(WebIDL::property_to_callback(vm, value, "start", WebIDL::OperationReturnsPromise::No)), | 
					
						
							|  |  |  |         .pull = TRY(WebIDL::property_to_callback(vm, value, "pull", WebIDL::OperationReturnsPromise::Yes)), | 
					
						
							|  |  |  |         .cancel = TRY(WebIDL::property_to_callback(vm, value, "cancel", WebIDL::OperationReturnsPromise::Yes)), | 
					
						
							| 
									
										
										
										
											2023-03-28 18:58:41 -07:00
										 |  |  |         .type = {}, | 
					
						
							|  |  |  |         .auto_allocate_chunk_size = {}, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-22 01:57:12 -07:00
										 |  |  |     auto type_value = TRY(object.get("type")); | 
					
						
							|  |  |  |     if (!type_value.is_undefined()) { | 
					
						
							|  |  |  |         auto type_string = TRY(type_value.to_string(vm)); | 
					
						
							|  |  |  |         if (type_string == "bytes"sv) | 
					
						
							| 
									
										
										
										
											2023-03-28 18:58:41 -07:00
										 |  |  |             underlying_source.type = ReadableStreamType::Bytes; | 
					
						
							| 
									
										
										
										
											2023-04-22 01:57:12 -07:00
										 |  |  |         else | 
					
						
							| 
									
										
										
										
											2023-12-28 12:39:05 +13:00
										 |  |  |             return vm.throw_completion<JS::TypeError>(MUST(String::formatted("Unknown stream type '{}'", type_value))); | 
					
						
							| 
									
										
										
										
											2023-03-28 18:58:41 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-28 11:47:56 +13:00
										 |  |  |     if (TRY(object.has_property("autoAllocateChunkSize"))) { | 
					
						
							|  |  |  |         auto value = TRY(object.get("autoAllocateChunkSize")); | 
					
						
							|  |  |  |         underlying_source.auto_allocate_chunk_size = TRY(WebIDL::convert_to_int<WebIDL::UnsignedLongLong>(vm, value, WebIDL::EnforceRange::Yes)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-03-28 18:58:41 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return underlying_source; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |