Skip to content

Commit

Permalink
feat: allow named import usage
Browse files Browse the repository at this point in the history
The main exports of the package are now available both as named exports
from the package as well as the previously available properties from
the default export. This removes the requirement to use the `esModuleInterop`
TypeScript option but retains backward compatiblity for existing usage.
  • Loading branch information
clydin committed Feb 21, 2024
1 parent 07250a8 commit 6fead79
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"name": "piscina",
"version": "4.3.2",
"description": "A fast, efficient Node.js Worker Thread Pool implementation",
"main": "./dist/src/index.js",
"main": "./dist/src/main.js",
"exports": {
"types": "./dist/src/index.d.ts",
"import": "./dist/esm-wrapper.mjs",
"require": "./dist/src/index.js"
"require": "./dist/src/main.js"
},
"types": "./dist/src/index.d.ts",
"scripts": {
Expand Down
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ class ThreadPool {
}
}

class Piscina extends EventEmitterAsyncResource {
export default class Piscina extends EventEmitterAsyncResource {
#pool : ThreadPool;

constructor (options : Options = {}) {
Expand Down Expand Up @@ -1346,4 +1346,14 @@ class Piscina extends EventEmitterAsyncResource {
static get queueOptionsSymbol () { return kQueueOptions; }
}

export = Piscina;
export const move = Piscina.move;
export const isWorkerThread = Piscina.isWorkerThread;
export const workerData = Piscina.workerData;

export {
Piscina,
kTransferable as transferableSymbol,
kValue as valueSymbol,
kQueueOptions as queueOptionsSymbol,
version
};
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Piscina from './index';

// Used as the require() entry point to maintain existing behavior
export = Piscina;
6 changes: 6 additions & 0 deletions test/fixtures/simple-isworkerthread-named-import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { isWorkerThread } from '../..';
import assert from 'assert';

assert.strictEqual(isWorkerThread, true);

export default function () { return 'done'; }
6 changes: 6 additions & 0 deletions test/fixtures/simple-workerdata-named-import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { workerData } from '../..';
import assert from 'assert';

assert.strictEqual(workerData, 'ABC');

export default function () { return 'done'; }
18 changes: 18 additions & 0 deletions test/simple-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ test('Piscina.isWorkerThread has the correct value (worker)', async ({ equal })
equal(result, 'done');
});

test('Piscina.isWorkerThread has the correct value (worker) with named import', async ({ equal }) => {
const worker = new Piscina({
filename: resolve(__dirname, 'fixtures/simple-isworkerthread-named-import.ts')
});
const result = await worker.runTask(null);
equal(result, 'done');
});

test('Piscina instance is an EventEmitter', async ({ ok }) => {
const piscina = new Piscina();
ok(piscina instanceof EventEmitter);
Expand Down Expand Up @@ -120,6 +128,16 @@ test('passing valid workerData works', async ({ equal }) => {
await pool.runTask(null);
});

test('passing valid workerData works with named import', async ({ equal }) => {
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/simple-workerdata-named-import.ts'),
workerData: 'ABC'
});
equal(Piscina.workerData, undefined);

await pool.runTask(null);
});

test('passing invalid workerData does not work', async ({ throws }) => {
throws(() => new Piscina(({
filename: resolve(__dirname, 'fixtures/simple-workerdata.ts'),
Expand Down

0 comments on commit 6fead79

Please sign in to comment.