File tree Expand file tree Collapse file tree 3 files changed +40
-3
lines changed Expand file tree Collapse file tree 3 files changed +40
-3
lines changed Original file line number Diff line number Diff line change @@ -856,4 +856,7 @@ E('ERR_WORKER_NEED_ABSOLUTE_PATH',
856856 TypeError ) ;
857857E ( 'ERR_WORKER_UNSERIALIZABLE_ERROR' ,
858858 'Serializing an uncaught exception failed' , Error ) ;
859+ E ( 'ERR_WORKER_UNSUPPORTED_EXTENSION' ,
860+ 'The worker script extension must be ".js" or ".mjs". Received "%s"' ,
861+ TypeError ) ;
859862E ( 'ERR_ZLIB_INITIALIZATION_FAILED' , 'Initialization failed' , Error ) ;
Original file line number Diff line number Diff line change @@ -8,7 +8,8 @@ const util = require('util');
88const {
99 ERR_INVALID_ARG_TYPE ,
1010 ERR_WORKER_NEED_ABSOLUTE_PATH ,
11- ERR_WORKER_UNSERIALIZABLE_ERROR
11+ ERR_WORKER_UNSERIALIZABLE_ERROR ,
12+ ERR_WORKER_UNSUPPORTED_EXTENSION ,
1213} = require ( 'internal/errors' ) . codes ;
1314
1415const { internalBinding } = require ( 'internal/bootstrap/loaders' ) ;
@@ -136,8 +137,14 @@ class Worker extends EventEmitter {
136137 throw new ERR_INVALID_ARG_TYPE ( 'filename' , 'string' , filename ) ;
137138 }
138139
139- if ( ! options . eval && ! path . isAbsolute ( filename ) ) {
140- throw new ERR_WORKER_NEED_ABSOLUTE_PATH ( filename ) ;
140+ if ( ! options . eval ) {
141+ if ( ! path . isAbsolute ( filename ) ) {
142+ throw new ERR_WORKER_NEED_ABSOLUTE_PATH ( filename ) ;
143+ }
144+ const ext = path . extname ( filename ) ;
145+ if ( ext !== '.js' && ext !== '.mjs' ) {
146+ throw new ERR_WORKER_UNSUPPORTED_EXTENSION ( ext ) ;
147+ }
141148 }
142149
143150 // Set up the C++ handle for the worker, as well as some internal wiring.
Original file line number Diff line number Diff line change 1+ // Flags: --experimental-worker
2+ 'use strict' ;
3+
4+ const common = require ( '../common' ) ;
5+ const assert = require ( 'assert' ) ;
6+ const { Worker } = require ( 'worker' ) ;
7+
8+ {
9+ const expectedErr = common . expectsError ( {
10+ code : 'ERR_WORKER_NEED_ABSOLUTE_PATH' ,
11+ type : TypeError
12+ } , 4 ) ;
13+ assert . throws ( ( ) => { new Worker ( 'a.js' ) ; } , expectedErr ) ;
14+ assert . throws ( ( ) => { new Worker ( 'b' ) ; } , expectedErr ) ;
15+ assert . throws ( ( ) => { new Worker ( 'c/d.js' ) ; } , expectedErr ) ;
16+ assert . throws ( ( ) => { new Worker ( 'a.mjs' ) ; } , expectedErr ) ;
17+ }
18+
19+ {
20+ const expectedErr = common . expectsError ( {
21+ code : 'ERR_WORKER_UNSUPPORTED_EXTENSION' ,
22+ type : TypeError
23+ } , 3 ) ;
24+ assert . throws ( ( ) => { new Worker ( '/b' ) ; } , expectedErr ) ;
25+ assert . throws ( ( ) => { new Worker ( '/c.wasm' ) ; } , expectedErr ) ;
26+ assert . throws ( ( ) => { new Worker ( '/d.txt' ) ; } , expectedErr ) ;
27+ }
You can’t perform that action at this time.
0 commit comments