@datadog/mobile-react-native-webview 2.4.3 → 2.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.
Files changed (86) hide show
  1. package/DatadogSDKReactNativeWebView.podspec +46 -0
  2. package/android/build.gradle +249 -0
  3. package/android/detekt.yml +572 -0
  4. package/android/gradle.properties +5 -0
  5. package/android/src/main/AndroidManifest.xml +11 -0
  6. package/android/src/newarch/com/datadog/reactnative/webview/DdSdkReactNativeWebViewPackage.kt +35 -0
  7. package/android/src/oldarch/com/datadog/reactnative/webview/DdSdkReactNativeWebViewManager.kt +112 -0
  8. package/android/src/oldarch/com/datadog/reactnative/webview/DdSdkReactNativeWebViewPackage.kt +36 -0
  9. package/android/src/test/kotlin/com/datadog/reactnative/tools/unit/GenericAssert.kt +18 -0
  10. package/android/src/test/kotlin/com/datadog/reactnative/webview/DatadogWebViewTest.kt +126 -0
  11. package/android/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker +1 -0
  12. package/ios/DatadogSDKReactNativeWebView.xcodeproj/project.pbxproj +272 -0
  13. package/ios/DatadogSDKReactNativeWebView.xcodeproj/project.xcworkspace/contents.xcworkspacedata +4 -0
  14. package/ios/Sources/DatadogSDKReactNativeWebView.h +12 -0
  15. package/ios/Sources/RCTDatadogWebView.h +21 -0
  16. package/ios/Sources/RCTDatadogWebView.mm +53 -0
  17. package/ios/Sources/RCTDatadogWebViewManager.h +10 -0
  18. package/ios/Sources/RCTDatadogWebViewManager.mm +84 -0
  19. package/ios/Sources/RCTDatadogWebViewTracking.swift +67 -0
  20. package/lib/commonjs/ext-specs/NativeDdLogs.js +19 -0
  21. package/lib/commonjs/ext-specs/NativeDdLogs.js.map +1 -0
  22. package/lib/commonjs/ext-specs/NativeDdSdk.js.map +1 -0
  23. package/lib/commonjs/index.js +40 -7
  24. package/lib/commonjs/index.js.map +1 -1
  25. package/lib/commonjs/specs/NativeDdWebView.js +16 -0
  26. package/lib/commonjs/specs/NativeDdWebView.js.map +1 -0
  27. package/lib/commonjs/utils/env-utils.js +17 -0
  28. package/lib/commonjs/utils/env-utils.js.map +1 -0
  29. package/lib/commonjs/utils/webview-js-utils.js +124 -0
  30. package/lib/commonjs/utils/webview-js-utils.js.map +1 -0
  31. package/lib/module/ext-specs/NativeDdLogs.js +17 -0
  32. package/lib/module/ext-specs/NativeDdLogs.js.map +1 -0
  33. package/lib/module/ext-specs/NativeDdSdk.js.map +1 -0
  34. package/lib/module/index.js +39 -7
  35. package/lib/module/index.js.map +1 -1
  36. package/lib/module/specs/NativeDdWebView.js +11 -0
  37. package/lib/module/specs/NativeDdWebView.js.map +1 -0
  38. package/lib/module/utils/env-utils.js +10 -0
  39. package/lib/module/utils/env-utils.js.map +1 -0
  40. package/lib/module/utils/webview-js-utils.js +115 -0
  41. package/lib/module/utils/webview-js-utils.js.map +1 -0
  42. package/lib/typescript/ext-specs/NativeDdLogs.d.ts +16 -0
  43. package/lib/typescript/ext-specs/NativeDdLogs.d.ts.map +1 -0
  44. package/lib/typescript/ext-specs/NativeDdSdk.d.ts.map +1 -0
  45. package/lib/typescript/index.d.ts +20 -0
  46. package/lib/typescript/index.d.ts.map +1 -1
  47. package/lib/typescript/specs/NativeDdWebView.d.ts +4 -0
  48. package/lib/typescript/specs/NativeDdWebView.d.ts.map +1 -0
  49. package/lib/typescript/utils/env-utils.d.ts +2 -0
  50. package/lib/typescript/utils/env-utils.d.ts.map +1 -0
  51. package/lib/typescript/utils/webview-js-utils.d.ts +34 -0
  52. package/lib/typescript/utils/webview-js-utils.d.ts.map +1 -0
  53. package/package.json +22 -4
  54. package/src/__tests__/WebviewDatadog.test.tsx +39 -43
  55. package/src/__tests__/WebviewDatadogInjectedJS.test.tsx +200 -1
  56. package/src/__tests__/WebviewDatadogPerformance.test.tsx +12 -0
  57. package/src/__tests__/__utils__/string-utils.ts +13 -0
  58. package/src/__tests__/webview-js-utils.test.ts +51 -0
  59. package/src/ext-specs/NativeDdLogs.ts +25 -0
  60. package/src/{NativeDdSdk.ts → ext-specs/NativeDdSdk.ts} +1 -0
  61. package/src/index.tsx +70 -13
  62. package/src/specs/NativeDdWebView.ts +16 -0
  63. package/src/utils/env-utils.ts +9 -0
  64. package/src/utils/webview-js-utils.ts +150 -0
  65. package/lib/commonjs/NativeDdSdk.js.map +0 -1
  66. package/lib/commonjs/__utils__/formatAllowedHosts.js +0 -65
  67. package/lib/commonjs/__utils__/formatAllowedHosts.js.map +0 -1
  68. package/lib/commonjs/__utils__/getInjectedJavaScriptBeforeContentLoaded.js +0 -32
  69. package/lib/commonjs/__utils__/getInjectedJavaScriptBeforeContentLoaded.js.map +0 -1
  70. package/lib/module/NativeDdSdk.js.map +0 -1
  71. package/lib/module/__utils__/formatAllowedHosts.js +0 -58
  72. package/lib/module/__utils__/formatAllowedHosts.js.map +0 -1
  73. package/lib/module/__utils__/getInjectedJavaScriptBeforeContentLoaded.js +0 -24
  74. package/lib/module/__utils__/getInjectedJavaScriptBeforeContentLoaded.js.map +0 -1
  75. package/lib/typescript/NativeDdSdk.d.ts.map +0 -1
  76. package/lib/typescript/__utils__/formatAllowedHosts.d.ts +0 -2
  77. package/lib/typescript/__utils__/formatAllowedHosts.d.ts.map +0 -1
  78. package/lib/typescript/__utils__/getInjectedJavaScriptBeforeContentLoaded.d.ts +0 -3
  79. package/lib/typescript/__utils__/getInjectedJavaScriptBeforeContentLoaded.d.ts.map +0 -1
  80. package/src/__tests__/formatAllowedHosts.test.ts +0 -40
  81. package/src/__tests__/getInjectedJavaScriptBeforeContentLoaded.test.ts +0 -86
  82. package/src/__utils__/formatAllowedHosts.ts +0 -70
  83. package/src/__utils__/getInjectedJavaScriptBeforeContentLoaded.ts +0 -29
  84. /package/lib/commonjs/{NativeDdSdk.js → ext-specs/NativeDdSdk.js} +0 -0
  85. /package/lib/module/{NativeDdSdk.js → ext-specs/NativeDdSdk.js} +0 -0
  86. /package/lib/typescript/{NativeDdSdk.d.ts → ext-specs/NativeDdSdk.d.ts} +0 -0
