@datadog/mobile-react-native-webview
Version:
A client-side React Native module to interact with react-native-webview and Datadog
124 lines (118 loc) • 3.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.wrapJsCodeInTryAndCatch = exports.getWebViewEventBridgingJS = exports.DATADOG_MESSAGE_PREFIX = void 0;
var _NativeDdSdk = require("../ext-specs/NativeDdSdk");
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/
const DATADOG_MESSAGE_PREFIX = exports.DATADOG_MESSAGE_PREFIX = '[DATADOG]';
/**
* Internal Datadog Message Type
*/
/**
* Internal Datadog Message Format.
*/
/**
* Wraps the given JS Code in a try and catch block.
* @param javascriptCode The JS Code to wrap in a try and catch block.
* @returns the wrapped JS code.
*/
const wrapJsCodeInTryAndCatch = javascriptCode => javascriptCode ? `
try{
${javascriptCode}
}
catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error);
window.ReactNativeWebView.postMessage(JSON.stringify({
source: 'DATADOG',
type: 'ERROR',
message: errorMsg
}));
true;
}` : undefined;
/**
* Legacy JS code for bridging the WebView events to DataDog native SDKs for consumption.
* @param allowedHosts The list of allowed hosts.
* @param customJavaScriptCode Custom user JS code to inject along with the Datadog bridging logic.
* @returns The JS code block as a string.
*/
exports.wrapJsCodeInTryAndCatch = wrapJsCodeInTryAndCatch;
const getWebViewEventBridgingJS = (allowedHosts, customJavaScriptCode) => `
window.DatadogEventBridge = {
send(msg) {
window.ReactNativeWebView.postMessage(JSON.stringify({
source: 'DATADOG',
type: 'NATIVE_EVENT',
message: msg
}));
true;
},
getAllowedWebViewHosts() {
return ${formatAllowedHosts(allowedHosts)}
}
};
try{
${customJavaScriptCode}
}
catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error);
window.ReactNativeWebView.postMessage(JSON.stringify({
source: 'DATADOG',
type: 'ERROR',
message: errorMsg
}));
true;
}
`;
exports.getWebViewEventBridgingJS = getWebViewEventBridgingJS;
function formatAllowedHosts(allowedHosts) {
try {
return `'${JSON.stringify(allowedHosts)}'`;
} catch (e) {
if (_NativeDdSdk.NativeDdSdk) {
_NativeDdSdk.NativeDdSdk.telemetryError(getErrorMessage(e), getErrorStackTrace(e), 'AllowedHostsError');
}
return "'[]'";
}
}
const getErrorMessage = error => {
const EMPTY_MESSAGE = 'Unknown Error';
let message = EMPTY_MESSAGE;
if (error === undefined || error === null) {
message = EMPTY_MESSAGE;
} else if (typeof error === 'object' && 'message' in error) {
message = String(error.message);
} else {
message = String(error);
}
return message;
};
const getErrorStackTrace = error => {
const EMPTY_STACK_TRACE = '';
let stack = EMPTY_STACK_TRACE;
try {
if (error === undefined || error === null) {
stack = EMPTY_STACK_TRACE;
} else if (typeof error === 'string') {
stack = EMPTY_STACK_TRACE;
} else if (typeof error === 'object') {
if ('stacktrace' in error) {
stack = String(error.stacktrace);
} else if ('stack' in error) {
stack = String(error.stack);
} else if ('componentStack' in error) {
stack = String(error.componentStack);
} else if ('sourceURL' in error && 'line' in error && 'column' in error) {
stack = `at ${error.sourceURL}:${error.line}:${error.column}`;
}
}
} catch (e) {
// Do nothing
}
return stack;
};
//# sourceMappingURL=webview-js-utils.js.map