@@ -1267,7 +1267,10 @@ describe('_INTERNAL_captureLog', () => {
12671267 } ) ;
12681268 } ) ;
12691269
1270- describe ( 'lone surrogate sanitization' , ( ) => {
1270+ // toWellFormed() is only available in Node 20+, Chrome 111+, Safari 15.4+, Firefox 119+, Hermes
1271+ const hasToWellFormed = typeof '' . isWellFormed === 'function' ;
1272+
1273+ describe . runIf ( hasToWellFormed ) ( 'lone surrogate sanitization' , ( ) => {
12711274 it ( 'sanitizes lone surrogates in log message body' , ( ) => {
12721275 const options = getDefaultTestClientOptions ( { dsn : PUBLIC_DSN , enableLogs : true } ) ;
12731276 const client = new TestClient ( options ) ;
@@ -1362,6 +1365,9 @@ describe('_INTERNAL_captureLog', () => {
13621365 } ) ;
13631366} ) ;
13641367
1368+ // toWellFormed() is only available in Node 20+, Chrome 111+, Safari 15.4+, Firefox 119+, Hermes
1369+ const hasToWellFormedGlobal = typeof '' . isWellFormed === 'function' ;
1370+
13651371describe ( '_INTERNAL_removeLoneSurrogates' , ( ) => {
13661372 it ( 'returns the same string when there are no surrogates' , ( ) => {
13671373 expect ( _INTERNAL_removeLoneSurrogates ( 'hello world' ) ) . toBe ( 'hello world' ) ;
@@ -1375,38 +1381,43 @@ describe('_INTERNAL_removeLoneSurrogates', () => {
13751381 expect ( _INTERNAL_removeLoneSurrogates ( 'hello 😀 world' ) ) . toBe ( 'hello 😀 world' ) ;
13761382 } ) ;
13771383
1378- it ( 'replaces a lone high surrogate with U+FFFD' , ( ) => {
1384+ it . runIf ( hasToWellFormedGlobal ) ( 'replaces a lone high surrogate with U+FFFD' , ( ) => {
13791385 expect ( _INTERNAL_removeLoneSurrogates ( 'before\uD800after' ) ) . toBe ( 'before\uFFFDafter' ) ;
13801386 } ) ;
13811387
1382- it ( 'replaces a lone low surrogate with U+FFFD' , ( ) => {
1388+ it . runIf ( hasToWellFormedGlobal ) ( 'replaces a lone low surrogate with U+FFFD' , ( ) => {
13831389 expect ( _INTERNAL_removeLoneSurrogates ( 'before\uDC00after' ) ) . toBe ( 'before\uFFFDafter' ) ;
13841390 } ) ;
13851391
1386- it ( 'replaces lone high surrogate at end of string' , ( ) => {
1392+ it . runIf ( hasToWellFormedGlobal ) ( 'replaces lone high surrogate at end of string' , ( ) => {
13871393 expect ( _INTERNAL_removeLoneSurrogates ( 'end\uD800' ) ) . toBe ( 'end\uFFFD' ) ;
13881394 } ) ;
13891395
1390- it ( 'replaces lone low surrogate at start of string' , ( ) => {
1396+ it . runIf ( hasToWellFormedGlobal ) ( 'replaces lone low surrogate at start of string' , ( ) => {
13911397 expect ( _INTERNAL_removeLoneSurrogates ( '\uDC00start' ) ) . toBe ( '\uFFFDstart' ) ;
13921398 } ) ;
13931399
1394- it ( 'replaces multiple lone surrogates' , ( ) => {
1400+ it . runIf ( hasToWellFormedGlobal ) ( 'replaces multiple lone surrogates' , ( ) => {
13951401 expect ( _INTERNAL_removeLoneSurrogates ( '\uD800\uD801\uDC00' ) ) . toBe ( '\uFFFD\uD801\uDC00' ) ;
13961402 } ) ;
13971403
1398- it ( 'handles two consecutive lone high surrogates' , ( ) => {
1404+ it . runIf ( hasToWellFormedGlobal ) ( 'handles two consecutive lone high surrogates' , ( ) => {
13991405 expect ( _INTERNAL_removeLoneSurrogates ( '\uD800\uD800' ) ) . toBe ( '\uFFFD\uFFFD' ) ;
14001406 } ) ;
14011407
1402- it ( 'handles mixed valid pairs and lone surrogates' , ( ) => {
1408+ it . runIf ( hasToWellFormedGlobal ) ( 'handles mixed valid pairs and lone surrogates' , ( ) => {
14031409 expect ( _INTERNAL_removeLoneSurrogates ( '\uD83D\uDE00\uD800' ) ) . toBe ( '😀\uFFFD' ) ;
14041410 } ) ;
14051411
1406- it ( 'handles the exact reproduction case from issue #5186' , ( ) => {
1412+ it . runIf ( hasToWellFormedGlobal ) ( 'handles the exact reproduction case from issue #5186' , ( ) => {
14071413 const badValue = '{"a":"\uD800"}' ;
14081414 const result = _INTERNAL_removeLoneSurrogates ( badValue ) ;
14091415 expect ( result ) . toBe ( '{"a":"\uFFFD"}' ) ;
14101416 expect ( ( ) => JSON . parse ( result ) ) . not . toThrow ( ) ;
14111417 } ) ;
1418+
1419+ it ( 'returns the string as-is when toWellFormed is not available' , ( ) => {
1420+ // Verify the function doesn't throw regardless of runtime support
1421+ expect ( _INTERNAL_removeLoneSurrogates ( 'normal string' ) ) . toBe ( 'normal string' ) ;
1422+ } ) ;
14121423} ) ;
0 commit comments