@@ -21,6 +21,7 @@ class PoolWorker {
2121 this . activeJobs = 0 ;
2222 this . onJobDone = onJobDone ;
2323 this . id = workerId ;
24+ this . allowedFunctions = options . allowedFunctions ;
2425
2526 workerId += 1 ;
2627 // Empty or invalid node args would break the child process
@@ -106,9 +107,29 @@ class PoolWorker {
106107 } ) ;
107108 }
108109
110+ getReplacer ( ) {
111+ return ( key , value ) => {
112+ if (
113+ value instanceof Function &&
114+ Array . isArray ( this . allowedFunctions ) &&
115+ this . allowedFunctions . includes ( value . name )
116+ ) {
117+ return {
118+ __serialized_type : 'Function' ,
119+ fnBody : value . toString ( ) ,
120+ args : new Array ( value . length ) . fill ( 'a' ) . map ( ( i , dx ) => `${ i } ${ dx } ` ) ,
121+ } ;
122+ }
123+ return replacer ( key , value ) ;
124+ } ;
125+ }
126+
109127 writeJson ( data ) {
110128 const lengthBuffer = Buffer . alloc ( 4 ) ;
111- const messageBuffer = Buffer . from ( JSON . stringify ( data , replacer ) , 'utf-8' ) ;
129+ const messageBuffer = Buffer . from (
130+ JSON . stringify ( data , this . getReplacer ( ) ) ,
131+ 'utf-8'
132+ ) ;
112133 lengthBuffer . writeInt32BE ( messageBuffer . length , 0 ) ;
113134 this . writePipe . write ( lengthBuffer ) ;
114135 this . writePipe . write ( messageBuffer ) ;
@@ -323,6 +344,7 @@ export default class WorkerPool {
323344 this . poolTimeout = options . poolTimeout ;
324345 this . workerNodeArgs = options . workerNodeArgs ;
325346 this . workerParallelJobs = options . workerParallelJobs ;
347+ this . workerAllowedFunctions = options . workerAllowedFunctions ;
326348 this . workers = new Set ( ) ;
327349 this . activeJobs = 0 ;
328350 this . timeout = null ;
@@ -389,6 +411,7 @@ export default class WorkerPool {
389411 {
390412 nodeArgs : this . workerNodeArgs ,
391413 parallelJobs : this . workerParallelJobs ,
414+ allowedFunctions : this . workerAllowedFunctions ,
392415 } ,
393416 ( ) => this . onJobDone ( )
394417 ) ;
0 commit comments