From 77125bc27adf43a83cdbc240cb8a3f6fc9500ead Mon Sep 17 00:00:00 2001 From: Antoine du HAMEL Date: Thu, 6 Feb 2020 15:39:47 +0100 Subject: [PATCH] worker_threads: add support for .cjs extension --- lib/internal/errors.js | 2 +- lib/internal/worker.js | 2 +- test/fixtures/worker-data.cjs | 3 +++ test/parallel/test-worker-cjs-workerdata.js | 15 +++++++++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/worker-data.cjs create mode 100644 test/parallel/test-worker-cjs-workerdata.js diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 0d88a92eb41ecc..8f2af51472ba55 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -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); diff --git a/lib/internal/worker.js b/lib/internal/worker.js index 683b1f6e6db8c7..d39150b8f208a7 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -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); } } diff --git a/test/fixtures/worker-data.cjs b/test/fixtures/worker-data.cjs new file mode 100644 index 00000000000000..731b77eb367863 --- /dev/null +++ b/test/fixtures/worker-data.cjs @@ -0,0 +1,3 @@ +const { workerData, parentPort } = require('worker_threads'); + +parentPort.postMessage(workerData); diff --git a/test/parallel/test-worker-cjs-workerdata.js b/test/parallel/test-worker-cjs-workerdata.js new file mode 100644 index 00000000000000..949011fee8173e --- /dev/null +++ b/test/parallel/test-worker-cjs-workerdata.js @@ -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); +}));