diff --git a/dist/mesh.js b/dist/mesh.js new file mode 100644 index 0000000..07b1f12 --- /dev/null +++ b/dist/mesh.js @@ -0,0 +1,1342 @@ +(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o= 2) { + this._execute = function (operation) { + return new Promise(function (resolve, reject) { + execute(operation, function (err, result) { + if (err) return reject(err); + resolve.apply(this, Array.prototype.slice.call(arguments, 1)); + }); + }); + }; + } else { + this._execute = execute; + } +} + +/** +*/ + +extend(Bus, WrapBus, { + + /** + */ + + execute: function execute(operation) { + var ret = this._execute(operation); + + // is a readable stream + if (ret && ret.read) return ret; + if (!ret || !ret.then) return BufferedResponse.create(void 0, ret); + if (ret.then) return Response.create(function (writable) { + ret.then(function (value) { + if (value != void 0) writable.write(value); + writable.close(); + }, writable.abort.bind(writable)); + }); + } +}); + +/** +*/ + +module.exports = WrapBus; + +},{"21":21,"22":22,"25":25,"4":4}],20:[function(require,module,exports){ +"use strict"; + +module.exports = function () { + var object = Object.create(this.prototype); + this.apply(object, arguments); + return object; +}; + +},{}],21:[function(require,module,exports){ + +/** + * IE8+ compatible subclassing. See https://babeljs.io/docs/advanced/caveats/ + */ + +'use strict'; + +module.exports = function (parent, child) { + + var props; + var pi; + + var c = child; + var p = parent; + + if (typeof c === 'function') { + pi = 2; + } else { + if (typeof p === 'object') { + c = function () {}; + pi = 0; + } else { + c = p || function () {}; + pi = 1; + } + + p = typeof this === 'function' ? this : Object; + } + + props = Array.prototype.slice.call(arguments, pi); + + function ctor() { + this.constructor = c; + } + + Object.assign(c, p); // copy static props + + ctor.prototype = p.prototype; + c.prototype = new ctor(); + + Object.assign(c.prototype, Object.assign.apply(Object, [{}].concat(props))); + + return c; +}; + +},{}],22:[function(require,module,exports){ +'use strict'; + +var Response = require(25); + +/** + */ + +function BufferedResponse(error, chunkValues) { + Response.call(this, function (writable) { + if (error) writable.abort(error); + chunkValues = Array.isArray(chunkValues) ? chunkValues : chunkValues != void 0 ? [chunkValues] : []; + chunkValues.forEach(writable.write.bind(writable)); + writable.close(); + }); +} + +/** + */ + +Response.extend(BufferedResponse); + +/** + */ + +module.exports = BufferedResponse; + +},{"25":25}],23:[function(require,module,exports){ +'use strict'; + +var BufferedResponse = require(22); +var extend = require(21); + +/** + */ + +function EmptyResponse() { + BufferedResponse.call(this); +} + +/** + */ + +BufferedResponse.extend(EmptyResponse); + +/** + */ + +module.exports = EmptyResponse; + +},{"21":21,"22":22}],24:[function(require,module,exports){ +'use strict'; + +var BufferedResponse = require(22); + +/** + */ + +function ErrorResponse(error) { + BufferedResponse.call(this, error); +} + +/** + */ + +BufferedResponse.extend(ErrorResponse); + +/** + */ + +module.exports = ErrorResponse; + +},{"22":22}],25:[function(require,module,exports){ +'use strict'; + +var WritableStream = require(29); +var extend = require(21); + +/** + * Creates a new Streamed response + */ + +function Response(run) { + + var writer = this._writer = WritableStream.create(); + this._reader = writer.getReader(); + + // todo - pass writable instead + if (run) { + // var ret = run(this._writable); + var ret = run(writer); + + // thenable? Automatically end + if (ret && ret.then) { + ret.then(function () { + writer.close(); + }, writer.abort.bind(writer)); + } + } +} + +/** + */ + +extend(Response, { + + /** + */ + + then: function then(resolve, reject) { + return this._writer.then(resolve, reject); + }, + + /** + */ + + 'catch': function _catch(reject) { + return this._writer['catch'](reject); + }, + + /** + */ + + read: function read() { + return this._reader.read(); + }, + + /** + */ + + pipeTo: function pipeTo(writable, options) { + return this._reader.pipeTo(writable, options); + }, + + /** + */ + + cancel: function cancel() { + return this._reader.cancel(); + } +}); + +/** + */ + +Response.create = require(20); +Response.extend = extend; + +/** + */ + +module.exports = Response; + +},{"20":20,"21":21,"29":29}],26:[function(require,module,exports){ +'use strict'; + +var Response = require(25); + +/** + */ + +function NodeStreamResponse(stream) { + + Response.call(this, function (writable) { + + if (!stream) return writable.close(); + + var pump = function pump() { + stream.resume(); + stream.once('data', function (data) { + stream.pause(); + writable.write(data).then(pump); + }); + }; + + var end = function end() { + writable.close(); + }; + + stream.once('end', end).once('error', writable.abort.bind(writable)); + + pump(); + }); +} + +/** + */ + +Response.extend(NodeStreamResponse); + +/** + */ + +module.exports = NodeStreamResponse; + +},{"25":25}],27:[function(require,module,exports){ +'use strict'; + +var extend = require(21); + +/** + */ + +function PassThrough() { + var _this = this; + + this._values = []; + this._promise = new Promise(function (resolve, reject) { + _this._resolve = resolve; + _this._reject = reject; + }); +} + +/** + */ + +extend(PassThrough, { + + /** + */ + + then: function then(resolve, reject) { + return this._promise.then(resolve, reject); + }, + + /** + */ + + __signalWrite: function __signalWrite() {}, + + /** + */ + + __signalRead: function __signalRead() {}, + + /** + */ + + write: function write(value) { + var _this2 = this; + + if (this._closed) { + return Promise.reject(new Error('cannot write to a closed stream')); + } + + this._values.push(value); + this.__signalWrite(); + return new Promise(function (resolve, reject) { + _this2.__signalRead = function () { + _this2.__signalRead = function () {}; + resolve(); + }; + }); + }, + + /** + */ + + read: function read() { + var _this3 = this; + + this.__signalRead(); + + if (this._error) { + this._reject(this._error); + return Promise.reject(this._error); + } + + if (!!this._values.length) { + return Promise.resolve({ value: this._values.shift(), done: false }); + } + + if (this._closed) { + this._resolve(); + return Promise.resolve({ value: void 0, done: true }); + } + + return new Promise(function (resolve, reject) { + _this3.__signalWrite = function () { + _this3.__signalWrite = function () {}; + _this3.read().then(resolve, reject); + }; + }); + }, + + /** + */ + + abort: function abort(error) { + this._error = error; + this.__signalWrite(); + }, + + /** + */ + + close: function close() { + this._closed = true; + this.__signalWrite(); + } +}); + +/** + */ + +PassThrough.create = require(20); + +/** + */ + +module.exports = PassThrough; + +},{"20":20,"21":21}],28:[function(require,module,exports){ +'use strict'; + +var extend = require(21); + +/** + */ + +function ReadableStream(passThrough) { + this._passThrough = passThrough; +} + +/** + */ + +extend(ReadableStream, { + + /** + */ + + read: function read() { + return this._passThrough.read(); + }, + + /** + */ + + pipeTo: function pipeTo(writable, options) { + var _this = this; + + if (!options) options = {}; + var pump = function pump() { + _this.read().then(function (item) { + if (item.done) { + if (!options.preventClose) writable.close(); + } else { + writable.write(item.value); + pump(); + } + }, options.preventAbort ? void 0 : writable.abort.bind(writable)); + }; + pump(); + return writable; + }, + + /** + */ + + cancel: function cancel() { + return this._passThrough.close(); + } +}); + +/** + */ + +ReadableStream.create = require(20); + +/** + */ + +module.exports = ReadableStream; + +},{"20":20,"21":21}],29:[function(require,module,exports){ +'use strict'; + +var extend = require(21); +var PassThrough = require(27); +var ReadableStream = require(28); + +/** + */ + +function WritableStream(options) { + this._passThrough = PassThrough.create(); + this._reader = ReadableStream.create(this._passThrough); +} + +/** + */ + +extend(WritableStream, { + + /** + */ + + then: function then(resolve, reject) { + return this._passThrough.then(resolve, reject); + }, + + /** + */ + + 'catch': function _catch(reject) { + return this.then(void 0, reject); + }, + + /** + */ + + write: function write(chunk) { + return this._passThrough.write(chunk); + }, + + /** + */ + + abort: function abort(error) { + this._passThrough.abort(error); + }, + + /** + */ + + close: function close() { + this._passThrough.close(); + }, + + /** + */ + + getReader: function getReader() { + return this._reader; + } +}); + +/** + */ + +WritableStream.create = require(20); +WritableStream.extend = require(21); + +/** + */ + +module.exports = WritableStream; + +},{"20":20,"21":21,"27":27,"28":28}]},{},[1]); diff --git a/dist/mesh.min.js b/dist/mesh.min.js new file mode 100644 index 0000000..151f51b --- /dev/null +++ b/dist/mesh.min.js @@ -0,0 +1 @@ +!function t(e,n,r){function i(o,u){if(!n[o]){if(!e[o]){var c="function"==typeof require&&require;if(!u&&c)return c(o,!0);if(s)return s(o,!0);var a=new Error("Cannot find module '"+o+"'");throw a.code="MODULE_NOT_FOUND",a}var f=n[o]={exports:{}};e[o][0].call(f.exports,function(t){var n=e[o][1][t];return i(n?n:t)},f,f.exports,t,e,n,r)}return n[o].exports}for(var s="function"==typeof require&&require,o=0;on;n++){var i=e[n];for(var s in i)void 0==t[s]&&(t[s]=i[s])}return t}function i(t,e){this._properties=t,this._bus=e}var s=t(4);s.extend(i,{execute:function(t){return this._bus.execute(r(t,this._properties))}}),e.exports=i},{4:4}],4:[function(t,e,n){"use strict";function r(){}var i=t(21);i(r,{execute:function(t){}}),r.create=t(20),r.extend=i,e.exports=r},{20:20,21:21}],5:[function(t,e,n){"use strict";function r(t,e){this._error=t,this._chunkValues=e}var i=t(4),s=t(22);i.extend(r,{execute:function(t){return s.create(this._error,this._chunkValues)}}),e.exports=r},{22:22,4:4}],6:[function(t,e,n){"use strict";function r(t,e){this._bus=t,this._catchError=e}var i=t(4),s=t(25);i.extend(r,{execute:function(t){var e=this;return s.create(function(n){e._bus.execute(t).pipeTo({write:function(t){n.write(t)},close:function(){n.close()},abort:function(r){try{e._catchError(r,t);n.close()}catch(i){n.abort(i)}}})})}}),e.exports=r},{25:25,4:4}],7:[function(t,e,n){"use strict";function r(t,e){this._ms=e,this._bus=t}var i=t(4),s=t(25);i.extend(r,{execute:function(t){var e=this;return s.create(function(n){setTimeout(function(){e._bus.execute(t).pipeTo(n)},e._ms)})}}),e.exports=r},{25:25,4:4}],8:[function(t,e,n){"use strict";function r(t){this._busses=t}var i=t(4),s=t(25);i.extend(r,{execute:function(t){var e=this;return s.create(function(n){var r=e._busses.concat(),i=function s(e){if(e===r.length)return n.close();var i=r[e].execute(t),o=!1;i.pipeTo({write:function(t){o=!0,n.write(t)},close:function(){o?n.close():s(e+1)},abort:n.abort.bind(n)})};i(0)})}}),e.exports=r},{25:25,4:4}],9:[function(t,e,n){"use strict";function r(t,e){this._bus=t,this._map=e}var i=t(4),s=t(25);i.extend(r,{execute:function(t){var e=this;return s.create(function(n){e._bus.execute(t).pipeTo({write:function(r){try{e._map(r,n,t)}catch(i){return n.abort(i),Promise.reject(i)}},close:function(){n.close()},abort:n.abort.bind(n)})})}}),e.exports=r},{25:25,4:4}],10:[function(t,e,n){"use strict";function r(){}var i=t(4),s=t(23);i.extend(r,{execute:function(t){return s.create()}}),e.exports=r},{23:23,4:4}],11:[function(t,e,n){"use strict";function r(t){this._busses=t}var i=t(4),s=t(25);i.extend(r,{execute:function(t){var e=this;return s.create(function(n){var r=e._busses.concat(),i=r.length;r.forEach(function(e){var r=e.execute(t);r.pipeTo(n,{preventClose:!0}),r.then(function(){--i||n.close()})})})}}),e.exports=r},{25:25,4:4}],12:[function(t,e,n){"use strict";function r(t){this._busses=t}var i=t(4),s=t(25);i.extend(r,{execute:function(t){var e=this;return s.create(function(n){var r=e._busses.concat(),i=r.length,s=-1;r.forEach(function(e,r){var o=e.execute(t);o.pipeTo({write:function(t){~s&&s!==r||(s=r,n.write(t))},close:function(){(~s&&s===r||0===--i)&&n.close()},abort:n.abort.bind(n)})})})}}),e.exports=r},{25:25,4:4}],13:[function(t,e,n){"use strict";function r(t){this._busses=t}var i=t(4);i.extend(r,{execute:function(t){return this._busses[Math.floor(Math.random()*this._busses.length)].execute(t)}}),e.exports=r},{4:4}],14:[function(t,e,n){"use strict";function r(t,e,n){i.call(this,t,n,e)}var i=t(2);i.extend(r),e.exports=r},{2:2}],15:[function(t,e,n){"use strict";function r(t,e,n){2===arguments.length&&(n=e,e=function(t,e){return!0}),this._maxRetries=t,this._bus=n,this._errorFilter=e}var i=t(4),s=t(25);i.extend(r,{execute:function(t){var e=this;return s.create(function(n){var r,i=!1,s=function o(s){if(!s)return n.abort(r);var u=e._bus.execute(t);u.pipeTo({write:function(t){i=!0,n.write(t)},close:function(){n.close()},abort:function(t){r=t,o(s-1)}})};s(e._maxRetries)})}}),e.exports=r},{25:25,4:4}],16:[function(t,e,n){"use strict";function r(t){this._busses=t,this._i=0}var i=t(4);i.extend(r,{execute:function(t){var e=this._busses[this._i].execute(t);return this._i=(this._i+1)%this._busses.length,e}}),e.exports=r},{4:4}],17:[function(t,e,n){"use strict";function r(t){this._busses=t}var i=t(4),s=t(25);i.extend(r,{execute:function(t){var e=this;return s.create(function(n){var r=e._busses.concat(),i=function s(e){if(e===r.length)return n.close();var i=r[e].execute(t);i.pipeTo(n,{preventClose:!0}),i.then(function(){return s(e+1)})};i(0)})}}),e.exports=r},{25:25,4:4}],18:[function(t,e,n){"use strict";function r(t){this._bus=t,this._tails=[]}var i=t(4),s=t(25);i.extend(r,{addTail:function(t){var e=this;return s.create(function(t){e._tails.push(t),t.then(function(){e._tails.splice(e._tails.indexOf(t),1)})})},execute:function(t){var e=this,n=this._bus.execute(t);return n.then(function(){e._tails.forEach(function(e){e.write(t)})}),n}}),e.exports=r},{25:25,4:4}],19:[function(t,e,n){"use strict";function r(t){t.length>=2?this._execute=function(e){return new Promise(function(n,r){t(e,function(t,e){return t?r(t):void n.apply(this,Array.prototype.slice.call(arguments,1))})})}:this._execute=t}var i=t(4),s=t(21),o=t(25),u=t(22);s(i,r,{execute:function(t){var e=this._execute(t);return e&&e.read?e:e&&e.then?e.then?o.create(function(t){e.then(function(e){void 0!=e&&t.write(e),t.close()},t.abort.bind(t))}):void 0:u.create(void 0,e)}}),e.exports=r},{21:21,22:22,25:25,4:4}],20:[function(t,e,n){"use strict";e.exports=function(){var t=Object.create(this.prototype);return this.apply(t,arguments),t}},{}],21:[function(t,e,n){"use strict";e.exports=function(t,e){function n(){this.constructor=s}var r,i,s=e,o=t;return"function"==typeof s?i=2:("object"==typeof o?(s=function(){},i=0):(s=o||function(){},i=1),o="function"==typeof this?this:Object),r=Array.prototype.slice.call(arguments,i),Object.assign(s,o),n.prototype=o.prototype,s.prototype=new n,Object.assign(s.prototype,Object.assign.apply(Object,[{}].concat(r))),s}},{}],22:[function(t,e,n){"use strict";function r(t,e){i.call(this,function(n){t&&n.abort(t),e=Array.isArray(e)?e:void 0!=e?[e]:[],e.forEach(n.write.bind(n)),n.close()})}var i=t(25);i.extend(r),e.exports=r},{25:25}],23:[function(t,e,n){"use strict";function r(){i.call(this)}var i=t(22);t(21);i.extend(r),e.exports=r},{21:21,22:22}],24:[function(t,e,n){"use strict";function r(t){i.call(this,t)}var i=t(22);i.extend(r),e.exports=r},{22:22}],25:[function(t,e,n){"use strict";function r(t){var e=this._writer=i.create();if(this._reader=e.getReader(),t){var n=t(e);n&&n.then&&n.then(function(){e.close()},e.abort.bind(e))}}var i=t(29),s=t(21);s(r,{then:function(t,e){return this._writer.then(t,e)},"catch":function(t){return this._writer["catch"](t)},read:function(){return this._reader.read()},pipeTo:function(t,e){return this._reader.pipeTo(t,e)},cancel:function(){return this._reader.cancel()}}),r.create=t(20),r.extend=s,e.exports=r},{20:20,21:21,29:29}],26:[function(t,e,n){"use strict";function r(t){i.call(this,function(e){if(!t)return e.close();var n=function i(){t.resume(),t.once("data",function(n){t.pause(),e.write(n).then(i)})},r=function(){e.close()};t.once("end",r).once("error",e.abort.bind(e)),n()})}var i=t(25);i.extend(r),e.exports=r},{25:25}],27:[function(t,e,n){"use strict";function r(){var t=this;this._values=[],this._promise=new Promise(function(e,n){t._resolve=e,t._reject=n})}var i=t(21);i(r,{then:function(t,e){return this._promise.then(t,e)},__signalWrite:function(){},__signalRead:function(){},write:function(t){var e=this;return this._closed?Promise.reject(new Error("cannot write to a closed stream")):(this._values.push(t),this.__signalWrite(),new Promise(function(t,n){e.__signalRead=function(){e.__signalRead=function(){},t()}}))},read:function(){var t=this;return this.__signalRead(),this._error?(this._reject(this._error),Promise.reject(this._error)):this._values.length?Promise.resolve({value:this._values.shift(),done:!1}):this._closed?(this._resolve(),Promise.resolve({value:void 0,done:!0})):new Promise(function(e,n){t.__signalWrite=function(){t.__signalWrite=function(){},t.read().then(e,n)}})},abort:function(t){this._error=t,this.__signalWrite()},close:function(){this._closed=!0,this.__signalWrite()}}),r.create=t(20),e.exports=r},{20:20,21:21}],28:[function(t,e,n){"use strict";function r(t){this._passThrough=t}var i=t(21);i(r,{read:function(){return this._passThrough.read()},pipeTo:function(t,e){var n=this;e||(e={});var r=function i(){n.read().then(function(n){n.done?e.preventClose||t.close():(t.write(n.value),i())},e.preventAbort?void 0:t.abort.bind(t))};return r(),t},cancel:function(){return this._passThrough.close()}}),r.create=t(20),e.exports=r},{20:20,21:21}],29:[function(t,e,n){"use strict";function r(t){this._passThrough=s.create(),this._reader=o.create(this._passThrough)}var i=t(21),s=t(27),o=t(28);i(r,{then:function(t,e){return this._passThrough.then(t,e)},"catch":function(t){return this.then(void 0,t)},write:function(t){return this._passThrough.write(t)},abort:function(t){this._passThrough.abort(t)},close:function(){this._passThrough.close()},getReader:function(){return this._reader}}),r.create=t(20),r.extend=t(21),e.exports=r},{20:20,21:21,27:27,28:28}]},{},[1]); \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js index cfeb7c6..86e936a 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -68,8 +68,9 @@ gulp.task('test-coveralls', ['test-coverage'], function() { */ gulp.task('bundle', function() { - return browserify('./lib/index.js'). + return browserify('./index.js'). plugin(collapser). + transform("babelify"). bundle(). pipe(source(pkg.name + '.js')). pipe(buffer()). diff --git a/package.json b/package.json index 66b751f..7ce9230 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ }, "homepage": "https://github.com/crcn/mesh.js", "devDependencies": { + "babelify": "^6.4.0", "browserify": "^9.0.4", "bundle-collapser": "^1.2.0", "co": "^4.6.0",