Skip to content

Commit 541718d

Browse files
committed
Use websocket endpoint for displaying compilation details
1 parent 4fd77e5 commit 541718d

File tree

1 file changed

+61
-26
lines changed

1 file changed

+61
-26
lines changed

apps/noir-compiler/src/app/services/noirPluginClient.ts

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import axios from 'axios'
88
export class NoirPluginClient extends PluginClient {
99
public internalEvents: EventManager
1010
public parser: NoirParser
11-
11+
public ws: WebSocket
1212
constructor() {
1313
super()
1414
this.methods = ['init', 'parse', 'compile']
1515
createClient(this)
1616
this.internalEvents = new EventManager()
1717
this.parser = new NoirParser()
18+
// @ts-ignore
19+
this.ws = new WebSocket(`${WS_URL}`)
1820
this.onload()
1921
}
2022

@@ -24,6 +26,26 @@ export class NoirPluginClient extends PluginClient {
2426

2527
onActivation(): void {
2628
this.internalEvents.emit('noir_activated')
29+
this.setupWebSocketEvents()
30+
}
31+
32+
setupWebSocketEvents(): void {
33+
this.ws.onopen = () => {
34+
console.log('WebSocket connection opened')
35+
}
36+
this.ws.onmessage = (event) => {
37+
const message = JSON.parse(event.data)
38+
39+
if (message.logMsg) {
40+
this.debugFn(message.logMsg)
41+
}
42+
}
43+
this.ws.onerror = (event) => {
44+
this.logFn('WebSocket error: ' + event)
45+
}
46+
this.ws.onclose = () => {
47+
console.log('WebSocket connection closed')
48+
}
2749
}
2850

2951
async setupNargoToml(): Promise<void> {
@@ -35,36 +57,49 @@ export class NoirPluginClient extends PluginClient {
3557
}
3658
}
3759

38-
async compile(path: string): Promise<void> {
39-
try {
40-
this.internalEvents.emit('noir_compiling_start')
41-
this.emit('statusChanged', { key: 'loading', title: 'Compiling Noir Program...', type: 'info' })
42-
// @ts-ignore
43-
this.call('terminal', 'log', { type: 'log', value: 'Compiling ' + path })
44-
await this.setupNargoToml()
45-
// @ts-ignore
46-
const zippedProject: Blob = await this.call('fileManager', 'download', '/', false)
47-
const formData = new FormData()
60+
generateRequestID(): string {
61+
const timestamp = Math.floor(Date.now() / 1000)
62+
const random = Math.random().toString(36).substring(2, 15)
4863

49-
formData.append('file', zippedProject, `${extractNameFromKey(path)}.zip`)
50-
// @ts-ignore
51-
const response = await axios.post(`${BASE_URL}/compile`, formData)
64+
return `req_${timestamp}_${random}`
65+
}
5266

53-
if (!response.data || !response.data.success) {
54-
this.internalEvents.emit('noir_compiling_errored', new Error('Compilation failed'))
55-
this.logFn('Compilation failed')
56-
return
57-
} else {
58-
const { compiledJson, proverToml } = response.data
67+
async compile(path: string): Promise<void> {
68+
try {
69+
const requestID = this.generateRequestID()
5970

60-
this.call('fileManager', 'writeFile', 'build/program.json', compiledJson)
61-
this.call('fileManager', 'writeFile', 'build/prover.toml', proverToml)
62-
this.internalEvents.emit('noir_compiling_done')
63-
this.emit('statusChanged', { key: 'succeed', title: 'Noir circuit compiled successfully', type: 'success' })
71+
if (this.ws.readyState === WebSocket.OPEN) {
72+
this.ws.send(JSON.stringify({ requestId: requestID }))
73+
this.internalEvents.emit('noir_compiling_start')
74+
this.emit('statusChanged', { key: 'loading', title: 'Compiling Noir Program...', type: 'info' })
75+
// @ts-ignore
76+
this.call('terminal', 'log', { type: 'log', value: 'Compiling ' + path })
77+
await this.setupNargoToml()
6478
// @ts-ignore
65-
this.debugFn('Compiled successfully')
79+
const zippedProject: Blob = await this.call('fileManager', 'download', '/', false)
80+
const formData = new FormData()
81+
82+
formData.append('file', zippedProject, `${extractNameFromKey(path)}.zip`)
6683
// @ts-ignore
67-
await this.call('editor', 'clearErrorMarkers', [path])
84+
const response = await axios.post(`${BASE_URL}/compile?requestId=${requestID}`, formData)
85+
86+
if (!response.data || !response.data.success) {
87+
this.internalEvents.emit('noir_compiling_errored', new Error('Compilation failed'))
88+
this.logFn('Compilation failed')
89+
return
90+
} else {
91+
const { compiledJson, proverToml } = response.data
92+
93+
this.call('fileManager', 'writeFile', 'build/program.json', compiledJson)
94+
this.call('fileManager', 'writeFile', 'build/prover.toml', proverToml)
95+
this.internalEvents.emit('noir_compiling_done')
96+
this.emit('statusChanged', { key: 'succeed', title: 'Noir circuit compiled successfully', type: 'success' })
97+
// @ts-ignore
98+
await this.call('editor', 'clearErrorMarkers', [path])
99+
}
100+
} else {
101+
this.internalEvents.emit('noir_compiling_errored', new Error('Compilation failed: WebSocket connection not open'))
102+
this.logFn('Compilation failed: WebSocket connection not open')
68103
}
69104
} catch (e) {
70105
const regex = /^\s*(\/[^:]+):(\d+):/gm;

0 commit comments

Comments
 (0)