vitest.setup.ts36 lines · main
1import { statSync } from 'fs'
2import { config } from 'dotenv'
3
4import './test/extensions'
5
6if (!process.env.CI) {
7 // Use keys from studio .env.local for local tests
8 const envPath = '../../apps/studio/.env.local'
9
10 statSync(envPath)
11 config({ path: envPath })
12}
13
14// Modify fetch to support wasm file URLs
15globalThis.fetch = async (input: string | URL | Request, init?: RequestInit) => {
16 if (input instanceof Request) {
17 return fetch(input, init)
18 }
19
20 const url = new URL(input)
21 if (url.protocol === 'file:' && url.pathname.endsWith('.wasm')) {
22 return fetchFileNode(input, 'application/wasm')
23 }
24
25 return fetch(input, init)
26}
27
28async function fetchFileNode(input: string | URL, type: string) {
29 const fs = await import('node:fs')
30 const { Readable } = await import('node:stream')
31 const { fileURLToPath } = await import('node:url')
32 const path = fileURLToPath(input)
33 const nodeStream = fs.createReadStream(path)
34 const stream = Readable.toWeb(nodeStream) as ReadableStream<Uint8Array>
35 return new Response(stream, { headers: { 'Content-Type': type } })
36}