@@ -27,8 +27,8 @@ const {
2727 ERR_INVALID_ARG_TYPE ,
2828 // eslint-disable-next-line no-unused-vars
2929 ERR_WORKER_INIT_FAILED ,
30+ ERR_INVALID_ARG_VALUE ,
3031} = errorCodes ;
31- const { validateString } = require ( 'internal/validators' ) ;
3232const { getOptionValue } = require ( 'internal/options' ) ;
3333
3434const workerIo = require ( 'internal/worker/io' ) ;
@@ -45,7 +45,7 @@ const {
4545 WritableWorkerStdio
4646} = workerIo ;
4747const { deserializeError } = require ( 'internal/error-serdes' ) ;
48- const { pathToFileURL } = require ( 'url' ) ;
48+ const { fileURLToPath , isURLInstance , pathToFileURL } = require ( 'internal/ url' ) ;
4949
5050const {
5151 ownsProcessState,
@@ -86,7 +86,6 @@ class Worker extends EventEmitter {
8686 constructor ( filename , options = { } ) {
8787 super ( ) ;
8888 debug ( `[${ threadId } ] create new worker` , filename , options ) ;
89- validateString ( filename , 'filename' ) ;
9089 if ( options . execArgv && ! ArrayIsArray ( options . execArgv ) ) {
9190 throw new ERR_INVALID_ARG_TYPE ( 'options.execArgv' ,
9291 'Array' ,
@@ -99,11 +98,33 @@ class Worker extends EventEmitter {
9998 }
10099 argv = options . argv . map ( String ) ;
101100 }
102- if ( ! options . eval ) {
103- if ( ! path . isAbsolute ( filename ) && ! / ^ \. \. ? [ \\ / ] / . test ( filename ) ) {
101+
102+ let url ;
103+ if ( options . eval ) {
104+ if ( typeof filename !== 'string' ) {
105+ throw new ERR_INVALID_ARG_VALUE (
106+ 'options.eval' ,
107+ options . eval ,
108+ 'must be false when \'filename\' is not a string'
109+ ) ;
110+ }
111+ url = null ;
112+ } else {
113+ if ( isURLInstance ( filename ) ) {
114+ url = filename ;
115+ filename = fileURLToPath ( filename ) ;
116+ } else if ( typeof filename !== 'string' ) {
117+ throw new ERR_INVALID_ARG_TYPE (
118+ 'filename' ,
119+ [ 'string' , 'URL' ] ,
120+ filename
121+ ) ;
122+ } else if ( path . isAbsolute ( filename ) || / ^ \. \. ? [ \\ / ] / . test ( filename ) ) {
123+ filename = path . resolve ( filename ) ;
124+ url = pathToFileURL ( filename ) ;
125+ } else {
104126 throw new ERR_WORKER_PATH ( filename ) ;
105127 }
106- filename = path . resolve ( filename ) ;
107128
108129 const ext = path . extname ( filename ) ;
109130 if ( ext !== '.js' && ext !== '.mjs' && ext !== '.cjs' ) {
@@ -125,7 +146,6 @@ class Worker extends EventEmitter {
125146 options . env ) ;
126147 }
127148
128- const url = options . eval ? null : pathToFileURL ( filename ) ;
129149 // Set up the C++ handle for the worker, as well as some internal wiring.
130150 this [ kHandle ] = new WorkerImpl ( url ,
131151 env === process . env ? null : env ,
0 commit comments