@@ -75,7 +75,7 @@ const apiClient = new Mwn({
7575} ) ;
7676apiClient . setRequestOptions ( { timeout : 10000 } ) ;
7777
78- const preprocessCodeTemplate = fs . readFileSync ( __dirname + '/isolate.vm.js' )
78+ const postprocessCodeTemplate = fs . readFileSync ( __dirname + '/isolate.vm.js' )
7979 . toString ( )
8080 . replace ( / ^ \/ \* .* ?\* \/ $ / m, '' ) ; // remove linter comments /* ... */
8181
@@ -130,9 +130,9 @@ async function makeSandboxedHttpRequest(url: string) {
130130 }
131131}
132132
133- export async function applyJsPreprocessing ( rows : Record < string , string > [ ] , jsCode : string , query : Query ) : Promise < Record < string , any > [ ] > {
134- log ( `[+] Applying JS preprocessing for ${ query } ` ) ;
135- query . emit ( 'preprocessing ' ) ;
133+ export async function applyJsPostProcessing ( rows : Record < string , string > [ ] , jsCode : string , query : Query ) : Promise < Record < string , any > [ ] > {
134+ log ( `[+] Applying JS postprocessing for ${ query } ` ) ;
135+ query . emit ( 'postprocessing ' ) ;
136136 let startTime = process . hrtime . bigint ( ) ;
137137
138138 // Import dynamically as this has native dependencies
@@ -172,10 +172,10 @@ export async function applyJsPreprocessing(rows: Record<string, string>[], jsCod
172172
173173 let result = rows ;
174174
175- let doPreprocessing = async ( ) => {
175+ let doPostProcessing = async ( ) => {
176176 try {
177- // jsCode is expected to declare function preprocess (rows) {...}
178- let fullCode = preprocessCodeTemplate . replace ( '"${JS_CODE}"' , jsCode ) ;
177+ // jsCode is expected to declare function postprocess (rows) {...}
178+ let fullCode = postprocessCodeTemplate . replace ( '"${JS_CODE}"' , jsCode ) ;
179179 let wrapped = await context . eval ( fullCode , {
180180 reference : true ,
181181 timeout : softTimeout
@@ -190,29 +190,29 @@ export async function applyJsPreprocessing(rows: Record<string, string>[], jsCod
190190 if ( Array . isArray ( userCodeResultParsed ) ) {
191191 result = userCodeResultParsed ;
192192 } else {
193- log ( `[E] JS preprocessing for ${ query } returned a non-array: ${ userCodeResult . slice ( 0 , 100 ) } ... Ignoring.` ) ;
194- query . warnings . push ( `JS preprocessing didn't return an array of rows, will be ignored` ) ;
193+ log ( `[E] JS postprocessing for ${ query } returned a non-array: ${ userCodeResult . slice ( 0 , 100 ) } ... Ignoring.` ) ;
194+ query . warnings . push ( `JS postprocessing didn't return an array of rows, will be ignored` ) ;
195195 query . emit ( 'js-no-array' ) ;
196196 }
197197 } else {
198- log ( `[E] JS preprocessing for ${ query } has an invalid return value: ${ userCodeResult } . Ignoring.` ) ;
199- query . warnings . push ( `JS preprocessing must have a transferable return value` ) ;
198+ log ( `[E] JS postprocessing for ${ query } has an invalid return value: ${ userCodeResult } . Ignoring.` ) ;
199+ query . warnings . push ( `JS postprocessing must have a transferable return value` ) ;
200200 query . emit ( 'js-invalid-return' ) ;
201201 }
202202 } catch ( e ) { // Shouldn't occur as we are the ones doing the JSON.stringify
203- log ( `[E] JS preprocessing for ${ query } returned a non-JSON: ${ userCodeResult . slice ( 0 , 100 ) } . Ignoring.` ) ;
203+ log ( `[E] JS postprocessing for ${ query } returned a non-JSON: ${ userCodeResult . slice ( 0 , 100 ) } . Ignoring.` ) ;
204204 }
205205 } catch ( e ) {
206- log ( `[E] JS preprocessing for ${ query } failed: ${ e . toString ( ) } ` ) ;
206+ log ( `[E] JS postprocessing for ${ query } failed: ${ e . toString ( ) } ` ) ;
207207 log ( e ) ;
208- query . warnings . push ( `JS preprocessing failed: ${ e . toString ( ) } ` ) ;
208+ query . warnings . push ( `JS postprocessing failed: ${ e . toString ( ) } ` ) ;
209209 query . emit ( 'js-failed' , e . toString ( ) ) ;
210210 }
211211 }
212212
213213 await timedPromise (
214214 hardTimeout ,
215- doPreprocessing ( ) ,
215+ doPostProcessing ( ) ,
216216 ( ) => {
217217 // In case isolated-vm timeout doesn't work
218218 log ( `[E] Past ${ hardTimeout / 1000 } second timeout, force-disposing isolate` ) ;
@@ -222,8 +222,8 @@ export async function applyJsPreprocessing(rows: Record<string, string>[], jsCod
222222
223223 let endTime = process . hrtime . bigint ( ) ;
224224 let timeTaken = ( Number ( endTime - startTime ) / 1e9 ) . toFixed ( 3 ) ;
225- log ( `[+] JS preprocessing for ${ query } took ${ timeTaken } seconds, cpuTime: ${ isolate . cpuTime } , wallTime: ${ isolate . wallTime } .` ) ;
226- query . emit ( 'preprocessing -complete' , timeTaken ) ;
225+ log ( `[+] JS postprocessing for ${ query } took ${ timeTaken } seconds, cpuTime: ${ isolate . cpuTime } , wallTime: ${ isolate . wallTime } .` ) ;
226+ query . emit ( 'postprocessing -complete' , timeTaken ) ;
227227
228228 return result ;
229229}
0 commit comments