Skip to content

Commit 990551c

Browse files
Nakshatra SharmaNakshatra Sharma
authored andcommitted
Fix Tauri Verilog compilation: use HTTP plugin to bypass CORS and improve error handling
1 parent 5fb1a1b commit 990551c

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@mdi/js": "^7.4.47",
2020
"@tauri-apps/api": "2.3.0",
2121
"@tauri-apps/plugin-fs": "2.2.0",
22+
"@tauri-apps/plugin-http": "^2.5.7",
2223
"@tiptap/core": "^2.0.3",
2324
"@tiptap/extension-character-count": "^2.0.3",
2425
"@tiptap/extension-subscript": "^2.0.3",
@@ -76,4 +77,4 @@
7677
"optionalDependencies": {
7778
"esbuild-linux-64": "^0.15.18"
7879
}
79-
}
80+
}

src-tauri/capabilities/default.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
},
1818
{
1919
"url": "https://circuitverse.org/api/v1/auth/signup"
20+
},
21+
{
22+
"url": "https://circuitverse.org/api/v1/simulator/verilogcv"
23+
},
24+
{
25+
"url": "https://circuitverse.org/*"
2026
}
2127
]
2228
},

src/simulator/src/Verilog2CV.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { showError, showMessage } from './utils'
3434
import { showProperties } from './ux'
3535
import { useSimulatorMobileStore } from '#/store/simulatorMobileStore'
3636
import { toRefs } from 'vue'
37+
import { isTauri } from '@tauri-apps/api/core'
3738

3839
var editor
3940
var verilogMode = false
@@ -257,15 +258,30 @@ export function YosysJSON2CV(
257258
}
258259
}
259260

260-
export default function generateVerilogCircuit(
261+
export default async function generateVerilogCircuit(
261262
verilogCode,
262263
scope = globalScope
263264
) {
264265
clearVerilogOutput()
265266
setVerilogOutput('Compiling Verilog code...', 'info')
266267

267268
var params = { code: verilogCode }
268-
fetch('/api/v1/simulator/verilogcv', {
269+
let apiUrl = '/api/v1/simulator/verilogcv'
270+
let fetchFn = window.fetch
271+
272+
if (isTauri()) {
273+
apiUrl = `https://circuitverse.org${apiUrl}`
274+
try {
275+
const http = await import('@tauri-apps/plugin-http')
276+
if (http && http.fetch) {
277+
fetchFn = http.fetch
278+
}
279+
} catch (error) {
280+
console.error('Failed to load Tauri HTTP plugin:', error)
281+
}
282+
}
283+
284+
fetchFn(apiUrl, {
269285
method: 'POST',
270286
headers: {
271287
'Content-Type': 'application/json',
@@ -276,6 +292,10 @@ export default function generateVerilogCircuit(
276292
if (!response.ok) {
277293
throw response
278294
}
295+
const contentType = response.headers.get('content-type')
296+
if (!contentType || !contentType.includes('application/json')) {
297+
throw new Error('Invalid response from server (not JSON)')
298+
}
279299
return response.json()
280300
})
281301
.then((circuitData) => {
@@ -297,14 +317,18 @@ export default function generateVerilogCircuit(
297317
setVerilogOutput('Verilog Circuit Successfully Created', 'success')
298318
})
299319
.catch((error) => {
320+
console.error('Verilog compilation error:', error)
321+
showError('Verilog compilation failed')
300322
if (error.status == 500) {
301-
showError('Could not connect to Yosys')
302323
setVerilogOutput('Could not connect to Yosys server', 'error')
303-
} else {
304-
showError('There is some issue with the code')
324+
} else if (typeof error.json === 'function') {
305325
error.json().then((errorMessage) => {
306-
setVerilogOutput(errorMessage.message, 'error')
326+
setVerilogOutput(errorMessage.message || 'Unknown error', 'error')
327+
}).catch(() => {
328+
setVerilogOutput('Error parsing error response', 'error')
307329
})
330+
} else {
331+
setVerilogOutput(error.message || 'Unknown error occurred', 'error')
308332
}
309333
})
310334
}

0 commit comments

Comments
 (0)