@datadog/mobile-react-native-webview 1.5.0
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.
- package/LICENSE +201 -0
- package/README.md +57 -0
- package/lib/commonjs/__utils__/formatAllowedHosts.js +72 -0
- package/lib/commonjs/__utils__/formatAllowedHosts.js.map +1 -0
- package/lib/commonjs/__utils__/getInjectedJavaScriptBeforeContentLoaded.js +36 -0
- package/lib/commonjs/__utils__/getInjectedJavaScriptBeforeContentLoaded.js.map +1 -0
- package/lib/commonjs/index.js +42 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/module/__utils__/formatAllowedHosts.js +63 -0
- package/lib/module/__utils__/formatAllowedHosts.js.map +1 -0
- package/lib/module/__utils__/getInjectedJavaScriptBeforeContentLoaded.js +24 -0
- package/lib/module/__utils__/getInjectedJavaScriptBeforeContentLoaded.js.map +1 -0
- package/lib/module/index.js +29 -0
- package/lib/module/index.js.map +1 -0
- package/lib/typescript/__utils__/formatAllowedHosts.d.ts +1 -0
- package/lib/typescript/__utils__/getInjectedJavaScriptBeforeContentLoaded.d.ts +2 -0
- package/lib/typescript/index.d.ts +7 -0
- package/package.json +74 -0
- package/src/__tests__/WebviewDatadog.test.tsx +54 -0
- package/src/__tests__/WebviewDatadogInjectedJS.test.tsx +36 -0
- package/src/__tests__/WebviewDatadogPerformance.test.tsx +46 -0
- package/src/__tests__/formatAllowedHosts.test.ts +40 -0
- package/src/__tests__/getInjectedJavaScriptBeforeContentLoaded.test.ts +86 -0
- package/src/__utils__/formatAllowedHosts.ts +70 -0
- package/src/__utils__/getInjectedJavaScriptBeforeContentLoaded.ts +29 -0
- package/src/index.tsx +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2017 Datadog, Inc.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# React-Native Monitoring for react-native-webview
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Real User Monitoring (RUM) allows you to monitor web views and eliminate blind spots in your hybrid React Native applications.
|
|
6
|
+
|
|
7
|
+
You can perform the following:
|
|
8
|
+
|
|
9
|
+
- Track user journeys across web and native components in mobile applications
|
|
10
|
+
- Scope the root cause of latency to web pages or native components in mobile applications
|
|
11
|
+
- Support users that have difficulty loading web pages on mobile devices
|
|
12
|
+
|
|
13
|
+
RUM supports web views created using [react-native-webview][3].
|
|
14
|
+
|
|
15
|
+
## Setup
|
|
16
|
+
|
|
17
|
+
### Prerequisites
|
|
18
|
+
|
|
19
|
+
Set up the RUM Browser SDK on the web page you want rendered on your mobile React Native application. For more information, see [RUM Browser Monitoring][1].
|
|
20
|
+
|
|
21
|
+
Add `react-native-webview` to your application following the [official installation documentation][4].
|
|
22
|
+
|
|
23
|
+
### Instrument your web views
|
|
24
|
+
|
|
25
|
+
Import `WebView` from `@datadog/mobile-react-native-webview` instead of `react-native-webview`:
|
|
26
|
+
|
|
27
|
+
```javascript
|
|
28
|
+
import { WebView } from '@datadog/mobile-react-native-webview';
|
|
29
|
+
// or
|
|
30
|
+
import WebView from '@datadog/mobile-react-native-webview';
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
You can use all existing functionalities from `react-native-webview` as the `WebView` component from `@datadog/mobile-react-native-webview` wraps the `react-native-webview` component.
|
|
34
|
+
|
|
35
|
+
Provide the list of hosts to be tracked by Datadog inside the web view by using the `allowedHosts` prop of your `WebView` component:
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
<WebView
|
|
39
|
+
source={{ uri: 'https://www.example.com' }}
|
|
40
|
+
allowedHosts={['example.com']}
|
|
41
|
+
/>
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Access your web views
|
|
45
|
+
|
|
46
|
+
Your web views appear in the [RUM Explorer][2] with associated `service` and `source` attributes. The `service` attribute indicates the web component the web view is generated from, and the `source` attribute denotes the mobile application's platform, such as React Native.
|
|
47
|
+
|
|
48
|
+
Filter on your React Native applications, and click a session. A side panel with a list of events in the session appears.
|
|
49
|
+
|
|
50
|
+
{{< img src="real_user_monitoring/react_native/reactnative_webview_session.png" alt="webview session example" >}}
|
|
51
|
+
|
|
52
|
+
Click **Open View waterfall** to navigate from the session to a resource waterfall visualization in the view's **Performance** tab.
|
|
53
|
+
|
|
54
|
+
[1]: https://docs.datadoghq.com/real_user_monitoring/browser/#npm
|
|
55
|
+
[2]: https://app.datadoghq.com/rum/explorer
|
|
56
|
+
[3]: https://github.com/react-native-webview/react-native-webview
|
|
57
|
+
[4]: https://github.com/react-native-webview/react-native-webview/blob/master/docs/Getting-Started.md
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.formatAllowedHosts = formatAllowedHosts;
|
|
7
|
+
|
|
8
|
+
var _reactNative = require("react-native");
|
|
9
|
+
|
|
10
|
+
/*
|
|
11
|
+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
|
|
12
|
+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
|
|
13
|
+
* Copyright 2016-Present Datadog, Inc.
|
|
14
|
+
*/
|
|
15
|
+
function formatAllowedHosts(allowedHosts) {
|
|
16
|
+
try {
|
|
17
|
+
return `'${JSON.stringify(allowedHosts)}'`;
|
|
18
|
+
} catch (e) {
|
|
19
|
+
if (_reactNative.NativeModules.DdSdk && _reactNative.NativeModules.DdSdk.telemetryError) {
|
|
20
|
+
_reactNative.NativeModules.DdSdk.telemetryError(getErrorMessage(e), getErrorStackTrace(e), 'AllowedHostsError');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return "'[]'";
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The next section is copied from packages/core/src/errorUtils
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
const EMPTY_MESSAGE = 'Unknown Error';
|
|
32
|
+
const EMPTY_STACK_TRACE = '';
|
|
33
|
+
|
|
34
|
+
const getErrorMessage = error => {
|
|
35
|
+
let message = EMPTY_MESSAGE;
|
|
36
|
+
|
|
37
|
+
if (error === undefined || error === null) {
|
|
38
|
+
message = EMPTY_MESSAGE;
|
|
39
|
+
} else if (typeof error === 'object' && 'message' in error) {
|
|
40
|
+
message = String(error.message);
|
|
41
|
+
} else {
|
|
42
|
+
message = String(error);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return message;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const getErrorStackTrace = error => {
|
|
49
|
+
let stack = EMPTY_STACK_TRACE;
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
if (error === undefined || error === null) {
|
|
53
|
+
stack = EMPTY_STACK_TRACE;
|
|
54
|
+
} else if (typeof error === 'string') {
|
|
55
|
+
stack = EMPTY_STACK_TRACE;
|
|
56
|
+
} else if (typeof error === 'object') {
|
|
57
|
+
if ('stacktrace' in error) {
|
|
58
|
+
stack = String(error.stacktrace);
|
|
59
|
+
} else if ('stack' in error) {
|
|
60
|
+
stack = String(error.stack);
|
|
61
|
+
} else if ('componentStack' in error) {
|
|
62
|
+
stack = String(error.componentStack);
|
|
63
|
+
} else if ('sourceURL' in error && 'line' in error && 'column' in error) {
|
|
64
|
+
stack = `at ${error.sourceURL}:${error.line}:${error.column}`;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
} catch (e) {// Do nothing
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return stack;
|
|
71
|
+
};
|
|
72
|
+
//# sourceMappingURL=formatAllowedHosts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["formatAllowedHosts.ts"],"names":["formatAllowedHosts","allowedHosts","JSON","stringify","e","NativeModules","DdSdk","telemetryError","getErrorMessage","getErrorStackTrace","EMPTY_MESSAGE","EMPTY_STACK_TRACE","error","message","undefined","String","stack","stacktrace","componentStack","sourceURL","line","column"],"mappings":";;;;;;;AAKA;;AALA;AACA;AACA;AACA;AACA;AAGO,SAASA,kBAAT,CAA4BC,YAA5B,EAA6D;AAChE,MAAI;AACA,WAAQ,IAAGC,IAAI,CAACC,SAAL,CAAeF,YAAf,CAA6B,GAAxC;AACH,GAFD,CAEE,OAAOG,CAAP,EAAe;AACb,QAAIC,2BAAcC,KAAd,IAAuBD,2BAAcC,KAAd,CAAoBC,cAA/C,EAA+D;AAC3DF,iCAAcC,KAAd,CAAoBC,cAApB,CACIC,eAAe,CAACJ,CAAD,CADnB,EAEIK,kBAAkB,CAACL,CAAD,CAFtB,EAGI,mBAHJ;AAKH;;AACD,WAAO,MAAP;AACH;AACJ;AAED;AACA;AACA;;;AAEA,MAAMM,aAAa,GAAG,eAAtB;AACA,MAAMC,iBAAiB,GAAG,EAA1B;;AAEA,MAAMH,eAAe,GAAII,KAAD,IAAoC;AACxD,MAAIC,OAAO,GAAGH,aAAd;;AACA,MAAIE,KAAK,KAAKE,SAAV,IAAuBF,KAAK,KAAK,IAArC,EAA2C;AACvCC,IAAAA,OAAO,GAAGH,aAAV;AACH,GAFD,MAEO,IAAI,OAAOE,KAAP,KAAiB,QAAjB,IAA6B,aAAaA,KAA9C,EAAqD;AACxDC,IAAAA,OAAO,GAAGE,MAAM,CAACH,KAAK,CAACC,OAAP,CAAhB;AACH,GAFM,MAEA;AACHA,IAAAA,OAAO,GAAGE,MAAM,CAACH,KAAD,CAAhB;AACH;;AAED,SAAOC,OAAP;AACH,CAXD;;AAaA,MAAMJ,kBAAkB,GAAIG,KAAD,IAAoC;AAC3D,MAAII,KAAK,GAAGL,iBAAZ;;AAEA,MAAI;AACA,QAAIC,KAAK,KAAKE,SAAV,IAAuBF,KAAK,KAAK,IAArC,EAA2C;AACvCI,MAAAA,KAAK,GAAGL,iBAAR;AACH,KAFD,MAEO,IAAI,OAAOC,KAAP,KAAiB,QAArB,EAA+B;AAClCI,MAAAA,KAAK,GAAGL,iBAAR;AACH,KAFM,MAEA,IAAI,OAAOC,KAAP,KAAiB,QAArB,EAA+B;AAClC,UAAI,gBAAgBA,KAApB,EAA2B;AACvBI,QAAAA,KAAK,GAAGD,MAAM,CAACH,KAAK,CAACK,UAAP,CAAd;AACH,OAFD,MAEO,IAAI,WAAWL,KAAf,EAAsB;AACzBI,QAAAA,KAAK,GAAGD,MAAM,CAACH,KAAK,CAACI,KAAP,CAAd;AACH,OAFM,MAEA,IAAI,oBAAoBJ,KAAxB,EAA+B;AAClCI,QAAAA,KAAK,GAAGD,MAAM,CAACH,KAAK,CAACM,cAAP,CAAd;AACH,OAFM,MAEA,IACH,eAAeN,KAAf,IACA,UAAUA,KADV,IAEA,YAAYA,KAHT,EAIL;AACEI,QAAAA,KAAK,GAAI,MAAKJ,KAAK,CAACO,SAAU,IAAGP,KAAK,CAACQ,IAAK,IAAGR,KAAK,CAACS,MAAO,EAA5D;AACH;AACJ;AACJ,GApBD,CAoBE,OAAOjB,CAAP,EAAU,CACR;AACH;;AACD,SAAOY,KAAP;AACH,CA3BD","sourcesContent":["/*\n * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.\n * This product includes software developed at Datadog (https://www.datadoghq.com/).\n * Copyright 2016-Present Datadog, Inc.\n */\nimport { NativeModules } from 'react-native';\n\nexport function formatAllowedHosts(allowedHosts?: string[]): string {\n try {\n return `'${JSON.stringify(allowedHosts)}'`;\n } catch (e: any) {\n if (NativeModules.DdSdk && NativeModules.DdSdk.telemetryError) {\n NativeModules.DdSdk.telemetryError(\n getErrorMessage(e),\n getErrorStackTrace(e),\n 'AllowedHostsError'\n );\n }\n return \"'[]'\";\n }\n}\n\n/**\n * The next section is copied from packages/core/src/errorUtils\n */\n\nconst EMPTY_MESSAGE = 'Unknown Error';\nconst EMPTY_STACK_TRACE = '';\n\nconst getErrorMessage = (error: any | undefined): string => {\n let message = EMPTY_MESSAGE;\n if (error === undefined || error === null) {\n message = EMPTY_MESSAGE;\n } else if (typeof error === 'object' && 'message' in error) {\n message = String(error.message);\n } else {\n message = String(error);\n }\n\n return message;\n};\n\nconst getErrorStackTrace = (error: any | undefined): string => {\n let stack = EMPTY_STACK_TRACE;\n\n try {\n if (error === undefined || error === null) {\n stack = EMPTY_STACK_TRACE;\n } else if (typeof error === 'string') {\n stack = EMPTY_STACK_TRACE;\n } else if (typeof error === 'object') {\n if ('stacktrace' in error) {\n stack = String(error.stacktrace);\n } else if ('stack' in error) {\n stack = String(error.stack);\n } else if ('componentStack' in error) {\n stack = String(error.componentStack);\n } else if (\n 'sourceURL' in error &&\n 'line' in error &&\n 'column' in error\n ) {\n stack = `at ${error.sourceURL}:${error.line}:${error.column}`;\n }\n }\n } catch (e) {\n // Do nothing\n }\n return stack;\n};\n"]}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getInjectedJavaScriptBeforeContentLoaded = exports.DATADOG_MESSAGE_PREFIX = void 0;
|
|
7
|
+
|
|
8
|
+
var _formatAllowedHosts = require("./formatAllowedHosts");
|
|
9
|
+
|
|
10
|
+
/*
|
|
11
|
+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
|
|
12
|
+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
|
|
13
|
+
* Copyright 2016-Present Datadog, Inc.
|
|
14
|
+
*/
|
|
15
|
+
const DATADOG_MESSAGE_PREFIX = '[DATADOG]';
|
|
16
|
+
exports.DATADOG_MESSAGE_PREFIX = DATADOG_MESSAGE_PREFIX;
|
|
17
|
+
|
|
18
|
+
const getInjectedJavaScriptBeforeContentLoaded = (allowedHosts, injectedJavaScriptBeforeContentLoaded) => `
|
|
19
|
+
window.DatadogEventBridge = {
|
|
20
|
+
send(msg) {
|
|
21
|
+
window.ReactNativeWebView.postMessage("${DATADOG_MESSAGE_PREFIX} " + msg)
|
|
22
|
+
},
|
|
23
|
+
getAllowedWebViewHosts() {
|
|
24
|
+
return ${(0, _formatAllowedHosts.formatAllowedHosts)(allowedHosts)}
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
try{
|
|
28
|
+
${injectedJavaScriptBeforeContentLoaded}
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
// The user defined code has crashed
|
|
32
|
+
}
|
|
33
|
+
`;
|
|
34
|
+
|
|
35
|
+
exports.getInjectedJavaScriptBeforeContentLoaded = getInjectedJavaScriptBeforeContentLoaded;
|
|
36
|
+
//# sourceMappingURL=getInjectedJavaScriptBeforeContentLoaded.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["getInjectedJavaScriptBeforeContentLoaded.ts"],"names":["DATADOG_MESSAGE_PREFIX","getInjectedJavaScriptBeforeContentLoaded","allowedHosts","injectedJavaScriptBeforeContentLoaded"],"mappings":";;;;;;;AAKA;;AALA;AACA;AACA;AACA;AACA;AAGO,MAAMA,sBAAsB,GAAG,WAA/B;;;AAEA,MAAMC,wCAAwC,GAAG,CACpDC,YADoD,EAEpDC,qCAFoD,KAInD;AACL;AACA;AACA,iDAAiDH,sBAAuB;AACxE;AACA;AACA,iBAAiB,4CAAmBE,YAAnB,CAAiC;AAClD;AACA;AACA;AACA,QAAQC,qCAAsC;AAC9C;AACA;AACA;AACA;AACA,GAnBO","sourcesContent":["/*\n * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.\n * This product includes software developed at Datadog (https://www.datadoghq.com/).\n * Copyright 2016-Present Datadog, Inc.\n */\nimport { formatAllowedHosts } from './formatAllowedHosts';\n\nexport const DATADOG_MESSAGE_PREFIX = '[DATADOG]';\n\nexport const getInjectedJavaScriptBeforeContentLoaded = (\n allowedHosts?: string[],\n injectedJavaScriptBeforeContentLoaded?: string\n): string =>\n `\n window.DatadogEventBridge = {\n send(msg) {\n window.ReactNativeWebView.postMessage(\"${DATADOG_MESSAGE_PREFIX} \" + msg)\n },\n getAllowedWebViewHosts() {\n return ${formatAllowedHosts(allowedHosts)}\n }\n };\n try{ \n ${injectedJavaScriptBeforeContentLoaded}\n }\n catch (error) {\n // The user defined code has crashed\n }\n `;\n"]}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = exports.WebView = void 0;
|
|
7
|
+
|
|
8
|
+
var _reactNativeWebview = require("react-native-webview");
|
|
9
|
+
|
|
10
|
+
var _reactNative = require("react-native");
|
|
11
|
+
|
|
12
|
+
var _react = _interopRequireWildcard(require("react"));
|
|
13
|
+
|
|
14
|
+
var _getInjectedJavaScriptBeforeContentLoaded = require("./__utils__/getInjectedJavaScriptBeforeContentLoaded");
|
|
15
|
+
|
|
16
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
17
|
+
|
|
18
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
19
|
+
|
|
20
|
+
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
21
|
+
|
|
22
|
+
const WebView = props => {
|
|
23
|
+
const userDefinedOnMessage = props.onMessage;
|
|
24
|
+
const onMessage = (0, _react.useCallback)(event => {
|
|
25
|
+
const message = event.nativeEvent.data;
|
|
26
|
+
|
|
27
|
+
if (message.startsWith(_getInjectedJavaScriptBeforeContentLoaded.DATADOG_MESSAGE_PREFIX)) {
|
|
28
|
+
_reactNative.NativeModules.DdSdk.consumeWebviewEvent(message.substring(_getInjectedJavaScriptBeforeContentLoaded.DATADOG_MESSAGE_PREFIX.length + 1));
|
|
29
|
+
} else {
|
|
30
|
+
userDefinedOnMessage === null || userDefinedOnMessage === void 0 ? void 0 : userDefinedOnMessage(event);
|
|
31
|
+
}
|
|
32
|
+
}, [userDefinedOnMessage]);
|
|
33
|
+
return /*#__PURE__*/_react.default.createElement(_reactNativeWebview.WebView, _extends({}, props, {
|
|
34
|
+
onMessage: onMessage,
|
|
35
|
+
injectedJavaScriptBeforeContentLoaded: (0, _getInjectedJavaScriptBeforeContentLoaded.getInjectedJavaScriptBeforeContentLoaded)(props.allowedHosts, props.injectedJavaScriptBeforeContentLoaded)
|
|
36
|
+
}));
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
exports.WebView = WebView;
|
|
40
|
+
var _default = WebView;
|
|
41
|
+
exports.default = _default;
|
|
42
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["index.tsx"],"names":["WebView","props","userDefinedOnMessage","onMessage","event","message","nativeEvent","data","startsWith","DATADOG_MESSAGE_PREFIX","NativeModules","DdSdk","consumeWebviewEvent","substring","length","allowedHosts","injectedJavaScriptBeforeContentLoaded"],"mappings":";;;;;;;AAMA;;AACA;;AACA;;AAEA;;;;;;;;AAUO,MAAMA,OAAO,GAAIC,KAAD,IAAkB;AACrC,QAAMC,oBAAoB,GAAGD,KAAK,CAACE,SAAnC;AACA,QAAMA,SAAS,GAAG,wBACbC,KAAD,IAAgC;AAC5B,UAAMC,OAAO,GAAGD,KAAK,CAACE,WAAN,CAAkBC,IAAlC;;AACA,QAAIF,OAAO,CAACG,UAAR,CAAmBC,gEAAnB,CAAJ,EAAgD;AAC5CC,iCAAcC,KAAd,CAAoBC,mBAApB,CACIP,OAAO,CAACQ,SAAR,CAAkBJ,iEAAuBK,MAAvB,GAAgC,CAAlD,CADJ;AAGH,KAJD,MAIO;AACHZ,MAAAA,oBAAoB,SAApB,IAAAA,oBAAoB,WAApB,YAAAA,oBAAoB,CAAGE,KAAH,CAApB;AACH;AACJ,GAVa,EAWd,CAACF,oBAAD,CAXc,CAAlB;AAaA,sBACI,6BAAC,2BAAD,eACQD,KADR;AAEI,IAAA,SAAS,EAAEE,SAFf;AAGI,IAAA,qCAAqC,EAAE,wFACnCF,KAAK,CAACc,YAD6B,EAEnCd,KAAK,CAACe,qCAF6B;AAH3C,KADJ;AAUH,CAzBM;;;eA2BQhB,O","sourcesContent":["/*\n * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.\n * This product includes software developed at Datadog (https://www.datadoghq.com/).\n * Copyright 2016-Present Datadog, Inc.\n */\nimport type { WebViewMessageEvent, WebViewProps } from 'react-native-webview';\nimport { WebView as RNWebView } from 'react-native-webview';\nimport { NativeModules } from 'react-native';\nimport React, { useCallback } from 'react';\n\nimport {\n DATADOG_MESSAGE_PREFIX,\n getInjectedJavaScriptBeforeContentLoaded\n} from './__utils__/getInjectedJavaScriptBeforeContentLoaded';\n\ntype Props = WebViewProps & {\n allowedHosts?: string[];\n injectedJavaScriptBeforeContentLoaded?: string;\n};\n\nexport const WebView = (props: Props) => {\n const userDefinedOnMessage = props.onMessage;\n const onMessage = useCallback(\n (event: WebViewMessageEvent) => {\n const message = event.nativeEvent.data;\n if (message.startsWith(DATADOG_MESSAGE_PREFIX)) {\n NativeModules.DdSdk.consumeWebviewEvent(\n message.substring(DATADOG_MESSAGE_PREFIX.length + 1)\n );\n } else {\n userDefinedOnMessage?.(event);\n }\n },\n [userDefinedOnMessage]\n );\n return (\n <RNWebView\n {...props}\n onMessage={onMessage}\n injectedJavaScriptBeforeContentLoaded={getInjectedJavaScriptBeforeContentLoaded(\n props.allowedHosts,\n props.injectedJavaScriptBeforeContentLoaded\n )}\n />\n );\n};\n\nexport default WebView;\n"]}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
|
|
3
|
+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
|
|
4
|
+
* Copyright 2016-Present Datadog, Inc.
|
|
5
|
+
*/
|
|
6
|
+
import { NativeModules } from 'react-native';
|
|
7
|
+
export function formatAllowedHosts(allowedHosts) {
|
|
8
|
+
try {
|
|
9
|
+
return `'${JSON.stringify(allowedHosts)}'`;
|
|
10
|
+
} catch (e) {
|
|
11
|
+
if (NativeModules.DdSdk && NativeModules.DdSdk.telemetryError) {
|
|
12
|
+
NativeModules.DdSdk.telemetryError(getErrorMessage(e), getErrorStackTrace(e), 'AllowedHostsError');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return "'[]'";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The next section is copied from packages/core/src/errorUtils
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const EMPTY_MESSAGE = 'Unknown Error';
|
|
23
|
+
const EMPTY_STACK_TRACE = '';
|
|
24
|
+
|
|
25
|
+
const getErrorMessage = error => {
|
|
26
|
+
let message = EMPTY_MESSAGE;
|
|
27
|
+
|
|
28
|
+
if (error === undefined || error === null) {
|
|
29
|
+
message = EMPTY_MESSAGE;
|
|
30
|
+
} else if (typeof error === 'object' && 'message' in error) {
|
|
31
|
+
message = String(error.message);
|
|
32
|
+
} else {
|
|
33
|
+
message = String(error);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return message;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const getErrorStackTrace = error => {
|
|
40
|
+
let stack = EMPTY_STACK_TRACE;
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
if (error === undefined || error === null) {
|
|
44
|
+
stack = EMPTY_STACK_TRACE;
|
|
45
|
+
} else if (typeof error === 'string') {
|
|
46
|
+
stack = EMPTY_STACK_TRACE;
|
|
47
|
+
} else if (typeof error === 'object') {
|
|
48
|
+
if ('stacktrace' in error) {
|
|
49
|
+
stack = String(error.stacktrace);
|
|
50
|
+
} else if ('stack' in error) {
|
|
51
|
+
stack = String(error.stack);
|
|
52
|
+
} else if ('componentStack' in error) {
|
|
53
|
+
stack = String(error.componentStack);
|
|
54
|
+
} else if ('sourceURL' in error && 'line' in error && 'column' in error) {
|
|
55
|
+
stack = `at ${error.sourceURL}:${error.line}:${error.column}`;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
} catch (e) {// Do nothing
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return stack;
|
|
62
|
+
};
|
|
63
|
+
//# sourceMappingURL=formatAllowedHosts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["formatAllowedHosts.ts"],"names":["NativeModules","formatAllowedHosts","allowedHosts","JSON","stringify","e","DdSdk","telemetryError","getErrorMessage","getErrorStackTrace","EMPTY_MESSAGE","EMPTY_STACK_TRACE","error","message","undefined","String","stack","stacktrace","componentStack","sourceURL","line","column"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA,SAASA,aAAT,QAA8B,cAA9B;AAEA,OAAO,SAASC,kBAAT,CAA4BC,YAA5B,EAA6D;AAChE,MAAI;AACA,WAAQ,IAAGC,IAAI,CAACC,SAAL,CAAeF,YAAf,CAA6B,GAAxC;AACH,GAFD,CAEE,OAAOG,CAAP,EAAe;AACb,QAAIL,aAAa,CAACM,KAAd,IAAuBN,aAAa,CAACM,KAAd,CAAoBC,cAA/C,EAA+D;AAC3DP,MAAAA,aAAa,CAACM,KAAd,CAAoBC,cAApB,CACIC,eAAe,CAACH,CAAD,CADnB,EAEII,kBAAkB,CAACJ,CAAD,CAFtB,EAGI,mBAHJ;AAKH;;AACD,WAAO,MAAP;AACH;AACJ;AAED;AACA;AACA;;AAEA,MAAMK,aAAa,GAAG,eAAtB;AACA,MAAMC,iBAAiB,GAAG,EAA1B;;AAEA,MAAMH,eAAe,GAAII,KAAD,IAAoC;AACxD,MAAIC,OAAO,GAAGH,aAAd;;AACA,MAAIE,KAAK,KAAKE,SAAV,IAAuBF,KAAK,KAAK,IAArC,EAA2C;AACvCC,IAAAA,OAAO,GAAGH,aAAV;AACH,GAFD,MAEO,IAAI,OAAOE,KAAP,KAAiB,QAAjB,IAA6B,aAAaA,KAA9C,EAAqD;AACxDC,IAAAA,OAAO,GAAGE,MAAM,CAACH,KAAK,CAACC,OAAP,CAAhB;AACH,GAFM,MAEA;AACHA,IAAAA,OAAO,GAAGE,MAAM,CAACH,KAAD,CAAhB;AACH;;AAED,SAAOC,OAAP;AACH,CAXD;;AAaA,MAAMJ,kBAAkB,GAAIG,KAAD,IAAoC;AAC3D,MAAII,KAAK,GAAGL,iBAAZ;;AAEA,MAAI;AACA,QAAIC,KAAK,KAAKE,SAAV,IAAuBF,KAAK,KAAK,IAArC,EAA2C;AACvCI,MAAAA,KAAK,GAAGL,iBAAR;AACH,KAFD,MAEO,IAAI,OAAOC,KAAP,KAAiB,QAArB,EAA+B;AAClCI,MAAAA,KAAK,GAAGL,iBAAR;AACH,KAFM,MAEA,IAAI,OAAOC,KAAP,KAAiB,QAArB,EAA+B;AAClC,UAAI,gBAAgBA,KAApB,EAA2B;AACvBI,QAAAA,KAAK,GAAGD,MAAM,CAACH,KAAK,CAACK,UAAP,CAAd;AACH,OAFD,MAEO,IAAI,WAAWL,KAAf,EAAsB;AACzBI,QAAAA,KAAK,GAAGD,MAAM,CAACH,KAAK,CAACI,KAAP,CAAd;AACH,OAFM,MAEA,IAAI,oBAAoBJ,KAAxB,EAA+B;AAClCI,QAAAA,KAAK,GAAGD,MAAM,CAACH,KAAK,CAACM,cAAP,CAAd;AACH,OAFM,MAEA,IACH,eAAeN,KAAf,IACA,UAAUA,KADV,IAEA,YAAYA,KAHT,EAIL;AACEI,QAAAA,KAAK,GAAI,MAAKJ,KAAK,CAACO,SAAU,IAAGP,KAAK,CAACQ,IAAK,IAAGR,KAAK,CAACS,MAAO,EAA5D;AACH;AACJ;AACJ,GApBD,CAoBE,OAAOhB,CAAP,EAAU,CACR;AACH;;AACD,SAAOW,KAAP;AACH,CA3BD","sourcesContent":["/*\n * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.\n * This product includes software developed at Datadog (https://www.datadoghq.com/).\n * Copyright 2016-Present Datadog, Inc.\n */\nimport { NativeModules } from 'react-native';\n\nexport function formatAllowedHosts(allowedHosts?: string[]): string {\n try {\n return `'${JSON.stringify(allowedHosts)}'`;\n } catch (e: any) {\n if (NativeModules.DdSdk && NativeModules.DdSdk.telemetryError) {\n NativeModules.DdSdk.telemetryError(\n getErrorMessage(e),\n getErrorStackTrace(e),\n 'AllowedHostsError'\n );\n }\n return \"'[]'\";\n }\n}\n\n/**\n * The next section is copied from packages/core/src/errorUtils\n */\n\nconst EMPTY_MESSAGE = 'Unknown Error';\nconst EMPTY_STACK_TRACE = '';\n\nconst getErrorMessage = (error: any | undefined): string => {\n let message = EMPTY_MESSAGE;\n if (error === undefined || error === null) {\n message = EMPTY_MESSAGE;\n } else if (typeof error === 'object' && 'message' in error) {\n message = String(error.message);\n } else {\n message = String(error);\n }\n\n return message;\n};\n\nconst getErrorStackTrace = (error: any | undefined): string => {\n let stack = EMPTY_STACK_TRACE;\n\n try {\n if (error === undefined || error === null) {\n stack = EMPTY_STACK_TRACE;\n } else if (typeof error === 'string') {\n stack = EMPTY_STACK_TRACE;\n } else if (typeof error === 'object') {\n if ('stacktrace' in error) {\n stack = String(error.stacktrace);\n } else if ('stack' in error) {\n stack = String(error.stack);\n } else if ('componentStack' in error) {\n stack = String(error.componentStack);\n } else if (\n 'sourceURL' in error &&\n 'line' in error &&\n 'column' in error\n ) {\n stack = `at ${error.sourceURL}:${error.line}:${error.column}`;\n }\n }\n } catch (e) {\n // Do nothing\n }\n return stack;\n};\n"]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
|
|
3
|
+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
|
|
4
|
+
* Copyright 2016-Present Datadog, Inc.
|
|
5
|
+
*/
|
|
6
|
+
import { formatAllowedHosts } from './formatAllowedHosts';
|
|
7
|
+
export const DATADOG_MESSAGE_PREFIX = '[DATADOG]';
|
|
8
|
+
export const getInjectedJavaScriptBeforeContentLoaded = (allowedHosts, injectedJavaScriptBeforeContentLoaded) => `
|
|
9
|
+
window.DatadogEventBridge = {
|
|
10
|
+
send(msg) {
|
|
11
|
+
window.ReactNativeWebView.postMessage("${DATADOG_MESSAGE_PREFIX} " + msg)
|
|
12
|
+
},
|
|
13
|
+
getAllowedWebViewHosts() {
|
|
14
|
+
return ${formatAllowedHosts(allowedHosts)}
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
try{
|
|
18
|
+
${injectedJavaScriptBeforeContentLoaded}
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
// The user defined code has crashed
|
|
22
|
+
}
|
|
23
|
+
`;
|
|
24
|
+
//# sourceMappingURL=getInjectedJavaScriptBeforeContentLoaded.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["getInjectedJavaScriptBeforeContentLoaded.ts"],"names":["formatAllowedHosts","DATADOG_MESSAGE_PREFIX","getInjectedJavaScriptBeforeContentLoaded","allowedHosts","injectedJavaScriptBeforeContentLoaded"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA,SAASA,kBAAT,QAAmC,sBAAnC;AAEA,OAAO,MAAMC,sBAAsB,GAAG,WAA/B;AAEP,OAAO,MAAMC,wCAAwC,GAAG,CACpDC,YADoD,EAEpDC,qCAFoD,KAInD;AACL;AACA;AACA,iDAAiDH,sBAAuB;AACxE;AACA;AACA,iBAAiBD,kBAAkB,CAACG,YAAD,CAAe;AAClD;AACA;AACA;AACA,QAAQC,qCAAsC;AAC9C;AACA;AACA;AACA;AACA,GAnBO","sourcesContent":["/*\n * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.\n * This product includes software developed at Datadog (https://www.datadoghq.com/).\n * Copyright 2016-Present Datadog, Inc.\n */\nimport { formatAllowedHosts } from './formatAllowedHosts';\n\nexport const DATADOG_MESSAGE_PREFIX = '[DATADOG]';\n\nexport const getInjectedJavaScriptBeforeContentLoaded = (\n allowedHosts?: string[],\n injectedJavaScriptBeforeContentLoaded?: string\n): string =>\n `\n window.DatadogEventBridge = {\n send(msg) {\n window.ReactNativeWebView.postMessage(\"${DATADOG_MESSAGE_PREFIX} \" + msg)\n },\n getAllowedWebViewHosts() {\n return ${formatAllowedHosts(allowedHosts)}\n }\n };\n try{ \n ${injectedJavaScriptBeforeContentLoaded}\n }\n catch (error) {\n // The user defined code has crashed\n }\n `;\n"]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
|
|
5
|
+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
|
|
6
|
+
* Copyright 2016-Present Datadog, Inc.
|
|
7
|
+
*/
|
|
8
|
+
import { WebView as RNWebView } from 'react-native-webview';
|
|
9
|
+
import { NativeModules } from 'react-native';
|
|
10
|
+
import React, { useCallback } from 'react';
|
|
11
|
+
import { DATADOG_MESSAGE_PREFIX, getInjectedJavaScriptBeforeContentLoaded } from './__utils__/getInjectedJavaScriptBeforeContentLoaded';
|
|
12
|
+
export const WebView = props => {
|
|
13
|
+
const userDefinedOnMessage = props.onMessage;
|
|
14
|
+
const onMessage = useCallback(event => {
|
|
15
|
+
const message = event.nativeEvent.data;
|
|
16
|
+
|
|
17
|
+
if (message.startsWith(DATADOG_MESSAGE_PREFIX)) {
|
|
18
|
+
NativeModules.DdSdk.consumeWebviewEvent(message.substring(DATADOG_MESSAGE_PREFIX.length + 1));
|
|
19
|
+
} else {
|
|
20
|
+
userDefinedOnMessage === null || userDefinedOnMessage === void 0 ? void 0 : userDefinedOnMessage(event);
|
|
21
|
+
}
|
|
22
|
+
}, [userDefinedOnMessage]);
|
|
23
|
+
return /*#__PURE__*/React.createElement(RNWebView, _extends({}, props, {
|
|
24
|
+
onMessage: onMessage,
|
|
25
|
+
injectedJavaScriptBeforeContentLoaded: getInjectedJavaScriptBeforeContentLoaded(props.allowedHosts, props.injectedJavaScriptBeforeContentLoaded)
|
|
26
|
+
}));
|
|
27
|
+
};
|
|
28
|
+
export default WebView;
|
|
29
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["index.tsx"],"names":["WebView","RNWebView","NativeModules","React","useCallback","DATADOG_MESSAGE_PREFIX","getInjectedJavaScriptBeforeContentLoaded","props","userDefinedOnMessage","onMessage","event","message","nativeEvent","data","startsWith","DdSdk","consumeWebviewEvent","substring","length","allowedHosts","injectedJavaScriptBeforeContentLoaded"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AAEA,SAASA,OAAO,IAAIC,SAApB,QAAqC,sBAArC;AACA,SAASC,aAAT,QAA8B,cAA9B;AACA,OAAOC,KAAP,IAAgBC,WAAhB,QAAmC,OAAnC;AAEA,SACIC,sBADJ,EAEIC,wCAFJ,QAGO,sDAHP;AAUA,OAAO,MAAMN,OAAO,GAAIO,KAAD,IAAkB;AACrC,QAAMC,oBAAoB,GAAGD,KAAK,CAACE,SAAnC;AACA,QAAMA,SAAS,GAAGL,WAAW,CACxBM,KAAD,IAAgC;AAC5B,UAAMC,OAAO,GAAGD,KAAK,CAACE,WAAN,CAAkBC,IAAlC;;AACA,QAAIF,OAAO,CAACG,UAAR,CAAmBT,sBAAnB,CAAJ,EAAgD;AAC5CH,MAAAA,aAAa,CAACa,KAAd,CAAoBC,mBAApB,CACIL,OAAO,CAACM,SAAR,CAAkBZ,sBAAsB,CAACa,MAAvB,GAAgC,CAAlD,CADJ;AAGH,KAJD,MAIO;AACHV,MAAAA,oBAAoB,SAApB,IAAAA,oBAAoB,WAApB,YAAAA,oBAAoB,CAAGE,KAAH,CAApB;AACH;AACJ,GAVwB,EAWzB,CAACF,oBAAD,CAXyB,CAA7B;AAaA,sBACI,oBAAC,SAAD,eACQD,KADR;AAEI,IAAA,SAAS,EAAEE,SAFf;AAGI,IAAA,qCAAqC,EAAEH,wCAAwC,CAC3EC,KAAK,CAACY,YADqE,EAE3EZ,KAAK,CAACa,qCAFqE;AAHnF,KADJ;AAUH,CAzBM;AA2BP,eAAepB,OAAf","sourcesContent":["/*\n * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.\n * This product includes software developed at Datadog (https://www.datadoghq.com/).\n * Copyright 2016-Present Datadog, Inc.\n */\nimport type { WebViewMessageEvent, WebViewProps } from 'react-native-webview';\nimport { WebView as RNWebView } from 'react-native-webview';\nimport { NativeModules } from 'react-native';\nimport React, { useCallback } from 'react';\n\nimport {\n DATADOG_MESSAGE_PREFIX,\n getInjectedJavaScriptBeforeContentLoaded\n} from './__utils__/getInjectedJavaScriptBeforeContentLoaded';\n\ntype Props = WebViewProps & {\n allowedHosts?: string[];\n injectedJavaScriptBeforeContentLoaded?: string;\n};\n\nexport const WebView = (props: Props) => {\n const userDefinedOnMessage = props.onMessage;\n const onMessage = useCallback(\n (event: WebViewMessageEvent) => {\n const message = event.nativeEvent.data;\n if (message.startsWith(DATADOG_MESSAGE_PREFIX)) {\n NativeModules.DdSdk.consumeWebviewEvent(\n message.substring(DATADOG_MESSAGE_PREFIX.length + 1)\n );\n } else {\n userDefinedOnMessage?.(event);\n }\n },\n [userDefinedOnMessage]\n );\n return (\n <RNWebView\n {...props}\n onMessage={onMessage}\n injectedJavaScriptBeforeContentLoaded={getInjectedJavaScriptBeforeContentLoaded(\n props.allowedHosts,\n props.injectedJavaScriptBeforeContentLoaded\n )}\n />\n );\n};\n\nexport default WebView;\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function formatAllowedHosts(allowedHosts?: string[]): string;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { WebViewProps } from 'react-native-webview';
|
|
2
|
+
declare type Props = WebViewProps & {
|
|
3
|
+
allowedHosts?: string[];
|
|
4
|
+
injectedJavaScriptBeforeContentLoaded?: string;
|
|
5
|
+
};
|
|
6
|
+
export declare const WebView: (props: Props) => JSX.Element;
|
|
7
|
+
export default WebView;
|
package/package.json
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@datadog/mobile-react-native-webview",
|
|
3
|
+
"version": "1.5.0",
|
|
4
|
+
"description": "A client-side React Native module to interact with react-native-webview and Datadog",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"datadog",
|
|
7
|
+
"react-native",
|
|
8
|
+
"ios",
|
|
9
|
+
"android",
|
|
10
|
+
"react-native-webview",
|
|
11
|
+
"webview"
|
|
12
|
+
],
|
|
13
|
+
"author": "Datadog (https://github.com/DataDog)",
|
|
14
|
+
"homepage": "https://github.com/DataDog/dd-sdk-reactnative/packages/react-native-webview#readme",
|
|
15
|
+
"repository": {
|
|
16
|
+
"url": "https://github.com/DataDog/dd-sdk-reactnative",
|
|
17
|
+
"directory": "packages/react-native-webview"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/DataDog/dd-sdk-reactnative/issues"
|
|
21
|
+
},
|
|
22
|
+
"license": "Apache-2.0",
|
|
23
|
+
"main": "lib/commonjs/index",
|
|
24
|
+
"files": [
|
|
25
|
+
"src",
|
|
26
|
+
"lib"
|
|
27
|
+
],
|
|
28
|
+
"types": "lib/typescript/index.d.ts",
|
|
29
|
+
"react-native": "src/index",
|
|
30
|
+
"source": "src/index",
|
|
31
|
+
"module": "lib/module/index",
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"test": "jest",
|
|
37
|
+
"lint": "eslint .",
|
|
38
|
+
"prepare": "rm -rf lib && yarn bob build"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@testing-library/react-native": "7.0.2"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@datadog/mobile-react-native": ">=1.5.0",
|
|
45
|
+
"react": ">=16.13.1",
|
|
46
|
+
"react-native": ">=0.63.4 <1.0",
|
|
47
|
+
"react-native-webview": "11.x.x"
|
|
48
|
+
},
|
|
49
|
+
"jest": {
|
|
50
|
+
"preset": "react-native",
|
|
51
|
+
"modulePathIgnorePatterns": [
|
|
52
|
+
"<rootDir>/lib/"
|
|
53
|
+
],
|
|
54
|
+
"testPathIgnorePatterns": [
|
|
55
|
+
"/__utils__/"
|
|
56
|
+
],
|
|
57
|
+
"transformIgnorePatterns": []
|
|
58
|
+
},
|
|
59
|
+
"react-native-builder-bob": {
|
|
60
|
+
"source": "src",
|
|
61
|
+
"output": "lib",
|
|
62
|
+
"targets": [
|
|
63
|
+
"commonjs",
|
|
64
|
+
"module",
|
|
65
|
+
[
|
|
66
|
+
"typescript",
|
|
67
|
+
{
|
|
68
|
+
"tsc": "./../../node_modules/.bin/tsc"
|
|
69
|
+
}
|
|
70
|
+
]
|
|
71
|
+
]
|
|
72
|
+
},
|
|
73
|
+
"gitHead": "04d48f34c3fe6f2d793cf4157eff5314253afd87"
|
|
74
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
|
|
3
|
+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
|
|
4
|
+
* Copyright 2016-Present Datadog, Inc.
|
|
5
|
+
*/
|
|
6
|
+
import { fireEvent, render } from '@testing-library/react-native';
|
|
7
|
+
import { NativeModules } from 'react-native';
|
|
8
|
+
import React from 'react';
|
|
9
|
+
|
|
10
|
+
import { DATADOG_MESSAGE_PREFIX } from '../__utils__/getInjectedJavaScriptBeforeContentLoaded';
|
|
11
|
+
import { WebView } from '../index';
|
|
12
|
+
|
|
13
|
+
describe('WebView', () => {
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
jest.clearAllMocks();
|
|
16
|
+
});
|
|
17
|
+
const DdMessage = 'custom datadog event';
|
|
18
|
+
const datadogEvent = {
|
|
19
|
+
nativeEvent: {
|
|
20
|
+
data: `${DATADOG_MESSAGE_PREFIX} ${DdMessage}`
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
const userDefinedEvent = {
|
|
24
|
+
nativeEvent: {
|
|
25
|
+
data: 'custom user-defined message'
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
it('calls provided onMessage props', async () => {
|
|
29
|
+
const onMessage = jest.fn();
|
|
30
|
+
const { findByTestId } = render(
|
|
31
|
+
<WebView onMessage={onMessage} testID="webView" allowedHosts={[]} />
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const webView = await findByTestId('webView');
|
|
35
|
+
|
|
36
|
+
fireEvent(webView, 'message', userDefinedEvent);
|
|
37
|
+
expect(onMessage).toHaveBeenCalledWith(userDefinedEvent);
|
|
38
|
+
|
|
39
|
+
fireEvent(webView, 'message', datadogEvent);
|
|
40
|
+
expect(onMessage).toHaveBeenCalledTimes(1);
|
|
41
|
+
expect(onMessage).not.toHaveBeenCalledWith(datadogEvent);
|
|
42
|
+
});
|
|
43
|
+
it('calls consumeWebviewEvent with Datadog logs', async () => {
|
|
44
|
+
const { findByTestId } = render(
|
|
45
|
+
<WebView testID="webView" allowedHosts={[]} />
|
|
46
|
+
);
|
|
47
|
+
const webView = await findByTestId('webView');
|
|
48
|
+
fireEvent(webView, 'message', datadogEvent);
|
|
49
|
+
|
|
50
|
+
expect(NativeModules.DdSdk.consumeWebviewEvent).toHaveBeenCalledWith(
|
|
51
|
+
DdMessage
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
|
|
3
|
+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
|
|
4
|
+
* Copyright 2016-Present Datadog, Inc.
|
|
5
|
+
*/
|
|
6
|
+
import { render } from '@testing-library/react-native';
|
|
7
|
+
import React from 'react';
|
|
8
|
+
|
|
9
|
+
import { WebView } from '../index';
|
|
10
|
+
|
|
11
|
+
jest.mock('react-native-webview', () => {
|
|
12
|
+
return {
|
|
13
|
+
WebView: jest.fn(props => {
|
|
14
|
+
// eslint-disable-next-line no-eval
|
|
15
|
+
eval(props.injectedJavaScriptBeforeContentLoaded);
|
|
16
|
+
return null;
|
|
17
|
+
})
|
|
18
|
+
};
|
|
19
|
+
});
|
|
20
|
+
const callfunction = jest.fn();
|
|
21
|
+
|
|
22
|
+
describe('Webview', () => {
|
|
23
|
+
it('should pass injectedJavaScriptBeforeContentLoaded prop to WebView component', () => {
|
|
24
|
+
const injectedJavaScriptBeforeContentLoaded = 'callfunction()';
|
|
25
|
+
const allowedHosts = ['example.com', 'localhost'];
|
|
26
|
+
render(
|
|
27
|
+
<WebView
|
|
28
|
+
allowedHosts={allowedHosts}
|
|
29
|
+
injectedJavaScriptBeforeContentLoaded={
|
|
30
|
+
injectedJavaScriptBeforeContentLoaded
|
|
31
|
+
}
|
|
32
|
+
/>
|
|
33
|
+
);
|
|
34
|
+
expect(callfunction).toHaveBeenCalled();
|
|
35
|
+
});
|
|
36
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
|
|
3
|
+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
|
|
4
|
+
* Copyright 2016-Present Datadog, Inc.
|
|
5
|
+
*/
|
|
6
|
+
import { render } from '@testing-library/react-native';
|
|
7
|
+
import { WebView as RNWebView } from 'react-native-webview';
|
|
8
|
+
import React from 'react';
|
|
9
|
+
|
|
10
|
+
import { WebView } from '../index';
|
|
11
|
+
|
|
12
|
+
jest.mock('react-native-webview', () => {
|
|
13
|
+
return {
|
|
14
|
+
WebView: jest.fn()
|
|
15
|
+
};
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe('WebView performance', () => {
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
jest.clearAllMocks();
|
|
21
|
+
});
|
|
22
|
+
it('should update the onMessage prop of the RNWebView component', () => {
|
|
23
|
+
const onMessage = jest.fn();
|
|
24
|
+
const allowedHosts = ['localhost', 'example.com'];
|
|
25
|
+
const { rerender } = render(
|
|
26
|
+
<WebView onMessage={onMessage} allowedHosts={allowedHosts} />
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
// Render the component with a new onMessage prop
|
|
30
|
+
rerender(<WebView onMessage={onMessage} allowedHosts={allowedHosts} />);
|
|
31
|
+
|
|
32
|
+
// Render the component with a new onMessage prop
|
|
33
|
+
const newOnMessage = jest.fn();
|
|
34
|
+
rerender(
|
|
35
|
+
<WebView onMessage={newOnMessage} allowedHosts={allowedHosts} />
|
|
36
|
+
);
|
|
37
|
+
const mockedWebView = jest.mocked(RNWebView);
|
|
38
|
+
// Verify that the onMessage prop of the RNWebView component has changed
|
|
39
|
+
expect(mockedWebView.mock.calls[0][0].onMessage).toBe(
|
|
40
|
+
mockedWebView.mock.calls[1][0].onMessage
|
|
41
|
+
);
|
|
42
|
+
expect(mockedWebView.mock.calls[0][0].onMessage).not.toBe(
|
|
43
|
+
mockedWebView.mock.calls[2][0].onMessage
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
|
|
3
|
+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
|
|
4
|
+
* Copyright 2016-Present Datadog, Inc.
|
|
5
|
+
*/
|
|
6
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
7
|
+
import { NativeModules } from 'react-native';
|
|
8
|
+
|
|
9
|
+
import { formatAllowedHosts } from '../__utils__/formatAllowedHosts';
|
|
10
|
+
|
|
11
|
+
describe('Format allowed hosts', () => {
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
jest.clearAllMocks();
|
|
14
|
+
});
|
|
15
|
+
it('returns the host in expected format', () => {
|
|
16
|
+
const allowedHosts = ['host1.com', 'example.fr', 'api.com'];
|
|
17
|
+
expect(formatAllowedHosts(allowedHosts)).toBe(
|
|
18
|
+
'\'["host1.com","example.fr","api.com"]\''
|
|
19
|
+
);
|
|
20
|
+
});
|
|
21
|
+
it('returns an empty arrary when the host is a BigInt', () => {
|
|
22
|
+
const allowedHosts = BigInt(1240);
|
|
23
|
+
// @ts-ignore
|
|
24
|
+
const result = formatAllowedHosts(allowedHosts);
|
|
25
|
+
expect(NativeModules.DdSdk.telemetryError).toHaveBeenCalled();
|
|
26
|
+
expect(result).toBe("'[]'");
|
|
27
|
+
});
|
|
28
|
+
it('returns an empty array when the host is a circular reference', () => {
|
|
29
|
+
type circularReference = {
|
|
30
|
+
host: string;
|
|
31
|
+
name?: circularReference | string;
|
|
32
|
+
};
|
|
33
|
+
const allowedHosts: circularReference = { host: 'value', name: '' };
|
|
34
|
+
allowedHosts.name = allowedHosts;
|
|
35
|
+
// @ts-ignore
|
|
36
|
+
const result = formatAllowedHosts(allowedHosts);
|
|
37
|
+
expect(NativeModules.DdSdk.telemetryError).toHaveBeenCalled();
|
|
38
|
+
expect(result).toBe("'[]'");
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
|
|
3
|
+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
|
|
4
|
+
* Copyright 2016-Present Datadog, Inc.
|
|
5
|
+
*/
|
|
6
|
+
/* eslint-disable no-eval */
|
|
7
|
+
import {
|
|
8
|
+
getInjectedJavaScriptBeforeContentLoaded,
|
|
9
|
+
DATADOG_MESSAGE_PREFIX
|
|
10
|
+
} from '../__utils__/getInjectedJavaScriptBeforeContentLoaded';
|
|
11
|
+
|
|
12
|
+
describe('getInjectedJavaScriptBeforeContentLoaded', () => {
|
|
13
|
+
const DdMessage = 'custom datadog event';
|
|
14
|
+
let allowedHosts: string[];
|
|
15
|
+
let script: string;
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
jest.clearAllMocks();
|
|
18
|
+
Object.defineProperty(window, 'ReactNativeWebView', {
|
|
19
|
+
value: {
|
|
20
|
+
postMessage: jest.fn()
|
|
21
|
+
},
|
|
22
|
+
writable: true
|
|
23
|
+
});
|
|
24
|
+
delete (window as any).DatadogEventBridge;
|
|
25
|
+
});
|
|
26
|
+
it('posts the message and returns allowed webview hosts', () => {
|
|
27
|
+
allowedHosts = ['example.com', 'localhost'];
|
|
28
|
+
script = getInjectedJavaScriptBeforeContentLoaded(allowedHosts);
|
|
29
|
+
eval(script);
|
|
30
|
+
|
|
31
|
+
// Posting the message
|
|
32
|
+
(window as any).DatadogEventBridge.send(DdMessage);
|
|
33
|
+
expect((window as any).ReactNativeWebView.postMessage).toBeCalledWith(
|
|
34
|
+
`${DATADOG_MESSAGE_PREFIX} ${DdMessage}`
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
// Getting the allowed hosts
|
|
38
|
+
expect(
|
|
39
|
+
(window as any).DatadogEventBridge.getAllowedWebViewHosts()
|
|
40
|
+
).toBe('["example.com","localhost"]');
|
|
41
|
+
});
|
|
42
|
+
it('should return an empty array for getAllowedWebViewHosts if no hosts are given', () => {
|
|
43
|
+
allowedHosts = [];
|
|
44
|
+
script = getInjectedJavaScriptBeforeContentLoaded(allowedHosts);
|
|
45
|
+
eval(script);
|
|
46
|
+
|
|
47
|
+
expect(
|
|
48
|
+
(window as any).DatadogEventBridge.getAllowedWebViewHosts()
|
|
49
|
+
).toBe('[]');
|
|
50
|
+
});
|
|
51
|
+
it('uses our injected javascript and the user provided implementation', () => {
|
|
52
|
+
allowedHosts = ['example.com', 'localhost'];
|
|
53
|
+
const callfunction = jest.fn();
|
|
54
|
+
const injectedJavaScriptBeforeContentLoaded = 'callfunction()';
|
|
55
|
+
script = getInjectedJavaScriptBeforeContentLoaded(
|
|
56
|
+
allowedHosts,
|
|
57
|
+
injectedJavaScriptBeforeContentLoaded
|
|
58
|
+
);
|
|
59
|
+
eval(script);
|
|
60
|
+
expect(callfunction).toHaveBeenCalled();
|
|
61
|
+
// Posting the message
|
|
62
|
+
(window as any).DatadogEventBridge.send(DdMessage);
|
|
63
|
+
expect((window as any).ReactNativeWebView.postMessage).toBeCalledWith(
|
|
64
|
+
`${DATADOG_MESSAGE_PREFIX} ${DdMessage}`
|
|
65
|
+
);
|
|
66
|
+
});
|
|
67
|
+
it('executes our injected javascript when the user provided implementation throws an error', () => {
|
|
68
|
+
allowedHosts = ['example.com', 'localhost'];
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
70
|
+
const callfunction = jest.fn().mockImplementation(() => {
|
|
71
|
+
throw new Error('The user functions throws an error');
|
|
72
|
+
});
|
|
73
|
+
const injectedJavaScriptBeforeContentLoaded = 'callfunction()';
|
|
74
|
+
script = getInjectedJavaScriptBeforeContentLoaded(
|
|
75
|
+
allowedHosts,
|
|
76
|
+
injectedJavaScriptBeforeContentLoaded
|
|
77
|
+
);
|
|
78
|
+
eval(script);
|
|
79
|
+
expect(() => eval(injectedJavaScriptBeforeContentLoaded)).toThrow();
|
|
80
|
+
// Posting the message
|
|
81
|
+
(window as any).DatadogEventBridge.send(DdMessage);
|
|
82
|
+
expect((window as any).ReactNativeWebView.postMessage).toBeCalledWith(
|
|
83
|
+
`${DATADOG_MESSAGE_PREFIX} ${DdMessage}`
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
|
|
3
|
+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
|
|
4
|
+
* Copyright 2016-Present Datadog, Inc.
|
|
5
|
+
*/
|
|
6
|
+
import { NativeModules } from 'react-native';
|
|
7
|
+
|
|
8
|
+
export function formatAllowedHosts(allowedHosts?: string[]): string {
|
|
9
|
+
try {
|
|
10
|
+
return `'${JSON.stringify(allowedHosts)}'`;
|
|
11
|
+
} catch (e: any) {
|
|
12
|
+
if (NativeModules.DdSdk && NativeModules.DdSdk.telemetryError) {
|
|
13
|
+
NativeModules.DdSdk.telemetryError(
|
|
14
|
+
getErrorMessage(e),
|
|
15
|
+
getErrorStackTrace(e),
|
|
16
|
+
'AllowedHostsError'
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
return "'[]'";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The next section is copied from packages/core/src/errorUtils
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
const EMPTY_MESSAGE = 'Unknown Error';
|
|
28
|
+
const EMPTY_STACK_TRACE = '';
|
|
29
|
+
|
|
30
|
+
const getErrorMessage = (error: any | undefined): string => {
|
|
31
|
+
let message = EMPTY_MESSAGE;
|
|
32
|
+
if (error === undefined || error === null) {
|
|
33
|
+
message = EMPTY_MESSAGE;
|
|
34
|
+
} else if (typeof error === 'object' && 'message' in error) {
|
|
35
|
+
message = String(error.message);
|
|
36
|
+
} else {
|
|
37
|
+
message = String(error);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return message;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const getErrorStackTrace = (error: any | undefined): string => {
|
|
44
|
+
let stack = EMPTY_STACK_TRACE;
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
if (error === undefined || error === null) {
|
|
48
|
+
stack = EMPTY_STACK_TRACE;
|
|
49
|
+
} else if (typeof error === 'string') {
|
|
50
|
+
stack = EMPTY_STACK_TRACE;
|
|
51
|
+
} else if (typeof error === 'object') {
|
|
52
|
+
if ('stacktrace' in error) {
|
|
53
|
+
stack = String(error.stacktrace);
|
|
54
|
+
} else if ('stack' in error) {
|
|
55
|
+
stack = String(error.stack);
|
|
56
|
+
} else if ('componentStack' in error) {
|
|
57
|
+
stack = String(error.componentStack);
|
|
58
|
+
} else if (
|
|
59
|
+
'sourceURL' in error &&
|
|
60
|
+
'line' in error &&
|
|
61
|
+
'column' in error
|
|
62
|
+
) {
|
|
63
|
+
stack = `at ${error.sourceURL}:${error.line}:${error.column}`;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
} catch (e) {
|
|
67
|
+
// Do nothing
|
|
68
|
+
}
|
|
69
|
+
return stack;
|
|
70
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
|
|
3
|
+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
|
|
4
|
+
* Copyright 2016-Present Datadog, Inc.
|
|
5
|
+
*/
|
|
6
|
+
import { formatAllowedHosts } from './formatAllowedHosts';
|
|
7
|
+
|
|
8
|
+
export const DATADOG_MESSAGE_PREFIX = '[DATADOG]';
|
|
9
|
+
|
|
10
|
+
export const getInjectedJavaScriptBeforeContentLoaded = (
|
|
11
|
+
allowedHosts?: string[],
|
|
12
|
+
injectedJavaScriptBeforeContentLoaded?: string
|
|
13
|
+
): string =>
|
|
14
|
+
`
|
|
15
|
+
window.DatadogEventBridge = {
|
|
16
|
+
send(msg) {
|
|
17
|
+
window.ReactNativeWebView.postMessage("${DATADOG_MESSAGE_PREFIX} " + msg)
|
|
18
|
+
},
|
|
19
|
+
getAllowedWebViewHosts() {
|
|
20
|
+
return ${formatAllowedHosts(allowedHosts)}
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
try{
|
|
24
|
+
${injectedJavaScriptBeforeContentLoaded}
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
// The user defined code has crashed
|
|
28
|
+
}
|
|
29
|
+
`;
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
|
|
3
|
+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
|
|
4
|
+
* Copyright 2016-Present Datadog, Inc.
|
|
5
|
+
*/
|
|
6
|
+
import type { WebViewMessageEvent, WebViewProps } from 'react-native-webview';
|
|
7
|
+
import { WebView as RNWebView } from 'react-native-webview';
|
|
8
|
+
import { NativeModules } from 'react-native';
|
|
9
|
+
import React, { useCallback } from 'react';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
DATADOG_MESSAGE_PREFIX,
|
|
13
|
+
getInjectedJavaScriptBeforeContentLoaded
|
|
14
|
+
} from './__utils__/getInjectedJavaScriptBeforeContentLoaded';
|
|
15
|
+
|
|
16
|
+
type Props = WebViewProps & {
|
|
17
|
+
allowedHosts?: string[];
|
|
18
|
+
injectedJavaScriptBeforeContentLoaded?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const WebView = (props: Props) => {
|
|
22
|
+
const userDefinedOnMessage = props.onMessage;
|
|
23
|
+
const onMessage = useCallback(
|
|
24
|
+
(event: WebViewMessageEvent) => {
|
|
25
|
+
const message = event.nativeEvent.data;
|
|
26
|
+
if (message.startsWith(DATADOG_MESSAGE_PREFIX)) {
|
|
27
|
+
NativeModules.DdSdk.consumeWebviewEvent(
|
|
28
|
+
message.substring(DATADOG_MESSAGE_PREFIX.length + 1)
|
|
29
|
+
);
|
|
30
|
+
} else {
|
|
31
|
+
userDefinedOnMessage?.(event);
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
[userDefinedOnMessage]
|
|
35
|
+
);
|
|
36
|
+
return (
|
|
37
|
+
<RNWebView
|
|
38
|
+
{...props}
|
|
39
|
+
onMessage={onMessage}
|
|
40
|
+
injectedJavaScriptBeforeContentLoaded={getInjectedJavaScriptBeforeContentLoaded(
|
|
41
|
+
props.allowedHosts,
|
|
42
|
+
props.injectedJavaScriptBeforeContentLoaded
|
|
43
|
+
)}
|
|
44
|
+
/>
|
|
45
|
+
);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export default WebView;
|