Fetching.cpp 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. /*
  2. * Copyright (c) 2022-2023, networkException <networkexception@serenityos.org>
  3. * Copyright (c) 2024, Tim Ledbetter <timledbetter@gmail.com>
  4. * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibCore/EventLoop.h>
  9. #include <LibGC/Function.h>
  10. #include <LibJS/Runtime/ModuleRequest.h>
  11. #include <LibTextCodec/Decoder.h>
  12. #include <LibWeb/Bindings/MainThreadVM.h>
  13. #include <LibWeb/Bindings/PrincipalHostDefined.h>
  14. #include <LibWeb/DOM/Document.h>
  15. #include <LibWeb/DOMURL/DOMURL.h>
  16. #include <LibWeb/Fetch/Fetching/Fetching.h>
  17. #include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
  18. #include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
  19. #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
  20. #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
  21. #include <LibWeb/Fetch/Infrastructure/URL.h>
  22. #include <LibWeb/HTML/HTMLScriptElement.h>
  23. #include <LibWeb/HTML/PotentialCORSRequest.h>
  24. #include <LibWeb/HTML/Scripting/ClassicScript.h>
  25. #include <LibWeb/HTML/Scripting/Environments.h>
  26. #include <LibWeb/HTML/Scripting/Fetching.h>
  27. #include <LibWeb/HTML/Scripting/ModuleScript.h>
  28. #include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
  29. #include <LibWeb/HTML/Window.h>
  30. #include <LibWeb/Infra/Strings.h>
  31. #include <LibWeb/MimeSniff/MimeType.h>
  32. namespace Web::HTML {
  33. GC_DEFINE_ALLOCATOR(FetchContext);
  34. OnFetchScriptComplete create_on_fetch_script_complete(GC::Heap& heap, Function<void(GC::Ptr<Script>)> function)
  35. {
  36. return GC::create_function(heap, move(function));
  37. }
  38. PerformTheFetchHook create_perform_the_fetch_hook(GC::Heap& heap, Function<WebIDL::ExceptionOr<void>(GC::Ref<Fetch::Infrastructure::Request>, TopLevelModule, Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction)> function)
  39. {
  40. return GC::create_function(heap, move(function));
  41. }
  42. ScriptFetchOptions default_script_fetch_options()
  43. {
  44. // The default script fetch options are a script fetch options whose cryptographic nonce is the empty string,
  45. // integrity metadata is the empty string, parser metadata is "not-parser-inserted", credentials mode is "same-origin",
  46. // referrer policy is the empty string, and fetch priority is "auto".
  47. return ScriptFetchOptions {
  48. .cryptographic_nonce = {},
  49. .integrity_metadata = {},
  50. .parser_metadata = Fetch::Infrastructure::Request::ParserMetadata::NotParserInserted,
  51. .credentials_mode = Fetch::Infrastructure::Request::CredentialsMode::SameOrigin,
  52. .referrer_policy = {},
  53. .fetch_priority = Fetch::Infrastructure::Request::Priority::Auto
  54. };
  55. }
  56. // https://html.spec.whatwg.org/multipage/webappapis.html#module-type-from-module-request
  57. ByteString module_type_from_module_request(JS::ModuleRequest const& module_request)
  58. {
  59. // 1. Let moduleType be "javascript".
  60. ByteString module_type = "javascript"sv;
  61. // 2. If moduleRequest.[[Attributes]] has a Record entry such that entry.[[Key]] is "type", then:
  62. for (auto const& entry : module_request.attributes) {
  63. if (entry.key != "type"sv)
  64. continue;
  65. // 1. If entry.[[Value]] is "javascript", then set moduleType to null.
  66. if (entry.value == "javascript"sv)
  67. module_type = nullptr;
  68. // 2. Otherwise, set moduleType to entry.[[Value]].
  69. else
  70. module_type = entry.value;
  71. }
  72. // 3. Return moduleType.
  73. return module_type;
  74. }
  75. // https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-specifier
  76. // https://whatpr.org/html/9893/webappapis.html#resolve-a-module-specifier
  77. WebIDL::ExceptionOr<URL::URL> resolve_module_specifier(Optional<Script&> referring_script, ByteString const& specifier)
  78. {
  79. auto& vm = Bindings::main_thread_vm();
  80. // 1. Let realm and baseURL be null.
  81. GC::Ptr<JS::Realm> realm;
  82. Optional<URL::URL> base_url;
  83. // 2. If referringScript is not null, then:
  84. if (referring_script.has_value()) {
  85. // 1. Set realm to referringScript's realm.
  86. realm = referring_script->realm();
  87. // 2. Set baseURL to referringScript's base URL.
  88. base_url = referring_script->base_url();
  89. }
  90. // 3. Otherwise:
  91. else {
  92. // 1. Assert: there is a current realm.
  93. VERIFY(vm.current_realm());
  94. // 2. Set realm to the current realm.
  95. realm = *vm.current_realm();
  96. // 3. Set baseURL to realm's principal realm's settings object's API base URL.
  97. base_url = Bindings::principal_host_defined_environment_settings_object(HTML::principal_realm(*realm)).api_base_url();
  98. }
  99. // 4. Let importMap be an empty import map.
  100. ImportMap import_map;
  101. // 5. If realm's global object implements Window, then set importMap to settingsObject's global object's import map.
  102. if (is<Window>(realm->global_object()))
  103. import_map = verify_cast<Window>(realm->global_object()).import_map();
  104. // 6. Let baseURLString be baseURL, serialized.
  105. auto base_url_string = base_url->serialize();
  106. // 7. Let asURL be the result of resolving a URL-like module specifier given specifier and baseURL.
  107. auto as_url = resolve_url_like_module_specifier(specifier, *base_url);
  108. // 8. Let normalizedSpecifier be the serialization of asURL, if asURL is non-null; otherwise, specifier.
  109. auto normalized_specifier = as_url.has_value() ? as_url->serialize().to_byte_string() : specifier;
  110. // 9. For each scopePrefix → scopeImports of importMap's scopes:
  111. for (auto const& entry : import_map.scopes()) {
  112. // FIXME: Clarify if the serialization steps need to be run here. The steps below assume
  113. // scopePrefix to be a string.
  114. auto const& scope_prefix = entry.key.serialize();
  115. auto const& scope_imports = entry.value;
  116. // 1. If scopePrefix is baseURLString, or if scopePrefix ends with U+002F (/) and scopePrefix is a code unit prefix of baseURLString, then:
  117. if (scope_prefix == base_url_string || (scope_prefix.ends_with('/') && Infra::is_code_unit_prefix(scope_prefix, base_url_string))) {
  118. // 1. Let scopeImportsMatch be the result of resolving an imports match given normalizedSpecifier, asURL, and scopeImports.
  119. auto scope_imports_match = TRY(resolve_imports_match(normalized_specifier, as_url, scope_imports));
  120. // 2. If scopeImportsMatch is not null, then return scopeImportsMatch.
  121. if (scope_imports_match.has_value())
  122. return scope_imports_match.release_value();
  123. }
  124. }
  125. // 10. Let topLevelImportsMatch be the result of resolving an imports match given normalizedSpecifier, asURL, and importMap's imports.
  126. auto top_level_imports_match = TRY(resolve_imports_match(normalized_specifier, as_url, import_map.imports()));
  127. // 11. If topLevelImportsMatch is not null, then return topLevelImportsMatch.
  128. if (top_level_imports_match.has_value())
  129. return top_level_imports_match.release_value();
  130. // 12. If asURL is not null, then return asURL.
  131. if (as_url.has_value())
  132. return as_url.release_value();
  133. // 13. Throw a TypeError indicating that specifier was a bare specifier, but was not remapped to anything by importMap.
  134. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, String::formatted("Failed to resolve non relative module specifier '{}' from an import map.", specifier).release_value_but_fixme_should_propagate_errors() };
  135. }
  136. // https://html.spec.whatwg.org/multipage/webappapis.html#resolving-an-imports-match
  137. WebIDL::ExceptionOr<Optional<URL::URL>> resolve_imports_match(ByteString const& normalized_specifier, Optional<URL::URL> as_url, ModuleSpecifierMap const& specifier_map)
  138. {
  139. // 1. For each specifierKey → resolutionResult of specifierMap:
  140. for (auto const& [specifier_key, resolution_result] : specifier_map) {
  141. // 1. If specifierKey is normalizedSpecifier, then:
  142. if (specifier_key == normalized_specifier) {
  143. // 1. If resolutionResult is null, then throw a TypeError indicating that resolution of specifierKey was blocked by a null entry.
  144. if (!resolution_result.has_value())
  145. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, String::formatted("Import resolution of '{}' was blocked by a null entry.", specifier_key).release_value_but_fixme_should_propagate_errors() };
  146. // 2. Assert: resolutionResult is a URL.
  147. VERIFY(resolution_result->is_valid());
  148. // 3. Return resolutionResult.
  149. return resolution_result;
  150. }
  151. // 2. If all of the following are true:
  152. if (
  153. // - specifierKey ends with U+002F (/);
  154. specifier_key.ends_with("/"sv) &&
  155. // - specifierKey is a code unit prefix of normalizedSpecifier; and
  156. Infra::is_code_unit_prefix(specifier_key, normalized_specifier) &&
  157. // - either asURL is null, or asURL is special,
  158. (!as_url.has_value() || as_url->is_special())
  159. // then:
  160. ) {
  161. // 1. If resolutionResult is null, then throw a TypeError indicating that the resolution of specifierKey was blocked by a null entry.
  162. if (!resolution_result.has_value())
  163. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, String::formatted("Import resolution of '{}' was blocked by a null entry.", specifier_key).release_value_but_fixme_should_propagate_errors() };
  164. // 2. Assert: resolutionResult is a URL.
  165. VERIFY(resolution_result->is_valid());
  166. // 3. Let afterPrefix be the portion of normalizedSpecifier after the initial specifierKey prefix.
  167. // FIXME: Clarify if this is meant by the portion after the initial specifierKey prefix.
  168. auto after_prefix = normalized_specifier.substring(specifier_key.length());
  169. // 4. Assert: resolutionResult, serialized, ends with U+002F (/), as enforced during parsing.
  170. VERIFY(resolution_result->serialize().ends_with('/'));
  171. // 5. Let url be the result of URL parsing afterPrefix with resolutionResult.
  172. auto url = DOMURL::parse(after_prefix, *resolution_result);
  173. // 6. If url is failure, then throw a TypeError indicating that resolution of normalizedSpecifier was blocked since the afterPrefix portion
  174. // could not be URL-parsed relative to the resolutionResult mapped to by the specifierKey prefix.
  175. if (!url.is_valid())
  176. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, String::formatted("Could not resolve '{}' as the after prefix portion could not be URL-parsed.", normalized_specifier).release_value_but_fixme_should_propagate_errors() };
  177. // 7. Assert: url is a URL.
  178. VERIFY(url.is_valid());
  179. // 8. If the serialization of resolutionResult is not a code unit prefix of the serialization of url, then throw a TypeError indicating
  180. // that the resolution of normalizedSpecifier was blocked due to it backtracking above its prefix specifierKey.
  181. if (!Infra::is_code_unit_prefix(resolution_result->serialize(), url.serialize()))
  182. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, String::formatted("Could not resolve '{}' as it backtracks above its prefix specifierKey.", normalized_specifier).release_value_but_fixme_should_propagate_errors() };
  183. // 9. Return url.
  184. return url;
  185. }
  186. }
  187. // 2. Return null.
  188. return Optional<URL::URL> {};
  189. }
  190. // https://html.spec.whatwg.org/multipage/webappapis.html#resolving-a-url-like-module-specifier
  191. Optional<URL::URL> resolve_url_like_module_specifier(ByteString const& specifier, URL::URL const& base_url)
  192. {
  193. // 1. If specifier starts with "/", "./", or "../", then:
  194. if (specifier.starts_with("/"sv) || specifier.starts_with("./"sv) || specifier.starts_with("../"sv)) {
  195. // 1. Let url be the result of URL parsing specifier with baseURL.
  196. auto url = DOMURL::parse(specifier, base_url);
  197. // 2. If url is failure, then return null.
  198. if (!url.is_valid())
  199. return {};
  200. // 3. Return url.
  201. return url;
  202. }
  203. // 2. Let url be the result of URL parsing specifier (with no base URL).
  204. auto url = DOMURL::parse(specifier);
  205. // 3. If url is failure, then return null.
  206. if (!url.is_valid())
  207. return {};
  208. // 4. Return url.
  209. return url;
  210. }
  211. // https://html.spec.whatwg.org/multipage/webappapis.html#set-up-the-classic-script-request
  212. static void set_up_classic_script_request(Fetch::Infrastructure::Request& request, ScriptFetchOptions const& options)
  213. {
  214. // Set request's cryptographic nonce metadata to options's cryptographic nonce, its integrity metadata to options's
  215. // integrity metadata, its parser metadata to options's parser metadata, its referrer policy to options's referrer
  216. // policy, its render-blocking to options's render-blocking, and its priority to options's fetch priority.
  217. request.set_cryptographic_nonce_metadata(options.cryptographic_nonce);
  218. request.set_integrity_metadata(options.integrity_metadata);
  219. request.set_parser_metadata(options.parser_metadata);
  220. request.set_referrer_policy(options.referrer_policy);
  221. request.set_render_blocking(options.render_blocking);
  222. request.set_priority(options.fetch_priority);
  223. }
  224. // https://html.spec.whatwg.org/multipage/webappapis.html#set-up-the-module-script-request
  225. static void set_up_module_script_request(Fetch::Infrastructure::Request& request, ScriptFetchOptions const& options)
  226. {
  227. // Set request's cryptographic nonce metadata to options's cryptographic nonce, its integrity metadata to options's
  228. // integrity metadata, its parser metadata to options's parser metadata, its credentials mode to options's credentials
  229. // mode, its referrer policy to options's referrer policy, its render-blocking to options's render-blocking, and its
  230. // priority to options's fetch priority.
  231. request.set_cryptographic_nonce_metadata(options.cryptographic_nonce);
  232. request.set_integrity_metadata(options.integrity_metadata);
  233. request.set_parser_metadata(options.parser_metadata);
  234. request.set_credentials_mode(options.credentials_mode);
  235. request.set_referrer_policy(options.referrer_policy);
  236. request.set_render_blocking(options.render_blocking);
  237. request.set_priority(options.fetch_priority);
  238. }
  239. // https://html.spec.whatwg.org/multipage/webappapis.html#get-the-descendant-script-fetch-options
  240. ScriptFetchOptions get_descendant_script_fetch_options(ScriptFetchOptions const& original_options, URL::URL const& url, EnvironmentSettingsObject& settings_object)
  241. {
  242. // 1. Let newOptions be a copy of originalOptions.
  243. auto new_options = original_options;
  244. // 2. Let integrity be the empty string.
  245. String integrity;
  246. // 3. If settingsObject's global object is a Window object, then set integrity to the result of resolving a module integrity metadata with url and settingsObject.
  247. if (is<Window>(settings_object.global_object()))
  248. integrity = resolve_a_module_integrity_metadata(url, settings_object);
  249. // 4. Set newOptions's integrity metadata to integrity.
  250. new_options.integrity_metadata = integrity;
  251. // 5. Set newOptions's fetch priority to "auto".
  252. new_options.fetch_priority = Fetch::Infrastructure::Request::Priority::Auto;
  253. // 6. Return newOptions.
  254. return new_options;
  255. }
  256. // https://html.spec.whatwg.org/multipage/webappapis.html#resolving-a-module-integrity-metadata
  257. String resolve_a_module_integrity_metadata(const URL::URL& url, EnvironmentSettingsObject& settings_object)
  258. {
  259. // 1. Assert: settingsObject's global object is a Window object.
  260. VERIFY(is<Window>(settings_object.global_object()));
  261. // 2. Let map be settingsObject's global object's import map.
  262. auto map = static_cast<Window const&>(settings_object.global_object()).import_map();
  263. // 3. If map's integrity[url] does not exist, then return the empty string.
  264. // 4. Return map's integrity[url].
  265. return MUST(String::from_byte_string(map.integrity().get(url).value_or("")));
  266. }
  267. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-classic-script
  268. WebIDL::ExceptionOr<void> fetch_classic_script(GC::Ref<HTMLScriptElement> element, URL::URL const& url, EnvironmentSettingsObject& settings_object, ScriptFetchOptions options, CORSSettingAttribute cors_setting, String character_encoding, OnFetchScriptComplete on_complete)
  269. {
  270. auto& realm = element->realm();
  271. auto& vm = realm.vm();
  272. // 1. Let request be the result of creating a potential-CORS request given url, "script", and CORS setting.
  273. auto request = create_potential_CORS_request(vm, url, Fetch::Infrastructure::Request::Destination::Script, cors_setting);
  274. // 2. Set request's client to settings object.
  275. request->set_client(&settings_object);
  276. // 3. Set request's initiator type to "script".
  277. request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::Script);
  278. // 4. Set up the classic script request given request and options.
  279. set_up_classic_script_request(*request, options);
  280. // 5. Fetch request with the following processResponseConsumeBody steps given response response and null, failure,
  281. // or a byte sequence bodyBytes:
  282. Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {};
  283. fetch_algorithms_input.process_response_consume_body = [&settings_object, options = move(options), character_encoding = move(character_encoding), on_complete = move(on_complete)](auto response, auto body_bytes) {
  284. // 1. Set response to response's unsafe response.
  285. response = response->unsafe_response();
  286. // 2. If either of the following conditions are met:
  287. // - bodyBytes is null or failure; or
  288. // - response's status is not an ok status,
  289. if (body_bytes.template has<Empty>() || body_bytes.template has<Fetch::Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag>() || !Fetch::Infrastructure::is_ok_status(response->status())) {
  290. // then run onComplete given null, and abort these steps.
  291. on_complete->function()(nullptr);
  292. return;
  293. }
  294. // 3. Let potentialMIMETypeForEncoding be the result of extracting a MIME type given response's header list.
  295. auto potential_mime_type_for_encoding = response->header_list()->extract_mime_type();
  296. // 4. Set character encoding to the result of legacy extracting an encoding given potentialMIMETypeForEncoding
  297. // and character encoding.
  298. auto extracted_character_encoding = Fetch::Infrastructure::legacy_extract_an_encoding(potential_mime_type_for_encoding, character_encoding);
  299. // 5. Let source text be the result of decoding bodyBytes to Unicode, using character encoding as the fallback
  300. // encoding.
  301. auto fallback_decoder = TextCodec::decoder_for(extracted_character_encoding);
  302. VERIFY(fallback_decoder.has_value());
  303. auto source_text = TextCodec::convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(*fallback_decoder, body_bytes.template get<ByteBuffer>()).release_value_but_fixme_should_propagate_errors();
  304. // 6. Let muted errors be true if response was CORS-cross-origin, and false otherwise.
  305. auto muted_errors = response->is_cors_cross_origin() ? ClassicScript::MutedErrors::Yes : ClassicScript::MutedErrors::No;
  306. // 7. Let script be the result of creating a classic script given source text, settings object's realm, response's URL,
  307. // options, and muted errors.
  308. // FIXME: Pass options.
  309. auto response_url = response->url().value_or({});
  310. auto script = ClassicScript::create(response_url.to_byte_string(), source_text, settings_object.realm(), response_url, 1, muted_errors);
  311. // 8. Run onComplete given script.
  312. on_complete->function()(script);
  313. };
  314. TRY(Fetch::Fetching::fetch(element->realm(), request, Fetch::Infrastructure::FetchAlgorithms::create(vm, move(fetch_algorithms_input))));
  315. return {};
  316. }
  317. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-classic-worker-script
  318. WebIDL::ExceptionOr<void> fetch_classic_worker_script(URL::URL const& url, EnvironmentSettingsObject& fetch_client, Fetch::Infrastructure::Request::Destination destination, EnvironmentSettingsObject& settings_object, PerformTheFetchHook perform_fetch, OnFetchScriptComplete on_complete)
  319. {
  320. auto& realm = settings_object.realm();
  321. auto& vm = realm.vm();
  322. // 1. Let request be a new request whose URL is url, client is fetchClient, destination is destination, initiator type is "other",
  323. // mode is "same-origin", credentials mode is "same-origin", parser metadata is "not parser-inserted",
  324. // and whose use-URL-credentials flag is set.
  325. auto request = Fetch::Infrastructure::Request::create(vm);
  326. request->set_url(url);
  327. request->set_client(&fetch_client);
  328. request->set_destination(destination);
  329. request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::Other);
  330. // FIXME: Use proper SameOrigin CORS mode once Origins are set properly in WorkerHost processes
  331. request->set_mode(Fetch::Infrastructure::Request::Mode::NoCORS);
  332. request->set_credentials_mode(Fetch::Infrastructure::Request::CredentialsMode::SameOrigin);
  333. request->set_parser_metadata(Fetch::Infrastructure::Request::ParserMetadata::NotParserInserted);
  334. request->set_use_url_credentials(true);
  335. auto process_response_consume_body = [&settings_object, on_complete = move(on_complete)](auto response, auto body_bytes) {
  336. // 1. Set response to response's unsafe response.
  337. response = response->unsafe_response();
  338. // 2. If either of the following conditions are met:
  339. // - bodyBytes is null or failure; or
  340. // - response's status is not an ok status,
  341. if (body_bytes.template has<Empty>() || body_bytes.template has<Fetch::Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag>() || !Fetch::Infrastructure::is_ok_status(response->status())) {
  342. // then run onComplete given null, and abort these steps.
  343. on_complete->function()(nullptr);
  344. return;
  345. }
  346. // 3. If all of the following are true:
  347. // - response's URL's scheme is an HTTP(S) scheme; and
  348. // - the result of extracting a MIME type from response's header list is not a JavaScript MIME type,
  349. auto maybe_mime_type = response->header_list()->extract_mime_type();
  350. auto mime_type_is_javascript = maybe_mime_type.has_value() && maybe_mime_type->is_javascript();
  351. if (response->url().has_value() && Fetch::Infrastructure::is_http_or_https_scheme(response->url()->scheme()) && !mime_type_is_javascript) {
  352. auto mime_type_serialized = maybe_mime_type.has_value() ? maybe_mime_type->serialized() : "unknown"_string;
  353. dbgln("Invalid non-javascript mime type \"{}\" for worker script at {}", mime_type_serialized, response->url().value());
  354. // then run onComplete given null, and abort these steps.
  355. on_complete->function()(nullptr);
  356. return;
  357. }
  358. // NOTE: Other fetch schemes are exempted from MIME type checking for historical web-compatibility reasons.
  359. // We might be able to tighten this in the future; see https://github.com/whatwg/html/issues/3255.
  360. // 4. Let sourceText be the result of UTF-8 decoding bodyBytes.
  361. auto decoder = TextCodec::decoder_for("UTF-8"sv);
  362. VERIFY(decoder.has_value());
  363. auto source_text = TextCodec::convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, body_bytes.template get<ByteBuffer>()).release_value_but_fixme_should_propagate_errors();
  364. // 5. Let script be the result of creating a classic script using sourceText, settingsObject's realm,
  365. // response's URL, and the default classic script fetch options.
  366. auto response_url = response->url().value_or({});
  367. auto script = ClassicScript::create(response_url.to_byte_string(), source_text, settings_object.realm(), response_url);
  368. // 6. Run onComplete given script.
  369. on_complete->function()(script);
  370. };
  371. // 2. If performFetch was given, run performFetch with request, true, and with processResponseConsumeBody as defined below.
  372. if (perform_fetch != nullptr) {
  373. TRY(perform_fetch->function()(request, TopLevelModule::Yes, move(process_response_consume_body)));
  374. }
  375. // Otherwise, fetch request with processResponseConsumeBody set to processResponseConsumeBody as defined below.
  376. else {
  377. Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {};
  378. fetch_algorithms_input.process_response_consume_body = move(process_response_consume_body);
  379. TRY(Fetch::Fetching::fetch(realm, request, Fetch::Infrastructure::FetchAlgorithms::create(vm, move(fetch_algorithms_input))));
  380. }
  381. return {};
  382. }
  383. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-classic-worker-imported-script
  384. WebIDL::ExceptionOr<GC::Ref<ClassicScript>> fetch_a_classic_worker_imported_script(URL::URL const& url, HTML::EnvironmentSettingsObject& settings_object, PerformTheFetchHook perform_fetch)
  385. {
  386. auto& realm = settings_object.realm();
  387. auto& vm = realm.vm();
  388. // 1. Let response be null.
  389. IGNORE_USE_IN_ESCAPING_LAMBDA GC::Ptr<Fetch::Infrastructure::Response> response = nullptr;
  390. // 2. Let bodyBytes be null.
  391. Fetch::Infrastructure::FetchAlgorithms::BodyBytes body_bytes;
  392. // 3. Let request be a new request whose URL is url, client is settingsObject, destination is "script", initiator type is "other",
  393. // parser metadata is "not parser-inserted", and whose use-URL-credentials flag is set.
  394. auto request = Fetch::Infrastructure::Request::create(vm);
  395. request->set_url(url);
  396. request->set_client(&settings_object);
  397. request->set_destination(Fetch::Infrastructure::Request::Destination::Script);
  398. request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::Other);
  399. request->set_parser_metadata(Fetch::Infrastructure::Request::ParserMetadata::NotParserInserted);
  400. request->set_use_url_credentials(true);
  401. auto process_response_consume_body = [&response, &body_bytes](GC::Ref<Fetch::Infrastructure::Response> res, Fetch::Infrastructure::FetchAlgorithms::BodyBytes bb) {
  402. // 1. Set bodyBytes to bb.
  403. body_bytes = move(bb);
  404. // 2. Set response to res.
  405. response = res;
  406. };
  407. // 4. If performFetch was given, run performFetch with request, isTopLevel, and with processResponseConsumeBody as defined below.
  408. if (perform_fetch) {
  409. TRY(perform_fetch->function()(request, TopLevelModule::Yes, move(process_response_consume_body)));
  410. }
  411. // Otherwise, fetch request with processResponseConsumeBody set to processResponseConsumeBody as defined below.
  412. else {
  413. Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {};
  414. fetch_algorithms_input.process_response_consume_body = move(process_response_consume_body);
  415. TRY(Fetch::Fetching::fetch(realm, request, Fetch::Infrastructure::FetchAlgorithms::create(vm, move(fetch_algorithms_input))));
  416. }
  417. // 5. Pause until response is not null.
  418. // FIXME: Consider using a "response holder" to avoid needing to annotate response as IGNORE_USE_IN_ESCAPING_LAMBDA.
  419. auto& event_loop = settings_object.responsible_event_loop();
  420. event_loop.spin_until(GC::create_function(vm.heap(), [&]() -> bool {
  421. return response;
  422. }));
  423. // 6. Set response to response's unsafe response.
  424. response = response->unsafe_response();
  425. // 7. If any of the following are true:
  426. // - bodyBytes is null or failure;
  427. // - response's status is not an ok status; or
  428. // - the result of extracting a MIME type from response's header list is not a JavaScript MIME type,
  429. // then throw a "NetworkError" DOMException.
  430. if (body_bytes.template has<Empty>() || body_bytes.template has<Fetch::Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag>()
  431. || !Fetch::Infrastructure::is_ok_status(response->status())
  432. || !response->header_list()->extract_mime_type().has_value() || !response->header_list()->extract_mime_type()->is_javascript()) {
  433. return WebIDL::NetworkError::create(realm, "Network error"_string);
  434. }
  435. // 8. Let sourceText be the result of UTF-8 decoding bodyBytes.
  436. auto decoder = TextCodec::decoder_for("UTF-8"sv);
  437. VERIFY(decoder.has_value());
  438. auto source_text = TextCodec::convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, body_bytes.get<ByteBuffer>()).release_value_but_fixme_should_propagate_errors();
  439. // 9. Let mutedErrors be true if response was CORS-cross-origin, and false otherwise.
  440. auto muted_errors = response->is_cors_cross_origin() ? ClassicScript::MutedErrors::Yes : ClassicScript::MutedErrors::No;
  441. // 10. Let script be the result of creating a classic script given sourceText, settingsObject's realm, response's URL, the default classic script fetch options, and mutedErrors.
  442. auto response_url = response->url().value_or({});
  443. auto script = ClassicScript::create(response_url.to_byte_string(), source_text, settings_object.realm(), response_url, 1, muted_errors);
  444. // 11. Return script.
  445. return script;
  446. }
  447. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-module-worker-script-tree
  448. WebIDL::ExceptionOr<void> fetch_module_worker_script_graph(URL::URL const& url, EnvironmentSettingsObject& fetch_client, Fetch::Infrastructure::Request::Destination destination, EnvironmentSettingsObject& settings_object, PerformTheFetchHook perform_fetch, OnFetchScriptComplete on_complete)
  449. {
  450. return fetch_worklet_module_worker_script_graph(url, fetch_client, destination, settings_object, move(perform_fetch), move(on_complete));
  451. }
  452. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-worklet/module-worker-script-graph
  453. // https://whatpr.org/html/9893/webappapis.html#fetch-a-worklet/module-worker-script-graph
  454. WebIDL::ExceptionOr<void> fetch_worklet_module_worker_script_graph(URL::URL const& url, EnvironmentSettingsObject& fetch_client, Fetch::Infrastructure::Request::Destination destination, EnvironmentSettingsObject& settings_object, PerformTheFetchHook perform_fetch, OnFetchScriptComplete on_complete)
  455. {
  456. auto& realm = settings_object.realm();
  457. auto& vm = realm.vm();
  458. // 1. Let options be a script fetch options whose cryptographic nonce is the empty string,
  459. // integrity metadata is the empty string, parser metadata is "not-parser-inserted",
  460. // credentials mode is credentialsMode, referrer policy is the empty string, and fetch priority is "auto".
  461. // FIXME: credentialsMode
  462. auto options = ScriptFetchOptions {
  463. .cryptographic_nonce = String {},
  464. .integrity_metadata = String {},
  465. .parser_metadata = Fetch::Infrastructure::Request::ParserMetadata::NotParserInserted,
  466. .credentials_mode = Fetch::Infrastructure::Request::CredentialsMode::SameOrigin,
  467. .referrer_policy = ReferrerPolicy::ReferrerPolicy::EmptyString,
  468. .fetch_priority = Fetch::Infrastructure::Request::Priority::Auto
  469. };
  470. // onSingleFetchComplete given result is the following algorithm:
  471. auto on_single_fetch_complete = create_on_fetch_script_complete(vm.heap(), [&realm, &fetch_client, destination, perform_fetch = perform_fetch, on_complete = move(on_complete)](auto result) mutable {
  472. // 1. If result is null, run onComplete with null, and abort these steps.
  473. if (!result) {
  474. dbgln("on single fetch complete with nool");
  475. on_complete->function()(nullptr);
  476. return;
  477. }
  478. // 2. Fetch the descendants of and link result given fetchClient, destination, and onComplete. If performFetch was given, pass it along as well.
  479. fetch_descendants_of_and_link_a_module_script(realm, verify_cast<JavaScriptModuleScript>(*result), fetch_client, destination, move(perform_fetch), on_complete);
  480. });
  481. // 2. Fetch a single module script given url, fetchClient, destination, options, settingsObject's realm, "client", true,
  482. // and onSingleFetchComplete as defined below. If performFetch was given, pass it along as well.
  483. fetch_single_module_script(realm, url, fetch_client, destination, options, settings_object.realm(), Fetch::Infrastructure::Request::Referrer::Client, {}, TopLevelModule::Yes, move(perform_fetch), on_single_fetch_complete);
  484. return {};
  485. }
  486. // https://html.spec.whatwg.org/multipage/webappapis.html#internal-module-script-graph-fetching-procedure
  487. void fetch_internal_module_script_graph(JS::Realm& realm, JS::ModuleRequest const& module_request, EnvironmentSettingsObject& fetch_client_settings_object, Fetch::Infrastructure::Request::Destination destination, ScriptFetchOptions const& options, Script& referring_script, HashTable<ModuleLocationTuple> const& visited_set, PerformTheFetchHook perform_fetch, OnFetchScriptComplete on_complete)
  488. {
  489. // 1. Let url be the result of resolving a module specifier given referringScript and moduleRequest.[[Specifier]].
  490. auto url = MUST(resolve_module_specifier(referring_script, module_request.module_specifier));
  491. // 2. Assert: the previous step never throws an exception, because resolving a module specifier must have been previously successful with these same two arguments.
  492. // NOTE: Handled by MUST above.
  493. // 3. Let moduleType be the result of running the module type from module request steps given moduleRequest.
  494. auto module_type = module_type_from_module_request(module_request);
  495. // 4. Assert: visited set contains (url, moduleType).
  496. VERIFY(visited_set.contains({ url, module_type }));
  497. // onSingleFetchComplete given result is the following algorithm:
  498. auto on_single_fetch_complete = create_on_fetch_script_complete(realm.heap(), [&realm, perform_fetch, on_complete, &fetch_client_settings_object, destination, visited_set](auto result) mutable {
  499. // 1. If result is null, run onComplete with null, and abort these steps.
  500. if (!result) {
  501. on_complete->function()(nullptr);
  502. return;
  503. }
  504. // 2. Fetch the descendants of result given fetch client settings object, destination, visited set, and with onComplete. If performFetch was given, pass it along as well.
  505. auto& module_script = verify_cast<JavaScriptModuleScript>(*result);
  506. fetch_descendants_of_a_module_script(realm, module_script, fetch_client_settings_object, destination, visited_set, perform_fetch, on_complete);
  507. });
  508. // 5. Fetch a single module script given url, fetch client settings object, destination, options, referringScript's settings object's realm,
  509. // referringScript's base URL, moduleRequest, false, and onSingleFetchComplete as defined below. If performFetch was given, pass it along as well.
  510. fetch_single_module_script(realm, url, fetch_client_settings_object, destination, options, referring_script.realm(), referring_script.base_url(), module_request, TopLevelModule::No, perform_fetch, on_single_fetch_complete);
  511. }
  512. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-the-descendants-of-a-module-script
  513. void fetch_descendants_of_a_module_script(JS::Realm& realm, JavaScriptModuleScript& module_script, EnvironmentSettingsObject& fetch_client_settings_object, Fetch::Infrastructure::Request::Destination destination, HashTable<ModuleLocationTuple> visited_set, PerformTheFetchHook perform_fetch, OnFetchScriptComplete on_complete)
  514. {
  515. // 1. If module script's record is null, run onComplete with module script and return.
  516. if (!module_script.record()) {
  517. on_complete->function()(&module_script);
  518. return;
  519. }
  520. // 2. Let record be module script's record.
  521. auto const& record = module_script.record();
  522. // 3. If record is not a Cyclic Module Record, or if record.[[RequestedModules]] is empty, run onComplete with module script and return.
  523. // FIXME: Currently record is always a cyclic module.
  524. if (record->requested_modules().is_empty()) {
  525. on_complete->function()(&module_script);
  526. return;
  527. }
  528. // 4. Let moduleRequests be a new empty list.
  529. Vector<JS::ModuleRequest> module_requests;
  530. // 5. For each ModuleRequest Record requested of record.[[RequestedModules]],
  531. for (auto const& requested : record->requested_modules()) {
  532. // 1. Let url be the result of resolving a module specifier given module script and requested.[[Specifier]].
  533. auto url = MUST(resolve_module_specifier(module_script, requested.module_specifier));
  534. // 2. Assert: the previous step never throws an exception, because resolving a module specifier must have been previously successful with these same two arguments.
  535. // NOTE: Handled by MUST above.
  536. // 3. Let moduleType be the result of running the module type from module request steps given requested.
  537. auto module_type = module_type_from_module_request(requested);
  538. // 4. If visited set does not contain (url, moduleType), then:
  539. if (!visited_set.contains({ url, module_type })) {
  540. // 1. Append requested to moduleRequests.
  541. module_requests.append(requested);
  542. // 2. Append (url, moduleType) to visited set.
  543. visited_set.set({ url, module_type });
  544. }
  545. }
  546. // FIXME: 6. Let options be the descendant script fetch options for module script's fetch options.
  547. ScriptFetchOptions options;
  548. // FIXME: 7. Assert: options is not null, as module script is a JavaScript module script.
  549. // 8. Let pendingCount be the length of moduleRequests.
  550. auto pending_count = module_requests.size();
  551. // 9. If pendingCount is zero, run onComplete with module script.
  552. if (pending_count == 0) {
  553. on_complete->function()(&module_script);
  554. return;
  555. }
  556. // 10. Let failed be false.
  557. bool failed = false;
  558. // 11. For each moduleRequest in moduleRequests, perform the internal module script graph fetching procedure given moduleRequest,
  559. // fetch client settings object, destination, options, module script, visited set, and onInternalFetchingComplete as defined below.
  560. // If performFetch was given, pass it along as well.
  561. for (auto const& module_request : module_requests) {
  562. // onInternalFetchingComplete given result is the following algorithm:
  563. auto on_internal_fetching_complete = create_on_fetch_script_complete(realm.heap(), [failed, pending_count, &module_script, on_complete](auto result) mutable {
  564. // 1. If failed is true, then abort these steps.
  565. if (failed)
  566. return;
  567. // 2. If result is null, then set failed to true, run onComplete with null, and abort these steps.
  568. if (!result) {
  569. failed = true;
  570. on_complete->function()(nullptr);
  571. return;
  572. }
  573. // 3. Assert: pendingCount is greater than zero.
  574. VERIFY(pending_count > 0);
  575. // 4. Decrement pendingCount by one.
  576. --pending_count;
  577. // 5. If pendingCount is zero, run onComplete with module script.
  578. if (pending_count == 0)
  579. on_complete->function()(&module_script);
  580. });
  581. fetch_internal_module_script_graph(realm, module_request, fetch_client_settings_object, destination, options, module_script, visited_set, perform_fetch, on_internal_fetching_complete);
  582. }
  583. }
  584. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-destination-from-module-type
  585. Fetch::Infrastructure::Request::Destination fetch_destination_from_module_type(Fetch::Infrastructure::Request::Destination default_destination, ByteString const& module_type)
  586. {
  587. // 1. If moduleType is "json", then return "json".
  588. if (module_type == "json"sv)
  589. return Fetch::Infrastructure::Request::Destination::JSON;
  590. // 2. If moduleType is "css", then return "style".
  591. if (module_type == "css"sv)
  592. return Fetch::Infrastructure::Request::Destination::Style;
  593. // 3. Return defaultDestination.
  594. return default_destination;
  595. }
  596. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-single-module-script
  597. // https://whatpr.org/html/9893/webappapis.html#fetch-a-single-module-script
  598. void fetch_single_module_script(JS::Realm& realm,
  599. URL::URL const& url,
  600. EnvironmentSettingsObject& fetch_client,
  601. Fetch::Infrastructure::Request::Destination destination,
  602. ScriptFetchOptions const& options,
  603. JS::Realm& module_map_realm,
  604. Web::Fetch::Infrastructure::Request::ReferrerType const& referrer,
  605. Optional<JS::ModuleRequest> const& module_request,
  606. TopLevelModule is_top_level,
  607. PerformTheFetchHook perform_fetch,
  608. OnFetchScriptComplete on_complete)
  609. {
  610. // 1. Let moduleType be "javascript".
  611. ByteString module_type = "javascript"sv;
  612. // 2. If moduleRequest was given, then set moduleType to the result of running the module type from module request steps given moduleRequest.
  613. if (module_request.has_value())
  614. module_type = module_type_from_module_request(*module_request);
  615. // 3. Assert: the result of running the module type allowed steps given moduleType and moduleMapRealm is true.
  616. // Otherwise we would not have reached this point because a failure would have been raised when inspecting moduleRequest.[[Assertions]]
  617. // in create a JavaScript module script or fetch a single imported module script.
  618. VERIFY(module_type_allowed(module_map_realm, module_type));
  619. // 4. Let moduleMap be moduleMapRealm's module map.
  620. auto& module_map = module_map_of_realm(module_map_realm);
  621. // 5. If moduleMap[(url, moduleType)] is "fetching", wait in parallel until that entry's value changes,
  622. // then queue a task on the networking task source to proceed with running the following steps.
  623. if (module_map.is_fetching(url, module_type)) {
  624. module_map.wait_for_change(realm.heap(), url, module_type, [on_complete, &realm](auto entry) -> void {
  625. HTML::queue_global_task(HTML::Task::Source::Networking, realm.global_object(), GC::create_function(realm.heap(), [on_complete, entry] {
  626. // FIXME: This should run other steps, for now we just assume the script loaded.
  627. VERIFY(entry.type == ModuleMap::EntryType::ModuleScript || entry.type == ModuleMap::EntryType::Failed);
  628. on_complete->function()(entry.module_script);
  629. }));
  630. });
  631. return;
  632. }
  633. // 6. If moduleMap[(url, moduleType)] exists, run onComplete given moduleMap[(url, moduleType)], and return.
  634. auto entry = module_map.get(url, module_type);
  635. if (entry.has_value() && entry->type == ModuleMap::EntryType::ModuleScript) {
  636. on_complete->function()(entry->module_script);
  637. return;
  638. }
  639. // 7. Set moduleMap[(url, moduleType)] to "fetching".
  640. module_map.set(url, module_type, { ModuleMap::EntryType::Fetching, nullptr });
  641. // 8. Let request be a new request whose URL is url, mode is "cors", referrer is referrer, and client is fetchClient.
  642. auto request = Fetch::Infrastructure::Request::create(realm.vm());
  643. request->set_url(url);
  644. request->set_mode(Fetch::Infrastructure::Request::Mode::CORS);
  645. request->set_referrer(referrer);
  646. request->set_client(&fetch_client);
  647. // 9. Set request's destination to the result of running the fetch destination from module type steps given destination and moduleType.
  648. request->set_destination(fetch_destination_from_module_type(destination, module_type));
  649. // 10. If destination is "worker", "sharedworker", or "serviceworker", and isTopLevel is true, then set request's mode to "same-origin".
  650. if ((destination == Fetch::Infrastructure::Request::Destination::Worker || destination == Fetch::Infrastructure::Request::Destination::SharedWorker || destination == Fetch::Infrastructure::Request::Destination::ServiceWorker) && is_top_level == TopLevelModule::Yes)
  651. request->set_mode(Fetch::Infrastructure::Request::Mode::SameOrigin);
  652. // 11. Set request's initiator type to "script".
  653. request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::Script);
  654. // 12. Set up the module script request given request and options.
  655. set_up_module_script_request(request, options);
  656. // 13. If performFetch was given, run performFetch with request, isTopLevel, and with processResponseConsumeBody as defined below.
  657. // Otherwise, fetch request with processResponseConsumeBody set to processResponseConsumeBody as defined below.
  658. // In both cases, let processResponseConsumeBody given response response and null, failure, or a byte sequence bodyBytes be the following algorithm:
  659. auto process_response_consume_body = [&module_map, url, module_type, &module_map_realm, on_complete](GC::Ref<Fetch::Infrastructure::Response> response, Fetch::Infrastructure::FetchAlgorithms::BodyBytes body_bytes) {
  660. // 1. If either of the following conditions are met:
  661. // - bodyBytes is null or failure; or
  662. // - response's status is not an ok status,
  663. if (body_bytes.has<Empty>() || body_bytes.has<Fetch::Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag>() || !Fetch::Infrastructure::is_ok_status(response->status())) {
  664. // then set moduleMap[(url, moduleType)] to null, run onComplete given null, and abort these steps.
  665. module_map.set(url, module_type, { ModuleMap::EntryType::Failed, nullptr });
  666. on_complete->function()(nullptr);
  667. return;
  668. }
  669. // 2. Let sourceText be the result of UTF-8 decoding bodyBytes.
  670. auto decoder = TextCodec::decoder_for("UTF-8"sv);
  671. VERIFY(decoder.has_value());
  672. auto source_text = TextCodec::convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, body_bytes.get<ByteBuffer>()).release_value_but_fixme_should_propagate_errors();
  673. // 3. Let mimeType be the result of extracting a MIME type from response's header list.
  674. auto mime_type = response->header_list()->extract_mime_type();
  675. // 4. Let moduleScript be null.
  676. GC::Ptr<JavaScriptModuleScript> module_script;
  677. // FIXME: 5. Let referrerPolicy be the result of parsing the `Referrer-Policy` header given response. [REFERRERPOLICY]
  678. // FIXME: 6. If referrerPolicy is not the empty string, set options's referrer policy to referrerPolicy.
  679. // 7. If mimeType is a JavaScript MIME type and moduleType is "javascript", then set moduleScript to the result of creating a JavaScript module script given sourceText, moduleMapRealm, response's URL, and options.
  680. // FIXME: Pass options.
  681. if (mime_type->is_javascript() && module_type == "javascript")
  682. module_script = JavaScriptModuleScript::create(url.basename(), source_text, module_map_realm, response->url().value_or({})).release_value_but_fixme_should_propagate_errors();
  683. // FIXME: 8. If the MIME type essence of mimeType is "text/css" and moduleType is "css", then set moduleScript to the result of creating a CSS module script given sourceText and settingsObject.
  684. // FIXME: 9. If mimeType is a JSON MIME type and moduleType is "json", then set moduleScript to the result of creating a JSON module script given sourceText and settingsObject.
  685. // 10. Set moduleMap[(url, moduleType)] to moduleScript, and run onComplete given moduleScript.
  686. module_map.set(url, module_type, { ModuleMap::EntryType::ModuleScript, module_script });
  687. on_complete->function()(module_script);
  688. };
  689. if (perform_fetch != nullptr) {
  690. perform_fetch->function()(request, is_top_level, move(process_response_consume_body)).release_value_but_fixme_should_propagate_errors();
  691. } else {
  692. Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {};
  693. fetch_algorithms_input.process_response_consume_body = move(process_response_consume_body);
  694. Fetch::Fetching::fetch(realm, request, Fetch::Infrastructure::FetchAlgorithms::create(realm.vm(), move(fetch_algorithms_input))).release_value_but_fixme_should_propagate_errors();
  695. }
  696. }
  697. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-module-script-tree
  698. // https://whatpr.org/html/9893/webappapis.html#fetch-a-module-script-tree
  699. void fetch_external_module_script_graph(JS::Realm& realm, URL::URL const& url, EnvironmentSettingsObject& settings_object, ScriptFetchOptions const& options, OnFetchScriptComplete on_complete)
  700. {
  701. // 1. Disallow further import maps given settingsObject's realm.
  702. disallow_further_import_maps(settings_object.realm());
  703. auto steps = create_on_fetch_script_complete(realm.heap(), [&realm, &settings_object, on_complete, url](auto result) mutable {
  704. // 1. If result is null, run onComplete given null, and abort these steps.
  705. if (!result) {
  706. on_complete->function()(nullptr);
  707. return;
  708. }
  709. // 2. Fetch the descendants of and link result given settingsObject, "script", and onComplete.
  710. auto& module_script = verify_cast<JavaScriptModuleScript>(*result);
  711. fetch_descendants_of_and_link_a_module_script(realm, module_script, settings_object, Fetch::Infrastructure::Request::Destination::Script, nullptr, on_complete);
  712. });
  713. // 2. Fetch a single module script given url, settingsObject, "script", options, settingsObject's realm, "client", true, and with the following steps given result:
  714. fetch_single_module_script(realm, url, settings_object, Fetch::Infrastructure::Request::Destination::Script, options, settings_object.realm(), Web::Fetch::Infrastructure::Request::Referrer::Client, {}, TopLevelModule::Yes, nullptr, steps);
  715. }
  716. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-an-inline-module-script-graph
  717. // https://whatpr.org/html/9893/webappapis.html#fetch-an-inline-module-script-graph
  718. void fetch_inline_module_script_graph(JS::Realm& realm, ByteString const& filename, ByteString const& source_text, URL::URL const& base_url, EnvironmentSettingsObject& settings_object, OnFetchScriptComplete on_complete)
  719. {
  720. // 1. Disallow further import maps given settingsObject's realm.
  721. disallow_further_import_maps(settings_object.realm());
  722. // 2. Let script be the result of creating a JavaScript module script using sourceText, settingsObject's realm, baseURL, and options.
  723. auto script = JavaScriptModuleScript::create(filename, source_text.view(), settings_object.realm(), base_url).release_value_but_fixme_should_propagate_errors();
  724. // 3. If script is null, run onComplete given null, and return.
  725. if (!script) {
  726. on_complete->function()(nullptr);
  727. return;
  728. }
  729. // 5. Fetch the descendants of and link script, given settingsObject, "script", and onComplete.
  730. fetch_descendants_of_and_link_a_module_script(realm, *script, settings_object, Fetch::Infrastructure::Request::Destination::Script, nullptr, on_complete);
  731. }
  732. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-single-imported-module-script
  733. void fetch_single_imported_module_script(JS::Realm& realm,
  734. URL::URL const& url,
  735. EnvironmentSettingsObject& fetch_client,
  736. Fetch::Infrastructure::Request::Destination destination,
  737. ScriptFetchOptions const& options,
  738. JS::Realm& module_map_realm,
  739. Fetch::Infrastructure::Request::ReferrerType referrer,
  740. JS::ModuleRequest const& module_request,
  741. PerformTheFetchHook perform_fetch,
  742. OnFetchScriptComplete on_complete)
  743. {
  744. // 1. Assert: moduleRequest.[[Attributes]] does not contain any Record entry such that entry.[[Key]] is not "type",
  745. // because we only asked for "type" attributes in HostGetSupportedImportAttributes.
  746. for (auto const& entry : module_request.attributes)
  747. VERIFY(entry.key == "type"sv);
  748. // 2. Let moduleType be the result of running the module type from module request steps given moduleRequest.
  749. auto module_type = module_type_from_module_request(module_request);
  750. // 3. If the result of running the module type allowed steps given moduleType and moduleMapRealm is false,
  751. // then run onComplete given null, and return.
  752. if (!module_type_allowed(module_map_realm, module_type)) {
  753. on_complete->function()(nullptr);
  754. return;
  755. }
  756. // 4. Fetch a single module script given url, fetchClient, destination, options, moduleMapRealm, referrer, moduleRequest, false,
  757. // and onComplete. If performFetch was given, pass it along as well.
  758. fetch_single_module_script(realm, url, fetch_client, destination, options, module_map_realm, referrer, module_request, TopLevelModule::No, perform_fetch, on_complete);
  759. }
  760. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-the-descendants-of-and-link-a-module-script
  761. void fetch_descendants_of_and_link_a_module_script(JS::Realm& realm,
  762. JavaScriptModuleScript& module_script,
  763. EnvironmentSettingsObject& fetch_client,
  764. Fetch::Infrastructure::Request::Destination destination,
  765. PerformTheFetchHook perform_fetch,
  766. OnFetchScriptComplete on_complete)
  767. {
  768. // 1. Let record be moduleScript's record.
  769. auto* record = module_script.record();
  770. // 2. If record is null, then:
  771. if (!record) {
  772. // 1. Set moduleScript's error to rethrow to moduleScript's parse error.
  773. module_script.set_error_to_rethrow(module_script.parse_error());
  774. // 2. Run onComplete given moduleScript.
  775. on_complete->function()(module_script);
  776. // 3. Return.
  777. return;
  778. }
  779. // 3. Let state be Record { [[ParseError]]: null, [[Destination]]: destination, [[PerformFetch]]: null, [[FetchClient]]: fetchClient }.
  780. auto state = realm.heap().allocate<FetchContext>(JS::js_null(), destination, nullptr, fetch_client);
  781. // 4. If performFetch was given, set state.[[PerformFetch]] to performFetch.
  782. state->perform_fetch = perform_fetch;
  783. // FIXME: These should most likely be steps in the spec.
  784. // NOTE: For reasons beyond my understanding, we cannot use TemporaryExecutionContext here.
  785. // Calling perform_a_microtask_checkpoint() on the fetch_client's responsible_event_loop
  786. // prevents this from functioning properly. HTMLParser::the_end would be run before
  787. // HTMLScriptElement::prepare_script had a chance to setup the callback to mark_done properly,
  788. // resulting in the event loop hanging forever awaiting for the script to be ready for parser
  789. // execution.
  790. realm.vm().push_execution_context(fetch_client.realm_execution_context());
  791. prepare_to_run_callback(realm);
  792. // 5. Let loadingPromise be record.LoadRequestedModules(state).
  793. auto& loading_promise = record->load_requested_modules(state);
  794. // 6. Upon fulfillment of loadingPromise, run the following steps:
  795. WebIDL::upon_fulfillment(loading_promise, GC::create_function(realm.heap(), [&realm, record, &module_script, on_complete](JS::Value) -> WebIDL::ExceptionOr<JS::Value> {
  796. // 1. Perform record.Link().
  797. auto linking_result = record->link(realm.vm());
  798. // If this throws an exception, set result's error to rethrow to that exception.
  799. if (linking_result.is_throw_completion())
  800. module_script.set_error_to_rethrow(linking_result.release_error().value().value());
  801. // 2. Run onComplete given moduleScript.
  802. on_complete->function()(module_script);
  803. return JS::js_undefined();
  804. }));
  805. // 7. Upon rejection of loadingPromise, run the following steps:
  806. WebIDL::upon_rejection(loading_promise, GC::create_function(realm.heap(), [state, &module_script, on_complete](JS::Value) -> WebIDL::ExceptionOr<JS::Value> {
  807. // 1. If state.[[ParseError]] is not null, set moduleScript's error to rethrow to state.[[ParseError]] and run
  808. // onComplete given moduleScript.
  809. if (!state->parse_error.is_null()) {
  810. module_script.set_error_to_rethrow(state->parse_error);
  811. on_complete->function()(module_script);
  812. }
  813. // 2. Otherwise, run onComplete given null.
  814. else {
  815. on_complete->function()(nullptr);
  816. }
  817. return JS::js_undefined();
  818. }));
  819. clean_up_after_running_callback(realm);
  820. realm.vm().pop_execution_context();
  821. }
  822. }