@datadog/mobile-react-native-webview 2.13.2 → 2.14.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.
Files changed (37) hide show
  1. package/android/build.gradle +8 -1
  2. package/android/src/newarch/com/datadog/reactnative/webview/DdSdkReactNativeWebViewManager.kt +125 -0
  3. package/android/src/newarch/com/datadog/reactnative/webview/DdSdkReactNativeWebViewPackage.kt +4 -4
  4. package/android/src/oldarch/com/datadog/reactnative/webview/DdSdkReactNativeWebViewManager.kt +5 -19
  5. package/android/src/test/kotlin/{com → main/com}/datadog/reactnative/tools/unit/GenericAssert.kt +1 -1
  6. package/android/src/test/{kotlin/com/datadog/reactnative/webview → webview}/DatadogWebViewTest.kt +2 -7
  7. package/android/src/testNewArch/kotlin/com/datadog/reactnative/webview/DatadogWebViewTest.kt +122 -0
  8. package/lib/commonjs/index.js +8 -10
  9. package/lib/commonjs/index.js.map +1 -1
  10. package/lib/commonjs/specs/NativeDdWebView.js +1 -2
  11. package/lib/commonjs/specs/NativeDdWebView.js.map +1 -1
  12. package/lib/commonjs/utils/webview-js-utils.js +37 -53
  13. package/lib/commonjs/utils/webview-js-utils.js.map +1 -1
  14. package/lib/module/index.js +10 -12
  15. package/lib/module/index.js.map +1 -1
  16. package/lib/module/specs/NativeDdWebView.js +1 -2
  17. package/lib/module/specs/NativeDdWebView.js.map +1 -1
  18. package/lib/module/utils/webview-js-utils.js +34 -50
  19. package/lib/module/utils/webview-js-utils.js.map +1 -1
  20. package/lib/typescript/index.d.ts.map +1 -1
  21. package/lib/typescript/specs/NativeDdWebView.d.ts +1 -1
  22. package/lib/typescript/specs/NativeDdWebView.d.ts.map +1 -1
  23. package/lib/typescript/utils/webview-js-utils.d.ts +4 -9
  24. package/lib/typescript/utils/webview-js-utils.d.ts.map +1 -1
  25. package/package.json +2 -2
  26. package/src/__tests__/WebviewDatadogInjectedJS.test.tsx +27 -61
  27. package/src/__tests__/webview-js-utils.test.ts +151 -6
  28. package/src/index.tsx +17 -20
  29. package/src/specs/NativeDdWebView.ts +3 -5
  30. package/src/utils/webview-js-utils.ts +49 -57
  31. package/lib/commonjs/utils/env-utils.js +0 -17
  32. package/lib/commonjs/utils/env-utils.js.map +0 -1
  33. package/lib/module/utils/env-utils.js +0 -10
  34. package/lib/module/utils/env-utils.js.map +0 -1
  35. package/lib/typescript/utils/env-utils.d.ts +0 -2
  36. package/lib/typescript/utils/env-utils.d.ts.map +0 -1
  37. package/src/utils/env-utils.ts +0 -9
@@ -29,70 +29,59 @@ export type DatadogMessageFormat = {
29
29
  };
30
30
 
31
31
  /**
32
- * Wraps the given JS Code in a try and catch block.
32
+ * Wraps the given JS Code in a try and catch block, including a
33
+ * comment with the given allowedHosts in JSON format
33
34
  * @param javascriptCode The JS Code to wrap in a try and catch block.
34
35
  * @returns the wrapped JS code.
35
36
  */
36
- export const wrapJsCodeInTryAndCatch = (
37
- javascriptCode?: string
38
- ): string | undefined =>
39
- javascriptCode
40
- ? `
41
- try{
42
- ${javascriptCode}
37
+ export function wrapJsCodeWithAllowedHosts(
38
+ javascriptCode?: string,
39
+ allowedHosts?: string[]
40
+ ): string | undefined {
41
+ let jsCode = '';
42
+ const hosts = formatAllowedHosts(allowedHosts);
43
+ if (hosts) {
44
+ jsCode = `// #allowedHosts=${JSON.stringify(allowedHosts)}\n`;
43
45
  }
