Skip to content

Commit

Permalink
worker: add support for .cjs extension
Browse files Browse the repository at this point in the history
PR-URL: #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>
  • Loading branch information
aduh95 authored and codebytere committed Feb 17, 2020
1 parent b35f83e commit 822f2ac
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,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);
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/worker-data.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { workerData, parentPort } = require('worker_threads');

parentPort.postMessage(workerData);
15 changes: 15 additions & 0 deletions test/parallel/test-worker-cjs-workerdata.js
Original file line number Diff line number Diff line change
@@ -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);
}));

0 comments on commit 822f2ac

Please sign in to comment.