From c46c1b12ef34ac9c010000f8e71a77dc7cb06642 Mon Sep 17 00:00:00 2001 From: Tuan Anh Tran Date: Sun, 31 Jan 2021 22:21:08 +0700 Subject: [PATCH] fix: load piscina only if available Signed-off-by: Tuan Anh Tran --- .github/workflows/ci.yaml | 2 +- index.js | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ae1847a..4787603 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -9,7 +9,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] - node-version: [12.x, 13.x, 14.x, 15.x] + node-version: [10.x, 11.x, 12.x, 13.x, 14.x, 15.x] runs-on: ${{matrix.os}} steps: - uses: actions/checkout@v2 diff --git a/index.js b/index.js index fddd295..de3d305 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,19 @@ const { resolve } = require('path') -const WorkerPool = require('piscina') -const pool = new WorkerPool({ filename: resolve(__dirname, 'worker.js') }) +let pool = null +try { + const WorkerPool = require('piscina') + pool = new WorkerPool({ filename: resolve(__dirname, 'worker.js') }) +} catch(e) { + if (e.code === 'MODULE_NOT_FOUND') { + console.warn('[camaro] worker_threads is not available, expect performance drop. Try using Node version >= 12.') + } + + const workerFn = require('./worker') + pool = { + runTask: async (args) => workerFn(args) + } +} function isNonEmptyString(str) { return typeof str === 'string' && str.length > 0