Skip to content

Commit 31e899f

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 31e899f

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
},
1818
{
1919
"url": "https://circuitverse.org/api/v1/auth/signup"
20+
},
21+
{
22+
"url": "https://circuitverse.org/api/v1/simulator/verilogcv"
2023
}
2124
]
2225
},

src/simulator/src/Verilog2CV.js

Lines changed: 28 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,28 @@ 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 { fetch } = await import('@tauri-apps/plugin-http')
276+
fetchFn = fetch
277+
} catch (error) {
278+
console.error('Failed to load Tauri HTTP plugin:', error)
279+
}
280+
}
281+
282+
fetchFn(apiUrl, {
269283
method: 'POST',
270284
headers: {
271285
'Content-Type': 'application/json',
@@ -276,6 +290,10 @@ export default function generateVerilogCircuit(
276290
if (!response.ok) {
277291
throw response
278292
}
293+
const contentType = response.headers.get('content-type')
294+
if (!contentType || !contentType.includes('application/json')) {
295+
throw new Error('Invalid response from server (not JSON)')
296+
}
279297
return response.json()
280298
})
281299
.then((circuitData) => {
@@ -297,14 +315,18 @@ export default function generateVerilogCircuit(
297315
setVerilogOutput('Verilog Circuit Successfully Created', 'success')
298316
})
299317
.catch((error) => {
318+
console.error('Verilog compilation error:', error)
319+
showError('Verilog compilation failed')
300320
if (error.status == 500) {
301-
showError('Could not connect to Yosys')
302321
setVerilogOutput('Could not connect to Yosys server', 'error')
303-
} else {
304-
showError('There is some issue with the code')
322+
} else if (typeof error.json === 'function') {
305323
error.json().then((errorMessage) => {
306-
setVerilogOutput(errorMessage.message, 'error')
324+
setVerilogOutput(errorMessage.message || 'Unknown error', 'error')
325+
}).catch(() => {
326+
setVerilogOutput('Error parsing error response', 'error')
307327
})
328+
} else {
329+
setVerilogOutput(error.message || 'Unknown error occurred', 'error')
308330
}
309331
})
310332
}

0 commit comments

Comments
 (0)