Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow named import usage #517

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1081,7 +1081,7 @@ class ThreadPool {
}
}

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

constructor (options : Options = {}) {
Expand Down Expand Up @@ -1365,4 +1365,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