react-native-windows 0.66.18 → 0.66.21

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.
Files changed (63) hide show
  1. package/CHANGELOG.json +58 -1
  2. package/CHANGELOG.md +31 -5
  3. package/Chakra/ChakraHelpers.cpp +0 -1
  4. package/Directory.Build.props +4 -0
  5. package/Folly/Folly.vcxproj +4 -5
  6. package/Libraries/Network/RCTNetworkingWinShared.js +7 -0
  7. package/Microsoft.ReactNative/Base/CoreNativeModules.cpp +3 -1
  8. package/Microsoft.ReactNative/IReactContext.cpp +17 -0
  9. package/Microsoft.ReactNative/IReactContext.h +2 -0
  10. package/Microsoft.ReactNative/IReactContext.idl +27 -0
  11. package/Microsoft.ReactNative/Modules/CreateModules.cpp +3 -3
  12. package/Microsoft.ReactNative/packages.config +1 -1
  13. package/Microsoft.ReactNative.Cxx/JSI/NodeApiJsiRuntime.cpp +17 -3
  14. package/Microsoft.ReactNative.Cxx/JSI/NodeApiJsiRuntime.h +48 -0
  15. package/PropertySheets/JSEngine.props +1 -1
  16. package/Scripts/OfficeReact.Win32.nuspec +1 -1
  17. package/Shared/BaseScriptStoreImpl.cpp +1 -1
  18. package/Shared/CppRuntimeOptions.h +50 -0
  19. package/Shared/CreateModules.h +17 -2
  20. package/Shared/InspectorPackagerConnection.cpp +7 -5
  21. package/Shared/InspectorPackagerConnection.h +2 -2
  22. package/Shared/JSI/ChakraApi.cpp +0 -1
  23. package/Shared/JSI/ChakraRuntime.cpp +0 -1
  24. package/Shared/JSI/NapiJsiV8RuntimeHolder.cpp +72 -2
  25. package/Shared/JSI/NapiJsiV8RuntimeHolder.h +2 -0
  26. package/Shared/Modules/BlobModule.cpp +376 -0
  27. package/Shared/Modules/BlobModule.h +153 -0
  28. package/Shared/Modules/CxxModuleUtilities.cpp +19 -0
  29. package/Shared/Modules/CxxModuleUtilities.h +23 -0
  30. package/Shared/Modules/FileReaderModule.cpp +156 -0
  31. package/Shared/Modules/FileReaderModule.h +54 -0
  32. package/Shared/Modules/HttpModule.cpp +73 -70
  33. package/Shared/Modules/HttpModule.h +10 -3
  34. package/Shared/Modules/IBlobPersistor.h +30 -0
  35. package/Shared/Modules/IHttpModuleProxy.h +30 -0
  36. package/Shared/Modules/IRequestBodyHandler.h +52 -0
  37. package/Shared/Modules/IResponseHandler.h +27 -0
  38. package/Shared/Modules/IUriHandler.h +37 -0
  39. package/Shared/Modules/IWebSocketModuleContentHandler.h +26 -0
  40. package/Shared/Modules/IWebSocketModuleProxy.h +22 -0
  41. package/Shared/Modules/NetworkingModule.cpp +1 -1
  42. package/Shared/Modules/WebSocketModule.cpp +93 -23
  43. package/Shared/Modules/WebSocketModule.h +35 -6
  44. package/Shared/Networking/IHttpResource.h +101 -0
  45. package/Shared/{IWebSocketResource.h → Networking/IWebSocketResource.h} +2 -2
  46. package/Shared/Networking/OriginPolicy.h +15 -0
  47. package/Shared/Networking/OriginPolicyHttpFilter.cpp +747 -0
  48. package/Shared/Networking/OriginPolicyHttpFilter.h +112 -0
  49. package/Shared/Networking/WinRTHttpResource.cpp +465 -0
  50. package/Shared/{WinRTHttpResource.h → Networking/WinRTHttpResource.h} +35 -20
  51. package/Shared/Networking/WinRTTypes.h +33 -0
  52. package/Shared/{WinRTWebSocketResource.cpp → Networking/WinRTWebSocketResource.cpp} +2 -2
  53. package/Shared/{WinRTWebSocketResource.h → Networking/WinRTWebSocketResource.h} +3 -3
  54. package/Shared/OInstance.cpp +26 -6
  55. package/Shared/OInstance.h +8 -4
  56. package/Shared/RuntimeOptions.cpp +96 -15
  57. package/Shared/RuntimeOptions.h +32 -8
  58. package/Shared/Shared.vcxitems +24 -6
  59. package/Shared/Shared.vcxitems.filters +78 -18
  60. package/fmt/fmt.vcxproj +4 -5
  61. package/package.json +1 -1
  62. package/Shared/IHttpResource.h +0 -53
  63. package/Shared/WinRTHttpResource.cpp +0 -317