44
- catch (error) {
45
- const errorMsg = error instanceof Error ? error.message : String(error);
46
- window.ReactNativeWebView.postMessage(JSON.stringify({
47
- source: 'DATADOG',
48
- type: 'ERROR',
49
- message: errorMsg
50
- }));
51
- true;
52
- }`
46
+
47
+ return javascriptCode
48
+ ? `${jsCode}\n${wrapJsCodeInTryAndCatch(javascriptCode)}`
49
+ : jsCode.trim().length > 0
50
+ ? jsCode
53
51
  : undefined;
52
+ }
54
53
 
55
- /**
56
- * Legacy JS code for bridging the WebView events to DataDog native SDKs for consumption.
57
- * @param allowedHosts The list of allowed hosts.
58
- * @param customJavaScriptCode Custom user JS code to inject along with the Datadog bridging logic.
59
- * @returns The JS code block as a string.
60
- */
61
- export const getWebViewEventBridgingJS = (
62
- allowedHosts?: string[],
63
- customJavaScriptCode?: string
64
- ): string =>
65
- `
66
- window.DatadogEventBridge = {
67
- send(msg) {
68
- window.ReactNativeWebView.postMessage(JSON.stringify({
69
- source: 'DATADOG',
70
- type: 'NATIVE_EVENT',
71
- message: msg
72
- }));
73
- true;
74
- },
75
- getAllowedWebViewHosts() {
76
- return ${formatAllowedHosts(allowedHosts)}
77
- }
78
- };
79
- try{
80
- ${customJavaScriptCode}
81
- }
82
- catch (error) {
83
- const errorMsg = error instanceof Error ? error.message : String(error);
84
- window.ReactNativeWebView.postMessage(JSON.stringify({
85
- source: 'DATADOG',
86
- type: 'ERROR',
87
- message: errorMsg
88
- }));
89
- true;
90
- }
91
- `;
54
+ export function wrapJsCodeInTryAndCatch(
55
+ javascriptCode: string | undefined
56
+ ): string | undefined {
57
+ return javascriptCode
58
+ ? `try{
59
+ ${javascriptCode}
60
+ }
61
+ catch (error) {
62
+ const errorMsg = error instanceof Error ? error.message : String(error);
63
+ window.ReactNativeWebView.postMessage(JSON.stringify({
64
+ source: 'DATADOG',
65
+ type: 'ERROR',
66
+ message: errorMsg
67
+ }));
68
+ true;
69
+ }`
70
+ : undefined;
71
+ }
92
72
 
93
- function formatAllowedHosts(allowedHosts?: string[]): string {
73
+ function formatAllowedHosts(allowedHosts?: string[]): string | undefined {
94
74
  try {
95
- return `'${JSON.stringify(allowedHosts)}'`;
75
+ if (!allowedHosts) {
76
+ throw new Error('allowedHosts is undefined');
77
+ }
78
+ const jsonHosts = JSON.stringify(allowedHosts);
79
+ if (!jsonHosts) {
80
+ throw new Error(
81
+ "JSON.stringify returned 'undefined' for the given hosts"
82
+ );
83
+ }
84
+ return jsonHosts;
96
85
  } catch (e: any) {
97
86
  if (NativeDdSdk) {
98
87
  NativeDdSdk.telemetryError(
@@ -101,7 +90,10 @@ function formatAllowedHosts(allowedHosts?: string[]): string {
101
90
  'AllowedHostsError'
102
91
  );
103
92
  }
104
- return "'[]'";
93
+ console.warn(
94
+ `[@datadog/mobile-react-native-webview] Invalid 'allowedHosts' format: ${e}`
95
+ );
96
+ return undefined;
105
97
  }
106
98
  }
107
99
 
@@ -1,17 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.isNewArchitecture = void 0;
7
- /*
8
- * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
9
- * This product includes software developed at Datadog (https://www.datadoghq.com/).
10
- * Copyright 2016-Present Datadog, Inc.
11
- */
12
-
13
- const isNewArchitecture = () => {
14
- return global?.nativeFabricUIManager !== undefined;
15
- };
16
- exports.isNewArchitecture = isNewArchitecture;
17
- //# sourceMappingURL=env-utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["isNewArchitecture","global","nativeFabricUIManager","undefined","exports"],"sourceRoot":"../../../src","sources":["utils/env-utils.ts"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA;;AAEO,MAAMA,iBAAiB,GAAGA,CAAA,KAAe;EAC5C,OAAQC,MAAM,EAAUC,qBAAqB,KAAKC,SAAS;AAC/D,CAAC;AAACC,OAAA,CAAAJ,iBAAA,GAAAA,iBAAA","ignoreList":[]}
@@ -1,10 +0,0 @@
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
-
7
- export const isNewArchitecture = () => {
8
- return global?.nativeFabricUIManager !== undefined;
9
- };
10
- //# sourceMappingURL=env-utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["isNewArchitecture","global","nativeFabricUIManager","undefined"],"sourceRoot":"../../../src","sources":["utils/env-utils.ts"],"mappings":"AAAA;AACA;AACA;AACA;AACA;;AAEA,OAAO,MAAMA,iBAAiB,GAAGA,CAAA,KAAe;EAC5C,OAAQC,MAAM,EAAUC,qBAAqB,KAAKC,SAAS;AAC/D,CAAC","ignoreList":[]}
@@ -1,2 +0,0 @@
1
- export declare const isNewArchitecture: () => boolean;
2
- //# sourceMappingURL=env-utils.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"env-utils.d.ts","sourceRoot":"","sources":["../../../src/utils/env-utils.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,iBAAiB,QAAO,OAEpC,CAAC"}
@@ -1,9 +0,0 @@
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
-
7
- export const isNewArchitecture = (): boolean => {
8
- return (global as any)?.nativeFabricUIManager !== undefined;
9
- };