[Pkg-javascript-commits] [node-pinkei] 01/02: Import Upstream version 2.0.4

Shanavas M shanavas-guest at moszumanska.debian.org
Fri Nov 4 17:14:19 UTC 2016


This is an automated email from the git hooks/post-receive script.

shanavas-guest pushed a commit to branch master
in repository node-pinkei.

commit 07035485b836772fb05db441e3c65e4245647531
Author: Shanavas M <shanavas at disroot.org>
Date:   Fri Nov 4 16:59:12 2016 +0000

    Import Upstream version 2.0.4
---
 .editorconfig  |  15 +++
 .gitattributes |   1 +
 .gitignore     |   2 +
 .travis.yml    |   9 ++
 appveyor.yml   |  22 +++++
 index.js       | 292 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 license        |  21 +++++
 media/logo.png | Bin 0 -> 80677 bytes
 package.json   |  36 +++++++
 readme.md      |  83 ++++++++++++++++
 test.js        | 290 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 11 files changed, 771 insertions(+)

diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..8f9d77e
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,15 @@
+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
+
+[*.md]
+trim_trailing_whitespace = false
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..176a458
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1 @@
+* text=auto
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c9106a7
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+node_modules
+.nyc_output
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..d656de9
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,9 @@
+sudo: false
+language: node_js
+node_js:
+  - 'stable'
+  - '4'
+  - 'iojs'
+  - '0.12'
+  - '0.10'
+after_success: npm run coverage
diff --git a/appveyor.yml b/appveyor.yml
new file mode 100644
index 0000000..6562afd
--- /dev/null
+++ b/appveyor.yml
@@ -0,0 +1,22 @@
+environment:
+  matrix:
+    - nodejs_version: '5'
+    - nodejs_version: '4'
+    - nodejs_version: '0.12'
+    - nodejs_version: '0.10'
+install:
+  - ps: Install-Product node $env:nodejs_version
+  - set CI=true
+  - npm -g install npm at latest || (timeout 30 && npm -g install npm at latest)
+  - set PATH=%APPDATA%\npm;%PATH%
+  - npm install || (timeout 30 && npm install)
+matrix:
+  fast_finish: true
+build: off
+version: '{build}'
+shallow_clone: true
+clone_depth: 1
+test_script:
+  - node --version
+  - npm --version
+  - npm run test || (timeout 30 && npm run test)
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..14ce1bf
--- /dev/null
+++ b/index.js
@@ -0,0 +1,292 @@
+'use strict';
+
+var PENDING = 'pending';
+var SETTLED = 'settled';
+var FULFILLED = 'fulfilled';
+var REJECTED = 'rejected';
+var NOOP = function () {};
+var isNode = typeof global !== 'undefined' && typeof global.process !== 'undefined' && typeof global.process.emit === 'function';
+
+var asyncSetTimer = typeof setImmediate === 'undefined' ? setTimeout : setImmediate;
+var asyncQueue = [];
+var asyncTimer;
+
+function asyncFlush() {
+	// run promise callbacks
+	for (var i = 0; i < asyncQueue.length; i++) {
+		asyncQueue[i][0](asyncQueue[i][1]);
+	}
+
+	// reset async asyncQueue
+	asyncQueue = [];
+	asyncTimer = false;
+}
+
+function asyncCall(callback, arg) {
+	asyncQueue.push([callback, arg]);
+
+	if (!asyncTimer) {
+		asyncTimer = true;
+		asyncSetTimer(asyncFlush, 0);
+	}
+}
+
+function invokeResolver(resolver, promise) {
+	function resolvePromise(value) {
+		resolve(promise, value);
+	}
+
+	function rejectPromise(reason) {
+		reject(promise, reason);
+	}
+
+	try {
+		resolver(resolvePromise, rejectPromise);
+	} catch (e) {
+		rejectPromise(e);
+	}
+}
+
+function invokeCallback(subscriber) {
+	var owner = subscriber.owner;
+	var settled = owner._state;
+	var value = owner._data;
+	var callback = subscriber[settled];
+	var promise = subscriber.then;
+
+	if (typeof callback === 'function') {
+		settled = FULFILLED;
+		try {
+			value = callback(value);
+		} catch (e) {
+			reject(promise, e);
+		}
+	}
+
+	if (!handleThenable(promise, value)) {
+		if (settled === FULFILLED) {
+			resolve(promise, value);
+		}
+
+		if (settled === REJECTED) {
+			reject(promise, value);
+		}
+	}
+}
+
+function handleThenable(promise, value) {
+	var resolved;
+
+	try {
+		if (promise === value) {
+			throw new TypeError('A promises callback cannot return that same promise.');
+		}
+
+		if (value && (typeof value === 'function' || typeof value === 'object')) {
+			// then should be retrieved only once
+			var then = value.then;
+
+			if (typeof then === 'function') {
+				then.call(value, function (val) {
+					if (!resolved) {
+						resolved = true;
+
+						if (value === val) {
+							fulfill(promise, val);
+						} else {
+							resolve(promise, val);
+						}
+					}
+				}, function (reason) {
+					if (!resolved) {
+						resolved = true;
+
+						reject(promise, reason);
+					}
+				});
+
+				return true;
+			}
+		}
+	} catch (e) {
+		if (!resolved) {
+			reject(promise, e);
+		}
+
+		return true;
+	}
+
+	return false;
+}
+
+function resolve(promise, value) {
+	if (promise === value || !handleThenable(promise, value)) {
+		fulfill(promise, value);
+	}
+}
+
+function fulfill(promise, value) {
+	if (promise._state === PENDING) {
+		promise._state = SETTLED;
+		promise._data = value;
+
+		asyncCall(publishFulfillment, promise);
+	}
+}
+
+function reject(promise, reason) {
+	if (promise._state === PENDING) {
+		promise._state = SETTLED;
+		promise._data = reason;
+
+		asyncCall(publishRejection, promise);
+	}
+}
+
+function publish(promise) {
+	promise._then = promise._then.forEach(invokeCallback);
+}
+
+function publishFulfillment(promise) {
+	promise._state = FULFILLED;
+	publish(promise);
+}
+
+function publishRejection(promise) {
+	promise._state = REJECTED;
+	publish(promise);
+	if (!promise._handled && isNode) {
+		global.process.emit('unhandledRejection', promise._data, promise);
+	}
+}
+
+function notifyRejectionHandled(promise) {
+	global.process.emit('rejectionHandled', promise);
+}
+
+/**
+ * @class
+ */
+function Promise(resolver) {
+	if (typeof resolver !== 'function') {
+		throw new TypeError('Promise resolver ' + resolver + ' is not a function');
+	}
+
+	if (this instanceof Promise === false) {
+		throw new TypeError('Failed to construct \'Promise\': Please use the \'new\' operator, this object constructor cannot be called as a function.');
+	}
+
+	this._then = [];
+
+	invokeResolver(resolver, this);
+}
+
+Promise.prototype = {
+	constructor: Promise,
+
+	_state: PENDING,
+	_then: null,
+	_data: undefined,
+	_handled: false,
+
+	then: function (onFulfillment, onRejection) {
+		var subscriber = {
+			owner: this,
+			then: new this.constructor(NOOP),
+			fulfilled: onFulfillment,
+			rejected: onRejection
+		};
+
+		if ((onRejection || onFulfillment) && !this._handled) {
+			this._handled = true;
+			if (this._state === REJECTED && isNode) {
+				asyncCall(notifyRejectionHandled, this);
+			}
+		}
+
+		if (this._state === FULFILLED || this._state === REJECTED) {
+			// already resolved, call callback async
+			asyncCall(invokeCallback, subscriber);
+		} else {
+			// subscribe
+			this._then.push(subscriber);
+		}
+
+		return subscriber.then;
+	},
+
+	catch: function (onRejection) {
+		return this.then(null, onRejection);
+	}
+};
+
+Promise.all = function (promises) {
+	if (!Array.isArray(promises)) {
+		throw new TypeError('You must pass an array to Promise.all().');
+	}
+
+	return new Promise(function (resolve, reject) {
+		var results = [];
+		var remaining = 0;
+
+		function resolver(index) {
+			remaining++;
+			return function (value) {
+				results[index] = value;
+				if (!--remaining) {
+					resolve(results);
+				}
+			};
+		}
+
+		for (var i = 0, promise; i < promises.length; i++) {
+			promise = promises[i];
+
+			if (promise && typeof promise.then === 'function') {
+				promise.then(resolver(i), reject);
+			} else {
+				results[i] = promise;
+			}
+		}
+
+		if (!remaining) {
+			resolve(results);
+		}
+	});
+};
+
+Promise.race = function (promises) {
+	if (!Array.isArray(promises)) {
+		throw new TypeError('You must pass an array to Promise.race().');
+	}
+
+	return new Promise(function (resolve, reject) {
+		for (var i = 0, promise; i < promises.length; i++) {
+			promise = promises[i];
+
+			if (promise && typeof promise.then === 'function') {
+				promise.then(resolve, reject);
+			} else {
+				resolve(promise);
+			}
+		}
+	});
+};
+
+Promise.resolve = function (value) {
+	if (value && typeof value === 'object' && value.constructor === Promise) {
+		return value;
+	}
+
+	return new Promise(function (resolve) {
+		resolve(value);
+	});
+};
+
+Promise.reject = function (reason) {
+	return new Promise(function (resolve, reject) {
+		reject(reason);
+	});
+};
+
+module.exports = Promise;
diff --git a/license b/license
new file mode 100644
index 0000000..1aeb74f
--- /dev/null
+++ b/license
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) Vsevolod Strukchinsky <floatdrop at gmail.com> (github.com/floatdrop)
+
+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/media/logo.png b/media/logo.png
new file mode 100644
index 0000000..85feb88
Binary files /dev/null and b/media/logo.png differ
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..cb9057b
--- /dev/null
+++ b/package.json
@@ -0,0 +1,36 @@
+{
+  "name": "pinkie",
+  "version": "2.0.4",
+  "description": "Itty bitty little widdle twinkie pinkie ES2015 Promise implementation",
+  "license": "MIT",
+  "repository": "floatdrop/pinkie",
+  "author": {
+    "name": "Vsevolod Strukchinsky",
+    "email": "floatdrop at gmail.com",
+    "url": "github.com/floatdrop"
+  },
+  "engines": {
+    "node": ">=0.10.0"
+  },
+  "scripts": {
+    "test": "xo && nyc mocha",
+    "coverage": "nyc report --reporter=text-lcov | coveralls"
+  },
+  "files": [
+    "index.js"
+  ],
+  "keywords": [
+    "promise",
+    "promises",
+    "es2015",
+    "es6"
+  ],
+  "devDependencies": {
+    "core-assert": "^0.1.1",
+    "coveralls": "^2.11.4",
+    "mocha": "*",
+    "nyc": "^3.2.2",
+    "promises-aplus-tests": "*",
+    "xo": "^0.10.1"
+  }
+}
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..1565f95
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,83 @@
+<h1 align="center">
+	<br>
+	<img width="256" src="media/logo.png" alt="pinkie">
+	<br>
+	<br>
+</h1>
+
+> Itty bitty little widdle twinkie pinkie [ES2015 Promise](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-promise-objects) implementation
+
+[![Build Status](https://travis-ci.org/floatdrop/pinkie.svg?branch=master)](https://travis-ci.org/floatdrop/pinkie)  [![Coverage Status](https://coveralls.io/repos/floatdrop/pinkie/badge.svg?branch=master&service=github)](https://coveralls.io/github/floatdrop/pinkie?branch=master)
+
+There are [tons of Promise implementations](https://github.com/promises-aplus/promises-spec/blob/master/implementations.md#standalone) out there, but all of them focus on browser compatibility and are often bloated with functionality.
+
+This module is an exact Promise specification polyfill (like [native-promise-only](https://github.com/getify/native-promise-only)), but in Node.js land (it should be browserify-able though).
+
+
+## Install
+
+```
+$ npm install --save pinkie
+```
+
+
+## Usage
+
+```js
+var fs = require('fs');
+var Promise = require('pinkie');
+
+new Promise(function (resolve, reject) {
+	fs.readFile('foo.json', 'utf8', function (err, data) {
+		if (err) {
+			reject(err);
+			return;
+		}
+
+		resolve(data);
+	});
+});
+//=> Promise
+```
+
+
+### API
+
+`pinkie` exports bare [ES2015 Promise](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-promise-objects) implementation and polyfills [Node.js rejection events](https://nodejs.org/api/process.html#process_event_unhandledrejection). In case you forgot:
+
+#### new Promise(executor)
+
+Returns new instance of `Promise`.
+
+##### executor
+
+*Required*  
+Type: `function`
+
+Function with two arguments `resolve` and `reject`. The first argument fulfills the promise, the second argument rejects it.
+
+#### pinkie.all(promises)
+
+Returns a promise that resolves when all of the promises in the `promises` Array argument have resolved.
+
+#### pinkie.race(promises)
+
+Returns a promise that resolves or rejects as soon as one of the promises in the `promises` Array resolves or rejects, with the value or reason from that promise.
+
+#### pinkie.reject(reason)
+
+Returns a Promise object that is rejected with the given `reason`.
+
+#### pinkie.resolve(value)
+
+Returns a Promise object that is resolved with the given `value`. If the `value` is a thenable (i.e. has a then method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise the returned promise will be fulfilled with the `value`.
+
+
+## Related
+
+- [pinkie-promise](https://github.com/floatdrop/pinkie-promise) - Returns the native Promise or this module
+
+
+## License
+
+MIT © [Vsevolod Strukchinsky](http://github.com/floatdrop)
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..6ace7c3
--- /dev/null
+++ b/test.js
@@ -0,0 +1,290 @@
+/* global describe, it, beforeEach, afterEach*/
+
+'use strict';
+
+var assert = require('core-assert');
+var Promise = require('./');
+
+describe('Promise', function () {
+	it('should throw without new', function () {
+		assert.throws(function () {
+			/* eslint-disable new-cap */
+			var promise = Promise(function () {});
+			/* eslint-enable new-cap */
+			assert.ok(promise);
+		}, /Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function\./);
+	});
+
+	it('should throw on invalid resolver type', function () {
+		assert.throws(function () {
+			var promise = new Promise('unicorns');
+			assert.ok(promise);
+		}, /Promise resolver unicorns is not a function/);
+	});
+
+	it('should reject on exception in resolver', function (done) {
+		new Promise(function () {
+			throw new Error('Bang!');
+		})
+		.catch(function (err) {
+			assert.equal(err.message, 'Bang!');
+			done();
+		});
+	});
+
+	it('should reject on exception in then', function (done) {
+		Promise.resolve(1)
+			.then(function () {
+				throw new Error('Bang!');
+			})
+			.catch(function (err) {
+				assert.equal(err.message, 'Bang!');
+				done();
+			});
+	});
+
+	it('should return Promise from resolve value', function (done) {
+		Promise.resolve(Promise.resolve(1))
+			.then(function (value) {
+				assert.equal(value, 1);
+				done();
+			});
+	});
+
+	// Is it really so? Seems like a bug
+	it('should resolve thenable in resolve', function (done) {
+		var thenable = {
+			then: function (cb) {
+				cb(thenable);
+			}
+		};
+
+		Promise.resolve(thenable).then(function (v) {
+			assert.equal(thenable, v);
+			done();
+		});
+	});
+});
+
+describe('Promise.all', function () {
+	it('should throw error on invalid argument', function () {
+		assert.throws(function () {
+			Promise.all('unicorns');
+		}, /You must pass an array to Promise.all()./);
+	});
+
+	it('should resolve empty array to empty array', function (done) {
+		Promise.all([]).then(function (value) {
+			assert.deepEqual(value, []);
+			done();
+		});
+	});
+
+	it('should resolve values to array', function (done) {
+		Promise.all([1, 2, 3]).then(function (value) {
+			assert.deepEqual(value, [1, 2, 3]);
+			done();
+		});
+	});
+
+	it('should resolve promises to array', function (done) {
+		Promise.all([1, 2, 3].map(Promise.resolve)).then(function (value) {
+			assert.deepEqual(value, [1, 2, 3]);
+			done();
+		});
+	});
+
+	it('should pass first rejected promise to onReject', function (done) {
+		Promise.all([Promise.resolve(1), Promise.reject(2), Promise.reject(3)]).then(function () {
+			done('onFullfil called');
+		}, function (reason) {
+			assert.deepEqual(reason, 2);
+			done();
+		});
+	});
+});
+
+function delayedResolve() {
+	return new Promise(function (resolve) {
+		setTimeout(resolve, 10);
+	});
+}
+
+describe('Promise.race', function () {
+	it('should throw error on invalid argument', function () {
+		assert.throws(function () {
+			Promise.race('unicorns');
+		}, /You must pass an array to Promise.race()./);
+	});
+
+	it('empty array should be pending', function (done) {
+		var p = Promise.race([]);
+		setTimeout(function () {
+			assert.deepEqual(p._state, 'pending');
+			done();
+		}, 5);
+	});
+
+	it('should resolve first value', function (done) {
+		Promise.race([1, 2, 3]).then(function (value) {
+			assert.deepEqual(value, 1);
+			done();
+		});
+	});
+
+	it('should resolve first promise', function (done) {
+		Promise.race([1, 2, 3].map(Promise.resolve)).then(function (value) {
+			assert.deepEqual(value, 1);
+			done();
+		});
+	});
+
+	it('should pass first rejected promise to onReject', function (done) {
+		Promise.race([delayedResolve(), delayedResolve(), Promise.reject(3)]).then(function () {
+			done('onFullfil called');
+		}, function (reason) {
+			assert.deepEqual(reason, 3);
+			done();
+		});
+	});
+});
+
+describe('unhandledRejection/rejectionHandled events', function () {
+	var slice = Array.prototype.slice;
+	var events;
+
+	function onUnhandledRejection(reason) {
+		var args = slice.call(arguments);
+		if (reason && reason.message) {
+			args[0] = reason.message;
+		}
+		events.push(['unhandledRejection', args]);
+	}
+
+	function onRejectionHandled() {
+		events.push(['rejectionHandled', slice.call(arguments)]);
+	}
+
+	beforeEach(function () {
+		events = [];
+		process.on('unhandledRejection', onUnhandledRejection);
+		process.on('rejectionHandled', onRejectionHandled);
+	});
+
+	afterEach(function () {
+		process.removeListener('unhandledRejection', onUnhandledRejection);
+		process.removeListener('rejectionHandled', onRejectionHandled);
+	});
+
+	it('should emit an unhandledRejection on the next turn', function (done) {
+		var promise = Promise.reject(new Error('next'));
+		assert.deepEqual(events, []);
+		nextLoop(function () {
+			assert.deepEqual(events, [
+				['unhandledRejection', ['next', promise]]
+			]);
+			done();
+		});
+	});
+
+	it('should not emit any events if handled before the next turn', function (done) {
+		var promise = Promise.reject(new Error('handled immediately after rejection'));
+		promise.catch(noop);
+		nextLoop(function () {
+			assert.deepEqual(events, []);
+			done();
+		});
+	});
+
+	it('should emit a rejectionHandled event if handledLater', function (done) {
+		var promise = Promise.reject(new Error('eventually handled'));
+		nextLoop(function () {
+			promise.catch(noop);
+			nextLoop(function () {
+				assert.deepEqual(events, [
+					['unhandledRejection', ['eventually handled', promise]],
+					['rejectionHandled', [promise]]
+				]);
+				done();
+			});
+		});
+	});
+
+	it('should not emit any events when handled by a chained promise', function (done) {
+		var promise = Promise.reject(new Error('chained'));
+		promise
+			.then(noop)
+			.then(noop)
+			.then(noop)
+			.catch(noop);
+		later(function () {
+			assert.deepStrictEqual(events, []);
+			done();
+		});
+	});
+
+	it('catch() should only emit rejectionHandled one branch of a forked promise chain at a time', function (done) {
+		var def = deferred();
+		var root = def.promise;
+
+		// build the first branch
+		root.then(noop).then(noop).catch(noop);
+
+		// build the second branch
+		var b1 = root.then(noop).then(noop);
+
+		def.reject(new Error('branching'));
+
+		var c;
+
+		later(step1);
+
+		function step1() {
+			b1.catch(noop);
+			c = root.then(noop);
+			later(step2);
+		}
+
+		function step2() {
+			assert.deepStrictEqual(events, [
+				['unhandledRejection', ['branching', b1]],
+				['rejectionHandled', [b1]],
+				['unhandledRejection', ['branching', c]]
+			]);
+			done();
+		}
+	});
+
+	function noop() {}
+
+	function nextLoop(fn) {
+		setImmediate(fn);
+	}
+
+	function later(fn) {
+		setTimeout(fn, 40);
+	}
+});
+
+function deferred() {
+	var resolve;
+	var reject;
+	var promise = new Promise(function (res, rej) {
+		resolve = res;
+		reject = rej;
+	});
+
+	return {
+		promise: promise,
+		resolve: resolve,
+		reject: reject
+	};
+}
+
+describe('Promises/A+ Tests', function () {
+	var adapter = {
+		deferred: deferred
+	};
+
+	require('promises-aplus-tests').mocha(adapter);
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-pinkei.git



More information about the Pkg-javascript-commits mailing list