@@ -0,0 +1,13 @@
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
+ // Utility function to remove indentation from a given string
8
+ export function dedent(str: string) {
9
+ const match = str.match(/^[ \t]*(?=\S)/gm);
10
+ const indent = match ? Math.min(...match.map(el => el.length)) : 0;
11
+ const regex = new RegExp(`^[ \\t]{${indent}}`, 'gm');
12
+ return indent > 0 ? str.replace(regex, '') : str;
13
+ }
@@ -0,0 +1,51 @@
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
+ import { wrapJsCodeInTryAndCatch } from '../utils/webview-js-utils';
8
+
9
+ import { dedent } from './__utils__/string-utils';
10
+
11
+ describe('WebView JS Utils', () => {
12
+ beforeEach(() => {
13
+ jest.clearAllMocks();
14
+ });
15
+
16
+ describe('M wrapJsCodeInTryCatch wraps JS code in try & catch with DD messaging W jsCode is not null', () => {
17
+ it('M returns the JS code wrapped in try and catch', () => {
18
+ // Given
19
+ const jsCode = "console.log('test')";
20
+
21
+ // When
22
+ const wrappedCode = wrapJsCodeInTryAndCatch(jsCode);
23
+
24
+ // Then
25
+ const expected = dedent(`
26
+ try{
27
+ console.log('test')
28
+ }
29
+ catch (error) {
30
+ const errorMsg = error instanceof Error ? error.message : String(error);
31
+ window.ReactNativeWebView.postMessage(JSON.stringify({
32
+ source: 'DATADOG',
33
+ type: 'ERROR',
34
+ message: errorMsg
35
+ }));
36
+ true;
37
+ }`);
38
+ expect(wrappedCode).toBeDefined();
39
+ expect(dedent(wrappedCode as string)).toBe(expected);
40
+ });
41
+
42
+ it('M returns undefined W { jsCode = undefined }', () => {
43
+ // Given
44
+ const jsCode = undefined;
45
+ // When
46
+ const wrappedCode = wrapJsCodeInTryAndCatch(jsCode);
47
+ // Then
48
+ expect(wrappedCode).toBe(undefined);
49
+ });
50
+ });
51
+ });
@@ -0,0 +1,25 @@
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
+ /* eslint-disable @typescript-eslint/ban-types */
8
+ import type { TurboModule } from 'react-native';
9
+ import { TurboModuleRegistry } from 'react-native';
10
+
11
+ /**
12
+ * Do not import this Spec directly, use DdNativeLogsType instead.
13
+ */
14
+ export interface Spec extends TurboModule {
15
+ readonly getConstants: () => {};
16
+ /**
17
+ * Send a log with ERROR level.
18
+ * @param message: The message to send.
19
+ * @param context: The additional context to send.
20
+ */
21
+ readonly error: (message: string, context: Object) => Promise<void>;
22
+ }
23
+
24
+ // eslint-disable-next-line import/no-default-export
25
+ export default TurboModuleRegistry.get<Spec>('DdLogs');
@@ -15,6 +15,7 @@ export interface PartialNativeDdSdkSpec extends TurboModule {
15
15
  consumeWebviewEvent(message: string): Promise<void>;
16
16
  telemetryError(message: string, stack: string, kind: string): Promise<void>;
17
17
  }
