From 26c6a16007ae080e2f48c11163808dc7b3fcfee6 Mon Sep 17 00:00:00 2001 From: Marco Saia Date: Fri, 5 Jul 2024 17:42:13 +0200 Subject: [PATCH] Added RN 63 BigInt polyfill --- packages/core/src/index.tsx | 4 ++++ packages/core/src/polyfills.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 packages/core/src/polyfills.js diff --git a/packages/core/src/index.tsx b/packages/core/src/index.tsx index 0819dd1e7..a6cb76e21 100644 --- a/packages/core/src/index.tsx +++ b/packages/core/src/index.tsx @@ -3,7 +3,9 @@ * This product includes software developed at Datadog (https://www.datadoghq.com/). * Copyright 2016-Present Datadog, Inc. */ +/* eslint-disable arca/import-ordering */ +import './polyfills'; import { DatadogProviderConfiguration, DdSdkReactNativeConfiguration, @@ -32,6 +34,8 @@ import { DefaultTimeProvider } from './utils/time-provider/DefaultTimeProvider'; import { TimeProvider } from './utils/time-provider/TimeProvider'; import type { Timestamp } from './utils/time-provider/TimeProvider'; +/* eslint-enable arca/import-ordering */ + export { DatadogProvider, DatadogProviderConfiguration, diff --git a/packages/core/src/polyfills.js b/packages/core/src/polyfills.js new file mode 100644 index 000000000..b20a4c847 --- /dev/null +++ b/packages/core/src/polyfills.js @@ -0,0 +1,31 @@ +import { version as reactNativeVersion } from 'react-native/package.json'; + +function applyBigIntPolyfill() { + const rnVersion = reactNativeVersion.split('.').map(Number); + const isRn63 = rnVersion[0] === 0 && rnVersion[1] === 63; + + if (isRn63 && typeof BigInt === 'undefined') { + try { + // eslint-disable-next-line global-require, import/no-extraneous-dependencies + global.BigInt = require('big-integer'); + console.warn( + 'React Native 0.63 does not support BigInt, which is required starting from v2.4.0 of Datadog React Native SDK. ' + + 'The missing type has been polyfilled using `big-integer` to grant back-compatibility. ' + + 'We strongly suggest updating to a greater version of React Native (>= 0.64).' + ); + } catch (e) { + if (e instanceof Error && e.code === 'MODULE_NOT_FOUND') { + throw new Error( + 'React Native 0.63 does not support BigInt, which is required starting from v2.4.0 of Datadog React Native SDK. ' + + 'We strongly suggest updating to a greater version of React Native (>= 0.64).\n\n' + + 'You can install `big-integer` to enable our polyfill that grants back-compatibility with RN 63:\n\n' + + '`yarn add big-integer`\n\nOR\n\n`npm install --save big-integer`' + ); + } else { + throw e; + } + } + } +} + +applyBigIntPolyfill();