react-native-windows 0.67.6 → 0.67.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,198 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+
4
+ #include "pch.h"
5
+
6
+ #include "HttpModule.h"
7
+
8
+ // React Native
9
+ #include <cxxreact/Instance.h>
10
+ #include <cxxreact/JsArgumentHelpers.h>
11
+
12
+ using facebook::react::Instance;
13
+ using folly::dynamic;
14
+ using std::shared_ptr;
15
+ using std::string;
16
+ using std::weak_ptr;
17
+
18
+ namespace {
19
+
20
+ using Microsoft::React::IHttpResource;
21
+
22
+ constexpr char moduleName[] = "Networking";
23
+
24
+ static void SendEvent(weak_ptr<Instance> weakReactInstance, string &&eventName, dynamic &&args) {
25
+ if (auto instance = weakReactInstance.lock()) {
26
+ instance->callJSFunction("RCTDeviceEventEmitter", "emit", dynamic::array(std::move(eventName), std::move(args)));
27
+ }
28
+ }
29
+
30
+ static shared_ptr<IHttpResource> CreateHttpResource(weak_ptr<Instance> weakReactInstance) {
31
+ auto resource = IHttpResource::Make();
32
+
33
+ resource->SetOnResponse([weakReactInstance](int64_t requestId, IHttpResource::Response &&response) {
34
+ dynamic headers = dynamic::object();
35
+ for (auto &header : response.Headers) {
36
+ headers[header.first] = header.second;
37
+ }
38
+
39
+ // TODO: Test response content.
40
+ dynamic args = dynamic::array(requestId, response.StatusCode, headers, response.Url);
41
+
42
+ SendEvent(weakReactInstance, "didReceiveNetworkResponse", std::move(args));
43
+ });
44
+
45
+ resource->SetOnData([weakReactInstance](int64_t requestId, std::string &&responseData) {
46
+ dynamic args = dynamic::array(requestId, std::move(responseData));
47
+
48
+ SendEvent(weakReactInstance, "didReceiveNetworkData", std::move(args));
49
+
50
+ // TODO: Move into separate method IF not executed right after onData()
51
+ SendEvent(weakReactInstance, "didCompleteNetworkResponse", dynamic::array(requestId));
52
+ });
53
+
54
+ resource->SetOnError([weakReactInstance](int64_t requestId, string &&message) {
55
+ dynamic args = dynamic::array(requestId, std::move(message));
56
+ // TODO: isTimeout errorArgs.push_back(true);
57
+
58
+ SendEvent(weakReactInstance, "didCompleteNetworkResponse", std::move(args));
59
+ });
60
+
61
+ return resource;
62
+ }
63
+
64
+ } // namespace
65
+
66
+ namespace Microsoft::React {
67
+
68
+ HttpModule::HttpModule() noexcept : m_holder{std::make_shared<ModuleHolder>()} {
69
+ m_holder->Module = this;
70
+ }
71
+
72
+ HttpModule::~HttpModule() noexcept /*override*/ {
73
+ m_holder->Module = nullptr;
74
+ }
75
+
76
+ #pragma region CxxModule
77
+
78
+ string HttpModule::getName() /*override*/ {
79
+ return moduleName;
80
+ }
81
+
82
+ std::map<string, dynamic> HttpModule::getConstants() {
83
+ return {};
84
+ }
85
+
86
+ // clang-format off
87
+ std::vector<facebook::xplat::module::CxxModule::Method> HttpModule::getMethods() {
88
+
89
+ auto weakHolder = weak_ptr<ModuleHolder>(m_holder);
90
+ auto holder = weakHolder.lock();
91
+ auto weakReactInstance = weak_ptr<Instance>(holder->Module->getInstance());
92
+
93
+ return
94
+ {
95
+ {
96
+ "sendRequest",
97
+ [weakHolder = weak_ptr<ModuleHolder>(m_holder)](dynamic args, Callback cxxCallback)
98
+ {
99
+ auto holder = weakHolder.lock();
100
+ if (!holder) {
101
+ return;
102
+ }
103
+
104
+ auto resource = holder->Module->m_resource;
105
+ if (resource || (resource = CreateHttpResource(holder->Module->getInstance())))
106
+ {
107
+ IHttpResource::BodyData bodyData;
108
+ auto params = facebook::xplat::jsArgAsObject(args, 0);
109
+ auto data = params["data"];
110
+ auto stringData = data["string"];
111
+ if (!stringData.empty())
112
+ {
113
+ bodyData = {IHttpResource::BodyData::Type::String, stringData.getString()};
114
+ }
115
+ else
116
+ {
117
+ auto base64Data = data["base64"];
118
+ if (!base64Data.empty())
119
+ {
120
+ bodyData = {IHttpResource::BodyData::Type::Base64, base64Data.getString()};
121
+ }
122
+ else
123
+ {
124
+ auto uriData = data["uri"];
125
+ if (!uriData.empty())
126
+ {
127
+ bodyData = {IHttpResource::BodyData::Type::Uri, uriData.getString()};
128
+ }
129
+ }
130
+ }
131
+ //TODO: Support FORM data
132
+
133
+ IHttpResource::Headers headers;
134
+ for (auto& header : params["headers"].items()) {
135
+ headers.emplace(header.first.getString(), header.second.getString());
136
+ }
137
+
138
+ resource->SendRequest(
139
+ params["method"].asString(),
140
+ params["url"].asString(),
141
+ std::move(headers),
142
+ std::move(bodyData),
143
+ params["responseType"].asString(),
144
+ params["incrementalUpdates"].asBool(),
145
+ static_cast<int64_t>(params["timeout"].asDouble()),
146
+ false,//withCredentials,
147
+ [cxxCallback = std::move(cxxCallback)](int64_t requestId) {
148
+ cxxCallback({requestId});
149
+ }
150
+ );
151
+ } // If resource available
152
+ }
153
+ },
154
+ {
155
+ "abortRequest",
156
+ [weakHolder = weak_ptr<ModuleHolder>(m_holder)](dynamic args)
157
+ {
158
+ auto holder = weakHolder.lock();
159
+ if (!holder)
160
+ {
161
+ return;
162
+ }
163
+
164
+ auto resource = holder->Module->m_resource;
165
+ if (resource || (resource = CreateHttpResource(holder->Module->getInstance())))
166
+ {
167
+ resource->AbortRequest(facebook::xplat::jsArgAsInt(args, 0));
168
+ }
169
+ }
170
+ },
171
+ {
172
+ "clearCookies",
173
+ [weakHolder = weak_ptr<ModuleHolder>(m_holder)](dynamic args)
174
+ {
175
+ auto holder = weakHolder.lock();
176
+ if (!holder)
177
+ {
178
+ return;
179
+ }
180
+
181
+ auto resource = holder->Module->m_resource;
182
+ if (resource || (resource = CreateHttpResource(holder->Module->getInstance())))
183
+ {
184
+ resource->ClearCookies();
185
+ }
186
+ }
187
+ }
188
+ };
189
+ }
190
+ // clang-format on
191
+
192
+ #pragma endregion CxxModule
193
+
194
+ /*extern*/ const char *GetHttpModuleName() noexcept {
195
+ return moduleName;
196
+ }
197
+
198
+ } // namespace Microsoft::React
@@ -0,0 +1,53 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+
4
+ #pragma once
5
+
6
+ #include "IHttpResource.h"
7
+
8
+ // React Native
9
+ #include <cxxreact/CxxModule.h>
10
+
11
+ namespace Microsoft::React {
12
+
13
+ ///
14
+ /// Realizes <c>NativeModules</c> projection.
15
+ /// <remarks>See src\Libraries\Network\RCTNetworkingWinShared.js</remarks>
16
+ ///
17
+ class HttpModule : public facebook::xplat::module::CxxModule {
18
+ public:
19
+ enum MethodId { SendRequest = 0, AbortRequest = 1, ClearCookies = 2, LAST = ClearCookies };
20
+
21
+ HttpModule() noexcept;
22
+
23
+ ~HttpModule() noexcept override;
24
+
25
+ #pragma region CxxModule
26
+
27
+ /// <summary>
28
+ /// <see cref="facebook::xplat::module::CxxModule::getName" />
29
+ /// </summary>
30
+ std::string getName() override;
31
+
32
+ /// <summary>
33
+ /// <see cref="facebook::xplat::module::CxxModule::getConstants" />
34
+ /// </summary>
35
+ std::map<std::string, folly::dynamic> getConstants() override;
36
+
37
+ /// <summary>
38
+ /// <see cref="facebook::xplat::module::CxxModule::getMethods" />
39
+ /// </summary>
40
+ /// <remarks>See See react-native/Libraries/WebSocket/WebSocket.js</remarks>
41
+ std::vector<Method> getMethods() override;
42
+
43
+ #pragma endregion CxxModule
44
+
45
+ private:
46
+ struct ModuleHolder {
47
+ HttpModule *Module{nullptr};
48
+ };
49
+
50
+ std::shared_ptr<IHttpResource> m_resource;
51
+ std::shared_ptr<ModuleHolder> m_holder;
52
+ };
53
+ } // namespace Microsoft::React
@@ -246,6 +246,10 @@ std::vector<facebook::xplat::module::CxxModule::Method> WebSocketModule::getMeth
246
246
  } // getMethods
