Fetching.cpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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() : 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("/"sv) && 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("/"sv));
  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. 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. auto& event_loop = settings_object.responsible_event_loop();
  419. event_loop.spin_until(GC::create_function(vm.heap(), [&]() -> bool {
  420. return response;
  421. }));
  422. // 6. Set response to response's unsafe response.
  423. response = response->unsafe_response();
  424. // 7. If any of the following are true:
  425. // - bodyBytes is null or failure;
  426. // - response's status is not an ok status; or
  427. // - the result of extracting a MIME type from response's header list is not a JavaScript MIME type,
  428. // then throw a "NetworkError" DOMException.
  429. if (body_bytes.template has<Empty>() || body_bytes.template has<Fetch::Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag>()
  430. || !Fetch::Infrastructure::is_ok_status(response->status())
  431. || !response->header_list()->extract_mime_type().has_value() || !response->header_list()->extract_mime_type()->is_javascript()) {
  432. return WebIDL::NetworkError::create(realm, "Network error"_string);
  433. }
  434. // 8. Let sourceText be the result of UTF-8 decoding bodyBytes.
  435. auto decoder = TextCodec::decoder_for("UTF-8"sv);
  436. VERIFY(decoder.has_value());
  437. 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();
  438. // 9. Let mutedErrors be true if response was CORS-cross-origin, and false otherwise.
  439. auto muted_errors = response->is_cors_cross_origin() ? ClassicScript::MutedErrors::Yes : ClassicScript::MutedErrors::No;
  440. // 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.
  441. auto response_url = response->url().value_or({});
  442. auto script = ClassicScript::create(response_url.to_byte_string(), source_text, settings_object.realm(), response_url, 1, muted_errors);
  443. // 11. Return script.
  444. return script;
  445. }
  446. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-module-worker-script-tree
  447. 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)
  448. {
  449. return fetch_worklet_module_worker_script_graph(url, fetch_client, destination, settings_object, move(perform_fetch), move(on_complete));
  450. }
  451. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-worklet/module-worker-script-graph
  452. // https://whatpr.org/html/9893/webappapis.html#fetch-a-worklet/module-worker-script-graph
  453. 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)
  454. {
  455. auto& realm = settings_object.realm();
  456. auto& vm = realm.vm();
  457. // 1. Let options be a script fetch options whose cryptographic nonce is the empty string,
  458. // integrity metadata is the empty string, parser metadata is "not-parser-inserted",
  459. // credentials mode is credentialsMode, referrer policy is the empty string, and fetch priority is "auto".
  460. // FIXME: credentialsMode
  461. auto options = ScriptFetchOptions {
  462. .cryptographic_nonce = String {},
  463. .integrity_metadata = String {},
  464. .parser_metadata = Fetch::Infrastructure::Request::ParserMetadata::NotParserInserted,
  465. .credentials_mode = Fetch::Infrastructure::Request::CredentialsMode::SameOrigin,
  466. .referrer_policy = ReferrerPolicy::ReferrerPolicy::EmptyString,
  467. .fetch_priority = Fetch::Infrastructure::Request::Priority::Auto
  468. };
  469. // onSingleFetchComplete given result is the following algorithm:
  470. 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 {
  471. // 1. If result is null, run onComplete with null, and abort these steps.
  472. if (!result) {
  473. dbgln("on single fetch complete with nool");
  474. on_complete->function()(nullptr);
  475. return;
  476. }
  477. // 2. Fetch the descendants of and link result given fetchClient, destination, and onComplete. If performFetch was given, pass it along as well.
  478. fetch_descendants_of_and_link_a_module_script(realm, verify_cast<JavaScriptModuleScript>(*result), fetch_client, destination, move(perform_fetch), on_complete);
  479. });
  480. // 2. Fetch a single module script given url, fetchClient, destination, options, settingsObject's realm, "client", true,
  481. // and onSingleFetchComplete as defined below. If performFetch was given, pass it along as well.
  482. 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);
  483. return {};
  484. }
  485. // https://html.spec.whatwg.org/multipage/webappapis.html#internal-module-script-graph-fetching-procedure
  486. 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)
  487. {
  488. // 1. Let url be the result of resolving a module specifier given referringScript and moduleRequest.[[Specifier]].
  489. auto url = MUST(resolve_module_specifier(referring_script, module_request.module_specifier));
  490. // 2. Assert: the previous step never throws an exception, because resolving a module specifier must have been previously successful with these same two arguments.
  491. // NOTE: Handled by MUST above.
  492. // 3. Let moduleType be the result of running the module type from module request steps given moduleRequest.
  493. auto module_type = module_type_from_module_request(module_request);
  494. // 4. Assert: visited set contains (url, moduleType).
  495. VERIFY(visited_set.contains({ url, module_type }));
  496. // onSingleFetchComplete given result is the following algorithm:
  497. 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 {
  498. // 1. If result is null, run onComplete with null, and abort these steps.
  499. if (!result) {
  500. on_complete->function()(nullptr);
  501. return;
  502. }
  503. // 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.
  504. auto& module_script = verify_cast<JavaScriptModuleScript>(*result);
  505. fetch_descendants_of_a_module_script(realm, module_script, fetch_client_settings_object, destination, visited_set, perform_fetch, on_complete);
  506. });
  507. // 5. Fetch a single module script given url, fetch client settings object, destination, options, referringScript's settings object's realm,
  508. // referringScript's base URL, moduleRequest, false, and onSingleFetchComplete as defined below. If performFetch was given, pass it along as well.
  509. 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);
  510. }
  511. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-the-descendants-of-a-module-script
  512. 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)
  513. {
  514. // 1. If module script's record is null, run onComplete with module script and return.
  515. if (!module_script.record()) {
  516. on_complete->function()(&module_script);
  517. return;
  518. }
  519. // 2. Let record be module script's record.
  520. auto const& record = module_script.record();
  521. // 3. If record is not a Cyclic Module Record, or if record.[[RequestedModules]] is empty, run onComplete with module script and return.
  522. // FIXME: Currently record is always a cyclic module.
  523. if (record->requested_modules().is_empty()) {
  524. on_complete->function()(&module_script);
  525. return;
  526. }
  527. // 4. Let moduleRequests be a new empty list.
  528. Vector<JS::ModuleRequest> module_requests;
  529. // 5. For each ModuleRequest Record requested of record.[[RequestedModules]],
  530. for (auto const& requested : record->requested_modules()) {
  531. // 1. Let url be the result of resolving a module specifier given module script and requested.[[Specifier]].
  532. auto url = MUST(resolve_module_specifier(module_script, requested.module_specifier));
  533. // 2. Assert: the previous step never throws an exception, because resolving a module specifier must have been previously successful with these same two arguments.
  534. // NOTE: Handled by MUST above.
  535. // 3. Let moduleType be the result of running the module type from module request steps given requested.
  536. auto module_type = module_type_from_module_request(requested);
  537. // 4. If visited set does not contain (url, moduleType), then:
  538. if (!visited_set.contains({ url, module_type })) {
  539. // 1. Append requested to moduleRequests.
  540. module_requests.append(requested);
  541. // 2. Append (url, moduleType) to visited set.
  542. visited_set.set({ url, module_type });
  543. }
  544. }
  545. // FIXME: 6. Let options be the descendant script fetch options for module script's fetch options.
  546. ScriptFetchOptions options;
  547. // FIXME: 7. Assert: options is not null, as module script is a JavaScript module script.
  548. // 8. Let pendingCount be the length of moduleRequests.
  549. auto pending_count = module_requests.size();
  550. // 9. If pendingCount is zero, run onComplete with module script.
  551. if (pending_count == 0) {
  552. on_complete->function()(&module_script);
  553. return;
  554. }
  555. // 10. Let failed be false.
  556. bool failed = false;
  557. // 11. For each moduleRequest in moduleRequests, perform the internal module script graph fetching procedure given moduleRequest,
  558. // fetch client settings object, destination, options, module script, visited set, and onInternalFetchingComplete as defined below.
  559. // If performFetch was given, pass it along as well.
  560. for (auto const& module_request : module_requests) {
  561. // onInternalFetchingComplete given result is the following algorithm:
  562. auto on_internal_fetching_complete = create_on_fetch_script_complete(realm.heap(), [failed, pending_count, &module_script, on_complete](auto result) mutable {
  563. // 1. If failed is true, then abort these steps.
  564. if (failed)
  565. return;
  566. // 2. If result is null, then set failed to true, run onComplete with null, and abort these steps.
  567. if (!result) {
  568. failed = true;
  569. on_complete->function()(nullptr);
  570. return;
  571. }
  572. // 3. Assert: pendingCount is greater than zero.
  573. VERIFY(pending_count > 0);
  574. // 4. Decrement pendingCount by one.
  575. --pending_count;
  576. // 5. If pendingCount is zero, run onComplete with module script.
  577. if (pending_count == 0)
  578. on_complete->function()(&module_script);
  579. });
  580. fetch_internal_module_script_graph(realm, module_request, fetch_client_settings_object, destination, options, module_script, visited_set, perform_fetch, on_internal_fetching_complete);
  581. }
  582. }
  583. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-destination-from-module-type
  584. Fetch::Infrastructure::Request::Destination fetch_destination_from_module_type(Fetch::Infrastructure::Request::Destination default_destination, ByteString const& module_type)
  585. {
  586. // 1. If moduleType is "json", then return "json".
  587. if (module_type == "json"sv)
  588. return Fetch::Infrastructure::Request::Destination::JSON;
  589. // 2. If moduleType is "css", then return "style".
  590. if (module_type == "css"sv)
  591. return Fetch::Infrastructure::Request::Destination::Style;
  592. // 3. Return defaultDestination.
  593. return default_destination;
  594. }
  595. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-single-module-script
  596. // https://whatpr.org/html/9893/webappapis.html#fetch-a-single-module-script
  597. void fetch_single_module_script(JS::Realm& realm,
  598. URL::URL const& url,
  599. EnvironmentSettingsObject& fetch_client,
  600. Fetch::Infrastructure::Request::Destination destination,
  601. ScriptFetchOptions const& options,
  602. JS::Realm& module_map_realm,
  603. Web::Fetch::Infrastructure::Request::ReferrerType const& referrer,
  604. Optional<JS::ModuleRequest> const& module_request,
  605. TopLevelModule is_top_level,
  606. PerformTheFetchHook perform_fetch,
  607. OnFetchScriptComplete on_complete)
  608. {
  609. // 1. Let moduleType be "javascript".
  610. ByteString module_type = "javascript"sv;
  611. // 2. If moduleRequest was given, then set moduleType to the result of running the module type from module request steps given moduleRequest.
  612. if (module_request.has_value())
  613. module_type = module_type_from_module_request(*module_request);
  614. // 3. Assert: the result of running the module type allowed steps given moduleType and moduleMapRealm is true.
  615. // Otherwise we would not have reached this point because a failure would have been raised when inspecting moduleRequest.[[Assertions]]
  616. // in create a JavaScript module script or fetch a single imported module script.
  617. VERIFY(module_type_allowed(module_map_realm, module_type));
  618. // 4. Let moduleMap be moduleMapRealm's module map.
  619. auto& module_map = module_map_of_realm(module_map_realm);
  620. // 5. If moduleMap[(url, moduleType)] is "fetching", wait in parallel until that entry's value changes,
  621. // then queue a task on the networking task source to proceed with running the following steps.
  622. if (module_map.is_fetching(url, module_type)) {
  623. module_map.wait_for_change(realm.heap(), url, module_type, [on_complete, &realm](auto entry) -> void {
  624. HTML::queue_global_task(HTML::Task::Source::Networking, realm.global_object(), GC::create_function(realm.heap(), [on_complete, entry] {
  625. // FIXME: This should run other steps, for now we just assume the script loaded.
  626. VERIFY(entry.type == ModuleMap::EntryType::ModuleScript || entry.type == ModuleMap::EntryType::Failed);
  627. on_complete->function()(entry.module_script);
  628. }));
  629. });
  630. return;
  631. }
  632. // 6. If moduleMap[(url, moduleType)] exists, run onComplete given moduleMap[(url, moduleType)], and return.
  633. auto entry = module_map.get(url, module_type);
  634. if (entry.has_value() && entry->type == ModuleMap::EntryType::ModuleScript) {
  635. on_complete->function()(entry->module_script);
  636. return;
  637. }
  638. // 7. Set moduleMap[(url, moduleType)] to "fetching".
  639. module_map.set(url, module_type, { ModuleMap::EntryType::Fetching, nullptr });
  640. // 8. Let request be a new request whose URL is url, mode is "cors", referrer is referrer, and client is fetchClient.
  641. auto request = Fetch::Infrastructure::Request::create(realm.vm());
  642. request->set_url(url);
  643. request->set_mode(Fetch::Infrastructure::Request::Mode::CORS);
  644. request->set_referrer(referrer);
  645. request->set_client(&fetch_client);
  646. // 9. Set request's destination to the result of running the fetch destination from module type steps given destination and moduleType.
  647. request->set_destination(fetch_destination_from_module_type(destination, module_type));
  648. // 10. If destination is "worker", "sharedworker", or "serviceworker", and isTopLevel is true, then set request's mode to "same-origin".
  649. if ((destination == Fetch::Infrastructure::Request::Destination::Worker || destination == Fetch::Infrastructure::Request::Destination::SharedWorker || destination == Fetch::Infrastructure::Request::Destination::ServiceWorker) && is_top_level == TopLevelModule::Yes)
  650. request->set_mode(Fetch::Infrastructure::Request::Mode::SameOrigin);
  651. // 11. Set request's initiator type to "script".
  652. request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::Script);
  653. // 12. Set up the module script request given request and options.
  654. set_up_module_script_request(request, options);
  655. // 13. If performFetch was given, run performFetch with request, isTopLevel, and with processResponseConsumeBody as defined below.
  656. // Otherwise, fetch request with processResponseConsumeBody set to processResponseConsumeBody as defined below.
  657. // In both cases, let processResponseConsumeBody given response response and null, failure, or a byte sequence bodyBytes be the following algorithm:
  658. 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) {
  659. // 1. If either of the following conditions are met:
  660. // - bodyBytes is null or failure; or
  661. // - response's status is not an ok status,
  662. if (body_bytes.has<Empty>() || body_bytes.has<Fetch::Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag>() || !Fetch::Infrastructure::is_ok_status(response->status())) {
  663. // then set moduleMap[(url, moduleType)] to null, run onComplete given null, and abort these steps.
  664. module_map.set(url, module_type, { ModuleMap::EntryType::Failed, nullptr });
  665. on_complete->function()(nullptr);
  666. return;
  667. }
  668. // 2. Let sourceText be the result of UTF-8 decoding bodyBytes.
  669. auto decoder = TextCodec::decoder_for("UTF-8"sv);
  670. VERIFY(decoder.has_value());
  671. 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();
  672. // 3. Let mimeType be the result of extracting a MIME type from response's header list.
  673. auto mime_type = response->header_list()->extract_mime_type();
  674. // 4. Let moduleScript be null.
  675. GC::Ptr<JavaScriptModuleScript> module_script;
  676. // FIXME: 5. Let referrerPolicy be the result of parsing the `Referrer-Policy` header given response. [REFERRERPOLICY]
  677. // FIXME: 6. If referrerPolicy is not the empty string, set options's referrer policy to referrerPolicy.
  678. // 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.
  679. // FIXME: Pass options.
  680. if (mime_type->is_javascript() && module_type == "javascript")
  681. module_script = JavaScriptModuleScript::create(url.basename(), source_text, module_map_realm, response->url().value_or({})).release_value_but_fixme_should_propagate_errors();
  682. // 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.
  683. // 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.
  684. // 10. Set moduleMap[(url, moduleType)] to moduleScript, and run onComplete given moduleScript.
  685. module_map.set(url, module_type, { ModuleMap::EntryType::ModuleScript, module_script });
  686. on_complete->function()(module_script);
  687. };
  688. if (perform_fetch != nullptr) {
  689. perform_fetch->function()(request, is_top_level, move(process_response_consume_body)).release_value_but_fixme_should_propagate_errors();
  690. } else {
  691. Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {};
  692. fetch_algorithms_input.process_response_consume_body = move(process_response_consume_body);
  693. Fetch::Fetching::fetch(realm, request, Fetch::Infrastructure::FetchAlgorithms::create(realm.vm(), move(fetch_algorithms_input))).release_value_but_fixme_should_propagate_errors();
  694. }
  695. }
  696. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-module-script-tree
  697. // https://whatpr.org/html/9893/webappapis.html#fetch-a-module-script-tree
  698. void fetch_external_module_script_graph(JS::Realm& realm, URL::URL const& url, EnvironmentSettingsObject& settings_object, ScriptFetchOptions const& options, OnFetchScriptComplete on_complete)
  699. {
  700. // 1. Disallow further import maps given settingsObject's realm.
  701. disallow_further_import_maps(settings_object.realm());
  702. auto steps = create_on_fetch_script_complete(realm.heap(), [&realm, &settings_object, on_complete, url](auto result) mutable {
  703. // 1. If result is null, run onComplete given null, and abort these steps.
  704. if (!result) {
  705. on_complete->function()(nullptr);
  706. return;
  707. }
  708. // 2. Fetch the descendants of and link result given settingsObject, "script", and onComplete.
  709. auto& module_script = verify_cast<JavaScriptModuleScript>(*result);
  710. fetch_descendants_of_and_link_a_module_script(realm, module_script, settings_object, Fetch::Infrastructure::Request::Destination::Script, nullptr, on_complete);
  711. });
  712. // 2. Fetch a single module script given url, settingsObject, "script", options, settingsObject's realm, "client", true, and with the following steps given result:
  713. 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);
  714. }
  715. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-an-inline-module-script-graph
  716. // https://whatpr.org/html/9893/webappapis.html#fetch-an-inline-module-script-graph
  717. 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)
  718. {
  719. // 1. Disallow further import maps given settingsObject's realm.
  720. disallow_further_import_maps(settings_object.realm());
  721. // 2. Let script be the result of creating a JavaScript module script using sourceText, settingsObject's realm, baseURL, and options.
  722. auto script = JavaScriptModuleScript::create(filename, source_text.view(), settings_object.realm(), base_url).release_value_but_fixme_should_propagate_errors();
  723. // 3. If script is null, run onComplete given null, and return.
  724. if (!script) {
  725. on_complete->function()(nullptr);
  726. return;
  727. }
  728. // 5. Fetch the descendants of and link script, given settingsObject, "script", and onComplete.
  729. fetch_descendants_of_and_link_a_module_script(realm, *script, settings_object, Fetch::Infrastructure::Request::Destination::Script, nullptr, on_complete);
  730. }
  731. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-single-imported-module-script
  732. void fetch_single_imported_module_script(JS::Realm& realm,
  733. URL::URL const& url,
  734. EnvironmentSettingsObject& fetch_client,
  735. Fetch::Infrastructure::Request::Destination destination,
  736. ScriptFetchOptions const& options,
  737. JS::Realm& module_map_realm,
  738. Fetch::Infrastructure::Request::ReferrerType referrer,
  739. JS::ModuleRequest const& module_request,
  740. PerformTheFetchHook perform_fetch,
  741. OnFetchScriptComplete on_complete)
  742. {
  743. // 1. Assert: moduleRequest.[[Attributes]] does not contain any Record entry such that entry.[[Key]] is not "type",
  744. // because we only asked for "type" attributes in HostGetSupportedImportAttributes.
  745. for (auto const& entry : module_request.attributes)
  746. VERIFY(entry.key == "type"sv);
  747. // 2. Let moduleType be the result of running the module type from module request steps given moduleRequest.
  748. auto module_type = module_type_from_module_request(module_request);
  749. // 3. If the result of running the module type allowed steps given moduleType and moduleMapRealm is false,
  750. // then run onComplete given null, and return.
  751. if (!module_type_allowed(module_map_realm, module_type)) {
  752. on_complete->function()(nullptr);
  753. return;
  754. }
  755. // 4. Fetch a single module script given url, fetchClient, destination, options, moduleMapRealm, referrer, moduleRequest, false,
  756. // and onComplete. If performFetch was given, pass it along as well.
  757. fetch_single_module_script(realm, url, fetch_client, destination, options, module_map_realm, referrer, module_request, TopLevelModule::No, perform_fetch, on_complete);
  758. }
  759. // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-the-descendants-of-and-link-a-module-script
  760. void fetch_descendants_of_and_link_a_module_script(JS::Realm& realm,
  761. JavaScriptModuleScript& module_script,
  762. EnvironmentSettingsObject& fetch_client,
  763. Fetch::Infrastructure::Request::Destination destination,
  764. PerformTheFetchHook perform_fetch,
  765. OnFetchScriptComplete on_complete)
  766. {
  767. // 1. Let record be moduleScript's record.
  768. auto* record = module_script.record();
  769. // 2. If record is null, then:
  770. if (!record) {
  771. // 1. Set moduleScript's error to rethrow to moduleScript's parse error.
  772. module_script.set_error_to_rethrow(module_script.parse_error());
  773. // 2. Run onComplete given moduleScript.
  774. on_complete->function()(module_script);
  775. // 3. Return.
  776. return;
  777. }
  778. // 3. Let state be Record { [[ParseError]]: null, [[Destination]]: destination, [[PerformFetch]]: null, [[FetchClient]]: fetchClient }.
  779. auto state = realm.heap().allocate<FetchContext>(JS::js_null(), destination, nullptr, fetch_client);
  780. // 4. If performFetch was given, set state.[[PerformFetch]] to performFetch.
  781. state->perform_fetch = perform_fetch;
  782. // FIXME: These should most likely be steps in the spec.
  783. // NOTE: For reasons beyond my understanding, we cannot use TemporaryExecutionContext here.
  784. // Calling perform_a_microtask_checkpoint() on the fetch_client's responsible_event_loop
  785. // prevents this from functioning properly. HTMLParser::the_end would be run before
  786. // HTMLScriptElement::prepare_script had a chance to setup the callback to mark_done properly,
  787. // resulting in the event loop hanging forever awaiting for the script to be ready for parser
  788. // execution.
  789. realm.vm().push_execution_context(fetch_client.realm_execution_context());
  790. prepare_to_run_callback(realm);
  791. // 5. Let loadingPromise be record.LoadRequestedModules(state).
  792. auto& loading_promise = record->load_requested_modules(state);
  793. // 6. Upon fulfillment of loadingPromise, run the following steps:
  794. WebIDL::upon_fulfillment(loading_promise, GC::create_function(realm.heap(), [&realm, record, &module_script, on_complete](JS::Value) -> WebIDL::ExceptionOr<JS::Value> {
  795. // 1. Perform record.Link().
  796. auto linking_result = record->link(realm.vm());
  797. // If this throws an exception, set result's error to rethrow to that exception.
  798. if (linking_result.is_throw_completion())
  799. module_script.set_error_to_rethrow(linking_result.release_error().value().value());
  800. // 2. Run onComplete given moduleScript.
  801. on_complete->function()(module_script);
  802. return JS::js_undefined();
  803. }));
  804. // 7. Upon rejection of loadingPromise, run the following steps:
  805. WebIDL::upon_rejection(loading_promise, GC::create_function(realm.heap(), [state, &module_script, on_complete](JS::Value) -> WebIDL::ExceptionOr<JS::Value> {
  806. // 1. If state.[[ParseError]] is not null, set moduleScript's error to rethrow to state.[[ParseError]] and run
  807. // onComplete given moduleScript.
  808. if (!state->parse_error.is_null()) {
  809. module_script.set_error_to_rethrow(state->parse_error);
  810. on_complete->function()(module_script);
  811. }
  812. // 2. Otherwise, run onComplete given null.
  813. else {
  814. on_complete->function()(nullptr);
  815. }
  816. return JS::js_undefined();
  817. }));
  818. clean_up_after_running_callback(realm);
  819. realm.vm().pop_execution_context();
  820. }
  821. }