diff --git a/change/react-native-windows-2020-05-21-16-49-04-dsmwinrt.json b/change/react-native-windows-2020-05-21-16-49-04-dsmwinrt.json new file mode 100644 index 00000000000..f1dc7766305 --- /dev/null +++ b/change/react-native-windows-2020-05-21-16-49-04-dsmwinrt.json @@ -0,0 +1,8 @@ +{ + "type": "prerelease", + "comment": "Drop LazySupportManager", + "packageName": "react-native-windows", + "email": "julio.rocha@microsoft.com", + "dependentChangeType": "patch", + "date": "2020-05-21T23:49:04.082Z" +} diff --git a/vnext/Desktop/DevSupportManager.cpp b/vnext/Desktop/DevSupportManager.cpp index 2877c043fa2..a2090b7fcc5 100644 --- a/vnext/Desktop/DevSupportManager.cpp +++ b/vnext/Desktop/DevSupportManager.cpp @@ -7,28 +7,32 @@ #include #include - -#include -#include -#include -#include -#include +#include + +#include +#include +#include +#include +#include +#include #include "Executors/WebSocketJSExecutor.h" #include "Utils.h" -using namespace boost::asio; -using namespace boost::asio::ip; -using namespace boost::beast; -using namespace boost::beast::http; using namespace Concurrency; -using namespace std; +using namespace winrt::Windows::Storage::Streams; +using namespace winrt::Windows::Web::Http; -using Microsoft::React::WebSocketJSExecutor; +using std::make_unique; +using std::ostringstream; +using std::shared_ptr; +using std::string; +using std::vector; +using winrt::Windows::Foundation::Uri; +using winrt::Windows::Networking::HostName; -namespace facebook { -namespace react { +namespace Microsoft::React { -DevSupportManager::DevSupportManager() : m_resolver{m_context} {} +DevSupportManager::DevSupportManager() {} DevSupportManager::~DevSupportManager() { StopPollingLiveReload(); @@ -55,36 +59,32 @@ string DevSupportManager::GetJavaScriptFromServer( const string &platform) { auto bundleUrl = DevServerHelper::get_BundleUrl(debugHost, jsBundleName, platform /*platform*/, "true" /*dev*/, "false" /*hot*/); - - Url url(bundleUrl); - auto const resolveResult = m_resolver.resolve(url.host, url.port); - tcp::socket socket{m_context}; - boost::system::error_code ec; - connect(socket, resolveResult, ec); - if (ec) { + Uri uri(Common::Unicode::Utf8ToUtf16(bundleUrl)); + Filters::HttpBaseProtocolFilter filter; + filter.CacheControl().ReadBehavior(Filters::HttpCacheReadBehavior::NoCache); + HttpClient client(filter); + client.DefaultRequestHeaders().Host(HostName(uri.Host().c_str())); + + HttpRequestMessage request(HttpMethod::Get(), uri); + HttpResponseMessage response = client.SendRequestAsync(request).get(); + + IBuffer buffer = response.Content().ReadAsBufferAsync().get(); + auto reader = DataReader::FromBuffer(buffer); + reader.UnicodeEncoding(UnicodeEncoding::Utf8); + auto length = reader.UnconsumedBufferLength(); + string result; + if (length > 0 || response.IsSuccessStatusCode()) { + vector data(length); + reader.ReadBytes(data); + result = string(reinterpret_cast(data.data()), data.size()); + } else { m_exceptionCaught = true; - return R"({"error:")"s + ec.message() + R"("})"s; + ostringstream stream; + stream << R"({"error":")" << static_cast(response.StatusCode()) << " downloading " << bundleUrl << R"("})"; + result = stream.str(); } - request request{verb::get, url.Target(), 11}; - request.set(field::host, url.host); - request.set(field::user_agent, BOOST_BEAST_VERSION_STRING); - - write(socket, request); - - flat_buffer buffer; - response response; - - parser p{std::move(response)}; - p.body_limit(25 * 1024 * 1024); // 25MB (boost default of 1MB is too small for dev bundles) - - read(socket, buffer, p.base()); - response = p.release(); - std::stringstream jsStringStream; - jsStringStream << response.body(); - // TODO: Check if UTF-8 processing is required. - - return jsStringStream.str(); + return result; } void DevSupportManager::StartPollingLiveReload(const string &debugHost, std::function onChangeCallback) { @@ -92,23 +92,16 @@ void DevSupportManager::StartPollingLiveReload(const string &debugHost, std::fun cancellation_token token = m_liveReloadCts.get_token(); while (!token.is_canceled()) { try { - Url url(DevServerHelper::get_OnChangeEndpointUrl(debugHost)); - auto const resolveResult = m_resolver.resolve(url.host, url.port); - tcp::socket socket{m_context}; - connect(socket, resolveResult); - - request request{verb::get, url.Target(), 11 /*HTTP 1.1*/}; - request.set(field::host, url.host); - request.set(field::user_agent, BOOST_BEAST_VERSION_STRING); - request.set(field::connection, "keep-alive"); - - write(socket, request); - flat_buffer buffer; - response response; - read(socket, buffer, response); - - if (response.result_int() == 205 /*ResetContent*/ && !token.is_canceled()) + Uri uri(Common::Unicode::Utf8ToUtf16(DevServerHelper::get_OnChangeEndpointUrl(debugHost))); + HttpClient client; + client.DefaultRequestHeaders().Host(HostName(uri.Host().c_str())); + client.DefaultRequestHeaders().Connection().TryParseAdd(L"keep-alive"); + + HttpResponseMessage response = client.GetAsync(uri, HttpCompletionOption::ResponseHeadersRead).get(); + + if (response.StatusCode() == HttpStatusCode::ResetContent /*ResetContent*/ && !token.is_canceled()) onChangeCallback(); + } catch (const std::exception & /*e*/) { // Just let the live reload stop working when the connection fails, // rather than bringing down the app. @@ -121,28 +114,19 @@ task DevSupportManager::LaunchDevToolsAsync(const string &debugHost, const auto t = task_from_result(); return t.then([=]() { - Url url(DevServerHelper::get_LaunchDevToolsCommandUrl(debugHost)); - auto const resolveResult = m_resolver.resolve(url.host, url.port); - tcp::socket socket{m_context}; - boost::system::error_code ec; - connect(socket, resolveResult, ec); - if (ec) { - m_exceptionCaught = true; - return; - } - - request request{verb::get, url.Target(), 11}; - request.set(field::host, url.host); - request.set(field::user_agent, BOOST_BEAST_VERSION_STRING); + Uri uri(Common::Unicode::Utf8ToUtf16(DevServerHelper::get_LaunchDevToolsCommandUrl(debugHost))); + HttpClient client; + client.DefaultRequestHeaders().Host(HostName(uri.Host().c_str())); + HttpResponseMessage response = client.GetAsync(uri, HttpCompletionOption::ResponseHeadersRead).get(); + }); +} - write(socket, request); +} // namespace Microsoft::React - flat_buffer buffer; - response response; +namespace facebook::react { - read(socket, buffer, response); - }); +shared_ptr CreateDevSupportManager() { + return std::make_shared(); } -} // namespace react -} // namespace facebook +} // namespace facebook::react diff --git a/vnext/Desktop/DevSupportManager.h b/vnext/Desktop/DevSupportManager.h index 28745fbb271..e990ccc1cc3 100644 --- a/vnext/Desktop/DevSupportManager.h +++ b/vnext/Desktop/DevSupportManager.h @@ -7,26 +7,20 @@ #undef U #include - -#include -#include -#include #include -namespace facebook { -namespace react { - -using JSECreator = - std::function(std::shared_ptr, std::shared_ptr)>; +namespace Microsoft::React { -struct DevSettings; +using JSECreator = std::function( + std::shared_ptr, + std::shared_ptr)>; -class DevSupportManager : public IDevSupportManager { +class DevSupportManager : public facebook::react::IDevSupportManager { public: DevSupportManager(); ~DevSupportManager(); - virtual JSECreator LoadJavaScriptInProxyMode(const DevSettings &settings) override; + virtual facebook::react::JSECreator LoadJavaScriptInProxyMode(const facebook::react::DevSettings &settings) override; virtual std::string GetJavaScriptFromServer( const std::string &debugHost, const std::string &jsBundleName, @@ -43,10 +37,7 @@ class DevSupportManager : public IDevSupportManager { const Concurrency::cancellation_token &token = Concurrency::cancellation_token::none()); bool m_exceptionCaught = false; - boost::asio::io_context m_context; - boost::asio::ip::tcp::resolver m_resolver; Concurrency::cancellation_token_source m_liveReloadCts; }; -} // namespace react -} // namespace facebook +} // namespace Microsoft::React diff --git a/vnext/Desktop/LazyDevSupportManager.cpp b/vnext/Desktop/LazyDevSupportManager.cpp deleted file mode 100644 index 812099aa636..00000000000 --- a/vnext/Desktop/LazyDevSupportManager.cpp +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -#include "pch.h" - -#include "LazyDevSupportManager.h" - -using std::string; - -namespace facebook { -namespace react { - -void LazyDevSupportManager::EnsureDevSupportManager() noexcept { - if (!m_dsm) - m_dsm = std::make_unique(); -} - -#pragma region LazyDevSupportManager - -JSECreator LazyDevSupportManager::LoadJavaScriptInProxyMode(const DevSettings &settings) /*override*/ -{ - EnsureDevSupportManager(); - - return m_dsm->LoadJavaScriptInProxyMode(settings); -} - -string LazyDevSupportManager::GetJavaScriptFromServer( - const string &debugHost, - const string &jsBundleName, - const string &platform) /*override*/ -{ - EnsureDevSupportManager(); - - return m_dsm->GetJavaScriptFromServer(debugHost, jsBundleName, platform); -} - -void LazyDevSupportManager::StartPollingLiveReload( - const string &debugHost, - std::function onChangeCallback) /*override*/ -{ - EnsureDevSupportManager(); - - m_dsm->StartPollingLiveReload(debugHost, onChangeCallback); -} - -void LazyDevSupportManager::StopPollingLiveReload() /*override*/ -{ - EnsureDevSupportManager(); - - m_dsm->StopPollingLiveReload(); -} - -bool LazyDevSupportManager::HasException() /*override*/ -{ - EnsureDevSupportManager(); - - return m_dsm->HasException(); -} - -#pragma endregion LazyDevSupportManager - -std::shared_ptr CreateDevSupportManager() { - return std::make_shared(); -} - -} // namespace react -} // namespace facebook diff --git a/vnext/Desktop/LazyDevSupportManager.h b/vnext/Desktop/LazyDevSupportManager.h deleted file mode 100644 index 1c34cdbfe68..00000000000 --- a/vnext/Desktop/LazyDevSupportManager.h +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -#pragma once - -#include -#include "DevSupportManager.h" - -namespace facebook { -namespace react { - -/// -// Proxy IDevSupportManager implementation. -// Wraps actual DevSupportManager instantiation until public methods are used. -/// -class LazyDevSupportManager : public IDevSupportManager { - std::unique_ptr m_dsm; - - void EnsureDevSupportManager() noexcept; - - public: -#pragma region IDevSupportManager - - JSECreator LoadJavaScriptInProxyMode(const DevSettings &settings) override; - std::string GetJavaScriptFromServer( - const std::string &debugHost, - const std::string &jsBundleName, - const std::string &platform) override; - void StartPollingLiveReload(const std::string &debugHost, std::function onChangeCallback) override; - void StopPollingLiveReload() override; - bool HasException() override; - -#pragma endregion IDevSupportManager -}; - -} // namespace react -} // namespace facebook diff --git a/vnext/Desktop/React.Windows.Desktop.vcxproj b/vnext/Desktop/React.Windows.Desktop.vcxproj index bf2397c594d..9f001c9422a 100644 --- a/vnext/Desktop/React.Windows.Desktop.vcxproj +++ b/vnext/Desktop/React.Windows.Desktop.vcxproj @@ -24,7 +24,6 @@ true true -lib $(MSBuildProjectName) - Microsoft.ReactNative - true {95048601-C3DC-475F-ADF8-7C0C764C10D5} React.Windows.Desktop @@ -161,7 +159,6 @@ - @@ -192,7 +189,6 @@ - @@ -235,7 +231,6 @@ - @@ -243,15 +238,13 @@ $(CppWinRTRootNamespace) - $(_CppWinRTOriginalRootNamespace) - <_CppWinRTOriginalRootNamespace/> + <_CppWinRTOriginalRootNamespace /> - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. diff --git a/vnext/Desktop/React.Windows.Desktop.vcxproj.filters b/vnext/Desktop/React.Windows.Desktop.vcxproj.filters index 1aa9154c621..36e18ade219 100644 --- a/vnext/Desktop/React.Windows.Desktop.vcxproj.filters +++ b/vnext/Desktop/React.Windows.Desktop.vcxproj.filters @@ -81,6 +81,7 @@ ABI + @@ -116,9 +117,6 @@ Source Files - - Source Files - Generated Files @@ -135,6 +133,13 @@ Source Files + + + + + + + @@ -166,9 +171,6 @@ Header Files - - Header Files - Header Files\Modules