@@ -8,13 +8,15 @@ import axios from 'axios'
8
8
export class NoirPluginClient extends PluginClient {
9
9
public internalEvents : EventManager
10
10
public parser : NoirParser
11
-
11
+ public ws : WebSocket
12
12
constructor ( ) {
13
13
super ( )
14
14
this . methods = [ 'init' , 'parse' , 'compile' ]
15
15
createClient ( this )
16
16
this . internalEvents = new EventManager ( )
17
17
this . parser = new NoirParser ( )
18
+ // @ts -ignore
19
+ this . ws = new WebSocket ( `${ WS_URL } ` )
18
20
this . onload ( )
19
21
}
20
22
@@ -24,6 +26,26 @@ export class NoirPluginClient extends PluginClient {
24
26
25
27
onActivation ( ) : void {
26
28
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
+ }
27
49
}
28
50
29
51
async setupNargoToml ( ) : Promise < void > {
@@ -35,36 +57,49 @@ export class NoirPluginClient extends PluginClient {
35
57
}
36
58
}
37
59
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 )
48
63
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
+ }
52
66
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 ( )
59
70
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 ( )
64
78
// @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` )
66
83
// @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' )
68
103
}
69
104
} catch ( e ) {
70
105
const regex = / ^ \s * ( \/ [ ^ : ] + ) : ( \d + ) : / gm;
0 commit comments