From d50391fe93a6319c2a554f34d39cce0c946564ec Mon Sep 17 00:00:00 2001 From: xqin Date: Wed, 22 May 2024 18:05:29 +0800 Subject: [PATCH] fix: support nodejs v16.x again (#572) --- .github/workflows/nodejs.yml | 4 ++-- src/common.ts | 14 ++++++++++++++ src/index.ts | 4 ++-- test/post-task.ts | 6 +++--- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 83252ded..1db01426 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -34,7 +34,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] - node-version: [18.x, 20.x, 22.x] + node-version: [16.x, 18.x, 20.x, 22.x] runs-on: ${{matrix.os}} steps: - uses: actions/checkout@v4 @@ -53,7 +53,7 @@ jobs: key: ${{ runner.os }}-node-${{ matrix.node-version }}-${{ hashFiles('**/package.json') }} restore-keys: | ${{ runner.os }}-node-${{ matrix.node-version }}- - + - name: Install Dependencies run: npm install - name: Run Tests diff --git a/src/common.ts b/src/common.ts index 51caa22e..9f372b4b 100644 --- a/src/common.ts +++ b/src/common.ts @@ -1,5 +1,6 @@ import type { Histogram } from 'node:perf_hooks'; import { fileURLToPath, URL } from 'node:url'; +import { availableParallelism, cpus } from 'node:os'; import type { HistogramSummary } from './types'; import { kMovable, kTransferable, kValue } from './symbols'; @@ -87,3 +88,16 @@ export function maybeFileURLToPath (filename : string) : string { ? fileURLToPath(new URL(filename)) : filename; } + +// TODO: drop on v5 +export function getAvailableParallelism () : number { + if (typeof availableParallelism === 'function') { + return availableParallelism(); + } + + try { + return cpus().length; + } catch { + return 1; + } +} diff --git a/src/index.ts b/src/index.ts index 9d985ac4..bfef7224 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,6 @@ import { Worker, MessageChannel, MessagePort, receiveMessageOnPort } from 'node:worker_threads'; import { once, EventEmitterAsyncResource } from 'node:events'; import { AsyncResource } from 'node:async_hooks'; -import { availableParallelism } from 'node:os'; import { resolve } from 'node:path'; import { inspect, types } from 'node:util'; import { RecordableHistogram, createHistogram, performance } from 'node:perf_hooks'; @@ -49,6 +48,7 @@ import { isMovable, createHistogramSummary, toHistogramIntegerNano, + getAvailableParallelism, maybeFileURLToPath } from './common'; import FixedQueue from './fixed-queue'; @@ -61,7 +61,7 @@ const { version } = JSON.parse( } )); -const cpuParallelism : number = availableParallelism(); +const cpuParallelism : number = getAvailableParallelism(); interface Options { filename? : string | null, diff --git a/test/post-task.ts b/test/post-task.ts index bdcbe3ff..8164756e 100644 --- a/test/post-task.ts +++ b/test/post-task.ts @@ -1,5 +1,5 @@ import { MessageChannel } from 'worker_threads'; -import { availableParallelism } from 'os'; +import { getAvailableParallelism } from '../dist/common'; import Piscina from '..'; import { test } from 'tap'; import { resolve } from 'path'; @@ -168,7 +168,7 @@ test('Piscina.maxThreads should return the max number of threads to be used (def filename: resolve(__dirname, 'fixtures/eval.js') }); - const maxThreads = (availableParallelism() ?? 1) * 1.5; + const maxThreads = getAvailableParallelism() * 1.5; equal(pool.maxThreads, maxThreads); }); @@ -189,7 +189,7 @@ test('Piscina.minThreads should return the max number of threads to be used (def const pool = new Piscina({ filename: resolve(__dirname, 'fixtures/eval.js') }); - const minThreads = Math.max(Math.floor(availableParallelism() / 2), 1); + const minThreads = Math.max(Math.floor(getAvailableParallelism() / 2), 1); plan(1); equal(pool.minThreads, minThreads);