@@ -15,17 +15,32 @@ const getWasm = (() => {
15
15
let wasm = null
16
16
let cnt = 0
17
17
let run = false
18
+ let int
18
19
async function init ( ) {
19
20
go = new _Go ( ) ;
20
21
run = true
21
22
const _wasm = await WebAssembly . instantiate (
22
23
gunzipSync ( fs . readFileSync ( WASM_URL ) ) , go . importObject )
23
- setInterval ( ( ) => {
24
+ int && clearInterval ( int )
25
+ int = setInterval ( ( ) => {
24
26
console . log ( `WASM SIZE: ${ Math . floor ( wasm . exports . memory . buffer . byteLength / 1024 / 1024 ) } MB` )
25
27
} , 5000 )
26
28
go . run ( _wasm . instance )
27
29
wasm = _wasm . instance
28
30
wasm . exports . setMaxSamples ( process . env . ADVANCED_PROMETHEUS_MAX_SAMPLES || 5000000 )
31
+ wasm . exportsWrap = Object . fromEntries (
32
+ Object . entries ( wasm . exports ) . map ( ( [ _k , _v ] ) => {
33
+ return [ _k , ( ...args ) => {
34
+ const _wasm = wasm
35
+ try {
36
+ return _wasm . exports [ _k ] . bind ( _wasm ) ( ...args )
37
+ } catch ( e ) {
38
+ _wasm === wasm && init ( )
39
+ throw e
40
+ }
41
+ } ]
42
+ } )
43
+ )
29
44
cnt = 0
30
45
run = false
31
46
}
@@ -56,7 +71,7 @@ module.exports.pqlRangeQuery = async (query, startMs, endMs, stepMs, getData) =>
56
71
const end = endMs || Date . now ( )
57
72
const step = stepMs || 15000
58
73
return await pql ( query ,
59
- ( ctx ) => _wasm . exports . pqlRangeQuery ( ctx . id , start , end , step ) ,
74
+ ( ctx ) => _wasm . exportsWrap . pqlRangeQuery ( ctx . id , start , end , step ) ,
60
75
( matchers ) => getData ( matchers , start , end ) )
61
76
}
62
77
@@ -71,7 +86,7 @@ module.exports.pqlInstantQuery = async (query, timeMs, getData) => {
71
86
const time = timeMs || Date . now ( )
72
87
const _wasm = getWasm ( )
73
88
return await pql ( query ,
74
- ( ctx ) => _wasm . exports . pqlInstantQuery ( ctx . id , time ) ,
89
+ ( ctx ) => _wasm . exportsWrap . pqlInstantQuery ( ctx . id , time ) ,
75
90
( matchers ) => getData ( matchers , time - 300000 , time ) )
76
91
}
77
92
@@ -82,7 +97,7 @@ module.exports.pqlMatchers = (query) => {
82
97
ctx . create ( )
83
98
try {
84
99
ctx . write ( query )
85
- const res1 = _wasm . exports . pqlSeries ( id )
100
+ const res1 = _wasm . exportsWrap . pqlSeries ( id )
86
101
if ( res1 !== 0 ) {
87
102
throw new WasmError ( ctx . read ( ) )
88
103
}
@@ -124,7 +139,7 @@ module.exports.TranspileTraceQL = (request) => {
124
139
_ctx = new Ctx ( id , _wasm )
125
140
_ctx . create ( )
126
141
_ctx . write ( JSON . stringify ( request ) )
127
- let res = _wasm . exports . transpileTraceQL ( id )
142
+ let res = _wasm . exportsWrap . transpileTraceQL ( id )
128
143
if ( res !== 0 ) {
129
144
throw new WasmError ( _ctx . read ( ) )
130
145
}
@@ -168,7 +183,7 @@ const pql = async (query, wasmCall, getData) => {
168
183
writer . writeBytes ( [ data ] )
169
184
}
170
185
ctx . write ( writer . buffer ( ) )
171
- _wasm . exports . onDataLoad ( reqId )
186
+ _wasm . exportsWrap . onDataLoad ( reqId )
172
187
return ctx . read ( )
173
188
} finally {
174
189
ctx && ctx . destroy ( )
@@ -183,7 +198,7 @@ class Ctx {
183
198
184
199
create ( ) {
185
200
try {
186
- this . wasm . exports . createCtx ( this . id )
201
+ this . wasm . exportsWrap . createCtx ( this . id )
187
202
this . created = true
188
203
} catch ( err ) {
189
204
throw err
@@ -192,7 +207,7 @@ class Ctx {
192
207
193
208
destroy ( ) {
194
209
try {
195
- if ( this . created ) this . wasm . exports . dealloc ( this . id )
210
+ if ( this . created ) this . wasm . exportsWrap . dealloc ( this . id )
196
211
} catch ( err ) {
197
212
throw err
198
213
}
@@ -206,8 +221,8 @@ class Ctx {
206
221
if ( typeof data === 'string' ) {
207
222
data = ( new TextEncoder ( ) ) . encode ( data )
208
223
}
209
- this . wasm . exports . alloc ( this . id , data . length )
210
- const ptr = this . wasm . exports . alloc ( this . id , data . length )
224
+ this . wasm . exportsWrap . alloc ( this . id , data . length )
225
+ const ptr = this . wasm . exportsWrap . alloc ( this . id , data . length )
211
226
new Uint8Array ( this . wasm . exports . memory . buffer ) . set ( data , ptr )
212
227
}
213
228
@@ -216,8 +231,8 @@ class Ctx {
216
231
*/
217
232
read ( ) {
218
233
const [ resPtr , resLen ] = [
219
- this . wasm . exports . getCtxResponse ( this . id ) ,
220
- this . wasm . exports . getCtxResponseLen ( this . id )
234
+ this . wasm . exportsWrap . getCtxResponse ( this . id ) ,
235
+ this . wasm . exportsWrap . getCtxResponseLen ( this . id )
221
236
]
222
237
return new TextDecoder ( ) . decode ( new Uint8Array ( this . wasm . exports . memory . buffer ) . subarray ( resPtr , resPtr + resLen ) )
223
238
}
0 commit comments