mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
worker: add support for .cjs extension
PR-URL: https://github.com/nodejs/node/pull/31662 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Yuta Hiroto <hello@hiroppy.me> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Denys Otrishko <shishugi@gmail.com>
This commit is contained in:
parent
94eb0f907b
commit
611a158f1a
@ -1371,7 +1371,7 @@ E('ERR_WORKER_PATH',
|
||||
E('ERR_WORKER_UNSERIALIZABLE_ERROR',
|
||||
'Serializing an uncaught exception failed', Error);
|
||||
E('ERR_WORKER_UNSUPPORTED_EXTENSION',
|
||||
'The worker script extension must be ".js" or ".mjs". Received "%s"',
|
||||
'The worker script extension must be ".js", ".mjs", or ".cjs". Received "%s"',
|
||||
TypeError);
|
||||
E('ERR_WORKER_UNSUPPORTED_OPERATION',
|
||||
'%s is not supported in workers', TypeError);
|
||||
|
@ -104,7 +104,7 @@ class Worker extends EventEmitter {
|
||||
filename = path.resolve(filename);
|
||||
|
||||
const ext = path.extname(filename);
|
||||
if (ext !== '.js' && ext !== '.mjs') {
|
||||
if (!/^\.[cm]?js$/.test(ext)) {
|
||||
throw new ERR_WORKER_UNSUPPORTED_EXTENSION(ext);
|
||||
}
|
||||
}
|
||||
|
3
test/fixtures/worker-data.cjs
vendored
Normal file
3
test/fixtures/worker-data.cjs
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
const { workerData, parentPort } = require('worker_threads');
|
||||
|
||||
parentPort.postMessage(workerData);
|
15
test/parallel/test-worker-cjs-workerdata.js
Normal file
15
test/parallel/test-worker-cjs-workerdata.js
Normal file
@ -0,0 +1,15 @@
|
||||
'use strict';
|
||||
const common = require('../common');
|
||||
const fixtures = require('../common/fixtures');
|
||||
const assert = require('assert');
|
||||
const { Worker } = require('worker_threads');
|
||||
|
||||
const workerData = 'Hello from main thread';
|
||||
|
||||
const worker = new Worker(fixtures.path('worker-data.cjs'), {
|
||||
workerData
|
||||
});
|
||||
|
||||
worker.on('message', common.mustCall((message) => {
|
||||
assert.strictEqual(message, workerData);
|
||||
}));
|
Loading…
Reference in New Issue
Block a user