diff --git a/README.md b/README.md index 7864f58..fd158f3 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ Common functionality shared throughout Clinic.js. - [Utils](#utils) - [`getLoggingPaths(toolName, toolSpecificFiles=[])`](#getloggingpathstoolname-toolspecificfiles) - [`checkForTranspiledCode(fileName)`](#checkfortranspiledcode-filename) + - [`workers`](#workers) - [Scripts](#scripts) - [`buildJs(opts = {})`](#buildjsopts) - [Usage](#usage) @@ -69,6 +70,19 @@ A function that will check file contents for transpiled code. Check also include Arguments: - `fileName` - Path to the file being passed in. +### `workers` + +An object that provides an information about Node.js worker thread support. + +```js +const { workers } = require('@nearform/node-clinic-common') + +console.log(workers.isMainThread) +console.log(workers.threadId) +``` + +If Worker Threads are not supported on the Node.js version used, the `isMainThread` property will be `true`, and the `threadId` property will be `0`. + *** ## Scripts @@ -408,7 +422,7 @@ style can be customised by defining these CSS vars in your CSS ### contexOverlay Displays an overlay containig the given `msg` right next to the targetElement or the targetRect -If **targetElement** and **targetRect** are `null||undefined` then the overlay content will be centerd on the page. +If **targetElement** and **targetRect** are `null||undefined` then the overlay content will be centerd on the page. ```js // const options = { // msg, @@ -426,7 +440,7 @@ If **targetElement** and **targetRect** are `null||undefined` then the overlay c offset: { y: 10, height: 20 }, targetElement: document.querySelector('.my-cool-element'), showArrow: true - }) + }) ``` ### Walkthrough diff --git a/index.js b/index.js index c34f29f..a910566 100644 --- a/index.js +++ b/index.js @@ -2,8 +2,10 @@ const getLoggingPaths = require('./lib/get-logging-paths') const checkForTranspiledCode = require('./lib/source-check') +const workers = require('./lib/workers') module.exports = { getLoggingPaths, - checkForTranspiledCode + checkForTranspiledCode, + workers } diff --git a/lib/workers.js b/lib/workers.js new file mode 100644 index 0000000..489a481 --- /dev/null +++ b/lib/workers.js @@ -0,0 +1,16 @@ +'use strict' + +function maybeWorkerThreads () { + let isMainThread = true + let threadId = 0 + try { + const workerThreads = require('worker_threads') + isMainThread = workerThreads.isMainThread + threadId = workerThreads.threadId + } catch (err) { + // Do nothing + } + return { isMainThread, threadId } +} + +module.exports = maybeWorkerThreads() diff --git a/test/workers.test.js b/test/workers.test.js new file mode 100644 index 0000000..f45a53b --- /dev/null +++ b/test/workers.test.js @@ -0,0 +1,34 @@ +const test = require('tap').test +const workers = require('../lib/workers.js') + +function hasWorkerThreads () { + let hasWorkerThreads = false + try { + hasWorkerThreads = !!require('worker_threads') + } catch (err) { + // Do nothing + } + return hasWorkerThreads +} + +test('test worker thread detection', (t) => { + t.strictEqual(workers.isMainThread, true) + t.strictEqual(workers.threadId, 0) + + if (hasWorkerThreads()) { + const { Worker } = require('worker_threads') + const worker = new Worker(` +const test = require('tap').test +const workers = require('./lib/workers.js') +const assert = require('assert') +assert.strictEqual(workers.isMainThread, false) +assert.strictEqual(workers.threadId, 1) + `, { eval: true }) + worker.on('exit', (code) => { + if (code !== 0) t.fail(`worker test failed with code ${code}`) + t.end() + }) + } else { + t.end() + } +})