@@ -1,53 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT License.
3
-
4
- #pragma once
5
-
6
- // Standard Library
7
- #include <functional>
8
- #include <memory>
9
- #include <string>
10
- #include <unordered_map>
11
-
12
- namespace Microsoft::React {
13
-
14
- struct IHttpResource {
15
- typedef std::unordered_map<std::string, std::string> Headers;
16
-
17
- struct BodyData {
18
- enum class Type : size_t { Empty, String, Base64, Uri, Form } Type = Type::Empty;
19
- std::string Data;
20
- };
21
-
22
- struct Response {
23
- int64_t StatusCode;
24
- Headers Headers;
25
- std::string Url;
26
- };
27
-
28
- static std::shared_ptr<IHttpResource> Make() noexcept;
29
-
30
- virtual ~IHttpResource() noexcept {}
31
-
32
- virtual void SendRequest(
33
- std::string &&method,
34
- std::string &&url,
35
- Headers &&headers,
36
- BodyData &&bodyData,
37
- std::string &&responseType,
38
- bool useIncrementalUpdates,
39
- int64_t timeout,
40
- bool withCredentials,
41
- std::function<void(int64_t)> &&callback) noexcept = 0;
42
- virtual void AbortRequest(int64_t requestId) noexcept = 0;
43
-
44
- virtual void ClearCookies() noexcept = 0;
45
-
46
- virtual void SetOnRequest(std::function<void(int64_t requestId)> &&handler) noexcept = 0; // TODO: Keep???
47
- virtual void SetOnResponse(std::function<void(int64_t requestId, Response &&response)> &&handler) noexcept = 0;
48
- virtual void SetOnData(std::function<void(int64_t requestId, std::string &&responseData)> &&handler) noexcept = 0;
49
- virtual void SetOnError(
50
- std::function<void(int64_t requestId, std::string &&errorMessage /*, bool isTimeout*/)> &&handler) noexcept = 0;
51
- };
52
-
53
- } // namespace Microsoft::React
@@ -1,317 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT License.
3
-
4
- #include "WinRTHttpResource.h"
5
-
6
- #include <Utils/CppWinrtLessExceptions.h>
7
- #include <Utils/WinRTConversions.h>
8
- #include <utilities.h>
9
-
10
- // Windows API
11
- #include <winrt/Windows.Security.Cryptography.h>
12
- #include <winrt/Windows.Storage.Streams.h>
13
- #include <winrt/Windows.Web.Http.Headers.h>
14
-
15
- using std::function;
16
- using std::scoped_lock;
17
- using std::shared_ptr;
18
- using std::string;
19
-
20
- using winrt::fire_and_forget;
21
- using winrt::hresult_error;
22
- using winrt::to_hstring;
23
- using winrt::to_string;
24
- using winrt::Windows::Foundation::Uri;
25
- using winrt::Windows::Security::Cryptography::CryptographicBuffer;
26
- using winrt::Windows::Storage::StorageFile;
27
- using winrt::Windows::Storage::Streams::DataReader;
28
- using winrt::Windows::Storage::Streams::UnicodeEncoding;
29
- using winrt::Windows::Web::Http::HttpBufferContent;
30
- using winrt::Windows::Web::Http::HttpMethod;
31
- using winrt::Windows::Web::Http::HttpRequestMessage;
32
- using winrt::Windows::Web::Http::HttpStreamContent;
33
- using winrt::Windows::Web::Http::HttpStringContent;
34
- using winrt::Windows::Web::Http::IHttpClient;
35
- using winrt::Windows::Web::Http::IHttpContent;
36
- using winrt::Windows::Web::Http::Headers::HttpMediaTypeHeaderValue;
37
-
38
- namespace Microsoft::React {
39
-
40
- #pragma region WinRTHttpResource
41
-
42
- // TODO: Check for multi-thread issues if there are multiple instances.
43
- /*static*/ int64_t WinRTHttpResource::s_lastRequestId = 0;
44
-
45
- WinRTHttpResource::WinRTHttpResource(IHttpClient &&client) noexcept : m_client{std::move(client)} {}
46
-
47
- WinRTHttpResource::WinRTHttpResource() noexcept : WinRTHttpResource(winrt::Windows::Web::Http::HttpClient()) {}
48
-
49
- #pragma region IHttpResource
50
-
51
- void WinRTHttpResource::SendRequest(
52
- string &&method,
53
- string &&url,
54
- Headers &&headers,
55
- BodyData &&bodyData,
56
- string &&responseType,
57
- bool useIncrementalUpdates,
58
- int64_t timeout,
59
- bool withCredentials,
60
- std::function<void(int64_t)> &&callback) noexcept /*override*/ {
61
- auto requestId = ++s_lastRequestId;
62
-
63
- // Enforce supported args
64
- assert(responseType == "text" || responseType == "base64");
65
-
66
- if (callback) {
67
- callback(requestId);
68
- }
69
-
70
- try {
71
- HttpMethod httpMethod{to_hstring(std::move(method))};
72
- Uri uri{to_hstring(std::move(url))};
73
- HttpRequestMessage request{httpMethod, uri};
74
- HttpMediaTypeHeaderValue contentType{nullptr};
75
- string contentEncoding;
76
- string contentLength;
77
-
78
- // Headers are generally case-insensitive
79
- // https://www.ietf.org/rfc/rfc2616.txt section 4.2
80
- for (auto &header : headers) {
81
- if (_stricmp(header.first.c_str(), "content-type") == 0) {
82
- bool success = HttpMediaTypeHeaderValue::TryParse(to_hstring(header.second), contentType);
83
- if (!success && m_onError) {
84
- return m_onError(requestId, "Failed to parse Content-Type");
85
- }
86
- } else if (_stricmp(header.first.c_str(), "content-encoding") == 0) {
87
- contentEncoding = header.second;
88
- } else if (_stricmp(header.first.c_str(), "content-length") == 0) {
89
- contentLength = header.second;
90
- } else if (_stricmp(header.first.c_str(), "authorization") == 0) {
91
- bool success =
92
- request.Headers().TryAppendWithoutValidation(to_hstring(header.first), to_hstring(header.second));
93
- if (!success && m_onError) {
94
- return m_onError(requestId, "Failed to append Authorization");
95
- }
96
- } else {
97
- request.Headers().Append(to_hstring(header.first), to_hstring(header.second));
98
- }
99
- }
100
-
101
- IHttpContent content{nullptr};
102
- if (BodyData::Type::String == bodyData.Type) {
103
- content = HttpStringContent{to_hstring(bodyData.Data)};
104
- } else if (BodyData::Type::Base64 == bodyData.Type) {
105
- auto buffer = CryptographicBuffer::DecodeFromBase64String(to_hstring(bodyData.Data));
106
- content = HttpBufferContent{buffer};
107
- } else if (BodyData::Type::Uri == bodyData.Type) {
108
- auto file = StorageFile::GetFileFromApplicationUriAsync(Uri{to_hstring(bodyData.Data)}).get();
109
- auto stream = file.OpenReadAsync().get();
110
- content = HttpStreamContent{stream};
111
- } else if (BodyData::Type::Form == bodyData.Type) {
112
- // TODO: Add support
113
- } else {
114
- // BodyData::Type::Empty
115
- // TODO: Error 'cause unsupported??
116
- }
117
-
118
- if (content != nullptr) {
119
- // Attach content headers
120
- if (contentType) {
121
- content.Headers().ContentType(contentType);
122
- }
123
- if (!contentEncoding.empty()) {
124
- if (!content.Headers().ContentEncoding().TryParseAdd(to_hstring(contentEncoding))) {
125
- if (m_onError) {
126
- m_onError(requestId, "Failed to parse Content-Encoding");
127
- }
128
- return;
129
- }
130
- }
131
- if (!contentLength.empty()) {
132
- const auto contentLengthHeader = _atoi64(contentLength.c_str()); // TODO: Alternatives to _atoi64?
133
- content.Headers().ContentLength(contentLengthHeader);
134
- }
135
-
136
- request.Content(content);
137
- }
138
-
139
- PerformSendRequest(requestId, std::move(request), responseType == "text");
140
- } catch (std::exception const &e) {
141
- if (m_onError) {
142
- m_onError(requestId, e.what());
143
- }
144
- } catch (hresult_error const &e) {
145
- if (m_onError) {
146
- m_onError(requestId, Utilities::HResultToString(e));
147
- }
148
- } catch (...) {
149
- m_onError(requestId, "Unidentified error sending HTTP request");
150
- }
151
- }
152
-
153
- void WinRTHttpResource::AbortRequest(int64_t requestId) noexcept /*override*/ {
154
- ResponseType request{nullptr};
155
-
156
- {
157
- scoped_lock lock{m_mutex};
158
- auto iter = m_responses.find(requestId);
159
- if (iter == std::end(m_responses)) {
160
- return;
161
- }
162
- request = iter->second;
163
- }
164
-
165
- try {
166
- request.Cancel();
167
- } catch (hresult_error const &e) {
168
- m_onError(requestId, Utilities::HResultToString(e));
169
- }
170
- }
171
-
172
- void WinRTHttpResource::ClearCookies() noexcept /*override*/ {
173
- assert(false);
174
- // NOT IMPLEMENTED
175
- }
176
-
177
- void WinRTHttpResource::SetOnRequest(function<void(int64_t requestId)> &&handler) noexcept /*override*/ {
178
- m_onRequest = std::move(handler);
179
- }
180
-
181
- void WinRTHttpResource::SetOnResponse(function<void(int64_t requestId, Response &&response)> &&handler) noexcept
182
- /*override*/ {
183
- m_onResponse = std::move(handler);
184
- }
185
-
186
- void WinRTHttpResource::SetOnData(function<void(int64_t requestId, std::string &&responseData)> &&handler) noexcept
187
- /*override*/ {
188
- m_onData = std::move(handler);
189
- }
190
-
191
- void WinRTHttpResource::SetOnError(function<void(int64_t requestId, string &&errorMessage)> &&handler) noexcept
192
- /*override*/ {
193
- m_onError = std::move(handler);
194
- }
195
-
196
- #pragma endregion IHttpResource
197
-
198
- void WinRTHttpResource::TrackResponse(int64_t requestId, ResponseType response) noexcept {
199
- scoped_lock lock{m_mutex};
200
- m_responses[requestId] = response;
201
- }
202
-
203
- void WinRTHttpResource::UntrackResponse(int64_t requestId) noexcept {
204
- scoped_lock lock{m_mutex};
205
- m_responses.erase(requestId);
206
- }
207
-
208
- fire_and_forget
209
- WinRTHttpResource::PerformSendRequest(int64_t requestId, HttpRequestMessage &&request, bool textResponse) noexcept {
210
- // Keep references after coroutine suspension.
211
- auto self = shared_from_this();
212
- auto coRequest = std::move(request);
213
-
214
- // Ensure background thread
215
- co_await winrt::resume_background();
216
-
217
- try {
218
- auto sendRequestOp = self->m_client.SendRequestAsync(coRequest);
219
-
220
- self->TrackResponse(requestId, sendRequestOp);
221
-
222
- co_await lessthrow_await_adapter<ResponseType>{sendRequestOp};
223
- auto result = sendRequestOp.ErrorCode();
224
- if (result < 0) {
225
- if (self->m_onError) {
226
- self->m_onError(requestId, Utilities::HResultToString(std::move(result)));
227
- }
228
- self->UntrackResponse(requestId);
229
- co_return;
230
- }
231
-
232
- auto response = sendRequestOp.GetResults();
233
- if (response) {
234
- if (self->m_onResponse) {
235
- Headers headers;
236
-
237
- // Gather headers for both the response content and the response itself
238
- // See Invoke-WebRequest PowerShell cmdlet or Chromium response handling
239
- for (auto header : response.Headers()) {
240
- headers.emplace(to_string(header.Key()), to_string(header.Value()));
241
- }
242
- for (auto header : response.Content().Headers()) {
243
- headers.emplace(to_string(header.Key()), to_string(header.Value()));
244
- }
245
- string url = to_string(response.RequestMessage().RequestUri().AbsoluteUri());
246
- self->m_onResponse(
247
- requestId, {static_cast<int32_t>(response.StatusCode()), std::move(headers), std::move(url)});
248
- }
249
- }
250
-
251
- // TODO: Incremental updates?
252
- if (response && response.Content()) {
253
- auto inputStream = co_await response.Content().ReadAsInputStreamAsync();
254
- auto reader = DataReader{inputStream};
255
-
256
- if (textResponse) {
257
- reader.UnicodeEncoding(UnicodeEncoding::Utf8);
258
- }
259
-
260
- // Only support response sizes up to 10MB.
261
- // TODO: WHY????
262
- co_await reader.LoadAsync(10 * 1024 * 1024);
263
- auto length = reader.UnconsumedBufferLength();
264
-
265
- if (textResponse) {
266
- std::vector<uint8_t> data(length);
267
- reader.ReadBytes(data);
268
- string responseData = string(Common::Utilities::CheckedReinterpretCast<char *>(data.data()), data.size());
269
-
270
- if (self->m_onData) {
271
- self->m_onData(requestId, std::move(responseData));
272
- }
273
- } else {
274
- auto buffer = reader.ReadBuffer(length);
275
- auto data = CryptographicBuffer::EncodeToBase64String(buffer);
276
- auto responseData = to_string(std::wstring_view(data));
277
-
278
- if (self->m_onData) {
279
- self->m_onData(requestId, std::move(responseData));
280
- }
281
- }
282
- } else {
283
- if (self->m_onError) {
284
- self->m_onError(requestId, response == nullptr ? "request failed" : "No response content");
285
- }
286
- }
287
- } catch (std::exception const &e) {
288
- if (self->m_onError) {
289
- self->m_onError(requestId, e.what());
290
- }
291
- } catch (hresult_error const &e) {
292
- if (self->m_onError) {
293
- self->m_onError(requestId, Utilities::HResultToString(e));
294
- }
295
- } catch (...) {
296
- if (self->m_onError) {
297
- self->m_onError(requestId, "Unhandled exception during request");
298
- }
299
- }
300
-
301
- self->UntrackResponse(requestId);
302
-
303
- // TODO: keep? See https://devblogs.microsoft.com/oldnewthing/?p=106160
304
- co_return;
305
- }
306
-
307
- #pragma endregion WinRTHttpResource
308
-
309
- #pragma region IHttpResource
310
-
311
- /*static*/ shared_ptr<IHttpResource> IHttpResource::Make() noexcept {
312
- return std::make_shared<WinRTHttpResource>();
313
- }
314
-
315
- #pragma endregion IHttpResource
316
-
317
- } // namespace Microsoft::React