18
+
18
19
  export const NativeDdSdk = TurboModuleRegistry.get<PartialNativeDdSdkSpec>(
19
20
  'DdSdk'
20
21
  );
package/src/index.tsx CHANGED
@@ -7,40 +7,97 @@ import type { WebViewMessageEvent, WebViewProps } from 'react-native-webview';
7
7
  import { WebView as RNWebView } from 'react-native-webview';
8
8
  import React, { forwardRef, useCallback } from 'react';
9
9
 
10
- import { NativeDdSdk } from './NativeDdSdk';
10
+ import NativeDdLogs from './ext-specs/NativeDdLogs';
11
+ import { NativeDdSdk } from './ext-specs/NativeDdSdk';
12
+ import { NativeDdWebView } from './specs/NativeDdWebView';
13
+ import { isNewArchitecture } from './utils/env-utils';
11
14
  import {
12
- DATADOG_MESSAGE_PREFIX,
13
- getInjectedJavaScriptBeforeContentLoaded
14
- } from './__utils__/getInjectedJavaScriptBeforeContentLoaded';
15
+ getWebViewEventBridgingJS,
16
+ wrapJsCodeInTryAndCatch
17
+ } from './utils/webview-js-utils';
18
+ import type { DatadogMessageFormat } from './utils/webview-js-utils';
15
19
 
16
20
  type Props = WebViewProps & {
21
+ /**
22
+ * The list of allowed hosts for Datadog WebView tracking.
23
+ */
17
24
  allowedHosts?: string[];
25
+ /**
26
+ * Whether injected User JS Code errors should be logged to Datadog (default: false).
27
+ */
28
+ logUserCodeErrors?: boolean;
29
+ /**
30
+ * Custom JS Code to inject before the WebView content is loaded.
31
+ */
18
32
  injectedJavaScriptBeforeContentLoaded?: string;
19
33
  };
20
34
 
