Skip to content

Instantly share code, notes, and snippets.

@bhyde
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhyde/9766023 to your computer and use it in GitHub Desktop.
Save bhyde/9766023 to your computer and use it in GitHub Desktop.
asparagus

P is a peer 2 peer scheme based on WebRTC.

The fun thing here is a network status widget defined in mon.js. It displays the list of peers and their isOpen status.

drc.html and drc.js attempt to be the simplest peers possible.

I don’t understand:

  1. Why I get to connect message for each peer I fire up.
  2. Why these new peers become known but why isOpen remains false.

Currently running at https://www.cozy.org/drc/drc.html

<!doctype html>
<html>
<head>
<title>Dr. Connect</title>
<script type='text/javascript' src="p.js"></script>
<script type='text/javascript' src="drc.js"></script>
<script type='text/javascript' src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type='text/javascript' src="mon.js"></script>
<script type='text/javascript'>
$(function (){
inject_p2p_monitor(window.p2p.join().p);
})
</script>
</head>
</html>
window.p2p = {};
window.p2p.join =
function(){
var me = window.p2p;
console.log('P.create');
me.p = P.create();
console.log('p.connect to onramp');
me.onramp = me.p.connect('ws://'+location.hostname+':20500/');
me.onramp.on('message',
function(peerAddress){
console.log('onramp/message: ' + peerAddress);
console.log('so do onramp.connect');
me.onramp.connect({ address: peerAddress, offerData: "Hi!" });});
me.onramp.on('connection',
function(peer){
console.log('onramp/connection: %O %s', peer, peer.address);});
return me;};
(function(){
var templates = {};
function define_template(name, template){ templates[name]=template; }
function expand_template() {
var args = Array.prototype.slice.call(arguments);
var template = templates[args.shift()];
return template.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined' ? args[number] : match; }); }
window.inject_p2p_monitor = function (p){
define_template('monitor_entry', '<div>{0} {1}</div>');
var m=$('<div/>');
m.css('float','right')
.css('width','30em')
.css('border', '1px solid black')
.css('font-size','60%');
function refresh (){
m.text('Network Status:');
p.getPeers().map(
function(p){
m.append(expand_template('monitor_entry',
p.isOpen()?"\u2714":"\u2718",
p.address));});}
function refresh_p2p_monitor_heartbeat (){
refresh();
setTimeout(refresh_p2p_monitor_heartbeat,1000);}
$('body').prepend(m);
refresh_p2p_monitor_heartbeat();
return m;};
})();
!function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);throw new Error("Cannot find module '"+g+"'")}var j=c[g]={exports:{}};b[g][0].call(j.exports,function(a){var c=b[g][1][a];return e(c?c:a)},j,j.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b){function c(){throw new Error("This method is not implemented")}function d(a,b,c){g.string(a),g.defined(b),this.address=a,this.peers=b,c&&(c.emitter&&(this.emitter=c.emitter),c.firewall&&(this.acceptRTCConnection=c.firewall)),this.emitter||(this.emitter=new d.Emitter)}function e(a){return"[object String]"===Object.prototype.toString.call(a)}var f=a("./JSONProtocol.js"),g=a("its"),h=a("events").EventEmitter;d.createWebRTCConnection=null,d.Emitter=h,d.prototype=Object.create(f.prototype),d.prototype.on=function(){return this.emitter.on.apply(this.emitter,arguments),this},d.prototype.removeListener=function(){return this.emitter.removeListener.apply(this.emitter,arguments),this},d.prototype.send=f.prototype.writeMessage,d.prototype.getPeer=function(a){return this.peers.get(a)},d.prototype.addPeer=function(a){return this.peers.add(a)},d.prototype.getPeers=function(){return this.peers.get()},d.prototype.connect=function(a){e(a)&&(a={address:a});var b=this,c=a.firewall||this.firewall,f=d.createWebRTCConnection(a,this.peers,this,{firewall:c});return f.writeOffer(a),this.peers.add(f),f.on("close",function(){b.peers.remove(f),b.emitter.emit("disconnection",f)}),this.emitter.emit("connection",f),f},d.prototype.readMessage=function(a){this.emitter.emit("message",a)},d.prototype.readArrayBuffer=function(a){this.emitter.emit("arraybuffer",a)},d.prototype.acceptRTCConnection=function(){return!0},d.prototype.readRelay=function(a,b){var c=this.getPeer(a);return c?(c.writeRelayedMessage(this.address,b),void 0):(this.emitter.emit("error",new Error("Unknown peer at address: "+a)),void 0)},d.prototype.readRelayedIceCandidate=function(a,b){var c=this.getPeer(a);return c?(c.readIceCandidate(b),void 0):(this.emitter.emit("error",new Error("Unknown peer at address: "+a)),void 0)},d.prototype.readRelayedOffer=function(a,b,c){if(!this.acceptRTCConnection(b,c))return!1;var e=this,f=d.createWebRTCConnection({address:a},this.peers,this,{firewall:this.firewall});this.addPeer(f),f.on("close",function(){e.peers.remove(f),e.emitter.emit("disconnection",f)}),f.readOffer(b),f.writeAnswer(),this.emitter.emit("connection",f)},d.prototype.readRelayedAnswer=function(a,b){var c=this.getPeer(a);return c?(c.readAnswer(b),void 0):(this.emitter.emit("error",new Error("Unknown peer at address: "+a)),void 0)},d.prototype.close=c,d.prototype.getReadyState=c,d.prototype.isOpen=function(){return"open"===this.getReadyState()},b.exports=d},{"./JSONProtocol.js":3,events:9,its:11}],2:[function(a,b){function c(){}function d(){this.connectionMap={},this.connectionList=[]}var e=a("its");d.prototype.get=function(a){return void 0===a?this.connectionList.slice():this.connectionMap[a]},d.prototype.add=function(a){e.defined(a);var b=a.address;return e.string(b),b in this.connectionMap?!1:(this.connectionMap[b]=a,this.connectionList.push(a),this.onAdd(a),!0)},d.prototype.onAdd=c,d.prototype.remove=function(a){e.defined(a);var b=a.address;e.string(b);var c=this.connectionMap[b];if(!c||c!==a)return!1;delete this.connectionMap[b];var d=this.connectionList.indexOf(a);return this.connectionList.splice(d,1),this.onRemove(a),!0},d.prototype.onRemove=c,b.exports=d},{its:11}],3:[function(a,b){function c(){throw new Error("This method is not implemented")}function d(){}d.prototype.PROTOCOL_NAME="p",d.prototype.MESSAGE_TYPE={DIRECT:0,RTC_OFFER:3,RTC_ANSWER:4,RTC_ICE_CANDIDATE:5,RELAY:6,RELAYED:7},d.prototype.readRaw=function(a){a instanceof ArrayBuffer?this.readArrayBuffer(a):this.readProtocolMessage(JSON.parse(a))},d.prototype.readProtocolMessage=function(a){var b=this.MESSAGE_TYPE,c=a[0];switch(c){case b.DIRECT:this.readMessage(a[1]);break;case b.RELAYED:this.readRelayedMessage(a[1],a[2]);break;case b.RELAY:this.readRelay(a[1],a[2]);break;default:throw new Error("Unknown message type: "+c)}},d.prototype.readRelayedMessage=function(a,b){var c=this.MESSAGE_TYPE,d=b[0];switch(d){case c.RTC_OFFER:this.readRelayedOffer(a,b[1],b[2]);break;case c.RTC_ANSWER:this.readRelayedAnswer(a,b[1]);break;case c.RTC_ICE_CANDIDATE:this.readRelayedIceCandidate(a,b[1]);break;default:throw new Error("Unknown message type: "+d)}},d.prototype.readMessage=c,d.prototype.readArrayBuffer=c,d.prototype.readRelay=c,d.prototype.readRelayedOffer=c,d.prototype.readRelayedAnswer=c,d.prototype.readRelayedIceCandidate=c,d.prototype.writeRaw=c,d.prototype.writeProtocolMessage=function(a){var b=JSON.stringify(a);this.writeRaw(b)},d.prototype.writeMessage=function(a){a instanceof ArrayBuffer?this.writeRaw(a):this.writeStringMessage(a)},d.prototype.writeStringMessage=function(a){this.writeProtocolMessage([this.MESSAGE_TYPE.DIRECT,a])},d.prototype.writeRelayedMessage=function(a,b){this.writeProtocolMessage([this.MESSAGE_TYPE.RELAYED,a,b])},d.prototype.writeRelayMessage=function(a,b){this.writeProtocolMessage([this.MESSAGE_TYPE.RELAY,a,b])},d.prototype.writeRelayAnswer=function(a,b){this.writeRelayMessage(a,[this.MESSAGE_TYPE.RTC_ANSWER,b])},d.prototype.writeRelayIceCandidate=function(a,b){this.writeRelayMessage(a,[this.MESSAGE_TYPE.RTC_ICE_CANDIDATE,b])},d.prototype.writeRelayOffer=function(a,b,c){this.writeRelayMessage(a,[this.MESSAGE_TYPE.RTC_OFFER,b,c])},b.exports=d},{}],4:[function(a,b){function c(a,b,c){g.defined(a),g.defined(b),this.emitter=a,this.peers=b,this.peers.onAdd=function(b){a.emit("connection",b)},this.peers.onRemove=function(b){a.emit("disconnection",b)},c&&c.firewall&&(this.firewall=c.firewall)}var d=a("events").EventEmitter,e=a("./ConnectionManager.js"),f=a("./WebSocketConnection.js"),g=(a("./WebRTCConnection.js"),a("its"));c.create=function(a){var b=new d,f=new e;return new c(b,f,a)},c.prototype.getPeers=function(){return this.peers.get()},c.prototype.connect=function(a){g.string(a);var b=this.peers,c=f.create(a,this.peers,{firewall:this.firewall});return b.add(c),c.on("close",function(){b.remove(c)}),c},c.prototype.on=function(){return this.emitter.on.apply(this.emitter,arguments),this},c.prototype.removeListener=function(){return this.emitter.removeListener.apply(this.emitter,arguments),this},b.exports=c},{"./ConnectionManager.js":2,"./WebRTCConnection.js":5,"./WebSocketConnection.js":6,events:9,its:11}],5:[function(a,b){function c(a,b,c,f,g){var h=this;e.string(a),e.defined(b),e.defined(c),e.defined(f),d.call(this,a,b,g),this.signalingChannel=f,this.rtcConnection=c,this.rtcDataChannel=c.createDataChannel(this.PROTOCOL_NAME,{reliable:!1}),this.close=c.close.bind(c),this.rtcConnection.addEventListener("icecandidate",function(b){b.candidate&&h.signalingChannel.writeRelayIceCandidate(a,b.candidate)}),this.rtcDataChannel.addEventListener("message",function(a){h.readRaw(a.data)}),this.rtcDataChannel.addEventListener("open",function(a){h.emitter.emit("open",a)}),this.rtcDataChannel.addEventListener("error",function(a){h.emitter.emit("error",a)}),this.rtcDataChannel.addEventListener("close",function(a){h.emitter.emit("close",a)})}var d=a("./Connection.js"),e=a("its"),f="undefined"!=typeof RTCPeerConnection?RTCPeerConnection:"undefined"!=typeof webkitRTCPeerConnection?webkitRTCPeerConnection:"undefined"!=typeof mozRTCPeerConnection?mozRTCPeerConnection:void 0,g="undefined"!=typeof RTCSessionDescription?RTCSessionDescription:"undefined"!=typeof mozRTCSessionDescription?mozRTCSessionDescription:void 0,h="undefined"!=typeof RTCIceCandidate?RTCIceCandidate:"undefined"!=typeof mozRTCIceCandidate?mozRTCIceCandidate:void 0,i=null,j={optional:[{RtpDataChannels:!0}],mandatory:{OfferToReceiveAudio:!1,OfferToReceiveVideo:!1}};c.create=function(a,b,d,e){var g=a.rtcConfiguration||i,h=a.mediaConstraints||j,k=new f(g,h);return new c(a.address,b,k,d,e)},c.prototype=Object.create(d.prototype),c.prototype.writeRaw=function(a){switch(this.rtcDataChannel.readyState){case"connecting":throw new Error("Can't send a message while RTCDataChannel connecting");case"open":this.rtcDataChannel.send(a);break;case"closing":case"closed":throw new Error("Can't send a message while RTCDataChannel is closing or closed")}},c.prototype.readAnswer=function(a){var b=new g(a);this.rtcConnection.setRemoteDescription(b)},c.prototype.readOffer=function(a){var b=new g(a);this.rtcConnection.setRemoteDescription(b)},c.prototype.readIceCandidate=function(a){this.emitter;this.rtcConnection.addIceCandidate(new h(a))},c.prototype.writeAnswer=function(){function a(a){b.emit("error",a)}var b=this.emitter,c=this.address,d=this.rtcConnection,e=this.signalingChannel;d.createAnswer(function(b){d.setLocalDescription(b,function(){e.writeRelayAnswer(c,b)},a)},a)},c.prototype.writeOffer=function(a){function b(a){c.emit("error",a)}var c=this.emitter,d=this.address,e=this.rtcConnection,f=this.signalingChannel;e.createOffer(function(c){e.setLocalDescription(c,function(){f.writeRelayOffer(d,c,a.offerData)},b)},b,a.mediaConstraints||j)},c.prototype.getReadyState=function(){return this.rtcDataChannel.readyState},d.createWebRTCConnection=c.create,b.exports=c},{"./Connection.js":1,its:11}],6:[function(a,b){function c(a,b,c,e){var f=this;d.call(this,a,b,e),this.webSocket=c,this.close=c.close.bind(c),this.webSocket.addEventListener("message",function(a){f.readRaw(a.data)}),this.webSocket.addEventListener("open",function(a){f.emitter.emit("open",a)}),this.webSocket.addEventListener("error",function(a){f.emitter.emit("error",a)}),this.webSocket.addEventListener("close",function(a){f.emitter.emit("close",a)})}var d=a("./Connection.js");c.create=function(a,b,d){var e=new WebSocket(a,c.prototype.PROTOCOL_NAME);return new c(a,b,e,d)},c.prototype=Object.create(d.prototype),c.prototype.writeRaw=function(a){switch(this.webSocket.readyState){case WebSocket.CONNECTING:throw new Error("Can't send a message while WebSocket connecting");case WebSocket.OPEN:this.webSocket.send(a);break;case WebSocket.CLOSING:case WebSocket.CLOSED:throw new Error("Can't send a message while WebSocket is closing or closed")}},c.prototype.getReadyState=function(){switch(this.webSocket.readyState){case WebSocket.CONNECTING:return"connecting";case WebSocket.OPEN:return"open";case WebSocket.CLOSING:return"closing";case WebSocket.CLOSED:return"closed"}},b.exports=c},{"./Connection.js":1}],7:[function(a){window.P=a("./P.js")},{"./P.js":4}],8:[function(a,b,c){function d(a){return"[object Array]"===j.call(a)}function e(a,b){var c;if(null===a)c={__proto__:null};else{if("object"!=typeof a)throw new TypeError("typeof prototype["+typeof a+"] != 'object'");var d=function(){};d.prototype=a,c=new d,c.__proto__=a}return"undefined"!=typeof b&&Object.defineProperties&&Object.defineProperties(c,b),c}function f(a){return"object"!=typeof a&&"function"!=typeof a||null===a}function g(a){if(f(a))throw new TypeError("Object.keys called on a non-object");var b=[];for(var c in a)k.call(a,c)&&b.push(c);return b}function h(a){if(f(a))throw new TypeError("Object.getOwnPropertyNames called on a non-object");var b=g(a);return c.isArray(a)&&-1===c.indexOf(a,"length")&&b.push("length"),b}function i(a,b){return{value:a[b]}}var j=Object.prototype.toString,k=Object.prototype.hasOwnProperty;c.isArray="function"==typeof Array.isArray?Array.isArray:d,c.indexOf=function(a,b){if(a.indexOf)return a.indexOf(b);for(var c=0;c<a.length;c++)if(b===a[c])return c;return-1},c.filter=function(a,b){if(a.filter)return a.filter(b);for(var c=[],d=0;d<a.length;d++)b(a[d],d,a)&&c.push(a[d]);return c},c.forEach=function(a,b,c){if(a.forEach)return a.forEach(b,c);for(var d=0;d<a.length;d++)b.call(c,a[d],d,a)},c.map=function(a,b){if(a.map)return a.map(b);for(var c=new Array(a.length),d=0;d<a.length;d++)c[d]=b(a[d],d,a);return c},c.reduce=function(a,b,c){if(a.reduce)return a.reduce(b,c);var d,e=!1;2<arguments.length&&(d=c,e=!0);for(var f=0,g=a.length;g>f;++f)a.hasOwnProperty(f)&&(e?d=b(d,a[f],f,a):(d=a[f],e=!0));return d},c.substr="b"!=="ab".substr(-1)?function(a,b,c){return 0>b&&(b=a.length+b),a.substr(b,c)}:function(a,b,c){return a.substr(b,c)},c.trim=function(a){return a.trim?a.trim():a.replace(/^\s+|\s+$/g,"")},c.bind=function(){var a=Array.prototype.slice.call(arguments),b=a.shift();if(b.bind)return b.bind.apply(b,a);var c=a.shift();return function(){b.apply(c,a.concat([Array.prototype.slice.call(arguments)]))}},c.create="function"==typeof Object.create?Object.create:e;var l="function"==typeof Object.keys?Object.keys:g,m="function"==typeof Object.getOwnPropertyNames?Object.getOwnPropertyNames:h;if((new Error).hasOwnProperty("description")){var n=function(a,b){return"[object Error]"===j.call(a)&&(b=c.filter(b,function(a){return"description"!==a&&"number"!==a&&"message"!==a})),b};c.keys=function(a){return n(a,l(a))},c.getOwnPropertyNames=function(a){return n(a,m(a))}}else c.keys=l,c.getOwnPropertyNames=m;if("function"==typeof Object.getOwnPropertyDescriptor)try{Object.getOwnPropertyDescriptor({a:1},"a"),c.getOwnPropertyDescriptor=Object.getOwnPropertyDescriptor}catch(o){c.getOwnPropertyDescriptor=function(a,b){try{return Object.getOwnPropertyDescriptor(a,b)}catch(c){return i(a,b)}}}else c.getOwnPropertyDescriptor=i},{}],9:[function(a,b){function c(){this._events=this._events||{},this._maxListeners=this._maxListeners||void 0}var d=a("util");b.exports=c,c.EventEmitter=c,c.prototype._events=void 0,c.prototype._maxListeners=void 0,c.defaultMaxListeners=10,c.prototype.setMaxListeners=function(a){if(!d.isNumber(a)||0>a)throw TypeError("n must be a positive number");return this._maxListeners=a,this},c.prototype.emit=function(a){var b,c,e,f,g,h;if(this._events||(this._events={}),"error"===a&&(!this._events.error||d.isObject(this._events.error)&&!this._events.error.length))throw b=arguments[1],b instanceof Error?b:TypeError('Uncaught, unspecified "error" event.');if(c=this._events[a],d.isUndefined(c))return!1;if(d.isFunction(c))switch(arguments.length){case 1:c.call(this);break;case 2:c.call(this,arguments[1]);break;case 3:c.call(this,arguments[1],arguments[2]);break;default:for(e=arguments.length,f=new Array(e-1),g=1;e>g;g++)f[g-1]=arguments[g];c.apply(this,f)}else if(d.isObject(c)){for(e=arguments.length,f=new Array(e-1),g=1;e>g;g++)f[g-1]=arguments[g];for(h=c.slice(),e=h.length,g=0;e>g;g++)h[g].apply(this,f)}return!0},c.prototype.addListener=function(a,b){var e;if(!d.isFunction(b))throw TypeError("listener must be a function");if(this._events||(this._events={}),this._events.newListener&&this.emit("newListener",a,d.isFunction(b.listener)?b.listener:b),this._events[a]?d.isObject(this._events[a])?this._events[a].push(b):this._events[a]=[this._events[a],b]:this._events[a]=b,d.isObject(this._events[a])&&!this._events[a].warned){var e;e=d.isUndefined(this._maxListeners)?c.defaultMaxListeners:this._maxListeners,e&&e>0&&this._events[a].length>e&&(this._events[a].warned=!0,console.error("(node) warning: possible EventEmitter memory leak detected. %d listeners added. Use emitter.setMaxListeners() to increase limit.",this._events[a].length),console.trace())}return this},c.prototype.on=c.prototype.addListener,c.prototype.once=function(a,b){function c(){this.removeListener(a,c),b.apply(this,arguments)}if(!d.isFunction(b))throw TypeError("listener must be a function");return c.listener=b,this.on(a,c),this},c.prototype.removeListener=function(a,b){var c,e,f,g;if(!d.isFunction(b))throw TypeError("listener must be a function");if(!this._events||!this._events[a])return this;if(c=this._events[a],f=c.length,e=-1,c===b||d.isFunction(c.listener)&&c.listener===b)delete this._events[a],this._events.removeListener&&this.emit("removeListener",a,b);else if(d.isObject(c)){for(g=f;g-->0;)if(c[g]===b||c[g].listener&&c[g].listener===b){e=g;break}if(0>e)return this;1===c.length?(c.length=0,delete this._events[a]):c.splice(e,1),this._events.removeListener&&this.emit("removeListener",a,b)}return this},c.prototype.removeAllListeners=function(a){var b,c;if(!this._events)return this;if(!this._events.removeListener)return 0===arguments.length?this._events={}:this._events[a]&&delete this._events[a],this;if(0===arguments.length){for(b in this._events)"removeListener"!==b&&this.removeAllListeners(b);return this.removeAllListeners("removeListener"),this._events={},this}if(c=this._events[a],d.isFunction(c))this.removeListener(a,c);else for(;c.length;)this.removeListener(a,c[c.length-1]);return delete this._events[a],this},c.prototype.listeners=function(a){var b;return b=this._events&&this._events[a]?d.isFunction(this._events[a])?[this._events[a]]:this._events[a].slice():[]},c.listenerCount=function(a,b){var c;return c=a._events&&a._events[b]?d.isFunction(a._events[b])?1:a._events[b].length:0}},{util:10}],10:[function(a,b,c){function d(a,b){var d={seen:[],stylize:f};return arguments.length>=3&&(d.depth=arguments[2]),arguments.length>=4&&(d.colors=arguments[3]),o(b)?d.showHidden=b:b&&c._extend(d,b),u(d.showHidden)&&(d.showHidden=!1),u(d.depth)&&(d.depth=2),u(d.colors)&&(d.colors=!1),u(d.customInspect)&&(d.customInspect=!0),d.colors&&(d.stylize=e),h(d,a,d.depth)}function e(a,b){var c=d.styles[b];return c?"["+d.colors[c][0]+"m"+a+"["+d.colors[c][1]+"m":a}function f(a){return a}function g(a){var b={};return G.forEach(a,function(a){b[a]=!0}),b}function h(a,b,d){if(a.customInspect&&b&&z(b.inspect)&&b.inspect!==c.inspect&&(!b.constructor||b.constructor.prototype!==b)){var e=b.inspect(d);return s(e)||(e=h(a,e,d)),e}var f=i(a,b);if(f)return f;var o=G.keys(b),p=g(o);if(a.showHidden&&(o=G.getOwnPropertyNames(b)),0===o.length){if(z(b)){var q=b.name?": "+b.name:"";return a.stylize("[Function"+q+"]","special")}if(v(b))return a.stylize(RegExp.prototype.toString.call(b),"regexp");if(x(b))return a.stylize(Date.prototype.toString.call(b),"date");if(y(b))return j(b)}var r="",t=!1,u=["{","}"];if(n(b)&&(t=!0,u=["[","]"]),z(b)){var w=b.name?": "+b.name:"";r=" [Function"+w+"]"}if(v(b)&&(r=" "+RegExp.prototype.toString.call(b)),x(b)&&(r=" "+Date.prototype.toUTCString.call(b)),y(b)&&(r=" "+j(b)),0===o.length&&(!t||0==b.length))return u[0]+r+u[1];if(0>d)return v(b)?a.stylize(RegExp.prototype.toString.call(b),"regexp"):a.stylize("[Object]","special");a.seen.push(b);var A;return A=t?k(a,b,d,p,o):o.map(function(c){return l(a,b,d,p,c,t)}),a.seen.pop(),m(A,r,u)}function i(a,b){if(u(b))return a.stylize("undefined","undefined");if(s(b)){var c="'"+JSON.stringify(b).replace(/^"|"$/g,"").replace(/'/g,"\\'").replace(/\\"/g,'"')+"'";return a.stylize(c,"string")}return r(b)?a.stylize(""+b,"number"):o(b)?a.stylize(""+b,"boolean"):p(b)?a.stylize("null","null"):void 0}function j(a){return"["+Error.prototype.toString.call(a)+"]"}function k(a,b,c,d,e){for(var f=[],g=0,h=b.length;h>g;++g)F(b,String(g))?f.push(l(a,b,c,d,String(g),!0)):f.push("");return G.forEach(e,function(e){e.match(/^\d+$/)||f.push(l(a,b,c,d,e,!0))}),f}function l(a,b,c,d,e,f){var g,i,j;if(j=G.getOwnPropertyDescriptor(b,e)||{value:b[e]},j.get?i=j.set?a.stylize("[Getter/Setter]","special"):a.stylize("[Getter]","special"):j.set&&(i=a.stylize("[Setter]","special")),F(d,e)||(g="["+e+"]"),i||(G.indexOf(a.seen,j.value)<0?(i=p(c)?h(a,j.value,null):h(a,j.value,c-1),i.indexOf("\n")>-1&&(i=f?i.split("\n").map(function(a){return" "+a}).join("\n").substr(2):"\n"+i.split("\n").map(function(a){return" "+a}).join("\n"))):i=a.stylize("[Circular]","special")),u(g)){if(f&&e.match(/^\d+$/))return i;g=JSON.stringify(""+e),g.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)?(g=g.substr(1,g.length-2),g=a.stylize(g,"name")):(g=g.replace(/'/g,"\\'").replace(/\\"/g,'"').replace(/(^"|"$)/g,"'"),g=a.stylize(g,"string"))}return g+": "+i}function m(a,b,c){var d=0,e=G.reduce(a,function(a,b){return d++,b.indexOf("\n")>=0&&d++,a+b.replace(/\u001b\[\d\d?m/g,"").length+1},0);return e>60?c[0]+(""===b?"":b+"\n ")+" "+a.join(",\n ")+" "+c[1]:c[0]+b+" "+a.join(", ")+" "+c[1]}function n(a){return G.isArray(a)}function o(a){return"boolean"==typeof a}function p(a){return null===a}function q(a){return null==a}function r(a){return"number"==typeof a}function s(a){return"string"==typeof a}function t(a){return"symbol"==typeof a}function u(a){return void 0===a}function v(a){return w(a)&&"[object RegExp]"===C(a)}function w(a){return"object"==typeof a&&a}function x(a){return w(a)&&"[object Date]"===C(a)}function y(a){return w(a)&&"[object Error]"===C(a)}function z(a){return"function"==typeof a}function A(a){return null===a||"boolean"==typeof a||"number"==typeof a||"string"==typeof a||"symbol"==typeof a||"undefined"==typeof a}function B(a){return a&&"object"==typeof a&&"function"==typeof a.copy&&"function"==typeof a.fill&&"function"==typeof a.binarySlice}function C(a){return Object.prototype.toString.call(a)}function D(a){return 10>a?"0"+a.toString(10):a.toString(10)}function E(){var a=new Date,b=[D(a.getHours()),D(a.getMinutes()),D(a.getSeconds())].join(":");return[a.getDate(),I[a.getMonth()],b].join(" ")}function F(a,b){return Object.prototype.hasOwnProperty.call(a,b)}var G=a("_shims"),H=/%[sdj%]/g;c.format=function(a){if(!s(a)){for(var b=[],c=0;c<arguments.length;c++)b.push(d(arguments[c]));return b.join(" ")}for(var c=1,e=arguments,f=e.length,g=String(a).replace(H,function(a){if("%%"===a)return"%";if(c>=f)return a;switch(a){case"%s":return String(e[c++]);case"%d":return Number(e[c++]);case"%j":try{return JSON.stringify(e[c++])}catch(b){return"[Circular]"}default:return a}}),h=e[c];f>c;h=e[++c])g+=p(h)||!w(h)?" "+h:" "+d(h);return g},c.inspect=d,d.colors={bold:[1,22],italic:[3,23],underline:[4,24],inverse:[7,27],white:[37,39],grey:[90,39],black:[30,39],blue:[34,39],cyan:[36,39],green:[32,39],magenta:[35,39],red:[31,39],yellow:[33,39]},d.styles={special:"cyan",number:"yellow","boolean":"yellow",undefined:"grey","null":"bold",string:"green",date:"magenta",regexp:"red"},c.isArray=n,c.isBoolean=o,c.isNull=p,c.isNullOrUndefined=q,c.isNumber=r,c.isString=s,c.isSymbol=t,c.isUndefined=u,c.isRegExp=v,c.isObject=w,c.isDate=x,c.isError=y,c.isFunction=z,c.isPrimitive=A,c.isBuffer=B;var I=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];c.log=function(){console.log("%s - %s",E(),c.format.apply(c,arguments))},c.inherits=function(a,b){a.super_=b,a.prototype=G.create(b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}})},c._extend=function(a,b){if(!b||!w(b))return a;for(var c=G.keys(b),d=c.length;d--;)a[c[d]]=b[c[d]];return a}},{_shims:8}],11:[function(a,b){b.exports=a("./lib/its.js")},{"./lib/its.js":12}],12:[function(a,b){var c=Array.prototype.slice,d=Object.prototype.toString,e=/%s/,f=function(a,b){for(var c=[],d=a.split(e),f=0,g=d.length;g>f;f++)c.push(d[f]),c.push(b[f]);return c.join("")},g=b.exports=function(a,b){if(a===!1)throw b&&"string"!=typeof b?b(arguments.length>3?f(arguments[2],c.call(arguments,3)):arguments[2]):new Error(arguments.length>2?f(b,c.call(arguments,2)):b);return a};g.type=function(a,b){if(a===!1)throw new TypeError(arguments.length>2?f(b,c.call(arguments,2)):b);return a},g.undefined=function(a){return g.type.apply(null,[void 0===a].concat(c.call(arguments,1)))},g.null=function(a){return g.type.apply(null,[null===a].concat(c.call(arguments,1)))},g.boolean=function(a){return g.type.apply(null,[a===!0||a===!1||"[object Boolean]"===d.call(a)].concat(c.call(arguments,1)))},g.array=function(a){return g.type.apply(null,["[object Array]"===d.call(a)].concat(c.call(arguments,1)))},g.object=function(a){return g.type.apply(null,[a===Object(a)].concat(c.call(arguments,1)))},function(){for(var a=[["args","Arguments"],["func","Function"],["string","String"],["number","Number"],["date","Date"],["regexp","RegExp"]],b=0,e=a.length;e>b;b++)!function(){var e=a[b];g[e[0]]=function(a){return g.type.apply(null,[d.call(a)==="[object "+e[1]+"]"].concat(c.call(arguments,1)))}}()}(),"function"!=typeof/./&&(g.func=function(a){return g.type.apply(null,["function"==typeof a].concat(c.call(arguments,1)))}),g.defined=function(a,b){if(void 0===a)throw new ReferenceError(arguments.length>2?f(b,c.call(arguments,2)):b);return a},g.range=function(a,b){if(a===!1)throw new RangeError(arguments.length>2?f(b,c.call(arguments,2)):b);return a}},{}]},{},[7]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment