From cc8036c6c99f2f04340a823e3e8a6804dcced2d5 Mon Sep 17 00:00:00 2001 From: Lance Ball Date: Wed, 14 Dec 2016 13:44:15 -0500 Subject: [PATCH] feat: create a browser distribution Ensures that opossum can run in a browser. Also - removed Makefile in favor of an npm-only build process. Fixes: https://github.com/bucharest-gold/opossum/issues/6 --- .gitignore | 2 ++ .travis.yml | 3 ++- Makefile | 20 ------------------ lib/circuit.js | 10 ++++++--- package.json | 38 +++++++++++++++++++++++++++------ test/browser/browser-tap.js | 38 +++++++++++++++++++++++++++++++++ test/browser/index.html | 42 +++++++++++++++++++++++++++++++++++++ test/test.js | 10 +++++++-- 8 files changed, 131 insertions(+), 32 deletions(-) delete mode 100644 Makefile create mode 100644 test/browser/browser-tap.js create mode 100644 test/browser/index.html diff --git a/.gitignore b/.gitignore index 2e5332d2..defcdfcd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ node_modules docs coverage +dist +test/browser/browserified-tests.js diff --git a/.travis.yml b/.travis.yml index 66824537..d22fac61 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,10 +3,11 @@ node_js: - "4" - "5" - "6" + - "7" before_script: - npm install -g coveralls script: - - make ci + - npm run ci notifications: irc: "chat.freenode.net#brass-monkey" after_success: diff --git a/Makefile b/Makefile deleted file mode 100644 index 19f88a94..00000000 --- a/Makefile +++ /dev/null @@ -1,20 +0,0 @@ -test: lint - npm test - -ci: lint - npm run prepublish - npm run coverage - -lint: node_modules - npm run lint - -publish: test - npm publish - -clean: - rm -rf node_modules coverage - -node_modules: package.json - npm install - -.PHONY: node_modules \ No newline at end of file diff --git a/lib/circuit.js b/lib/circuit.js index b9ea8b38..9f1fc8e5 100644 --- a/lib/circuit.js +++ b/lib/circuit.js @@ -50,11 +50,15 @@ class CircuitBreaker extends EventEmitter { this[NUM_FAILURES] = 0; function _startTimer (circuit) { - return () => - setTimeout(() => { + return () => { + const timer = setTimeout(() => { circuit[STATE] = HALF_OPEN; circuit.emit('halfOpen'); - }, circuit.options.resetTimeout).unref(); + }, circuit.options.resetTimeout); + if (typeof timer.unref === 'function') { + timer.unref(); + } + }; } this.on('open', _startTimer(this)); diff --git a/package.json b/package.json index c29faa3d..f0b27137 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,33 @@ "author": "Red Hat, Inc.", "license": "Apache-2.0", "scripts": { - "test": "tape test/*.js | tap-spec", - "lint": "eslint test/*.js index.js", + "preci": "npm install && npm run dependency-check", + "ci": "npm run build && npm run test:ci", + + "prebuild": "npm run lint", + "build": "npm run build:browser && npm run build:compress && npm run build:docs", + "build:browser": "browserify index.js lib/*.js > dist/opossum.js", + "build:compress": "escompress dist/opossum.js > dist/opossum-min.js", + "build:docs": "jsdoc --verbose -d docs -t ./node_modules/ink-docstrap/template -R README.md index.js lib", + + "pretest": "npm run lint", + "test": "npm run test:console", + "test:ci": "npm run test:console && npm run test:headless && npm run test:coverage", + "test:console": "tape test/*.js | tap-spec", + "pretest:headless": "npm run build", + "test:headless": "browserify ./test/test.js | tape-run", + "pretest:browser": "npm run build", + "test:browser": "browserify ./test/test.js > test/browser/browserified-tests.js && opener http://localhost:9007/test/browser/index.html && http-server . -p 9007", + "test:coverage": "istanbul cover tape test/*.js", + "prepublish": "nsp check", - "coverage": "istanbul cover tape test/*.js", - "dependencyCheck": "szero . --ci", + "postpublish": "./publish-docs.sh", + + "prerelease": "npm test", "release": "standard-version", - "docs": "jsdoc --verbose -d docs -t ./node_modules/ink-docstrap/template -R README.md index.js lib" + + "lint": "eslint test/*.js index.js lib/*.js", + "dependency-check": "szero . --ci" }, "repository": { "type": "git", @@ -28,20 +48,26 @@ }, "homepage": "https://github.com/bucharest-gold/opossum", "devDependencies": { + "browserify": "~13.1.1", + "escompress": "~0.5.0", "eslint": "~3.8.1", "eslint-config-semistandard": "~7.0.0", "eslint-config-standard": "~6.2.0", "eslint-plugin-promise": "~3.3.0", "eslint-plugin-react": "~6.4.1", "eslint-plugin-standard": "~2.0.1", + "http-server": "~0.9.0", "ink-docstrap": "~1.3.0", "istanbul": "~0.4.5", "jsdoc": "~3.4.2", "nsp": "~2.6.2", + "opener": "~1.4.2", "standard-version": "^3.0.0", "szero": "^0.5.1", + "tap-browser-color": "~0.1.2", "tap-spec": "~4.1.1", - "tape": "~4.6.2" + "tape": "~4.6.2", + "tape-run": "~2.1.4" }, "description": "A fail-fast circuit breaker for promises and callbacks", "keywords": [ diff --git a/test/browser/browser-tap.js b/test/browser/browser-tap.js new file mode 100644 index 00000000..59e67c8c --- /dev/null +++ b/test/browser/browser-tap.js @@ -0,0 +1,38 @@ +// for browser output +// TODO: This is all pretty hackey +const log = console.log; +const inBrowser = typeof document !== 'undefined'; + +function enable () { + console.log = function () { + if (inBrowser) { + printToBrowser(arguments[0]); + } + log.apply(log, Array.prototype.slice.call(arguments)); + }; +} + +function disable () { + console.log = log; +} + +function printToBrowser (line) { + if (!line || line.length < 1) return; + const p = document.body.appendChild(document.createElement('p')); + const statusBar = document.getElementById('status-bar'); + if (line.startsWith('ok') || line.startsWith('# pass')) { + p.style.color = 'green'; + } else if (line.startsWith('not ok') || line.startsWith('# fail')) { + p.style.color = 'red'; + statusBar.style.backgroundColor = 'red'; + } else if (line.startsWith('# tests')) { + p.className = 'test-count'; + } else if (line.startsWith('#')) { + p.className = 'test-case'; + } + p.innerHTML = line; +} + +module.exports = exports = { + enable, disable +}; diff --git a/test/browser/index.html b/test/browser/index.html new file mode 100644 index 00000000..a368c97e --- /dev/null +++ b/test/browser/index.html @@ -0,0 +1,42 @@ + + + + + + + Testing Opossum + + + + + + + + + + +
+
+

Opossum Test Results

+
+ + diff --git a/test/test.js b/test/test.js index 4b19a42c..ab727601 100644 --- a/test/test.js +++ b/test/test.js @@ -1,9 +1,12 @@ 'use strict'; +const browser = require('./browser/browser-tap'); const test = require('tape'); const Fidelity = require('fidelity'); const circuitBreaker = require('../'); +browser.enable(); + test('api', (t) => { const breaker = circuitBreaker(passFail); t.ok(circuitBreaker.promisify); @@ -347,9 +350,12 @@ function passFail (x) { */ function slowFunction () { return new Fidelity((resolve, reject) => { - setTimeout(() => { + const timer = setTimeout(() => { resolve('done'); - }, 10000).unref(); + }, 10000); + if (typeof timer.unref === 'function') { + timer.unref(); + } }); }