Skip to content

Commit

Permalink
feat: added support for Node v10+ and browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
niftylettuce committed Aug 17, 2020
1 parent 1b9a6ed commit dcf7103
Show file tree
Hide file tree
Showing 16 changed files with 2,145 additions and 75 deletions.
4 changes: 4 additions & 0 deletions .browserslistrc
@@ -0,0 +1,4 @@
# Browsers that we support

> 1%
last 2 versions
10 changes: 10 additions & 0 deletions .dist.babelrc
@@ -0,0 +1,10 @@
{
"presets": [
["@babel/env", {
"targets": {
"browsers": [ "> 1%", "last 2 versions" ]
}
}]
],
"plugins": ["@babel/plugin-transform-runtime"],
}
36 changes: 36 additions & 0 deletions .dist.eslintrc
@@ -0,0 +1,36 @@
{
"extends": ["eslint:recommended"],
"env": {
"node": false,
"browser": true,
"amd": true,
"es6": true,
"commonjs": true
},
"plugins": ["compat"],
"rules": {
"no-unused-vars": "off",
"no-func-assign": "off",
"no-unsafe-finally": "off",
"no-empty": "off",
"no-undef": "off",
"no-constant-condition": "off",
"no-cond-assign": "off",
"no-control-regex": "off",
"no-fallthrough": "off",
"no-global-assign": "off",
"no-misleading-character-class": "off",
"no-prototype-builtins": "off",
"no-sparse-arrays": "off",
"no-useless-escape": "off",
"no-extra-semi": "off",
"no-unreachable": "off",
"no-redeclare": "off"
},
"globals": {
},
"settings": {
"polyfills": [
]
}
}
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -22,3 +22,5 @@ coverage
# Files #
###################
*.log
lib
dist
12 changes: 12 additions & 0 deletions .lib.babelrc
@@ -0,0 +1,12 @@
{
"presets": [
["@babel/env", {
"targets": {
"node": "10",
"browsers": [ "> 1%", "last 2 versions" ]
}
}]
],
"plugins": ["@babel/plugin-transform-runtime"],
"sourceMaps": "both"
}
12 changes: 12 additions & 0 deletions .lib.eslintrc
@@ -0,0 +1,12 @@
{
"extends": ["eslint:recommended", "plugin:node/recommended"],
"env": { "browser": true" },
"plugins": ["compat"],
"rules": {
"no-unsafe-finally": "off",
"no-func-assign": "off"
},
"settings": {
"polyfills": []
}
}
3 changes: 2 additions & 1 deletion .travis.yml
@@ -1,6 +1,7 @@
language: node_js
node_js:
- 'lts/*'
- 10
- 12
script:
- npm run test-coverage
after_success:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -16,7 +16,7 @@
</div>
<hr />
<div align="center">
Uses <a href="https://nodejs.org/api/worker_threads.html">workers</a> to spawn sandboxed processes, and supports <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function">async/await</a>, <a href="https://github.com/sindresorhus/p-retry">retries</a>, <a href="https://github.com/sindresorhus/p-throttle">throttling</a>, <a href="#concurrency">concurrency</a>, and <a href="#cancellation-retries-stalled-jobs-and-graceful-reloading">cancelable jobs with graceful shutdown</a>. Simple, fast, and the most lightweight tool for the job. Made for <a href="https://forwardemail.net">Forward Email</a> and <a href="https://lad.js.org">Lad</a>.
Works in Node v10+ and browsers (thanks to <a href="https://github.com/chjj/bthreads">bthreads</a> polyfill), uses <a href="https://nodejs.org/api/worker_threads.html">workers</a> to spawn sandboxed processes, and supports <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function">async/await</a>, <a href="https://github.com/sindresorhus/p-retry">retries</a>, <a href="https://github.com/sindresorhus/p-throttle">throttling</a>, <a href="#concurrency">concurrency</a>, and <a href="#cancellation-retries-stalled-jobs-and-graceful-reloading">cancelable jobs with graceful shutdown</a>. Simple, fast, and the most lightweight tool for the job. Made for <a href="https://forwardemail.net">Forward Email</a> and <a href="https://lad.js.org">Lad</a>.
</div>
<hr />
<div align="center">:heart: Love this project? Support <a href="https://github.com/niftylettuce" target="_blank">@niftylettuce's</a> <a href="https://en.wikipedia.org/wiki/Free_and_open-source_software" target="_blank">FOSS</a> on <a href="https://patreon.com/niftylettuce" target="_blank">Patreon</a> or <a href="https://paypal.me/niftylettuce">PayPal</a> :unicorn:</div>
Expand Down
36 changes: 30 additions & 6 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "bree",
"description": "The best job scheduler for Node.js with support for cron, dates, ms, later, and human-friendly strings. Uses workers to spawn sandboxed processes, and supports async/await, retries, throttling, concurrency, and cancelable promises (graceful shutdown). Simple, fast, and the most lightweight tool for the job. Made for Forward Email and Lad.",
"description": "The best job scheduler for Node.js with support for cron, dates, ms, later, and human-friendly strings. Works in Node v10+ and browsers (thanks to bthreads polyfill), uses workers to spawn sandboxed processes, and supports async/await, retries, throttling, concurrency, and cancelable promises (graceful shutdown). Simple, fast, and the most lightweight tool for the job. Made for Forward Email and Lad.",
"version": "1.1.28",
"author": "Nick Baugh <niftylettuce@gmail.com> (http://niftylettuce.com/)",
"ava": {
Expand All @@ -21,7 +21,9 @@
"shadowgate15 (https://github.com/shadowgate15)"
],
"dependencies": {
"@babel/runtime": "^7.11.2",
"boolean": "^3.0.1",
"bthreads": "^0.5.1",
"combine-errors": "^3.0.3",
"cron-validate": "^1.3.0",
"debug": "^4.1.1",
Expand All @@ -32,29 +34,39 @@
"safe-timers": "^1.1.0"
},
"devDependencies": {
"@babel/cli": "^7.10.5",
"@babel/core": "^7.11.1",
"@babel/plugin-transform-runtime": "^7.11.0",
"@babel/preset-env": "^7.11.0",
"@commitlint/cli": "latest",
"@commitlint/config-conventional": "latest",
"@sinonjs/fake-timers": "^6.0.1",
"ava": "latest",
"babelify": "^10.0.0",
"browserify": "^16.5.2",
"codecov": "latest",
"cross-env": "latest",
"delay": "^4.4.0",
"eslint": "^7.7.0",
"eslint-config-xo-lass": "latest",
"eslint-plugin-compat": "^3.8.0",
"eslint-plugin-node": "^11.1.0",
"fixpack": "latest",
"husky": "latest",
"into-stream": "^6.0.0",
"lint-staged": "latest",
"nyc": "latest",
"remark-cli": "latest",
"remark-preset-github": "latest",
"tinyify": "^3.0.0",
"xo": "^0.33.0"
},
"engines": {
"node": ">= 12.11.0"
"node": ">= 10"
},
"files": [
"index.js"
"lib",
"dist"
],
"homepage": "https://github.com/breejs/bree",
"husky": {
Expand Down Expand Up @@ -147,7 +159,9 @@
"workers"
],
"license": "MIT",
"main": "index.js",
"main": "lib/index.js",
"jsdelivr": "dist/bree.min.js",
"unpkg": "dist/bree.min.js",
"publishConfig": {
"access": "public"
},
Expand All @@ -156,11 +170,21 @@
"url": "https://github.com/breejs/bree"
},
"scripts": {
"ava": "cross-env NODE_ENV=test ava",
"browserify": "browserify src/index.js -o dist/bree.js -s Bree -g [ babelify --configFile ./.dist.babelrc ]",
"build": "npm run build:clean && npm run build:lib && npm run build:dist",
"build:clean": "rimraf lib dist",
"build:dist": "npm run browserify && npm run minify",
"build:lib": "babel --config-file ./.lib.babelrc src --out-dir lib",
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
"lint": "yarn run lint:js && yarn run lint:md",
"lint": "yarn run lint:js && yarn run lint:md && yarn run lint:lib && yarn run lint:dist",
"lint:dist": "eslint --no-inline-config -c .dist.eslintrc dist",
"lint:js": "xo",
"lint:lib": "eslint -c .lib.eslintrc lib",
"lint:md": "remark . -qfo",
"pretest": "yarn run lint",
"minify": "cross-env NODE_ENV=production browserify src/index.js -o dist/bree.min.js -s Bree -g [ babelify --configFile ./.dist.babelrc ] -p tinyify",
"nyc": "cross-env NODE_ENV=test nyc ava",
"pretest": "yarn run build && yarn run lint",
"test": "cross-env NODE_ENV=test ava",
"test-coverage": "cross-env NODE_ENV=test nyc yarn run test"
},
Expand Down
6 changes: 3 additions & 3 deletions index.js → src/index.js
@@ -1,5 +1,4 @@
const EventEmitter = require('events');
const { Worker } = require('worker_threads');
const { resolve, join } = require('path');
const { statSync } = require('fs');

