Skip to content

Commit

Permalink
added ESM support for worker scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaWise committed Jun 20, 2023
1 parent 53f5aa4 commit 260c083
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/worker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
const url = require('url');
const worker = require('worker_threads');
const Movable = require('./movable');
const { OP_YIELD, OP_RESPONSE, OP_CALLBACK, OP_GENERATOR, OP_READY, WOP_REQUEST, WOP_CALLBACK } = require('./constants');
Expand All @@ -24,8 +25,13 @@ worker.workerData = worker.workerData.workerData;
worker.parentPort = new worker.MessageChannel().port1;

// Load the user's worker script. It may export a promise.
// TODO: add support for ESM files
Promise.resolve(require(FILENAME)).then((methods) => {
new Promise((resolve) => {
try {
resolve(require(FILENAME));
} catch (_) {
resolve(import(url.pathToFileURL(FILENAME).href));
}
}).then((methods) => {
if (typeof methods !== 'object' || methods === null) {
throw new TypeError('Worker must export an object');
}
Expand Down
8 changes: 5 additions & 3 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { expect } = require('chai');
const ThreadPool = require('..');

const WORKER = require.resolve('./workers/worker.js');
const WORKER_ESM = require.resolve('./workers/worker-esm.mjs');
const INVALID_WORKER = require.resolve('./workers/invalid-worker.js');
const INVALID_WORKER_ASYNC = require.resolve('./workers/invalid-worker-async.js');
const INVALID_WORKER_EXPORT = require.resolve('./workers/invalid-worker-export.js');
Expand Down Expand Up @@ -39,9 +40,6 @@ describe('basic functionality', function () {
expect(() => new ThreadPool({ filename: 'workers/worker.js' })).to.throw(TypeError);
expect(() => new ThreadPool({ filename: 'test/workers/worker.js' })).to.throw(TypeError);
});
it('does not accept windows-style absolute paths for the filename', function () {
expect(() => new ThreadPool({ filename: path.win32.normalize(WORKER) })).to.throw(TypeError);
});
it('does not accept non-JS file extensions in the filename', function () {
expect(() => new ThreadPool({ filename: WORKER.slice(0, -3) + '.css' })).to.throw(TypeError);
expect(() => new ThreadPool({ filename: WORKER.slice(0, -3) + '.jsx' })).to.throw(TypeError);
Expand Down Expand Up @@ -90,6 +88,10 @@ describe('basic functionality', function () {
});
});
});
it('accepts worker scripts as ESM modules', async function () {
pool = new ThreadPool({ filename: WORKER_ESM });
expect(await pool.call('echo', 123)).to.deep.equal([123]);
});
});

describe('ThreadPool properties', function () {
Expand Down
3 changes: 3 additions & 0 deletions test/workers/worker-esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function echo(...args) {
return args;
}

0 comments on commit 260c083

Please sign in to comment.