forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
worker_threads: add type option to new Worker()
Allows eval of ESM string. Fixes: nodejs#30682
- Loading branch information
Showing
5 changed files
with
99 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const { Worker } = require('worker_threads'); | ||
|
||
const moduleString = 'export default import.meta.url;'; | ||
const cjsString = 'module.exports=__filename;'; | ||
|
||
const cjsLoadingESMError = /Unexpected token 'export'/; | ||
const esmLoadingCJSError = /module is not defined/; | ||
const invalidValueError = | ||
/The argument 'options\.type' must be either "module" or "classic"/; | ||
|
||
function runInWorker(evalString, options) { | ||
return new Promise((resolve, reject) => { | ||
const worker = new Worker(evalString, options); | ||
worker.on('error', reject); | ||
worker.on('exit', resolve); | ||
}); | ||
} | ||
|
||
let options; | ||
|
||
options = { eval: true }; | ||
assert.rejects(runInWorker(moduleString, options), cjsLoadingESMError); | ||
runInWorker(cjsString, options); // does not reject | ||
|
||
options = { eval: true, type: 'classic' }; | ||
assert.rejects(runInWorker(moduleString, options), cjsLoadingESMError); | ||
runInWorker(cjsString, options); // does not reject | ||
|
||
options = { eval: true, type: 'module' }; | ||
runInWorker(moduleString, options); // does not reject | ||
assert.rejects(runInWorker(cjsString, options), esmLoadingCJSError); | ||
|
||
options = { eval: true, type: 'wasm' }; | ||
assert.rejects(runInWorker(moduleString, options), invalidValueError); | ||
assert.rejects(runInWorker(cjsString, options), invalidValueError); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters