44// Import SQL.js from CDN using classic worker syntax
55importScripts ( 'https://cdn.jsdelivr.net/npm/sql.js@1.10.3/dist/sql-wasm.js' ) ;
66
7+ // Import streaming JSON parser using UMD build
8+ // This avoids CORS/Dynamic Import issues with ESM modules in classic workers
9+ importScripts ( 'https://unpkg.com/@streamparser/json@0.0.23/dist/json.min.js' ) ;
10+
711let db = null ;
812let tableName = 'data' ;
913let sampleSize = 100 ;
@@ -15,15 +19,14 @@ let currentBatch = [];
1519let existingColumnsSet = new Set ( ) ;
1620let pendingColumns = [ ] ;
1721let parser = null ;
18- let JSONParser = null ;
1922
2023/**
2124 * Initialize the Stream Parser
2225 *
23- * Note: This uses dynamic ES module loading because:
24- * 1. The @streamparser/json library is ESM-only
25- * 2. Classic workers (needed for importScripts) don't support ES modules natively
26- * 3. The library is loaded from esm.sh CDN which properly handles ESM module resolution
26+ * Note: This uses importScripts to load the UMD build because:
27+ * 1. The @streamparser/json library has a UMD build available
28+ * 2. Classic workers (needed for SQL.js importScripts) work best with importScripts for all dependencies
29+ * 3. Using importScripts avoids CORS issues that can occur with dynamic import()
2730 *
2831 * For production deployments with strict security requirements, consider:
2932 * - Hosting the library locally
@@ -32,9 +35,12 @@ let JSONParser = null;
3235 */
3336async function initParser ( ) {
3437 try {
35- // Load the core library
36- const module = await import ( 'https://esm.sh/@streamparser/json@0.0.23' ) ;
37- JSONParser = module . JSONParser ;
38+ // Use the global JSONParser exposed by the importScripts above
39+ if ( typeof self . JSONParser === 'undefined' ) {
40+ throw new Error ( 'JSONParser library failed to load via importScripts' ) ;
41+ }
42+
43+ const JSONParser = self . JSONParser ;
3844
3945 // Capture the root element
4046 parser = new JSONParser ( { paths : [ '$' ] , keepStack : false } ) ;
@@ -65,6 +71,8 @@ async function initParser() {
6571
6672 } catch ( error ) {
6773 console . error ( '[DB Worker] Failed to initialize parser:' , error ) ;
74+ // Re-throw the error so initDatabase knows initialization failed
75+ throw error ;
6876 }
6977}
7078
0 commit comments