11/**
2- * Hermes Compatibility Test
2+ * Hermes + CSP Compatibility Test
33 *
4- * Asserts dist/index.cjs contains no bare import() expressions.
5- * hermesc (React Native bytecode compiler) rejects import() at parse time —
6- * before dead-code elimination — so the syntax must be physically absent.
7- * The only allowed occurrence is inside a new Function() string body, which
8- * is opaque to all static parsers including hermesc.
4+ * Asserts dist/index.cjs contains:
5+ * - No import() expressions -> hermesc (React Native bytecode compiler) rejects
6+ * import() at parse time, before dead-code elimination.
7+ * - No new Function() calls -> browsers with a strict Content-Security-Policy
8+ * (no 'unsafe-eval') block new Function() identically to eval() at runtime.
9+ *
10+ * Both constraints are satisfied by pointing the CJS bundle at the TypeScript
11+ * CommonJS-compiled output of @supabase/tracing (dist/main/), where import()
12+ * has already been downcompiled to require() by tsc.
913 *
1014 * Run with: node test/bundle-hermes-compat.test.cjs
1115 */
@@ -14,31 +18,25 @@ const assert = require('assert')
1418const fs = require ( 'fs' )
1519const path = require ( 'path' )
1620
17- console . log ( 'Testing Hermes (React Native) compatibility...\n' )
21+ console . log ( 'Testing Hermes + CSP compatibility of dist/index.cjs ...\n' )
1822
1923const cjsPath = path . join ( __dirname , '../dist/index.cjs' )
2024const cjs = fs . readFileSync ( cjsPath , 'utf8' )
21- const lines = cjs . split ( '\n' )
2225
23- let bareImportCount = 0
24- for ( let i = 0 ; i < lines . length ; i ++ ) {
25- const line = lines [ i ]
26- if ( ! line . includes ( 'import(' ) ) continue
27- if ( ! line . includes ( 'new Function(' ) ) {
28- bareImportCount ++
29- console . error (
30- `❌ Bare import() at line ${ i + 1 } :\n ${ line . trim ( ) } \n` +
31- ` This breaks hermesc (Hermes bytecode compiler for React Native).`
32- )
33- }
34- }
26+ // Check 1: no import() expressions (breaks hermesc / React Native)
27+ assert . ok (
28+ ! cjs . includes ( 'import(' ) ,
29+ 'dist/index.cjs contains import() — breaks hermesc (Hermes bytecode compiler for React Native)'
30+ )
31+ console . log ( '1. No import() expressions in dist/index.cjs' )
32+ console . log ( ' Hermes-safe (React Native compatible)\n' )
3533
36- assert . strictEqual (
37- bareImportCount ,
38- 0 ,
39- ` ${ bareImportCount } bare import() expression(s) in dist/index.cjs — breaks React Native Hermes builds.`
34+ // Check 2: no new Function() (breaks browser strict CSP)
35+ assert . ok (
36+ ! cjs . includes ( 'new Function(' ) ,
37+ ' dist/index.cjs contains new Function() — breaks browser strict Content-Security-Policy (unsafe-eval)'
4038)
39+ console . log ( '2. No new Function() in dist/index.cjs' )
40+ console . log ( ' CSP-safe (no unsafe-eval required)\n' )
4141
42- console . log ( '1. No bare import() expressions in dist/index.cjs' )
43- console . log ( ' ✅ Hermes-safe (React Native compatible)\n' )
44- console . log ( '🎉 Hermes compatibility test passed!' )
42+ console . log ( 'All Hermes + CSP compatibility checks passed.' )
0 commit comments