Fetching.cpp 53 KB

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