Skip to content

Commit

Permalink
feat: create a browser distribution
Browse files Browse the repository at this point in the history
Ensures that opossum can run in a browser.

Also - removed Makefile in favor of an npm-only build process.

Fixes: #6
  • Loading branch information
lance committed Dec 14, 2016
1 parent 5699e88 commit cc8036c
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
docs
coverage
dist
test/browser/browserified-tests.js
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 0 additions & 20 deletions Makefile

This file was deleted.

10 changes: 7 additions & 3 deletions lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
38 changes: 32 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": [
Expand Down
38 changes: 38 additions & 0 deletions test/browser/browser-tap.js
Original file line number Diff line number Diff line change
@@ -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
};
42 changes: 42 additions & 0 deletions test/browser/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!doctype html>

<html lang="en">
<head>
<meta charset="utf-8">

<title>Testing Opossum</title>
<meta name="description" content="Testing Opossum">
<meta name="author" content="lball@redhat.com">
<style>
body {
margin: 1em;
font-family: sans-serif;
font-size: large;
}
p {
margin: 1ex;
}
p.test-case, p.test-count {
font-weight: bold;
padding-top: 1.5em;
border-top: 1px dotted;
}
div#status-bar {
width: 100%;
height: 2em;
background-color: green;
}
</style>
</head>

<body>
<!-- load opossum -->
<script src="../../browser/opossum-min.js"></script>
<!-- load the test -->
<script src="browserified-tests.js"></script>
<div id='status-bar'></div>
<div id='results'>
<h1>Opossum Test Results</h1>
</div>
</body>
</html>
10 changes: 8 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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();
}
});
}

Expand Down

0 comments on commit cc8036c

Please sign in to comment.