Skip to content

Commit fb118f6

Browse files
authored
Merge pull request #11 from andreisugu/copilot/implement-streaming-json-parser
Fix parser initialization: replace dynamic import with importScripts to resolve CORS failures
2 parents 2597d8d + b8dbe03 commit fb118f6

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

public/workers/db-worker.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
// Import SQL.js from CDN using classic worker syntax
55
importScripts('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+
711
let db = null;
812
let tableName = 'data';
913
let sampleSize = 100;
@@ -15,15 +19,14 @@ let currentBatch = [];
1519
let existingColumnsSet = new Set();
1620
let pendingColumns = [];
1721
let 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
*/
3336
async 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

Comments
 (0)