Expand All @@ -10,6 +9,7 @@ const humanInterval = require('human-interval');
const isSANB = require('is-string-and-not-blank');
const later = require('later');
const ms = require('ms');
const threads = require('bthreads');
const { boolean } = require('boolean');
const { setTimeout, setInterval } = require('safe-timers');

Expand Down Expand Up @@ -41,7 +41,7 @@ class Bree extends EventEmitter {
// could also be mjs if desired
// (this is the default extension if you just specify a job's name without ".js" or ".mjs")
defaultExtension: 'js',
// default worker options to pass to `new Worker`
// default worker options to pass to ~`new Worker`~ `new threads.Worker`
// (can be overridden on a per job basis)
// <https://nodejs.org/api/worker_threads.html#worker_threads_new_worker_filename_options>
worker: {},
Expand Down Expand Up @@ -582,7 +582,7 @@ class Bree extends EventEmitter {
...(job.worker && job.worker.workerData ? job.worker.workerData : {})
}
};
this.workers[name] = new Worker(job.path, object);
this.workers[name] = new threads.Worker(job.path, object);
this.emit('worker created', name);
debug('worker started', name);

Expand Down
2 changes: 1 addition & 1 deletion test/jobs/done.js
@@ -1,5 +1,5 @@
const delay = require('delay');
const { parentPort } = require('worker_threads');
const { parentPort } = require('bthreads');

(async () => {
await delay(1);
Expand Down
2 changes: 1 addition & 1 deletion test/jobs/message-ungraceful.js
@@ -1,4 +1,4 @@
const { parentPort } = require('worker_threads');
const { parentPort } = require('bthreads');

setInterval(() => {}, 10);

Expand Down
3 changes: 2 additions & 1 deletion test/jobs/message.js
@@ -1,4 +1,4 @@
const { parentPort } = require('worker_threads');
const { parentPort } = require('bthreads');

setInterval(() => {}, 10);

Expand All @@ -11,6 +11,7 @@ if (parentPort) {
}

parentPort.postMessage(message);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(0);
});
}
2 changes: 1 addition & 1 deletion test/jobs/worker-data.js
@@ -1,3 +1,3 @@
const { parentPort, workerData } = require('worker_threads');
const { parentPort, workerData } = require('bthreads');

if (parentPort) parentPort.postMessage(workerData);
2 changes: 1 addition & 1 deletion test/jobs/worker-options.js
@@ -1,3 +1,3 @@
const { parentPort } = require('worker_threads');
const { parentPort } = require('bthreads');

if (parentPort) parentPort.postMessage(process.argv[2]);

0 comments on commit dcf7103

Please sign in to comment.