[Pkg-javascript-commits] [node-currently-unhandled] 01/04: Import Upstream version 0.4.1
Praveen Arimbrathodiyil
praveen at moszumanska.debian.org
Wed Oct 26 05:50:57 UTC 2016
This is an automated email from the git hooks/post-receive script.
praveen pushed a commit to branch master
in repository node-currently-unhandled.
commit e5520d0d4547275f435854c7cd9ea3da46ef6f8d
Author: Praveen Arimbrathodiyil <praveen at debian.org>
Date: Wed Oct 26 11:04:01 2016 +0530
Import Upstream version 0.4.1
---
.editorconfig | 12 ++++
.gitattributes | 2 +
.gitignore | 4 ++
.travis.yml | 16 +++++
browser-test.js | 32 ++++++++++
browser.js | 27 +++++++++
core.js | 33 +++++++++++
index.js | 12 ++++
license | 21 +++++++
package.json | 71 ++++++++++++++++++++++
readme.md | 44 ++++++++++++++
test.js | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12 files changed, 453 insertions(+)
diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..98a761d
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,12 @@
+root = true
+
+[*]
+indent_style = tab
+end_of_line = lf
+charset = utf-8
+trim_trailing_whitespace = true
+insert_final_newline = true
+
+[{package.json,*.yml}]
+indent_style = space
+indent_size = 2
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..391f0a4
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,2 @@
+* text=auto
+*.js text eol=lf
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..2bcbfe5
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+node_modules
+.nyc_output
+coverage
+browser-bluebird-test.js
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..644a0d4
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,16 @@
+language: node_js
+
+node_js:
+ - '6'
+ - '5'
+ - '4'
+ - '0.12'
+ - '0.10'
+
+before_script:
+ - "export DISPLAY=:99.0"
+ - "sh -e /etc/init.d/xvfb start"
+ - sleep 3 # give xvfb some time to start
+
+after_script:
+ - 'cat coverage/lcov.info | ./node_modules/.bin/coveralls'
diff --git a/browser-test.js b/browser-test.js
new file mode 100644
index 0000000..03e1b62
--- /dev/null
+++ b/browser-test.js
@@ -0,0 +1,32 @@
+var assert = require('assert');
+var delay = require('delay');
+var fn = require('./');
+
+it('works in a browser', function () {
+ var currentlyUnhandled = fn();
+ var messages = function () {
+ return currentlyUnhandled().map(function (event) {
+ return event.reason.message;
+ });
+ };
+ var p1;
+ var p2;
+
+ p1 = Promise.reject(new Error('foo'));
+
+ return delay(300).then(function () {
+ assert.deepEqual(messages(), ['foo']);
+ p2 = Promise.reject(new Error('bar'));
+ return delay(300);
+ }).then(function () {
+ assert.deepEqual(messages(), ['foo', 'bar']);
+ p1.catch(function () {});
+ return delay(300);
+ }).then(function () {
+ assert.deepEqual(messages(), ['bar']);
+ p2.catch(function () {});
+ return delay(300);
+ }).then(function () {
+ assert.deepEqual(messages(), []);
+ });
+});
diff --git a/browser.js b/browser.js
new file mode 100644
index 0000000..aa9e56f
--- /dev/null
+++ b/browser.js
@@ -0,0 +1,27 @@
+'use strict';
+var core = require('./core');
+
+function unwrapEvent(event) {
+ if (event && event.detail && event.detail.promise) {
+ return event.detail;
+ }
+
+ return event;
+}
+
+module.exports = function (w) {
+ w = w || window;
+ var c = core();
+
+ w.addEventListener('unhandledrejection', function (event) {
+ event = unwrapEvent(event);
+ c.onUnhandledRejection(event.reason, event.promise);
+ });
+
+ w.addEventListener('rejectionhandled', function (event) {
+ event = unwrapEvent(event);
+ c.onRejectionHandled(event.promise);
+ });
+
+ return c.currentlyUnhandled;
+};
diff --git a/core.js b/core.js
new file mode 100644
index 0000000..73db22e
--- /dev/null
+++ b/core.js
@@ -0,0 +1,33 @@
+'use strict';
+var arrayFindIndex = require('array-find-index');
+
+module.exports = function () {
+ var unhandledRejections = [];
+
+ function onUnhandledRejection(reason, promise) {
+ unhandledRejections.push({reason: reason, promise: promise});
+ }
+
+ function onRejectionHandled(promise) {
+ var index = arrayFindIndex(unhandledRejections, function (x) {
+ return x.promise === promise;
+ });
+
+ unhandledRejections.splice(index, 1);
+ }
+
+ function currentlyUnhandled() {
+ return unhandledRejections.map(function (entry) {
+ return {
+ reason: entry.reason,
+ promise: entry.promise
+ };
+ });
+ }
+
+ return {
+ onUnhandledRejection: onUnhandledRejection,
+ onRejectionHandled: onRejectionHandled,
+ currentlyUnhandled: currentlyUnhandled
+ };
+};
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..238b9d9
--- /dev/null
+++ b/index.js
@@ -0,0 +1,12 @@
+'use strict';
+var core = require('./core');
+
+module.exports = function (p) {
+ p = p || process;
+ var c = core();
+
+ p.on('unhandledRejection', c.onUnhandledRejection);
+ p.on('rejectionHandled', c.onRejectionHandled);
+
+ return c.currentlyUnhandled;
+};
diff --git a/license b/license
new file mode 100644
index 0000000..ad5d021
--- /dev/null
+++ b/license
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) James Talmage <james at talmage.io> (github.com/jamestalmage)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..3baae3d
--- /dev/null
+++ b/package.json
@@ -0,0 +1,71 @@
+{
+ "name": "currently-unhandled",
+ "version": "0.4.1",
+ "description": "Track the list of currently unhandled promise rejections.",
+ "license": "MIT",
+ "repository": "jamestalmage/currently-unhandled",
+ "author": {
+ "name": "James Talmage",
+ "email": "james at talmage.io",
+ "url": "github.com/jamestalmage"
+ },
+ "engines": {
+ "node": ">=0.10.0"
+ },
+ "scripts": {
+ "test": "xo && nyc ava"
+ },
+ "files": [
+ "index.js",
+ "core.js",
+ "browser.js"
+ ],
+ "keywords": [
+ "unhandled",
+ "unhandledRejection",
+ "rejected",
+ "promises"
+ ],
+ "dependencies": {
+ "array-find-index": "^1.0.1"
+ },
+ "devDependencies": {
+ "ava": "^0.15.1",
+ "bluebird": "^3.4.0",
+ "browserify": "^13.0.1",
+ "coveralls": "^2.11.9",
+ "delay": "^1.3.1",
+ "is-ci": "^1.0.8",
+ "karma": "^0.13.22",
+ "karma-browserify": "^5.0.5",
+ "karma-chrome-launcher": "^1.0.1",
+ "karma-firefox-launcher": "^1.0.0",
+ "karma-mocha": "^1.0.1",
+ "mocha": "^2.5.3",
+ "nyc": "^6.4.0",
+ "watchify": "^3.7.0",
+ "xo": "^0.15.0"
+ },
+ "browser": {
+ "./index.js": "./browser.js"
+ },
+ "ava": {
+ "files": "test.js"
+ },
+ "nyc": {
+ "reporter": [
+ "lcov",
+ "text"
+ ]
+ },
+ "xo": {
+ "ignores": [
+ "browser-bluebird-test.js"
+ ],
+ "envs": [
+ "browser",
+ "mocha",
+ "node"
+ ]
+ }
+}
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..b73c568
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,44 @@
+# currently-unhandled [![Build Status](https://travis-ci.org/jamestalmage/currently-unhandled.svg?branch=master)](https://travis-ci.org/jamestalmage/currently-unhandled) [![Coverage Status](https://coveralls.io/repos/github/jamestalmage/currently-unhandled/badge.svg?branch=master)](https://coveralls.io/github/jamestalmage/currently-unhandled?branch=master)
+
+> Track the list of currently unhandled promise rejections.
+
+
+## Install
+
+```
+$ npm install --save currently-unhandled
+```
+
+
+## Usage
+
+```js
+const currentlyUnhandled = require('currently-unhandled')(); // <- note the invocation
+
+var fooError = new Error('foo');
+var p = Promise.reject(new Error('foo'));
+
+// on the next tick - unhandled rejected promise is added to the list:
+currentlyUnhandled();
+//=> [{promise: p, reason: fooError}]'
+
+p.catch(() => {});
+
+// on the next tick - handled promise is now removed from the list:
+currentlyUnhandled();
+//=> [];
+```
+
+## API
+
+### currentlyUnhandled()
+
+Returns an array of objects with `promise` and `reason` properties representing the rejected promises that currently do not have a rejection handler. The list grows and shrinks as unhandledRejections are published, and later handled.
+
+## Browser Support
+
+This module can be bundled with `browserify`. At time of writing, it will work with native Promises in the Chrome browser only. For best cross-browser support, use `bluebird` instead of native Promise support in browsers.
+
+## License
+
+MIT © [James Talmage](http://github.com/jamestalmage)
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..2514429
--- /dev/null
+++ b/test.js
@@ -0,0 +1,179 @@
+import fs from 'fs';
+import path from 'path';
+import {EventEmitter} from 'events';
+import test from 'ava';
+import delay from 'delay';
+import isCi from 'is-ci';
+import {Server as KarmaServer} from 'karma';
+import browser from './browser';
+import node from './';
+
+const implementations = {browser, node};
+
+fs.writeFileSync(
+ path.join(__dirname, 'browser-bluebird-test.js'),
+
+ 'var Promise = require("bluebird");\n' +
+ fs.readFileSync(
+ path.join(__dirname, 'browser-test.js'),
+ 'utf8'
+ )
+);
+
+const mocks = {
+ node() {
+ const ee = new EventEmitter();
+
+ return {
+ mock: {
+ on(name, handler) {
+ ee.on(name, handler);
+ }
+ },
+ unhandled({promise, reason}) {
+ ee.emit('unhandledRejection', reason, promise);
+ },
+ handled({promise}) {
+ ee.emit('rejectionHandled', promise);
+ }
+ };
+ },
+ browser() {
+ const ee = new EventEmitter();
+
+ return {
+ mock: {
+ addEventListener(name, handler) {
+ ee.on(name, handler);
+ }
+ },
+ unhandled({promise, reason}) {
+ ee.emit('unhandledrejection', {promise, reason});
+ },
+ handled({promise, reason}) {
+ ee.emit('rejectionhandled', {promise, reason});
+ }
+ };
+ }
+};
+
+function event(reason) {
+ return {
+ promise: {},
+ reason: reason
+ };
+}
+
+function messagesHelper(currentlyUnhandled) {
+ return () => currentlyUnhandled().map(obj => obj.reason.message);
+}
+
+function generateTests(type) {
+ function setup() {
+ const mock = mocks[type]();
+ const currentlyUnhandled = implementations[type](mock.mock);
+
+ const messages = messagesHelper(currentlyUnhandled);
+
+ return {mock, currentlyUnhandled, messages};
+ }
+
+ test(`${type}: adds promises as they are rejected`, t => {
+ const {mock, currentlyUnhandled, messages} = setup();
+
+ const event1 = event(new Error('foo'));
+ mock.unhandled(event1);
+
+ let unhandled = currentlyUnhandled();
+
+ t.is(unhandled.length, 1);
+ t.is(unhandled[0].promise, event1.promise);
+ t.is(unhandled[0].reason.message, 'foo');
+
+ const event2 = event(new Error('bar'));
+ mock.unhandled(event2);
+
+ t.is(currentlyUnhandled().length, 2);
+ t.deepEqual(messages(), ['foo', 'bar']);
+ });
+
+ function removalMacro(t, index, expected) {
+ const {mock, messages} = setup();
+
+ const events = [
+ event(new Error('foo')),
+ event(new Error('bar')),
+ event(new Error('baz'))
+ ];
+
+ events.forEach(mock.unhandled);
+
+ t.deepEqual(messages(), ['foo', 'bar', 'baz']);
+
+ mock.handled(events[index]);
+
+ t.deepEqual(messages(), expected);
+ }
+
+ test(`${type}: removes promises as they are handled (from beginning)`, removalMacro, 0, ['bar', 'baz']);
+ test(`${type}: removes promises as they are handled (from middle)`, removalMacro, 1, ['foo', 'baz']);
+ test(`${type}: removes promises as they are handled (from middle)`, removalMacro, 2, ['foo', 'bar']);
+}
+
+generateTests('browser');
+generateTests('node');
+
+test.serial('node: works as advertised', async t => {
+ var currentlyUnhandled = node();
+ var messages = messagesHelper(currentlyUnhandled);
+
+ var p1 = Promise.reject(new Error('foo'));
+ await delay(10);
+ t.deepEqual(messages(), ['foo']);
+
+ var p2 = Promise.reject(new Error('bar'));
+ await delay(10);
+ t.deepEqual(messages(), ['foo', 'bar']);
+
+ p1.catch(() => {});
+ await delay(10);
+ t.deepEqual(messages(), ['bar']);
+
+ p2.catch(() => {});
+ await delay(10);
+ t.deepEqual(messages(), []);
+});
+
+function browserMacro(t, file, browsers) {
+ new KarmaServer({
+ frameworks: ['browserify', 'mocha'],
+ files: [file],
+ browsers,
+
+ preprocessors: {
+ [file]: ['browserify']
+ },
+
+ browserify: {
+ debug: true
+ },
+
+ singleRun: true,
+ autoWatch: false
+ }, exitCode => {
+ if (exitCode) {
+ t.fail(`karma exited with: ${exitCode}`);
+ }
+ t.end();
+ }).start();
+}
+
+if (!isCi) {
+ test.serial.cb('actual browser (native promise)', browserMacro, 'browser-test.js', ['Chrome']); // eslint-disable-line ava/test-ended
+}
+
+test.serial.cb('actual browser (bluebird)', browserMacro, 'browser-bluebird-test.js', isCi ? ['Firefox'] : ['Firefox', 'Chrome']); // eslint-disable-line ava/test-ended
+
+test.todo('add Firefox as tested browser when it supports the feature');
+test.todo('add Safari as tested browser when it supports the feature');
+test.todo('add IE as tested browser when it supports the feature');
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-currently-unhandled.git
More information about the Pkg-javascript-commits
mailing list