bot18
Version:
A high-frequency cryptocurrency trading bot by Zenbot creator @carlos8f
1,286 lines (1,097 loc) • 32.2 kB
JavaScript
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.ltx = f()}})(function(){var define,module,exports;return (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<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var parse = require('./lib/parse')
var Parser = require('./lib/Parser')
var escape = require('./lib/escape')
var Element = require('./lib/Element')
var equal = require('./lib/equal')
var createElement = require('./lib/createElement')
var tag = require('./lib/tag')
var tagString = require('./lib/tagString')
var is = require('./lib/is')
var clone = require('./lib/clone')
var stringify = require('./lib/stringify')
exports = module.exports = function ltx () {
return tag.apply(null, arguments)
}
exports.Element = Element
exports.equal = equal.equal
exports.nameEqual = equal.name
exports.attrsEqual = equal.attrs
exports.childrenEqual = equal.children
exports.isNode = is.isNode
exports.isElement = is.isElement
exports.isText = is.isText
exports.clone = clone
exports.createElement = createElement
exports.escapeXML = escape.escapeXML
exports.unescapeXML = escape.unescapeXML
exports.escapeXMLText = escape.escapeXMLText
exports.unescapeXMLText = escape.unescapeXMLText
exports.Parser = Parser
exports.parse = parse
exports.tag = tag
exports.tagString = tagString
exports.stringify = stringify
},{"./lib/Element":2,"./lib/Parser":3,"./lib/clone":4,"./lib/createElement":5,"./lib/equal":6,"./lib/escape":7,"./lib/is":8,"./lib/parse":9,"./lib/stringify":11,"./lib/tag":12,"./lib/tagString":13}],2:[function(require,module,exports){
var escape = require('./escape')
var escapeXML = escape.escapeXML
var escapeXMLText = escape.escapeXMLText
var equality = require('./equal')
var equal = equality.equal
var nameEqual = equality.name
var attrsEqual = equality.attrs
var childrenEqual = equality.children
var clone = require('./clone')
/**
* Element
*
* Attributes are in the element.attrs object. Children is a list of
* either other Elements or Strings for text content.
**/
function Element (name, attrs) {
this.name = name
this.parent = null
this.children = []
this.attrs = {}
this.setAttrs(attrs)
}
/* Accessors */
/**
* if (element.is('message', 'jabber:client')) ...
**/
Element.prototype.is = function (name, xmlns) {
return (this.getName() === name) &&
(!xmlns || (this.getNS() === xmlns))
}
/* without prefix */
Element.prototype.getName = function () {
if (this.name.indexOf(':') >= 0) {
return this.name.substr(this.name.indexOf(':') + 1)
} else {
return this.name
}
}
/**
* retrieves the namespace of the current element, upwards recursively
**/
Element.prototype.getNS = function () {
if (this.name.indexOf(':') >= 0) {
var prefix = this.name.substr(0, this.name.indexOf(':'))
return this.findNS(prefix)
}
return this.findNS()
}
/**
* find the namespace to the given prefix, upwards recursively
**/
Element.prototype.findNS = function (prefix) {
if (!prefix) {
/* default namespace */
if (this.attrs.xmlns) {
return this.attrs.xmlns
} else if (this.parent) {
return this.parent.findNS()
}
} else {
/* prefixed namespace */
var attr = 'xmlns:' + prefix
if (this.attrs[attr]) {
return this.attrs[attr]
} else if (this.parent) {
return this.parent.findNS(prefix)
}
}
}
/**
* Recursiverly gets all xmlns defined, in the form of {url:prefix}
**/
Element.prototype.getXmlns = function () {
var namespaces = {}
if (this.parent) {
namespaces = this.parent.getXmlns()
}
for (var attr in this.attrs) {
var m = attr.match('xmlns:?(.*)')
if (this.attrs.hasOwnProperty(attr) && m) {
namespaces[this.attrs[attr]] = m[1]
}
}
return namespaces
}
Element.prototype.setAttrs = function (attrs) {
if (typeof attrs === 'string') {
this.attrs.xmlns = attrs
} else if (attrs) {
Object.keys(attrs).forEach(function (key) {
this.attrs[key] = attrs[key]
}, this)
}
}
/**
* xmlns can be null, returns the matching attribute.
**/
Element.prototype.getAttr = function (name, xmlns) {
if (!xmlns) {
return this.attrs[name]
}
var namespaces = this.getXmlns()
if (!namespaces[xmlns]) {
return null
}
return this.attrs[[namespaces[xmlns], name].join(':')]
}
/**
* xmlns can be null
**/
Element.prototype.getChild = function (name, xmlns) {
return this.getChildren(name, xmlns)[0]
}
/**
* xmlns can be null
**/
Element.prototype.getChildren = function (name, xmlns) {
var result = []
for (var i = 0; i < this.children.length; i++) {
var child = this.children[i]
if (child.getName &&
(child.getName() === name) &&
(!xmlns || (child.getNS() === xmlns))) {
result.push(child)
}
}
return result
}
/**
* xmlns and recursive can be null
**/
Element.prototype.getChildByAttr = function (attr, val, xmlns, recursive) {
return this.getChildrenByAttr(attr, val, xmlns, recursive)[0]
}
/**
* xmlns and recursive can be null
**/
Element.prototype.getChildrenByAttr = function (attr, val, xmlns, recursive) {
var result = []
for (var i = 0; i < this.children.length; i++) {
var child = this.children[i]
if (child.attrs &&
(child.attrs[attr] === val) &&
(!xmlns || (child.getNS() === xmlns))) {
result.push(child)
}
if (recursive && child.getChildrenByAttr) {
result.push(child.getChildrenByAttr(attr, val, xmlns, true))
}
}
if (recursive) {
result = [].concat.apply([], result)
}
return result
}
Element.prototype.getChildrenByFilter = function (filter, recursive) {
var result = []
for (var i = 0; i < this.children.length; i++) {
var child = this.children[i]
if (filter(child)) {
result.push(child)
}
if (recursive && child.getChildrenByFilter) {
result.push(child.getChildrenByFilter(filter, true))
}
}
if (recursive) {
result = [].concat.apply([], result)
}
return result
}
Element.prototype.getText = function () {
var text = ''
for (var i = 0; i < this.children.length; i++) {
var child = this.children[i]
if ((typeof child === 'string') || (typeof child === 'number')) {
text += child
}
}
return text
}
Element.prototype.getChildText = function (name, xmlns) {
var child = this.getChild(name, xmlns)
return child ? child.getText() : null
}
/**
* Return all direct descendents that are Elements.
* This differs from `getChildren` in that it will exclude text nodes,
* processing instructions, etc.
*/
Element.prototype.getChildElements = function () {
return this.getChildrenByFilter(function (child) {
return child instanceof Element
})
}
/* Builder */
/** returns uppermost parent */
Element.prototype.root = function () {
if (this.parent) {
return this.parent.root()
}
return this
}
Element.prototype.tree = Element.prototype.root
/** just parent or itself */
Element.prototype.up = function () {
if (this.parent) {
return this.parent
}
return this
}
/** create child node and return it */
Element.prototype.c = function (name, attrs) {
return this.cnode(new Element(name, attrs))
}
Element.prototype.cnode = function (child) {
this.children.push(child)
if (typeof child === 'object') {
child.parent = this
}
return child
}
/** add text node and return element */
Element.prototype.t = function (text) {
this.children.push(text)
return this
}
/* Manipulation */
/**
* Either:
* el.remove(childEl)
* el.remove('author', 'urn:...')
*/
Element.prototype.remove = function (el, xmlns) {
var filter
if (typeof el === 'string') {
/* 1st parameter is tag name */
filter = function (child) {
return !(child.is &&
child.is(el, xmlns))
}
} else {
/* 1st parameter is element */
filter = function (child) {
return child !== el
}
}
this.children = this.children.filter(filter)
return this
}
Element.prototype.clone = function () {
return clone(this)
}
Element.prototype.text = function (val) {
if (val && this.children.length === 1) {
this.children[0] = val
return this
}
return this.getText()
}
Element.prototype.attr = function (attr, val) {
if (typeof val !== 'undefined' || val === null) {
if (!this.attrs) {
this.attrs = {}
}
this.attrs[attr] = val
return this
}
return this.attrs[attr]
}
/* Serialization */
Element.prototype.toString = function () {
var s = ''
this.write(function (c) {
s += c
})
return s
}
Element.prototype.toJSON = function () {
return {
name: this.name,
attrs: this.attrs,
children: this.children.map(function (child) {
return child && child.toJSON ? child.toJSON() : child
})
}
}
Element.prototype._addChildren = function (writer) {
writer('>')
for (var i = 0; i < this.children.length; i++) {
var child = this.children[i]
/* Skip null/undefined */
if (child || (child === 0)) {
if (child.write) {
child.write(writer)
} else if (typeof child === 'string') {
writer(escapeXMLText(child))
} else if (child.toString) {
writer(escapeXMLText(child.toString(10)))
}
}
}
writer('</')
writer(this.name)
writer('>')
}
Element.prototype.write = function (writer) {
writer('<')
writer(this.name)
for (var k in this.attrs) {
var v = this.attrs[k]
if (v != null) { // === null || undefined
writer(' ')
writer(k)
writer('="')
if (typeof v !== 'string') {
v = v.toString()
}
writer(escapeXML(v))
writer('"')
}
}
if (this.children.length === 0) {
writer('/>')
} else {
this._addChildren(writer)
}
}
Element.prototype.nameEquals = function (el) {
return nameEqual(this, el)
}
Element.prototype.attrsEquals = function (el) {
return attrsEqual(this, el)
}
Element.prototype.childrenEquals = function (el) {
return childrenEqual(this, el)
}
Element.prototype.equals = function (el) {
return equal(this, el)
}
module.exports = Element
},{"./clone":4,"./equal":6,"./escape":7}],3:[function(require,module,exports){
var EventEmitter = require('events').EventEmitter
var inherits = require('inherits')
var Element = require('./Element')
var LtxParser = require('./parsers/ltx')
var Parser = function (options) {
EventEmitter.call(this)
var ParserInterface = this.Parser = (options && options.Parser) || this.DefaultParser
var ElementInterface = this.Element = (options && options.Element) || this.DefaultElement
this.parser = new ParserInterface()
var el
var self = this
this.parser.on('startElement', function (name, attrs) {
var child = new ElementInterface(name, attrs)
if (!el) {
el = child
} else {
el = el.cnode(child)
}
})
this.parser.on('endElement', function (name) {
if (!el) {
/* Err */
} else if (name === el.name) {
if (el.parent) {
el = el.parent
} else if (!self.tree) {
self.tree = el
el = undefined
}
}
})
this.parser.on('text', function (str) {
if (el) {
el.t(str)
}
})
this.parser.on('error', function (e) {
self.error = e
self.emit('error', e)
})
}
inherits(Parser, EventEmitter)
Parser.prototype.DefaultParser = LtxParser
Parser.prototype.DefaultElement = Element
Parser.prototype.write = function (data) {
this.parser.write(data)
}
Parser.prototype.end = function (data) {
this.parser.end(data)
if (!this.error) {
if (this.tree) {
this.emit('tree', this.tree)
} else {
this.emit('error', new Error('Incomplete document'))
}
}
}
module.exports = Parser
},{"./Element":2,"./parsers/ltx":10,"events":14,"inherits":15}],4:[function(require,module,exports){
module.exports = function clone (el) {
var clone = new el.constructor(el.name, el.attrs)
for (var i = 0; i < el.children.length; i++) {
var child = el.children[i]
clone.cnode(child.clone ? child.clone() : child)
}
return clone
}
},{}],5:[function(require,module,exports){
var Element = require('./Element')
/**
* JSX compatible API, use this function as pragma
* https://facebook.github.io/jsx/
*
* @param {string} name name of the element
* @param {object} attrs object of attribute key/value pairs
* @return {Element} Element
*/
module.exports = function createElement (name, attrs /*, child1, child2, ... */) {
var el = new Element(name, attrs)
for (var i = 2; i < arguments.length; i++) {
var child = arguments[i]
if (child) el.cnode(child)
}
return el
}
},{"./Element":2}],6:[function(require,module,exports){
function nameEqual (a, b) {
return a.name === b.name
}
function attrsEqual (a, b) {
var attrs = a.attrs
var keys = Object.keys(attrs)
var length = keys.length
if (length !== Object.keys(b.attrs).length) return false
for (var i = 0, l = length; i < l; i++) {
var key = keys[i]
var value = attrs[key]
if (value == null || b.attrs[key] == null) { // === null || undefined
if (value !== b.attrs[key]) return false
} else if (value.toString() !== b.attrs[key].toString()) {
return false
}
}
return true
}
function childrenEqual (a, b) {
var children = a.children
var length = children.length
if (length !== b.children.length) return false
for (var i = 0, l = length; i < l; i++) {
var child = children[i]
if (typeof child === 'string') {
if (child !== b.children[i]) return false
} else {
if (!child.equals(b.children[i])) return false
}
}
return true
}
function equal (a, b) {
if (!nameEqual(a, b)) return false
if (!attrsEqual(a, b)) return false
if (!childrenEqual(a, b)) return false
return true
}
module.exports.name = nameEqual
module.exports.attrs = attrsEqual
module.exports.children = childrenEqual
module.exports.equal = equal
},{}],7:[function(require,module,exports){
var escapeXMLTable = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
'\'': '''
}
function escapeXMLReplace (match) {
return escapeXMLTable[match]
}
var unescapeXMLTable = {
'&': '&',
'&': '&',
'<': '<',
'<': '<',
'>': '>',
'>': '>',
'"': '"',
'"': '"',
''': "'",
''': "'"
}
function unescapeXMLReplace (match) {
return unescapeXMLTable[match]
}
exports.escapeXML = function escapeXML (s) {
return s.replace(/&|<|>|"|'/g, escapeXMLReplace)
}
exports.unescapeXML = function unescapeXML (s) {
return s.replace(/&(amp|#38|lt|#60|gt|#62|quot|#34|apos|#39);/g, unescapeXMLReplace)
}
exports.escapeXMLText = function escapeXMLText (s) {
return s.replace(/&|<|>/g, escapeXMLReplace)
}
exports.unescapeXMLText = function unescapeXMLText (s) {
return s.replace(/&(amp|#38|lt|#60|gt|#62);/g, unescapeXMLReplace)
}
},{}],8:[function(require,module,exports){
var Element = require('./Element')
module.exports.isNode = function is (el) {
return el instanceof Element || typeof el === 'string'
}
module.exports.isElement = function isElement (el) {
return el instanceof Element
}
module.exports.isText = function isText (el) {
return typeof el === 'string'
}
},{"./Element":2}],9:[function(require,module,exports){
var Parser = require('./Parser')
module.exports = function parse (data, options) {
var p
if (typeof options === 'function') {
p = new options() // eslint-disable-line
} else {
p = new Parser(options)
}
var result = null
var error = null
p.on('tree', function (tree) {
result = tree
})
p.on('error', function (e) {
error = e
})
p.write(data)
p.end()
if (error) {
throw error
} else {
return result
}
}
},{"./Parser":3}],10:[function(require,module,exports){
var inherits = require('inherits')
var EventEmitter = require('events').EventEmitter
var unescapeXML = require('../escape').unescapeXML
var STATE_TEXT = 0
var STATE_IGNORE_COMMENT = 1
var STATE_IGNORE_INSTRUCTION = 2
var STATE_TAG_NAME = 3
var STATE_TAG = 4
var STATE_ATTR_NAME = 5
var STATE_ATTR_EQ = 6
var STATE_ATTR_QUOT = 7
var STATE_ATTR_VALUE = 8
var STATE_CDATA = 9
var SaxLtx = module.exports = function SaxLtx () {
EventEmitter.call(this)
var state = STATE_TEXT
var remainder
var tagName
var attrs
var endTag
var selfClosing
var attrQuote
var recordStart = 0
var attrName
this._handleTagOpening = function (endTag, tagName, attrs) {
if (!endTag) {
this.emit('startElement', tagName, attrs)
if (selfClosing) {
this.emit('endElement', tagName)
}
} else {
this.emit('endElement', tagName)
}
}
this.write = function (data) {
if (typeof data !== 'string') {
data = data.toString()
}
var pos = 0
/* Anything from previous write()? */
if (remainder) {
data = remainder + data
pos += remainder.length
remainder = null
}
function endRecording () {
if (typeof recordStart === 'number') {
var recorded = data.slice(recordStart, pos)
recordStart = undefined
return recorded
}
}
for (; pos < data.length; pos++) {
var c = data.charCodeAt(pos)
switch (state) {
case STATE_TEXT:
if (c === 60 /* < */) {
var text = endRecording()
if (text) {
this.emit('text', unescapeXML(text))
}
state = STATE_TAG_NAME
recordStart = pos + 1
attrs = {}
}
break
case STATE_CDATA:
if (c === 93 /* ] */ && data.substr(pos + 1, 2) === ']>') {
var cData = endRecording()
if (cData) {
this.emit('text', cData)
}
state = STATE_IGNORE_COMMENT
}
break
case STATE_TAG_NAME:
if (c === 47 /* / */ && recordStart === pos) {
recordStart = pos + 1
endTag = true
} else if (c === 33 /* ! */) {
if (data.substr(pos + 1, 7) === '[CDATA[') {
recordStart = pos + 8
state = STATE_CDATA
} else {
recordStart = undefined
state = STATE_IGNORE_COMMENT
}
} else if (c === 63 /* ? */) {
recordStart = undefined
state = STATE_IGNORE_INSTRUCTION
} else if (c <= 32 || c === 47 /* / */ || c === 62 /* > */) {
tagName = endRecording()
pos--
state = STATE_TAG
}
break
case STATE_IGNORE_COMMENT:
if (c === 62 /* > */) {
var prevFirst = data.charCodeAt(pos - 1)
var prevSecond = data.charCodeAt(pos - 2)
if ((prevFirst === 45 /* - */ && prevSecond === 45 /* - */) ||
(prevFirst === 93 /* ] */ && prevSecond === 93 /* ] */)) {
state = STATE_TEXT
}
}
break
case STATE_IGNORE_INSTRUCTION:
if (c === 62 /* > */) {
var prev = data.charCodeAt(pos - 1)
if (prev === 63 /* ? */) {
state = STATE_TEXT
}
}
break
case STATE_TAG:
if (c === 62 /* > */) {
this._handleTagOpening(endTag, tagName, attrs)
tagName = undefined
attrs = undefined
endTag = undefined
selfClosing = undefined
state = STATE_TEXT
recordStart = pos + 1
} else if (c === 47 /* / */) {
selfClosing = true
} else if (c > 32) {
recordStart = pos
state = STATE_ATTR_NAME
}
break
case STATE_ATTR_NAME:
if (c <= 32 || c === 61 /* = */) {
attrName = endRecording()
pos--
state = STATE_ATTR_EQ
}
break
case STATE_ATTR_EQ:
if (c === 61 /* = */) {
state = STATE_ATTR_QUOT
}
break
case STATE_ATTR_QUOT:
if (c === 34 /* " */ || c === 39 /* ' */) {
attrQuote = c
state = STATE_ATTR_VALUE
recordStart = pos + 1
}
break
case STATE_ATTR_VALUE:
if (c === attrQuote) {
var value = unescapeXML(endRecording())
attrs[attrName] = value
attrName = undefined
state = STATE_TAG
}
break
}
}
if (typeof recordStart === 'number' &&
recordStart <= data.length) {
remainder = data.slice(recordStart)
recordStart = 0
}
}
/*
var origEmit = this.emit
this.emit = function() {
console.log('ltx', arguments)
origEmit.apply(this, arguments)
}
*/
}
inherits(SaxLtx, EventEmitter)
SaxLtx.prototype.end = function (data) {
if (data) {
this.write(data)
}
/* Uh, yeah */
this.write = function () {}
}
},{"../escape":7,"events":14,"inherits":15}],11:[function(require,module,exports){
function stringify (el, indent, level) {
if (typeof indent === 'number') indent = ' '.repeat(indent)
if (!level) level = 1
var s = ''
s += '<' + el.name
Object.keys(el.attrs).forEach(function (k) {
s += ' ' + k + '=' + '"' + el.attrs[k] + '"'
})
if (el.children.length) {
s += '>'
el.children.forEach(function (child, i) {
if (indent) s += '\n' + indent.repeat(level)
if (typeof child === 'string') {
s += child
} else {
s += stringify(child, indent, level + 1)
}
})
if (indent) s += '\n' + indent.repeat(level - 1)
s += '</' + el.name + '>'
} else {
s += '/>'
}
return s
}
module.exports = stringify
},{}],12:[function(require,module,exports){
var tagString = require('./tagString')
var parse = require('./parse')
module.exports = function tag (/* [literals], ...substitutions */) {
return parse(tagString.apply(null, arguments))
}
},{"./parse":9,"./tagString":13}],13:[function(require,module,exports){
var escape = require('./escape').escapeXML
module.exports = function tagString (/* [literals], ...substitutions */) {
var literals = arguments[0]
var str = ''
for (var i = 1; i < arguments.length; i++) {
str += literals[i - 1]
str += escape(arguments[i])
}
str += literals[literals.length - 1]
return str
}
},{"./escape":7}],14:[function(require,module,exports){
// Copyright Joyent, Inc. and other Node contributors.
//
// 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.
function EventEmitter() {
this._events = this._events || {};
this._maxListeners = this._maxListeners || undefined;
}
module.exports = EventEmitter;
// Backwards-compat with node 0.10.x
EventEmitter.EventEmitter = EventEmitter;
EventEmitter.prototype._events = undefined;
EventEmitter.prototype._maxListeners = undefined;
// By default EventEmitters will print a warning if more than 10 listeners are
// added to it. This is a useful default which helps finding memory leaks.
EventEmitter.defaultMaxListeners = 10;
// Obviously not all Emitters should be limited to 10. This function allows
// that to be increased. Set to zero for unlimited.
EventEmitter.prototype.setMaxListeners = function(n) {
if (!isNumber(n) || n < 0 || isNaN(n))
throw TypeError('n must be a positive number');
this._maxListeners = n;
return this;
};
EventEmitter.prototype.emit = function(type) {
var er, handler, len, args, i, listeners;
if (!this._events)
this._events = {};
// If there is no 'error' event listener then throw.
if (type === 'error') {
if (!this._events.error ||
(isObject(this._events.error) && !this._events.error.length)) {
er = arguments[1];
if (er instanceof Error) {
throw er; // Unhandled 'error' event
} else {
// At least give some kind of context to the user
var err = new Error('Uncaught, unspecified "error" event. (' + er + ')');
err.context = er;
throw err;
}
}
}
handler = this._events[type];
if (isUndefined(handler))
return false;
if (isFunction(handler)) {
switch (arguments.length) {
// fast cases
case 1:
handler.call(this);
break;
case 2:
handler.call(this, arguments[1]);
break;
case 3:
handler.call(this, arguments[1], arguments[2]);
break;
// slower
default:
args = Array.prototype.slice.call(arguments, 1);
handler.apply(this, args);
}
} else if (isObject(handler)) {
args = Array.prototype.slice.call(arguments, 1);
listeners = handler.slice();
len = listeners.length;
for (i = 0; i < len; i++)
listeners[i].apply(this, args);
}
return true;
};
EventEmitter.prototype.addListener = function(type, listener) {
var m;
if (!isFunction(listener))
throw TypeError('listener must be a function');
if (!this._events)
this._events = {};
// To avoid recursion in the case that type === "newListener"! Before
// adding it to the listeners, first emit "newListener".
if (this._events.newListener)
this.emit('newListener', type,
isFunction(listener.listener) ?
listener.listener : listener);
if (!this._events[type])
// Optimize the case of one listener. Don't need the extra array object.
this._events[type] = listener;
else if (isObject(this._events[type]))
// If we've already got an array, just append.
this._events[type].push(listener);
else
// Adding the second element, need to change to array.
this._events[type] = [this._events[type], listener];
// Check for listener leak
if (isObject(this._events[type]) && !this._events[type].warned) {
if (!isUndefined(this._maxListeners)) {
m = this._maxListeners;
} else {
m = EventEmitter.defaultMaxListeners;
}
if (m && m > 0 && this._events[type].length > m) {
this._events[type].warned = true;
console.error('(node) warning: possible EventEmitter memory ' +
'leak detected. %d listeners added. ' +
'Use emitter.setMaxListeners() to increase limit.',
this._events[type].length);
if (typeof console.trace === 'function') {
// not supported in IE 10
console.trace();
}
}
}
return this;
};
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
EventEmitter.prototype.once = function(type, listener) {
if (!isFunction(listener))
throw TypeError('listener must be a function');
var fired = false;
function g() {
this.removeListener(type, g);
if (!fired) {
fired = true;
listener.apply(this, arguments);
}
}
g.listener = listener;
this.on(type, g);
return this;
};
// emits a 'removeListener' event iff the listener was removed
EventEmitter.prototype.removeListener = function(type, listener) {
var list, position, length, i;
if (!isFunction(listener))
throw TypeError('listener must be a function');
if (!this._events || !this._events[type])
return this;
list = this._events[type];
length = list.length;
position = -1;
if (list === listener ||
(isFunction(list.listener) && list.listener === listener)) {
delete this._events[type];
if (this._events.removeListener)
this.emit('removeListener', type, listener);
} else if (isObject(list)) {
for (i = length; i-- > 0;) {
if (list[i] === listener ||
(list[i].listener && list[i].listener === listener)) {
position = i;
break;
}
}
if (position < 0)
return this;
if (list.length === 1) {
list.length = 0;
delete this._events[type];
} else {
list.splice(position, 1);
}
if (this._events.removeListener)
this.emit('removeListener', type, listener);
}
return this;
};
EventEmitter.prototype.removeAllListeners = function(type) {
var key, listeners;
if (!this._events)
return this;
// not listening for removeListener, no need to emit
if (!this._events.removeListener) {
if (arguments.length === 0)
this._events = {};
else if (this._events[type])
delete this._events[type];
return this;
}
// emit removeListener for all listeners on all events
if (arguments.length === 0) {
for (key in this._events) {
if (key === 'removeListener') continue;
this.removeAllListeners(key);
}
this.removeAllListeners('removeListener');
this._events = {};
return this;
}
listeners = this._events[type];
if (isFunction(listeners)) {
this.removeListener(type, listeners);
} else if (listeners) {
// LIFO order
while (listeners.length)
this.removeListener(type, listeners[listeners.length - 1]);
}
delete this._events[type];
return this;
};
EventEmitter.prototype.listeners = function(type) {
var ret;
if (!this._events || !this._events[type])
ret = [];
else if (isFunction(this._events[type]))
ret = [this._events[type]];
else
ret = this._events[type].slice();
return ret;
};
EventEmitter.prototype.listenerCount = function(type) {
if (this._events) {
var evlistener = this._events[type];
if (isFunction(evlistener))
return 1;
else if (evlistener)
return evlistener.length;
}
return 0;
};
EventEmitter.listenerCount = function(emitter, type) {
return emitter.listenerCount(type);
};
function isFunction(arg) {
return typeof arg === 'function';
}
function isNumber(arg) {
return typeof arg === 'number';
}
function isObject(arg) {
return typeof arg === 'object' && arg !== null;
}
function isUndefined(arg) {
return arg === void 0;
}
},{}],15:[function(require,module,exports){
if (typeof Object.create === 'function') {
// implementation from standard node.js 'util' module
module.exports = function inherits(ctor, superCtor) {
ctor.super_ = superCtor
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
};
} else {
// old school shim for old browsers
module.exports = function inherits(ctor, superCtor) {
ctor.super_ = superCtor
var TempCtor = function () {}
TempCtor.prototype = superCtor.prototype
ctor.prototype = new TempCtor()
ctor.prototype.constructor = ctor
}
}
},{}]},{},[1])(1)
});