247
247
  // clang-format on
248
248
 
249
+ /*extern*/ const char *GetWebSocketModuleName() noexcept {
250
+ return moduleName;
251
+ }
252
+
249
253
  /*extern*/ std::unique_ptr<facebook::xplat::module::CxxModule> CreateWebSocketModule() noexcept {
250
254
  return std::make_unique<WebSocketModule>();
251
255
  }
@@ -26,6 +26,8 @@
26
26
  #include <cxxreact/ModuleRegistry.h>
27
27
 
28
28
  #include <Modules/ExceptionsManagerModule.h>
29
+ #include <Modules/HttpModule.h>
30
+ #include <Modules/NetworkingModule.h>
29
31
  #include <Modules/PlatformConstantsModule.h>
30
32
  #include <Modules/SourceCodeModule.h>
31
33
  #include <Modules/StatusBarManagerModule.h>
@@ -146,6 +148,18 @@ using namespace Microsoft::JSI;
146
148
 
147
149
  using std::make_shared;
148
150
 
151
+ namespace Microsoft::React {
152
+
153
+ /*extern*/ std::unique_ptr<facebook::xplat::module::CxxModule> CreateHttpModule() noexcept {
154
+ if (GetRuntimeOptionBool("Http.UseMonolithicModule")) {
155
+ return std::make_unique<NetworkingModule>();
156
+ } else {
157
+ return std::make_unique<HttpModule>();
158
+ }
159
+ }
160
+
161
+ } // namespace Microsoft::React
162
+
149
163
  namespace facebook {
150
164
  namespace react {
151
165
 
@@ -613,7 +627,13 @@ std::vector<std::unique_ptr<NativeModule>> InstanceImpl::GetDefaultNativeModules
613
627
 
614
628
  modules.push_back(std::make_unique<CxxNativeModule>(
615
629
  m_innerInstance,
616
- "WebSocketModule",
630
+ Microsoft::React::GetHttpModuleName(),
631
+ [nativeQueue]() -> std::unique_ptr<xplat::module::CxxModule> { return Microsoft::React::CreateHttpModule(); },
632
+ nativeQueue));
633
+
634
+ modules.push_back(std::make_unique<CxxNativeModule>(
635
+ m_innerInstance,
636
+ Microsoft::React::GetWebSocketModuleName(),
617
637
  [nativeQueue]() -> std::unique_ptr<xplat::module::CxxModule> {
618
638
  return Microsoft::React::CreateWebSocketModule();
619
639
  },
@@ -46,6 +46,7 @@
46
46
  <ExcludedFromBuild Condition="'$(ApplicationType)' == ''">true</ExcludedFromBuild>
47
47
  </ClCompile>
48
48
  <ClCompile Include="$(MSBuildThisFileDirectory)Modules\ExceptionsManagerModule.cpp" />
49
+ <ClCompile Include="$(MSBuildThisFileDirectory)Modules\HttpModule.cpp" />
49
50
  <ClCompile Include="$(MSBuildThisFileDirectory)Modules\I18nModule.cpp" />
50
51
  <ClCompile Include="$(MSBuildThisFileDirectory)Modules\NetworkingModule.cpp" />
51
52
  <ClCompile Include="$(MSBuildThisFileDirectory)Modules\PlatformConstantsModule.cpp" />
@@ -61,9 +62,11 @@
61
62
  <ClCompile Include="$(MSBuildThisFileDirectory)tracing\tracing.cpp" />
62
63
  <ClCompile Include="$(MSBuildThisFileDirectory)TurboModuleManager.cpp" />
63
64
  <ClCompile Include="$(MSBuildThisFileDirectory)Utils.cpp" />
65
+ <ClCompile Include="$(MSBuildThisFileDirectory)Utils\WinRTConversions.cpp" />
64
66
  <ClCompile Include="$(MSBuildThisFileDirectory)V8JSIRuntimeHolder.cpp">
65
67
  <ExcludedFromBuild Condition="'$(UseV8)' != 'true'">true</ExcludedFromBuild>
66
68
  </ClCompile>
69
+ <ClCompile Include="$(MSBuildThisFileDirectory)WinRTHttpResource.cpp" />
67
70
  <ClCompile Include="$(MSBuildThisFileDirectory)WinRTWebSocketResource.cpp" />
68
71
  </ItemGroup>
69
72
  <ItemGroup>
@@ -83,6 +86,7 @@
83
86
  <ClInclude Include="$(MSBuildThisFileDirectory)JSI\NapiJsiV8RuntimeHolder.h" />
84
87
  <ClInclude Include="$(MSBuildThisFileDirectory)JSI\RuntimeHolder.h" />
85
88
  <ClInclude Include="$(MSBuildThisFileDirectory)JSI\ScriptStore.h" />
89
+ <ClInclude Include="$(MSBuildThisFileDirectory)Modules\HttpModule.h" />
86
90
  <ClInclude Include="$(MSBuildThisFileDirectory)Modules\NetworkingModule.h" />
87
91
  <ClInclude Include="$(MSBuildThisFileDirectory)RuntimeOptions.h" />
88
92
  <ClInclude Include="$(MSBuildThisFileDirectory)Modules\AsyncStorageModuleWin32.h" />
@@ -130,8 +134,10 @@
130
134
  <ClInclude Include="$(MSBuildThisFileDirectory)TurboModuleRegistry.h" />
131
135
  <ClInclude Include="$(MSBuildThisFileDirectory)Utils.h" />
132
136
  <ClInclude Include="$(MSBuildThisFileDirectory)Utils\CppWinrtLessExceptions.h" />
137
+ <ClInclude Include="$(MSBuildThisFileDirectory)Utils\WinRTConversions.h" />
133
138
  <ClInclude Include="$(MSBuildThisFileDirectory)V8JSIRuntimeHolder.h" />
134
139
  <ClInclude Include="$(MSBuildThisFileDirectory)WebSocketJSExecutorFactory.h" />
140
+ <ClInclude Include="$(MSBuildThisFileDirectory)WinRTHttpResource.h" />
135
141
  <ClInclude Include="$(MSBuildThisFileDirectory)WinRTWebSocketResource.h" />
136
142
  </ItemGroup>
137
143
  <ItemGroup>
@@ -133,6 +133,15 @@
133
133
  <ClCompile Include="$(MSBuildThisFileDirectory)HermesShim.cpp">
134
134
  <Filter>Source Files</Filter>
135
135
  </ClCompile>
136
+ <ClCompile Include="$(MSBuildThisFileDirectory)Modules\HttpModule.cpp">
137
+ <Filter>Source Files\Modules</Filter>
138
+ </ClCompile>
139
+ <ClCompile Include="$(MSBuildThisFileDirectory)WinRTHttpResource.cpp">
140
+ <Filter>Source Files</Filter>
141
+ </ClCompile>
142
+ <ClCompile Include="$(MSBuildThisFileDirectory)Utils\WinRTConversions.cpp">
143
+ <Filter>Source Files\Utils</Filter>
144
+ </ClCompile>
136
145
  </ItemGroup>
137
146
  <ItemGroup>
138
147
  <Filter Include="Source Files">
@@ -183,6 +192,9 @@
183
192
  <Filter Include="Source Files\JSI">
184
193
  <UniqueIdentifier>{1a3ad55f-1297-41b3-ba2a-0f819e69270c}</UniqueIdentifier>
185
194
  </Filter>
195
+ <Filter Include="Source Files\Utils">
196
+ <UniqueIdentifier>{e78de2f1-a7e5-4a81-b69b-4a1f7fa91cde}</UniqueIdentifier>
197
+ </Filter>
186
198
  </ItemGroup>
187
199
  <ItemGroup>
188
200
  <ClInclude Include="$(MSBuildThisFileDirectory)AsyncStorage\StorageFileIO.h">
@@ -381,6 +393,15 @@
381
393
  <ClInclude Include="$(MSBuildThisFileDirectory)HermesShim.h">
382
394
  <Filter>Header Files</Filter>
383
395
  </ClInclude>
396
+ <ClInclude Include="$(MSBuildThisFileDirectory)Modules\HttpModule.h">
397
+ <Filter>Header Files\Modules</Filter>
398
+ </ClInclude>
399
+ <ClInclude Include="$(MSBuildThisFileDirectory)WinRTHttpResource.h">
400
+ <Filter>Header Files</Filter>
401
+ </ClInclude>
402
+ <ClInclude Include="$(MSBuildThisFileDirectory)Utils\WinRTConversions.h">
403
+ <Filter>Header Files\Utils</Filter>
404
+ </ClInclude>
384
405
  </ItemGroup>
385
406
  <ItemGroup>
386
407
  <None Include="$(MSBuildThisFileDirectory)tracing\rnw.wprp">
@@ -0,0 +1,22 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+
4
+ #include "WinRTConversions.h"
5
+
6
+ // Standard Library
7
+ #include <sstream>
8
+
9
+ namespace Microsoft::React::Utilities {
10
+
11
+ std::string HResultToString(winrt::hresult_error const &e) {
12
+ std::stringstream stream;
13
+ stream << "[0x" << std::hex << e.code() << "] " << winrt::to_string(e.message());
14
+
15
+ return stream.str();
16
+ }
17
+
18
+ std::string HResultToString(winrt::hresult &&result) {
19
+ return HResultToString(winrt::hresult_error(std::move(result), winrt::hresult_error::from_abi));
20
+ }
21
+
22
+ } // namespace Microsoft::React::Utilities
@@ -0,0 +1,15 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+
4
+ #pragma once
5
+
6
+ // Windows API
7
+ #include <winrt/base.h>
8
+
9
+ namespace Microsoft::React::Utilities {
10
+
11
+ std::string HResultToString(winrt::hresult_error const &e);
12
+
13
+ std::string HResultToString(winrt::hresult &&result);
14
+
15
+ } // namespace Microsoft::React::Utilities