mirror of
https://github.com/h-sphere/sql-seal.git
synced 2026-07-22 10:10:28 +00:00
40 lines
1 KiB
JavaScript
40 lines
1 KiB
JavaScript
|
|
// Jest setup file for ESM mode
|
||
|
|
// Polyfill fetch to handle file:// URLs for wa-sqlite WASM loading
|
||
|
|
|
||
|
|
import { readFileSync } from 'fs';
|
||
|
|
import { fileURLToPath } from 'url';
|
||
|
|
|
||
|
|
const originalFetch = global.fetch;
|
||
|
|
|
||
|
|
global.fetch = async function (url, options) {
|
||
|
|
// Handle file:// URLs by reading from filesystem
|
||
|
|
if (typeof url === 'string' && url.startsWith('file://')) {
|
||
|
|
try {
|
||
|
|
const filePath = fileURLToPath(url);
|
||
|
|
const buffer = readFileSync(filePath);
|
||
|
|
|
||
|
|
// Return a proper Response object
|
||
|
|
return new Response(buffer, {
|
||
|
|
status: 200,
|
||
|
|
statusText: 'OK',
|
||
|
|
headers: {
|
||
|
|
'Content-Type': 'application/wasm',
|
||
|
|
'Content-Length': buffer.length.toString(),
|
||
|
|
},
|
||
|
|
});
|
||
|
|
} catch (error) {
|
||
|
|
return new Response(null, {
|
||
|
|
status: 404,
|
||
|
|
statusText: 'Not Found',
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Fall back to original fetch for http:// and https://
|
||
|
|
if (originalFetch) {
|
||
|
|
return originalFetch(url, options);
|
||
|
|
}
|
||
|
|
|
||
|
|
throw new Error('fetch is not available');
|
||
|
|
};
|