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:
Antoine du HAMEL 2020-02-06 15:39:47 +01:00 committed by Anna Henningsen
parent 94eb0f907b
commit 611a158f1a
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
4 changed files with 20 additions and 2 deletions

View File

@ -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);

View File

@ -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
View File

@ -0,0 +1,3 @@
const { workerData, parentPort } = require('worker_threads');
parentPort.postMessage(workerData);

View 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);
}));