21
35
  const WebViewComponent = (props: Props, ref: React.Ref<RNWebView<Props>>) => {
22
36
  const userDefinedOnMessage = props.onMessage;
37
+
23
38
  const onMessage = useCallback(
24
39
  (event: WebViewMessageEvent) => {
40
+ const handleDatadogMessage = (ddMessage: DatadogMessageFormat) => {
41
+ if (
42
+ ddMessage.type === 'ERROR' &&
43
+ ddMessage.message != null &&
44
+ (props.logUserCodeErrors ?? false)
45
+ ) {
46
+ NativeDdLogs?.error(ddMessage.message, {});
47
+ } else if (
48
+ ddMessage.type === 'NATIVE_EVENT' &&
49
+ ddMessage.message != null
50
+ ) {
51
+ NativeDdSdk?.consumeWebviewEvent(ddMessage.message);
52
+ }
53
+ };
54
+
25
55
  const message = event.nativeEvent.data;
26
- if (message.startsWith(DATADOG_MESSAGE_PREFIX)) {
27
- NativeDdSdk?.consumeWebviewEvent(
28
- message.substring(DATADOG_MESSAGE_PREFIX.length + 1)
29
- );
30
- } else {
56
+ if (message == null) {
57
+ return;
58
+ }
59
+
60
+ try {
61
+ const jsonMsg = JSON.parse(message);
62
+ if (jsonMsg && jsonMsg.source === 'DATADOG') {
63
+ handleDatadogMessage(jsonMsg);
64
+ } else {
65
+ userDefinedOnMessage?.(event);
66
+ }
67
+ } catch (err) {
31
68
  userDefinedOnMessage?.(event);
32
69
  }
33
70
  },
34
- [userDefinedOnMessage]
71
+ [userDefinedOnMessage, props.logUserCodeErrors]
35
72
  );
73
+
74
+ const getInjectedJavascriptBeforeContentLoaded = (): string | undefined => {
75
+ if (isNewArchitecture()) {
76
+ return getWebViewEventBridgingJS(
77
+ props.allowedHosts,
78
+ props.injectedJavaScriptBeforeContentLoaded
79
+ );
80
+ } else {
81
+ return wrapJsCodeInTryAndCatch(
82
+ props.injectedJavaScriptBeforeContentLoaded
83
+ );
84
+ }
85
+ };
86
+
36
87
  return (
37
88
  <RNWebView
38
89
  {...props}
39
90
  onMessage={onMessage}
40
- injectedJavaScriptBeforeContentLoaded={getInjectedJavaScriptBeforeContentLoaded(
41
- props.allowedHosts,
42
- props.injectedJavaScriptBeforeContentLoaded
91
+ nativeConfig={{
92
+ component: NativeDdWebView,
93
+ props: {
94
+ allowedHosts: props.allowedHosts
95
+ }
96
+ }}
97
+ injectedJavaScript={wrapJsCodeInTryAndCatch(
98
+ props.injectedJavaScript
43
99
  )}
100
+ injectedJavaScriptBeforeContentLoaded={getInjectedJavascriptBeforeContentLoaded()}
44
101
  ref={ref}
45
102
  />
46
103
  );
@@ -0,0 +1,16 @@
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
+ import type { CommonNativeWebViewProps } from 'react-native-webview/lib/WebViewTypes';
8
+ import { requireNativeComponent } from 'react-native';
9
+
10
+ import { isNewArchitecture } from '../utils/env-utils';
11
+
12
+ const NativeDdWebView = !isNewArchitecture()
13
+ ? requireNativeComponent<CommonNativeWebViewProps>('DdReactNativeWebView')
14
+ : undefined;
15
+
16
+ export { NativeDdWebView };
@@ -0,0 +1,9 @@
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
+ };
@@ -0,0 +1,150 @@
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 { NativeDdSdk } from '../ext-specs/NativeDdSdk';
7
+
8
+ export const DATADOG_MESSAGE_PREFIX = '[DATADOG]';
9
+
10
+ /**
11
+ * Internal Datadog Message Type
12
+ */
13
+ export type DatadogMessageType =
14
+ /**
15
+ * Signals errors that occured during the execution of JavaScript code in the WebView.
16
+ */
17
+ | 'ERROR'
18
+ /**
19
+ * Signals events that should be forwarded and consumed by the native SDK.
20
+ */
21
+ | 'NATIVE_EVENT';
22
+
23
+ /**
24
+ * Internal Datadog Message Format.
25
+ */
26
+ export type DatadogMessageFormat = {
27
+ type: DatadogMessageType;
28
+ message: string;
29
+ };
30
+
31
+ /**
32
+ * Wraps the given JS Code in a try and catch block.
33
+ * @param javascriptCode The JS Code to wrap in a try and catch block.
34
+ * @returns the wrapped JS code.
35
+ */
36
+ export const wrapJsCodeInTryAndCatch = (
37
+ javascriptCode?: string
38
+ ): string | undefined =>
39
+ javascriptCode
40
+ ? `
41
+ try{
42
+ ${javascriptCode}
43
+ }
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
+ }`
53
+ : undefined;
54
+
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
+ `;
92
+
93
+ function formatAllowedHosts(allowedHosts?: string[]): string {
94
+ try {
95
+ return `'${JSON.stringify(allowedHosts)}'`;
96
+ } catch (e: any) {
97
+ if (NativeDdSdk) {
98
+ NativeDdSdk.telemetryError(
99
+ getErrorMessage(e),
100
+ getErrorStackTrace(e),
101
+ 'AllowedHostsError'
102
+ );
103
+ }
104
+ return "'[]'";
105
+ }
106
+ }
107
+
108
+ const getErrorMessage = (error: any | undefined): string => {
109
+ const EMPTY_MESSAGE = 'Unknown Error';
110
+ let message = EMPTY_MESSAGE;
111
+ if (error === undefined || error === null) {
112
+ message = EMPTY_MESSAGE;
113
+ } else if (typeof error === 'object' && 'message' in error) {
114
+ message = String(error.message);
115
+ } else {
116
+ message = String(error);
117
+ }
118
+
119
+ return message;
120
+ };
121
+
122
+ const getErrorStackTrace = (error: any | undefined): string => {
123
+ const EMPTY_STACK_TRACE = '';
124
+ let stack = EMPTY_STACK_TRACE;
125
+
126
+ try {
127
+ if (error === undefined || error === null) {
128
+ stack = EMPTY_STACK_TRACE;
129
+ } else if (typeof error === 'string') {
130
+ stack = EMPTY_STACK_TRACE;
131
+ } else if (typeof error === 'object') {
132
+ if ('stacktrace' in error) {
133
+ stack = String(error.stacktrace);
134
+ } else if ('stack' in error) {
135
+ stack = String(error.stack);
136
+ } else if ('componentStack' in error) {
137
+ stack = String(error.componentStack);
138
+ } else if (
139
+ 'sourceURL' in error &&
140
+ 'line' in error &&
141
+ 'column' in error
142
+ ) {
143
+ stack = `at ${error.sourceURL}:${error.line}:${error.column}`;
144
+ }
145
+ }
146
+ } catch (e) {
147
+ // Do nothing
148
+ }
149
+ return stack;
150
+ };
@@ -1 +0,0 @@
1
- {"version":3,"names":["_reactNative","require","NativeDdSdk","exports","TurboModuleRegistry","get"],"sourceRoot":"../../src","sources":["NativeDdSdk.ts"],"mappings":";;;;;;AAOA,IAAAA,YAAA,GAAAC,OAAA;AAPA;AACA;AACA;AACA;AACA;;AAKA;AACA;AACA;AACA;;AAKO,MAAMC,WAAW,GAAAC,OAAA,CAAAD,WAAA,GAAGE,gCAAmB,CAACC,GAAG,CAC9C,OACJ,CAAC","ignoreList":[]}
@@ -1,65 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.formatAllowedHosts = formatAllowedHosts;
7
- var _NativeDdSdk = require("../NativeDdSdk");
8
- /*
9
- * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
10
- * This product includes software developed at Datadog (https://www.datadoghq.com/).
11
- * Copyright 2016-Present Datadog, Inc.
12
- */
13
-
14
- function formatAllowedHosts(allowedHosts) {
15
- try {
16
- return `'${JSON.stringify(allowedHosts)}'`;
17
- } catch (e) {
18
- if (_NativeDdSdk.NativeDdSdk) {
19
- _NativeDdSdk.NativeDdSdk.telemetryError(getErrorMessage(e), getErrorStackTrace(e), 'AllowedHostsError');
20
- }
21
- return "'[]'";
22
- }
23
- }
24
-
25
- /**
26
- * The next section is copied from packages/core/src/errorUtils
27
- */
28
-
29
- const EMPTY_MESSAGE = 'Unknown Error';
30
- const EMPTY_STACK_TRACE = '';
31
- const getErrorMessage = error => {
32
- let message = EMPTY_MESSAGE;
33
- if (error === undefined || error === null) {
34
- message = EMPTY_MESSAGE;
35
- } else if (typeof error === 'object' && 'message' in error) {
36
- message = String(error.message);
37
- } else {
38
- message = String(error);
39
- }
40
- return message;
41
- };
42
- const getErrorStackTrace = error => {
43
- let stack = EMPTY_STACK_TRACE;
44
- try {
45
- if (error === undefined || error === null) {
46
- stack = EMPTY_STACK_TRACE;
47
- } else if (typeof error === 'string') {
48
- stack = EMPTY_STACK_TRACE;
49
- } else if (typeof error === 'object') {
50
- if ('stacktrace' in error) {
51
- stack = String(error.stacktrace);
52
- } else if ('stack' in error) {
53
- stack = String(error.stack);
54
- } else if ('componentStack' in error) {
55
- stack = String(error.componentStack);
56
- } else if ('sourceURL' in error && 'line' in error && 'column' in error) {
57
- stack = `at ${error.sourceURL}:${error.line}:${error.column}`;
58
- }
59
- }
60
- } catch (e) {
61
- // Do nothing
62
- }
63
- return stack;
64
- };
65
- //# sourceMappingURL=formatAllowedHosts.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["_NativeDdSdk","require","formatAllowedHosts","allowedHosts","JSON","stringify","e","NativeDdSdk","telemetryError","getErrorMessage","getErrorStackTrace","EMPTY_MESSAGE","EMPTY_STACK_TRACE","error","message","undefined","String","stack","stacktrace","componentStack","sourceURL","line","column"],"sourceRoot":"../../../src","sources":["__utils__/formatAllowedHosts.ts"],"mappings":";;;;;;AAKA,IAAAA,YAAA,GAAAC,OAAA;AALA;AACA;AACA;AACA;AACA;;AAGO,SAASC,kBAAkBA,CAACC,YAAuB,EAAU;EAChE,IAAI;IACA,OAAO,IAAIC,IAAI,CAACC,SAAS,CAACF,YAAY,CAAC,GAAG;EAC9C,CAAC,CAAC,OAAOG,CAAM,EAAE;IACb,IAAIC,wBAAW,EAAE;MACbA,wBAAW,CAACC,cAAc,CACtBC,eAAe,CAACH,CAAC,CAAC,EAClBI,kBAAkB,CAACJ,CAAC,CAAC,EACrB,mBACJ,CAAC;IACL;IACA,OAAO,MAAM;EACjB;AACJ;;AAEA;AACA;AACA;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AAK,CAACO,SAAS,IAAIP,KAAK,CAACQ,IAAI,IAAIR,KAAK,CAACS,MAAM,EAAE;MACjE;IACJ;EACJ,CAAC,CAAC,OAAOhB,CAAC,EAAE;IACR;EAAA;EAEJ,OAAOW,KAAK;AAChB,CAAC","ignoreList":[]}
@@ -1,32 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getInjectedJavaScriptBeforeContentLoaded = exports.DATADOG_MESSAGE_PREFIX = void 0;
7
- var _formatAllowedHosts = require("./formatAllowedHosts");
8
- /*
9
- * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
10
- * This product includes software developed at Datadog (https://www.datadoghq.com/).
11
- * Copyright 2016-Present Datadog, Inc.
12
- */
13
-
14
- const DATADOG_MESSAGE_PREFIX = exports.DATADOG_MESSAGE_PREFIX = '[DATADOG]';
15
- const getInjectedJavaScriptBeforeContentLoaded = (allowedHosts, injectedJavaScriptBeforeContentLoaded) => `
16
- window.DatadogEventBridge = {
17
- send(msg) {
18
- window.ReactNativeWebView.postMessage("${DATADOG_MESSAGE_PREFIX} " + msg)
19
- },
20
- getAllowedWebViewHosts() {
21
- return ${(0, _formatAllowedHosts.formatAllowedHosts)(allowedHosts)}
22
- }
23
- };
24
- try{
25
- ${injectedJavaScriptBeforeContentLoaded}
26
- }
27
- catch (error) {
28
- // The user defined code has crashed
29
- }
30
- `;
31
- exports.getInjectedJavaScriptBeforeContentLoaded = getInjectedJavaScriptBeforeContentLoaded;
32
- //# sourceMappingURL=getInjectedJavaScriptBeforeContentLoaded.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["_formatAllowedHosts","require","DATADOG_MESSAGE_PREFIX","exports","getInjectedJavaScriptBeforeContentLoaded","allowedHosts","injectedJavaScriptBeforeContentLoaded","formatAllowedHosts"],"sourceRoot":"../../../src","sources":["__utils__/getInjectedJavaScriptBeforeContentLoaded.ts"],"mappings":";;;;;;AAKA,IAAAA,mBAAA,GAAAC,OAAA;AALA;AACA;AACA;AACA;AACA;;AAGO,MAAMC,sBAAsB,GAAAC,OAAA,CAAAD,sBAAA,GAAG,WAAW;AAE1C,MAAME,wCAAwC,GAAGA,CACpDC,YAAuB,EACvBC,qCAA8C,KAE9C;AACJ;AACA;AACA,iDAAiDJ,sBAAsB;AACvE;AACA;AACA,iBAAiB,IAAAK,sCAAkB,EAACF,YAAY,CAAC;AACjD;AACA;AACA;AACA,QAAQC,qCAAqC;AAC7C;AACA;AACA;AACA;AACA,GAAG;AAACH,OAAA,CAAAC,wCAAA,GAAAA,wCAAA","ignoreList":[]}
@@ -1 +0,0 @@
1
- {"version":3,"names":["TurboModuleRegistry","NativeDdSdk","get"],"sourceRoot":"../../src","sources":["NativeDdSdk.ts"],"mappings":"AAAA;AACA;AACA;AACA;AACA;;AAGA,SAASA,mBAAmB,QAAQ,cAAc;;AAElD;AACA;AACA;AACA;;AAKA,OAAO,MAAMC,WAAW,GAAGD,mBAAmB,CAACE,GAAG,CAC9C,OACJ,CAAC","ignoreList":[]}
@@ -1,58 +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
- import { NativeDdSdk } from '../NativeDdSdk';
7
- export function formatAllowedHosts(allowedHosts) {
8
- try {
9
- return `'${JSON.stringify(allowedHosts)}'`;
10
- } catch (e) {
11
- if (NativeDdSdk) {
12
- NativeDdSdk.telemetryError(getErrorMessage(e), getErrorStackTrace(e), 'AllowedHostsError');
13
- }
14
- return "'[]'";
15
- }
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
- const getErrorMessage = error => {
25
- let message = EMPTY_MESSAGE;
26
- if (error === undefined || error === null) {
27
- message = EMPTY_MESSAGE;
28
- } else if (typeof error === 'object' && 'message' in error) {
29
- message = String(error.message);
30
- } else {
31
- message = String(error);
32
- }
33
- return message;
34
- };
35
- const getErrorStackTrace = error => {
36
- let stack = EMPTY_STACK_TRACE;
37
- try {
38
- if (error === undefined || error === null) {
39
- stack = EMPTY_STACK_TRACE;
40
- } else if (typeof error === 'string') {
41
- stack = EMPTY_STACK_TRACE;
42
- } else if (typeof error === 'object') {
43
- if ('stacktrace' in error) {
44
- stack = String(error.stacktrace);
45
- } else if ('stack' in error) {
46
- stack = String(error.stack);
47
- } else if ('componentStack' in error) {
48
- stack = String(error.componentStack);
49
- } else if ('sourceURL' in error && 'line' in error && 'column' in error) {
50
- stack = `at ${error.sourceURL}:${error.line}:${error.column}`;
51
- }
52
- }
53
- } catch (e) {
54
- // Do nothing
55
- }
56
- return stack;
57
- };
58
- //# sourceMappingURL=formatAllowedHosts.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["NativeDdSdk","formatAllowedHosts","allowedHosts","JSON","stringify","e","telemetryError","getErrorMessage","getErrorStackTrace","EMPTY_MESSAGE","EMPTY_STACK_TRACE","error","message","undefined","String","stack","stacktrace","componentStack","sourceURL","line","column"],"sourceRoot":"../../../src","sources":["__utils__/formatAllowedHosts.ts"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA,SAASA,WAAW,QAAQ,gBAAgB;AAE5C,OAAO,SAASC,kBAAkBA,CAACC,YAAuB,EAAU;EAChE,IAAI;IACA,OAAO,IAAIC,IAAI,CAACC,SAAS,CAACF,YAAY,CAAC,GAAG;EAC9C,CAAC,CAAC,OAAOG,CAAM,EAAE;IACb,IAAIL,WAAW,EAAE;MACbA,WAAW,CAACM,cAAc,CACtBC,eAAe,CAACF,CAAC,CAAC,EAClBG,kBAAkB,CAACH,CAAC,CAAC,EACrB,mBACJ,CAAC;IACL;IACA,OAAO,MAAM;EACjB;AACJ;;AAEA;AACA;AACA;;AAEA,MAAMI,aAAa,GAAG,eAAe;AACrC,MAAMC,iBAAiB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AK,CAACS,MAAM,EAAE;MACjE;IACJ;EACJ,CAAC,CAAC,OAAOf,CAAC,EAAE;IACR;EAAA;EAEJ,OAAOU,KAAK;AAChB,CAAC","ignoreList":[]}
@@ -1,24 +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
- 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
@@ -1 +0,0 @@
1
- {"version":3,"names":["formatAllowedHosts","DATADOG_MESSAGE_PREFIX","getInjectedJavaScriptBeforeContentLoaded","allowedHosts","injectedJavaScriptBeforeContentLoaded"],"sourceRoot":"../../../src","sources":["__utils__/getInjectedJavaScriptBeforeContentLoaded.ts"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA,SAASA,kBAAkB,QAAQ,sBAAsB;AAEzD,OAAO,MAAMC,sBAAsB,GAAG,WAAW;AAEjD,OAAO,MAAMC,wCAAwC,GAAGA,CACpDC,YAAuB,EACvBC,qCAA8C,KAE9C;AACJ;AACA;AACA,iDAAiDH,sBAAsB;AACvE;AACA;AACA,iBAAiBD,kBAAkB,CAACG,YAAY,CAAC;AACjD;AACA;AACA;AACA,QAAQC,qCAAqC;AAC7C;AACA;AACA;AACA;AACA,GAAG","ignoreList":[]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"NativeDdSdk.d.ts","sourceRoot":"","sources":["../../src/NativeDdSdk.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAGhD;;;GAGG;AACH,MAAM,WAAW,sBAAuB,SAAQ,WAAW;IACvD,mBAAmB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACpD,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC/E;AACD,eAAO,MAAM,WAAW,+BAEvB,CAAC"}
@@ -1,2 +0,0 @@
1
- export declare function formatAllowedHosts(allowedHosts?: string[]): string;
2
- //# sourceMappingURL=formatAllowedHosts.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"formatAllowedHosts.d.ts","sourceRoot":"","sources":["../../../src/__utils__/formatAllowedHosts.ts"],"names":[],"mappings":"AAOA,wBAAgB,kBAAkB,CAAC,YAAY,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,CAalE"}
@@ -1,3 +0,0 @@
1
- export declare const DATADOG_MESSAGE_PREFIX = "[DATADOG]";
2
- export declare const getInjectedJavaScriptBeforeContentLoaded: (allowedHosts?: string[], injectedJavaScriptBeforeContentLoaded?: string) => string;
3
- //# sourceMappingURL=getInjectedJavaScriptBeforeContentLoaded.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"getInjectedJavaScriptBeforeContentLoaded.d.ts","sourceRoot":"","sources":["../../../src/__utils__/getInjectedJavaScriptBeforeContentLoaded.ts"],"names":[],"mappings":"AAOA,eAAO,MAAM,sBAAsB,cAAc,CAAC;AAElD,eAAO,MAAM,wCAAwC,kBAClC,MAAM,EAAE,0CACiB,MAAM,KAC/C,MAgBA,CAAC"}
@@ -1,40 +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
- /* 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
- });