Skip to content

Commit c8b7be3

Browse files
committed
Update
1 parent 65bba73 commit c8b7be3

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

chat-worker/src/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
// and DML statements are rejected up front for defence in depth.
1616
// ============================================================
1717

18+
// MUST be first — polyfills browser globals that sql.js reads at init.
19+
import './polyfill.js';
1820
import initSqlJs from 'sql.js';
1921

2022
// Bundled at build time via wrangler.toml [[rules]].
@@ -235,8 +237,9 @@ export default {
235237
}
236238
});
237239
} catch (e) {
238-
console.error('chat error:', e);
239-
return jsonError(e.message || 'Internal error', 500, env);
240+
console.error('chat error:', e?.stack || e);
241+
const detail = e?.stack ? `${e.message}\n${e.stack}` : (e?.message || String(e));
242+
return jsonError(detail, 500, env);
240243
}
241244
}
242245
};

chat-worker/src/polyfill.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Workers don't expose `location` or `document` — sql.js (Emscripten)
2+
// reads both during WASM init to locate the .wasm file. We stub them
3+
// here so sql.js can finish initialising; the stub URL is never fetched
4+
// because the worker passes its own `instantiateWasm` callback.
5+
//
6+
// This file MUST be imported before `sql.js`. ES-module imports are
7+
// evaluated in source order for side-effect imports, so put this first.
8+
9+
if (typeof globalThis.location === 'undefined') {
10+
globalThis.location = new URL('https://localhost/');
11+
}
12+
if (typeof globalThis.document === 'undefined') {
13+
globalThis.document = { currentScript: { src: 'https://localhost/' } };
14+
}

0 commit comments

Comments
 (0)