@@ -27,8 +27,8 @@ const {
27
27
ERR_INVALID_ARG_TYPE ,
28
28
// eslint-disable-next-line no-unused-vars
29
29
ERR_WORKER_INIT_FAILED ,
30
+ ERR_INVALID_ARG_VALUE ,
30
31
} = errorCodes ;
31
- const { validateString } = require ( 'internal/validators' ) ;
32
32
const { getOptionValue } = require ( 'internal/options' ) ;
33
33
34
34
const workerIo = require ( 'internal/worker/io' ) ;
@@ -45,7 +45,7 @@ const {
45
45
WritableWorkerStdio
46
46
} = workerIo ;
47
47
const { deserializeError } = require ( 'internal/error-serdes' ) ;
48
- const { pathToFileURL } = require ( 'url' ) ;
48
+ const { fileURLToPath , isURLInstance , pathToFileURL } = require ( 'internal/ url' ) ;
49
49
50
50
const {
51
51
ownsProcessState,
@@ -86,7 +86,6 @@ class Worker extends EventEmitter {
86
86
constructor ( filename , options = { } ) {
87
87
super ( ) ;
88
88
debug ( `[${ threadId } ] create new worker` , filename , options ) ;
89
- validateString ( filename , 'filename' ) ;
90
89
if ( options . execArgv && ! ArrayIsArray ( options . execArgv ) ) {
91
90
throw new ERR_INVALID_ARG_TYPE ( 'options.execArgv' ,
92
91
'Array' ,
@@ -99,11 +98,33 @@ class Worker extends EventEmitter {
99
98
}
100
99
argv = options . argv . map ( String ) ;
101
100
}
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 {
104
126
throw new ERR_WORKER_PATH ( filename ) ;
105
127
}
106
- filename = path . resolve ( filename ) ;
107
128
108
129
const ext = path . extname ( filename ) ;
109
130
if ( ext !== '.js' && ext !== '.mjs' && ext !== '.cjs' ) {
@@ -125,7 +146,6 @@ class Worker extends EventEmitter {
125
146
options . env ) ;
126
147
}
127
148
128
- const url = options . eval ? null : pathToFileURL ( filename ) ;
129
149
// Set up the C++ handle for the worker, as well as some internal wiring.
130
150
this [ kHandle ] = new WorkerImpl ( url ,
131
151
env === process . env ? null : env ,
0 commit comments