(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.QB = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i = Object} oc `Object` by default, * allows to inject custom object constructor for tests * @returns {Readonly} * * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze */ function freeze(object, oc) { if (oc === undefined) { oc = Object } return oc && typeof oc.freeze === 'function' ? oc.freeze(object) : object } /** * Since we can not rely on `Object.assign` we provide a simplified version * that is sufficient for our needs. * * @param {Object} target * @param {Object | null | undefined} source * * @returns {Object} target * @throws TypeError if target is not an object * * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign * @see https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.assign */ function assign(target, source) { if (target === null || typeof target !== 'object') { throw new TypeError('target is not an object') } for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key] } } return target } /** * All mime types that are allowed as input to `DOMParser.parseFromString` * * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString#Argument02 MDN * @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#domparsersupportedtype WHATWG HTML Spec * @see DOMParser.prototype.parseFromString */ var MIME_TYPE = freeze({ /** * `text/html`, the only mime type that triggers treating an XML document as HTML. * * @see DOMParser.SupportedType.isHTML * @see https://www.iana.org/assignments/media-types/text/html IANA MimeType registration * @see https://en.wikipedia.org/wiki/HTML Wikipedia * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString MDN * @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring WHATWG HTML Spec */ HTML: 'text/html', /** * Helper method to check a mime type if it indicates an HTML document * * @param {string} [value] * @returns {boolean} * * @see https://www.iana.org/assignments/media-types/text/html IANA MimeType registration * @see https://en.wikipedia.org/wiki/HTML Wikipedia * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString MDN * @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring */ isHTML: function (value) { return value === MIME_TYPE.HTML }, /** * `application/xml`, the standard mime type for XML documents. * * @see https://www.iana.org/assignments/media-types/application/xml IANA MimeType registration * @see https://tools.ietf.org/html/rfc7303#section-9.1 RFC 7303 * @see https://en.wikipedia.org/wiki/XML_and_MIME Wikipedia */ XML_APPLICATION: 'application/xml', /** * `text/html`, an alias for `application/xml`. * * @see https://tools.ietf.org/html/rfc7303#section-9.2 RFC 7303 * @see https://www.iana.org/assignments/media-types/text/xml IANA MimeType registration * @see https://en.wikipedia.org/wiki/XML_and_MIME Wikipedia */ XML_TEXT: 'text/xml', /** * `application/xhtml+xml`, indicates an XML document that has the default HTML namespace, * but is parsed as an XML document. * * @see https://www.iana.org/assignments/media-types/application/xhtml+xml IANA MimeType registration * @see https://dom.spec.whatwg.org/#dom-domimplementation-createdocument WHATWG DOM Spec * @see https://en.wikipedia.org/wiki/XHTML Wikipedia */ XML_XHTML_APPLICATION: 'application/xhtml+xml', /** * `image/svg+xml`, * * @see https://www.iana.org/assignments/media-types/image/svg+xml IANA MimeType registration * @see https://www.w3.org/TR/SVG11/ W3C SVG 1.1 * @see https://en.wikipedia.org/wiki/Scalable_Vector_Graphics Wikipedia */ XML_SVG_IMAGE: 'image/svg+xml', }) /** * Namespaces that are used in this code base. * * @see http://www.w3.org/TR/REC-xml-names */ var NAMESPACE = freeze({ /** * The XHTML namespace. * * @see http://www.w3.org/1999/xhtml */ HTML: 'http://www.w3.org/1999/xhtml', /** * Checks if `uri` equals `NAMESPACE.HTML`. * * @param {string} [uri] * * @see NAMESPACE.HTML */ isHTML: function (uri) { return uri === NAMESPACE.HTML }, /** * The SVG namespace. * * @see http://www.w3.org/2000/svg */ SVG: 'http://www.w3.org/2000/svg', /** * The `xml:` namespace. * * @see http://www.w3.org/XML/1998/namespace */ XML: 'http://www.w3.org/XML/1998/namespace', /** * The `xmlns:` namespace * * @see https://www.w3.org/2000/xmlns/ */ XMLNS: 'http://www.w3.org/2000/xmlns/', }) exports.assign = assign; exports.freeze = freeze; exports.MIME_TYPE = MIME_TYPE; exports.NAMESPACE = NAMESPACE; },{}],2:[function(require,module,exports){ var conventions = require("./conventions"); var dom = require('./dom') var entities = require('./entities'); var sax = require('./sax'); var DOMImplementation = dom.DOMImplementation; var NAMESPACE = conventions.NAMESPACE; var ParseError = sax.ParseError; var XMLReader = sax.XMLReader; /** * Normalizes line ending according to https://www.w3.org/TR/xml11/#sec-line-ends: * * > XML parsed entities are often stored in computer files which, * > for editing convenience, are organized into lines. * > These lines are typically separated by some combination * > of the characters CARRIAGE RETURN (#xD) and LINE FEED (#xA). * > * > To simplify the tasks of applications, the XML processor must behave * > as if it normalized all line breaks in external parsed entities (including the document entity) * > on input, before parsing, by translating all of the following to a single #xA character: * > * > 1. the two-character sequence #xD #xA * > 2. the two-character sequence #xD #x85 * > 3. the single character #x85 * > 4. the single character #x2028 * > 5. any #xD character that is not immediately followed by #xA or #x85. * * @param {string} input * @returns {string} */ function normalizeLineEndings(input) { return input .replace(/\r[\n\u0085]/g, '\n') .replace(/[\r\u0085\u2028]/g, '\n') } /** * @typedef Locator * @property {number} [columnNumber] * @property {number} [lineNumber] */ /** * @typedef DOMParserOptions * @property {DOMHandler} [domBuilder] * @property {Function} [errorHandler] * @property {(string) => string} [normalizeLineEndings] used to replace line endings before parsing * defaults to `normalizeLineEndings` * @property {Locator} [locator] * @property {Record} [xmlns] * * @see normalizeLineEndings */ /** * The DOMParser interface provides the ability to parse XML or HTML source code * from a string into a DOM `Document`. * * _xmldom is different from the spec in that it allows an `options` parameter, * to override the default behavior._ * * @param {DOMParserOptions} [options] * @constructor * * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser * @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-parsing-and-serialization */ function DOMParser(options){ this.options = options ||{locator:{}}; } DOMParser.prototype.parseFromString = function(source,mimeType){ var options = this.options; var sax = new XMLReader(); var domBuilder = options.domBuilder || new DOMHandler();//contentHandler and LexicalHandler var errorHandler = options.errorHandler; var locator = options.locator; var defaultNSMap = options.xmlns||{}; var isHTML = /\/x?html?$/.test(mimeType);//mimeType.toLowerCase().indexOf('html') > -1; var entityMap = isHTML ? entities.HTML_ENTITIES : entities.XML_ENTITIES; if(locator){ domBuilder.setDocumentLocator(locator) } sax.errorHandler = buildErrorHandler(errorHandler,domBuilder,locator); sax.domBuilder = options.domBuilder || domBuilder; if(isHTML){ defaultNSMap[''] = NAMESPACE.HTML; } defaultNSMap.xml = defaultNSMap.xml || NAMESPACE.XML; var normalize = options.normalizeLineEndings || normalizeLineEndings; if (source && typeof source === 'string') { sax.parse( normalize(source), defaultNSMap, entityMap ) } else { sax.errorHandler.error('invalid doc source') } return domBuilder.doc; } function buildErrorHandler(errorImpl,domBuilder,locator){ if(!errorImpl){ if(domBuilder instanceof DOMHandler){ return domBuilder; } errorImpl = domBuilder ; } var errorHandler = {} var isCallback = errorImpl instanceof Function; locator = locator||{} function build(key){ var fn = errorImpl[key]; if(!fn && isCallback){ fn = errorImpl.length == 2?function(msg){errorImpl(key,msg)}:errorImpl; } errorHandler[key] = fn && function(msg){ fn('[xmldom '+key+']\t'+msg+_locator(locator)); }||function(){}; } build('warning'); build('error'); build('fatalError'); return errorHandler; } //console.log('#\n\n\n\n\n\n\n####') /** * +ContentHandler+ErrorHandler * +LexicalHandler+EntityResolver2 * -DeclHandler-DTDHandler * * DefaultHandler:EntityResolver, DTDHandler, ContentHandler, ErrorHandler * DefaultHandler2:DefaultHandler,LexicalHandler, DeclHandler, EntityResolver2 * @link http://www.saxproject.org/apidoc/org/xml/sax/helpers/DefaultHandler.html */ function DOMHandler() { this.cdata = false; } function position(locator,node){ node.lineNumber = locator.lineNumber; node.columnNumber = locator.columnNumber; } /** * @see org.xml.sax.ContentHandler#startDocument * @link http://www.saxproject.org/apidoc/org/xml/sax/ContentHandler.html */ DOMHandler.prototype = { startDocument : function() { this.doc = new DOMImplementation().createDocument(null, null, null); if (this.locator) { this.doc.documentURI = this.locator.systemId; } }, startElement:function(namespaceURI, localName, qName, attrs) { var doc = this.doc; var el = doc.createElementNS(namespaceURI, qName||localName); var len = attrs.length; appendElement(this, el); this.currentElement = el; this.locator && position(this.locator,el) for (var i = 0 ; i < len; i++) { var namespaceURI = attrs.getURI(i); var value = attrs.getValue(i); var qName = attrs.getQName(i); var attr = doc.createAttributeNS(namespaceURI, qName); this.locator &&position(attrs.getLocator(i),attr); attr.value = attr.nodeValue = value; el.setAttributeNode(attr) } }, endElement:function(namespaceURI, localName, qName) { var current = this.currentElement var tagName = current.tagName; this.currentElement = current.parentNode; }, startPrefixMapping:function(prefix, uri) { }, endPrefixMapping:function(prefix) { }, processingInstruction:function(target, data) { var ins = this.doc.createProcessingInstruction(target, data); this.locator && position(this.locator,ins) appendElement(this, ins); }, ignorableWhitespace:function(ch, start, length) { }, characters:function(chars, start, length) { chars = _toString.apply(this,arguments) //console.log(chars) if(chars){ if (this.cdata) { var charNode = this.doc.createCDATASection(chars); } else { var charNode = this.doc.createTextNode(chars); } if(this.currentElement){ this.currentElement.appendChild(charNode); }else if(/^\s*$/.test(chars)){ this.doc.appendChild(charNode); //process xml } this.locator && position(this.locator,charNode) } }, skippedEntity:function(name) { }, endDocument:function() { this.doc.normalize(); }, setDocumentLocator:function (locator) { if(this.locator = locator){// && !('lineNumber' in locator)){ locator.lineNumber = 0; } }, //LexicalHandler comment:function(chars, start, length) { chars = _toString.apply(this,arguments) var comm = this.doc.createComment(chars); this.locator && position(this.locator,comm) appendElement(this, comm); }, startCDATA:function() { //used in characters() methods this.cdata = true; }, endCDATA:function() { this.cdata = false; }, startDTD:function(name, publicId, systemId) { var impl = this.doc.implementation; if (impl && impl.createDocumentType) { var dt = impl.createDocumentType(name, publicId, systemId); this.locator && position(this.locator,dt) appendElement(this, dt); this.doc.doctype = dt; } }, /** * @see org.xml.sax.ErrorHandler * @link http://www.saxproject.org/apidoc/org/xml/sax/ErrorHandler.html */ warning:function(error) { console.warn('[xmldom warning]\t'+error,_locator(this.locator)); }, error:function(error) { console.error('[xmldom error]\t'+error,_locator(this.locator)); }, fatalError:function(error) { throw new ParseError(error, this.locator); } } function _locator(l){ if(l){ return '\n@'+(l.systemId ||'')+'#[line:'+l.lineNumber+',col:'+l.columnNumber+']' } } function _toString(chars,start,length){ if(typeof chars == 'string'){ return chars.substr(start,length) }else{//java sax connect width xmldom on rhino(what about: "? && !(chars instanceof String)") if(chars.length >= start+length || start){ return new java.lang.String(chars,start,length)+''; } return chars; } } /* * @link http://www.saxproject.org/apidoc/org/xml/sax/ext/LexicalHandler.html * used method of org.xml.sax.ext.LexicalHandler: * #comment(chars, start, length) * #startCDATA() * #endCDATA() * #startDTD(name, publicId, systemId) * * * IGNORED method of org.xml.sax.ext.LexicalHandler: * #endDTD() * #startEntity(name) * #endEntity(name) * * * @link http://www.saxproject.org/apidoc/org/xml/sax/ext/DeclHandler.html * IGNORED method of org.xml.sax.ext.DeclHandler * #attributeDecl(eName, aName, type, mode, value) * #elementDecl(name, model) * #externalEntityDecl(name, publicId, systemId) * #internalEntityDecl(name, value) * @link http://www.saxproject.org/apidoc/org/xml/sax/ext/EntityResolver2.html * IGNORED method of org.xml.sax.EntityResolver2 * #resolveEntity(String name,String publicId,String baseURI,String systemId) * #resolveEntity(publicId, systemId) * #getExternalSubset(name, baseURI) * @link http://www.saxproject.org/apidoc/org/xml/sax/DTDHandler.html * IGNORED method of org.xml.sax.DTDHandler * #notationDecl(name, publicId, systemId) {}; * #unparsedEntityDecl(name, publicId, systemId, notationName) {}; */ "endDTD,startEntity,endEntity,attributeDecl,elementDecl,externalEntityDecl,internalEntityDecl,resolveEntity,getExternalSubset,notationDecl,unparsedEntityDecl".replace(/\w+/g,function(key){ DOMHandler.prototype[key] = function(){return null} }) /* Private static helpers treated below as private instance methods, so don't need to add these to the public API; we might use a Relator to also get rid of non-standard public properties */ function appendElement (hander,node) { if (!hander.currentElement) { hander.doc.appendChild(node); } else { hander.currentElement.appendChild(node); } }//appendChild and setAttributeNS are preformance key exports.__DOMHandler = DOMHandler; exports.normalizeLineEndings = normalizeLineEndings; exports.DOMParser = DOMParser; },{"./conventions":1,"./dom":3,"./entities":4,"./sax":6}],3:[function(require,module,exports){ var conventions = require("./conventions"); var NAMESPACE = conventions.NAMESPACE; /** * A prerequisite for `[].filter`, to drop elements that are empty * @param {string} input * @returns {boolean} */ function notEmptyString (input) { return input !== '' } /** * @see https://infra.spec.whatwg.org/#split-on-ascii-whitespace * @see https://infra.spec.whatwg.org/#ascii-whitespace * * @param {string} input * @returns {string[]} (can be empty) */ function splitOnASCIIWhitespace(input) { // U+0009 TAB, U+000A LF, U+000C FF, U+000D CR, U+0020 SPACE return input ? input.split(/[\t\n\f\r ]+/).filter(notEmptyString) : [] } /** * Adds element as a key to current if it is not already present. * * @param {Record} current * @param {string} element * @returns {Record} */ function orderedSetReducer (current, element) { if (!current.hasOwnProperty(element)) { current[element] = true; } return current; } /** * @see https://infra.spec.whatwg.org/#ordered-set * @param {string} input * @returns {string[]} */ function toOrderedSet(input) { if (!input) return []; var list = splitOnASCIIWhitespace(input); return Object.keys(list.reduce(orderedSetReducer, {})) } /** * Uses `list.indexOf` to implement something like `Array.prototype.includes`, * which we can not rely on being available. * * @param {any[]} list * @returns {function(any): boolean} */ function arrayIncludes (list) { return function(element) { return list && list.indexOf(element) !== -1; } } function copy(src,dest){ for(var p in src){ dest[p] = src[p]; } } /** ^\w+\.prototype\.([_\w]+)\s*=\s*((?:.*\{\s*?[\r\n][\s\S]*?^})|\S.*?(?=[;\r\n]));? ^\w+\.prototype\.([_\w]+)\s*=\s*(\S.*?(?=[;\r\n]));? */ function _extends(Class,Super){ var pt = Class.prototype; if(!(pt instanceof Super)){ function t(){}; t.prototype = Super.prototype; t = new t(); copy(pt,t); Class.prototype = pt = t; } if(pt.constructor != Class){ if(typeof Class != 'function'){ console.error("unknown Class:"+Class) } pt.constructor = Class } } // Node Types var NodeType = {} var ELEMENT_NODE = NodeType.ELEMENT_NODE = 1; var ATTRIBUTE_NODE = NodeType.ATTRIBUTE_NODE = 2; var TEXT_NODE = NodeType.TEXT_NODE = 3; var CDATA_SECTION_NODE = NodeType.CDATA_SECTION_NODE = 4; var ENTITY_REFERENCE_NODE = NodeType.ENTITY_REFERENCE_NODE = 5; var ENTITY_NODE = NodeType.ENTITY_NODE = 6; var PROCESSING_INSTRUCTION_NODE = NodeType.PROCESSING_INSTRUCTION_NODE = 7; var COMMENT_NODE = NodeType.COMMENT_NODE = 8; var DOCUMENT_NODE = NodeType.DOCUMENT_NODE = 9; var DOCUMENT_TYPE_NODE = NodeType.DOCUMENT_TYPE_NODE = 10; var DOCUMENT_FRAGMENT_NODE = NodeType.DOCUMENT_FRAGMENT_NODE = 11; var NOTATION_NODE = NodeType.NOTATION_NODE = 12; // ExceptionCode var ExceptionCode = {} var ExceptionMessage = {}; var INDEX_SIZE_ERR = ExceptionCode.INDEX_SIZE_ERR = ((ExceptionMessage[1]="Index size error"),1); var DOMSTRING_SIZE_ERR = ExceptionCode.DOMSTRING_SIZE_ERR = ((ExceptionMessage[2]="DOMString size error"),2); var HIERARCHY_REQUEST_ERR = ExceptionCode.HIERARCHY_REQUEST_ERR = ((ExceptionMessage[3]="Hierarchy request error"),3); var WRONG_DOCUMENT_ERR = ExceptionCode.WRONG_DOCUMENT_ERR = ((ExceptionMessage[4]="Wrong document"),4); var INVALID_CHARACTER_ERR = ExceptionCode.INVALID_CHARACTER_ERR = ((ExceptionMessage[5]="Invalid character"),5); var NO_DATA_ALLOWED_ERR = ExceptionCode.NO_DATA_ALLOWED_ERR = ((ExceptionMessage[6]="No data allowed"),6); var NO_MODIFICATION_ALLOWED_ERR = ExceptionCode.NO_MODIFICATION_ALLOWED_ERR = ((ExceptionMessage[7]="No modification allowed"),7); var NOT_FOUND_ERR = ExceptionCode.NOT_FOUND_ERR = ((ExceptionMessage[8]="Not found"),8); var NOT_SUPPORTED_ERR = ExceptionCode.NOT_SUPPORTED_ERR = ((ExceptionMessage[9]="Not supported"),9); var INUSE_ATTRIBUTE_ERR = ExceptionCode.INUSE_ATTRIBUTE_ERR = ((ExceptionMessage[10]="Attribute in use"),10); //level2 var INVALID_STATE_ERR = ExceptionCode.INVALID_STATE_ERR = ((ExceptionMessage[11]="Invalid state"),11); var SYNTAX_ERR = ExceptionCode.SYNTAX_ERR = ((ExceptionMessage[12]="Syntax error"),12); var INVALID_MODIFICATION_ERR = ExceptionCode.INVALID_MODIFICATION_ERR = ((ExceptionMessage[13]="Invalid modification"),13); var NAMESPACE_ERR = ExceptionCode.NAMESPACE_ERR = ((ExceptionMessage[14]="Invalid namespace"),14); var INVALID_ACCESS_ERR = ExceptionCode.INVALID_ACCESS_ERR = ((ExceptionMessage[15]="Invalid access"),15); /** * DOM Level 2 * Object DOMException * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/ecma-script-binding.html * @see http://www.w3.org/TR/REC-DOM-Level-1/ecma-script-language-binding.html */ function DOMException(code, message) { if(message instanceof Error){ var error = message; }else{ error = this; Error.call(this, ExceptionMessage[code]); this.message = ExceptionMessage[code]; if(Error.captureStackTrace) Error.captureStackTrace(this, DOMException); } error.code = code; if(message) this.message = this.message + ": " + message; return error; }; DOMException.prototype = Error.prototype; copy(ExceptionCode,DOMException) /** * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-536297177 * The NodeList interface provides the abstraction of an ordered collection of nodes, without defining or constraining how this collection is implemented. NodeList objects in the DOM are live. * The items in the NodeList are accessible via an integral index, starting from 0. */ function NodeList() { }; NodeList.prototype = { /** * The number of nodes in the list. The range of valid child node indices is 0 to length-1 inclusive. * @standard level1 */ length:0, /** * Returns the indexth item in the collection. If index is greater than or equal to the number of nodes in the list, this returns null. * @standard level1 * @param index unsigned long * Index into the collection. * @return Node * The node at the indexth position in the NodeList, or null if that is not a valid index. */ item: function(index) { return this[index] || null; }, toString:function(isHTML,nodeFilter){ for(var buf = [], i = 0;i=0){ var lastIndex = list.length-1 while(i0 || key == 'xmlns'){ // return null; // } //console.log() var i = this.length; while(i--){ var attr = this[i]; //console.log(attr.nodeName,key) if(attr.nodeName == key){ return attr; } } }, setNamedItem: function(attr) { var el = attr.ownerElement; if(el && el!=this._ownerElement){ throw new DOMException(INUSE_ATTRIBUTE_ERR); } var oldAttr = this.getNamedItem(attr.nodeName); _addNamedNode(this._ownerElement,this,attr,oldAttr); return oldAttr; }, /* returns Node */ setNamedItemNS: function(attr) {// raises: WRONG_DOCUMENT_ERR,NO_MODIFICATION_ALLOWED_ERR,INUSE_ATTRIBUTE_ERR var el = attr.ownerElement, oldAttr; if(el && el!=this._ownerElement){ throw new DOMException(INUSE_ATTRIBUTE_ERR); } oldAttr = this.getNamedItemNS(attr.namespaceURI,attr.localName); _addNamedNode(this._ownerElement,this,attr,oldAttr); return oldAttr; }, /* returns Node */ removeNamedItem: function(key) { var attr = this.getNamedItem(key); _removeNamedNode(this._ownerElement,this,attr); return attr; },// raises: NOT_FOUND_ERR,NO_MODIFICATION_ALLOWED_ERR //for level2 removeNamedItemNS:function(namespaceURI,localName){ var attr = this.getNamedItemNS(namespaceURI,localName); _removeNamedNode(this._ownerElement,this,attr); return attr; }, getNamedItemNS: function(namespaceURI, localName) { var i = this.length; while(i--){ var node = this[i]; if(node.localName == localName && node.namespaceURI == namespaceURI){ return node; } } return null; } }; /** * The DOMImplementation interface represents an object providing methods * which are not dependent on any particular document. * Such an object is returned by the `Document.implementation` property. * * __The individual methods describe the differences compared to the specs.__ * * @constructor * * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation MDN * @see https://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#ID-102161490 DOM Level 1 Core (Initial) * @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-102161490 DOM Level 2 Core * @see https://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-102161490 DOM Level 3 Core * @see https://dom.spec.whatwg.org/#domimplementation DOM Living Standard */ function DOMImplementation() { } DOMImplementation.prototype = { /** * The DOMImplementation.hasFeature() method returns a Boolean flag indicating if a given feature is supported. * The different implementations fairly diverged in what kind of features were reported. * The latest version of the spec settled to force this method to always return true, where the functionality was accurate and in use. * * @deprecated It is deprecated and modern browsers return true in all cases. * * @param {string} feature * @param {string} [version] * @returns {boolean} always true * * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/hasFeature MDN * @see https://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#ID-5CED94D7 DOM Level 1 Core * @see https://dom.spec.whatwg.org/#dom-domimplementation-hasfeature DOM Living Standard */ hasFeature: function(feature, version) { return true; }, /** * Creates an XML Document object of the specified type with its document element. * * __It behaves slightly different from the description in the living standard__: * - There is no interface/class `XMLDocument`, it returns a `Document` instance. * - `contentType`, `encoding`, `mode`, `origin`, `url` fields are currently not declared. * - this implementation is not validating names or qualified names * (when parsing XML strings, the SAX parser takes care of that) * * @param {string|null} namespaceURI * @param {string} qualifiedName * @param {DocumentType=null} doctype * @returns {Document} * * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createDocument MDN * @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#Level-2-Core-DOM-createDocument DOM Level 2 Core (initial) * @see https://dom.spec.whatwg.org/#dom-domimplementation-createdocument DOM Level 2 Core * * @see https://dom.spec.whatwg.org/#validate-and-extract DOM: Validate and extract * @see https://www.w3.org/TR/xml/#NT-NameStartChar XML Spec: Names * @see https://www.w3.org/TR/xml-names/#ns-qualnames XML Namespaces: Qualified names */ createDocument: function(namespaceURI, qualifiedName, doctype){ var doc = new Document(); doc.implementation = this; doc.childNodes = new NodeList(); doc.doctype = doctype || null; if (doctype){ doc.appendChild(doctype); } if (qualifiedName){ var root = doc.createElementNS(namespaceURI, qualifiedName); doc.appendChild(root); } return doc; }, /** * Returns a doctype, with the given `qualifiedName`, `publicId`, and `systemId`. * * __This behavior is slightly different from the in the specs__: * - this implementation is not validating names or qualified names * (when parsing XML strings, the SAX parser takes care of that) * * @param {string} qualifiedName * @param {string} [publicId] * @param {string} [systemId] * @returns {DocumentType} which can either be used with `DOMImplementation.createDocument` upon document creation * or can be put into the document via methods like `Node.insertBefore()` or `Node.replaceChild()` * * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createDocumentType MDN * @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#Level-2-Core-DOM-createDocType DOM Level 2 Core * @see https://dom.spec.whatwg.org/#dom-domimplementation-createdocumenttype DOM Living Standard * * @see https://dom.spec.whatwg.org/#validate-and-extract DOM: Validate and extract * @see https://www.w3.org/TR/xml/#NT-NameStartChar XML Spec: Names * @see https://www.w3.org/TR/xml-names/#ns-qualnames XML Namespaces: Qualified names */ createDocumentType: function(qualifiedName, publicId, systemId){ var node = new DocumentType(); node.name = qualifiedName; node.nodeName = qualifiedName; node.publicId = publicId || ''; node.systemId = systemId || ''; return node; } }; /** * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1950641247 */ function Node() { }; Node.prototype = { firstChild : null, lastChild : null, previousSibling : null, nextSibling : null, attributes : null, parentNode : null, childNodes : null, ownerDocument : null, nodeValue : null, namespaceURI : null, prefix : null, localName : null, // Modified in DOM Level 2: insertBefore:function(newChild, refChild){//raises return _insertBefore(this,newChild,refChild); }, replaceChild:function(newChild, oldChild){//raises this.insertBefore(newChild,oldChild); if(oldChild){ this.removeChild(oldChild); } }, removeChild:function(oldChild){ return _removeChild(this,oldChild); }, appendChild:function(newChild){ return this.insertBefore(newChild,null); }, hasChildNodes:function(){ return this.firstChild != null; }, cloneNode:function(deep){ return cloneNode(this.ownerDocument||this,this,deep); }, // Modified in DOM Level 2: normalize:function(){ var child = this.firstChild; while(child){ var next = child.nextSibling; if(next && next.nodeType == TEXT_NODE && child.nodeType == TEXT_NODE){ this.removeChild(next); child.appendData(next.data); }else{ child.normalize(); child = next; } } }, // Introduced in DOM Level 2: isSupported:function(feature, version){ return this.ownerDocument.implementation.hasFeature(feature,version); }, // Introduced in DOM Level 2: hasAttributes:function(){ return this.attributes.length>0; }, /** * Look up the prefix associated to the given namespace URI, starting from this node. * **The default namespace declarations are ignored by this method.** * See Namespace Prefix Lookup for details on the algorithm used by this method. * * _Note: The implementation seems to be incomplete when compared to the algorithm described in the specs._ * * @param {string | null} namespaceURI * @returns {string | null} * @see https://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-lookupNamespacePrefix * @see https://www.w3.org/TR/DOM-Level-3-Core/namespaces-algorithms.html#lookupNamespacePrefixAlgo * @see https://dom.spec.whatwg.org/#dom-node-lookupprefix * @see https://github.com/xmldom/xmldom/issues/322 */ lookupPrefix:function(namespaceURI){ var el = this; while(el){ var map = el._nsMap; //console.dir(map) if(map){ for(var n in map){ if(map[n] == namespaceURI){ return n; } } } el = el.nodeType == ATTRIBUTE_NODE?el.ownerDocument : el.parentNode; } return null; }, // Introduced in DOM Level 3: lookupNamespaceURI:function(prefix){ var el = this; while(el){ var map = el._nsMap; //console.dir(map) if(map){ if(prefix in map){ return map[prefix] ; } } el = el.nodeType == ATTRIBUTE_NODE?el.ownerDocument : el.parentNode; } return null; }, // Introduced in DOM Level 3: isDefaultNamespace:function(namespaceURI){ var prefix = this.lookupPrefix(namespaceURI); return prefix == null; } }; function _xmlEncoder(c){ return c == '<' && '<' || c == '>' && '>' || c == '&' && '&' || c == '"' && '"' || '&#'+c.charCodeAt()+';' } copy(NodeType,Node); copy(NodeType,Node.prototype); /** * @param callback return true for continue,false for break * @return boolean true: break visit; */ function _visitNode(node,callback){ if(callback(node)){ return true; } if(node = node.firstChild){ do{ if(_visitNode(node,callback)){return true} }while(node=node.nextSibling) } } function Document(){ } function _onAddAttribute(doc,el,newAttr){ doc && doc._inc++; var ns = newAttr.namespaceURI ; if(ns === NAMESPACE.XMLNS){ //update namespace el._nsMap[newAttr.prefix?newAttr.localName:''] = newAttr.value } } function _onRemoveAttribute(doc,el,newAttr,remove){ doc && doc._inc++; var ns = newAttr.namespaceURI ; if(ns === NAMESPACE.XMLNS){ //update namespace delete el._nsMap[newAttr.prefix?newAttr.localName:''] } } /** * Updates `el.childNodes`, updating the indexed items and it's `length`. * Passing `newChild` means it will be appended. * Otherwise it's assumed that an item has been removed, * and `el.firstNode` and it's `.nextSibling` are used * to walk the current list of child nodes. * * @param {Document} doc * @param {Node} el * @param {Node} [newChild] * @private */ function _onUpdateChild (doc, el, newChild) { if(doc && doc._inc){ doc._inc++; //update childNodes var cs = el.childNodes; if (newChild) { cs[cs.length++] = newChild; } else { var child = el.firstChild; var i = 0; while (child) { cs[i++] = child; child = child.nextSibling; } cs.length = i; delete cs[cs.length]; } } } /** * Removes the connections between `parentNode` and `child` * and any existing `child.previousSibling` or `child.nextSibling`. * * @see https://github.com/xmldom/xmldom/issues/135 * @see https://github.com/xmldom/xmldom/issues/145 * * @param {Node} parentNode * @param {Node} child * @returns {Node} the child that was removed. * @private */ function _removeChild (parentNode, child) { var previous = child.previousSibling; var next = child.nextSibling; if (previous) { previous.nextSibling = next; } else { parentNode.firstChild = next; } if (next) { next.previousSibling = previous; } else { parentNode.lastChild = previous; } child.parentNode = null; child.previousSibling = null; child.nextSibling = null; _onUpdateChild(parentNode.ownerDocument, parentNode); return child; } /** * preformance key(refChild == null) */ function _insertBefore(parentNode,newChild,nextChild){ var cp = newChild.parentNode; if(cp){ cp.removeChild(newChild);//remove and update } if(newChild.nodeType === DOCUMENT_FRAGMENT_NODE){ var newFirst = newChild.firstChild; if (newFirst == null) { return newChild; } var newLast = newChild.lastChild; }else{ newFirst = newLast = newChild; } var pre = nextChild ? nextChild.previousSibling : parentNode.lastChild; newFirst.previousSibling = pre; newLast.nextSibling = nextChild; if(pre){ pre.nextSibling = newFirst; }else{ parentNode.firstChild = newFirst; } if(nextChild == null){ parentNode.lastChild = newLast; }else{ nextChild.previousSibling = newLast; } do{ newFirst.parentNode = parentNode; }while(newFirst !== newLast && (newFirst= newFirst.nextSibling)) _onUpdateChild(parentNode.ownerDocument||parentNode,parentNode); //console.log(parentNode.lastChild.nextSibling == null) if (newChild.nodeType == DOCUMENT_FRAGMENT_NODE) { newChild.firstChild = newChild.lastChild = null; } return newChild; } /** * Appends `newChild` to `parentNode`. * If `newChild` is already connected to a `parentNode` it is first removed from it. * * @see https://github.com/xmldom/xmldom/issues/135 * @see https://github.com/xmldom/xmldom/issues/145 * @param {Node} parentNode * @param {Node} newChild * @returns {Node} * @private */ function _appendSingleChild (parentNode, newChild) { if (newChild.parentNode) { newChild.parentNode.removeChild(newChild); } newChild.parentNode = parentNode; newChild.previousSibling = parentNode.lastChild; newChild.nextSibling = null; if (newChild.previousSibling) { newChild.previousSibling.nextSibling = newChild; } else { parentNode.firstChild = newChild; } parentNode.lastChild = newChild; _onUpdateChild(parentNode.ownerDocument, parentNode, newChild); return newChild; } Document.prototype = { //implementation : null, nodeName : '#document', nodeType : DOCUMENT_NODE, /** * The DocumentType node of the document. * * @readonly * @type DocumentType */ doctype : null, documentElement : null, _inc : 1, insertBefore : function(newChild, refChild){//raises if(newChild.nodeType == DOCUMENT_FRAGMENT_NODE){ var child = newChild.firstChild; while(child){ var next = child.nextSibling; this.insertBefore(child,refChild); child = next; } return newChild; } if(this.documentElement == null && newChild.nodeType == ELEMENT_NODE){ this.documentElement = newChild; } return _insertBefore(this,newChild,refChild),(newChild.ownerDocument = this),newChild; }, removeChild : function(oldChild){ if(this.documentElement == oldChild){ this.documentElement = null; } return _removeChild(this,oldChild); }, // Introduced in DOM Level 2: importNode : function(importedNode,deep){ return importNode(this,importedNode,deep); }, // Introduced in DOM Level 2: getElementById : function(id){ var rtv = null; _visitNode(this.documentElement,function(node){ if(node.nodeType == ELEMENT_NODE){ if(node.getAttribute('id') == id){ rtv = node; return true; } } }) return rtv; }, /** * The `getElementsByClassName` method of `Document` interface returns an array-like object * of all child elements which have **all** of the given class name(s). * * Returns an empty list if `classeNames` is an empty string or only contains HTML white space characters. * * * Warning: This is a live LiveNodeList. * Changes in the DOM will reflect in the array as the changes occur. * If an element selected by this array no longer qualifies for the selector, * it will automatically be removed. Be aware of this for iteration purposes. * * @param {string} classNames is a string representing the class name(s) to match; multiple class names are separated by (ASCII-)whitespace * * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName * @see https://dom.spec.whatwg.org/#concept-getelementsbyclassname */ getElementsByClassName: function(classNames) { var classNamesSet = toOrderedSet(classNames) return new LiveNodeList(this, function(base) { var ls = []; if (classNamesSet.length > 0) { _visitNode(base.documentElement, function(node) { if(node !== base && node.nodeType === ELEMENT_NODE) { var nodeClassNames = node.getAttribute('class') // can be null if the attribute does not exist if (nodeClassNames) { // before splitting and iterating just compare them for the most common case var matches = classNames === nodeClassNames; if (!matches) { var nodeClassNamesSet = toOrderedSet(nodeClassNames) matches = classNamesSet.every(arrayIncludes(nodeClassNamesSet)) } if(matches) { ls.push(node); } } } }); } return ls; }); }, //document factory method: createElement : function(tagName){ var node = new Element(); node.ownerDocument = this; node.nodeName = tagName; node.tagName = tagName; node.localName = tagName; node.childNodes = new NodeList(); var attrs = node.attributes = new NamedNodeMap(); attrs._ownerElement = node; return node; }, createDocumentFragment : function(){ var node = new DocumentFragment(); node.ownerDocument = this; node.childNodes = new NodeList(); return node; }, createTextNode : function(data){ var node = new Text(); node.ownerDocument = this; node.appendData(data) return node; }, createComment : function(data){ var node = new Comment(); node.ownerDocument = this; node.appendData(data) return node; }, createCDATASection : function(data){ var node = new CDATASection(); node.ownerDocument = this; node.appendData(data) return node; }, createProcessingInstruction : function(target,data){ var node = new ProcessingInstruction(); node.ownerDocument = this; node.tagName = node.target = target; node.nodeValue= node.data = data; return node; }, createAttribute : function(name){ var node = new Attr(); node.ownerDocument = this; node.name = name; node.nodeName = name; node.localName = name; node.specified = true; return node; }, createEntityReference : function(name){ var node = new EntityReference(); node.ownerDocument = this; node.nodeName = name; return node; }, // Introduced in DOM Level 2: createElementNS : function(namespaceURI,qualifiedName){ var node = new Element(); var pl = qualifiedName.split(':'); var attrs = node.attributes = new NamedNodeMap(); node.childNodes = new NodeList(); node.ownerDocument = this; node.nodeName = qualifiedName; node.tagName = qualifiedName; node.namespaceURI = namespaceURI; if(pl.length == 2){ node.prefix = pl[0]; node.localName = pl[1]; }else{ //el.prefix = null; node.localName = qualifiedName; } attrs._ownerElement = node; return node; }, // Introduced in DOM Level 2: createAttributeNS : function(namespaceURI,qualifiedName){ var node = new Attr(); var pl = qualifiedName.split(':'); node.ownerDocument = this; node.nodeName = qualifiedName; node.name = qualifiedName; node.namespaceURI = namespaceURI; node.specified = true; if(pl.length == 2){ node.prefix = pl[0]; node.localName = pl[1]; }else{ //el.prefix = null; node.localName = qualifiedName; } return node; } }; _extends(Document,Node); function Element() { this._nsMap = {}; }; Element.prototype = { nodeType : ELEMENT_NODE, hasAttribute : function(name){ return this.getAttributeNode(name)!=null; }, getAttribute : function(name){ var attr = this.getAttributeNode(name); return attr && attr.value || ''; }, getAttributeNode : function(name){ return this.attributes.getNamedItem(name); }, setAttribute : function(name, value){ var attr = this.ownerDocument.createAttribute(name); attr.value = attr.nodeValue = "" + value; this.setAttributeNode(attr) }, removeAttribute : function(name){ var attr = this.getAttributeNode(name) attr && this.removeAttributeNode(attr); }, //four real opeartion method appendChild:function(newChild){ if(newChild.nodeType === DOCUMENT_FRAGMENT_NODE){ return this.insertBefore(newChild,null); }else{ return _appendSingleChild(this,newChild); } }, setAttributeNode : function(newAttr){ return this.attributes.setNamedItem(newAttr); }, setAttributeNodeNS : function(newAttr){ return this.attributes.setNamedItemNS(newAttr); }, removeAttributeNode : function(oldAttr){ //console.log(this == oldAttr.ownerElement) return this.attributes.removeNamedItem(oldAttr.nodeName); }, //get real attribute name,and remove it by removeAttributeNode removeAttributeNS : function(namespaceURI, localName){ var old = this.getAttributeNodeNS(namespaceURI, localName); old && this.removeAttributeNode(old); }, hasAttributeNS : function(namespaceURI, localName){ return this.getAttributeNodeNS(namespaceURI, localName)!=null; }, getAttributeNS : function(namespaceURI, localName){ var attr = this.getAttributeNodeNS(namespaceURI, localName); return attr && attr.value || ''; }, setAttributeNS : function(namespaceURI, qualifiedName, value){ var attr = this.ownerDocument.createAttributeNS(namespaceURI, qualifiedName); attr.value = attr.nodeValue = "" + value; this.setAttributeNode(attr) }, getAttributeNodeNS : function(namespaceURI, localName){ return this.attributes.getNamedItemNS(namespaceURI, localName); }, getElementsByTagName : function(tagName){ return new LiveNodeList(this,function(base){ var ls = []; _visitNode(base,function(node){ if(node !== base && node.nodeType == ELEMENT_NODE && (tagName === '*' || node.tagName == tagName)){ ls.push(node); } }); return ls; }); }, getElementsByTagNameNS : function(namespaceURI, localName){ return new LiveNodeList(this,function(base){ var ls = []; _visitNode(base,function(node){ if(node !== base && node.nodeType === ELEMENT_NODE && (namespaceURI === '*' || node.namespaceURI === namespaceURI) && (localName === '*' || node.localName == localName)){ ls.push(node); } }); return ls; }); } }; Document.prototype.getElementsByTagName = Element.prototype.getElementsByTagName; Document.prototype.getElementsByTagNameNS = Element.prototype.getElementsByTagNameNS; _extends(Element,Node); function Attr() { }; Attr.prototype.nodeType = ATTRIBUTE_NODE; _extends(Attr,Node); function CharacterData() { }; CharacterData.prototype = { data : '', substringData : function(offset, count) { return this.data.substring(offset, offset+count); }, appendData: function(text) { text = this.data+text; this.nodeValue = this.data = text; this.length = text.length; }, insertData: function(offset,text) { this.replaceData(offset,0,text); }, appendChild:function(newChild){ throw new Error(ExceptionMessage[HIERARCHY_REQUEST_ERR]) }, deleteData: function(offset, count) { this.replaceData(offset,count,""); }, replaceData: function(offset, count, text) { var start = this.data.substring(0,offset); var end = this.data.substring(offset+count); text = start + text + end; this.nodeValue = this.data = text; this.length = text.length; } } _extends(CharacterData,Node); function Text() { }; Text.prototype = { nodeName : "#text", nodeType : TEXT_NODE, splitText : function(offset) { var text = this.data; var newText = text.substring(offset); text = text.substring(0, offset); this.data = this.nodeValue = text; this.length = text.length; var newNode = this.ownerDocument.createTextNode(newText); if(this.parentNode){ this.parentNode.insertBefore(newNode, this.nextSibling); } return newNode; } } _extends(Text,CharacterData); function Comment() { }; Comment.prototype = { nodeName : "#comment", nodeType : COMMENT_NODE } _extends(Comment,CharacterData); function CDATASection() { }; CDATASection.prototype = { nodeName : "#cdata-section", nodeType : CDATA_SECTION_NODE } _extends(CDATASection,CharacterData); function DocumentType() { }; DocumentType.prototype.nodeType = DOCUMENT_TYPE_NODE; _extends(DocumentType,Node); function Notation() { }; Notation.prototype.nodeType = NOTATION_NODE; _extends(Notation,Node); function Entity() { }; Entity.prototype.nodeType = ENTITY_NODE; _extends(Entity,Node); function EntityReference() { }; EntityReference.prototype.nodeType = ENTITY_REFERENCE_NODE; _extends(EntityReference,Node); function DocumentFragment() { }; DocumentFragment.prototype.nodeName = "#document-fragment"; DocumentFragment.prototype.nodeType = DOCUMENT_FRAGMENT_NODE; _extends(DocumentFragment,Node); function ProcessingInstruction() { } ProcessingInstruction.prototype.nodeType = PROCESSING_INSTRUCTION_NODE; _extends(ProcessingInstruction,Node); function XMLSerializer(){} XMLSerializer.prototype.serializeToString = function(node,isHtml,nodeFilter){ return nodeSerializeToString.call(node,isHtml,nodeFilter); } Node.prototype.toString = nodeSerializeToString; function nodeSerializeToString(isHtml,nodeFilter){ var buf = []; var refNode = this.nodeType == 9 && this.documentElement || this; var prefix = refNode.prefix; var uri = refNode.namespaceURI; if(uri && prefix == null){ //console.log(prefix) var prefix = refNode.lookupPrefix(uri); if(prefix == null){ //isHTML = true; var visibleNamespaces=[ {namespace:uri,prefix:null} //{namespace:uri,prefix:''} ] } } serializeToString(this,buf,isHtml,nodeFilter,visibleNamespaces); //console.log('###',this.nodeType,uri,prefix,buf.join('')) return buf.join(''); } function needNamespaceDefine(node, isHTML, visibleNamespaces) { var prefix = node.prefix || ''; var uri = node.namespaceURI; // According to [Namespaces in XML 1.0](https://www.w3.org/TR/REC-xml-names/#ns-using) , // and more specifically https://www.w3.org/TR/REC-xml-names/#nsc-NoPrefixUndecl : // > In a namespace declaration for a prefix [...], the attribute value MUST NOT be empty. // in a similar manner [Namespaces in XML 1.1](https://www.w3.org/TR/xml-names11/#ns-using) // and more specifically https://www.w3.org/TR/xml-names11/#nsc-NSDeclared : // > [...] Furthermore, the attribute value [...] must not be an empty string. // so serializing empty namespace value like xmlns:ds="" would produce an invalid XML document. if (!uri) { return false; } if (prefix === "xml" && uri === NAMESPACE.XML || uri === NAMESPACE.XMLNS) { return false; } var i = visibleNamespaces.length while (i--) { var ns = visibleNamespaces[i]; // get namespace prefix if (ns.prefix === prefix) { return ns.namespace !== uri; } } return true; } /** * Well-formed constraint: No < in Attribute Values * > The replacement text of any entity referred to directly or indirectly * > in an attribute value must not contain a <. * @see https://www.w3.org/TR/xml11/#CleanAttrVals * @see https://www.w3.org/TR/xml11/#NT-AttValue * * Literal whitespace other than space that appear in attribute values * are serialized as their entity references, so they will be preserved. * (In contrast to whitespace literals in the input which are normalized to spaces) * @see https://www.w3.org/TR/xml11/#AVNormalize * @see https://w3c.github.io/DOM-Parsing/#serializing-an-element-s-attributes */ function addSerializedAttribute(buf, qualifiedName, value) { buf.push(' ', qualifiedName, '="', value.replace(/[<>&"\t\n\r]/g, _xmlEncoder), '"') } function serializeToString(node,buf,isHTML,nodeFilter,visibleNamespaces){ if (!visibleNamespaces) { visibleNamespaces = []; } if(nodeFilter){ node = nodeFilter(node); if(node){ if(typeof node == 'string'){ buf.push(node); return; } }else{ return; } //buf.sort.apply(attrs, attributeSorter); } switch(node.nodeType){ case ELEMENT_NODE: var attrs = node.attributes; var len = attrs.length; var child = node.firstChild; var nodeName = node.tagName; isHTML = NAMESPACE.isHTML(node.namespaceURI) || isHTML var prefixedNodeName = nodeName if (!isHTML && !node.prefix && node.namespaceURI) { var defaultNS // lookup current default ns from `xmlns` attribute for (var ai = 0; ai < attrs.length; ai++) { if (attrs.item(ai).name === 'xmlns') { defaultNS = attrs.item(ai).value break } } if (!defaultNS) { // lookup current default ns in visibleNamespaces for (var nsi = visibleNamespaces.length - 1; nsi >= 0; nsi--) { var namespace = visibleNamespaces[nsi] if (namespace.prefix === '' && namespace.namespace === node.namespaceURI) { defaultNS = namespace.namespace break } } } if (defaultNS !== node.namespaceURI) { for (var nsi = visibleNamespaces.length - 1; nsi >= 0; nsi--) { var namespace = visibleNamespaces[nsi] if (namespace.namespace === node.namespaceURI) { if (namespace.prefix) { prefixedNodeName = namespace.prefix + ':' + nodeName } break } } } } buf.push('<', prefixedNodeName); for(var i=0;i'); //if is cdata child node if(isHTML && /^script$/i.test(nodeName)){ while(child){ if(child.data){ buf.push(child.data); }else{ serializeToString(child, buf, isHTML, nodeFilter, visibleNamespaces.slice()); } child = child.nextSibling; } }else { while(child){ serializeToString(child, buf, isHTML, nodeFilter, visibleNamespaces.slice()); child = child.nextSibling; } } buf.push(''); }else{ buf.push('/>'); } // remove added visible namespaces //visibleNamespaces.length = startVisibleNamespaces; return; case DOCUMENT_NODE: case DOCUMENT_FRAGMENT_NODE: var child = node.firstChild; while(child){ serializeToString(child, buf, isHTML, nodeFilter, visibleNamespaces.slice()); child = child.nextSibling; } return; case ATTRIBUTE_NODE: return addSerializedAttribute(buf, node.name, node.value); case TEXT_NODE: /** * The ampersand character (&) and the left angle bracket (<) must not appear in their literal form, * except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. * If they are needed elsewhere, they must be escaped using either numeric character references or the strings * `&` and `<` respectively. * The right angle bracket (>) may be represented using the string " > ", and must, for compatibility, * be escaped using either `>` or a character reference when it appears in the string `]]>` in content, * when that string is not marking the end of a CDATA section. * * In the content of elements, character data is any string of characters * which does not contain the start-delimiter of any markup * and does not include the CDATA-section-close delimiter, `]]>`. * * @see https://www.w3.org/TR/xml/#NT-CharData * @see https://w3c.github.io/DOM-Parsing/#xml-serializing-a-text-node */ return buf.push(node.data .replace(/[<&>]/g,_xmlEncoder) ); case CDATA_SECTION_NODE: return buf.push( ''); case COMMENT_NODE: return buf.push( ""); case DOCUMENT_TYPE_NODE: var pubid = node.publicId; var sysid = node.systemId; buf.push(''); }else if(sysid && sysid!='.'){ buf.push(' SYSTEM ', sysid, '>'); }else{ var sub = node.internalSubset; if(sub){ buf.push(" [",sub,"]"); } buf.push(">"); } return; case PROCESSING_INSTRUCTION_NODE: return buf.push( ""); case ENTITY_REFERENCE_NODE: return buf.push( '&',node.nodeName,';'); //case ENTITY_NODE: //case NOTATION_NODE: default: buf.push('??',node.nodeName); } } function importNode(doc,node,deep){ var node2; switch (node.nodeType) { case ELEMENT_NODE: node2 = node.cloneNode(false); node2.ownerDocument = doc; //var attrs = node2.attributes; //var len = attrs.length; //for(var i=0;i', lt:'<', quot:'"'}) /** * A map of currently 241 entities that are detected in an HTML document. * They contain all entries from `XML_ENTITIES`. * * @see XML_ENTITIES * @see DOMParser.parseFromString * @see DOMImplementation.prototype.createHTMLDocument * @see https://html.spec.whatwg.org/#named-character-references WHATWG HTML(5) Spec * @see https://www.w3.org/TR/xml-entity-names/ W3C XML Entity Names * @see https://www.w3.org/TR/html4/sgml/entities.html W3C HTML4/SGML * @see https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Character_entity_references_in_HTML Wikipedia (HTML) * @see https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Entities_representing_special_characters_in_XHTML Wikpedia (XHTML) */ exports.HTML_ENTITIES = freeze({ lt: '<', gt: '>', amp: '&', quot: '"', apos: "'", Agrave: "À", Aacute: "Á", Acirc: "Â", Atilde: "Ã", Auml: "Ä", Aring: "Å", AElig: "Æ", Ccedil: "Ç", Egrave: "È", Eacute: "É", Ecirc: "Ê", Euml: "Ë", Igrave: "Ì", Iacute: "Í", Icirc: "Î", Iuml: "Ï", ETH: "Ð", Ntilde: "Ñ", Ograve: "Ò", Oacute: "Ó", Ocirc: "Ô", Otilde: "Õ", Ouml: "Ö", Oslash: "Ø", Ugrave: "Ù", Uacute: "Ú", Ucirc: "Û", Uuml: "Ü", Yacute: "Ý", THORN: "Þ", szlig: "ß", agrave: "à", aacute: "á", acirc: "â", atilde: "ã", auml: "ä", aring: "å", aelig: "æ", ccedil: "ç", egrave: "è", eacute: "é", ecirc: "ê", euml: "ë", igrave: "ì", iacute: "í", icirc: "î", iuml: "ï", eth: "ð", ntilde: "ñ", ograve: "ò", oacute: "ó", ocirc: "ô", otilde: "õ", ouml: "ö", oslash: "ø", ugrave: "ù", uacute: "ú", ucirc: "û", uuml: "ü", yacute: "ý", thorn: "þ", yuml: "ÿ", nbsp: "\u00a0", iexcl: "¡", cent: "¢", pound: "£", curren: "¤", yen: "¥", brvbar: "¦", sect: "§", uml: "¨", copy: "©", ordf: "ª", laquo: "«", not: "¬", shy: "­­", reg: "®", macr: "¯", deg: "°", plusmn: "±", sup2: "²", sup3: "³", acute: "´", micro: "µ", para: "¶", middot: "·", cedil: "¸", sup1: "¹", ordm: "º", raquo: "»", frac14: "¼", frac12: "½", frac34: "¾", iquest: "¿", times: "×", divide: "÷", forall: "∀", part: "∂", exist: "∃", empty: "∅", nabla: "∇", isin: "∈", notin: "∉", ni: "∋", prod: "∏", sum: "∑", minus: "−", lowast: "∗", radic: "√", prop: "∝", infin: "∞", ang: "∠", and: "∧", or: "∨", cap: "∩", cup: "∪", 'int': "∫", there4: "∴", sim: "∼", cong: "≅", asymp: "≈", ne: "≠", equiv: "≡", le: "≤", ge: "≥", sub: "⊂", sup: "⊃", nsub: "⊄", sube: "⊆", supe: "⊇", oplus: "⊕", otimes: "⊗", perp: "⊥", sdot: "⋅", Alpha: "Α", Beta: "Β", Gamma: "Γ", Delta: "Δ", Epsilon: "Ε", Zeta: "Ζ", Eta: "Η", Theta: "Θ", Iota: "Ι", Kappa: "Κ", Lambda: "Λ", Mu: "Μ", Nu: "Ν", Xi: "Ξ", Omicron: "Ο", Pi: "Π", Rho: "Ρ", Sigma: "Σ", Tau: "Τ", Upsilon: "Υ", Phi: "Φ", Chi: "Χ", Psi: "Ψ", Omega: "Ω", alpha: "α", beta: "β", gamma: "γ", delta: "δ", epsilon: "ε", zeta: "ζ", eta: "η", theta: "θ", iota: "ι", kappa: "κ", lambda: "λ", mu: "μ", nu: "ν", xi: "ξ", omicron: "ο", pi: "π", rho: "ρ", sigmaf: "ς", sigma: "σ", tau: "τ", upsilon: "υ", phi: "φ", chi: "χ", psi: "ψ", omega: "ω", thetasym: "ϑ", upsih: "ϒ", piv: "ϖ", OElig: "Œ", oelig: "œ", Scaron: "Š", scaron: "š", Yuml: "Ÿ", fnof: "ƒ", circ: "ˆ", tilde: "˜", ensp: " ", emsp: " ", thinsp: " ", zwnj: "‌", zwj: "‍", lrm: "‎", rlm: "‏", ndash: "–", mdash: "—", lsquo: "‘", rsquo: "’", sbquo: "‚", ldquo: "“", rdquo: "”", bdquo: "„", dagger: "†", Dagger: "‡", bull: "•", hellip: "…", permil: "‰", prime: "′", Prime: "″", lsaquo: "‹", rsaquo: "›", oline: "‾", euro: "€", trade: "™", larr: "←", uarr: "↑", rarr: "→", darr: "↓", harr: "↔", crarr: "↵", lceil: "⌈", rceil: "⌉", lfloor: "⌊", rfloor: "⌋", loz: "◊", spades: "♠", clubs: "♣", hearts: "♥", diams: "♦" }); /** * @deprecated use `HTML_ENTITIES` instead * @see HTML_ENTITIES */ exports.entityMap = exports.HTML_ENTITIES },{"./conventions":1}],5:[function(require,module,exports){ var dom = require('./dom') exports.DOMImplementation = dom.DOMImplementation exports.XMLSerializer = dom.XMLSerializer exports.DOMParser = require('./dom-parser').DOMParser },{"./dom":3,"./dom-parser":2}],6:[function(require,module,exports){ var NAMESPACE = require("./conventions").NAMESPACE; //[4] NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF] //[4a] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040] //[5] Name ::= NameStartChar (NameChar)* var nameStartChar = /[A-Z_a-z\xC0-\xD6\xD8-\xF6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]///\u10000-\uEFFFF var nameChar = new RegExp("[\\-\\.0-9"+nameStartChar.source.slice(1,-1)+"\\u00B7\\u0300-\\u036F\\u203F-\\u2040]"); var tagNamePattern = new RegExp('^'+nameStartChar.source+nameChar.source+'*(?:\:'+nameStartChar.source+nameChar.source+'*)?$'); //var tagNamePattern = /^[a-zA-Z_][\w\-\.]*(?:\:[a-zA-Z_][\w\-\.]*)?$/ //var handlers = 'resolveEntity,getExternalSubset,characters,endDocument,endElement,endPrefixMapping,ignorableWhitespace,processingInstruction,setDocumentLocator,skippedEntity,startDocument,startElement,startPrefixMapping,notationDecl,unparsedEntityDecl,error,fatalError,warning,attributeDecl,elementDecl,externalEntityDecl,internalEntityDecl,comment,endCDATA,endDTD,endEntity,startCDATA,startDTD,startEntity'.split(',') //S_TAG, S_ATTR, S_EQ, S_ATTR_NOQUOT_VALUE //S_ATTR_SPACE, S_ATTR_END, S_TAG_SPACE, S_TAG_CLOSE var S_TAG = 0;//tag name offerring var S_ATTR = 1;//attr name offerring var S_ATTR_SPACE=2;//attr name end and space offer var S_EQ = 3;//=space? var S_ATTR_NOQUOT_VALUE = 4;//attr value(no quot value only) var S_ATTR_END = 5;//attr value end and no space(quot end) var S_TAG_SPACE = 6;//(attr value end || tag end ) && (space offer) var S_TAG_CLOSE = 7;//closed el /** * Creates an error that will not be caught by XMLReader aka the SAX parser. * * @param {string} message * @param {any?} locator Optional, can provide details about the location in the source * @constructor */ function ParseError(message, locator) { this.message = message this.locator = locator if(Error.captureStackTrace) Error.captureStackTrace(this, ParseError); } ParseError.prototype = new Error(); ParseError.prototype.name = ParseError.name function XMLReader(){ } XMLReader.prototype = { parse:function(source,defaultNSMap,entityMap){ var domBuilder = this.domBuilder; domBuilder.startDocument(); _copy(defaultNSMap ,defaultNSMap = {}) parse(source,defaultNSMap,entityMap, domBuilder,this.errorHandler); domBuilder.endDocument(); } } function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){ function fixedFromCharCode(code) { // String.prototype.fromCharCode does not supports // > 2 bytes unicode chars directly if (code > 0xffff) { code -= 0x10000; var surrogate1 = 0xd800 + (code >> 10) , surrogate2 = 0xdc00 + (code & 0x3ff); return String.fromCharCode(surrogate1, surrogate2); } else { return String.fromCharCode(code); } } function entityReplacer(a){ var k = a.slice(1,-1); if (Object.hasOwnProperty.call(entityMap, k)) { return entityMap[k]; }else if(k.charAt(0) === '#'){ return fixedFromCharCode(parseInt(k.substr(1).replace('x','0x'))) }else{ errorHandler.error('entity not found:'+a); return a; } } function appendText(end){//has some bugs if(end>start){ var xt = source.substring(start,end).replace(/&#?\w+;/g,entityReplacer); locator&&position(start); domBuilder.characters(xt,0,end-start); start = end } } function position(p,m){ while(p>=lineEnd && (m = linePattern.exec(source))){ lineStart = m.index; lineEnd = lineStart + m[0].length; locator.lineNumber++; //console.log('line++:',locator,startPos,endPos) } locator.columnNumber = p-lineStart+1; } var lineStart = 0; var lineEnd = 0; var linePattern = /.*(?:\r\n?|\n)|.*$/g var locator = domBuilder.locator; var parseStack = [{currentNSMap:defaultNSMapCopy}] var closeMap = {}; var start = 0; while(true){ try{ var tagStart = source.indexOf('<',start); if(tagStart<0){ if(!source.substr(start).match(/^\s*$/)){ var doc = domBuilder.doc; var text = doc.createTextNode(source.substr(start)); doc.appendChild(text); domBuilder.currentElement = text; } return; } if(tagStart>start){ appendText(tagStart); } switch(source.charAt(tagStart+1)){ case '/': var end = source.indexOf('>',tagStart+3); var tagName = source.substring(tagStart + 2, end).replace(/[ \t\n\r]+$/g, ''); var config = parseStack.pop(); if(end<0){ tagName = source.substring(tagStart+2).replace(/[\s<].*/,''); errorHandler.error("end tag name: "+tagName+' is not complete:'+config.tagName); end = tagStart+1+tagName.length; }else if(tagName.match(/\s locator&&position(tagStart); end = parseInstruction(source,tagStart,domBuilder); break; case '!':// start){ start = end; }else{ //TODO: 这里有可能sax回退,有位置错误风险 appendText(Math.max(tagStart,start)+1); } } } function copyLocator(f,t){ t.lineNumber = f.lineNumber; t.columnNumber = f.columnNumber; return t; } /** * @see #appendElement(source,elStartEnd,el,selfClosed,entityReplacer,domBuilder,parseStack); * @return end of the elementStartPart(end of elementEndPart for selfClosed el) */ function parseElementStartPart(source,start,el,currentNSMap,entityReplacer,errorHandler){ /** * @param {string} qname * @param {string} value * @param {number} startIndex */ function addAttribute(qname, value, startIndex) { if (el.attributeNames.hasOwnProperty(qname)) { errorHandler.fatalError('Attribute ' + qname + ' redefined') } el.addValue( qname, // @see https://www.w3.org/TR/xml/#AVNormalize // since the xmldom sax parser does not "interpret" DTD the following is not implemented: // - recursive replacement of (DTD) entity references // - trimming and collapsing multiple spaces into a single one for attributes that are not of type CDATA value.replace(/[\t\n\r]/g, ' ').replace(/&#?\w+;/g, entityReplacer), startIndex ) } var attrName; var value; var p = ++start; var s = S_TAG;//status while(true){ var c = source.charAt(p); switch(c){ case '=': if(s === S_ATTR){//attrName attrName = source.slice(start,p); s = S_EQ; }else if(s === S_ATTR_SPACE){ s = S_EQ; }else{ //fatalError: equal must after attrName or space after attrName throw new Error('attribute equal must after attrName'); // No known test case } break; case '\'': case '"': if(s === S_EQ || s === S_ATTR //|| s == S_ATTR_SPACE ){//equal if(s === S_ATTR){ errorHandler.warning('attribute value must after "="') attrName = source.slice(start,p) } start = p+1; p = source.indexOf(c,start) if(p>0){ value = source.slice(start, p); addAttribute(attrName, value, start-1); s = S_ATTR_END; }else{ //fatalError: no end quot match throw new Error('attribute value no end \''+c+'\' match'); } }else if(s == S_ATTR_NOQUOT_VALUE){ value = source.slice(start, p); addAttribute(attrName, value, start); errorHandler.warning('attribute "'+attrName+'" missed start quot('+c+')!!'); start = p+1; s = S_ATTR_END }else{ //fatalError: no equal before throw new Error('attribute value must after "="'); // No known test case } break; case '/': switch(s){ case S_TAG: el.setTagName(source.slice(start,p)); case S_ATTR_END: case S_TAG_SPACE: case S_TAG_CLOSE: s =S_TAG_CLOSE; el.closed = true; case S_ATTR_NOQUOT_VALUE: case S_ATTR: case S_ATTR_SPACE: break; //case S_EQ: default: throw new Error("attribute invalid close char('/')") // No known test case } break; case ''://end document errorHandler.error('unexpected end of input'); if(s == S_TAG){ el.setTagName(source.slice(start,p)); } return p; case '>': switch(s){ case S_TAG: el.setTagName(source.slice(start,p)); case S_ATTR_END: case S_TAG_SPACE: case S_TAG_CLOSE: break;//normal case S_ATTR_NOQUOT_VALUE://Compatible state case S_ATTR: value = source.slice(start,p); if(value.slice(-1) === '/'){ el.closed = true; value = value.slice(0,-1) } case S_ATTR_SPACE: if(s === S_ATTR_SPACE){ value = attrName; } if(s == S_ATTR_NOQUOT_VALUE){ errorHandler.warning('attribute "'+value+'" missed quot(")!'); addAttribute(attrName, value, start) }else{ if(!NAMESPACE.isHTML(currentNSMap['']) || !value.match(/^(?:disabled|checked|selected)$/i)){ errorHandler.warning('attribute "'+value+'" missed value!! "'+value+'" instead!!') } addAttribute(value, value, start) } break; case S_EQ: throw new Error('attribute value missed!!'); } // console.log(tagName,tagNamePattern,tagNamePattern.test(tagName)) return p; /*xml space '\x20' | #x9 | #xD | #xA; */ case '\u0080': c = ' '; default: if(c<= ' '){//space switch(s){ case S_TAG: el.setTagName(source.slice(start,p));//tagName s = S_TAG_SPACE; break; case S_ATTR: attrName = source.slice(start,p) s = S_ATTR_SPACE; break; case S_ATTR_NOQUOT_VALUE: var value = source.slice(start, p); errorHandler.warning('attribute "'+value+'" missed quot(")!!'); addAttribute(attrName, value, start) case S_ATTR_END: s = S_TAG_SPACE; break; //case S_TAG_SPACE: //case S_EQ: //case S_ATTR_SPACE: // void();break; //case S_TAG_CLOSE: //ignore warning } }else{//not space //S_TAG, S_ATTR, S_EQ, S_ATTR_NOQUOT_VALUE //S_ATTR_SPACE, S_ATTR_END, S_TAG_SPACE, S_TAG_CLOSE switch(s){ //case S_TAG:void();break; //case S_ATTR:void();break; //case S_ATTR_NOQUOT_VALUE:void();break; case S_ATTR_SPACE: var tagName = el.tagName; if (!NAMESPACE.isHTML(currentNSMap['']) || !attrName.match(/^(?:disabled|checked|selected)$/i)) { errorHandler.warning('attribute "'+attrName+'" missed value!! "'+attrName+'" instead2!!') } addAttribute(attrName, attrName, start); start = p; s = S_ATTR; break; case S_ATTR_END: errorHandler.warning('attribute space is required"'+attrName+'"!!') case S_TAG_SPACE: s = S_ATTR; start = p; break; case S_EQ: s = S_ATTR_NOQUOT_VALUE; start = p; break; case S_TAG_CLOSE: throw new Error("elements closed character '/' and '>' must be connected to"); } } }//end outer switch //console.log('p++',p) p++; } } /** * @return true if has new namespace define */ function appendElement(el,domBuilder,currentNSMap){ var tagName = el.tagName; var localNSMap = null; //var currentNSMap = parseStack[parseStack.length-1].currentNSMap; var i = el.length; while(i--){ var a = el[i]; var qName = a.qName; var value = a.value; var nsp = qName.indexOf(':'); if(nsp>0){ var prefix = a.prefix = qName.slice(0,nsp); var localName = qName.slice(nsp+1); var nsPrefix = prefix === 'xmlns' && localName }else{ localName = qName; prefix = null nsPrefix = qName === 'xmlns' && '' } //can not set prefix,because prefix !== '' a.localName = localName ; //prefix == null for no ns prefix attribute if(nsPrefix !== false){//hack!! if(localNSMap == null){ localNSMap = {} //console.log(currentNSMap,0) _copy(currentNSMap,currentNSMap={}) //console.log(currentNSMap,1) } currentNSMap[nsPrefix] = localNSMap[nsPrefix] = value; a.uri = NAMESPACE.XMLNS domBuilder.startPrefixMapping(nsPrefix, value) } } var i = el.length; while(i--){ a = el[i]; var prefix = a.prefix; if(prefix){//no prefix attribute has no namespace if(prefix === 'xml'){ a.uri = NAMESPACE.XML; }if(prefix !== 'xmlns'){ a.uri = currentNSMap[prefix || ''] //{console.log('###'+a.qName,domBuilder.locator.systemId+'',currentNSMap,a.uri)} } } } var nsp = tagName.indexOf(':'); if(nsp>0){ prefix = el.prefix = tagName.slice(0,nsp); localName = el.localName = tagName.slice(nsp+1); }else{ prefix = null;//important!! localName = el.localName = tagName; } //no prefix element has default namespace var ns = el.uri = currentNSMap[prefix || '']; domBuilder.startElement(ns,localName,tagName,el); //endPrefixMapping and startPrefixMapping have not any help for dom builder //localNSMap = null if(el.closed){ domBuilder.endElement(ns,localName,tagName); if(localNSMap){ for(prefix in localNSMap){ domBuilder.endPrefixMapping(prefix) } } }else{ el.currentNSMap = currentNSMap; el.localNSMap = localNSMap; //parseStack.push(el); return true; } } function parseHtmlSpecialContent(source,elStartEnd,tagName,entityReplacer,domBuilder){ if(/^(?:script|textarea)$/i.test(tagName)){ var elEndStart = source.indexOf('',elStartEnd); var text = source.substring(elStartEnd+1,elEndStart); if(/[&<]/.test(text)){ if(/^script$/i.test(tagName)){ //if(!/\]\]>/.test(text)){ //lexHandler.startCDATA(); domBuilder.characters(text,0,text.length); //lexHandler.endCDATA(); return elEndStart; //} }//}else{//text area text = text.replace(/&#?\w+;/g,entityReplacer); domBuilder.characters(text,0,text.length); return elEndStart; //} } } return elStartEnd+1; } function fixSelfClosed(source,elStartEnd,tagName,closeMap){ //if(tagName in closeMap){ var pos = closeMap[tagName]; if(pos == null){ //console.log(tagName) pos = source.lastIndexOf('') if(pos',start+4); //append comment source.substring(4,end)//', pos) if (endcomment !== -1) { pos = endcomment + 2 // target the '>' character } } else if (state === STATE_IGNORE_CDATA) { // if we're looping through a CDATA, fast-forward using // indexOf to the first end-CDATA character ]]> var endCDATA = data.indexOf(']]>', pos) if (endCDATA !== -1) { pos = endCDATA + 2 // target the '>' character } } 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_TEXT } 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 attrQuoteChar = c === 34 ? '"' : "'" 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 } } } inherits(SaxLtx, EventEmitter) SaxLtx.prototype.end = function (data) { if (data) { this.write(data) } /* Uh, yeah */ this.write = function () {} } },{"../escape":96,"events":80,"inherits":85}],100:[function(require,module,exports){ 'use strict' 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 += '' } else { s += '/>' } return s } module.exports = stringify },{}],101:[function(require,module,exports){ 'use strict' var tagString = require('./tagString') var parse = require('./parse') module.exports = function tag (/* [literals], ...substitutions */) { return parse(tagString.apply(null, arguments)) } },{"./parse":98,"./tagString":102}],102:[function(require,module,exports){ 'use strict' 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":96}],103:[function(require,module,exports){ 'use strict' var inherits = require('inherits') var HashBase = require('hash-base') var Buffer = require('safe-buffer').Buffer var ARRAY16 = new Array(16) function MD5 () { HashBase.call(this, 64) // state this._a = 0x67452301 this._b = 0xefcdab89 this._c = 0x98badcfe this._d = 0x10325476 } inherits(MD5, HashBase) MD5.prototype._update = function () { var M = ARRAY16 for (var i = 0; i < 16; ++i) M[i] = this._block.readInt32LE(i * 4) var a = this._a var b = this._b var c = this._c var d = this._d a = fnF(a, b, c, d, M[0], 0xd76aa478, 7) d = fnF(d, a, b, c, M[1], 0xe8c7b756, 12) c = fnF(c, d, a, b, M[2], 0x242070db, 17) b = fnF(b, c, d, a, M[3], 0xc1bdceee, 22) a = fnF(a, b, c, d, M[4], 0xf57c0faf, 7) d = fnF(d, a, b, c, M[5], 0x4787c62a, 12) c = fnF(c, d, a, b, M[6], 0xa8304613, 17) b = fnF(b, c, d, a, M[7], 0xfd469501, 22) a = fnF(a, b, c, d, M[8], 0x698098d8, 7) d = fnF(d, a, b, c, M[9], 0x8b44f7af, 12) c = fnF(c, d, a, b, M[10], 0xffff5bb1, 17) b = fnF(b, c, d, a, M[11], 0x895cd7be, 22) a = fnF(a, b, c, d, M[12], 0x6b901122, 7) d = fnF(d, a, b, c, M[13], 0xfd987193, 12) c = fnF(c, d, a, b, M[14], 0xa679438e, 17) b = fnF(b, c, d, a, M[15], 0x49b40821, 22) a = fnG(a, b, c, d, M[1], 0xf61e2562, 5) d = fnG(d, a, b, c, M[6], 0xc040b340, 9) c = fnG(c, d, a, b, M[11], 0x265e5a51, 14) b = fnG(b, c, d, a, M[0], 0xe9b6c7aa, 20) a = fnG(a, b, c, d, M[5], 0xd62f105d, 5) d = fnG(d, a, b, c, M[10], 0x02441453, 9) c = fnG(c, d, a, b, M[15], 0xd8a1e681, 14) b = fnG(b, c, d, a, M[4], 0xe7d3fbc8, 20) a = fnG(a, b, c, d, M[9], 0x21e1cde6, 5) d = fnG(d, a, b, c, M[14], 0xc33707d6, 9) c = fnG(c, d, a, b, M[3], 0xf4d50d87, 14) b = fnG(b, c, d, a, M[8], 0x455a14ed, 20) a = fnG(a, b, c, d, M[13], 0xa9e3e905, 5) d = fnG(d, a, b, c, M[2], 0xfcefa3f8, 9) c = fnG(c, d, a, b, M[7], 0x676f02d9, 14) b = fnG(b, c, d, a, M[12], 0x8d2a4c8a, 20) a = fnH(a, b, c, d, M[5], 0xfffa3942, 4) d = fnH(d, a, b, c, M[8], 0x8771f681, 11) c = fnH(c, d, a, b, M[11], 0x6d9d6122, 16) b = fnH(b, c, d, a, M[14], 0xfde5380c, 23) a = fnH(a, b, c, d, M[1], 0xa4beea44, 4) d = fnH(d, a, b, c, M[4], 0x4bdecfa9, 11) c = fnH(c, d, a, b, M[7], 0xf6bb4b60, 16) b = fnH(b, c, d, a, M[10], 0xbebfbc70, 23) a = fnH(a, b, c, d, M[13], 0x289b7ec6, 4) d = fnH(d, a, b, c, M[0], 0xeaa127fa, 11) c = fnH(c, d, a, b, M[3], 0xd4ef3085, 16) b = fnH(b, c, d, a, M[6], 0x04881d05, 23) a = fnH(a, b, c, d, M[9], 0xd9d4d039, 4) d = fnH(d, a, b, c, M[12], 0xe6db99e5, 11) c = fnH(c, d, a, b, M[15], 0x1fa27cf8, 16) b = fnH(b, c, d, a, M[2], 0xc4ac5665, 23) a = fnI(a, b, c, d, M[0], 0xf4292244, 6) d = fnI(d, a, b, c, M[7], 0x432aff97, 10) c = fnI(c, d, a, b, M[14], 0xab9423a7, 15) b = fnI(b, c, d, a, M[5], 0xfc93a039, 21) a = fnI(a, b, c, d, M[12], 0x655b59c3, 6) d = fnI(d, a, b, c, M[3], 0x8f0ccc92, 10) c = fnI(c, d, a, b, M[10], 0xffeff47d, 15) b = fnI(b, c, d, a, M[1], 0x85845dd1, 21) a = fnI(a, b, c, d, M[8], 0x6fa87e4f, 6) d = fnI(d, a, b, c, M[15], 0xfe2ce6e0, 10) c = fnI(c, d, a, b, M[6], 0xa3014314, 15) b = fnI(b, c, d, a, M[13], 0x4e0811a1, 21) a = fnI(a, b, c, d, M[4], 0xf7537e82, 6) d = fnI(d, a, b, c, M[11], 0xbd3af235, 10) c = fnI(c, d, a, b, M[2], 0x2ad7d2bb, 15) b = fnI(b, c, d, a, M[9], 0xeb86d391, 21) this._a = (this._a + a) | 0 this._b = (this._b + b) | 0 this._c = (this._c + c) | 0 this._d = (this._d + d) | 0 } MD5.prototype._digest = function () { // create padding and handle blocks this._block[this._blockOffset++] = 0x80 if (this._blockOffset > 56) { this._block.fill(0, this._blockOffset, 64) this._update() this._blockOffset = 0 } this._block.fill(0, this._blockOffset, 56) this._block.writeUInt32LE(this._length[0], 56) this._block.writeUInt32LE(this._length[1], 60) this._update() // produce result var buffer = Buffer.allocUnsafe(16) buffer.writeInt32LE(this._a, 0) buffer.writeInt32LE(this._b, 4) buffer.writeInt32LE(this._c, 8) buffer.writeInt32LE(this._d, 12) return buffer } function rotl (x, n) { return (x << n) | (x >>> (32 - n)) } function fnF (a, b, c, d, m, k, s) { return (rotl((a + ((b & c) | ((~b) & d)) + m + k) | 0, s) + b) | 0 } function fnG (a, b, c, d, m, k, s) { return (rotl((a + ((b & d) | (c & (~d))) + m + k) | 0, s) + b) | 0 } function fnH (a, b, c, d, m, k, s) { return (rotl((a + (b ^ c ^ d) + m + k) | 0, s) + b) | 0 } function fnI (a, b, c, d, m, k, s) { return (rotl((a + ((c ^ (b | (~d)))) + m + k) | 0, s) + b) | 0 } module.exports = MD5 },{"hash-base":82,"inherits":85,"safe-buffer":173}],104:[function(require,module,exports){ /** * Helpers. */ var s = 1000; var m = s * 60; var h = m * 60; var d = h * 24; var y = d * 365.25; /** * Parse or format the given `val`. * * Options: * * - `long` verbose formatting [false] * * @param {String|Number} val * @param {Object} [options] * @throws {Error} throw an error if val is not a non-empty string or a number * @return {String|Number} * @api public */ module.exports = function(val, options) { options = options || {}; var type = typeof val; if (type === 'string' && val.length > 0) { return parse(val); } else if (type === 'number' && isNaN(val) === false) { return options.long ? fmtLong(val) : fmtShort(val); } throw new Error( 'val is not a non-empty string or a valid number. val=' + JSON.stringify(val) ); }; /** * Parse the given `str` and return milliseconds. * * @param {String} str * @return {Number} * @api private */ function parse(str) { str = String(str); if (str.length > 100) { return; } var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec( str ); if (!match) { return; } var n = parseFloat(match[1]); var type = (match[2] || 'ms').toLowerCase(); switch (type) { case 'years': case 'year': case 'yrs': case 'yr': case 'y': return n * y; case 'days': case 'day': case 'd': return n * d; case 'hours': case 'hour': case 'hrs': case 'hr': case 'h': return n * h; case 'minutes': case 'minute': case 'mins': case 'min': case 'm': return n * m; case 'seconds': case 'second': case 'secs': case 'sec': case 's': return n * s; case 'milliseconds': case 'millisecond': case 'msecs': case 'msec': case 'ms': return n; default: return undefined; } } /** * Short format for `ms`. * * @param {Number} ms * @return {String} * @api private */ function fmtShort(ms) { if (ms >= d) { return Math.round(ms / d) + 'd'; } if (ms >= h) { return Math.round(ms / h) + 'h'; } if (ms >= m) { return Math.round(ms / m) + 'm'; } if (ms >= s) { return Math.round(ms / s) + 's'; } return ms + 'ms'; } /** * Long format for `ms`. * * @param {Number} ms * @return {String} * @api private */ function fmtLong(ms) { return plural(ms, d, 'day') || plural(ms, h, 'hour') || plural(ms, m, 'minute') || plural(ms, s, 'second') || ms + ' ms'; } /** * Pluralization helper. */ function plural(ms, n, name) { if (ms < n) { return; } if (ms < n * 1.5) { return Math.floor(ms / n) + ' ' + name; } return Math.ceil(ms / n) + ' ' + name + 's'; } },{}],105:[function(require,module,exports){ (function (global,Buffer){(function (){ /** * index.js * * a request API compatible with window.fetch */ var parse_url = require('url').parse; var resolve_url = require('url').resolve; var http = require('http'); var https = require('https'); var zlib = require('zlib'); var stream = require('stream'); var Body = require('./lib/body'); var Response = require('./lib/response'); var Headers = require('./lib/headers'); var Request = require('./lib/request'); var FetchError = require('./lib/fetch-error'); // commonjs module.exports = Fetch; // es6 default export compatibility module.exports.default = module.exports; /** * Fetch class * * @param Mixed url Absolute url or Request instance * @param Object opts Fetch options * @return Promise */ function Fetch(url, opts) { // allow call as function if (!(this instanceof Fetch)) return new Fetch(url, opts); // allow custom promise if (!Fetch.Promise) { throw new Error('native promise missing, set Fetch.Promise to your favorite alternative'); } Body.Promise = Fetch.Promise; var self = this; // wrap http.request into fetch return new Fetch.Promise(function(resolve, reject) { // build request object var options = new Request(url, opts); if (!options.protocol || !options.hostname) { throw new Error('only absolute urls are supported'); } if (options.protocol !== 'http:' && options.protocol !== 'https:') { throw new Error('only http(s) protocols are supported'); } var send; if (options.protocol === 'https:') { send = https.request; } else { send = http.request; } // normalize headers var headers = new Headers(options.headers); if (options.compress) { headers.set('accept-encoding', 'gzip,deflate'); } if (!headers.has('user-agent')) { headers.set('user-agent', 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)'); } if (!headers.has('connection') && !options.agent) { headers.set('connection', 'close'); } if (!headers.has('accept')) { headers.set('accept', '*/*'); } // detect form data input from form-data module, this hack avoid the need to pass multipart header manually if (!headers.has('content-type') && options.body && typeof options.body.getBoundary === 'function') { headers.set('content-type', 'multipart/form-data; boundary=' + options.body.getBoundary()); } // bring node-fetch closer to browser behavior by setting content-length automatically if (!headers.has('content-length') && /post|put|patch|delete/i.test(options.method)) { if (typeof options.body === 'string') { headers.set('content-length', Buffer.byteLength(options.body)); // detect form data input from form-data module, this hack avoid the need to add content-length header manually } else if (options.body && typeof options.body.getLengthSync === 'function') { // for form-data 1.x if (options.body._lengthRetrievers && options.body._lengthRetrievers.length == 0) { headers.set('content-length', options.body.getLengthSync().toString()); // for form-data 2.x } else if (options.body.hasKnownLength && options.body.hasKnownLength()) { headers.set('content-length', options.body.getLengthSync().toString()); } // this is only necessary for older nodejs releases (before iojs merge) } else if (options.body === undefined || options.body === null) { headers.set('content-length', '0'); } } options.headers = headers.raw(); // http.request only support string as host header, this hack make custom host header possible if (options.headers.host) { options.headers.host = options.headers.host[0]; } // send request var req = send(options); var reqTimeout; if (options.timeout) { req.once('socket', function(socket) { reqTimeout = setTimeout(function() { req.abort(); reject(new FetchError('network timeout at: ' + options.url, 'request-timeout')); }, options.timeout); }); } req.on('error', function(err) { clearTimeout(reqTimeout); reject(new FetchError('request to ' + options.url + ' failed, reason: ' + err.message, 'system', err)); }); req.on('response', function(res) { clearTimeout(reqTimeout); // handle redirect if (self.isRedirect(res.statusCode) && options.redirect !== 'manual') { if (options.redirect === 'error') { reject(new FetchError('redirect mode is set to error: ' + options.url, 'no-redirect')); return; } if (options.counter >= options.follow) { reject(new FetchError('maximum redirect reached at: ' + options.url, 'max-redirect')); return; } if (!res.headers.location) { reject(new FetchError('redirect location header missing at: ' + options.url, 'invalid-redirect')); return; } // per fetch spec, for POST request with 301/302 response, or any request with 303 response, use GET when following redirect if (res.statusCode === 303 || ((res.statusCode === 301 || res.statusCode === 302) && options.method === 'POST')) { options.method = 'GET'; delete options.body; delete options.headers['content-length']; } options.counter++; resolve(Fetch(resolve_url(options.url, res.headers.location), options)); return; } // normalize location header for manual redirect mode var headers = new Headers(res.headers); if (options.redirect === 'manual' && headers.has('location')) { headers.set('location', resolve_url(options.url, headers.get('location'))); } // prepare response var body = res.pipe(new stream.PassThrough()); var response_options = { url: options.url , status: res.statusCode , statusText: res.statusMessage , headers: headers , size: options.size , timeout: options.timeout }; // response object var output; // in following scenarios we ignore compression support // 1. compression support is disabled // 2. HEAD request // 3. no content-encoding header // 4. no content response (204) // 5. content not modified response (304) if (!options.compress || options.method === 'HEAD' || !headers.has('content-encoding') || res.statusCode === 204 || res.statusCode === 304) { output = new Response(body, response_options); resolve(output); return; } // otherwise, check for gzip or deflate var name = headers.get('content-encoding'); // for gzip if (name == 'gzip' || name == 'x-gzip') { body = body.pipe(zlib.createGunzip()); output = new Response(body, response_options); resolve(output); return; // for deflate } else if (name == 'deflate' || name == 'x-deflate') { // handle the infamous raw deflate response from old servers // a hack for old IIS and Apache servers var raw = res.pipe(new stream.PassThrough()); raw.once('data', function(chunk) { // see http://stackoverflow.com/questions/37519828 if ((chunk[0] & 0x0F) === 0x08) { body = body.pipe(zlib.createInflate()); } else { body = body.pipe(zlib.createInflateRaw()); } output = new Response(body, response_options); resolve(output); }); return; } // otherwise, use response as-is output = new Response(body, response_options); resolve(output); return; }); // accept string, buffer or readable stream as body // per spec we will call tostring on non-stream objects if (typeof options.body === 'string') { req.write(options.body); req.end(); } else if (options.body instanceof Buffer) { req.write(options.body); req.end(); } else if (typeof options.body === 'object' && options.body.pipe) { options.body.pipe(req); } else if (typeof options.body === 'object') { req.write(options.body.toString()); req.end(); } else { req.end(); } }); }; /** * Redirect code matching * * @param Number code Status code * @return Boolean */ Fetch.prototype.isRedirect = function(code) { return code === 301 || code === 302 || code === 303 || code === 307 || code === 308; } // expose Promise Fetch.Promise = global.Promise; Fetch.Response = Response; Fetch.Headers = Headers; Fetch.Request = Request; }).call(this)}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {},require("buffer").Buffer) },{"./lib/body":106,"./lib/fetch-error":107,"./lib/headers":108,"./lib/request":109,"./lib/response":110,"buffer":47,"http":192,"https":83,"stream":176,"url":212,"zlib":31}],106:[function(require,module,exports){ (function (global,Buffer){(function (){ /** * body.js * * Body interface provides common methods for Request and Response */ var convert = require('encoding').convert; var bodyStream = require('is-stream'); var PassThrough = require('stream').PassThrough; var FetchError = require('./fetch-error'); module.exports = Body; /** * Body class * * @param Stream body Readable stream * @param Object opts Response options * @return Void */ function Body(body, opts) { opts = opts || {}; this.body = body; this.bodyUsed = false; this.size = opts.size || 0; this.timeout = opts.timeout || 0; this._raw = []; this._abort = false; } /** * Decode response as json * * @return Promise */ Body.prototype.json = function() { var self = this; return this._decode().then(function(buffer) { try { return JSON.parse(buffer.toString()); } catch (err) { return Body.Promise.reject(new FetchError('invalid json response body at ' + self.url + ' reason: ' + err.message, 'invalid-json')); } }); }; /** * Decode response as text * * @return Promise */ Body.prototype.text = function() { return this._decode().then(function(buffer) { return buffer.toString(); }); }; /** * Decode response as buffer (non-spec api) * * @return Promise */ Body.prototype.buffer = function() { return this._decode(); }; /** * Decode buffers into utf-8 string * * @return Promise */ Body.prototype._decode = function() { var self = this; if (this.bodyUsed) { return Body.Promise.reject(new Error('body used already for: ' + this.url)); } this.bodyUsed = true; this._bytes = 0; this._abort = false; this._raw = []; return new Body.Promise(function(resolve, reject) { var resTimeout; // body is string if (typeof self.body === 'string') { self._bytes = self.body.length; self._raw = [new Buffer(self.body)]; return resolve(self._convert()); } // body is buffer if (self.body instanceof Buffer) { self._bytes = self.body.length; self._raw = [self.body]; return resolve(self._convert()); } // allow timeout on slow response body if (self.timeout) { resTimeout = setTimeout(function() { self._abort = true; reject(new FetchError('response timeout at ' + self.url + ' over limit: ' + self.timeout, 'body-timeout')); }, self.timeout); } // handle stream error, such as incorrect content-encoding self.body.on('error', function(err) { reject(new FetchError('invalid response body at: ' + self.url + ' reason: ' + err.message, 'system', err)); }); // body is stream self.body.on('data', function(chunk) { if (self._abort || chunk === null) { return; } if (self.size && self._bytes + chunk.length > self.size) { self._abort = true; reject(new FetchError('content size at ' + self.url + ' over limit: ' + self.size, 'max-size')); return; } self._bytes += chunk.length; self._raw.push(chunk); }); self.body.on('end', function() { if (self._abort) { return; } clearTimeout(resTimeout); resolve(self._convert()); }); }); }; /** * Detect buffer encoding and convert to target encoding * ref: http://www.w3.org/TR/2011/WD-html5-20110113/parsing.html#determining-the-character-encoding * * @param String encoding Target encoding * @return String */ Body.prototype._convert = function(encoding) { encoding = encoding || 'utf-8'; var ct = this.headers.get('content-type'); var charset = 'utf-8'; var res, str; // header if (ct) { // skip encoding detection altogether if not html/xml/plain text if (!/text\/html|text\/plain|\+xml|\/xml/i.test(ct)) { return Buffer.concat(this._raw); } res = /charset=([^;]*)/i.exec(ct); } // no charset in content type, peek at response body for at most 1024 bytes if (!res && this._raw.length > 0) { for (var i = 0; i < this._raw.length; i++) { str += this._raw[i].toString() if (str.length > 1024) { break; } } str = str.substr(0, 1024); } // html5 if (!res && str) { res = /= 200 && this.status < 300; Body.call(this, body, opts); } Response.prototype = Object.create(Body.prototype); /** * Clone this response * * @return Response */ Response.prototype.clone = function() { return new Response(this._clone(this), { url: this.url , status: this.status , statusText: this.statusText , headers: this.headers , ok: this.ok }); }; },{"./body":106,"./headers":108,"http":192}],111:[function(require,module,exports){ 'use strict' var Client = require('./lib/Client') var SASL = require('./lib/sasl') var core = require('node-xmpp-core') module.exports = Client module.exports.Client = Client module.exports.SASL = SASL core.exportCoreUtils(module.exports) },{"./lib/Client":112,"./lib/sasl":120,"node-xmpp-core":123}],112:[function(require,module,exports){ (function (__dirname){(function (){ 'use strict' var Session = require('./session') var core = require('node-xmpp-core') var JID = core.JID var Stanza = core.Stanza var Element = core.Element var inherits = core.inherits var sasl = require('./sasl') var Anonymous = require('./authentication/anonymous') var Plain = require('./authentication/plain') var DigestMD5 = require('./authentication/digestmd5') var XOAuth2 = require('./authentication/xoauth2') var External = require('./authentication/external') var exec = require('child_process').exec var debug = require('debug')('xmpp:client') var path = require('path') var NS_CLIENT = 'jabber:client' var NS_REGISTER = 'jabber:iq:register' var NS_XMPP_SASL = 'urn:ietf:params:xml:ns:xmpp-sasl' var NS_XMPP_BIND = 'urn:ietf:params:xml:ns:xmpp-bind' var NS_XMPP_SESSION = 'urn:ietf:params:xml:ns:xmpp-session' var STATE_PREAUTH = 0 var STATE_AUTH = 1 var STATE_AUTHED = 2 var STATE_BIND = 3 var STATE_SESSION = 4 var STATE_ONLINE = 5 var IQID_SESSION = 'sess' var IQID_BIND = 'bind' var decode64, encode64, Buffer if (typeof btoa === 'undefined') { var btoa = null var atob = null } if (typeof btoa === 'function') { decode64 = function (encoded) { return atob(encoded) } } else { Buffer = require('buffer').Buffer decode64 = function (encoded) { return (new Buffer(encoded, 'base64')).toString('utf8') } } if (typeof atob === 'function') { encode64 = function (decoded) { return btoa(decoded) } } else { Buffer = require('buffer').Buffer encode64 = function (decoded) { return (new Buffer(decoded, 'utf8')).toString('base64') } } /** * params object: * jid: String (required) * password: String (required) * host: String (optional) * port: Number (optional) * reconnect: Boolean (optional) * autostart: Boolean (optional) - if we start connecting to a given port * register: Boolean (option) - register account before authentication * legacySSL: Boolean (optional) - connect to the legacy SSL port, requires at least the host to be specified * credentials: Dictionary (optional) - TLS or SSL key and certificate credentials * actAs: String (optional) - if admin user act on behalf of another user (just user) * disallowTLS: Boolean (optional) - prevent upgrading the connection to a secure one via TLS * preferred: String (optional) - Preferred SASL mechanism to use * bosh.url: String (optional) - BOSH endpoint to use * bosh.prebind: Function(error, data) (optional) - Just prebind a new BOSH session for browser client use * error String - Result of XMPP error. Ex : [Error: XMPP authentication failure] * data Object - Result of XMPP BOSH connection. * * Examples: * var cl = new xmpp.Client({ * jid: "me@example.com", * password: "secret" * }) * var gtalk = new xmpp.Client({ * jid: 'me@gmail.com', * oauth2_token: 'xxxx.xxxxxxxxxxx', // from OAuth2 * oauth2_auth: 'http://www.google.com/talk/protocol/auth', * host: 'talk.google.com' * }) * var prebind = new xmpp.Client({ * jid: "me@example.com", * password: "secret", * bosh: { * url: "http://example.com/http-bind", * prebind: function(error, data) { * if (error) {} * res.send({ rid: data.rid, sid: data.sid }) * } * } * }) * * Example SASL EXTERNAL: * * var myCredentials = { * // These are necessary only if using the client certificate authentication * key: fs.readFileSync('key.pem'), * cert: fs.readFileSync('cert.pem'), * // passphrase: 'optional' * } * var cl = new xmppClient({ * jid: "me@example.com", * credentials: myCredentials * preferred: 'EXTERNAL' // not really required, but possible * }) * */ function Client (options) { this.options = {} if (options) this.options = options this.availableSaslMechanisms = [ XOAuth2, External, DigestMD5, Plain, Anonymous ] if (this.options.autostart !== false) this.connect() } inherits(Client, Session) Client.NS_CLIENT = NS_CLIENT Client.prototype.connect = function () { if (this.options.bosh && this.options.bosh.prebind) { return this._connectViaBosh() } this._useStandardConnect() } Client.prototype._useStandardConnect = function () { this.options.xmlns = NS_CLIENT delete this.did_bind delete this.did_session this.state = STATE_PREAUTH this.on('end', function () { this.state = STATE_PREAUTH delete this.did_bind delete this.did_session }) Session.call(this, this.options) this.options.jid = this.jid this.connection.on('disconnect', function (error) { this.state = STATE_PREAUTH if (!this.connection.reconnect) { if (error) this.emit('error', error) this.emit('offline') } delete this.did_bind delete this.did_session }.bind(this)) /* If server and client have multiple possible auth mechanisms * we try to select the preferred one */ if (this.options.preferred) { this.preferredSaslMechanism = this.options.preferred } else { this.preferredSaslMechanism = 'DIGEST-MD5' } var mechs = sasl.detectMechanisms(this.options, this.availableSaslMechanisms) this.availableSaslMechanisms = mechs } Client.prototype._connectViaBosh = function () { debug('load bosh prebind') var cb = this.options.bosh.prebind delete this.options.bosh.prebind var cmd = 'node ' + path.join(__dirname, 'prebind.js') + ' ' + encodeURI(JSON.stringify(this.options)) exec( cmd, function (error, stdout, stderr) { if (error) { cb(error, null) } else { var r = stdout.match(/rid:+[ 0-9]*/i) var s = stdout.match(/sid:+[ a-z+'"-_A-Z+0-9]*/i) if (!r || !s) { return cb(stderr) } r = (r[0].split(':'))[1].trim() s = (s[0].split(':'))[1] .replace("'", '') .replace("'", '') .trim() if (r && s) { return cb(null, { rid: r, sid: s }) } cb(stderr) } } ) } Client.prototype.onStanza = function (stanza) { /* Actually, we shouldn't wait for if * this.streamAttrs.version is missing, but who uses pre-XMPP-1.0 * these days anyway? */ if (stanza.name === 'stream:error') { return this._handleStreamError(stanza) } if ((this.state !== STATE_ONLINE) && stanza.is('features')) { this.streamFeatures = stanza return this.useFeatures() } this._handleStanza(stanza) } Client.prototype._handleStanza = function (stanza) { switch (this.state) { case STATE_ONLINE: this.emit('stanza', stanza) break case STATE_PREAUTH: this.emit('stanza:preauth', stanza) break case STATE_AUTH: this._handleAuthState(stanza) break case STATE_BIND: if (stanza.is('iq') && (stanza.attrs.id === IQID_BIND)) { this._handleBindState(stanza) } break case STATE_SESSION: if ((stanza.is('iq') === true) && (stanza.attrs.id === IQID_SESSION)) { this._handleSessionState(stanza) } break } } Client.prototype._handleStreamError = function (stanza) { if (!this.reconnect) { this.emit('error', stanza) } } Client.prototype._handleSessionState = function (stanza) { if (stanza.attrs.type === 'result') { this.state = STATE_AUTHED this.did_session = true /* no stream restart, but next feature (most probably we'll go online next) */ this.useFeatures() } else { this.emit('error', 'Cannot bind resource') } } Client.prototype._handleBindState = function (stanza) { if (stanza.attrs.type === 'result') { this.state = STATE_AUTHED this.did_bind = true var bindEl = stanza.getChild('bind', NS_XMPP_BIND) if (bindEl && bindEl.getChild('jid')) { this.jid = new JID(bindEl.getChild('jid').getText()) } /* no stream restart, but next feature */ this.useFeatures() } else { this.emit('error', 'Cannot bind resource') } } Client.prototype._handleAuthState = function (stanza) { if (stanza.is('challenge', NS_XMPP_SASL)) { var challengeMsg = decode64(stanza.getText()) var responseMsg = encode64(this.mech.challenge(challengeMsg)) var response = new Element('response', {xmlns: NS_XMPP_SASL}).t(responseMsg) this.send(response) } else if (stanza.is('success', NS_XMPP_SASL)) { this.mech = null this.state = STATE_AUTHED this.emit('auth') } else { this.emit('error', 'XMPP authentication failure') } } Client.prototype._handlePreAuthState = function () { this.state = STATE_AUTH var offeredMechs = this.streamFeatures.getChild('mechanisms', NS_XMPP_SASL).getChildren('mechanism', NS_XMPP_SASL).map(function (el) { return el.getText() }) this.mech = sasl.selectMechanism( offeredMechs, this.preferredSaslMechanism, this.availableSaslMechanisms ) if (this.mech) { this.mech.authzid = this.jid.bare().toString() this.mech.authcid = this.jid.local this.mech.password = this.password this.mech.api_key = this.api_key this.mech.access_token = this.access_token this.mech.oauth2_token = this.oauth2_token this.mech.oauth2_auth = this.oauth2_auth this.mech.realm = this.jid.domain // anything? if (this.actAs) this.mech.actAs = this.actAs.user this.mech.digest_uri = 'xmpp/' + this.jid.domain var authMsg = encode64(this.mech.auth()) var attrs = this.mech.authAttrs() attrs.xmlns = NS_XMPP_SASL attrs.mechanism = this.mech.name this.send(new Element('auth', attrs).t(authMsg)) } else { this.emit('error', new Error('No usable SASL mechanism')) } } /** * Either we just received , or we just enabled a * feature and are looking for the next. */ Client.prototype.useFeatures = function () { if ((this.state === STATE_PREAUTH) && this.register) { delete this.register this.doRegister() } else if ((this.state === STATE_PREAUTH) && this.streamFeatures.getChild('mechanisms', NS_XMPP_SASL)) { this._handlePreAuthState() } else if ((this.state === STATE_AUTHED) && !this.did_bind && this.streamFeatures.getChild('bind', NS_XMPP_BIND)) { this.state = STATE_BIND var bindEl = new Stanza('iq', { type: 'set', id: IQID_BIND }).c('bind', { xmlns: NS_XMPP_BIND }) if (this.jid.resource) { bindEl.c('resource').t(this.jid.resource) } this.send(bindEl) } else if ((this.state === STATE_AUTHED) && !this.did_session && this.streamFeatures.getChild('session', NS_XMPP_SESSION)) { this.state = STATE_SESSION var stanza = new Stanza('iq', { type: 'set', to: this.jid.domain, id: IQID_SESSION }).c('session', { xmlns: NS_XMPP_SESSION }) this.send(stanza) } else if (this.state === STATE_AUTHED) { /* Ok, we're authenticated and all features have been processed */ this.state = STATE_ONLINE this.emit('online', { jid: this.jid }) } } Client.prototype.doRegister = function () { var id = 'register' + Math.ceil(Math.random() * 99999) var iq = new Stanza('iq', { type: 'set', id: id, to: this.jid.domain }).c('query', {xmlns: NS_REGISTER}) .c('username').t(this.jid.local).up() .c('password').t(this.password) this.send(iq) var self = this var onReply = function (reply) { if (reply.is('iq') && (reply.attrs.id === id)) { self.removeListener('stanza', onReply) if (reply.attrs.type === 'result') { /* Registration successful, proceed to auth */ self.useFeatures() } else { self.emit('error', new Error('Registration error')) } } } this.on('stanza:preauth', onReply) } /** * returns all registered sasl mechanisms */ Client.prototype.getSaslMechanisms = function () { return this.availableSaslMechanisms } /** * removes all registered sasl mechanisms */ Client.prototype.clearSaslMechanism = function () { this.availableSaslMechanisms = [] } /** * register a new sasl mechanism */ Client.prototype.registerSaslMechanism = function (method) { // check if method is registered if (this.availableSaslMechanisms.indexOf(method) === -1) { this.availableSaslMechanisms.push(method) } } /** * unregister an existing sasl mechanism */ Client.prototype.unregisterSaslMechanism = function (method) { // check if method is registered var index = this.availableSaslMechanisms.indexOf(method) if (index >= 0) { this.availableSaslMechanisms = this.availableSaslMechanisms.splice(index, 1) } } module.exports = Client }).call(this)}).call(this,"/node_modules/node-xmpp-client/lib") },{"./authentication/anonymous":113,"./authentication/digestmd5":114,"./authentication/external":115,"./authentication/plain":117,"./authentication/xoauth2":118,"./sasl":120,"./session":121,"buffer":47,"child_process":32,"debug":56,"node-xmpp-core":123,"path":29}],113:[function(require,module,exports){ 'use strict' var Mechanism = require('./mechanism') var inherits = require('node-xmpp-core').inherits /** * @see http://tools.ietf.org/html/rfc4505 * @see http://xmpp.org/extensions/xep-0175.html */ function Anonymous () {} inherits(Anonymous, Mechanism) Anonymous.prototype.name = 'ANONYMOUS' Anonymous.prototype.auth = function () { return this.authzid } Anonymous.prototype.match = function () { return true } module.exports = Anonymous },{"./mechanism":116,"node-xmpp-core":123}],114:[function(require,module,exports){ 'use strict' var inherits = require('node-xmpp-core').inherits var Mechanism = require('./mechanism') var crypto = require('crypto') var MD5 = require('md5.js') /** * Hash a string */ function md5 (s, encoding) { // we ignore crypto in the browser field of package.json var hash = crypto.createHash ? crypto.createHash('md5') : new MD5() return hash.update(s, 'binary').digest(encoding || 'binary') } function md5Hex (s) { return md5(s, 'hex') } /** * Parse SASL serialization */ function parseDict (s) { var result = {} while (s) { var m if ((m = /^(.+?)=(.*?[^\\]),\s*(.*)/.exec(s))) { result[m[1]] = m[2].replace(/"/g, '') s = m[3] } else if ((m = /^(.+?)=(.+?),\s*(.*)/.exec(s))) { result[m[1]] = m[2] s = m[3] } else if ((m = /^(.+?)="(.*?[^\\])"$/.exec(s))) { result[m[1]] = m[2] s = m[3] } else if ((m = /^(.+?)=(.+?)$/.exec(s))) { result[m[1]] = m[2] s = m[3] } else { s = null } } return result } /** * SASL serialization */ function encodeDict (dict) { var s = '' for (var k in dict) { var v = dict[k] if (v) s += ',' + k + '="' + v + '"' } return s.substr(1) // without first ',' } /** * Right-justify a string, * eg. pad with 0s */ function rjust (s, targetLen, padding) { while (s.length < targetLen) { s = padding + s } return s } /** * Generate a string of 8 digits * (number used once) */ function generateNonce () { var result = '' for (var i = 0; i < 8; i++) { result += String.fromCharCode(48 + Math.ceil(Math.random() * 10)) } return result } /** * @see http://tools.ietf.org/html/rfc2831 * @see http://wiki.xmpp.org/web/SASLandDIGEST-MD5 */ function DigestMD5 () { this.nonce_count = 0 this.cnonce = generateNonce() this.authcid = null this.actAs = null this.realm = null this.password = null } inherits(DigestMD5, Mechanism) DigestMD5.prototype.name = 'DIGEST-MD5' DigestMD5.prototype.auth = function () { return '' } DigestMD5.prototype.getNC = function () { return rjust(this.nonce_count.toString(), 8, '0') } DigestMD5.prototype.responseValue = function (s) { var dict = parseDict(s) if (dict.realm) { this.realm = dict.realm } var value if (dict.nonce && dict.qop) { this.nonce_count++ var a1 = md5(this.authcid + ':' + this.realm + ':' + this.password) + ':' + dict.nonce + ':' + this.cnonce if (this.actAs) a1 += ':' + this.actAs var a2 = 'AUTHENTICATE:' + this.digest_uri if ((dict.qop === 'auth-int') || (dict.qop === 'auth-conf')) { a2 += ':00000000000000000000000000000000' } value = md5Hex(md5Hex(a1) + ':' + dict.nonce + ':' + this.getNC() + ':' + this.cnonce + ':' + dict.qop + ':' + md5Hex(a2)) } return value } DigestMD5.prototype.challenge = function (s) { var dict = parseDict(s) if (dict.realm) { this.realm = dict.realm } var response if (dict.nonce && dict.qop) { var responseValue = this.responseValue(s) response = { username: this.authcid, realm: this.realm, nonce: dict.nonce, cnonce: this.cnonce, nc: this.getNC(), qop: dict.qop, 'digest-uri': this.digest_uri, response: responseValue, charset: 'utf-8' } if (this.actAs) response.authzid = this.actAs } else if (dict.rspauth) { return '' } return encodeDict(response) } DigestMD5.prototype.serverChallenge = function () { var dict = {} dict.realm = '' this.nonce = dict.nonce = generateNonce() dict.qop = 'auth' this.charset = dict.charset = 'utf-8' dict.algorithm = 'md5-sess' return encodeDict(dict) } // Used on the server to check for auth! DigestMD5.prototype.response = function (s) { var dict = parseDict(s) this.authcid = dict.username if (dict.nonce !== this.nonce) return false if (!dict.cnonce) return false this.cnonce = dict.cnonce if (this.charset !== dict.charset) return false this.response = dict.response return true } DigestMD5.prototype.match = function (options) { if (options.password) return true return false } module.exports = DigestMD5 },{"./mechanism":116,"crypto":29,"md5.js":103,"node-xmpp-core":123}],115:[function(require,module,exports){ 'use strict' var inherits = require('node-xmpp-core').inherits var Mechanism = require('./mechanism') /** * @see http://xmpp.org/extensions/xep-0178.html */ function External () {} inherits(External, Mechanism) External.prototype.name = 'EXTERNAL' External.prototype.auth = function () { return (this.authzid) } External.prototype.match = function (options) { if (options.credentials) return true return false } module.exports = External },{"./mechanism":116,"node-xmpp-core":123}],116:[function(require,module,exports){ 'use strict' /** * Each implemented mechanism offers multiple methods * - name : name of the auth method * - auth : * - match: checks if the client has enough options to * offer this mechanis to xmpp servers * - authServer: takes a stanza and extracts the information */ var inherits = require('node-xmpp-core').inherits var EventEmitter = require('events').EventEmitter // Mechanisms function Mechanism () {} inherits(Mechanism, EventEmitter) Mechanism.prototype.authAttrs = function () { return {} } module.exports = Mechanism },{"events":80,"node-xmpp-core":123}],117:[function(require,module,exports){ 'use strict' var inherits = require('node-xmpp-core').inherits var Mechanism = require('./mechanism') function Plain () {} inherits(Plain, Mechanism) Plain.prototype.name = 'PLAIN' Plain.prototype.auth = function () { return this.authzid + '\0' + this.authcid + '\0' + this.password } Plain.prototype.match = function (options) { if (options.password) return true return false } module.exports = Plain },{"./mechanism":116,"node-xmpp-core":123}],118:[function(require,module,exports){ 'use strict' var inherits = require('node-xmpp-core').inherits var Mechanism = require('./mechanism') /** * @see https://developers.google.com/talk/jep_extensions/oauth */ function XOAuth2 () { this.oauth2_auth = null this.authzid = null } inherits(XOAuth2, Mechanism) XOAuth2.prototype.name = 'X-OAUTH2' XOAuth2.prototype.NS_GOOGLE_AUTH = 'http://www.google.com/talk/protocol/auth' XOAuth2.prototype.auth = function () { return '\0' + this.authzid + '\0' + this.oauth2_token } XOAuth2.prototype.authAttrs = function () { return { 'auth:service': 'oauth2', 'xmlns:auth': this.oauth2_auth } } XOAuth2.prototype.match = function (options) { return (options.oauth2_auth === XOAuth2.prototype.NS_GOOGLE_AUTH) } module.exports = XOAuth2 },{"./mechanism":116,"node-xmpp-core":123}],119:[function(require,module,exports){ (function (process){(function (){ 'use strict' var EventEmitter = require('events').EventEmitter var core = require('node-xmpp-core') var inherits = core.inherits var ltx = core.ltx var request = require('request') var debug = require('debug')('xmpp:client:bosh') function BOSHConnection (opts) { var that = this EventEmitter.call(this) this.boshURL = opts.bosh.url this.jid = opts.jid this.wait = opts.bosh.wait || 60 this.xmlnsAttrs = { xmlns: 'http://jabber.org/protocol/httpbind', 'xmlns:xmpp': 'urn:xmpp:xbosh', 'xmlns:stream': 'http://etherx.jabber.org/streams' } if (opts.xmlns) { for (var prefix in opts.xmlns) { if (prefix) { this.xmlnsAttrs['xmlns:' + prefix] = opts.xmlns[prefix] } else { this.xmlnsAttrs.xmlns = opts.xmlns[prefix] } } } this.currentRequests = 0 this.queue = [] this.rid = Math.ceil(Math.random() * 9999999999) this.request({ to: this.jid.domain, ver: '1.6', wait: this.wait, hold: '1', content: this.contentType, 'xmpp:version': '1.0' }, [], function (err, bodyEl) { if (err) { that.emit('error', err) } else if (bodyEl && bodyEl.attrs) { that.sid = bodyEl.attrs.sid that.maxRequests = parseInt(bodyEl.attrs.requests, 10) || 2 if (that.sid && (that.maxRequests > 0)) { that.emit('connect') that.processResponse(bodyEl) process.nextTick(that.mayRequest.bind(that)) } else { that.emit('error', 'Invalid parameters') } } }) } inherits(BOSHConnection, EventEmitter) BOSHConnection.prototype.contentType = 'text/xml; charset=utf-8' BOSHConnection.prototype.send = function (stanza) { this.queue.push(stanza.root()) process.nextTick(this.mayRequest.bind(this)) } BOSHConnection.prototype.startStream = function () { var that = this this.rid++ this.request({ to: this.jid.domain, 'xmpp:restart': 'true' }, [], function (err, bodyEl) { if (err) { that.emit('error', err) that.emit('disconnect') that.emit('end') delete that.sid that.emit('close') } else { that.streamOpened = true if (bodyEl) that.processResponse(bodyEl) process.nextTick(that.mayRequest.bind(that)) } }) } BOSHConnection.prototype.processResponse = function (bodyEl) { debug('process bosh server response ' + bodyEl.toString()) if (bodyEl && bodyEl.children) { for (var i = 0; i < bodyEl.children.length; i++) { var child = bodyEl.children[i] if (child.name && child.attrs && child.children) { this.emit('stanza', child) } } } if (bodyEl && (bodyEl.attrs.type === 'terminate')) { if (!this.shutdown || bodyEl.attrs.condition) { this.emit('error', new Error(bodyEl.attrs.condition || 'Session terminated')) } this.emit('disconnect') this.emit('end') this.emit('close') } } BOSHConnection.prototype.mayRequest = function () { var canRequest = /* Must have a session already */ this.sid && /* We can only receive when one request is in flight */ ((this.currentRequests === 0) || /* Is there something to send, and are we allowed? */ (((this.queue.length > 0) && (this.currentRequests < this.maxRequests))) ) if (!canRequest) return var stanzas = this.queue this.queue = [] this.rid++ this.request({}, stanzas, function (err, bodyEl) { if (err) { this.emit('error', err) this.emit('disconnect') this.emit('end') delete this.sid this.emit('close') } else { if (bodyEl) this.processResponse(bodyEl) process.nextTick(this.mayRequest.bind(this)) } }.bind(this)) } BOSHConnection.prototype.end = function (stanzas) { stanzas = stanzas || [] if (typeof stanzas !== Array) stanzas = [stanzas] stanzas = this.queue.concat(stanzas) this.shutdown = true this.queue = [] this.rid++ this.request({ type: 'terminate' }, stanzas, function (err, bodyEl) { if (err) { } else if (bodyEl) { this.processResponse(bodyEl) } this.emit('disconnect') this.emit('end') delete this.sid this.emit('close') }.bind(this)) } BOSHConnection.prototype.maxHTTPRetries = 5 BOSHConnection.prototype.request = function (attrs, children, cb, retry) { var that = this retry = retry || 0 attrs.rid = this.rid.toString() if (this.sid) attrs.sid = this.sid for (var k in this.xmlnsAttrs) { attrs[k] = this.xmlnsAttrs[k] } var boshEl = new ltx.Element('body', attrs) for (var i = 0; i < children.length; i++) { boshEl.cnode(children[i]) } debug('send bosh request:' + boshEl.toString()) request({ uri: this.boshURL, method: 'POST', headers: { 'Content-Type': this.contentType }, body: boshEl.toString() }, function (err, res, body) { that.currentRequests-- if (err) { if (retry < that.maxHTTPRetries) { return that.request(attrs, children, cb, retry + 1) } else { return cb(err) } } if ((res.statusCode < 200) || (res.statusCode >= 400)) { return cb(new Error('HTTP status ' + res.statusCode)) } var bodyEl try { bodyEl = ltx.parse(body) } catch (e) { return cb(e) } if (bodyEl && (bodyEl.attrs.type === 'terminate') && bodyEl.attrs.condition) { cb(new Error(bodyEl.attrs.condition)) } else if (bodyEl) { cb(null, bodyEl) } else { cb(new Error('no ')) } } ) this.currentRequests++ } module.exports = BOSHConnection }).call(this)}).call(this,require('_process')) },{"_process":152,"debug":56,"events":80,"node-xmpp-core":123,"request":28}],120:[function(require,module,exports){ 'use strict' var Mechanism = require('./authentication/mechanism') /** * Available methods for client-side authentication (Client) * @param Array offeredMechs methods offered by server * @param Array preferredMech preferred methods by client * @param Array availableMech available methods on client */ function selectMechanism (offeredMechs, preferredMech, availableMech) { var mechClasses = [] var byName = {} var Mech if (Array.isArray(availableMech)) { mechClasses = mechClasses.concat(availableMech) } mechClasses.forEach(function (mechClass) { byName[mechClass.prototype.name] = mechClass }) /* Any preferred? */ if (byName[preferredMech] && (offeredMechs.indexOf(preferredMech) >= 0)) { Mech = byName[preferredMech] } /* By priority */ mechClasses.forEach(function (mechClass) { if (!Mech && (offeredMechs.indexOf(mechClass.prototype.name) >= 0)) { Mech = mechClass } }) return Mech ? new Mech() : null } /** * Will detect the available mechanisms based on the given options * @param {[type]} options client configuration * @param Array availableMech available methods on client * @return {[type]} available options */ function detectMechanisms (options, availableMech) { var mechClasses = availableMech || [] var detect = [] mechClasses.forEach(function (mechClass) { var match = mechClass.prototype.match if (match(options)) detect.push(mechClass) }) return detect } exports.selectMechanism = selectMechanism exports.detectMechanisms = detectMechanisms exports.AbstractMechanism = Mechanism },{"./authentication/mechanism":116}],121:[function(require,module,exports){ (function (process){(function (){ 'use strict' var tls = require('tls') var EventEmitter = require('events').EventEmitter var core = require('node-xmpp-core') var inherits = core.inherits var Connection = core.Connection var JID = core.JID var SRV = core.SRV var BOSHConnection = require('./bosh') var WSConnection = require('./websockets') var debug = require('debug')('xmpp:client:session') function Session (opts) { EventEmitter.call(this) this.setOptions(opts) if (opts.websocket && opts.websocket.url) { debug('start websocket connection') this._setupWebsocketConnection(opts) } else if (opts.bosh && opts.bosh.url) { debug('start bosh connection') this._setupBoshConnection(opts) } else { debug('start socket connection') this._setupSocketConnection(opts) } } inherits(Session, EventEmitter) Session.prototype._setupSocketConnection = function (opts) { var params = { xmlns: { '': opts.xmlns }, streamAttrs: { version: '1.0', to: this.jid.domain }, serialized: opts.serialized } for (var key in opts) { if (!(key in params)) { params[key] = opts[key] } } this.connection = new Connection(params) this._addConnectionListeners() if (opts.host || opts.port) { this._socketConnectionToHost(opts) } else if (!SRV) { throw new Error('Cannot load SRV') } else { this._performSrvLookup(opts) } } Session.prototype._socketConnectionToHost = function (opts) { var _this = this if (opts.legacySSL) { this.connection.allowTLS = false this.connection.connect({ socket: function () { return tls.connect( opts.port || 5223, opts.host || 'localhost', opts.credentials || {}, function () { if (this.socket.authorized) { _this.emit('connect', this.socket) } else { _this.emit('error', 'unauthorized') } }.bind(this) ) } }) } else { if (opts.credentials) { this.connection.credentials = tls .createSecureContext(opts.credentials) } if (opts.disallowTLS) this.connection.allowTLS = false this.connection.listen({ socket: function () { // wait for connect event listeners process.nextTick(function () { this.socket.connect(opts.port || 5222, opts.host) }.bind(this)) var socket = opts.socket opts.socket = null return socket // maybe create new socket } }) } } Session.prototype._performSrvLookup = function (opts) { if (opts.legacySSL) { throw new Error('LegacySSL mode does not support DNS lookups') } if (opts.credentials) { this.connection.credentials = tls.createSecureContext(opts.credentials) } if (opts.disallowTLS) { this.connection.allowTLS = false } this.connection.listen({socket: SRV.connect({ socket: opts.socket, services: ['_xmpp-client._tcp'], domain: this.jid.domain, defaultPort: 5222 })}) } Session.prototype._setupBoshConnection = function (opts) { this.connection = new BOSHConnection({ jid: this.jid, bosh: opts.bosh }) this._addConnectionListeners() this.connection.on('connected', function () { // Clients start , servers reply if (this.connection.startStream) { this.connection.startStream() } }.bind(this)) } Session.prototype._setupWebsocketConnection = function (opts) { this.connection = new WSConnection({ jid: this.jid, websocket: opts.websocket }) this._addConnectionListeners() this.connection.on('connected', function () { // Clients start , servers reply if (this.connection.startStream) { this.connection.startStream() } }.bind(this)) } Session.prototype.setOptions = function (opts) { this.jid = (typeof opts.jid === 'string') ? new JID(opts.jid) : opts.jid this.password = opts.password this.preferredSaslMechanism = opts.preferredSaslMechanism this.api_key = opts.api_key this.access_token = opts.access_token this.oauth2_token = opts.oauth2_token this.oauth2_auth = opts.oauth2_auth this.register = opts.register if (typeof opts.actAs === 'string') { this.actAs = new JID(opts.actAs) } else { this.actAs = opts.actAs } } Session.prototype._addConnectionListeners = function (con) { con = con || this.connection con.on('stanza', this.onStanza.bind(this)) con.on('drain', this.emit.bind(this, 'drain')) con.on('end', this.emit.bind(this, 'end')) con.on('close', this.emit.bind(this, 'close')) con.on('error', this.emit.bind(this, 'error')) con.on('connect', this.emit.bind(this, 'connect')) con.on('reconnect', this.emit.bind(this, 'reconnect')) con.on('disconnect', this.emit.bind(this, 'disconnect')) if (con.startStream) { con.on('connect', function () { // Clients start , servers reply con.startStream() }) this.on('auth', function () { con.startStream() }) } } Session.prototype.pause = function () { if (this.connection && this.connection.pause) { this.connection.pause() } } Session.prototype.resume = function () { if (this.connection && this.connection.resume) { this.connection.resume() } } Session.prototype.send = function (stanza) { return this.connection ? this.connection.send(stanza) : false } Session.prototype.end = function () { if (this.connection) { this.connection.end() } } Session.prototype.onStanza = function () {} module.exports = Session }).call(this)}).call(this,require('_process')) },{"./bosh":119,"./websockets":122,"_process":152,"debug":56,"events":80,"node-xmpp-core":123,"tls":32}],122:[function(require,module,exports){ 'use strict' var EventEmitter = require('events').EventEmitter var core = require('node-xmpp-core') var Element = core.Element var StreamParser = core.StreamParser var Connection = core.Connection var inherits = core.inherits var ws = require('ws') // we ignore ws in the browser field of package.json var WebSocket = ws.Server ? ws : window.WebSocket var debug = require('debug')('xmpp:client:websocket') var NS_FRAMING = 'urn:ietf:params:xml:ns:xmpp-framing' function WSConnection (opts) { EventEmitter.call(this) this.url = opts.websocket.url this.jid = opts.jid this.xmlns = { '': NS_FRAMING } this.websocket = new WebSocket(this.url, ['xmpp'], opts.websocket.options) this.websocket.onopen = this.onopen.bind(this) this.websocket.onmessage = this.onmessage.bind(this) this.websocket.onclose = this.onclose.bind(this) this.websocket.onerror = this.onerror.bind(this) } inherits(WSConnection, EventEmitter) WSConnection.prototype.maxStanzaSize = 65535 WSConnection.prototype.xmppVersion = '1.0' WSConnection.prototype.onopen = function () { this.startParser() this.emit('connected') } WSConnection.prototype.startParser = function () { var self = this this.parser = new StreamParser(this.maxStanzaSize) this.parser.on('start', function (attrs) { self.streamAttrs = attrs /* We need those xmlns often, store them extra */ self.streamNsAttrs = {} for (var k in attrs) { if ((k === 'xmlns') || (k.substr(0, 6) === 'xmlns:')) { self.streamNsAttrs[k] = attrs[k] } } /* Notify in case we don't wait for (Component or non-1.0 streams) */ self.emit('streamStart', attrs) }) this.parser.on('stanza', function (stanza) { // self.onStanza(self.addStreamNs(stanza)) self.onStanza(stanza) }) this.parser.on('error', this.onerror.bind(this)) this.parser.on('end', function () { self.stopParser() self.end() }) } WSConnection.prototype.stopParser = function () { /* No more events, please (may happen however) */ if (this.parser) { /* Get GC'ed */ delete this.parser } } WSConnection.prototype.onmessage = function (msg) { debug('ws msg <--', msg.data) if (msg && msg.data && this.parser) { this.parser.write(msg.data) } } WSConnection.prototype.onStanza = function (stanza) { if (stanza.is('error', Connection.NS_STREAM)) { /* TODO: extract error text */ this.emit('error', stanza) } else { this.emit('stanza', stanza) } } WSConnection.prototype.startStream = function () { var attrs = {} for (var k in this.xmlns) { if (this.xmlns.hasOwnProperty(k)) { if (!k) { attrs.xmlns = this.xmlns[k] } else { attrs['xmlns:' + k] = this.xmlns[k] } } } if (this.xmppVersion) attrs.version = this.xmppVersion if (this.streamTo) attrs.to = this.streamTo if (this.jid) attrs.to = this.jid.domain this.send(new Element('open', attrs)) this.streamOpened = true } WSConnection.prototype.send = function (stanza) { if (stanza.root) stanza = stanza.root() if (!stanza.attrs.xmlns && (stanza.is('iq') || stanza.is('presence') || stanza.is('message'))) { stanza.attrs.xmlns = 'jabber:client' } stanza = stanza.toString() debug('ws send -->', stanza) this.websocket.send(stanza) } WSConnection.prototype.onclose = function () { this.emit('disconnect') this.emit('close') } WSConnection.prototype.end = function () { this.send(new Element('close', {xmlns: NS_FRAMING})) this.emit('disconnect') this.emit('end') if (this.websocket) this.websocket.close() } WSConnection.prototype.onerror = function (e) { this.emit('error', e) } module.exports = WSConnection },{"debug":56,"events":80,"node-xmpp-core":123,"ws":29}],123:[function(require,module,exports){ 'use strict' var Connection = require('./lib/Connection') var StreamParser = require('@xmpp/streamparser') var JID = require('@xmpp/jid') var xml = require('@xmpp/xml') var inherits = require('inherits') exports.SRV = require('./lib/SRV') exports.exportCoreUtils = function (obj) { // core obj.Connection = Connection obj.StreamParser = StreamParser // jid obj.JID = JID // inherits obj.inherits = inherits // xml obj.stanza = xml obj.Stanza = xml.Stanza obj.createStanza = xml.createStanza obj.IQ = xml.IQ obj.Presence = xml.Presence obj.Message = xml.Message obj.Parser = xml.Parser obj.parse = xml.parse // ltx obj.ltx = xml.ltx obj.createElement = xml.createElement obj.Element = xml.Element obj.escapeXML = xml.escapeXML obj.escapeXMLText = xml.escapeXMLText } exports.exportCoreUtils(exports) },{"./lib/Connection":124,"./lib/SRV":29,"@xmpp/jid":125,"@xmpp/streamparser":7,"@xmpp/xml":129,"inherits":85}],124:[function(require,module,exports){ 'use strict' var net = require('net') var EventEmitter = require('events').EventEmitter var inherits = require('inherits') var Element = require('@xmpp/xml').Element var reconnect = require('reconnect-core') var StreamParser = require('@xmpp/streamparser') var starttls = require('node-xmpp-tls-connect') var debug = require('debug')('xmpp:connection') var assign = require('lodash.assign') var NS_XMPP_TLS = 'urn:ietf:params:xml:ns:xmpp-tls' var NS_STREAM = 'http://etherx.jabber.org/streams' var NS_XMPP_STREAMS = 'urn:ietf:params:xml:ns:xmpp-streams' var INITIAL_RECONNECT_DELAY = 1e3 var MAX_RECONNECT_DELAY = 30e3 var STREAM_OPEN = 'stream:stream' var STREAM_CLOSE = '' function defaultInjection (emitter, opts) { // clone opts var options = assign({}, opts) // add computed options options.initialDelay = (opts && (opts.initialReconnectDelay || opts.reconnectDelay)) || INITIAL_RECONNECT_DELAY options.maxDelay = (opts && opts.maxReconnectDelay) || MAX_RECONNECT_DELAY options.immediate = opts && opts.socket && (typeof opts.socket !== 'function') options.type = opts && opts.delayType options.emitter = emitter // return calculated options return options } /** Base class for connection-based streams (TCP). The socket parameter is optional for incoming connections. */ function Connection (opts) { EventEmitter.call(this) this.streamAttrs = (opts && opts.streamAttrs) || {} this.xmlns = (opts && opts.xmlns) || {} this.xmlns.stream = NS_STREAM this.streamOpen = (opts && opts.streamOpen) || STREAM_OPEN this.streamClose = (opts && opts.streamClose) || STREAM_CLOSE this.rejectUnauthorized = !!(opts && opts.rejectUnauthorized) this.serialized = !!(opts && opts.serialized) this.requestCert = !!(opts && opts.requestCert) this.servername = (opts && opts.servername) this.boundOnData = this.onData.bind(this) this.boundOnClose = this.onClose.bind(this) this.boundEmitData = this.emit.bind(this, 'data') this.boundEmitDrain = this.emit.bind(this, 'drain') this._setupSocket(defaultInjection(this, opts)) this.once('reconnect', function () { this.reconnect = opts && opts.reconnect }) } inherits(Connection, EventEmitter) Connection.prototype.NS_XMPP_TLS = NS_XMPP_TLS Connection.NS_STREAM = NS_STREAM Connection.prototype.NS_XMPP_STREAMS = NS_XMPP_STREAMS // Defaults Connection.prototype.allowTLS = true Connection.prototype._setupSocket = function (options) { debug('setup socket') var previousOptions = {} var inject = reconnect(function (opts) { var previousSocket = this.socket /* if this opts.preserve is on * the previous options are stored until next time. * this is needed to restore from a setSecure call. */ if (opts.preserve === 'on') { opts.preserve = previousOptions previousOptions = opts } else if (opts.preserve) { // switch back to the preversed options opts = previousOptions = opts.preserve } else { // keep some state for eg SRV.connect opts = previousOptions = opts || previousOptions } if (typeof opts.socket === 'function') { debug('use lazy socket') /* lazy evaluation * (can be retriggered by calling connection.connect() * without arguments after a previous * connection.connect({socket:function() { … }})) */ this.socket = opts.socket.call(this) } else { debug('use standard socket') // only use this socket once this.socket = opts.socket opts.socket = null if (this.socket) { this.once('connect', function () { inject.options.immediate = false }) } } this.socket = this.socket || new net.Socket() if (previousSocket !== this.socket) { this.setupStream() } return this.socket }.bind(this)) inject(inject.options = options) // wrap the end function provided by reconnect-core to trigger the stream end logic var end = this.end this.end = this.disconnect = function () { this.closeStream() end() } this.on('connection', function () { if (!this.parser) this.startParser() }) this.on('end', function () { previousOptions = {} }) } /** Used by both the constructor and by reinitialization in setSecure(). */ Connection.prototype.setupStream = function () { debug('setup stream') this.socket.on('end', this.onEnd.bind(this)) this.socket.on('data', this.boundOnData) this.socket.on('close', this.boundOnClose) // let them sniff unparsed XML this.socket.on('data', this.boundEmitData) this.socket.on('drain', this.boundEmitDrain) // ignore errors after disconnect this.socket.on('error', function () {}) if (!this.socket.serializeStanza) { /** * This is optimized for continuous TCP streams. If your "socket" * actually transports frames (WebSockets) and you can't have * stanzas split across those, use: * cb(el.toString()) */ if (this.serialized) { this.socket.serializeStanza = function (el, cb) { // Continuously write out el.write(function (s) { cb(s) }) } } else { this.socket.serializeStanza = function (el, cb) { cb(el.toString()) } } } } Connection.prototype.pause = function () { if (this.socket.pause) this.socket.pause() } Connection.prototype.resume = function () { if (this.socket.resume) this.socket.resume() } /** Climbs the stanza up if a child was passed, but you can send strings and buffers too. Returns whether the socket flushed data. */ Connection.prototype.send = function (stanza) { if (!this.socket || !this.streamOpened) return if (!this.socket.writable) { this.socket.end() return } debug('send: ' + stanza.toString()) var flushed = true if (stanza.root) { var el = this.rmXmlns(stanza.root()) this.socket.serializeStanza(el, function (s) { flushed = this.write(s) }.bind(this.socket)) } else { flushed = this.socket.write(stanza) } return flushed } Connection.prototype.startParser = function () { var self = this this.parser = new StreamParser(this.maxStanzaSize) this.parser.on('streamStart', function (attrs) { /* We need those xmlns often, store them extra */ self.streamNsAttrs = {} for (var k in attrs) { if (k === 'xmlns' || (k.substr(0, 6) === 'xmlns:')) { self.streamNsAttrs[k] = attrs[k] } } /* Notify in case we don't wait for (Component or non-1.0 streams) */ self.emit('streamStart', attrs) }) this.parser.on('stanza', function (stanza) { self.onStanza(self.addStreamNs(stanza)) }) this.parser.on('error', function (e) { self.error(e.condition || 'internal-server-error', e.message) }) this.parser.once('end', function () { self.stopParser() if (self.reconnect) { self.once('reconnect', self.startParser.bind(self)) } else { self.end() } }) } Connection.prototype.stopParser = function () { /* No more events, please (may happen however) */ if (this.parser) { var parser = this.parser /* Get GC'ed */ this.parser = null parser.end() } } /** * http://xmpp.org/rfcs/rfc6120.html#streams-open */ Connection.prototype.openStream = function () { var attrs = {} for (var k in this.xmlns) { if (this.xmlns.hasOwnProperty(k)) { if (!k) { attrs.xmlns = this.xmlns[k] } else { attrs['xmlns:' + k] = this.xmlns[k] } } } for (k in this.streamAttrs) { if (this.streamAttrs.hasOwnProperty(k)) { attrs[k] = this.streamAttrs[k] } } if (this.streamTo) { // in case of a component connecting attrs.to = this.streamTo } var el = new Element(this.streamOpen, attrs) var streamOpen if (el.name === 'stream:stream') { // make it non-empty to cut the closing tag el.t(' ') var s = el.toString() streamOpen = s.substr(0, s.indexOf(' ')) } else { streamOpen = el.toString() } this.streamOpened = true this.send(streamOpen) } // FIXME deprecate Connection.prototype.startStream = Connection.prototype.openStream /** * http://xmpp.org/rfcs/rfc6120.html#streams-close */ Connection.prototype.closeStream = function () { this.send(this.streamClose) this.streamOpened = false } // FIXME deprecate Connection.prototype.endStream = Connection.prototype.closeStream Connection.prototype.onData = function (data) { debug('receive: ' + data.toString('utf8')) if (this.parser) { this.parser.write(data) } } Connection.prototype.setSecure = function (credentials, isServer, servername) { // Remove old event listeners this.socket.removeListener('data', this.boundOnData) this.socket.removeListener('data', this.boundEmitData) // retain socket 'end' listeners because ssl layer doesn't support it this.socket.removeListener('drain', this.boundEmitDrain) this.socket.removeListener('close', this.boundOnClose) // remove idle_timeout if (this.socket.clearTimer) { this.socket.clearTimer() } var cleartext = starttls({ socket: this.socket, rejectUnauthorized: this.rejectUnauthorized, credentials: credentials || this.credentials, requestCert: this.requestCert, isServer: !!isServer, servername: isServer && servername }, function () { this.isSecure = true this.once('disconnect', function () { this.isSecure = false }) cleartext.emit('connect', cleartext) }.bind(this)) cleartext.on('clientError', this.emit.bind(this, 'error')) if (!this.reconnect) { this.reconnect = true // need this so stopParser works properly this.once('reconnect', function () { this.reconnect = false }) } this.stopParser() // if we reconnect we need to get back to the previous socket creation this.listen({ socket: cleartext, preserve: 'on' }) } function getAllText (el) { return !el.children ? el : el.children.reduce(function (text, child) { return text + getAllText(child) }, '') } /** * This is not an event listener, but takes care of the TLS handshake * before 'stanza' events are emitted to the derived classes. */ Connection.prototype.onStanza = function (stanza) { if (stanza.is('error', NS_STREAM)) { var error = new Error('' + getAllText(stanza)) error.stanza = stanza this.socket.emit('error', error) } else if (stanza.is('features', this.NS_STREAM) && this.allowTLS && !this.isSecure && stanza.getChild('starttls', this.NS_XMPP_TLS)) { /* Signal willingness to perform TLS handshake */ this.send(new Element('starttls', { xmlns: this.NS_XMPP_TLS })) } else if (this.allowTLS && stanza.is('proceed', this.NS_XMPP_TLS)) { /* Server is waiting for TLS handshake */ this.setSecure() } else { this.emit('stanza', stanza) } } /** * Add stream xmlns to a stanza * * Does not add our default xmlns as it is different for * C2S/S2S/Component connections. */ Connection.prototype.addStreamNs = function (stanza) { for (var attr in this.streamNsAttrs) { if (!stanza.attrs[attr] && !((attr === 'xmlns') && (this.streamNsAttrs[attr] === this.xmlns[''])) ) { stanza.attrs[attr] = this.streamNsAttrs[attr] } } return stanza } /** * Remove superfluous xmlns that were aleady declared in * our */ Connection.prototype.rmXmlns = function (stanza) { for (var prefix in this.xmlns) { var attr = prefix ? 'xmlns:' + prefix : 'xmlns' if (stanza.attrs[attr] === this.xmlns[prefix]) { stanza.attrs[attr] = null } } return stanza } /** * XMPP-style end connection for user */ Connection.prototype.onEnd = function () { this.closeStream() if (!this.reconnect) { this.emit('end') } } Connection.prototype.onClose = function () { if (!this.reconnect) { this.emit('close') } } /** * End connection with stream error. * Emits 'error' event too. * * @param {String} condition XMPP error condition, see RFC3920 4.7.3. Defined Conditions * @param {String} text Optional error message */ Connection.prototype.error = function (condition, message) { this.emit('error', new Error(message)) if (!this.socket || !this.socket.writable) return /* RFC 3920, 4.7.1 stream-level errors rules */ if (!this.streamOpened) this.openStream() var error = new Element('stream:error') error.c(condition, { xmlns: NS_XMPP_STREAMS }) if (message) { error.c('text', { xmlns: NS_XMPP_STREAMS, 'xml:lang': 'en' }).t(message) } this.send(error) this.end() } module.exports = Connection },{"@xmpp/streamparser":7,"@xmpp/xml":129,"debug":56,"events":80,"inherits":85,"lodash.assign":89,"net":32,"node-xmpp-tls-connect":29,"reconnect-core":171}],125:[function(require,module,exports){ 'use strict' var JID = require('./lib/JID') var tag = require('./lib/tag') module.exports = function createJID (a, b, c) { if (Array.isArray(a)) { return tag.apply(null, arguments) } return new JID(a, b, c) } module.exports.JID = JID module.exports.tag = tag module.exports.equal = function (a, b) { return a.equals(b) } module.exports.is = function (a) { return a instanceof JID } },{"./lib/JID":126,"./lib/tag":128}],126:[function(require,module,exports){ 'use strict' var escaping = require('./escaping') /** * JID implements * - XMPP addresses according to RFC6122 * - XEP-0106: JID Escaping * * @see http://tools.ietf.org/html/rfc6122#section-2 * @see http://xmpp.org/extensions/xep-0106.html */ function JID (a, b, c) { this._local = null this.user = null // DEPRECATED this._domain = null this._resource = null if (a && (!b) && (!c)) { this.parseJID(a) } else if (b) { this.setLocal(a) this.setDomain(b) this.setResource(c) } else { throw new Error('Argument error') } } JID.prototype.parseJID = function (s) { var resourceStart = s.indexOf('/') if (resourceStart !== -1) { this.setResource(s.substr(resourceStart + 1)) s = s.substr(0, resourceStart) } var atStart = s.indexOf('@') if (atStart !== -1) { this.setLocal(s.substr(0, atStart)) s = s.substr(atStart + 1) } this.setDomain(s) } JID.prototype.toString = function (unescape) { var s = this._domain if (this._local) s = this.getLocal(unescape) + '@' + s if (this._resource) s = s + '/' + this._resource return s } /** * Convenience method to distinguish users **/ JID.prototype.bare = function () { if (this._resource) { return new JID(this._local, this._domain, null) } else { return this } } /** * Comparison function **/ JID.prototype.equals = function (other) { return (this._local === other._local) && (this._domain === other._domain) && (this._resource === other._resource) } /** * http://xmpp.org/rfcs/rfc6122.html#addressing-localpart **/ JID.prototype.setLocal = function (local, escape) { escape = escape || escaping.detect(local) if (escape) { local = escaping.escape(local) } this._local = local && local.toLowerCase() this.user = this._local return this } JID.prototype.setUser = function () { console.log('JID.setUser: Use JID.setLocal instead') this.setLocal.apply(this, arguments) } JID.prototype.getUser = function () { console.log('JID.getUser: Use JID.getLocal instead') return this.getLocal.apply(this, arguments) } JID.prototype.getLocal = function (unescape) { unescape = unescape || false var local = null if (unescape) { local = escaping.unescape(this._local) } else { local = this._local } return local } Object.defineProperty(JID.prototype, 'local', { get: JID.prototype.getLocal, set: JID.prototype.setLocal }) /** * http://xmpp.org/rfcs/rfc6122.html#addressing-domain */ JID.prototype.setDomain = function (domain) { this._domain = domain.toLowerCase() return this } JID.prototype.getDomain = function () { return this._domain } Object.defineProperty(JID.prototype, 'domain', { get: JID.prototype.getDomain, set: JID.prototype.setDomain }) /** * http://xmpp.org/rfcs/rfc6122.html#addressing-resourcepart */ JID.prototype.setResource = function (resource) { this._resource = resource return this } JID.prototype.getResource = function () { return this._resource } Object.defineProperty(JID.prototype, 'resource', { get: JID.prototype.getResource, set: JID.prototype.setResource }) JID.prototype.detectEscape = escaping.detectEscape // DEPRECATED JID.prototype.escapeLocal = escaping.escape // DEPRECATED JID.prototype.unescapeLocal = escaping.unescape // DEPRECATED module.exports = JID },{"./escaping":127}],127:[function(require,module,exports){ 'use strict' module.exports.detect = function (local) { if (!local) return false // remove all escaped sequences var tmp = local .replace(/\\20/g, '') .replace(/\\22/g, '') .replace(/\\26/g, '') .replace(/\\27/g, '') .replace(/\\2f/g, '') .replace(/\\3a/g, '') .replace(/\\3c/g, '') .replace(/\\3e/g, '') .replace(/\\40/g, '') .replace(/\\5c/g, '') // detect if we have unescaped sequences var search = tmp.search(/\\| |"|&|'|\/|:|<|>|@/g) if (search === -1) { return false } else { return true } } /** * Escape the local part of a JID. * * @see http://xmpp.org/extensions/xep-0106.html * @param String local local part of a jid * @return An escaped local part */ module.exports.escape = function (local) { if (local === null) return null return local .replace(/^\s+|\s+$/g, '') .replace(/\\/g, '\\5c') .replace(/ /g, '\\20') .replace(/"/g, '\\22') .replace(/&/g, '\\26') .replace(/'/g, '\\27') .replace(/\//g, '\\2f') .replace(/:/g, '\\3a') .replace(//g, '\\3e') .replace(/@/g, '\\40') .replace(/\3a/g, '\u0005c3a') } /** * Unescape a local part of a JID. * * @see http://xmpp.org/extensions/xep-0106.html * @param String local local part of a jid * @return unescaped local part */ module.exports.unescape = function (local) { if (local === null) return null return local .replace(/\\20/g, ' ') .replace(/\\22/g, '"') .replace(/\\26/g, '&') .replace(/\\27/g, '\'') .replace(/\\2f/g, '/') .replace(/\\3a/g, ':') .replace(/\\3c/g, '<') .replace(/\\3e/g, '>') .replace(/\\40/g, '@') .replace(/\\5c/g, '\\') } },{}],128:[function(require,module,exports){ 'use strict' var JID = require('./JID') module.exports = function tag (/* [literals], ...substitutions */) { var literals = arguments[0] var substitutions = Array.prototype.slice.call(arguments, 1) var str = '' for (var i = 0; i < substitutions.length; i++) { str += literals[i] str += substitutions[i] } str += literals[literals.length - 1] return new JID(str) } },{"./JID":126}],129:[function(require,module,exports){ arguments[4][8][0].apply(exports,arguments) },{"./lib/IQ":130,"./lib/Message":131,"./lib/Parser":132,"./lib/Presence":133,"./lib/Stanza":134,"./lib/createStanza":135,"./lib/parse":136,"./lib/tag":137,"dup":8,"ltx":90}],130:[function(require,module,exports){ arguments[4][9][0].apply(exports,arguments) },{"./Stanza":134,"dup":9,"inherits":85}],131:[function(require,module,exports){ arguments[4][10][0].apply(exports,arguments) },{"./Stanza":134,"dup":10,"inherits":85}],132:[function(require,module,exports){ arguments[4][11][0].apply(exports,arguments) },{"./createStanza":135,"dup":11,"inherits":85,"ltx":90}],133:[function(require,module,exports){ arguments[4][12][0].apply(exports,arguments) },{"./Stanza":134,"dup":12,"inherits":85}],134:[function(require,module,exports){ arguments[4][13][0].apply(exports,arguments) },{"dup":13,"inherits":85,"ltx":90}],135:[function(require,module,exports){ arguments[4][14][0].apply(exports,arguments) },{"./Stanza":134,"dup":14,"ltx":90}],136:[function(require,module,exports){ arguments[4][15][0].apply(exports,arguments) },{"./Parser":132,"dup":15,"ltx":90}],137:[function(require,module,exports){ arguments[4][16][0].apply(exports,arguments) },{"./parse":136,"dup":16,"ltx":90}],138:[function(require,module,exports){ /* object-assign (c) Sindre Sorhus @license MIT */ 'use strict'; /* eslint-disable no-unused-vars */ var getOwnPropertySymbols = Object.getOwnPropertySymbols; var hasOwnProperty = Object.prototype.hasOwnProperty; var propIsEnumerable = Object.prototype.propertyIsEnumerable; function toObject(val) { if (val === null || val === undefined) { throw new TypeError('Object.assign cannot be called with null or undefined'); } return Object(val); } function shouldUseNative() { try { if (!Object.assign) { return false; } // Detect buggy property enumeration order in older V8 versions. // https://bugs.chromium.org/p/v8/issues/detail?id=4118 var test1 = new String('abc'); // eslint-disable-line no-new-wrappers test1[5] = 'de'; if (Object.getOwnPropertyNames(test1)[0] === '5') { return false; } // https://bugs.chromium.org/p/v8/issues/detail?id=3056 var test2 = {}; for (var i = 0; i < 10; i++) { test2['_' + String.fromCharCode(i)] = i; } var order2 = Object.getOwnPropertyNames(test2).map(function (n) { return test2[n]; }); if (order2.join('') !== '0123456789') { return false; } // https://bugs.chromium.org/p/v8/issues/detail?id=3056 var test3 = {}; 'abcdefghijklmnopqrst'.split('').forEach(function (letter) { test3[letter] = letter; }); if (Object.keys(Object.assign({}, test3)).join('') !== 'abcdefghijklmnopqrst') { return false; } return true; } catch (err) { // We don't expect any of the above to throw, but better to be safe. return false; } } module.exports = shouldUseNative() ? Object.assign : function (target, source) { var from; var to = toObject(target); var symbols; for (var s = 1; s < arguments.length; s++) { from = Object(arguments[s]); for (var key in from) { if (hasOwnProperty.call(from, key)) { to[key] = from[key]; } } if (getOwnPropertySymbols) { symbols = getOwnPropertySymbols(from); for (var i = 0; i < symbols.length; i++) { if (propIsEnumerable.call(from, symbols[i])) { to[symbols[i]] = from[symbols[i]]; } } } } return to; }; },{}],139:[function(require,module,exports){ exports.endianness = function () { return 'LE' }; exports.hostname = function () { if (typeof location !== 'undefined') { return location.hostname } else return ''; }; exports.loadavg = function () { return [] }; exports.uptime = function () { return 0 }; exports.freemem = function () { return Number.MAX_VALUE; }; exports.totalmem = function () { return Number.MAX_VALUE; }; exports.cpus = function () { return [] }; exports.type = function () { return 'Browser' }; exports.release = function () { if (typeof navigator !== 'undefined') { return navigator.appVersion; } return ''; }; exports.networkInterfaces = exports.getNetworkInterfaces = function () { return {} }; exports.arch = function () { return 'javascript' }; exports.platform = function () { return 'browser' }; exports.tmpdir = exports.tmpDir = function () { return '/tmp'; }; exports.EOL = '\n'; },{}],140:[function(require,module,exports){ 'use strict'; var TYPED_OK = (typeof Uint8Array !== 'undefined') && (typeof Uint16Array !== 'undefined') && (typeof Int32Array !== 'undefined'); exports.assign = function (obj /*from1, from2, from3, ...*/) { var sources = Array.prototype.slice.call(arguments, 1); while (sources.length) { var source = sources.shift(); if (!source) { continue; } if (typeof source !== 'object') { throw new TypeError(source + 'must be non-object'); } for (var p in source) { if (source.hasOwnProperty(p)) { obj[p] = source[p]; } } } return obj; }; // reduce buffer size, avoiding mem copy exports.shrinkBuf = function (buf, size) { if (buf.length === size) { return buf; } if (buf.subarray) { return buf.subarray(0, size); } buf.length = size; return buf; }; var fnTyped = { arraySet: function (dest, src, src_offs, len, dest_offs) { if (src.subarray && dest.subarray) { dest.set(src.subarray(src_offs, src_offs + len), dest_offs); return; } // Fallback to ordinary array for (var i = 0; i < len; i++) { dest[dest_offs + i] = src[src_offs + i]; } }, // Join array of chunks to single array. flattenChunks: function (chunks) { var i, l, len, pos, chunk, result; // calculate data length len = 0; for (i = 0, l = chunks.length; i < l; i++) { len += chunks[i].length; } // join chunks result = new Uint8Array(len); pos = 0; for (i = 0, l = chunks.length; i < l; i++) { chunk = chunks[i]; result.set(chunk, pos); pos += chunk.length; } return result; } }; var fnUntyped = { arraySet: function (dest, src, src_offs, len, dest_offs) { for (var i = 0; i < len; i++) { dest[dest_offs + i] = src[src_offs + i]; } }, // Join array of chunks to single array. flattenChunks: function (chunks) { return [].concat.apply([], chunks); } }; // Enable/Disable typed arrays use, for testing // exports.setTyped = function (on) { if (on) { exports.Buf8 = Uint8Array; exports.Buf16 = Uint16Array; exports.Buf32 = Int32Array; exports.assign(exports, fnTyped); } else { exports.Buf8 = Array; exports.Buf16 = Array; exports.Buf32 = Array; exports.assign(exports, fnUntyped); } }; exports.setTyped(TYPED_OK); },{}],141:[function(require,module,exports){ 'use strict'; // Note: adler32 takes 12% for level 0 and 2% for level 6. // It doesn't worth to make additional optimizationa as in original. // Small size is preferable. function adler32(adler, buf, len, pos) { var s1 = (adler & 0xffff) |0, s2 = ((adler >>> 16) & 0xffff) |0, n = 0; while (len !== 0) { // Set limit ~ twice less than 5552, to keep // s2 in 31-bits, because we force signed ints. // in other case %= will fail. n = len > 2000 ? 2000 : len; len -= n; do { s1 = (s1 + buf[pos++]) |0; s2 = (s2 + s1) |0; } while (--n); s1 %= 65521; s2 %= 65521; } return (s1 | (s2 << 16)) |0; } module.exports = adler32; },{}],142:[function(require,module,exports){ 'use strict'; module.exports = { /* Allowed flush values; see deflate() and inflate() below for details */ Z_NO_FLUSH: 0, Z_PARTIAL_FLUSH: 1, Z_SYNC_FLUSH: 2, Z_FULL_FLUSH: 3, Z_FINISH: 4, Z_BLOCK: 5, Z_TREES: 6, /* Return codes for the compression/decompression functions. Negative values * are errors, positive values are used for special but normal events. */ Z_OK: 0, Z_STREAM_END: 1, Z_NEED_DICT: 2, Z_ERRNO: -1, Z_STREAM_ERROR: -2, Z_DATA_ERROR: -3, //Z_MEM_ERROR: -4, Z_BUF_ERROR: -5, //Z_VERSION_ERROR: -6, /* compression levels */ Z_NO_COMPRESSION: 0, Z_BEST_SPEED: 1, Z_BEST_COMPRESSION: 9, Z_DEFAULT_COMPRESSION: -1, Z_FILTERED: 1, Z_HUFFMAN_ONLY: 2, Z_RLE: 3, Z_FIXED: 4, Z_DEFAULT_STRATEGY: 0, /* Possible values of the data_type field (though see inflate()) */ Z_BINARY: 0, Z_TEXT: 1, //Z_ASCII: 1, // = Z_TEXT (deprecated) Z_UNKNOWN: 2, /* The deflate compression method */ Z_DEFLATED: 8 //Z_NULL: null // Use -1 or null inline, depending on var type }; },{}],143:[function(require,module,exports){ 'use strict'; // Note: we can't get significant speed boost here. // So write code to minimize size - no pregenerated tables // and array tools dependencies. // Use ordinary array, since untyped makes no boost here function makeTable() { var c, table = []; for (var n = 0; n < 256; n++) { c = n; for (var k = 0; k < 8; k++) { c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1)); } table[n] = c; } return table; } // Create table on load. Just 255 signed longs. Not a problem. var crcTable = makeTable(); function crc32(crc, buf, len, pos) { var t = crcTable, end = pos + len; crc ^= -1; for (var i = pos; i < end; i++) { crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF]; } return (crc ^ (-1)); // >>> 0; } module.exports = crc32; },{}],144:[function(require,module,exports){ 'use strict'; var utils = require('../utils/common'); var trees = require('./trees'); var adler32 = require('./adler32'); var crc32 = require('./crc32'); var msg = require('./messages'); /* Public constants ==========================================================*/ /* ===========================================================================*/ /* Allowed flush values; see deflate() and inflate() below for details */ var Z_NO_FLUSH = 0; var Z_PARTIAL_FLUSH = 1; //var Z_SYNC_FLUSH = 2; var Z_FULL_FLUSH = 3; var Z_FINISH = 4; var Z_BLOCK = 5; //var Z_TREES = 6; /* Return codes for the compression/decompression functions. Negative values * are errors, positive values are used for special but normal events. */ var Z_OK = 0; var Z_STREAM_END = 1; //var Z_NEED_DICT = 2; //var Z_ERRNO = -1; var Z_STREAM_ERROR = -2; var Z_DATA_ERROR = -3; //var Z_MEM_ERROR = -4; var Z_BUF_ERROR = -5; //var Z_VERSION_ERROR = -6; /* compression levels */ //var Z_NO_COMPRESSION = 0; //var Z_BEST_SPEED = 1; //var Z_BEST_COMPRESSION = 9; var Z_DEFAULT_COMPRESSION = -1; var Z_FILTERED = 1; var Z_HUFFMAN_ONLY = 2; var Z_RLE = 3; var Z_FIXED = 4; var Z_DEFAULT_STRATEGY = 0; /* Possible values of the data_type field (though see inflate()) */ //var Z_BINARY = 0; //var Z_TEXT = 1; //var Z_ASCII = 1; // = Z_TEXT var Z_UNKNOWN = 2; /* The deflate compression method */ var Z_DEFLATED = 8; /*============================================================================*/ var MAX_MEM_LEVEL = 9; /* Maximum value for memLevel in deflateInit2 */ var MAX_WBITS = 15; /* 32K LZ77 window */ var DEF_MEM_LEVEL = 8; var LENGTH_CODES = 29; /* number of length codes, not counting the special END_BLOCK code */ var LITERALS = 256; /* number of literal bytes 0..255 */ var L_CODES = LITERALS + 1 + LENGTH_CODES; /* number of Literal or Length codes, including the END_BLOCK code */ var D_CODES = 30; /* number of distance codes */ var BL_CODES = 19; /* number of codes used to transfer the bit lengths */ var HEAP_SIZE = 2 * L_CODES + 1; /* maximum heap size */ var MAX_BITS = 15; /* All codes must not exceed MAX_BITS bits */ var MIN_MATCH = 3; var MAX_MATCH = 258; var MIN_LOOKAHEAD = (MAX_MATCH + MIN_MATCH + 1); var PRESET_DICT = 0x20; var INIT_STATE = 42; var EXTRA_STATE = 69; var NAME_STATE = 73; var COMMENT_STATE = 91; var HCRC_STATE = 103; var BUSY_STATE = 113; var FINISH_STATE = 666; var BS_NEED_MORE = 1; /* block not completed, need more input or more output */ var BS_BLOCK_DONE = 2; /* block flush performed */ var BS_FINISH_STARTED = 3; /* finish started, need only more output at next deflate */ var BS_FINISH_DONE = 4; /* finish done, accept no more input or output */ var OS_CODE = 0x03; // Unix :) . Don't detect, use this default. function err(strm, errorCode) { strm.msg = msg[errorCode]; return errorCode; } function rank(f) { return ((f) << 1) - ((f) > 4 ? 9 : 0); } function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } } /* ========================================================================= * Flush as much pending output as possible. All deflate() output goes * through this function so some applications may wish to modify it * to avoid allocating a large strm->output buffer and copying into it. * (See also read_buf()). */ function flush_pending(strm) { var s = strm.state; //_tr_flush_bits(s); var len = s.pending; if (len > strm.avail_out) { len = strm.avail_out; } if (len === 0) { return; } utils.arraySet(strm.output, s.pending_buf, s.pending_out, len, strm.next_out); strm.next_out += len; s.pending_out += len; strm.total_out += len; strm.avail_out -= len; s.pending -= len; if (s.pending === 0) { s.pending_out = 0; } } function flush_block_only(s, last) { trees._tr_flush_block(s, (s.block_start >= 0 ? s.block_start : -1), s.strstart - s.block_start, last); s.block_start = s.strstart; flush_pending(s.strm); } function put_byte(s, b) { s.pending_buf[s.pending++] = b; } /* ========================================================================= * Put a short in the pending buffer. The 16-bit value is put in MSB order. * IN assertion: the stream state is correct and there is enough room in * pending_buf. */ function putShortMSB(s, b) { // put_byte(s, (Byte)(b >> 8)); // put_byte(s, (Byte)(b & 0xff)); s.pending_buf[s.pending++] = (b >>> 8) & 0xff; s.pending_buf[s.pending++] = b & 0xff; } /* =========================================================================== * Read a new buffer from the current input stream, update the adler32 * and total number of bytes read. All deflate() input goes through * this function so some applications may wish to modify it to avoid * allocating a large strm->input buffer and copying from it. * (See also flush_pending()). */ function read_buf(strm, buf, start, size) { var len = strm.avail_in; if (len > size) { len = size; } if (len === 0) { return 0; } strm.avail_in -= len; // zmemcpy(buf, strm->next_in, len); utils.arraySet(buf, strm.input, strm.next_in, len, start); if (strm.state.wrap === 1) { strm.adler = adler32(strm.adler, buf, len, start); } else if (strm.state.wrap === 2) { strm.adler = crc32(strm.adler, buf, len, start); } strm.next_in += len; strm.total_in += len; return len; } /* =========================================================================== * Set match_start to the longest match starting at the given string and * return its length. Matches shorter or equal to prev_length are discarded, * in which case the result is equal to prev_length and match_start is * garbage. * IN assertions: cur_match is the head of the hash chain for the current * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 * OUT assertion: the match length is not greater than s->lookahead. */ function longest_match(s, cur_match) { var chain_length = s.max_chain_length; /* max hash chain length */ var scan = s.strstart; /* current string */ var match; /* matched string */ var len; /* length of current match */ var best_len = s.prev_length; /* best match length so far */ var nice_match = s.nice_match; /* stop if match long enough */ var limit = (s.strstart > (s.w_size - MIN_LOOKAHEAD)) ? s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0/*NIL*/; var _win = s.window; // shortcut var wmask = s.w_mask; var prev = s.prev; /* Stop when cur_match becomes <= limit. To simplify the code, * we prevent matches with the string of window index 0. */ var strend = s.strstart + MAX_MATCH; var scan_end1 = _win[scan + best_len - 1]; var scan_end = _win[scan + best_len]; /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. * It is easy to get rid of this optimization if necessary. */ // Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); /* Do not waste too much time if we already have a good match: */ if (s.prev_length >= s.good_match) { chain_length >>= 2; } /* Do not look for matches beyond the end of the input. This is necessary * to make deflate deterministic. */ if (nice_match > s.lookahead) { nice_match = s.lookahead; } // Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); do { // Assert(cur_match < s->strstart, "no future"); match = cur_match; /* Skip to next match if the match length cannot increase * or if the match length is less than 2. Note that the checks below * for insufficient lookahead only occur occasionally for performance * reasons. Therefore uninitialized memory will be accessed, and * conditional jumps will be made that depend on those values. * However the length of the match is limited to the lookahead, so * the output of deflate is not affected by the uninitialized values. */ if (_win[match + best_len] !== scan_end || _win[match + best_len - 1] !== scan_end1 || _win[match] !== _win[scan] || _win[++match] !== _win[scan + 1]) { continue; } /* The check at best_len-1 can be removed because it will be made * again later. (This heuristic is not always a win.) * It is not necessary to compare scan[2] and match[2] since they * are always equal when the other bytes match, given that * the hash keys are equal and that HASH_BITS >= 8. */ scan += 2; match++; // Assert(*scan == *match, "match[2]?"); /* We check for insufficient lookahead only every 8th comparison; * the 256th check will be made at strstart+258. */ do { /*jshint noempty:false*/ } while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && scan < strend); // Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); len = MAX_MATCH - (strend - scan); scan = strend - MAX_MATCH; if (len > best_len) { s.match_start = cur_match; best_len = len; if (len >= nice_match) { break; } scan_end1 = _win[scan + best_len - 1]; scan_end = _win[scan + best_len]; } } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length !== 0); if (best_len <= s.lookahead) { return best_len; } return s.lookahead; } /* =========================================================================== * Fill the window when the lookahead becomes insufficient. * Updates strstart and lookahead. * * IN assertion: lookahead < MIN_LOOKAHEAD * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD * At least one byte has been read, or avail_in == 0; reads are * performed for at least two bytes (required for the zip translate_eol * option -- not supported here). */ function fill_window(s) { var _w_size = s.w_size; var p, n, m, more, str; //Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead"); do { more = s.window_size - s.lookahead - s.strstart; // JS ints have 32 bit, block below not needed /* Deal with !@#$% 64K limit: */ //if (sizeof(int) <= 2) { // if (more == 0 && s->strstart == 0 && s->lookahead == 0) { // more = wsize; // // } else if (more == (unsigned)(-1)) { // /* Very unlikely, but possible on 16 bit machine if // * strstart == 0 && lookahead == 1 (input done a byte at time) // */ // more--; // } //} /* If the window is almost full and there is insufficient lookahead, * move the upper half to the lower one to make room in the upper half. */ if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) { utils.arraySet(s.window, s.window, _w_size, _w_size, 0); s.match_start -= _w_size; s.strstart -= _w_size; /* we now have strstart >= MAX_DIST */ s.block_start -= _w_size; /* Slide the hash table (could be avoided with 32 bit values at the expense of memory usage). We slide even when level == 0 to keep the hash table consistent if we switch back to level > 0 later. (Using level 0 permanently is not an optimal usage of zlib, so we don't care about this pathological case.) */ n = s.hash_size; p = n; do { m = s.head[--p]; s.head[p] = (m >= _w_size ? m - _w_size : 0); } while (--n); n = _w_size; p = n; do { m = s.prev[--p]; s.prev[p] = (m >= _w_size ? m - _w_size : 0); /* If n is not on any hash chain, prev[n] is garbage but * its value will never be used. */ } while (--n); more += _w_size; } if (s.strm.avail_in === 0) { break; } /* If there was no sliding: * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && * more == window_size - lookahead - strstart * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) * => more >= window_size - 2*WSIZE + 2 * In the BIG_MEM or MMAP case (not yet supported), * window_size == input_size + MIN_LOOKAHEAD && * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. * Otherwise, window_size == 2*WSIZE so more >= 2. * If there was sliding, more >= WSIZE. So in all cases, more >= 2. */ //Assert(more >= 2, "more < 2"); n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more); s.lookahead += n; /* Initialize the hash value now that we have some input: */ if (s.lookahead + s.insert >= MIN_MATCH) { str = s.strstart - s.insert; s.ins_h = s.window[str]; /* UPDATE_HASH(s, s->ins_h, s->window[str + 1]); */ s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + 1]) & s.hash_mask; //#if MIN_MATCH != 3 // Call update_hash() MIN_MATCH-3 more times //#endif while (s.insert) { /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */ s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH - 1]) & s.hash_mask; s.prev[str & s.w_mask] = s.head[s.ins_h]; s.head[s.ins_h] = str; str++; s.insert--; if (s.lookahead + s.insert < MIN_MATCH) { break; } } } /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, * but this is not important since only literal bytes will be emitted. */ } while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0); /* If the WIN_INIT bytes after the end of the current data have never been * written, then zero those bytes in order to avoid memory check reports of * the use of uninitialized (or uninitialised as Julian writes) bytes by * the longest match routines. Update the high water mark for the next * time through here. WIN_INIT is set to MAX_MATCH since the longest match * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead. */ // if (s.high_water < s.window_size) { // var curr = s.strstart + s.lookahead; // var init = 0; // // if (s.high_water < curr) { // /* Previous high water mark below current data -- zero WIN_INIT // * bytes or up to end of window, whichever is less. // */ // init = s.window_size - curr; // if (init > WIN_INIT) // init = WIN_INIT; // zmemzero(s->window + curr, (unsigned)init); // s->high_water = curr + init; // } // else if (s->high_water < (ulg)curr + WIN_INIT) { // /* High water mark at or above current data, but below current data // * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up // * to end of window, whichever is less. // */ // init = (ulg)curr + WIN_INIT - s->high_water; // if (init > s->window_size - s->high_water) // init = s->window_size - s->high_water; // zmemzero(s->window + s->high_water, (unsigned)init); // s->high_water += init; // } // } // // Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, // "not enough room for search"); } /* =========================================================================== * Copy without compression as much as possible from the input stream, return * the current block state. * This function does not insert new strings in the dictionary since * uncompressible data is probably not useful. This function is used * only for the level=0 compression option. * NOTE: this function should be optimized to avoid extra copying from * window to pending_buf. */ function deflate_stored(s, flush) { /* Stored blocks are limited to 0xffff bytes, pending_buf is limited * to pending_buf_size, and each stored block has a 5 byte header: */ var max_block_size = 0xffff; if (max_block_size > s.pending_buf_size - 5) { max_block_size = s.pending_buf_size - 5; } /* Copy as much as possible from input to output: */ for (;;) { /* Fill the window as much as possible: */ if (s.lookahead <= 1) { //Assert(s->strstart < s->w_size+MAX_DIST(s) || // s->block_start >= (long)s->w_size, "slide too late"); // if (!(s.strstart < s.w_size + (s.w_size - MIN_LOOKAHEAD) || // s.block_start >= s.w_size)) { // throw new Error("slide too late"); // } fill_window(s); if (s.lookahead === 0 && flush === Z_NO_FLUSH) { return BS_NEED_MORE; } if (s.lookahead === 0) { break; } /* flush the current block */ } //Assert(s->block_start >= 0L, "block gone"); // if (s.block_start < 0) throw new Error("block gone"); s.strstart += s.lookahead; s.lookahead = 0; /* Emit a stored block if pending_buf will be full: */ var max_start = s.block_start + max_block_size; if (s.strstart === 0 || s.strstart >= max_start) { /* strstart == 0 is possible when wraparound on 16-bit machine */ s.lookahead = s.strstart - max_start; s.strstart = max_start; /*** FLUSH_BLOCK(s, 0); ***/ flush_block_only(s, false); if (s.strm.avail_out === 0) { return BS_NEED_MORE; } /***/ } /* Flush if we may have to slide, otherwise block_start may become * negative and the data will be gone: */ if (s.strstart - s.block_start >= (s.w_size - MIN_LOOKAHEAD)) { /*** FLUSH_BLOCK(s, 0); ***/ flush_block_only(s, false); if (s.strm.avail_out === 0) { return BS_NEED_MORE; } /***/ } } s.insert = 0; if (flush === Z_FINISH) { /*** FLUSH_BLOCK(s, 1); ***/ flush_block_only(s, true); if (s.strm.avail_out === 0) { return BS_FINISH_STARTED; } /***/ return BS_FINISH_DONE; } if (s.strstart > s.block_start) { /*** FLUSH_BLOCK(s, 0); ***/ flush_block_only(s, false); if (s.strm.avail_out === 0) { return BS_NEED_MORE; } /***/ } return BS_NEED_MORE; } /* =========================================================================== * Compress as much as possible from the input stream, return the current * block state. * This function does not perform lazy evaluation of matches and inserts * new strings in the dictionary only for unmatched strings or for short * matches. It is used only for the fast compression options. */ function deflate_fast(s, flush) { var hash_head; /* head of the hash chain */ var bflush; /* set if current block must be flushed */ for (;;) { /* Make sure that we always have enough lookahead, except * at the end of the input file. We need MAX_MATCH bytes * for the next match, plus MIN_MATCH bytes to insert the * string following the next match. */ if (s.lookahead < MIN_LOOKAHEAD) { fill_window(s); if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) { return BS_NEED_MORE; } if (s.lookahead === 0) { break; /* flush the current block */ } } /* Insert the string window[strstart .. strstart+2] in the * dictionary, and set hash_head to the head of the hash chain: */ hash_head = 0/*NIL*/; if (s.lookahead >= MIN_MATCH) { /*** INSERT_STRING(s, s.strstart, hash_head); ***/ s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask; hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h]; s.head[s.ins_h] = s.strstart; /***/ } /* Find the longest match, discarding those <= prev_length. * At this point we have always match_length < MIN_MATCH */ if (hash_head !== 0/*NIL*/ && ((s.strstart - hash_head) <= (s.w_size - MIN_LOOKAHEAD))) { /* To simplify the code, we prevent matches with the string * of window index 0 (in particular we have to avoid a match * of the string with itself at the start of the input file). */ s.match_length = longest_match(s, hash_head); /* longest_match() sets match_start */ } if (s.match_length >= MIN_MATCH) { // check_match(s, s.strstart, s.match_start, s.match_length); // for debug only /*** _tr_tally_dist(s, s.strstart - s.match_start, s.match_length - MIN_MATCH, bflush); ***/ bflush = trees._tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH); s.lookahead -= s.match_length; /* Insert new strings in the hash table only if the match length * is not too large. This saves time but degrades compression. */ if (s.match_length <= s.max_lazy_match/*max_insert_length*/ && s.lookahead >= MIN_MATCH) { s.match_length--; /* string at strstart already in table */ do { s.strstart++; /*** INSERT_STRING(s, s.strstart, hash_head); ***/ s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask; hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h]; s.head[s.ins_h] = s.strstart; /***/ /* strstart never exceeds WSIZE-MAX_MATCH, so there are * always MIN_MATCH bytes ahead. */ } while (--s.match_length !== 0); s.strstart++; } else { s.strstart += s.match_length; s.match_length = 0; s.ins_h = s.window[s.strstart]; /* UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]); */ s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + 1]) & s.hash_mask; //#if MIN_MATCH != 3 // Call UPDATE_HASH() MIN_MATCH-3 more times //#endif /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not * matter since it will be recomputed at next deflate call. */ } } else { /* No match, output a literal byte */ //Tracevv((stderr,"%c", s.window[s.strstart])); /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/ bflush = trees._tr_tally(s, 0, s.window[s.strstart]); s.lookahead--; s.strstart++; } if (bflush) { /*** FLUSH_BLOCK(s, 0); ***/ flush_block_only(s, false); if (s.strm.avail_out === 0) { return BS_NEED_MORE; } /***/ } } s.insert = ((s.strstart < (MIN_MATCH - 1)) ? s.strstart : MIN_MATCH - 1); if (flush === Z_FINISH) { /*** FLUSH_BLOCK(s, 1); ***/ flush_block_only(s, true); if (s.strm.avail_out === 0) { return BS_FINISH_STARTED; } /***/ return BS_FINISH_DONE; } if (s.last_lit) { /*** FLUSH_BLOCK(s, 0); ***/ flush_block_only(s, false); if (s.strm.avail_out === 0) { return BS_NEED_MORE; } /***/ } return BS_BLOCK_DONE; } /* =========================================================================== * Same as above, but achieves better compression. We use a lazy * evaluation for matches: a match is finally adopted only if there is * no better match at the next window position. */ function deflate_slow(s, flush) { var hash_head; /* head of hash chain */ var bflush; /* set if current block must be flushed */ var max_insert; /* Process the input block. */ for (;;) { /* Make sure that we always have enough lookahead, except * at the end of the input file. We need MAX_MATCH bytes * for the next match, plus MIN_MATCH bytes to insert the * string following the next match. */ if (s.lookahead < MIN_LOOKAHEAD) { fill_window(s); if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) { return BS_NEED_MORE; } if (s.lookahead === 0) { break; } /* flush the current block */ } /* Insert the string window[strstart .. strstart+2] in the * dictionary, and set hash_head to the head of the hash chain: */ hash_head = 0/*NIL*/; if (s.lookahead >= MIN_MATCH) { /*** INSERT_STRING(s, s.strstart, hash_head); ***/ s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask; hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h]; s.head[s.ins_h] = s.strstart; /***/ } /* Find the longest match, discarding those <= prev_length. */ s.prev_length = s.match_length; s.prev_match = s.match_start; s.match_length = MIN_MATCH - 1; if (hash_head !== 0/*NIL*/ && s.prev_length < s.max_lazy_match && s.strstart - hash_head <= (s.w_size - MIN_LOOKAHEAD)/*MAX_DIST(s)*/) { /* To simplify the code, we prevent matches with the string * of window index 0 (in particular we have to avoid a match * of the string with itself at the start of the input file). */ s.match_length = longest_match(s, hash_head); /* longest_match() sets match_start */ if (s.match_length <= 5 && (s.strategy === Z_FILTERED || (s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096/*TOO_FAR*/))) { /* If prev_match is also MIN_MATCH, match_start is garbage * but we will ignore the current match anyway. */ s.match_length = MIN_MATCH - 1; } } /* If there was a match at the previous step and the current * match is not better, output the previous match: */ if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) { max_insert = s.strstart + s.lookahead - MIN_MATCH; /* Do not insert strings in hash table beyond this. */ //check_match(s, s.strstart-1, s.prev_match, s.prev_length); /***_tr_tally_dist(s, s.strstart - 1 - s.prev_match, s.prev_length - MIN_MATCH, bflush);***/ bflush = trees._tr_tally(s, s.strstart - 1 - s.prev_match, s.prev_length - MIN_MATCH); /* Insert in hash table all strings up to the end of the match. * strstart-1 and strstart are already inserted. If there is not * enough lookahead, the last two strings are not inserted in * the hash table. */ s.lookahead -= s.prev_length - 1; s.prev_length -= 2; do { if (++s.strstart <= max_insert) { /*** INSERT_STRING(s, s.strstart, hash_head); ***/ s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask; hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h]; s.head[s.ins_h] = s.strstart; /***/ } } while (--s.prev_length !== 0); s.match_available = 0; s.match_length = MIN_MATCH - 1; s.strstart++; if (bflush) { /*** FLUSH_BLOCK(s, 0); ***/ flush_block_only(s, false); if (s.strm.avail_out === 0) { return BS_NEED_MORE; } /***/ } } else if (s.match_available) { /* If there was no match at the previous position, output a * single literal. If there was a match but the current match * is longer, truncate the previous match to a single literal. */ //Tracevv((stderr,"%c", s->window[s->strstart-1])); /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/ bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]); if (bflush) { /*** FLUSH_BLOCK_ONLY(s, 0) ***/ flush_block_only(s, false); /***/ } s.strstart++; s.lookahead--; if (s.strm.avail_out === 0) { return BS_NEED_MORE; } } else { /* There is no previous match to compare with, wait for * the next step to decide. */ s.match_available = 1; s.strstart++; s.lookahead--; } } //Assert (flush != Z_NO_FLUSH, "no flush?"); if (s.match_available) { //Tracevv((stderr,"%c", s->window[s->strstart-1])); /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/ bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]); s.match_available = 0; } s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1; if (flush === Z_FINISH) { /*** FLUSH_BLOCK(s, 1); ***/ flush_block_only(s, true); if (s.strm.avail_out === 0) { return BS_FINISH_STARTED; } /***/ return BS_FINISH_DONE; } if (s.last_lit) { /*** FLUSH_BLOCK(s, 0); ***/ flush_block_only(s, false); if (s.strm.avail_out === 0) { return BS_NEED_MORE; } /***/ } return BS_BLOCK_DONE; } /* =========================================================================== * For Z_RLE, simply look for runs of bytes, generate matches only of distance * one. Do not maintain a hash table. (It will be regenerated if this run of * deflate switches away from Z_RLE.) */ function deflate_rle(s, flush) { var bflush; /* set if current block must be flushed */ var prev; /* byte at distance one to match */ var scan, strend; /* scan goes up to strend for length of run */ var _win = s.window; for (;;) { /* Make sure that we always have enough lookahead, except * at the end of the input file. We need MAX_MATCH bytes * for the longest run, plus one for the unrolled loop. */ if (s.lookahead <= MAX_MATCH) { fill_window(s); if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH) { return BS_NEED_MORE; } if (s.lookahead === 0) { break; } /* flush the current block */ } /* See how many times the previous byte repeats */ s.match_length = 0; if (s.lookahead >= MIN_MATCH && s.strstart > 0) { scan = s.strstart - 1; prev = _win[scan]; if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) { strend = s.strstart + MAX_MATCH; do { /*jshint noempty:false*/ } while (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && scan < strend); s.match_length = MAX_MATCH - (strend - scan); if (s.match_length > s.lookahead) { s.match_length = s.lookahead; } } //Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan"); } /* Emit match if have run of MIN_MATCH or longer, else emit literal */ if (s.match_length >= MIN_MATCH) { //check_match(s, s.strstart, s.strstart - 1, s.match_length); /*** _tr_tally_dist(s, 1, s.match_length - MIN_MATCH, bflush); ***/ bflush = trees._tr_tally(s, 1, s.match_length - MIN_MATCH); s.lookahead -= s.match_length; s.strstart += s.match_length; s.match_length = 0; } else { /* No match, output a literal byte */ //Tracevv((stderr,"%c", s->window[s->strstart])); /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/ bflush = trees._tr_tally(s, 0, s.window[s.strstart]); s.lookahead--; s.strstart++; } if (bflush) { /*** FLUSH_BLOCK(s, 0); ***/ flush_block_only(s, false); if (s.strm.avail_out === 0) { return BS_NEED_MORE; } /***/ } } s.insert = 0; if (flush === Z_FINISH) { /*** FLUSH_BLOCK(s, 1); ***/ flush_block_only(s, true); if (s.strm.avail_out === 0) { return BS_FINISH_STARTED; } /***/ return BS_FINISH_DONE; } if (s.last_lit) { /*** FLUSH_BLOCK(s, 0); ***/ flush_block_only(s, false); if (s.strm.avail_out === 0) { return BS_NEED_MORE; } /***/ } return BS_BLOCK_DONE; } /* =========================================================================== * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. * (It will be regenerated if this run of deflate switches away from Huffman.) */ function deflate_huff(s, flush) { var bflush; /* set if current block must be flushed */ for (;;) { /* Make sure that we have a literal to write. */ if (s.lookahead === 0) { fill_window(s); if (s.lookahead === 0) { if (flush === Z_NO_FLUSH) { return BS_NEED_MORE; } break; /* flush the current block */ } } /* Output a literal byte */ s.match_length = 0; //Tracevv((stderr,"%c", s->window[s->strstart])); /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/ bflush = trees._tr_tally(s, 0, s.window[s.strstart]); s.lookahead--; s.strstart++; if (bflush) { /*** FLUSH_BLOCK(s, 0); ***/ flush_block_only(s, false); if (s.strm.avail_out === 0) { return BS_NEED_MORE; } /***/ } } s.insert = 0; if (flush === Z_FINISH) { /*** FLUSH_BLOCK(s, 1); ***/ flush_block_only(s, true); if (s.strm.avail_out === 0) { return BS_FINISH_STARTED; } /***/ return BS_FINISH_DONE; } if (s.last_lit) { /*** FLUSH_BLOCK(s, 0); ***/ flush_block_only(s, false); if (s.strm.avail_out === 0) { return BS_NEED_MORE; } /***/ } return BS_BLOCK_DONE; } /* Values for max_lazy_match, good_match and max_chain_length, depending on * the desired pack level (0..9). The values given below have been tuned to * exclude worst case performance for pathological files. Better values may be * found for specific files. */ function Config(good_length, max_lazy, nice_length, max_chain, func) { this.good_length = good_length; this.max_lazy = max_lazy; this.nice_length = nice_length; this.max_chain = max_chain; this.func = func; } var configuration_table; configuration_table = [ /* good lazy nice chain */ new Config(0, 0, 0, 0, deflate_stored), /* 0 store only */ new Config(4, 4, 8, 4, deflate_fast), /* 1 max speed, no lazy matches */ new Config(4, 5, 16, 8, deflate_fast), /* 2 */ new Config(4, 6, 32, 32, deflate_fast), /* 3 */ new Config(4, 4, 16, 16, deflate_slow), /* 4 lazy matches */ new Config(8, 16, 32, 32, deflate_slow), /* 5 */ new Config(8, 16, 128, 128, deflate_slow), /* 6 */ new Config(8, 32, 128, 256, deflate_slow), /* 7 */ new Config(32, 128, 258, 1024, deflate_slow), /* 8 */ new Config(32, 258, 258, 4096, deflate_slow) /* 9 max compression */ ]; /* =========================================================================== * Initialize the "longest match" routines for a new zlib stream */ function lm_init(s) { s.window_size = 2 * s.w_size; /*** CLEAR_HASH(s); ***/ zero(s.head); // Fill with NIL (= 0); /* Set the default configuration parameters: */ s.max_lazy_match = configuration_table[s.level].max_lazy; s.good_match = configuration_table[s.level].good_length; s.nice_match = configuration_table[s.level].nice_length; s.max_chain_length = configuration_table[s.level].max_chain; s.strstart = 0; s.block_start = 0; s.lookahead = 0; s.insert = 0; s.match_length = s.prev_length = MIN_MATCH - 1; s.match_available = 0; s.ins_h = 0; } function DeflateState() { this.strm = null; /* pointer back to this zlib stream */ this.status = 0; /* as the name implies */ this.pending_buf = null; /* output still pending */ this.pending_buf_size = 0; /* size of pending_buf */ this.pending_out = 0; /* next pending byte to output to the stream */ this.pending = 0; /* nb of bytes in the pending buffer */ this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */ this.gzhead = null; /* gzip header information to write */ this.gzindex = 0; /* where in extra, name, or comment */ this.method = Z_DEFLATED; /* can only be DEFLATED */ this.last_flush = -1; /* value of flush param for previous deflate call */ this.w_size = 0; /* LZ77 window size (32K by default) */ this.w_bits = 0; /* log2(w_size) (8..16) */ this.w_mask = 0; /* w_size - 1 */ this.window = null; /* Sliding window. Input bytes are read into the second half of the window, * and move to the first half later to keep a dictionary of at least wSize * bytes. With this organization, matches are limited to a distance of * wSize-MAX_MATCH bytes, but this ensures that IO is always * performed with a length multiple of the block size. */ this.window_size = 0; /* Actual size of window: 2*wSize, except when the user input buffer * is directly used as sliding window. */ this.prev = null; /* Link to older string with same hash index. To limit the size of this * array to 64K, this link is maintained only for the last 32K strings. * An index in this array is thus a window index modulo 32K. */ this.head = null; /* Heads of the hash chains or NIL. */ this.ins_h = 0; /* hash index of string to be inserted */ this.hash_size = 0; /* number of elements in hash table */ this.hash_bits = 0; /* log2(hash_size) */ this.hash_mask = 0; /* hash_size-1 */ this.hash_shift = 0; /* Number of bits by which ins_h must be shifted at each input * step. It must be such that after MIN_MATCH steps, the oldest * byte no longer takes part in the hash key, that is: * hash_shift * MIN_MATCH >= hash_bits */ this.block_start = 0; /* Window position at the beginning of the current output block. Gets * negative when the window is moved backwards. */ this.match_length = 0; /* length of best match */ this.prev_match = 0; /* previous match */ this.match_available = 0; /* set if previous match exists */ this.strstart = 0; /* start of string to insert */ this.match_start = 0; /* start of matching string */ this.lookahead = 0; /* number of valid bytes ahead in window */ this.prev_length = 0; /* Length of the best match at previous step. Matches not greater than this * are discarded. This is used in the lazy match evaluation. */ this.max_chain_length = 0; /* To speed up deflation, hash chains are never searched beyond this * length. A higher limit improves compression ratio but degrades the * speed. */ this.max_lazy_match = 0; /* Attempt to find a better match only when the current match is strictly * smaller than this value. This mechanism is used only for compression * levels >= 4. */ // That's alias to max_lazy_match, don't use directly //this.max_insert_length = 0; /* Insert new strings in the hash table only if the match length is not * greater than this length. This saves time but degrades compression. * max_insert_length is used only for compression levels <= 3. */ this.level = 0; /* compression level (1..9) */ this.strategy = 0; /* favor or force Huffman coding*/ this.good_match = 0; /* Use a faster search when the previous match is longer than this */ this.nice_match = 0; /* Stop searching when current match exceeds this */ /* used by trees.c: */ /* Didn't use ct_data typedef below to suppress compiler warning */ // struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */ // struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */ // struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */ // Use flat array of DOUBLE size, with interleaved fata, // because JS does not support effective this.dyn_ltree = new utils.Buf16(HEAP_SIZE * 2); this.dyn_dtree = new utils.Buf16((2 * D_CODES + 1) * 2); this.bl_tree = new utils.Buf16((2 * BL_CODES + 1) * 2); zero(this.dyn_ltree); zero(this.dyn_dtree); zero(this.bl_tree); this.l_desc = null; /* desc. for literal tree */ this.d_desc = null; /* desc. for distance tree */ this.bl_desc = null; /* desc. for bit length tree */ //ush bl_count[MAX_BITS+1]; this.bl_count = new utils.Buf16(MAX_BITS + 1); /* number of codes at each bit length for an optimal tree */ //int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */ this.heap = new utils.Buf16(2 * L_CODES + 1); /* heap used to build the Huffman trees */ zero(this.heap); this.heap_len = 0; /* number of elements in the heap */ this.heap_max = 0; /* element of largest frequency */ /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. * The same heap array is used to build all trees. */ this.depth = new utils.Buf16(2 * L_CODES + 1); //uch depth[2*L_CODES+1]; zero(this.depth); /* Depth of each subtree used as tie breaker for trees of equal frequency */ this.l_buf = 0; /* buffer index for literals or lengths */ this.lit_bufsize = 0; /* Size of match buffer for literals/lengths. There are 4 reasons for * limiting lit_bufsize to 64K: * - frequencies can be kept in 16 bit counters * - if compression is not successful for the first block, all input * data is still in the window so we can still emit a stored block even * when input comes from standard input. (This can also be done for * all blocks if lit_bufsize is not greater than 32K.) * - if compression is not successful for a file smaller than 64K, we can * even emit a stored file instead of a stored block (saving 5 bytes). * This is applicable only for zip (not gzip or zlib). * - creating new Huffman trees less frequently may not provide fast * adaptation to changes in the input data statistics. (Take for * example a binary file with poorly compressible code followed by * a highly compressible string table.) Smaller buffer sizes give * fast adaptation but have of course the overhead of transmitting * trees more frequently. * - I can't count above 4 */ this.last_lit = 0; /* running index in l_buf */ this.d_buf = 0; /* Buffer index for distances. To simplify the code, d_buf and l_buf have * the same number of elements. To use different lengths, an extra flag * array would be necessary. */ this.opt_len = 0; /* bit length of current block with optimal trees */ this.static_len = 0; /* bit length of current block with static trees */ this.matches = 0; /* number of string matches in current block */ this.insert = 0; /* bytes at end of window left to insert */ this.bi_buf = 0; /* Output buffer. bits are inserted starting at the bottom (least * significant bits). */ this.bi_valid = 0; /* Number of valid bits in bi_buf. All bits above the last valid bit * are always zero. */ // Used for window memory init. We safely ignore it for JS. That makes // sense only for pointers and memory check tools. //this.high_water = 0; /* High water mark offset in window for initialized bytes -- bytes above * this are set to zero in order to avoid memory check warnings when * longest match routines access bytes past the input. This is then * updated to the new high water mark. */ } function deflateResetKeep(strm) { var s; if (!strm || !strm.state) { return err(strm, Z_STREAM_ERROR); } strm.total_in = strm.total_out = 0; strm.data_type = Z_UNKNOWN; s = strm.state; s.pending = 0; s.pending_out = 0; if (s.wrap < 0) { s.wrap = -s.wrap; /* was made negative by deflate(..., Z_FINISH); */ } s.status = (s.wrap ? INIT_STATE : BUSY_STATE); strm.adler = (s.wrap === 2) ? 0 // crc32(0, Z_NULL, 0) : 1; // adler32(0, Z_NULL, 0) s.last_flush = Z_NO_FLUSH; trees._tr_init(s); return Z_OK; } function deflateReset(strm) { var ret = deflateResetKeep(strm); if (ret === Z_OK) { lm_init(strm.state); } return ret; } function deflateSetHeader(strm, head) { if (!strm || !strm.state) { return Z_STREAM_ERROR; } if (strm.state.wrap !== 2) { return Z_STREAM_ERROR; } strm.state.gzhead = head; return Z_OK; } function deflateInit2(strm, level, method, windowBits, memLevel, strategy) { if (!strm) { // === Z_NULL return Z_STREAM_ERROR; } var wrap = 1; if (level === Z_DEFAULT_COMPRESSION) { level = 6; } if (windowBits < 0) { /* suppress zlib wrapper */ wrap = 0; windowBits = -windowBits; } else if (windowBits > 15) { wrap = 2; /* write gzip wrapper instead */ windowBits -= 16; } if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED || windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { return err(strm, Z_STREAM_ERROR); } if (windowBits === 8) { windowBits = 9; } /* until 256-byte window bug fixed */ var s = new DeflateState(); strm.state = s; s.strm = strm; s.wrap = wrap; s.gzhead = null; s.w_bits = windowBits; s.w_size = 1 << s.w_bits; s.w_mask = s.w_size - 1; s.hash_bits = memLevel + 7; s.hash_size = 1 << s.hash_bits; s.hash_mask = s.hash_size - 1; s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH); s.window = new utils.Buf8(s.w_size * 2); s.head = new utils.Buf16(s.hash_size); s.prev = new utils.Buf16(s.w_size); // Don't need mem init magic for JS. //s.high_water = 0; /* nothing written to s->window yet */ s.lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ s.pending_buf_size = s.lit_bufsize * 4; //overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); //s->pending_buf = (uchf *) overlay; s.pending_buf = new utils.Buf8(s.pending_buf_size); // It is offset from `s.pending_buf` (size is `s.lit_bufsize * 2`) //s->d_buf = overlay + s->lit_bufsize/sizeof(ush); s.d_buf = 1 * s.lit_bufsize; //s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize; s.l_buf = (1 + 2) * s.lit_bufsize; s.level = level; s.strategy = strategy; s.method = method; return deflateReset(strm); } function deflateInit(strm, level) { return deflateInit2(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY); } function deflate(strm, flush) { var old_flush, s; var beg, val; // for gzip header write only if (!strm || !strm.state || flush > Z_BLOCK || flush < 0) { return strm ? err(strm, Z_STREAM_ERROR) : Z_STREAM_ERROR; } s = strm.state; if (!strm.output || (!strm.input && strm.avail_in !== 0) || (s.status === FINISH_STATE && flush !== Z_FINISH)) { return err(strm, (strm.avail_out === 0) ? Z_BUF_ERROR : Z_STREAM_ERROR); } s.strm = strm; /* just in case */ old_flush = s.last_flush; s.last_flush = flush; /* Write the header */ if (s.status === INIT_STATE) { if (s.wrap === 2) { // GZIP header strm.adler = 0; //crc32(0L, Z_NULL, 0); put_byte(s, 31); put_byte(s, 139); put_byte(s, 8); if (!s.gzhead) { // s->gzhead == Z_NULL put_byte(s, 0); put_byte(s, 0); put_byte(s, 0); put_byte(s, 0); put_byte(s, 0); put_byte(s, s.level === 9 ? 2 : (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ? 4 : 0)); put_byte(s, OS_CODE); s.status = BUSY_STATE; } else { put_byte(s, (s.gzhead.text ? 1 : 0) + (s.gzhead.hcrc ? 2 : 0) + (!s.gzhead.extra ? 0 : 4) + (!s.gzhead.name ? 0 : 8) + (!s.gzhead.comment ? 0 : 16) ); put_byte(s, s.gzhead.time & 0xff); put_byte(s, (s.gzhead.time >> 8) & 0xff); put_byte(s, (s.gzhead.time >> 16) & 0xff); put_byte(s, (s.gzhead.time >> 24) & 0xff); put_byte(s, s.level === 9 ? 2 : (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ? 4 : 0)); put_byte(s, s.gzhead.os & 0xff); if (s.gzhead.extra && s.gzhead.extra.length) { put_byte(s, s.gzhead.extra.length & 0xff); put_byte(s, (s.gzhead.extra.length >> 8) & 0xff); } if (s.gzhead.hcrc) { strm.adler = crc32(strm.adler, s.pending_buf, s.pending, 0); } s.gzindex = 0; s.status = EXTRA_STATE; } } else // DEFLATE header { var header = (Z_DEFLATED + ((s.w_bits - 8) << 4)) << 8; var level_flags = -1; if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) { level_flags = 0; } else if (s.level < 6) { level_flags = 1; } else if (s.level === 6) { level_flags = 2; } else { level_flags = 3; } header |= (level_flags << 6); if (s.strstart !== 0) { header |= PRESET_DICT; } header += 31 - (header % 31); s.status = BUSY_STATE; putShortMSB(s, header); /* Save the adler32 of the preset dictionary: */ if (s.strstart !== 0) { putShortMSB(s, strm.adler >>> 16); putShortMSB(s, strm.adler & 0xffff); } strm.adler = 1; // adler32(0L, Z_NULL, 0); } } //#ifdef GZIP if (s.status === EXTRA_STATE) { if (s.gzhead.extra/* != Z_NULL*/) { beg = s.pending; /* start of bytes to update crc */ while (s.gzindex < (s.gzhead.extra.length & 0xffff)) { if (s.pending === s.pending_buf_size) { if (s.gzhead.hcrc && s.pending > beg) { strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg); } flush_pending(strm); beg = s.pending; if (s.pending === s.pending_buf_size) { break; } } put_byte(s, s.gzhead.extra[s.gzindex] & 0xff); s.gzindex++; } if (s.gzhead.hcrc && s.pending > beg) { strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg); } if (s.gzindex === s.gzhead.extra.length) { s.gzindex = 0; s.status = NAME_STATE; } } else { s.status = NAME_STATE; } } if (s.status === NAME_STATE) { if (s.gzhead.name/* != Z_NULL*/) { beg = s.pending; /* start of bytes to update crc */ //int val; do { if (s.pending === s.pending_buf_size) { if (s.gzhead.hcrc && s.pending > beg) { strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg); } flush_pending(strm); beg = s.pending; if (s.pending === s.pending_buf_size) { val = 1; break; } } // JS specific: little magic to add zero terminator to end of string if (s.gzindex < s.gzhead.name.length) { val = s.gzhead.name.charCodeAt(s.gzindex++) & 0xff; } else { val = 0; } put_byte(s, val); } while (val !== 0); if (s.gzhead.hcrc && s.pending > beg) { strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg); } if (val === 0) { s.gzindex = 0; s.status = COMMENT_STATE; } } else { s.status = COMMENT_STATE; } } if (s.status === COMMENT_STATE) { if (s.gzhead.comment/* != Z_NULL*/) { beg = s.pending; /* start of bytes to update crc */ //int val; do { if (s.pending === s.pending_buf_size) { if (s.gzhead.hcrc && s.pending > beg) { strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg); } flush_pending(strm); beg = s.pending; if (s.pending === s.pending_buf_size) { val = 1; break; } } // JS specific: little magic to add zero terminator to end of string if (s.gzindex < s.gzhead.comment.length) { val = s.gzhead.comment.charCodeAt(s.gzindex++) & 0xff; } else { val = 0; } put_byte(s, val); } while (val !== 0); if (s.gzhead.hcrc && s.pending > beg) { strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg); } if (val === 0) { s.status = HCRC_STATE; } } else { s.status = HCRC_STATE; } } if (s.status === HCRC_STATE) { if (s.gzhead.hcrc) { if (s.pending + 2 > s.pending_buf_size) { flush_pending(strm); } if (s.pending + 2 <= s.pending_buf_size) { put_byte(s, strm.adler & 0xff); put_byte(s, (strm.adler >> 8) & 0xff); strm.adler = 0; //crc32(0L, Z_NULL, 0); s.status = BUSY_STATE; } } else { s.status = BUSY_STATE; } } //#endif /* Flush as much pending output as possible */ if (s.pending !== 0) { flush_pending(strm); if (strm.avail_out === 0) { /* Since avail_out is 0, deflate will be called again with * more output space, but possibly with both pending and * avail_in equal to zero. There won't be anything to do, * but this is not an error situation so make sure we * return OK instead of BUF_ERROR at next call of deflate: */ s.last_flush = -1; return Z_OK; } /* Make sure there is something to do and avoid duplicate consecutive * flushes. For repeated and useless calls with Z_FINISH, we keep * returning Z_STREAM_END instead of Z_BUF_ERROR. */ } else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) && flush !== Z_FINISH) { return err(strm, Z_BUF_ERROR); } /* User must not provide more input after the first FINISH: */ if (s.status === FINISH_STATE && strm.avail_in !== 0) { return err(strm, Z_BUF_ERROR); } /* Start a new block or continue the current one. */ if (strm.avail_in !== 0 || s.lookahead !== 0 || (flush !== Z_NO_FLUSH && s.status !== FINISH_STATE)) { var bstate = (s.strategy === Z_HUFFMAN_ONLY) ? deflate_huff(s, flush) : (s.strategy === Z_RLE ? deflate_rle(s, flush) : configuration_table[s.level].func(s, flush)); if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) { s.status = FINISH_STATE; } if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) { if (strm.avail_out === 0) { s.last_flush = -1; /* avoid BUF_ERROR next call, see above */ } return Z_OK; /* If flush != Z_NO_FLUSH && avail_out == 0, the next call * of deflate should use the same flush parameter to make sure * that the flush is complete. So we don't have to output an * empty block here, this will be done at next call. This also * ensures that for a very small output buffer, we emit at most * one empty block. */ } if (bstate === BS_BLOCK_DONE) { if (flush === Z_PARTIAL_FLUSH) { trees._tr_align(s); } else if (flush !== Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */ trees._tr_stored_block(s, 0, 0, false); /* For a full flush, this empty block will be recognized * as a special marker by inflate_sync(). */ if (flush === Z_FULL_FLUSH) { /*** CLEAR_HASH(s); ***/ /* forget history */ zero(s.head); // Fill with NIL (= 0); if (s.lookahead === 0) { s.strstart = 0; s.block_start = 0; s.insert = 0; } } } flush_pending(strm); if (strm.avail_out === 0) { s.last_flush = -1; /* avoid BUF_ERROR at next call, see above */ return Z_OK; } } } //Assert(strm->avail_out > 0, "bug2"); //if (strm.avail_out <= 0) { throw new Error("bug2");} if (flush !== Z_FINISH) { return Z_OK; } if (s.wrap <= 0) { return Z_STREAM_END; } /* Write the trailer */ if (s.wrap === 2) { put_byte(s, strm.adler & 0xff); put_byte(s, (strm.adler >> 8) & 0xff); put_byte(s, (strm.adler >> 16) & 0xff); put_byte(s, (strm.adler >> 24) & 0xff); put_byte(s, strm.total_in & 0xff); put_byte(s, (strm.total_in >> 8) & 0xff); put_byte(s, (strm.total_in >> 16) & 0xff); put_byte(s, (strm.total_in >> 24) & 0xff); } else { putShortMSB(s, strm.adler >>> 16); putShortMSB(s, strm.adler & 0xffff); } flush_pending(strm); /* If avail_out is zero, the application will call deflate again * to flush the rest. */ if (s.wrap > 0) { s.wrap = -s.wrap; } /* write the trailer only once! */ return s.pending !== 0 ? Z_OK : Z_STREAM_END; } function deflateEnd(strm) { var status; if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) { return Z_STREAM_ERROR; } status = strm.state.status; if (status !== INIT_STATE && status !== EXTRA_STATE && status !== NAME_STATE && status !== COMMENT_STATE && status !== HCRC_STATE && status !== BUSY_STATE && status !== FINISH_STATE ) { return err(strm, Z_STREAM_ERROR); } strm.state = null; return status === BUSY_STATE ? err(strm, Z_DATA_ERROR) : Z_OK; } /* ========================================================================= * Initializes the compression dictionary from the given byte * sequence without producing any compressed output. */ function deflateSetDictionary(strm, dictionary) { var dictLength = dictionary.length; var s; var str, n; var wrap; var avail; var next; var input; var tmpDict; if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) { return Z_STREAM_ERROR; } s = strm.state; wrap = s.wrap; if (wrap === 2 || (wrap === 1 && s.status !== INIT_STATE) || s.lookahead) { return Z_STREAM_ERROR; } /* when using zlib wrappers, compute Adler-32 for provided dictionary */ if (wrap === 1) { /* adler32(strm->adler, dictionary, dictLength); */ strm.adler = adler32(strm.adler, dictionary, dictLength, 0); } s.wrap = 0; /* avoid computing Adler-32 in read_buf */ /* if dictionary would fill window, just replace the history */ if (dictLength >= s.w_size) { if (wrap === 0) { /* already empty otherwise */ /*** CLEAR_HASH(s); ***/ zero(s.head); // Fill with NIL (= 0); s.strstart = 0; s.block_start = 0; s.insert = 0; } /* use the tail */ // dictionary = dictionary.slice(dictLength - s.w_size); tmpDict = new utils.Buf8(s.w_size); utils.arraySet(tmpDict, dictionary, dictLength - s.w_size, s.w_size, 0); dictionary = tmpDict; dictLength = s.w_size; } /* insert dictionary into window and hash */ avail = strm.avail_in; next = strm.next_in; input = strm.input; strm.avail_in = dictLength; strm.next_in = 0; strm.input = dictionary; fill_window(s); while (s.lookahead >= MIN_MATCH) { str = s.strstart; n = s.lookahead - (MIN_MATCH - 1); do { /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */ s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH - 1]) & s.hash_mask; s.prev[str & s.w_mask] = s.head[s.ins_h]; s.head[s.ins_h] = str; str++; } while (--n); s.strstart = str; s.lookahead = MIN_MATCH - 1; fill_window(s); } s.strstart += s.lookahead; s.block_start = s.strstart; s.insert = s.lookahead; s.lookahead = 0; s.match_length = s.prev_length = MIN_MATCH - 1; s.match_available = 0; strm.next_in = next; strm.input = input; strm.avail_in = avail; s.wrap = wrap; return Z_OK; } exports.deflateInit = deflateInit; exports.deflateInit2 = deflateInit2; exports.deflateReset = deflateReset; exports.deflateResetKeep = deflateResetKeep; exports.deflateSetHeader = deflateSetHeader; exports.deflate = deflate; exports.deflateEnd = deflateEnd; exports.deflateSetDictionary = deflateSetDictionary; exports.deflateInfo = 'pako deflate (from Nodeca project)'; /* Not implemented exports.deflateBound = deflateBound; exports.deflateCopy = deflateCopy; exports.deflateParams = deflateParams; exports.deflatePending = deflatePending; exports.deflatePrime = deflatePrime; exports.deflateTune = deflateTune; */ },{"../utils/common":140,"./adler32":141,"./crc32":143,"./messages":148,"./trees":149}],145:[function(require,module,exports){ 'use strict'; // See state defs from inflate.js var BAD = 30; /* got a data error -- remain here until reset */ var TYPE = 12; /* i: waiting for type bits, including last-flag bit */ /* Decode literal, length, and distance codes and write out the resulting literal and match bytes until either not enough input or output is available, an end-of-block is encountered, or a data error is encountered. When large enough input and output buffers are supplied to inflate(), for example, a 16K input buffer and a 64K output buffer, more than 95% of the inflate execution time is spent in this routine. Entry assumptions: state.mode === LEN strm.avail_in >= 6 strm.avail_out >= 258 start >= strm.avail_out state.bits < 8 On return, state.mode is one of: LEN -- ran out of enough output space or enough available input TYPE -- reached end of block code, inflate() to interpret next block BAD -- error in block data Notes: - The maximum input bits used by a length/distance pair is 15 bits for the length code, 5 bits for the length extra, 15 bits for the distance code, and 13 bits for the distance extra. This totals 48 bits, or six bytes. Therefore if strm.avail_in >= 6, then there is enough input to avoid checking for available input while decoding. - The maximum bytes that a single length/distance pair can output is 258 bytes, which is the maximum length that can be coded. inflate_fast() requires strm.avail_out >= 258 for each loop to avoid checking for output space. */ module.exports = function inflate_fast(strm, start) { var state; var _in; /* local strm.input */ var last; /* have enough input while in < last */ var _out; /* local strm.output */ var beg; /* inflate()'s initial strm.output */ var end; /* while out < end, enough space available */ //#ifdef INFLATE_STRICT var dmax; /* maximum distance from zlib header */ //#endif var wsize; /* window size or zero if not using window */ var whave; /* valid bytes in the window */ var wnext; /* window write index */ // Use `s_window` instead `window`, avoid conflict with instrumentation tools var s_window; /* allocated sliding window, if wsize != 0 */ var hold; /* local strm.hold */ var bits; /* local strm.bits */ var lcode; /* local strm.lencode */ var dcode; /* local strm.distcode */ var lmask; /* mask for first level of length codes */ var dmask; /* mask for first level of distance codes */ var here; /* retrieved table entry */ var op; /* code bits, operation, extra bits, or */ /* window position, window bytes to copy */ var len; /* match length, unused bytes */ var dist; /* match distance */ var from; /* where to copy match from */ var from_source; var input, output; // JS specific, because we have no pointers /* copy state to local variables */ state = strm.state; //here = state.here; _in = strm.next_in; input = strm.input; last = _in + (strm.avail_in - 5); _out = strm.next_out; output = strm.output; beg = _out - (start - strm.avail_out); end = _out + (strm.avail_out - 257); //#ifdef INFLATE_STRICT dmax = state.dmax; //#endif wsize = state.wsize; whave = state.whave; wnext = state.wnext; s_window = state.window; hold = state.hold; bits = state.bits; lcode = state.lencode; dcode = state.distcode; lmask = (1 << state.lenbits) - 1; dmask = (1 << state.distbits) - 1; /* decode literals and length/distances until end-of-block or not enough input data or output space */ top: do { if (bits < 15) { hold += input[_in++] << bits; bits += 8; hold += input[_in++] << bits; bits += 8; } here = lcode[hold & lmask]; dolen: for (;;) { // Goto emulation op = here >>> 24/*here.bits*/; hold >>>= op; bits -= op; op = (here >>> 16) & 0xff/*here.op*/; if (op === 0) { /* literal */ //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? // "inflate: literal '%c'\n" : // "inflate: literal 0x%02x\n", here.val)); output[_out++] = here & 0xffff/*here.val*/; } else if (op & 16) { /* length base */ len = here & 0xffff/*here.val*/; op &= 15; /* number of extra bits */ if (op) { if (bits < op) { hold += input[_in++] << bits; bits += 8; } len += hold & ((1 << op) - 1); hold >>>= op; bits -= op; } //Tracevv((stderr, "inflate: length %u\n", len)); if (bits < 15) { hold += input[_in++] << bits; bits += 8; hold += input[_in++] << bits; bits += 8; } here = dcode[hold & dmask]; dodist: for (;;) { // goto emulation op = here >>> 24/*here.bits*/; hold >>>= op; bits -= op; op = (here >>> 16) & 0xff/*here.op*/; if (op & 16) { /* distance base */ dist = here & 0xffff/*here.val*/; op &= 15; /* number of extra bits */ if (bits < op) { hold += input[_in++] << bits; bits += 8; if (bits < op) { hold += input[_in++] << bits; bits += 8; } } dist += hold & ((1 << op) - 1); //#ifdef INFLATE_STRICT if (dist > dmax) { strm.msg = 'invalid distance too far back'; state.mode = BAD; break top; } //#endif hold >>>= op; bits -= op; //Tracevv((stderr, "inflate: distance %u\n", dist)); op = _out - beg; /* max distance in output */ if (dist > op) { /* see if copy from window */ op = dist - op; /* distance back in window */ if (op > whave) { if (state.sane) { strm.msg = 'invalid distance too far back'; state.mode = BAD; break top; } // (!) This block is disabled in zlib defailts, // don't enable it for binary compatibility //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR // if (len <= op - whave) { // do { // output[_out++] = 0; // } while (--len); // continue top; // } // len -= op - whave; // do { // output[_out++] = 0; // } while (--op > whave); // if (op === 0) { // from = _out - dist; // do { // output[_out++] = output[from++]; // } while (--len); // continue top; // } //#endif } from = 0; // window index from_source = s_window; if (wnext === 0) { /* very common case */ from += wsize - op; if (op < len) { /* some from window */ len -= op; do { output[_out++] = s_window[from++]; } while (--op); from = _out - dist; /* rest from output */ from_source = output; } } else if (wnext < op) { /* wrap around window */ from += wsize + wnext - op; op -= wnext; if (op < len) { /* some from end of window */ len -= op; do { output[_out++] = s_window[from++]; } while (--op); from = 0; if (wnext < len) { /* some from start of window */ op = wnext; len -= op; do { output[_out++] = s_window[from++]; } while (--op); from = _out - dist; /* rest from output */ from_source = output; } } } else { /* contiguous in window */ from += wnext - op; if (op < len) { /* some from window */ len -= op; do { output[_out++] = s_window[from++]; } while (--op); from = _out - dist; /* rest from output */ from_source = output; } } while (len > 2) { output[_out++] = from_source[from++]; output[_out++] = from_source[from++]; output[_out++] = from_source[from++]; len -= 3; } if (len) { output[_out++] = from_source[from++]; if (len > 1) { output[_out++] = from_source[from++]; } } } else { from = _out - dist; /* copy direct from output */ do { /* minimum length is three */ output[_out++] = output[from++]; output[_out++] = output[from++]; output[_out++] = output[from++]; len -= 3; } while (len > 2); if (len) { output[_out++] = output[from++]; if (len > 1) { output[_out++] = output[from++]; } } } } else if ((op & 64) === 0) { /* 2nd level distance code */ here = dcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))]; continue dodist; } else { strm.msg = 'invalid distance code'; state.mode = BAD; break top; } break; // need to emulate goto via "continue" } } else if ((op & 64) === 0) { /* 2nd level length code */ here = lcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))]; continue dolen; } else if (op & 32) { /* end-of-block */ //Tracevv((stderr, "inflate: end of block\n")); state.mode = TYPE; break top; } else { strm.msg = 'invalid literal/length code'; state.mode = BAD; break top; } break; // need to emulate goto via "continue" } } while (_in < last && _out < end); /* return unused bytes (on entry, bits < 8, so in won't go too far back) */ len = bits >> 3; _in -= len; bits -= len << 3; hold &= (1 << bits) - 1; /* update state and return */ strm.next_in = _in; strm.next_out = _out; strm.avail_in = (_in < last ? 5 + (last - _in) : 5 - (_in - last)); strm.avail_out = (_out < end ? 257 + (end - _out) : 257 - (_out - end)); state.hold = hold; state.bits = bits; return; }; },{}],146:[function(require,module,exports){ 'use strict'; var utils = require('../utils/common'); var adler32 = require('./adler32'); var crc32 = require('./crc32'); var inflate_fast = require('./inffast'); var inflate_table = require('./inftrees'); var CODES = 0; var LENS = 1; var DISTS = 2; /* Public constants ==========================================================*/ /* ===========================================================================*/ /* Allowed flush values; see deflate() and inflate() below for details */ //var Z_NO_FLUSH = 0; //var Z_PARTIAL_FLUSH = 1; //var Z_SYNC_FLUSH = 2; //var Z_FULL_FLUSH = 3; var Z_FINISH = 4; var Z_BLOCK = 5; var Z_TREES = 6; /* Return codes for the compression/decompression functions. Negative values * are errors, positive values are used for special but normal events. */ var Z_OK = 0; var Z_STREAM_END = 1; var Z_NEED_DICT = 2; //var Z_ERRNO = -1; var Z_STREAM_ERROR = -2; var Z_DATA_ERROR = -3; var Z_MEM_ERROR = -4; var Z_BUF_ERROR = -5; //var Z_VERSION_ERROR = -6; /* The deflate compression method */ var Z_DEFLATED = 8; /* STATES ====================================================================*/ /* ===========================================================================*/ var HEAD = 1; /* i: waiting for magic header */ var FLAGS = 2; /* i: waiting for method and flags (gzip) */ var TIME = 3; /* i: waiting for modification time (gzip) */ var OS = 4; /* i: waiting for extra flags and operating system (gzip) */ var EXLEN = 5; /* i: waiting for extra length (gzip) */ var EXTRA = 6; /* i: waiting for extra bytes (gzip) */ var NAME = 7; /* i: waiting for end of file name (gzip) */ var COMMENT = 8; /* i: waiting for end of comment (gzip) */ var HCRC = 9; /* i: waiting for header crc (gzip) */ var DICTID = 10; /* i: waiting for dictionary check value */ var DICT = 11; /* waiting for inflateSetDictionary() call */ var TYPE = 12; /* i: waiting for type bits, including last-flag bit */ var TYPEDO = 13; /* i: same, but skip check to exit inflate on new block */ var STORED = 14; /* i: waiting for stored size (length and complement) */ var COPY_ = 15; /* i/o: same as COPY below, but only first time in */ var COPY = 16; /* i/o: waiting for input or output to copy stored block */ var TABLE = 17; /* i: waiting for dynamic block table lengths */ var LENLENS = 18; /* i: waiting for code length code lengths */ var CODELENS = 19; /* i: waiting for length/lit and distance code lengths */ var LEN_ = 20; /* i: same as LEN below, but only first time in */ var LEN = 21; /* i: waiting for length/lit/eob code */ var LENEXT = 22; /* i: waiting for length extra bits */ var DIST = 23; /* i: waiting for distance code */ var DISTEXT = 24; /* i: waiting for distance extra bits */ var MATCH = 25; /* o: waiting for output space to copy string */ var LIT = 26; /* o: waiting for output space to write literal */ var CHECK = 27; /* i: waiting for 32-bit check value */ var LENGTH = 28; /* i: waiting for 32-bit length (gzip) */ var DONE = 29; /* finished check, done -- remain here until reset */ var BAD = 30; /* got a data error -- remain here until reset */ var MEM = 31; /* got an inflate() memory error -- remain here until reset */ var SYNC = 32; /* looking for synchronization bytes to restart inflate() */ /* ===========================================================================*/ var ENOUGH_LENS = 852; var ENOUGH_DISTS = 592; //var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS); var MAX_WBITS = 15; /* 32K LZ77 window */ var DEF_WBITS = MAX_WBITS; function zswap32(q) { return (((q >>> 24) & 0xff) + ((q >>> 8) & 0xff00) + ((q & 0xff00) << 8) + ((q & 0xff) << 24)); } function InflateState() { this.mode = 0; /* current inflate mode */ this.last = false; /* true if processing last block */ this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */ this.havedict = false; /* true if dictionary provided */ this.flags = 0; /* gzip header method and flags (0 if zlib) */ this.dmax = 0; /* zlib header max distance (INFLATE_STRICT) */ this.check = 0; /* protected copy of check value */ this.total = 0; /* protected copy of output count */ // TODO: may be {} this.head = null; /* where to save gzip header information */ /* sliding window */ this.wbits = 0; /* log base 2 of requested window size */ this.wsize = 0; /* window size or zero if not using window */ this.whave = 0; /* valid bytes in the window */ this.wnext = 0; /* window write index */ this.window = null; /* allocated sliding window, if needed */ /* bit accumulator */ this.hold = 0; /* input bit accumulator */ this.bits = 0; /* number of bits in "in" */ /* for string and stored block copying */ this.length = 0; /* literal or length of data to copy */ this.offset = 0; /* distance back to copy string from */ /* for table and code decoding */ this.extra = 0; /* extra bits needed */ /* fixed and dynamic code tables */ this.lencode = null; /* starting table for length/literal codes */ this.distcode = null; /* starting table for distance codes */ this.lenbits = 0; /* index bits for lencode */ this.distbits = 0; /* index bits for distcode */ /* dynamic table building */ this.ncode = 0; /* number of code length code lengths */ this.nlen = 0; /* number of length code lengths */ this.ndist = 0; /* number of distance code lengths */ this.have = 0; /* number of code lengths in lens[] */ this.next = null; /* next available space in codes[] */ this.lens = new utils.Buf16(320); /* temporary storage for code lengths */ this.work = new utils.Buf16(288); /* work area for code table building */ /* because we don't have pointers in js, we use lencode and distcode directly as buffers so we don't need codes */ //this.codes = new utils.Buf32(ENOUGH); /* space for code tables */ this.lendyn = null; /* dynamic table for length/literal codes (JS specific) */ this.distdyn = null; /* dynamic table for distance codes (JS specific) */ this.sane = 0; /* if false, allow invalid distance too far */ this.back = 0; /* bits back of last unprocessed length/lit */ this.was = 0; /* initial length of match */ } function inflateResetKeep(strm) { var state; if (!strm || !strm.state) { return Z_STREAM_ERROR; } state = strm.state; strm.total_in = strm.total_out = state.total = 0; strm.msg = ''; /*Z_NULL*/ if (state.wrap) { /* to support ill-conceived Java test suite */ strm.adler = state.wrap & 1; } state.mode = HEAD; state.last = 0; state.havedict = 0; state.dmax = 32768; state.head = null/*Z_NULL*/; state.hold = 0; state.bits = 0; //state.lencode = state.distcode = state.next = state.codes; state.lencode = state.lendyn = new utils.Buf32(ENOUGH_LENS); state.distcode = state.distdyn = new utils.Buf32(ENOUGH_DISTS); state.sane = 1; state.back = -1; //Tracev((stderr, "inflate: reset\n")); return Z_OK; } function inflateReset(strm) { var state; if (!strm || !strm.state) { return Z_STREAM_ERROR; } state = strm.state; state.wsize = 0; state.whave = 0; state.wnext = 0; return inflateResetKeep(strm); } function inflateReset2(strm, windowBits) { var wrap; var state; /* get the state */ if (!strm || !strm.state) { return Z_STREAM_ERROR; } state = strm.state; /* extract wrap request from windowBits parameter */ if (windowBits < 0) { wrap = 0; windowBits = -windowBits; } else { wrap = (windowBits >> 4) + 1; if (windowBits < 48) { windowBits &= 15; } } /* set number of window bits, free window if different */ if (windowBits && (windowBits < 8 || windowBits > 15)) { return Z_STREAM_ERROR; } if (state.window !== null && state.wbits !== windowBits) { state.window = null; } /* update state and reset the rest of it */ state.wrap = wrap; state.wbits = windowBits; return inflateReset(strm); } function inflateInit2(strm, windowBits) { var ret; var state; if (!strm) { return Z_STREAM_ERROR; } //strm.msg = Z_NULL; /* in case we return an error */ state = new InflateState(); //if (state === Z_NULL) return Z_MEM_ERROR; //Tracev((stderr, "inflate: allocated\n")); strm.state = state; state.window = null/*Z_NULL*/; ret = inflateReset2(strm, windowBits); if (ret !== Z_OK) { strm.state = null/*Z_NULL*/; } return ret; } function inflateInit(strm) { return inflateInit2(strm, DEF_WBITS); } /* Return state with length and distance decoding tables and index sizes set to fixed code decoding. Normally this returns fixed tables from inffixed.h. If BUILDFIXED is defined, then instead this routine builds the tables the first time it's called, and returns those tables the first time and thereafter. This reduces the size of the code by about 2K bytes, in exchange for a little execution time. However, BUILDFIXED should not be used for threaded applications, since the rewriting of the tables and virgin may not be thread-safe. */ var virgin = true; var lenfix, distfix; // We have no pointers in JS, so keep tables separate function fixedtables(state) { /* build fixed huffman tables if first call (may not be thread safe) */ if (virgin) { var sym; lenfix = new utils.Buf32(512); distfix = new utils.Buf32(32); /* literal/length table */ sym = 0; while (sym < 144) { state.lens[sym++] = 8; } while (sym < 256) { state.lens[sym++] = 9; } while (sym < 280) { state.lens[sym++] = 7; } while (sym < 288) { state.lens[sym++] = 8; } inflate_table(LENS, state.lens, 0, 288, lenfix, 0, state.work, { bits: 9 }); /* distance table */ sym = 0; while (sym < 32) { state.lens[sym++] = 5; } inflate_table(DISTS, state.lens, 0, 32, distfix, 0, state.work, { bits: 5 }); /* do this just once */ virgin = false; } state.lencode = lenfix; state.lenbits = 9; state.distcode = distfix; state.distbits = 5; } /* Update the window with the last wsize (normally 32K) bytes written before returning. If window does not exist yet, create it. This is only called when a window is already in use, or when output has been written during this inflate call, but the end of the deflate stream has not been reached yet. It is also called to create a window for dictionary data when a dictionary is loaded. Providing output buffers larger than 32K to inflate() should provide a speed advantage, since only the last 32K of output is copied to the sliding window upon return from inflate(), and since all distances after the first 32K of output will fall in the output data, making match copies simpler and faster. The advantage may be dependent on the size of the processor's data caches. */ function updatewindow(strm, src, end, copy) { var dist; var state = strm.state; /* if it hasn't been done already, allocate space for the window */ if (state.window === null) { state.wsize = 1 << state.wbits; state.wnext = 0; state.whave = 0; state.window = new utils.Buf8(state.wsize); } /* copy state->wsize or less output bytes into the circular window */ if (copy >= state.wsize) { utils.arraySet(state.window, src, end - state.wsize, state.wsize, 0); state.wnext = 0; state.whave = state.wsize; } else { dist = state.wsize - state.wnext; if (dist > copy) { dist = copy; } //zmemcpy(state->window + state->wnext, end - copy, dist); utils.arraySet(state.window, src, end - copy, dist, state.wnext); copy -= dist; if (copy) { //zmemcpy(state->window, end - copy, copy); utils.arraySet(state.window, src, end - copy, copy, 0); state.wnext = copy; state.whave = state.wsize; } else { state.wnext += dist; if (state.wnext === state.wsize) { state.wnext = 0; } if (state.whave < state.wsize) { state.whave += dist; } } } return 0; } function inflate(strm, flush) { var state; var input, output; // input/output buffers var next; /* next input INDEX */ var put; /* next output INDEX */ var have, left; /* available input and output */ var hold; /* bit buffer */ var bits; /* bits in bit buffer */ var _in, _out; /* save starting available input and output */ var copy; /* number of stored or match bytes to copy */ var from; /* where to copy match bytes from */ var from_source; var here = 0; /* current decoding table entry */ var here_bits, here_op, here_val; // paked "here" denormalized (JS specific) //var last; /* parent table entry */ var last_bits, last_op, last_val; // paked "last" denormalized (JS specific) var len; /* length to copy for repeats, bits to drop */ var ret; /* return code */ var hbuf = new utils.Buf8(4); /* buffer for gzip header crc calculation */ var opts; var n; // temporary var for NEED_BITS var order = /* permutation of code lengths */ [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ]; if (!strm || !strm.state || !strm.output || (!strm.input && strm.avail_in !== 0)) { return Z_STREAM_ERROR; } state = strm.state; if (state.mode === TYPE) { state.mode = TYPEDO; } /* skip check */ //--- LOAD() --- put = strm.next_out; output = strm.output; left = strm.avail_out; next = strm.next_in; input = strm.input; have = strm.avail_in; hold = state.hold; bits = state.bits; //--- _in = have; _out = left; ret = Z_OK; inf_leave: // goto emulation for (;;) { switch (state.mode) { case HEAD: if (state.wrap === 0) { state.mode = TYPEDO; break; } //=== NEEDBITS(16); while (bits < 16) { if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; } //===// if ((state.wrap & 2) && hold === 0x8b1f) { /* gzip header */ state.check = 0/*crc32(0L, Z_NULL, 0)*/; //=== CRC2(state.check, hold); hbuf[0] = hold & 0xff; hbuf[1] = (hold >>> 8) & 0xff; state.check = crc32(state.check, hbuf, 2, 0); //===// //=== INITBITS(); hold = 0; bits = 0; //===// state.mode = FLAGS; break; } state.flags = 0; /* expect zlib header */ if (state.head) { state.head.done = false; } if (!(state.wrap & 1) || /* check if zlib header allowed */ (((hold & 0xff)/*BITS(8)*/ << 8) + (hold >> 8)) % 31) { strm.msg = 'incorrect header check'; state.mode = BAD; break; } if ((hold & 0x0f)/*BITS(4)*/ !== Z_DEFLATED) { strm.msg = 'unknown compression method'; state.mode = BAD; break; } //--- DROPBITS(4) ---// hold >>>= 4; bits -= 4; //---// len = (hold & 0x0f)/*BITS(4)*/ + 8; if (state.wbits === 0) { state.wbits = len; } else if (len > state.wbits) { strm.msg = 'invalid window size'; state.mode = BAD; break; } state.dmax = 1 << len; //Tracev((stderr, "inflate: zlib header ok\n")); strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/; state.mode = hold & 0x200 ? DICTID : TYPE; //=== INITBITS(); hold = 0; bits = 0; //===// break; case FLAGS: //=== NEEDBITS(16); */ while (bits < 16) { if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; } //===// state.flags = hold; if ((state.flags & 0xff) !== Z_DEFLATED) { strm.msg = 'unknown compression method'; state.mode = BAD; break; } if (state.flags & 0xe000) { strm.msg = 'unknown header flags set'; state.mode = BAD; break; } if (state.head) { state.head.text = ((hold >> 8) & 1); } if (state.flags & 0x0200) { //=== CRC2(state.check, hold); hbuf[0] = hold & 0xff; hbuf[1] = (hold >>> 8) & 0xff; state.check = crc32(state.check, hbuf, 2, 0); //===// } //=== INITBITS(); hold = 0; bits = 0; //===// state.mode = TIME; /* falls through */ case TIME: //=== NEEDBITS(32); */ while (bits < 32) { if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; } //===// if (state.head) { state.head.time = hold; } if (state.flags & 0x0200) { //=== CRC4(state.check, hold) hbuf[0] = hold & 0xff; hbuf[1] = (hold >>> 8) & 0xff; hbuf[2] = (hold >>> 16) & 0xff; hbuf[3] = (hold >>> 24) & 0xff; state.check = crc32(state.check, hbuf, 4, 0); //=== } //=== INITBITS(); hold = 0; bits = 0; //===// state.mode = OS; /* falls through */ case OS: //=== NEEDBITS(16); */ while (bits < 16) { if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; } //===// if (state.head) { state.head.xflags = (hold & 0xff); state.head.os = (hold >> 8); } if (state.flags & 0x0200) { //=== CRC2(state.check, hold); hbuf[0] = hold & 0xff; hbuf[1] = (hold >>> 8) & 0xff; state.check = crc32(state.check, hbuf, 2, 0); //===// } //=== INITBITS(); hold = 0; bits = 0; //===// state.mode = EXLEN; /* falls through */ case EXLEN: if (state.flags & 0x0400) { //=== NEEDBITS(16); */ while (bits < 16) { if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; } //===// state.length = hold; if (state.head) { state.head.extra_len = hold; } if (state.flags & 0x0200) { //=== CRC2(state.check, hold); hbuf[0] = hold & 0xff; hbuf[1] = (hold >>> 8) & 0xff; state.check = crc32(state.check, hbuf, 2, 0); //===// } //=== INITBITS(); hold = 0; bits = 0; //===// } else if (state.head) { state.head.extra = null/*Z_NULL*/; } state.mode = EXTRA; /* falls through */ case EXTRA: if (state.flags & 0x0400) { copy = state.length; if (copy > have) { copy = have; } if (copy) { if (state.head) { len = state.head.extra_len - state.length; if (!state.head.extra) { // Use untyped array for more conveniend processing later state.head.extra = new Array(state.head.extra_len); } utils.arraySet( state.head.extra, input, next, // extra field is limited to 65536 bytes // - no need for additional size check copy, /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/ len ); //zmemcpy(state.head.extra + len, next, // len + copy > state.head.extra_max ? // state.head.extra_max - len : copy); } if (state.flags & 0x0200) { state.check = crc32(state.check, input, copy, next); } have -= copy; next += copy; state.length -= copy; } if (state.length) { break inf_leave; } } state.length = 0; state.mode = NAME; /* falls through */ case NAME: if (state.flags & 0x0800) { if (have === 0) { break inf_leave; } copy = 0; do { // TODO: 2 or 1 bytes? len = input[next + copy++]; /* use constant limit because in js we should not preallocate memory */ if (state.head && len && (state.length < 65536 /*state.head.name_max*/)) { state.head.name += String.fromCharCode(len); } } while (len && copy < have); if (state.flags & 0x0200) { state.check = crc32(state.check, input, copy, next); } have -= copy; next += copy; if (len) { break inf_leave; } } else if (state.head) { state.head.name = null; } state.length = 0; state.mode = COMMENT; /* falls through */ case COMMENT: if (state.flags & 0x1000) { if (have === 0) { break inf_leave; } copy = 0; do { len = input[next + copy++]; /* use constant limit because in js we should not preallocate memory */ if (state.head && len && (state.length < 65536 /*state.head.comm_max*/)) { state.head.comment += String.fromCharCode(len); } } while (len && copy < have); if (state.flags & 0x0200) { state.check = crc32(state.check, input, copy, next); } have -= copy; next += copy; if (len) { break inf_leave; } } else if (state.head) { state.head.comment = null; } state.mode = HCRC; /* falls through */ case HCRC: if (state.flags & 0x0200) { //=== NEEDBITS(16); */ while (bits < 16) { if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; } //===// if (hold !== (state.check & 0xffff)) { strm.msg = 'header crc mismatch'; state.mode = BAD; break; } //=== INITBITS(); hold = 0; bits = 0; //===// } if (state.head) { state.head.hcrc = ((state.flags >> 9) & 1); state.head.done = true; } strm.adler = state.check = 0; state.mode = TYPE; break; case DICTID: //=== NEEDBITS(32); */ while (bits < 32) { if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; } //===// strm.adler = state.check = zswap32(hold); //=== INITBITS(); hold = 0; bits = 0; //===// state.mode = DICT; /* falls through */ case DICT: if (state.havedict === 0) { //--- RESTORE() --- strm.next_out = put; strm.avail_out = left; strm.next_in = next; strm.avail_in = have; state.hold = hold; state.bits = bits; //--- return Z_NEED_DICT; } strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/; state.mode = TYPE; /* falls through */ case TYPE: if (flush === Z_BLOCK || flush === Z_TREES) { break inf_leave; } /* falls through */ case TYPEDO: if (state.last) { //--- BYTEBITS() ---// hold >>>= bits & 7; bits -= bits & 7; //---// state.mode = CHECK; break; } //=== NEEDBITS(3); */ while (bits < 3) { if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; } //===// state.last = (hold & 0x01)/*BITS(1)*/; //--- DROPBITS(1) ---// hold >>>= 1; bits -= 1; //---// switch ((hold & 0x03)/*BITS(2)*/) { case 0: /* stored block */ //Tracev((stderr, "inflate: stored block%s\n", // state.last ? " (last)" : "")); state.mode = STORED; break; case 1: /* fixed block */ fixedtables(state); //Tracev((stderr, "inflate: fixed codes block%s\n", // state.last ? " (last)" : "")); state.mode = LEN_; /* decode codes */ if (flush === Z_TREES) { //--- DROPBITS(2) ---// hold >>>= 2; bits -= 2; //---// break inf_leave; } break; case 2: /* dynamic block */ //Tracev((stderr, "inflate: dynamic codes block%s\n", // state.last ? " (last)" : "")); state.mode = TABLE; break; case 3: strm.msg = 'invalid block type'; state.mode = BAD; } //--- DROPBITS(2) ---// hold >>>= 2; bits -= 2; //---// break; case STORED: //--- BYTEBITS() ---// /* go to byte boundary */ hold >>>= bits & 7; bits -= bits & 7; //---// //=== NEEDBITS(32); */ while (bits < 32) { if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; } //===// if ((hold & 0xffff) !== ((hold >>> 16) ^ 0xffff)) { strm.msg = 'invalid stored block lengths'; state.mode = BAD; break; } state.length = hold & 0xffff; //Tracev((stderr, "inflate: stored length %u\n", // state.length)); //=== INITBITS(); hold = 0; bits = 0; //===// state.mode = COPY_; if (flush === Z_TREES) { break inf_leave; } /* falls through */ case COPY_: state.mode = COPY; /* falls through */ case COPY: copy = state.length; if (copy) { if (copy > have) { copy = have; } if (copy > left) { copy = left; } if (copy === 0) { break inf_leave; } //--- zmemcpy(put, next, copy); --- utils.arraySet(output, input, next, copy, put); //---// have -= copy; next += copy; left -= copy; put += copy; state.length -= copy; break; } //Tracev((stderr, "inflate: stored end\n")); state.mode = TYPE; break; case TABLE: //=== NEEDBITS(14); */ while (bits < 14) { if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; } //===// state.nlen = (hold & 0x1f)/*BITS(5)*/ + 257; //--- DROPBITS(5) ---// hold >>>= 5; bits -= 5; //---// state.ndist = (hold & 0x1f)/*BITS(5)*/ + 1; //--- DROPBITS(5) ---// hold >>>= 5; bits -= 5; //---// state.ncode = (hold & 0x0f)/*BITS(4)*/ + 4; //--- DROPBITS(4) ---// hold >>>= 4; bits -= 4; //---// //#ifndef PKZIP_BUG_WORKAROUND if (state.nlen > 286 || state.ndist > 30) { strm.msg = 'too many length or distance symbols'; state.mode = BAD; break; } //#endif //Tracev((stderr, "inflate: table sizes ok\n")); state.have = 0; state.mode = LENLENS; /* falls through */ case LENLENS: while (state.have < state.ncode) { //=== NEEDBITS(3); while (bits < 3) { if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; } //===// state.lens[order[state.have++]] = (hold & 0x07);//BITS(3); //--- DROPBITS(3) ---// hold >>>= 3; bits -= 3; //---// } while (state.have < 19) { state.lens[order[state.have++]] = 0; } // We have separate tables & no pointers. 2 commented lines below not needed. //state.next = state.codes; //state.lencode = state.next; // Switch to use dynamic table state.lencode = state.lendyn; state.lenbits = 7; opts = { bits: state.lenbits }; ret = inflate_table(CODES, state.lens, 0, 19, state.lencode, 0, state.work, opts); state.lenbits = opts.bits; if (ret) { strm.msg = 'invalid code lengths set'; state.mode = BAD; break; } //Tracev((stderr, "inflate: code lengths ok\n")); state.have = 0; state.mode = CODELENS; /* falls through */ case CODELENS: while (state.have < state.nlen + state.ndist) { for (;;) { here = state.lencode[hold & ((1 << state.lenbits) - 1)];/*BITS(state.lenbits)*/ here_bits = here >>> 24; here_op = (here >>> 16) & 0xff; here_val = here & 0xffff; if ((here_bits) <= bits) { break; } //--- PULLBYTE() ---// if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; //---// } if (here_val < 16) { //--- DROPBITS(here.bits) ---// hold >>>= here_bits; bits -= here_bits; //---// state.lens[state.have++] = here_val; } else { if (here_val === 16) { //=== NEEDBITS(here.bits + 2); n = here_bits + 2; while (bits < n) { if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; } //===// //--- DROPBITS(here.bits) ---// hold >>>= here_bits; bits -= here_bits; //---// if (state.have === 0) { strm.msg = 'invalid bit length repeat'; state.mode = BAD; break; } len = state.lens[state.have - 1]; copy = 3 + (hold & 0x03);//BITS(2); //--- DROPBITS(2) ---// hold >>>= 2; bits -= 2; //---// } else if (here_val === 17) { //=== NEEDBITS(here.bits + 3); n = here_bits + 3; while (bits < n) { if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; } //===// //--- DROPBITS(here.bits) ---// hold >>>= here_bits; bits -= here_bits; //---// len = 0; copy = 3 + (hold & 0x07);//BITS(3); //--- DROPBITS(3) ---// hold >>>= 3; bits -= 3; //---// } else { //=== NEEDBITS(here.bits + 7); n = here_bits + 7; while (bits < n) { if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; } //===// //--- DROPBITS(here.bits) ---// hold >>>= here_bits; bits -= here_bits; //---// len = 0; copy = 11 + (hold & 0x7f);//BITS(7); //--- DROPBITS(7) ---// hold >>>= 7; bits -= 7; //---// } if (state.have + copy > state.nlen + state.ndist) { strm.msg = 'invalid bit length repeat'; state.mode = BAD; break; } while (copy--) { state.lens[state.have++] = len; } } } /* handle error breaks in while */ if (state.mode === BAD) { break; } /* check for end-of-block code (better have one) */ if (state.lens[256] === 0) { strm.msg = 'invalid code -- missing end-of-block'; state.mode = BAD; break; } /* build code tables -- note: do not change the lenbits or distbits values here (9 and 6) without reading the comments in inftrees.h concerning the ENOUGH constants, which depend on those values */ state.lenbits = 9; opts = { bits: state.lenbits }; ret = inflate_table(LENS, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts); // We have separate tables & no pointers. 2 commented lines below not needed. // state.next_index = opts.table_index; state.lenbits = opts.bits; // state.lencode = state.next; if (ret) { strm.msg = 'invalid literal/lengths set'; state.mode = BAD; break; } state.distbits = 6; //state.distcode.copy(state.codes); // Switch to use dynamic table state.distcode = state.distdyn; opts = { bits: state.distbits }; ret = inflate_table(DISTS, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts); // We have separate tables & no pointers. 2 commented lines below not needed. // state.next_index = opts.table_index; state.distbits = opts.bits; // state.distcode = state.next; if (ret) { strm.msg = 'invalid distances set'; state.mode = BAD; break; } //Tracev((stderr, 'inflate: codes ok\n')); state.mode = LEN_; if (flush === Z_TREES) { break inf_leave; } /* falls through */ case LEN_: state.mode = LEN; /* falls through */ case LEN: if (have >= 6 && left >= 258) { //--- RESTORE() --- strm.next_out = put; strm.avail_out = left; strm.next_in = next; strm.avail_in = have; state.hold = hold; state.bits = bits; //--- inflate_fast(strm, _out); //--- LOAD() --- put = strm.next_out; output = strm.output; left = strm.avail_out; next = strm.next_in; input = strm.input; have = strm.avail_in; hold = state.hold; bits = state.bits; //--- if (state.mode === TYPE) { state.back = -1; } break; } state.back = 0; for (;;) { here = state.lencode[hold & ((1 << state.lenbits) - 1)]; /*BITS(state.lenbits)*/ here_bits = here >>> 24; here_op = (here >>> 16) & 0xff; here_val = here & 0xffff; if (here_bits <= bits) { break; } //--- PULLBYTE() ---// if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; //---// } if (here_op && (here_op & 0xf0) === 0) { last_bits = here_bits; last_op = here_op; last_val = here_val; for (;;) { here = state.lencode[last_val + ((hold & ((1 << (last_bits + last_op)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits)]; here_bits = here >>> 24; here_op = (here >>> 16) & 0xff; here_val = here & 0xffff; if ((last_bits + here_bits) <= bits) { break; } //--- PULLBYTE() ---// if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; //---// } //--- DROPBITS(last.bits) ---// hold >>>= last_bits; bits -= last_bits; //---// state.back += last_bits; } //--- DROPBITS(here.bits) ---// hold >>>= here_bits; bits -= here_bits; //---// state.back += here_bits; state.length = here_val; if (here_op === 0) { //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? // "inflate: literal '%c'\n" : // "inflate: literal 0x%02x\n", here.val)); state.mode = LIT; break; } if (here_op & 32) { //Tracevv((stderr, "inflate: end of block\n")); state.back = -1; state.mode = TYPE; break; } if (here_op & 64) { strm.msg = 'invalid literal/length code'; state.mode = BAD; break; } state.extra = here_op & 15; state.mode = LENEXT; /* falls through */ case LENEXT: if (state.extra) { //=== NEEDBITS(state.extra); n = state.extra; while (bits < n) { if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; } //===// state.length += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/; //--- DROPBITS(state.extra) ---// hold >>>= state.extra; bits -= state.extra; //---// state.back += state.extra; } //Tracevv((stderr, "inflate: length %u\n", state.length)); state.was = state.length; state.mode = DIST; /* falls through */ case DIST: for (;;) { here = state.distcode[hold & ((1 << state.distbits) - 1)];/*BITS(state.distbits)*/ here_bits = here >>> 24; here_op = (here >>> 16) & 0xff; here_val = here & 0xffff; if ((here_bits) <= bits) { break; } //--- PULLBYTE() ---// if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; //---// } if ((here_op & 0xf0) === 0) { last_bits = here_bits; last_op = here_op; last_val = here_val; for (;;) { here = state.distcode[last_val + ((hold & ((1 << (last_bits + last_op)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits)]; here_bits = here >>> 24; here_op = (here >>> 16) & 0xff; here_val = here & 0xffff; if ((last_bits + here_bits) <= bits) { break; } //--- PULLBYTE() ---// if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; //---// } //--- DROPBITS(last.bits) ---// hold >>>= last_bits; bits -= last_bits; //---// state.back += last_bits; } //--- DROPBITS(here.bits) ---// hold >>>= here_bits; bits -= here_bits; //---// state.back += here_bits; if (here_op & 64) { strm.msg = 'invalid distance code'; state.mode = BAD; break; } state.offset = here_val; state.extra = (here_op) & 15; state.mode = DISTEXT; /* falls through */ case DISTEXT: if (state.extra) { //=== NEEDBITS(state.extra); n = state.extra; while (bits < n) { if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; } //===// state.offset += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/; //--- DROPBITS(state.extra) ---// hold >>>= state.extra; bits -= state.extra; //---// state.back += state.extra; } //#ifdef INFLATE_STRICT if (state.offset > state.dmax) { strm.msg = 'invalid distance too far back'; state.mode = BAD; break; } //#endif //Tracevv((stderr, "inflate: distance %u\n", state.offset)); state.mode = MATCH; /* falls through */ case MATCH: if (left === 0) { break inf_leave; } copy = _out - left; if (state.offset > copy) { /* copy from window */ copy = state.offset - copy; if (copy > state.whave) { if (state.sane) { strm.msg = 'invalid distance too far back'; state.mode = BAD; break; } // (!) This block is disabled in zlib defailts, // don't enable it for binary compatibility //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR // Trace((stderr, "inflate.c too far\n")); // copy -= state.whave; // if (copy > state.length) { copy = state.length; } // if (copy > left) { copy = left; } // left -= copy; // state.length -= copy; // do { // output[put++] = 0; // } while (--copy); // if (state.length === 0) { state.mode = LEN; } // break; //#endif } if (copy > state.wnext) { copy -= state.wnext; from = state.wsize - copy; } else { from = state.wnext - copy; } if (copy > state.length) { copy = state.length; } from_source = state.window; } else { /* copy from output */ from_source = output; from = put - state.offset; copy = state.length; } if (copy > left) { copy = left; } left -= copy; state.length -= copy; do { output[put++] = from_source[from++]; } while (--copy); if (state.length === 0) { state.mode = LEN; } break; case LIT: if (left === 0) { break inf_leave; } output[put++] = state.length; left--; state.mode = LEN; break; case CHECK: if (state.wrap) { //=== NEEDBITS(32); while (bits < 32) { if (have === 0) { break inf_leave; } have--; // Use '|' insdead of '+' to make sure that result is signed hold |= input[next++] << bits; bits += 8; } //===// _out -= left; strm.total_out += _out; state.total += _out; if (_out) { strm.adler = state.check = /*UPDATE(state.check, put - _out, _out);*/ (state.flags ? crc32(state.check, output, _out, put - _out) : adler32(state.check, output, _out, put - _out)); } _out = left; // NB: crc32 stored as signed 32-bit int, zswap32 returns signed too if ((state.flags ? hold : zswap32(hold)) !== state.check) { strm.msg = 'incorrect data check'; state.mode = BAD; break; } //=== INITBITS(); hold = 0; bits = 0; //===// //Tracev((stderr, "inflate: check matches trailer\n")); } state.mode = LENGTH; /* falls through */ case LENGTH: if (state.wrap && state.flags) { //=== NEEDBITS(32); while (bits < 32) { if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; } //===// if (hold !== (state.total & 0xffffffff)) { strm.msg = 'incorrect length check'; state.mode = BAD; break; } //=== INITBITS(); hold = 0; bits = 0; //===// //Tracev((stderr, "inflate: length matches trailer\n")); } state.mode = DONE; /* falls through */ case DONE: ret = Z_STREAM_END; break inf_leave; case BAD: ret = Z_DATA_ERROR; break inf_leave; case MEM: return Z_MEM_ERROR; case SYNC: /* falls through */ default: return Z_STREAM_ERROR; } } // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave" /* Return from inflate(), updating the total counts and the check value. If there was no progress during the inflate() call, return a buffer error. Call updatewindow() to create and/or update the window state. Note: a memory error from inflate() is non-recoverable. */ //--- RESTORE() --- strm.next_out = put; strm.avail_out = left; strm.next_in = next; strm.avail_in = have; state.hold = hold; state.bits = bits; //--- if (state.wsize || (_out !== strm.avail_out && state.mode < BAD && (state.mode < CHECK || flush !== Z_FINISH))) { if (updatewindow(strm, strm.output, strm.next_out, _out - strm.avail_out)) { state.mode = MEM; return Z_MEM_ERROR; } } _in -= strm.avail_in; _out -= strm.avail_out; strm.total_in += _in; strm.total_out += _out; state.total += _out; if (state.wrap && _out) { strm.adler = state.check = /*UPDATE(state.check, strm.next_out - _out, _out);*/ (state.flags ? crc32(state.check, output, _out, strm.next_out - _out) : adler32(state.check, output, _out, strm.next_out - _out)); } strm.data_type = state.bits + (state.last ? 64 : 0) + (state.mode === TYPE ? 128 : 0) + (state.mode === LEN_ || state.mode === COPY_ ? 256 : 0); if (((_in === 0 && _out === 0) || flush === Z_FINISH) && ret === Z_OK) { ret = Z_BUF_ERROR; } return ret; } function inflateEnd(strm) { if (!strm || !strm.state /*|| strm->zfree == (free_func)0*/) { return Z_STREAM_ERROR; } var state = strm.state; if (state.window) { state.window = null; } strm.state = null; return Z_OK; } function inflateGetHeader(strm, head) { var state; /* check state */ if (!strm || !strm.state) { return Z_STREAM_ERROR; } state = strm.state; if ((state.wrap & 2) === 0) { return Z_STREAM_ERROR; } /* save header structure */ state.head = head; head.done = false; return Z_OK; } function inflateSetDictionary(strm, dictionary) { var dictLength = dictionary.length; var state; var dictid; var ret; /* check state */ if (!strm /* == Z_NULL */ || !strm.state /* == Z_NULL */) { return Z_STREAM_ERROR; } state = strm.state; if (state.wrap !== 0 && state.mode !== DICT) { return Z_STREAM_ERROR; } /* check for correct dictionary identifier */ if (state.mode === DICT) { dictid = 1; /* adler32(0, null, 0)*/ /* dictid = adler32(dictid, dictionary, dictLength); */ dictid = adler32(dictid, dictionary, dictLength, 0); if (dictid !== state.check) { return Z_DATA_ERROR; } } /* copy dictionary to window using updatewindow(), which will amend the existing dictionary if appropriate */ ret = updatewindow(strm, dictionary, dictLength, dictLength); if (ret) { state.mode = MEM; return Z_MEM_ERROR; } state.havedict = 1; // Tracev((stderr, "inflate: dictionary set\n")); return Z_OK; } exports.inflateReset = inflateReset; exports.inflateReset2 = inflateReset2; exports.inflateResetKeep = inflateResetKeep; exports.inflateInit = inflateInit; exports.inflateInit2 = inflateInit2; exports.inflate = inflate; exports.inflateEnd = inflateEnd; exports.inflateGetHeader = inflateGetHeader; exports.inflateSetDictionary = inflateSetDictionary; exports.inflateInfo = 'pako inflate (from Nodeca project)'; /* Not implemented exports.inflateCopy = inflateCopy; exports.inflateGetDictionary = inflateGetDictionary; exports.inflateMark = inflateMark; exports.inflatePrime = inflatePrime; exports.inflateSync = inflateSync; exports.inflateSyncPoint = inflateSyncPoint; exports.inflateUndermine = inflateUndermine; */ },{"../utils/common":140,"./adler32":141,"./crc32":143,"./inffast":145,"./inftrees":147}],147:[function(require,module,exports){ 'use strict'; var utils = require('../utils/common'); var MAXBITS = 15; var ENOUGH_LENS = 852; var ENOUGH_DISTS = 592; //var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS); var CODES = 0; var LENS = 1; var DISTS = 2; var lbase = [ /* Length codes 257..285 base */ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 ]; var lext = [ /* Length codes 257..285 extra */ 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78 ]; var dbase = [ /* Distance codes 0..29 base */ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 0, 0 ]; var dext = [ /* Distance codes 0..29 extra */ 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 64, 64 ]; module.exports = function inflate_table(type, lens, lens_index, codes, table, table_index, work, opts) { var bits = opts.bits; //here = opts.here; /* table entry for duplication */ var len = 0; /* a code's length in bits */ var sym = 0; /* index of code symbols */ var min = 0, max = 0; /* minimum and maximum code lengths */ var root = 0; /* number of index bits for root table */ var curr = 0; /* number of index bits for current table */ var drop = 0; /* code bits to drop for sub-table */ var left = 0; /* number of prefix codes available */ var used = 0; /* code entries in table used */ var huff = 0; /* Huffman code */ var incr; /* for incrementing code, index */ var fill; /* index for replicating entries */ var low; /* low bits for current root entry */ var mask; /* mask for low root bits */ var next; /* next available space in table */ var base = null; /* base value table to use */ var base_index = 0; // var shoextra; /* extra bits table to use */ var end; /* use base and extra for symbol > end */ var count = new utils.Buf16(MAXBITS + 1); //[MAXBITS+1]; /* number of codes of each length */ var offs = new utils.Buf16(MAXBITS + 1); //[MAXBITS+1]; /* offsets in table for each length */ var extra = null; var extra_index = 0; var here_bits, here_op, here_val; /* Process a set of code lengths to create a canonical Huffman code. The code lengths are lens[0..codes-1]. Each length corresponds to the symbols 0..codes-1. The Huffman code is generated by first sorting the symbols by length from short to long, and retaining the symbol order for codes with equal lengths. Then the code starts with all zero bits for the first code of the shortest length, and the codes are integer increments for the same length, and zeros are appended as the length increases. For the deflate format, these bits are stored backwards from their more natural integer increment ordering, and so when the decoding tables are built in the large loop below, the integer codes are incremented backwards. This routine assumes, but does not check, that all of the entries in lens[] are in the range 0..MAXBITS. The caller must assure this. 1..MAXBITS is interpreted as that code length. zero means that that symbol does not occur in this code. The codes are sorted by computing a count of codes for each length, creating from that a table of starting indices for each length in the sorted table, and then entering the symbols in order in the sorted table. The sorted table is work[], with that space being provided by the caller. The length counts are used for other purposes as well, i.e. finding the minimum and maximum length codes, determining if there are any codes at all, checking for a valid set of lengths, and looking ahead at length counts to determine sub-table sizes when building the decoding tables. */ /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */ for (len = 0; len <= MAXBITS; len++) { count[len] = 0; } for (sym = 0; sym < codes; sym++) { count[lens[lens_index + sym]]++; } /* bound code lengths, force root to be within code lengths */ root = bits; for (max = MAXBITS; max >= 1; max--) { if (count[max] !== 0) { break; } } if (root > max) { root = max; } if (max === 0) { /* no symbols to code at all */ //table.op[opts.table_index] = 64; //here.op = (var char)64; /* invalid code marker */ //table.bits[opts.table_index] = 1; //here.bits = (var char)1; //table.val[opts.table_index++] = 0; //here.val = (var short)0; table[table_index++] = (1 << 24) | (64 << 16) | 0; //table.op[opts.table_index] = 64; //table.bits[opts.table_index] = 1; //table.val[opts.table_index++] = 0; table[table_index++] = (1 << 24) | (64 << 16) | 0; opts.bits = 1; return 0; /* no symbols, but wait for decoding to report error */ } for (min = 1; min < max; min++) { if (count[min] !== 0) { break; } } if (root < min) { root = min; } /* check for an over-subscribed or incomplete set of lengths */ left = 1; for (len = 1; len <= MAXBITS; len++) { left <<= 1; left -= count[len]; if (left < 0) { return -1; } /* over-subscribed */ } if (left > 0 && (type === CODES || max !== 1)) { return -1; /* incomplete set */ } /* generate offsets into symbol table for each length for sorting */ offs[1] = 0; for (len = 1; len < MAXBITS; len++) { offs[len + 1] = offs[len] + count[len]; } /* sort symbols by length, by symbol order within each length */ for (sym = 0; sym < codes; sym++) { if (lens[lens_index + sym] !== 0) { work[offs[lens[lens_index + sym]]++] = sym; } } /* Create and fill in decoding tables. In this loop, the table being filled is at next and has curr index bits. The code being used is huff with length len. That code is converted to an index by dropping drop bits off of the bottom. For codes where len is less than drop + curr, those top drop + curr - len bits are incremented through all values to fill the table with replicated entries. root is the number of index bits for the root table. When len exceeds root, sub-tables are created pointed to by the root entry with an index of the low root bits of huff. This is saved in low to check for when a new sub-table should be started. drop is zero when the root table is being filled, and drop is root when sub-tables are being filled. When a new sub-table is needed, it is necessary to look ahead in the code lengths to determine what size sub-table is needed. The length counts are used for this, and so count[] is decremented as codes are entered in the tables. used keeps track of how many table entries have been allocated from the provided *table space. It is checked for LENS and DIST tables against the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in the initial root table size constants. See the comments in inftrees.h for more information. sym increments through all symbols, and the loop terminates when all codes of length max, i.e. all codes, have been processed. This routine permits incomplete codes, so another loop after this one fills in the rest of the decoding tables with invalid code markers. */ /* set up for code type */ // poor man optimization - use if-else instead of switch, // to avoid deopts in old v8 if (type === CODES) { base = extra = work; /* dummy value--not used */ end = 19; } else if (type === LENS) { base = lbase; base_index -= 257; extra = lext; extra_index -= 257; end = 256; } else { /* DISTS */ base = dbase; extra = dext; end = -1; } /* initialize opts for loop */ huff = 0; /* starting code */ sym = 0; /* starting code symbol */ len = min; /* starting code length */ next = table_index; /* current table to fill in */ curr = root; /* current table index bits */ drop = 0; /* current bits to drop from code for index */ low = -1; /* trigger new sub-table when len > root */ used = 1 << root; /* use root table entries */ mask = used - 1; /* mask for comparing low */ /* check available table space */ if ((type === LENS && used > ENOUGH_LENS) || (type === DISTS && used > ENOUGH_DISTS)) { return 1; } var i = 0; /* process all codes and make table entries */ for (;;) { i++; /* create table entry */ here_bits = len - drop; if (work[sym] < end) { here_op = 0; here_val = work[sym]; } else if (work[sym] > end) { here_op = extra[extra_index + work[sym]]; here_val = base[base_index + work[sym]]; } else { here_op = 32 + 64; /* end of block */ here_val = 0; } /* replicate for those indices with low len bits equal to huff */ incr = 1 << (len - drop); fill = 1 << curr; min = fill; /* save offset to next table */ do { fill -= incr; table[next + (huff >> drop) + fill] = (here_bits << 24) | (here_op << 16) | here_val |0; } while (fill !== 0); /* backwards increment the len-bit code huff */ incr = 1 << (len - 1); while (huff & incr) { incr >>= 1; } if (incr !== 0) { huff &= incr - 1; huff += incr; } else { huff = 0; } /* go to next symbol, update count, len */ sym++; if (--count[len] === 0) { if (len === max) { break; } len = lens[lens_index + work[sym]]; } /* create new sub-table if needed */ if (len > root && (huff & mask) !== low) { /* if first time, transition to sub-tables */ if (drop === 0) { drop = root; } /* increment past last table */ next += min; /* here min is 1 << curr */ /* determine length of next table */ curr = len - drop; left = 1 << curr; while (curr + drop < max) { left -= count[curr + drop]; if (left <= 0) { break; } curr++; left <<= 1; } /* check for enough space */ used += 1 << curr; if ((type === LENS && used > ENOUGH_LENS) || (type === DISTS && used > ENOUGH_DISTS)) { return 1; } /* point entry in root table to sub-table */ low = huff & mask; /*table.op[low] = curr; table.bits[low] = root; table.val[low] = next - opts.table_index;*/ table[low] = (root << 24) | (curr << 16) | (next - table_index) |0; } } /* fill in remaining table entry if code is incomplete (guaranteed to have at most one remaining entry, since if the code is incomplete, the maximum code length that was allowed to get this far is one bit) */ if (huff !== 0) { //table.op[next + huff] = 64; /* invalid code marker */ //table.bits[next + huff] = len - drop; //table.val[next + huff] = 0; table[next + huff] = ((len - drop) << 24) | (64 << 16) |0; } /* set return parameters */ //opts.table_index += used; opts.bits = root; return 0; }; },{"../utils/common":140}],148:[function(require,module,exports){ 'use strict'; module.exports = { 2: 'need dictionary', /* Z_NEED_DICT 2 */ 1: 'stream end', /* Z_STREAM_END 1 */ 0: '', /* Z_OK 0 */ '-1': 'file error', /* Z_ERRNO (-1) */ '-2': 'stream error', /* Z_STREAM_ERROR (-2) */ '-3': 'data error', /* Z_DATA_ERROR (-3) */ '-4': 'insufficient memory', /* Z_MEM_ERROR (-4) */ '-5': 'buffer error', /* Z_BUF_ERROR (-5) */ '-6': 'incompatible version' /* Z_VERSION_ERROR (-6) */ }; },{}],149:[function(require,module,exports){ 'use strict'; var utils = require('../utils/common'); /* Public constants ==========================================================*/ /* ===========================================================================*/ //var Z_FILTERED = 1; //var Z_HUFFMAN_ONLY = 2; //var Z_RLE = 3; var Z_FIXED = 4; //var Z_DEFAULT_STRATEGY = 0; /* Possible values of the data_type field (though see inflate()) */ var Z_BINARY = 0; var Z_TEXT = 1; //var Z_ASCII = 1; // = Z_TEXT var Z_UNKNOWN = 2; /*============================================================================*/ function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } } // From zutil.h var STORED_BLOCK = 0; var STATIC_TREES = 1; var DYN_TREES = 2; /* The three kinds of block type */ var MIN_MATCH = 3; var MAX_MATCH = 258; /* The minimum and maximum match lengths */ // From deflate.h /* =========================================================================== * Internal compression state. */ var LENGTH_CODES = 29; /* number of length codes, not counting the special END_BLOCK code */ var LITERALS = 256; /* number of literal bytes 0..255 */ var L_CODES = LITERALS + 1 + LENGTH_CODES; /* number of Literal or Length codes, including the END_BLOCK code */ var D_CODES = 30; /* number of distance codes */ var BL_CODES = 19; /* number of codes used to transfer the bit lengths */ var HEAP_SIZE = 2 * L_CODES + 1; /* maximum heap size */ var MAX_BITS = 15; /* All codes must not exceed MAX_BITS bits */ var Buf_size = 16; /* size of bit buffer in bi_buf */ /* =========================================================================== * Constants */ var MAX_BL_BITS = 7; /* Bit length codes must not exceed MAX_BL_BITS bits */ var END_BLOCK = 256; /* end of block literal code */ var REP_3_6 = 16; /* repeat previous bit length 3-6 times (2 bits of repeat count) */ var REPZ_3_10 = 17; /* repeat a zero length 3-10 times (3 bits of repeat count) */ var REPZ_11_138 = 18; /* repeat a zero length 11-138 times (7 bits of repeat count) */ /* eslint-disable comma-spacing,array-bracket-spacing */ var extra_lbits = /* extra bits for each length code */ [0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0]; var extra_dbits = /* extra bits for each distance code */ [0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13]; var extra_blbits = /* extra bits for each bit length code */ [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7]; var bl_order = [16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]; /* eslint-enable comma-spacing,array-bracket-spacing */ /* The lengths of the bit length codes are sent in order of decreasing * probability, to avoid transmitting the lengths for unused bit length codes. */ /* =========================================================================== * Local data. These are initialized only once. */ // We pre-fill arrays with 0 to avoid uninitialized gaps var DIST_CODE_LEN = 512; /* see definition of array dist_code below */ // !!!! Use flat array insdead of structure, Freq = i*2, Len = i*2+1 var static_ltree = new Array((L_CODES + 2) * 2); zero(static_ltree); /* The static literal tree. Since the bit lengths are imposed, there is no * need for the L_CODES extra codes used during heap construction. However * The codes 286 and 287 are needed to build a canonical tree (see _tr_init * below). */ var static_dtree = new Array(D_CODES * 2); zero(static_dtree); /* The static distance tree. (Actually a trivial tree since all codes use * 5 bits.) */ var _dist_code = new Array(DIST_CODE_LEN); zero(_dist_code); /* Distance codes. The first 256 values correspond to the distances * 3 .. 258, the last 256 values correspond to the top 8 bits of * the 15 bit distances. */ var _length_code = new Array(MAX_MATCH - MIN_MATCH + 1); zero(_length_code); /* length code for each normalized match length (0 == MIN_MATCH) */ var base_length = new Array(LENGTH_CODES); zero(base_length); /* First normalized length for each code (0 = MIN_MATCH) */ var base_dist = new Array(D_CODES); zero(base_dist); /* First normalized distance for each code (0 = distance of 1) */ function StaticTreeDesc(static_tree, extra_bits, extra_base, elems, max_length) { this.static_tree = static_tree; /* static tree or NULL */ this.extra_bits = extra_bits; /* extra bits for each code or NULL */ this.extra_base = extra_base; /* base index for extra_bits */ this.elems = elems; /* max number of elements in the tree */ this.max_length = max_length; /* max bit length for the codes */ // show if `static_tree` has data or dummy - needed for monomorphic objects this.has_stree = static_tree && static_tree.length; } var static_l_desc; var static_d_desc; var static_bl_desc; function TreeDesc(dyn_tree, stat_desc) { this.dyn_tree = dyn_tree; /* the dynamic tree */ this.max_code = 0; /* largest code with non zero frequency */ this.stat_desc = stat_desc; /* the corresponding static tree */ } function d_code(dist) { return dist < 256 ? _dist_code[dist] : _dist_code[256 + (dist >>> 7)]; } /* =========================================================================== * Output a short LSB first on the stream. * IN assertion: there is enough room in pendingBuf. */ function put_short(s, w) { // put_byte(s, (uch)((w) & 0xff)); // put_byte(s, (uch)((ush)(w) >> 8)); s.pending_buf[s.pending++] = (w) & 0xff; s.pending_buf[s.pending++] = (w >>> 8) & 0xff; } /* =========================================================================== * Send a value on a given number of bits. * IN assertion: length <= 16 and value fits in length bits. */ function send_bits(s, value, length) { if (s.bi_valid > (Buf_size - length)) { s.bi_buf |= (value << s.bi_valid) & 0xffff; put_short(s, s.bi_buf); s.bi_buf = value >> (Buf_size - s.bi_valid); s.bi_valid += length - Buf_size; } else { s.bi_buf |= (value << s.bi_valid) & 0xffff; s.bi_valid += length; } } function send_code(s, c, tree) { send_bits(s, tree[c * 2]/*.Code*/, tree[c * 2 + 1]/*.Len*/); } /* =========================================================================== * Reverse the first len bits of a code, using straightforward code (a faster * method would use a table) * IN assertion: 1 <= len <= 15 */ function bi_reverse(code, len) { var res = 0; do { res |= code & 1; code >>>= 1; res <<= 1; } while (--len > 0); return res >>> 1; } /* =========================================================================== * Flush the bit buffer, keeping at most 7 bits in it. */ function bi_flush(s) { if (s.bi_valid === 16) { put_short(s, s.bi_buf); s.bi_buf = 0; s.bi_valid = 0; } else if (s.bi_valid >= 8) { s.pending_buf[s.pending++] = s.bi_buf & 0xff; s.bi_buf >>= 8; s.bi_valid -= 8; } } /* =========================================================================== * Compute the optimal bit lengths for a tree and update the total bit length * for the current block. * IN assertion: the fields freq and dad are set, heap[heap_max] and * above are the tree nodes sorted by increasing frequency. * OUT assertions: the field len is set to the optimal bit length, the * array bl_count contains the frequencies for each bit length. * The length opt_len is updated; static_len is also updated if stree is * not null. */ function gen_bitlen(s, desc) // deflate_state *s; // tree_desc *desc; /* the tree descriptor */ { var tree = desc.dyn_tree; var max_code = desc.max_code; var stree = desc.stat_desc.static_tree; var has_stree = desc.stat_desc.has_stree; var extra = desc.stat_desc.extra_bits; var base = desc.stat_desc.extra_base; var max_length = desc.stat_desc.max_length; var h; /* heap index */ var n, m; /* iterate over the tree elements */ var bits; /* bit length */ var xbits; /* extra bits */ var f; /* frequency */ var overflow = 0; /* number of elements with bit length too large */ for (bits = 0; bits <= MAX_BITS; bits++) { s.bl_count[bits] = 0; } /* In a first pass, compute the optimal bit lengths (which may * overflow in the case of the bit length tree). */ tree[s.heap[s.heap_max] * 2 + 1]/*.Len*/ = 0; /* root of the heap */ for (h = s.heap_max + 1; h < HEAP_SIZE; h++) { n = s.heap[h]; bits = tree[tree[n * 2 + 1]/*.Dad*/ * 2 + 1]/*.Len*/ + 1; if (bits > max_length) { bits = max_length; overflow++; } tree[n * 2 + 1]/*.Len*/ = bits; /* We overwrite tree[n].Dad which is no longer needed */ if (n > max_code) { continue; } /* not a leaf node */ s.bl_count[bits]++; xbits = 0; if (n >= base) { xbits = extra[n - base]; } f = tree[n * 2]/*.Freq*/; s.opt_len += f * (bits + xbits); if (has_stree) { s.static_len += f * (stree[n * 2 + 1]/*.Len*/ + xbits); } } if (overflow === 0) { return; } // Trace((stderr,"\nbit length overflow\n")); /* This happens for example on obj2 and pic of the Calgary corpus */ /* Find the first bit length which could increase: */ do { bits = max_length - 1; while (s.bl_count[bits] === 0) { bits--; } s.bl_count[bits]--; /* move one leaf down the tree */ s.bl_count[bits + 1] += 2; /* move one overflow item as its brother */ s.bl_count[max_length]--; /* The brother of the overflow item also moves one step up, * but this does not affect bl_count[max_length] */ overflow -= 2; } while (overflow > 0); /* Now recompute all bit lengths, scanning in increasing frequency. * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all * lengths instead of fixing only the wrong ones. This idea is taken * from 'ar' written by Haruhiko Okumura.) */ for (bits = max_length; bits !== 0; bits--) { n = s.bl_count[bits]; while (n !== 0) { m = s.heap[--h]; if (m > max_code) { continue; } if (tree[m * 2 + 1]/*.Len*/ !== bits) { // Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits)); s.opt_len += (bits - tree[m * 2 + 1]/*.Len*/) * tree[m * 2]/*.Freq*/; tree[m * 2 + 1]/*.Len*/ = bits; } n--; } } } /* =========================================================================== * Generate the codes for a given tree and bit counts (which need not be * optimal). * IN assertion: the array bl_count contains the bit length statistics for * the given tree and the field len is set for all tree elements. * OUT assertion: the field code is set for all tree elements of non * zero code length. */ function gen_codes(tree, max_code, bl_count) // ct_data *tree; /* the tree to decorate */ // int max_code; /* largest code with non zero frequency */ // ushf *bl_count; /* number of codes at each bit length */ { var next_code = new Array(MAX_BITS + 1); /* next code value for each bit length */ var code = 0; /* running code value */ var bits; /* bit index */ var n; /* code index */ /* The distribution counts are first used to generate the code values * without bit reversal. */ for (bits = 1; bits <= MAX_BITS; bits++) { next_code[bits] = code = (code + bl_count[bits - 1]) << 1; } /* Check that the bit counts in bl_count are consistent. The last code * must be all ones. */ //Assert (code + bl_count[MAX_BITS]-1 == (1< length code (0..28) */ length = 0; for (code = 0; code < LENGTH_CODES - 1; code++) { base_length[code] = length; for (n = 0; n < (1 << extra_lbits[code]); n++) { _length_code[length++] = code; } } //Assert (length == 256, "tr_static_init: length != 256"); /* Note that the length 255 (match length 258) can be represented * in two different ways: code 284 + 5 bits or code 285, so we * overwrite length_code[255] to use the best encoding: */ _length_code[length - 1] = code; /* Initialize the mapping dist (0..32K) -> dist code (0..29) */ dist = 0; for (code = 0; code < 16; code++) { base_dist[code] = dist; for (n = 0; n < (1 << extra_dbits[code]); n++) { _dist_code[dist++] = code; } } //Assert (dist == 256, "tr_static_init: dist != 256"); dist >>= 7; /* from now on, all distances are divided by 128 */ for (; code < D_CODES; code++) { base_dist[code] = dist << 7; for (n = 0; n < (1 << (extra_dbits[code] - 7)); n++) { _dist_code[256 + dist++] = code; } } //Assert (dist == 256, "tr_static_init: 256+dist != 512"); /* Construct the codes of the static literal tree */ for (bits = 0; bits <= MAX_BITS; bits++) { bl_count[bits] = 0; } n = 0; while (n <= 143) { static_ltree[n * 2 + 1]/*.Len*/ = 8; n++; bl_count[8]++; } while (n <= 255) { static_ltree[n * 2 + 1]/*.Len*/ = 9; n++; bl_count[9]++; } while (n <= 279) { static_ltree[n * 2 + 1]/*.Len*/ = 7; n++; bl_count[7]++; } while (n <= 287) { static_ltree[n * 2 + 1]/*.Len*/ = 8; n++; bl_count[8]++; } /* Codes 286 and 287 do not exist, but we must include them in the * tree construction to get a canonical Huffman tree (longest code * all ones) */ gen_codes(static_ltree, L_CODES + 1, bl_count); /* The static distance tree is trivial: */ for (n = 0; n < D_CODES; n++) { static_dtree[n * 2 + 1]/*.Len*/ = 5; static_dtree[n * 2]/*.Code*/ = bi_reverse(n, 5); } // Now data ready and we can init static trees static_l_desc = new StaticTreeDesc(static_ltree, extra_lbits, LITERALS + 1, L_CODES, MAX_BITS); static_d_desc = new StaticTreeDesc(static_dtree, extra_dbits, 0, D_CODES, MAX_BITS); static_bl_desc = new StaticTreeDesc(new Array(0), extra_blbits, 0, BL_CODES, MAX_BL_BITS); //static_init_done = true; } /* =========================================================================== * Initialize a new block. */ function init_block(s) { var n; /* iterates over tree elements */ /* Initialize the trees. */ for (n = 0; n < L_CODES; n++) { s.dyn_ltree[n * 2]/*.Freq*/ = 0; } for (n = 0; n < D_CODES; n++) { s.dyn_dtree[n * 2]/*.Freq*/ = 0; } for (n = 0; n < BL_CODES; n++) { s.bl_tree[n * 2]/*.Freq*/ = 0; } s.dyn_ltree[END_BLOCK * 2]/*.Freq*/ = 1; s.opt_len = s.static_len = 0; s.last_lit = s.matches = 0; } /* =========================================================================== * Flush the bit buffer and align the output on a byte boundary */ function bi_windup(s) { if (s.bi_valid > 8) { put_short(s, s.bi_buf); } else if (s.bi_valid > 0) { //put_byte(s, (Byte)s->bi_buf); s.pending_buf[s.pending++] = s.bi_buf; } s.bi_buf = 0; s.bi_valid = 0; } /* =========================================================================== * Copy a stored block, storing first the length and its * one's complement if requested. */ function copy_block(s, buf, len, header) //DeflateState *s; //charf *buf; /* the input data */ //unsigned len; /* its length */ //int header; /* true if block header must be written */ { bi_windup(s); /* align on byte boundary */ if (header) { put_short(s, len); put_short(s, ~len); } // while (len--) { // put_byte(s, *buf++); // } utils.arraySet(s.pending_buf, s.window, buf, len, s.pending); s.pending += len; } /* =========================================================================== * Compares to subtrees, using the tree depth as tie breaker when * the subtrees have equal frequency. This minimizes the worst case length. */ function smaller(tree, n, m, depth) { var _n2 = n * 2; var _m2 = m * 2; return (tree[_n2]/*.Freq*/ < tree[_m2]/*.Freq*/ || (tree[_n2]/*.Freq*/ === tree[_m2]/*.Freq*/ && depth[n] <= depth[m])); } /* =========================================================================== * Restore the heap property by moving down the tree starting at node k, * exchanging a node with the smallest of its two sons if necessary, stopping * when the heap property is re-established (each father smaller than its * two sons). */ function pqdownheap(s, tree, k) // deflate_state *s; // ct_data *tree; /* the tree to restore */ // int k; /* node to move down */ { var v = s.heap[k]; var j = k << 1; /* left son of k */ while (j <= s.heap_len) { /* Set j to the smallest of the two sons: */ if (j < s.heap_len && smaller(tree, s.heap[j + 1], s.heap[j], s.depth)) { j++; } /* Exit if v is smaller than both sons */ if (smaller(tree, v, s.heap[j], s.depth)) { break; } /* Exchange v with the smallest son */ s.heap[k] = s.heap[j]; k = j; /* And continue down the tree, setting j to the left son of k */ j <<= 1; } s.heap[k] = v; } // inlined manually // var SMALLEST = 1; /* =========================================================================== * Send the block data compressed using the given Huffman trees */ function compress_block(s, ltree, dtree) // deflate_state *s; // const ct_data *ltree; /* literal tree */ // const ct_data *dtree; /* distance tree */ { var dist; /* distance of matched string */ var lc; /* match length or unmatched char (if dist == 0) */ var lx = 0; /* running index in l_buf */ var code; /* the code to send */ var extra; /* number of extra bits to send */ if (s.last_lit !== 0) { do { dist = (s.pending_buf[s.d_buf + lx * 2] << 8) | (s.pending_buf[s.d_buf + lx * 2 + 1]); lc = s.pending_buf[s.l_buf + lx]; lx++; if (dist === 0) { send_code(s, lc, ltree); /* send a literal byte */ //Tracecv(isgraph(lc), (stderr," '%c' ", lc)); } else { /* Here, lc is the match length - MIN_MATCH */ code = _length_code[lc]; send_code(s, code + LITERALS + 1, ltree); /* send the length code */ extra = extra_lbits[code]; if (extra !== 0) { lc -= base_length[code]; send_bits(s, lc, extra); /* send the extra length bits */ } dist--; /* dist is now the match distance - 1 */ code = d_code(dist); //Assert (code < D_CODES, "bad d_code"); send_code(s, code, dtree); /* send the distance code */ extra = extra_dbits[code]; if (extra !== 0) { dist -= base_dist[code]; send_bits(s, dist, extra); /* send the extra distance bits */ } } /* literal or match pair ? */ /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */ //Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx, // "pendingBuf overflow"); } while (lx < s.last_lit); } send_code(s, END_BLOCK, ltree); } /* =========================================================================== * Construct one Huffman tree and assigns the code bit strings and lengths. * Update the total bit length for the current block. * IN assertion: the field freq is set for all tree elements. * OUT assertions: the fields len and code are set to the optimal bit length * and corresponding code. The length opt_len is updated; static_len is * also updated if stree is not null. The field max_code is set. */ function build_tree(s, desc) // deflate_state *s; // tree_desc *desc; /* the tree descriptor */ { var tree = desc.dyn_tree; var stree = desc.stat_desc.static_tree; var has_stree = desc.stat_desc.has_stree; var elems = desc.stat_desc.elems; var n, m; /* iterate over heap elements */ var max_code = -1; /* largest code with non zero frequency */ var node; /* new node being created */ /* Construct the initial heap, with least frequent element in * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1]. * heap[0] is not used. */ s.heap_len = 0; s.heap_max = HEAP_SIZE; for (n = 0; n < elems; n++) { if (tree[n * 2]/*.Freq*/ !== 0) { s.heap[++s.heap_len] = max_code = n; s.depth[n] = 0; } else { tree[n * 2 + 1]/*.Len*/ = 0; } } /* The pkzip format requires that at least one distance code exists, * and that at least one bit should be sent even if there is only one * possible code. So to avoid special checks later on we force at least * two codes of non zero frequency. */ while (s.heap_len < 2) { node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0); tree[node * 2]/*.Freq*/ = 1; s.depth[node] = 0; s.opt_len--; if (has_stree) { s.static_len -= stree[node * 2 + 1]/*.Len*/; } /* node is 0 or 1 so it does not have extra bits */ } desc.max_code = max_code; /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree, * establish sub-heaps of increasing lengths: */ for (n = (s.heap_len >> 1/*int /2*/); n >= 1; n--) { pqdownheap(s, tree, n); } /* Construct the Huffman tree by repeatedly combining the least two * frequent nodes. */ node = elems; /* next internal node of the tree */ do { //pqremove(s, tree, n); /* n = node of least frequency */ /*** pqremove ***/ n = s.heap[1/*SMALLEST*/]; s.heap[1/*SMALLEST*/] = s.heap[s.heap_len--]; pqdownheap(s, tree, 1/*SMALLEST*/); /***/ m = s.heap[1/*SMALLEST*/]; /* m = node of next least frequency */ s.heap[--s.heap_max] = n; /* keep the nodes sorted by frequency */ s.heap[--s.heap_max] = m; /* Create a new node father of n and m */ tree[node * 2]/*.Freq*/ = tree[n * 2]/*.Freq*/ + tree[m * 2]/*.Freq*/; s.depth[node] = (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1; tree[n * 2 + 1]/*.Dad*/ = tree[m * 2 + 1]/*.Dad*/ = node; /* and insert the new node in the heap */ s.heap[1/*SMALLEST*/] = node++; pqdownheap(s, tree, 1/*SMALLEST*/); } while (s.heap_len >= 2); s.heap[--s.heap_max] = s.heap[1/*SMALLEST*/]; /* At this point, the fields freq and dad are set. We can now * generate the bit lengths. */ gen_bitlen(s, desc); /* The field len is now set, we can generate the bit codes */ gen_codes(tree, max_code, s.bl_count); } /* =========================================================================== * Scan a literal or distance tree to determine the frequencies of the codes * in the bit length tree. */ function scan_tree(s, tree, max_code) // deflate_state *s; // ct_data *tree; /* the tree to be scanned */ // int max_code; /* and its largest code of non zero frequency */ { var n; /* iterates over all tree elements */ var prevlen = -1; /* last emitted length */ var curlen; /* length of current code */ var nextlen = tree[0 * 2 + 1]/*.Len*/; /* length of next code */ var count = 0; /* repeat count of the current code */ var max_count = 7; /* max repeat count */ var min_count = 4; /* min repeat count */ if (nextlen === 0) { max_count = 138; min_count = 3; } tree[(max_code + 1) * 2 + 1]/*.Len*/ = 0xffff; /* guard */ for (n = 0; n <= max_code; n++) { curlen = nextlen; nextlen = tree[(n + 1) * 2 + 1]/*.Len*/; if (++count < max_count && curlen === nextlen) { continue; } else if (count < min_count) { s.bl_tree[curlen * 2]/*.Freq*/ += count; } else if (curlen !== 0) { if (curlen !== prevlen) { s.bl_tree[curlen * 2]/*.Freq*/++; } s.bl_tree[REP_3_6 * 2]/*.Freq*/++; } else if (count <= 10) { s.bl_tree[REPZ_3_10 * 2]/*.Freq*/++; } else { s.bl_tree[REPZ_11_138 * 2]/*.Freq*/++; } count = 0; prevlen = curlen; if (nextlen === 0) { max_count = 138; min_count = 3; } else if (curlen === nextlen) { max_count = 6; min_count = 3; } else { max_count = 7; min_count = 4; } } } /* =========================================================================== * Send a literal or distance tree in compressed form, using the codes in * bl_tree. */ function send_tree(s, tree, max_code) // deflate_state *s; // ct_data *tree; /* the tree to be scanned */ // int max_code; /* and its largest code of non zero frequency */ { var n; /* iterates over all tree elements */ var prevlen = -1; /* last emitted length */ var curlen; /* length of current code */ var nextlen = tree[0 * 2 + 1]/*.Len*/; /* length of next code */ var count = 0; /* repeat count of the current code */ var max_count = 7; /* max repeat count */ var min_count = 4; /* min repeat count */ /* tree[max_code+1].Len = -1; */ /* guard already set */ if (nextlen === 0) { max_count = 138; min_count = 3; } for (n = 0; n <= max_code; n++) { curlen = nextlen; nextlen = tree[(n + 1) * 2 + 1]/*.Len*/; if (++count < max_count && curlen === nextlen) { continue; } else if (count < min_count) { do { send_code(s, curlen, s.bl_tree); } while (--count !== 0); } else if (curlen !== 0) { if (curlen !== prevlen) { send_code(s, curlen, s.bl_tree); count--; } //Assert(count >= 3 && count <= 6, " 3_6?"); send_code(s, REP_3_6, s.bl_tree); send_bits(s, count - 3, 2); } else if (count <= 10) { send_code(s, REPZ_3_10, s.bl_tree); send_bits(s, count - 3, 3); } else { send_code(s, REPZ_11_138, s.bl_tree); send_bits(s, count - 11, 7); } count = 0; prevlen = curlen; if (nextlen === 0) { max_count = 138; min_count = 3; } else if (curlen === nextlen) { max_count = 6; min_count = 3; } else { max_count = 7; min_count = 4; } } } /* =========================================================================== * Construct the Huffman tree for the bit lengths and return the index in * bl_order of the last bit length code to send. */ function build_bl_tree(s) { var max_blindex; /* index of last bit length code of non zero freq */ /* Determine the bit length frequencies for literal and distance trees */ scan_tree(s, s.dyn_ltree, s.l_desc.max_code); scan_tree(s, s.dyn_dtree, s.d_desc.max_code); /* Build the bit length tree: */ build_tree(s, s.bl_desc); /* opt_len now includes the length of the tree representations, except * the lengths of the bit lengths codes and the 5+5+4 bits for the counts. */ /* Determine the number of bit length codes to send. The pkzip format * requires that at least 4 bit length codes be sent. (appnote.txt says * 3 but the actual value used is 4.) */ for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex--) { if (s.bl_tree[bl_order[max_blindex] * 2 + 1]/*.Len*/ !== 0) { break; } } /* Update opt_len to include the bit length tree and counts */ s.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4; //Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", // s->opt_len, s->static_len)); return max_blindex; } /* =========================================================================== * Send the header for a block using dynamic Huffman trees: the counts, the * lengths of the bit length codes, the literal tree and the distance tree. * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. */ function send_all_trees(s, lcodes, dcodes, blcodes) // deflate_state *s; // int lcodes, dcodes, blcodes; /* number of codes for each tree */ { var rank; /* index in bl_order */ //Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes"); //Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES, // "too many codes"); //Tracev((stderr, "\nbl counts: ")); send_bits(s, lcodes - 257, 5); /* not +255 as stated in appnote.txt */ send_bits(s, dcodes - 1, 5); send_bits(s, blcodes - 4, 4); /* not -3 as stated in appnote.txt */ for (rank = 0; rank < blcodes; rank++) { //Tracev((stderr, "\nbl code %2d ", bl_order[rank])); send_bits(s, s.bl_tree[bl_order[rank] * 2 + 1]/*.Len*/, 3); } //Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent)); send_tree(s, s.dyn_ltree, lcodes - 1); /* literal tree */ //Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent)); send_tree(s, s.dyn_dtree, dcodes - 1); /* distance tree */ //Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent)); } /* =========================================================================== * Check if the data type is TEXT or BINARY, using the following algorithm: * - TEXT if the two conditions below are satisfied: * a) There are no non-portable control characters belonging to the * "black list" (0..6, 14..25, 28..31). * b) There is at least one printable character belonging to the * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255). * - BINARY otherwise. * - The following partially-portable control characters form a * "gray list" that is ignored in this detection algorithm: * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}). * IN assertion: the fields Freq of dyn_ltree are set. */ function detect_data_type(s) { /* black_mask is the bit mask of black-listed bytes * set bits 0..6, 14..25, and 28..31 * 0xf3ffc07f = binary 11110011111111111100000001111111 */ var black_mask = 0xf3ffc07f; var n; /* Check for non-textual ("black-listed") bytes. */ for (n = 0; n <= 31; n++, black_mask >>>= 1) { if ((black_mask & 1) && (s.dyn_ltree[n * 2]/*.Freq*/ !== 0)) { return Z_BINARY; } } /* Check for textual ("white-listed") bytes. */ if (s.dyn_ltree[9 * 2]/*.Freq*/ !== 0 || s.dyn_ltree[10 * 2]/*.Freq*/ !== 0 || s.dyn_ltree[13 * 2]/*.Freq*/ !== 0) { return Z_TEXT; } for (n = 32; n < LITERALS; n++) { if (s.dyn_ltree[n * 2]/*.Freq*/ !== 0) { return Z_TEXT; } } /* There are no "black-listed" or "white-listed" bytes: * this stream either is empty or has tolerated ("gray-listed") bytes only. */ return Z_BINARY; } var static_init_done = false; /* =========================================================================== * Initialize the tree data structures for a new zlib stream. */ function _tr_init(s) { if (!static_init_done) { tr_static_init(); static_init_done = true; } s.l_desc = new TreeDesc(s.dyn_ltree, static_l_desc); s.d_desc = new TreeDesc(s.dyn_dtree, static_d_desc); s.bl_desc = new TreeDesc(s.bl_tree, static_bl_desc); s.bi_buf = 0; s.bi_valid = 0; /* Initialize the first block of the first file: */ init_block(s); } /* =========================================================================== * Send a stored block */ function _tr_stored_block(s, buf, stored_len, last) //DeflateState *s; //charf *buf; /* input block */ //ulg stored_len; /* length of input block */ //int last; /* one if this is the last block for a file */ { send_bits(s, (STORED_BLOCK << 1) + (last ? 1 : 0), 3); /* send block type */ copy_block(s, buf, stored_len, true); /* with header */ } /* =========================================================================== * Send one empty static block to give enough lookahead for inflate. * This takes 10 bits, of which 7 may remain in the bit buffer. */ function _tr_align(s) { send_bits(s, STATIC_TREES << 1, 3); send_code(s, END_BLOCK, static_ltree); bi_flush(s); } /* =========================================================================== * Determine the best encoding for the current block: dynamic trees, static * trees or store, and output the encoded block to the zip file. */ function _tr_flush_block(s, buf, stored_len, last) //DeflateState *s; //charf *buf; /* input block, or NULL if too old */ //ulg stored_len; /* length of input block */ //int last; /* one if this is the last block for a file */ { var opt_lenb, static_lenb; /* opt_len and static_len in bytes */ var max_blindex = 0; /* index of last bit length code of non zero freq */ /* Build the Huffman trees unless a stored block is forced */ if (s.level > 0) { /* Check if the file is binary or text */ if (s.strm.data_type === Z_UNKNOWN) { s.strm.data_type = detect_data_type(s); } /* Construct the literal and distance trees */ build_tree(s, s.l_desc); // Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len, // s->static_len)); build_tree(s, s.d_desc); // Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, // s->static_len)); /* At this point, opt_len and static_len are the total bit lengths of * the compressed block data, excluding the tree representations. */ /* Build the bit length tree for the above two trees, and get the index * in bl_order of the last bit length code to send. */ max_blindex = build_bl_tree(s); /* Determine the best encoding. Compute the block lengths in bytes. */ opt_lenb = (s.opt_len + 3 + 7) >>> 3; static_lenb = (s.static_len + 3 + 7) >>> 3; // Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", // opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, // s->last_lit)); if (static_lenb <= opt_lenb) { opt_lenb = static_lenb; } } else { // Assert(buf != (char*)0, "lost buf"); opt_lenb = static_lenb = stored_len + 5; /* force a stored block */ } if ((stored_len + 4 <= opt_lenb) && (buf !== -1)) { /* 4: two words for the lengths */ /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE. * Otherwise we can't have processed more than WSIZE input bytes since * the last block flush, because compression would have been * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to * transform a block into a stored block. */ _tr_stored_block(s, buf, stored_len, last); } else if (s.strategy === Z_FIXED || static_lenb === opt_lenb) { send_bits(s, (STATIC_TREES << 1) + (last ? 1 : 0), 3); compress_block(s, static_ltree, static_dtree); } else { send_bits(s, (DYN_TREES << 1) + (last ? 1 : 0), 3); send_all_trees(s, s.l_desc.max_code + 1, s.d_desc.max_code + 1, max_blindex + 1); compress_block(s, s.dyn_ltree, s.dyn_dtree); } // Assert (s->compressed_len == s->bits_sent, "bad compressed size"); /* The above check is made mod 2^32, for files larger than 512 MB * and uLong implemented on 32 bits. */ init_block(s); if (last) { bi_windup(s); } // Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3, // s->compressed_len-7*last)); } /* =========================================================================== * Save the match info and tally the frequency counts. Return true if * the current block must be flushed. */ function _tr_tally(s, dist, lc) // deflate_state *s; // unsigned dist; /* distance of matched string */ // unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */ { //var out_length, in_length, dcode; s.pending_buf[s.d_buf + s.last_lit * 2] = (dist >>> 8) & 0xff; s.pending_buf[s.d_buf + s.last_lit * 2 + 1] = dist & 0xff; s.pending_buf[s.l_buf + s.last_lit] = lc & 0xff; s.last_lit++; if (dist === 0) { /* lc is the unmatched char */ s.dyn_ltree[lc * 2]/*.Freq*/++; } else { s.matches++; /* Here, lc is the match length - MIN_MATCH */ dist--; /* dist = match distance - 1 */ //Assert((ush)dist < (ush)MAX_DIST(s) && // (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && // (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match"); s.dyn_ltree[(_length_code[lc] + LITERALS + 1) * 2]/*.Freq*/++; s.dyn_dtree[d_code(dist) * 2]/*.Freq*/++; } // (!) This block is disabled in zlib defailts, // don't enable it for binary compatibility //#ifdef TRUNCATE_BLOCK // /* Try to guess if it is profitable to stop the current block here */ // if ((s.last_lit & 0x1fff) === 0 && s.level > 2) { // /* Compute an upper bound for the compressed length */ // out_length = s.last_lit*8; // in_length = s.strstart - s.block_start; // // for (dcode = 0; dcode < D_CODES; dcode++) { // out_length += s.dyn_dtree[dcode*2]/*.Freq*/ * (5 + extra_dbits[dcode]); // } // out_length >>>= 3; // //Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ", // // s->last_lit, in_length, out_length, // // 100L - out_length*100L/in_length)); // if (s.matches < (s.last_lit>>1)/*int /2*/ && out_length < (in_length>>1)/*int /2*/) { // return true; // } // } //#endif return (s.last_lit === s.lit_bufsize - 1); /* We avoid equality with lit_bufsize because of wraparound at 64K * on 16 bit machines and because stored blocks are restricted to * 64K-1 bytes. */ } exports._tr_init = _tr_init; exports._tr_stored_block = _tr_stored_block; exports._tr_flush_block = _tr_flush_block; exports._tr_tally = _tr_tally; exports._tr_align = _tr_align; },{"../utils/common":140}],150:[function(require,module,exports){ 'use strict'; function ZStream() { /* next input byte */ this.input = null; // JS specific, because we have no pointers this.next_in = 0; /* number of bytes available at input */ this.avail_in = 0; /* total number of input bytes read so far */ this.total_in = 0; /* next output byte should be put there */ this.output = null; // JS specific, because we have no pointers this.next_out = 0; /* remaining free space at output */ this.avail_out = 0; /* total number of bytes output so far */ this.total_out = 0; /* last error message, NULL if no error */ this.msg = ''/*Z_NULL*/; /* not visible by applications */ this.state = null; /* best guess about the data type: binary or text */ this.data_type = 2/*Z_UNKNOWN*/; /* adler32 value of the uncompressed data */ this.adler = 0; } module.exports = ZStream; },{}],151:[function(require,module,exports){ (function (process){(function (){ 'use strict'; if (typeof process === 'undefined' || !process.version || process.version.indexOf('v0.') === 0 || process.version.indexOf('v1.') === 0 && process.version.indexOf('v1.8.') !== 0) { module.exports = { nextTick: nextTick }; } else { module.exports = process } function nextTick(fn, arg1, arg2, arg3) { if (typeof fn !== 'function') { throw new TypeError('"callback" argument must be a function'); } var len = arguments.length; var args, i; switch (len) { case 0: case 1: return process.nextTick(fn); case 2: return process.nextTick(function afterTickOne() { fn.call(null, arg1); }); case 3: return process.nextTick(function afterTickTwo() { fn.call(null, arg1, arg2); }); case 4: return process.nextTick(function afterTickThree() { fn.call(null, arg1, arg2, arg3); }); default: args = new Array(len - 1); i = 0; while (i < args.length) { args[i++] = arguments[i]; } return process.nextTick(function afterTick() { fn.apply(null, args); }); } } }).call(this)}).call(this,require('_process')) },{"_process":152}],152:[function(require,module,exports){ // shim for using process in browser var process = module.exports = {}; // cached from whatever global is present so that test runners that stub it // don't break things. But we need to wrap it in a try catch in case it is // wrapped in strict mode code which doesn't define any globals. It's inside a // function because try/catches deoptimize in certain engines. var cachedSetTimeout; var cachedClearTimeout; function defaultSetTimout() { throw new Error('setTimeout has not been defined'); } function defaultClearTimeout () { throw new Error('clearTimeout has not been defined'); } (function () { try { if (typeof setTimeout === 'function') { cachedSetTimeout = setTimeout; } else { cachedSetTimeout = defaultSetTimout; } } catch (e) { cachedSetTimeout = defaultSetTimout; } try { if (typeof clearTimeout === 'function') { cachedClearTimeout = clearTimeout; } else { cachedClearTimeout = defaultClearTimeout; } } catch (e) { cachedClearTimeout = defaultClearTimeout; } } ()) function runTimeout(fun) { if (cachedSetTimeout === setTimeout) { //normal enviroments in sane situations return setTimeout(fun, 0); } // if setTimeout wasn't available but was latter defined if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) { cachedSetTimeout = setTimeout; return setTimeout(fun, 0); } try { // when when somebody has screwed with setTimeout but no I.E. maddness return cachedSetTimeout(fun, 0); } catch(e){ try { // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally return cachedSetTimeout.call(null, fun, 0); } catch(e){ // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error return cachedSetTimeout.call(this, fun, 0); } } } function runClearTimeout(marker) { if (cachedClearTimeout === clearTimeout) { //normal enviroments in sane situations return clearTimeout(marker); } // if clearTimeout wasn't available but was latter defined if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) { cachedClearTimeout = clearTimeout; return clearTimeout(marker); } try { // when when somebody has screwed with setTimeout but no I.E. maddness return cachedClearTimeout(marker); } catch (e){ try { // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally return cachedClearTimeout.call(null, marker); } catch (e){ // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error. // Some versions of I.E. have different rules for clearTimeout vs setTimeout return cachedClearTimeout.call(this, marker); } } } var queue = []; var draining = false; var currentQueue; var queueIndex = -1; function cleanUpNextTick() { if (!draining || !currentQueue) { return; } draining = false; if (currentQueue.length) { queue = currentQueue.concat(queue); } else { queueIndex = -1; } if (queue.length) { drainQueue(); } } function drainQueue() { if (draining) { return; } var timeout = runTimeout(cleanUpNextTick); draining = true; var len = queue.length; while(len) { currentQueue = queue; queue = []; while (++queueIndex < len) { if (currentQueue) { currentQueue[queueIndex].run(); } } queueIndex = -1; len = queue.length; } currentQueue = null; draining = false; runClearTimeout(timeout); } process.nextTick = function (fun) { var args = new Array(arguments.length - 1); if (arguments.length > 1) { for (var i = 1; i < arguments.length; i++) { args[i - 1] = arguments[i]; } } queue.push(new Item(fun, args)); if (queue.length === 1 && !draining) { runTimeout(drainQueue); } }; // v8 likes predictible objects function Item(fun, array) { this.fun = fun; this.array = array; } Item.prototype.run = function () { this.fun.apply(null, this.array); }; process.title = 'browser'; process.browser = true; process.env = {}; process.argv = []; process.version = ''; // empty string to avoid regexp issues process.versions = {}; function noop() {} process.on = noop; process.addListener = noop; process.once = noop; process.off = noop; process.removeListener = noop; process.removeAllListeners = noop; process.emit = noop; process.prependListener = noop; process.prependOnceListener = noop; process.listeners = function (name) { return [] } process.binding = function (name) { throw new Error('process.binding is not supported'); }; process.cwd = function () { return '/' }; process.chdir = function (dir) { throw new Error('process.chdir is not supported'); }; process.umask = function() { return 0; }; },{}],153:[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. 'use strict'; // If obj.hasOwnProperty has been overridden, then calling // obj.hasOwnProperty(prop) will break. // See: https://github.com/joyent/node/issues/1707 function hasOwnProperty(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); } module.exports = function(qs, sep, eq, options) { sep = sep || '&'; eq = eq || '='; var obj = {}; if (typeof qs !== 'string' || qs.length === 0) { return obj; } var regexp = /\+/g; qs = qs.split(sep); var maxKeys = 1000; if (options && typeof options.maxKeys === 'number') { maxKeys = options.maxKeys; } var len = qs.length; // maxKeys <= 0 means that we should not limit keys count if (maxKeys > 0 && len > maxKeys) { len = maxKeys; } for (var i = 0; i < len; ++i) { var x = qs[i].replace(regexp, '%20'), idx = x.indexOf(eq), kstr, vstr, k, v; if (idx >= 0) { kstr = x.substr(0, idx); vstr = x.substr(idx + 1); } else { kstr = x; vstr = ''; } k = decodeURIComponent(kstr); v = decodeURIComponent(vstr); if (!hasOwnProperty(obj, k)) { obj[k] = v; } else if (isArray(obj[k])) { obj[k].push(v); } else { obj[k] = [obj[k], v]; } } return obj; }; var isArray = Array.isArray || function (xs) { return Object.prototype.toString.call(xs) === '[object Array]'; }; },{}],154:[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. 'use strict'; var stringifyPrimitive = function(v) { switch (typeof v) { case 'string': return v; case 'boolean': return v ? 'true' : 'false'; case 'number': return isFinite(v) ? v : ''; default: return ''; } }; module.exports = function(obj, sep, eq, name) { sep = sep || '&'; eq = eq || '='; if (obj === null) { obj = undefined; } if (typeof obj === 'object') { return map(objectKeys(obj), function(k) { var ks = encodeURIComponent(stringifyPrimitive(k)) + eq; if (isArray(obj[k])) { return map(obj[k], function(v) { return ks + encodeURIComponent(stringifyPrimitive(v)); }).join(sep); } else { return ks + encodeURIComponent(stringifyPrimitive(obj[k])); } }).join(sep); } if (!name) return ''; return encodeURIComponent(stringifyPrimitive(name)) + eq + encodeURIComponent(stringifyPrimitive(obj)); }; var isArray = Array.isArray || function (xs) { return Object.prototype.toString.call(xs) === '[object Array]'; }; function map (xs, f) { if (xs.map) return xs.map(f); var res = []; for (var i = 0; i < xs.length; i++) { res.push(f(xs[i], i)); } return res; } var objectKeys = Object.keys || function (obj) { var res = []; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) res.push(key); } return res; }; },{}],155:[function(require,module,exports){ 'use strict'; exports.decode = exports.parse = require('./decode'); exports.encode = exports.stringify = require('./encode'); },{"./decode":153,"./encode":154}],156:[function(require,module,exports){ 'use strict'; function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } var codes = {}; function createErrorType(code, message, Base) { if (!Base) { Base = Error; } function getMessage(arg1, arg2, arg3) { if (typeof message === 'string') { return message; } else { return message(arg1, arg2, arg3); } } var NodeError = /*#__PURE__*/ function (_Base) { _inheritsLoose(NodeError, _Base); function NodeError(arg1, arg2, arg3) { return _Base.call(this, getMessage(arg1, arg2, arg3)) || this; } return NodeError; }(Base); NodeError.prototype.name = Base.name; NodeError.prototype.code = code; codes[code] = NodeError; } // https://github.com/nodejs/node/blob/v10.8.0/lib/internal/errors.js function oneOf(expected, thing) { if (Array.isArray(expected)) { var len = expected.length; expected = expected.map(function (i) { return String(i); }); if (len > 2) { return "one of ".concat(thing, " ").concat(expected.slice(0, len - 1).join(', '), ", or ") + expected[len - 1]; } else if (len === 2) { return "one of ".concat(thing, " ").concat(expected[0], " or ").concat(expected[1]); } else { return "of ".concat(thing, " ").concat(expected[0]); } } else { return "of ".concat(thing, " ").concat(String(expected)); } } // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith function startsWith(str, search, pos) { return str.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search; } // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith function endsWith(str, search, this_len) { if (this_len === undefined || this_len > str.length) { this_len = str.length; } return str.substring(this_len - search.length, this_len) === search; } // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes function includes(str, search, start) { if (typeof start !== 'number') { start = 0; } if (start + search.length > str.length) { return false; } else { return str.indexOf(search, start) !== -1; } } createErrorType('ERR_INVALID_OPT_VALUE', function (name, value) { return 'The value "' + value + '" is invalid for option "' + name + '"'; }, TypeError); createErrorType('ERR_INVALID_ARG_TYPE', function (name, expected, actual) { // determiner: 'must be' or 'must not be' var determiner; if (typeof expected === 'string' && startsWith(expected, 'not ')) { determiner = 'must not be'; expected = expected.replace(/^not /, ''); } else { determiner = 'must be'; } var msg; if (endsWith(name, ' argument')) { // For cases like 'first argument' msg = "The ".concat(name, " ").concat(determiner, " ").concat(oneOf(expected, 'type')); } else { var type = includes(name, '.') ? 'property' : 'argument'; msg = "The \"".concat(name, "\" ").concat(type, " ").concat(determiner, " ").concat(oneOf(expected, 'type')); } msg += ". Received type ".concat(typeof actual); return msg; }, TypeError); createErrorType('ERR_STREAM_PUSH_AFTER_EOF', 'stream.push() after EOF'); createErrorType('ERR_METHOD_NOT_IMPLEMENTED', function (name) { return 'The ' + name + ' method is not implemented'; }); createErrorType('ERR_STREAM_PREMATURE_CLOSE', 'Premature close'); createErrorType('ERR_STREAM_DESTROYED', function (name) { return 'Cannot call ' + name + ' after a stream was destroyed'; }); createErrorType('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times'); createErrorType('ERR_STREAM_CANNOT_PIPE', 'Cannot pipe, not readable'); createErrorType('ERR_STREAM_WRITE_AFTER_END', 'write after end'); createErrorType('ERR_STREAM_NULL_VALUES', 'May not write null values to stream', TypeError); createErrorType('ERR_UNKNOWN_ENCODING', function (arg) { return 'Unknown encoding: ' + arg; }, TypeError); createErrorType('ERR_STREAM_UNSHIFT_AFTER_END_EVENT', 'stream.unshift() after end event'); module.exports.codes = codes; },{}],157:[function(require,module,exports){ (function (process){(function (){ // 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. // a duplex stream is just a stream that is both readable and writable. // Since JS doesn't have multiple prototypal inheritance, this class // prototypally inherits from Readable, and then parasitically from // Writable. 'use strict'; /**/ var objectKeys = Object.keys || function (obj) { var keys = []; for (var key in obj) { keys.push(key); } return keys; }; /**/ module.exports = Duplex; var Readable = require('./_stream_readable'); var Writable = require('./_stream_writable'); require('inherits')(Duplex, Readable); { // Allow the keys array to be GC'ed. var keys = objectKeys(Writable.prototype); for (var v = 0; v < keys.length; v++) { var method = keys[v]; if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method]; } } function Duplex(options) { if (!(this instanceof Duplex)) return new Duplex(options); Readable.call(this, options); Writable.call(this, options); this.allowHalfOpen = true; if (options) { if (options.readable === false) this.readable = false; if (options.writable === false) this.writable = false; if (options.allowHalfOpen === false) { this.allowHalfOpen = false; this.once('end', onend); } } } Object.defineProperty(Duplex.prototype, 'writableHighWaterMark', { // making it explicit this property is not enumerable // because otherwise some prototype manipulation in // userland will fail enumerable: false, get: function get() { return this._writableState.highWaterMark; } }); Object.defineProperty(Duplex.prototype, 'writableBuffer', { // making it explicit this property is not enumerable // because otherwise some prototype manipulation in // userland will fail enumerable: false, get: function get() { return this._writableState && this._writableState.getBuffer(); } }); Object.defineProperty(Duplex.prototype, 'writableLength', { // making it explicit this property is not enumerable // because otherwise some prototype manipulation in // userland will fail enumerable: false, get: function get() { return this._writableState.length; } }); // the no-half-open enforcer function onend() { // If the writable side ended, then we're ok. if (this._writableState.ended) return; // no more data can be written. // But allow more writes to happen in this tick. process.nextTick(onEndNT, this); } function onEndNT(self) { self.end(); } Object.defineProperty(Duplex.prototype, 'destroyed', { // making it explicit this property is not enumerable // because otherwise some prototype manipulation in // userland will fail enumerable: false, get: function get() { if (this._readableState === undefined || this._writableState === undefined) { return false; } return this._readableState.destroyed && this._writableState.destroyed; }, set: function set(value) { // we ignore the value if the stream // has not been initialized yet if (this._readableState === undefined || this._writableState === undefined) { return; } // backward compatibility, the user is explicitly // managing destroyed this._readableState.destroyed = value; this._writableState.destroyed = value; } }); }).call(this)}).call(this,require('_process')) },{"./_stream_readable":159,"./_stream_writable":161,"_process":152,"inherits":85}],158:[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. // a passthrough stream. // basically just the most minimal sort of Transform stream. // Every written chunk gets output as-is. 'use strict'; module.exports = PassThrough; var Transform = require('./_stream_transform'); require('inherits')(PassThrough, Transform); function PassThrough(options) { if (!(this instanceof PassThrough)) return new PassThrough(options); Transform.call(this, options); } PassThrough.prototype._transform = function (chunk, encoding, cb) { cb(null, chunk); }; },{"./_stream_transform":160,"inherits":85}],159:[function(require,module,exports){ (function (process,global){(function (){ // 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. 'use strict'; module.exports = Readable; /**/ var Duplex; /**/ Readable.ReadableState = ReadableState; /**/ var EE = require('events').EventEmitter; var EElistenerCount = function EElistenerCount(emitter, type) { return emitter.listeners(type).length; }; /**/ /**/ var Stream = require('./internal/streams/stream'); /**/ var Buffer = require('buffer').Buffer; var OurUint8Array = global.Uint8Array || function () {}; function _uint8ArrayToBuffer(chunk) { return Buffer.from(chunk); } function _isUint8Array(obj) { return Buffer.isBuffer(obj) || obj instanceof OurUint8Array; } /**/ var debugUtil = require('util'); var debug; if (debugUtil && debugUtil.debuglog) { debug = debugUtil.debuglog('stream'); } else { debug = function debug() {}; } /**/ var BufferList = require('./internal/streams/buffer_list'); var destroyImpl = require('./internal/streams/destroy'); var _require = require('./internal/streams/state'), getHighWaterMark = _require.getHighWaterMark; var _require$codes = require('../errors').codes, ERR_INVALID_ARG_TYPE = _require$codes.ERR_INVALID_ARG_TYPE, ERR_STREAM_PUSH_AFTER_EOF = _require$codes.ERR_STREAM_PUSH_AFTER_EOF, ERR_METHOD_NOT_IMPLEMENTED = _require$codes.ERR_METHOD_NOT_IMPLEMENTED, ERR_STREAM_UNSHIFT_AFTER_END_EVENT = _require$codes.ERR_STREAM_UNSHIFT_AFTER_END_EVENT; // Lazy loaded to improve the startup performance. var StringDecoder; var createReadableStreamAsyncIterator; var from; require('inherits')(Readable, Stream); var errorOrDestroy = destroyImpl.errorOrDestroy; var kProxyEvents = ['error', 'close', 'destroy', 'pause', 'resume']; function prependListener(emitter, event, fn) { // Sadly this is not cacheable as some libraries bundle their own // event emitter implementation with them. if (typeof emitter.prependListener === 'function') return emitter.prependListener(event, fn); // This is a hack to make sure that our error handler is attached before any // userland ones. NEVER DO THIS. This is here only because this code needs // to continue to work with older versions of Node.js that do not include // the prependListener() method. The goal is to eventually remove this hack. if (!emitter._events || !emitter._events[event]) emitter.on(event, fn);else if (Array.isArray(emitter._events[event])) emitter._events[event].unshift(fn);else emitter._events[event] = [fn, emitter._events[event]]; } function ReadableState(options, stream, isDuplex) { Duplex = Duplex || require('./_stream_duplex'); options = options || {}; // Duplex streams are both readable and writable, but share // the same options object. // However, some cases require setting options to different // values for the readable and the writable sides of the duplex stream. // These options can be provided separately as readableXXX and writableXXX. if (typeof isDuplex !== 'boolean') isDuplex = stream instanceof Duplex; // object stream flag. Used to make read(n) ignore n and to // make all the buffer merging and length checks go away this.objectMode = !!options.objectMode; if (isDuplex) this.objectMode = this.objectMode || !!options.readableObjectMode; // the point at which it stops calling _read() to fill the buffer // Note: 0 is a valid value, means "don't call _read preemptively ever" this.highWaterMark = getHighWaterMark(this, options, 'readableHighWaterMark', isDuplex); // A linked list is used to store data chunks instead of an array because the // linked list can remove elements from the beginning faster than // array.shift() this.buffer = new BufferList(); this.length = 0; this.pipes = null; this.pipesCount = 0; this.flowing = null; this.ended = false; this.endEmitted = false; this.reading = false; // a flag to be able to tell if the event 'readable'/'data' is emitted // immediately, or on a later tick. We set this to true at first, because // any actions that shouldn't happen until "later" should generally also // not happen before the first read call. this.sync = true; // whenever we return null, then we set a flag to say // that we're awaiting a 'readable' event emission. this.needReadable = false; this.emittedReadable = false; this.readableListening = false; this.resumeScheduled = false; this.paused = true; // Should close be emitted on destroy. Defaults to true. this.emitClose = options.emitClose !== false; // Should .destroy() be called after 'end' (and potentially 'finish') this.autoDestroy = !!options.autoDestroy; // has it been destroyed this.destroyed = false; // Crypto is kind of old and crusty. Historically, its default string // encoding is 'binary' so we have to make this configurable. // Everything else in the universe uses 'utf8', though. this.defaultEncoding = options.defaultEncoding || 'utf8'; // the number of writers that are awaiting a drain event in .pipe()s this.awaitDrain = 0; // if true, a maybeReadMore has been scheduled this.readingMore = false; this.decoder = null; this.encoding = null; if (options.encoding) { if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder; this.decoder = new StringDecoder(options.encoding); this.encoding = options.encoding; } } function Readable(options) { Duplex = Duplex || require('./_stream_duplex'); if (!(this instanceof Readable)) return new Readable(options); // Checking for a Stream.Duplex instance is faster here instead of inside // the ReadableState constructor, at least with V8 6.5 var isDuplex = this instanceof Duplex; this._readableState = new ReadableState(options, this, isDuplex); // legacy this.readable = true; if (options) { if (typeof options.read === 'function') this._read = options.read; if (typeof options.destroy === 'function') this._destroy = options.destroy; } Stream.call(this); } Object.defineProperty(Readable.prototype, 'destroyed', { // making it explicit this property is not enumerable // because otherwise some prototype manipulation in // userland will fail enumerable: false, get: function get() { if (this._readableState === undefined) { return false; } return this._readableState.destroyed; }, set: function set(value) { // we ignore the value if the stream // has not been initialized yet if (!this._readableState) { return; } // backward compatibility, the user is explicitly // managing destroyed this._readableState.destroyed = value; } }); Readable.prototype.destroy = destroyImpl.destroy; Readable.prototype._undestroy = destroyImpl.undestroy; Readable.prototype._destroy = function (err, cb) { cb(err); }; // Manually shove something into the read() buffer. // This returns true if the highWaterMark has not been hit yet, // similar to how Writable.write() returns true if you should // write() some more. Readable.prototype.push = function (chunk, encoding) { var state = this._readableState; var skipChunkCheck; if (!state.objectMode) { if (typeof chunk === 'string') { encoding = encoding || state.defaultEncoding; if (encoding !== state.encoding) { chunk = Buffer.from(chunk, encoding); encoding = ''; } skipChunkCheck = true; } } else { skipChunkCheck = true; } return readableAddChunk(this, chunk, encoding, false, skipChunkCheck); }; // Unshift should *always* be something directly out of read() Readable.prototype.unshift = function (chunk) { return readableAddChunk(this, chunk, null, true, false); }; function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) { debug('readableAddChunk', chunk); var state = stream._readableState; if (chunk === null) { state.reading = false; onEofChunk(stream, state); } else { var er; if (!skipChunkCheck) er = chunkInvalid(state, chunk); if (er) { errorOrDestroy(stream, er); } else if (state.objectMode || chunk && chunk.length > 0) { if (typeof chunk !== 'string' && !state.objectMode && Object.getPrototypeOf(chunk) !== Buffer.prototype) { chunk = _uint8ArrayToBuffer(chunk); } if (addToFront) { if (state.endEmitted) errorOrDestroy(stream, new ERR_STREAM_UNSHIFT_AFTER_END_EVENT());else addChunk(stream, state, chunk, true); } else if (state.ended) { errorOrDestroy(stream, new ERR_STREAM_PUSH_AFTER_EOF()); } else if (state.destroyed) { return false; } else { state.reading = false; if (state.decoder && !encoding) { chunk = state.decoder.write(chunk); if (state.objectMode || chunk.length !== 0) addChunk(stream, state, chunk, false);else maybeReadMore(stream, state); } else { addChunk(stream, state, chunk, false); } } } else if (!addToFront) { state.reading = false; maybeReadMore(stream, state); } } // We can push more data if we are below the highWaterMark. // Also, if we have no data yet, we can stand some more bytes. // This is to work around cases where hwm=0, such as the repl. return !state.ended && (state.length < state.highWaterMark || state.length === 0); } function addChunk(stream, state, chunk, addToFront) { if (state.flowing && state.length === 0 && !state.sync) { state.awaitDrain = 0; stream.emit('data', chunk); } else { // update the buffer info. state.length += state.objectMode ? 1 : chunk.length; if (addToFront) state.buffer.unshift(chunk);else state.buffer.push(chunk); if (state.needReadable) emitReadable(stream); } maybeReadMore(stream, state); } function chunkInvalid(state, chunk) { var er; if (!_isUint8Array(chunk) && typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) { er = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer', 'Uint8Array'], chunk); } return er; } Readable.prototype.isPaused = function () { return this._readableState.flowing === false; }; // backwards compatibility. Readable.prototype.setEncoding = function (enc) { if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder; var decoder = new StringDecoder(enc); this._readableState.decoder = decoder; // If setEncoding(null), decoder.encoding equals utf8 this._readableState.encoding = this._readableState.decoder.encoding; // Iterate over current buffer to convert already stored Buffers: var p = this._readableState.buffer.head; var content = ''; while (p !== null) { content += decoder.write(p.data); p = p.next; } this._readableState.buffer.clear(); if (content !== '') this._readableState.buffer.push(content); this._readableState.length = content.length; return this; }; // Don't raise the hwm > 1GB var MAX_HWM = 0x40000000; function computeNewHighWaterMark(n) { if (n >= MAX_HWM) { // TODO(ronag): Throw ERR_VALUE_OUT_OF_RANGE. n = MAX_HWM; } else { // Get the next highest power of 2 to prevent increasing hwm excessively in // tiny amounts n--; n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; n++; } return n; } // This function is designed to be inlinable, so please take care when making // changes to the function body. function howMuchToRead(n, state) { if (n <= 0 || state.length === 0 && state.ended) return 0; if (state.objectMode) return 1; if (n !== n) { // Only flow one buffer at a time if (state.flowing && state.length) return state.buffer.head.data.length;else return state.length; } // If we're asking for more than the current hwm, then raise the hwm. if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n); if (n <= state.length) return n; // Don't have enough if (!state.ended) { state.needReadable = true; return 0; } return state.length; } // you can override either this method, or the async _read(n) below. Readable.prototype.read = function (n) { debug('read', n); n = parseInt(n, 10); var state = this._readableState; var nOrig = n; if (n !== 0) state.emittedReadable = false; // if we're doing read(0) to trigger a readable event, but we // already have a bunch of data in the buffer, then just trigger // the 'readable' event and move on. if (n === 0 && state.needReadable && ((state.highWaterMark !== 0 ? state.length >= state.highWaterMark : state.length > 0) || state.ended)) { debug('read: emitReadable', state.length, state.ended); if (state.length === 0 && state.ended) endReadable(this);else emitReadable(this); return null; } n = howMuchToRead(n, state); // if we've ended, and we're now clear, then finish it up. if (n === 0 && state.ended) { if (state.length === 0) endReadable(this); return null; } // All the actual chunk generation logic needs to be // *below* the call to _read. The reason is that in certain // synthetic stream cases, such as passthrough streams, _read // may be a completely synchronous operation which may change // the state of the read buffer, providing enough data when // before there was *not* enough. // // So, the steps are: // 1. Figure out what the state of things will be after we do // a read from the buffer. // // 2. If that resulting state will trigger a _read, then call _read. // Note that this may be asynchronous, or synchronous. Yes, it is // deeply ugly to write APIs this way, but that still doesn't mean // that the Readable class should behave improperly, as streams are // designed to be sync/async agnostic. // Take note if the _read call is sync or async (ie, if the read call // has returned yet), so that we know whether or not it's safe to emit // 'readable' etc. // // 3. Actually pull the requested chunks out of the buffer and return. // if we need a readable event, then we need to do some reading. var doRead = state.needReadable; debug('need readable', doRead); // if we currently have less than the highWaterMark, then also read some if (state.length === 0 || state.length - n < state.highWaterMark) { doRead = true; debug('length less than watermark', doRead); } // however, if we've ended, then there's no point, and if we're already // reading, then it's unnecessary. if (state.ended || state.reading) { doRead = false; debug('reading or ended', doRead); } else if (doRead) { debug('do read'); state.reading = true; state.sync = true; // if the length is currently zero, then we *need* a readable event. if (state.length === 0) state.needReadable = true; // call internal read method this._read(state.highWaterMark); state.sync = false; // If _read pushed data synchronously, then `reading` will be false, // and we need to re-evaluate how much data we can return to the user. if (!state.reading) n = howMuchToRead(nOrig, state); } var ret; if (n > 0) ret = fromList(n, state);else ret = null; if (ret === null) { state.needReadable = state.length <= state.highWaterMark; n = 0; } else { state.length -= n; state.awaitDrain = 0; } if (state.length === 0) { // If we have nothing in the buffer, then we want to know // as soon as we *do* get something into the buffer. if (!state.ended) state.needReadable = true; // If we tried to read() past the EOF, then emit end on the next tick. if (nOrig !== n && state.ended) endReadable(this); } if (ret !== null) this.emit('data', ret); return ret; }; function onEofChunk(stream, state) { debug('onEofChunk'); if (state.ended) return; if (state.decoder) { var chunk = state.decoder.end(); if (chunk && chunk.length) { state.buffer.push(chunk); state.length += state.objectMode ? 1 : chunk.length; } } state.ended = true; if (state.sync) { // if we are sync, wait until next tick to emit the data. // Otherwise we risk emitting data in the flow() // the readable code triggers during a read() call emitReadable(stream); } else { // emit 'readable' now to make sure it gets picked up. state.needReadable = false; if (!state.emittedReadable) { state.emittedReadable = true; emitReadable_(stream); } } } // Don't emit readable right away in sync mode, because this can trigger // another read() call => stack overflow. This way, it might trigger // a nextTick recursion warning, but that's not so bad. function emitReadable(stream) { var state = stream._readableState; debug('emitReadable', state.needReadable, state.emittedReadable); state.needReadable = false; if (!state.emittedReadable) { debug('emitReadable', state.flowing); state.emittedReadable = true; process.nextTick(emitReadable_, stream); } } function emitReadable_(stream) { var state = stream._readableState; debug('emitReadable_', state.destroyed, state.length, state.ended); if (!state.destroyed && (state.length || state.ended)) { stream.emit('readable'); state.emittedReadable = false; } // The stream needs another readable event if // 1. It is not flowing, as the flow mechanism will take // care of it. // 2. It is not ended. // 3. It is below the highWaterMark, so we can schedule // another readable later. state.needReadable = !state.flowing && !state.ended && state.length <= state.highWaterMark; flow(stream); } // at this point, the user has presumably seen the 'readable' event, // and called read() to consume some data. that may have triggered // in turn another _read(n) call, in which case reading = true if // it's in progress. // However, if we're not ended, or reading, and the length < hwm, // then go ahead and try to read some more preemptively. function maybeReadMore(stream, state) { if (!state.readingMore) { state.readingMore = true; process.nextTick(maybeReadMore_, stream, state); } } function maybeReadMore_(stream, state) { // Attempt to read more data if we should. // // The conditions for reading more data are (one of): // - Not enough data buffered (state.length < state.highWaterMark). The loop // is responsible for filling the buffer with enough data if such data // is available. If highWaterMark is 0 and we are not in the flowing mode // we should _not_ attempt to buffer any extra data. We'll get more data // when the stream consumer calls read() instead. // - No data in the buffer, and the stream is in flowing mode. In this mode // the loop below is responsible for ensuring read() is called. Failing to // call read here would abort the flow and there's no other mechanism for // continuing the flow if the stream consumer has just subscribed to the // 'data' event. // // In addition to the above conditions to keep reading data, the following // conditions prevent the data from being read: // - The stream has ended (state.ended). // - There is already a pending 'read' operation (state.reading). This is a // case where the the stream has called the implementation defined _read() // method, but they are processing the call asynchronously and have _not_ // called push() with new data. In this case we skip performing more // read()s. The execution ends in this method again after the _read() ends // up calling push() with more data. while (!state.reading && !state.ended && (state.length < state.highWaterMark || state.flowing && state.length === 0)) { var len = state.length; debug('maybeReadMore read 0'); stream.read(0); if (len === state.length) // didn't get any data, stop spinning. break; } state.readingMore = false; } // abstract method. to be overridden in specific implementation classes. // call cb(er, data) where data is <= n in length. // for virtual (non-string, non-buffer) streams, "length" is somewhat // arbitrary, and perhaps not very meaningful. Readable.prototype._read = function (n) { errorOrDestroy(this, new ERR_METHOD_NOT_IMPLEMENTED('_read()')); }; Readable.prototype.pipe = function (dest, pipeOpts) { var src = this; var state = this._readableState; switch (state.pipesCount) { case 0: state.pipes = dest; break; case 1: state.pipes = [state.pipes, dest]; break; default: state.pipes.push(dest); break; } state.pipesCount += 1; debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts); var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr; var endFn = doEnd ? onend : unpipe; if (state.endEmitted) process.nextTick(endFn);else src.once('end', endFn); dest.on('unpipe', onunpipe); function onunpipe(readable, unpipeInfo) { debug('onunpipe'); if (readable === src) { if (unpipeInfo && unpipeInfo.hasUnpiped === false) { unpipeInfo.hasUnpiped = true; cleanup(); } } } function onend() { debug('onend'); dest.end(); } // when the dest drains, it reduces the awaitDrain counter // on the source. This would be more elegant with a .once() // handler in flow(), but adding and removing repeatedly is // too slow. var ondrain = pipeOnDrain(src); dest.on('drain', ondrain); var cleanedUp = false; function cleanup() { debug('cleanup'); // cleanup event handlers once the pipe is broken dest.removeListener('close', onclose); dest.removeListener('finish', onfinish); dest.removeListener('drain', ondrain); dest.removeListener('error', onerror); dest.removeListener('unpipe', onunpipe); src.removeListener('end', onend); src.removeListener('end', unpipe); src.removeListener('data', ondata); cleanedUp = true; // if the reader is waiting for a drain event from this // specific writer, then it would cause it to never start // flowing again. // So, if this is awaiting a drain, then we just call it now. // If we don't know, then assume that we are waiting for one. if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain(); } src.on('data', ondata); function ondata(chunk) { debug('ondata'); var ret = dest.write(chunk); debug('dest.write', ret); if (ret === false) { // If the user unpiped during `dest.write()`, it is possible // to get stuck in a permanently paused state if that write // also returned false. // => Check whether `dest` is still a piping destination. if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) { debug('false write response, pause', state.awaitDrain); state.awaitDrain++; } src.pause(); } } // if the dest has an error, then stop piping into it. // however, don't suppress the throwing behavior for this. function onerror(er) { debug('onerror', er); unpipe(); dest.removeListener('error', onerror); if (EElistenerCount(dest, 'error') === 0) errorOrDestroy(dest, er); } // Make sure our error handler is attached before userland ones. prependListener(dest, 'error', onerror); // Both close and finish should trigger unpipe, but only once. function onclose() { dest.removeListener('finish', onfinish); unpipe(); } dest.once('close', onclose); function onfinish() { debug('onfinish'); dest.removeListener('close', onclose); unpipe(); } dest.once('finish', onfinish); function unpipe() { debug('unpipe'); src.unpipe(dest); } // tell the dest that it's being piped to dest.emit('pipe', src); // start the flow if it hasn't been started already. if (!state.flowing) { debug('pipe resume'); src.resume(); } return dest; }; function pipeOnDrain(src) { return function pipeOnDrainFunctionResult() { var state = src._readableState; debug('pipeOnDrain', state.awaitDrain); if (state.awaitDrain) state.awaitDrain--; if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) { state.flowing = true; flow(src); } }; } Readable.prototype.unpipe = function (dest) { var state = this._readableState; var unpipeInfo = { hasUnpiped: false }; // if we're not piping anywhere, then do nothing. if (state.pipesCount === 0) return this; // just one destination. most common case. if (state.pipesCount === 1) { // passed in one, but it's not the right one. if (dest && dest !== state.pipes) return this; if (!dest) dest = state.pipes; // got a match. state.pipes = null; state.pipesCount = 0; state.flowing = false; if (dest) dest.emit('unpipe', this, unpipeInfo); return this; } // slow case. multiple pipe destinations. if (!dest) { // remove all. var dests = state.pipes; var len = state.pipesCount; state.pipes = null; state.pipesCount = 0; state.flowing = false; for (var i = 0; i < len; i++) { dests[i].emit('unpipe', this, { hasUnpiped: false }); } return this; } // try to find the right one. var index = indexOf(state.pipes, dest); if (index === -1) return this; state.pipes.splice(index, 1); state.pipesCount -= 1; if (state.pipesCount === 1) state.pipes = state.pipes[0]; dest.emit('unpipe', this, unpipeInfo); return this; }; // set up data events if they are asked for // Ensure readable listeners eventually get something Readable.prototype.on = function (ev, fn) { var res = Stream.prototype.on.call(this, ev, fn); var state = this._readableState; if (ev === 'data') { // update readableListening so that resume() may be a no-op // a few lines down. This is needed to support once('readable'). state.readableListening = this.listenerCount('readable') > 0; // Try start flowing on next tick if stream isn't explicitly paused if (state.flowing !== false) this.resume(); } else if (ev === 'readable') { if (!state.endEmitted && !state.readableListening) { state.readableListening = state.needReadable = true; state.flowing = false; state.emittedReadable = false; debug('on readable', state.length, state.reading); if (state.length) { emitReadable(this); } else if (!state.reading) { process.nextTick(nReadingNextTick, this); } } } return res; }; Readable.prototype.addListener = Readable.prototype.on; Readable.prototype.removeListener = function (ev, fn) { var res = Stream.prototype.removeListener.call(this, ev, fn); if (ev === 'readable') { // We need to check if there is someone still listening to // readable and reset the state. However this needs to happen // after readable has been emitted but before I/O (nextTick) to // support once('readable', fn) cycles. This means that calling // resume within the same tick will have no // effect. process.nextTick(updateReadableListening, this); } return res; }; Readable.prototype.removeAllListeners = function (ev) { var res = Stream.prototype.removeAllListeners.apply(this, arguments); if (ev === 'readable' || ev === undefined) { // We need to check if there is someone still listening to // readable and reset the state. However this needs to happen // after readable has been emitted but before I/O (nextTick) to // support once('readable', fn) cycles. This means that calling // resume within the same tick will have no // effect. process.nextTick(updateReadableListening, this); } return res; }; function updateReadableListening(self) { var state = self._readableState; state.readableListening = self.listenerCount('readable') > 0; if (state.resumeScheduled && !state.paused) { // flowing needs to be set to true now, otherwise // the upcoming resume will not flow. state.flowing = true; // crude way to check if we should resume } else if (self.listenerCount('data') > 0) { self.resume(); } } function nReadingNextTick(self) { debug('readable nexttick read 0'); self.read(0); } // pause() and resume() are remnants of the legacy readable stream API // If the user uses them, then switch into old mode. Readable.prototype.resume = function () { var state = this._readableState; if (!state.flowing) { debug('resume'); // we flow only if there is no one listening // for readable, but we still have to call // resume() state.flowing = !state.readableListening; resume(this, state); } state.paused = false; return this; }; function resume(stream, state) { if (!state.resumeScheduled) { state.resumeScheduled = true; process.nextTick(resume_, stream, state); } } function resume_(stream, state) { debug('resume', state.reading); if (!state.reading) { stream.read(0); } state.resumeScheduled = false; stream.emit('resume'); flow(stream); if (state.flowing && !state.reading) stream.read(0); } Readable.prototype.pause = function () { debug('call pause flowing=%j', this._readableState.flowing); if (this._readableState.flowing !== false) { debug('pause'); this._readableState.flowing = false; this.emit('pause'); } this._readableState.paused = true; return this; }; function flow(stream) { var state = stream._readableState; debug('flow', state.flowing); while (state.flowing && stream.read() !== null) { ; } } // wrap an old-style stream as the async data source. // This is *not* part of the readable stream interface. // It is an ugly unfortunate mess of history. Readable.prototype.wrap = function (stream) { var _this = this; var state = this._readableState; var paused = false; stream.on('end', function () { debug('wrapped end'); if (state.decoder && !state.ended) { var chunk = state.decoder.end(); if (chunk && chunk.length) _this.push(chunk); } _this.push(null); }); stream.on('data', function (chunk) { debug('wrapped data'); if (state.decoder) chunk = state.decoder.write(chunk); // don't skip over falsy values in objectMode if (state.objectMode && (chunk === null || chunk === undefined)) return;else if (!state.objectMode && (!chunk || !chunk.length)) return; var ret = _this.push(chunk); if (!ret) { paused = true; stream.pause(); } }); // proxy all the other methods. // important when wrapping filters and duplexes. for (var i in stream) { if (this[i] === undefined && typeof stream[i] === 'function') { this[i] = function methodWrap(method) { return function methodWrapReturnFunction() { return stream[method].apply(stream, arguments); }; }(i); } } // proxy certain important events. for (var n = 0; n < kProxyEvents.length; n++) { stream.on(kProxyEvents[n], this.emit.bind(this, kProxyEvents[n])); } // when we try to consume some more bytes, simply unpause the // underlying stream. this._read = function (n) { debug('wrapped _read', n); if (paused) { paused = false; stream.resume(); } }; return this; }; if (typeof Symbol === 'function') { Readable.prototype[Symbol.asyncIterator] = function () { if (createReadableStreamAsyncIterator === undefined) { createReadableStreamAsyncIterator = require('./internal/streams/async_iterator'); } return createReadableStreamAsyncIterator(this); }; } Object.defineProperty(Readable.prototype, 'readableHighWaterMark', { // making it explicit this property is not enumerable // because otherwise some prototype manipulation in // userland will fail enumerable: false, get: function get() { return this._readableState.highWaterMark; } }); Object.defineProperty(Readable.prototype, 'readableBuffer', { // making it explicit this property is not enumerable // because otherwise some prototype manipulation in // userland will fail enumerable: false, get: function get() { return this._readableState && this._readableState.buffer; } }); Object.defineProperty(Readable.prototype, 'readableFlowing', { // making it explicit this property is not enumerable // because otherwise some prototype manipulation in // userland will fail enumerable: false, get: function get() { return this._readableState.flowing; }, set: function set(state) { if (this._readableState) { this._readableState.flowing = state; } } }); // exposed for testing purposes only. Readable._fromList = fromList; Object.defineProperty(Readable.prototype, 'readableLength', { // making it explicit this property is not enumerable // because otherwise some prototype manipulation in // userland will fail enumerable: false, get: function get() { return this._readableState.length; } }); // Pluck off n bytes from an array of buffers. // Length is the combined lengths of all the buffers in the list. // This function is designed to be inlinable, so please take care when making // changes to the function body. function fromList(n, state) { // nothing buffered if (state.length === 0) return null; var ret; if (state.objectMode) ret = state.buffer.shift();else if (!n || n >= state.length) { // read it all, truncate the list if (state.decoder) ret = state.buffer.join('');else if (state.buffer.length === 1) ret = state.buffer.first();else ret = state.buffer.concat(state.length); state.buffer.clear(); } else { // read part of list ret = state.buffer.consume(n, state.decoder); } return ret; } function endReadable(stream) { var state = stream._readableState; debug('endReadable', state.endEmitted); if (!state.endEmitted) { state.ended = true; process.nextTick(endReadableNT, state, stream); } } function endReadableNT(state, stream) { debug('endReadableNT', state.endEmitted, state.length); // Check that we didn't get one last unshift. if (!state.endEmitted && state.length === 0) { state.endEmitted = true; stream.readable = false; stream.emit('end'); if (state.autoDestroy) { // In case of duplex streams we need a way to detect // if the writable side is ready for autoDestroy as well var wState = stream._writableState; if (!wState || wState.autoDestroy && wState.finished) { stream.destroy(); } } } } if (typeof Symbol === 'function') { Readable.from = function (iterable, opts) { if (from === undefined) { from = require('./internal/streams/from'); } return from(Readable, iterable, opts); }; } function indexOf(xs, x) { for (var i = 0, l = xs.length; i < l; i++) { if (xs[i] === x) return i; } return -1; } }).call(this)}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) },{"../errors":156,"./_stream_duplex":157,"./internal/streams/async_iterator":162,"./internal/streams/buffer_list":163,"./internal/streams/destroy":164,"./internal/streams/from":166,"./internal/streams/state":168,"./internal/streams/stream":169,"_process":152,"buffer":47,"events":80,"inherits":85,"string_decoder/":207,"util":29}],160:[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. // a transform stream is a readable/writable stream where you do // something with the data. Sometimes it's called a "filter", // but that's not a great name for it, since that implies a thing where // some bits pass through, and others are simply ignored. (That would // be a valid example of a transform, of course.) // // While the output is causally related to the input, it's not a // necessarily symmetric or synchronous transformation. For example, // a zlib stream might take multiple plain-text writes(), and then // emit a single compressed chunk some time in the future. // // Here's how this works: // // The Transform stream has all the aspects of the readable and writable // stream classes. When you write(chunk), that calls _write(chunk,cb) // internally, and returns false if there's a lot of pending writes // buffered up. When you call read(), that calls _read(n) until // there's enough pending readable data buffered up. // // In a transform stream, the written data is placed in a buffer. When // _read(n) is called, it transforms the queued up data, calling the // buffered _write cb's as it consumes chunks. If consuming a single // written chunk would result in multiple output chunks, then the first // outputted bit calls the readcb, and subsequent chunks just go into // the read buffer, and will cause it to emit 'readable' if necessary. // // This way, back-pressure is actually determined by the reading side, // since _read has to be called to start processing a new chunk. However, // a pathological inflate type of transform can cause excessive buffering // here. For example, imagine a stream where every byte of input is // interpreted as an integer from 0-255, and then results in that many // bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in // 1kb of data being output. In this case, you could write a very small // amount of input, and end up with a very large amount of output. In // such a pathological inflating mechanism, there'd be no way to tell // the system to stop doing the transform. A single 4MB write could // cause the system to run out of memory. // // However, even in such a pathological case, only a single written chunk // would be consumed, and then the rest would wait (un-transformed) until // the results of the previous transformed chunk were consumed. 'use strict'; module.exports = Transform; var _require$codes = require('../errors').codes, ERR_METHOD_NOT_IMPLEMENTED = _require$codes.ERR_METHOD_NOT_IMPLEMENTED, ERR_MULTIPLE_CALLBACK = _require$codes.ERR_MULTIPLE_CALLBACK, ERR_TRANSFORM_ALREADY_TRANSFORMING = _require$codes.ERR_TRANSFORM_ALREADY_TRANSFORMING, ERR_TRANSFORM_WITH_LENGTH_0 = _require$codes.ERR_TRANSFORM_WITH_LENGTH_0; var Duplex = require('./_stream_duplex'); require('inherits')(Transform, Duplex); function afterTransform(er, data) { var ts = this._transformState; ts.transforming = false; var cb = ts.writecb; if (cb === null) { return this.emit('error', new ERR_MULTIPLE_CALLBACK()); } ts.writechunk = null; ts.writecb = null; if (data != null) // single equals check for both `null` and `undefined` this.push(data); cb(er); var rs = this._readableState; rs.reading = false; if (rs.needReadable || rs.length < rs.highWaterMark) { this._read(rs.highWaterMark); } } function Transform(options) { if (!(this instanceof Transform)) return new Transform(options); Duplex.call(this, options); this._transformState = { afterTransform: afterTransform.bind(this), needTransform: false, transforming: false, writecb: null, writechunk: null, writeencoding: null }; // start out asking for a readable event once data is transformed. this._readableState.needReadable = true; // we have implemented the _read method, and done the other things // that Readable wants before the first _read call, so unset the // sync guard flag. this._readableState.sync = false; if (options) { if (typeof options.transform === 'function') this._transform = options.transform; if (typeof options.flush === 'function') this._flush = options.flush; } // When the writable side finishes, then flush out anything remaining. this.on('prefinish', prefinish); } function prefinish() { var _this = this; if (typeof this._flush === 'function' && !this._readableState.destroyed) { this._flush(function (er, data) { done(_this, er, data); }); } else { done(this, null, null); } } Transform.prototype.push = function (chunk, encoding) { this._transformState.needTransform = false; return Duplex.prototype.push.call(this, chunk, encoding); }; // This is the part where you do stuff! // override this function in implementation classes. // 'chunk' is an input chunk. // // Call `push(newChunk)` to pass along transformed output // to the readable side. You may call 'push' zero or more times. // // Call `cb(err)` when you are done with this chunk. If you pass // an error, then that'll put the hurt on the whole operation. If you // never call cb(), then you'll never get another chunk. Transform.prototype._transform = function (chunk, encoding, cb) { cb(new ERR_METHOD_NOT_IMPLEMENTED('_transform()')); }; Transform.prototype._write = function (chunk, encoding, cb) { var ts = this._transformState; ts.writecb = cb; ts.writechunk = chunk; ts.writeencoding = encoding; if (!ts.transforming) { var rs = this._readableState; if (ts.needTransform || rs.needReadable || rs.length < rs.highWaterMark) this._read(rs.highWaterMark); } }; // Doesn't matter what the args are here. // _transform does all the work. // That we got here means that the readable side wants more data. Transform.prototype._read = function (n) { var ts = this._transformState; if (ts.writechunk !== null && !ts.transforming) { ts.transforming = true; this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform); } else { // mark that we need a transform, so that any data that comes in // will get processed, now that we've asked for it. ts.needTransform = true; } }; Transform.prototype._destroy = function (err, cb) { Duplex.prototype._destroy.call(this, err, function (err2) { cb(err2); }); }; function done(stream, er, data) { if (er) return stream.emit('error', er); if (data != null) // single equals check for both `null` and `undefined` stream.push(data); // TODO(BridgeAR): Write a test for these two error cases // if there's nothing in the write buffer, then that means // that nothing more will ever be provided if (stream._writableState.length) throw new ERR_TRANSFORM_WITH_LENGTH_0(); if (stream._transformState.transforming) throw new ERR_TRANSFORM_ALREADY_TRANSFORMING(); return stream.push(null); } },{"../errors":156,"./_stream_duplex":157,"inherits":85}],161:[function(require,module,exports){ (function (process,global){(function (){ // 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. // A bit simpler than readable streams. // Implement an async ._write(chunk, encoding, cb), and it'll handle all // the drain event emission and buffering. 'use strict'; module.exports = Writable; /* */ function WriteReq(chunk, encoding, cb) { this.chunk = chunk; this.encoding = encoding; this.callback = cb; this.next = null; } // It seems a linked list but it is not // there will be only 2 of these for each stream function CorkedRequest(state) { var _this = this; this.next = null; this.entry = null; this.finish = function () { onCorkedFinish(_this, state); }; } /* */ /**/ var Duplex; /**/ Writable.WritableState = WritableState; /**/ var internalUtil = { deprecate: require('util-deprecate') }; /**/ /**/ var Stream = require('./internal/streams/stream'); /**/ var Buffer = require('buffer').Buffer; var OurUint8Array = global.Uint8Array || function () {}; function _uint8ArrayToBuffer(chunk) { return Buffer.from(chunk); } function _isUint8Array(obj) { return Buffer.isBuffer(obj) || obj instanceof OurUint8Array; } var destroyImpl = require('./internal/streams/destroy'); var _require = require('./internal/streams/state'), getHighWaterMark = _require.getHighWaterMark; var _require$codes = require('../errors').codes, ERR_INVALID_ARG_TYPE = _require$codes.ERR_INVALID_ARG_TYPE, ERR_METHOD_NOT_IMPLEMENTED = _require$codes.ERR_METHOD_NOT_IMPLEMENTED, ERR_MULTIPLE_CALLBACK = _require$codes.ERR_MULTIPLE_CALLBACK, ERR_STREAM_CANNOT_PIPE = _require$codes.ERR_STREAM_CANNOT_PIPE, ERR_STREAM_DESTROYED = _require$codes.ERR_STREAM_DESTROYED, ERR_STREAM_NULL_VALUES = _require$codes.ERR_STREAM_NULL_VALUES, ERR_STREAM_WRITE_AFTER_END = _require$codes.ERR_STREAM_WRITE_AFTER_END, ERR_UNKNOWN_ENCODING = _require$codes.ERR_UNKNOWN_ENCODING; var errorOrDestroy = destroyImpl.errorOrDestroy; require('inherits')(Writable, Stream); function nop() {} function WritableState(options, stream, isDuplex) { Duplex = Duplex || require('./_stream_duplex'); options = options || {}; // Duplex streams are both readable and writable, but share // the same options object. // However, some cases require setting options to different // values for the readable and the writable sides of the duplex stream, // e.g. options.readableObjectMode vs. options.writableObjectMode, etc. if (typeof isDuplex !== 'boolean') isDuplex = stream instanceof Duplex; // object stream flag to indicate whether or not this stream // contains buffers or objects. this.objectMode = !!options.objectMode; if (isDuplex) this.objectMode = this.objectMode || !!options.writableObjectMode; // the point at which write() starts returning false // Note: 0 is a valid value, means that we always return false if // the entire buffer is not flushed immediately on write() this.highWaterMark = getHighWaterMark(this, options, 'writableHighWaterMark', isDuplex); // if _final has been called this.finalCalled = false; // drain event flag. this.needDrain = false; // at the start of calling end() this.ending = false; // when end() has been called, and returned this.ended = false; // when 'finish' is emitted this.finished = false; // has it been destroyed this.destroyed = false; // should we decode strings into buffers before passing to _write? // this is here so that some node-core streams can optimize string // handling at a lower level. var noDecode = options.decodeStrings === false; this.decodeStrings = !noDecode; // Crypto is kind of old and crusty. Historically, its default string // encoding is 'binary' so we have to make this configurable. // Everything else in the universe uses 'utf8', though. this.defaultEncoding = options.defaultEncoding || 'utf8'; // not an actual buffer we keep track of, but a measurement // of how much we're waiting to get pushed to some underlying // socket or file. this.length = 0; // a flag to see when we're in the middle of a write. this.writing = false; // when true all writes will be buffered until .uncork() call this.corked = 0; // a flag to be able to tell if the onwrite cb is called immediately, // or on a later tick. We set this to true at first, because any // actions that shouldn't happen until "later" should generally also // not happen before the first write call. this.sync = true; // a flag to know if we're processing previously buffered items, which // may call the _write() callback in the same tick, so that we don't // end up in an overlapped onwrite situation. this.bufferProcessing = false; // the callback that's passed to _write(chunk,cb) this.onwrite = function (er) { onwrite(stream, er); }; // the callback that the user supplies to write(chunk,encoding,cb) this.writecb = null; // the amount that is being written when _write is called. this.writelen = 0; this.bufferedRequest = null; this.lastBufferedRequest = null; // number of pending user-supplied write callbacks // this must be 0 before 'finish' can be emitted this.pendingcb = 0; // emit prefinish if the only thing we're waiting for is _write cbs // This is relevant for synchronous Transform streams this.prefinished = false; // True if the error was already emitted and should not be thrown again this.errorEmitted = false; // Should close be emitted on destroy. Defaults to true. this.emitClose = options.emitClose !== false; // Should .destroy() be called after 'finish' (and potentially 'end') this.autoDestroy = !!options.autoDestroy; // count buffered requests this.bufferedRequestCount = 0; // allocate the first CorkedRequest, there is always // one allocated and free to use, and we maintain at most two this.corkedRequestsFree = new CorkedRequest(this); } WritableState.prototype.getBuffer = function getBuffer() { var current = this.bufferedRequest; var out = []; while (current) { out.push(current); current = current.next; } return out; }; (function () { try { Object.defineProperty(WritableState.prototype, 'buffer', { get: internalUtil.deprecate(function writableStateBufferGetter() { return this.getBuffer(); }, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' + 'instead.', 'DEP0003') }); } catch (_) {} })(); // Test _writableState for inheritance to account for Duplex streams, // whose prototype chain only points to Readable. var realHasInstance; if (typeof Symbol === 'function' && Symbol.hasInstance && typeof Function.prototype[Symbol.hasInstance] === 'function') { realHasInstance = Function.prototype[Symbol.hasInstance]; Object.defineProperty(Writable, Symbol.hasInstance, { value: function value(object) { if (realHasInstance.call(this, object)) return true; if (this !== Writable) return false; return object && object._writableState instanceof WritableState; } }); } else { realHasInstance = function realHasInstance(object) { return object instanceof this; }; } function Writable(options) { Duplex = Duplex || require('./_stream_duplex'); // Writable ctor is applied to Duplexes, too. // `realHasInstance` is necessary because using plain `instanceof` // would return false, as no `_writableState` property is attached. // Trying to use the custom `instanceof` for Writable here will also break the // Node.js LazyTransform implementation, which has a non-trivial getter for // `_writableState` that would lead to infinite recursion. // Checking for a Stream.Duplex instance is faster here instead of inside // the WritableState constructor, at least with V8 6.5 var isDuplex = this instanceof Duplex; if (!isDuplex && !realHasInstance.call(Writable, this)) return new Writable(options); this._writableState = new WritableState(options, this, isDuplex); // legacy. this.writable = true; if (options) { if (typeof options.write === 'function') this._write = options.write; if (typeof options.writev === 'function') this._writev = options.writev; if (typeof options.destroy === 'function') this._destroy = options.destroy; if (typeof options.final === 'function') this._final = options.final; } Stream.call(this); } // Otherwise people can pipe Writable streams, which is just wrong. Writable.prototype.pipe = function () { errorOrDestroy(this, new ERR_STREAM_CANNOT_PIPE()); }; function writeAfterEnd(stream, cb) { var er = new ERR_STREAM_WRITE_AFTER_END(); // TODO: defer error events consistently everywhere, not just the cb errorOrDestroy(stream, er); process.nextTick(cb, er); } // Checks that a user-supplied chunk is valid, especially for the particular // mode the stream is in. Currently this means that `null` is never accepted // and undefined/non-string values are only allowed in object mode. function validChunk(stream, state, chunk, cb) { var er; if (chunk === null) { er = new ERR_STREAM_NULL_VALUES(); } else if (typeof chunk !== 'string' && !state.objectMode) { er = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer'], chunk); } if (er) { errorOrDestroy(stream, er); process.nextTick(cb, er); return false; } return true; } Writable.prototype.write = function (chunk, encoding, cb) { var state = this._writableState; var ret = false; var isBuf = !state.objectMode && _isUint8Array(chunk); if (isBuf && !Buffer.isBuffer(chunk)) { chunk = _uint8ArrayToBuffer(chunk); } if (typeof encoding === 'function') { cb = encoding; encoding = null; } if (isBuf) encoding = 'buffer';else if (!encoding) encoding = state.defaultEncoding; if (typeof cb !== 'function') cb = nop; if (state.ending) writeAfterEnd(this, cb);else if (isBuf || validChunk(this, state, chunk, cb)) { state.pendingcb++; ret = writeOrBuffer(this, state, isBuf, chunk, encoding, cb); } return ret; }; Writable.prototype.cork = function () { this._writableState.corked++; }; Writable.prototype.uncork = function () { var state = this._writableState; if (state.corked) { state.corked--; if (!state.writing && !state.corked && !state.bufferProcessing && state.bufferedRequest) clearBuffer(this, state); } }; Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) { // node::ParseEncoding() requires lower case. if (typeof encoding === 'string') encoding = encoding.toLowerCase(); if (!(['hex', 'utf8', 'utf-8', 'ascii', 'binary', 'base64', 'ucs2', 'ucs-2', 'utf16le', 'utf-16le', 'raw'].indexOf((encoding + '').toLowerCase()) > -1)) throw new ERR_UNKNOWN_ENCODING(encoding); this._writableState.defaultEncoding = encoding; return this; }; Object.defineProperty(Writable.prototype, 'writableBuffer', { // making it explicit this property is not enumerable // because otherwise some prototype manipulation in // userland will fail enumerable: false, get: function get() { return this._writableState && this._writableState.getBuffer(); } }); function decodeChunk(state, chunk, encoding) { if (!state.objectMode && state.decodeStrings !== false && typeof chunk === 'string') { chunk = Buffer.from(chunk, encoding); } return chunk; } Object.defineProperty(Writable.prototype, 'writableHighWaterMark', { // making it explicit this property is not enumerable // because otherwise some prototype manipulation in // userland will fail enumerable: false, get: function get() { return this._writableState.highWaterMark; } }); // if we're already writing something, then just put this // in the queue, and wait our turn. Otherwise, call _write // If we return false, then we need a drain event, so set that flag. function writeOrBuffer(stream, state, isBuf, chunk, encoding, cb) { if (!isBuf) { var newChunk = decodeChunk(state, chunk, encoding); if (chunk !== newChunk) { isBuf = true; encoding = 'buffer'; chunk = newChunk; } } var len = state.objectMode ? 1 : chunk.length; state.length += len; var ret = state.length < state.highWaterMark; // we must ensure that previous needDrain will not be reset to false. if (!ret) state.needDrain = true; if (state.writing || state.corked) { var last = state.lastBufferedRequest; state.lastBufferedRequest = { chunk: chunk, encoding: encoding, isBuf: isBuf, callback: cb, next: null }; if (last) { last.next = state.lastBufferedRequest; } else { state.bufferedRequest = state.lastBufferedRequest; } state.bufferedRequestCount += 1; } else { doWrite(stream, state, false, len, chunk, encoding, cb); } return ret; } function doWrite(stream, state, writev, len, chunk, encoding, cb) { state.writelen = len; state.writecb = cb; state.writing = true; state.sync = true; if (state.destroyed) state.onwrite(new ERR_STREAM_DESTROYED('write'));else if (writev) stream._writev(chunk, state.onwrite);else stream._write(chunk, encoding, state.onwrite); state.sync = false; } function onwriteError(stream, state, sync, er, cb) { --state.pendingcb; if (sync) { // defer the callback if we are being called synchronously // to avoid piling up things on the stack process.nextTick(cb, er); // this can emit finish, and it will always happen // after error process.nextTick(finishMaybe, stream, state); stream._writableState.errorEmitted = true; errorOrDestroy(stream, er); } else { // the caller expect this to happen before if // it is async cb(er); stream._writableState.errorEmitted = true; errorOrDestroy(stream, er); // this can emit finish, but finish must // always follow error finishMaybe(stream, state); } } function onwriteStateUpdate(state) { state.writing = false; state.writecb = null; state.length -= state.writelen; state.writelen = 0; } function onwrite(stream, er) { var state = stream._writableState; var sync = state.sync; var cb = state.writecb; if (typeof cb !== 'function') throw new ERR_MULTIPLE_CALLBACK(); onwriteStateUpdate(state); if (er) onwriteError(stream, state, sync, er, cb);else { // Check if we're actually ready to finish, but don't emit yet var finished = needFinish(state) || stream.destroyed; if (!finished && !state.corked && !state.bufferProcessing && state.bufferedRequest) { clearBuffer(stream, state); } if (sync) { process.nextTick(afterWrite, stream, state, finished, cb); } else { afterWrite(stream, state, finished, cb); } } } function afterWrite(stream, state, finished, cb) { if (!finished) onwriteDrain(stream, state); state.pendingcb--; cb(); finishMaybe(stream, state); } // Must force callback to be called on nextTick, so that we don't // emit 'drain' before the write() consumer gets the 'false' return // value, and has a chance to attach a 'drain' listener. function onwriteDrain(stream, state) { if (state.length === 0 && state.needDrain) { state.needDrain = false; stream.emit('drain'); } } // if there's something in the buffer waiting, then process it function clearBuffer(stream, state) { state.bufferProcessing = true; var entry = state.bufferedRequest; if (stream._writev && entry && entry.next) { // Fast case, write everything using _writev() var l = state.bufferedRequestCount; var buffer = new Array(l); var holder = state.corkedRequestsFree; holder.entry = entry; var count = 0; var allBuffers = true; while (entry) { buffer[count] = entry; if (!entry.isBuf) allBuffers = false; entry = entry.next; count += 1; } buffer.allBuffers = allBuffers; doWrite(stream, state, true, state.length, buffer, '', holder.finish); // doWrite is almost always async, defer these to save a bit of time // as the hot path ends with doWrite state.pendingcb++; state.lastBufferedRequest = null; if (holder.next) { state.corkedRequestsFree = holder.next; holder.next = null; } else { state.corkedRequestsFree = new CorkedRequest(state); } state.bufferedRequestCount = 0; } else { // Slow case, write chunks one-by-one while (entry) { var chunk = entry.chunk; var encoding = entry.encoding; var cb = entry.callback; var len = state.objectMode ? 1 : chunk.length; doWrite(stream, state, false, len, chunk, encoding, cb); entry = entry.next; state.bufferedRequestCount--; // if we didn't call the onwrite immediately, then // it means that we need to wait until it does. // also, that means that the chunk and cb are currently // being processed, so move the buffer counter past them. if (state.writing) { break; } } if (entry === null) state.lastBufferedRequest = null; } state.bufferedRequest = entry; state.bufferProcessing = false; } Writable.prototype._write = function (chunk, encoding, cb) { cb(new ERR_METHOD_NOT_IMPLEMENTED('_write()')); }; Writable.prototype._writev = null; Writable.prototype.end = function (chunk, encoding, cb) { var state = this._writableState; if (typeof chunk === 'function') { cb = chunk; chunk = null; encoding = null; } else if (typeof encoding === 'function') { cb = encoding; encoding = null; } if (chunk !== null && chunk !== undefined) this.write(chunk, encoding); // .end() fully uncorks if (state.corked) { state.corked = 1; this.uncork(); } // ignore unnecessary end() calls. if (!state.ending) endWritable(this, state, cb); return this; }; Object.defineProperty(Writable.prototype, 'writableLength', { // making it explicit this property is not enumerable // because otherwise some prototype manipulation in // userland will fail enumerable: false, get: function get() { return this._writableState.length; } }); function needFinish(state) { return state.ending && state.length === 0 && state.bufferedRequest === null && !state.finished && !state.writing; } function callFinal(stream, state) { stream._final(function (err) { state.pendingcb--; if (err) { errorOrDestroy(stream, err); } state.prefinished = true; stream.emit('prefinish'); finishMaybe(stream, state); }); } function prefinish(stream, state) { if (!state.prefinished && !state.finalCalled) { if (typeof stream._final === 'function' && !state.destroyed) { state.pendingcb++; state.finalCalled = true; process.nextTick(callFinal, stream, state); } else { state.prefinished = true; stream.emit('prefinish'); } } } function finishMaybe(stream, state) { var need = needFinish(state); if (need) { prefinish(stream, state); if (state.pendingcb === 0) { state.finished = true; stream.emit('finish'); if (state.autoDestroy) { // In case of duplex streams we need a way to detect // if the readable side is ready for autoDestroy as well var rState = stream._readableState; if (!rState || rState.autoDestroy && rState.endEmitted) { stream.destroy(); } } } } return need; } function endWritable(stream, state, cb) { state.ending = true; finishMaybe(stream, state); if (cb) { if (state.finished) process.nextTick(cb);else stream.once('finish', cb); } state.ended = true; stream.writable = false; } function onCorkedFinish(corkReq, state, err) { var entry = corkReq.entry; corkReq.entry = null; while (entry) { var cb = entry.callback; state.pendingcb--; cb(err); entry = entry.next; } // reuse the free corkReq. state.corkedRequestsFree.next = corkReq; } Object.defineProperty(Writable.prototype, 'destroyed', { // making it explicit this property is not enumerable // because otherwise some prototype manipulation in // userland will fail enumerable: false, get: function get() { if (this._writableState === undefined) { return false; } return this._writableState.destroyed; }, set: function set(value) { // we ignore the value if the stream // has not been initialized yet if (!this._writableState) { return; } // backward compatibility, the user is explicitly // managing destroyed this._writableState.destroyed = value; } }); Writable.prototype.destroy = destroyImpl.destroy; Writable.prototype._undestroy = destroyImpl.undestroy; Writable.prototype._destroy = function (err, cb) { cb(err); }; }).call(this)}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) },{"../errors":156,"./_stream_duplex":157,"./internal/streams/destroy":164,"./internal/streams/state":168,"./internal/streams/stream":169,"_process":152,"buffer":47,"inherits":85,"util-deprecate":214}],162:[function(require,module,exports){ (function (process){(function (){ 'use strict'; var _Object$setPrototypeO; function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var finished = require('./end-of-stream'); var kLastResolve = Symbol('lastResolve'); var kLastReject = Symbol('lastReject'); var kError = Symbol('error'); var kEnded = Symbol('ended'); var kLastPromise = Symbol('lastPromise'); var kHandlePromise = Symbol('handlePromise'); var kStream = Symbol('stream'); function createIterResult(value, done) { return { value: value, done: done }; } function readAndResolve(iter) { var resolve = iter[kLastResolve]; if (resolve !== null) { var data = iter[kStream].read(); // we defer if data is null // we can be expecting either 'end' or // 'error' if (data !== null) { iter[kLastPromise] = null; iter[kLastResolve] = null; iter[kLastReject] = null; resolve(createIterResult(data, false)); } } } function onReadable(iter) { // we wait for the next tick, because it might // emit an error with process.nextTick process.nextTick(readAndResolve, iter); } function wrapForNext(lastPromise, iter) { return function (resolve, reject) { lastPromise.then(function () { if (iter[kEnded]) { resolve(createIterResult(undefined, true)); return; } iter[kHandlePromise](resolve, reject); }, reject); }; } var AsyncIteratorPrototype = Object.getPrototypeOf(function () {}); var ReadableStreamAsyncIteratorPrototype = Object.setPrototypeOf((_Object$setPrototypeO = { get stream() { return this[kStream]; }, next: function next() { var _this = this; // if we have detected an error in the meanwhile // reject straight away var error = this[kError]; if (error !== null) { return Promise.reject(error); } if (this[kEnded]) { return Promise.resolve(createIterResult(undefined, true)); } if (this[kStream].destroyed) { // We need to defer via nextTick because if .destroy(err) is // called, the error will be emitted via nextTick, and // we cannot guarantee that there is no error lingering around // waiting to be emitted. return new Promise(function (resolve, reject) { process.nextTick(function () { if (_this[kError]) { reject(_this[kError]); } else { resolve(createIterResult(undefined, true)); } }); }); } // if we have multiple next() calls // we will wait for the previous Promise to finish // this logic is optimized to support for await loops, // where next() is only called once at a time var lastPromise = this[kLastPromise]; var promise; if (lastPromise) { promise = new Promise(wrapForNext(lastPromise, this)); } else { // fast path needed to support multiple this.push() // without triggering the next() queue var data = this[kStream].read(); if (data !== null) { return Promise.resolve(createIterResult(data, false)); } promise = new Promise(this[kHandlePromise]); } this[kLastPromise] = promise; return promise; } }, _defineProperty(_Object$setPrototypeO, Symbol.asyncIterator, function () { return this; }), _defineProperty(_Object$setPrototypeO, "return", function _return() { var _this2 = this; // destroy(err, cb) is a private API // we can guarantee we have that here, because we control the // Readable class this is attached to return new Promise(function (resolve, reject) { _this2[kStream].destroy(null, function (err) { if (err) { reject(err); return; } resolve(createIterResult(undefined, true)); }); }); }), _Object$setPrototypeO), AsyncIteratorPrototype); var createReadableStreamAsyncIterator = function createReadableStreamAsyncIterator(stream) { var _Object$create; var iterator = Object.create(ReadableStreamAsyncIteratorPrototype, (_Object$create = {}, _defineProperty(_Object$create, kStream, { value: stream, writable: true }), _defineProperty(_Object$create, kLastResolve, { value: null, writable: true }), _defineProperty(_Object$create, kLastReject, { value: null, writable: true }), _defineProperty(_Object$create, kError, { value: null, writable: true }), _defineProperty(_Object$create, kEnded, { value: stream._readableState.endEmitted, writable: true }), _defineProperty(_Object$create, kHandlePromise, { value: function value(resolve, reject) { var data = iterator[kStream].read(); if (data) { iterator[kLastPromise] = null; iterator[kLastResolve] = null; iterator[kLastReject] = null; resolve(createIterResult(data, false)); } else { iterator[kLastResolve] = resolve; iterator[kLastReject] = reject; } }, writable: true }), _Object$create)); iterator[kLastPromise] = null; finished(stream, function (err) { if (err && err.code !== 'ERR_STREAM_PREMATURE_CLOSE') { var reject = iterator[kLastReject]; // reject if we are waiting for data in the Promise // returned by next() and store the error if (reject !== null) { iterator[kLastPromise] = null; iterator[kLastResolve] = null; iterator[kLastReject] = null; reject(err); } iterator[kError] = err; return; } var resolve = iterator[kLastResolve]; if (resolve !== null) { iterator[kLastPromise] = null; iterator[kLastResolve] = null; iterator[kLastReject] = null; resolve(createIterResult(undefined, true)); } iterator[kEnded] = true; }); stream.on('readable', onReadable.bind(null, iterator)); return iterator; }; module.exports = createReadableStreamAsyncIterator; }).call(this)}).call(this,require('_process')) },{"./end-of-stream":165,"_process":152}],163:[function(require,module,exports){ 'use strict'; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var _require = require('buffer'), Buffer = _require.Buffer; var _require2 = require('util'), inspect = _require2.inspect; var custom = inspect && inspect.custom || 'inspect'; function copyBuffer(src, target, offset) { Buffer.prototype.copy.call(src, target, offset); } module.exports = /*#__PURE__*/ function () { function BufferList() { _classCallCheck(this, BufferList); this.head = null; this.tail = null; this.length = 0; } _createClass(BufferList, [{ key: "push", value: function push(v) { var entry = { data: v, next: null }; if (this.length > 0) this.tail.next = entry;else this.head = entry; this.tail = entry; ++this.length; } }, { key: "unshift", value: function unshift(v) { var entry = { data: v, next: this.head }; if (this.length === 0) this.tail = entry; this.head = entry; ++this.length; } }, { key: "shift", value: function shift() { if (this.length === 0) return; var ret = this.head.data; if (this.length === 1) this.head = this.tail = null;else this.head = this.head.next; --this.length; return ret; } }, { key: "clear", value: function clear() { this.head = this.tail = null; this.length = 0; } }, { key: "join", value: function join(s) { if (this.length === 0) return ''; var p = this.head; var ret = '' + p.data; while (p = p.next) { ret += s + p.data; } return ret; } }, { key: "concat", value: function concat(n) { if (this.length === 0) return Buffer.alloc(0); var ret = Buffer.allocUnsafe(n >>> 0); var p = this.head; var i = 0; while (p) { copyBuffer(p.data, ret, i); i += p.data.length; p = p.next; } return ret; } // Consumes a specified amount of bytes or characters from the buffered data. }, { key: "consume", value: function consume(n, hasStrings) { var ret; if (n < this.head.data.length) { // `slice` is the same for buffers and strings. ret = this.head.data.slice(0, n); this.head.data = this.head.data.slice(n); } else if (n === this.head.data.length) { // First chunk is a perfect match. ret = this.shift(); } else { // Result spans more than one buffer. ret = hasStrings ? this._getString(n) : this._getBuffer(n); } return ret; } }, { key: "first", value: function first() { return this.head.data; } // Consumes a specified amount of characters from the buffered data. }, { key: "_getString", value: function _getString(n) { var p = this.head; var c = 1; var ret = p.data; n -= ret.length; while (p = p.next) { var str = p.data; var nb = n > str.length ? str.length : n; if (nb === str.length) ret += str;else ret += str.slice(0, n); n -= nb; if (n === 0) { if (nb === str.length) { ++c; if (p.next) this.head = p.next;else this.head = this.tail = null; } else { this.head = p; p.data = str.slice(nb); } break; } ++c; } this.length -= c; return ret; } // Consumes a specified amount of bytes from the buffered data. }, { key: "_getBuffer", value: function _getBuffer(n) { var ret = Buffer.allocUnsafe(n); var p = this.head; var c = 1; p.data.copy(ret); n -= p.data.length; while (p = p.next) { var buf = p.data; var nb = n > buf.length ? buf.length : n; buf.copy(ret, ret.length - n, 0, nb); n -= nb; if (n === 0) { if (nb === buf.length) { ++c; if (p.next) this.head = p.next;else this.head = this.tail = null; } else { this.head = p; p.data = buf.slice(nb); } break; } ++c; } this.length -= c; return ret; } // Make sure the linked list only shows the minimal necessary information. }, { key: custom, value: function value(_, options) { return inspect(this, _objectSpread({}, options, { // Only inspect one level. depth: 0, // It should not recurse. customInspect: false })); } }]); return BufferList; }(); },{"buffer":47,"util":29}],164:[function(require,module,exports){ (function (process){(function (){ 'use strict'; // undocumented cb() API, needed for core, not for public API function destroy(err, cb) { var _this = this; var readableDestroyed = this._readableState && this._readableState.destroyed; var writableDestroyed = this._writableState && this._writableState.destroyed; if (readableDestroyed || writableDestroyed) { if (cb) { cb(err); } else if (err) { if (!this._writableState) { process.nextTick(emitErrorNT, this, err); } else if (!this._writableState.errorEmitted) { this._writableState.errorEmitted = true; process.nextTick(emitErrorNT, this, err); } } return this; } // we set destroyed to true before firing error callbacks in order // to make it re-entrance safe in case destroy() is called within callbacks if (this._readableState) { this._readableState.destroyed = true; } // if this is a duplex stream mark the writable part as destroyed as well if (this._writableState) { this._writableState.destroyed = true; } this._destroy(err || null, function (err) { if (!cb && err) { if (!_this._writableState) { process.nextTick(emitErrorAndCloseNT, _this, err); } else if (!_this._writableState.errorEmitted) { _this._writableState.errorEmitted = true; process.nextTick(emitErrorAndCloseNT, _this, err); } else { process.nextTick(emitCloseNT, _this); } } else if (cb) { process.nextTick(emitCloseNT, _this); cb(err); } else { process.nextTick(emitCloseNT, _this); } }); return this; } function emitErrorAndCloseNT(self, err) { emitErrorNT(self, err); emitCloseNT(self); } function emitCloseNT(self) { if (self._writableState && !self._writableState.emitClose) return; if (self._readableState && !self._readableState.emitClose) return; self.emit('close'); } function undestroy() { if (this._readableState) { this._readableState.destroyed = false; this._readableState.reading = false; this._readableState.ended = false; this._readableState.endEmitted = false; } if (this._writableState) { this._writableState.destroyed = false; this._writableState.ended = false; this._writableState.ending = false; this._writableState.finalCalled = false; this._writableState.prefinished = false; this._writableState.finished = false; this._writableState.errorEmitted = false; } } function emitErrorNT(self, err) { self.emit('error', err); } function errorOrDestroy(stream, err) { // We have tests that rely on errors being emitted // in the same tick, so changing this is semver major. // For now when you opt-in to autoDestroy we allow // the error to be emitted nextTick. In a future // semver major update we should change the default to this. var rState = stream._readableState; var wState = stream._writableState; if (rState && rState.autoDestroy || wState && wState.autoDestroy) stream.destroy(err);else stream.emit('error', err); } module.exports = { destroy: destroy, undestroy: undestroy, errorOrDestroy: errorOrDestroy }; }).call(this)}).call(this,require('_process')) },{"_process":152}],165:[function(require,module,exports){ // Ported from https://github.com/mafintosh/end-of-stream with // permission from the author, Mathias Buus (@mafintosh). 'use strict'; var ERR_STREAM_PREMATURE_CLOSE = require('../../../errors').codes.ERR_STREAM_PREMATURE_CLOSE; function once(callback) { var called = false; return function () { if (called) return; called = true; for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } callback.apply(this, args); }; } function noop() {} function isRequest(stream) { return stream.setHeader && typeof stream.abort === 'function'; } function eos(stream, opts, callback) { if (typeof opts === 'function') return eos(stream, null, opts); if (!opts) opts = {}; callback = once(callback || noop); var readable = opts.readable || opts.readable !== false && stream.readable; var writable = opts.writable || opts.writable !== false && stream.writable; var onlegacyfinish = function onlegacyfinish() { if (!stream.writable) onfinish(); }; var writableEnded = stream._writableState && stream._writableState.finished; var onfinish = function onfinish() { writable = false; writableEnded = true; if (!readable) callback.call(stream); }; var readableEnded = stream._readableState && stream._readableState.endEmitted; var onend = function onend() { readable = false; readableEnded = true; if (!writable) callback.call(stream); }; var onerror = function onerror(err) { callback.call(stream, err); }; var onclose = function onclose() { var err; if (readable && !readableEnded) { if (!stream._readableState || !stream._readableState.ended) err = new ERR_STREAM_PREMATURE_CLOSE(); return callback.call(stream, err); } if (writable && !writableEnded) { if (!stream._writableState || !stream._writableState.ended) err = new ERR_STREAM_PREMATURE_CLOSE(); return callback.call(stream, err); } }; var onrequest = function onrequest() { stream.req.on('finish', onfinish); }; if (isRequest(stream)) { stream.on('complete', onfinish); stream.on('abort', onclose); if (stream.req) onrequest();else stream.on('request', onrequest); } else if (writable && !stream._writableState) { // legacy streams stream.on('end', onlegacyfinish); stream.on('close', onlegacyfinish); } stream.on('end', onend); stream.on('finish', onfinish); if (opts.error !== false) stream.on('error', onerror); stream.on('close', onclose); return function () { stream.removeListener('complete', onfinish); stream.removeListener('abort', onclose); stream.removeListener('request', onrequest); if (stream.req) stream.req.removeListener('finish', onfinish); stream.removeListener('end', onlegacyfinish); stream.removeListener('close', onlegacyfinish); stream.removeListener('finish', onfinish); stream.removeListener('end', onend); stream.removeListener('error', onerror); stream.removeListener('close', onclose); }; } module.exports = eos; },{"../../../errors":156}],166:[function(require,module,exports){ module.exports = function () { throw new Error('Readable.from is not available in the browser') }; },{}],167:[function(require,module,exports){ // Ported from https://github.com/mafintosh/pump with // permission from the author, Mathias Buus (@mafintosh). 'use strict'; var eos; function once(callback) { var called = false; return function () { if (called) return; called = true; callback.apply(void 0, arguments); }; } var _require$codes = require('../../../errors').codes, ERR_MISSING_ARGS = _require$codes.ERR_MISSING_ARGS, ERR_STREAM_DESTROYED = _require$codes.ERR_STREAM_DESTROYED; function noop(err) { // Rethrow the error if it exists to avoid swallowing it if (err) throw err; } function isRequest(stream) { return stream.setHeader && typeof stream.abort === 'function'; } function destroyer(stream, reading, writing, callback) { callback = once(callback); var closed = false; stream.on('close', function () { closed = true; }); if (eos === undefined) eos = require('./end-of-stream'); eos(stream, { readable: reading, writable: writing }, function (err) { if (err) return callback(err); closed = true; callback(); }); var destroyed = false; return function (err) { if (closed) return; if (destroyed) return; destroyed = true; // request.destroy just do .end - .abort is what we want if (isRequest(stream)) return stream.abort(); if (typeof stream.destroy === 'function') return stream.destroy(); callback(err || new ERR_STREAM_DESTROYED('pipe')); }; } function call(fn) { fn(); } function pipe(from, to) { return from.pipe(to); } function popCallback(streams) { if (!streams.length) return noop; if (typeof streams[streams.length - 1] !== 'function') return noop; return streams.pop(); } function pipeline() { for (var _len = arguments.length, streams = new Array(_len), _key = 0; _key < _len; _key++) { streams[_key] = arguments[_key]; } var callback = popCallback(streams); if (Array.isArray(streams[0])) streams = streams[0]; if (streams.length < 2) { throw new ERR_MISSING_ARGS('streams'); } var error; var destroys = streams.map(function (stream, i) { var reading = i < streams.length - 1; var writing = i > 0; return destroyer(stream, reading, writing, function (err) { if (!error) error = err; if (err) destroys.forEach(call); if (reading) return; destroys.forEach(call); callback(error); }); }); return streams.reduce(pipe); } module.exports = pipeline; },{"../../../errors":156,"./end-of-stream":165}],168:[function(require,module,exports){ 'use strict'; var ERR_INVALID_OPT_VALUE = require('../../../errors').codes.ERR_INVALID_OPT_VALUE; function highWaterMarkFrom(options, isDuplex, duplexKey) { return options.highWaterMark != null ? options.highWaterMark : isDuplex ? options[duplexKey] : null; } function getHighWaterMark(state, options, duplexKey, isDuplex) { var hwm = highWaterMarkFrom(options, isDuplex, duplexKey); if (hwm != null) { if (!(isFinite(hwm) && Math.floor(hwm) === hwm) || hwm < 0) { var name = isDuplex ? duplexKey : 'highWaterMark'; throw new ERR_INVALID_OPT_VALUE(name, hwm); } return Math.floor(hwm); } // Default value return state.objectMode ? 16 : 16 * 1024; } module.exports = { getHighWaterMark: getHighWaterMark }; },{"../../../errors":156}],169:[function(require,module,exports){ arguments[4][41][0].apply(exports,arguments) },{"dup":41,"events":80}],170:[function(require,module,exports){ exports = module.exports = require('./lib/_stream_readable.js'); exports.Stream = exports; exports.Readable = exports; exports.Writable = require('./lib/_stream_writable.js'); exports.Duplex = require('./lib/_stream_duplex.js'); exports.Transform = require('./lib/_stream_transform.js'); exports.PassThrough = require('./lib/_stream_passthrough.js'); exports.finished = require('./lib/internal/streams/end-of-stream.js'); exports.pipeline = require('./lib/internal/streams/pipeline.js'); },{"./lib/_stream_duplex.js":157,"./lib/_stream_passthrough.js":158,"./lib/_stream_readable.js":159,"./lib/_stream_transform.js":160,"./lib/_stream_writable.js":161,"./lib/internal/streams/end-of-stream.js":165,"./lib/internal/streams/pipeline.js":167}],171:[function(require,module,exports){ var EventEmitter = require('events').EventEmitter var backoff = require('backoff') var noop = function () {} module.exports = function (createConnection) { return function (opts, onConnect) { onConnect = 'function' == typeof opts ? opts : onConnect opts = 'object' == typeof opts ? opts : {initialDelay: 1e3, maxDelay: 30e3} if(!onConnect) onConnect = opts.onConnect var emitter = opts.emitter || new EventEmitter() emitter.connected = false emitter.reconnect = true if(onConnect) emitter.on('connect', onConnect) var backoffMethod = (backoff[opts.type] || backoff.fibonacci) (opts) backoffMethod.on('backoff', function (n, d) { emitter.emit('backoff', n, d) }) var args var cleanup = noop backoffMethod.on('ready', attempt) function attempt (n, delay) { if(!emitter.reconnect) return cleanup() emitter.emit('reconnect', n, delay) var con = createConnection.apply(null, args) if (con !== emitter._connection) emitter.emit('connection', con) emitter._connection = con cleanup = onCleanup function onCleanup(err) { cleanup = noop con.removeListener('connect', connect) con.removeListener('error', onDisconnect) con.removeListener('close', onDisconnect) con.removeListener('end' , onDisconnect) //hack to make http not crash. //HTTP IS THE WORST PROTOCOL. if(con.constructor.name == 'Request') con.on('error', noop) } function onDisconnect (err) { emitter.connected = false onCleanup(err) //emit disconnect before checking reconnect, so user has a chance to decide not to. emitter.emit('disconnect', err) if(!emitter.reconnect) return try { backoffMethod.backoff() } catch (_) { } } function connect() { backoffMethod.reset() emitter.connected = true if(onConnect) con.removeListener('connect', onConnect) emitter.emit('connect', con) } con .on('error', onDisconnect) .on('close', onDisconnect) .on('end' , onDisconnect) if(opts.immediate || con.constructor.name == 'Request') { emitter.connected = true emitter.emit('connect', con) con.once('data', function () { //this is the only way to know for sure that data is coming... backoffMethod.reset() }) } else { con.on('connect', connect) } } emitter.connect = emitter.listen = function () { this.reconnect = true backoffMethod.reset() args = [].slice.call(arguments) attempt(0, 0) return emitter } //force reconnection emitter.end = emitter.disconnect = function () { emitter.reconnect = false if(emitter._connection) emitter._connection.end() emitter.emit('disconnect') return emitter } return emitter } } },{"backoff":21,"events":80}],172:[function(require,module,exports){ /* * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. */ /* eslint-env node */ 'use strict'; var SDPUtils = require('sdp'); function fixStatsType(stat) { return { inboundrtp: 'inbound-rtp', outboundrtp: 'outbound-rtp', candidatepair: 'candidate-pair', localcandidate: 'local-candidate', remotecandidate: 'remote-candidate' }[stat.type] || stat.type; } function writeMediaSection(transceiver, caps, type, stream, dtlsRole) { var sdp = SDPUtils.writeRtpDescription(transceiver.kind, caps); // Map ICE parameters (ufrag, pwd) to SDP. sdp += SDPUtils.writeIceParameters( transceiver.iceGatherer.getLocalParameters()); // Map DTLS parameters to SDP. sdp += SDPUtils.writeDtlsParameters( transceiver.dtlsTransport.getLocalParameters(), type === 'offer' ? 'actpass' : dtlsRole || 'active'); sdp += 'a=mid:' + transceiver.mid + '\r\n'; if (transceiver.rtpSender && transceiver.rtpReceiver) { sdp += 'a=sendrecv\r\n'; } else if (transceiver.rtpSender) { sdp += 'a=sendonly\r\n'; } else if (transceiver.rtpReceiver) { sdp += 'a=recvonly\r\n'; } else { sdp += 'a=inactive\r\n'; } if (transceiver.rtpSender) { var trackId = transceiver.rtpSender._initialTrackId || transceiver.rtpSender.track.id; transceiver.rtpSender._initialTrackId = trackId; // spec. var msid = 'msid:' + (stream ? stream.id : '-') + ' ' + trackId + '\r\n'; sdp += 'a=' + msid; // for Chrome. Legacy should no longer be required. sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].ssrc + ' ' + msid; // RTX if (transceiver.sendEncodingParameters[0].rtx) { sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].rtx.ssrc + ' ' + msid; sdp += 'a=ssrc-group:FID ' + transceiver.sendEncodingParameters[0].ssrc + ' ' + transceiver.sendEncodingParameters[0].rtx.ssrc + '\r\n'; } } // FIXME: this should be written by writeRtpDescription. sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].ssrc + ' cname:' + SDPUtils.localCName + '\r\n'; if (transceiver.rtpSender && transceiver.sendEncodingParameters[0].rtx) { sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].rtx.ssrc + ' cname:' + SDPUtils.localCName + '\r\n'; } return sdp; } // Edge does not like // 1) stun: filtered after 14393 unless ?transport=udp is present // 2) turn: that does not have all of turn:host:port?transport=udp // 3) turn: with ipv6 addresses // 4) turn: occurring muliple times function filterIceServers(iceServers, edgeVersion) { var hasTurn = false; iceServers = JSON.parse(JSON.stringify(iceServers)); return iceServers.filter(function(server) { if (server && (server.urls || server.url)) { var urls = server.urls || server.url; if (server.url && !server.urls) { console.warn('RTCIceServer.url is deprecated! Use urls instead.'); } var isString = typeof urls === 'string'; if (isString) { urls = [urls]; } urls = urls.filter(function(url) { var validTurn = url.indexOf('turn:') === 0 && url.indexOf('transport=udp') !== -1 && url.indexOf('turn:[') === -1 && !hasTurn; if (validTurn) { hasTurn = true; return true; } return url.indexOf('stun:') === 0 && edgeVersion >= 14393 && url.indexOf('?transport=udp') === -1; }); delete server.url; server.urls = isString ? urls[0] : urls; return !!urls.length; } }); } // Determines the intersection of local and remote capabilities. function getCommonCapabilities(localCapabilities, remoteCapabilities) { var commonCapabilities = { codecs: [], headerExtensions: [], fecMechanisms: [] }; var findCodecByPayloadType = function(pt, codecs) { pt = parseInt(pt, 10); for (var i = 0; i < codecs.length; i++) { if (codecs[i].payloadType === pt || codecs[i].preferredPayloadType === pt) { return codecs[i]; } } }; var rtxCapabilityMatches = function(lRtx, rRtx, lCodecs, rCodecs) { var lCodec = findCodecByPayloadType(lRtx.parameters.apt, lCodecs); var rCodec = findCodecByPayloadType(rRtx.parameters.apt, rCodecs); return lCodec && rCodec && lCodec.name.toLowerCase() === rCodec.name.toLowerCase(); }; localCapabilities.codecs.forEach(function(lCodec) { for (var i = 0; i < remoteCapabilities.codecs.length; i++) { var rCodec = remoteCapabilities.codecs[i]; if (lCodec.name.toLowerCase() === rCodec.name.toLowerCase() && lCodec.clockRate === rCodec.clockRate) { if (lCodec.name.toLowerCase() === 'rtx' && lCodec.parameters && rCodec.parameters.apt) { // for RTX we need to find the local rtx that has a apt // which points to the same local codec as the remote one. if (!rtxCapabilityMatches(lCodec, rCodec, localCapabilities.codecs, remoteCapabilities.codecs)) { continue; } } rCodec = JSON.parse(JSON.stringify(rCodec)); // deepcopy // number of channels is the highest common number of channels rCodec.numChannels = Math.min(lCodec.numChannels, rCodec.numChannels); // push rCodec so we reply with offerer payload type commonCapabilities.codecs.push(rCodec); // determine common feedback mechanisms rCodec.rtcpFeedback = rCodec.rtcpFeedback.filter(function(fb) { for (var j = 0; j < lCodec.rtcpFeedback.length; j++) { if (lCodec.rtcpFeedback[j].type === fb.type && lCodec.rtcpFeedback[j].parameter === fb.parameter) { return true; } } return false; }); // FIXME: also need to determine .parameters // see https://github.com/openpeer/ortc/issues/569 break; } } }); localCapabilities.headerExtensions.forEach(function(lHeaderExtension) { for (var i = 0; i < remoteCapabilities.headerExtensions.length; i++) { var rHeaderExtension = remoteCapabilities.headerExtensions[i]; if (lHeaderExtension.uri === rHeaderExtension.uri) { commonCapabilities.headerExtensions.push(rHeaderExtension); break; } } }); // FIXME: fecMechanisms return commonCapabilities; } // is action=setLocalDescription with type allowed in signalingState function isActionAllowedInSignalingState(action, type, signalingState) { return { offer: { setLocalDescription: ['stable', 'have-local-offer'], setRemoteDescription: ['stable', 'have-remote-offer'] }, answer: { setLocalDescription: ['have-remote-offer', 'have-local-pranswer'], setRemoteDescription: ['have-local-offer', 'have-remote-pranswer'] } }[type][action].indexOf(signalingState) !== -1; } function maybeAddCandidate(iceTransport, candidate) { // Edge's internal representation adds some fields therefore // not all fieldѕ are taken into account. var alreadyAdded = iceTransport.getRemoteCandidates() .find(function(remoteCandidate) { return candidate.foundation === remoteCandidate.foundation && candidate.ip === remoteCandidate.ip && candidate.port === remoteCandidate.port && candidate.priority === remoteCandidate.priority && candidate.protocol === remoteCandidate.protocol && candidate.type === remoteCandidate.type; }); if (!alreadyAdded) { iceTransport.addRemoteCandidate(candidate); } return !alreadyAdded; } function makeError(name, description) { var e = new Error(description); e.name = name; // legacy error codes from https://heycam.github.io/webidl/#idl-DOMException-error-names e.code = { NotSupportedError: 9, InvalidStateError: 11, InvalidAccessError: 15, TypeError: undefined, OperationError: undefined }[name]; return e; } module.exports = function(window, edgeVersion) { // https://w3c.github.io/mediacapture-main/#mediastream // Helper function to add the track to the stream and // dispatch the event ourselves. function addTrackToStreamAndFireEvent(track, stream) { stream.addTrack(track); stream.dispatchEvent(new window.MediaStreamTrackEvent('addtrack', {track: track})); } function removeTrackFromStreamAndFireEvent(track, stream) { stream.removeTrack(track); stream.dispatchEvent(new window.MediaStreamTrackEvent('removetrack', {track: track})); } function fireAddTrack(pc, track, receiver, streams) { var trackEvent = new Event('track'); trackEvent.track = track; trackEvent.receiver = receiver; trackEvent.transceiver = {receiver: receiver}; trackEvent.streams = streams; window.setTimeout(function() { pc._dispatchEvent('track', trackEvent); }); } var RTCPeerConnection = function(config) { var pc = this; var _eventTarget = document.createDocumentFragment(); ['addEventListener', 'removeEventListener', 'dispatchEvent'] .forEach(function(method) { pc[method] = _eventTarget[method].bind(_eventTarget); }); this.canTrickleIceCandidates = null; this.needNegotiation = false; this.localStreams = []; this.remoteStreams = []; this._localDescription = null; this._remoteDescription = null; this.signalingState = 'stable'; this.iceConnectionState = 'new'; this.connectionState = 'new'; this.iceGatheringState = 'new'; config = JSON.parse(JSON.stringify(config || {})); this.usingBundle = config.bundlePolicy === 'max-bundle'; if (config.rtcpMuxPolicy === 'negotiate') { throw(makeError('NotSupportedError', 'rtcpMuxPolicy \'negotiate\' is not supported')); } else if (!config.rtcpMuxPolicy) { config.rtcpMuxPolicy = 'require'; } switch (config.iceTransportPolicy) { case 'all': case 'relay': break; default: config.iceTransportPolicy = 'all'; break; } switch (config.bundlePolicy) { case 'balanced': case 'max-compat': case 'max-bundle': break; default: config.bundlePolicy = 'balanced'; break; } config.iceServers = filterIceServers(config.iceServers || [], edgeVersion); this._iceGatherers = []; if (config.iceCandidatePoolSize) { for (var i = config.iceCandidatePoolSize; i > 0; i--) { this._iceGatherers.push(new window.RTCIceGatherer({ iceServers: config.iceServers, gatherPolicy: config.iceTransportPolicy })); } } else { config.iceCandidatePoolSize = 0; } this._config = config; // per-track iceGathers, iceTransports, dtlsTransports, rtpSenders, ... // everything that is needed to describe a SDP m-line. this.transceivers = []; this._sdpSessionId = SDPUtils.generateSessionId(); this._sdpSessionVersion = 0; this._dtlsRole = undefined; // role for a=setup to use in answers. this._isClosed = false; }; Object.defineProperty(RTCPeerConnection.prototype, 'localDescription', { configurable: true, get: function() { return this._localDescription; } }); Object.defineProperty(RTCPeerConnection.prototype, 'remoteDescription', { configurable: true, get: function() { return this._remoteDescription; } }); // set up event handlers on prototype RTCPeerConnection.prototype.onicecandidate = null; RTCPeerConnection.prototype.onaddstream = null; RTCPeerConnection.prototype.ontrack = null; RTCPeerConnection.prototype.onremovestream = null; RTCPeerConnection.prototype.onsignalingstatechange = null; RTCPeerConnection.prototype.oniceconnectionstatechange = null; RTCPeerConnection.prototype.onconnectionstatechange = null; RTCPeerConnection.prototype.onicegatheringstatechange = null; RTCPeerConnection.prototype.onnegotiationneeded = null; RTCPeerConnection.prototype.ondatachannel = null; RTCPeerConnection.prototype._dispatchEvent = function(name, event) { if (this._isClosed) { return; } this.dispatchEvent(event); if (typeof this['on' + name] === 'function') { this['on' + name](event); } }; RTCPeerConnection.prototype._emitGatheringStateChange = function() { var event = new Event('icegatheringstatechange'); this._dispatchEvent('icegatheringstatechange', event); }; RTCPeerConnection.prototype.getConfiguration = function() { return this._config; }; RTCPeerConnection.prototype.getLocalStreams = function() { return this.localStreams; }; RTCPeerConnection.prototype.getRemoteStreams = function() { return this.remoteStreams; }; // internal helper to create a transceiver object. // (which is not yet the same as the WebRTC 1.0 transceiver) RTCPeerConnection.prototype._createTransceiver = function(kind, doNotAdd) { var hasBundleTransport = this.transceivers.length > 0; var transceiver = { track: null, iceGatherer: null, iceTransport: null, dtlsTransport: null, localCapabilities: null, remoteCapabilities: null, rtpSender: null, rtpReceiver: null, kind: kind, mid: null, sendEncodingParameters: null, recvEncodingParameters: null, stream: null, associatedRemoteMediaStreams: [], wantReceive: true }; if (this.usingBundle && hasBundleTransport) { transceiver.iceTransport = this.transceivers[0].iceTransport; transceiver.dtlsTransport = this.transceivers[0].dtlsTransport; } else { var transports = this._createIceAndDtlsTransports(); transceiver.iceTransport = transports.iceTransport; transceiver.dtlsTransport = transports.dtlsTransport; } if (!doNotAdd) { this.transceivers.push(transceiver); } return transceiver; }; RTCPeerConnection.prototype.addTrack = function(track, stream) { if (this._isClosed) { throw makeError('InvalidStateError', 'Attempted to call addTrack on a closed peerconnection.'); } var alreadyExists = this.transceivers.find(function(s) { return s.track === track; }); if (alreadyExists) { throw makeError('InvalidAccessError', 'Track already exists.'); } var transceiver; for (var i = 0; i < this.transceivers.length; i++) { if (!this.transceivers[i].track && this.transceivers[i].kind === track.kind) { transceiver = this.transceivers[i]; } } if (!transceiver) { transceiver = this._createTransceiver(track.kind); } this._maybeFireNegotiationNeeded(); if (this.localStreams.indexOf(stream) === -1) { this.localStreams.push(stream); } transceiver.track = track; transceiver.stream = stream; transceiver.rtpSender = new window.RTCRtpSender(track, transceiver.dtlsTransport); return transceiver.rtpSender; }; RTCPeerConnection.prototype.addStream = function(stream) { var pc = this; if (edgeVersion >= 15025) { stream.getTracks().forEach(function(track) { pc.addTrack(track, stream); }); } else { // Clone is necessary for local demos mostly, attaching directly // to two different senders does not work (build 10547). // Fixed in 15025 (or earlier) var clonedStream = stream.clone(); stream.getTracks().forEach(function(track, idx) { var clonedTrack = clonedStream.getTracks()[idx]; track.addEventListener('enabled', function(event) { clonedTrack.enabled = event.enabled; }); }); clonedStream.getTracks().forEach(function(track) { pc.addTrack(track, clonedStream); }); } }; RTCPeerConnection.prototype.removeTrack = function(sender) { if (this._isClosed) { throw makeError('InvalidStateError', 'Attempted to call removeTrack on a closed peerconnection.'); } if (!(sender instanceof window.RTCRtpSender)) { throw new TypeError('Argument 1 of RTCPeerConnection.removeTrack ' + 'does not implement interface RTCRtpSender.'); } var transceiver = this.transceivers.find(function(t) { return t.rtpSender === sender; }); if (!transceiver) { throw makeError('InvalidAccessError', 'Sender was not created by this connection.'); } var stream = transceiver.stream; transceiver.rtpSender.stop(); transceiver.rtpSender = null; transceiver.track = null; transceiver.stream = null; // remove the stream from the set of local streams var localStreams = this.transceivers.map(function(t) { return t.stream; }); if (localStreams.indexOf(stream) === -1 && this.localStreams.indexOf(stream) > -1) { this.localStreams.splice(this.localStreams.indexOf(stream), 1); } this._maybeFireNegotiationNeeded(); }; RTCPeerConnection.prototype.removeStream = function(stream) { var pc = this; stream.getTracks().forEach(function(track) { var sender = pc.getSenders().find(function(s) { return s.track === track; }); if (sender) { pc.removeTrack(sender); } }); }; RTCPeerConnection.prototype.getSenders = function() { return this.transceivers.filter(function(transceiver) { return !!transceiver.rtpSender; }) .map(function(transceiver) { return transceiver.rtpSender; }); }; RTCPeerConnection.prototype.getReceivers = function() { return this.transceivers.filter(function(transceiver) { return !!transceiver.rtpReceiver; }) .map(function(transceiver) { return transceiver.rtpReceiver; }); }; RTCPeerConnection.prototype._createIceGatherer = function(sdpMLineIndex, usingBundle) { var pc = this; if (usingBundle && sdpMLineIndex > 0) { return this.transceivers[0].iceGatherer; } else if (this._iceGatherers.length) { return this._iceGatherers.shift(); } var iceGatherer = new window.RTCIceGatherer({ iceServers: this._config.iceServers, gatherPolicy: this._config.iceTransportPolicy }); Object.defineProperty(iceGatherer, 'state', {value: 'new', writable: true} ); this.transceivers[sdpMLineIndex].bufferedCandidateEvents = []; this.transceivers[sdpMLineIndex].bufferCandidates = function(event) { var end = !event.candidate || Object.keys(event.candidate).length === 0; // polyfill since RTCIceGatherer.state is not implemented in // Edge 10547 yet. iceGatherer.state = end ? 'completed' : 'gathering'; if (pc.transceivers[sdpMLineIndex].bufferedCandidateEvents !== null) { pc.transceivers[sdpMLineIndex].bufferedCandidateEvents.push(event); } }; iceGatherer.addEventListener('localcandidate', this.transceivers[sdpMLineIndex].bufferCandidates); return iceGatherer; }; // start gathering from an RTCIceGatherer. RTCPeerConnection.prototype._gather = function(mid, sdpMLineIndex) { var pc = this; var iceGatherer = this.transceivers[sdpMLineIndex].iceGatherer; if (iceGatherer.onlocalcandidate) { return; } var bufferedCandidateEvents = this.transceivers[sdpMLineIndex].bufferedCandidateEvents; this.transceivers[sdpMLineIndex].bufferedCandidateEvents = null; iceGatherer.removeEventListener('localcandidate', this.transceivers[sdpMLineIndex].bufferCandidates); iceGatherer.onlocalcandidate = function(evt) { if (pc.usingBundle && sdpMLineIndex > 0) { // if we know that we use bundle we can drop candidates with // ѕdpMLineIndex > 0. If we don't do this then our state gets // confused since we dispose the extra ice gatherer. return; } var event = new Event('icecandidate'); event.candidate = {sdpMid: mid, sdpMLineIndex: sdpMLineIndex}; var cand = evt.candidate; // Edge emits an empty object for RTCIceCandidateComplete‥ var end = !cand || Object.keys(cand).length === 0; if (end) { // polyfill since RTCIceGatherer.state is not implemented in // Edge 10547 yet. if (iceGatherer.state === 'new' || iceGatherer.state === 'gathering') { iceGatherer.state = 'completed'; } } else { if (iceGatherer.state === 'new') { iceGatherer.state = 'gathering'; } // RTCIceCandidate doesn't have a component, needs to be added cand.component = 1; // also the usernameFragment. TODO: update SDP to take both variants. cand.ufrag = iceGatherer.getLocalParameters().usernameFragment; var serializedCandidate = SDPUtils.writeCandidate(cand); event.candidate = Object.assign(event.candidate, SDPUtils.parseCandidate(serializedCandidate)); event.candidate.candidate = serializedCandidate; event.candidate.toJSON = function() { return { candidate: event.candidate.candidate, sdpMid: event.candidate.sdpMid, sdpMLineIndex: event.candidate.sdpMLineIndex, usernameFragment: event.candidate.usernameFragment }; }; } // update local description. var sections = SDPUtils.getMediaSections(pc._localDescription.sdp); if (!end) { sections[event.candidate.sdpMLineIndex] += 'a=' + event.candidate.candidate + '\r\n'; } else { sections[event.candidate.sdpMLineIndex] += 'a=end-of-candidates\r\n'; } pc._localDescription.sdp = SDPUtils.getDescription(pc._localDescription.sdp) + sections.join(''); var complete = pc.transceivers.every(function(transceiver) { return transceiver.iceGatherer && transceiver.iceGatherer.state === 'completed'; }); if (pc.iceGatheringState !== 'gathering') { pc.iceGatheringState = 'gathering'; pc._emitGatheringStateChange(); } // Emit candidate. Also emit null candidate when all gatherers are // complete. if (!end) { pc._dispatchEvent('icecandidate', event); } if (complete) { pc._dispatchEvent('icecandidate', new Event('icecandidate')); pc.iceGatheringState = 'complete'; pc._emitGatheringStateChange(); } }; // emit already gathered candidates. window.setTimeout(function() { bufferedCandidateEvents.forEach(function(e) { iceGatherer.onlocalcandidate(e); }); }, 0); }; // Create ICE transport and DTLS transport. RTCPeerConnection.prototype._createIceAndDtlsTransports = function() { var pc = this; var iceTransport = new window.RTCIceTransport(null); iceTransport.onicestatechange = function() { pc._updateIceConnectionState(); pc._updateConnectionState(); }; var dtlsTransport = new window.RTCDtlsTransport(iceTransport); dtlsTransport.ondtlsstatechange = function() { pc._updateConnectionState(); }; dtlsTransport.onerror = function() { // onerror does not set state to failed by itself. Object.defineProperty(dtlsTransport, 'state', {value: 'failed', writable: true}); pc._updateConnectionState(); }; return { iceTransport: iceTransport, dtlsTransport: dtlsTransport }; }; // Destroy ICE gatherer, ICE transport and DTLS transport. // Without triggering the callbacks. RTCPeerConnection.prototype._disposeIceAndDtlsTransports = function( sdpMLineIndex) { var iceGatherer = this.transceivers[sdpMLineIndex].iceGatherer; if (iceGatherer) { delete iceGatherer.onlocalcandidate; delete this.transceivers[sdpMLineIndex].iceGatherer; } var iceTransport = this.transceivers[sdpMLineIndex].iceTransport; if (iceTransport) { delete iceTransport.onicestatechange; delete this.transceivers[sdpMLineIndex].iceTransport; } var dtlsTransport = this.transceivers[sdpMLineIndex].dtlsTransport; if (dtlsTransport) { delete dtlsTransport.ondtlsstatechange; delete dtlsTransport.onerror; delete this.transceivers[sdpMLineIndex].dtlsTransport; } }; // Start the RTP Sender and Receiver for a transceiver. RTCPeerConnection.prototype._transceive = function(transceiver, send, recv) { var params = getCommonCapabilities(transceiver.localCapabilities, transceiver.remoteCapabilities); if (send && transceiver.rtpSender) { params.encodings = transceiver.sendEncodingParameters; params.rtcp = { cname: SDPUtils.localCName, compound: transceiver.rtcpParameters.compound }; if (transceiver.recvEncodingParameters.length) { params.rtcp.ssrc = transceiver.recvEncodingParameters[0].ssrc; } transceiver.rtpSender.send(params); } if (recv && transceiver.rtpReceiver && params.codecs.length > 0) { // remove RTX field in Edge 14942 if (transceiver.kind === 'video' && transceiver.recvEncodingParameters && edgeVersion < 15019) { transceiver.recvEncodingParameters.forEach(function(p) { delete p.rtx; }); } if (transceiver.recvEncodingParameters.length) { params.encodings = transceiver.recvEncodingParameters; } else { params.encodings = [{}]; } params.rtcp = { compound: transceiver.rtcpParameters.compound }; if (transceiver.rtcpParameters.cname) { params.rtcp.cname = transceiver.rtcpParameters.cname; } if (transceiver.sendEncodingParameters.length) { params.rtcp.ssrc = transceiver.sendEncodingParameters[0].ssrc; } transceiver.rtpReceiver.receive(params); } }; RTCPeerConnection.prototype.setLocalDescription = function(description) { var pc = this; // Note: pranswer is not supported. if (['offer', 'answer'].indexOf(description.type) === -1) { return Promise.reject(makeError('TypeError', 'Unsupported type "' + description.type + '"')); } if (!isActionAllowedInSignalingState('setLocalDescription', description.type, pc.signalingState) || pc._isClosed) { return Promise.reject(makeError('InvalidStateError', 'Can not set local ' + description.type + ' in state ' + pc.signalingState)); } var sections; var sessionpart; if (description.type === 'offer') { // VERY limited support for SDP munging. Limited to: // * changing the order of codecs sections = SDPUtils.splitSections(description.sdp); sessionpart = sections.shift(); sections.forEach(function(mediaSection, sdpMLineIndex) { var caps = SDPUtils.parseRtpParameters(mediaSection); pc.transceivers[sdpMLineIndex].localCapabilities = caps; }); pc.transceivers.forEach(function(transceiver, sdpMLineIndex) { pc._gather(transceiver.mid, sdpMLineIndex); }); } else if (description.type === 'answer') { sections = SDPUtils.splitSections(pc._remoteDescription.sdp); sessionpart = sections.shift(); var isIceLite = SDPUtils.matchPrefix(sessionpart, 'a=ice-lite').length > 0; sections.forEach(function(mediaSection, sdpMLineIndex) { var transceiver = pc.transceivers[sdpMLineIndex]; var iceGatherer = transceiver.iceGatherer; var iceTransport = transceiver.iceTransport; var dtlsTransport = transceiver.dtlsTransport; var localCapabilities = transceiver.localCapabilities; var remoteCapabilities = transceiver.remoteCapabilities; // treat bundle-only as not-rejected. var rejected = SDPUtils.isRejected(mediaSection) && SDPUtils.matchPrefix(mediaSection, 'a=bundle-only').length === 0; if (!rejected && !transceiver.rejected) { var remoteIceParameters = SDPUtils.getIceParameters( mediaSection, sessionpart); var remoteDtlsParameters = SDPUtils.getDtlsParameters( mediaSection, sessionpart); if (isIceLite) { remoteDtlsParameters.role = 'server'; } if (!pc.usingBundle || sdpMLineIndex === 0) { pc._gather(transceiver.mid, sdpMLineIndex); if (iceTransport.state === 'new') { iceTransport.start(iceGatherer, remoteIceParameters, isIceLite ? 'controlling' : 'controlled'); } if (dtlsTransport.state === 'new') { dtlsTransport.start(remoteDtlsParameters); } } // Calculate intersection of capabilities. var params = getCommonCapabilities(localCapabilities, remoteCapabilities); // Start the RTCRtpSender. The RTCRtpReceiver for this // transceiver has already been started in setRemoteDescription. pc._transceive(transceiver, params.codecs.length > 0, false); } }); } pc._localDescription = { type: description.type, sdp: description.sdp }; if (description.type === 'offer') { pc._updateSignalingState('have-local-offer'); } else { pc._updateSignalingState('stable'); } return Promise.resolve(); }; RTCPeerConnection.prototype.setRemoteDescription = function(description) { var pc = this; // Note: pranswer is not supported. if (['offer', 'answer'].indexOf(description.type) === -1) { return Promise.reject(makeError('TypeError', 'Unsupported type "' + description.type + '"')); } if (!isActionAllowedInSignalingState('setRemoteDescription', description.type, pc.signalingState) || pc._isClosed) { return Promise.reject(makeError('InvalidStateError', 'Can not set remote ' + description.type + ' in state ' + pc.signalingState)); } var streams = {}; pc.remoteStreams.forEach(function(stream) { streams[stream.id] = stream; }); var receiverList = []; var sections = SDPUtils.splitSections(description.sdp); var sessionpart = sections.shift(); var isIceLite = SDPUtils.matchPrefix(sessionpart, 'a=ice-lite').length > 0; var usingBundle = SDPUtils.matchPrefix(sessionpart, 'a=group:BUNDLE ').length > 0; pc.usingBundle = usingBundle; var iceOptions = SDPUtils.matchPrefix(sessionpart, 'a=ice-options:')[0]; if (iceOptions) { pc.canTrickleIceCandidates = iceOptions.substr(14).split(' ') .indexOf('trickle') >= 0; } else { pc.canTrickleIceCandidates = false; } sections.forEach(function(mediaSection, sdpMLineIndex) { var lines = SDPUtils.splitLines(mediaSection); var kind = SDPUtils.getKind(mediaSection); // treat bundle-only as not-rejected. var rejected = SDPUtils.isRejected(mediaSection) && SDPUtils.matchPrefix(mediaSection, 'a=bundle-only').length === 0; var protocol = lines[0].substr(2).split(' ')[2]; var direction = SDPUtils.getDirection(mediaSection, sessionpart); var remoteMsid = SDPUtils.parseMsid(mediaSection); var mid = SDPUtils.getMid(mediaSection) || SDPUtils.generateIdentifier(); // Reject datachannels which are not implemented yet. if (rejected || (kind === 'application' && (protocol === 'DTLS/SCTP' || protocol === 'UDP/DTLS/SCTP'))) { // TODO: this is dangerous in the case where a non-rejected m-line // becomes rejected. pc.transceivers[sdpMLineIndex] = { mid: mid, kind: kind, protocol: protocol, rejected: true }; return; } if (!rejected && pc.transceivers[sdpMLineIndex] && pc.transceivers[sdpMLineIndex].rejected) { // recycle a rejected transceiver. pc.transceivers[sdpMLineIndex] = pc._createTransceiver(kind, true); } var transceiver; var iceGatherer; var iceTransport; var dtlsTransport; var rtpReceiver; var sendEncodingParameters; var recvEncodingParameters; var localCapabilities; var track; // FIXME: ensure the mediaSection has rtcp-mux set. var remoteCapabilities = SDPUtils.parseRtpParameters(mediaSection); var remoteIceParameters; var remoteDtlsParameters; if (!rejected) { remoteIceParameters = SDPUtils.getIceParameters(mediaSection, sessionpart); remoteDtlsParameters = SDPUtils.getDtlsParameters(mediaSection, sessionpart); remoteDtlsParameters.role = 'client'; } recvEncodingParameters = SDPUtils.parseRtpEncodingParameters(mediaSection); var rtcpParameters = SDPUtils.parseRtcpParameters(mediaSection); var isComplete = SDPUtils.matchPrefix(mediaSection, 'a=end-of-candidates', sessionpart).length > 0; var cands = SDPUtils.matchPrefix(mediaSection, 'a=candidate:') .map(function(cand) { return SDPUtils.parseCandidate(cand); }) .filter(function(cand) { return cand.component === 1; }); // Check if we can use BUNDLE and dispose transports. if ((description.type === 'offer' || description.type === 'answer') && !rejected && usingBundle && sdpMLineIndex > 0 && pc.transceivers[sdpMLineIndex]) { pc._disposeIceAndDtlsTransports(sdpMLineIndex); pc.transceivers[sdpMLineIndex].iceGatherer = pc.transceivers[0].iceGatherer; pc.transceivers[sdpMLineIndex].iceTransport = pc.transceivers[0].iceTransport; pc.transceivers[sdpMLineIndex].dtlsTransport = pc.transceivers[0].dtlsTransport; if (pc.transceivers[sdpMLineIndex].rtpSender) { pc.transceivers[sdpMLineIndex].rtpSender.setTransport( pc.transceivers[0].dtlsTransport); } if (pc.transceivers[sdpMLineIndex].rtpReceiver) { pc.transceivers[sdpMLineIndex].rtpReceiver.setTransport( pc.transceivers[0].dtlsTransport); } } if (description.type === 'offer' && !rejected) { transceiver = pc.transceivers[sdpMLineIndex] || pc._createTransceiver(kind); transceiver.mid = mid; if (!transceiver.iceGatherer) { transceiver.iceGatherer = pc._createIceGatherer(sdpMLineIndex, usingBundle); } if (cands.length && transceiver.iceTransport.state === 'new') { if (isComplete && (!usingBundle || sdpMLineIndex === 0)) { transceiver.iceTransport.setRemoteCandidates(cands); } else { cands.forEach(function(candidate) { maybeAddCandidate(transceiver.iceTransport, candidate); }); } } localCapabilities = window.RTCRtpReceiver.getCapabilities(kind); // filter RTX until additional stuff needed for RTX is implemented // in adapter.js if (edgeVersion < 15019) { localCapabilities.codecs = localCapabilities.codecs.filter( function(codec) { return codec.name !== 'rtx'; }); } sendEncodingParameters = transceiver.sendEncodingParameters || [{ ssrc: (2 * sdpMLineIndex + 2) * 1001 }]; // TODO: rewrite to use http://w3c.github.io/webrtc-pc/#set-associated-remote-streams var isNewTrack = false; if (direction === 'sendrecv' || direction === 'sendonly') { isNewTrack = !transceiver.rtpReceiver; rtpReceiver = transceiver.rtpReceiver || new window.RTCRtpReceiver(transceiver.dtlsTransport, kind); if (isNewTrack) { var stream; track = rtpReceiver.track; // FIXME: does not work with Plan B. if (remoteMsid && remoteMsid.stream === '-') { // no-op. a stream id of '-' means: no associated stream. } else if (remoteMsid) { if (!streams[remoteMsid.stream]) { streams[remoteMsid.stream] = new window.MediaStream(); Object.defineProperty(streams[remoteMsid.stream], 'id', { get: function() { return remoteMsid.stream; } }); } Object.defineProperty(track, 'id', { get: function() { return remoteMsid.track; } }); stream = streams[remoteMsid.stream]; } else { if (!streams.default) { streams.default = new window.MediaStream(); } stream = streams.default; } if (stream) { addTrackToStreamAndFireEvent(track, stream); transceiver.associatedRemoteMediaStreams.push(stream); } receiverList.push([track, rtpReceiver, stream]); } } else if (transceiver.rtpReceiver && transceiver.rtpReceiver.track) { transceiver.associatedRemoteMediaStreams.forEach(function(s) { var nativeTrack = s.getTracks().find(function(t) { return t.id === transceiver.rtpReceiver.track.id; }); if (nativeTrack) { removeTrackFromStreamAndFireEvent(nativeTrack, s); } }); transceiver.associatedRemoteMediaStreams = []; } transceiver.localCapabilities = localCapabilities; transceiver.remoteCapabilities = remoteCapabilities; transceiver.rtpReceiver = rtpReceiver; transceiver.rtcpParameters = rtcpParameters; transceiver.sendEncodingParameters = sendEncodingParameters; transceiver.recvEncodingParameters = recvEncodingParameters; // Start the RTCRtpReceiver now. The RTPSender is started in // setLocalDescription. pc._transceive(pc.transceivers[sdpMLineIndex], false, isNewTrack); } else if (description.type === 'answer' && !rejected) { transceiver = pc.transceivers[sdpMLineIndex]; iceGatherer = transceiver.iceGatherer; iceTransport = transceiver.iceTransport; dtlsTransport = transceiver.dtlsTransport; rtpReceiver = transceiver.rtpReceiver; sendEncodingParameters = transceiver.sendEncodingParameters; localCapabilities = transceiver.localCapabilities; pc.transceivers[sdpMLineIndex].recvEncodingParameters = recvEncodingParameters; pc.transceivers[sdpMLineIndex].remoteCapabilities = remoteCapabilities; pc.transceivers[sdpMLineIndex].rtcpParameters = rtcpParameters; if (cands.length && iceTransport.state === 'new') { if ((isIceLite || isComplete) && (!usingBundle || sdpMLineIndex === 0)) { iceTransport.setRemoteCandidates(cands); } else { cands.forEach(function(candidate) { maybeAddCandidate(transceiver.iceTransport, candidate); }); } } if (!usingBundle || sdpMLineIndex === 0) { if (iceTransport.state === 'new') { iceTransport.start(iceGatherer, remoteIceParameters, 'controlling'); } if (dtlsTransport.state === 'new') { dtlsTransport.start(remoteDtlsParameters); } } // If the offer contained RTX but the answer did not, // remove RTX from sendEncodingParameters. var commonCapabilities = getCommonCapabilities( transceiver.localCapabilities, transceiver.remoteCapabilities); var hasRtx = commonCapabilities.codecs.filter(function(c) { return c.name.toLowerCase() === 'rtx'; }).length; if (!hasRtx && transceiver.sendEncodingParameters[0].rtx) { delete transceiver.sendEncodingParameters[0].rtx; } pc._transceive(transceiver, direction === 'sendrecv' || direction === 'recvonly', direction === 'sendrecv' || direction === 'sendonly'); // TODO: rewrite to use http://w3c.github.io/webrtc-pc/#set-associated-remote-streams if (rtpReceiver && (direction === 'sendrecv' || direction === 'sendonly')) { track = rtpReceiver.track; if (remoteMsid) { if (!streams[remoteMsid.stream]) { streams[remoteMsid.stream] = new window.MediaStream(); } addTrackToStreamAndFireEvent(track, streams[remoteMsid.stream]); receiverList.push([track, rtpReceiver, streams[remoteMsid.stream]]); } else { if (!streams.default) { streams.default = new window.MediaStream(); } addTrackToStreamAndFireEvent(track, streams.default); receiverList.push([track, rtpReceiver, streams.default]); } } else { // FIXME: actually the receiver should be created later. delete transceiver.rtpReceiver; } } }); if (pc._dtlsRole === undefined) { pc._dtlsRole = description.type === 'offer' ? 'active' : 'passive'; } pc._remoteDescription = { type: description.type, sdp: description.sdp }; if (description.type === 'offer') { pc._updateSignalingState('have-remote-offer'); } else { pc._updateSignalingState('stable'); } Object.keys(streams).forEach(function(sid) { var stream = streams[sid]; if (stream.getTracks().length) { if (pc.remoteStreams.indexOf(stream) === -1) { pc.remoteStreams.push(stream); var event = new Event('addstream'); event.stream = stream; window.setTimeout(function() { pc._dispatchEvent('addstream', event); }); } receiverList.forEach(function(item) { var track = item[0]; var receiver = item[1]; if (stream.id !== item[2].id) { return; } fireAddTrack(pc, track, receiver, [stream]); }); } }); receiverList.forEach(function(item) { if (item[2]) { return; } fireAddTrack(pc, item[0], item[1], []); }); // check whether addIceCandidate({}) was called within four seconds after // setRemoteDescription. window.setTimeout(function() { if (!(pc && pc.transceivers)) { return; } pc.transceivers.forEach(function(transceiver) { if (transceiver.iceTransport && transceiver.iceTransport.state === 'new' && transceiver.iceTransport.getRemoteCandidates().length > 0) { console.warn('Timeout for addRemoteCandidate. Consider sending ' + 'an end-of-candidates notification'); transceiver.iceTransport.addRemoteCandidate({}); } }); }, 4000); return Promise.resolve(); }; RTCPeerConnection.prototype.close = function() { this.transceivers.forEach(function(transceiver) { /* not yet if (transceiver.iceGatherer) { transceiver.iceGatherer.close(); } */ if (transceiver.iceTransport) { transceiver.iceTransport.stop(); } if (transceiver.dtlsTransport) { transceiver.dtlsTransport.stop(); } if (transceiver.rtpSender) { transceiver.rtpSender.stop(); } if (transceiver.rtpReceiver) { transceiver.rtpReceiver.stop(); } }); // FIXME: clean up tracks, local streams, remote streams, etc this._isClosed = true; this._updateSignalingState('closed'); }; // Update the signaling state. RTCPeerConnection.prototype._updateSignalingState = function(newState) { this.signalingState = newState; var event = new Event('signalingstatechange'); this._dispatchEvent('signalingstatechange', event); }; // Determine whether to fire the negotiationneeded event. RTCPeerConnection.prototype._maybeFireNegotiationNeeded = function() { var pc = this; if (this.signalingState !== 'stable' || this.needNegotiation === true) { return; } this.needNegotiation = true; window.setTimeout(function() { if (pc.needNegotiation) { pc.needNegotiation = false; var event = new Event('negotiationneeded'); pc._dispatchEvent('negotiationneeded', event); } }, 0); }; // Update the ice connection state. RTCPeerConnection.prototype._updateIceConnectionState = function() { var newState; var states = { 'new': 0, closed: 0, checking: 0, connected: 0, completed: 0, disconnected: 0, failed: 0 }; this.transceivers.forEach(function(transceiver) { if (transceiver.iceTransport && !transceiver.rejected) { states[transceiver.iceTransport.state]++; } }); newState = 'new'; if (states.failed > 0) { newState = 'failed'; } else if (states.checking > 0) { newState = 'checking'; } else if (states.disconnected > 0) { newState = 'disconnected'; } else if (states.new > 0) { newState = 'new'; } else if (states.connected > 0) { newState = 'connected'; } else if (states.completed > 0) { newState = 'completed'; } if (newState !== this.iceConnectionState) { this.iceConnectionState = newState; var event = new Event('iceconnectionstatechange'); this._dispatchEvent('iceconnectionstatechange', event); } }; // Update the connection state. RTCPeerConnection.prototype._updateConnectionState = function() { var newState; var states = { 'new': 0, closed: 0, connecting: 0, connected: 0, completed: 0, disconnected: 0, failed: 0 }; this.transceivers.forEach(function(transceiver) { if (transceiver.iceTransport && transceiver.dtlsTransport && !transceiver.rejected) { states[transceiver.iceTransport.state]++; states[transceiver.dtlsTransport.state]++; } }); // ICETransport.completed and connected are the same for this purpose. states.connected += states.completed; newState = 'new'; if (states.failed > 0) { newState = 'failed'; } else if (states.connecting > 0) { newState = 'connecting'; } else if (states.disconnected > 0) { newState = 'disconnected'; } else if (states.new > 0) { newState = 'new'; } else if (states.connected > 0) { newState = 'connected'; } if (newState !== this.connectionState) { this.connectionState = newState; var event = new Event('connectionstatechange'); this._dispatchEvent('connectionstatechange', event); } }; RTCPeerConnection.prototype.createOffer = function() { var pc = this; if (pc._isClosed) { return Promise.reject(makeError('InvalidStateError', 'Can not call createOffer after close')); } var numAudioTracks = pc.transceivers.filter(function(t) { return t.kind === 'audio'; }).length; var numVideoTracks = pc.transceivers.filter(function(t) { return t.kind === 'video'; }).length; // Determine number of audio and video tracks we need to send/recv. var offerOptions = arguments[0]; if (offerOptions) { // Reject Chrome legacy constraints. if (offerOptions.mandatory || offerOptions.optional) { throw new TypeError( 'Legacy mandatory/optional constraints not supported.'); } if (offerOptions.offerToReceiveAudio !== undefined) { if (offerOptions.offerToReceiveAudio === true) { numAudioTracks = 1; } else if (offerOptions.offerToReceiveAudio === false) { numAudioTracks = 0; } else { numAudioTracks = offerOptions.offerToReceiveAudio; } } if (offerOptions.offerToReceiveVideo !== undefined) { if (offerOptions.offerToReceiveVideo === true) { numVideoTracks = 1; } else if (offerOptions.offerToReceiveVideo === false) { numVideoTracks = 0; } else { numVideoTracks = offerOptions.offerToReceiveVideo; } } } pc.transceivers.forEach(function(transceiver) { if (transceiver.kind === 'audio') { numAudioTracks--; if (numAudioTracks < 0) { transceiver.wantReceive = false; } } else if (transceiver.kind === 'video') { numVideoTracks--; if (numVideoTracks < 0) { transceiver.wantReceive = false; } } }); // Create M-lines for recvonly streams. while (numAudioTracks > 0 || numVideoTracks > 0) { if (numAudioTracks > 0) { pc._createTransceiver('audio'); numAudioTracks--; } if (numVideoTracks > 0) { pc._createTransceiver('video'); numVideoTracks--; } } var sdp = SDPUtils.writeSessionBoilerplate(pc._sdpSessionId, pc._sdpSessionVersion++); pc.transceivers.forEach(function(transceiver, sdpMLineIndex) { // For each track, create an ice gatherer, ice transport, // dtls transport, potentially rtpsender and rtpreceiver. var track = transceiver.track; var kind = transceiver.kind; var mid = transceiver.mid || SDPUtils.generateIdentifier(); transceiver.mid = mid; if (!transceiver.iceGatherer) { transceiver.iceGatherer = pc._createIceGatherer(sdpMLineIndex, pc.usingBundle); } var localCapabilities = window.RTCRtpSender.getCapabilities(kind); // filter RTX until additional stuff needed for RTX is implemented // in adapter.js if (edgeVersion < 15019) { localCapabilities.codecs = localCapabilities.codecs.filter( function(codec) { return codec.name !== 'rtx'; }); } localCapabilities.codecs.forEach(function(codec) { // work around https://bugs.chromium.org/p/webrtc/issues/detail?id=6552 // by adding level-asymmetry-allowed=1 if (codec.name === 'H264' && codec.parameters['level-asymmetry-allowed'] === undefined) { codec.parameters['level-asymmetry-allowed'] = '1'; } // for subsequent offers, we might have to re-use the payload // type of the last offer. if (transceiver.remoteCapabilities && transceiver.remoteCapabilities.codecs) { transceiver.remoteCapabilities.codecs.forEach(function(remoteCodec) { if (codec.name.toLowerCase() === remoteCodec.name.toLowerCase() && codec.clockRate === remoteCodec.clockRate) { codec.preferredPayloadType = remoteCodec.payloadType; } }); } }); localCapabilities.headerExtensions.forEach(function(hdrExt) { var remoteExtensions = transceiver.remoteCapabilities && transceiver.remoteCapabilities.headerExtensions || []; remoteExtensions.forEach(function(rHdrExt) { if (hdrExt.uri === rHdrExt.uri) { hdrExt.id = rHdrExt.id; } }); }); // generate an ssrc now, to be used later in rtpSender.send var sendEncodingParameters = transceiver.sendEncodingParameters || [{ ssrc: (2 * sdpMLineIndex + 1) * 1001 }]; if (track) { // add RTX if (edgeVersion >= 15019 && kind === 'video' && !sendEncodingParameters[0].rtx) { sendEncodingParameters[0].rtx = { ssrc: sendEncodingParameters[0].ssrc + 1 }; } } if (transceiver.wantReceive) { transceiver.rtpReceiver = new window.RTCRtpReceiver( transceiver.dtlsTransport, kind); } transceiver.localCapabilities = localCapabilities; transceiver.sendEncodingParameters = sendEncodingParameters; }); // always offer BUNDLE and dispose on return if not supported. if (pc._config.bundlePolicy !== 'max-compat') { sdp += 'a=group:BUNDLE ' + pc.transceivers.map(function(t) { return t.mid; }).join(' ') + '\r\n'; } sdp += 'a=ice-options:trickle\r\n'; pc.transceivers.forEach(function(transceiver, sdpMLineIndex) { sdp += writeMediaSection(transceiver, transceiver.localCapabilities, 'offer', transceiver.stream, pc._dtlsRole); sdp += 'a=rtcp-rsize\r\n'; if (transceiver.iceGatherer && pc.iceGatheringState !== 'new' && (sdpMLineIndex === 0 || !pc.usingBundle)) { transceiver.iceGatherer.getLocalCandidates().forEach(function(cand) { cand.component = 1; sdp += 'a=' + SDPUtils.writeCandidate(cand) + '\r\n'; }); if (transceiver.iceGatherer.state === 'completed') { sdp += 'a=end-of-candidates\r\n'; } } }); var desc = new window.RTCSessionDescription({ type: 'offer', sdp: sdp }); return Promise.resolve(desc); }; RTCPeerConnection.prototype.createAnswer = function() { var pc = this; if (pc._isClosed) { return Promise.reject(makeError('InvalidStateError', 'Can not call createAnswer after close')); } if (!(pc.signalingState === 'have-remote-offer' || pc.signalingState === 'have-local-pranswer')) { return Promise.reject(makeError('InvalidStateError', 'Can not call createAnswer in signalingState ' + pc.signalingState)); } var sdp = SDPUtils.writeSessionBoilerplate(pc._sdpSessionId, pc._sdpSessionVersion++); if (pc.usingBundle) { sdp += 'a=group:BUNDLE ' + pc.transceivers.map(function(t) { return t.mid; }).join(' ') + '\r\n'; } sdp += 'a=ice-options:trickle\r\n'; var mediaSectionsInOffer = SDPUtils.getMediaSections( pc._remoteDescription.sdp).length; pc.transceivers.forEach(function(transceiver, sdpMLineIndex) { if (sdpMLineIndex + 1 > mediaSectionsInOffer) { return; } if (transceiver.rejected) { if (transceiver.kind === 'application') { if (transceiver.protocol === 'DTLS/SCTP') { // legacy fmt sdp += 'm=application 0 DTLS/SCTP 5000\r\n'; } else { sdp += 'm=application 0 ' + transceiver.protocol + ' webrtc-datachannel\r\n'; } } else if (transceiver.kind === 'audio') { sdp += 'm=audio 0 UDP/TLS/RTP/SAVPF 0\r\n' + 'a=rtpmap:0 PCMU/8000\r\n'; } else if (transceiver.kind === 'video') { sdp += 'm=video 0 UDP/TLS/RTP/SAVPF 120\r\n' + 'a=rtpmap:120 VP8/90000\r\n'; } sdp += 'c=IN IP4 0.0.0.0\r\n' + 'a=inactive\r\n' + 'a=mid:' + transceiver.mid + '\r\n'; return; } // FIXME: look at direction. if (transceiver.stream) { var localTrack; if (transceiver.kind === 'audio') { localTrack = transceiver.stream.getAudioTracks()[0]; } else if (transceiver.kind === 'video') { localTrack = transceiver.stream.getVideoTracks()[0]; } if (localTrack) { // add RTX if (edgeVersion >= 15019 && transceiver.kind === 'video' && !transceiver.sendEncodingParameters[0].rtx) { transceiver.sendEncodingParameters[0].rtx = { ssrc: transceiver.sendEncodingParameters[0].ssrc + 1 }; } } } // Calculate intersection of capabilities. var commonCapabilities = getCommonCapabilities( transceiver.localCapabilities, transceiver.remoteCapabilities); var hasRtx = commonCapabilities.codecs.filter(function(c) { return c.name.toLowerCase() === 'rtx'; }).length; if (!hasRtx && transceiver.sendEncodingParameters[0].rtx) { delete transceiver.sendEncodingParameters[0].rtx; } sdp += writeMediaSection(transceiver, commonCapabilities, 'answer', transceiver.stream, pc._dtlsRole); if (transceiver.rtcpParameters && transceiver.rtcpParameters.reducedSize) { sdp += 'a=rtcp-rsize\r\n'; } }); var desc = new window.RTCSessionDescription({ type: 'answer', sdp: sdp }); return Promise.resolve(desc); }; RTCPeerConnection.prototype.addIceCandidate = function(candidate) { var pc = this; var sections; if (candidate && !(candidate.sdpMLineIndex !== undefined || candidate.sdpMid)) { return Promise.reject(new TypeError('sdpMLineIndex or sdpMid required')); } // TODO: needs to go into ops queue. return new Promise(function(resolve, reject) { if (!pc._remoteDescription) { return reject(makeError('InvalidStateError', 'Can not add ICE candidate without a remote description')); } else if (!candidate || candidate.candidate === '') { for (var j = 0; j < pc.transceivers.length; j++) { if (pc.transceivers[j].rejected) { continue; } pc.transceivers[j].iceTransport.addRemoteCandidate({}); sections = SDPUtils.getMediaSections(pc._remoteDescription.sdp); sections[j] += 'a=end-of-candidates\r\n'; pc._remoteDescription.sdp = SDPUtils.getDescription(pc._remoteDescription.sdp) + sections.join(''); if (pc.usingBundle) { break; } } } else { var sdpMLineIndex = candidate.sdpMLineIndex; if (candidate.sdpMid) { for (var i = 0; i < pc.transceivers.length; i++) { if (pc.transceivers[i].mid === candidate.sdpMid) { sdpMLineIndex = i; break; } } } var transceiver = pc.transceivers[sdpMLineIndex]; if (transceiver) { if (transceiver.rejected) { return resolve(); } var cand = Object.keys(candidate.candidate).length > 0 ? SDPUtils.parseCandidate(candidate.candidate) : {}; // Ignore Chrome's invalid candidates since Edge does not like them. if (cand.protocol === 'tcp' && (cand.port === 0 || cand.port === 9)) { return resolve(); } // Ignore RTCP candidates, we assume RTCP-MUX. if (cand.component && cand.component !== 1) { return resolve(); } // when using bundle, avoid adding candidates to the wrong // ice transport. And avoid adding candidates added in the SDP. if (sdpMLineIndex === 0 || (sdpMLineIndex > 0 && transceiver.iceTransport !== pc.transceivers[0].iceTransport)) { if (!maybeAddCandidate(transceiver.iceTransport, cand)) { return reject(makeError('OperationError', 'Can not add ICE candidate')); } } // update the remoteDescription. var candidateString = candidate.candidate.trim(); if (candidateString.indexOf('a=') === 0) { candidateString = candidateString.substr(2); } sections = SDPUtils.getMediaSections(pc._remoteDescription.sdp); sections[sdpMLineIndex] += 'a=' + (cand.type ? candidateString : 'end-of-candidates') + '\r\n'; pc._remoteDescription.sdp = SDPUtils.getDescription(pc._remoteDescription.sdp) + sections.join(''); } else { return reject(makeError('OperationError', 'Can not add ICE candidate')); } } resolve(); }); }; RTCPeerConnection.prototype.getStats = function(selector) { if (selector && selector instanceof window.MediaStreamTrack) { var senderOrReceiver = null; this.transceivers.forEach(function(transceiver) { if (transceiver.rtpSender && transceiver.rtpSender.track === selector) { senderOrReceiver = transceiver.rtpSender; } else if (transceiver.rtpReceiver && transceiver.rtpReceiver.track === selector) { senderOrReceiver = transceiver.rtpReceiver; } }); if (!senderOrReceiver) { throw makeError('InvalidAccessError', 'Invalid selector.'); } return senderOrReceiver.getStats(); } var promises = []; this.transceivers.forEach(function(transceiver) { ['rtpSender', 'rtpReceiver', 'iceGatherer', 'iceTransport', 'dtlsTransport'].forEach(function(method) { if (transceiver[method]) { promises.push(transceiver[method].getStats()); } }); }); return Promise.all(promises).then(function(allStats) { var results = new Map(); allStats.forEach(function(stats) { stats.forEach(function(stat) { results.set(stat.id, stat); }); }); return results; }); }; // fix low-level stat names and return Map instead of object. var ortcObjects = ['RTCRtpSender', 'RTCRtpReceiver', 'RTCIceGatherer', 'RTCIceTransport', 'RTCDtlsTransport']; ortcObjects.forEach(function(ortcObjectName) { var obj = window[ortcObjectName]; if (obj && obj.prototype && obj.prototype.getStats) { var nativeGetstats = obj.prototype.getStats; obj.prototype.getStats = function() { return nativeGetstats.apply(this) .then(function(nativeStats) { var mapStats = new Map(); Object.keys(nativeStats).forEach(function(id) { nativeStats[id].type = fixStatsType(nativeStats[id]); mapStats.set(id, nativeStats[id]); }); return mapStats; }); }; } }); // legacy callback shims. Should be moved to adapter.js some days. var methods = ['createOffer', 'createAnswer']; methods.forEach(function(method) { var nativeMethod = RTCPeerConnection.prototype[method]; RTCPeerConnection.prototype[method] = function() { var args = arguments; if (typeof args[0] === 'function' || typeof args[1] === 'function') { // legacy return nativeMethod.apply(this, [arguments[2]]) .then(function(description) { if (typeof args[0] === 'function') { args[0].apply(null, [description]); } }, function(error) { if (typeof args[1] === 'function') { args[1].apply(null, [error]); } }); } return nativeMethod.apply(this, arguments); }; }); methods = ['setLocalDescription', 'setRemoteDescription', 'addIceCandidate']; methods.forEach(function(method) { var nativeMethod = RTCPeerConnection.prototype[method]; RTCPeerConnection.prototype[method] = function() { var args = arguments; if (typeof args[1] === 'function' || typeof args[2] === 'function') { // legacy return nativeMethod.apply(this, arguments) .then(function() { if (typeof args[1] === 'function') { args[1].apply(null); } }, function(error) { if (typeof args[2] === 'function') { args[2].apply(null, [error]); } }); } return nativeMethod.apply(this, arguments); }; }); // getStats is special. It doesn't have a spec legacy method yet we support // getStats(something, cb) without error callbacks. ['getStats'].forEach(function(method) { var nativeMethod = RTCPeerConnection.prototype[method]; RTCPeerConnection.prototype[method] = function() { var args = arguments; if (typeof args[1] === 'function') { return nativeMethod.apply(this, arguments) .then(function() { if (typeof args[1] === 'function') { args[1].apply(null); } }); } return nativeMethod.apply(this, arguments); }; }); return RTCPeerConnection; }; },{"sdp":175}],173:[function(require,module,exports){ /*! safe-buffer. MIT License. Feross Aboukhadijeh */ /* eslint-disable node/no-deprecated-api */ var buffer = require('buffer') var Buffer = buffer.Buffer // alternative to using Object.keys for old browsers function copyProps (src, dst) { for (var key in src) { dst[key] = src[key] } } if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) { module.exports = buffer } else { // Copy properties from require('buffer') copyProps(buffer, exports) exports.Buffer = SafeBuffer } function SafeBuffer (arg, encodingOrOffset, length) { return Buffer(arg, encodingOrOffset, length) } SafeBuffer.prototype = Object.create(Buffer.prototype) // Copy static methods from Buffer copyProps(Buffer, SafeBuffer) SafeBuffer.from = function (arg, encodingOrOffset, length) { if (typeof arg === 'number') { throw new TypeError('Argument must not be a number') } return Buffer(arg, encodingOrOffset, length) } SafeBuffer.alloc = function (size, fill, encoding) { if (typeof size !== 'number') { throw new TypeError('Argument must be a number') } var buf = Buffer(size) if (fill !== undefined) { if (typeof encoding === 'string') { buf.fill(fill, encoding) } else { buf.fill(fill) } } else { buf.fill(0) } return buf } SafeBuffer.allocUnsafe = function (size) { if (typeof size !== 'number') { throw new TypeError('Argument must be a number') } return Buffer(size) } SafeBuffer.allocUnsafeSlow = function (size) { if (typeof size !== 'number') { throw new TypeError('Argument must be a number') } return buffer.SlowBuffer(size) } },{"buffer":47}],174:[function(require,module,exports){ (function (process){(function (){ /* eslint-disable node/no-deprecated-api */ 'use strict' var buffer = require('buffer') var Buffer = buffer.Buffer var safer = {} var key for (key in buffer) { if (!buffer.hasOwnProperty(key)) continue if (key === 'SlowBuffer' || key === 'Buffer') continue safer[key] = buffer[key] } var Safer = safer.Buffer = {} for (key in Buffer) { if (!Buffer.hasOwnProperty(key)) continue if (key === 'allocUnsafe' || key === 'allocUnsafeSlow') continue Safer[key] = Buffer[key] } safer.Buffer.prototype = Buffer.prototype if (!Safer.from || Safer.from === Uint8Array.from) { Safer.from = function (value, encodingOrOffset, length) { if (typeof value === 'number') { throw new TypeError('The "value" argument must not be of type number. Received type ' + typeof value) } if (value && typeof value.length === 'undefined') { throw new TypeError('The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type ' + typeof value) } return Buffer(value, encodingOrOffset, length) } } if (!Safer.alloc) { Safer.alloc = function (size, fill, encoding) { if (typeof size !== 'number') { throw new TypeError('The "size" argument must be of type number. Received type ' + typeof size) } if (size < 0 || size >= 2 * (1 << 30)) { throw new RangeError('The value "' + size + '" is invalid for option "size"') } var buf = Buffer(size) if (!fill || fill.length === 0) { buf.fill(0) } else if (typeof encoding === 'string') { buf.fill(fill, encoding) } else { buf.fill(fill) } return buf } } if (!safer.kStringMaxLength) { try { safer.kStringMaxLength = process.binding('buffer').kStringMaxLength } catch (e) { // we can't determine kStringMaxLength in environments where process.binding // is unsupported, so let's not set it } } if (!safer.constants) { safer.constants = { MAX_LENGTH: safer.kMaxLength } if (safer.kStringMaxLength) { safer.constants.MAX_STRING_LENGTH = safer.kStringMaxLength } } module.exports = safer }).call(this)}).call(this,require('_process')) },{"_process":152,"buffer":47}],175:[function(require,module,exports){ /* eslint-env node */ 'use strict'; // SDP helpers. var SDPUtils = {}; // Generate an alphanumeric identifier for cname or mids. // TODO: use UUIDs instead? https://gist.github.com/jed/982883 SDPUtils.generateIdentifier = function() { return Math.random().toString(36).substr(2, 10); }; // The RTCP CNAME used by all peerconnections from the same JS. SDPUtils.localCName = SDPUtils.generateIdentifier(); // Splits SDP into lines, dealing with both CRLF and LF. SDPUtils.splitLines = function(blob) { return blob.trim().split('\n').map(function(line) { return line.trim(); }); }; // Splits SDP into sessionpart and mediasections. Ensures CRLF. SDPUtils.splitSections = function(blob) { var parts = blob.split('\nm='); return parts.map(function(part, index) { return (index > 0 ? 'm=' + part : part).trim() + '\r\n'; }); }; // returns the session description. SDPUtils.getDescription = function(blob) { var sections = SDPUtils.splitSections(blob); return sections && sections[0]; }; // returns the individual media sections. SDPUtils.getMediaSections = function(blob) { var sections = SDPUtils.splitSections(blob); sections.shift(); return sections; }; // Returns lines that start with a certain prefix. SDPUtils.matchPrefix = function(blob, prefix) { return SDPUtils.splitLines(blob).filter(function(line) { return line.indexOf(prefix) === 0; }); }; // Parses an ICE candidate line. Sample input: // candidate:702786350 2 udp 41819902 8.8.8.8 60769 typ relay raddr 8.8.8.8 // rport 55996" SDPUtils.parseCandidate = function(line) { var parts; // Parse both variants. if (line.indexOf('a=candidate:') === 0) { parts = line.substring(12).split(' '); } else { parts = line.substring(10).split(' '); } var candidate = { foundation: parts[0], component: parseInt(parts[1], 10), protocol: parts[2].toLowerCase(), priority: parseInt(parts[3], 10), ip: parts[4], address: parts[4], // address is an alias for ip. port: parseInt(parts[5], 10), // skip parts[6] == 'typ' type: parts[7] }; for (var i = 8; i < parts.length; i += 2) { switch (parts[i]) { case 'raddr': candidate.relatedAddress = parts[i + 1]; break; case 'rport': candidate.relatedPort = parseInt(parts[i + 1], 10); break; case 'tcptype': candidate.tcpType = parts[i + 1]; break; case 'ufrag': candidate.ufrag = parts[i + 1]; // for backward compability. candidate.usernameFragment = parts[i + 1]; break; default: // extension handling, in particular ufrag candidate[parts[i]] = parts[i + 1]; break; } } return candidate; }; // Translates a candidate object into SDP candidate attribute. SDPUtils.writeCandidate = function(candidate) { var sdp = []; sdp.push(candidate.foundation); sdp.push(candidate.component); sdp.push(candidate.protocol.toUpperCase()); sdp.push(candidate.priority); sdp.push(candidate.address || candidate.ip); sdp.push(candidate.port); var type = candidate.type; sdp.push('typ'); sdp.push(type); if (type !== 'host' && candidate.relatedAddress && candidate.relatedPort) { sdp.push('raddr'); sdp.push(candidate.relatedAddress); sdp.push('rport'); sdp.push(candidate.relatedPort); } if (candidate.tcpType && candidate.protocol.toLowerCase() === 'tcp') { sdp.push('tcptype'); sdp.push(candidate.tcpType); } if (candidate.usernameFragment || candidate.ufrag) { sdp.push('ufrag'); sdp.push(candidate.usernameFragment || candidate.ufrag); } return 'candidate:' + sdp.join(' '); }; // Parses an ice-options line, returns an array of option tags. // a=ice-options:foo bar SDPUtils.parseIceOptions = function(line) { return line.substr(14).split(' '); }; // Parses an rtpmap line, returns RTCRtpCoddecParameters. Sample input: // a=rtpmap:111 opus/48000/2 SDPUtils.parseRtpMap = function(line) { var parts = line.substr(9).split(' '); var parsed = { payloadType: parseInt(parts.shift(), 10) // was: id }; parts = parts[0].split('/'); parsed.name = parts[0]; parsed.clockRate = parseInt(parts[1], 10); // was: clockrate parsed.channels = parts.length === 3 ? parseInt(parts[2], 10) : 1; // legacy alias, got renamed back to channels in ORTC. parsed.numChannels = parsed.channels; return parsed; }; // Generate an a=rtpmap line from RTCRtpCodecCapability or // RTCRtpCodecParameters. SDPUtils.writeRtpMap = function(codec) { var pt = codec.payloadType; if (codec.preferredPayloadType !== undefined) { pt = codec.preferredPayloadType; } var channels = codec.channels || codec.numChannels || 1; return 'a=rtpmap:' + pt + ' ' + codec.name + '/' + codec.clockRate + (channels !== 1 ? '/' + channels : '') + '\r\n'; }; // Parses an a=extmap line (headerextension from RFC 5285). Sample input: // a=extmap:2 urn:ietf:params:rtp-hdrext:toffset // a=extmap:2/sendonly urn:ietf:params:rtp-hdrext:toffset SDPUtils.parseExtmap = function(line) { var parts = line.substr(9).split(' '); return { id: parseInt(parts[0], 10), direction: parts[0].indexOf('/') > 0 ? parts[0].split('/')[1] : 'sendrecv', uri: parts[1] }; }; // Generates a=extmap line from RTCRtpHeaderExtensionParameters or // RTCRtpHeaderExtension. SDPUtils.writeExtmap = function(headerExtension) { return 'a=extmap:' + (headerExtension.id || headerExtension.preferredId) + (headerExtension.direction && headerExtension.direction !== 'sendrecv' ? '/' + headerExtension.direction : '') + ' ' + headerExtension.uri + '\r\n'; }; // Parses an ftmp line, returns dictionary. Sample input: // a=fmtp:96 vbr=on;cng=on // Also deals with vbr=on; cng=on SDPUtils.parseFmtp = function(line) { var parsed = {}; var kv; var parts = line.substr(line.indexOf(' ') + 1).split(';'); for (var j = 0; j < parts.length; j++) { kv = parts[j].trim().split('='); parsed[kv[0].trim()] = kv[1]; } return parsed; }; // Generates an a=ftmp line from RTCRtpCodecCapability or RTCRtpCodecParameters. SDPUtils.writeFmtp = function(codec) { var line = ''; var pt = codec.payloadType; if (codec.preferredPayloadType !== undefined) { pt = codec.preferredPayloadType; } if (codec.parameters && Object.keys(codec.parameters).length) { var params = []; Object.keys(codec.parameters).forEach(function(param) { if (codec.parameters[param]) { params.push(param + '=' + codec.parameters[param]); } else { params.push(param); } }); line += 'a=fmtp:' + pt + ' ' + params.join(';') + '\r\n'; } return line; }; // Parses an rtcp-fb line, returns RTCPRtcpFeedback object. Sample input: // a=rtcp-fb:98 nack rpsi SDPUtils.parseRtcpFb = function(line) { var parts = line.substr(line.indexOf(' ') + 1).split(' '); return { type: parts.shift(), parameter: parts.join(' ') }; }; // Generate a=rtcp-fb lines from RTCRtpCodecCapability or RTCRtpCodecParameters. SDPUtils.writeRtcpFb = function(codec) { var lines = ''; var pt = codec.payloadType; if (codec.preferredPayloadType !== undefined) { pt = codec.preferredPayloadType; } if (codec.rtcpFeedback && codec.rtcpFeedback.length) { // FIXME: special handling for trr-int? codec.rtcpFeedback.forEach(function(fb) { lines += 'a=rtcp-fb:' + pt + ' ' + fb.type + (fb.parameter && fb.parameter.length ? ' ' + fb.parameter : '') + '\r\n'; }); } return lines; }; // Parses an RFC 5576 ssrc media attribute. Sample input: // a=ssrc:3735928559 cname:something SDPUtils.parseSsrcMedia = function(line) { var sp = line.indexOf(' '); var parts = { ssrc: parseInt(line.substr(7, sp - 7), 10) }; var colon = line.indexOf(':', sp); if (colon > -1) { parts.attribute = line.substr(sp + 1, colon - sp - 1); parts.value = line.substr(colon + 1); } else { parts.attribute = line.substr(sp + 1); } return parts; }; SDPUtils.parseSsrcGroup = function(line) { var parts = line.substr(13).split(' '); return { semantics: parts.shift(), ssrcs: parts.map(function(ssrc) { return parseInt(ssrc, 10); }) }; }; // Extracts the MID (RFC 5888) from a media section. // returns the MID or undefined if no mid line was found. SDPUtils.getMid = function(mediaSection) { var mid = SDPUtils.matchPrefix(mediaSection, 'a=mid:')[0]; if (mid) { return mid.substr(6); } }; SDPUtils.parseFingerprint = function(line) { var parts = line.substr(14).split(' '); return { algorithm: parts[0].toLowerCase(), // algorithm is case-sensitive in Edge. value: parts[1] }; }; // Extracts DTLS parameters from SDP media section or sessionpart. // FIXME: for consistency with other functions this should only // get the fingerprint line as input. See also getIceParameters. SDPUtils.getDtlsParameters = function(mediaSection, sessionpart) { var lines = SDPUtils.matchPrefix(mediaSection + sessionpart, 'a=fingerprint:'); // Note: a=setup line is ignored since we use the 'auto' role. // Note2: 'algorithm' is not case sensitive except in Edge. return { role: 'auto', fingerprints: lines.map(SDPUtils.parseFingerprint) }; }; // Serializes DTLS parameters to SDP. SDPUtils.writeDtlsParameters = function(params, setupType) { var sdp = 'a=setup:' + setupType + '\r\n'; params.fingerprints.forEach(function(fp) { sdp += 'a=fingerprint:' + fp.algorithm + ' ' + fp.value + '\r\n'; }); return sdp; }; // Parses a=crypto lines into // https://rawgit.com/aboba/edgertc/master/msortc-rs4.html#dictionary-rtcsrtpsdesparameters-members SDPUtils.parseCryptoLine = function(line) { var parts = line.substr(9).split(' '); return { tag: parseInt(parts[0], 10), cryptoSuite: parts[1], keyParams: parts[2], sessionParams: parts.slice(3), }; }; SDPUtils.writeCryptoLine = function(parameters) { return 'a=crypto:' + parameters.tag + ' ' + parameters.cryptoSuite + ' ' + (typeof parameters.keyParams === 'object' ? SDPUtils.writeCryptoKeyParams(parameters.keyParams) : parameters.keyParams) + (parameters.sessionParams ? ' ' + parameters.sessionParams.join(' ') : '') + '\r\n'; }; // Parses the crypto key parameters into // https://rawgit.com/aboba/edgertc/master/msortc-rs4.html#rtcsrtpkeyparam* SDPUtils.parseCryptoKeyParams = function(keyParams) { if (keyParams.indexOf('inline:') !== 0) { return null; } var parts = keyParams.substr(7).split('|'); return { keyMethod: 'inline', keySalt: parts[0], lifeTime: parts[1], mkiValue: parts[2] ? parts[2].split(':')[0] : undefined, mkiLength: parts[2] ? parts[2].split(':')[1] : undefined, }; }; SDPUtils.writeCryptoKeyParams = function(keyParams) { return keyParams.keyMethod + ':' + keyParams.keySalt + (keyParams.lifeTime ? '|' + keyParams.lifeTime : '') + (keyParams.mkiValue && keyParams.mkiLength ? '|' + keyParams.mkiValue + ':' + keyParams.mkiLength : ''); }; // Extracts all SDES paramters. SDPUtils.getCryptoParameters = function(mediaSection, sessionpart) { var lines = SDPUtils.matchPrefix(mediaSection + sessionpart, 'a=crypto:'); return lines.map(SDPUtils.parseCryptoLine); }; // Parses ICE information from SDP media section or sessionpart. // FIXME: for consistency with other functions this should only // get the ice-ufrag and ice-pwd lines as input. SDPUtils.getIceParameters = function(mediaSection, sessionpart) { var ufrag = SDPUtils.matchPrefix(mediaSection + sessionpart, 'a=ice-ufrag:')[0]; var pwd = SDPUtils.matchPrefix(mediaSection + sessionpart, 'a=ice-pwd:')[0]; if (!(ufrag && pwd)) { return null; } return { usernameFragment: ufrag.substr(12), password: pwd.substr(10), }; }; // Serializes ICE parameters to SDP. SDPUtils.writeIceParameters = function(params) { return 'a=ice-ufrag:' + params.usernameFragment + '\r\n' + 'a=ice-pwd:' + params.password + '\r\n'; }; // Parses the SDP media section and returns RTCRtpParameters. SDPUtils.parseRtpParameters = function(mediaSection) { var description = { codecs: [], headerExtensions: [], fecMechanisms: [], rtcp: [] }; var lines = SDPUtils.splitLines(mediaSection); var mline = lines[0].split(' '); for (var i = 3; i < mline.length; i++) { // find all codecs from mline[3..] var pt = mline[i]; var rtpmapline = SDPUtils.matchPrefix( mediaSection, 'a=rtpmap:' + pt + ' ')[0]; if (rtpmapline) { var codec = SDPUtils.parseRtpMap(rtpmapline); var fmtps = SDPUtils.matchPrefix( mediaSection, 'a=fmtp:' + pt + ' '); // Only the first a=fmtp: is considered. codec.parameters = fmtps.length ? SDPUtils.parseFmtp(fmtps[0]) : {}; codec.rtcpFeedback = SDPUtils.matchPrefix( mediaSection, 'a=rtcp-fb:' + pt + ' ') .map(SDPUtils.parseRtcpFb); description.codecs.push(codec); // parse FEC mechanisms from rtpmap lines. switch (codec.name.toUpperCase()) { case 'RED': case 'ULPFEC': description.fecMechanisms.push(codec.name.toUpperCase()); break; default: // only RED and ULPFEC are recognized as FEC mechanisms. break; } } } SDPUtils.matchPrefix(mediaSection, 'a=extmap:').forEach(function(line) { description.headerExtensions.push(SDPUtils.parseExtmap(line)); }); // FIXME: parse rtcp. return description; }; // Generates parts of the SDP media section describing the capabilities / // parameters. SDPUtils.writeRtpDescription = function(kind, caps) { var sdp = ''; // Build the mline. sdp += 'm=' + kind + ' '; sdp += caps.codecs.length > 0 ? '9' : '0'; // reject if no codecs. sdp += ' UDP/TLS/RTP/SAVPF '; sdp += caps.codecs.map(function(codec) { if (codec.preferredPayloadType !== undefined) { return codec.preferredPayloadType; } return codec.payloadType; }).join(' ') + '\r\n'; sdp += 'c=IN IP4 0.0.0.0\r\n'; sdp += 'a=rtcp:9 IN IP4 0.0.0.0\r\n'; // Add a=rtpmap lines for each codec. Also fmtp and rtcp-fb. caps.codecs.forEach(function(codec) { sdp += SDPUtils.writeRtpMap(codec); sdp += SDPUtils.writeFmtp(codec); sdp += SDPUtils.writeRtcpFb(codec); }); var maxptime = 0; caps.codecs.forEach(function(codec) { if (codec.maxptime > maxptime) { maxptime = codec.maxptime; } }); if (maxptime > 0) { sdp += 'a=maxptime:' + maxptime + '\r\n'; } sdp += 'a=rtcp-mux\r\n'; if (caps.headerExtensions) { caps.headerExtensions.forEach(function(extension) { sdp += SDPUtils.writeExtmap(extension); }); } // FIXME: write fecMechanisms. return sdp; }; // Parses the SDP media section and returns an array of // RTCRtpEncodingParameters. SDPUtils.parseRtpEncodingParameters = function(mediaSection) { var encodingParameters = []; var description = SDPUtils.parseRtpParameters(mediaSection); var hasRed = description.fecMechanisms.indexOf('RED') !== -1; var hasUlpfec = description.fecMechanisms.indexOf('ULPFEC') !== -1; // filter a=ssrc:... cname:, ignore PlanB-msid var ssrcs = SDPUtils.matchPrefix(mediaSection, 'a=ssrc:') .map(function(line) { return SDPUtils.parseSsrcMedia(line); }) .filter(function(parts) { return parts.attribute === 'cname'; }); var primarySsrc = ssrcs.length > 0 && ssrcs[0].ssrc; var secondarySsrc; var flows = SDPUtils.matchPrefix(mediaSection, 'a=ssrc-group:FID') .map(function(line) { var parts = line.substr(17).split(' '); return parts.map(function(part) { return parseInt(part, 10); }); }); if (flows.length > 0 && flows[0].length > 1 && flows[0][0] === primarySsrc) { secondarySsrc = flows[0][1]; } description.codecs.forEach(function(codec) { if (codec.name.toUpperCase() === 'RTX' && codec.parameters.apt) { var encParam = { ssrc: primarySsrc, codecPayloadType: parseInt(codec.parameters.apt, 10) }; if (primarySsrc && secondarySsrc) { encParam.rtx = {ssrc: secondarySsrc}; } encodingParameters.push(encParam); if (hasRed) { encParam = JSON.parse(JSON.stringify(encParam)); encParam.fec = { ssrc: primarySsrc, mechanism: hasUlpfec ? 'red+ulpfec' : 'red' }; encodingParameters.push(encParam); } } }); if (encodingParameters.length === 0 && primarySsrc) { encodingParameters.push({ ssrc: primarySsrc }); } // we support both b=AS and b=TIAS but interpret AS as TIAS. var bandwidth = SDPUtils.matchPrefix(mediaSection, 'b='); if (bandwidth.length) { if (bandwidth[0].indexOf('b=TIAS:') === 0) { bandwidth = parseInt(bandwidth[0].substr(7), 10); } else if (bandwidth[0].indexOf('b=AS:') === 0) { // use formula from JSEP to convert b=AS to TIAS value. bandwidth = parseInt(bandwidth[0].substr(5), 10) * 1000 * 0.95 - (50 * 40 * 8); } else { bandwidth = undefined; } encodingParameters.forEach(function(params) { params.maxBitrate = bandwidth; }); } return encodingParameters; }; // parses http://draft.ortc.org/#rtcrtcpparameters* SDPUtils.parseRtcpParameters = function(mediaSection) { var rtcpParameters = {}; // Gets the first SSRC. Note tha with RTX there might be multiple // SSRCs. var remoteSsrc = SDPUtils.matchPrefix(mediaSection, 'a=ssrc:') .map(function(line) { return SDPUtils.parseSsrcMedia(line); }) .filter(function(obj) { return obj.attribute === 'cname'; })[0]; if (remoteSsrc) { rtcpParameters.cname = remoteSsrc.value; rtcpParameters.ssrc = remoteSsrc.ssrc; } // Edge uses the compound attribute instead of reducedSize // compound is !reducedSize var rsize = SDPUtils.matchPrefix(mediaSection, 'a=rtcp-rsize'); rtcpParameters.reducedSize = rsize.length > 0; rtcpParameters.compound = rsize.length === 0; // parses the rtcp-mux attrіbute. // Note that Edge does not support unmuxed RTCP. var mux = SDPUtils.matchPrefix(mediaSection, 'a=rtcp-mux'); rtcpParameters.mux = mux.length > 0; return rtcpParameters; }; // parses either a=msid: or a=ssrc:... msid lines and returns // the id of the MediaStream and MediaStreamTrack. SDPUtils.parseMsid = function(mediaSection) { var parts; var spec = SDPUtils.matchPrefix(mediaSection, 'a=msid:'); if (spec.length === 1) { parts = spec[0].substr(7).split(' '); return {stream: parts[0], track: parts[1]}; } var planB = SDPUtils.matchPrefix(mediaSection, 'a=ssrc:') .map(function(line) { return SDPUtils.parseSsrcMedia(line); }) .filter(function(msidParts) { return msidParts.attribute === 'msid'; }); if (planB.length > 0) { parts = planB[0].value.split(' '); return {stream: parts[0], track: parts[1]}; } }; // SCTP // parses draft-ietf-mmusic-sctp-sdp-26 first and falls back // to draft-ietf-mmusic-sctp-sdp-05 SDPUtils.parseSctpDescription = function(mediaSection) { var mline = SDPUtils.parseMLine(mediaSection); var maxSizeLine = SDPUtils.matchPrefix(mediaSection, 'a=max-message-size:'); var maxMessageSize; if (maxSizeLine.length > 0) { maxMessageSize = parseInt(maxSizeLine[0].substr(19), 10); } if (isNaN(maxMessageSize)) { maxMessageSize = 65536; } var sctpPort = SDPUtils.matchPrefix(mediaSection, 'a=sctp-port:'); if (sctpPort.length > 0) { return { port: parseInt(sctpPort[0].substr(12), 10), protocol: mline.fmt, maxMessageSize: maxMessageSize }; } var sctpMapLines = SDPUtils.matchPrefix(mediaSection, 'a=sctpmap:'); if (sctpMapLines.length > 0) { var parts = SDPUtils.matchPrefix(mediaSection, 'a=sctpmap:')[0] .substr(10) .split(' '); return { port: parseInt(parts[0], 10), protocol: parts[1], maxMessageSize: maxMessageSize }; } }; // SCTP // outputs the draft-ietf-mmusic-sctp-sdp-26 version that all browsers // support by now receiving in this format, unless we originally parsed // as the draft-ietf-mmusic-sctp-sdp-05 format (indicated by the m-line // protocol of DTLS/SCTP -- without UDP/ or TCP/) SDPUtils.writeSctpDescription = function(media, sctp) { var output = []; if (media.protocol !== 'DTLS/SCTP') { output = [ 'm=' + media.kind + ' 9 ' + media.protocol + ' ' + sctp.protocol + '\r\n', 'c=IN IP4 0.0.0.0\r\n', 'a=sctp-port:' + sctp.port + '\r\n' ]; } else { output = [ 'm=' + media.kind + ' 9 ' + media.protocol + ' ' + sctp.port + '\r\n', 'c=IN IP4 0.0.0.0\r\n', 'a=sctpmap:' + sctp.port + ' ' + sctp.protocol + ' 65535\r\n' ]; } if (sctp.maxMessageSize !== undefined) { output.push('a=max-message-size:' + sctp.maxMessageSize + '\r\n'); } return output.join(''); }; // Generate a session ID for SDP. // https://tools.ietf.org/html/draft-ietf-rtcweb-jsep-20#section-5.2.1 // recommends using a cryptographically random +ve 64-bit value // but right now this should be acceptable and within the right range SDPUtils.generateSessionId = function() { return Math.random().toString().substr(2, 21); }; // Write boilder plate for start of SDP // sessId argument is optional - if not supplied it will // be generated randomly // sessVersion is optional and defaults to 2 // sessUser is optional and defaults to 'thisisadapterortc' SDPUtils.writeSessionBoilerplate = function(sessId, sessVer, sessUser) { var sessionId; var version = sessVer !== undefined ? sessVer : 2; if (sessId) { sessionId = sessId; } else { sessionId = SDPUtils.generateSessionId(); } var user = sessUser || 'thisisadapterortc'; // FIXME: sess-id should be an NTP timestamp. return 'v=0\r\n' + 'o=' + user + ' ' + sessionId + ' ' + version + ' IN IP4 127.0.0.1\r\n' + 's=-\r\n' + 't=0 0\r\n'; }; SDPUtils.writeMediaSection = function(transceiver, caps, type, stream) { var sdp = SDPUtils.writeRtpDescription(transceiver.kind, caps); // Map ICE parameters (ufrag, pwd) to SDP. sdp += SDPUtils.writeIceParameters( transceiver.iceGatherer.getLocalParameters()); // Map DTLS parameters to SDP. sdp += SDPUtils.writeDtlsParameters( transceiver.dtlsTransport.getLocalParameters(), type === 'offer' ? 'actpass' : 'active'); sdp += 'a=mid:' + transceiver.mid + '\r\n'; if (transceiver.direction) { sdp += 'a=' + transceiver.direction + '\r\n'; } else if (transceiver.rtpSender && transceiver.rtpReceiver) { sdp += 'a=sendrecv\r\n'; } else if (transceiver.rtpSender) { sdp += 'a=sendonly\r\n'; } else if (transceiver.rtpReceiver) { sdp += 'a=recvonly\r\n'; } else { sdp += 'a=inactive\r\n'; } if (transceiver.rtpSender) { // spec. var msid = 'msid:' + stream.id + ' ' + transceiver.rtpSender.track.id + '\r\n'; sdp += 'a=' + msid; // for Chrome. sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].ssrc + ' ' + msid; if (transceiver.sendEncodingParameters[0].rtx) { sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].rtx.ssrc + ' ' + msid; sdp += 'a=ssrc-group:FID ' + transceiver.sendEncodingParameters[0].ssrc + ' ' + transceiver.sendEncodingParameters[0].rtx.ssrc + '\r\n'; } } // FIXME: this should be written by writeRtpDescription. sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].ssrc + ' cname:' + SDPUtils.localCName + '\r\n'; if (transceiver.rtpSender && transceiver.sendEncodingParameters[0].rtx) { sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].rtx.ssrc + ' cname:' + SDPUtils.localCName + '\r\n'; } return sdp; }; // Gets the direction from the mediaSection or the sessionpart. SDPUtils.getDirection = function(mediaSection, sessionpart) { // Look for sendrecv, sendonly, recvonly, inactive, default to sendrecv. var lines = SDPUtils.splitLines(mediaSection); for (var i = 0; i < lines.length; i++) { switch (lines[i]) { case 'a=sendrecv': case 'a=sendonly': case 'a=recvonly': case 'a=inactive': return lines[i].substr(2); default: // FIXME: What should happen here? } } if (sessionpart) { return SDPUtils.getDirection(sessionpart); } return 'sendrecv'; }; SDPUtils.getKind = function(mediaSection) { var lines = SDPUtils.splitLines(mediaSection); var mline = lines[0].split(' '); return mline[0].substr(2); }; SDPUtils.isRejected = function(mediaSection) { return mediaSection.split(' ', 2)[1] === '0'; }; SDPUtils.parseMLine = function(mediaSection) { var lines = SDPUtils.splitLines(mediaSection); var parts = lines[0].substr(2).split(' '); return { kind: parts[0], port: parseInt(parts[1], 10), protocol: parts[2], fmt: parts.slice(3).join(' ') }; }; SDPUtils.parseOLine = function(mediaSection) { var line = SDPUtils.matchPrefix(mediaSection, 'o=')[0]; var parts = line.substr(2).split(' '); return { username: parts[0], sessionId: parts[1], sessionVersion: parseInt(parts[2], 10), netType: parts[3], addressType: parts[4], address: parts[5] }; }; // a very naive interpretation of a valid SDP. SDPUtils.isValidSDP = function(blob) { if (typeof blob !== 'string' || blob.length === 0) { return false; } var lines = SDPUtils.splitLines(blob); for (var i = 0; i < lines.length; i++) { if (lines[i].length < 2 || lines[i].charAt(1) !== '=') { return false; } // TODO: check the modifier a bit more. } return true; }; // Expose public methods. if (typeof module === 'object') { module.exports = SDPUtils; } },{}],176:[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. module.exports = Stream; var EE = require('events').EventEmitter; var inherits = require('inherits'); inherits(Stream, EE); Stream.Readable = require('readable-stream/readable.js'); Stream.Writable = require('readable-stream/writable.js'); Stream.Duplex = require('readable-stream/duplex.js'); Stream.Transform = require('readable-stream/transform.js'); Stream.PassThrough = require('readable-stream/passthrough.js'); // Backwards-compat with node 0.4.x Stream.Stream = Stream; // old-style streams. Note that the pipe method (the only relevant // part of this class) is overridden in the Readable class. function Stream() { EE.call(this); } Stream.prototype.pipe = function(dest, options) { var source = this; function ondata(chunk) { if (dest.writable) { if (false === dest.write(chunk) && source.pause) { source.pause(); } } } source.on('data', ondata); function ondrain() { if (source.readable && source.resume) { source.resume(); } } dest.on('drain', ondrain); // If the 'end' option is not supplied, dest.end() will be called when // source gets the 'end' or 'close' events. Only dest.end() once. if (!dest._isStdio && (!options || options.end !== false)) { source.on('end', onend); source.on('close', onclose); } var didOnEnd = false; function onend() { if (didOnEnd) return; didOnEnd = true; dest.end(); } function onclose() { if (didOnEnd) return; didOnEnd = true; if (typeof dest.destroy === 'function') dest.destroy(); } // don't leave dangling pipes when there are errors. function onerror(er) { cleanup(); if (EE.listenerCount(this, 'error') === 0) { throw er; // Unhandled stream error in pipe. } } source.on('error', onerror); dest.on('error', onerror); // remove all the event listeners that were added. function cleanup() { source.removeListener('data', ondata); dest.removeListener('drain', ondrain); source.removeListener('end', onend); source.removeListener('close', onclose); source.removeListener('error', onerror); dest.removeListener('error', onerror); source.removeListener('end', cleanup); source.removeListener('close', cleanup); dest.removeListener('close', cleanup); } source.on('end', cleanup); source.on('close', cleanup); dest.on('close', cleanup); dest.emit('pipe', source); // Allow for unix-like usage: A.pipe(B).pipe(C) return dest; }; },{"events":80,"inherits":85,"readable-stream/duplex.js":177,"readable-stream/passthrough.js":186,"readable-stream/readable.js":187,"readable-stream/transform.js":188,"readable-stream/writable.js":189}],177:[function(require,module,exports){ module.exports = require('./lib/_stream_duplex.js'); },{"./lib/_stream_duplex.js":178}],178:[function(require,module,exports){ arguments[4][34][0].apply(exports,arguments) },{"./_stream_readable":180,"./_stream_writable":182,"core-util-is":49,"dup":34,"inherits":85,"process-nextick-args":151}],179:[function(require,module,exports){ arguments[4][35][0].apply(exports,arguments) },{"./_stream_transform":181,"core-util-is":49,"dup":35,"inherits":85}],180:[function(require,module,exports){ arguments[4][36][0].apply(exports,arguments) },{"./_stream_duplex":178,"./internal/streams/BufferList":183,"./internal/streams/destroy":184,"./internal/streams/stream":185,"_process":152,"core-util-is":49,"dup":36,"events":80,"inherits":85,"isarray":88,"process-nextick-args":151,"safe-buffer":190,"string_decoder/":191,"util":29}],181:[function(require,module,exports){ arguments[4][37][0].apply(exports,arguments) },{"./_stream_duplex":178,"core-util-is":49,"dup":37,"inherits":85}],182:[function(require,module,exports){ arguments[4][38][0].apply(exports,arguments) },{"./_stream_duplex":178,"./internal/streams/destroy":184,"./internal/streams/stream":185,"_process":152,"core-util-is":49,"dup":38,"inherits":85,"process-nextick-args":151,"safe-buffer":190,"timers":210,"util-deprecate":214}],183:[function(require,module,exports){ arguments[4][39][0].apply(exports,arguments) },{"dup":39,"safe-buffer":190,"util":29}],184:[function(require,module,exports){ arguments[4][40][0].apply(exports,arguments) },{"dup":40,"process-nextick-args":151}],185:[function(require,module,exports){ arguments[4][41][0].apply(exports,arguments) },{"dup":41,"events":80}],186:[function(require,module,exports){ module.exports = require('./readable').PassThrough },{"./readable":187}],187:[function(require,module,exports){ arguments[4][43][0].apply(exports,arguments) },{"./lib/_stream_duplex.js":178,"./lib/_stream_passthrough.js":179,"./lib/_stream_readable.js":180,"./lib/_stream_transform.js":181,"./lib/_stream_writable.js":182,"dup":43}],188:[function(require,module,exports){ arguments[4][44][0].apply(exports,arguments) },{"./readable":187,"dup":44}],189:[function(require,module,exports){ module.exports = require('./lib/_stream_writable.js'); },{"./lib/_stream_writable.js":182}],190:[function(require,module,exports){ arguments[4][45][0].apply(exports,arguments) },{"buffer":47,"dup":45}],191:[function(require,module,exports){ arguments[4][42][0].apply(exports,arguments) },{"dup":42,"safe-buffer":190}],192:[function(require,module,exports){ (function (global){(function (){ var ClientRequest = require('./lib/request') var response = require('./lib/response') var extend = require('xtend') var statusCodes = require('builtin-status-codes') var url = require('url') var http = exports http.request = function (opts, cb) { if (typeof opts === 'string') opts = url.parse(opts) else opts = extend(opts) // Normally, the page is loaded from http or https, so not specifying a protocol // will result in a (valid) protocol-relative url. However, this won't work if // the protocol is something else, like 'file:' var defaultProtocol = global.location.protocol.search(/^https?:$/) === -1 ? 'http:' : '' var protocol = opts.protocol || defaultProtocol var host = opts.hostname || opts.host var port = opts.port var path = opts.path || '/' // Necessary for IPv6 addresses if (host && host.indexOf(':') !== -1) host = '[' + host + ']' // This may be a relative url. The browser should always be able to interpret it correctly. opts.url = (host ? (protocol + '//' + host) : '') + (port ? ':' + port : '') + path opts.method = (opts.method || 'GET').toUpperCase() opts.headers = opts.headers || {} // Also valid opts.auth, opts.mode var req = new ClientRequest(opts) if (cb) req.on('response', cb) return req } http.get = function get (opts, cb) { var req = http.request(opts, cb) req.end() return req } http.ClientRequest = ClientRequest http.IncomingMessage = response.IncomingMessage http.Agent = function () {} http.Agent.defaultMaxSockets = 4 http.globalAgent = new http.Agent() http.STATUS_CODES = statusCodes http.METHODS = [ 'CHECKOUT', 'CONNECT', 'COPY', 'DELETE', 'GET', 'HEAD', 'LOCK', 'M-SEARCH', 'MERGE', 'MKACTIVITY', 'MKCOL', 'MOVE', 'NOTIFY', 'OPTIONS', 'PATCH', 'POST', 'PROPFIND', 'PROPPATCH', 'PURGE', 'PUT', 'REPORT', 'SEARCH', 'SUBSCRIBE', 'TRACE', 'UNLOCK', 'UNSUBSCRIBE' ] }).call(this)}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) },{"./lib/request":194,"./lib/response":195,"builtin-status-codes":48,"url":212,"xtend":233}],193:[function(require,module,exports){ (function (global){(function (){ exports.fetch = isFunction(global.fetch) && isFunction(global.ReadableStream) exports.writableStream = isFunction(global.WritableStream) exports.abortController = isFunction(global.AbortController) exports.blobConstructor = false try { new Blob([new ArrayBuffer(1)]) exports.blobConstructor = true } catch (e) {} // The xhr request to example.com may violate some restrictive CSP configurations, // so if we're running in a browser that supports `fetch`, avoid calling getXHR() // and assume support for certain features below. var xhr function getXHR () { // Cache the xhr value if (xhr !== undefined) return xhr if (global.XMLHttpRequest) { xhr = new global.XMLHttpRequest() // If XDomainRequest is available (ie only, where xhr might not work // cross domain), use the page location. Otherwise use example.com // Note: this doesn't actually make an http request. try { xhr.open('GET', global.XDomainRequest ? '/' : 'https://example.com') } catch(e) { xhr = null } } else { // Service workers don't have XHR xhr = null } return xhr } function checkTypeSupport (type) { var xhr = getXHR() if (!xhr) return false try { xhr.responseType = type return xhr.responseType === type } catch (e) {} return false } // For some strange reason, Safari 7.0 reports typeof global.ArrayBuffer === 'object'. // Safari 7.1 appears to have fixed this bug. var haveArrayBuffer = typeof global.ArrayBuffer !== 'undefined' var haveSlice = haveArrayBuffer && isFunction(global.ArrayBuffer.prototype.slice) // If fetch is supported, then arraybuffer will be supported too. Skip calling // checkTypeSupport(), since that calls getXHR(). exports.arraybuffer = exports.fetch || (haveArrayBuffer && checkTypeSupport('arraybuffer')) // These next two tests unavoidably show warnings in Chrome. Since fetch will always // be used if it's available, just return false for these to avoid the warnings. exports.msstream = !exports.fetch && haveSlice && checkTypeSupport('ms-stream') exports.mozchunkedarraybuffer = !exports.fetch && haveArrayBuffer && checkTypeSupport('moz-chunked-arraybuffer') // If fetch is supported, then overrideMimeType will be supported too. Skip calling // getXHR(). exports.overrideMimeType = exports.fetch || (getXHR() ? isFunction(getXHR().overrideMimeType) : false) exports.vbArray = isFunction(global.VBArray) function isFunction (value) { return typeof value === 'function' } xhr = null // Help gc }).call(this)}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) },{}],194:[function(require,module,exports){ (function (process,global,Buffer){(function (){ var capability = require('./capability') var inherits = require('inherits') var response = require('./response') var stream = require('readable-stream') var toArrayBuffer = require('to-arraybuffer') var IncomingMessage = response.IncomingMessage var rStates = response.readyStates function decideMode (preferBinary, useFetch) { if (capability.fetch && useFetch) { return 'fetch' } else if (capability.mozchunkedarraybuffer) { return 'moz-chunked-arraybuffer' } else if (capability.msstream) { return 'ms-stream' } else if (capability.arraybuffer && preferBinary) { return 'arraybuffer' } else if (capability.vbArray && preferBinary) { return 'text:vbarray' } else { return 'text' } } var ClientRequest = module.exports = function (opts) { var self = this stream.Writable.call(self) self._opts = opts self._body = [] self._headers = {} if (opts.auth) self.setHeader('Authorization', 'Basic ' + new Buffer(opts.auth).toString('base64')) Object.keys(opts.headers).forEach(function (name) { self.setHeader(name, opts.headers[name]) }) var preferBinary var useFetch = true if (opts.mode === 'disable-fetch' || ('requestTimeout' in opts && !capability.abortController)) { // If the use of XHR should be preferred. Not typically needed. useFetch = false preferBinary = true } else if (opts.mode === 'prefer-streaming') { // If streaming is a high priority but binary compatibility and // the accuracy of the 'content-type' header aren't preferBinary = false } else if (opts.mode === 'allow-wrong-content-type') { // If streaming is more important than preserving the 'content-type' header preferBinary = !capability.overrideMimeType } else if (!opts.mode || opts.mode === 'default' || opts.mode === 'prefer-fast') { // Use binary if text streaming may corrupt data or the content-type header, or for speed preferBinary = true } else { throw new Error('Invalid value for opts.mode') } self._mode = decideMode(preferBinary, useFetch) self._fetchTimer = null self.on('finish', function () { self._onFinish() }) } inherits(ClientRequest, stream.Writable) ClientRequest.prototype.setHeader = function (name, value) { var self = this var lowerName = name.toLowerCase() // This check is not necessary, but it prevents warnings from browsers about setting unsafe // headers. To be honest I'm not entirely sure hiding these warnings is a good thing, but // http-browserify did it, so I will too. if (unsafeHeaders.indexOf(lowerName) !== -1) return self._headers[lowerName] = { name: name, value: value } } ClientRequest.prototype.getHeader = function (name) { var header = this._headers[name.toLowerCase()] if (header) return header.value return null } ClientRequest.prototype.removeHeader = function (name) { var self = this delete self._headers[name.toLowerCase()] } ClientRequest.prototype._onFinish = function () { var self = this if (self._destroyed) return var opts = self._opts var headersObj = self._headers var body = null if (opts.method !== 'GET' && opts.method !== 'HEAD') { if (capability.arraybuffer) { body = toArrayBuffer(Buffer.concat(self._body)) } else if (capability.blobConstructor) { body = new global.Blob(self._body.map(function (buffer) { return toArrayBuffer(buffer) }), { type: (headersObj['content-type'] || {}).value || '' }) } else { // get utf8 string body = Buffer.concat(self._body).toString() } } // create flattened list of headers var headersList = [] Object.keys(headersObj).forEach(function (keyName) { var name = headersObj[keyName].name var value = headersObj[keyName].value if (Array.isArray(value)) { value.forEach(function (v) { headersList.push([name, v]) }) } else { headersList.push([name, value]) } }) if (self._mode === 'fetch') { var signal = null var fetchTimer = null if (capability.abortController) { var controller = new AbortController() signal = controller.signal self._fetchAbortController = controller if ('requestTimeout' in opts && opts.requestTimeout !== 0) { self._fetchTimer = global.setTimeout(function () { self.emit('requestTimeout') if (self._fetchAbortController) self._fetchAbortController.abort() }, opts.requestTimeout) } } global.fetch(self._opts.url, { method: self._opts.method, headers: headersList, body: body || undefined, mode: 'cors', credentials: opts.withCredentials ? 'include' : 'same-origin', signal: signal }).then(function (response) { self._fetchResponse = response self._connect() }, function (reason) { global.clearTimeout(self._fetchTimer) if (!self._destroyed) self.emit('error', reason) }) } else { var xhr = self._xhr = new global.XMLHttpRequest() try { xhr.open(self._opts.method, self._opts.url, true) } catch (err) { process.nextTick(function () { self.emit('error', err) }) return } // Can't set responseType on really old browsers if ('responseType' in xhr) xhr.responseType = self._mode.split(':')[0] if ('withCredentials' in xhr) xhr.withCredentials = !!opts.withCredentials if (self._mode === 'text' && 'overrideMimeType' in xhr) xhr.overrideMimeType('text/plain; charset=x-user-defined') if ('requestTimeout' in opts) { xhr.timeout = opts.requestTimeout xhr.ontimeout = function () { self.emit('requestTimeout') } } headersList.forEach(function (header) { xhr.setRequestHeader(header[0], header[1]) }) self._response = null xhr.onreadystatechange = function () { switch (xhr.readyState) { case rStates.LOADING: case rStates.DONE: self._onXHRProgress() break } } // Necessary for streaming in Firefox, since xhr.response is ONLY defined // in onprogress, not in onreadystatechange with xhr.readyState = 3 if (self._mode === 'moz-chunked-arraybuffer') { xhr.onprogress = function () { self._onXHRProgress() } } xhr.onerror = function () { if (self._destroyed) return self.emit('error', new Error('XHR error')) } try { xhr.send(body) } catch (err) { process.nextTick(function () { self.emit('error', err) }) return } } } /** * Checks if xhr.status is readable and non-zero, indicating no error. * Even though the spec says it should be available in readyState 3, * accessing it throws an exception in IE8 */ function statusValid (xhr) { try { var status = xhr.status return (status !== null && status !== 0) } catch (e) { return false } } ClientRequest.prototype._onXHRProgress = function () { var self = this if (!statusValid(self._xhr) || self._destroyed) return if (!self._response) self._connect() self._response._onXHRProgress() } ClientRequest.prototype._connect = function () { var self = this if (self._destroyed) return self._response = new IncomingMessage(self._xhr, self._fetchResponse, self._mode, self._fetchTimer) self._response.on('error', function(err) { self.emit('error', err) }) self.emit('response', self._response) } ClientRequest.prototype._write = function (chunk, encoding, cb) { var self = this self._body.push(chunk) cb() } ClientRequest.prototype.abort = ClientRequest.prototype.destroy = function () { var self = this self._destroyed = true global.clearTimeout(self._fetchTimer) if (self._response) self._response._destroyed = true if (self._xhr) self._xhr.abort() else if (self._fetchAbortController) self._fetchAbortController.abort() } ClientRequest.prototype.end = function (data, encoding, cb) { var self = this if (typeof data === 'function') { cb = data data = undefined } stream.Writable.prototype.end.call(self, data, encoding, cb) } ClientRequest.prototype.flushHeaders = function () {} ClientRequest.prototype.setTimeout = function () {} ClientRequest.prototype.setNoDelay = function () {} ClientRequest.prototype.setSocketKeepAlive = function () {} // Taken from http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader%28%29-method var unsafeHeaders = [ 'accept-charset', 'accept-encoding', 'access-control-request-headers', 'access-control-request-method', 'connection', 'content-length', 'cookie', 'cookie2', 'date', 'dnt', 'expect', 'host', 'keep-alive', 'origin', 'referer', 'te', 'trailer', 'transfer-encoding', 'upgrade', 'via' ] }).call(this)}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {},require("buffer").Buffer) },{"./capability":193,"./response":195,"_process":152,"buffer":47,"inherits":85,"readable-stream":204,"to-arraybuffer":211}],195:[function(require,module,exports){ (function (process,global,Buffer){(function (){ var capability = require('./capability') var inherits = require('inherits') var stream = require('readable-stream') var rStates = exports.readyStates = { UNSENT: 0, OPENED: 1, HEADERS_RECEIVED: 2, LOADING: 3, DONE: 4 } var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode, fetchTimer) { var self = this stream.Readable.call(self) self._mode = mode self.headers = {} self.rawHeaders = [] self.trailers = {} self.rawTrailers = [] // Fake the 'close' event, but only once 'end' fires self.on('end', function () { // The nextTick is necessary to prevent the 'request' module from causing an infinite loop process.nextTick(function () { self.emit('close') }) }) if (mode === 'fetch') { self._fetchResponse = response self.url = response.url self.statusCode = response.status self.statusMessage = response.statusText response.headers.forEach(function (header, key){ self.headers[key.toLowerCase()] = header self.rawHeaders.push(key, header) }) if (capability.writableStream) { var writable = new WritableStream({ write: function (chunk) { return new Promise(function (resolve, reject) { if (self._destroyed) { reject() } else if(self.push(new Buffer(chunk))) { resolve() } else { self._resumeFetch = resolve } }) }, close: function () { global.clearTimeout(fetchTimer) if (!self._destroyed) self.push(null) }, abort: function (err) { if (!self._destroyed) self.emit('error', err) } }) try { response.body.pipeTo(writable).catch(function (err) { global.clearTimeout(fetchTimer) if (!self._destroyed) self.emit('error', err) }) return } catch (e) {} // pipeTo method isn't defined. Can't find a better way to feature test this } // fallback for when writableStream or pipeTo aren't available var reader = response.body.getReader() function read () { reader.read().then(function (result) { if (self._destroyed) return if (result.done) { global.clearTimeout(fetchTimer) self.push(null) return } self.push(new Buffer(result.value)) read() }).catch(function (err) { global.clearTimeout(fetchTimer) if (!self._destroyed) self.emit('error', err) }) } read() } else { self._xhr = xhr self._pos = 0 self.url = xhr.responseURL self.statusCode = xhr.status self.statusMessage = xhr.statusText var headers = xhr.getAllResponseHeaders().split(/\r?\n/) headers.forEach(function (header) { var matches = header.match(/^([^:]+):\s*(.*)/) if (matches) { var key = matches[1].toLowerCase() if (key === 'set-cookie') { if (self.headers[key] === undefined) { self.headers[key] = [] } self.headers[key].push(matches[2]) } else if (self.headers[key] !== undefined) { self.headers[key] += ', ' + matches[2] } else { self.headers[key] = matches[2] } self.rawHeaders.push(matches[1], matches[2]) } }) self._charset = 'x-user-defined' if (!capability.overrideMimeType) { var mimeType = self.rawHeaders['mime-type'] if (mimeType) { var charsetMatch = mimeType.match(/;\s*charset=([^;])(;|$)/) if (charsetMatch) { self._charset = charsetMatch[1].toLowerCase() } } if (!self._charset) self._charset = 'utf-8' // best guess } } } inherits(IncomingMessage, stream.Readable) IncomingMessage.prototype._read = function () { var self = this var resolve = self._resumeFetch if (resolve) { self._resumeFetch = null resolve() } } IncomingMessage.prototype._onXHRProgress = function () { var self = this var xhr = self._xhr var response = null switch (self._mode) { case 'text:vbarray': // For IE9 if (xhr.readyState !== rStates.DONE) break try { // This fails in IE8 response = new global.VBArray(xhr.responseBody).toArray() } catch (e) {} if (response !== null) { self.push(new Buffer(response)) break } // Falls through in IE8 case 'text': try { // This will fail when readyState = 3 in IE9. Switch mode and wait for readyState = 4 response = xhr.responseText } catch (e) { self._mode = 'text:vbarray' break } if (response.length > self._pos) { var newData = response.substr(self._pos) if (self._charset === 'x-user-defined') { var buffer = new Buffer(newData.length) for (var i = 0; i < newData.length; i++) buffer[i] = newData.charCodeAt(i) & 0xff self.push(buffer) } else { self.push(newData, self._charset) } self._pos = response.length } break case 'arraybuffer': if (xhr.readyState !== rStates.DONE || !xhr.response) break response = xhr.response self.push(new Buffer(new Uint8Array(response))) break case 'moz-chunked-arraybuffer': // take whole response = xhr.response if (xhr.readyState !== rStates.LOADING || !response) break self.push(new Buffer(new Uint8Array(response))) break case 'ms-stream': response = xhr.response if (xhr.readyState !== rStates.LOADING) break var reader = new global.MSStreamReader() reader.onprogress = function () { if (reader.result.byteLength > self._pos) { self.push(new Buffer(new Uint8Array(reader.result.slice(self._pos)))) self._pos = reader.result.byteLength } } reader.onload = function () { self.push(null) } // reader.onerror = ??? // TODO: this reader.readAsArrayBuffer(response) break } // The ms-stream case handles end separately in reader.onload() if (self._xhr.readyState === rStates.DONE && self._mode !== 'ms-stream') { self.push(null) } } }).call(this)}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {},require("buffer").Buffer) },{"./capability":193,"_process":152,"buffer":47,"inherits":85,"readable-stream":204}],196:[function(require,module,exports){ arguments[4][34][0].apply(exports,arguments) },{"./_stream_readable":198,"./_stream_writable":200,"core-util-is":49,"dup":34,"inherits":85,"process-nextick-args":151}],197:[function(require,module,exports){ arguments[4][35][0].apply(exports,arguments) },{"./_stream_transform":199,"core-util-is":49,"dup":35,"inherits":85}],198:[function(require,module,exports){ arguments[4][36][0].apply(exports,arguments) },{"./_stream_duplex":196,"./internal/streams/BufferList":201,"./internal/streams/destroy":202,"./internal/streams/stream":203,"_process":152,"core-util-is":49,"dup":36,"events":80,"inherits":85,"isarray":88,"process-nextick-args":151,"safe-buffer":205,"string_decoder/":206,"util":29}],199:[function(require,module,exports){ arguments[4][37][0].apply(exports,arguments) },{"./_stream_duplex":196,"core-util-is":49,"dup":37,"inherits":85}],200:[function(require,module,exports){ arguments[4][38][0].apply(exports,arguments) },{"./_stream_duplex":196,"./internal/streams/destroy":202,"./internal/streams/stream":203,"_process":152,"core-util-is":49,"dup":38,"inherits":85,"process-nextick-args":151,"safe-buffer":205,"timers":210,"util-deprecate":214}],201:[function(require,module,exports){ arguments[4][39][0].apply(exports,arguments) },{"dup":39,"safe-buffer":205,"util":29}],202:[function(require,module,exports){ arguments[4][40][0].apply(exports,arguments) },{"dup":40,"process-nextick-args":151}],203:[function(require,module,exports){ arguments[4][41][0].apply(exports,arguments) },{"dup":41,"events":80}],204:[function(require,module,exports){ arguments[4][43][0].apply(exports,arguments) },{"./lib/_stream_duplex.js":196,"./lib/_stream_passthrough.js":197,"./lib/_stream_readable.js":198,"./lib/_stream_transform.js":199,"./lib/_stream_writable.js":200,"dup":43}],205:[function(require,module,exports){ arguments[4][45][0].apply(exports,arguments) },{"buffer":47,"dup":45}],206:[function(require,module,exports){ arguments[4][42][0].apply(exports,arguments) },{"dup":42,"safe-buffer":205}],207:[function(require,module,exports){ arguments[4][42][0].apply(exports,arguments) },{"dup":42,"safe-buffer":173}],208:[function(require,module,exports){ (function (global){(function (){ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.strophe = {})); })(this, (function (exports) { 'use strict'; var global$1 = typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}; /* * This module provides uniform * Shims APIs and globals that are not present in all JS environments, * the most common example for Strophe being browser APIs like WebSocket * and DOM that don't exist under nodejs. * * Usually these will be supplied in nodejs by conditionally requiring a * NPM module that provides a compatible implementation. */ /* global global */ /** * WHATWG WebSockets API * https://www.w3.org/TR/websockets/ * * Interface to use the web socket protocol * * Used implementations: * - supported browsers: built-in in WebSocket global * https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#Browser_compatibility * - nodejs: use standard-compliant 'ws' module * https://www.npmjs.com/package/ws */ function getWebSocketImplementation() { let WebSocketImplementation = global$1.WebSocket; if (typeof WebSocketImplementation === 'undefined') { try { WebSocketImplementation = require('ws'); } catch (err) { throw new Error('You must install the "ws" package to use Strophe in nodejs.'); } } return WebSocketImplementation; } const WebSocket = getWebSocketImplementation(); /** * DOMParser * https://w3c.github.io/DOM-Parsing/#the-domparser-interface * * Interface to parse XML strings into Document objects * * Used implementations: * - supported browsers: built-in in DOMParser global * https://developer.mozilla.org/en-US/docs/Web/API/DOMParser#Browser_compatibility * - nodejs: use '@xmldom/xmldom' module * https://www.npmjs.com/package/@xmldom/xmldom */ function getDOMParserImplementation() { let DOMParserImplementation = global$1.DOMParser; if (typeof DOMParserImplementation === 'undefined') { try { DOMParserImplementation = require('@xmldom/xmldom').DOMParser; } catch (err) { throw new Error('You must install the "@xmldom/xmldom" package to use Strophe in nodejs.'); } } return DOMParserImplementation; } const DOMParser = getDOMParserImplementation(); /** * Gets IE xml doc object. Used by getDummyXMLDocument shim. * * Returns: * A Microsoft XML DOM Object * See Also: * http://msdn.microsoft.com/en-us/library/ms757837%28VS.85%29.aspx */ function _getIEXmlDom() { const docStrings = ["Msxml2.DOMDocument.6.0", "Msxml2.DOMDocument.5.0", "Msxml2.DOMDocument.4.0", "MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument", "MSXML.DOMDocument", "Microsoft.XMLDOM"]; for (let d = 0; d < docStrings.length; d++) { try { // eslint-disable-next-line no-undef const doc = new ActiveXObject(docStrings[d]); return doc; } catch (e) {// Try next one } } } /** * Creates a dummy XML DOM document to serve as an element and text node generator. * * Used implementations: * - IE < 10: avoid using createDocument() due to a memory leak, use ie-specific * workaround * - other supported browsers: use document's createDocument * - nodejs: use '@xmldom/xmldom' */ function getDummyXMLDOMDocument() { // nodejs if (typeof document === 'undefined') { try { const DOMImplementation = require('@xmldom/xmldom').DOMImplementation; return new DOMImplementation().createDocument('jabber:client', 'strophe', null); } catch (err) { throw new Error('You must install the "@xmldom/xmldom" package to use Strophe in nodejs.'); } } // IE < 10 if (document.implementation.createDocument === undefined || document.implementation.createDocument && document.documentMode && document.documentMode < 10) { const doc = _getIEXmlDom(); doc.appendChild(doc.createElement('strophe')); return doc; } // All other supported browsers return document.implementation.createDocument('jabber:client', 'strophe', null); } /* * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algorithm, as defined in RFC 1321. * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet * Distributed under the BSD License * See http://pajhome.org.uk/crypt/md5 for more info. */ /* * Everything that isn't used by Strophe has been stripped here! */ /* * Add integers, wrapping at 2^32. This uses 16-bit operations internally * to work around bugs in some JS interpreters. */ const safe_add$1 = function (x, y) { const lsw = (x & 0xFFFF) + (y & 0xFFFF); const msw = (x >> 16) + (y >> 16) + (lsw >> 16); return msw << 16 | lsw & 0xFFFF; }; /* * Bitwise rotate a 32-bit number to the left. */ const bit_rol = function (num, cnt) { return num << cnt | num >>> 32 - cnt; }; /* * Convert a string to an array of little-endian words */ const str2binl = function (str) { if (typeof str !== "string") { throw new Error("str2binl was passed a non-string"); } const bin = []; for (let i = 0; i < str.length * 8; i += 8) { bin[i >> 5] |= (str.charCodeAt(i / 8) & 255) << i % 32; } return bin; }; /* * Convert an array of little-endian words to a string */ const binl2str = function (bin) { let str = ""; for (let i = 0; i < bin.length * 32; i += 8) { str += String.fromCharCode(bin[i >> 5] >>> i % 32 & 255); } return str; }; /* * Convert an array of little-endian words to a hex string. */ const binl2hex = function (binarray) { const hex_tab = "0123456789abcdef"; let str = ""; for (let i = 0; i < binarray.length * 4; i++) { str += hex_tab.charAt(binarray[i >> 2] >> i % 4 * 8 + 4 & 0xF) + hex_tab.charAt(binarray[i >> 2] >> i % 4 * 8 & 0xF); } return str; }; /* * These functions implement the four basic operations the algorithm uses. */ const md5_cmn = function (q, a, b, x, s, t) { return safe_add$1(bit_rol(safe_add$1(safe_add$1(a, q), safe_add$1(x, t)), s), b); }; const md5_ff = function (a, b, c, d, x, s, t) { return md5_cmn(b & c | ~b & d, a, b, x, s, t); }; const md5_gg = function (a, b, c, d, x, s, t) { return md5_cmn(b & d | c & ~d, a, b, x, s, t); }; const md5_hh = function (a, b, c, d, x, s, t) { return md5_cmn(b ^ c ^ d, a, b, x, s, t); }; const md5_ii = function (a, b, c, d, x, s, t) { return md5_cmn(c ^ (b | ~d), a, b, x, s, t); }; /* * Calculate the MD5 of an array of little-endian words, and a bit length */ const core_md5 = function (x, len) { /* append padding */ x[len >> 5] |= 0x80 << len % 32; x[(len + 64 >>> 9 << 4) + 14] = len; let a = 1732584193; let b = -271733879; let c = -1732584194; let d = 271733878; let olda, oldb, oldc, oldd; for (let i = 0; i < x.length; i += 16) { olda = a; oldb = b; oldc = c; oldd = d; a = md5_ff(a, b, c, d, x[i + 0], 7, -680876936); d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586); c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819); b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330); a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897); d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426); c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341); b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983); a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416); d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417); c = md5_ff(c, d, a, b, x[i + 10], 17, -42063); b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162); a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682); d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101); c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290); b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329); a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510); d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632); c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713); b = md5_gg(b, c, d, a, x[i + 0], 20, -373897302); a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691); d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083); c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335); b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848); a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438); d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690); c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961); b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501); a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467); d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784); c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473); b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734); a = md5_hh(a, b, c, d, x[i + 5], 4, -378558); d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463); c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562); b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556); a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060); d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353); c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632); b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640); a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174); d = md5_hh(d, a, b, c, x[i + 0], 11, -358537222); c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979); b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189); a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487); d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835); c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520); b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651); a = md5_ii(a, b, c, d, x[i + 0], 6, -198630844); d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415); c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905); b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055); a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571); d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606); c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523); b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799); a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359); d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744); c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380); b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649); a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070); d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379); c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259); b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551); a = safe_add$1(a, olda); b = safe_add$1(b, oldb); c = safe_add$1(c, oldc); d = safe_add$1(d, oldd); } return [a, b, c, d]; }; /* * These are the functions you'll usually want to call. * They take string arguments and return either hex or base-64 encoded * strings. */ const MD5 = { hexdigest: function (s) { return binl2hex(core_md5(str2binl(s), s.length * 8)); }, hash: function (s) { return binl2str(core_md5(str2binl(s), s.length * 8)); } }; /** Class: Strophe.SASLMechanism * * Encapsulates an SASL authentication mechanism. * * User code may override the priority for each mechanism or disable it completely. * See for information about changing priority and for informatian on * how to disable a mechanism. * * By default, all mechanisms are enabled and the priorities are * * SCRAM-SHA-1 - 60 * PLAIN - 50 * OAUTHBEARER - 40 * X-OAUTH2 - 30 * ANONYMOUS - 20 * EXTERNAL - 10 * * See: Strophe.Connection.addSupportedSASLMechanisms */ class SASLMechanism { /** * PrivateConstructor: Strophe.SASLMechanism * SASL auth mechanism abstraction. * * Parameters: * (String) name - SASL Mechanism name. * (Boolean) isClientFirst - If client should send response first without challenge. * (Number) priority - Priority. * * Returns: * A new Strophe.SASLMechanism object. */ constructor(name, isClientFirst, priority) { /** PrivateVariable: mechname * Mechanism name. */ this.mechname = name; /** PrivateVariable: isClientFirst * If client sends response without initial server challenge. */ this.isClientFirst = isClientFirst; /** Variable: priority * Determines which is chosen for authentication (Higher is better). * Users may override this to prioritize mechanisms differently. * * Example: (This will cause Strophe to choose the mechanism that the server sent first) * * > Strophe.SASLPlain.priority = Strophe.SASLSHA1.priority; * * See for a list of available mechanisms. * */ this.priority = priority; } /** * Function: test * Checks if mechanism able to run. * To disable a mechanism, make this return false; * * To disable plain authentication run * > Strophe.SASLPlain.test = function() { * > return false; * > } * * See for a list of available mechanisms. * * Parameters: * (Strophe.Connection) connection - Target Connection. * * Returns: * (Boolean) If mechanism was able to run. */ test() { // eslint-disable-line class-methods-use-this return true; } /** PrivateFunction: onStart * Called before starting mechanism on some connection. * * Parameters: * (Strophe.Connection) connection - Target Connection. */ onStart(connection) { this._connection = connection; } /** PrivateFunction: onChallenge * Called by protocol implementation on incoming challenge. * * By deafult, if the client is expected to send data first (isClientFirst === true), * this method is called with `challenge` as null on the first call, * unless `clientChallenge` is overridden in the relevant subclass. * * Parameters: * (Strophe.Connection) connection - Target Connection. * (String) challenge - current challenge to handle. * * Returns: * (String) Mechanism response. */ onChallenge(connection, challenge) { // eslint-disable-line throw new Error("You should implement challenge handling!"); } /** PrivateFunction: clientChallenge * Called by the protocol implementation if the client is expected to send * data first in the authentication exchange (i.e. isClientFirst === true). * * Parameters: * (Strophe.Connection) connection - Target Connection. * * Returns: * (String) Mechanism response. */ clientChallenge(connection) { if (!this.isClientFirst) { throw new Error("clientChallenge should not be called if isClientFirst is false!"); } return this.onChallenge(connection); } /** PrivateFunction: onFailure * Protocol informs mechanism implementation about SASL failure. */ onFailure() { this._connection = null; } /** PrivateFunction: onSuccess * Protocol informs mechanism implementation about SASL success. */ onSuccess() { this._connection = null; } } class SASLAnonymous extends SASLMechanism { /** PrivateConstructor: SASLAnonymous * SASL ANONYMOUS authentication. */ constructor() { let mechname = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'ANONYMOUS'; let isClientFirst = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; let priority = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 20; super(mechname, isClientFirst, priority); } test(connection) { // eslint-disable-line class-methods-use-this return connection.authcid === null; } } class SASLExternal extends SASLMechanism { /** PrivateConstructor: SASLExternal * SASL EXTERNAL authentication. * * The EXTERNAL mechanism allows a client to request the server to use * credentials established by means external to the mechanism to * authenticate the client. The external means may be, for instance, * TLS services. */ constructor() { let mechname = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'EXTERNAL'; let isClientFirst = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; let priority = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 10; super(mechname, isClientFirst, priority); } onChallenge(connection) { // eslint-disable-line class-methods-use-this /** According to XEP-178, an authzid SHOULD NOT be presented when the * authcid contained or implied in the client certificate is the JID (i.e. * authzid) with which the user wants to log in as. * * To NOT send the authzid, the user should therefore set the authcid equal * to the JID when instantiating a new Strophe.Connection object. */ return connection.authcid === connection.authzid ? '' : connection.authzid; } } const utils = { utf16to8: function (str) { var i, c; var out = ""; var len = str.length; for (i = 0; i < len; i++) { c = str.charCodeAt(i); if (c >= 0x0000 && c <= 0x007F) { out += str.charAt(i); } else if (c > 0x07FF) { out += String.fromCharCode(0xE0 | c >> 12 & 0x0F); out += String.fromCharCode(0x80 | c >> 6 & 0x3F); out += String.fromCharCode(0x80 | c >> 0 & 0x3F); } else { out += String.fromCharCode(0xC0 | c >> 6 & 0x1F); out += String.fromCharCode(0x80 | c >> 0 & 0x3F); } } return out; }, addCookies: function (cookies) { /* Parameters: * (Object) cookies - either a map of cookie names * to string values or to maps of cookie values. * * For example: * { "myCookie": "1234" } * * or: * { "myCookie": { * "value": "1234", * "domain": ".example.org", * "path": "/", * "expires": expirationDate * } * } * * These values get passed to Strophe.Connection via * options.cookies */ cookies = cookies || {}; for (const cookieName in cookies) { if (Object.prototype.hasOwnProperty.call(cookies, cookieName)) { let expires = ''; let domain = ''; let path = ''; const cookieObj = cookies[cookieName]; const isObj = typeof cookieObj === "object"; const cookieValue = escape(unescape(isObj ? cookieObj.value : cookieObj)); if (isObj) { expires = cookieObj.expires ? ";expires=" + cookieObj.expires : ''; domain = cookieObj.domain ? ";domain=" + cookieObj.domain : ''; path = cookieObj.path ? ";path=" + cookieObj.path : ''; } document.cookie = cookieName + '=' + cookieValue + expires + domain + path; } } } }; class SASLOAuthBearer extends SASLMechanism { /** PrivateConstructor: SASLOAuthBearer * SASL OAuth Bearer authentication. */ constructor() { let mechname = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'OAUTHBEARER'; let isClientFirst = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; let priority = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 40; super(mechname, isClientFirst, priority); } test(connection) { // eslint-disable-line class-methods-use-this return connection.pass !== null; } onChallenge(connection) { // eslint-disable-line class-methods-use-this let auth_str = 'n,'; if (connection.authcid !== null) { auth_str = auth_str + 'a=' + connection.authzid; } auth_str = auth_str + ','; auth_str = auth_str + "\u0001"; auth_str = auth_str + 'auth=Bearer '; auth_str = auth_str + connection.pass; auth_str = auth_str + "\u0001"; auth_str = auth_str + "\u0001"; return utils.utf16to8(auth_str); } } class SASLPlain extends SASLMechanism { /** PrivateConstructor: SASLPlain * SASL PLAIN authentication. */ constructor() { let mechname = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'PLAIN'; let isClientFirst = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; let priority = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 50; super(mechname, isClientFirst, priority); } test(connection) { // eslint-disable-line class-methods-use-this return connection.authcid !== null; } onChallenge(connection) { // eslint-disable-line class-methods-use-this const { authcid, authzid, domain, pass } = connection; if (!domain) { throw new Error("SASLPlain onChallenge: domain is not defined!"); } // Only include authzid if it differs from authcid. // See: https://tools.ietf.org/html/rfc6120#section-6.3.8 let auth_str = authzid !== `${authcid}@${domain}` ? authzid : ''; auth_str = auth_str + "\u0000"; auth_str = auth_str + authcid; auth_str = auth_str + "\u0000"; auth_str = auth_str + pass; return utils.utf16to8(auth_str); } } /* * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined * in FIPS PUB 180-1 * Version 2.1a Copyright Paul Johnston 2000 - 2002. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet * Distributed under the BSD License * See http://pajhome.org.uk/crypt/md5 for details. */ /* global define */ /* Some functions and variables have been stripped for use with Strophe */ /* * Calculate the SHA-1 of an array of big-endian words, and a bit length */ function core_sha1(x, len) { /* append padding */ x[len >> 5] |= 0x80 << 24 - len % 32; x[(len + 64 >> 9 << 4) + 15] = len; var w = new Array(80); var a = 1732584193; var b = -271733879; var c = -1732584194; var d = 271733878; var e = -1009589776; var i, j, t, olda, oldb, oldc, oldd, olde; for (i = 0; i < x.length; i += 16) { olda = a; oldb = b; oldc = c; oldd = d; olde = e; for (j = 0; j < 80; j++) { if (j < 16) { w[j] = x[i + j]; } else { w[j] = rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1); } t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)), safe_add(safe_add(e, w[j]), sha1_kt(j))); e = d; d = c; c = rol(b, 30); b = a; a = t; } a = safe_add(a, olda); b = safe_add(b, oldb); c = safe_add(c, oldc); d = safe_add(d, oldd); e = safe_add(e, olde); } return [a, b, c, d, e]; } /* * Perform the appropriate triplet combination function for the current * iteration */ function sha1_ft(t, b, c, d) { if (t < 20) { return b & c | ~b & d; } if (t < 40) { return b ^ c ^ d; } if (t < 60) { return b & c | b & d | c & d; } return b ^ c ^ d; } /* * Determine the appropriate additive constant for the current iteration */ function sha1_kt(t) { return t < 20 ? 1518500249 : t < 40 ? 1859775393 : t < 60 ? -1894007588 : -899497514; } /* * Calculate the HMAC-SHA1 of a key and some data */ function core_hmac_sha1(key, data) { var bkey = str2binb(key); if (bkey.length > 16) { bkey = core_sha1(bkey, key.length * 8); } var ipad = new Array(16), opad = new Array(16); for (var i = 0; i < 16; i++) { ipad[i] = bkey[i] ^ 0x36363636; opad[i] = bkey[i] ^ 0x5C5C5C5C; } var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * 8); return core_sha1(opad.concat(hash), 512 + 160); } /* * Add integers, wrapping at 2^32. This uses 16-bit operations internally * to work around bugs in some JS interpreters. */ function safe_add(x, y) { var lsw = (x & 0xFFFF) + (y & 0xFFFF); var msw = (x >> 16) + (y >> 16) + (lsw >> 16); return msw << 16 | lsw & 0xFFFF; } /* * Bitwise rotate a 32-bit number to the left. */ function rol(num, cnt) { return num << cnt | num >>> 32 - cnt; } /* * Convert an 8-bit or 16-bit string to an array of big-endian words * In 8-bit function, characters >255 have their hi-byte silently ignored. */ function str2binb(str) { var bin = []; var mask = 255; for (var i = 0; i < str.length * 8; i += 8) { bin[i >> 5] |= (str.charCodeAt(i / 8) & mask) << 24 - i % 32; } return bin; } /* * Convert an array of big-endian words to a base-64 string */ function binb2b64(binarray) { var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var str = ""; var triplet, j; for (var i = 0; i < binarray.length * 4; i += 3) { triplet = (binarray[i >> 2] >> 8 * (3 - i % 4) & 0xFF) << 16 | (binarray[i + 1 >> 2] >> 8 * (3 - (i + 1) % 4) & 0xFF) << 8 | binarray[i + 2 >> 2] >> 8 * (3 - (i + 2) % 4) & 0xFF; for (j = 0; j < 4; j++) { if (i * 8 + j * 6 > binarray.length * 32) { str += "="; } else { str += tab.charAt(triplet >> 6 * (3 - j) & 0x3F); } } } return str; } /* * Convert an array of big-endian words to a string */ function binb2str(bin) { var str = ""; var mask = 255; for (var i = 0; i < bin.length * 32; i += 8) { str += String.fromCharCode(bin[i >> 5] >>> 24 - i % 32 & mask); } return str; } /* * These are the functions you'll usually want to call * They take string arguments and return either hex or base-64 encoded strings */ const SHA1 = { b64_hmac_sha1: function (key, data) { return binb2b64(core_hmac_sha1(key, data)); }, b64_sha1: function (s) { return binb2b64(core_sha1(str2binb(s), s.length * 8)); }, binb2str: binb2str, core_hmac_sha1: core_hmac_sha1, str_hmac_sha1: function (key, data) { return binb2str(core_hmac_sha1(key, data)); }, str_sha1: function (s) { return binb2str(core_sha1(str2binb(s), s.length * 8)); } }; class SASLSHA1 extends SASLMechanism { /** PrivateConstructor: SASLSHA1 * SASL SCRAM SHA 1 authentication. */ constructor() { let mechname = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'SCRAM-SHA-1'; let isClientFirst = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; let priority = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 60; super(mechname, isClientFirst, priority); } test(connection) { // eslint-disable-line class-methods-use-this return connection.authcid !== null; } onChallenge(connection, challenge) { // eslint-disable-line class-methods-use-this let nonce, salt, iter, Hi, U, U_old, i, k; let responseText = "c=biws,"; let authMessage = `${connection._sasl_data["client-first-message-bare"]},${challenge},`; const cnonce = connection._sasl_data.cnonce; const attribMatch = /([a-z]+)=([^,]+)(,|$)/; while (challenge.match(attribMatch)) { const matches = challenge.match(attribMatch); challenge = challenge.replace(matches[0], ""); switch (matches[1]) { case "r": nonce = matches[2]; break; case "s": salt = matches[2]; break; case "i": iter = matches[2]; break; } } if (nonce.slice(0, cnonce.length) !== cnonce) { connection._sasl_data = {}; return connection._sasl_failure_cb(); } responseText += "r=" + nonce; authMessage += responseText; salt = atob(salt); salt += "\x00\x00\x00\x01"; const pass = utils.utf16to8(connection.pass); Hi = U_old = SHA1.core_hmac_sha1(pass, salt); for (i = 1; i < iter; i++) { U = SHA1.core_hmac_sha1(pass, SHA1.binb2str(U_old)); for (k = 0; k < 5; k++) { Hi[k] ^= U[k]; } U_old = U; } Hi = SHA1.binb2str(Hi); const clientKey = SHA1.core_hmac_sha1(Hi, "Client Key"); const serverKey = SHA1.str_hmac_sha1(Hi, "Server Key"); const clientSignature = SHA1.core_hmac_sha1(SHA1.str_sha1(SHA1.binb2str(clientKey)), authMessage); connection._sasl_data["server-signature"] = SHA1.b64_hmac_sha1(serverKey, authMessage); for (k = 0; k < 5; k++) { clientKey[k] ^= clientSignature[k]; } responseText += ",p=" + btoa(SHA1.binb2str(clientKey)); return responseText; } clientChallenge(connection, test_cnonce) { // eslint-disable-line class-methods-use-this const cnonce = test_cnonce || MD5.hexdigest("" + Math.random() * 1234567890); let auth_str = "n=" + utils.utf16to8(connection.authcid); auth_str += ",r="; auth_str += cnonce; connection._sasl_data.cnonce = cnonce; connection._sasl_data["client-first-message-bare"] = auth_str; auth_str = "n,," + auth_str; return auth_str; } } class SASLXOAuth2 extends SASLMechanism { /** PrivateConstructor: SASLXOAuth2 * SASL X-OAuth2 authentication. */ constructor() { let mechname = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'X-OAUTH2'; let isClientFirst = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; let priority = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 30; super(mechname, isClientFirst, priority); } test(connection) { // eslint-disable-line class-methods-use-this return connection.pass !== null; } onChallenge(connection) { // eslint-disable-line class-methods-use-this let auth_str = '\u0000'; if (connection.authcid !== null) { auth_str = auth_str + connection.authzid; } auth_str = auth_str + "\u0000"; auth_str = auth_str + connection.pass; return utils.utf16to8(auth_str); } } /** * Implementation of atob() according to the HTML and Infra specs, except that * instead of throwing INVALID_CHARACTER_ERR we return null. */ function atob$2(data) { if (arguments.length === 0) { throw new TypeError("1 argument required, but only 0 present."); } // Web IDL requires DOMStrings to just be converted using ECMAScript // ToString, which in our case amounts to using a template literal. data = `${data}`; // "Remove all ASCII whitespace from data." data = data.replace(/[ \t\n\f\r]/g, ""); // "If data's length divides by 4 leaving no remainder, then: if data ends // with one or two U+003D (=) code points, then remove them from data." if (data.length % 4 === 0) { data = data.replace(/==?$/, ""); } // "If data's length divides by 4 leaving a remainder of 1, then return // failure." // // "If data contains a code point that is not one of // // U+002B (+) // U+002F (/) // ASCII alphanumeric // // then return failure." if (data.length % 4 === 1 || /[^+/0-9A-Za-z]/.test(data)) { return null; } // "Let output be an empty byte sequence." let output = ""; // "Let buffer be an empty buffer that can have bits appended to it." // // We append bits via left-shift and or. accumulatedBits is used to track // when we've gotten to 24 bits. let buffer = 0; let accumulatedBits = 0; // "Let position be a position variable for data, initially pointing at the // start of data." // // "While position does not point past the end of data:" for (let i = 0; i < data.length; i++) { // "Find the code point pointed to by position in the second column of // Table 1: The Base 64 Alphabet of RFC 4648. Let n be the number given in // the first cell of the same row. // // "Append to buffer the six bits corresponding to n, most significant bit // first." // // atobLookup() implements the table from RFC 4648. buffer <<= 6; buffer |= atobLookup(data[i]); accumulatedBits += 6; // "If buffer has accumulated 24 bits, interpret them as three 8-bit // big-endian numbers. Append three bytes with values equal to those // numbers to output, in the same order, and then empty buffer." if (accumulatedBits === 24) { output += String.fromCharCode((buffer & 0xff0000) >> 16); output += String.fromCharCode((buffer & 0xff00) >> 8); output += String.fromCharCode(buffer & 0xff); buffer = accumulatedBits = 0; } // "Advance position by 1." } // "If buffer is not empty, it contains either 12 or 18 bits. If it contains // 12 bits, then discard the last four and interpret the remaining eight as // an 8-bit big-endian number. If it contains 18 bits, then discard the last // two and interpret the remaining 16 as two 8-bit big-endian numbers. Append // the one or two bytes with values equal to those one or two numbers to // output, in the same order." if (accumulatedBits === 12) { buffer >>= 4; output += String.fromCharCode(buffer); } else if (accumulatedBits === 18) { buffer >>= 2; output += String.fromCharCode((buffer & 0xff00) >> 8); output += String.fromCharCode(buffer & 0xff); } // "Return output." return output; } /** * A lookup table for atob(), which converts an ASCII character to the * corresponding six-bit number. */ const keystr$1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; function atobLookup(chr) { const index = keystr$1.indexOf(chr); // Throw exception if character is not in the lookup string; should not be hit in tests return index < 0 ? undefined : index; } var atob_1 = atob$2; /** * btoa() as defined by the HTML and Infra specs, which mostly just references * RFC 4648. */ function btoa$2(s) { if (arguments.length === 0) { throw new TypeError("1 argument required, but only 0 present."); } let i; // String conversion as required by Web IDL. s = `${s}`; // "The btoa() method must throw an "InvalidCharacterError" DOMException if // data contains any character whose code point is greater than U+00FF." for (i = 0; i < s.length; i++) { if (s.charCodeAt(i) > 255) { return null; } } let out = ""; for (i = 0; i < s.length; i += 3) { const groupsOfSix = [undefined, undefined, undefined, undefined]; groupsOfSix[0] = s.charCodeAt(i) >> 2; groupsOfSix[1] = (s.charCodeAt(i) & 0x03) << 4; if (s.length > i + 1) { groupsOfSix[1] |= s.charCodeAt(i + 1) >> 4; groupsOfSix[2] = (s.charCodeAt(i + 1) & 0x0f) << 2; } if (s.length > i + 2) { groupsOfSix[2] |= s.charCodeAt(i + 2) >> 6; groupsOfSix[3] = s.charCodeAt(i + 2) & 0x3f; } for (let j = 0; j < groupsOfSix.length; j++) { if (typeof groupsOfSix[j] === "undefined") { out += "="; } else { out += btoaLookup(groupsOfSix[j]); } } } return out; } /** * Lookup table for btoa(), which converts a six-bit number into the * corresponding ASCII character. */ const keystr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; function btoaLookup(index) { if (index >= 0 && index < 64) { return keystr[index]; } // Throw INVALID_CHARACTER_ERR exception here -- won't be hit in the tests. return undefined; } var btoa_1 = btoa$2; const atob$1 = atob_1; const btoa$1 = btoa_1; var abab = { atob: atob$1, btoa: btoa$1 }; /* This program is distributed under the terms of the MIT license. Please see the LICENSE file for details. Copyright 2006-2018, OGG, LLC */ /** Function: $build * Create a Strophe.Builder. * This is an alias for 'new Strophe.Builder(name, attrs)'. * * Parameters: * (String) name - The root element name. * (Object) attrs - The attributes for the root element in object notation. * * Returns: * A new Strophe.Builder object. */ function $build(name, attrs) { return new Strophe.Builder(name, attrs); } /** Function: $msg * Create a Strophe.Builder with a element as the root. * * Parameters: * (Object) attrs - The element attributes in object notation. * * Returns: * A new Strophe.Builder object. */ function $msg(attrs) { return new Strophe.Builder("message", attrs); } /** Function: $iq * Create a Strophe.Builder with an element as the root. * * Parameters: * (Object) attrs - The element attributes in object notation. * * Returns: * A new Strophe.Builder object. */ function $iq(attrs) { return new Strophe.Builder("iq", attrs); } /** Function: $pres * Create a Strophe.Builder with a element as the root. * * Parameters: * (Object) attrs - The element attributes in object notation. * * Returns: * A new Strophe.Builder object. */ function $pres(attrs) { return new Strophe.Builder("presence", attrs); } /** Class: Strophe * An object container for all Strophe library functions. * * This class is just a container for all the objects and constants * used in the library. It is not meant to be instantiated, but to * provide a namespace for library objects, constants, and functions. */ const Strophe = { /** Constant: VERSION */ VERSION: "1.5.0", /** Constants: XMPP Namespace Constants * Common namespace constants from the XMPP RFCs and XEPs. * * NS.HTTPBIND - HTTP BIND namespace from XEP 124. * NS.BOSH - BOSH namespace from XEP 206. * NS.CLIENT - Main XMPP client namespace. * NS.AUTH - Legacy authentication namespace. * NS.ROSTER - Roster operations namespace. * NS.PROFILE - Profile namespace. * NS.DISCO_INFO - Service discovery info namespace from XEP 30. * NS.DISCO_ITEMS - Service discovery items namespace from XEP 30. * NS.MUC - Multi-User Chat namespace from XEP 45. * NS.SASL - XMPP SASL namespace from RFC 3920. * NS.STREAM - XMPP Streams namespace from RFC 3920. * NS.BIND - XMPP Binding namespace from RFC 3920 and RFC 6120. * NS.SESSION - XMPP Session namespace from RFC 3920. * NS.XHTML_IM - XHTML-IM namespace from XEP 71. * NS.XHTML - XHTML body namespace from XEP 71. */ NS: { HTTPBIND: "http://jabber.org/protocol/httpbind", BOSH: "urn:xmpp:xbosh", CLIENT: "jabber:client", AUTH: "jabber:iq:auth", ROSTER: "jabber:iq:roster", PROFILE: "jabber:iq:profile", DISCO_INFO: "http://jabber.org/protocol/disco#info", DISCO_ITEMS: "http://jabber.org/protocol/disco#items", MUC: "http://jabber.org/protocol/muc", SASL: "urn:ietf:params:xml:ns:xmpp-sasl", STREAM: "http://etherx.jabber.org/streams", FRAMING: "urn:ietf:params:xml:ns:xmpp-framing", BIND: "urn:ietf:params:xml:ns:xmpp-bind", SESSION: "urn:ietf:params:xml:ns:xmpp-session", VERSION: "jabber:iq:version", STANZAS: "urn:ietf:params:xml:ns:xmpp-stanzas", XHTML_IM: "http://jabber.org/protocol/xhtml-im", XHTML: "http://www.w3.org/1999/xhtml" }, /** Constants: XHTML_IM Namespace * contains allowed tags, tag attributes, and css properties. * Used in the createHtml function to filter incoming html into the allowed XHTML-IM subset. * See http://xmpp.org/extensions/xep-0071.html#profile-summary for the list of recommended * allowed tags and their attributes. */ XHTML: { tags: ['a', 'blockquote', 'br', 'cite', 'em', 'img', 'li', 'ol', 'p', 'span', 'strong', 'ul', 'body'], attributes: { 'a': ['href'], 'blockquote': ['style'], 'br': [], 'cite': ['style'], 'em': [], 'img': ['src', 'alt', 'style', 'height', 'width'], 'li': ['style'], 'ol': ['style'], 'p': ['style'], 'span': ['style'], 'strong': [], 'ul': ['style'], 'body': [] }, css: ['background-color', 'color', 'font-family', 'font-size', 'font-style', 'font-weight', 'margin-left', 'margin-right', 'text-align', 'text-decoration'], /** Function: XHTML.validTag * * Utility method to determine whether a tag is allowed * in the XHTML_IM namespace. * * XHTML tag names are case sensitive and must be lower case. */ validTag(tag) { for (let i = 0; i < Strophe.XHTML.tags.length; i++) { if (tag === Strophe.XHTML.tags[i]) { return true; } } return false; }, /** Function: XHTML.validAttribute * * Utility method to determine whether an attribute is allowed * as recommended per XEP-0071 * * XHTML attribute names are case sensitive and must be lower case. */ validAttribute(tag, attribute) { if (typeof Strophe.XHTML.attributes[tag] !== 'undefined' && Strophe.XHTML.attributes[tag].length > 0) { for (let i = 0; i < Strophe.XHTML.attributes[tag].length; i++) { if (attribute === Strophe.XHTML.attributes[tag][i]) { return true; } } } return false; }, validCSS(style) { for (let i = 0; i < Strophe.XHTML.css.length; i++) { if (style === Strophe.XHTML.css[i]) { return true; } } return false; } }, /** Constants: Connection Status Constants * Connection status constants for use by the connection handler * callback. * * Status.ERROR - An error has occurred * Status.CONNECTING - The connection is currently being made * Status.CONNFAIL - The connection attempt failed * Status.AUTHENTICATING - The connection is authenticating * Status.AUTHFAIL - The authentication attempt failed * Status.CONNECTED - The connection has succeeded * Status.DISCONNECTED - The connection has been terminated * Status.DISCONNECTING - The connection is currently being terminated * Status.ATTACHED - The connection has been attached * Status.REDIRECT - The connection has been redirected * Status.CONNTIMEOUT - The connection has timed out */ Status: { ERROR: 0, CONNECTING: 1, CONNFAIL: 2, AUTHENTICATING: 3, AUTHFAIL: 4, CONNECTED: 5, DISCONNECTED: 6, DISCONNECTING: 7, ATTACHED: 8, REDIRECT: 9, CONNTIMEOUT: 10, BINDREQUIRED: 11, ATTACHFAIL: 12 }, ErrorCondition: { BAD_FORMAT: "bad-format", CONFLICT: "conflict", MISSING_JID_NODE: "x-strophe-bad-non-anon-jid", NO_AUTH_MECH: "no-auth-mech", UNKNOWN_REASON: "unknown" }, /** Constants: Log Level Constants * Logging level indicators. * * LogLevel.DEBUG - Debug output * LogLevel.INFO - Informational output * LogLevel.WARN - Warnings * LogLevel.ERROR - Errors * LogLevel.FATAL - Fatal errors */ LogLevel: { DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, FATAL: 4 }, /** PrivateConstants: DOM Element Type Constants * DOM element types. * * ElementType.NORMAL - Normal element. * ElementType.TEXT - Text data element. * ElementType.FRAGMENT - XHTML fragment element. */ ElementType: { NORMAL: 1, TEXT: 3, CDATA: 4, FRAGMENT: 11 }, /** PrivateConstants: Timeout Values * Timeout values for error states. These values are in seconds. * These should not be changed unless you know exactly what you are * doing. * * TIMEOUT - Timeout multiplier. A waiting request will be considered * failed after Math.floor(TIMEOUT * wait) seconds have elapsed. * This defaults to 1.1, and with default wait, 66 seconds. * SECONDARY_TIMEOUT - Secondary timeout multiplier. In cases where * Strophe can detect early failure, it will consider the request * failed if it doesn't return after * Math.floor(SECONDARY_TIMEOUT * wait) seconds have elapsed. * This defaults to 0.1, and with default wait, 6 seconds. */ TIMEOUT: 1.1, SECONDARY_TIMEOUT: 0.1, /** Function: addNamespace * This function is used to extend the current namespaces in * Strophe.NS. It takes a key and a value with the key being the * name of the new namespace, with its actual value. * For example: * Strophe.addNamespace('PUBSUB', "http://jabber.org/protocol/pubsub"); * * Parameters: * (String) name - The name under which the namespace will be * referenced under Strophe.NS * (String) value - The actual namespace. */ addNamespace(name, value) { Strophe.NS[name] = value; }, /** Function: forEachChild * Map a function over some or all child elements of a given element. * * This is a small convenience function for mapping a function over * some or all of the children of an element. If elemName is null, all * children will be passed to the function, otherwise only children * whose tag names match elemName will be passed. * * Parameters: * (XMLElement) elem - The element to operate on. * (String) elemName - The child element tag name filter. * (Function) func - The function to apply to each child. This * function should take a single argument, a DOM element. */ forEachChild(elem, elemName, func) { for (let i = 0; i < elem.childNodes.length; i++) { const childNode = elem.childNodes[i]; if (childNode.nodeType === Strophe.ElementType.NORMAL && (!elemName || this.isTagEqual(childNode, elemName))) { func(childNode); } } }, /** Function: isTagEqual * Compare an element's tag name with a string. * * This function is case sensitive. * * Parameters: * (XMLElement) el - A DOM element. * (String) name - The element name. * * Returns: * true if the element's tag name matches _el_, and false * otherwise. */ isTagEqual(el, name) { return el.tagName === name; }, /** PrivateVariable: _xmlGenerator * _Private_ variable that caches a DOM document to * generate elements. */ _xmlGenerator: null, /** Function: xmlGenerator * Get the DOM document to generate elements. * * Returns: * The currently used DOM document. */ xmlGenerator() { if (!Strophe._xmlGenerator) { Strophe._xmlGenerator = getDummyXMLDOMDocument(); } return Strophe._xmlGenerator; }, /** Function: xmlElement * Create an XML DOM element. * * This function creates an XML DOM element correctly across all * implementations. Note that these are not HTML DOM elements, which * aren't appropriate for XMPP stanzas. * * Parameters: * (String) name - The name for the element. * (Array|Object) attrs - An optional array or object containing * key/value pairs to use as element attributes. The object should * be in the format {'key': 'value'} or {key: 'value'}. The array * should have the format [['key1', 'value1'], ['key2', 'value2']]. * (String) text - The text child data for the element. * * Returns: * A new XML DOM element. */ xmlElement(name) { if (!name) { return null; } const node = Strophe.xmlGenerator().createElement(name); // FIXME: this should throw errors if args are the wrong type or // there are more than two optional args for (let a = 1; a < arguments.length; a++) { const arg = arguments[a]; if (!arg) { continue; } if (typeof arg === "string" || typeof arg === "number") { node.appendChild(Strophe.xmlTextNode(arg)); } else if (typeof arg === "object" && typeof arg.sort === "function") { for (let i = 0; i < arg.length; i++) { const attr = arg[i]; if (typeof attr === "object" && typeof attr.sort === "function" && attr[1] !== undefined && attr[1] !== null) { node.setAttribute(attr[0], attr[1]); } } } else if (typeof arg === "object") { for (const k in arg) { if (Object.prototype.hasOwnProperty.call(arg, k) && arg[k] !== undefined && arg[k] !== null) { node.setAttribute(k, arg[k]); } } } } return node; }, /* Function: xmlescape * Excapes invalid xml characters. * * Parameters: * (String) text - text to escape. * * Returns: * Escaped text. */ xmlescape(text) { text = text.replace(/\&/g, "&"); text = text.replace(//g, ">"); text = text.replace(/'/g, "'"); text = text.replace(/"/g, """); return text; }, /* Function: xmlunescape * Unexcapes invalid xml characters. * * Parameters: * (String) text - text to unescape. * * Returns: * Unescaped text. */ xmlunescape(text) { text = text.replace(/\&/g, "&"); text = text.replace(/</g, "<"); text = text.replace(/>/g, ">"); text = text.replace(/'/g, "'"); text = text.replace(/"/g, "\""); return text; }, /** Function: xmlTextNode * Creates an XML DOM text node. * * Provides a cross implementation version of document.createTextNode. * * Parameters: * (String) text - The content of the text node. * * Returns: * A new XML DOM text node. */ xmlTextNode(text) { return Strophe.xmlGenerator().createTextNode(text); }, /** Function: xmlHtmlNode * Creates an XML DOM html node. * * Parameters: * (String) html - The content of the html node. * * Returns: * A new XML DOM text node. */ xmlHtmlNode(html) { let node; //ensure text is escaped if (DOMParser) { const parser = new DOMParser(); node = parser.parseFromString(html, "text/xml"); } else { node = new ActiveXObject("Microsoft.XMLDOM"); node.async = "false"; node.loadXML(html); } return node; }, /** Function: getText * Get the concatenation of all text children of an element. * * Parameters: * (XMLElement) elem - A DOM element. * * Returns: * A String with the concatenated text of all text element children. */ getText(elem) { if (!elem) { return null; } let str = ""; if (elem.childNodes.length === 0 && elem.nodeType === Strophe.ElementType.TEXT) { str += elem.nodeValue; } for (let i = 0; i < elem.childNodes.length; i++) { if (elem.childNodes[i].nodeType === Strophe.ElementType.TEXT) { str += elem.childNodes[i].nodeValue; } } return Strophe.xmlescape(str); }, /** Function: copyElement * Copy an XML DOM element. * * This function copies a DOM element and all its descendants and returns * the new copy. * * Parameters: * (XMLElement) elem - A DOM element. * * Returns: * A new, copied DOM element tree. */ copyElement(elem) { let el; if (elem.nodeType === Strophe.ElementType.NORMAL) { el = Strophe.xmlElement(elem.tagName); for (let i = 0; i < elem.attributes.length; i++) { el.setAttribute(elem.attributes[i].nodeName, elem.attributes[i].value); } for (let i = 0; i < elem.childNodes.length; i++) { el.appendChild(Strophe.copyElement(elem.childNodes[i])); } } else if (elem.nodeType === Strophe.ElementType.TEXT) { el = Strophe.xmlGenerator().createTextNode(elem.nodeValue); } return el; }, /** Function: createHtml * Copy an HTML DOM element into an XML DOM. * * This function copies a DOM element and all its descendants and returns * the new copy. * * Parameters: * (HTMLElement) elem - A DOM element. * * Returns: * A new, copied DOM element tree. */ createHtml(elem) { let el; if (elem.nodeType === Strophe.ElementType.NORMAL) { const tag = elem.nodeName.toLowerCase(); // XHTML tags must be lower case. if (Strophe.XHTML.validTag(tag)) { try { el = Strophe.xmlElement(tag); for (let i = 0; i < Strophe.XHTML.attributes[tag].length; i++) { const attribute = Strophe.XHTML.attributes[tag][i]; let value = elem.getAttribute(attribute); if (typeof value === 'undefined' || value === null || value === '' || value === false || value === 0) { continue; } if (attribute === 'style' && typeof value === 'object' && typeof value.cssText !== 'undefined') { value = value.cssText; // we're dealing with IE, need to get CSS out } // filter out invalid css styles if (attribute === 'style') { const css = []; const cssAttrs = value.split(';'); for (let j = 0; j < cssAttrs.length; j++) { const attr = cssAttrs[j].split(':'); const cssName = attr[0].replace(/^\s*/, "").replace(/\s*$/, "").toLowerCase(); if (Strophe.XHTML.validCSS(cssName)) { const cssValue = attr[1].replace(/^\s*/, "").replace(/\s*$/, ""); css.push(cssName + ': ' + cssValue); } } if (css.length > 0) { value = css.join('; '); el.setAttribute(attribute, value); } } else { el.setAttribute(attribute, value); } } for (let i = 0; i < elem.childNodes.length; i++) { el.appendChild(Strophe.createHtml(elem.childNodes[i])); } } catch (e) { // invalid elements el = Strophe.xmlTextNode(''); } } else { el = Strophe.xmlGenerator().createDocumentFragment(); for (let i = 0; i < elem.childNodes.length; i++) { el.appendChild(Strophe.createHtml(elem.childNodes[i])); } } } else if (elem.nodeType === Strophe.ElementType.FRAGMENT) { el = Strophe.xmlGenerator().createDocumentFragment(); for (let i = 0; i < elem.childNodes.length; i++) { el.appendChild(Strophe.createHtml(elem.childNodes[i])); } } else if (elem.nodeType === Strophe.ElementType.TEXT) { el = Strophe.xmlTextNode(elem.nodeValue); } return el; }, /** Function: escapeNode * Escape the node part (also called local part) of a JID. * * Parameters: * (String) node - A node (or local part). * * Returns: * An escaped node (or local part). */ escapeNode(node) { if (typeof node !== "string") { return node; } return node.replace(/^\s+|\s+$/g, '').replace(/\\/g, "\\5c").replace(/ /g, "\\20").replace(/\"/g, "\\22").replace(/\&/g, "\\26").replace(/\'/g, "\\27").replace(/\//g, "\\2f").replace(/:/g, "\\3a").replace(//g, "\\3e").replace(/@/g, "\\40"); }, /** Function: unescapeNode * Unescape a node part (also called local part) of a JID. * * Parameters: * (String) node - A node (or local part). * * Returns: * An unescaped node (or local part). */ unescapeNode(node) { if (typeof node !== "string") { return node; } return node.replace(/\\20/g, " ").replace(/\\22/g, '"').replace(/\\26/g, "&").replace(/\\27/g, "'").replace(/\\2f/g, "/").replace(/\\3a/g, ":").replace(/\\3c/g, "<").replace(/\\3e/g, ">").replace(/\\40/g, "@").replace(/\\5c/g, "\\"); }, /** Function: getNodeFromJid * Get the node portion of a JID String. * * Parameters: * (String) jid - A JID. * * Returns: * A String containing the node. */ getNodeFromJid(jid) { if (jid.indexOf("@") < 0) { return null; } return jid.split("@")[0]; }, /** Function: getDomainFromJid * Get the domain portion of a JID String. * * Parameters: * (String) jid - A JID. * * Returns: * A String containing the domain. */ getDomainFromJid(jid) { const bare = Strophe.getBareJidFromJid(jid); if (bare.indexOf("@") < 0) { return bare; } else { const parts = bare.split("@"); parts.splice(0, 1); return parts.join('@'); } }, /** Function: getResourceFromJid * Get the resource portion of a JID String. * * Parameters: * (String) jid - A JID. * * Returns: * A String containing the resource. */ getResourceFromJid(jid) { if (!jid) { return null; } const s = jid.split("/"); if (s.length < 2) { return null; } s.splice(0, 1); return s.join('/'); }, /** Function: getBareJidFromJid * Get the bare JID from a JID String. * * Parameters: * (String) jid - A JID. * * Returns: * A String containing the bare JID. */ getBareJidFromJid(jid) { return jid ? jid.split("/")[0] : null; }, /** PrivateFunction: _handleError * _Private_ function that properly logs an error to the console */ _handleError(e) { if (typeof e.stack !== "undefined") { Strophe.fatal(e.stack); } if (e.sourceURL) { Strophe.fatal("error: " + this.handler + " " + e.sourceURL + ":" + e.line + " - " + e.name + ": " + e.message); } else if (e.fileName) { Strophe.fatal("error: " + this.handler + " " + e.fileName + ":" + e.lineNumber + " - " + e.name + ": " + e.message); } else { Strophe.fatal("error: " + e.message); } }, /** Function: log * User overrideable logging function. * * This function is called whenever the Strophe library calls any * of the logging functions. The default implementation of this * function logs only fatal errors. If client code wishes to handle the logging * messages, it should override this with * > Strophe.log = function (level, msg) { * > (user code here) * > }; * * Please note that data sent and received over the wire is logged * via Strophe.Connection.rawInput() and Strophe.Connection.rawOutput(). * * The different levels and their meanings are * * DEBUG - Messages useful for debugging purposes. * INFO - Informational messages. This is mostly information like * 'disconnect was called' or 'SASL auth succeeded'. * WARN - Warnings about potential problems. This is mostly used * to report transient connection errors like request timeouts. * ERROR - Some error occurred. * FATAL - A non-recoverable fatal error occurred. * * Parameters: * (Integer) level - The log level of the log message. This will * be one of the values in Strophe.LogLevel. * (String) msg - The log message. */ log(level, msg) { if (level === this.LogLevel.FATAL) { var _console; (_console = console) === null || _console === void 0 ? void 0 : _console.error(msg); } }, /** Function: debug * Log a message at the Strophe.LogLevel.DEBUG level. * * Parameters: * (String) msg - The log message. */ debug(msg) { this.log(this.LogLevel.DEBUG, msg); }, /** Function: info * Log a message at the Strophe.LogLevel.INFO level. * * Parameters: * (String) msg - The log message. */ info(msg) { this.log(this.LogLevel.INFO, msg); }, /** Function: warn * Log a message at the Strophe.LogLevel.WARN level. * * Parameters: * (String) msg - The log message. */ warn(msg) { this.log(this.LogLevel.WARN, msg); }, /** Function: error * Log a message at the Strophe.LogLevel.ERROR level. * * Parameters: * (String) msg - The log message. */ error(msg) { this.log(this.LogLevel.ERROR, msg); }, /** Function: fatal * Log a message at the Strophe.LogLevel.FATAL level. * * Parameters: * (String) msg - The log message. */ fatal(msg) { this.log(this.LogLevel.FATAL, msg); }, /** Function: serialize * Render a DOM element and all descendants to a String. * * Parameters: * (XMLElement) elem - A DOM element. * * Returns: * The serialized element tree as a String. */ serialize(elem) { if (!elem) { return null; } if (typeof elem.tree === "function") { elem = elem.tree(); } const names = [...Array(elem.attributes.length).keys()].map(i => elem.attributes[i].nodeName); names.sort(); let result = names.reduce((a, n) => `${a} ${n}="${Strophe.xmlescape(elem.attributes.getNamedItem(n).value)}"`, `<${elem.nodeName}`); if (elem.childNodes.length > 0) { result += ">"; for (let i = 0; i < elem.childNodes.length; i++) { const child = elem.childNodes[i]; switch (child.nodeType) { case Strophe.ElementType.NORMAL: // normal element, so recurse result += Strophe.serialize(child); break; case Strophe.ElementType.TEXT: // text element to escape values result += Strophe.xmlescape(child.nodeValue); break; case Strophe.ElementType.CDATA: // cdata section so don't escape values result += ""; } } result += ""; } else { result += "/>"; } return result; }, /** PrivateVariable: _requestId * _Private_ variable that keeps track of the request ids for * connections. */ _requestId: 0, /** PrivateVariable: Strophe.connectionPlugins * _Private_ variable Used to store plugin names that need * initialization on Strophe.Connection construction. */ _connectionPlugins: {}, /** Function: addConnectionPlugin * Extends the Strophe.Connection object with the given plugin. * * Parameters: * (String) name - The name of the extension. * (Object) ptype - The plugin's prototype. */ addConnectionPlugin(name, ptype) { Strophe._connectionPlugins[name] = ptype; } }; /** Class: Strophe.Builder * XML DOM builder. * * This object provides an interface similar to JQuery but for building * DOM elements easily and rapidly. All the functions except for toString() * and tree() return the object, so calls can be chained. Here's an * example using the $iq() builder helper. * > $iq({to: 'you', from: 'me', type: 'get', id: '1'}) * > .c('query', {xmlns: 'strophe:example'}) * > .c('example') * > .toString() * * The above generates this XML fragment * > * > * > * > * > * The corresponding DOM manipulations to get a similar fragment would be * a lot more tedious and probably involve several helper variables. * * Since adding children makes new operations operate on the child, up() * is provided to traverse up the tree. To add two children, do * > builder.c('child1', ...).up().c('child2', ...) * The next operation on the Builder will be relative to the second child. */ /** Constructor: Strophe.Builder * Create a Strophe.Builder object. * * The attributes should be passed in object notation. For example * > let b = new Builder('message', {to: 'you', from: 'me'}); * or * > let b = new Builder('messsage', {'xml:lang': 'en'}); * * Parameters: * (String) name - The name of the root element. * (Object) attrs - The attributes for the root element in object notation. * * Returns: * A new Strophe.Builder. */ Strophe.Builder = class Builder { constructor(name, attrs) { // Set correct namespace for jabber:client elements if (name === "presence" || name === "message" || name === "iq") { if (attrs && !attrs.xmlns) { attrs.xmlns = Strophe.NS.CLIENT; } else if (!attrs) { attrs = { xmlns: Strophe.NS.CLIENT }; } } // Holds the tree being built. this.nodeTree = Strophe.xmlElement(name, attrs); // Points to the current operation node. this.node = this.nodeTree; } /** Function: tree * Return the DOM tree. * * This function returns the current DOM tree as an element object. This * is suitable for passing to functions like Strophe.Connection.send(). * * Returns: * The DOM tree as a element object. */ tree() { return this.nodeTree; } /** Function: toString * Serialize the DOM tree to a String. * * This function returns a string serialization of the current DOM * tree. It is often used internally to pass data to a * Strophe.Request object. * * Returns: * The serialized DOM tree in a String. */ toString() { return Strophe.serialize(this.nodeTree); } /** Function: up * Make the current parent element the new current element. * * This function is often used after c() to traverse back up the tree. * For example, to add two children to the same element * > builder.c('child1', {}).up().c('child2', {}); * * Returns: * The Stophe.Builder object. */ up() { this.node = this.node.parentNode; return this; } /** Function: root * Make the root element the new current element. * * When at a deeply nested element in the tree, this function can be used * to jump back to the root of the tree, instead of having to repeatedly * call up(). * * Returns: * The Stophe.Builder object. */ root() { this.node = this.nodeTree; return this; } /** Function: attrs * Add or modify attributes of the current element. * * The attributes should be passed in object notation. This function * does not move the current element pointer. * * Parameters: * (Object) moreattrs - The attributes to add/modify in object notation. * * Returns: * The Strophe.Builder object. */ attrs(moreattrs) { for (const k in moreattrs) { if (Object.prototype.hasOwnProperty.call(moreattrs, k)) { if (moreattrs[k] === undefined) { this.node.removeAttribute(k); } else { this.node.setAttribute(k, moreattrs[k]); } } } return this; } /** Function: c * Add a child to the current element and make it the new current * element. * * This function moves the current element pointer to the child, * unless text is provided. If you need to add another child, it * is necessary to use up() to go back to the parent in the tree. * * Parameters: * (String) name - The name of the child. * (Object) attrs - The attributes of the child in object notation. * (String) text - The text to add to the child. * * Returns: * The Strophe.Builder object. */ c(name, attrs, text) { const child = Strophe.xmlElement(name, attrs, text); this.node.appendChild(child); if (typeof text !== "string" && typeof text !== "number") { this.node = child; } return this; } /** Function: cnode * Add a child to the current element and make it the new current * element. * * This function is the same as c() except that instead of using a * name and an attributes object to create the child it uses an * existing DOM element object. * * Parameters: * (XMLElement) elem - A DOM element. * * Returns: * The Strophe.Builder object. */ cnode(elem) { let impNode; const xmlGen = Strophe.xmlGenerator(); try { impNode = xmlGen.importNode !== undefined; } catch (e) { impNode = false; } const newElem = impNode ? xmlGen.importNode(elem, true) : Strophe.copyElement(elem); this.node.appendChild(newElem); this.node = newElem; return this; } /** Function: t * Add a child text element. * * This *does not* make the child the new current element since there * are no children of text elements. * * Parameters: * (String) text - The text data to append to the current element. * * Returns: * The Strophe.Builder object. */ t(text) { const child = Strophe.xmlTextNode(text); this.node.appendChild(child); return this; } /** Function: h * Replace current element contents with the HTML passed in. * * This *does not* make the child the new current element * * Parameters: * (String) html - The html to insert as contents of current element. * * Returns: * The Strophe.Builder object. */ h(html) { const fragment = Strophe.xmlGenerator().createElement('body'); // force the browser to try and fix any invalid HTML tags fragment.innerHTML = html; // copy cleaned html into an xml dom const xhtml = Strophe.createHtml(fragment); while (xhtml.childNodes.length > 0) { this.node.appendChild(xhtml.childNodes[0]); } return this; } }; /** PrivateClass: Strophe.Handler * _Private_ helper class for managing stanza handlers. * * A Strophe.Handler encapsulates a user provided callback function to be * executed when matching stanzas are received by the connection. * Handlers can be either one-off or persistant depending on their * return value. Returning true will cause a Handler to remain active, and * returning false will remove the Handler. * * Users will not use Strophe.Handler objects directly, but instead they * will use Strophe.Connection.addHandler() and * Strophe.Connection.deleteHandler(). */ /** PrivateConstructor: Strophe.Handler * Create and initialize a new Strophe.Handler. * * Parameters: * (Function) handler - A function to be executed when the handler is run. * (String) ns - The namespace to match. * (String) name - The element name to match. * (String) type - The element type to match. * (String) id - The element id attribute to match. * (String) from - The element from attribute to match. * (Object) options - Handler options * * Returns: * A new Strophe.Handler object. */ Strophe.Handler = function (handler, ns, name, type, id, from, options) { this.handler = handler; this.ns = ns; this.name = name; this.type = type; this.id = id; this.options = options || { 'matchBareFromJid': false, 'ignoreNamespaceFragment': false }; // BBB: Maintain backward compatibility with old `matchBare` option if (this.options.matchBare) { Strophe.warn('The "matchBare" option is deprecated, use "matchBareFromJid" instead.'); this.options.matchBareFromJid = this.options.matchBare; delete this.options.matchBare; } if (this.options.matchBareFromJid) { this.from = from ? Strophe.getBareJidFromJid(from) : null; } else { this.from = from; } // whether the handler is a user handler or a system handler this.user = true; }; Strophe.Handler.prototype = { /** PrivateFunction: getNamespace * Returns the XML namespace attribute on an element. * If `ignoreNamespaceFragment` was passed in for this handler, then the * URL fragment will be stripped. * * Parameters: * (XMLElement) elem - The XML element with the namespace. * * Returns: * The namespace, with optionally the fragment stripped. */ getNamespace(elem) { let elNamespace = elem.getAttribute("xmlns"); if (elNamespace && this.options.ignoreNamespaceFragment) { elNamespace = elNamespace.split('#')[0]; } return elNamespace; }, /** PrivateFunction: namespaceMatch * Tests if a stanza matches the namespace set for this Strophe.Handler. * * Parameters: * (XMLElement) elem - The XML element to test. * * Returns: * true if the stanza matches and false otherwise. */ namespaceMatch(elem) { let nsMatch = false; if (!this.ns) { return true; } else { Strophe.forEachChild(elem, null, elem => { if (this.getNamespace(elem) === this.ns) { nsMatch = true; } }); return nsMatch || this.getNamespace(elem) === this.ns; } }, /** PrivateFunction: isMatch * Tests if a stanza matches the Strophe.Handler. * * Parameters: * (XMLElement) elem - The XML element to test. * * Returns: * true if the stanza matches and false otherwise. */ isMatch(elem) { let from = elem.getAttribute('from'); if (this.options.matchBareFromJid) { from = Strophe.getBareJidFromJid(from); } const elem_type = elem.getAttribute("type"); if (this.namespaceMatch(elem) && (!this.name || Strophe.isTagEqual(elem, this.name)) && (!this.type || (Array.isArray(this.type) ? this.type.indexOf(elem_type) !== -1 : elem_type === this.type)) && (!this.id || elem.getAttribute("id") === this.id) && (!this.from || from === this.from)) { return true; } return false; }, /** PrivateFunction: run * Run the callback on a matching stanza. * * Parameters: * (XMLElement) elem - The DOM element that triggered the * Strophe.Handler. * * Returns: * A boolean indicating if the handler should remain active. */ run(elem) { let result = null; try { result = this.handler(elem); } catch (e) { Strophe._handleError(e); throw e; } return result; }, /** PrivateFunction: toString * Get a String representation of the Strophe.Handler object. * * Returns: * A String. */ toString() { return "{Handler: " + this.handler + "(" + this.name + "," + this.id + "," + this.ns + ")}"; } }; /** PrivateClass: Strophe.TimedHandler * _Private_ helper class for managing timed handlers. * * A Strophe.TimedHandler encapsulates a user provided callback that * should be called after a certain period of time or at regular * intervals. The return value of the callback determines whether the * Strophe.TimedHandler will continue to fire. * * Users will not use Strophe.TimedHandler objects directly, but instead * they will use Strophe.Connection.addTimedHandler() and * Strophe.Connection.deleteTimedHandler(). */ Strophe.TimedHandler = class TimedHandler { /** PrivateConstructor: Strophe.TimedHandler * Create and initialize a new Strophe.TimedHandler object. * * Parameters: * (Integer) period - The number of milliseconds to wait before the * handler is called. * (Function) handler - The callback to run when the handler fires. This * function should take no arguments. * * Returns: * A new Strophe.TimedHandler object. */ constructor(period, handler) { this.period = period; this.handler = handler; this.lastCalled = new Date().getTime(); this.user = true; } /** PrivateFunction: run * Run the callback for the Strophe.TimedHandler. * * Returns: * true if the Strophe.TimedHandler should be called again, and false * otherwise. */ run() { this.lastCalled = new Date().getTime(); return this.handler(); } /** PrivateFunction: reset * Reset the last called time for the Strophe.TimedHandler. */ reset() { this.lastCalled = new Date().getTime(); } /** PrivateFunction: toString * Get a string representation of the Strophe.TimedHandler object. * * Returns: * The string representation. */ toString() { return "{TimedHandler: " + this.handler + "(" + this.period + ")}"; } }; /** Class: Strophe.Connection * XMPP Connection manager. * * This class is the main part of Strophe. It manages a BOSH or websocket * connection to an XMPP server and dispatches events to the user callbacks * as data arrives. It supports SASL PLAIN, SASL SCRAM-SHA-1 * and legacy authentication. * * After creating a Strophe.Connection object, the user will typically * call connect() with a user supplied callback to handle connection level * events like authentication failure, disconnection, or connection * complete. * * The user will also have several event handlers defined by using * addHandler() and addTimedHandler(). These will allow the user code to * respond to interesting stanzas or do something periodically with the * connection. These handlers will be active once authentication is * finished. * * To send data to the connection, use send(). */ /** Constructor: Strophe.Connection * Create and initialize a Strophe.Connection object. * * The transport-protocol for this connection will be chosen automatically * based on the given service parameter. URLs starting with "ws://" or * "wss://" will use WebSockets, URLs starting with "http://", "https://" * or without a protocol will use BOSH. * * To make Strophe connect to the current host you can leave out the protocol * and host part and just pass the path, e.g. * * > let conn = new Strophe.Connection("/http-bind/"); * * Options common to both Websocket and BOSH: * ------------------------------------------ * * cookies: * * The *cookies* option allows you to pass in cookies to be added to the * document. These cookies will then be included in the BOSH XMLHttpRequest * or in the websocket connection. * * The passed in value must be a map of cookie names and string values. * * > { "myCookie": { * > "value": "1234", * > "domain": ".example.org", * > "path": "/", * > "expires": expirationDate * > } * > } * * Note that cookies can't be set in this way for other domains (i.e. cross-domain). * Those cookies need to be set under those domains, for example they can be * set server-side by making a XHR call to that domain to ask it to set any * necessary cookies. * * mechanisms: * * The *mechanisms* option allows you to specify the SASL mechanisms that this * instance of Strophe.Connection (and therefore your XMPP client) will * support. * * The value must be an array of objects with Strophe.SASLMechanism * prototypes. * * If nothing is specified, then the following mechanisms (and their * priorities) are registered: * * SCRAM-SHA-1 - 60 * PLAIN - 50 * OAUTHBEARER - 40 * X-OAUTH2 - 30 * ANONYMOUS - 20 * EXTERNAL - 10 * * explicitResourceBinding: * * If `explicitResourceBinding` is set to a truthy value, then the XMPP client * needs to explicitly call `Strophe.Connection.prototype.bind` once the XMPP * server has advertised the "urn:ietf:params:xml:ns:xmpp-bind" feature. * * Making this step explicit allows client authors to first finish other * stream related tasks, such as setting up an XEP-0198 Stream Management * session, before binding the JID resource for this session. * * WebSocket options: * ------------------ * * protocol: * * If you want to connect to the current host with a WebSocket connection you * can tell Strophe to use WebSockets through a "protocol" attribute in the * optional options parameter. Valid values are "ws" for WebSocket and "wss" * for Secure WebSocket. * So to connect to "wss://CURRENT_HOSTNAME/xmpp-websocket" you would call * * > let conn = new Strophe.Connection("/xmpp-websocket/", {protocol: "wss"}); * * Note that relative URLs _NOT_ starting with a "/" will also include the path * of the current site. * * Also because downgrading security is not permitted by browsers, when using * relative URLs both BOSH and WebSocket connections will use their secure * variants if the current connection to the site is also secure (https). * * worker: * * Set this option to URL from where the shared worker script should be loaded. * * To run the websocket connection inside a shared worker. * This allows you to share a single websocket-based connection between * multiple Strophe.Connection instances, for example one per browser tab. * * The script to use is the one in `src/shared-connection-worker.js`. * * BOSH options: * ------------- * * By adding "sync" to the options, you can control if requests will * be made synchronously or not. The default behaviour is asynchronous. * If you want to make requests synchronous, make "sync" evaluate to true. * > let conn = new Strophe.Connection("/http-bind/", {sync: true}); * * You can also toggle this on an already established connection. * > conn.options.sync = true; * * The *customHeaders* option can be used to provide custom HTTP headers to be * included in the XMLHttpRequests made. * * The *keepalive* option can be used to instruct Strophe to maintain the * current BOSH session across interruptions such as webpage reloads. * * It will do this by caching the sessions tokens in sessionStorage, and when * "restore" is called it will check whether there are cached tokens with * which it can resume an existing session. * * The *withCredentials* option should receive a Boolean value and is used to * indicate wether cookies should be included in ajax requests (by default * they're not). * Set this value to true if you are connecting to a BOSH service * and for some reason need to send cookies to it. * In order for this to work cross-domain, the server must also enable * credentials by setting the Access-Control-Allow-Credentials response header * to "true". For most usecases however this setting should be false (which * is the default). * Additionally, when using Access-Control-Allow-Credentials, the * Access-Control-Allow-Origin header can't be set to the wildcard "*", but * instead must be restricted to actual domains. * * The *contentType* option can be set to change the default Content-Type * of "text/xml; charset=utf-8", which can be useful to reduce the amount of * CORS preflight requests that are sent to the server. * * Parameters: * (String) service - The BOSH or WebSocket service URL. * (Object) options - A hash of configuration options * * Returns: * A new Strophe.Connection object. */ Strophe.Connection = class Connection { constructor(service, options) { // The service URL this.service = service; // Configuration options this.options = options || {}; this.setProtocol(); /* The connected JID. */ this.jid = ""; /* the JIDs domain */ this.domain = null; /* stream:features */ this.features = null; // SASL this._sasl_data = {}; this.do_bind = false; this.do_session = false; this.mechanisms = {}; // handler lists this.timedHandlers = []; this.handlers = []; this.removeTimeds = []; this.removeHandlers = []; this.addTimeds = []; this.addHandlers = []; this.protocolErrorHandlers = { 'HTTP': {}, 'websocket': {} }; this._idleTimeout = null; this._disconnectTimeout = null; this.authenticated = false; this.connected = false; this.disconnecting = false; this.do_authentication = true; this.paused = false; this.restored = false; this._data = []; this._uniqueId = 0; this._sasl_success_handler = null; this._sasl_failure_handler = null; this._sasl_challenge_handler = null; // Max retries before disconnecting this.maxRetries = 5; // Call onIdle callback every 1/10th of a second this._idleTimeout = setTimeout(() => this._onIdle(), 100); utils.addCookies(this.options.cookies); this.registerSASLMechanisms(this.options.mechanisms); // A client must always respond to incoming IQ "set" and "get" stanzas. // See https://datatracker.ietf.org/doc/html/rfc6120#section-8.2.3 // // This is a fallback handler which gets called when no other handler // was called for a received IQ "set" or "get". this.iqFallbackHandler = new Strophe.Handler(iq => this.send($iq({ type: 'error', id: iq.getAttribute('id') }).c('error', { 'type': 'cancel' }).c('service-unavailable', { 'xmlns': Strophe.NS.STANZAS })), null, 'iq', ['get', 'set']); // initialize plugins for (const k in Strophe._connectionPlugins) { if (Object.prototype.hasOwnProperty.call(Strophe._connectionPlugins, k)) { const F = function () {}; F.prototype = Strophe._connectionPlugins[k]; this[k] = new F(); this[k].init(this); } } } /** Function: setProtocol * Select protocal based on this.options or this.service */ setProtocol() { const proto = this.options.protocol || ""; if (this.options.worker) { this._proto = new Strophe.WorkerWebsocket(this); } else if (this.service.indexOf("ws:") === 0 || this.service.indexOf("wss:") === 0 || proto.indexOf("ws") === 0) { this._proto = new Strophe.Websocket(this); } else { this._proto = new Strophe.Bosh(this); } } /** Function: reset * Reset the connection. * * This function should be called after a connection is disconnected * before that connection is reused. */ reset() { this._proto._reset(); // SASL this.do_session = false; this.do_bind = false; // handler lists this.timedHandlers = []; this.handlers = []; this.removeTimeds = []; this.removeHandlers = []; this.addTimeds = []; this.addHandlers = []; this.authenticated = false; this.connected = false; this.disconnecting = false; this.restored = false; this._data = []; this._requests = []; this._uniqueId = 0; } /** Function: pause * Pause the request manager. * * This will prevent Strophe from sending any more requests to the * server. This is very useful for temporarily pausing * BOSH-Connections while a lot of send() calls are happening quickly. * This causes Strophe to send the data in a single request, saving * many request trips. */ pause() { this.paused = true; } /** Function: resume * Resume the request manager. * * This resumes after pause() has been called. */ resume() { this.paused = false; } /** Function: getUniqueId * Generate a unique ID for use in elements. * * All stanzas are required to have unique id attributes. This * function makes creating these easy. Each connection instance has * a counter which starts from zero, and the value of this counter * plus a colon followed by the suffix becomes the unique id. If no * suffix is supplied, the counter is used as the unique id. * * Suffixes are used to make debugging easier when reading the stream * data, and their use is recommended. The counter resets to 0 for * every new connection for the same reason. For connections to the * same server that authenticate the same way, all the ids should be * the same, which makes it easy to see changes. This is useful for * automated testing as well. * * Parameters: * (String) suffix - A optional suffix to append to the id. * * Returns: * A unique string to be used for the id attribute. */ getUniqueId(suffix) { // eslint-disable-line class-methods-use-this const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { const r = Math.random() * 16 | 0, v = c === 'x' ? r : r & 0x3 | 0x8; return v.toString(16); }); if (typeof suffix === "string" || typeof suffix === "number") { return uuid + ":" + suffix; } else { return uuid + ""; } } /** Function: addProtocolErrorHandler * Register a handler function for when a protocol (websocker or HTTP) * error occurs. * * NOTE: Currently only HTTP errors for BOSH requests are handled. * Patches that handle websocket errors would be very welcome. * * Parameters: * (String) protocol - 'HTTP' or 'websocket' * (Integer) status_code - Error status code (e.g 500, 400 or 404) * (Function) callback - Function that will fire on Http error * * Example: * function onError(err_code){ * //do stuff * } * * let conn = Strophe.connect('http://example.com/http-bind'); * conn.addProtocolErrorHandler('HTTP', 500, onError); * // Triggers HTTP 500 error and onError handler will be called * conn.connect('user_jid@incorrect_jabber_host', 'secret', onConnect); */ addProtocolErrorHandler(protocol, status_code, callback) { this.protocolErrorHandlers[protocol][status_code] = callback; } /** Function: connect * Starts the connection process. * * As the connection process proceeds, the user supplied callback will * be triggered multiple times with status updates. The callback * should take two arguments - the status code and the error condition. * * The status code will be one of the values in the Strophe.Status * constants. The error condition will be one of the conditions * defined in RFC 3920 or the condition 'strophe-parsererror'. * * The Parameters _wait_, _hold_ and _route_ are optional and only relevant * for BOSH connections. Please see XEP 124 for a more detailed explanation * of the optional parameters. * * Parameters: * (String) jid - The user's JID. This may be a bare JID, * or a full JID. If a node is not supplied, SASL OAUTHBEARER or * SASL ANONYMOUS authentication will be attempted (OAUTHBEARER will * process the provided password value as an access token). * (String) pass - The user's password. * (Function) callback - The connect callback function. * (Integer) wait - The optional HTTPBIND wait value. This is the * time the server will wait before returning an empty result for * a request. The default setting of 60 seconds is recommended. * (Integer) hold - The optional HTTPBIND hold value. This is the * number of connections the server will hold at one time. This * should almost always be set to 1 (the default). * (String) route - The optional route value. * (String) authcid - The optional alternative authentication identity * (username) if intending to impersonate another user. * When using the SASL-EXTERNAL authentication mechanism, for example * with client certificates, then the authcid value is used to * determine whether an authorization JID (authzid) should be sent to * the server. The authzid should NOT be sent to the server if the * authzid and authcid are the same. So to prevent it from being sent * (for example when the JID is already contained in the client * certificate), set authcid to that same JID. See XEP-178 for more * details. * (Integer) disconnection_timeout - The optional disconnection timeout * in milliseconds before _doDisconnect will be called. */ connect(jid, pass, callback, wait, hold, route, authcid) { let disconnection_timeout = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : 3000; this.jid = jid; /** Variable: authzid * Authorization identity. */ this.authzid = Strophe.getBareJidFromJid(this.jid); /** Variable: authcid * Authentication identity (User name). */ this.authcid = authcid || Strophe.getNodeFromJid(this.jid); /** Variable: pass * Authentication identity (User password). */ this.pass = pass; this.connect_callback = callback; this.disconnecting = false; this.connected = false; this.authenticated = false; this.restored = false; this.disconnection_timeout = disconnection_timeout; // parse jid for domain this.domain = Strophe.getDomainFromJid(this.jid); this._changeConnectStatus(Strophe.Status.CONNECTING, null); this._proto._connect(wait, hold, route); } /** Function: attach * Attach to an already created and authenticated BOSH session. * * This function is provided to allow Strophe to attach to BOSH * sessions which have been created externally, perhaps by a Web * application. This is often used to support auto-login type features * without putting user credentials into the page. * * Parameters: * (String) jid - The full JID that is bound by the session. * (String) sid - The SID of the BOSH session. * (String) rid - The current RID of the BOSH session. This RID * will be used by the next request. * (Function) callback The connect callback function. * (Integer) wait - The optional HTTPBIND wait value. This is the * time the server will wait before returning an empty result for * a request. The default setting of 60 seconds is recommended. * Other settings will require tweaks to the Strophe.TIMEOUT value. * (Integer) hold - The optional HTTPBIND hold value. This is the * number of connections the server will hold at one time. This * should almost always be set to 1 (the default). * (Integer) wind - The optional HTTBIND window value. This is the * allowed range of request ids that are valid. The default is 5. */ attach(jid, sid, rid, callback, wait, hold, wind) { if (this._proto._attach) { return this._proto._attach(jid, sid, rid, callback, wait, hold, wind); } else { const error = new Error('The "attach" method is not available for your connection protocol'); error.name = 'StropheSessionError'; throw error; } } /** Function: restore * Attempt to restore a cached BOSH session. * * This function is only useful in conjunction with providing the * "keepalive":true option when instantiating a new Strophe.Connection. * * When "keepalive" is set to true, Strophe will cache the BOSH tokens * RID (Request ID) and SID (Session ID) and then when this function is * called, it will attempt to restore the session from those cached * tokens. * * This function must therefore be called instead of connect or attach. * * For an example on how to use it, please see examples/restore.js * * Parameters: * (String) jid - The user's JID. This may be a bare JID or a full JID. * (Function) callback - The connect callback function. * (Integer) wait - The optional HTTPBIND wait value. This is the * time the server will wait before returning an empty result for * a request. The default setting of 60 seconds is recommended. * (Integer) hold - The optional HTTPBIND hold value. This is the * number of connections the server will hold at one time. This * should almost always be set to 1 (the default). * (Integer) wind - The optional HTTBIND window value. This is the * allowed range of request ids that are valid. The default is 5. */ restore(jid, callback, wait, hold, wind) { if (this._sessionCachingSupported()) { this._proto._restore(jid, callback, wait, hold, wind); } else { const error = new Error('The "restore" method can only be used with a BOSH connection.'); error.name = 'StropheSessionError'; throw error; } } /** PrivateFunction: _sessionCachingSupported * Checks whether sessionStorage and JSON are supported and whether we're * using BOSH. */ _sessionCachingSupported() { if (this._proto instanceof Strophe.Bosh) { if (!JSON) { return false; } try { sessionStorage.setItem('_strophe_', '_strophe_'); sessionStorage.removeItem('_strophe_'); } catch (e) { return false; } return true; } return false; } /** Function: xmlInput * User overrideable function that receives XML data coming into the * connection. * * The default function does nothing. User code can override this with * > Strophe.Connection.xmlInput = function (elem) { * > (user code) * > }; * * Due to limitations of current Browsers' XML-Parsers the opening and closing * tag for WebSocket-Connoctions will be passed as selfclosing here. * * BOSH-Connections will have all stanzas wrapped in a tag. See * if you want to strip this tag. * * Parameters: * (XMLElement) elem - The XML data received by the connection. */ xmlInput(elem) { // eslint-disable-line return; } /** Function: xmlOutput * User overrideable function that receives XML data sent to the * connection. * * The default function does nothing. User code can override this with * > Strophe.Connection.xmlOutput = function (elem) { * > (user code) * > }; * * Due to limitations of current Browsers' XML-Parsers the opening and closing * tag for WebSocket-Connoctions will be passed as selfclosing here. * * BOSH-Connections will have all stanzas wrapped in a tag. See * if you want to strip this tag. * * Parameters: * (XMLElement) elem - The XMLdata sent by the connection. */ xmlOutput(elem) { // eslint-disable-line return; } /** Function: rawInput * User overrideable function that receives raw data coming into the * connection. * * The default function does nothing. User code can override this with * > Strophe.Connection.rawInput = function (data) { * > (user code) * > }; * * Parameters: * (String) data - The data received by the connection. */ rawInput(data) { // eslint-disable-line return; } /** Function: rawOutput * User overrideable function that receives raw data sent to the * connection. * * The default function does nothing. User code can override this with * > Strophe.Connection.rawOutput = function (data) { * > (user code) * > }; * * Parameters: * (String) data - The data sent by the connection. */ rawOutput(data) { // eslint-disable-line return; } /** Function: nextValidRid * User overrideable function that receives the new valid rid. * * The default function does nothing. User code can override this with * > Strophe.Connection.nextValidRid = function (rid) { * > (user code) * > }; * * Parameters: * (Number) rid - The next valid rid */ nextValidRid(rid) { // eslint-disable-line return; } /** Function: send * Send a stanza. * * This function is called to push data onto the send queue to * go out over the wire. Whenever a request is sent to the BOSH * server, all pending data is sent and the queue is flushed. * * Parameters: * (XMLElement | * [XMLElement] | * Strophe.Builder) elem - The stanza to send. */ send(elem) { if (elem === null) { return; } if (typeof elem.sort === "function") { for (let i = 0; i < elem.length; i++) { this._queueData(elem[i]); } } else if (typeof elem.tree === "function") { this._queueData(elem.tree()); } else { this._queueData(elem); } this._proto._send(); } /** Function: flush * Immediately send any pending outgoing data. * * Normally send() queues outgoing data until the next idle period * (100ms), which optimizes network use in the common cases when * several send()s are called in succession. flush() can be used to * immediately send all pending data. */ flush() { // cancel the pending idle period and run the idle function // immediately clearTimeout(this._idleTimeout); this._onIdle(); } /** Function: sendPresence * Helper function to send presence stanzas. The main benefit is for * sending presence stanzas for which you expect a responding presence * stanza with the same id (for example when leaving a chat room). * * Parameters: * (XMLElement) elem - The stanza to send. * (Function) callback - The callback function for a successful request. * (Function) errback - The callback function for a failed or timed * out request. On timeout, the stanza will be null. * (Integer) timeout - The time specified in milliseconds for a * timeout to occur. * * Returns: * The id used to send the presence. */ sendPresence(elem, callback, errback, timeout) { let timeoutHandler = null; if (typeof elem.tree === "function") { elem = elem.tree(); } let id = elem.getAttribute('id'); if (!id) { // inject id if not found id = this.getUniqueId("sendPresence"); elem.setAttribute("id", id); } if (typeof callback === "function" || typeof errback === "function") { const handler = this.addHandler(stanza => { // remove timeout handler if there is one if (timeoutHandler) { this.deleteTimedHandler(timeoutHandler); } if (stanza.getAttribute('type') === 'error') { if (errback) { errback(stanza); } } else if (callback) { callback(stanza); } }, null, 'presence', null, id); // if timeout specified, set up a timeout handler. if (timeout) { timeoutHandler = this.addTimedHandler(timeout, () => { // get rid of normal handler this.deleteHandler(handler); // call errback on timeout with null stanza if (errback) { errback(null); } return false; }); } } this.send(elem); return id; } /** Function: sendIQ * Helper function to send IQ stanzas. * * Parameters: * (XMLElement) elem - The stanza to send. * (Function) callback - The callback function for a successful request. * (Function) errback - The callback function for a failed or timed * out request. On timeout, the stanza will be null. * (Integer) timeout - The time specified in milliseconds for a * timeout to occur. * * Returns: * The id used to send the IQ. */ sendIQ(elem, callback, errback, timeout) { let timeoutHandler = null; if (typeof elem.tree === "function") { elem = elem.tree(); } let id = elem.getAttribute('id'); if (!id) { // inject id if not found id = this.getUniqueId("sendIQ"); elem.setAttribute("id", id); } if (typeof callback === "function" || typeof errback === "function") { const handler = this.addHandler(stanza => { // remove timeout handler if there is one if (timeoutHandler) { this.deleteTimedHandler(timeoutHandler); } const iqtype = stanza.getAttribute('type'); if (iqtype === 'result') { if (callback) { callback(stanza); } } else if (iqtype === 'error') { if (errback) { errback(stanza); } } else { const error = new Error(`Got bad IQ type of ${iqtype}`); error.name = "StropheError"; throw error; } }, null, 'iq', ['error', 'result'], id); // if timeout specified, set up a timeout handler. if (timeout) { timeoutHandler = this.addTimedHandler(timeout, () => { // get rid of normal handler this.deleteHandler(handler); // call errback on timeout with null stanza if (errback) { errback(null); } return false; }); } } this.send(elem); return id; } /** PrivateFunction: _queueData * Queue outgoing data for later sending. Also ensures that the data * is a DOMElement. */ _queueData(element) { if (element === null || !element.tagName || !element.childNodes) { const error = new Error("Cannot queue non-DOMElement."); error.name = "StropheError"; throw error; } this._data.push(element); } /** PrivateFunction: _sendRestart * Send an xmpp:restart stanza. */ _sendRestart() { this._data.push("restart"); this._proto._sendRestart(); this._idleTimeout = setTimeout(() => this._onIdle(), 100); } /** Function: addTimedHandler * Add a timed handler to the connection. * * This function adds a timed handler. The provided handler will * be called every period milliseconds until it returns false, * the connection is terminated, or the handler is removed. Handlers * that wish to continue being invoked should return true. * * Because of method binding it is necessary to save the result of * this function if you wish to remove a handler with * deleteTimedHandler(). * * Note that user handlers are not active until authentication is * successful. * * Parameters: * (Integer) period - The period of the handler. * (Function) handler - The callback function. * * Returns: * A reference to the handler that can be used to remove it. */ addTimedHandler(period, handler) { const thand = new Strophe.TimedHandler(period, handler); this.addTimeds.push(thand); return thand; } /** Function: deleteTimedHandler * Delete a timed handler for a connection. * * This function removes a timed handler from the connection. The * handRef parameter is *not* the function passed to addTimedHandler(), * but is the reference returned from addTimedHandler(). * * Parameters: * (Strophe.TimedHandler) handRef - The handler reference. */ deleteTimedHandler(handRef) { // this must be done in the Idle loop so that we don't change // the handlers during iteration this.removeTimeds.push(handRef); } /** Function: addHandler * Add a stanza handler for the connection. * * This function adds a stanza handler to the connection. The * handler callback will be called for any stanza that matches * the parameters. Note that if multiple parameters are supplied, * they must all match for the handler to be invoked. * * The handler will receive the stanza that triggered it as its argument. * *The handler should return true if it is to be invoked again; * returning false will remove the handler after it returns.* * * As a convenience, the ns parameters applies to the top level element * and also any of its immediate children. This is primarily to make * matching /iq/query elements easy. * * Options * ~~~~~~~ * With the options argument, you can specify boolean flags that affect how * matches are being done. * * Currently two flags exist: * * - matchBareFromJid: * When set to true, the from parameter and the * from attribute on the stanza will be matched as bare JIDs instead * of full JIDs. To use this, pass {matchBareFromJid: true} as the * value of options. The default value for matchBareFromJid is false. * * - ignoreNamespaceFragment: * When set to true, a fragment specified on the stanza's namespace * URL will be ignored when it's matched with the one configured for * the handler. * * This means that if you register like this: * > connection.addHandler( * > handler, * > 'http://jabber.org/protocol/muc', * > null, null, null, null, * > {'ignoreNamespaceFragment': true} * > ); * * Then a stanza with XML namespace of * 'http://jabber.org/protocol/muc#user' will also be matched. If * 'ignoreNamespaceFragment' is false, then only stanzas with * 'http://jabber.org/protocol/muc' will be matched. * * Deleting the handler * ~~~~~~~~~~~~~~~~~~~~ * The return value should be saved if you wish to remove the handler * with deleteHandler(). * * Parameters: * (Function) handler - The user callback. * (String) ns - The namespace to match. * (String) name - The stanza name to match. * (String|Array) type - The stanza type (or types if an array) to match. * (String) id - The stanza id attribute to match. * (String) from - The stanza from attribute to match. * (String) options - The handler options * * Returns: * A reference to the handler that can be used to remove it. */ addHandler(handler, ns, name, type, id, from, options) { const hand = new Strophe.Handler(handler, ns, name, type, id, from, options); this.addHandlers.push(hand); return hand; } /** Function: deleteHandler * Delete a stanza handler for a connection. * * This function removes a stanza handler from the connection. The * handRef parameter is *not* the function passed to addHandler(), * but is the reference returned from addHandler(). * * Parameters: * (Strophe.Handler) handRef - The handler reference. */ deleteHandler(handRef) { // this must be done in the Idle loop so that we don't change // the handlers during iteration this.removeHandlers.push(handRef); // If a handler is being deleted while it is being added, // prevent it from getting added const i = this.addHandlers.indexOf(handRef); if (i >= 0) { this.addHandlers.splice(i, 1); } } /** Function: registerSASLMechanisms * * Register the SASL mechanisms which will be supported by this instance of * Strophe.Connection (i.e. which this XMPP client will support). * * Parameters: * (Array) mechanisms - Array of objects with Strophe.SASLMechanism prototypes * */ registerSASLMechanisms(mechanisms) { this.mechanisms = {}; mechanisms = mechanisms || [Strophe.SASLAnonymous, Strophe.SASLExternal, Strophe.SASLOAuthBearer, Strophe.SASLXOAuth2, Strophe.SASLPlain, Strophe.SASLSHA1]; mechanisms.forEach(m => this.registerSASLMechanism(m)); } /** Function: registerSASLMechanism * * Register a single SASL mechanism, to be supported by this client. * * Parameters: * (Object) mechanism - Object with a Strophe.SASLMechanism prototype * */ registerSASLMechanism(Mechanism) { const mechanism = new Mechanism(); this.mechanisms[mechanism.mechname] = mechanism; } /** Function: disconnect * Start the graceful disconnection process. * * This function starts the disconnection process. This process starts * by sending unavailable presence and sending BOSH body of type * terminate. A timeout handler makes sure that disconnection happens * even if the BOSH server does not respond. * If the Connection object isn't connected, at least tries to abort all pending requests * so the connection object won't generate successful requests (which were already opened). * * The user supplied connection callback will be notified of the * progress as this process happens. * * Parameters: * (String) reason - The reason the disconnect is occuring. */ disconnect(reason) { this._changeConnectStatus(Strophe.Status.DISCONNECTING, reason); if (reason) { Strophe.warn("Disconnect was called because: " + reason); } else { Strophe.info("Disconnect was called"); } if (this.connected) { let pres = false; this.disconnecting = true; if (this.authenticated) { pres = $pres({ 'xmlns': Strophe.NS.CLIENT, 'type': 'unavailable' }); } // setup timeout handler this._disconnectTimeout = this._addSysTimedHandler(this.disconnection_timeout, this._onDisconnectTimeout.bind(this)); this._proto._disconnect(pres); } else { Strophe.warn("Disconnect was called before Strophe connected to the server"); this._proto._abortAllRequests(); this._doDisconnect(); } } /** PrivateFunction: _changeConnectStatus * _Private_ helper function that makes sure plugins and the user's * callback are notified of connection status changes. * * Parameters: * (Integer) status - the new connection status, one of the values * in Strophe.Status * (String) condition - the error condition or null * (XMLElement) elem - The triggering stanza. */ _changeConnectStatus(status, condition, elem) { // notify all plugins listening for status changes for (const k in Strophe._connectionPlugins) { if (Object.prototype.hasOwnProperty.call(Strophe._connectionPlugins, k)) { const plugin = this[k]; if (plugin.statusChanged) { try { plugin.statusChanged(status, condition); } catch (err) { Strophe.error(`${k} plugin caused an exception changing status: ${err}`); } } } } // notify the user's callback if (this.connect_callback) { try { this.connect_callback(status, condition, elem); } catch (e) { Strophe._handleError(e); Strophe.error(`User connection callback caused an exception: ${e}`); } } } /** PrivateFunction: _doDisconnect * _Private_ function to disconnect. * * This is the last piece of the disconnection logic. This resets the * connection and alerts the user's connection callback. */ _doDisconnect(condition) { if (typeof this._idleTimeout === "number") { clearTimeout(this._idleTimeout); } // Cancel Disconnect Timeout if (this._disconnectTimeout !== null) { this.deleteTimedHandler(this._disconnectTimeout); this._disconnectTimeout = null; } Strophe.debug("_doDisconnect was called"); this._proto._doDisconnect(); this.authenticated = false; this.disconnecting = false; this.restored = false; // delete handlers this.handlers = []; this.timedHandlers = []; this.removeTimeds = []; this.removeHandlers = []; this.addTimeds = []; this.addHandlers = []; // tell the parent we disconnected this._changeConnectStatus(Strophe.Status.DISCONNECTED, condition); this.connected = false; } /** PrivateFunction: _dataRecv * _Private_ handler to processes incoming data from the the connection. * * Except for _connect_cb handling the initial connection request, * this function handles the incoming data for all requests. This * function also fires stanza handlers that match each incoming * stanza. * * Parameters: * (Strophe.Request) req - The request that has data ready. * (string) req - The stanza a raw string (optiona). */ _dataRecv(req, raw) { const elem = this._proto._reqToData(req); if (elem === null) { return; } if (this.xmlInput !== Strophe.Connection.prototype.xmlInput) { if (elem.nodeName === this._proto.strip && elem.childNodes.length) { this.xmlInput(elem.childNodes[0]); } else { this.xmlInput(elem); } } if (this.rawInput !== Strophe.Connection.prototype.rawInput) { if (raw) { this.rawInput(raw); } else { this.rawInput(Strophe.serialize(elem)); } } // remove handlers scheduled for deletion while (this.removeHandlers.length > 0) { const hand = this.removeHandlers.pop(); const i = this.handlers.indexOf(hand); if (i >= 0) { this.handlers.splice(i, 1); } } // add handlers scheduled for addition while (this.addHandlers.length > 0) { this.handlers.push(this.addHandlers.pop()); } // handle graceful disconnect if (this.disconnecting && this._proto._emptyQueue()) { this._doDisconnect(); return; } const type = elem.getAttribute("type"); if (type !== null && type === "terminate") { // Don't process stanzas that come in after disconnect if (this.disconnecting) { return; } // an error occurred let cond = elem.getAttribute("condition"); const conflict = elem.getElementsByTagName("conflict"); if (cond !== null) { if (cond === "remote-stream-error" && conflict.length > 0) { cond = "conflict"; } this._changeConnectStatus(Strophe.Status.CONNFAIL, cond); } else { this._changeConnectStatus(Strophe.Status.CONNFAIL, Strophe.ErrorCondition.UNKOWN_REASON); } this._doDisconnect(cond); return; } // send each incoming stanza through the handler chain Strophe.forEachChild(elem, null, child => { const matches = []; this.handlers = this.handlers.reduce((handlers, handler) => { try { if (handler.isMatch(child) && (this.authenticated || !handler.user)) { if (handler.run(child)) { handlers.push(handler); } matches.push(handler); } else { handlers.push(handler); } } catch (e) { // if the handler throws an exception, we consider it as false Strophe.warn('Removing Strophe handlers due to uncaught exception: ' + e.message); } return handlers; }, []); // If no handler was fired for an incoming IQ with type="set", // then we return an IQ error stanza with service-unavailable. if (!matches.length && this.iqFallbackHandler.isMatch(child)) { this.iqFallbackHandler.run(child); } }); } /** PrivateFunction: _connect_cb * _Private_ handler for initial connection request. * * This handler is used to process the initial connection request * response from the BOSH server. It is used to set up authentication * handlers and start the authentication process. * * SASL authentication will be attempted if available, otherwise * the code will fall back to legacy authentication. * * Parameters: * (Strophe.Request) req - The current request. * (Function) _callback - low level (xmpp) connect callback function. * Useful for plugins with their own xmpp connect callback (when they * want to do something special). */ _connect_cb(req, _callback, raw) { Strophe.debug("_connect_cb was called"); this.connected = true; let bodyWrap; try { bodyWrap = this._proto._reqToData(req); } catch (e) { if (e.name !== Strophe.ErrorCondition.BAD_FORMAT) { throw e; } this._changeConnectStatus(Strophe.Status.CONNFAIL, Strophe.ErrorCondition.BAD_FORMAT); this._doDisconnect(Strophe.ErrorCondition.BAD_FORMAT); } if (!bodyWrap) { return; } if (this.xmlInput !== Strophe.Connection.prototype.xmlInput) { if (bodyWrap.nodeName === this._proto.strip && bodyWrap.childNodes.length) { this.xmlInput(bodyWrap.childNodes[0]); } else { this.xmlInput(bodyWrap); } } if (this.rawInput !== Strophe.Connection.prototype.rawInput) { if (raw) { this.rawInput(raw); } else { this.rawInput(Strophe.serialize(bodyWrap)); } } const conncheck = this._proto._connect_cb(bodyWrap); if (conncheck === Strophe.Status.CONNFAIL) { return; } // Check for the stream:features tag let hasFeatures; if (bodyWrap.getElementsByTagNameNS) { hasFeatures = bodyWrap.getElementsByTagNameNS(Strophe.NS.STREAM, "features").length > 0; } else { hasFeatures = bodyWrap.getElementsByTagName("stream:features").length > 0 || bodyWrap.getElementsByTagName("features").length > 0; } if (!hasFeatures) { this._proto._no_auth_received(_callback); return; } const matched = Array.from(bodyWrap.getElementsByTagName("mechanism")).map(m => this.mechanisms[m.textContent]).filter(m => m); if (matched.length === 0) { if (bodyWrap.getElementsByTagName("auth").length === 0) { // There are no matching SASL mechanisms and also no legacy // auth available. this._proto._no_auth_received(_callback); return; } } if (this.do_authentication !== false) { this.authenticate(matched); } } /** Function: sortMechanismsByPriority * * Sorts an array of objects with prototype SASLMechanism according to * their priorities. * * Parameters: * (Array) mechanisms - Array of SASL mechanisms. * */ sortMechanismsByPriority(mechanisms) { // eslint-disable-line class-methods-use-this // Sorting mechanisms according to priority. for (let i = 0; i < mechanisms.length - 1; ++i) { let higher = i; for (let j = i + 1; j < mechanisms.length; ++j) { if (mechanisms[j].priority > mechanisms[higher].priority) { higher = j; } } if (higher !== i) { const swap = mechanisms[i]; mechanisms[i] = mechanisms[higher]; mechanisms[higher] = swap; } } return mechanisms; } /** Function: authenticate * Set up authentication * * Continues the initial connection request by setting up authentication * handlers and starting the authentication process. * * SASL authentication will be attempted if available, otherwise * the code will fall back to legacy authentication. * * Parameters: * (Array) matched - Array of SASL mechanisms supported. * */ authenticate(matched) { if (!this._attemptSASLAuth(matched)) { this._attemptLegacyAuth(); } } /** PrivateFunction: _attemptSASLAuth * * Iterate through an array of SASL mechanisms and attempt authentication * with the highest priority (enabled) mechanism. * * Parameters: * (Array) mechanisms - Array of SASL mechanisms. * * Returns: * (Boolean) mechanism_found - true or false, depending on whether a * valid SASL mechanism was found with which authentication could be * started. */ _attemptSASLAuth(mechanisms) { mechanisms = this.sortMechanismsByPriority(mechanisms || []); let mechanism_found = false; for (let i = 0; i < mechanisms.length; ++i) { if (!mechanisms[i].test(this)) { continue; } this._sasl_success_handler = this._addSysHandler(this._sasl_success_cb.bind(this), null, "success", null, null); this._sasl_failure_handler = this._addSysHandler(this._sasl_failure_cb.bind(this), null, "failure", null, null); this._sasl_challenge_handler = this._addSysHandler(this._sasl_challenge_cb.bind(this), null, "challenge", null, null); this._sasl_mechanism = mechanisms[i]; this._sasl_mechanism.onStart(this); const request_auth_exchange = $build("auth", { 'xmlns': Strophe.NS.SASL, 'mechanism': this._sasl_mechanism.mechname }); if (this._sasl_mechanism.isClientFirst) { const response = this._sasl_mechanism.clientChallenge(this); request_auth_exchange.t(abab.btoa(response)); } this.send(request_auth_exchange.tree()); mechanism_found = true; break; } return mechanism_found; } /** PrivateFunction: _sasl_challenge_cb * _Private_ handler for the SASL challenge * */ _sasl_challenge_cb(elem) { const challenge = abab.atob(Strophe.getText(elem)); const response = this._sasl_mechanism.onChallenge(this, challenge); const stanza = $build('response', { 'xmlns': Strophe.NS.SASL }); if (response !== "") { stanza.t(abab.btoa(response)); } this.send(stanza.tree()); return true; } /** PrivateFunction: _attemptLegacyAuth * * Attempt legacy (i.e. non-SASL) authentication. */ _attemptLegacyAuth() { if (Strophe.getNodeFromJid(this.jid) === null) { // we don't have a node, which is required for non-anonymous // client connections this._changeConnectStatus(Strophe.Status.CONNFAIL, Strophe.ErrorCondition.MISSING_JID_NODE); this.disconnect(Strophe.ErrorCondition.MISSING_JID_NODE); } else { // Fall back to legacy authentication this._changeConnectStatus(Strophe.Status.AUTHENTICATING, null); this._addSysHandler(this._onLegacyAuthIQResult.bind(this), null, null, null, "_auth_1"); this.send($iq({ 'type': "get", 'to': this.domain, 'id': "_auth_1" }).c("query", { xmlns: Strophe.NS.AUTH }).c("username", {}).t(Strophe.getNodeFromJid(this.jid)).tree()); } } /** PrivateFunction: _onLegacyAuthIQResult * _Private_ handler for legacy authentication. * * This handler is called in response to the initial * for legacy authentication. It builds an authentication and * sends it, creating a handler (calling back to _auth2_cb()) to * handle the result * * Parameters: * (XMLElement) elem - The stanza that triggered the callback. * * Returns: * false to remove the handler. */ _onLegacyAuthIQResult(elem) { // eslint-disable-line no-unused-vars // build plaintext auth iq const iq = $iq({ type: "set", id: "_auth_2" }).c('query', { xmlns: Strophe.NS.AUTH }).c('username', {}).t(Strophe.getNodeFromJid(this.jid)).up().c('password').t(this.pass); if (!Strophe.getResourceFromJid(this.jid)) { // since the user has not supplied a resource, we pick // a default one here. unlike other auth methods, the server // cannot do this for us. this.jid = Strophe.getBareJidFromJid(this.jid) + '/strophe'; } iq.up().c('resource', {}).t(Strophe.getResourceFromJid(this.jid)); this._addSysHandler(this._auth2_cb.bind(this), null, null, null, "_auth_2"); this.send(iq.tree()); return false; } /** PrivateFunction: _sasl_success_cb * _Private_ handler for succesful SASL authentication. * * Parameters: * (XMLElement) elem - The matching stanza. * * Returns: * false to remove the handler. */ _sasl_success_cb(elem) { if (this._sasl_data["server-signature"]) { let serverSignature; const success = abab.atob(Strophe.getText(elem)); const attribMatch = /([a-z]+)=([^,]+)(,|$)/; const matches = success.match(attribMatch); if (matches[1] === "v") { serverSignature = matches[2]; } if (serverSignature !== this._sasl_data["server-signature"]) { // remove old handlers this.deleteHandler(this._sasl_failure_handler); this._sasl_failure_handler = null; if (this._sasl_challenge_handler) { this.deleteHandler(this._sasl_challenge_handler); this._sasl_challenge_handler = null; } this._sasl_data = {}; return this._sasl_failure_cb(null); } } Strophe.info("SASL authentication succeeded."); if (this._sasl_mechanism) { this._sasl_mechanism.onSuccess(); } // remove old handlers this.deleteHandler(this._sasl_failure_handler); this._sasl_failure_handler = null; if (this._sasl_challenge_handler) { this.deleteHandler(this._sasl_challenge_handler); this._sasl_challenge_handler = null; } const streamfeature_handlers = []; const wrapper = (handlers, elem) => { while (handlers.length) { this.deleteHandler(handlers.pop()); } this._onStreamFeaturesAfterSASL(elem); return false; }; streamfeature_handlers.push(this._addSysHandler(elem => wrapper(streamfeature_handlers, elem), null, "stream:features", null, null)); streamfeature_handlers.push(this._addSysHandler(elem => wrapper(streamfeature_handlers, elem), Strophe.NS.STREAM, "features", null, null)); // we must send an xmpp:restart now this._sendRestart(); return false; } /** PrivateFunction: _onStreamFeaturesAfterSASL * Parameters: * (XMLElement) elem - The matching stanza. * * Returns: * false to remove the handler. */ _onStreamFeaturesAfterSASL(elem) { // save stream:features for future usage this.features = elem; for (let i = 0; i < elem.childNodes.length; i++) { const child = elem.childNodes[i]; if (child.nodeName === 'bind') { this.do_bind = true; } if (child.nodeName === 'session') { this.do_session = true; } } if (!this.do_bind) { this._changeConnectStatus(Strophe.Status.AUTHFAIL, null); return false; } else if (!this.options.explicitResourceBinding) { this.bind(); } else { this._changeConnectStatus(Strophe.Status.BINDREQUIRED, null); } return false; } /** Function: bind * * Sends an IQ to the XMPP server to bind a JID resource for this session. * * https://tools.ietf.org/html/rfc6120#section-7.5 * * If `explicitResourceBinding` was set to a truthy value in the options * passed to the Strophe.Connection constructor, then this function needs * to be called explicitly by the client author. * * Otherwise it'll be called automatically as soon as the XMPP server * advertises the "urn:ietf:params:xml:ns:xmpp-bind" stream feature. */ bind() { if (!this.do_bind) { Strophe.log(Strophe.LogLevel.INFO, `Strophe.Connection.prototype.bind called but "do_bind" is false`); return; } this._addSysHandler(this._onResourceBindResultIQ.bind(this), null, null, null, "_bind_auth_2"); const resource = Strophe.getResourceFromJid(this.jid); if (resource) { this.send($iq({ type: "set", id: "_bind_auth_2" }).c('bind', { xmlns: Strophe.NS.BIND }).c('resource', {}).t(resource).tree()); } else { this.send($iq({ type: "set", id: "_bind_auth_2" }).c('bind', { xmlns: Strophe.NS.BIND }).tree()); } } /** PrivateFunction: _onResourceBindIQ * _Private_ handler for binding result and session start. * * Parameters: * (XMLElement) elem - The matching stanza. * * Returns: * false to remove the handler. */ _onResourceBindResultIQ(elem) { if (elem.getAttribute("type") === "error") { Strophe.warn("Resource binding failed."); const conflict = elem.getElementsByTagName("conflict"); let condition; if (conflict.length > 0) { condition = Strophe.ErrorCondition.CONFLICT; } this._changeConnectStatus(Strophe.Status.AUTHFAIL, condition, elem); return false; } // TODO - need to grab errors const bind = elem.getElementsByTagName("bind"); if (bind.length > 0) { const jidNode = bind[0].getElementsByTagName("jid"); if (jidNode.length > 0) { this.authenticated = true; this.jid = Strophe.getText(jidNode[0]); if (this.do_session) { this._establishSession(); } else { this._changeConnectStatus(Strophe.Status.CONNECTED, null); } } } else { Strophe.warn("Resource binding failed."); this._changeConnectStatus(Strophe.Status.AUTHFAIL, null, elem); return false; } } /** PrivateFunction: _establishSession * Send IQ request to establish a session with the XMPP server. * * See https://xmpp.org/rfcs/rfc3921.html#session * * Note: The protocol for session establishment has been determined as * unnecessary and removed in RFC-6121. */ _establishSession() { if (!this.do_session) { throw new Error(`Strophe.Connection.prototype._establishSession ` + `called but apparently ${Strophe.NS.SESSION} wasn't advertised by the server`); } this._addSysHandler(this._onSessionResultIQ.bind(this), null, null, null, "_session_auth_2"); this.send($iq({ type: "set", id: "_session_auth_2" }).c('session', { xmlns: Strophe.NS.SESSION }).tree()); } /** PrivateFunction: _onSessionResultIQ * _Private_ handler for the server's IQ response to a client's session * request. * * This sets Connection.authenticated to true on success, which * starts the processing of user handlers. * * See https://xmpp.org/rfcs/rfc3921.html#session * * Note: The protocol for session establishment has been determined as * unnecessary and removed in RFC-6121. * * Parameters: * (XMLElement) elem - The matching stanza. * * Returns: * false to remove the handler. */ _onSessionResultIQ(elem) { if (elem.getAttribute("type") === "result") { this.authenticated = true; this._changeConnectStatus(Strophe.Status.CONNECTED, null); } else if (elem.getAttribute("type") === "error") { this.authenticated = false; Strophe.warn("Session creation failed."); this._changeConnectStatus(Strophe.Status.AUTHFAIL, null, elem); return false; } return false; } /** PrivateFunction: _sasl_failure_cb * _Private_ handler for SASL authentication failure. * * Parameters: * (XMLElement) elem - The matching stanza. * * Returns: * false to remove the handler. */ _sasl_failure_cb(elem) { // delete unneeded handlers if (this._sasl_success_handler) { this.deleteHandler(this._sasl_success_handler); this._sasl_success_handler = null; } if (this._sasl_challenge_handler) { this.deleteHandler(this._sasl_challenge_handler); this._sasl_challenge_handler = null; } if (this._sasl_mechanism) this._sasl_mechanism.onFailure(); this._changeConnectStatus(Strophe.Status.AUTHFAIL, null, elem); return false; } /** PrivateFunction: _auth2_cb * _Private_ handler to finish legacy authentication. * * This handler is called when the result from the jabber:iq:auth * stanza is returned. * * Parameters: * (XMLElement) elem - The stanza that triggered the callback. * * Returns: * false to remove the handler. */ _auth2_cb(elem) { if (elem.getAttribute("type") === "result") { this.authenticated = true; this._changeConnectStatus(Strophe.Status.CONNECTED, null); } else if (elem.getAttribute("type") === "error") { this._changeConnectStatus(Strophe.Status.AUTHFAIL, null, elem); this.disconnect('authentication failed'); } return false; } /** PrivateFunction: _addSysTimedHandler * _Private_ function to add a system level timed handler. * * This function is used to add a Strophe.TimedHandler for the * library code. System timed handlers are allowed to run before * authentication is complete. * * Parameters: * (Integer) period - The period of the handler. * (Function) handler - The callback function. */ _addSysTimedHandler(period, handler) { const thand = new Strophe.TimedHandler(period, handler); thand.user = false; this.addTimeds.push(thand); return thand; } /** PrivateFunction: _addSysHandler * _Private_ function to add a system level stanza handler. * * This function is used to add a Strophe.Handler for the * library code. System stanza handlers are allowed to run before * authentication is complete. * * Parameters: * (Function) handler - The callback function. * (String) ns - The namespace to match. * (String) name - The stanza name to match. * (String) type - The stanza type attribute to match. * (String) id - The stanza id attribute to match. */ _addSysHandler(handler, ns, name, type, id) { const hand = new Strophe.Handler(handler, ns, name, type, id); hand.user = false; this.addHandlers.push(hand); return hand; } /** PrivateFunction: _onDisconnectTimeout * _Private_ timeout handler for handling non-graceful disconnection. * * If the graceful disconnect process does not complete within the * time allotted, this handler finishes the disconnect anyway. * * Returns: * false to remove the handler. */ _onDisconnectTimeout() { Strophe.debug("_onDisconnectTimeout was called"); this._changeConnectStatus(Strophe.Status.CONNTIMEOUT, null); this._proto._onDisconnectTimeout(); // actually disconnect this._doDisconnect(); return false; } /** PrivateFunction: _onIdle * _Private_ handler to process events during idle cycle. * * This handler is called every 100ms to fire timed handlers that * are ready and keep poll requests going. */ _onIdle() { // add timed handlers scheduled for addition // NOTE: we add before remove in the case a timed handler is // added and then deleted before the next _onIdle() call. while (this.addTimeds.length > 0) { this.timedHandlers.push(this.addTimeds.pop()); } // remove timed handlers that have been scheduled for deletion while (this.removeTimeds.length > 0) { const thand = this.removeTimeds.pop(); const i = this.timedHandlers.indexOf(thand); if (i >= 0) { this.timedHandlers.splice(i, 1); } } // call ready timed handlers const now = new Date().getTime(); const newList = []; for (let i = 0; i < this.timedHandlers.length; i++) { const thand = this.timedHandlers[i]; if (this.authenticated || !thand.user) { const since = thand.lastCalled + thand.period; if (since - now <= 0) { if (thand.run()) { newList.push(thand); } } else { newList.push(thand); } } } this.timedHandlers = newList; clearTimeout(this._idleTimeout); this._proto._onIdle(); // reactivate the timer only if connected if (this.connected) { this._idleTimeout = setTimeout(() => this._onIdle(), 100); } } }; Strophe.SASLMechanism = SASLMechanism; /** Constants: SASL mechanisms * Available authentication mechanisms * * Strophe.SASLAnonymous - SASL ANONYMOUS authentication. * Strophe.SASLPlain - SASL PLAIN authentication. * Strophe.SASLSHA1 - SASL SCRAM-SHA-1 authentication * Strophe.SASLOAuthBearer - SASL OAuth Bearer authentication * Strophe.SASLExternal - SASL EXTERNAL authentication * Strophe.SASLXOAuth2 - SASL X-OAuth2 authentication */ Strophe.SASLAnonymous = SASLAnonymous; Strophe.SASLPlain = SASLPlain; Strophe.SASLSHA1 = SASLSHA1; Strophe.SASLOAuthBearer = SASLOAuthBearer; Strophe.SASLExternal = SASLExternal; Strophe.SASLXOAuth2 = SASLXOAuth2; var core = { 'Strophe': Strophe, '$build': $build, '$iq': $iq, '$msg': $msg, '$pres': $pres, 'SHA1': SHA1, 'MD5': MD5, 'b64_hmac_sha1': SHA1.b64_hmac_sha1, 'b64_sha1': SHA1.b64_sha1, 'str_hmac_sha1': SHA1.str_hmac_sha1, 'str_sha1': SHA1.str_sha1 }; /* This program is distributed under the terms of the MIT license. Please see the LICENSE file for details. Copyright 2006-2008, OGG, LLC */ /** PrivateClass: Strophe.Request * _Private_ helper class that provides a cross implementation abstraction * for a BOSH related XMLHttpRequest. * * The Strophe.Request class is used internally to encapsulate BOSH request * information. It is not meant to be used from user's code. */ Strophe.Request = class Request { /** PrivateConstructor: Strophe.Request * Create and initialize a new Strophe.Request object. * * Parameters: * (XMLElement) elem - The XML data to be sent in the request. * (Function) func - The function that will be called when the * XMLHttpRequest readyState changes. * (Integer) rid - The BOSH rid attribute associated with this request. * (Integer) sends - The number of times this same request has been sent. */ constructor(elem, func, rid, sends) { this.id = ++Strophe._requestId; this.xmlData = elem; this.data = Strophe.serialize(elem); // save original function in case we need to make a new request // from this one. this.origFunc = func; this.func = func; this.rid = rid; this.date = NaN; this.sends = sends || 0; this.abort = false; this.dead = null; this.age = function () { if (!this.date) { return 0; } const now = new Date(); return (now - this.date) / 1000; }; this.timeDead = function () { if (!this.dead) { return 0; } const now = new Date(); return (now - this.dead) / 1000; }; this.xhr = this._newXHR(); } /** PrivateFunction: getResponse * Get a response from the underlying XMLHttpRequest. * * This function attempts to get a response from the request and checks * for errors. * * Throws: * "parsererror" - A parser error occured. * "bad-format" - The entity has sent XML that cannot be processed. * * Returns: * The DOM element tree of the response. */ getResponse() { let node = null; if (this.xhr.responseXML && this.xhr.responseXML.documentElement) { node = this.xhr.responseXML.documentElement; if (node.tagName === "parsererror") { Strophe.error("invalid response received"); Strophe.error("responseText: " + this.xhr.responseText); Strophe.error("responseXML: " + Strophe.serialize(this.xhr.responseXML)); throw new Error("parsererror"); } } else if (this.xhr.responseText) { // In React Native, we may get responseText but no responseXML. We can try to parse it manually. Strophe.debug("Got responseText but no responseXML; attempting to parse it with DOMParser..."); node = new DOMParser().parseFromString(this.xhr.responseText, 'application/xml').documentElement; if (!node) { throw new Error('Parsing produced null node'); } else if (node.querySelector('parsererror')) { Strophe.error("invalid response received: " + node.querySelector('parsererror').textContent); Strophe.error("responseText: " + this.xhr.responseText); const error = new Error(); error.name = Strophe.ErrorCondition.BAD_FORMAT; throw error; } } return node; } /** PrivateFunction: _newXHR * _Private_ helper function to create XMLHttpRequests. * * This function creates XMLHttpRequests across all implementations. * * Returns: * A new XMLHttpRequest. */ _newXHR() { let xhr = null; if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); if (xhr.overrideMimeType) { xhr.overrideMimeType("text/xml; charset=utf-8"); } } else if (window.ActiveXObject) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } // use Function.bind() to prepend ourselves as an argument xhr.onreadystatechange = this.func.bind(null, this); return xhr; } }; /** Class: Strophe.Bosh * _Private_ helper class that handles BOSH Connections * * The Strophe.Bosh class is used internally by Strophe.Connection * to encapsulate BOSH sessions. It is not meant to be used from user's code. */ /** File: bosh.js * A JavaScript library to enable BOSH in Strophejs. * * this library uses Bidirectional-streams Over Synchronous HTTP (BOSH) * to emulate a persistent, stateful, two-way connection to an XMPP server. * More information on BOSH can be found in XEP 124. */ /** PrivateConstructor: Strophe.Bosh * Create and initialize a Strophe.Bosh object. * * Parameters: * (Strophe.Connection) connection - The Strophe.Connection that will use BOSH. * * Returns: * A new Strophe.Bosh object. */ Strophe.Bosh = class Bosh { constructor(connection) { this._conn = connection; /* request id for body tags */ this.rid = Math.floor(Math.random() * 4294967295); /* The current session ID. */ this.sid = null; // default BOSH values this.hold = 1; this.wait = 60; this.window = 5; this.errors = 0; this.inactivity = null; this.lastResponseHeaders = null; this._requests = []; } /** PrivateFunction: _buildBody * _Private_ helper function to generate the wrapper for BOSH. * * Returns: * A Strophe.Builder with a element. */ _buildBody() { const bodyWrap = $build('body', { 'rid': this.rid++, 'xmlns': Strophe.NS.HTTPBIND }); if (this.sid !== null) { bodyWrap.attrs({ 'sid': this.sid }); } if (this._conn.options.keepalive && this._conn._sessionCachingSupported()) { this._cacheSession(); } return bodyWrap; } /** PrivateFunction: _reset * Reset the connection. * * This function is called by the reset function of the Strophe Connection */ _reset() { this.rid = Math.floor(Math.random() * 4294967295); this.sid = null; this.errors = 0; if (this._conn._sessionCachingSupported()) { window.sessionStorage.removeItem('strophe-bosh-session'); } this._conn.nextValidRid(this.rid); } /** PrivateFunction: _connect * _Private_ function that initializes the BOSH connection. * * Creates and sends the Request that initializes the BOSH connection. */ _connect(wait, hold, route) { this.wait = wait || this.wait; this.hold = hold || this.hold; this.errors = 0; const body = this._buildBody().attrs({ "to": this._conn.domain, "xml:lang": "en", "wait": this.wait, "hold": this.hold, "content": "text/xml; charset=utf-8", "ver": "1.6", "xmpp:version": "1.0", "xmlns:xmpp": Strophe.NS.BOSH }); if (route) { body.attrs({ 'route': route }); } const _connect_cb = this._conn._connect_cb; this._requests.push(new Strophe.Request(body.tree(), this._onRequestStateChange.bind(this, _connect_cb.bind(this._conn)), body.tree().getAttribute("rid"))); this._throttledRequestHandler(); } /** PrivateFunction: _attach * Attach to an already created and authenticated BOSH session. * * This function is provided to allow Strophe to attach to BOSH * sessions which have been created externally, perhaps by a Web * application. This is often used to support auto-login type features * without putting user credentials into the page. * * Parameters: * (String) jid - The full JID that is bound by the session. * (String) sid - The SID of the BOSH session. * (String) rid - The current RID of the BOSH session. This RID * will be used by the next request. * (Function) callback The connect callback function. * (Integer) wait - The optional HTTPBIND wait value. This is the * time the server will wait before returning an empty result for * a request. The default setting of 60 seconds is recommended. * Other settings will require tweaks to the Strophe.TIMEOUT value. * (Integer) hold - The optional HTTPBIND hold value. This is the * number of connections the server will hold at one time. This * should almost always be set to 1 (the default). * (Integer) wind - The optional HTTBIND window value. This is the * allowed range of request ids that are valid. The default is 5. */ _attach(jid, sid, rid, callback, wait, hold, wind) { this._conn.jid = jid; this.sid = sid; this.rid = rid; this._conn.connect_callback = callback; this._conn.domain = Strophe.getDomainFromJid(this._conn.jid); this._conn.authenticated = true; this._conn.connected = true; this.wait = wait || this.wait; this.hold = hold || this.hold; this.window = wind || this.window; this._conn._changeConnectStatus(Strophe.Status.ATTACHED, null); } /** PrivateFunction: _restore * Attempt to restore a cached BOSH session * * Parameters: * (String) jid - The full JID that is bound by the session. * This parameter is optional but recommended, specifically in cases * where prebinded BOSH sessions are used where it's important to know * that the right session is being restored. * (Function) callback The connect callback function. * (Integer) wait - The optional HTTPBIND wait value. This is the * time the server will wait before returning an empty result for * a request. The default setting of 60 seconds is recommended. * Other settings will require tweaks to the Strophe.TIMEOUT value. * (Integer) hold - The optional HTTPBIND hold value. This is the * number of connections the server will hold at one time. This * should almost always be set to 1 (the default). * (Integer) wind - The optional HTTBIND window value. This is the * allowed range of request ids that are valid. The default is 5. */ _restore(jid, callback, wait, hold, wind) { const session = JSON.parse(window.sessionStorage.getItem('strophe-bosh-session')); if (typeof session !== "undefined" && session !== null && session.rid && session.sid && session.jid && (typeof jid === "undefined" || jid === null || Strophe.getBareJidFromJid(session.jid) === Strophe.getBareJidFromJid(jid) || // If authcid is null, then it's an anonymous login, so // we compare only the domains: Strophe.getNodeFromJid(jid) === null && Strophe.getDomainFromJid(session.jid) === jid)) { this._conn.restored = true; this._attach(session.jid, session.sid, session.rid, callback, wait, hold, wind); } else { const error = new Error("_restore: no restoreable session."); error.name = "StropheSessionError"; throw error; } } /** PrivateFunction: _cacheSession * _Private_ handler for the beforeunload event. * * This handler is used to process the Bosh-part of the initial request. * Parameters: * (Strophe.Request) bodyWrap - The received stanza. */ _cacheSession() { if (this._conn.authenticated) { if (this._conn.jid && this.rid && this.sid) { window.sessionStorage.setItem('strophe-bosh-session', JSON.stringify({ 'jid': this._conn.jid, 'rid': this.rid, 'sid': this.sid })); } } else { window.sessionStorage.removeItem('strophe-bosh-session'); } } /** PrivateFunction: _connect_cb * _Private_ handler for initial connection request. * * This handler is used to process the Bosh-part of the initial request. * Parameters: * (Strophe.Request) bodyWrap - The received stanza. */ _connect_cb(bodyWrap) { const typ = bodyWrap.getAttribute("type"); if (typ !== null && typ === "terminate") { // an error occurred let cond = bodyWrap.getAttribute("condition"); Strophe.error("BOSH-Connection failed: " + cond); const conflict = bodyWrap.getElementsByTagName("conflict"); if (cond !== null) { if (cond === "remote-stream-error" && conflict.length > 0) { cond = "conflict"; } this._conn._changeConnectStatus(Strophe.Status.CONNFAIL, cond); } else { this._conn._changeConnectStatus(Strophe.Status.CONNFAIL, "unknown"); } this._conn._doDisconnect(cond); return Strophe.Status.CONNFAIL; } // check to make sure we don't overwrite these if _connect_cb is // called multiple times in the case of missing stream:features if (!this.sid) { this.sid = bodyWrap.getAttribute("sid"); } const wind = bodyWrap.getAttribute('requests'); if (wind) { this.window = parseInt(wind, 10); } const hold = bodyWrap.getAttribute('hold'); if (hold) { this.hold = parseInt(hold, 10); } const wait = bodyWrap.getAttribute('wait'); if (wait) { this.wait = parseInt(wait, 10); } const inactivity = bodyWrap.getAttribute('inactivity'); if (inactivity) { this.inactivity = parseInt(inactivity, 10); } } /** PrivateFunction: _disconnect * _Private_ part of Connection.disconnect for Bosh * * Parameters: * (Request) pres - This stanza will be sent before disconnecting. */ _disconnect(pres) { this._sendTerminate(pres); } /** PrivateFunction: _doDisconnect * _Private_ function to disconnect. * * Resets the SID and RID. */ _doDisconnect() { this.sid = null; this.rid = Math.floor(Math.random() * 4294967295); if (this._conn._sessionCachingSupported()) { window.sessionStorage.removeItem('strophe-bosh-session'); } this._conn.nextValidRid(this.rid); } /** PrivateFunction: _emptyQueue * _Private_ function to check if the Request queue is empty. * * Returns: * True, if there are no Requests queued, False otherwise. */ _emptyQueue() { return this._requests.length === 0; } /** PrivateFunction: _callProtocolErrorHandlers * _Private_ function to call error handlers registered for HTTP errors. * * Parameters: * (Strophe.Request) req - The request that is changing readyState. */ _callProtocolErrorHandlers(req) { const reqStatus = Bosh._getRequestStatus(req); const err_callback = this._conn.protocolErrorHandlers.HTTP[reqStatus]; if (err_callback) { err_callback.call(this, reqStatus); } } /** PrivateFunction: _hitError * _Private_ function to handle the error count. * * Requests are resent automatically until their error count reaches * 5. Each time an error is encountered, this function is called to * increment the count and disconnect if the count is too high. * * Parameters: * (Integer) reqStatus - The request status. */ _hitError(reqStatus) { this.errors++; Strophe.warn("request errored, status: " + reqStatus + ", number of errors: " + this.errors); if (this.errors > 4) { this._conn._onDisconnectTimeout(); } } /** PrivateFunction: _no_auth_received * * Called on stream start/restart when no stream:features * has been received and sends a blank poll request. */ _no_auth_received(callback) { Strophe.warn("Server did not yet offer a supported authentication " + "mechanism. Sending a blank poll request."); if (callback) { callback = callback.bind(this._conn); } else { callback = this._conn._connect_cb.bind(this._conn); } const body = this._buildBody(); this._requests.push(new Strophe.Request(body.tree(), this._onRequestStateChange.bind(this, callback), body.tree().getAttribute("rid"))); this._throttledRequestHandler(); } /** PrivateFunction: _onDisconnectTimeout * _Private_ timeout handler for handling non-graceful disconnection. * * Cancels all remaining Requests and clears the queue. */ _onDisconnectTimeout() { this._abortAllRequests(); } /** PrivateFunction: _abortAllRequests * _Private_ helper function that makes sure all pending requests are aborted. */ _abortAllRequests() { while (this._requests.length > 0) { const req = this._requests.pop(); req.abort = true; req.xhr.abort(); req.xhr.onreadystatechange = function () {}; } } /** PrivateFunction: _onIdle * _Private_ handler called by Strophe.Connection._onIdle * * Sends all queued Requests or polls with empty Request if there are none. */ _onIdle() { const data = this._conn._data; // if no requests are in progress, poll if (this._conn.authenticated && this._requests.length === 0 && data.length === 0 && !this._conn.disconnecting) { Strophe.debug("no requests during idle cycle, sending blank request"); data.push(null); } if (this._conn.paused) { return; } if (this._requests.length < 2 && data.length > 0) { const body = this._buildBody(); for (let i = 0; i < data.length; i++) { if (data[i] !== null) { if (data[i] === "restart") { body.attrs({ "to": this._conn.domain, "xml:lang": "en", "xmpp:restart": "true", "xmlns:xmpp": Strophe.NS.BOSH }); } else { body.cnode(data[i]).up(); } } } delete this._conn._data; this._conn._data = []; this._requests.push(new Strophe.Request(body.tree(), this._onRequestStateChange.bind(this, this._conn._dataRecv.bind(this._conn)), body.tree().getAttribute("rid"))); this._throttledRequestHandler(); } if (this._requests.length > 0) { const time_elapsed = this._requests[0].age(); if (this._requests[0].dead !== null) { if (this._requests[0].timeDead() > Math.floor(Strophe.SECONDARY_TIMEOUT * this.wait)) { this._throttledRequestHandler(); } } if (time_elapsed > Math.floor(Strophe.TIMEOUT * this.wait)) { Strophe.warn("Request " + this._requests[0].id + " timed out, over " + Math.floor(Strophe.TIMEOUT * this.wait) + " seconds since last activity"); this._throttledRequestHandler(); } } } /** PrivateFunction: _getRequestStatus * * Returns the HTTP status code from a Strophe.Request * * Parameters: * (Strophe.Request) req - The Strophe.Request instance. * (Integer) def - The default value that should be returned if no * status value was found. */ static _getRequestStatus(req, def) { let reqStatus; if (req.xhr.readyState === 4) { try { reqStatus = req.xhr.status; } catch (e) { // ignore errors from undefined status attribute. Works // around a browser bug Strophe.error("Caught an error while retrieving a request's status, " + "reqStatus: " + reqStatus); } } if (typeof reqStatus === "undefined") { reqStatus = typeof def === 'number' ? def : 0; } return reqStatus; } /** PrivateFunction: _onRequestStateChange * _Private_ handler for Strophe.Request state changes. * * This function is called when the XMLHttpRequest readyState changes. * It contains a lot of error handling logic for the many ways that * requests can fail, and calls the request callback when requests * succeed. * * Parameters: * (Function) func - The handler for the request. * (Strophe.Request) req - The request that is changing readyState. */ _onRequestStateChange(func, req) { Strophe.debug("request id " + req.id + "." + req.sends + " state changed to " + req.xhr.readyState); if (req.abort) { req.abort = false; return; } if (req.xhr.readyState !== 4) { // The request is not yet complete return; } const reqStatus = Bosh._getRequestStatus(req); this.lastResponseHeaders = req.xhr.getAllResponseHeaders(); if (this._conn.disconnecting && reqStatus >= 400) { this._hitError(reqStatus); this._callProtocolErrorHandlers(req); return; } const reqIs0 = this._requests[0] === req; const reqIs1 = this._requests[1] === req; const valid_request = reqStatus > 0 && reqStatus < 500; const too_many_retries = req.sends > this._conn.maxRetries; if (valid_request || too_many_retries) { // remove from internal queue this._removeRequest(req); Strophe.debug("request id " + req.id + " should now be removed"); } if (reqStatus === 200) { // request succeeded // if request 1 finished, or request 0 finished and request // 1 is over Strophe.SECONDARY_TIMEOUT seconds old, we need to // restart the other - both will be in the first spot, as the // completed request has been removed from the queue already if (reqIs1 || reqIs0 && this._requests.length > 0 && this._requests[0].age() > Math.floor(Strophe.SECONDARY_TIMEOUT * this.wait)) { this._restartRequest(0); } this._conn.nextValidRid(Number(req.rid) + 1); Strophe.debug("request id " + req.id + "." + req.sends + " got 200"); func(req); // call handler this.errors = 0; } else if (reqStatus === 0 || reqStatus >= 400 && reqStatus < 600 || reqStatus >= 12000) { // request failed Strophe.error("request id " + req.id + "." + req.sends + " error " + reqStatus + " happened"); this._hitError(reqStatus); this._callProtocolErrorHandlers(req); if (reqStatus >= 400 && reqStatus < 500) { this._conn._changeConnectStatus(Strophe.Status.DISCONNECTING, null); this._conn._doDisconnect(); } } else { Strophe.error("request id " + req.id + "." + req.sends + " error " + reqStatus + " happened"); } if (!valid_request && !too_many_retries) { this._throttledRequestHandler(); } else if (too_many_retries && !this._conn.connected) { this._conn._changeConnectStatus(Strophe.Status.CONNFAIL, "giving-up"); } } /** PrivateFunction: _processRequest * _Private_ function to process a request in the queue. * * This function takes requests off the queue and sends them and * restarts dead requests. * * Parameters: * (Integer) i - The index of the request in the queue. */ _processRequest(i) { let req = this._requests[i]; const reqStatus = Bosh._getRequestStatus(req, -1); // make sure we limit the number of retries if (req.sends > this._conn.maxRetries) { this._conn._onDisconnectTimeout(); return; } const time_elapsed = req.age(); const primary_timeout = !isNaN(time_elapsed) && time_elapsed > Math.floor(Strophe.TIMEOUT * this.wait); const secondary_timeout = req.dead !== null && req.timeDead() > Math.floor(Strophe.SECONDARY_TIMEOUT * this.wait); const server_error = req.xhr.readyState === 4 && (reqStatus < 1 || reqStatus >= 500); if (primary_timeout || secondary_timeout || server_error) { if (secondary_timeout) { Strophe.error(`Request ${this._requests[i].id} timed out (secondary), restarting`); } req.abort = true; req.xhr.abort(); // setting to null fails on IE6, so set to empty function req.xhr.onreadystatechange = function () {}; this._requests[i] = new Strophe.Request(req.xmlData, req.origFunc, req.rid, req.sends); req = this._requests[i]; } if (req.xhr.readyState === 0) { Strophe.debug("request id " + req.id + "." + req.sends + " posting"); try { const content_type = this._conn.options.contentType || "text/xml; charset=utf-8"; req.xhr.open("POST", this._conn.service, this._conn.options.sync ? false : true); if (typeof req.xhr.setRequestHeader !== 'undefined') { // IE9 doesn't have setRequestHeader req.xhr.setRequestHeader("Content-Type", content_type); } if (this._conn.options.withCredentials) { req.xhr.withCredentials = true; } } catch (e2) { Strophe.error("XHR open failed: " + e2.toString()); if (!this._conn.connected) { this._conn._changeConnectStatus(Strophe.Status.CONNFAIL, "bad-service"); } this._conn.disconnect(); return; } // Fires the XHR request -- may be invoked immediately // or on a gradually expanding retry window for reconnects const sendFunc = () => { req.date = new Date(); if (this._conn.options.customHeaders) { const headers = this._conn.options.customHeaders; for (const header in headers) { if (Object.prototype.hasOwnProperty.call(headers, header)) { req.xhr.setRequestHeader(header, headers[header]); } } } req.xhr.send(req.data); }; // Implement progressive backoff for reconnects -- // First retry (send === 1) should also be instantaneous if (req.sends > 1) { // Using a cube of the retry number creates a nicely // expanding retry window const backoff = Math.min(Math.floor(Strophe.TIMEOUT * this.wait), Math.pow(req.sends, 3)) * 1000; setTimeout(function () { // XXX: setTimeout should be called only with function expressions (23974bc1) sendFunc(); }, backoff); } else { sendFunc(); } req.sends++; if (this._conn.xmlOutput !== Strophe.Connection.prototype.xmlOutput) { if (req.xmlData.nodeName === this.strip && req.xmlData.childNodes.length) { this._conn.xmlOutput(req.xmlData.childNodes[0]); } else { this._conn.xmlOutput(req.xmlData); } } if (this._conn.rawOutput !== Strophe.Connection.prototype.rawOutput) { this._conn.rawOutput(req.data); } } else { Strophe.debug("_processRequest: " + (i === 0 ? "first" : "second") + " request has readyState of " + req.xhr.readyState); } } /** PrivateFunction: _removeRequest * _Private_ function to remove a request from the queue. * * Parameters: * (Strophe.Request) req - The request to remove. */ _removeRequest(req) { Strophe.debug("removing request"); for (let i = this._requests.length - 1; i >= 0; i--) { if (req === this._requests[i]) { this._requests.splice(i, 1); } } // IE6 fails on setting to null, so set to empty function req.xhr.onreadystatechange = function () {}; this._throttledRequestHandler(); } /** PrivateFunction: _restartRequest * _Private_ function to restart a request that is presumed dead. * * Parameters: * (Integer) i - The index of the request in the queue. */ _restartRequest(i) { const req = this._requests[i]; if (req.dead === null) { req.dead = new Date(); } this._processRequest(i); } /** PrivateFunction: _reqToData * _Private_ function to get a stanza out of a request. * * Tries to extract a stanza out of a Request Object. * When this fails the current connection will be disconnected. * * Parameters: * (Object) req - The Request. * * Returns: * The stanza that was passed. */ _reqToData(req) { try { return req.getResponse(); } catch (e) { if (e.message !== "parsererror") { throw e; } this._conn.disconnect("strophe-parsererror"); } } /** PrivateFunction: _sendTerminate * _Private_ function to send initial disconnect sequence. * * This is the first step in a graceful disconnect. It sends * the BOSH server a terminate body and includes an unavailable * presence if authentication has completed. */ _sendTerminate(pres) { Strophe.debug("_sendTerminate was called"); const body = this._buildBody().attrs({ type: "terminate" }); if (pres) { body.cnode(pres.tree()); } const req = new Strophe.Request(body.tree(), this._onRequestStateChange.bind(this, this._conn._dataRecv.bind(this._conn)), body.tree().getAttribute("rid")); this._requests.push(req); this._throttledRequestHandler(); } /** PrivateFunction: _send * _Private_ part of the Connection.send function for BOSH * * Just triggers the RequestHandler to send the messages that are in the queue */ _send() { clearTimeout(this._conn._idleTimeout); this._throttledRequestHandler(); this._conn._idleTimeout = setTimeout(() => this._conn._onIdle(), 100); } /** PrivateFunction: _sendRestart * * Send an xmpp:restart stanza. */ _sendRestart() { this._throttledRequestHandler(); clearTimeout(this._conn._idleTimeout); } /** PrivateFunction: _throttledRequestHandler * _Private_ function to throttle requests to the connection window. * * This function makes sure we don't send requests so fast that the * request ids overflow the connection window in the case that one * request died. */ _throttledRequestHandler() { if (!this._requests) { Strophe.debug("_throttledRequestHandler called with " + "undefined requests"); } else { Strophe.debug("_throttledRequestHandler called with " + this._requests.length + " requests"); } if (!this._requests || this._requests.length === 0) { return; } if (this._requests.length > 0) { this._processRequest(0); } if (this._requests.length > 1 && Math.abs(this._requests[0].rid - this._requests[1].rid) < this.window) { this._processRequest(1); } } }; /** Variable: strip * * BOSH-Connections will have all stanzas wrapped in a tag when * passed to or . * To strip this tag, User code can set to "body": * * > Strophe.Bosh.prototype.strip = "body"; * * This will enable stripping of the body tag in both * and . */ Strophe.Bosh.prototype.strip = null; /* This program is distributed under the terms of the MIT license. Please see the LICENSE file for details. Copyright 2006-2008, OGG, LLC */ /** Class: Strophe.WebSocket * _Private_ helper class that handles WebSocket Connections * * The Strophe.WebSocket class is used internally by Strophe.Connection * to encapsulate WebSocket sessions. It is not meant to be used from user's code. */ /** File: websocket.js * A JavaScript library to enable XMPP over Websocket in Strophejs. * * This file implements XMPP over WebSockets for Strophejs. * If a Connection is established with a Websocket url (ws://...) * Strophe will use WebSockets. * For more information on XMPP-over-WebSocket see RFC 7395: * http://tools.ietf.org/html/rfc7395 * * WebSocket support implemented by Andreas Guth (andreas.guth@rwth-aachen.de) */ Strophe.Websocket = class Websocket { /** PrivateConstructor: Strophe.Websocket * Create and initialize a Strophe.WebSocket object. * Currently only sets the connection Object. * * Parameters: * (Strophe.Connection) connection - The Strophe.Connection that will use WebSockets. * * Returns: * A new Strophe.WebSocket object. */ constructor(connection) { this._conn = connection; this.strip = "wrapper"; const service = connection.service; if (service.indexOf("ws:") !== 0 && service.indexOf("wss:") !== 0) { // If the service is not an absolute URL, assume it is a path and put the absolute // URL together from options, current URL and the path. let new_service = ""; if (connection.options.protocol === "ws" && window.location.protocol !== "https:") { new_service += "ws"; } else { new_service += "wss"; } new_service += "://" + window.location.host; if (service.indexOf("/") !== 0) { new_service += window.location.pathname + service; } else { new_service += service; } connection.service = new_service; } } /** PrivateFunction: _buildStream * _Private_ helper function to generate the start tag for WebSockets * * Returns: * A Strophe.Builder with a element. */ _buildStream() { return $build("open", { "xmlns": Strophe.NS.FRAMING, "to": this._conn.domain, "version": '1.0' }); } /** PrivateFunction: _checkStreamError * _Private_ checks a message for stream:error * * Parameters: * (Strophe.Request) bodyWrap - The received stanza. * connectstatus - The ConnectStatus that will be set on error. * Returns: * true if there was a streamerror, false otherwise. */ _checkStreamError(bodyWrap, connectstatus) { let errors; if (bodyWrap.getElementsByTagNameNS) { errors = bodyWrap.getElementsByTagNameNS(Strophe.NS.STREAM, "error"); } else { errors = bodyWrap.getElementsByTagName("stream:error"); } if (errors.length === 0) { return false; } const error = errors[0]; let condition = ""; let text = ""; const ns = "urn:ietf:params:xml:ns:xmpp-streams"; for (let i = 0; i < error.childNodes.length; i++) { const e = error.childNodes[i]; if (e.getAttribute("xmlns") !== ns) { break; } if (e.nodeName === "text") { text = e.textContent; } else { condition = e.nodeName; } } let errorString = "WebSocket stream error: "; if (condition) { errorString += condition; } else { errorString += "unknown"; } if (text) { errorString += " - " + text; } Strophe.error(errorString); // close the connection on stream_error this._conn._changeConnectStatus(connectstatus, condition); this._conn._doDisconnect(); return true; } /** PrivateFunction: _reset * Reset the connection. * * This function is called by the reset function of the Strophe Connection. * Is not needed by WebSockets. */ _reset() { // eslint-disable-line class-methods-use-this return; } /** PrivateFunction: _connect * _Private_ function called by Strophe.Connection.connect * * Creates a WebSocket for a connection and assigns Callbacks to it. * Does nothing if there already is a WebSocket. */ _connect() { // Ensure that there is no open WebSocket from a previous Connection. this._closeSocket(); this.socket = new WebSocket(this._conn.service, "xmpp"); this.socket.onopen = () => this._onOpen(); this.socket.onerror = e => this._onError(e); this.socket.onclose = e => this._onClose(e); // Gets replaced with this._onMessage once _onInitialMessage is called this.socket.onmessage = message => this._onInitialMessage(message); } /** PrivateFunction: _connect_cb * _Private_ function called by Strophe.Connection._connect_cb * * checks for stream:error * * Parameters: * (Strophe.Request) bodyWrap - The received stanza. */ _connect_cb(bodyWrap) { const error = this._checkStreamError(bodyWrap, Strophe.Status.CONNFAIL); if (error) { return Strophe.Status.CONNFAIL; } } /** PrivateFunction: _handleStreamStart * _Private_ function that checks the opening tag for errors. * * Disconnects if there is an error and returns false, true otherwise. * * Parameters: * (Node) message - Stanza containing the tag. */ _handleStreamStart(message) { let error = false; // Check for errors in the tag const ns = message.getAttribute("xmlns"); if (typeof ns !== "string") { error = "Missing xmlns in "; } else if (ns !== Strophe.NS.FRAMING) { error = "Wrong xmlns in : " + ns; } const ver = message.getAttribute("version"); if (typeof ver !== "string") { error = "Missing version in "; } else if (ver !== "1.0") { error = "Wrong version in : " + ver; } if (error) { this._conn._changeConnectStatus(Strophe.Status.CONNFAIL, error); this._conn._doDisconnect(); return false; } return true; } /** PrivateFunction: _onInitialMessage * _Private_ function that handles the first connection messages. * * On receiving an opening stream tag this callback replaces itself with the real * message handler. On receiving a stream error the connection is terminated. */ _onInitialMessage(message) { if (message.data.indexOf("\s*)*/, ""); if (data === '') return; const streamStart = new DOMParser().parseFromString(data, "text/xml").documentElement; this._conn.xmlInput(streamStart); this._conn.rawInput(message.data); //_handleStreamSteart will check for XML errors and disconnect on error if (this._handleStreamStart(streamStart)) { //_connect_cb will check for stream:error and disconnect on error this._connect_cb(streamStart); } } else if (message.data.indexOf("WSS, WS->ANY const isSecureRedirect = service.indexOf("wss:") >= 0 && see_uri.indexOf("wss:") >= 0 || service.indexOf("ws:") >= 0; if (isSecureRedirect) { this._conn._changeConnectStatus(Strophe.Status.REDIRECT, "Received see-other-uri, resetting connection"); this._conn.reset(); this._conn.service = see_uri; this._connect(); } } else { this._conn._changeConnectStatus(Strophe.Status.CONNFAIL, "Received closing stream"); this._conn._doDisconnect(); } } else { this._replaceMessageHandler(); const string = this._streamWrap(message.data); const elem = new DOMParser().parseFromString(string, "text/xml").documentElement; this._conn._connect_cb(elem, null, message.data); } } /** PrivateFunction: _replaceMessageHandler * * Called by _onInitialMessage in order to replace itself with the general message handler. * This method is overridden by Strophe.WorkerWebsocket, which manages a * websocket connection via a service worker and doesn't have direct access * to the socket. */ _replaceMessageHandler() { this.socket.onmessage = m => this._onMessage(m); } /** PrivateFunction: _disconnect * _Private_ function called by Strophe.Connection.disconnect * * Disconnects and sends a last stanza if one is given * * Parameters: * (Request) pres - This stanza will be sent before disconnecting. */ _disconnect(pres) { if (this.socket && this.socket.readyState !== WebSocket.CLOSED) { if (pres) { this._conn.send(pres); } const close = $build("close", { "xmlns": Strophe.NS.FRAMING }); this._conn.xmlOutput(close.tree()); const closeString = Strophe.serialize(close); this._conn.rawOutput(closeString); try { this.socket.send(closeString); } catch (e) { Strophe.warn("Couldn't send tag."); } } setTimeout(() => this._conn._doDisconnect, 0); } /** PrivateFunction: _doDisconnect * _Private_ function to disconnect. * * Just closes the Socket for WebSockets */ _doDisconnect() { Strophe.debug("WebSockets _doDisconnect was called"); this._closeSocket(); } /** PrivateFunction _streamWrap * _Private_ helper function to wrap a stanza in a tag. * This is used so Strophe can process stanzas from WebSockets like BOSH */ _streamWrap(stanza) { // eslint-disable-line class-methods-use-this return "" + stanza + ''; } /** PrivateFunction: _closeSocket * _Private_ function to close the WebSocket. * * Closes the socket if it is still open and deletes it */ _closeSocket() { if (this.socket) { try { this.socket.onclose = null; this.socket.onerror = null; this.socket.onmessage = null; this.socket.close(); } catch (e) { Strophe.debug(e.message); } } this.socket = null; } /** PrivateFunction: _emptyQueue * _Private_ function to check if the message queue is empty. * * Returns: * True, because WebSocket messages are send immediately after queueing. */ _emptyQueue() { // eslint-disable-line class-methods-use-this return true; } /** PrivateFunction: _onClose * _Private_ function to handle websockets closing. */ _onClose(e) { if (this._conn.connected && !this._conn.disconnecting) { Strophe.error("Websocket closed unexpectedly"); this._conn._doDisconnect(); } else if (e && e.code === 1006 && !this._conn.connected && this.socket) { // in case the onError callback was not called (Safari 10 does not // call onerror when the initial connection fails) we need to // dispatch a CONNFAIL status update to be consistent with the // behavior on other browsers. Strophe.error("Websocket closed unexcectedly"); this._conn._changeConnectStatus(Strophe.Status.CONNFAIL, "The WebSocket connection could not be established or was disconnected."); this._conn._doDisconnect(); } else { Strophe.debug("Websocket closed"); } } /** PrivateFunction: _no_auth_received * * Called on stream start/restart when no stream:features * has been received. */ _no_auth_received(callback) { Strophe.error("Server did not offer a supported authentication mechanism"); this._conn._changeConnectStatus(Strophe.Status.CONNFAIL, Strophe.ErrorCondition.NO_AUTH_MECH); if (callback) { callback.call(this._conn); } this._conn._doDisconnect(); } /** PrivateFunction: _onDisconnectTimeout * _Private_ timeout handler for handling non-graceful disconnection. * * This does nothing for WebSockets */ _onDisconnectTimeout() {} // eslint-disable-line class-methods-use-this /** PrivateFunction: _abortAllRequests * _Private_ helper function that makes sure all pending requests are aborted. */ _abortAllRequests() {} // eslint-disable-line class-methods-use-this /** PrivateFunction: _onError * _Private_ function to handle websockets errors. * * Parameters: * (Object) error - The websocket error. */ _onError(error) { Strophe.error("Websocket error " + JSON.stringify(error)); this._conn._changeConnectStatus(Strophe.Status.CONNFAIL, "The WebSocket connection could not be established or was disconnected."); this._disconnect(); } /** PrivateFunction: _onIdle * _Private_ function called by Strophe.Connection._onIdle * * sends all queued stanzas */ _onIdle() { const data = this._conn._data; if (data.length > 0 && !this._conn.paused) { for (let i = 0; i < data.length; i++) { if (data[i] !== null) { let stanza; if (data[i] === "restart") { stanza = this._buildStream().tree(); } else { stanza = data[i]; } const rawStanza = Strophe.serialize(stanza); this._conn.xmlOutput(stanza); this._conn.rawOutput(rawStanza); this.socket.send(rawStanza); } } this._conn._data = []; } } /** PrivateFunction: _onMessage * _Private_ function to handle websockets messages. * * This function parses each of the messages as if they are full documents. * [TODO : We may actually want to use a SAX Push parser]. * * Since all XMPP traffic starts with * * * The first stanza will always fail to be parsed. * * Additionally, the seconds stanza will always be with * the stream NS defined in the previous stanza, so we need to 'force' * the inclusion of the NS in this stanza. * * Parameters: * (string) message - The websocket message. */ _onMessage(message) { let elem; // check for closing stream const close = ''; if (message.data === close) { this._conn.rawInput(close); this._conn.xmlInput(message); if (!this._conn.disconnecting) { this._conn._doDisconnect(); } return; } else if (message.data.search(" tag before we close the connection return; } this._conn._dataRecv(elem, message.data); } /** PrivateFunction: _onOpen * _Private_ function to handle websockets connection setup. * * The opening stream tag is sent here. */ _onOpen() { Strophe.debug("Websocket open"); const start = this._buildStream(); this._conn.xmlOutput(start.tree()); const startString = Strophe.serialize(start); this._conn.rawOutput(startString); this.socket.send(startString); } /** PrivateFunction: _reqToData * _Private_ function to get a stanza out of a request. * * WebSockets don't use requests, so the passed argument is just returned. * * Parameters: * (Object) stanza - The stanza. * * Returns: * The stanza that was passed. */ _reqToData(stanza) { // eslint-disable-line class-methods-use-this return stanza; } /** PrivateFunction: _send * _Private_ part of the Connection.send function for WebSocket * * Just flushes the messages that are in the queue */ _send() { this._conn.flush(); } /** PrivateFunction: _sendRestart * * Send an xmpp:restart stanza. */ _sendRestart() { clearTimeout(this._conn._idleTimeout); this._conn._onIdle.bind(this._conn)(); } }; /* This program is distributed under the terms of the MIT license. Please see the LICENSE file for details. Copyright 2020, JC Brand */ const lmap = {}; lmap['debug'] = Strophe.LogLevel.DEBUG; lmap['info'] = Strophe.LogLevel.INFO; lmap['warn'] = Strophe.LogLevel.WARN; lmap['error'] = Strophe.LogLevel.ERROR; lmap['fatal'] = Strophe.LogLevel.FATAL; /** Class: Strophe.WorkerWebsocket * _Private_ helper class that handles a websocket connection inside a shared worker. */ Strophe.WorkerWebsocket = class WorkerWebsocket extends Strophe.Websocket { /** PrivateConstructor: Strophe.WorkerWebsocket * Create and initialize a Strophe.WorkerWebsocket object. * * Parameters: * (Strophe.Connection) connection - The Strophe.Connection * * Returns: * A new Strophe.WorkerWebsocket object. */ constructor(connection) { super(connection); this._conn = connection; this.worker = new SharedWorker(this._conn.options.worker, 'Strophe XMPP Connection'); this.worker.onerror = e => { var _console; (_console = console) === null || _console === void 0 ? void 0 : _console.error(e); Strophe.log(Strophe.LogLevel.ERROR, `Shared Worker Error: ${e}`); }; } get socket() { return { 'send': str => this.worker.port.postMessage(['send', str]) }; } _connect() { this._messageHandler = m => this._onInitialMessage(m); this.worker.port.start(); this.worker.port.onmessage = ev => this._onWorkerMessage(ev); this.worker.port.postMessage(['_connect', this._conn.service, this._conn.jid]); } _attach(callback) { this._messageHandler = m => this._onMessage(m); this._conn.connect_callback = callback; this.worker.port.start(); this.worker.port.onmessage = ev => this._onWorkerMessage(ev); this.worker.port.postMessage(['_attach', this._conn.service]); } _attachCallback(status, jid) { if (status === Strophe.Status.ATTACHED) { this._conn.jid = jid; this._conn.authenticated = true; this._conn.connected = true; this._conn.restored = true; this._conn._changeConnectStatus(Strophe.Status.ATTACHED); } else if (status === Strophe.Status.ATTACHFAIL) { this._conn.authenticated = false; this._conn.connected = false; this._conn.restored = false; this._conn._changeConnectStatus(Strophe.Status.ATTACHFAIL); } } _disconnect(readyState, pres) { pres && this._conn.send(pres); const close = $build("close", { "xmlns": Strophe.NS.FRAMING }); this._conn.xmlOutput(close.tree()); const closeString = Strophe.serialize(close); this._conn.rawOutput(closeString); this.worker.port.postMessage(['send', closeString]); this._conn._doDisconnect(); } _onClose(e) { if (this._conn.connected && !this._conn.disconnecting) { Strophe.error("Websocket closed unexpectedly"); this._conn._doDisconnect(); } else if (e && e.code === 1006 && !this._conn.connected) { // in case the onError callback was not called (Safari 10 does not // call onerror when the initial connection fails) we need to // dispatch a CONNFAIL status update to be consistent with the // behavior on other browsers. Strophe.error("Websocket closed unexcectedly"); this._conn._changeConnectStatus(Strophe.Status.CONNFAIL, "The WebSocket connection could not be established or was disconnected."); this._conn._doDisconnect(); } else { Strophe.debug("Websocket closed"); } } _closeSocket() { this.worker.port.postMessage(['_closeSocket']); } /** PrivateFunction: _replaceMessageHandler * * Called by _onInitialMessage in order to replace itself with the general message handler. * This method is overridden by Strophe.WorkerWebsocket, which manages a * websocket connection via a service worker and doesn't have direct access * to the socket. */ _replaceMessageHandler() { this._messageHandler = m => this._onMessage(m); } /** PrivateFunction: _onWorkerMessage * _Private_ function that handles messages received from the service worker */ _onWorkerMessage(ev) { const { data } = ev; const method_name = data[0]; if (method_name === '_onMessage') { this._messageHandler(data[1]); } else if (method_name in this) { try { this[method_name].apply(this, ev.data.slice(1)); } catch (e) { Strophe.log(Strophe.LogLevel.ERROR, e); } } else if (method_name === 'log') { const level = data[1]; const msg = data[2]; Strophe.log(lmap[level], msg); } else { Strophe.log(Strophe.LogLevel.ERROR, `Found unhandled service worker message: ${data}`); } } }; global$1.$build = core.$build; global$1.$iq = core.$iq; global$1.$msg = core.$msg; global$1.$pres = core.$pres; global$1.Strophe = core.Strophe; const { b64_sha1 } = SHA1; exports.$build = $build; exports.$iq = $iq; exports.$msg = $msg; exports.$pres = $pres; exports.Strophe = Strophe; exports.b64_sha1 = b64_sha1; Object.defineProperty(exports, '__esModule', { value: true }); })); }).call(this)}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) },{"@xmldom/xmldom":5,"ws":209}],209:[function(require,module,exports){ 'use strict'; module.exports = function () { throw new Error( 'ws does not work in the browser. Browser clients must use the native ' + 'WebSocket object' ); }; },{}],210:[function(require,module,exports){ (function (setImmediate,clearImmediate){(function (){ var nextTick = require('process/browser.js').nextTick; var apply = Function.prototype.apply; var slice = Array.prototype.slice; var immediateIds = {}; var nextImmediateId = 0; // DOM APIs, for completeness exports.setTimeout = function() { return new Timeout(apply.call(setTimeout, window, arguments), clearTimeout); }; exports.setInterval = function() { return new Timeout(apply.call(setInterval, window, arguments), clearInterval); }; exports.clearTimeout = exports.clearInterval = function(timeout) { timeout.close(); }; function Timeout(id, clearFn) { this._id = id; this._clearFn = clearFn; } Timeout.prototype.unref = Timeout.prototype.ref = function() {}; Timeout.prototype.close = function() { this._clearFn.call(window, this._id); }; // Does not start the time, just sets up the members needed. exports.enroll = function(item, msecs) { clearTimeout(item._idleTimeoutId); item._idleTimeout = msecs; }; exports.unenroll = function(item) { clearTimeout(item._idleTimeoutId); item._idleTimeout = -1; }; exports._unrefActive = exports.active = function(item) { clearTimeout(item._idleTimeoutId); var msecs = item._idleTimeout; if (msecs >= 0) { item._idleTimeoutId = setTimeout(function onTimeout() { if (item._onTimeout) item._onTimeout(); }, msecs); } }; // That's not how node.js implements it but the exposed api is the same. exports.setImmediate = typeof setImmediate === "function" ? setImmediate : function(fn) { var id = nextImmediateId++; var args = arguments.length < 2 ? false : slice.call(arguments, 1); immediateIds[id] = true; nextTick(function onNextTick() { if (immediateIds[id]) { // fn.call() is faster so we optimize for the common use-case // @see http://jsperf.com/call-apply-segu if (args) { fn.apply(null, args); } else { fn.call(null); } // Prevent ids from leaking exports.clearImmediate(id); } }); return id; }; exports.clearImmediate = typeof clearImmediate === "function" ? clearImmediate : function(id) { delete immediateIds[id]; }; }).call(this)}).call(this,require("timers").setImmediate,require("timers").clearImmediate) },{"process/browser.js":152,"timers":210}],211:[function(require,module,exports){ var Buffer = require('buffer').Buffer module.exports = function (buf) { // If the buffer is backed by a Uint8Array, a faster version will work if (buf instanceof Uint8Array) { // If the buffer isn't a subarray, return the underlying ArrayBuffer if (buf.byteOffset === 0 && buf.byteLength === buf.buffer.byteLength) { return buf.buffer } else if (typeof buf.buffer.slice === 'function') { // Otherwise we need to get a proper copy return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength) } } if (Buffer.isBuffer(buf)) { // This is the slow version that will work with any Buffer // implementation (even in old browsers) var arrayCopy = new Uint8Array(buf.length) var len = buf.length for (var i = 0; i < len; i++) { arrayCopy[i] = buf[i] } return arrayCopy.buffer } else { throw new Error('Argument must be a Buffer') } } },{"buffer":47}],212:[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. 'use strict'; var punycode = require('punycode'); var util = require('./util'); exports.parse = urlParse; exports.resolve = urlResolve; exports.resolveObject = urlResolveObject; exports.format = urlFormat; exports.Url = Url; function Url() { this.protocol = null; this.slashes = null; this.auth = null; this.host = null; this.port = null; this.hostname = null; this.hash = null; this.search = null; this.query = null; this.pathname = null; this.path = null; this.href = null; } // Reference: RFC 3986, RFC 1808, RFC 2396 // define these here so at least they only have to be // compiled once on the first module load. var protocolPattern = /^([a-z0-9.+-]+:)/i, portPattern = /:[0-9]*$/, // Special case for a simple path URL simplePathPattern = /^(\/\/?(?!\/)[^\?\s]*)(\?[^\s]*)?$/, // RFC 2396: characters reserved for delimiting URLs. // We actually just auto-escape these. delims = ['<', '>', '"', '`', ' ', '\r', '\n', '\t'], // RFC 2396: characters not allowed for various reasons. unwise = ['{', '}', '|', '\\', '^', '`'].concat(delims), // Allowed by RFCs, but cause of XSS attacks. Always escape these. autoEscape = ['\''].concat(unwise), // Characters that are never ever allowed in a hostname. // Note that any invalid chars are also handled, but these // are the ones that are *expected* to be seen, so we fast-path // them. nonHostChars = ['%', '/', '?', ';', '#'].concat(autoEscape), hostEndingChars = ['/', '?', '#'], hostnameMaxLen = 255, hostnamePartPattern = /^[+a-z0-9A-Z_-]{0,63}$/, hostnamePartStart = /^([+a-z0-9A-Z_-]{0,63})(.*)$/, // protocols that can allow "unsafe" and "unwise" chars. unsafeProtocol = { 'javascript': true, 'javascript:': true }, // protocols that never have a hostname. hostlessProtocol = { 'javascript': true, 'javascript:': true }, // protocols that always contain a // bit. slashedProtocol = { 'http': true, 'https': true, 'ftp': true, 'gopher': true, 'file': true, 'http:': true, 'https:': true, 'ftp:': true, 'gopher:': true, 'file:': true }, querystring = require('querystring'); function urlParse(url, parseQueryString, slashesDenoteHost) { if (url && util.isObject(url) && url instanceof Url) return url; var u = new Url; u.parse(url, parseQueryString, slashesDenoteHost); return u; } Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) { if (!util.isString(url)) { throw new TypeError("Parameter 'url' must be a string, not " + typeof url); } // Copy chrome, IE, opera backslash-handling behavior. // Back slashes before the query string get converted to forward slashes // See: https://code.google.com/p/chromium/issues/detail?id=25916 var queryIndex = url.indexOf('?'), splitter = (queryIndex !== -1 && queryIndex < url.indexOf('#')) ? '?' : '#', uSplit = url.split(splitter), slashRegex = /\\/g; uSplit[0] = uSplit[0].replace(slashRegex, '/'); url = uSplit.join(splitter); var rest = url; // trim before proceeding. // This is to support parse stuff like " http://foo.com \n" rest = rest.trim(); if (!slashesDenoteHost && url.split('#').length === 1) { // Try fast path regexp var simplePath = simplePathPattern.exec(rest); if (simplePath) { this.path = rest; this.href = rest; this.pathname = simplePath[1]; if (simplePath[2]) { this.search = simplePath[2]; if (parseQueryString) { this.query = querystring.parse(this.search.substr(1)); } else { this.query = this.search.substr(1); } } else if (parseQueryString) { this.search = ''; this.query = {}; } return this; } } var proto = protocolPattern.exec(rest); if (proto) { proto = proto[0]; var lowerProto = proto.toLowerCase(); this.protocol = lowerProto; rest = rest.substr(proto.length); } // figure out if it's got a host // user@server is *always* interpreted as a hostname, and url // resolution will treat //foo/bar as host=foo,path=bar because that's // how the browser resolves relative URLs. if (slashesDenoteHost || proto || rest.match(/^\/\/[^@\/]+@[^@\/]+/)) { var slashes = rest.substr(0, 2) === '//'; if (slashes && !(proto && hostlessProtocol[proto])) { rest = rest.substr(2); this.slashes = true; } } if (!hostlessProtocol[proto] && (slashes || (proto && !slashedProtocol[proto]))) { // there's a hostname. // the first instance of /, ?, ;, or # ends the host. // // If there is an @ in the hostname, then non-host chars *are* allowed // to the left of the last @ sign, unless some host-ending character // comes *before* the @-sign. // URLs are obnoxious. // // ex: // http://a@b@c/ => user:a@b host:c // http://a@b?@c => user:a host:c path:/?@c // v0.12 TODO(isaacs): This is not quite how Chrome does things. // Review our test case against browsers more comprehensively. // find the first instance of any hostEndingChars var hostEnd = -1; for (var i = 0; i < hostEndingChars.length; i++) { var hec = rest.indexOf(hostEndingChars[i]); if (hec !== -1 && (hostEnd === -1 || hec < hostEnd)) hostEnd = hec; } // at this point, either we have an explicit point where the // auth portion cannot go past, or the last @ char is the decider. var auth, atSign; if (hostEnd === -1) { // atSign can be anywhere. atSign = rest.lastIndexOf('@'); } else { // atSign must be in auth portion. // http://a@b/c@d => host:b auth:a path:/c@d atSign = rest.lastIndexOf('@', hostEnd); } // Now we have a portion which is definitely the auth. // Pull that off. if (atSign !== -1) { auth = rest.slice(0, atSign); rest = rest.slice(atSign + 1); this.auth = decodeURIComponent(auth); } // the host is the remaining to the left of the first non-host char hostEnd = -1; for (var i = 0; i < nonHostChars.length; i++) { var hec = rest.indexOf(nonHostChars[i]); if (hec !== -1 && (hostEnd === -1 || hec < hostEnd)) hostEnd = hec; } // if we still have not hit it, then the entire thing is a host. if (hostEnd === -1) hostEnd = rest.length; this.host = rest.slice(0, hostEnd); rest = rest.slice(hostEnd); // pull out port. this.parseHost(); // we've indicated that there is a hostname, // so even if it's empty, it has to be present. this.hostname = this.hostname || ''; // if hostname begins with [ and ends with ] // assume that it's an IPv6 address. var ipv6Hostname = this.hostname[0] === '[' && this.hostname[this.hostname.length - 1] === ']'; // validate a little. if (!ipv6Hostname) { var hostparts = this.hostname.split(/\./); for (var i = 0, l = hostparts.length; i < l; i++) { var part = hostparts[i]; if (!part) continue; if (!part.match(hostnamePartPattern)) { var newpart = ''; for (var j = 0, k = part.length; j < k; j++) { if (part.charCodeAt(j) > 127) { // we replace non-ASCII char with a temporary placeholder // we need this to make sure size of hostname is not // broken by replacing non-ASCII by nothing newpart += 'x'; } else { newpart += part[j]; } } // we test again with ASCII char only if (!newpart.match(hostnamePartPattern)) { var validParts = hostparts.slice(0, i); var notHost = hostparts.slice(i + 1); var bit = part.match(hostnamePartStart); if (bit) { validParts.push(bit[1]); notHost.unshift(bit[2]); } if (notHost.length) { rest = '/' + notHost.join('.') + rest; } this.hostname = validParts.join('.'); break; } } } } if (this.hostname.length > hostnameMaxLen) { this.hostname = ''; } else { // hostnames are always lower case. this.hostname = this.hostname.toLowerCase(); } if (!ipv6Hostname) { // IDNA Support: Returns a punycoded representation of "domain". // It only converts parts of the domain name that // have non-ASCII characters, i.e. it doesn't matter if // you call it with a domain that already is ASCII-only. this.hostname = punycode.toASCII(this.hostname); } var p = this.port ? ':' + this.port : ''; var h = this.hostname || ''; this.host = h + p; this.href += this.host; // strip [ and ] from the hostname // the host field still retains them, though if (ipv6Hostname) { this.hostname = this.hostname.substr(1, this.hostname.length - 2); if (rest[0] !== '/') { rest = '/' + rest; } } } // now rest is set to the post-host stuff. // chop off any delim chars. if (!unsafeProtocol[lowerProto]) { // First, make 100% sure that any "autoEscape" chars get // escaped, even if encodeURIComponent doesn't think they // need to be. for (var i = 0, l = autoEscape.length; i < l; i++) { var ae = autoEscape[i]; if (rest.indexOf(ae) === -1) continue; var esc = encodeURIComponent(ae); if (esc === ae) { esc = escape(ae); } rest = rest.split(ae).join(esc); } } // chop off from the tail first. var hash = rest.indexOf('#'); if (hash !== -1) { // got a fragment string. this.hash = rest.substr(hash); rest = rest.slice(0, hash); } var qm = rest.indexOf('?'); if (qm !== -1) { this.search = rest.substr(qm); this.query = rest.substr(qm + 1); if (parseQueryString) { this.query = querystring.parse(this.query); } rest = rest.slice(0, qm); } else if (parseQueryString) { // no query string, but parseQueryString still requested this.search = ''; this.query = {}; } if (rest) this.pathname = rest; if (slashedProtocol[lowerProto] && this.hostname && !this.pathname) { this.pathname = '/'; } //to support http.request if (this.pathname || this.search) { var p = this.pathname || ''; var s = this.search || ''; this.path = p + s; } // finally, reconstruct the href based on what has been validated. this.href = this.format(); return this; }; // format a parsed object into a url string function urlFormat(obj) { // ensure it's an object, and not a string url. // If it's an obj, this is a no-op. // this way, you can call url_format() on strings // to clean up potentially wonky urls. if (util.isString(obj)) obj = urlParse(obj); if (!(obj instanceof Url)) return Url.prototype.format.call(obj); return obj.format(); } Url.prototype.format = function() { var auth = this.auth || ''; if (auth) { auth = encodeURIComponent(auth); auth = auth.replace(/%3A/i, ':'); auth += '@'; } var protocol = this.protocol || '', pathname = this.pathname || '', hash = this.hash || '', host = false, query = ''; if (this.host) { host = auth + this.host; } else if (this.hostname) { host = auth + (this.hostname.indexOf(':') === -1 ? this.hostname : '[' + this.hostname + ']'); if (this.port) { host += ':' + this.port; } } if (this.query && util.isObject(this.query) && Object.keys(this.query).length) { query = querystring.stringify(this.query); } var search = this.search || (query && ('?' + query)) || ''; if (protocol && protocol.substr(-1) !== ':') protocol += ':'; // only the slashedProtocols get the //. Not mailto:, xmpp:, etc. // unless they had them to begin with. if (this.slashes || (!protocol || slashedProtocol[protocol]) && host !== false) { host = '//' + (host || ''); if (pathname && pathname.charAt(0) !== '/') pathname = '/' + pathname; } else if (!host) { host = ''; } if (hash && hash.charAt(0) !== '#') hash = '#' + hash; if (search && search.charAt(0) !== '?') search = '?' + search; pathname = pathname.replace(/[?#]/g, function(match) { return encodeURIComponent(match); }); search = search.replace('#', '%23'); return protocol + host + pathname + search + hash; }; function urlResolve(source, relative) { return urlParse(source, false, true).resolve(relative); } Url.prototype.resolve = function(relative) { return this.resolveObject(urlParse(relative, false, true)).format(); }; function urlResolveObject(source, relative) { if (!source) return relative; return urlParse(source, false, true).resolveObject(relative); } Url.prototype.resolveObject = function(relative) { if (util.isString(relative)) { var rel = new Url(); rel.parse(relative, false, true); relative = rel; } var result = new Url(); var tkeys = Object.keys(this); for (var tk = 0; tk < tkeys.length; tk++) { var tkey = tkeys[tk]; result[tkey] = this[tkey]; } // hash is always overridden, no matter what. // even href="" will remove it. result.hash = relative.hash; // if the relative url is empty, then there's nothing left to do here. if (relative.href === '') { result.href = result.format(); return result; } // hrefs like //foo/bar always cut to the protocol. if (relative.slashes && !relative.protocol) { // take everything except the protocol from relative var rkeys = Object.keys(relative); for (var rk = 0; rk < rkeys.length; rk++) { var rkey = rkeys[rk]; if (rkey !== 'protocol') result[rkey] = relative[rkey]; } //urlParse appends trailing / to urls like http://www.example.com if (slashedProtocol[result.protocol] && result.hostname && !result.pathname) { result.path = result.pathname = '/'; } result.href = result.format(); return result; } if (relative.protocol && relative.protocol !== result.protocol) { // if it's a known url protocol, then changing // the protocol does weird things // first, if it's not file:, then we MUST have a host, // and if there was a path // to begin with, then we MUST have a path. // if it is file:, then the host is dropped, // because that's known to be hostless. // anything else is assumed to be absolute. if (!slashedProtocol[relative.protocol]) { var keys = Object.keys(relative); for (var v = 0; v < keys.length; v++) { var k = keys[v]; result[k] = relative[k]; } result.href = result.format(); return result; } result.protocol = relative.protocol; if (!relative.host && !hostlessProtocol[relative.protocol]) { var relPath = (relative.pathname || '').split('/'); while (relPath.length && !(relative.host = relPath.shift())); if (!relative.host) relative.host = ''; if (!relative.hostname) relative.hostname = ''; if (relPath[0] !== '') relPath.unshift(''); if (relPath.length < 2) relPath.unshift(''); result.pathname = relPath.join('/'); } else { result.pathname = relative.pathname; } result.search = relative.search; result.query = relative.query; result.host = relative.host || ''; result.auth = relative.auth; result.hostname = relative.hostname || relative.host; result.port = relative.port; // to support http.request if (result.pathname || result.search) { var p = result.pathname || ''; var s = result.search || ''; result.path = p + s; } result.slashes = result.slashes || relative.slashes; result.href = result.format(); return result; } var isSourceAbs = (result.pathname && result.pathname.charAt(0) === '/'), isRelAbs = ( relative.host || relative.pathname && relative.pathname.charAt(0) === '/' ), mustEndAbs = (isRelAbs || isSourceAbs || (result.host && relative.pathname)), removeAllDots = mustEndAbs, srcPath = result.pathname && result.pathname.split('/') || [], relPath = relative.pathname && relative.pathname.split('/') || [], psychotic = result.protocol && !slashedProtocol[result.protocol]; // if the url is a non-slashed url, then relative // links like ../.. should be able // to crawl up to the hostname, as well. This is strange. // result.protocol has already been set by now. // Later on, put the first path part into the host field. if (psychotic) { result.hostname = ''; result.port = null; if (result.host) { if (srcPath[0] === '') srcPath[0] = result.host; else srcPath.unshift(result.host); } result.host = ''; if (relative.protocol) { relative.hostname = null; relative.port = null; if (relative.host) { if (relPath[0] === '') relPath[0] = relative.host; else relPath.unshift(relative.host); } relative.host = null; } mustEndAbs = mustEndAbs && (relPath[0] === '' || srcPath[0] === ''); } if (isRelAbs) { // it's absolute. result.host = (relative.host || relative.host === '') ? relative.host : result.host; result.hostname = (relative.hostname || relative.hostname === '') ? relative.hostname : result.hostname; result.search = relative.search; result.query = relative.query; srcPath = relPath; // fall through to the dot-handling below. } else if (relPath.length) { // it's relative // throw away the existing file, and take the new path instead. if (!srcPath) srcPath = []; srcPath.pop(); srcPath = srcPath.concat(relPath); result.search = relative.search; result.query = relative.query; } else if (!util.isNullOrUndefined(relative.search)) { // just pull out the search. // like href='?foo'. // Put this after the other two cases because it simplifies the booleans if (psychotic) { result.hostname = result.host = srcPath.shift(); //occationaly the auth can get stuck only in host //this especially happens in cases like //url.resolveObject('mailto:local1@domain1', 'local2@domain2') var authInHost = result.host && result.host.indexOf('@') > 0 ? result.host.split('@') : false; if (authInHost) { result.auth = authInHost.shift(); result.host = result.hostname = authInHost.shift(); } } result.search = relative.search; result.query = relative.query; //to support http.request if (!util.isNull(result.pathname) || !util.isNull(result.search)) { result.path = (result.pathname ? result.pathname : '') + (result.search ? result.search : ''); } result.href = result.format(); return result; } if (!srcPath.length) { // no path at all. easy. // we've already handled the other stuff above. result.pathname = null; //to support http.request if (result.search) { result.path = '/' + result.search; } else { result.path = null; } result.href = result.format(); return result; } // if a url ENDs in . or .., then it must get a trailing slash. // however, if it ends in anything else non-slashy, // then it must NOT get a trailing slash. var last = srcPath.slice(-1)[0]; var hasTrailingSlash = ( (result.host || relative.host || srcPath.length > 1) && (last === '.' || last === '..') || last === ''); // strip single dots, resolve double dots to parent dir // if the path tries to go above the root, `up` ends up > 0 var up = 0; for (var i = srcPath.length; i >= 0; i--) { last = srcPath[i]; if (last === '.') { srcPath.splice(i, 1); } else if (last === '..') { srcPath.splice(i, 1); up++; } else if (up) { srcPath.splice(i, 1); up--; } } // if the path is allowed to go above the root, restore leading ..s if (!mustEndAbs && !removeAllDots) { for (; up--; up) { srcPath.unshift('..'); } } if (mustEndAbs && srcPath[0] !== '' && (!srcPath[0] || srcPath[0].charAt(0) !== '/')) { srcPath.unshift(''); } if (hasTrailingSlash && (srcPath.join('/').substr(-1) !== '/')) { srcPath.push(''); } var isAbsolute = srcPath[0] === '' || (srcPath[0] && srcPath[0].charAt(0) === '/'); // put the host back if (psychotic) { result.hostname = result.host = isAbsolute ? '' : srcPath.length ? srcPath.shift() : ''; //occationaly the auth can get stuck only in host //this especially happens in cases like //url.resolveObject('mailto:local1@domain1', 'local2@domain2') var authInHost = result.host && result.host.indexOf('@') > 0 ? result.host.split('@') : false; if (authInHost) { result.auth = authInHost.shift(); result.host = result.hostname = authInHost.shift(); } } mustEndAbs = mustEndAbs || (result.host && srcPath.length); if (mustEndAbs && !isAbsolute) { srcPath.unshift(''); } if (!srcPath.length) { result.pathname = null; result.path = null; } else { result.pathname = srcPath.join('/'); } //to support request.http if (!util.isNull(result.pathname) || !util.isNull(result.search)) { result.path = (result.pathname ? result.pathname : '') + (result.search ? result.search : ''); } result.auth = relative.auth || result.auth; result.slashes = result.slashes || relative.slashes; result.href = result.format(); return result; }; Url.prototype.parseHost = function() { var host = this.host; var port = portPattern.exec(host); if (port) { port = port[0]; if (port !== ':') { this.port = port.substr(1); } host = host.substr(0, host.length - port.length); } if (host) this.hostname = host; }; },{"./util":213,"punycode":33,"querystring":155}],213:[function(require,module,exports){ 'use strict'; module.exports = { isString: function(arg) { return typeof(arg) === 'string'; }, isObject: function(arg) { return typeof(arg) === 'object' && arg !== null; }, isNull: function(arg) { return arg === null; }, isNullOrUndefined: function(arg) { return arg == null; } }; },{}],214:[function(require,module,exports){ (function (global){(function (){ /** * Module exports. */ module.exports = deprecate; /** * Mark that a method should not be used. * Returns a modified function which warns once by default. * * If `localStorage.noDeprecation = true` is set, then it is a no-op. * * If `localStorage.throwDeprecation = true` is set, then deprecated functions * will throw an Error when invoked. * * If `localStorage.traceDeprecation = true` is set, then deprecated functions * will invoke `console.trace()` instead of `console.error()`. * * @param {Function} fn - the function to deprecate * @param {String} msg - the string to print to the console when `fn` is invoked * @returns {Function} a new "deprecated" version of `fn` * @api public */ function deprecate (fn, msg) { if (config('noDeprecation')) { return fn; } var warned = false; function deprecated() { if (!warned) { if (config('throwDeprecation')) { throw new Error(msg); } else if (config('traceDeprecation')) { console.trace(msg); } else { console.warn(msg); } warned = true; } return fn.apply(this, arguments); } return deprecated; } /** * Checks `localStorage` for boolean values for the given `name`. * * @param {String} name * @returns {Boolean} * @api private */ function config (name) { // accessing global.localStorage can trigger a DOMException in sandboxed iframes try { if (!global.localStorage) return false; } catch (_) { return false; } var val = global.localStorage[name]; if (null == val) return false; return String(val).toLowerCase() === 'true'; } }).call(this)}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) },{}],215:[function(require,module,exports){ arguments[4][18][0].apply(exports,arguments) },{"dup":18}],216:[function(require,module,exports){ arguments[4][19][0].apply(exports,arguments) },{"dup":19}],217:[function(require,module,exports){ arguments[4][20][0].apply(exports,arguments) },{"./support/isBuffer":216,"_process":152,"dup":20,"inherits":215}],218:[function(require,module,exports){ /* * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. */ /* eslint-env node */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _adapter_factory = require('./adapter_factory.js'); var adapter = (0, _adapter_factory.adapterFactory)({ window: typeof window === 'undefined' ? undefined : window }); exports.default = adapter; },{"./adapter_factory.js":219}],219:[function(require,module,exports){ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.adapterFactory = adapterFactory; var _utils = require('./utils'); var utils = _interopRequireWildcard(_utils); var _chrome_shim = require('./chrome/chrome_shim'); var chromeShim = _interopRequireWildcard(_chrome_shim); var _edge_shim = require('./edge/edge_shim'); var edgeShim = _interopRequireWildcard(_edge_shim); var _firefox_shim = require('./firefox/firefox_shim'); var firefoxShim = _interopRequireWildcard(_firefox_shim); var _safari_shim = require('./safari/safari_shim'); var safariShim = _interopRequireWildcard(_safari_shim); var _common_shim = require('./common_shim'); var commonShim = _interopRequireWildcard(_common_shim); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } // Shimming starts here. /* * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. */ function adapterFactory() { var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, window = _ref.window; var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { shimChrome: true, shimFirefox: true, shimEdge: true, shimSafari: true }; // Utils. var logging = utils.log; var browserDetails = utils.detectBrowser(window); var adapter = { browserDetails: browserDetails, commonShim: commonShim, extractVersion: utils.extractVersion, disableLog: utils.disableLog, disableWarnings: utils.disableWarnings }; // Shim browser if found. switch (browserDetails.browser) { case 'chrome': if (!chromeShim || !chromeShim.shimPeerConnection || !options.shimChrome) { logging('Chrome shim is not included in this adapter release.'); return adapter; } if (browserDetails.version === null) { logging('Chrome shim can not determine version, not shimming.'); return adapter; } logging('adapter.js shimming chrome.'); // Export to the adapter global object visible in the browser. adapter.browserShim = chromeShim; // Must be called before shimPeerConnection. commonShim.shimAddIceCandidateNullOrEmpty(window, browserDetails); chromeShim.shimGetUserMedia(window, browserDetails); chromeShim.shimMediaStream(window, browserDetails); chromeShim.shimPeerConnection(window, browserDetails); chromeShim.shimOnTrack(window, browserDetails); chromeShim.shimAddTrackRemoveTrack(window, browserDetails); chromeShim.shimGetSendersWithDtmf(window, browserDetails); chromeShim.shimGetStats(window, browserDetails); chromeShim.shimSenderReceiverGetStats(window, browserDetails); chromeShim.fixNegotiationNeeded(window, browserDetails); commonShim.shimRTCIceCandidate(window, browserDetails); commonShim.shimConnectionState(window, browserDetails); commonShim.shimMaxMessageSize(window, browserDetails); commonShim.shimSendThrowTypeError(window, browserDetails); commonShim.removeExtmapAllowMixed(window, browserDetails); break; case 'firefox': if (!firefoxShim || !firefoxShim.shimPeerConnection || !options.shimFirefox) { logging('Firefox shim is not included in this adapter release.'); return adapter; } logging('adapter.js shimming firefox.'); // Export to the adapter global object visible in the browser. adapter.browserShim = firefoxShim; // Must be called before shimPeerConnection. commonShim.shimAddIceCandidateNullOrEmpty(window, browserDetails); firefoxShim.shimGetUserMedia(window, browserDetails); firefoxShim.shimPeerConnection(window, browserDetails); firefoxShim.shimOnTrack(window, browserDetails); firefoxShim.shimRemoveStream(window, browserDetails); firefoxShim.shimSenderGetStats(window, browserDetails); firefoxShim.shimReceiverGetStats(window, browserDetails); firefoxShim.shimRTCDataChannel(window, browserDetails); firefoxShim.shimAddTransceiver(window, browserDetails); firefoxShim.shimGetParameters(window, browserDetails); firefoxShim.shimCreateOffer(window, browserDetails); firefoxShim.shimCreateAnswer(window, browserDetails); commonShim.shimRTCIceCandidate(window, browserDetails); commonShim.shimConnectionState(window, browserDetails); commonShim.shimMaxMessageSize(window, browserDetails); commonShim.shimSendThrowTypeError(window, browserDetails); break; case 'edge': if (!edgeShim || !edgeShim.shimPeerConnection || !options.shimEdge) { logging('MS edge shim is not included in this adapter release.'); return adapter; } logging('adapter.js shimming edge.'); // Export to the adapter global object visible in the browser. adapter.browserShim = edgeShim; edgeShim.shimGetUserMedia(window, browserDetails); edgeShim.shimGetDisplayMedia(window, browserDetails); edgeShim.shimPeerConnection(window, browserDetails); edgeShim.shimReplaceTrack(window, browserDetails); // the edge shim implements the full RTCIceCandidate object. commonShim.shimMaxMessageSize(window, browserDetails); commonShim.shimSendThrowTypeError(window, browserDetails); break; case 'safari': if (!safariShim || !options.shimSafari) { logging('Safari shim is not included in this adapter release.'); return adapter; } logging('adapter.js shimming safari.'); // Export to the adapter global object visible in the browser. adapter.browserShim = safariShim; // Must be called before shimCallbackAPI. commonShim.shimAddIceCandidateNullOrEmpty(window, browserDetails); safariShim.shimRTCIceServerUrls(window, browserDetails); safariShim.shimCreateOfferLegacy(window, browserDetails); safariShim.shimCallbacksAPI(window, browserDetails); safariShim.shimLocalStreamsAPI(window, browserDetails); safariShim.shimRemoteStreamsAPI(window, browserDetails); safariShim.shimTrackEventTransceiver(window, browserDetails); safariShim.shimGetUserMedia(window, browserDetails); safariShim.shimAudioContext(window, browserDetails); commonShim.shimRTCIceCandidate(window, browserDetails); commonShim.shimMaxMessageSize(window, browserDetails); commonShim.shimSendThrowTypeError(window, browserDetails); commonShim.removeExtmapAllowMixed(window, browserDetails); break; default: logging('Unsupported browser!'); break; } return adapter; } // Browser shims. },{"./chrome/chrome_shim":220,"./common_shim":223,"./edge/edge_shim":224,"./firefox/firefox_shim":228,"./safari/safari_shim":231,"./utils":232}],220:[function(require,module,exports){ /* * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. */ /* eslint-env node */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.shimGetDisplayMedia = exports.shimGetUserMedia = undefined; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var _getusermedia = require('./getusermedia'); Object.defineProperty(exports, 'shimGetUserMedia', { enumerable: true, get: function get() { return _getusermedia.shimGetUserMedia; } }); var _getdisplaymedia = require('./getdisplaymedia'); Object.defineProperty(exports, 'shimGetDisplayMedia', { enumerable: true, get: function get() { return _getdisplaymedia.shimGetDisplayMedia; } }); exports.shimMediaStream = shimMediaStream; exports.shimOnTrack = shimOnTrack; exports.shimGetSendersWithDtmf = shimGetSendersWithDtmf; exports.shimGetStats = shimGetStats; exports.shimSenderReceiverGetStats = shimSenderReceiverGetStats; exports.shimAddTrackRemoveTrackWithNative = shimAddTrackRemoveTrackWithNative; exports.shimAddTrackRemoveTrack = shimAddTrackRemoveTrack; exports.shimPeerConnection = shimPeerConnection; exports.fixNegotiationNeeded = fixNegotiationNeeded; var _utils = require('../utils.js'); var utils = _interopRequireWildcard(_utils); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function shimMediaStream(window) { window.MediaStream = window.MediaStream || window.webkitMediaStream; } function shimOnTrack(window) { if ((typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object' && window.RTCPeerConnection && !('ontrack' in window.RTCPeerConnection.prototype)) { Object.defineProperty(window.RTCPeerConnection.prototype, 'ontrack', { get: function get() { return this._ontrack; }, set: function set(f) { if (this._ontrack) { this.removeEventListener('track', this._ontrack); } this.addEventListener('track', this._ontrack = f); }, enumerable: true, configurable: true }); var origSetRemoteDescription = window.RTCPeerConnection.prototype.setRemoteDescription; window.RTCPeerConnection.prototype.setRemoteDescription = function setRemoteDescription() { var _this = this; if (!this._ontrackpoly) { this._ontrackpoly = function (e) { // onaddstream does not fire when a track is added to an existing // stream. But stream.onaddtrack is implemented so we use that. e.stream.addEventListener('addtrack', function (te) { var receiver = void 0; if (window.RTCPeerConnection.prototype.getReceivers) { receiver = _this.getReceivers().find(function (r) { return r.track && r.track.id === te.track.id; }); } else { receiver = { track: te.track }; } var event = new Event('track'); event.track = te.track; event.receiver = receiver; event.transceiver = { receiver: receiver }; event.streams = [e.stream]; _this.dispatchEvent(event); }); e.stream.getTracks().forEach(function (track) { var receiver = void 0; if (window.RTCPeerConnection.prototype.getReceivers) { receiver = _this.getReceivers().find(function (r) { return r.track && r.track.id === track.id; }); } else { receiver = { track: track }; } var event = new Event('track'); event.track = track; event.receiver = receiver; event.transceiver = { receiver: receiver }; event.streams = [e.stream]; _this.dispatchEvent(event); }); }; this.addEventListener('addstream', this._ontrackpoly); } return origSetRemoteDescription.apply(this, arguments); }; } else { // even if RTCRtpTransceiver is in window, it is only used and // emitted in unified-plan. Unfortunately this means we need // to unconditionally wrap the event. utils.wrapPeerConnectionEvent(window, 'track', function (e) { if (!e.transceiver) { Object.defineProperty(e, 'transceiver', { value: { receiver: e.receiver } }); } return e; }); } } function shimGetSendersWithDtmf(window) { // Overrides addTrack/removeTrack, depends on shimAddTrackRemoveTrack. if ((typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object' && window.RTCPeerConnection && !('getSenders' in window.RTCPeerConnection.prototype) && 'createDTMFSender' in window.RTCPeerConnection.prototype) { var shimSenderWithDtmf = function shimSenderWithDtmf(pc, track) { return { track: track, get dtmf() { if (this._dtmf === undefined) { if (track.kind === 'audio') { this._dtmf = pc.createDTMFSender(track); } else { this._dtmf = null; } } return this._dtmf; }, _pc: pc }; }; // augment addTrack when getSenders is not available. if (!window.RTCPeerConnection.prototype.getSenders) { window.RTCPeerConnection.prototype.getSenders = function getSenders() { this._senders = this._senders || []; return this._senders.slice(); // return a copy of the internal state. }; var origAddTrack = window.RTCPeerConnection.prototype.addTrack; window.RTCPeerConnection.prototype.addTrack = function addTrack(track, stream) { var sender = origAddTrack.apply(this, arguments); if (!sender) { sender = shimSenderWithDtmf(this, track); this._senders.push(sender); } return sender; }; var origRemoveTrack = window.RTCPeerConnection.prototype.removeTrack; window.RTCPeerConnection.prototype.removeTrack = function removeTrack(sender) { origRemoveTrack.apply(this, arguments); var idx = this._senders.indexOf(sender); if (idx !== -1) { this._senders.splice(idx, 1); } }; } var origAddStream = window.RTCPeerConnection.prototype.addStream; window.RTCPeerConnection.prototype.addStream = function addStream(stream) { var _this2 = this; this._senders = this._senders || []; origAddStream.apply(this, [stream]); stream.getTracks().forEach(function (track) { _this2._senders.push(shimSenderWithDtmf(_this2, track)); }); }; var origRemoveStream = window.RTCPeerConnection.prototype.removeStream; window.RTCPeerConnection.prototype.removeStream = function removeStream(stream) { var _this3 = this; this._senders = this._senders || []; origRemoveStream.apply(this, [stream]); stream.getTracks().forEach(function (track) { var sender = _this3._senders.find(function (s) { return s.track === track; }); if (sender) { // remove sender _this3._senders.splice(_this3._senders.indexOf(sender), 1); } }); }; } else if ((typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object' && window.RTCPeerConnection && 'getSenders' in window.RTCPeerConnection.prototype && 'createDTMFSender' in window.RTCPeerConnection.prototype && window.RTCRtpSender && !('dtmf' in window.RTCRtpSender.prototype)) { var origGetSenders = window.RTCPeerConnection.prototype.getSenders; window.RTCPeerConnection.prototype.getSenders = function getSenders() { var _this4 = this; var senders = origGetSenders.apply(this, []); senders.forEach(function (sender) { return sender._pc = _this4; }); return senders; }; Object.defineProperty(window.RTCRtpSender.prototype, 'dtmf', { get: function get() { if (this._dtmf === undefined) { if (this.track.kind === 'audio') { this._dtmf = this._pc.createDTMFSender(this.track); } else { this._dtmf = null; } } return this._dtmf; } }); } } function shimGetStats(window) { if (!window.RTCPeerConnection) { return; } var origGetStats = window.RTCPeerConnection.prototype.getStats; window.RTCPeerConnection.prototype.getStats = function getStats() { var _this5 = this; var _arguments = Array.prototype.slice.call(arguments), selector = _arguments[0], onSucc = _arguments[1], onErr = _arguments[2]; // If selector is a function then we are in the old style stats so just // pass back the original getStats format to avoid breaking old users. if (arguments.length > 0 && typeof selector === 'function') { return origGetStats.apply(this, arguments); } // When spec-style getStats is supported, return those when called with // either no arguments or the selector argument is null. if (origGetStats.length === 0 && (arguments.length === 0 || typeof selector !== 'function')) { return origGetStats.apply(this, []); } var fixChromeStats_ = function fixChromeStats_(response) { var standardReport = {}; var reports = response.result(); reports.forEach(function (report) { var standardStats = { id: report.id, timestamp: report.timestamp, type: { localcandidate: 'local-candidate', remotecandidate: 'remote-candidate' }[report.type] || report.type }; report.names().forEach(function (name) { standardStats[name] = report.stat(name); }); standardReport[standardStats.id] = standardStats; }); return standardReport; }; // shim getStats with maplike support var makeMapStats = function makeMapStats(stats) { return new Map(Object.keys(stats).map(function (key) { return [key, stats[key]]; })); }; if (arguments.length >= 2) { var successCallbackWrapper_ = function successCallbackWrapper_(response) { onSucc(makeMapStats(fixChromeStats_(response))); }; return origGetStats.apply(this, [successCallbackWrapper_, selector]); } // promise-support return new Promise(function (resolve, reject) { origGetStats.apply(_this5, [function (response) { resolve(makeMapStats(fixChromeStats_(response))); }, reject]); }).then(onSucc, onErr); }; } function shimSenderReceiverGetStats(window) { if (!((typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object' && window.RTCPeerConnection && window.RTCRtpSender && window.RTCRtpReceiver)) { return; } // shim sender stats. if (!('getStats' in window.RTCRtpSender.prototype)) { var origGetSenders = window.RTCPeerConnection.prototype.getSenders; if (origGetSenders) { window.RTCPeerConnection.prototype.getSenders = function getSenders() { var _this6 = this; var senders = origGetSenders.apply(this, []); senders.forEach(function (sender) { return sender._pc = _this6; }); return senders; }; } var origAddTrack = window.RTCPeerConnection.prototype.addTrack; if (origAddTrack) { window.RTCPeerConnection.prototype.addTrack = function addTrack() { var sender = origAddTrack.apply(this, arguments); sender._pc = this; return sender; }; } window.RTCRtpSender.prototype.getStats = function getStats() { var sender = this; return this._pc.getStats().then(function (result) { return ( /* Note: this will include stats of all senders that * send a track with the same id as sender.track as * it is not possible to identify the RTCRtpSender. */ utils.filterStats(result, sender.track, true) ); }); }; } // shim receiver stats. if (!('getStats' in window.RTCRtpReceiver.prototype)) { var origGetReceivers = window.RTCPeerConnection.prototype.getReceivers; if (origGetReceivers) { window.RTCPeerConnection.prototype.getReceivers = function getReceivers() { var _this7 = this; var receivers = origGetReceivers.apply(this, []); receivers.forEach(function (receiver) { return receiver._pc = _this7; }); return receivers; }; } utils.wrapPeerConnectionEvent(window, 'track', function (e) { e.receiver._pc = e.srcElement; return e; }); window.RTCRtpReceiver.prototype.getStats = function getStats() { var receiver = this; return this._pc.getStats().then(function (result) { return utils.filterStats(result, receiver.track, false); }); }; } if (!('getStats' in window.RTCRtpSender.prototype && 'getStats' in window.RTCRtpReceiver.prototype)) { return; } // shim RTCPeerConnection.getStats(track). var origGetStats = window.RTCPeerConnection.prototype.getStats; window.RTCPeerConnection.prototype.getStats = function getStats() { if (arguments.length > 0 && arguments[0] instanceof window.MediaStreamTrack) { var track = arguments[0]; var sender = void 0; var receiver = void 0; var err = void 0; this.getSenders().forEach(function (s) { if (s.track === track) { if (sender) { err = true; } else { sender = s; } } }); this.getReceivers().forEach(function (r) { if (r.track === track) { if (receiver) { err = true; } else { receiver = r; } } return r.track === track; }); if (err || sender && receiver) { return Promise.reject(new DOMException('There are more than one sender or receiver for the track.', 'InvalidAccessError')); } else if (sender) { return sender.getStats(); } else if (receiver) { return receiver.getStats(); } return Promise.reject(new DOMException('There is no sender or receiver for the track.', 'InvalidAccessError')); } return origGetStats.apply(this, arguments); }; } function shimAddTrackRemoveTrackWithNative(window) { // shim addTrack/removeTrack with native variants in order to make // the interactions with legacy getLocalStreams behave as in other browsers. // Keeps a mapping stream.id => [stream, rtpsenders...] window.RTCPeerConnection.prototype.getLocalStreams = function getLocalStreams() { var _this8 = this; this._shimmedLocalStreams = this._shimmedLocalStreams || {}; return Object.keys(this._shimmedLocalStreams).map(function (streamId) { return _this8._shimmedLocalStreams[streamId][0]; }); }; var origAddTrack = window.RTCPeerConnection.prototype.addTrack; window.RTCPeerConnection.prototype.addTrack = function addTrack(track, stream) { if (!stream) { return origAddTrack.apply(this, arguments); } this._shimmedLocalStreams = this._shimmedLocalStreams || {}; var sender = origAddTrack.apply(this, arguments); if (!this._shimmedLocalStreams[stream.id]) { this._shimmedLocalStreams[stream.id] = [stream, sender]; } else if (this._shimmedLocalStreams[stream.id].indexOf(sender) === -1) { this._shimmedLocalStreams[stream.id].push(sender); } return sender; }; var origAddStream = window.RTCPeerConnection.prototype.addStream; window.RTCPeerConnection.prototype.addStream = function addStream(stream) { var _this9 = this; this._shimmedLocalStreams = this._shimmedLocalStreams || {}; stream.getTracks().forEach(function (track) { var alreadyExists = _this9.getSenders().find(function (s) { return s.track === track; }); if (alreadyExists) { throw new DOMException('Track already exists.', 'InvalidAccessError'); } }); var existingSenders = this.getSenders(); origAddStream.apply(this, arguments); var newSenders = this.getSenders().filter(function (newSender) { return existingSenders.indexOf(newSender) === -1; }); this._shimmedLocalStreams[stream.id] = [stream].concat(newSenders); }; var origRemoveStream = window.RTCPeerConnection.prototype.removeStream; window.RTCPeerConnection.prototype.removeStream = function removeStream(stream) { this._shimmedLocalStreams = this._shimmedLocalStreams || {}; delete this._shimmedLocalStreams[stream.id]; return origRemoveStream.apply(this, arguments); }; var origRemoveTrack = window.RTCPeerConnection.prototype.removeTrack; window.RTCPeerConnection.prototype.removeTrack = function removeTrack(sender) { var _this10 = this; this._shimmedLocalStreams = this._shimmedLocalStreams || {}; if (sender) { Object.keys(this._shimmedLocalStreams).forEach(function (streamId) { var idx = _this10._shimmedLocalStreams[streamId].indexOf(sender); if (idx !== -1) { _this10._shimmedLocalStreams[streamId].splice(idx, 1); } if (_this10._shimmedLocalStreams[streamId].length === 1) { delete _this10._shimmedLocalStreams[streamId]; } }); } return origRemoveTrack.apply(this, arguments); }; } function shimAddTrackRemoveTrack(window, browserDetails) { if (!window.RTCPeerConnection) { return; } // shim addTrack and removeTrack. if (window.RTCPeerConnection.prototype.addTrack && browserDetails.version >= 65) { return shimAddTrackRemoveTrackWithNative(window); } // also shim pc.getLocalStreams when addTrack is shimmed // to return the original streams. var origGetLocalStreams = window.RTCPeerConnection.prototype.getLocalStreams; window.RTCPeerConnection.prototype.getLocalStreams = function getLocalStreams() { var _this11 = this; var nativeStreams = origGetLocalStreams.apply(this); this._reverseStreams = this._reverseStreams || {}; return nativeStreams.map(function (stream) { return _this11._reverseStreams[stream.id]; }); }; var origAddStream = window.RTCPeerConnection.prototype.addStream; window.RTCPeerConnection.prototype.addStream = function addStream(stream) { var _this12 = this; this._streams = this._streams || {}; this._reverseStreams = this._reverseStreams || {}; stream.getTracks().forEach(function (track) { var alreadyExists = _this12.getSenders().find(function (s) { return s.track === track; }); if (alreadyExists) { throw new DOMException('Track already exists.', 'InvalidAccessError'); } }); // Add identity mapping for consistency with addTrack. // Unless this is being used with a stream from addTrack. if (!this._reverseStreams[stream.id]) { var newStream = new window.MediaStream(stream.getTracks()); this._streams[stream.id] = newStream; this._reverseStreams[newStream.id] = stream; stream = newStream; } origAddStream.apply(this, [stream]); }; var origRemoveStream = window.RTCPeerConnection.prototype.removeStream; window.RTCPeerConnection.prototype.removeStream = function removeStream(stream) { this._streams = this._streams || {}; this._reverseStreams = this._reverseStreams || {}; origRemoveStream.apply(this, [this._streams[stream.id] || stream]); delete this._reverseStreams[this._streams[stream.id] ? this._streams[stream.id].id : stream.id]; delete this._streams[stream.id]; }; window.RTCPeerConnection.prototype.addTrack = function addTrack(track, stream) { var _this13 = this; if (this.signalingState === 'closed') { throw new DOMException('The RTCPeerConnection\'s signalingState is \'closed\'.', 'InvalidStateError'); } var streams = [].slice.call(arguments, 1); if (streams.length !== 1 || !streams[0].getTracks().find(function (t) { return t === track; })) { // this is not fully correct but all we can manage without // [[associated MediaStreams]] internal slot. throw new DOMException('The adapter.js addTrack polyfill only supports a single ' + ' stream which is associated with the specified track.', 'NotSupportedError'); } var alreadyExists = this.getSenders().find(function (s) { return s.track === track; }); if (alreadyExists) { throw new DOMException('Track already exists.', 'InvalidAccessError'); } this._streams = this._streams || {}; this._reverseStreams = this._reverseStreams || {}; var oldStream = this._streams[stream.id]; if (oldStream) { // this is using odd Chrome behaviour, use with caution: // https://bugs.chromium.org/p/webrtc/issues/detail?id=7815 // Note: we rely on the high-level addTrack/dtmf shim to // create the sender with a dtmf sender. oldStream.addTrack(track); // Trigger ONN async. Promise.resolve().then(function () { _this13.dispatchEvent(new Event('negotiationneeded')); }); } else { var newStream = new window.MediaStream([track]); this._streams[stream.id] = newStream; this._reverseStreams[newStream.id] = stream; this.addStream(newStream); } return this.getSenders().find(function (s) { return s.track === track; }); }; // replace the internal stream id with the external one and // vice versa. function replaceInternalStreamId(pc, description) { var sdp = description.sdp; Object.keys(pc._reverseStreams || []).forEach(function (internalId) { var externalStream = pc._reverseStreams[internalId]; var internalStream = pc._streams[externalStream.id]; sdp = sdp.replace(new RegExp(internalStream.id, 'g'), externalStream.id); }); return new RTCSessionDescription({ type: description.type, sdp: sdp }); } function replaceExternalStreamId(pc, description) { var sdp = description.sdp; Object.keys(pc._reverseStreams || []).forEach(function (internalId) { var externalStream = pc._reverseStreams[internalId]; var internalStream = pc._streams[externalStream.id]; sdp = sdp.replace(new RegExp(externalStream.id, 'g'), internalStream.id); }); return new RTCSessionDescription({ type: description.type, sdp: sdp }); } ['createOffer', 'createAnswer'].forEach(function (method) { var nativeMethod = window.RTCPeerConnection.prototype[method]; var methodObj = _defineProperty({}, method, function () { var _this14 = this; var args = arguments; var isLegacyCall = arguments.length && typeof arguments[0] === 'function'; if (isLegacyCall) { return nativeMethod.apply(this, [function (description) { var desc = replaceInternalStreamId(_this14, description); args[0].apply(null, [desc]); }, function (err) { if (args[1]) { args[1].apply(null, err); } }, arguments[2]]); } return nativeMethod.apply(this, arguments).then(function (description) { return replaceInternalStreamId(_this14, description); }); }); window.RTCPeerConnection.prototype[method] = methodObj[method]; }); var origSetLocalDescription = window.RTCPeerConnection.prototype.setLocalDescription; window.RTCPeerConnection.prototype.setLocalDescription = function setLocalDescription() { if (!arguments.length || !arguments[0].type) { return origSetLocalDescription.apply(this, arguments); } arguments[0] = replaceExternalStreamId(this, arguments[0]); return origSetLocalDescription.apply(this, arguments); }; // TODO: mangle getStats: https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamstats-streamidentifier var origLocalDescription = Object.getOwnPropertyDescriptor(window.RTCPeerConnection.prototype, 'localDescription'); Object.defineProperty(window.RTCPeerConnection.prototype, 'localDescription', { get: function get() { var description = origLocalDescription.get.apply(this); if (description.type === '') { return description; } return replaceInternalStreamId(this, description); } }); window.RTCPeerConnection.prototype.removeTrack = function removeTrack(sender) { var _this15 = this; if (this.signalingState === 'closed') { throw new DOMException('The RTCPeerConnection\'s signalingState is \'closed\'.', 'InvalidStateError'); } // We can not yet check for sender instanceof RTCRtpSender // since we shim RTPSender. So we check if sender._pc is set. if (!sender._pc) { throw new DOMException('Argument 1 of RTCPeerConnection.removeTrack ' + 'does not implement interface RTCRtpSender.', 'TypeError'); } var isLocal = sender._pc === this; if (!isLocal) { throw new DOMException('Sender was not created by this connection.', 'InvalidAccessError'); } // Search for the native stream the senders track belongs to. this._streams = this._streams || {}; var stream = void 0; Object.keys(this._streams).forEach(function (streamid) { var hasTrack = _this15._streams[streamid].getTracks().find(function (track) { return sender.track === track; }); if (hasTrack) { stream = _this15._streams[streamid]; } }); if (stream) { if (stream.getTracks().length === 1) { // if this is the last track of the stream, remove the stream. This // takes care of any shimmed _senders. this.removeStream(this._reverseStreams[stream.id]); } else { // relying on the same odd chrome behaviour as above. stream.removeTrack(sender.track); } this.dispatchEvent(new Event('negotiationneeded')); } }; } function shimPeerConnection(window, browserDetails) { if (!window.RTCPeerConnection && window.webkitRTCPeerConnection) { // very basic support for old versions. window.RTCPeerConnection = window.webkitRTCPeerConnection; } if (!window.RTCPeerConnection) { return; } // shim implicit creation of RTCSessionDescription/RTCIceCandidate if (browserDetails.version < 53) { ['setLocalDescription', 'setRemoteDescription', 'addIceCandidate'].forEach(function (method) { var nativeMethod = window.RTCPeerConnection.prototype[method]; var methodObj = _defineProperty({}, method, function () { arguments[0] = new (method === 'addIceCandidate' ? window.RTCIceCandidate : window.RTCSessionDescription)(arguments[0]); return nativeMethod.apply(this, arguments); }); window.RTCPeerConnection.prototype[method] = methodObj[method]; }); } } // Attempt to fix ONN in plan-b mode. function fixNegotiationNeeded(window, browserDetails) { utils.wrapPeerConnectionEvent(window, 'negotiationneeded', function (e) { var pc = e.target; if (browserDetails.version < 72 || pc.getConfiguration && pc.getConfiguration().sdpSemantics === 'plan-b') { if (pc.signalingState !== 'stable') { return; } } return e; }); } },{"../utils.js":232,"./getdisplaymedia":221,"./getusermedia":222}],221:[function(require,module,exports){ /* * Copyright (c) 2018 The adapter.js project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. */ /* eslint-env node */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.shimGetDisplayMedia = shimGetDisplayMedia; function shimGetDisplayMedia(window, getSourceId) { if (window.navigator.mediaDevices && 'getDisplayMedia' in window.navigator.mediaDevices) { return; } if (!window.navigator.mediaDevices) { return; } // getSourceId is a function that returns a promise resolving with // the sourceId of the screen/window/tab to be shared. if (typeof getSourceId !== 'function') { console.error('shimGetDisplayMedia: getSourceId argument is not ' + 'a function'); return; } window.navigator.mediaDevices.getDisplayMedia = function getDisplayMedia(constraints) { return getSourceId(constraints).then(function (sourceId) { var widthSpecified = constraints.video && constraints.video.width; var heightSpecified = constraints.video && constraints.video.height; var frameRateSpecified = constraints.video && constraints.video.frameRate; constraints.video = { mandatory: { chromeMediaSource: 'desktop', chromeMediaSourceId: sourceId, maxFrameRate: frameRateSpecified || 3 } }; if (widthSpecified) { constraints.video.mandatory.maxWidth = widthSpecified; } if (heightSpecified) { constraints.video.mandatory.maxHeight = heightSpecified; } return window.navigator.mediaDevices.getUserMedia(constraints); }); }; } },{}],222:[function(require,module,exports){ /* * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. */ /* eslint-env node */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; exports.shimGetUserMedia = shimGetUserMedia; var _utils = require('../utils.js'); var utils = _interopRequireWildcard(_utils); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } var logging = utils.log; function shimGetUserMedia(window, browserDetails) { var navigator = window && window.navigator; if (!navigator.mediaDevices) { return; } var constraintsToChrome_ = function constraintsToChrome_(c) { if ((typeof c === 'undefined' ? 'undefined' : _typeof(c)) !== 'object' || c.mandatory || c.optional) { return c; } var cc = {}; Object.keys(c).forEach(function (key) { if (key === 'require' || key === 'advanced' || key === 'mediaSource') { return; } var r = _typeof(c[key]) === 'object' ? c[key] : { ideal: c[key] }; if (r.exact !== undefined && typeof r.exact === 'number') { r.min = r.max = r.exact; } var oldname_ = function oldname_(prefix, name) { if (prefix) { return prefix + name.charAt(0).toUpperCase() + name.slice(1); } return name === 'deviceId' ? 'sourceId' : name; }; if (r.ideal !== undefined) { cc.optional = cc.optional || []; var oc = {}; if (typeof r.ideal === 'number') { oc[oldname_('min', key)] = r.ideal; cc.optional.push(oc); oc = {}; oc[oldname_('max', key)] = r.ideal; cc.optional.push(oc); } else { oc[oldname_('', key)] = r.ideal; cc.optional.push(oc); } } if (r.exact !== undefined && typeof r.exact !== 'number') { cc.mandatory = cc.mandatory || {}; cc.mandatory[oldname_('', key)] = r.exact; } else { ['min', 'max'].forEach(function (mix) { if (r[mix] !== undefined) { cc.mandatory = cc.mandatory || {}; cc.mandatory[oldname_(mix, key)] = r[mix]; } }); } }); if (c.advanced) { cc.optional = (cc.optional || []).concat(c.advanced); } return cc; }; var shimConstraints_ = function shimConstraints_(constraints, func) { if (browserDetails.version >= 61) { return func(constraints); } constraints = JSON.parse(JSON.stringify(constraints)); if (constraints && _typeof(constraints.audio) === 'object') { var remap = function remap(obj, a, b) { if (a in obj && !(b in obj)) { obj[b] = obj[a]; delete obj[a]; } }; constraints = JSON.parse(JSON.stringify(constraints)); remap(constraints.audio, 'autoGainControl', 'googAutoGainControl'); remap(constraints.audio, 'noiseSuppression', 'googNoiseSuppression'); constraints.audio = constraintsToChrome_(constraints.audio); } if (constraints && _typeof(constraints.video) === 'object') { // Shim facingMode for mobile & surface pro. var face = constraints.video.facingMode; face = face && ((typeof face === 'undefined' ? 'undefined' : _typeof(face)) === 'object' ? face : { ideal: face }); var getSupportedFacingModeLies = browserDetails.version < 66; if (face && (face.exact === 'user' || face.exact === 'environment' || face.ideal === 'user' || face.ideal === 'environment') && !(navigator.mediaDevices.getSupportedConstraints && navigator.mediaDevices.getSupportedConstraints().facingMode && !getSupportedFacingModeLies)) { delete constraints.video.facingMode; var matches = void 0; if (face.exact === 'environment' || face.ideal === 'environment') { matches = ['back', 'rear']; } else if (face.exact === 'user' || face.ideal === 'user') { matches = ['front']; } if (matches) { // Look for matches in label, or use last cam for back (typical). return navigator.mediaDevices.enumerateDevices().then(function (devices) { devices = devices.filter(function (d) { return d.kind === 'videoinput'; }); var dev = devices.find(function (d) { return matches.some(function (match) { return d.label.toLowerCase().includes(match); }); }); if (!dev && devices.length && matches.includes('back')) { dev = devices[devices.length - 1]; // more likely the back cam } if (dev) { constraints.video.deviceId = face.exact ? { exact: dev.deviceId } : { ideal: dev.deviceId }; } constraints.video = constraintsToChrome_(constraints.video); logging('chrome: ' + JSON.stringify(constraints)); return func(constraints); }); } } constraints.video = constraintsToChrome_(constraints.video); } logging('chrome: ' + JSON.stringify(constraints)); return func(constraints); }; var shimError_ = function shimError_(e) { if (browserDetails.version >= 64) { return e; } return { name: { PermissionDeniedError: 'NotAllowedError', PermissionDismissedError: 'NotAllowedError', InvalidStateError: 'NotAllowedError', DevicesNotFoundError: 'NotFoundError', ConstraintNotSatisfiedError: 'OverconstrainedError', TrackStartError: 'NotReadableError', MediaDeviceFailedDueToShutdown: 'NotAllowedError', MediaDeviceKillSwitchOn: 'NotAllowedError', TabCaptureError: 'AbortError', ScreenCaptureError: 'AbortError', DeviceCaptureError: 'AbortError' }[e.name] || e.name, message: e.message, constraint: e.constraint || e.constraintName, toString: function toString() { return this.name + (this.message && ': ') + this.message; } }; }; var getUserMedia_ = function getUserMedia_(constraints, onSuccess, onError) { shimConstraints_(constraints, function (c) { navigator.webkitGetUserMedia(c, onSuccess, function (e) { if (onError) { onError(shimError_(e)); } }); }); }; navigator.getUserMedia = getUserMedia_.bind(navigator); // Even though Chrome 45 has navigator.mediaDevices and a getUserMedia // function which returns a Promise, it does not accept spec-style // constraints. if (navigator.mediaDevices.getUserMedia) { var origGetUserMedia = navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices); navigator.mediaDevices.getUserMedia = function (cs) { return shimConstraints_(cs, function (c) { return origGetUserMedia(c).then(function (stream) { if (c.audio && !stream.getAudioTracks().length || c.video && !stream.getVideoTracks().length) { stream.getTracks().forEach(function (track) { track.stop(); }); throw new DOMException('', 'NotFoundError'); } return stream; }, function (e) { return Promise.reject(shimError_(e)); }); }); }; } } },{"../utils.js":232}],223:[function(require,module,exports){ /* * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. */ /* eslint-env node */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; exports.shimRTCIceCandidate = shimRTCIceCandidate; exports.shimMaxMessageSize = shimMaxMessageSize; exports.shimSendThrowTypeError = shimSendThrowTypeError; exports.shimConnectionState = shimConnectionState; exports.removeExtmapAllowMixed = removeExtmapAllowMixed; exports.shimAddIceCandidateNullOrEmpty = shimAddIceCandidateNullOrEmpty; var _sdp = require('sdp'); var _sdp2 = _interopRequireDefault(_sdp); var _utils = require('./utils'); var utils = _interopRequireWildcard(_utils); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function shimRTCIceCandidate(window) { // foundation is arbitrarily chosen as an indicator for full support for // https://w3c.github.io/webrtc-pc/#rtcicecandidate-interface if (!window.RTCIceCandidate || window.RTCIceCandidate && 'foundation' in window.RTCIceCandidate.prototype) { return; } var NativeRTCIceCandidate = window.RTCIceCandidate; window.RTCIceCandidate = function RTCIceCandidate(args) { // Remove the a= which shouldn't be part of the candidate string. if ((typeof args === 'undefined' ? 'undefined' : _typeof(args)) === 'object' && args.candidate && args.candidate.indexOf('a=') === 0) { args = JSON.parse(JSON.stringify(args)); args.candidate = args.candidate.substr(2); } if (args.candidate && args.candidate.length) { // Augment the native candidate with the parsed fields. var nativeCandidate = new NativeRTCIceCandidate(args); var parsedCandidate = _sdp2.default.parseCandidate(args.candidate); var augmentedCandidate = Object.assign(nativeCandidate, parsedCandidate); // Add a serializer that does not serialize the extra attributes. augmentedCandidate.toJSON = function toJSON() { return { candidate: augmentedCandidate.candidate, sdpMid: augmentedCandidate.sdpMid, sdpMLineIndex: augmentedCandidate.sdpMLineIndex, usernameFragment: augmentedCandidate.usernameFragment }; }; return augmentedCandidate; } return new NativeRTCIceCandidate(args); }; window.RTCIceCandidate.prototype = NativeRTCIceCandidate.prototype; // Hook up the augmented candidate in onicecandidate and // addEventListener('icecandidate', ...) utils.wrapPeerConnectionEvent(window, 'icecandidate', function (e) { if (e.candidate) { Object.defineProperty(e, 'candidate', { value: new window.RTCIceCandidate(e.candidate), writable: 'false' }); } return e; }); } function shimMaxMessageSize(window, browserDetails) { if (!window.RTCPeerConnection) { return; } if (!('sctp' in window.RTCPeerConnection.prototype)) { Object.defineProperty(window.RTCPeerConnection.prototype, 'sctp', { get: function get() { return typeof this._sctp === 'undefined' ? null : this._sctp; } }); } var sctpInDescription = function sctpInDescription(description) { if (!description || !description.sdp) { return false; } var sections = _sdp2.default.splitSections(description.sdp); sections.shift(); return sections.some(function (mediaSection) { var mLine = _sdp2.default.parseMLine(mediaSection); return mLine && mLine.kind === 'application' && mLine.protocol.indexOf('SCTP') !== -1; }); }; var getRemoteFirefoxVersion = function getRemoteFirefoxVersion(description) { // TODO: Is there a better solution for detecting Firefox? var match = description.sdp.match(/mozilla...THIS_IS_SDPARTA-(\d+)/); if (match === null || match.length < 2) { return -1; } var version = parseInt(match[1], 10); // Test for NaN (yes, this is ugly) return version !== version ? -1 : version; }; var getCanSendMaxMessageSize = function getCanSendMaxMessageSize(remoteIsFirefox) { // Every implementation we know can send at least 64 KiB. // Note: Although Chrome is technically able to send up to 256 KiB, the // data does not reach the other peer reliably. // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=8419 var canSendMaxMessageSize = 65536; if (browserDetails.browser === 'firefox') { if (browserDetails.version < 57) { if (remoteIsFirefox === -1) { // FF < 57 will send in 16 KiB chunks using the deprecated PPID // fragmentation. canSendMaxMessageSize = 16384; } else { // However, other FF (and RAWRTC) can reassemble PPID-fragmented // messages. Thus, supporting ~2 GiB when sending. canSendMaxMessageSize = 2147483637; } } else if (browserDetails.version < 60) { // Currently, all FF >= 57 will reset the remote maximum message size // to the default value when a data channel is created at a later // stage. :( // See: https://bugzilla.mozilla.org/show_bug.cgi?id=1426831 canSendMaxMessageSize = browserDetails.version === 57 ? 65535 : 65536; } else { // FF >= 60 supports sending ~2 GiB canSendMaxMessageSize = 2147483637; } } return canSendMaxMessageSize; }; var getMaxMessageSize = function getMaxMessageSize(description, remoteIsFirefox) { // Note: 65536 bytes is the default value from the SDP spec. Also, // every implementation we know supports receiving 65536 bytes. var maxMessageSize = 65536; // FF 57 has a slightly incorrect default remote max message size, so // we need to adjust it here to avoid a failure when sending. // See: https://bugzilla.mozilla.org/show_bug.cgi?id=1425697 if (browserDetails.browser === 'firefox' && browserDetails.version === 57) { maxMessageSize = 65535; } var match = _sdp2.default.matchPrefix(description.sdp, 'a=max-message-size:'); if (match.length > 0) { maxMessageSize = parseInt(match[0].substr(19), 10); } else if (browserDetails.browser === 'firefox' && remoteIsFirefox !== -1) { // If the maximum message size is not present in the remote SDP and // both local and remote are Firefox, the remote peer can receive // ~2 GiB. maxMessageSize = 2147483637; } return maxMessageSize; }; var origSetRemoteDescription = window.RTCPeerConnection.prototype.setRemoteDescription; window.RTCPeerConnection.prototype.setRemoteDescription = function setRemoteDescription() { this._sctp = null; // Chrome decided to not expose .sctp in plan-b mode. // As usual, adapter.js has to do an 'ugly worakaround' // to cover up the mess. if (browserDetails.browser === 'chrome' && browserDetails.version >= 76) { var _getConfiguration = this.getConfiguration(), sdpSemantics = _getConfiguration.sdpSemantics; if (sdpSemantics === 'plan-b') { Object.defineProperty(this, 'sctp', { get: function get() { return typeof this._sctp === 'undefined' ? null : this._sctp; }, enumerable: true, configurable: true }); } } if (sctpInDescription(arguments[0])) { // Check if the remote is FF. var isFirefox = getRemoteFirefoxVersion(arguments[0]); // Get the maximum message size the local peer is capable of sending var canSendMMS = getCanSendMaxMessageSize(isFirefox); // Get the maximum message size of the remote peer. var remoteMMS = getMaxMessageSize(arguments[0], isFirefox); // Determine final maximum message size var maxMessageSize = void 0; if (canSendMMS === 0 && remoteMMS === 0) { maxMessageSize = Number.POSITIVE_INFINITY; } else if (canSendMMS === 0 || remoteMMS === 0) { maxMessageSize = Math.max(canSendMMS, remoteMMS); } else { maxMessageSize = Math.min(canSendMMS, remoteMMS); } // Create a dummy RTCSctpTransport object and the 'maxMessageSize' // attribute. var sctp = {}; Object.defineProperty(sctp, 'maxMessageSize', { get: function get() { return maxMessageSize; } }); this._sctp = sctp; } return origSetRemoteDescription.apply(this, arguments); }; } function shimSendThrowTypeError(window) { if (!(window.RTCPeerConnection && 'createDataChannel' in window.RTCPeerConnection.prototype)) { return; } // Note: Although Firefox >= 57 has a native implementation, the maximum // message size can be reset for all data channels at a later stage. // See: https://bugzilla.mozilla.org/show_bug.cgi?id=1426831 function wrapDcSend(dc, pc) { var origDataChannelSend = dc.send; dc.send = function send() { var data = arguments[0]; var length = data.length || data.size || data.byteLength; if (dc.readyState === 'open' && pc.sctp && length > pc.sctp.maxMessageSize) { throw new TypeError('Message too large (can send a maximum of ' + pc.sctp.maxMessageSize + ' bytes)'); } return origDataChannelSend.apply(dc, arguments); }; } var origCreateDataChannel = window.RTCPeerConnection.prototype.createDataChannel; window.RTCPeerConnection.prototype.createDataChannel = function createDataChannel() { var dataChannel = origCreateDataChannel.apply(this, arguments); wrapDcSend(dataChannel, this); return dataChannel; }; utils.wrapPeerConnectionEvent(window, 'datachannel', function (e) { wrapDcSend(e.channel, e.target); return e; }); } /* shims RTCConnectionState by pretending it is the same as iceConnectionState. * See https://bugs.chromium.org/p/webrtc/issues/detail?id=6145#c12 * for why this is a valid hack in Chrome. In Firefox it is slightly incorrect * since DTLS failures would be hidden. See * https://bugzilla.mozilla.org/show_bug.cgi?id=1265827 * for the Firefox tracking bug. */ function shimConnectionState(window) { if (!window.RTCPeerConnection || 'connectionState' in window.RTCPeerConnection.prototype) { return; } var proto = window.RTCPeerConnection.prototype; Object.defineProperty(proto, 'connectionState', { get: function get() { return { completed: 'connected', checking: 'connecting' }[this.iceConnectionState] || this.iceConnectionState; }, enumerable: true, configurable: true }); Object.defineProperty(proto, 'onconnectionstatechange', { get: function get() { return this._onconnectionstatechange || null; }, set: function set(cb) { if (this._onconnectionstatechange) { this.removeEventListener('connectionstatechange', this._onconnectionstatechange); delete this._onconnectionstatechange; } if (cb) { this.addEventListener('connectionstatechange', this._onconnectionstatechange = cb); } }, enumerable: true, configurable: true }); ['setLocalDescription', 'setRemoteDescription'].forEach(function (method) { var origMethod = proto[method]; proto[method] = function () { if (!this._connectionstatechangepoly) { this._connectionstatechangepoly = function (e) { var pc = e.target; if (pc._lastConnectionState !== pc.connectionState) { pc._lastConnectionState = pc.connectionState; var newEvent = new Event('connectionstatechange', e); pc.dispatchEvent(newEvent); } return e; }; this.addEventListener('iceconnectionstatechange', this._connectionstatechangepoly); } return origMethod.apply(this, arguments); }; }); } function removeExtmapAllowMixed(window, browserDetails) { /* remove a=extmap-allow-mixed for webrtc.org < M71 */ if (!window.RTCPeerConnection) { return; } if (browserDetails.browser === 'chrome' && browserDetails.version >= 71) { return; } if (browserDetails.browser === 'safari' && browserDetails.version >= 605) { return; } var nativeSRD = window.RTCPeerConnection.prototype.setRemoteDescription; window.RTCPeerConnection.prototype.setRemoteDescription = function setRemoteDescription(desc) { if (desc && desc.sdp && desc.sdp.indexOf('\na=extmap-allow-mixed') !== -1) { var sdp = desc.sdp.split('\n').filter(function (line) { return line.trim() !== 'a=extmap-allow-mixed'; }).join('\n'); // Safari enforces read-only-ness of RTCSessionDescription fields. if (window.RTCSessionDescription && desc instanceof window.RTCSessionDescription) { arguments[0] = new window.RTCSessionDescription({ type: desc.type, sdp: sdp }); } else { desc.sdp = sdp; } } return nativeSRD.apply(this, arguments); }; } function shimAddIceCandidateNullOrEmpty(window, browserDetails) { // Support for addIceCandidate(null or undefined) // as well as addIceCandidate({candidate: "", ...}) // https://bugs.chromium.org/p/chromium/issues/detail?id=978582 // Note: must be called before other polyfills which change the signature. if (!(window.RTCPeerConnection && window.RTCPeerConnection.prototype)) { return; } var nativeAddIceCandidate = window.RTCPeerConnection.prototype.addIceCandidate; if (!nativeAddIceCandidate || nativeAddIceCandidate.length === 0) { return; } window.RTCPeerConnection.prototype.addIceCandidate = function addIceCandidate() { if (!arguments[0]) { if (arguments[1]) { arguments[1].apply(null); } return Promise.resolve(); } // Firefox 68+ emits and processes {candidate: "", ...}, ignore // in older versions. // Native support for ignoring exists for Chrome M77+. // Safari ignores as well, exact version unknown but works in the same // version that also ignores addIceCandidate(null). if ((browserDetails.browser === 'chrome' && browserDetails.version < 78 || browserDetails.browser === 'firefox' && browserDetails.version < 68 || browserDetails.browser === 'safari') && arguments[0] && arguments[0].candidate === '') { return Promise.resolve(); } return nativeAddIceCandidate.apply(this, arguments); }; } },{"./utils":232,"sdp":175}],224:[function(require,module,exports){ /* * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. */ /* eslint-env node */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.shimGetDisplayMedia = exports.shimGetUserMedia = undefined; var _getusermedia = require('./getusermedia'); Object.defineProperty(exports, 'shimGetUserMedia', { enumerable: true, get: function get() { return _getusermedia.shimGetUserMedia; } }); var _getdisplaymedia = require('./getdisplaymedia'); Object.defineProperty(exports, 'shimGetDisplayMedia', { enumerable: true, get: function get() { return _getdisplaymedia.shimGetDisplayMedia; } }); exports.shimPeerConnection = shimPeerConnection; exports.shimReplaceTrack = shimReplaceTrack; var _utils = require('../utils'); var utils = _interopRequireWildcard(_utils); var _filtericeservers = require('./filtericeservers'); var _rtcpeerconnectionShim = require('rtcpeerconnection-shim'); var _rtcpeerconnectionShim2 = _interopRequireDefault(_rtcpeerconnectionShim); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function shimPeerConnection(window, browserDetails) { if (window.RTCIceGatherer) { if (!window.RTCIceCandidate) { window.RTCIceCandidate = function RTCIceCandidate(args) { return args; }; } if (!window.RTCSessionDescription) { window.RTCSessionDescription = function RTCSessionDescription(args) { return args; }; } // this adds an additional event listener to MediaStrackTrack that signals // when a tracks enabled property was changed. Workaround for a bug in // addStream, see below. No longer required in 15025+ if (browserDetails.version < 15025) { var origMSTEnabled = Object.getOwnPropertyDescriptor(window.MediaStreamTrack.prototype, 'enabled'); Object.defineProperty(window.MediaStreamTrack.prototype, 'enabled', { set: function set(value) { origMSTEnabled.set.call(this, value); var ev = new Event('enabled'); ev.enabled = value; this.dispatchEvent(ev); } }); } } // ORTC defines the DTMF sender a bit different. // https://github.com/w3c/ortc/issues/714 if (window.RTCRtpSender && !('dtmf' in window.RTCRtpSender.prototype)) { Object.defineProperty(window.RTCRtpSender.prototype, 'dtmf', { get: function get() { if (this._dtmf === undefined) { if (this.track.kind === 'audio') { this._dtmf = new window.RTCDtmfSender(this); } else if (this.track.kind === 'video') { this._dtmf = null; } } return this._dtmf; } }); } // Edge currently only implements the RTCDtmfSender, not the // RTCDTMFSender alias. See http://draft.ortc.org/#rtcdtmfsender2* if (window.RTCDtmfSender && !window.RTCDTMFSender) { window.RTCDTMFSender = window.RTCDtmfSender; } var RTCPeerConnectionShim = (0, _rtcpeerconnectionShim2.default)(window, browserDetails.version); window.RTCPeerConnection = function RTCPeerConnection(config) { if (config && config.iceServers) { config.iceServers = (0, _filtericeservers.filterIceServers)(config.iceServers, browserDetails.version); utils.log('ICE servers after filtering:', config.iceServers); } return new RTCPeerConnectionShim(config); }; window.RTCPeerConnection.prototype = RTCPeerConnectionShim.prototype; } function shimReplaceTrack(window) { // ORTC has replaceTrack -- https://github.com/w3c/ortc/issues/614 if (window.RTCRtpSender && !('replaceTrack' in window.RTCRtpSender.prototype)) { window.RTCRtpSender.prototype.replaceTrack = window.RTCRtpSender.prototype.setTrack; } } },{"../utils":232,"./filtericeservers":225,"./getdisplaymedia":226,"./getusermedia":227,"rtcpeerconnection-shim":172}],225:[function(require,module,exports){ /* * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. */ /* eslint-env node */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.filterIceServers = filterIceServers; var _utils = require('../utils'); var utils = _interopRequireWildcard(_utils); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } // Edge does not like // 1) stun: filtered after 14393 unless ?transport=udp is present // 2) turn: that does not have all of turn:host:port?transport=udp // 3) turn: with ipv6 addresses // 4) turn: occurring muliple times function filterIceServers(iceServers, edgeVersion) { var hasTurn = false; iceServers = JSON.parse(JSON.stringify(iceServers)); return iceServers.filter(function (server) { if (server && (server.urls || server.url)) { var urls = server.urls || server.url; if (server.url && !server.urls) { utils.deprecated('RTCIceServer.url', 'RTCIceServer.urls'); } var isString = typeof urls === 'string'; if (isString) { urls = [urls]; } urls = urls.filter(function (url) { // filter STUN unconditionally. if (url.indexOf('stun:') === 0) { return false; } var validTurn = url.startsWith('turn') && !url.startsWith('turn:[') && url.includes('transport=udp'); if (validTurn && !hasTurn) { hasTurn = true; return true; } return validTurn && !hasTurn; }); delete server.url; server.urls = isString ? urls[0] : urls; return !!urls.length; } }); } },{"../utils":232}],226:[function(require,module,exports){ /* * Copyright (c) 2018 The adapter.js project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. */ /* eslint-env node */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.shimGetDisplayMedia = shimGetDisplayMedia; function shimGetDisplayMedia(window) { if (!('getDisplayMedia' in window.navigator)) { return; } if (!window.navigator.mediaDevices) { return; } if (window.navigator.mediaDevices && 'getDisplayMedia' in window.navigator.mediaDevices) { return; } window.navigator.mediaDevices.getDisplayMedia = window.navigator.getDisplayMedia.bind(window.navigator); } },{}],227:[function(require,module,exports){ /* * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. */ /* eslint-env node */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.shimGetUserMedia = shimGetUserMedia; function shimGetUserMedia(window) { var navigator = window && window.navigator; var shimError_ = function shimError_(e) { return { name: { PermissionDeniedError: 'NotAllowedError' }[e.name] || e.name, message: e.message, constraint: e.constraint, toString: function toString() { return this.name; } }; }; // getUserMedia error shim. var origGetUserMedia = navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices); navigator.mediaDevices.getUserMedia = function (c) { return origGetUserMedia(c).catch(function (e) { return Promise.reject(shimError_(e)); }); }; } },{}],228:[function(require,module,exports){ /* * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. */ /* eslint-env node */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.shimGetDisplayMedia = exports.shimGetUserMedia = undefined; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var _getusermedia = require('./getusermedia'); Object.defineProperty(exports, 'shimGetUserMedia', { enumerable: true, get: function get() { return _getusermedia.shimGetUserMedia; } }); var _getdisplaymedia = require('./getdisplaymedia'); Object.defineProperty(exports, 'shimGetDisplayMedia', { enumerable: true, get: function get() { return _getdisplaymedia.shimGetDisplayMedia; } }); exports.shimOnTrack = shimOnTrack; exports.shimPeerConnection = shimPeerConnection; exports.shimSenderGetStats = shimSenderGetStats; exports.shimReceiverGetStats = shimReceiverGetStats; exports.shimRemoveStream = shimRemoveStream; exports.shimRTCDataChannel = shimRTCDataChannel; exports.shimAddTransceiver = shimAddTransceiver; exports.shimGetParameters = shimGetParameters; exports.shimCreateOffer = shimCreateOffer; exports.shimCreateAnswer = shimCreateAnswer; var _utils = require('../utils'); var utils = _interopRequireWildcard(_utils); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function shimOnTrack(window) { if ((typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object' && window.RTCTrackEvent && 'receiver' in window.RTCTrackEvent.prototype && !('transceiver' in window.RTCTrackEvent.prototype)) { Object.defineProperty(window.RTCTrackEvent.prototype, 'transceiver', { get: function get() { return { receiver: this.receiver }; } }); } } function shimPeerConnection(window, browserDetails) { if ((typeof window === 'undefined' ? 'undefined' : _typeof(window)) !== 'object' || !(window.RTCPeerConnection || window.mozRTCPeerConnection)) { return; // probably media.peerconnection.enabled=false in about:config } if (!window.RTCPeerConnection && window.mozRTCPeerConnection) { // very basic support for old versions. window.RTCPeerConnection = window.mozRTCPeerConnection; } if (browserDetails.version < 53) { // shim away need for obsolete RTCIceCandidate/RTCSessionDescription. ['setLocalDescription', 'setRemoteDescription', 'addIceCandidate'].forEach(function (method) { var nativeMethod = window.RTCPeerConnection.prototype[method]; var methodObj = _defineProperty({}, method, function () { arguments[0] = new (method === 'addIceCandidate' ? window.RTCIceCandidate : window.RTCSessionDescription)(arguments[0]); return nativeMethod.apply(this, arguments); }); window.RTCPeerConnection.prototype[method] = methodObj[method]; }); } var modernStatsTypes = { inboundrtp: 'inbound-rtp', outboundrtp: 'outbound-rtp', candidatepair: 'candidate-pair', localcandidate: 'local-candidate', remotecandidate: 'remote-candidate' }; var nativeGetStats = window.RTCPeerConnection.prototype.getStats; window.RTCPeerConnection.prototype.getStats = function getStats() { var _arguments = Array.prototype.slice.call(arguments), selector = _arguments[0], onSucc = _arguments[1], onErr = _arguments[2]; return nativeGetStats.apply(this, [selector || null]).then(function (stats) { if (browserDetails.version < 53 && !onSucc) { // Shim only promise getStats with spec-hyphens in type names // Leave callback version alone; misc old uses of forEach before Map try { stats.forEach(function (stat) { stat.type = modernStatsTypes[stat.type] || stat.type; }); } catch (e) { if (e.name !== 'TypeError') { throw e; } // Avoid TypeError: "type" is read-only, in old versions. 34-43ish stats.forEach(function (stat, i) { stats.set(i, Object.assign({}, stat, { type: modernStatsTypes[stat.type] || stat.type })); }); } } return stats; }).then(onSucc, onErr); }; } function shimSenderGetStats(window) { if (!((typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object' && window.RTCPeerConnection && window.RTCRtpSender)) { return; } if (window.RTCRtpSender && 'getStats' in window.RTCRtpSender.prototype) { return; } var origGetSenders = window.RTCPeerConnection.prototype.getSenders; if (origGetSenders) { window.RTCPeerConnection.prototype.getSenders = function getSenders() { var _this = this; var senders = origGetSenders.apply(this, []); senders.forEach(function (sender) { return sender._pc = _this; }); return senders; }; } var origAddTrack = window.RTCPeerConnection.prototype.addTrack; if (origAddTrack) { window.RTCPeerConnection.prototype.addTrack = function addTrack() { var sender = origAddTrack.apply(this, arguments); sender._pc = this; return sender; }; } window.RTCRtpSender.prototype.getStats = function getStats() { return this.track ? this._pc.getStats(this.track) : Promise.resolve(new Map()); }; } function shimReceiverGetStats(window) { if (!((typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object' && window.RTCPeerConnection && window.RTCRtpSender)) { return; } if (window.RTCRtpSender && 'getStats' in window.RTCRtpReceiver.prototype) { return; } var origGetReceivers = window.RTCPeerConnection.prototype.getReceivers; if (origGetReceivers) { window.RTCPeerConnection.prototype.getReceivers = function getReceivers() { var _this2 = this; var receivers = origGetReceivers.apply(this, []); receivers.forEach(function (receiver) { return receiver._pc = _this2; }); return receivers; }; } utils.wrapPeerConnectionEvent(window, 'track', function (e) { e.receiver._pc = e.srcElement; return e; }); window.RTCRtpReceiver.prototype.getStats = function getStats() { return this._pc.getStats(this.track); }; } function shimRemoveStream(window) { if (!window.RTCPeerConnection || 'removeStream' in window.RTCPeerConnection.prototype) { return; } window.RTCPeerConnection.prototype.removeStream = function removeStream(stream) { var _this3 = this; utils.deprecated('removeStream', 'removeTrack'); this.getSenders().forEach(function (sender) { if (sender.track && stream.getTracks().includes(sender.track)) { _this3.removeTrack(sender); } }); }; } function shimRTCDataChannel(window) { // rename DataChannel to RTCDataChannel (native fix in FF60): // https://bugzilla.mozilla.org/show_bug.cgi?id=1173851 if (window.DataChannel && !window.RTCDataChannel) { window.RTCDataChannel = window.DataChannel; } } function shimAddTransceiver(window) { // https://github.com/webrtcHacks/adapter/issues/998#issuecomment-516921647 // Firefox ignores the init sendEncodings options passed to addTransceiver // https://bugzilla.mozilla.org/show_bug.cgi?id=1396918 if (!((typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object' && window.RTCPeerConnection)) { return; } var origAddTransceiver = window.RTCPeerConnection.prototype.addTransceiver; if (origAddTransceiver) { window.RTCPeerConnection.prototype.addTransceiver = function addTransceiver() { this.setParametersPromises = []; var initParameters = arguments[1]; var shouldPerformCheck = initParameters && 'sendEncodings' in initParameters; if (shouldPerformCheck) { // If sendEncodings params are provided, validate grammar initParameters.sendEncodings.forEach(function (encodingParam) { if ('rid' in encodingParam) { var ridRegex = /^[a-z0-9]{0,16}$/i; if (!ridRegex.test(encodingParam.rid)) { throw new TypeError('Invalid RID value provided.'); } } if ('scaleResolutionDownBy' in encodingParam) { if (!(parseFloat(encodingParam.scaleResolutionDownBy) >= 1.0)) { throw new RangeError('scale_resolution_down_by must be >= 1.0'); } } if ('maxFramerate' in encodingParam) { if (!(parseFloat(encodingParam.maxFramerate) >= 0)) { throw new RangeError('max_framerate must be >= 0.0'); } } }); } var transceiver = origAddTransceiver.apply(this, arguments); if (shouldPerformCheck) { // Check if the init options were applied. If not we do this in an // asynchronous way and save the promise reference in a global object. // This is an ugly hack, but at the same time is way more robust than // checking the sender parameters before and after the createOffer // Also note that after the createoffer we are not 100% sure that // the params were asynchronously applied so we might miss the // opportunity to recreate offer. var sender = transceiver.sender; var params = sender.getParameters(); if (!('encodings' in params) || // Avoid being fooled by patched getParameters() below. params.encodings.length === 1 && Object.keys(params.encodings[0]).length === 0) { params.encodings = initParameters.sendEncodings; sender.sendEncodings = initParameters.sendEncodings; this.setParametersPromises.push(sender.setParameters(params).then(function () { delete sender.sendEncodings; }).catch(function () { delete sender.sendEncodings; })); } } return transceiver; }; } } function shimGetParameters(window) { if (!((typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object' && window.RTCRtpSender)) { return; } var origGetParameters = window.RTCRtpSender.prototype.getParameters; if (origGetParameters) { window.RTCRtpSender.prototype.getParameters = function getParameters() { var params = origGetParameters.apply(this, arguments); if (!('encodings' in params)) { params.encodings = [].concat(this.sendEncodings || [{}]); } return params; }; } } function shimCreateOffer(window) { // https://github.com/webrtcHacks/adapter/issues/998#issuecomment-516921647 // Firefox ignores the init sendEncodings options passed to addTransceiver // https://bugzilla.mozilla.org/show_bug.cgi?id=1396918 if (!((typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object' && window.RTCPeerConnection)) { return; } var origCreateOffer = window.RTCPeerConnection.prototype.createOffer; window.RTCPeerConnection.prototype.createOffer = function createOffer() { var _this4 = this, _arguments2 = arguments; if (this.setParametersPromises && this.setParametersPromises.length) { return Promise.all(this.setParametersPromises).then(function () { return origCreateOffer.apply(_this4, _arguments2); }).finally(function () { _this4.setParametersPromises = []; }); } return origCreateOffer.apply(this, arguments); }; } function shimCreateAnswer(window) { // https://github.com/webrtcHacks/adapter/issues/998#issuecomment-516921647 // Firefox ignores the init sendEncodings options passed to addTransceiver // https://bugzilla.mozilla.org/show_bug.cgi?id=1396918 if (!((typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object' && window.RTCPeerConnection)) { return; } var origCreateAnswer = window.RTCPeerConnection.prototype.createAnswer; window.RTCPeerConnection.prototype.createAnswer = function createAnswer() { var _this5 = this, _arguments3 = arguments; if (this.setParametersPromises && this.setParametersPromises.length) { return Promise.all(this.setParametersPromises).then(function () { return origCreateAnswer.apply(_this5, _arguments3); }).finally(function () { _this5.setParametersPromises = []; }); } return origCreateAnswer.apply(this, arguments); }; } },{"../utils":232,"./getdisplaymedia":229,"./getusermedia":230}],229:[function(require,module,exports){ /* * Copyright (c) 2018 The adapter.js project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. */ /* eslint-env node */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.shimGetDisplayMedia = shimGetDisplayMedia; function shimGetDisplayMedia(window, preferredMediaSource) { if (window.navigator.mediaDevices && 'getDisplayMedia' in window.navigator.mediaDevices) { return; } if (!window.navigator.mediaDevices) { return; } window.navigator.mediaDevices.getDisplayMedia = function getDisplayMedia(constraints) { if (!(constraints && constraints.video)) { var err = new DOMException('getDisplayMedia without video ' + 'constraints is undefined'); err.name = 'NotFoundError'; // from https://heycam.github.io/webidl/#idl-DOMException-error-names err.code = 8; return Promise.reject(err); } if (constraints.video === true) { constraints.video = { mediaSource: preferredMediaSource }; } else { constraints.video.mediaSource = preferredMediaSource; } return window.navigator.mediaDevices.getUserMedia(constraints); }; } },{}],230:[function(require,module,exports){ /* * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. */ /* eslint-env node */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; exports.shimGetUserMedia = shimGetUserMedia; var _utils = require('../utils'); var utils = _interopRequireWildcard(_utils); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function shimGetUserMedia(window, browserDetails) { var navigator = window && window.navigator; var MediaStreamTrack = window && window.MediaStreamTrack; navigator.getUserMedia = function (constraints, onSuccess, onError) { // Replace Firefox 44+'s deprecation warning with unprefixed version. utils.deprecated('navigator.getUserMedia', 'navigator.mediaDevices.getUserMedia'); navigator.mediaDevices.getUserMedia(constraints).then(onSuccess, onError); }; if (!(browserDetails.version > 55 && 'autoGainControl' in navigator.mediaDevices.getSupportedConstraints())) { var remap = function remap(obj, a, b) { if (a in obj && !(b in obj)) { obj[b] = obj[a]; delete obj[a]; } }; var nativeGetUserMedia = navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices); navigator.mediaDevices.getUserMedia = function (c) { if ((typeof c === 'undefined' ? 'undefined' : _typeof(c)) === 'object' && _typeof(c.audio) === 'object') { c = JSON.parse(JSON.stringify(c)); remap(c.audio, 'autoGainControl', 'mozAutoGainControl'); remap(c.audio, 'noiseSuppression', 'mozNoiseSuppression'); } return nativeGetUserMedia(c); }; if (MediaStreamTrack && MediaStreamTrack.prototype.getSettings) { var nativeGetSettings = MediaStreamTrack.prototype.getSettings; MediaStreamTrack.prototype.getSettings = function () { var obj = nativeGetSettings.apply(this, arguments); remap(obj, 'mozAutoGainControl', 'autoGainControl'); remap(obj, 'mozNoiseSuppression', 'noiseSuppression'); return obj; }; } if (MediaStreamTrack && MediaStreamTrack.prototype.applyConstraints) { var nativeApplyConstraints = MediaStreamTrack.prototype.applyConstraints; MediaStreamTrack.prototype.applyConstraints = function (c) { if (this.kind === 'audio' && (typeof c === 'undefined' ? 'undefined' : _typeof(c)) === 'object') { c = JSON.parse(JSON.stringify(c)); remap(c, 'autoGainControl', 'mozAutoGainControl'); remap(c, 'noiseSuppression', 'mozNoiseSuppression'); } return nativeApplyConstraints.apply(this, [c]); }; } } } },{"../utils":232}],231:[function(require,module,exports){ /* * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; exports.shimLocalStreamsAPI = shimLocalStreamsAPI; exports.shimRemoteStreamsAPI = shimRemoteStreamsAPI; exports.shimCallbacksAPI = shimCallbacksAPI; exports.shimGetUserMedia = shimGetUserMedia; exports.shimConstraints = shimConstraints; exports.shimRTCIceServerUrls = shimRTCIceServerUrls; exports.shimTrackEventTransceiver = shimTrackEventTransceiver; exports.shimCreateOfferLegacy = shimCreateOfferLegacy; exports.shimAudioContext = shimAudioContext; var _utils = require('../utils'); var utils = _interopRequireWildcard(_utils); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function shimLocalStreamsAPI(window) { if ((typeof window === 'undefined' ? 'undefined' : _typeof(window)) !== 'object' || !window.RTCPeerConnection) { return; } if (!('getLocalStreams' in window.RTCPeerConnection.prototype)) { window.RTCPeerConnection.prototype.getLocalStreams = function getLocalStreams() { if (!this._localStreams) { this._localStreams = []; } return this._localStreams; }; } if (!('addStream' in window.RTCPeerConnection.prototype)) { var _addTrack = window.RTCPeerConnection.prototype.addTrack; window.RTCPeerConnection.prototype.addStream = function addStream(stream) { var _this = this; if (!this._localStreams) { this._localStreams = []; } if (!this._localStreams.includes(stream)) { this._localStreams.push(stream); } // Try to emulate Chrome's behaviour of adding in audio-video order. // Safari orders by track id. stream.getAudioTracks().forEach(function (track) { return _addTrack.call(_this, track, stream); }); stream.getVideoTracks().forEach(function (track) { return _addTrack.call(_this, track, stream); }); }; window.RTCPeerConnection.prototype.addTrack = function addTrack(track) { var _this2 = this; for (var _len = arguments.length, streams = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { streams[_key - 1] = arguments[_key]; } if (streams) { streams.forEach(function (stream) { if (!_this2._localStreams) { _this2._localStreams = [stream]; } else if (!_this2._localStreams.includes(stream)) { _this2._localStreams.push(stream); } }); } return _addTrack.apply(this, arguments); }; } if (!('removeStream' in window.RTCPeerConnection.prototype)) { window.RTCPeerConnection.prototype.removeStream = function removeStream(stream) { var _this3 = this; if (!this._localStreams) { this._localStreams = []; } var index = this._localStreams.indexOf(stream); if (index === -1) { return; } this._localStreams.splice(index, 1); var tracks = stream.getTracks(); this.getSenders().forEach(function (sender) { if (tracks.includes(sender.track)) { _this3.removeTrack(sender); } }); }; } } function shimRemoteStreamsAPI(window) { if ((typeof window === 'undefined' ? 'undefined' : _typeof(window)) !== 'object' || !window.RTCPeerConnection) { return; } if (!('getRemoteStreams' in window.RTCPeerConnection.prototype)) { window.RTCPeerConnection.prototype.getRemoteStreams = function getRemoteStreams() { return this._remoteStreams ? this._remoteStreams : []; }; } if (!('onaddstream' in window.RTCPeerConnection.prototype)) { Object.defineProperty(window.RTCPeerConnection.prototype, 'onaddstream', { get: function get() { return this._onaddstream; }, set: function set(f) { var _this4 = this; if (this._onaddstream) { this.removeEventListener('addstream', this._onaddstream); this.removeEventListener('track', this._onaddstreampoly); } this.addEventListener('addstream', this._onaddstream = f); this.addEventListener('track', this._onaddstreampoly = function (e) { e.streams.forEach(function (stream) { if (!_this4._remoteStreams) { _this4._remoteStreams = []; } if (_this4._remoteStreams.includes(stream)) { return; } _this4._remoteStreams.push(stream); var event = new Event('addstream'); event.stream = stream; _this4.dispatchEvent(event); }); }); } }); var origSetRemoteDescription = window.RTCPeerConnection.prototype.setRemoteDescription; window.RTCPeerConnection.prototype.setRemoteDescription = function setRemoteDescription() { var pc = this; if (!this._onaddstreampoly) { this.addEventListener('track', this._onaddstreampoly = function (e) { e.streams.forEach(function (stream) { if (!pc._remoteStreams) { pc._remoteStreams = []; } if (pc._remoteStreams.indexOf(stream) >= 0) { return; } pc._remoteStreams.push(stream); var event = new Event('addstream'); event.stream = stream; pc.dispatchEvent(event); }); }); } return origSetRemoteDescription.apply(pc, arguments); }; } } function shimCallbacksAPI(window) { if ((typeof window === 'undefined' ? 'undefined' : _typeof(window)) !== 'object' || !window.RTCPeerConnection) { return; } var prototype = window.RTCPeerConnection.prototype; var origCreateOffer = prototype.createOffer; var origCreateAnswer = prototype.createAnswer; var setLocalDescription = prototype.setLocalDescription; var setRemoteDescription = prototype.setRemoteDescription; var addIceCandidate = prototype.addIceCandidate; prototype.createOffer = function createOffer(successCallback, failureCallback) { var options = arguments.length >= 2 ? arguments[2] : arguments[0]; var promise = origCreateOffer.apply(this, [options]); if (!failureCallback) { return promise; } promise.then(successCallback, failureCallback); return Promise.resolve(); }; prototype.createAnswer = function createAnswer(successCallback, failureCallback) { var options = arguments.length >= 2 ? arguments[2] : arguments[0]; var promise = origCreateAnswer.apply(this, [options]); if (!failureCallback) { return promise; } promise.then(successCallback, failureCallback); return Promise.resolve(); }; var withCallback = function withCallback(description, successCallback, failureCallback) { var promise = setLocalDescription.apply(this, [description]); if (!failureCallback) { return promise; } promise.then(successCallback, failureCallback); return Promise.resolve(); }; prototype.setLocalDescription = withCallback; withCallback = function withCallback(description, successCallback, failureCallback) { var promise = setRemoteDescription.apply(this, [description]); if (!failureCallback) { return promise; } promise.then(successCallback, failureCallback); return Promise.resolve(); }; prototype.setRemoteDescription = withCallback; withCallback = function withCallback(candidate, successCallback, failureCallback) { var promise = addIceCandidate.apply(this, [candidate]); if (!failureCallback) { return promise; } promise.then(successCallback, failureCallback); return Promise.resolve(); }; prototype.addIceCandidate = withCallback; } function shimGetUserMedia(window) { var navigator = window && window.navigator; if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { // shim not needed in Safari 12.1 var mediaDevices = navigator.mediaDevices; var _getUserMedia = mediaDevices.getUserMedia.bind(mediaDevices); navigator.mediaDevices.getUserMedia = function (constraints) { return _getUserMedia(shimConstraints(constraints)); }; } if (!navigator.getUserMedia && navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { navigator.getUserMedia = function getUserMedia(constraints, cb, errcb) { navigator.mediaDevices.getUserMedia(constraints).then(cb, errcb); }.bind(navigator); } } function shimConstraints(constraints) { if (constraints && constraints.video !== undefined) { return Object.assign({}, constraints, { video: utils.compactObject(constraints.video) }); } return constraints; } function shimRTCIceServerUrls(window) { if (!window.RTCPeerConnection) { return; } // migrate from non-spec RTCIceServer.url to RTCIceServer.urls var OrigPeerConnection = window.RTCPeerConnection; window.RTCPeerConnection = function RTCPeerConnection(pcConfig, pcConstraints) { if (pcConfig && pcConfig.iceServers) { var newIceServers = []; for (var i = 0; i < pcConfig.iceServers.length; i++) { var server = pcConfig.iceServers[i]; if (!server.hasOwnProperty('urls') && server.hasOwnProperty('url')) { utils.deprecated('RTCIceServer.url', 'RTCIceServer.urls'); server = JSON.parse(JSON.stringify(server)); server.urls = server.url; delete server.url; newIceServers.push(server); } else { newIceServers.push(pcConfig.iceServers[i]); } } pcConfig.iceServers = newIceServers; } return new OrigPeerConnection(pcConfig, pcConstraints); }; window.RTCPeerConnection.prototype = OrigPeerConnection.prototype; // wrap static methods. Currently just generateCertificate. if ('generateCertificate' in OrigPeerConnection) { Object.defineProperty(window.RTCPeerConnection, 'generateCertificate', { get: function get() { return OrigPeerConnection.generateCertificate; } }); } } function shimTrackEventTransceiver(window) { // Add event.transceiver member over deprecated event.receiver if ((typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object' && window.RTCTrackEvent && 'receiver' in window.RTCTrackEvent.prototype && !('transceiver' in window.RTCTrackEvent.prototype)) { Object.defineProperty(window.RTCTrackEvent.prototype, 'transceiver', { get: function get() { return { receiver: this.receiver }; } }); } } function shimCreateOfferLegacy(window) { var origCreateOffer = window.RTCPeerConnection.prototype.createOffer; window.RTCPeerConnection.prototype.createOffer = function createOffer(offerOptions) { if (offerOptions) { if (typeof offerOptions.offerToReceiveAudio !== 'undefined') { // support bit values offerOptions.offerToReceiveAudio = !!offerOptions.offerToReceiveAudio; } var audioTransceiver = this.getTransceivers().find(function (transceiver) { return transceiver.receiver.track.kind === 'audio'; }); if (offerOptions.offerToReceiveAudio === false && audioTransceiver) { if (audioTransceiver.direction === 'sendrecv') { if (audioTransceiver.setDirection) { audioTransceiver.setDirection('sendonly'); } else { audioTransceiver.direction = 'sendonly'; } } else if (audioTransceiver.direction === 'recvonly') { if (audioTransceiver.setDirection) { audioTransceiver.setDirection('inactive'); } else { audioTransceiver.direction = 'inactive'; } } } else if (offerOptions.offerToReceiveAudio === true && !audioTransceiver) { this.addTransceiver('audio'); } if (typeof offerOptions.offerToReceiveVideo !== 'undefined') { // support bit values offerOptions.offerToReceiveVideo = !!offerOptions.offerToReceiveVideo; } var videoTransceiver = this.getTransceivers().find(function (transceiver) { return transceiver.receiver.track.kind === 'video'; }); if (offerOptions.offerToReceiveVideo === false && videoTransceiver) { if (videoTransceiver.direction === 'sendrecv') { if (videoTransceiver.setDirection) { videoTransceiver.setDirection('sendonly'); } else { videoTransceiver.direction = 'sendonly'; } } else if (videoTransceiver.direction === 'recvonly') { if (videoTransceiver.setDirection) { videoTransceiver.setDirection('inactive'); } else { videoTransceiver.direction = 'inactive'; } } } else if (offerOptions.offerToReceiveVideo === true && !videoTransceiver) { this.addTransceiver('video'); } } return origCreateOffer.apply(this, arguments); }; } function shimAudioContext(window) { if ((typeof window === 'undefined' ? 'undefined' : _typeof(window)) !== 'object' || window.AudioContext) { return; } window.AudioContext = window.webkitAudioContext; } },{"../utils":232}],232:[function(require,module,exports){ /* * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. */ /* eslint-env node */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; exports.extractVersion = extractVersion; exports.wrapPeerConnectionEvent = wrapPeerConnectionEvent; exports.disableLog = disableLog; exports.disableWarnings = disableWarnings; exports.log = log; exports.deprecated = deprecated; exports.detectBrowser = detectBrowser; exports.compactObject = compactObject; exports.walkStats = walkStats; exports.filterStats = filterStats; function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var logDisabled_ = true; var deprecationWarnings_ = true; /** * Extract browser version out of the provided user agent string. * * @param {!string} uastring userAgent string. * @param {!string} expr Regular expression used as match criteria. * @param {!number} pos position in the version string to be returned. * @return {!number} browser version. */ function extractVersion(uastring, expr, pos) { var match = uastring.match(expr); return match && match.length >= pos && parseInt(match[pos], 10); } // Wraps the peerconnection event eventNameToWrap in a function // which returns the modified event object (or false to prevent // the event). function wrapPeerConnectionEvent(window, eventNameToWrap, wrapper) { if (!window.RTCPeerConnection) { return; } var proto = window.RTCPeerConnection.prototype; var nativeAddEventListener = proto.addEventListener; proto.addEventListener = function (nativeEventName, cb) { if (nativeEventName !== eventNameToWrap) { return nativeAddEventListener.apply(this, arguments); } var wrappedCallback = function wrappedCallback(e) { var modifiedEvent = wrapper(e); if (modifiedEvent) { if (cb.handleEvent) { cb.handleEvent(modifiedEvent); } else { cb(modifiedEvent); } } }; this._eventMap = this._eventMap || {}; if (!this._eventMap[eventNameToWrap]) { this._eventMap[eventNameToWrap] = new Map(); } this._eventMap[eventNameToWrap].set(cb, wrappedCallback); return nativeAddEventListener.apply(this, [nativeEventName, wrappedCallback]); }; var nativeRemoveEventListener = proto.removeEventListener; proto.removeEventListener = function (nativeEventName, cb) { if (nativeEventName !== eventNameToWrap || !this._eventMap || !this._eventMap[eventNameToWrap]) { return nativeRemoveEventListener.apply(this, arguments); } if (!this._eventMap[eventNameToWrap].has(cb)) { return nativeRemoveEventListener.apply(this, arguments); } var unwrappedCb = this._eventMap[eventNameToWrap].get(cb); this._eventMap[eventNameToWrap].delete(cb); if (this._eventMap[eventNameToWrap].size === 0) { delete this._eventMap[eventNameToWrap]; } if (Object.keys(this._eventMap).length === 0) { delete this._eventMap; } return nativeRemoveEventListener.apply(this, [nativeEventName, unwrappedCb]); }; Object.defineProperty(proto, 'on' + eventNameToWrap, { get: function get() { return this['_on' + eventNameToWrap]; }, set: function set(cb) { if (this['_on' + eventNameToWrap]) { this.removeEventListener(eventNameToWrap, this['_on' + eventNameToWrap]); delete this['_on' + eventNameToWrap]; } if (cb) { this.addEventListener(eventNameToWrap, this['_on' + eventNameToWrap] = cb); } }, enumerable: true, configurable: true }); } function disableLog(bool) { if (typeof bool !== 'boolean') { return new Error('Argument type: ' + (typeof bool === 'undefined' ? 'undefined' : _typeof(bool)) + '. Please use a boolean.'); } logDisabled_ = bool; return bool ? 'adapter.js logging disabled' : 'adapter.js logging enabled'; } /** * Disable or enable deprecation warnings * @param {!boolean} bool set to true to disable warnings. */ function disableWarnings(bool) { if (typeof bool !== 'boolean') { return new Error('Argument type: ' + (typeof bool === 'undefined' ? 'undefined' : _typeof(bool)) + '. Please use a boolean.'); } deprecationWarnings_ = !bool; return 'adapter.js deprecation warnings ' + (bool ? 'disabled' : 'enabled'); } function log() { if ((typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object') { if (logDisabled_) { return; } if (typeof console !== 'undefined' && typeof console.log === 'function') { console.log.apply(console, arguments); } } } /** * Shows a deprecation warning suggesting the modern and spec-compatible API. */ function deprecated(oldMethod, newMethod) { if (!deprecationWarnings_) { return; } console.warn(oldMethod + ' is deprecated, please use ' + newMethod + ' instead.'); } /** * Browser detector. * * @return {object} result containing browser and version * properties. */ function detectBrowser(window) { // Returned result object. var result = { browser: null, version: null }; // Fail early if it's not a browser if (typeof window === 'undefined' || !window.navigator) { result.browser = 'Not a browser.'; return result; } var navigator = window.navigator; if (navigator.mozGetUserMedia) { // Firefox. result.browser = 'firefox'; result.version = extractVersion(navigator.userAgent, /Firefox\/(\d+)\./, 1); } else if (navigator.webkitGetUserMedia || window.isSecureContext === false && window.webkitRTCPeerConnection && !window.RTCIceGatherer) { // Chrome, Chromium, Webview, Opera. // Version matches Chrome/WebRTC version. // Chrome 74 removed webkitGetUserMedia on http as well so we need the // more complicated fallback to webkitRTCPeerConnection. result.browser = 'chrome'; result.version = extractVersion(navigator.userAgent, /Chrom(e|ium)\/(\d+)\./, 2); } else if (navigator.mediaDevices && navigator.userAgent.match(/Edge\/(\d+).(\d+)$/)) { // Edge. result.browser = 'edge'; result.version = extractVersion(navigator.userAgent, /Edge\/(\d+).(\d+)$/, 2); } else if (window.RTCPeerConnection && navigator.userAgent.match(/AppleWebKit\/(\d+)\./)) { // Safari. result.browser = 'safari'; result.version = extractVersion(navigator.userAgent, /AppleWebKit\/(\d+)\./, 1); result.supportsUnifiedPlan = window.RTCRtpTransceiver && 'currentDirection' in window.RTCRtpTransceiver.prototype; } else { // Default fallthrough: not supported. result.browser = 'Not a supported browser.'; return result; } return result; } /** * Checks if something is an object. * * @param {*} val The something you want to check. * @return true if val is an object, false otherwise. */ function isObject(val) { return Object.prototype.toString.call(val) === '[object Object]'; } /** * Remove all empty objects and undefined values * from a nested object -- an enhanced and vanilla version * of Lodash's `compact`. */ function compactObject(data) { if (!isObject(data)) { return data; } return Object.keys(data).reduce(function (accumulator, key) { var isObj = isObject(data[key]); var value = isObj ? compactObject(data[key]) : data[key]; var isEmptyObject = isObj && !Object.keys(value).length; if (value === undefined || isEmptyObject) { return accumulator; } return Object.assign(accumulator, _defineProperty({}, key, value)); }, {}); } /* iterates the stats graph recursively. */ function walkStats(stats, base, resultSet) { if (!base || resultSet.has(base.id)) { return; } resultSet.set(base.id, base); Object.keys(base).forEach(function (name) { if (name.endsWith('Id')) { walkStats(stats, stats.get(base[name]), resultSet); } else if (name.endsWith('Ids')) { base[name].forEach(function (id) { walkStats(stats, stats.get(id), resultSet); }); } }); } /* filter getStats for a sender/receiver track. */ function filterStats(result, track, outbound) { var streamStatsType = outbound ? 'outbound-rtp' : 'inbound-rtp'; var filteredResult = new Map(); if (track === null) { return filteredResult; } var trackStats = []; result.forEach(function (value) { if (value.type === 'track' && value.trackIdentifier === track.id) { trackStats.push(value); } }); trackStats.forEach(function (trackStat) { result.forEach(function (stats) { if (stats.type === streamStatsType && stats.trackId === trackStat.id) { walkStats(result, stats, filteredResult); } }); }); return filteredResult; } },{}],233:[function(require,module,exports){ module.exports = extend var hasOwnProperty = Object.prototype.hasOwnProperty; function extend() { var target = {} for (var i = 0; i < arguments.length; i++) { var source = arguments[i] for (var key in source) { if (hasOwnProperty.call(source, key)) { target[key] = source[key] } } } return target } },{}],234:[function(require,module,exports){ 'use strict'; /** JSHint inline rules */ /* globals Strophe, $pres, $msg, $iq */ var chatUtils = require('./qbChatHelpers'), config = require('../../qbConfig'), Utils = require('../../qbUtils'), StreamManagement = require('../../plugins/streamManagement'); var unsupportedError = 'This function isn\'t supported outside of the browser (...yet)'; var XMPP; /** create StropheJS or NodeXMPP connection object */ if (Utils.getEnv().browser) { var Connection = require('../../qbStrophe'); Strophe.addNamespace('CARBONS', chatUtils.MARKERS.CARBONS); Strophe.addNamespace('CHAT_MARKERS', chatUtils.MARKERS.CHAT); Strophe.addNamespace('PRIVACY_LIST', chatUtils.MARKERS.PRIVACY); Strophe.addNamespace('CHAT_STATES', chatUtils.MARKERS.STATES); } else if (Utils.getEnv().nativescript) { XMPP = require('nativescript-xmpp-client'); } else if (Utils.getEnv().node) { XMPP = require('node-xmpp-client'); } function ChatProxy(service) { var self = this; var originSendFunction; self.webrtcSignalingProcessor = null; /** * Browser env. * Uses by Strophe */ if (Utils.getEnv().browser) { // strophe js self.connection = Connection(); /** Add extension methods to track handlers for removal on reconnect */ self.connection.XHandlerReferences = []; self.connection.XAddTrackedHandler = function (handler, ns, name, type, id, from, options) { self.connection.XHandlerReferences.push(self.connection.addHandler(handler, ns, name, type, id, from, options)); }; self.connection.XDeleteHandlers = function () { while (self.connection.XHandlerReferences.length) { self.connection.deleteHandler(self.connection.XHandlerReferences.pop()); } }; originSendFunction = self.connection.send; self.connection.send = function (stanza) { if (!self.connection.connected) { throw new chatUtils.ChatNotConnectedError('Chat is not connected'); } originSendFunction.call(self.connection, stanza); }; } else { // nativescript-xmpp-client if (Utils.getEnv().nativescript) { self.Client = new XMPP.Client({ 'websocket': { 'url': config.chatProtocol.websocket }, 'autostart': false }); // node-xmpp-client } else if (Utils.getEnv().node) { self.Client = new XMPP({ 'autostart': false }); } // override 'send' function to add some logs originSendFunction = self.Client.send; self.Client.send = function (stanza) { Utils.QBLog('[QBChat]', 'SENT:', stanza.toString()); originSendFunction.call(self.Client, stanza); }; self.nodeStanzasCallbacks = {}; } this.service = service; // Check the chat connection (return true/false) this.isConnected = false; // Check the chat connecting state (return true/false) this._isConnecting = false; this._isLogout = false; this._checkConnectionTimer = undefined; this._checkConnectionPingTimer = undefined; this._chatPingFailedCounter = 0; this._onlineStatus = true; this._checkExpiredSessionTimer = undefined; this._sessionHasExpired = false; this._pings = {}; // this.helpers = new Helpers(); // var options = { service: service, helpers: self.helpers, stropheClient: self.connection, xmppClient: self.Client, nodeStanzasCallbacks: self.nodeStanzasCallbacks }; this.roster = new RosterProxy(options); this.privacylist = new PrivacyListProxy(options); this.muc = new MucProxy(options); // this.chatUtils = chatUtils; if (config.streamManagement.enable) { if (config.chatProtocol.active === 2) { this.streamManagement = new StreamManagement(config.streamManagement); self._sentMessageCallback = function (messageLost, messageSent) { if (typeof self.onSentMessageCallback === 'function') { if (messageSent) { self.onSentMessageCallback(null, messageSent); } else { self.onSentMessageCallback(messageLost); } } }; } else { Utils.QBLog('[QBChat] StreamManagement:', 'BOSH protocol doesn\'t support stream management. Set WebSocket as the "chatProtocol" parameter to use this functionality. https://quickblox.com/developers/Javascript#Configuration'); } } /** * User's callbacks (listener-functions): * - onMessageListener (userId, message) * - onMessageErrorListener (messageId, error) * - onSentMessageCallback (messageLost, messageSent) * - onMessageTypingListener (isTyping, userId, dialogId) * - onDeliveredStatusListener (messageId, dialogId, userId); * - onReadStatusListener (messageId, dialogId, userId); * - onSystemMessageListener (message) * - onKickOccupant(dialogId, initiatorUserId) * - onJoinOccupant(dialogId, userId) * - onLeaveOccupant(dialogId, userId) * - onContactListListener (userId, type) * - onSubscribeListener (userId) * - onConfirmSubscribeListener (userId) * - onRejectSubscribeListener (userId) * - onLastUserActivityListener (userId, seconds) * - onDisconnectedListener * - onReconnectListener * - onSessionExpiredListener */ /** * You need to set onMessageListener function, to get messages. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Create_new_dialog More info.} * @function onMessageListener * @memberOf QB.chat * @param {Number} userId - Sender id * @param {Object} message - The message model object **/ /** * Blocked entities receive an error when try to chat with a user in a 1-1 chat and receivie nothing in a group chat. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Blocked_user_attempts_to_communicate_with_user More info.} * @function onMessageErrorListener * @memberOf QB.chat * @param {Number} messageId - The message id * @param {Object} error - The error object **/ /** * This feature defines an approach for ensuring is the message delivered to the server. This feature is unabled by default. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Sent_message More info.} * @function onSentMessageCallback * @memberOf QB.chat * @param {Object} messageLost - The lost message model object (Fail) * @param {Object} messageSent - The sent message model object (Success) **/ /** * Show typing status in chat or groupchat. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Typing_status More info.} * @function onMessageTypingListener * @memberOf QB.chat * @param {Boolean} isTyping - Typing Status (true - typing, false - stop typing) * @param {Number} userId - Typing user id * @param {String} dialogId - The dialog id **/ /** * Receive delivery confirmations {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Delivered_status More info.} * @function onDeliveredStatusListener * @memberOf QB.chat * @param {String} messageId - Delivered message id * @param {String} dialogId - The dialog id * @param {Number} userId - User id **/ /** * You can manage 'read' notifications in chat. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Read_status More info.} * @function onReadStatusListener * @memberOf QB.chat * @param {String} messageId - Read message id * @param {String} dialogId - The dialog id * @param {Number} userId - User Id **/ /** * These messages work over separated channel and won't be mixed with the regular chat messages. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#System_notifications More info.} * @function onSystemMessageListener * @memberOf QB.chat * @param {Object} message - The system message model object. Always have type: 'headline' **/ /** * You will receive this callback when you are in group chat dialog(joined) and other user (chat dialog's creator) removed you from occupants. * @function onKickOccupant * @memberOf QB.chat * @param {String} dialogId - An id of chat dialog where you was kicked from. * @param {Number} initiatorUserId - An id of user who has kicked you. **/ /** * You will receive this callback when some user joined group chat dialog you are in. * @function onJoinOccupant * @memberOf QB.chat * @param {String} dialogId - An id of chat dialog that user joined. * @param {Number} userId - An id of user who joined chat dialog. **/ /** * You will receive this callback when some user left group chat dialog you are in. * @function onLeaveOccupant * @memberOf QB.chat * @param {String} dialogId - An id of chat dialog that user left. * @param {Number} userId - An id of user who left chat dialog. **/ /** * Receive user status (online / offline). {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Roster_callbacks More info.} * @function onContactListListener * @memberOf QB.chat * @param {Number} userId - The sender ID * @param {String} type - If user leave the chat, type will be 'unavailable' **/ /** * Receive subscription request. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Roster_callbacks More info.} * @function onSubscribeListener * @memberOf QB.chat * @param {Number} userId - The sender ID **/ /** * Receive confirm request. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Roster_callbacks More info.} * @function onConfirmSubscribeListener * @memberOf QB.chat * @param {Number} userId - The sender ID **/ /** * Receive reject request. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Roster_callbacks More info.} * @function onRejectSubscribeListener * @memberOf QB.chat * @param {Number} userId - The sender ID **/ /** * Receive user's last activity (time ago). {@link https://xmpp.org/extensions/xep-0012.html More info.} * @function onLastUserActivityListener * @memberOf QB.chat * @param {Number} userId - The user's ID which last activity time we receive * @param {Number} seconds - Time ago (last activity in seconds or 0 if user online or undefined if user never registered in chat) */ /** * Run after disconnect from chat. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Logout_from_Chat More info.} * @function onDisconnectedListener * @memberOf QB.chat **/ /** * By default Javascript SDK reconnects automatically when connection to server is lost. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Reconnection More info.} * @function onReconnectListener * @memberOf QB.chat **/ this._onMessage = function (stanza) { var from = chatUtils.getAttr(stanza, 'from'), to = chatUtils.getAttr(stanza, 'to'), type = chatUtils.getAttr(stanza, 'type'), messageId = chatUtils.getAttr(stanza, 'id'), markable = chatUtils.getElement(stanza, 'markable'), delivered = chatUtils.getElement(stanza, 'received'), read = chatUtils.getElement(stanza, 'displayed'), composing = chatUtils.getElement(stanza, 'composing'), paused = chatUtils.getElement(stanza, 'paused'), invite = chatUtils.getElement(stanza, 'invite'), delay = chatUtils.getElement(stanza, 'delay'), extraParams = chatUtils.getElement(stanza, 'extraParams'), bodyContent = chatUtils.getElementText(stanza, 'body'), forwarded = chatUtils.getElement(stanza, 'forwarded'), extraParamsParsed, recipientId, recipient, jid; if (Utils.getEnv().browser) { recipient = stanza.querySelector('forwarded') ? stanza.querySelector('forwarded').querySelector('message').getAttribute('to') : null; jid = self.connection.jid; } else { var forwardedMessage = forwarded ? chatUtils.getElement(forwarded, 'message') : null; recipient = forwardedMessage ? chatUtils.getAttr(forwardedMessage, 'to') : null; jid = self.Client.options.jid.user; } recipientId = recipient ? self.helpers.getIdFromNode(recipient) : null; var dialogId = type === 'groupchat' ? self.helpers.getDialogIdFromNode(from) : null, userId = type === 'groupchat' ? self.helpers.getIdFromResource(from) : self.helpers.getIdFromNode(from), marker = delivered || read || null; // ignore invite messages from MUC if (invite) return true; if (extraParams) { extraParamsParsed = chatUtils.parseExtraParams(extraParams); if (extraParamsParsed.dialogId) { dialogId = extraParamsParsed.dialogId; } } if (composing || paused) { if (typeof self.onMessageTypingListener === 'function' && (type === 'chat' || type === 'groupchat' || !delay)) { Utils.safeCallbackCall(self.onMessageTypingListener, !!composing, userId, dialogId); } return true; } if (marker) { if (delivered) { if (typeof self.onDeliveredStatusListener === 'function' && type === 'chat') { Utils.safeCallbackCall(self.onDeliveredStatusListener, chatUtils.getAttr(delivered, 'id'), dialogId, userId); } } else { if (typeof self.onReadStatusListener === 'function' && type === 'chat') { Utils.safeCallbackCall(self.onReadStatusListener, chatUtils.getAttr(read, 'id'), dialogId, userId); } } return true; } // autosend 'received' status (ignore messages from yourself) if (markable && userId != self.helpers.getIdFromNode(jid)) { var autoSendReceiveStatusParams = { messageId: messageId, userId: userId, dialogId: dialogId }; self.sendDeliveredStatus(autoSendReceiveStatusParams); } var message = { id: messageId, dialog_id: dialogId, recipient_id: recipientId, type: type, body: bodyContent, extension: extraParamsParsed ? extraParamsParsed.extension : null, delay: delay }; if (markable) { message.markable = 1; } if (typeof self.onMessageListener === 'function' && (type === 'chat' || type === 'groupchat')) { Utils.safeCallbackCall(self.onMessageListener, userId, message); } // we must return true to keep the handler alive // returning false would remove it after it finishes return true; }; this._onPresence = function (stanza) { var from = chatUtils.getAttr(stanza, 'from'), to = chatUtils.getAttr(stanza, 'to'), id = chatUtils.getAttr(stanza, 'id'), type = chatUtils.getAttr(stanza, 'type'), currentUserId = self.helpers.getIdFromNode(self.helpers.userCurrentJid(Utils.getEnv().browser ? self.connection : self.Client)), x = chatUtils.getElement(stanza, 'x'), xXMLNS, status, statusCode, dialogId, userId; if (x) { xXMLNS = chatUtils.getAttr(x, 'xmlns'); status = chatUtils.getElement(x, 'status'); if (status) { statusCode = chatUtils.getAttr(status, 'code'); } } // MUC presences go here if (xXMLNS && xXMLNS == "http://jabber.org/protocol/muc#user") { dialogId = self.helpers.getDialogIdFromNode(from); userId = self.helpers.getUserIdFromRoomJid(from); // KICK from dialog event if (status && statusCode == "301") { if (typeof self.onKickOccupant === 'function') { var actorElement = chatUtils.getElement(chatUtils.getElement(x, 'item'), 'actor'); var initiatorUserJid = chatUtils.getAttr(actorElement, 'jid'); Utils.safeCallbackCall(self.onKickOccupant, dialogId, self.helpers.getIdFromNode(initiatorUserJid)); } delete self.muc.joinedRooms[self.helpers.getRoomJidFromRoomFullJid(from)]; return true; // Occupants JOIN/LEAVE events } else if (!status) { if (userId != currentUserId) { // Leave if (type && type === 'unavailable') { if (typeof self.onLeaveOccupant === 'function') { Utils.safeCallbackCall(self.onLeaveOccupant, dialogId, parseInt(userId)); } return true; // Join } else { if (typeof self.onJoinOccupant === 'function') { Utils.safeCallbackCall(self.onJoinOccupant, dialogId, parseInt(userId)); } return true; } } } } if (!Utils.getEnv().browser) { /** MUC */ if (xXMLNS) { if (xXMLNS == "http://jabber.org/protocol/muc#user") { /** * if you make 'leave' from dialog * stanza will be contains type="unavailable" */ if (type && type === 'unavailable') { /** LEAVE from dialog */ if (status && statusCode == "110") { if (typeof self.nodeStanzasCallbacks['muc:leave'] === 'function') { Utils.safeCallbackCall(self.nodeStanzasCallbacks['muc:leave'], null); } } return true; } /** JOIN to dialog success */ if (id.endsWith(":join") && status && statusCode == "110") { if (typeof self.nodeStanzasCallbacks[id] === 'function') { self.nodeStanzasCallbacks[id](stanza); } return true; } // an error } else if (type && type === 'error' && xXMLNS == "http://jabber.org/protocol/muc") { /** JOIN to dialog error */ if (id.endsWith(":join")) { if (typeof self.nodeStanzasCallbacks[id] === 'function') { self.nodeStanzasCallbacks[id](stanza); } } return true; } } } // ROSTER presences go here userId = self.helpers.getIdFromNode(from); if (!type) { if (typeof self.onContactListListener === 'function' && self.roster.contacts[userId] && self.roster.contacts[userId].subscription !== 'none') { Utils.safeCallbackCall(self.onContactListListener, userId); } } else { switch (type) { case 'subscribe': if (self.roster.contacts[userId] && self.roster.contacts[userId].subscription === 'to') { self.roster.contacts[userId] = { subscription: 'both', ask: null }; self.roster._sendSubscriptionPresence({ jid: from, type: 'subscribed' }); } else { if (typeof self.onSubscribeListener === 'function') { Utils.safeCallbackCall(self.onSubscribeListener, userId); } } break; case 'subscribed': if (self.roster.contacts[userId] && self.roster.contacts[userId].subscription === 'from') { self.roster.contacts[userId] = { subscription: 'both', ask: null }; } else { self.roster.contacts[userId] = { subscription: 'to', ask: null }; if (typeof self.onConfirmSubscribeListener === 'function') { Utils.safeCallbackCall(self.onConfirmSubscribeListener, userId); } } break; case 'unsubscribed': self.roster.contacts[userId] = { subscription: 'none', ask: null }; if (typeof self.onRejectSubscribeListener === 'function') { Utils.safeCallbackCall(self.onRejectSubscribeListener, userId); } break; case 'unsubscribe': self.roster.contacts[userId] = { subscription: 'to', ask: null }; break; case 'unavailable': if (typeof self.onContactListListener === 'function' && self.roster.contacts[userId] && self.roster.contacts[userId].subscription !== 'none') { Utils.safeCallbackCall(self.onContactListListener, userId, type); } // send initial presence if one of client (instance) goes offline if (userId === currentUserId) { if (Utils.getEnv().browser) { self.connection.send($pres()); } else { self.Client.send(chatUtils.createStanza(XMPP.Stanza, null, 'presence')); } } break; } } // we must return true to keep the handler alive // returning false would remove it after it finishes return true; }; this._onIQ = function (stanza) { var stanzaId = chatUtils.getAttr(stanza, 'id'); var isLastActivity = stanzaId.indexOf('lastActivity') > -1; var isPong = stanzaId.indexOf('ping') > -1; var ping = chatUtils.getElement(stanza, 'ping'); var type = chatUtils.getAttr(stanza, 'type'); var from = chatUtils.getAttr(stanza, 'from'); var userId = from ? self.helpers.getIdFromNode(from) : null; if (typeof self.onLastUserActivityListener === 'function' && isLastActivity) { var query = chatUtils.getElement(stanza, 'query'), error = chatUtils.getElement(stanza, 'error'), seconds = error ? undefined : +chatUtils.getAttr(query, 'seconds'); Utils.safeCallbackCall(self.onLastUserActivityListener, userId, seconds); } if ((ping || isPong) && type) { if (type === 'get' && ping && self.isConnected) { // pong var builder = Utils.getEnv().browser ? $iq : XMPP.Stanza; var pongParams = { from: self.helpers.getUserCurrentJid(), id: stanzaId, to: from, type: 'result' }; var pongStanza = chatUtils.createStanza(builder, pongParams, 'iq'); if (Utils.getEnv().browser) { self.connection.send(pongStanza); } else { self.Client.send(pongStanza); } } else { var pingRequest = self._pings[stanzaId]; if (pingRequest) { if (pingRequest.callback) { pingRequest.callback(null); } if (pingRequest.interval) { clearInterval(pingRequest.interval); } self._pings[stanzaId] = undefined; delete self._pings[stanzaId]; } } } if (!Utils.getEnv().browser) { if (self.nodeStanzasCallbacks[stanzaId]) { Utils.safeCallbackCall(self.nodeStanzasCallbacks[stanzaId], stanza); delete self.nodeStanzasCallbacks[stanzaId]; } } return true; }; this._onSystemMessageListener = function (stanza) { var from = chatUtils.getAttr(stanza, 'from'), to = chatUtils.getAttr(stanza, 'to'), messageId = chatUtils.getAttr(stanza, 'id'), extraParams = chatUtils.getElement(stanza, 'extraParams'), userId = self.helpers.getIdFromNode(from), delay = chatUtils.getElement(stanza, 'delay'), moduleIdentifier = chatUtils.getElementText(extraParams, 'moduleIdentifier'), bodyContent = chatUtils.getElementText(stanza, 'body'), extraParamsParsed = chatUtils.parseExtraParams(extraParams), message; if (moduleIdentifier === 'SystemNotifications' && typeof self.onSystemMessageListener === 'function') { message = { id: messageId, userId: userId, body: bodyContent, extension: extraParamsParsed.extension }; Utils.safeCallbackCall(self.onSystemMessageListener, message); } else if (self.webrtcSignalingProcessor && !delay && moduleIdentifier === 'WebRTCVideoChat') { self.webrtcSignalingProcessor._onMessage(from, extraParams, delay, userId, extraParamsParsed.extension); } /** * we must return true to keep the handler alive * returning false would remove it after it finishes */ return true; }; this._onMessageErrorListener = function (stanza) { // // // Service not available. // var messageId = chatUtils.getAttr(stanza, 'id'); var error = chatUtils.getErrorFromXMLNode(stanza); // fire 'onMessageErrorListener' // if (typeof self.onMessageErrorListener === 'function') { Utils.safeCallbackCall(self.onMessageErrorListener, messageId, error); } // we must return true to keep the handler alive // returning false would remove it after it finishes return true; }; } /* Chat module: Core ----------------------------------------------------------------------------- */ ChatProxy.prototype = { /** * self.connection to the chat. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Login_to_Chat More info.} * @memberof QB.chat * @param {Object} params - Connect to the chat parameters * @param {Number} params.userId - Connect to the chat by user id (use instead params.email and params.jid) * @param {String} params.jid - Connect to the chat by user jid (use instead params.userId and params.email) * @param {String} params.email - Connect to the chat by user's email (use instead params.userId and params.jid) * @param {String} params.password - The user's password or session token * @param {chatConnectCallback} callback - The chatConnectCallback callback * */ connect: function (params, callback) { /** * This callback Returns error or contact list. * @callback chatConnectCallback * @param {Object} error - The error object * @param {(Object|Boolean)} response - Object of subscribed users (roster) or empty body. * */ Utils.QBLog('[QBChat]', 'Connect with parameters ' + JSON.stringify(params)); var self = this, userJid = chatUtils.buildUserJid(params), isInitialConnect = typeof callback === 'function', err; if (self._isConnecting) { err = Utils.getError(422, 'Status.REJECT - The connection is still in the Status.CONNECTING state', 'QBChat'); if (isInitialConnect) { callback(err, null); } return; } if (self.isConnected) { Utils.QBLog('[QBChat]', 'Status.CONNECTED - You are already connected'); if (isInitialConnect) { callback(null, self.roster.contacts); } return; } self._isConnecting = true; self._isLogout = false; /** Connect for browser env. */ if (Utils.getEnv().browser) { Utils.QBLog('[QBChat]', '!!---Browser env - connected--!!'); self.connection.connect(userJid, params.password, function (status) { Utils.QBLog('[QBChat]', 'self.connection.connect called with status ' + status); switch (status) { case Strophe.Status.ERROR: self.isConnected = false; self._isConnecting = false; err = Utils.getError(422, 'Status.ERROR - An error has occurred', 'QBChat'); if (isInitialConnect) { callback(err, null); } break; case Strophe.Status.CONNFAIL: self.isConnected = false; self._isConnecting = false; err = Utils.getError(422, 'Status.CONNFAIL - The connection attempt failed', 'QBChat'); if (isInitialConnect) { callback(err, null); } break; case Strophe.Status.AUTHENTICATING: Utils.QBLog('[QBChat]', 'Status.AUTHENTICATING'); break; case Strophe.Status.AUTHFAIL: self.isConnected = false; self._isConnecting = false; err = Utils.getError(401, 'Status.AUTHFAIL - The authentication attempt failed', 'QBChat'); if (isInitialConnect) { callback(err, null); } if (!self.isConnected && typeof self.onReconnectFailedListener === 'function') { Utils.safeCallbackCall(self.onReconnectFailedListener, err); } break; case Strophe.Status.CONNECTING: Utils.QBLog('[QBChat]', 'Status.CONNECTING', '(Chat Protocol - ' + (config.chatProtocol.active === 1 ? 'BOSH' : 'WebSocket' + ')')); break; case Strophe.Status.CONNECTED: // Remove any handlers that might exist from a previous connection via // extension method added to the connection on initialization in qbMain. // NOTE: streamManagement also adds handlers, so do this first. self.connection.XDeleteHandlers(); self.connection.XAddTrackedHandler(self._onMessage, null, 'message', 'chat'); self.connection.XAddTrackedHandler(self._onMessage, null, 'message', 'groupchat'); self.connection.XAddTrackedHandler(self._onPresence, null, 'presence'); self.connection.XAddTrackedHandler(self._onIQ, null, 'iq'); self.connection.XAddTrackedHandler(self._onSystemMessageListener, null, 'message', 'headline'); self.connection.XAddTrackedHandler(self._onMessageErrorListener, null, 'message', 'error'); var noTimerId = typeof self._checkConnectionPingTimer === 'undefined'; noTimerId = config.pingLocalhostTimeInterval === 0 ? false : noTimerId; if (noTimerId) { Utils.QBLog('[QBChat]', 'Init ping to chat at ', Utils.getCurrentTime()); self._checkConnectionPingTimer = setInterval(function () { try { self.pingchat(function (error) { if (error) { Utils.QBLog('[QBChat]', 'Chat Ping: ', 'failed, at ', Utils.getCurrentTime(), '_chatPingFailedCounter: ', self._chatPingFailedCounter, ' error: ', error); self._chatPingFailedCounter += 1; if (self._chatPingFailedCounter > 6) { self.isConnected = false; self._isConnecting = false; self._chatPingFailedCounter = 0; self._establishConnection(params); } } else { Utils.QBLog('[QBChat]', 'Chat Ping: ', 'ok, at ', Utils.getCurrentTime(), '_chatPingFailedCounter: ', self._chatPingFailedCounter); self._chatPingFailedCounter = 0; } }); } catch (err) { Utils.QBLog('[QBChat]', 'Chat Ping: ', 'Exception, at ', Utils.getCurrentTime(), ', detail info: ', err); } }, config.pingLocalhostTimeInterval * 1000); } if (typeof self.onSessionExpiredListener === 'function') { var noExpiredSessionTimerId = typeof self._checkExpiredSessionTimer === 'undefined'; if (noExpiredSessionTimerId) { Utils.QBLog('[QBChat]', 'Init timer for check session expired at ', ((new Date()).toTimeString().split(' ')[0])); self._checkExpiredSessionTimer = setInterval(function () { var timeNow = new Date(); if (typeof config.qbTokenExpirationDate !== 'undefined') { var timeLag = Math.round((timeNow.getTime() - config.qbTokenExpirationDate.getTime()) / (1000 * 60)); if (timeLag >= 0) { self._sessionHasExpired = true; Utils.safeCallbackCall(self.onSessionExpiredListener, null); } } // TODO: in task [CROS-823], may by need to change 5 * 1000 to config.liveSessionInterval * 1000 }, 5 * 1000); } } self._postConnectActions(function (roster) { callback(null, roster); }, isInitialConnect); break; case Strophe.Status.DISCONNECTING: Utils.QBLog('[QBChat]', 'Status.DISCONNECTING'); break; case Strophe.Status.DISCONNECTED: Utils.QBLog('[QBChat]', 'Status.DISCONNECTED at ' + chatUtils.getLocalTime()); // fire 'onDisconnectedListener' only once if (self.isConnected && typeof self.onDisconnectedListener === 'function') { Utils.safeCallbackCall(self.onDisconnectedListener); } self.isConnected = false; self._isConnecting = false; self.connection.reset(); // reconnect to chat and enable check connection self._establishConnection(params); break; case Strophe.Status.ATTACHED: Utils.QBLog('[QBChat]', 'Status.ATTACHED'); break; } }); } /** connect for node */ if (!Utils.getEnv().browser) { Utils.QBLog('[QBChat]', '!!--call branch code connect for node--!!'); // Remove all connection handlers exist from a previous connection self.Client.removeAllListeners(); self.Client.on('connect', function () { Utils.QBLog('[QBChat]', 'Status.CONNECTING', '(Chat Protocol - ' + (config.chatProtocol.active === 1 ? 'BOSH' : 'WebSocket' + ')')); }); self.Client.on('auth', function () { Utils.QBLog('[QBChat]', 'Status.AUTHENTICATING'); }); self.Client.on('online', function () { self._postConnectActions(function (roster) { callback(null, roster); }, isInitialConnect); }); self.Client.on('stanza', function (stanza) { Utils.QBLog('[QBChat] RECV:', stanza.toString()); /** * Detect typeof incoming stanza * and fire the Listener */ if (stanza.is('presence')) { self._onPresence(stanza); } else if (stanza.is('iq')) { self._onIQ(stanza); } else if (stanza.is('message')) { if (stanza.attrs.type === 'headline') { self._onSystemMessageListener(stanza); } else if (stanza.attrs.type === 'error') { self._onMessageErrorListener(stanza); } else { self._onMessage(stanza); } } }); self.Client.on('disconnect', function () { Utils.QBLog('[QBChat]', 'Status.DISCONNECTED - ' + chatUtils.getLocalTime()); if (typeof self.onDisconnectedListener === 'function') { Utils.safeCallbackCall(self.onDisconnectedListener); } self.isConnected = false; self._isConnecting = false; // reconnect to chat and enable check connection self._establishConnection(params); }); self.Client.on('error', function () { Utils.QBLog('[QBChat]', 'Status.ERROR - ' + chatUtils.getLocalTime()); err = Utils.getError(422, 'Status.ERROR - An error has occurred', 'QBChat'); if (isInitialConnect) { callback(err, null); } self.isConnected = false; self._isConnecting = false; }); self.Client.on('end', function () { self.Client.removeAllListeners(); }); self.Client.options.jid = userJid; self.Client.options.password = params.password; self.Client.connect(); } }, /** * Actions after the connection is established * * - enable stream management (the configuration setting); * - save user's JID; * - enable carbons; * - get and storage the user's roster (if the initial connect); * - recover the joined rooms and fire 'onReconnectListener' (if the reconnect); * - send initial presence to the chat server. */ _postConnectActions: function (callback, isInitialConnect) { Utils.QBLog('[QBChat]', 'Status.CONNECTED at ' + chatUtils.getLocalTime()); var self = this; var isBrowser = Utils.getEnv().browser; var xmppClient = isBrowser ? self.connection : self.Client; var presence = isBrowser ? $pres() : chatUtils.createStanza(XMPP.Stanza, null, 'presence'); if (config.streamManagement.enable && config.chatProtocol.active === 2) { self.streamManagement.enable(self.connection, null); self.streamManagement.sentMessageCallback = self._sentMessageCallback; } self.helpers.setUserCurrentJid(self.helpers.userCurrentJid(xmppClient)); self.isConnected = true; self._isConnecting = false; self._sessionHasExpired = false; self._enableCarbons(); if (isInitialConnect) { self.roster.get(function (contacts) { xmppClient.send(presence); self.roster.contacts = contacts; callback(self.roster.contacts); }); } else { var rooms = Object.keys(self.muc.joinedRooms); xmppClient.send(presence); Utils.QBLog('[QBChat]', 'Re-joining ' + rooms.length + " rooms..."); for (var i = 0, len = rooms.length; i < len; i++) { self.muc.join(rooms[i]); } if (typeof self.onReconnectListener === 'function') { Utils.safeCallbackCall(self.onReconnectListener); } } }, _establishConnection: function (params) { var self = this; Utils.QBLog('[QBChat]', '_establishConnection called'); if (self._isLogout || self._checkConnectionTimer) { Utils.QBLog('[QBChat]', '_establishConnection return'); return; } var _connect = function () { Utils.QBLog('[QBChat]', 'call _connect() in _establishConnection '); if (!self.isConnected && !self._isConnecting && !self._sessionHasExpired) { Utils.QBLog('[QBChat]', 'call connect() again in _establishConnection '); self.connect(params); } else { Utils.QBLog('[QBChat]', 'stop timer in _establishConnection '); clearInterval(self._checkConnectionTimer); self._checkConnectionTimer = undefined; } }; _connect(); self._checkConnectionTimer = setInterval(function () { Utils.QBLog('[QBChat]', 'self._checkConnectionTimer called with config.chatReconnectionTimeInterval = ' + config.chatReconnectionTimeInterval); _connect(); }, config.chatReconnectionTimeInterval * 1000); }, reconnect: function () { Utils.QBLog('[QBChat]', 'Call reconnect '); clearInterval(this._checkConnectionTimer); this._checkConnectionTimer = undefined; this.muc.joinedRooms = {}; this.helpers.setUserCurrentJid(''); if (Utils.getEnv().browser) { this.connection.flush(); this.connection.disconnect(); } else { this.Client.end(); } }, /** * Send message to 1 to 1 or group dialog. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Chat_in_dialog More info.} * @memberof QB.chat * @param {String | Number} jid_or_user_id - Use opponent id or jid for 1 to 1 chat, and room jid for group chat. * @param {Object} message - The message object. * @returns {String} messageId - The current message id (was generated by SDK) * */ send: function (jid_or_user_id, message) { Utils.QBLog('[QBChat]', 'Call send ' + JSON.stringify(message)); var self = this, builder = Utils.getEnv().browser ? $msg : XMPP.Stanza; var paramsCreateMsg = { from: self.helpers.getUserCurrentJid(), to: this.helpers.jidOrUserId(jid_or_user_id), type: message.type ? message.type : 'chat', id: message.id ? message.id : Utils.getBsonObjectId() }; var stanza = chatUtils.createStanza(builder, paramsCreateMsg); if (message.body) { stanza.c('body', { xmlns: chatUtils.MARKERS.CLIENT, }).t(message.body).up(); } if (message.markable) { stanza.c('markable', { xmlns: chatUtils.MARKERS.CHAT }).up(); } if (message.extension) { stanza.c('extraParams', { xmlns: chatUtils.MARKERS.CLIENT }); stanza = chatUtils.filledExtraParams(stanza, message.extension); } if (Utils.getEnv().browser) { if (config.streamManagement.enable) { message.id = paramsCreateMsg.id; message.jid_or_user_id = jid_or_user_id; self.connection.send(stanza, message); } else { self.connection.send(stanza); } } else { if (config.streamManagement.enable) { message.id = paramsCreateMsg.id; message.jid_or_user_id = jid_or_user_id; self.Client.send(stanza, message); } else { self.Client.send(stanza); } } return paramsCreateMsg.id; }, /** * Send system message (system notification) to 1 to 1 or group dialog. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#System_notifications More info.} * @memberof QB.chat * @param {String | Number} jid_or_user_id - Use opponent id or jid for 1 to 1 chat, and room jid for group chat. * @param {Object} message - The message object. * @returns {String} messageId - The current message id (was generated by SDK) * */ sendSystemMessage: function (jid_or_user_id, message) { Utils.QBLog('[QBChat]', 'Call sendSystemMessage ' + JSON.stringify(message)); var self = this, builder = Utils.getEnv().browser ? $msg : XMPP.Stanza, paramsCreateMsg = { type: 'headline', id: message.id ? message.id : Utils.getBsonObjectId(), to: this.helpers.jidOrUserId(jid_or_user_id) }; var stanza = chatUtils.createStanza(builder, paramsCreateMsg); if (message.body) { stanza.c('body', { xmlns: chatUtils.MARKERS.CLIENT, }).t(message.body).up(); } if (Utils.getEnv().browser) { // custom parameters if (message.extension) { stanza.c('extraParams', { xmlns: chatUtils.MARKERS.CLIENT }).c('moduleIdentifier').t('SystemNotifications').up(); stanza = chatUtils.filledExtraParams(stanza, message.extension); } self.connection.send(stanza); } else { if (message.extension) { stanza.c('extraParams', { xmlns: chatUtils.MARKERS.CLIENT }).c('moduleIdentifier').t('SystemNotifications'); stanza = chatUtils.filledExtraParams(stanza, message.extension); } self.Client.send(stanza); } return paramsCreateMsg.id; }, /** * Send is typing status. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Typing_status More info.} * @memberof QB.chat * @param {String | Number} jid_or_user_id - Use opponent id or jid for 1 to 1 chat, and room jid for group chat. * */ sendIsTypingStatus: function (jid_or_user_id) { Utils.QBLog('[QBChat]', 'Call sendIsTypingStatus '); var self = this, stanzaParams = { from: self.helpers.getUserCurrentJid(), to: this.helpers.jidOrUserId(jid_or_user_id), type: this.helpers.typeChat(jid_or_user_id) }, builder = Utils.getEnv().browser ? $msg : XMPP.Stanza; var stanza = chatUtils.createStanza(builder, stanzaParams); stanza.c('composing', { xmlns: chatUtils.MARKERS.STATES }); if (Utils.getEnv().browser) { self.connection.send(stanza); } else { self.Client.send(stanza); } }, /** * Send is stop typing status. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Typing_status More info.} * @memberof QB.chat * @param {String | Number} jid_or_user_id - Use opponent id or jid for 1 to 1 chat, and room jid for group chat. * */ sendIsStopTypingStatus: function (jid_or_user_id) { Utils.QBLog('[QBChat]', 'Call sendIsStopTypingStatus '); var self = this, stanzaParams = { from: self.helpers.getUserCurrentJid(), to: this.helpers.jidOrUserId(jid_or_user_id), type: this.helpers.typeChat(jid_or_user_id) }, builder = Utils.getEnv().browser ? $msg : XMPP.Stanza; var stanza = chatUtils.createStanza(builder, stanzaParams); stanza.c('paused', { xmlns: chatUtils.MARKERS.STATES }); if (Utils.getEnv().browser) { self.connection.send(stanza); } else { self.Client.send(stanza); } }, /** * Send is delivered status. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Delivered_status More info.} * @memberof QB.chats * @param {Object} params - Object of parameters * @param {Number} params.userId - The receiver id * @param {Number} params.messageId - The delivered message id * @param {Number} params.dialogId - The dialog id * */ sendDeliveredStatus: function (params) { Utils.QBLog('[QBChat]', 'Call sendDeliveredStatus '); var self = this, stanzaParams = { type: 'chat', from: self.helpers.getUserCurrentJid(), id: Utils.getBsonObjectId(), to: this.helpers.jidOrUserId(params.userId) }, builder = Utils.getEnv().browser ? $msg : XMPP.Stanza; var stanza = chatUtils.createStanza(builder, stanzaParams); stanza.c('received', { xmlns: chatUtils.MARKERS.MARKERS, id: params.messageId }).up(); stanza.c('extraParams', { xmlns: chatUtils.MARKERS.CLIENT }).c('dialog_id').t(params.dialogId); if (Utils.getEnv().browser) { self.connection.send(stanza); } else { self.Client.send(stanza); } }, /** * Send is read status. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Read_status More info.} * @memberof QB.chat * @param {Object} params - Object of parameters * @param {Number} params.userId - The receiver id * @param {Number} params.messageId - The delivered message id * @param {Number} params.dialogId - The dialog id * */ sendReadStatus: function (params) { Utils.QBLog('[QBChat]', 'Call sendReadStatus ' + JSON.stringify(params)); var self = this, stanzaParams = { type: 'chat', from: self.helpers.getUserCurrentJid(), to: this.helpers.jidOrUserId(params.userId), id: Utils.getBsonObjectId() }, builder = Utils.getEnv().browser ? $msg : XMPP.Stanza; var stanza = chatUtils.createStanza(builder, stanzaParams); stanza.c('displayed', { xmlns: chatUtils.MARKERS.MARKERS, id: params.messageId }).up(); stanza.c('extraParams', { xmlns: chatUtils.MARKERS.CLIENT }).c('dialog_id').t(params.dialogId); if (Utils.getEnv().browser) { self.connection.send(stanza); } else { self.Client.send(stanza); } }, /** * Send query to get last user activity by QB.chat.onLastUserActivityListener(userId, seconds). {@link https://xmpp.org/extensions/xep-0012.html More info.} * @memberof QB.chat * @param {(Number|String)} jid_or_user_id - The user id or jid, that the last activity we want to know * */ getLastUserActivity: function (jid_or_user_id) { Utils.QBLog('[QBChat]', 'Call getLastUserActivity '); var iqParams, builder, iq; iqParams = { 'from': this.helpers.getUserCurrentJid(), 'id': this.helpers.getUniqueId('lastActivity'), 'to': this.helpers.jidOrUserId(jid_or_user_id), 'type': 'get' }; builder = Utils.getEnv().browser ? $iq : XMPP.Stanza; iq = chatUtils.createStanza(builder, iqParams, 'iq'); iq.c('query', { 'xmlns': chatUtils.MARKERS.LAST }); if (Utils.getEnv().browser) { this.connection.sendIQ(iq); } else { this.Client.send(iq); } }, pingchat: function (callback) { Utils.QBLog('[QBChat]', 'ping chat'); var self = this; var id = this.helpers.getUniqueId('ping'); var builder = Utils.getEnv().browser ? $iq : XMPP.Stanza; var to; var _callback; var stanza; //to = config.endpoints.chat; //to = 'http://localhost'; to = config.endpoints.chat? config.endpoints.chat : 'chat.quickblox.com'; _callback = callback; var iqParams = { from: this.helpers.getUserCurrentJid(), id: id, to: to, type: 'get' }; stanza = chatUtils.createStanza(builder, iqParams, 'iq'); stanza.c('ping', {xmlns: "urn:xmpp:ping"}); var noAnswer = function () { _callback('Chat ping No answer'); self._pings[id] = undefined; delete self._pings[id]; }; if (Utils.getEnv().browser) { this.connection.send(stanza); } else { this.Client.send(stanza); } this._pings[id] = { callback: _callback, interval: setTimeout(noAnswer, config.pingTimeout * 1000) }; return id; }, ping: function (jid_or_user_id, callback) { Utils.QBLog('[QBChat]', 'Call ping '); var self = this; var id = this.helpers.getUniqueId('ping'); var builder = Utils.getEnv().browser ? $iq : XMPP.Stanza; var to; var _callback; var stanza; if ((typeof jid_or_user_id === 'string' || typeof jid_or_user_id === 'number') && typeof callback === 'function') { to = this.helpers.jidOrUserId(jid_or_user_id); _callback = callback; } else { if (typeof jid_or_user_id === 'function' && !callback) { to = config.endpoints.chat; _callback = jid_or_user_id; } else { throw new Error('Invalid arguments provided. Either userId/jid (number/string) and callback or only callback should be provided.'); } } var iqParams = { from: this.helpers.getUserCurrentJid(), id: id, to: to, type: 'get' }; stanza = chatUtils.createStanza(builder, iqParams, 'iq'); stanza.c('ping', {xmlns: "urn:xmpp:ping"}); var noAnswer = function () { _callback('No answer'); self._pings[id] = undefined; delete self._pings[id]; }; if (Utils.getEnv().browser) { this.connection.send(stanza); } else { this.Client.send(stanza); } this._pings[id] = { callback: _callback, interval: setTimeout(noAnswer, config.pingTimeout * 1000) }; return id; }, /** * Logout from the Chat. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Logout_from_Chat More info.} * @memberof QB.chat * */ disconnect: function () { Utils.QBLog('[QBChat]', 'Call disconnect '); clearInterval(this._checkConnectionTimer); clearInterval(this._checkExpiredSessionTimer); this._checkConnectionTimer = undefined; this._checkExpiredSessionTimer = undefined; this.muc.joinedRooms = {}; this._isLogout = true; this.helpers.setUserCurrentJid(''); if (Utils.getEnv().browser) { this.connection.flush(); this.connection.disconnect(); if (this._checkConnectionPingTimer !== undefined) { Utils.QBLog('[QBChat]', 'Stop ping'); clearInterval(this._checkConnectionPingTimer); this._checkConnectionPingTimer = undefined; } } else { this.Client.end(); } }, addListener: function (params, callback) { Utils.QBLog('[Deprecated!]', 'Avoid using it, this feature will be removed in future version.'); if (!Utils.getEnv().browser) { throw new Error(unsupportedError); } return this.connection.XAddTrackedHandler(handler, null, params.name || null, params.type || null, params.id || null, params.from || null); function handler() { callback(); // if 'false' - a handler will be performed only once return params.live !== false; } }, deleteListener: function (ref) { Utils.QBLog('[Deprecated!]', 'Avoid using it, this feature will be removed in future version.'); if (!Utils.getEnv().browser) { throw new Error(unsupportedError); } this.connection.deleteHandler(ref); }, /** * Carbons XEP [http://xmpp.org/extensions/xep-0280.html] */ _enableCarbons: function (cb) { var self = this, carbonParams = { type: 'set', from: self.helpers.getUserCurrentJid(), id: chatUtils.getUniqueId('enableCarbons') }, builder = Utils.getEnv().browser ? $iq : XMPP.Stanza; var iq = chatUtils.createStanza(builder, carbonParams, 'iq'); iq.c('enable', { xmlns: chatUtils.MARKERS.CARBONS }); if (Utils.getEnv().browser) { self.connection.sendIQ(iq); } else { self.Client.send(iq); } } }; /* Chat module: Roster * * Integration of Roster Items and Presence Subscriptions * http://xmpp.org/rfcs/rfc3921.html#int * default - Mutual Subscription * ----------------------------------------------------------------------------- */ /** * @namespace QB.chat.roster **/ function RosterProxy(options) { this.service = options.service; this.helpers = options.helpers; this.connection = options.stropheClient; this.Client = options.xmppClient; this.nodeStanzasCallbacks = options.nodeStanzasCallbacks; // this.contacts = {}; } RosterProxy.prototype = { /** * Receive contact list. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Contact_List More info.} * @memberof QB.chat.roster * @param {getRosterCallback} callback - The callback function. * */ get: function (callback) { /** * This callback Return contact list. * @callback getRosterCallback * @param {Object} roster - Object of subscribed users. * */ var self = this, items, userId, contacts = {}, iqParams = { 'type': 'get', 'from': self.helpers.getUserCurrentJid(), 'id': chatUtils.getUniqueId('getRoster') }, builder = Utils.getEnv().browser ? $iq : XMPP.Stanza; var iq = chatUtils.createStanza(builder, iqParams, 'iq'); function _getItems(stanza) { if (Utils.getEnv().browser) { return stanza.getElementsByTagName('item'); } else { return stanza.getChild('query').children; } } function _callbackWrap(stanza) { var items = _getItems(stanza); for (var i = 0, len = items.length; i < len; i++) { var userId = self.helpers.getIdFromNode(chatUtils.getAttr(items[i], 'jid')), ask = chatUtils.getAttr(items[i], 'ask'), subscription = chatUtils.getAttr(items[i], 'subscription'); contacts[userId] = { subscription: subscription, ask: ask || null }; } callback(contacts); } iq.c('query', { xmlns: chatUtils.MARKERS.ROSTER }); if (Utils.getEnv().browser) { self.connection.sendIQ(iq, _callbackWrap); } else { self.nodeStanzasCallbacks[iqParams.id] = _callbackWrap; self.Client.send(iq); } }, /** * Add users to contact list. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Add_users More info.} * @memberof QB.chat.roster * @param {String | Number} jidOrUserId - Use opponent id for 1 to 1 chat, and jid for group chat. * @param {addRosterCallback} callback - The callback function. * */ add: function (jidOrUserId, callback) { /** * Callback for QB.chat.roster.add(). Run without parameters. * @callback addRosterCallback * */ var self = this; var userJid = this.helpers.jidOrUserId(jidOrUserId); var userId = this.helpers.getIdFromNode(userJid).toString(); self.contacts[userId] = { subscription: 'none', ask: 'subscribe' }; self._sendSubscriptionPresence({ jid: userJid, type: 'subscribe' }); if (typeof callback === 'function') { callback(); } }, /** * Confirm subscription with some user. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Confirm_subscription_request More info.} * @memberof QB.chat.roster * @param {String | Number} jidOrUserId - Use opponent id for 1 to 1 chat, and jid for group chat. * @param {confirmRosterCallback} callback - The callback function. * */ confirm: function (jidOrUserId, callback) { /** * Callback for QB.chat.roster.confirm(). Run without parameters. * @callback confirmRosterCallback * */ var self = this; var userJid = this.helpers.jidOrUserId(jidOrUserId); var userId = this.helpers.getIdFromNode(userJid).toString(); self.contacts[userId] = { subscription: 'from', ask: 'subscribe' }; self._sendSubscriptionPresence({ jid: userJid, type: 'subscribed' }); self._sendSubscriptionPresence({ jid: userJid, type: 'subscribe' }); if (typeof callback === 'function') { callback(); } }, /** * Reject subscription with some user. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Reject_subscription_request More info.} * @memberof QB.chat.roster * @param {String | Number} jidOrUserId - Use opponent id for 1 to 1 chat, and jid for group chat. * @param {rejectRosterCallback} callback - The callback function. * */ reject: function (jidOrUserId, callback) { /** * Callback for QB.chat.roster.reject(). Run without parameters. * @callback rejectRosterCallback * */ var self = this; var userJid = this.helpers.jidOrUserId(jidOrUserId); var userId = this.helpers.getIdFromNode(userJid).toString(); self.contacts[userId] = { subscription: 'none', ask: null }; self._sendSubscriptionPresence({ jid: userJid, type: 'unsubscribed' }); if (typeof callback === 'function') { callback(); } }, /** * Remove subscription with some user from your contact list. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Remove_users More info.} * @memberof QB.chat.roster * @param {String | Number} jidOrUserId - Use opponent id for 1 to 1 chat, and jid for group chat. * @param {removeRosterCallback} callback - The callback function. * */ remove: function (jidOrUserId, callback) { /** * Callback for QB.chat.roster.remove(). Run without parameters. * @callback removeRosterCallback * */ var self = this, userJid = this.helpers.jidOrUserId(jidOrUserId), userId = this.helpers.getIdFromNode(userJid); var iqParams = { 'type': 'set', 'from': self.connection ? self.connection.jid : self.Client.jid.user, 'id': chatUtils.getUniqueId('getRoster') }; var builder = Utils.getEnv().browser ? $iq : XMPP.Stanza, iq = chatUtils.createStanza(builder, iqParams, 'iq'); function _callbackWrap() { delete self.contacts[userId]; if (typeof callback === 'function') { callback(); } } iq.c('query', { xmlns: chatUtils.MARKERS.ROSTER }).c('item', { jid: userJid, subscription: 'remove' }); if (Utils.getEnv().browser) { self.connection.sendIQ(iq, _callbackWrap); } else { self.nodeStanzasCallbacks[iqParams.id] = _callbackWrap; self.Client.send(iq); } }, _sendSubscriptionPresence: function (params) { var builder = Utils.getEnv().browser ? $pres : XMPP.Stanza, presParams = { to: params.jid, type: params.type }; var pres = chatUtils.createStanza(builder, presParams, 'presence'); if (Utils.getEnv().browser) { this.connection.send(pres); } else { this.Client.send(pres); } } }; /* Chat module: Group Chat (Dialog) * * Multi-User Chat * http://xmpp.org/extensions/xep-0045.html * ----------------------------------------------------------------------------- */ /** * @namespace QB.chat.muc * */ function MucProxy(options) { this.service = options.service; this.helpers = options.helpers; this.connection = options.stropheClient; this.Client = options.xmppClient; this.nodeStanzasCallbacks = options.nodeStanzasCallbacks; // this.joinedRooms = {}; } MucProxy.prototype = { /** * Join to the group dialog. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Chat_in_group_dialog More info.} * @memberof QB.chat.muc * @param {String} dialogIdOrJid - Use dialog jid or dialog id to join to this dialog. * @param {joinMacCallback} callback - The callback function. * */ join: function (dialogIdOrJid, callback) { /** * Callback for QB.chat.muc.join(). * @param {Object} error - Returns error object or null * @param {Object} responce - Returns responce * @callback joinMacCallback * */ var self = this, id = chatUtils.getUniqueId('join'); var dialogJid = this.helpers.getDialogJid(dialogIdOrJid); var presParams = { id: id, from: self.helpers.getUserCurrentJid(), to: self.helpers.getRoomJid(dialogJid) }, builder = Utils.getEnv().browser ? $pres : XMPP.Stanza; var pres = chatUtils.createStanza(builder, presParams, 'presence'); pres.c('x', { xmlns: chatUtils.MARKERS.MUC }).c('history', {maxstanzas: 0}); this.joinedRooms[dialogJid] = true; function handleJoinAnswer(stanza) { var id = chatUtils.getAttr(stanza, 'id'); var from = chatUtils.getAttr(stanza, 'from'); var dialogId = self.helpers.getDialogIdFromNode(from); var x = chatUtils.getElement(stanza, 'x'); var xXMLNS = chatUtils.getAttr(x, 'xmlns'); var status = chatUtils.getElement(x, 'status'); var statusCode = chatUtils.getAttr(status, 'code'); if (callback.length == 1) { Utils.safeCallbackCall(callback, stanza); return true; } if (status && statusCode == '110') { Utils.safeCallbackCall(callback, null, { dialogId: dialogId }); } else { var type = chatUtils.getAttr(stanza, 'type'); if (type && type === 'error' && xXMLNS == 'http://jabber.org/protocol/muc' && id.endsWith(':join')) { var errorEl = chatUtils.getElement(stanza, 'error'); var code = chatUtils.getAttr(errorEl, 'code'); var errorMessage = chatUtils.getElementText(errorEl, 'text'); Utils.safeCallbackCall(callback, { code: code || 500, message: errorMessage || 'Unknown issue' }, { dialogId: dialogId }); } } } if (Utils.getEnv().browser) { if (typeof callback === 'function') { self.connection.XAddTrackedHandler(handleJoinAnswer, null, 'presence', null, id); } self.connection.send(pres); } else { if (typeof callback === 'function') { self.nodeStanzasCallbacks[id] = handleJoinAnswer; } self.Client.send(pres); } }, /** * Leave group chat dialog. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Chat_in_group_dialog More info.} * @memberof QB.chat.muc * @param {String} dialogJid - Use dialog jid to join to this dialog. * @param {leaveMacCallback} callback - The callback function. * */ leave: function (jid, callback) { /** * Callback for QB.chat.muc.leave(). * run without parameters; * @callback leaveMacCallback * */ var self = this, presParams = { type: 'unavailable', from: self.helpers.getUserCurrentJid(), to: self.helpers.getRoomJid(jid) }, builder = Utils.getEnv().browser ? $pres : XMPP.Stanza; var pres = chatUtils.createStanza(builder, presParams, 'presence'); delete this.joinedRooms[jid]; function handleLeaveAnswer(stanza) { var id = chatUtils.getAttr(stanza, 'id'); var from = chatUtils.getAttr(stanza, 'from'); var dialogId = self.helpers.getDialogIdFromNode(from); var x = chatUtils.getElement(stanza, 'x'); var xXMLNS = chatUtils.getAttr(x, 'xmlns'); var status = chatUtils.getElement(x, 'status'); var statusCode = chatUtils.getAttr(status, 'code'); if (status && statusCode == '110') { Utils.safeCallbackCall(callback, null, { dialogId: dialogId }); } else { var type = chatUtils.getAttr(stanza, 'type'); if (type && type === 'error' && xXMLNS == 'http://jabber.org/protocol/muc' && id.endsWith(':join')) { var errorEl = chatUtils.getElement(stanza, 'error'); var code = chatUtils.getAttr(errorEl, 'code'); var errorMessage = chatUtils.getElementText(errorEl, 'text'); Utils.safeCallbackCall(callback, { code: code || 500, message: errorMessage || 'Unknown issue' }, { dialogId: dialogId }); } } } if (Utils.getEnv().browser) { var roomJid = self.helpers.getRoomJid(jid); if (typeof callback === 'function') { self.connection.XAddTrackedHandler(handleLeaveAnswer, null, 'presence', presParams.type, null, roomJid); } self.connection.send(pres); } else { /** The answer don't contain id */ if (typeof callback === 'function') { self.nodeStanzasCallbacks['muc:leave'] = handleLeaveAnswer; } self.Client.send(pres); } }, /** * Leave group chat dialog. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Chat_in_group_dialog More info.} * @memberof QB.chat.muc * @param {String} dialogJid - Use dialog jid to join to this dialog. * @param {listOnlineUsersMacCallback} callback - The callback function. * */ listOnlineUsers: function (dialogJID, callback) { /** * Callback for QB.chat.muc.leave(). * @param {Object} Users - list of online users * @callback listOnlineUsersMacCallback * */ var self = this, onlineUsers = []; var iqParams = { type: 'get', to: dialogJID, from: self.helpers.getUserCurrentJid(), id: chatUtils.getUniqueId('muc_disco_items'), }, builder = Utils.getEnv().browser ? $iq : XMPP.Stanza; var iq = chatUtils.createStanza(builder, iqParams, 'iq'); iq.c('query', { xmlns: 'http://jabber.org/protocol/disco#items' }); function _getUsers(stanza) { var stanzaId = stanza.attrs.id; if (self.nodeStanzasCallbacks[stanzaId]) { var users = [], items = stanza.getChild('query').getChildElements('item'), userId; for (var i = 0, len = items.length; i < len; i++) { userId = self.helpers.getUserIdFromRoomJid(items[i].attrs.jid); users.push(parseInt(userId)); } callback(users); } } if (Utils.getEnv().browser) { self.connection.sendIQ(iq, function (stanza) { var items = stanza.getElementsByTagName('item'), userId; for (var i = 0, len = items.length; i < len; i++) { userId = self.helpers.getUserIdFromRoomJid(items[i].getAttribute('jid')); onlineUsers.push(parseInt(userId)); } callback(onlineUsers); }); } else { self.Client.send(iq); self.nodeStanzasCallbacks[iqParams.id] = _getUsers; } } }; /* Chat module: Privacy list * * Privacy list * http://xmpp.org/extensions/xep-0016.html * by default 'mutualBlock' is work in one side ----------------------------------------------------------------------------- */ function PrivacyListProxy(options) { this.service = options.service; this.helpers = options.helpers; this.connection = options.stropheClient; this.Client = options.xmppClient; this.nodeStanzasCallbacks = options.nodeStanzasCallbacks; } /** * @namespace QB.chat.privacylist **/ PrivacyListProxy.prototype = { /** * Create a privacy list. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Create_a_privacy_list_or_edit_existing_list More info.} * @memberof QB.chat.privacylist * @param {Object} list - privacy list object. * @param {createPrivacylistCallback} callback - The callback function. * */ create: function (list, callback) { /** * Callback for QB.chat.privacylist.create(). * @param {Object} error - The error object * @callback createPrivacylistCallback * */ var self = this, userId, userJid, userMuc, userAction, mutualBlock, listPrivacy = {}, listUserId = []; /** Filled listPrivacys */ for (var i = list.items.length - 1; i >= 0; i--) { var user = list.items[i]; listPrivacy[user.user_id] = { action: user.action, mutualBlock: user.mutualBlock === true ? true : false }; } /** Filled listUserId */ listUserId = Object.keys(listPrivacy); var iqParams = { type: 'set', from: self.helpers.getUserCurrentJid(), id: chatUtils.getUniqueId('edit') }, builder = Utils.getEnv().browser ? $iq : XMPP.Stanza; var iq = chatUtils.createStanza(builder, iqParams, 'iq'); iq.c('query', { xmlns: chatUtils.MARKERS.PRIVACY }).c('list', { name: list.name }); function createPrivacyItem(iq, params) { if (Utils.getEnv().browser) { iq.c('item', { type: 'jid', value: params.jidOrMuc, action: params.userAction, order: params.order }).c('message', {}) .up().c('presence-in', {}) .up().c('presence-out', {}) .up().c('iq', {}) .up().up(); } else { var list = iq.getChild('query').getChild('list'); list.c('item', { type: 'jid', value: params.jidOrMuc, action: params.userAction, order: params.order }).c('message', {}) .up().c('presence-in', {}) .up().c('presence-out', {}) .up().c('iq', {}) .up().up(); } return iq; } function createPrivacyItemMutal(iq, params) { if (Utils.getEnv().browser) { iq.c('item', { type: 'jid', value: params.jidOrMuc, action: params.userAction, order: params.order }).up(); } else { var list = iq.getChild('query').getChild('list'); list.c('item', { type: 'jid', value: params.jidOrMuc, action: params.userAction, order: params.order }).up(); } return iq; } for (var index = 0, j = 0, len = listUserId.length; index < len; index++, j = j + 2) { userId = listUserId[index]; mutualBlock = listPrivacy[userId].mutualBlock; userAction = listPrivacy[userId].action; userJid = self.helpers.jidOrUserId(parseInt(userId, 10)); userMuc = self.helpers.getUserNickWithMucDomain(userId); if (mutualBlock && userAction === 'deny') { iq = createPrivacyItemMutal(iq, { order: j + 1, jidOrMuc: userJid, userAction: userAction }); iq = createPrivacyItemMutal(iq, { order: j + 2, jidOrMuc: userMuc, userAction: userAction }).up().up(); } else { iq = createPrivacyItem(iq, { order: j + 1, jidOrMuc: userJid, userAction: userAction }); iq = createPrivacyItem(iq, { order: j + 2, jidOrMuc: userMuc, userAction: userAction }); } } if (Utils.getEnv().browser) { self.connection.sendIQ(iq, function (stanzaResult) { callback(null); }, function (stanzaError) { if (stanzaError) { var errorObject = chatUtils.getErrorFromXMLNode(stanzaError); callback(errorObject); } else { callback(Utils.getError(408)); } }); } else { self.Client.send(iq); self.nodeStanzasCallbacks[iqParams.id] = function (stanza) { if (!stanza.getChildElements('error').length) { callback(null); } else { callback(Utils.getError(408)); } }; } }, /** * Get the privacy list. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Retrieve_a_privacy_list More info.} * @memberof QB.chat.privacylist * @param {String} name - The name of the list. * @param {getListPrivacylistCallback} callback - The callback function. * */ getList: function (name, callback) { /** * Callback for QB.chat.privacylist.getList(). * @param {Object} error - The error object * @param {Object} response - The privacy list object * @callback getListPrivacylistCallback * */ var self = this, items, userJid, userId, usersList = [], list = {}; var iqParams = { type: 'get', from: self.helpers.getUserCurrentJid(), id: chatUtils.getUniqueId('getlist') }, builder = Utils.getEnv().browser ? $iq : XMPP.Stanza; var iq = chatUtils.createStanza(builder, iqParams, 'iq'); iq.c('query', { xmlns: chatUtils.MARKERS.PRIVACY }).c('list', { name: name }); if (Utils.getEnv().browser) { self.connection.sendIQ(iq, function (stanzaResult) { items = stanzaResult.getElementsByTagName('item'); for (var i = 0, len = items.length; i < len; i = i + 2) { userJid = items[i].getAttribute('value'); userId = self.helpers.getIdFromNode(userJid); usersList.push({ user_id: userId, action: items[i].getAttribute('action') }); } list = { name: name, items: usersList }; callback(null, list); }, function (stanzaError) { if (stanzaError) { var errorObject = chatUtils.getErrorFromXMLNode(stanzaError); callback(errorObject, null); } else { callback(Utils.getError(408), null); } }); } else { self.nodeStanzasCallbacks[iqParams.id] = function (stanza) { var stanzaQuery = stanza.getChild('query'), list = stanzaQuery ? stanzaQuery.getChild('list') : null, items = list ? list.getChildElements('item') : null, userJid, userId, usersList = []; for (var i = 0, len = items.length; i < len; i = i + 2) { userJid = items[i].attrs.value; userId = self.helpers.getIdFromNode(userJid); usersList.push({ user_id: userId, action: items[i].attrs.action }); } list = { name: list.attrs.name, items: usersList }; callback(null, list); delete self.nodeStanzasCallbacks[iqParams.id]; }; self.Client.send(iq); } }, /** * Update the privacy list. * @memberof QB.chat.privacylist * @param {String} name - The name of the list. * @param {updatePrivacylistCallback} callback - The callback function. * */ update: function (listWithUpdates, callback) { /** * Callback for QB.chat.privacylist.update(). * @param {Object} error - The error object * @param {Object} response - The privacy list object * @callback updatePrivacylistCallback * */ var self = this; self.getList(listWithUpdates.name, function (error, existentList) { if (error) { callback(error, null); } else { var updatedList = {}; updatedList.items = Utils.MergeArrayOfObjects(existentList.items, listWithUpdates.items); updatedList.name = listWithUpdates.name; self.create(updatedList, function (err, result) { if (error) { callback(err, null); } else { callback(null, result); } }); } }); }, /** * Get names of privacy lists. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Retrieve_privacy_lists_names More info.} * Run without parameters * @memberof QB.chat.privacylist * @param {getNamesPrivacylistCallback} callback - The callback function. * */ getNames: function (callback) { /** * Callback for QB.chat.privacylist.getNames(). * @param {Object} error - The error object * @param {Object} response - The privacy list object (var names = response.names;) * @callback getNamesPrivacylistCallback * */ var self = this, iq, stanzaParams = { 'type': 'get', 'from': self.helpers.getUserCurrentJid(), 'id': chatUtils.getUniqueId('getNames') }; if (Utils.getEnv().browser) { iq = $iq(stanzaParams).c('query', { xmlns: Strophe.NS.PRIVACY_LIST }); self.connection.sendIQ(iq, function (stanzaResult) { var allNames = [], namesList = {}, defaultList = stanzaResult.getElementsByTagName('default'), activeList = stanzaResult.getElementsByTagName('active'), allLists = stanzaResult.getElementsByTagName('list'); var defaultName = defaultList && defaultList.length > 0 ? defaultList[0].getAttribute('name') : null; var activeName = activeList && activeList.length > 0 ? activeList[0].getAttribute('name') : null; for (var i = 0, len = allLists.length; i < len; i++) { allNames.push(allLists[i].getAttribute('name')); } namesList = { 'default': defaultName, 'active': activeName, 'names': allNames }; callback(null, namesList); }, function (stanzaError) { if (stanzaError) { var errorObject = chatUtils.getErrorFromXMLNode(stanzaError); callback(errorObject, null); } else { callback(Utils.getError(408), null); } }); } else { iq = new XMPP.Stanza('iq', stanzaParams); iq.c('query', { xmlns: chatUtils.MARKERS.PRIVACY }); self.nodeStanzasCallbacks[iq.attrs.id] = function (stanza) { if (stanza.attrs.type !== 'error') { var allNames = [], namesList = {}, query = stanza.getChild('query'), defaultList = query.getChild('default'), activeList = query.getChild('active'), allLists = query.getChildElements('list'); var defaultName = defaultList ? defaultList.attrs.name : null, activeName = activeList ? activeList.attrs.name : null; for (var i = 0, len = allLists.length; i < len; i++) { allNames.push(allLists[i].attrs.name); } namesList = { 'default': defaultName, 'active': activeName, 'names': allNames }; callback(null, namesList); } else { callback(Utils.getError(408)); } }; self.Client.send(iq); } }, /** * Delete privacy list. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Delete_existing_privacy_list More info.} * @param {String} name - The name of privacy list. * @memberof QB.chat.privacylist * @param {deletePrivacylistCallback} callback - The callback function. * */ delete: function (name, callback) { /** * Callback for QB.chat.privacylist.delete(). * @param {Object} error - The error object * @callback deletePrivacylistCallback * */ var iq, stanzaParams = { 'from': this.connection ? this.connection.jid : this.Client.jid.user, 'type': 'set', 'id': chatUtils.getUniqueId('remove') }; if (Utils.getEnv().browser) { iq = $iq(stanzaParams).c('query', { xmlns: Strophe.NS.PRIVACY_LIST }).c('list', { name: name ? name : '' }); this.connection.sendIQ(iq, function (stanzaResult) { callback(null); }, function (stanzaError) { if (stanzaError) { var errorObject = chatUtils.getErrorFromXMLNode(stanzaError); callback(errorObject); } else { callback(Utils.getError(408)); } }); } else { iq = new XMPP.Stanza('iq', stanzaParams); iq.c('query', { xmlns: chatUtils.MARKERS.PRIVACY }).c('list', { name: name ? name : '' }); this.nodeStanzasCallbacks[stanzaParams.id] = function (stanza) { if (!stanza.getChildElements('error').length) { callback(null); } else { callback(Utils.getError(408)); } }; this.Client.send(iq); } }, /** * Set as default privacy list. {@link https://quickblox.com/developers/Web_XMPP_Chat_Sample#Activate_a_privacy_list More info.} * @param {String} name - The name of privacy list. * @memberof QB.chat.privacylist * @param {setAsDefaultPrivacylistCallback} callback - The callback function. * */ setAsDefault: function (name, callback) { /** * Callback for QB.chat.privacylist.setAsDefault(). * @param {Object} error - The error object * @callback setAsDefaultPrivacylistCallback * */ var self = this, iq, stanzaParams = { 'from': this.connection ? this.connection.jid : this.Client.jid.user, 'type': 'set', 'id': chatUtils.getUniqueId('default') }; if (Utils.getEnv().browser) { iq = $iq(stanzaParams).c('query', { xmlns: Strophe.NS.PRIVACY_LIST }).c('default', name && name.length > 0 ? {name: name} : {}); this.connection.sendIQ(iq, function (stanzaResult) { setAsActive(self); //Activate list after setting it as default. }, function (stanzaError) { if (stanzaError) { var errorObject = chatUtils.getErrorFromXMLNode(stanzaError); callback(errorObject); } else { callback(Utils.getError(408)); } }); } else { iq = new XMPP.Stanza('iq', stanzaParams); iq.c('query', { xmlns: chatUtils.MARKERS.PRIVACY }).c('default', name && name.length > 0 ? {name: name} : {}); this.nodeStanzasCallbacks[stanzaParams.id] = function (stanza) { if (!stanza.getChildElements('error').length) { setAsActive(self); //Activate list after setting it as default. } else { callback(Utils.getError(408)); } }; this.Client.send(iq); } /** * Set as active privacy list after setting as default. * @param {PrivacyListProxy Object} self - The name of privacy list. * */ function setAsActive(self) { var setAsActiveIq, setAsActiveStanzaParams = { 'from': self.connection ? self.connection.jid : self.Client.jid.user, 'type': 'set', 'id': chatUtils.getUniqueId('active1') }; if (Utils.getEnv().browser) { setAsActiveIq = $iq(setAsActiveStanzaParams).c('query', { xmlns: Strophe.NS.PRIVACY_LIST }).c('active', name && name.length > 0 ? {name: name} : {}); self.connection.sendIQ(setAsActiveIq, function (setAsActiveStanzaResult) { callback(null); }, function (setAsActiveStanzaError) { if (setAsActiveStanzaError) { var setAsActiveErrorObject = chatUtils.getErrorFromXMLNode(setAsActiveStanzaError); callback(setAsActiveErrorObject); } else { callback(Utils.getError(408)); } }); } else { setAsActiveIq = new XMPP.Stanza('iq', setAsActiveStanzaParams); setAsActiveIq.c('query', { xmlns: chatUtils.MARKERS.PRIVACY }).c('active', name && name.length > 0 ? {name: name} : {}); self.nodeStanzasCallbacks[setAsActiveStanzaParams.id] = function (setAsActistanza) { if (!setAsActistanza.getChildElements('error').length) { callback(null); } else { callback(Utils.getError(408)); } }; self.Client.send(setAsActiveIq); } } } }; /* Helpers ----------------------------------------------------------------------------- */ function Helpers() { this._userCurrentJid = ''; } /** * @namespace QB.chat.helpers * */ Helpers.prototype = { /** * Get unique id. * @memberof QB.chat.helpers * @param {String | Number} suffix - not required parameter. * @returns {String} - UniqueId. * */ getUniqueId: chatUtils.getUniqueId, /** * Get unique id. * @memberof QB.chat.helpers * @param {String | Number} jid_or_user_id - Jid or user id. * @returns {String} - jid. * */ jidOrUserId: function (jid_or_user_id) { var jid; if (typeof jid_or_user_id === 'string') { jid = jid_or_user_id; } else if (typeof jid_or_user_id === 'number') { jid = jid_or_user_id + '-' + config.creds.appId + '@' + config.endpoints.chat; } else { throw new Error('The method "jidOrUserId" may take jid or id'); } return jid; }, /** * Get the chat type. * @memberof QB.chat.helpers * @param {String | Number} jid_or_user_id - Jid or user id. * @returns {String} - jid. * */ typeChat: function (jid_or_user_id) { var chatType; if (typeof jid_or_user_id === 'string') { chatType = jid_or_user_id.indexOf("muc") > -1 ? 'groupchat' : 'chat'; } else if (typeof jid_or_user_id === 'number') { chatType = 'chat'; } else { throw new Error(unsupportedError); } return chatType; }, /** * Get the recipint id. * @memberof QB.chat.helpers * @param {Array} occupantsIds - Array of user ids. * @param {Number} UserId - Jid or user id. * @returns {Number} recipient - recipient id. * */ getRecipientId: function (occupantsIds, UserId) { var recipient = null; occupantsIds.forEach(function (item) { if (item != UserId) { recipient = item; } }); return recipient; }, /** * Get the User jid id. * @memberof QB.chat.helpers * @param {Number} UserId - The user id. * @param {Number} appId - The application id. * @returns {String} jid - The user jid. * */ getUserJid: function (userId, appId) { if (!appId) { return userId + '-' + config.creds.appId + '@' + config.endpoints.chat; } return userId + '-' + appId + '@' + config.endpoints.chat; }, /** * Get the User nick with the muc domain. * @memberof QB.chat.helpers * @param {Number} UserId - The user id. * @returns {String} mucDomainWithNick - The mac domain with he nick. * */ getUserNickWithMucDomain: function (userId) { return config.endpoints.muc + '/' + userId; }, /** * Get the User id from jid. * @memberof QB.chat.helpers * @param {String} jid - The user jid. * @returns {Number} id - The user id. * */ getIdFromNode: function (jid) { return (jid.indexOf('@') < 0) ? null : parseInt(jid.split('@')[0].split('-')[0]); }, /** * Get the dialog id from jid. * @memberof QB.chat.helpers * @param {String} jid - The dialog jid. * @returns {String} dialogId - The dialog id. * */ getDialogIdFromNode: function (jid) { if (jid.indexOf('@') < 0) return null; return jid.split('@')[0].split('_')[1]; }, /** * Get the room jid from dialog id. * @memberof QB.chat.helpers * @param {String} dialogId - The dialog id. * @returns {String} jid - The dialog jid. * */ getRoomJidFromDialogId: function (dialogId) { return config.creds.appId + '_' + dialogId + '@' + config.endpoints.muc; }, /** * Get the full room jid from room bare jid & user jid. * @memberof QB.chat.helpers * @param {String} jid - dialog's bare jid. * @param {String} userJid - user's jid. * @returns {String} jid - dialog's full jid. * */ getRoomJid: function (jid) { return jid + '/' + this.getIdFromNode(this._userCurrentJid); }, /** * Get user id from dialog's full jid. * @memberof QB.chat.helpers * @param {String} jid - dialog's full jid. * @returns {String} user_id - User Id. * */ getIdFromResource: function (jid) { var s = jid.split('/'); if (s.length < 2) return null; s.splice(0, 1); return parseInt(s.join('/')); }, /** * Get bare dialog's jid from dialog's full jid. * @memberof QB.chat.helpers * @param {String} jid - dialog's full jid. * @returns {String} room_jid - dialog's bare jid. * */ getRoomJidFromRoomFullJid: function (jid) { var s = jid.split('/'); if (s.length < 2) return null; return s[0]; }, /** * Generate BSON ObjectId. * @memberof QB.chat.helpers * @returns {String} BsonObjectId - The bson object id. **/ getBsonObjectId: function () { return Utils.getBsonObjectId(); }, /** * Get the user id from the room jid. * @memberof QB.chat.helpers * @param {String} jid - resourse jid. * @returns {String} userId - The user id. * */ getUserIdFromRoomJid: function (jid) { var arrayElements = jid.toString().split('/'); if (arrayElements.length === 0) { return null; } return arrayElements[arrayElements.length - 1]; }, userCurrentJid: function (client) { try { if (client instanceof Strophe.Connection) { return client.jid; } else { return client.jid.user + '@' + client.jid._domain + '/' + client.jid._resource; } } catch (e) { // ReferenceError: Strophe is not defined return client.jid.user + '@' + client.jid._domain + '/' + client.jid._resource; } }, getUserCurrentJid: function () { return this._userCurrentJid; }, setUserCurrentJid: function (jid) { this._userCurrentJid = jid; }, getDialogJid: function (identifier) { return identifier.indexOf('@') > 0 ? identifier : this.getRoomJidFromDialogId(identifier); } }; /** * @namespace QB.chat * */ module.exports = ChatProxy; },{"../../plugins/streamManagement":251,"../../qbConfig":252,"../../qbStrophe":255,"../../qbUtils":256,"./qbChatHelpers":235,"nativescript-xmpp-client":undefined,"node-xmpp-client":111}],235:[function(require,module,exports){ 'use strict'; var utils = require('../../qbUtils'); var config = require('../../qbConfig'); var ERR_UNKNOWN_INTERFACE = 'Unknown interface. SDK support browser / node env.'; var MARKERS = { CLIENT: 'jabber:client', CHAT: 'urn:xmpp:chat-markers:0', STATES: 'http://jabber.org/protocol/chatstates', MARKERS: 'urn:xmpp:chat-markers:0', CARBONS: 'urn:xmpp:carbons:2', ROSTER: 'jabber:iq:roster', MUC: 'http://jabber.org/protocol/muc', PRIVACY: 'jabber:iq:privacy', LAST: 'jabber:iq:last' }; function ChatNotConnectedError(message, fileName, lineNumber) { var instance = new Error(message, fileName, lineNumber); instance.name = 'ChatNotConnectedError'; Object.setPrototypeOf(instance, Object.getPrototypeOf(this)); if (Error.captureStackTrace) { Error.captureStackTrace(instance, ChatNotConnectedError); } return instance; } ChatNotConnectedError.prototype = Object.create(Error.prototype, { constructor: { value: Error, enumerable: false, writable: true, configurable: true } }); if (Object.setPrototypeOf) { Object.setPrototypeOf(ChatNotConnectedError, Error); } else { ChatNotConnectedError.__proto__ = Error; // jshint ignore:line } var qbChatHelpers = { MARKERS: MARKERS, ChatNotConnectedError: ChatNotConnectedError, /** * @param {params} this object may contains Jid or Id property * @return {string} jid of user */ buildUserJid: function(params) { var jid; if ('userId' in params) { jid = params.userId + '-' + config.creds.appId + '@' + config.endpoints.chat; if ('resource' in params) { jid = jid + '/' + params.resource; } } else if ('jid' in params) { jid = params.jid; } return jid; }, createStanza: function(builder, params, type) { var stanza; if(utils.getEnv().browser) { stanza = builder(params); } else { stanza = new builder(type ? type : 'message', params); } return stanza; }, getAttr: function(el, attrName) { var attr; if(!el) { return null; } if(typeof el.getAttribute === 'function') { attr = el.getAttribute(attrName); } else if(el.attrs) { attr = el.attrs[attrName]; } return attr ? attr : null; }, getElement: function(stanza, elName) { var el; if(typeof stanza.querySelector === 'function') { el = stanza.querySelector(elName); } else if(typeof stanza.getChild === 'function'){ el = stanza.getChild(elName) || stanza.children.find(child => typeof child.getChild === 'function' && this.getElement(child, elName)); } else { throw ERR_UNKNOWN_INTERFACE; } return el ? el : null; }, getAllElements: function(stanza, elName) { var el; if(typeof stanza.querySelectorAll === 'function') { el = stanza.querySelectorAll(elName); } else if(typeof stanza.getChild === 'function'){ el = stanza.getChild(elName); } else { throw ERR_UNKNOWN_INTERFACE; } return el ? el : null; }, getElementText: function(stanza, elName) { var el, txt; if(typeof stanza.querySelector === 'function') { el = stanza.querySelector(elName); txt = el ? el.textContent : null; } else if(typeof stanza.getChildText === 'function') { txt = stanza.getChildText(elName) || stanza.children.find(child => typeof child.getChildText === 'function' && this.getElementText(child, elName)); } else { throw ERR_UNKNOWN_INTERFACE; } return txt ? txt : null; }, _JStoXML: function(title, obj, msg) { var self = this; msg.c(title); Object.keys(obj).forEach(function(field) { if (typeof obj[field] === 'object') { self._JStoXML(field, obj[field], msg); } else { msg.c(field).t(obj[field]).up(); } }); msg.up(); }, _XMLtoJS: function(extension, title, obj) { var self = this; extension[title] = {}; for (var i = 0, len = obj.childNodes.length; i < len; i++) { if (obj.childNodes[i].childNodes.length > 1) { extension[title] = self._XMLtoJS(extension[title], obj.childNodes[i].tagName, obj.childNodes[i]); } else { extension[title][obj.childNodes[i].tagName] = obj.childNodes[i].textContent; } } return extension; }, filledExtraParams: function(stanza, extension) { var helper = this; Object.keys(extension).forEach(function(field) { if (field === 'attachments') { extension[field].forEach(function(attach) { if (utils.getEnv().browser) { stanza.c('attachment', attach).up(); } else { stanza.getChild('extraParams') .c('attachment', attach).up(); } }); } else if (typeof extension[field] === 'object') { helper._JStoXML(field, extension[field], stanza); } else { if (utils.getEnv().browser) { stanza.c(field).t(extension[field]).up(); } else { stanza.getChild('extraParams') .c(field).t(extension[field]).up(); } } }); stanza.up(); return stanza; }, parseExtraParams: function(extraParams) { var self = this; if (!extraParams) { return null; } var extension = {}; var dialogId, attach, attributes; var attachments = []; if (utils.getEnv().browser) { for (var i = 0, len = extraParams.childNodes.length; i < len; i++) { // parse attachments if (extraParams.childNodes[i].tagName === 'attachment') { attach = {}; attributes = extraParams.childNodes[i].attributes; for (var j = 0, len2 = attributes.length; j < len2; j++) { if (attributes[j].name === 'size') { attach[attributes[j].name] = parseInt(attributes[j].value); } else { attach[attributes[j].name] = attributes[j].value; } } attachments.push(attach); // parse 'dialog_id' } else if (extraParams.childNodes[i].tagName === 'dialog_id') { dialogId = extraParams.childNodes[i].textContent; extension.dialog_id = dialogId; // parse other user's custom parameters } else { if (extraParams.childNodes[i].childNodes.length > 1) { // Firefox issue with 4K XML node limit: // http://www.coderholic.com/firefox-4k-xml-node-limit/ var nodeTextContentSize = extraParams.childNodes[i].textContent.length; if (nodeTextContentSize > 4096) { var wholeNodeContent = ""; for (var k=0; k 0) { extension.attachments = attachments; } if (extension.moduleIdentifier) { delete extension.moduleIdentifier; } return { extension: extension, dialogId: dialogId }; }, getUniqueId: function(suffix) { var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : r & 0x3 | 0x8; return v.toString(16); }); if (typeof(suffix) == 'string' || typeof(suffix) == 'number') { return uuid + ':' + suffix; } else { return uuid + ''; } }, getErrorFromXMLNode: function(stanzaError) { var errorElement = this.getElement(stanzaError, 'error'); var errorCode = parseInt(this.getAttr(errorElement, 'code')); var errorText = this.getElementText(errorElement, 'text'); return utils.getError(errorCode, errorText); }, getLocalTime: function() { return (new Date()).toTimeString().split(' ')[0]; } }; module.exports = qbChatHelpers; },{"../../qbConfig":252,"../../qbUtils":256}],236:[function(require,module,exports){ 'use strict'; var config = require('../../qbConfig'), Utils = require('../../qbUtils'); var DIALOGS_API_URL = config.urls.chat + '/Dialog'; function DialogProxy(service) { this.service = service; } /** * @namespace QB.chat.dialog **/ DialogProxy.prototype = { /** * Retrieve list of dialogs({@link https://docs.quickblox.com/docs/js-chat-dialogs#retrieve-list-of-dialogs read more}). * @memberof QB.chat.dialog * @param {Object} params - Some filters to get only chat dialogs you need. * @param {listDialogCallback} callback - The callback function. * */ list: function(params, callback) { /** * Callback for QB.chat.dialog.list(). * @param {Object} error - The error object. * @param {Object} resDialogs - the dialog list. * @callback listDialogCallback * */ if (typeof params === 'function' && typeof callback === 'undefined') { callback = params; params = {}; } this.service.ajax({ url: Utils.getUrl(DIALOGS_API_URL), data: params }, callback); }, /** * Create new dialog({@link https://docs.quickblox.com/docs/js-chat-dialogs#create-dialog read more}). * @memberof QB.chat.dialog * @param {Object} params - Object of parameters. * @param {createDialogCallback} callback - The callback function. * */ create: function(params, callback) { /** * Callback for QB.chat.dialog.create(). * @param {Object} error - The error object. * @param {Object} createdDialog - the dialog object. * @callback createDialogCallback * */ if (params && params.occupants_ids && Utils.isArray(params.occupants_ids)) { params.occupants_ids = params.occupants_ids.join(', '); } this.service.ajax({ url: Utils.getUrl(DIALOGS_API_URL), type: 'POST', data: params }, callback); }, /** * Update group dialog({@link https://docs.quickblox.com/docs/js-chat-dialogs#update-dialog read more}). * @memberof QB.chat.dialog * @param {String} id - The dialog ID. * @param {Object} params - Object of parameters. * @param {updateDialogCallback} callback - The callback function. * */ update: function(id, params, callback) { /** * Callback for QB.chat.dialog.update() * @param {Object} error - The error object. * @param {Object} res - the dialog object. * @callback updateDialogCallback * */ this.service.ajax({ 'url': Utils.getUrl(DIALOGS_API_URL, id), 'type': 'PUT', 'contentType': 'application/json; charset=utf-8', 'isNeedStringify': true, 'data': params }, callback); }, /** * Delete a dialog or dialogs({@link https://docs.quickblox.com/docs/js-chat-dialogs#delete-dialog read more}). * @memberof QB.chat.dialog * @param {Array} id - The dialog IDs array. * @param {Object | function} params_or_callback - Object of parameters or callback function. * @param {deleteDialogCallback} callback - The callback function. * */ delete: function(id, params_or_callback, callback) { /** * Callback for QB.chat.dialog.delete(). * @param {Object} error - The error object. * @callback deleteDialogCallback * */ var ajaxParams = { url: Utils.getUrl(DIALOGS_API_URL, id), type: 'DELETE', dataType: 'text' }; if (arguments.length === 2) { this.service.ajax(ajaxParams, params_or_callback); } else if (arguments.length === 3) { ajaxParams.data = params_or_callback; this.service.ajax(ajaxParams, callback); } } }; module.exports = DialogProxy; },{"../../qbConfig":252,"../../qbUtils":256}],237:[function(require,module,exports){ 'use strict'; var config = require('../../qbConfig'), Utils = require('../../qbUtils'); var MESSAGES_API_URL = config.urls.chat + '/Message'; function MessageProxy(service) { this.service = service; } /** * @namespace QB.chat.message **/ MessageProxy.prototype = { /** * Get a chat history({@link https://docs.quickblox.com/docs/js-chat-messaging#retrieve-chat-history read more}). * @memberof QB.chat.message * @param {Object} params - Object of parameters. * @param {listMessageCallback} callback - The callback function. * */ list: function(params, callback) { /** * Callback for QB.chat.message.list(). * @param {Object} error - The error object. * @param {Object} messages - The messages object. * @callback listMessageCallback * */ this.service.ajax({ url: Utils.getUrl(MESSAGES_API_URL), data: params }, callback); }, /** * Create message. * @memberof QB.chat.message * @param {Object} params - Object of parameters. * @param {createMessageCallback} callback - The callback function. * */ create: function(params, callback) { /** * Callback for QB.chat.message.create(). * @param {Object} error - The error object. * @param {Object} messages - The message object. * @callback createMessageCallback * */ this.service.ajax({ url: Utils.getUrl(MESSAGES_API_URL), type: 'POST', data: params }, callback); }, /** * Update message({@link https://docs.quickblox.com/docs/js-chat-messaging#update-message read more}). * @memberof QB.chat.message * @param {String} id - The message id. * @param {Object} params - Object of parameters. * @param {Number} [params.read] - Mark message as read (read=1). * @param {Number} [params.delivered] - Mark message as delivered (delivered=1). * @param {String} [params.message] - The message's text. * @param {updateMessageCallback} callback - The callback function. * */ update: function(id, params, callback) { /** * Callback for QB.chat.message.update(). * @param {Object} error - The error object. * @param {Object} response - Empty body. * @callback updateMessageCallback * */ var attrAjax = { 'type': 'PUT', 'dataType': 'text', 'url': Utils.getUrl(MESSAGES_API_URL, id), 'data': params }; this.service.ajax(attrAjax, callback); }, /** * Delete message({@link https://docs.quickblox.com/docs/js-chat-messaging#delete-message read more}). * @memberof QB.chat.message * @param {String} id - The message id. * @param {Object} params - Object of parameters. * @param {deleteMessageCallback} callback - The callback function. * */ delete: function(id, params_or_callback, callback) { /** * Callback for QB.chat.message.delete(). * @param {Object} error - The error object. * @param {String} res - Empty string. * @callback deleteMessageCallback * */ var ajaxParams = { url: Utils.getUrl(MESSAGES_API_URL, id), type: 'DELETE', dataType: 'text' }; if (arguments.length === 2) { this.service.ajax(ajaxParams, params_or_callback); } else if (arguments.length === 3) { ajaxParams.data = params_or_callback; this.service.ajax(ajaxParams, callback); } }, /** * Get unread messages counter for one or group of dialogs({@link https://docs.quickblox.com/docs/js-chat-dialogs#get-number-of-unread-messages read more}). * @memberof QB.chat.message * @param {Object} params - Object of parameters. * @param {unreadCountMessageCallback} callback - The callback function. * */ unreadCount: function(params, callback) { /** * Callback for QB.chat.message.unreadCount(). * @param {Object} error - The error object. * @param {Object} res - The requested dialogs Object. * @callback unreadCountMessageCallback * */ if (params && params.chat_dialog_ids && Utils.isArray(params.chat_dialog_ids)) { params.chat_dialog_ids = params.chat_dialog_ids.join(); } this.service.ajax({ url: Utils.getUrl(MESSAGES_API_URL + '/unread'), data: params }, callback); } }; module.exports = MessageProxy; },{"../../qbConfig":252,"../../qbUtils":256}],238:[function(require,module,exports){ 'use strict'; var Utils = require('../qbUtils'); var config = require('../qbConfig'); /** * Address Book * @namespace QB.addressbook */ function AddressBook(service) { this.service = service; } AddressBook.prototype = { /** * The method is used to create, update and delete contacts in address book.
* If contact doesn't exist in address book then it will be created. If contacts exists then it will be updated. * If pass 'destroy: 1' then the contact will be removed.
* {@link https://docs.quickblox.com/docs/js-address-book#upload-address-book Found more here}.
* The method accepts 2 or 3 parameters. * @memberof QB.addressbook * @param {Object[]} list - A list of contacts to create / update / delete. * @param {Object} [options] * @param {string} [options.udid] - User's device identifier. If specified all operations will be in this context. Max length 64 symbols. * If not - it means a user has one global address book across all his devices. * @param {number} [options.force] - Defines force rewrite mode. * If set 1 then all previous contacts for device context will be replaced by new ones. * @param {Function} callback - The savedAddressBookCallback function. * * @example * var people = [{ * 'name':'Frederic Cartwright', * 'phone': '8879108395' * }, * { * 'phone': '8759108396', * 'destroy': 1 * }]; * * var options = { * force: 1, * udid: 'A337E8A4-80AD-8ABA-9F5D-579EFF6BACAB' * }; * * function addressBookSaved(err, response) { * if(err) { * * } else { * console.log('All constacts saved'); * } * } * * QB.addressbook.uploadAddressBook(addressBookList, savedAddressBookCallback); * // or second parameters can be options * QB.addressbook.uploadAddressBook(addressBookList, options, savedAddressBookCallback); * */ uploadAddressBook: function(list, optionsOrcallback, callback) { if (!Array.isArray(list)) { new Error('First parameter must be an Array.'); return; } var opts, cb; if(isFunction(optionsOrcallback)) { cb = optionsOrcallback; } else { opts = optionsOrcallback; cb = callback; } var data = { contacts: list }; if(opts) { if(opts.force) { data.force = opts.force; } if(opts.udid) { data.udid = opts.udid; } } this.service.ajax({ 'type': 'POST', 'url': Utils.getUrl(config.urls.addressbook), 'data': data, 'contentType': 'application/json; charset=utf-8', 'isNeedStringify': true },function(err, res) { if (err) { cb(err, null); } else { cb(null, res); } }); }, _isFakeErrorEmptyAddressBook: function(err) { var errDetails = err.detail ? err.detail : err.message.errors; return err.code === 404 && errDetails[0] === 'Empty address book'; }, /** * Retrive all contacts from address book({@link https://docs.quickblox.com/docs/js-address-book#retrieve-address-book read more}). * The method accepts 1 or 2 parameters. * @memberof QB.addressbook * @param {string|function} udidOrCallback - You could pass udid of address book or * callback function if you want to get contacts from global address book. * @param {function} [callback] - Callback function is used as 2nd parameter if you pass udid as 1st parameters. * This callback takes 2 arguments: an error and a response. * * @example * var UDID = 'D337E8A4-80AD-8ABA-9F5D-579EFF6BACAB'; * * function gotContacts(err, contacts) { * contacts.forEach( (contact) => { alert(contact); }) * } * * QB.addressbook.get(gotContacts); * // or you could specify what address book you need by udid * QB.addressbook.get(UDID, gotContacts); */ get: function(udidOrCallback, callback) { var self = this; var udid, cb; if(isFunction(udidOrCallback)) { cb = udidOrCallback; } else { udid = udidOrCallback; cb = callback; } if(!isFunction(cb)) { throw new Error('The QB.addressbook.get accept callback function is required.'); } var ajaxParams = { 'type': 'GET', 'url': Utils.getUrl(config.urls.addressbook), 'contentType': 'application/json; charset=utf-8', 'isNeedStringify': true }; if(udid) { ajaxParams.data = {'udid': udid}; } this.service.ajax(ajaxParams, function(err, res) { if (err) { // Don't ask me why. // Thanks to backend developers for this var isFakeErrorEmptyAddressBook = self._isFakeErrorEmptyAddressBook(err); if(isFakeErrorEmptyAddressBook) { cb(null, []); } else { cb(err, null); } } else { cb(null, res); } }); }, /** * Retrieve QuickBlox users that have phone numbers from your address book({@link https://docs.quickblox.com/docs/js-address-book#retrieve-registered-users read more}). * The methods accepts 1 or 2 parameters. * @memberof QB.addressbook * @param {boolean|function} udidOrCallback - You can pass isCompact parameter or callback object. If isCompact is passed then only user's id and phone fields will be returned from server. Otherwise - all standard user's fields will be returned. * @param {function} [callback] - Callback function is useв as 2nd parameter if you pass `isCompact` as 1st parameter. * This callback takes 2 arguments: an error and a response. */ getRegisteredUsers: function(isCompactOrCallback, callback) { var self = this; var isCompact, cb; if(isFunction(isCompactOrCallback)) { cb = isCompactOrCallback; } else { isCompact = isCompactOrCallback; cb = callback; } if(!isFunction(cb)) { throw new Error('The QB.addressbook.get accept callback function is required.'); } var ajaxParams = { 'type': 'GET', 'url': Utils.getUrl(config.urls.addressbookRegistered), 'contentType': 'application/json; charset=utf-8' }; if(isCompact) { ajaxParams.data = {'compact': 1}; } this.service.ajax(ajaxParams, function(err, res) { if (err) { // Don't ask me why. // Thanks to backend developers for this var isFakeErrorEmptyAddressBook = self._isFakeErrorEmptyAddressBook(err); if(isFakeErrorEmptyAddressBook) { cb(null, []); } else { cb(err, null); } } else { cb(null, res); } }); } }; module.exports = AddressBook; function isFunction(func) { return !!(func && func.constructor && func.call && func.apply); } },{"../qbConfig":252,"../qbUtils":256}],239:[function(require,module,exports){ 'use strict'; var config = require('../qbConfig'), Utils = require('../qbUtils'), sha1 = require('crypto-js/hmac-sha1'), sha256 = require('crypto-js/hmac-sha256'); function AuthProxy(service) { this.service = service; } AuthProxy.prototype = { getSession: function(callback) { this.service.ajax({url: Utils.getUrl(config.urls.session)}, function(err,res){ if (err) { callback(err, null); } else { callback (err, res); } }); }, createSession: function(params, callback) { if (config.creds.appId === '' || config.creds.authKey === '' || config.creds.authSecret === '') { throw new Error('Cannot create a new session without app credentials (app ID, auth key and auth secret)'); } var _this = this, message; if (typeof params === 'function' && typeof callback === 'undefined') { callback = params; params = {}; } // Signature of message with SHA-1 using secret key message = generateAuthMsg(params); message.signature = signMessage(message, config.creds.authSecret); this.service.ajax({url: Utils.getUrl(config.urls.session), type: 'POST', data: message}, function(err, res) { if (err) { callback(err, null); } else { _this.service.setSession(res.session); callback(null, res.session); } }); }, destroySession: function(callback) { var _this = this; this.service.ajax({url: Utils.getUrl(config.urls.session), type: 'DELETE', dataType: 'text'}, function(err, res) { if (err) { callback(err, null); } else { _this.service.setSession(null); callback(null, res); } }); }, login: function(params, callback) { var ajaxParams = { type: 'POST', url: Utils.getUrl(config.urls.login), data: params }; function handleResponce(err, res) { if (err) { callback(err, null); } else { callback(null, res.user); } } this.service.ajax(ajaxParams,handleResponce); }, logout: function(callback) { this.service.ajax({url: Utils.getUrl(config.urls.login), type: 'DELETE', dataType:'text'}, callback); } }; module.exports = AuthProxy; /* Private ---------------------------------------------------------------------- */ function generateAuthMsg(params) { var message = { application_id: config.creds.appId, auth_key: config.creds.authKey, nonce: Utils.randomNonce(), timestamp: Utils.unixTime() }; // With user authorization if (params.login && params.password) { message.user = {login: params.login, password: params.password}; } else if (params.email && params.password) { message.user = {email: params.email, password: params.password}; } else if (params.provider) { // Via social networking provider (e.g. facebook, twitter etc.) message.provider = params.provider; if (params.scope) { message.scope = params.scope; } if (params.keys && params.keys.token) { message.keys = {token: params.keys.token}; } if (params.keys && params.keys.secret) { message.keys.secret = params.keys.secret; } } return message; } function signMessage(message, secret) { var sessionMsg = Object.keys(message).map(function(val) { if (typeof message[val] === 'object') { return Object.keys(message[val]).map(function(val1) { return val + '[' + val1 + ']=' + message[val][val1]; }).sort().join('&'); } else { return val + '=' + message[val]; } }).sort().join('&'); var cryptoSessionMsg; if(config.hash === 'sha1') { cryptoSessionMsg = sha1(sessionMsg, secret).toString(); } else if(config.hash === 'sha256') { cryptoSessionMsg = sha256(sessionMsg, secret).toString(); } else { throw new Error('Quickblox SDK: unknown crypto standards, available sha1 or sha256'); } return cryptoSessionMsg; } },{"../qbConfig":252,"../qbUtils":256,"crypto-js/hmac-sha1":51,"crypto-js/hmac-sha256":52}],240:[function(require,module,exports){ 'use strict'; /* * QuickBlox JavaScript SDK * * Content module * * For an overview of this module and what it can be used for * see http://quickblox.com/modules/content * * The API itself is described at http://quickblox.com/developers/Content * */ var config = require('../qbConfig'), Utils = require('../qbUtils'); // For server-side applications through using npm package 'quickblox' you should include the following lines var isBrowser = typeof window !== 'undefined'; /** * Content * @namespace QB.content **/ function ContentProxy(service) { this.service = service; } ContentProxy.prototype = { /** * Get a list of files for current user({@link https://docs.quickblox.com/docs/js-content#retrieve-files read more}). * @memberof QB.content * @param {object} params - Object of parameters. * @param {number} [params.page=1] - Used to paginate the results when more than one page of files retrieved. * @param {number} [params.per_page=10] - The maximum number of files to return per page, if not specified then the default is 10. * @param {listOfFilesCallback} callback - The listOfFilesCallback function. */ list: function(params, callback) { /** * Callback for QB.content.list(params, callback). * @callback listOfFilesCallback * @param {object} error - The error object. * @param {object} response - Object with Array of files. */ if (typeof params === 'function' && typeof callback === 'undefined') { callback = params; params = null; } this.service.ajax({url: Utils.getUrl(config.urls.blobs), data: params, type: 'GET'}, function(err,result){ if (err) { callback(err, null); } else { callback (err, result); } }); }, /** * Create new file object. * @private * @memberof QB.content * @param {object} params - Object of parameters. * @param {string} params.content_type - The file's mime({@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types content type}). * @param {string} params.name - The file's name. * @param {boolean} [params.public=false] - The file's visibility. public means it will be possible to access this file without QuickBlox session token provided. Default is 'false'. * @param {createFileCallback} callback - The createFileCallback function. */ create: function(params, callback) { /** * Callback for QB.content.create(params, callback). * @callback createFileCallback * @param {object} error - The error object. * @param {object} response - The file object (blob-object-access). */ this.service.ajax({ type: 'POST', data: {blob: params}, url: Utils.getUrl(config.urls.blobs) }, function(err, result) { if (err) { callback(err, null); } else { callback(err, result.blob); } }); }, /** * Delete file by id({@link https://docs.quickblox.com/docs/js-content#delete-file read more}). * @memberof QB.content * @param {Number} id - blob_id. * @param {deleteFileCallback} callback - The deleteFileCallback function. */ delete: function(id, callback) { /** * Callback for QB.content.delete(id, callback). * @callback deleteFileCallback * @param {object} error - The error object. * @param {object} response - Boolean. */ this.service.ajax({url: Utils.getUrl(config.urls.blobs, id), type: 'DELETE', dataType: 'text'}, function(err, result) { if (err) { callback(err, null); } else { callback(null, true); } }); }, /** * Create file > upload file > mark file as uploaded > return result({@link https://docs.quickblox.com/docs/js-content#upload-file read more}). * @memberof QB.content * @param {object} params - Object of parameters. * @param {object} params.file - File object. * @param {string} params.name - The file's name. * @param {string} params.type - The file's mime ({@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types content type}). * @param {number} params.size - Size of file, in bytes. * @param {boolean} [params.public=false] - The file's visibility. public means it will be possible to access this file without QuickBlox session token provided. Default is 'false'. * @param {createAndUploadFileCallback} callback - The createAndUploadFileCallback function. */ createAndUpload: function(params, callback) { /** * Callback for QB.content.createAndUpload(params, callback). * @callback createAndUploadFileCallback * @param {object} error - The error object. * @param {object} response - The file object (blob-object-access). */ var _this = this, createParams= {}, file, name, type, size, fileId; var clonedParams = JSON.parse(JSON.stringify(params)); clonedParams.file.data = "..."; file = params.file; name = params.name || file.name; type = params.type || file.type; size = params.size || file.size; createParams.name = name; createParams.content_type = type; if (params.public) { createParams.public = params.public; } if (params.tag_list) { createParams.tag_list = params.tag_list; } // Create a file object this.create(createParams, function(err, createResult){ if (err) { callback(err, null); } else { var uri = parseUri(createResult.blob_object_access.params), uploadUrl = uri.protocol + "://" + uri.authority + uri.path, uploadParams = {url: uploadUrl}, data = {}; fileId = createResult.id; createResult.size = size; Object.keys(uri.queryKey).forEach(function(val) { data[val] = decodeURIComponent(uri.queryKey[val]); }); data.file = file; uploadParams.data = data; // Upload the file to Amazon S3 _this.upload(uploadParams, function(err, result) { if (err) { callback(err, null); } else { // Mark file as uploaded _this.markUploaded({ id: fileId, size: size }, function(err, result){ if (err) { callback(err, null); } else { callback(null, createResult); } }); } }); } }); }, /** * Upload a file to cloud storage. * @private * @memberof QB.content * @param {Object} params - Object of parameters (see into source code of QB.content.createAndUpload(params, callback) to know how to prepare the params object). * @param {string} params.url - File url. * @param {object} params.data - Formed data with file. * @param {uploadFileCallback} callback - The uploadFileCallback function. */ upload: function(params, callback) { /** * Callback for QB.content.upload(params, callback). * @callback uploadFileCallback * @param {object} error - The error object. * @param {object} response - The empty object. */ var uploadParams = { type: 'POST', dataType: 'text', contentType: false, url: params.url, data: params.data }; this.service.ajax(uploadParams, function(err, xmlDoc) { if (err) { callback (err, null); } else { callback (null, {}); } }); }, /** * Declare file uploaded. The file's 'status' field will be set to 'complete'. * @private * @memberof QB.content * @param {object} params - Object of parameters. * @param {number} params.blob_id - The id of file to declare as uploaded. * @param {number} params.size - Size of file, in bytes. * @param {markUploadedFileCallback} callback - The markUploadedFileCallback function. */ markUploaded: function (params, callback) { /** * Callback for QB.content.markUploaded(params, callback). * @callback markUploadedFileCallback * @param {object} error - The error object. * @param {object} response - The empty body. */ this.service.ajax({ url: Utils.getUrl(config.urls.blobs, params.id + '/complete'), type: 'PUT', data: { size: params.size }, dataType: 'text' }, function(err, res){ if (err) { callback (err, null); } else { callback (null, res); } }); }, /** * Retrieve file object info by id({@link https://docs.quickblox.com/docs/js-content#get-file-info read more}). * @memberof QB.content * @param {number} id - File object id. * @param {getFileInfoByIdCallback} callback - The getFileInfoByIdCallback function return file's object. */ getInfo: function (id, callback) { /** * Callback for QB.content.getInfo(id, callback). * @callback getFileInfoByIdCallback * @param {object} error - The error object. * @param {object} response - The file object (blob-object-access). */ this.service.ajax({url: Utils.getUrl(config.urls.blobs, id)}, function (err, res) { if (err) { callback (err, null); } else { callback (null, res); } }); }, /** * Download file by UID. If the file is public then it's possible to download it without a session token({@link https://docs.quickblox.com/docs/js-content#download-file-by-uid read more}). * @memberof QB.content * @param {String} uid - File object uid. * @param {downloadFileByUIDCallback} callback - The downloadFileByUIDCallback function. */ getFile: function (uid, callback) { /** * Callback for QB.content.getFile(uid, callback). * @callback downloadFileByUIDCallback * @param {object} error - The error object. * @param {object} response - The file object. */ this.service.ajax({url: Utils.getUrl(config.urls.blobs, uid)}, function (err, res) { if (err) { callback (err, null); } else { callback (null, res); } }); }, /** * Edit a file by ID({@link https://docs.quickblox.com/docs/js-content#update-file read more}). * @memberof QB.content * @param {object} params - Object of parameters. * @param {number} params.id - File object id. * @param {string} [params.name] - New file name. * @param {updateFileCallback} callback - The updateFileCallback function. */ update: function (params, callback) { /** * Callback for QB.content.update(uid, callback). * @callback updateFileCallback * @param {object} error - The error object. * @param {object} response - The file object (blob-object-access). */ var data = {}; data.blob = {}; if (typeof params.name !== 'undefined') { data.blob.name = params.name; } this.service.ajax({url: Utils.getUrl(config.urls.blobs, params.id), data: data}, function(err, res) { if (err) { callback (err, null); } else { callback (null, res); } }); }, /** * Get private URL for file download by file_uid (blob_uid) ({@link https://docs.quickblox.com/docs/js-content#get-private-url read more}). * @memberof QB.content * @param {String} fileUID - File object id. */ privateUrl: function (fileUID) { return "https://" + config.endpoints.api + "/blobs/" + fileUID + "?token=" + this.service.getSession().token; }, /** * Get public URL for file download by file_uid (blob_uid) ({@link https://docs.quickblox.com/docs/js-content#get-public-url read more}). * @memberof QB.content * @param {String} fileUID - File object id. */ publicUrl: function (fileUID) { return "https://" + config.endpoints.api + "/blobs/" + fileUID; } }; module.exports = ContentProxy; // parseUri 1.2.2 // (c) Steven Levithan // MIT License // http://blog.stevenlevithan.com/archives/parseuri function parseUri (str) { var o = parseUri.options, m = o.parser[o.strictMode ? "strict" : "loose"].exec(str), uri = {}, i = 14; while (i--) {uri[o.key[i]] = m[i] || "";} uri[o.q.name] = {}; uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) { if ($1) {uri[o.q.name][$1] = $2;} }); return uri; } parseUri.options = { strictMode: false, key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"], q: { name: "queryKey", parser: /(?:^|&)([^&=]*)=?([^&]*)/g }, parser: { strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ } }; },{"../qbConfig":252,"../qbUtils":256}],241:[function(require,module,exports){ 'use strict'; var config = require('../qbConfig'); var Utils = require('../qbUtils'); /** * Custom Objects Module * @namespace QB.data **/ function DataProxy(service){ this.service = service; } DataProxy.prototype = { /** * Create new custom object({@link https://docs.quickblox.com/docs/js-custom-objects#create-records read more}). * * @memberof QB.data * * @param {string} className - A class name to which a new object belongs. * @param {object} data - Object of parameters (custom fields' names and their values). * @param {createDataCallback} callback - The createDataCallback function. * * @example * var data = { * 'name': 'John', * 'age':'20', * 'family': [ * 'Stark', * 'Targaryen' * ] * }; * * function createdDataCallback(error, response) { * if (error) { * console.log(error); * } else { * console.log(response); * } * } * * QB.data.create('GameOfThrones', data, createdDataCallback); */ create: function(className, data, callback) { /** * Callback for QB.data.create(className, data, callback). * @callback createDataCallback * @param {object} error - The error object. * @param {object} response - An object. */ var ajaxParams = { 'type': 'POST', 'data': data, 'isNeedStringify': true, 'contentType': 'application/json; charset=utf-8', 'url': Utils.getUrl(config.urls.data, className) }; this.service.ajax(ajaxParams, function(err,res) { if (err) { callback(err, null); } else { callback (err, res); } }); }, /** * Search for records of particular class({@link https://docs.quickblox.com/docs/js-custom-objects#retrieve-records read more}). * * @memberof QB.data * * @param {string} className - A class name to which a new record belongs. * @param {(object|string[])} filters - Search records with field which contains exactly specified value or by array of records' ids to retrieve. * @param {number} [filters.skip=0] - Skip N records in search results. Useful for pagination. Default (if not specified) - 0. * @param {number} [filters.limit=100] - Limit search results to N records. Useful for pagination. Default and max values - 100. If limit is equal to -1 only last record will be returned. * @param {string} [filters.*] - {@link https://docs.quickblox.com/docs/js-custom-objects#search-operators See more filters' parameters}. * @param {listOfDataCallback} callback - The listOfDataCallback function. * @example * var filter = { * 'skip': 0, * 'limit': 100, * 'sort_desc': 'updated_at', * 'family': { * 'in': 'Stark,Targaryen' * } * }; * * var ids = ['53aaa06f535c12cea9007496', '53aaa06f535c12cea9007495']; * * // use filter or ids to get records: * QB.data.list('GameOfThrones', filter, function(error, response) { * if (error) { * console.log(error); * } else { * console.log(response); * } * }); */ list: function(className, filters, callback) { /** * Callback for QB.data.list(className, filters, callback). * @callback listOfDataCallback * @param {object} error - The error object. * @param {object} response - Object with Array of files. */ // make filters an optional parameter if (typeof callback === 'undefined' && typeof filters === 'function') { callback = filters; filters = null; } this.service.ajax({url: Utils.getUrl(config.urls.data, className), data: filters}, function(err,result){ if (err) { callback(err, null); } else { callback (err, result); } }); }, /** * Update record by ID of particular class({@link https://docs.quickblox.com/docs/js-custom-objects#update-records Read more}). * @memberof QB.data * @param {string} className - A class name of record. * @param {object} data - Object of parameters. * @param {string} data._id - An ID of record to update. * @param {updateDataCallback} callback - The updateDataCallback function. * @example * QB.data.update('GameOfThrones', { * '_id': '53aaa06f535c12cea9007496', * 'pull': { * 'family':'Stark' * } * }, function(error, response) { * if (error) { * console.log(error); * } else { * console.log(response); * } * }); */ update: function(className, data, callback) { /** * Callback for QB.data.update(className, data, callback). * @callback updateDataCallback * @param {object} error - The error object. * @param {object} response - An object. */ this.service.ajax({ 'url': Utils.getUrl(config.urls.data, className + '/' + data._id), 'type': 'PUT', 'contentType': 'application/json; charset=utf-8', 'isNeedStringify': true, 'data': data }, function(err,result){ if (err) { callback(err, null); } else { callback (err, result); } }); }, /** * Delete record/records by ID, IDs or criteria (filters) of particular class({@link https://docs.quickblox.com/docs/js-custom-objects#delete-records read more}). * * @memberof QB.data * * @param {string} className - A class name of record. * @param {(string|array|object)} requestedData - An ID of record or an array of record's ids or object of criteria rules to delete. * @param {deletedDataCallback} callback - The deletedDataCallback function. * * @example * var className = 'Movie'; * * function deletedMovie(error, responce) { * if(error) { * throw new Error(error.toString()); * } else { * console.log(`Deleted movies with ids: ${responce.deleted.toString()}`); * } * } * * // By ID, must be string * var id = '502f7c4036c9ae2163000002'; * QB.data.delete(className, id, deletedMovie); * * // By IDs, must be array * var ids = ['502f7c4036c9ae2163000002', '502f7c4036c9ae2163000003']; * QB.data.delete(className, ids, deletedMovie); * * var criteria = { 'price': { 'gt': 100 }; * function deletedMoviesByCriteria(error, responce) { * if(error) { * throw new Error(error.toString()); * } else { * // Note! Deleted by creteria doesn't return ids of deleted objects * console.log(`Deleted ${responce.deletedCount} movies`); * } * } * QB.data.delete(className, criteria, deletedMoviesByCriteria); * * */ delete: function(className, requestedData, callback) { /** * Callback for QB.data.delete(className, requestedData, callback). * @callback deletedDataCallback * @param {object} error - The error object. * @param {object|null} response * @param {array} response.deleted - Array of ids of deleted records. If you delete BY CRITERIA this property will be null. * @param {number} response.deletedCount - Count of deleted records. */ var typesData = { id: 1, ids: 2, criteria: 3 }; var requestedTypeOf; var responceNormalized = { deleted: [], deletedCount: 0 }; var ajaxParams = { type: 'DELETE', dataType: 'text' }; /** Define what type of data passed by client */ if(typeof requestedData === 'string') { requestedTypeOf = typesData.id; } else if(Utils.isArray(requestedData)) { requestedTypeOf = typesData.ids; } else if(Utils.isObject(requestedData)) { requestedTypeOf = typesData.criteria; } if(requestedTypeOf === typesData.id) { ajaxParams.url = Utils.getUrl(config.urls.data, className + '/' + requestedData); } else if(requestedTypeOf === typesData.ids) { ajaxParams.url = Utils.getUrl(config.urls.data, className + '/' + requestedData.toString()); } else if(requestedTypeOf === typesData.criteria) { ajaxParams.url = Utils.getUrl(config.urls.data, className + '/by_criteria'); ajaxParams.data = requestedData; } function handleDeleteCO(error, result) { if (error) { callback(error, null); } else { var response; if(requestedTypeOf === typesData.id) { responceNormalized.deleted.push(requestedData); responceNormalized.deletedCount = responceNormalized.deleted.length; } else if(requestedTypeOf === typesData.ids) { response = JSON.parse(result); responceNormalized.deleted = response.SuccessfullyDeleted.ids.slice(0); responceNormalized.deletedCount = responceNormalized.deleted.length; } else if(requestedTypeOf === typesData.criteria) { response = JSON.parse(result); responceNormalized.deleted = null; responceNormalized.deletedCount = response.total_deleted; } callback (error, responceNormalized); } } this.service.ajax(ajaxParams, handleDeleteCO); }, /** * Upload file to file field({@link https://docs.quickblox.com/docs/js-custom-objects#files read more}). * @memberof QB.data * @param {string} className - A class name to which a new object belongs. * @param {object} params - Object of parameters. * @param {string} [params.field_name] - The file's field name. * @param {string} [params.name] - The file's name. * @param {object} [params.file] - File object. * @param {uploadFileToDataCallback} callback - The uploadFileToDataCallback function. */ uploadFile: function(className, params, callback) { /** * Callback for QB.data.uploadFile(className, params, callback). * @callback uploadFileToDataCallback * @param {object} error - The error object. * @param {object} response - The file object. */ var data = { field_name: params.field_name, file: { data: params.file, name: params.name } }; this.service.ajax({ 'url': Utils.getUrl(config.urls.data, className + '/' + params.id + '/file'), 'type': 'POST', 'fileToCustomObject': true, 'contentType': false, 'data': data, }, function(err, result){ if (err) { callback(err, null); } else { callback (err, result); } }); }, /** * Download file from file field by ID({@link https://docs.quickblox.com/docs/js-custom-objects#download-file read more}). * @memberof QB.data * @param {string} className - A class name of record. * @param {object} params - Object of parameters. * @param {string} params.field_name - The file's field name. * @param {string} params.id - The record's ID. * @param {downloadFileFromDataCallback} callback - The downloadFileFromDataCallback function. */ downloadFile: function(className, params, callback) { /** * Callback for QB.data.downloadFile(className, params, callback). * @callback downloadFileFromDataCallback * @param {object} error - The error object. * @param {object} response - The file object. */ var result = Utils.getUrl(config.urls.data, className + '/' + params.id + '/file'); result += '?field_name=' + params.field_name + '&token=' + this.service.getSession().token; callback(null, result); }, /** * Return file's URL from file field by ID. * @memberof QB.data * @param {string} className - A class name of record. * @param {object} params - Object of parameters. * @param {string} params.field_name - The file's field name. * @param {string} params.id - The record's ID. */ fileUrl: function(className, params) { var result = Utils.getUrl(config.urls.data, className + '/' + params.id + '/file'); result += '?field_name=' + params.field_name + '&token=' + this.service.getSession().token; return result; }, /** * Delete file from file field by ID({@link https://docs.quickblox.com/docs/js-custom-objects#delete-file read more}). * @memberof QB.data * @param {string} className - A class name of record. * @param {object} params - Object of parameters. * @param {string} params.field_name - The file's field name. * @param {string} params.id - The record's ID. * @param {deleteFileFromDataCallback} callback - The deleteFileFromDataCallback function. */ deleteFile: function(className, params, callback) { /** * Callback for QB.data.deleteFile(className, params, callback). * @callback deleteFileFromDataCallback * @param {object} error - The error object. * @param {object} response - Empty body. */ this.service.ajax({url: Utils.getUrl(config.urls.data, className + '/' + params.id + '/file'), data: {field_name: params.field_name}, dataType: 'text', type: 'DELETE'}, function(err, result) { if (err) { callback(err, null); } else { callback (err, true); } }); } }; module.exports = DataProxy; },{"../qbConfig":252,"../qbUtils":256}],242:[function(require,module,exports){ (function (Buffer){(function (){ 'use strict'; /* * QuickBlox JavaScript SDK * * Push Notifications Module * */ var config = require('../qbConfig'), Utils = require('../qbUtils'); var isBrowser = typeof window !== "undefined"; function PushNotificationsProxy(service) { this.service = service; this.subscriptions = new SubscriptionsProxy(service); this.events = new EventsProxy(service); this.base64Encode = function(str) { if (isBrowser) { return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { return String.fromCharCode('0x' + p1); })); } else { return new Buffer(str).toString('base64'); } }; } /** * Push Notifications Subscriptions * @namespace QB.pushnotifications.subscriptions **/ function SubscriptionsProxy(service){ this.service = service; } SubscriptionsProxy.prototype = { /** * Create device based subscription. * @memberof QB.pushnotifications.subscriptions * @param {object} params - Object of parameters. * @param {string} params.notification_channel - Declare which notification channels could be used to notify user about events. Allowed values: apns, apns_voip, gcm, mpns, bbps and email. * @param {object} params.push_token - Object of parameters. * @param {string} params.push_token.environment - Determine application mode. It allows conveniently separate development and production modes. Allowed values: evelopment or production. * @param {string} [params.push_token.bundle_identifier] - A unique identifier for client's application. In iOS, this is the Bundle Identifier. In Android - package id. * @param {string} params.push_token.client_identification_sequence - Identifies client device in 3-rd party service like APNS, GCM/FCM, BBPS or MPNS. Initially retrieved from 3-rd service and should be send to QuickBlox to let it send push notifications to the client. * @param {object} params.device - Object of parameters. * @param {string} params.device.platform - Platform of device, which is the source of application running. Allowed values: ios, android, windows_phone, blackberry. * @param {string} params.device.udid - UDID (Unique Device identifier) of device, which is the source of application running. This must be anything sequence which uniquely identify particular device. This is needed to support schema: 1 User - Multiple devices. * @param {createPushSubscriptionCallback} callback - The createPushSubscriptionCallback function. */ create: function(params, callback) { /** * Callback for QB.pushnotifications.subscriptions.create(params, callback). * @callback createPushSubscriptionCallback * @param {object} error - The error object. * @param {object} response - Array of all existent user's subscriptions. */ this.service.ajax({url: Utils.getUrl(config.urls.subscriptions), type: 'POST', data: params}, callback); }, /** * Retrieve subscriptions for the user which is specified in the session token. * @memberof QB.pushnotifications.subscriptions * @param {listPushSubscriptionCallback} callback - The listPushSubscriptionCallback function. */ list: function(callback) { /** * Callback for QB.pushnotifications.subscriptions.list(callback). * @callback listPushSubscriptionCallback * @param {object} error - The error object. * @param {object} response - Array of all existent user's subscriptions. */ this.service.ajax({url: Utils.getUrl(config.urls.subscriptions)}, callback); }, /** * Remove a subscription by its identifier. * @memberof QB.pushnotifications.subscriptions * @param {number} id - An id of subscription to remove. * @param {deletePushSubscriptionCallback} callback - The deletePushSubscriptionCallback function. */ delete: function(id, callback) { /** * Callback for QB.pushnotifications.subscriptions.delete(id, callback). * @callback deletePushSubscriptionCallback * @param {object} error - The error object. * @param {object} response - Empty body. */ var attrAjax = { 'type': 'DELETE', 'dataType': 'text', 'url': Utils.getUrl(config.urls.subscriptions, id) }; this.service.ajax(attrAjax, function(err, res){ if (err) { callback(err, null); } else { callback(null, true); } }); } }; /** * Push Notifications Events * @namespace QB.pushnotifications.events **/ function EventsProxy(service){ this.service = service; } EventsProxy.prototype = { /** * Create notification event. This request will immediately produce notification delivery (push notification or email) ({@link https://docs.quickblox.com/docs/js-push-notifications#send-push-notifications read more}). * @memberof QB.pushnotifications.events * * @param {object} params - Object of parameters. * @param {string} params.notification_type - Type of notification. Allowed values: push or email. * @param {string} params.environment - An environment of the notification. Allowed values: development or production. * @param {string} params.message - A payload of event. For push notifications: if event[push_type] not present - should be Base64 encoded text. * * @param {string} [params.push_type] - Push Notification type. Used only if event[notification_type] = push, ignored in other cases. If not present - Notification will be delivered to all possible devices for specified users. Each platform has their own standard format. If specified - Notification will be delivered to the specified platform only. Allowed values: apns, apns_voip, gcm, mpns or bbps. * @param {string} [params.event_type] - Allowed values: one_shot, fixed_date or period_date. one_shot - a one-time event, which causes by an external object (the value is only valid if the 'date' is not specified). fixed_date - a one-time event, which occurs at a specified 'date' (the value is valid only if the 'date' is given). period_date - reusable event that occurs within a given 'period' from the initial 'date' (the value is only valid if the 'period' specified). By default: fixed_date, if 'date' is specified. period_date, if 'period' is specified. one_shot, if 'date' is not specified. * @param {string} [params.name] - The name of the event. Service information. Only for your own usage. * @param {number} [params.period] - The period of the event in seconds. Required if the event[event_type] = period_date. Possible values: 86400 (1 day). 604800 (1 week). 2592000 (1 month). 31557600 (1 year). * @param {number} [params.date] - The date of the event to send on. Required if event[event_type] = fixed_date or period_date. If event[event_type] = fixed_date, the date can not be in the pas. * * @param {object} [params.user] - User's object of parameters. * @param {number[]} [params.user.ids] - Notification's recipients - should contain a string of users' ids divided by commas. * @param {object} [params.user.tags] - User's object of tags. * @param {string[]} [params.user.tags.any] - Notification's recipients - should contain a string of tags divided by commas. Recipients (users) must have at least one tag that specified in the list. * @param {string[]} [params.user.tags.all] - Notification's recipients - should contain a string of tags divided by commas. Recipients (users) must exactly have only all tags that specified in list. * @param {string[]} [params.user.tags.exclude] - Notification's recipients - should contain a string of tags divided by commas. Recipients (users) mustn't have tags that specified in list. * * @param {object} [params.external_user] - External user's object of parameters. * @param {number[]} [params.external_user.ids] - Notification's recipients - should contain a string of tags divided by commas. Recipients (users) mustn't have tags that specified in list. * * @param {createPushEventCallback} callback - The createPushEventCallback function. */ create: function(params, callback) { /** * Callback for QB.pushnotifications.events.create(params, callback). * @callback createPushEventCallback * @param {object} error - The error object. * @param {object} response - An event object. */ this.service.ajax({ 'url': Utils.getUrl(config.urls.events), 'type': 'POST', 'contentType': 'application/json; charset=utf-8', 'isNeedStringify': true, 'data': { 'event': params } }, callback); }, /** * Get list of events which were created by current user. * @memberof QB.pushnotifications.events * @param {object} params - Object of parameters. * @param {number} [params.page=1] - Used to paginate the results when more than one page of events retrieved. * @param {number} [params.per_page=10] - The maximum number of events to return per page, if not specified then the default is 10. * @param {listPushEventsCallback} callback - The listOfFilesCallback function. */ list: function(params, callback) { /** * Callback for QB.pushnotifications.events.list(params, callback). * @callback listPushEventsCallback * @param {object} error - The error object. * @param {object} response - An array of events' objects. */ if (typeof params === 'function' && typeof callback ==='undefined') { callback = params; params = null; } this.service.ajax({url: Utils.getUrl(config.urls.events), data: params}, callback); }, /** * Retrieve an event by ID. * @memberof QB.pushnotifications.events * @param {number} id - An id of event to retrieve. * @param {getPushEventByIdCallback} callback - The getPushEventByIdCallback function. */ get: function(id, callback) { /** * Callback for QB.pushnotifications.events.get(id, callback). * @callback getPushEventByIdCallback * @param {object} error - The error object. * @param {object} response - An array of events' objects. */ this.service.ajax({url: Utils.getUrl(config.urls.events, id)}, callback); }, /** * Delete an event by ID. * @memberof QB.pushnotifications.events * @param {number} id - An id of event to delete. * @param {deletePushEventByIdCallback} callback - The deletePushEventByIdCallback function. */ delete: function(id, callback) { /** * Callback for QB.pushnotifications.events.delete(id, callback). * @callback deletePushEventByIdCallback * @param {object} error - The error object. * @param {object} response - Empty body. */ this.service.ajax({url: Utils.getUrl(config.urls.events, id), dataType: 'text', type: 'DELETE'}, callback); }, /** * Retrieve an event's status by ID * @memberof QB.pushnotifications.events * @param {number} id - An id of event to retrieve its status. * @param {getPushEventStatusByIdCallback} callback - The getPushEventStatusByIdCallback function. */ status: function(id, callback) { /** * Callback for QB.pushnotifications.events.status(id, callback). * @callback getPushEventStatusByIdCallback * @param {object} error - The error object. * @param {object} response - An array of events' objects. */ this.service.ajax({url: Utils.getUrl(config.urls.events, id + '/status')}, callback); } }; module.exports = PushNotificationsProxy; }).call(this)}).call(this,require("buffer").Buffer) },{"../qbConfig":252,"../qbUtils":256,"buffer":47}],243:[function(require,module,exports){ 'use strict'; /* * QuickBlox JavaScript SDK * * Users Module * */ var config = require('../qbConfig'), Utils = require('../qbUtils'); var DATE_FIELDS = ['created_at', 'updated_at', 'last_request_at']; var NUMBER_FIELDS = ['id', 'external_user_id']; var resetPasswordUrl = config.urls.users + '/password/reset'; /** * @namespace QB.users **/ function UsersProxy(service) { this.service = service; } UsersProxy.prototype = { /** * Call this API to get a list of current users of you app. By default it returns upto 10 users, but you can change this by adding pagination parameters. You can filter the list of users by supplying a filter string. You can sort results by ask/desc({@link https://docs.quickblox.com/docs/js-users#retrieve-users read more}). * @memberof QB.users * @param {object} params - Object of parameters. * @param {number} [params.page=1] - Used to paginate the results when more than one page of users retrieved. * @param {number} [params.per_page=10] - The maximum number of users to return per page, if not specified then the default is 10. * @param {string} [params.filter] - You can filter the list of users by supplying a {@link https://docs.quickblox.com/docs/js-users#search-operators filter string}. Possible operators: gt, lt, ge, le, eq, ne, between, in. Allowed fields' types: string,number,date. Allowed fields: id, full_name, email, login, phone, website, created_at, updated_at, last_request_at, external_user_id, twitter_id, twitter_digits_id, facebook_id (example: 'field_type+field_name+operator+value'). * @param {string} [params.order] - Parameter to sort results. Possible values: asc and desc. Allowed fields' types: string,number,date. Allowed fields: id, full_name, email, login, phone, website, created_at, updated_at, last_request_at, external_user_id, twitter_id, twitter_digits_id, facebook_id ('asc+date+created_at' (format is 'sort_type+field_type+field_name')). * @param {listUsersCallback} callback - The listUsersCallback function. */ listUsers: function(params, callback) { /** * Callback for QB.users.listUsers(params, callback). * @callback listUsersCallback * @param {object} error - The error object. * @param {object} response - Object with Array of users. */ var message = {}, filters = [], item; if (typeof params === 'function' && typeof callback === 'undefined') { callback = params; params = {}; } if (params && params.filter) { if (Utils.isArray(params.filter)) { params.filter.forEach(function(el) { item = generateFilter(el); filters.push(item); }); } else { item = generateFilter(params.filter); filters.push(item); } message.filter = filters; } if (params.order) { message.order = generateOrder(params.order); } if (params.page) { message.page = params.page; } if (params.per_page) { message.per_page = params.per_page; } this.service.ajax({ url: Utils.getUrl(config.urls.users), data: message}, callback); }, /** * Retrieve a specific user or users. * @memberof QB.users * @param {(number|object)} params - User ID (number) or object of parameters (object with one of next required properties). * @param {string} params.login - The login of the user to be retrieved({@link https://docs.quickblox.com/docs/js-users#retrieve-user-by-login read more}). * @param {string} params.full_name - The full name of users to be retrieved({@link https://docs.quickblox.com/docs/js-users#retrieve-users-by-full-name read more}). * @param {string} params.facebook_id - The user's facebook uid. * @param {string} params.twitter_id - The user's twitter uid. * @param {string} params.phone - The user's phone number({@link https://docs.quickblox.com/docs/js-users#retrieve-users-by-phone-number read more}). * @param {string} params.email - The user's email address({@link https://docs.quickblox.com/docs/js-users#retrieve-user-by-email read more}). * @param {(string|string[])} params.tags - A comma separated list of tags associated with users({@link https://docs.quickblox.com/docs/js-users#retrieve-users-by-tags read more}). * @param {(number|string)} params.external - An uid that represents the user in an external user registry({@link https://docs.quickblox.com/docs/js-users#retrieve-user-by-external-user-id read more}). * @param {string} [params.page=1] - Used to paginate the results when more than one page of users retrieved (can be used with get by 'full_name' or 'tags'). * @param {string} [params.per_page=10] - The maximum number of users to return per page, if not specified then the default is 10 (can be used with get by 'full_name' or 'tags'). * @param {getUsersCallback} callback - The getUsersCallback function. * @example * var params = {'email': 'example-email@gmail.com'}; * * // for search by 'full_name' or 'tags': * // var params = { * // 'full_name': 'test_user', * // 'page': 2, * // 'per_page': 25 * // }; * * // for search by user's ID: * // var id = 53454; * * // use params or id to get records: * QB.users.get(params, function(error, response) { * if (error) { * console.log(error); * } else { * console.log(response); * } * }); */ get: function(params, callback) { /** * Callback for QB.users.get(params, callback). * @callback getUsersCallback * @param {object} error - The error object. * @param {object} response - The user object or object with Array of users. */ var url; if (typeof params === 'number') { url = params; params = {}; } else { if (params.login) { url = 'by_login'; } else if (params.full_name) { url = 'by_full_name'; } else if (params.facebook_id) { url = 'by_facebook_id'; } else if (params.twitter_id) { url = 'by_twitter_id'; } else if (params.phone) { url = 'phone'; } else if (params.email) { url = 'by_email'; } else if (params.tags) { url = 'by_tags'; } else if (params.external) { url = 'external/' + params.external; params = {}; } } this.service.ajax({url: Utils.getUrl(config.urls.users, url), data: params}, function(err, res) { if (err) { callback(err, null); } else { callback(null, res.user || res); } }); }, /** * Registers a new app user. Call this API to register a user for the app. You must provide either a user login or email address along with their password, passing both email address and login is permitted but not required({@link https://docs.quickblox.com/docs/js-users#create-user read more}). * @memberof QB.users * @param {object} params - Object of user's parameters. * @param {string} params.login - The user's login name. * @param {string} params.password - The user's password for this app. * @param {string} params.email - The user's email address. * @param {string} [params.full_name] - The user's full name. * @param {string} [params.phone] - The user's phone number. * @param {string} [params.website] - The user's web address, or other url. * @param {string} [params.facebook_id] - The user's facebook uid. * @param {string} [params.twitter_id] - The user's twitter uid. * @param {number} [params.blob_id] - The id of an associated blob for this user, for example their photo. * @param {(number|string)} [params.external_user_id] - An uid that represents the user in an external user registry. * @param {(string|string[])} [params.tag_list] - A comma separated list of tags associated with the user. Set up user tags and address them separately in your app. * @param {string} [params.custom_data] - The user's additional info. * @param {createUserCallback} callback - The createUserCallback function. */ create: function(params, callback) { /** * Callback for QB.users.create(params, callback). * @callback createUserCallback * @param {object} error - The error object. * @param {object} response - The user object. */ this.service.ajax({url: Utils.getUrl(config.urls.users), type: 'POST', data: {user: params}}, function(err, res) { if (err) { callback(err, null); } else { callback(null, res.user); } }); }, /** * Update current user. In normal usage, nobody except the user is allowed to modify their own data. Any fields you don’t specify will remain unchanged, so you can update just a subset of the user’s data. login/email and password may be changed, but the new login/email must not already be in use({@link https://docs.quickblox.com/docs/js-users#update-user read more}). * @memberof QB.users * @param {number} id - The id of user to update. * @param {object} params - object of user's parameters. * @param {string} [params.login] - The user's login name. * @param {string} [params.old_password] - The user's old password for this app. * @param {string} [params.password] - The user's new password for this app. * @param {string} [params.email] - The user's email address. * @param {string} [params.full_name] - The user's full name. * @param {string} [params.phone] - The user's phone number. * @param {string} [params.website] - The user's web address, or other url. * @param {string} [params.facebook_id] - The user's facebook uid. * @param {string} [params.twitter_id] - The user's twitter uid. * @param {number} [params.blob_id] - The id of an associated blob for this user, for example their photo. * @param {(number|string)} [params.external_user_id] - An uid that represents the user in an external user registry. * @param {(string|string[])} [params.tag_list] - A comma separated list of tags associated with the user. Set up user tags and address them separately in your app. * @param {string} [params.custom_data] - The user's additional info. * @param {updateUserCallback} callback - The updateUserCallback function. */ update: function(id, params, callback) { /** * Callback for QB.users.update(id, params, callback). * @callback updateUserCallback * @param {object} error - The error object. * @param {object} response - The user object. */ this.service.ajax({url: Utils.getUrl(config.urls.users, id), type: 'PUT', data: {user: params}}, function(err, res) { if (err) { callback(err, null); } else { callback(null, res.user); } }); }, /** * Remove a user from the app, {@linkhttps://docs.quickblox.com/docs/js-users#delete-user by user's id} or uid that represents the user in an external user registry. * @memberof QB.users * @param {(number|object)} params - An id of user to remove or object with external user id. * @param {(number|string)} params.external - An id of user to remove or object with external user id. * @param {deleteUserCallback} callback - An uid that represents the user in an external user registry. * @example * // parameter as user id: * var params = 567831; * * // parameter as external user id: * // var params = {'external': 'ebdf831abd12da4bcf12f22d'}; * * QB.users.delete(params, function(error, response) { * if (error) { * console.log(error); * } else { * console.log(response); * } * }); */ delete: function(params, callback) { /** * Callback for QB.users.delete(params, callback). * @callback deleteUserCallback * @param {object} error - The error object. * @param {object} response - Empty body. */ var url; if (typeof params === 'number') { url = params; } else { if (params.external) { url = 'external/' + params.external; } } this.service.ajax({url: Utils.getUrl(config.urls.users, url), type: 'DELETE', dataType: 'text'}, callback); }, /** * You can initiate password resets for users who have emails associated with their account. Password reset instruction will be sent to this email address({@link https://docs.quickblox.com/docs/js-users#reset-user-password read more}). * @memberof QB.users * @param {string} email - The user's email to send reset password instruction. * @param {resetPasswordByEmailCallback} callback - The resetPasswordByEmailCallback function. */ resetPassword: function(email, callback) { /** * Callback for QB.users.resetPassword(email, callback). * @callback resetPasswordByEmailCallback * @param {object} error - The error object. * @param {object} response - Empty body. */ this.service.ajax({url: Utils.getUrl(resetPasswordUrl), data: {email: email}, dataType: 'text'}, callback); } }; module.exports = UsersProxy; /* Private ---------------------------------------------------------------------- */ function generateFilter(obj) { var type = obj.field in DATE_FIELDS ? 'date' : typeof obj.value; if (Utils.isArray(obj.value)) { if (type === 'object') { type = typeof obj.value[0]; } obj.value = obj.value.toString(); } return [type, obj.field, obj.param, obj.value].join(' '); } function generateOrder(obj) { var type = obj.field in DATE_FIELDS ? 'date' : obj.field in NUMBER_FIELDS ? 'number' : 'string'; return [obj.sort, type, obj.field].join(' '); } },{"../qbConfig":252,"../qbUtils":256}],244:[function(require,module,exports){ 'use strict'; /** * QuickBlox JavaScript SDK * WebRTC Module (WebRTC peer connection model) */ /** Modules */ var config = require('../../qbConfig'); var Helpers = require('./qbWebRTCHelpers'); /** * @namespace QB.webrtc.RTCPeerConnection */ /** * @function qbRTCPeerConnection * @memberOf QB.webrtc.RTCPeerConnection * @param {RTCConfiguration} [config] */ var qbRTCPeerConnection = function qbRTCPeerConnection(config) { this._pc = new window.RTCPeerConnection(config); this.remoteStream = undefined; this.preferredCodec = 'VP8'; }; qbRTCPeerConnection.State = { NEW: 1, CONNECTING: 2, CHECKING: 3, CONNECTED: 4, DISCONNECTED: 5, FAILED: 6, CLOSED: 7, COMPLETED: 8 }; qbRTCPeerConnection.prototype._init = function (delegate, userID, sessionID, polite) { Helpers.trace('RTCPeerConnection init.', 'userID: ', userID, ', sessionID: ', sessionID, ', polite: ', polite ); this.delegate = delegate; this.localIceCandidates = []; this.remoteIceCandidates = []; /** @type {RTCSessionDescriptionInit|undefined} */ this.remoteSDP = undefined; this.sessionID = sessionID; this.polite = polite; this.userID = userID; this._reconnecting = false; this.state = qbRTCPeerConnection.State.NEW; this._pc.onicecandidate = this.onIceCandidateCallback.bind(this); this._pc.onsignalingstatechange = this.onSignalingStateCallback.bind(this); this._pc.oniceconnectionstatechange = this.onIceConnectionStateCallback.bind(this); this._pc.ontrack = this.onTrackCallback.bind(this); /** * We use this timer to dial a user - * produce the call requests each N seconds. */ this.dialingTimer = null; /** We use this timer to wait for answer from callee */ this.answerTimeInterval = 0; this.statsReportTimer = null; }; qbRTCPeerConnection.prototype.release = function () { this._clearDialingTimer(); this._clearStatsReportTimer(); this._pc.close(); this.state = qbRTCPeerConnection.State.CLOSED; this._pc.onicecandidate = null; this._pc.onsignalingstatechange = null; this._pc.oniceconnectionstatechange = null; this._pc.ontrack = null; if (navigator.userAgent.includes("Edge")) { this.connectionState = 'closed'; this.iceConnectionState = 'closed'; } }; qbRTCPeerConnection.prototype.negotiate = function () { var self = this; return this.setLocalSessionDescription({ type: 'offer', options: { iceRestart: true } }, function (error) { if (error) { return Helpers.traceError("Error in 'negotiate': " + error); } var description = self._pc.localDescription.toJSON(); self.delegate.update({ reason: 'reconnect', sessionDescription: { offerId: self.offerId, sdp: description.sdp, type: description.type } }, self.userID); }); }; /** * Save remote SDP for future use. * @function setRemoteSDP * @memberOf QB.webrtc.RTCPeerConnection * @param {RTCSessionDescriptionInit} newSDP */ qbRTCPeerConnection.prototype.setRemoteSDP = function (newSDP) { if (!newSDP) { throw new Error("sdp string can't be empty."); } else { this.remoteSDP = newSDP; } }; /** * Returns SDP if it was set previously. * @function getRemoteSDP * @memberOf QB.webrtc.RTCPeerConnection * @returns {RTCSessionDescriptionInit|undefined} */ qbRTCPeerConnection.prototype.getRemoteSDP = function () { return this.remoteSDP; }; /** * Create offer or answer SDP and set as local description. * @function setLocalSessionDescription * @memberOf QB.webrtc.RTCPeerConnection * @param {Object} params * @param {'answer'|'offer'} params.type * @param {RTCOfferOptions} [params.options] * @param {Function} callback */ qbRTCPeerConnection.prototype.setLocalSessionDescription = function (params, callback) { var self = this; self.state = qbRTCPeerConnection.State.CONNECTING; var supportsSetCodecPreferences = window.RTCRtpTransceiver && 'setCodecPreferences' in window.RTCRtpTransceiver.prototype && Boolean(Helpers.getVersionSafari()); if (supportsSetCodecPreferences) { self._pc.getTransceivers().forEach(function (transceiver) { var kind = transceiver.sender.track.kind; var sendCodecs = window.RTCRtpSender.getCapabilities(kind).codecs; var recvCodecs = window.RTCRtpReceiver.getCapabilities(kind).codecs; if (kind === 'video') { var preferredCodecSendIndex = sendCodecs.findIndex(function(codec) { return codec .mimeType .toLowerCase() .includes(self.preferredCodec.toLowerCase()); }); if (preferredCodecSendIndex !== -1) { var arrayWithPreferredSendCodec = sendCodecs.splice( preferredCodecSendIndex, 1 ); sendCodecs.unshift(arrayWithPreferredSendCodec[0]); } var preferredCodecRecvIndex = recvCodecs.findIndex(function(codec) { return codec .mimeType .toLowerCase() .includes(self.preferredCodec.toLowerCase()); }); if (preferredCodecRecvIndex !== -1) { var arrayWithPreferredRecvCodec = recvCodecs.splice( preferredCodecRecvIndex, 1 ); recvCodecs.unshift(arrayWithPreferredRecvCodec[0]); } transceiver.setCodecPreferences(sendCodecs.concat(recvCodecs)); } }); } /** * @param {RTCSessionDescriptionInit} description */ function successCallback(description) { var modifiedDescription = _removeExtmapMixedFromSDP(description); modifiedDescription.sdp = setPreferredCodec( modifiedDescription.sdp, 'video', self.preferredCodec ); if (self.delegate.bandwidth) { modifiedDescription.sdp = setMediaBitrate( modifiedDescription.sdp, 'video', self.delegate.bandwidth ); } self._pc.setLocalDescription(modifiedDescription) .then(function () { callback(null); }). catch(function (error) { Helpers.traceError( "Error in 'setLocalSessionDescription': " + error ); callback(error); }); } if (params.type === 'answer') { this._pc.createAnswer(params.options) .then(successCallback) .catch(callback); } else { this._pc.createOffer(params.options) .then(successCallback) .catch(callback); } }; /** * Set remote session description. * @function setRemoteSessionDescription * @memberOf QB.webrtc.RTCPeerConnection * @param {RTCSessionDescriptionInit} description * @param {Function} callback * @returns {void} */ qbRTCPeerConnection.prototype.setRemoteSessionDescription = function ( description, callback ) { var modifiedSDP = this.delegate.bandwidth ? { type: description.type, sdp: setMediaBitrate(description.sdp, 'video', this.delegate.bandwidth) } : description; this._pc.setRemoteDescription(modifiedSDP) .then(function () { callback(); }) .catch(function (error) { Helpers.traceError( "Error in 'setRemoteSessionDescription': " + error ); callback(error); }); }; /** * Add local stream. * @function addLocalStream * @memberOf QB.webrtc.RTCPeerConnection * @param {MediaStream} stream */ qbRTCPeerConnection.prototype.addLocalStream = function (stream) { if (!stream) { throw new Error("'qbRTCPeerConnection.addLocalStream' error: stream is not defined"); } var self = this; stream.getTracks().forEach(function (track) { self._pc.addTrack(track, stream); }); }; /** * Add ice candidate. * @function _addIceCandidate * @memberOf QB.webrtc.RTCPeerConnection * @param {RTCIceCandidateInit} iceCandidate * @returns {Promise} */ qbRTCPeerConnection.prototype._addIceCandidate = function (iceCandidate) { return this._pc.addIceCandidate(iceCandidate).catch(function (error) { Helpers.traceError("Error on 'addIceCandidate': " + error); }); }; /** * Add ice candidates. * @function addCandidates * @memberOf QB.webrtc.RTCPeerConnection * @param {Array} iceCandidates */ qbRTCPeerConnection.prototype.addCandidates = function (iceCandidates) { var self = this; iceCandidates.forEach(function (candidate) { self.remoteIceCandidates.push(candidate); }); if (this._pc.remoteDescription) { self.remoteIceCandidates.forEach(function (candidate) { self._addIceCandidate(candidate); }); } }; qbRTCPeerConnection.prototype.toString = function sessionToString() { return ( 'sessionID: ' + this.sessionID + ', userID: ' + this.userID + ', state: ' + this.state ); }; /** * CALLBACKS */ qbRTCPeerConnection.prototype.onSignalingStateCallback = function () { if (this._pc.signalingState === 'stable' && this.localIceCandidates.length > 0) { this.delegate.processIceCandidates(this, this.localIceCandidates); while (this.localIceCandidates.length) { this.localIceCandidates.pop(); } } }; /** * @param {RTCPeerConnectionIceEvent} event */ qbRTCPeerConnection.prototype.onIceCandidateCallback = function (event) { if (event.candidate) { var candidate = { candidate: event.candidate.candidate, sdpMid: event.candidate.sdpMid, sdpMLineIndex: event.candidate.sdpMLineIndex }; if (this._pc.signalingState === 'stable') { this.delegate.processIceCandidates(this, [candidate]); } else { // collecting internally the ice candidates // will send a bit later this.localIceCandidates.push(candidate); } } }; /** * Handler of remote media track event * @param {RTCTrackEvent} event */ qbRTCPeerConnection.prototype.onTrackCallback = function (event) { this.remoteStream = event.streams[0]; if (typeof this.delegate._onRemoteStreamListener === 'function' && ['connected', 'completed'].includes(this._pc.iceConnectionState)) { this.delegate._onRemoteStreamListener(this.userID, this.remoteStream); } this._getStatsWrap(); }; qbRTCPeerConnection.prototype.onIceConnectionStateCallback = function () { Helpers.trace("onIceConnectionStateCallback: " + this._pc.iceConnectionState); var connectionState = null; switch (this._pc.iceConnectionState) { case 'checking': this.state = qbRTCPeerConnection.State.CHECKING; connectionState = Helpers.SessionConnectionState.CONNECTING; break; case 'connected': if (this._reconnecting) { this.delegate._stopReconnectTimer(this.userID); } this.state = qbRTCPeerConnection.State.CONNECTED; connectionState = Helpers.SessionConnectionState.CONNECTED; break; case 'completed': if (this._reconnecting) { this.delegate._stopReconnectTimer(this.userID); } this.state = qbRTCPeerConnection.State.COMPLETED; connectionState = Helpers.SessionConnectionState.COMPLETED; break; case 'failed': this.delegate._startReconnectTimer(this.userID); this.state = qbRTCPeerConnection.State.FAILED; connectionState = Helpers.SessionConnectionState.FAILED; break; case 'disconnected': this.delegate._startReconnectTimer(this.userID); this.state = qbRTCPeerConnection.State.DISCONNECTED; connectionState = Helpers.SessionConnectionState.DISCONNECTED; break; // TODO: this state doesn't fires on Safari 11 case 'closed': this.delegate._stopReconnectTimer(this.userID); this.state = qbRTCPeerConnection.State.CLOSED; connectionState = Helpers.SessionConnectionState.CLOSED; break; default: break; } if (typeof this.delegate ._onSessionConnectionStateChangedListener === 'function' && connectionState) { this.delegate._onSessionConnectionStateChangedListener( this.userID, connectionState ); } if (this._pc.iceConnectionState === 'connected' && typeof this.delegate._onRemoteStreamListener === 'function') { this.delegate._onRemoteStreamListener(this.userID, this.remoteStream); } }; /** * PRIVATE */ qbRTCPeerConnection.prototype._clearStatsReportTimer = function () { if (this.statsReportTimer) { clearInterval(this.statsReportTimer); this.statsReportTimer = null; } }; qbRTCPeerConnection.prototype._getStatsWrap = function () { var self = this, statsReportInterval, lastResult; if (config.webrtc && config.webrtc.statsReportTimeInterval && !self.statsReportTimer) { if (isNaN(+config.webrtc.statsReportTimeInterval)) { Helpers.traceError('statsReportTimeInterval (' + config.webrtc.statsReportTimeInterval + ') must be integer.'); return; } statsReportInterval = config.webrtc.statsReportTimeInterval * 1000; var _statsReportCallback = function () { _getStats(self._pc, lastResult, function (results, lastResults) { lastResult = lastResults; self.delegate._onCallStatsReport(self.userID, results, null); }, function errorLog(err) { Helpers.traceError('_getStats error. ' + err.name + ': ' + err.message); self.delegate._onCallStatsReport(self.userID, null, err); }); }; Helpers.trace('Stats tracker has been started.'); self.statsReportTimer = setInterval(_statsReportCallback, statsReportInterval); } }; qbRTCPeerConnection.prototype._clearDialingTimer = function () { if (this.dialingTimer) { Helpers.trace('_clearDialingTimer'); clearInterval(this.dialingTimer); this.dialingTimer = null; this.answerTimeInterval = 0; } }; qbRTCPeerConnection.prototype._startDialingTimer = function (extension) { var self = this; var dialingTimeInterval = config.webrtc.dialingTimeInterval * 1000; Helpers.trace('_startDialingTimer, dialingTimeInterval: ' + dialingTimeInterval); var _dialingCallback = function (extension, skipIncrement) { if (!skipIncrement) { self.answerTimeInterval += dialingTimeInterval; } Helpers.trace('_dialingCallback, answerTimeInterval: ' + self.answerTimeInterval); if (self.answerTimeInterval >= config.webrtc.answerTimeInterval * 1000) { self._clearDialingTimer(); self.delegate.processOnNotAnswer(self); } else { self.delegate.processCall(self, extension); } }; self.dialingTimer = setInterval( _dialingCallback, dialingTimeInterval, extension, false ); // call for the 1st time _dialingCallback(extension, true); }; /** * PRIVATE */ function _getStats(peer, lastResults, successCallback, errorCallback) { var statistic = { 'local': { 'audio': {}, 'video': {}, 'candidate': {} }, 'remote': { 'audio': {}, 'video': {}, 'candidate': {} } }; if (Helpers.getVersionFirefox()) { var localStream = peer.getLocalStreams().length ? peer.getLocalStreams()[0] : peer.delegate.localStream, localVideoSettings = localStream.getVideoTracks().length ? localStream.getVideoTracks()[0].getSettings() : null; statistic.local.video.frameHeight = localVideoSettings && localVideoSettings.height; statistic.local.video.frameWidth = localVideoSettings && localVideoSettings.width; } peer.getStats(null).then(function (results) { results.forEach(function (result) { var item; if (result.bytesReceived && result.type === 'inbound-rtp') { item = statistic.remote[result.mediaType]; item.bitrate = _getBitratePerSecond(result, lastResults, false); item.bytesReceived = result.bytesReceived; item.packetsReceived = result.packetsReceived; item.timestamp = result.timestamp; if (result.mediaType === 'video' && result.framerateMean) { item.framesPerSecond = Math.round(result.framerateMean * 10) / 10; } } else if (result.bytesSent && result.type === 'outbound-rtp') { item = statistic.local[result.mediaType]; item.bitrate = _getBitratePerSecond(result, lastResults, true); item.bytesSent = result.bytesSent; item.packetsSent = result.packetsSent; item.timestamp = result.timestamp; if (result.mediaType === 'video' && result.framerateMean) { item.framesPerSecond = Math.round(result.framerateMean * 10) / 10; } } else if (result.type === 'local-candidate') { item = statistic.local.candidate; if (result.candidateType === 'host' && result.mozLocalTransport === 'udp' && result.transport === 'udp') { item.protocol = result.transport; item.ip = result.ipAddress; item.port = result.portNumber; } else if (!Helpers.getVersionFirefox()) { item.protocol = result.protocol; item.ip = result.ip; item.port = result.port; } } else if (result.type === 'remote-candidate') { item = statistic.remote.candidate; item.protocol = result.protocol || result.transport; item.ip = result.ip || result.ipAddress; item.port = result.port || result.portNumber; } else if (result.type === 'track' && result.kind === 'video' && !Helpers.getVersionFirefox()) { if (result.remoteSource) { item = statistic.remote.video; item.frameHeight = result.frameHeight; item.frameWidth = result.frameWidth; item.framesPerSecond = _getFramesPerSecond(result, lastResults, false); } else { item = statistic.local.video; item.frameHeight = result.frameHeight; item.frameWidth = result.frameWidth; item.framesPerSecond = _getFramesPerSecond(result, lastResults, true); } } }); successCallback(statistic, results); }, errorCallback); function _getBitratePerSecond(result, lastResults, isLocal) { var lastResult = lastResults && lastResults.get(result.id), seconds = lastResult ? ((result.timestamp - lastResult.timestamp) / 1000) : 5, kilo = 1024, bit = 8, bitrate; if (!lastResult) { bitrate = 0; } else if (isLocal) { bitrate = bit * (result.bytesSent - lastResult.bytesSent) / (kilo * seconds); } else { bitrate = bit * (result.bytesReceived - lastResult.bytesReceived) / (kilo * seconds); } return Math.round(bitrate); } function _getFramesPerSecond(result, lastResults, isLocal) { var lastResult = lastResults && lastResults.get(result.id), seconds = lastResult ? ((result.timestamp - lastResult.timestamp) / 1000) : 5, framesPerSecond; if (!lastResult) { framesPerSecond = 0; } else if (isLocal) { framesPerSecond = (result.framesSent - lastResult.framesSent) / seconds; } else { framesPerSecond = (result.framesReceived - lastResult.framesReceived) / seconds; } return Math.round(framesPerSecond * 10) / 10; } } // Find the line in sdpLines[startLine...endLine - 1] that starts with |prefix| // and, if specified, contains |substr| (case-insensitive search). function findLineInRange( sdpLines, startLine, endLine, prefix, substr, direction ) { if (direction === undefined) { direction = 'asc'; } direction = direction || 'asc'; if (direction === 'asc') { // Search beginning to end var realEndLine = endLine !== -1 ? endLine : sdpLines.length; for (var i = startLine; i < realEndLine; ++i) { if (sdpLines[i].indexOf(prefix) === 0) { if (!substr || sdpLines[i].toLowerCase().indexOf(substr.toLowerCase()) !== -1) { return i; } } } } else { // Search end to beginning var realStartLine = startLine !== -1 ? startLine : sdpLines.length-1; for (var j = realStartLine; j >= 0; --j) { if (sdpLines[j].indexOf(prefix) === 0) { if (!substr || sdpLines[j].toLowerCase().indexOf(substr.toLowerCase()) !== -1) { return j; } } } } return null; } /** * Gets the codec payload type from an a=rtpmap:X line. * @param {string} sdpLine * @returns {string|null} */ function getCodecPayloadTypeFromLine(sdpLine) { var pattern = new RegExp('a=rtpmap:(\\d+) [a-zA-Z0-9-]+\\/\\d+'); var result = sdpLine.match(pattern); return (result && result.length === 2) ? result[1] : null; } /** * Returns a new m= line with the specified codec as the first one. * @param {string} mLine * @param {string} payload * @returns {string} */ function setDefaultCodec(mLine, payload) { var elements = mLine.split(' '); // Just copy the first three parameters; codec order starts on fourth. var newLine = elements.slice(0, 3); // Put target payload first and copy in the rest. newLine.push(payload); for (var i = 3; i < elements.length; i++) { if (elements[i] !== payload) { newLine.push(elements[i]); } } return newLine.join(' '); } /** * Mangles SDP to put preferred codec at the beginning * @param {string} sdp * @param {'audio'|'video'} type * @param {string} codec VP9, VP8, H264, opus etc. * @returns {string} */ function setPreferredCodec(sdp, type, codec) { if (!codec) { return sdp; } var sdpLines = sdp.split('\r\n'); // Search for m line. var mLineIndex = findLineInRange(sdpLines, 0, -1, 'm=', type); if (mLineIndex === null) { return sdp; } // If the codec is available, set it as the default in m line. var payload = null; // Iterate through rtpmap enumerations to find all matching codec entries for (var i = sdpLines.length-1; i >= 0 ; --i) { // Finds first match in rtpmap var index = findLineInRange(sdpLines, i, 0, 'a=rtpmap', codec, 'desc'); if (index !== null) { // Skip all of the entries between i and index match i = index; payload = getCodecPayloadTypeFromLine(sdpLines[index]); if (payload) { // Move codec to top sdpLines[mLineIndex] = setDefaultCodec(sdpLines[mLineIndex], payload); } } else { // No match means we can break the loop break; } } sdp = sdpLines.join('\r\n'); return sdp; } /** * This is to fix error on legacy WebRTC implementations * @param {RTCSessionDescriptionInit} description */ function _removeExtmapMixedFromSDP(description) { if (description && description.sdp && description.sdp.indexOf('\na=extmap-allow-mixed') !== -1) { description.sdp = description.sdp .split('\n') .filter(function (line) { return line.trim() !== 'a=extmap-allow-mixed'; }) .join('\n'); } return description; } /** * @param {string} sdp * @param {'audio'|'video'} media * @param {number} [bitrate] * @returns {string} */ function setMediaBitrate(sdp, media, bitrate) { if (!bitrate) { return sdp .replace(/b=AS:.*\r\n/, '') .replace(/b=TIAS:.*\r\n/, ''); } var lines = sdp.split('\n'), line = -1, modifier = Helpers.getVersionFirefox() ? 'TIAS' : 'AS', amount = Helpers.getVersionFirefox() ? bitrate * 1024 : bitrate; for (var i = 0; i < lines.length; i++) { if (lines[i].indexOf("m=" + media) === 0) { line = i; break; } } if (line === -1) { return sdp; } line++; while (lines[line].indexOf('i=') === 0 || lines[line].indexOf('c=') === 0) { line++; } if (lines[line].indexOf('b') === 0) { lines[line] = 'b=' + modifier + ':' + amount; return lines.join('\n'); } var newLines = lines.slice(0, line); newLines.push('b=' + modifier + ':' + amount); newLines = newLines.concat(lines.slice(line, lines.length)); return newLines.join('\n'); } module.exports = qbRTCPeerConnection; },{"../../qbConfig":252,"./qbWebRTCHelpers":246}],245:[function(require,module,exports){ 'use strict'; /** * QuickBlox JavaScript SDK * WebRTC Module (WebRTC client) */ /** * @namespace QB.webrtc */ /* * User's callbacks (listener-functions): * - onCallListener(session, extension) * - onAcceptCallListener(session, userID, extension) * - onRejectCallListener(session, userID, extension) * - onStopCallListener(session, userID, extension) * - onUpdateCallListener(session, userID, extension) * - onInvalidEventsListener (state, session, userID, extension) * - onDevicesChangeListener() */ var WebRTCSession = require('./qbWebRTCSession'); var WebRTCSignalingProcessor = require('./qbWebRTCSignalingProcessor'); var WebRTCSignalingProvider = require('./qbWebRTCSignalingProvider'); var Helpers = require('./qbWebRTCHelpers'); var RTCPeerConnection = require('./qbRTCPeerConnection'); var SignalingConstants = require('./qbWebRTCSignalingConstants'); var Utils = require('../../qbUtils'); var config = require('../../qbConfig'); var LiveSessionStates = [ WebRTCSession.State.NEW, WebRTCSession.State.CONNECTING, WebRTCSession.State.ACTIVE ]; function WebRTCClient(service, chat) { this.chat = chat; this.signalingProcessor = new WebRTCSignalingProcessor(service, this); this.signalingProvider = new WebRTCSignalingProvider(service, chat); chat.webrtcSignalingProcessor = this.signalingProcessor; this.SessionConnectionState = Helpers.SessionConnectionState; this.CallType = Helpers.CallType; this.PeerConnectionState = RTCPeerConnection.State; this.sessions = {}; if (navigator.mediaDevices && 'ondevicechange' in navigator.mediaDevices) { navigator.mediaDevices.ondevicechange = this._onDevicesChangeListener.bind(this); } } /** * Return data or all active devices. * @function getMediaDevices * @memberof QB.webrtc * @param {String} spec - Specify what type of devices you wnat to get. * Possible values: audioinput, audiooutput, videoinput. * @returns {Array} Array of devices. */ WebRTCClient.prototype.getMediaDevices = function(spec) { var specDevices = [], errMsg = 'Browser does not support output device selection.'; return new Promise(function(resolve, reject) { if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) { reject(errMsg); Helpers.traceWarning(errMsg); } else { navigator.mediaDevices.enumerateDevices().then(function(devices) { if(spec) { devices.forEach(function(device, i) { if(device.kind === spec) { specDevices.push(device); } }); resolve(specDevices); } else { resolve(devices); } }); } }); }; /** * A map with all sessions the user had/have. * @member {Object.} sessions * @memberof QB.webrtc */ WebRTCClient.prototype.sessions = {}; /** * Creates the new session({@link https://docs.quickblox.com/docs/js-video-calling#create-session read more}). * @function createNewSession * @memberof QB.webrtc * @param {Array} opponentsIDs - Opponents IDs. * @param {1|2} ct - Call type. * @param {number} [cID=yourUserId] - Initiator ID. * @param {object} [opts] * @param {number} [opts.bandwidth=0] - Bandwidth limit (kbps). * * @returns {WebRTCSession} */ WebRTCClient.prototype.createNewSession = function(opponentsIDs, ct, cID, opts) { var opponentsIdNASessions = getOpponentsIdNASessions(this.sessions), callerID = cID || Helpers.getIdFromNode(this.chat.connection.jid), bandwidth = opts && opts.bandwidth && (!isNaN(opts.bandwidth)) ? +opts.bandwidth : 0, isIdentifyOpponents = false, callType = ct || 2; if (!opponentsIDs) { throw new Error('Can\'t create a session without the opponentsIDs.'); } if (!Array.isArray(opponentsIDs)) { throw new Error('"opponentsIDs" should be Array of numbers'); } opponentsIDs.forEach(function (id) { var value = parseInt(id); if (isNaN(value)) { throw new Error('"opponentsIDs" should be Array of numbers'); } id = value; }); isIdentifyOpponents = isOpponentsEqual(opponentsIdNASessions, opponentsIDs); if (!isIdentifyOpponents) { return this._createAndStoreSession(null, callerID, opponentsIDs, callType, bandwidth); } else { throw new Error('Can\'t create a session with the same opponentsIDs. There is a session already in NEW or ACTIVE state.'); } }; WebRTCClient.prototype._createAndStoreSession = function(sessionID, callerID, opponentsIDs, callType, bandwidth) { var newSession = new WebRTCSession({ sessionID: sessionID, initiatorID: callerID, opIDs: opponentsIDs, callType: callType, signalingProvider: this.signalingProvider, currentUserID: Helpers.getIdFromNode(this.chat.connection.jid), bandwidth: bandwidth }); /** set callbacks */ newSession.onUserNotAnswerListener = this.onUserNotAnswerListener; newSession.onReconnectListener = this.onReconnectListener; newSession.onRemoteStreamListener = this.onRemoteStreamListener; newSession.onSessionConnectionStateChangedListener = this.onSessionConnectionStateChangedListener; newSession.onSessionCloseListener = this.onSessionCloseListener; newSession.onCallStatsReport = this.onCallStatsReport; this.sessions[newSession.ID] = newSession; return newSession; }; /** * Deletes a session. * @function clearSession * @memberof QB.webrtc * @param {string} sessionId - Session id. * */ WebRTCClient.prototype.clearSession = function(sessionId) { delete WebRTCClient.sessions[sessionId]; }; /** * Check all session and find session with status 'NEW' or 'ACTIVE' which ID != provided. * @param {string} sessionID - Session id. * @returns {boolean} True if active or new session exist. */ WebRTCClient.prototype.isExistLiveSessionExceptSessionID = function(sessionID) { var exist = false; var sessions = this.sessions; if (Object.keys(sessions).length > 0) { exist = Object.keys(sessions).some(function (key) { var session = sessions[key]; var sessionActive = LiveSessionStates.includes(session.state); return sessionActive && session.ID !== sessionID; }); } return exist; }; WebRTCClient.prototype.getNewSessionsCount = function (exceptId) { var sessions = this.sessions; return Object.keys(sessions).reduce(function (count, sessionId) { var session = sessions[sessionId]; if (session.ID === exceptId) { return count; } var sessionActive = ( session.state === WebRTCSession.State.NEW ); return sessionActive ? count + 1 : count; }, 0); }; /** * DELEGATE (signaling) */ WebRTCClient.prototype._onCallListener = function(userID, sessionID, extension) { var userInfo = extension.userInfo || {}; var currentUserID = Helpers.getIdFromNode(this.chat.connection.jid); if (userID === currentUserID && !extension.opponentsIDs.includes(currentUserID)) { Helpers.trace( 'Ignore "onCall" signal from current user.' + ' userID:' + userID + ', sessionID: ' + sessionID ); return; } Helpers.trace("onCall. UserID:" + userID + ". SessionID: " + sessionID); var otherActiveSessions = this.isExistLiveSessionExceptSessionID(sessionID); var newSessionsCount = this.getNewSessionsCount(sessionID); if (otherActiveSessions && !this.sessions[sessionID]) { newSessionsCount++; } var reject = ( otherActiveSessions && (config.webrtc.autoReject || newSessionsCount > config.webrtc.incomingLimit) ); if (reject) { Helpers.trace('User with id ' + userID + ' is busy at the moment.'); delete extension.sessionDescription; delete extension.platform; extension.sessionID = sessionID; this.signalingProvider.sendMessage(userID, extension, SignalingConstants.SignalingType.REJECT); if (typeof this.onInvalidEventsListener === 'function'){ Utils.safeCallbackCall(this.onInvalidEventsListener, 'onCall', sessionID, userID, userInfo); } } else { var session = this.sessions[sessionID], bandwidth = +userInfo.bandwidth || 0; if (!session) { session = this._createAndStoreSession( sessionID, extension.callerID, extension.opponentsIDs, extension.callType, bandwidth ); if (typeof this.onCallListener === 'function') { Utils.safeCallbackCall(this.onCallListener, session, userInfo); } } session.processOnCall(userID, extension); } }; WebRTCClient.prototype._onAcceptListener = function(userID, sessionID, extension) { var session = this.sessions[sessionID], userInfo = extension.userInfo || {}; Helpers.trace("onAccept. UserID:" + userID + ". SessionID: " + sessionID); if (session) { if (session.state === WebRTCSession.State.CONNECTING || session.state === WebRTCSession.State.ACTIVE) { if (typeof this.onAcceptCallListener === 'function') { Utils.safeCallbackCall(this.onAcceptCallListener, session, userID, userInfo); } session.processOnAccept(userID, extension); } else { if (typeof this.onInvalidEventsListener === 'function'){ Utils.safeCallbackCall(this.onInvalidEventsListener, 'onAccept', session, userID, userInfo); } Helpers.traceWarning("Ignore 'onAccept', the session( " + sessionID + " ) has invalid state."); } } else { Helpers.traceError("Ignore 'onAccept', there is no information about session " + sessionID + " by some reason."); } }; WebRTCClient.prototype._onRejectListener = function(userID, sessionID, extension) { var that = this, session = that.sessions[sessionID]; Helpers.trace("onReject. UserID:" + userID + ". SessionID: " + sessionID); if (session) { var userInfo = extension.userInfo || {}; if (typeof this.onRejectCallListener === 'function') { Utils.safeCallbackCall(that.onRejectCallListener, session, userID, userInfo); } session.processOnReject(userID, extension); } else { Helpers.traceError("Ignore 'onReject', there is no information about session " + sessionID + " by some reason."); } }; WebRTCClient.prototype._onStopListener = function(userID, sessionID, extension) { Helpers.trace("onStop. UserID:" + userID + ". SessionID: " + sessionID); var session = this.sessions[sessionID], userInfo = extension.userInfo || {}; if (session && LiveSessionStates.includes(session.state)) { if (typeof this.onStopCallListener === 'function') { Utils.safeCallbackCall(this.onStopCallListener, session, userID, userInfo); } // Need to make this asynchronously, to keep the strophe handler alive setTimeout(session.processOnStop.bind(session), 10, userID, extension); } else { if (typeof this.onInvalidEventsListener === 'function'){ Utils.safeCallbackCall(this.onInvalidEventsListener, 'onStop', session, userID, userInfo); } Helpers.traceError("Ignore 'onStop', there is no information about session " + sessionID + " by some reason."); } }; WebRTCClient.prototype._onIceCandidatesListener = function(userID, sessionID, extension) { var session = this.sessions[sessionID]; Helpers.trace("onIceCandidates. UserID:" + userID + ". SessionID: " + sessionID + ". ICE candidates count: " + extension.iceCandidates.length); if (session) { if (session.state === WebRTCSession.State.CONNECTING || session.state === WebRTCSession.State.ACTIVE) { session.processOnIceCandidates(userID, extension); } else { Helpers.traceWarning('Ignore \'OnIceCandidates\', the session ( ' + sessionID + ' ) has invalid state.'); } } else { Helpers.traceError("Ignore 'OnIceCandidates', there is no information about session " + sessionID + " by some reason."); } }; WebRTCClient.prototype._onUpdateListener = function(userID, sessionID, extension) { var session = this.sessions[sessionID], userInfo = extension.userInfo || {}; Helpers.trace("onUpdate. UserID:" + userID + ". SessionID: " + sessionID + ". Extension: " + JSON.stringify(userInfo)); if (session) { if (extension.reason) { session.processOnUpdate(userID, extension); } if (typeof this.onUpdateCallListener === 'function') { Utils.safeCallbackCall(this.onUpdateCallListener, session, userID, userInfo); } } }; WebRTCClient.prototype._onDevicesChangeListener = function() { if (typeof this.onDevicesChangeListener === 'function') { Utils.safeCallbackCall(this.onDevicesChangeListener); } }; module.exports = WebRTCClient; /** * PRIVATE FUNCTIONS */ function isOpponentsEqual(exOpponents, currentOpponents) { var ans = false, cOpponents = currentOpponents.sort(); if (exOpponents.length) { exOpponents.forEach(function(i) { var array = i.sort(); ans = (array.length == cOpponents.length) && array.every(function(el, index) { return el === cOpponents[index]; }); }); } return ans; } function getOpponentsIdNASessions(sessions) { var opponents = []; if (Object.keys(sessions).length > 0) { Object.keys(sessions).forEach(function(key, i, arr) { var session = sessions[key]; if (session.state === WebRTCSession.State.NEW || session.state === WebRTCSession.State.ACTIVE) { opponents.push(session.opponentsIDs); } }); } return opponents; } },{"../../qbConfig":252,"../../qbUtils":256,"./qbRTCPeerConnection":244,"./qbWebRTCHelpers":246,"./qbWebRTCSession":247,"./qbWebRTCSignalingConstants":248,"./qbWebRTCSignalingProcessor":249,"./qbWebRTCSignalingProvider":250}],246:[function(require,module,exports){ 'use strict'; /** * QuickBlox JavaScript SDK * WebRTC Module (WebRTC helpers) */ var config = require('../../qbConfig'); var WebRTCHelpers = { getUserJid: function(id, appId) { return id + '-' + appId + '@' + config.endpoints.chat; }, getIdFromNode: function(jid) { if (jid.indexOf('@') < 0) return null; return parseInt(jid.split('@')[0].split('-')[0]); }, trace: function(text) { if (config.debug) { console.log('[QBWebRTC]:', text); } }, traceWarning: function(text) { if (config.debug) { console.warn('[QBWebRTC]:', text); } }, traceError: function(text) { if (config.debug) { console.error('[QBWebRTC]:', text); } }, getLocalTime: function() { var arr = new Date().toString().split(' '); return arr.slice(1,5).join('-'); }, isIOS: function() { if (!window || !window.navigator || !window.navigator.userAgent) { return false; } var ua = window.navigator.userAgent; return Boolean(ua.match(/iP(ad|hone)/i)); }, isIOSSafari: function() { if (!window || !window.navigator || !window.navigator.userAgent) { return false; } var ua = window.navigator.userAgent; var iOS = Boolean(ua.match(/iP(ad|hone)/i)); var isWebkit = Boolean(ua.match(/WebKit/i)); var isChrome = Boolean(ua.match(/CriOS/i)); return iOS && isWebkit && !isChrome; }, isIOSChrome: function() { if (!window || !window.navigator || !window.navigator.userAgent) { return false; } var ua = window.navigator.userAgent; var iOS = Boolean(ua.match(/iP(ad|hone)/i)); var isWebkit = Boolean(ua.match(/WebKit/i)); var isChrome = Boolean(ua.match(/CriOS/i)); return iOS && !isWebkit && isChrome; }, getVersionFirefox: function() { var ua = navigator ? navigator.userAgent : false; var version; if (ua) { var ffInfo = ua.match(/(?:firefox)[ \/](\d+)/i) || []; version = ffInfo[1] ? + ffInfo[1] : null; } return version; }, getVersionSafari: function() { var ua = navigator ? navigator.userAgent : false; var version; if (ua) { var sInfo = ua.match(/(?:safari)[ \/](\d+)/i) || []; if (sInfo.length) { var sVer = ua.match(/(?:version)[ \/](\d+)/i) || []; if (sVer) { version = sVer[1] ? + sVer[1] : null; } else { version = null; } } else { version = null; } } return version; }, /** * Return a Promise that resolves after `timeout` milliseconds. * @param {number} [timeout=0] * @returns {Promise} */ delay: function(timeout) { timeout = typeof timeout === 'number' && timeout > 0 ? timeout : 0; return new Promise(function (resolve) { setTimeout(resolve, timeout); }); }, /** * [SessionConnectionState] * @enum {number} */ SessionConnectionState: { UNDEFINED: 0, CONNECTING: 1, CONNECTED: 2, FAILED: 3, DISCONNECTED: 4, CLOSED: 5, COMPLETED: 6 }, /** * [CallType] * @enum {number} */ CallType: { VIDEO: 1, AUDIO: 2 }, }; module.exports = WebRTCHelpers; },{"../../qbConfig":252}],247:[function(require,module,exports){ 'use strict'; /** * QuickBlox JavaScript SDK * WebRTC Module (WebRTC session model) */ /** * User's callbacks (listener-functions): * - onUserNotAnswerListener(session, userID) * - onRemoteStreamListener(session, userID, stream) * - onSessionConnectionStateChangedListener(session, userID, connectionState) * - onSessionCloseListener(session) * - onCallStatsReport(session, userId, stats, error) * - onReconnectListener(session, userId, state) */ /** * @namespace QB.webrtc.WebRTCSession */ /** * @typedef {Object} MediaParams * @property {boolean | MediaTrackConstraints} [params.audio] * @property {boolean | MediaTrackConstraints} [params.video] * @property {string} [params.elemId] - Id of HTMLVideoElement. * @property {Object} [params.options] * @property {boolean} [params.options.muted] * @property {boolean} [params.options.mirror] */ var config = require('../../qbConfig'); var qbRTCPeerConnection = require('./qbRTCPeerConnection'); var Utils = require('../../qbUtils'); var Helpers = require('./qbWebRTCHelpers'); var SignalingConstants = require('./qbWebRTCSignalingConstants'); var ICE_TIMEOUT = 5000; // 5 seconds /** * State of a session */ WebRTCSession.State = { NEW: 1, ACTIVE: 2, HUNGUP: 3, REJECTED: 4, CLOSED: 5 }; var ReconnectionState = { RECONNECTING: 'reconnecting', RECONNECTED: 'reconnected', FAILED: 'failed' }; /** * QuickBlox WebRTC session. * @param {Object} params * @param {1|2} params.callType - Type of a call * 1 - VIDEO * 2 - AUDIO * @param {Array} params.opIDs - An array with opponents. * @param {number} params.currentUserID - Current user ID. * @param {number} params.initiatorID - Call initiator ID. * @param {string} [params.sessionID] - Session identifier (optional). * @param {number} [params.bandwidth] - Bandwidth limit. */ function WebRTCSession(params) { this.ID = params.sessionID ? params.sessionID : generateUUID(); this.state = WebRTCSession.State.NEW; this.initiatorID = parseInt(params.initiatorID); this.opponentsIDs = params.opIDs; this.callType = parseInt(params.callType); /*** @type {{[userId: number]: qbRTCPeerConnection}} */ this.peerConnections = {}; /*** @type {MediaParams} */ this.mediaParams = null; /*** @type {{[userID: number]: number | undefined}} */ this.iceConnectTimers = {}; /*** @type {{[userID: number]: number | undefined}} */ this.reconnectTimers = {}; this.signalingProvider = params.signalingProvider; this.currentUserID = params.currentUserID; this.bandwidth = params.bandwidth; /*** * We use this timeout to fix next issue: * "From Android/iOS make a call to Web and kill the Android/iOS app instantly. Web accept/reject popup will be still visible. * We need a way to hide it if sach situation happened." */ this.answerTimer = null; this.startCallTime = 0; this.acceptCallTime = 0; /*** @type {MediaStream | undefined} */ this.localStream = undefined; } /** * Get the user media stream({@link https://docs.quickblox.com/docs/js-video-calling#access-local-media-stream read more}). * @function getUserMedia * @memberof QB.webrtc.WebRTCSession * @param {MediaParams} params - Media stream constraints and additional options. * @param {Function} callback - Callback to get a result of the function. */ WebRTCSession.prototype.getUserMedia = function (params, callback) { if (!navigator.mediaDevices.getUserMedia) { throw new Error('getUserMedia() is not supported in your browser'); } var self = this; var mediaConstraints = { audio: params.audio || false, video: params.video || false }; function successCallback(stream) { self.localStream = stream; self.mediaParams = Object.assign({}, params); if (params.elemId) { self.attachMediaStream(params.elemId, stream, params.options); } if (callback && typeof callback === 'function') { callback(undefined, stream); } } navigator .mediaDevices .getUserMedia(mediaConstraints) .then(successCallback) .catch(callback); }; /** * Get the state of connection. * @function connectionStateForUser * @memberof QB.webrtc.WebRTCSession * @param {number} userID */ WebRTCSession.prototype.connectionStateForUser = function (userID) { var peerConnection = this.peerConnections[userID]; return peerConnection ? peerConnection.state : undefined; }; /** * Attach media stream to audio/video element({@link https://docs.quickblox.com/docs/js-video-calling#attach-local-media-stream read more}). * @function attachMediaStream * @memberof QB.webrtc.WebRTCSession * @param {string} elementId - The Id of an ellement to attach a stream. * @param {MediaStream} stream - The stream to attach. * @param {Object} [options] - The additional options. * @param {boolean} [options.muted] - Whether video element should be muted. * @param {boolean} [options.mirror] - Whether video should be "mirrored". */ WebRTCSession.prototype.attachMediaStream = function (elementId, stream, options) { var elem = document.getElementById(elementId); if (elem) { if (elem instanceof HTMLMediaElement) { if ('srcObject' in elem) { elem.srcObject = stream; } else { elem.src = window.URL.createObjectURL(stream); } if (options && options.muted) { elem.muted = true; } if (options && options.mirror) { elem.style.transform = 'scaleX(-1)'; } if (!elem.autoplay) { elem.onloadedmetadata = function () { elem.play(); }; } } else { throw new Error('Cannot attach media stream to element with id "' + elementId + '" because it is not of type HTMLMediaElement'); } } else { throw new Error('Unable to attach media stream, cannot find element by Id "' + elementId + '"'); } }; /** * Detach media stream from audio/video element. * @function detachMediaStream * @memberof QB.webrtc.WebRTCSession * @param {string} elementId - The Id of an element to detach a stream. */ WebRTCSession.prototype.detachMediaStream = function (elementId) { var elem = document.getElementById(elementId); if (elem && elem instanceof HTMLMediaElement) { elem.pause(); if (elem.srcObject && typeof elem.srcObject === 'object') { elem.srcObject.getTracks().forEach(function (track) { track.stop(); track.enabled = false; }); elem.srcObject = null; } else { elem.src = ''; } elem.removeAttribute("src"); elem.removeAttribute("srcObject"); } }; /** * Switch media tracks in audio/video HTML's element and replace its in peers({@link https://docs.quickblox.com/docs/js-video-calling-advanced#switch-camera read more}). * @function switchMediaTracks * @memberof QB.webrtc.WebRTCSession * @param {Object} deviceIds - An object with deviceIds of plugged devices. * @param {string} [deviceIds.audio] - The deviceId, it can be gotten from QB.webrtc.getMediaDevices('audioinput'). * @param {string} [deviceIds.video] - The deviceId, it can be gotten from QB.webrtc.getMediaDevices('videoinput'). * @param {Function} callback - The callback to get a result of the function. */ WebRTCSession.prototype.switchMediaTracks = function (deviceIds, callback) { if (!navigator.mediaDevices.getUserMedia) { throw new Error('getUserMedia() is not supported in your browser'); } var self = this; if (deviceIds && deviceIds.audio) { this.mediaParams.audio.deviceId = deviceIds.audio; } if (deviceIds && deviceIds.video) { this.mediaParams.video.deviceId = deviceIds.video; } this.localStream.getTracks().forEach(function (track) { track.stop(); }); navigator.mediaDevices.getUserMedia({ audio: self.mediaParams.audio || false, video: self.mediaParams.video || false }).then(function (stream) { self._replaceTracks(stream); callback(null, stream); }).catch(function (error) { callback(error, null); }); }; WebRTCSession.prototype._replaceTracks = function (stream) { var localStream = this.localStream; var elemId = this.mediaParams.elemId; var ops = this.mediaParams.options; var currentStreamTracks = localStream.getTracks(); var newStreamTracks = stream.getTracks(); this.detachMediaStream(elemId); newStreamTracks.forEach(function (newTrack) { const currentTrack = currentStreamTracks.find(function (track) { return track.kind === newTrack.kind; }); if (currentTrack) { currentTrack.stop(); localStream.removeTrack(currentTrack); localStream.addTrack(newTrack); } }); if (elemId) { this.attachMediaStream(elemId, stream, ops); } /*** @param {RTCPeerConnection} peer */ function _replaceTracksForPeer(peer) { return Promise.all(peer.getSenders().map(function (sender) { return sender.replaceTrack(newStreamTracks.find(function (track) { return track.kind === sender.track.kind; })); })); } return Promise.all(Object .values(this.peerConnections) .map(function (peerConnection) { return peerConnection._pc; }) .map(_replaceTracksForPeer) ); }; /** * Initiate a call({@link https://docs.quickblox.com/docs/js-video-calling#make-a-call read more}). * @function call * @memberof QB.webrtc.WebRTCSession * @param {Object} [extension] - A map with a custom parametrs . * @param {Function} [callback] */ WebRTCSession.prototype.call = function (extension, callback) { var self = this, ext = _prepareExtension(extension); Helpers.trace('Call, extension: ' + JSON.stringify(ext.userInfo)); self.state = WebRTCSession.State.ACTIVE; // First this check if we connected to the signalling channel // to make sure that opponents will receive `call` signal self._reconnectToChat(function () { if (self.state === WebRTCSession.State.ACTIVE) { // create a peer connection for each opponent self.opponentsIDs.forEach(function (userID) { self._callInternal(userID, ext); }); } }); if (typeof callback === 'function') { callback(null); } }; WebRTCSession.prototype._callInternal = function (userID, extension) { var self = this; var peer = this._createPeer(userID, this.currentUserID < userID); this.peerConnections[userID] = peer; peer.addLocalStream(self.localStream); peer.setLocalSessionDescription({ type: 'offer' }, function (error) { if (error) { Helpers.trace("setLocalSessionDescription error: " + error); } else { Helpers.trace("setLocalSessionDescription success"); /** let's send call requests to user */ peer._startDialingTimer(extension); } }); }; /** * Accept a call({@link https://docs.quickblox.com/docs/js-video-calling#accept-a-call read more}). * @function accept * @memberof QB.webrtc.WebRTCSession * @param {Object} extension - A map with custom parameters. */ WebRTCSession.prototype.accept = function (extension) { var self = this, ext = _prepareExtension(extension); Helpers.trace('Accept, extension: ' + JSON.stringify(ext.userInfo)); if (self.state === WebRTCSession.State.ACTIVE) { Helpers.traceError("Can't accept, the session is already active, return."); return; } if (self.state === WebRTCSession.State.CLOSED) { Helpers.traceError("Can't accept, the session is already closed, return."); self.stop({}); return; } self.state = WebRTCSession.State.ACTIVE; self.acceptCallTime = new Date(); self._clearAnswerTimer(); self._acceptInternal(self.initiatorID, ext); /** The group call logic starts here */ var oppIDs = self._uniqueOpponentsIDsWithoutInitiator(); /** in a case of group video chat */ if (oppIDs.length > 0) { var offerTime = (self.acceptCallTime - self.startCallTime) / 1000; self._startWaitingOfferOrAnswerTimer(offerTime); /** * here we have to decide to which users the user should call. * We have a rule: If a userID1 > userID2 then a userID1 should call to userID2. */ oppIDs.forEach(function (opID, i, arr) { if (self.currentUserID > opID) { /** call to the user */ self._callInternal(opID, {}, true); } }); } }; WebRTCSession.prototype._acceptInternal = function (userID, extension) { var self = this; /** create a peer connection */ var peerConnection = this.peerConnections[userID]; if (peerConnection) { var remoteSDP = peerConnection.getRemoteSDP(); peerConnection.addLocalStream(self.localStream); peerConnection.setRemoteSessionDescription(remoteSDP, function (error) { if (error) { Helpers.traceError("'setRemoteSessionDescription' error: " + error); } else { Helpers.trace("'setRemoteSessionDescription' success"); peerConnection.setLocalSessionDescription({ type: 'answer' }, function (err) { if (err) { Helpers.trace("setLocalSessionDescription error: " + err); } else { Helpers.trace("'setLocalSessionDescription' success"); extension.sessionID = self.ID; extension.callType = self.callType; extension.callerID = self.initiatorID; extension.opponentsIDs = self.opponentsIDs; if (peerConnection._pc.localDescription) { extension.sdp = peerConnection ._pc .localDescription .toJSON() .sdp; } self.signalingProvider.sendMessage( userID, extension, SignalingConstants.SignalingType.ACCEPT ); } }); } }); } else { Helpers.traceError( "Can't accept the call, peer connection for userID " + userID + " was not found" ); } }; /** * Reject a call({@link https://docs.quickblox.com/docs/js-video-calling#reject-a-call read more}). * @function reject * @memberof QB.webrtc.WebRTCSession * @param {Object} extension - A map with custom parameters. */ WebRTCSession.prototype.reject = function (extension) { var self = this, ext = _prepareExtension(extension); Helpers.trace('Reject, extension: ' + JSON.stringify(ext.userInfo)); self.state = WebRTCSession.State.REJECTED; self._clearAnswerTimer(); ext.sessionID = self.ID; ext.callType = self.callType; ext.callerID = self.initiatorID; ext.opponentsIDs = self.opponentsIDs; Object.keys(self.peerConnections).forEach(function (key) { var peerConnection = self.peerConnections[key]; self.signalingProvider.sendMessage( peerConnection.userID, ext, SignalingConstants.SignalingType.REJECT ); }); self._close(); }; /** * Stop a call({@link https://docs.quickblox.com/docs/js-video-calling#end-a-call read more}). * @function stop * @memberof QB.webrtc.WebRTCSession * @param {Object} extension - A map with custom parameters. */ WebRTCSession.prototype.stop = function (extension) { var self = this, ext = _prepareExtension(extension); Helpers.trace('Stop, extension: ' + JSON.stringify(ext.userInfo)); self.state = WebRTCSession.State.HUNGUP; if (self.answerTimer) { self._clearAnswerTimer(); } ext.sessionID = self.ID; ext.callType = self.callType; ext.callerID = self.initiatorID; ext.opponentsIDs = self.opponentsIDs; Object.keys(self.peerConnections).forEach(function (key) { var peerConnection = self.peerConnections[key]; self.signalingProvider.sendMessage( peerConnection.userID, ext, SignalingConstants.SignalingType.STOP ); }); self._close(); }; /** * Close connection with a user. * @function closeConnection * @memberof QB.webrtc.WebRTCSession * @param {Number} userId - Id of a user. */ WebRTCSession.prototype.closeConnection = function (userId) { var self = this, peer = this.peerConnections[userId]; if (!peer) { Helpers.traceWarn('Not found connection with user (' + userId + ')'); return false; } try { peer.release(); } catch (e) { Helpers.traceError(e); } finally { self._closeSessionIfAllConnectionsClosed(); } }; /** * Update a call. * @function update * @memberof QB.webrtc.WebRTCSession * @param {Object} extension - A map with custom parameters. * @param {number} [userID] */ WebRTCSession.prototype.update = function (extension, userID) { var self = this, ext = typeof extension === 'object' ? extension : {}; Helpers.trace('Update, extension: ' + JSON.stringify(extension)); if (extension === null) { Helpers.trace("extension is null, no parameters to update"); return; } ext.sessionID = this.ID; ext.callType = this.callType; ext.callerID = this.initiatorID; ext.opponentsIDs = this.opponentsIDs; if (userID) { self.signalingProvider.sendMessage( userID, ext, SignalingConstants.SignalingType.PARAMETERS_CHANGED ); } else { for (var key in self.peerConnections) { var peer = self.peerConnections[key]; self.signalingProvider.sendMessage( peer.userID, ext, SignalingConstants.SignalingType.PARAMETERS_CHANGED ); } } }; /** * Mutes the stream({@link https://docs.quickblox.com/docs/js-video-calling-advanced#mute-audio read more}). * @function mute * @memberof QB.webrtc.WebRTCSession * @param {string} type - 'audio' or 'video' */ WebRTCSession.prototype.mute = function (type) { this._muteStream(0, type); }; /** * Unmutes the stream({@link https://docs.quickblox.com/docs/js-video-calling-advanced#mute-audio read more}). * @function unmute * @memberof QB.webrtc.WebRTCSession * @param {string} type - 'audio' or 'video' */ WebRTCSession.prototype.unmute = function (type) { this._muteStream(1, type); }; /** * DELEGATES (rtc client) */ WebRTCSession.prototype.processOnCall = function (callerID, extension) { var self = this, opponentsIds = self._uniqueOpponentsIDs(); opponentsIds.forEach(function (opponentID) { var peer = self.peerConnections[opponentID]; if (!peer) { /** create peer connections for each opponent */ peer = self._createPeer( opponentID, self.currentUserID < opponentID ); self.peerConnections[opponentID] = peer; if (opponentID == callerID) { self._startAnswerTimer(); } } if (opponentID == callerID) { peer.setRemoteSDP(new window.RTCSessionDescription({ sdp: extension.sdp, type: 'offer' })); /** The group call logic starts here */ if (callerID != self.initiatorID && self.state === WebRTCSession.State.ACTIVE) { self._acceptInternal(callerID, {}); } } }); }; WebRTCSession.prototype.processOnAccept = function (userID, extension) { var self = this; var peerConnection = this.peerConnections[userID]; if (peerConnection) { peerConnection._clearDialingTimer(); if (peerConnection._pc.signalingState !== 'stable') { var remoteSessionDescription = new window.RTCSessionDescription({ sdp: extension.sdp, type: 'answer' }); peerConnection.setRemoteSDP(remoteSessionDescription); peerConnection.setRemoteSessionDescription(remoteSessionDescription, function (error) { if (error) { Helpers.traceError("'setRemoteSessionDescription' error: " + error); } else { Helpers.trace("'setRemoteSessionDescription' success"); if (self.state !== WebRTCSession.State.ACTIVE) { self.state = WebRTCSession.State.ACTIVE; } } }); } else { Helpers.traceError("Ignore 'onAccept', PeerConnection is already in 'stable' state"); } } else { Helpers.traceError( "Ignore 'OnAccept': peer connection for user with Id " + userID + " was not found" ); } }; WebRTCSession.prototype.processOnReject = function (userID, extension) { var peerConnection = this.peerConnections[userID]; this._clearWaitingOfferOrAnswerTimer(); if (peerConnection) { peerConnection.release(); } else { Helpers.traceError("Ignore 'OnReject', there is no information about peer connection by some reason."); } this._closeSessionIfAllConnectionsClosed(); }; WebRTCSession.prototype.processOnStop = function (userID, extension) { var self = this; this._clearAnswerTimer(); var peerConnection = self.peerConnections[userID]; if (peerConnection) { peerConnection.release(); if (peerConnection._reconnecting) { peerConnection._reconnecting = false; } this._stopReconnectTimer(userID); } else { Helpers.traceError("Ignore 'OnStop', there is no information about peer connection by some reason."); } this._closeSessionIfAllConnectionsClosed(); }; WebRTCSession.prototype.processOnIceCandidates = function (userID, extension) { var peerConnection = this.peerConnections[userID]; if (peerConnection) { peerConnection.addCandidates(extension.iceCandidates); } else { Helpers.traceError("Ignore 'OnIceCandidates', there is no information about peer connection by some reason."); } }; WebRTCSession.prototype.processOnUpdate = function (userID, extension) { var SRD = extension.sessionDescription; var reason = extension.reason; var sessionIsActive = this.state === WebRTCSession.State.ACTIVE; if (sessionIsActive && reason && reason === 'reconnect') { var peer = this.peerConnections[userID]; if (peer) { if (SRD) { if (SRD.type === 'offer') { this._processReconnectOffer(userID, SRD); } else { this._processReconnectAnswer(userID, SRD); } } } else { Helpers.traceError( "Ignore 'OnUpdate': peer connection for user with Id " + userID + " was not found" ); } } }; /** * @param {number} userID * @param {RTCSessionDescriptionInit} SRD */ WebRTCSession.prototype._processReconnectOffer = function (userID, SRD) { var self = this; if (this.peerConnections[userID].polite) { this._reconnect(this.peerConnections[userID]); var peer = this.peerConnections[userID]; var offerId = SRD.offerId; var remoteDescription = new window.RTCSessionDescription({ sdp: SRD.sdp, type: SRD.type }); peer.setRemoteSDP(remoteDescription); peer.setRemoteSessionDescription(remoteDescription, function (e) { if (e) { Helpers.traceError('"setRemoteSessionDescription" error:' + e.message); } else { peer.setLocalSessionDescription({ type: 'answer' }, function () { var description = peer._pc.localDescription.toJSON(); var ext = { reason: 'reconnect', sessionDescription: { offerId: offerId, sdp: description.sdp, type: description.type } }; self.update(ext, userID); }); } }); } else { this._reconnect(this.peerConnections[userID], true); } }; /** * @param {number} userID * @param {RTCSessionDescriptionInit} SRD */ WebRTCSession.prototype._processReconnectAnswer = function (userID, SRD) { var peer = this.peerConnections[userID]; var offerId = SRD.offerId; if (peer && peer.offerId && offerId && peer.offerId === offerId) { var remoteDescription = new window.RTCSessionDescription({ sdp: SRD.sdp, type: SRD.type }); peer.setRemoteSDP(remoteDescription); peer.setRemoteSessionDescription(remoteDescription, function (e) { if (e) { Helpers.traceError('"setRemoteSessionDescription" error:' + e.message); } }); } }; /** * Send "call" signal to the opponent * @param {qbRTCPeerConnection} peerConnection * @param {Object} ext */ WebRTCSession.prototype.processCall = function (peerConnection, ext) { var extension = ext || {}; if (!peerConnection._pc.localDescription) return; extension.sessionID = this.ID; extension.callType = this.callType; extension.callerID = this.initiatorID; extension.opponentsIDs = this.opponentsIDs; extension.sdp = peerConnection._pc.localDescription.sdp; //TODO: set bandwidth to the userInfo object extension.userInfo = ext && ext.userInfo ? ext.userInfo : {}; extension.userInfo.bandwidth = this.bandwidth; this.signalingProvider.sendMessage( peerConnection.userID, extension, SignalingConstants.SignalingType.CALL ); }; WebRTCSession.prototype.processIceCandidates = function (peerConnection, iceCandidates) { var extension = {}; extension.sessionID = this.ID; extension.callType = this.callType; extension.callerID = this.initiatorID; extension.opponentsIDs = this.opponentsIDs; this.signalingProvider.sendCandidate(peerConnection.userID, iceCandidates, extension); }; WebRTCSession.prototype.processOnNotAnswer = function (peerConnection) { Helpers.trace("Answer timeout callback for session " + this.ID + " for user " + peerConnection.userID); this._clearWaitingOfferOrAnswerTimer(); peerConnection.release(); if (typeof this.onUserNotAnswerListener === 'function') { Utils.safeCallbackCall(this.onUserNotAnswerListener, this, peerConnection.userID); } this._closeSessionIfAllConnectionsClosed(); }; /** * DELEGATES (peer connection) */ WebRTCSession.prototype._onRemoteStreamListener = function (userID, stream) { if (typeof this.onRemoteStreamListener === 'function') { Utils.safeCallbackCall(this.onRemoteStreamListener, this, userID, stream); } }; /** * [_onCallStatsReport return statistics about the peer] * @param {number} userId [id of user (callee)] * @param {array} stats [array of statistics] * * Fire onCallStatsReport callbacks with parameters(userId, stats, error). * If stats will be invalid callback return null and error */ WebRTCSession.prototype._onCallStatsReport = function (userId, stats, error) { if (typeof this.onCallStatsReport === 'function') { Utils.safeCallbackCall(this.onCallStatsReport, this, userId, stats, error); } }; WebRTCSession.prototype._onSessionConnectionStateChangedListener = function (userID, connectionState) { var StateClosed = Helpers.SessionConnectionState.CLOSED; var peer = this.peerConnections[userID]; if (typeof this.onSessionConnectionStateChangedListener === 'function') { Utils.safeCallbackCall( this.onSessionConnectionStateChangedListener, this, userID, connectionState ); } if (connectionState === StateClosed && peer) { peer._pc.onicecandidate = null; peer._pc.onsignalingstatechange = null; peer._pc.ontrack = null; peer._pc.oniceconnectionstatechange = null; delete this.peerConnections[userID]; } }; /** * Private * @param {number} userId * @param {boolean} polite * @returns {qbRTCPeerConnection} */ WebRTCSession.prototype._createPeer = function (userId, polite) { if (!window.RTCPeerConnection) { throw new Error('_createPeer error: RTCPeerConnection is not supported in your browser'); } this.startCallTime = new Date(); var pcConfig = { iceServers: config.webrtc.iceServers, }; Helpers.trace("_createPeer configuration: " + JSON.stringify(pcConfig)); var peer = new qbRTCPeerConnection(pcConfig); peer._init(this, userId, this.ID, polite); return peer; }; WebRTCSession.prototype._startReconnectTimer = function (userID) { var self = this; var delay = config.webrtc.disconnectTimeInterval * 1000; var peer = this.peerConnections[userID]; peer._reconnecting = true; var reconnectTimeoutCallback = function () { Helpers.trace('disconnectTimeInterval reached for userID ' + userID); self._stopReconnectTimer(userID); self.peerConnections[userID].release(); self._onSessionConnectionStateChangedListener( userID, Helpers.SessionConnectionState.CLOSED ); self._closeSessionIfAllConnectionsClosed(); }; if (typeof this.onReconnectListener === 'function') { Utils.safeCallbackCall( this.onReconnectListener, this, userID, ReconnectionState.RECONNECTING ); } Helpers.trace( '_startReconnectTimer for userID:' + userID + ', timeout: ' + delay ); var iceConnectTimeoutCallback = function () { Helpers.trace('iceConnectTimeout reached for user ' + userID); if (self.iceConnectTimers[userID]) { clearTimeout(self.iceConnectTimers[userID]); self.iceConnectTimers[userID] = undefined; if (!self.reconnectTimers[userID]) { /** If connection won't be recovered - close session */ self.reconnectTimers[userID] = setTimeout( reconnectTimeoutCallback, delay - ICE_TIMEOUT ); self._reconnectToChat(function () { if (self.state === WebRTCSession.State.ACTIVE && self.reconnectTimers[userID]) { // only if session is active self._reconnect(peer, true); } }); } } }; if (!this.iceConnectTimers[userID]) { /** * Wait a bit before reconnecting. If network has not been changed - * ICE candidates are still valid and connection may recover shortly */ this.iceConnectTimers[userID] = setTimeout( iceConnectTimeoutCallback, ICE_TIMEOUT ); } }; WebRTCSession.prototype._stopReconnectTimer = function (userID) { var peer = this.peerConnections[userID]; if (this.iceConnectTimers[userID]) { clearTimeout(this.iceConnectTimers[userID]); this.iceConnectTimers[userID] = undefined; } if (this.reconnectTimers[userID]) { Helpers.trace('_stopReconnectTimer for userID: ' + userID); clearTimeout(this.reconnectTimers[userID]); this.reconnectTimers[userID] = undefined; } if (peer && peer._reconnecting) { peer._reconnecting = false; if (typeof this.onReconnectListener === 'function') { var state = peer._pc.iceConnectionState; Utils.safeCallbackCall( this.onReconnectListener, this, userID, state === 'connected' ? ReconnectionState.RECONNECTED : ReconnectionState.FAILED ); } } }; /** * Ping server until pong received, then call the `callback` * @param {Function} callback */ WebRTCSession.prototype._reconnectToChat = function (callback) { var self = this; var signalingProvider = this.signalingProvider; var reconnectToChat = function () { var _onReconnectListener = signalingProvider.chat.onReconnectListener; signalingProvider.chat.onReconnectListener = function () { if (typeof _onReconnectListener === 'function') { _onReconnectListener(); } signalingProvider.chat.onReconnectListener = _onReconnectListener; callback(); }; signalingProvider.chat.reconnect(); }; if (signalingProvider && signalingProvider.chat) { try { /** * Ping chat server to make sure that chat connection * has been established */ signalingProvider.chat.ping(function (e) { if (self.state !== WebRTCSession.State.CLOSED) { if (e) { // If not - reconnect to chat reconnectToChat(); } else { // If chat connected - call `callback` callback(); } } }); } catch (e) { if (self.state !== WebRTCSession.State.CLOSED) { /** Catch `ChatNotConnected` error and reconnect to chat */ reconnectToChat(); } } } }; /** * @param {qbRTCPeerConnection} peerConnection * @param {boolean} [negotiate] * @returns {void} */ WebRTCSession.prototype._reconnect = function (peerConnection, negotiate) { if (!peerConnection || !peerConnection.userID) { return; } var userId = peerConnection.userID; var polite = peerConnection.polite; var _reconnecting = peerConnection._reconnecting; peerConnection.release(); var pcConfig = { iceServers: config.webrtc.iceServers, }; Helpers.trace("_reconnect peer configuration: " + JSON.stringify(pcConfig)); var peer = new qbRTCPeerConnection(pcConfig); this.peerConnections[userId] = peer; peer._init(this, userId, this.ID, polite); peer._reconnecting = _reconnecting; peer.addLocalStream(this.localStream); if (negotiate) { peer.offerId = generateUUID(); peer.negotiate(); } }; /** close peer connection and local stream */ WebRTCSession.prototype._close = function () { Helpers.trace('_close'); for (var key in this.peerConnections) { var peer = this.peerConnections[key]; this._stopReconnectTimer(peer.userID); try { peer.release(); } catch (e) { console.warn('Peer close error:', e); } } this._closeLocalMediaStream(); if (typeof this._detectSilentAudioTaskCleanup === 'function') { this._detectSilentAudioTaskCleanup(); this._detectSilentAudioTaskCleanup = undefined; } if (typeof this._detectSilentVideoTaskCleanup === 'function') { this._detectSilentVideoTaskCleanup(); this._detectSilentVideoTaskCleanup = undefined; } this.state = WebRTCSession.State.CLOSED; if (typeof this.onSessionCloseListener === 'function') { Utils.safeCallbackCall(this.onSessionCloseListener, this); } }; WebRTCSession.prototype._closeSessionIfAllConnectionsClosed = function () { var isAllConnectionsClosed = Object .values(this.peerConnections) .every(function (peer) { return peer.state === qbRTCPeerConnection.State.CLOSED; }); Helpers.trace("All peer connections closed: " + isAllConnectionsClosed); if (isAllConnectionsClosed) { this._closeLocalMediaStream(); if (typeof this.onSessionCloseListener === 'function') { this.onSessionCloseListener(this); } this.state = WebRTCSession.State.CLOSED; } }; WebRTCSession.prototype._closeLocalMediaStream = function () { if (this.localStream) { this.localStream.getTracks().forEach(function (track) { track.stop(); track.enabled = false; }); this.localStream = null; } }; WebRTCSession.prototype._muteStream = function (bool, type) { if (type === 'audio' && this.localStream.getAudioTracks().length > 0) { this.localStream.getAudioTracks().forEach(function (track) { track.enabled = !!bool; }); return; } if (type === 'video' && this.localStream.getVideoTracks().length > 0) { this.localStream.getVideoTracks().forEach(function (track) { track.enabled = !!bool; }); return; } }; WebRTCSession.prototype._clearAnswerTimer = function () { if (this.answerTimer) { Helpers.trace("_clearAnswerTimer"); clearTimeout(this.answerTimer); this.answerTimer = null; } }; WebRTCSession.prototype._startAnswerTimer = function () { Helpers.trace("_startAnswerTimer"); var self = this; var answerTimeoutCallback = function () { Helpers.trace("_answerTimeoutCallback"); if (typeof self.onSessionCloseListener === 'function') { self._close(); } self.answerTimer = null; }; var answerTimeInterval = config.webrtc.answerTimeInterval * 1000; this.answerTimer = setTimeout(answerTimeoutCallback, answerTimeInterval); }; WebRTCSession.prototype._clearWaitingOfferOrAnswerTimer = function () { if (this.waitingOfferOrAnswerTimer) { Helpers.trace("_clearWaitingOfferOrAnswerTimer"); clearTimeout(this.waitingOfferOrAnswerTimer); this.waitingOfferOrAnswerTimer = null; } }; WebRTCSession.prototype._startWaitingOfferOrAnswerTimer = function (time) { var self = this, timeout = (config.webrtc.answerTimeInterval - time) < 0 ? 1 : config.webrtc.answerTimeInterval - time, waitingOfferOrAnswerTimeoutCallback = function () { Helpers.trace("waitingOfferOrAnswerTimeoutCallback"); if (Object.keys(self.peerConnections).length > 0) { Object.keys(self.peerConnections).forEach(function (key) { var peerConnection = self.peerConnections[key]; if (peerConnection.state === qbRTCPeerConnection.State.CONNECTING || peerConnection.state === qbRTCPeerConnection.State.NEW) { self.processOnNotAnswer(peerConnection); } }); } self.waitingOfferOrAnswerTimer = null; }; Helpers.trace("_startWaitingOfferOrAnswerTimer, timeout: " + timeout); this.waitingOfferOrAnswerTimer = setTimeout(waitingOfferOrAnswerTimeoutCallback, timeout * 1000); }; WebRTCSession.prototype._uniqueOpponentsIDs = function () { var self = this; var opponents = []; if (this.initiatorID !== this.currentUserID) { opponents.push(this.initiatorID); } this.opponentsIDs.forEach(function (userID, i, arr) { if (userID != self.currentUserID) { opponents.push(parseInt(userID)); } }); return opponents; }; WebRTCSession.prototype._uniqueOpponentsIDsWithoutInitiator = function () { var self = this; var opponents = []; this.opponentsIDs.forEach(function (userID, i, arr) { if (userID != self.currentUserID) { opponents.push(parseInt(userID)); } }); return opponents; }; WebRTCSession.prototype.toString = function sessionToString() { return 'ID: ' + this.ID + ', initiatorID: ' + this.initiatorID + ', opponentsIDs: ' + this.opponentsIDs + ', state: ' + this.state + ', callType: ' + this.callType; }; function generateUUID() { var d = new Date().getTime(); var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = (d + Math.random() * 16) % 16 | 0; d = Math.floor(d / 16); return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16); }); return uuid; } /** * private _prepareExtension - replace property null to empty string * return object with property or empty if extension didn't set */ function _prepareExtension(extension) { var ext = {}; try { if (({}).toString.call(extension) === '[object Object]') { ext.userInfo = extension; ext = JSON.parse(JSON.stringify(ext).replace(/null/g, "\"\"")); } else { throw new Error('Invalid type of "extension" object.'); } } catch (err) { Helpers.traceWarning(err.message); } return ext; } module.exports = WebRTCSession; },{"../../qbConfig":252,"../../qbUtils":256,"./qbRTCPeerConnection":244,"./qbWebRTCHelpers":246,"./qbWebRTCSignalingConstants":248}],248:[function(require,module,exports){ 'use strict'; /** * QuickBlox JavaScript SDK * WebRTC Module (WebRTC signaling constants) */ function WebRTCSignalingConstants() {} WebRTCSignalingConstants.MODULE_ID = "WebRTCVideoChat"; WebRTCSignalingConstants.SignalingType = { CALL: 'call', ACCEPT: 'accept', REJECT: 'reject', STOP: 'hangUp', CANDIDATE: 'iceCandidates', PARAMETERS_CHANGED: 'update' }; module.exports = WebRTCSignalingConstants; },{}],249:[function(require,module,exports){ 'use strict'; /** * QuickBlox JavaScript SDK * WebRTC Module (WebRTC signaling provider) */ require('strophe.js'); var SignalingConstants = require('./qbWebRTCSignalingConstants'); function WebRTCSignalingProcessor(service, delegate) { var self = this; self.service = service; self.delegate = delegate; this._onMessage = function(from, extraParams, delay, userId) { var extension = self._getExtension(extraParams), sessionId = extension.sessionID, signalType = extension.signalType; /** cleanup */ delete extension.moduleIdentifier; delete extension.sessionID; delete extension.signalType; switch (signalType) { case SignalingConstants.SignalingType.CALL: if (typeof self.delegate._onCallListener === 'function'){ self.delegate._onCallListener(userId, sessionId, extension); } break; case SignalingConstants.SignalingType.ACCEPT: if (typeof self.delegate._onAcceptListener === 'function'){ self.delegate._onAcceptListener(userId, sessionId, extension); } break; case SignalingConstants.SignalingType.REJECT: if (typeof self.delegate._onRejectListener === 'function'){ self.delegate._onRejectListener(userId, sessionId, extension); } break; case SignalingConstants.SignalingType.STOP: if (typeof self.delegate._onStopListener === 'function'){ self.delegate._onStopListener(userId, sessionId, extension); } break; case SignalingConstants.SignalingType.CANDIDATE: if (typeof self.delegate._onIceCandidatesListener === 'function'){ self.delegate._onIceCandidatesListener(userId, sessionId, extension); } break; case SignalingConstants.SignalingType.PARAMETERS_CHANGED: if (typeof self.delegate._onUpdateListener === 'function'){ self.delegate._onUpdateListener(userId, sessionId, extension); } break; } }; /** * Convert XML into JS object * @param {Element} extraParams * @returns {Object} */ this._getExtension = function(extraParams) { if (!extraParams) { return {}; } var extension = {}, iceCandidates = [], opponents = []; extraParams.childNodes.forEach(function (childNode) { if (childNode.nodeName === 'iceCandidates') { /** iceCandidates */ childNode.childNodes.forEach(function (candidateNode) { var candidate = {}; candidateNode.childNodes.forEach(function (node) { candidate[node.nodeName] = node.textContent; }); iceCandidates.push(candidate); }); } else if (childNode.nodeName === 'opponentsIDs') { /** opponentsIDs */ childNode.childNodes.forEach(function (opponentNode) { var opponentId = opponentNode.textContent; opponents.push(parseInt(opponentId)); }); } else { if (childNode.childNodes.length > 1) { extension = self._XMLtoJS( extension, childNode.nodeName, childNode ); } else { if (childNode.nodeName === 'userInfo') { extension = self._XMLtoJS( extension, childNode.nodeName, childNode ); } else { extension[childNode.nodeName] = childNode.textContent; } } } }); if (iceCandidates.length > 0) { extension.iceCandidates = iceCandidates; } if (opponents.length > 0) { extension.opponentsIDs = opponents; } return extension; }; /** * * @param {Object} extension * @param {string} title * @param {Element} element * @returns {Object} */ this._XMLtoJS = function(extension, title, element) { var self = this; extension[title] = {}; element.childNodes.forEach(function (childNode) { if (childNode.childNodes.length > 1) { extension[title] = self._XMLtoJS( extension[title], childNode.nodeName, childNode ); } else { extension[title][childNode.nodeName] = childNode.textContent; } }); return extension; }; } module.exports = WebRTCSignalingProcessor; },{"./qbWebRTCSignalingConstants":248,"strophe.js":208}],250:[function(require,module,exports){ 'use strict'; /** JSHint inline rules */ /* globals Strophe, $msg */ /** * QuickBlox JavaScript SDK * WebRTC Module (WebRTC signaling processor) */ require('strophe.js'); var Helpers = require('./qbWebRTCHelpers'); var SignalingConstants = require('./qbWebRTCSignalingConstants'); var Utils = require('../../qbUtils'); var config = require('../../qbConfig'); function WebRTCSignalingProvider(service, chat) { this.service = service; this.chat = chat; } WebRTCSignalingProvider.prototype.sendCandidate = function (userId, iceCandidates, ext) { var extension = ext || {}; extension.iceCandidates = iceCandidates; this.sendMessage(userId, extension, SignalingConstants.SignalingType.CANDIDATE); }; WebRTCSignalingProvider.prototype.sendMessage = function (userId, ext, signalingType) { var extension = ext || {}, self = this; /** basic parameters */ extension.moduleIdentifier = SignalingConstants.MODULE_ID; extension.signalType = signalingType; /** extension.sessionID */ /** extension.callType */ extension.platform = 'web'; /** extension.callerID */ /** extension.opponentsIDs */ /** extension.sdp */ if (extension.userInfo && !Object.keys(extension.userInfo).length) { delete extension.userInfo; } var params = { to: Helpers.getUserJid(userId, config.creds.appId), type: 'headline', id: Utils.getBsonObjectId() }; var msg = $msg(params).c('extraParams', { xmlns: Strophe.NS.CLIENT }); Object.keys(extension).forEach(function (field) { if (field === 'iceCandidates') { /** iceCandidates */ msg.c('iceCandidates'); extension[field].forEach(function (candidate) { msg.c('iceCandidate'); Object.keys(candidate).forEach(function (key) { msg.c(key).t(candidate[key]).up(); }); msg.up(); }); msg.up(); } else if (field === 'opponentsIDs') { /** opponentsIDs */ msg.c('opponentsIDs'); extension[field].forEach(function (opponentId) { msg.c('opponentID').t(opponentId).up(); }); msg.up(); } else if (typeof extension[field] === 'object') { self._JStoXML(field, extension[field], msg); } else { msg.c(field).t(extension[field]).up(); } }); this.chat.connection.send(msg); }; /** * * @param {string} title * @param {Object} obj * @param {Strophe.Builder} msg */ WebRTCSignalingProvider.prototype._JStoXML = function (title, obj, msg) { var self = this; msg.c(title); Object.keys(obj).forEach(function (field) { if (typeof obj[field] === 'object') self._JStoXML(field, obj[field], msg); else msg.c(field).t(obj[field]).up(); }); msg.up(); }; module.exports = WebRTCSignalingProvider; },{"../../qbConfig":252,"../../qbUtils":256,"./qbWebRTCHelpers":246,"./qbWebRTCSignalingConstants":248,"strophe.js":208}],251:[function(require,module,exports){ 'use strict'; /** * QuickBlox JavaScript SDK * Chat Stream Management plugin * doc: http://xmpp.org/extensions/xep-0198.html * * To enable this features add to config * ```javascript * streamManagement: { - enable: true - } * ``` * * Uses listener by QB.chat.onSentMessageCallback * * ```javascript * QB.chat.onSentMessageCallback = function (messageLost, messageSent) { * if (messageLost) { * console.error('sendErrorCallback', messageLost); * } else { * console.info('sendMessageSuccessCallback', messageSent); * } * }; * ``` */ /** JSHint inline rules */ /* globals $build */ var Utils = require('../qbUtils'), chatUtils = require('../modules/chat/qbChatHelpers'); function StreamManagement(options) { this._NS = 'urn:xmpp:sm:3'; this._isStreamManagementEnabled = false; // Counter of the incoming stanzas this._clientProcessedStanzasCounter = null; // The client send stanza counter. this._clientSentStanzasCounter = null; this._intervalId = null; this._timeInterval = Utils.getTimeIntervalForCallBackMessage(); this.sentMessageCallback = null; if(Utils.getEnv().browser){ this._parser = new DOMParser(); } // connection this._c = null; this._nodeBuilder = null; // Original connection.send method this._originalSend = null; // In progress stanzas queue this._stanzasQueue = []; } StreamManagement.prototype.enable = function (connection, client) { var self = this, stanza, enableParams = { xmlns: self._NS }; if(!self._isStreamManagementEnabled){ self._c = connection; self._originalSend = this._c.send; self._c.send = this.send.bind(self); } if(Utils.getEnv().browser){ this._clientProcessedStanzasCounter = null; this._clientSentStanzasCounter = null; self._addEnableHandlers(); stanza = $build('enable', enableParams); } else { self._nodeBuilder = client.Stanza; self._addEnableHandlers(); stanza = chatUtils.createStanza(self._nodeBuilder, enableParams, 'enable'); } self._c.send(stanza); }; StreamManagement.prototype._timeoutCallback = function () { var self = this, now = Date.now(), updatedStanzasQueue = []; if(self._stanzasQueue.length){ for(var i = 0; i < self._stanzasQueue.length; i++){ if(self._stanzasQueue[i] && self._stanzasQueue[i].time < now){ self.sentMessageCallback(self._stanzasQueue[i].message); } else { updatedStanzasQueue.push(self._stanzasQueue[i]); } } self._stanzasQueue = updatedStanzasQueue; } }; StreamManagement.prototype._addEnableHandlers = function () { var self = this; if (Utils.getEnv().browser) { self._c.XAddTrackedHandler(_incomingStanzaHandler.bind(self)); } else { self._c.on('stanza', _incomingStanzaHandler.bind(self)); } function _incomingStanzaHandler (stanza){ /* * Getting incoming stanza tagName * */ var tagName = stanza.name || stanza.tagName || stanza.nodeTree.tagName; if(tagName === 'enabled'){ self._isStreamManagementEnabled = true; return true; } if (self._isStreamManagementEnabled && tagName === 'message') { clearInterval(self._intervalId); self._intervalId = setInterval(self._timeoutCallback.bind(self), self._timeInterval); return true; } if(chatUtils.getAttr(stanza, 'xmlns') !== self._NS){ self._increaseReceivedStanzasCounter(); } if(tagName === 'r'){ var params = { xmlns: self._NS, h: self._clientProcessedStanzasCounter }, answerStanza = Utils.getEnv().browser ? $build('a', params) : chatUtils.createStanza(self._nodeBuilder, params, 'a'); self._originalSend.call(self._c, answerStanza); return true; } if(tagName === 'a'){ var h = parseInt(chatUtils.getAttr(stanza, 'h')); self._checkCounterOnIncomeStanza(h); } return true; } }; StreamManagement.prototype.send = function (stanza, message) { var self = this, stanzaXML = stanza.nodeTree ? this._parser.parseFromString(stanza.nodeTree.outerHTML, "application/xml").childNodes[0] : stanza, tagName = stanzaXML.name || stanzaXML.tagName || stanzaXML.nodeTree.tagName, type = chatUtils.getAttr(stanzaXML, 'type'), bodyContent = chatUtils.getElementText(stanzaXML, 'body') || '', attachments = chatUtils.getAllElements(stanzaXML, 'attachment') || ''; try { self._originalSend.call(self._c, stanza); } catch (e) { Utils.QBLog('[QBChat]', e.message); } finally { if (tagName === 'message' && (type === 'chat' || type === 'groupchat') && (bodyContent || attachments.length)) { self._sendStanzasRequest({ message: message, time: Date.now() + self._timeInterval, expect: self._clientSentStanzasCounter }); } self._clientSentStanzasCounter++; } }; StreamManagement.prototype._sendStanzasRequest = function (data) { var self = this; if(self._isStreamManagementEnabled){ self._stanzasQueue.push(data); var stanza = Utils.getEnv().browser ? $build('r', { xmlns: self._NS }) : chatUtils.createStanza(self._nodeBuilder, { xmlns: self._NS }, 'r'); if (self._c.connected) { self._originalSend.call(self._c, stanza); } else { self._checkCounterOnIncomeStanza(); } } }; StreamManagement.prototype.getClientSentStanzasCounter = function(){ return this._clientSentStanzasCounter; }; StreamManagement.prototype._checkCounterOnIncomeStanza = function (count){ var updatedStanzasQueue = []; if(this._stanzasQueue.length){ for(var i = 0; i < this._stanzasQueue.length; i++){ if(this._stanzasQueue[i].expect == count){ this.sentMessageCallback(null, this._stanzasQueue[i].message); } else { updatedStanzasQueue.push(this._stanzasQueue[i]); } } this._stanzasQueue = updatedStanzasQueue; } }; StreamManagement.prototype._increaseReceivedStanzasCounter = function(){ this._clientProcessedStanzasCounter++; }; module.exports = StreamManagement; },{"../modules/chat/qbChatHelpers":235,"../qbUtils":256}],252:[function(require,module,exports){ 'use strict'; /* * QuickBlox JavaScript SDK * * Configuration Module * * NOTE: * - config.webrtc.statsReportTimeInterval [integer, sec]: * could add listener onCallStatsReport(session, userId, bytesReceived) if * want to get stats (bytesReceived) about peer every X sec; */ var config = { version: '2.16.4', buildNumber: '1159', creds: { 'appId': 0, 'authKey': '', 'authSecret': '', 'accountKey': '' }, endpoints: { api: 'api.quickblox.com', chat: 'chat.quickblox.com', muc: 'muc.chat.quickblox.com' }, hash: 'sha1', streamManagement: { enable: false }, chatProtocol: { bosh: 'https://chat.quickblox.com:5281', websocket: 'wss://chat.quickblox.com:5291', active: 2 }, pingTimeout: 1, pingLocalhostTimeInterval: 5, chatReconnectionTimeInterval: 3, webrtc: { answerTimeInterval: 60, autoReject: true, incomingLimit: 1, dialingTimeInterval: 5, disconnectTimeInterval: 30, statsReportTimeInterval: false, iceServers: [ { urls: ['turn:turn.quickblox.com', 'stun:turn.quickblox.com'], username: 'quickblox', credential: 'baccb97ba2d92d71e26eb9886da5f1e0' } ] }, urls: { account: 'account_settings', session: 'session', login: 'login', users: 'users', chat: 'chat', blobs: 'blobs', geodata: 'geodata', pushtokens: 'push_tokens', subscriptions: 'subscriptions', events: 'events', data: 'data', addressbook: 'address_book', addressbookRegistered: 'address_book/registered_users', type: '.json' }, on: { sessionExpired: null }, timeout: null, debug: { mode: 0, file: null }, addISOTime: false, qbTokenExpirationDate: null, liveSessionInterval: 120, callBackInterval: 30, }; config.set = function(options) { if (typeof options.endpoints === 'object' && options.endpoints.chat) { config.endpoints.muc = 'muc.'+options.endpoints.chat; config.chatProtocol.bosh = 'https://'+options.endpoints.chat+':5281'; config.chatProtocol.websocket = 'wss://'+options.endpoints.chat+':5291'; } Object.keys(options).forEach(function(key) { if(key !== 'set' && config.hasOwnProperty(key)) { if(typeof options[key] !== 'object') { config[key] = options[key]; } else { Object.keys(options[key]).forEach(function(nextkey) { if(config[key].hasOwnProperty(nextkey)){ config[key][nextkey] = options[key][nextkey]; } }); } } // backward compatibility: for config.iceServers if(key === 'iceServers') { config.webrtc.iceServers = options[key]; } }); }; config.updateSessionExpirationDate = function (tokenExpirationDate, headerHasToken = false) { var connectionTimeLag = 1; // minute var newDate; if (headerHasToken) { var d = tokenExpirationDate.replaceAll('-','/'); newDate = new Date(d); newDate.setMinutes ( newDate.getMinutes() - connectionTimeLag); } else { newDate = new Date(tokenExpirationDate); newDate.setMinutes ( newDate.getMinutes() - connectionTimeLag); newDate.setMinutes ( newDate.getMinutes() + config.liveSessionInterval ); } config.qbTokenExpirationDate = newDate; }; module.exports = config; },{}],253:[function(require,module,exports){ 'use strict'; /* * QuickBlox JavaScript SDK * * Main SDK Module * */ var config = require('./qbConfig'); var Utils = require('./qbUtils'); // Actual QuickBlox API starts here function QuickBlox() {} QuickBlox.prototype = { /** * Return current version of QuickBlox JavaScript SDK * @memberof QB * */ version: config.version, /** * Return current build number of QuickBlox JavaScript SDK * @memberof QB * */ buildNumber: config.buildNumber, _getOS: Utils.getOS.bind(Utils), /** * @memberof QB * Initialize QuickBlox SDK({@link https://docs.quickblox.com/docs/js-setup#initialize-quickblox-sdk read more}) * @param {Number | String} appIdOrToken - Application ID (from your admin panel) or Session Token. * @param {String | Number} authKeyOrAppId - Authorization key or Application ID. You need to set up Application ID if you use session token as appIdOrToken parameter. * @param {String} authSecret - Authorization secret key (from your admin panel). * @param {Object} configMap - Settings object for QuickBlox SDK. */ init: function(appIdOrToken, authKeyOrAppId, authSecret, accountKey, configMap) { console.log('current platform: ', Utils.getEnv()); if (typeof accountKey === 'string' && accountKey.length) { if (configMap && typeof configMap === 'object') { config.set(configMap); } config.creds.accountKey = accountKey; } else { console.warn('Parameter "accountKey" is missing. This will lead to error in next major release'); console.warn('NOTE: Account migration will not work without "accountKey"'); if (typeof accountKey === 'object') { config.set(accountKey); } } var SHARED_API_ENDPOINT = "api.quickblox.com"; var SHARED_CHAT_ENDPOINT = "chat.quickblox.com"; /** include dependencies */ var Proxy = require('./qbProxy'), Auth = require('./modules/qbAuth'), Users = require('./modules/qbUsers'), Content = require('./modules/qbContent'), PushNotifications = require('./modules/qbPushNotifications'), Data = require('./modules/qbData'), AddressBook = require('./modules/qbAddressBook'), Chat = require('./modules/chat/qbChat'), DialogProxy = require('./modules/chat/qbDialog'), MessageProxy = require('./modules/chat/qbMessage'); this.service = new Proxy(); this.auth = new Auth(this.service); this.users = new Users(this.service); this.content = new Content(this.service); this.pushnotifications = new PushNotifications(this.service); this.data = new Data(this.service); this.addressbook = new AddressBook(this.service); this.chat = new Chat(this.service); this.chat.dialog = new DialogProxy(this.service); this.chat.message = new MessageProxy(this.service); if (Utils.getEnv().browser) { /** add adapter.js*/ require('webrtc-adapter'); /** add WebRTC API if API is avaible */ if( Utils.isWebRTCAvailble() ) { var WebRTCClient = require('./modules/webrtc/qbWebRTCClient'); this.webrtc = new WebRTCClient(this.service, this.chat); } else { this.webrtc = false; } } else { this.webrtc = false; } // Initialization by outside token if (typeof appIdOrToken === 'string' && (!authKeyOrAppId || typeof authKeyOrAppId === 'number') && !authSecret) { if(typeof authKeyOrAppId === 'number'){ config.creds.appId = authKeyOrAppId; } this.service.setSession({ token: appIdOrToken }); } else { config.creds.appId = appIdOrToken; config.creds.authKey = authKeyOrAppId; config.creds.authSecret = authSecret; } var shouldGetSettings = config.creds.accountKey && ( !config.endpoints.api || config.endpoints.api === SHARED_API_ENDPOINT || !config.endpoints.chat || config.endpoints.chat === SHARED_CHAT_ENDPOINT ); if (shouldGetSettings) { var accountSettingsUrl = [ 'https://', SHARED_API_ENDPOINT, '/', config.urls.account, config.urls.type ].join(''); // account settings this.service.ajax({ url: accountSettingsUrl }, function (err, response) { if (!err && typeof response === 'object') { var update = { endpoints: { api: response.api_endpoint.replace(/^https?:\/\//, ''), chat: response.chat_endpoint } }; config.set(update); } }); } }, /** * Init QuickBlox SDK with User Account data to start session with token({@link https://docs.quickblox.com/docs/js-setup#initialize-quickblox-sdk-without-authorization-key-and-secret read more}). * @memberof QB * @param {Number} appId - Application ID (from your admin panel). * @param {String | Number} accountKey - Account key (from your admin panel). * @param {Object} configMap - Settings object for QuickBlox SDK. */ initWithAppId: function(appId, accountKey, configMap) { //добавить проверку типа параметров if (typeof appId !== 'number') { throw new Error('Type of appId must be a number'); } if (appId === '' || appId === undefined || appId === null || accountKey === '' || accountKey === undefined || accountKey === null) { throw new Error('Cannot init QuickBlox without app credentials (app ID, auth key)'); } else { this.init('', appId, null, accountKey, configMap); } }, /** * Return current session({@link https://docs.quickblox.com/docs/js-authentication#get-session read more}). * @memberof QB * @param {getSessionCallback} callback - The getSessionCallback function. * */ getSession: function(callback) { /** * This callback return session object.. * @callback getSessionCallback * @param {Object} error - The error object. * @param {Object} session - Contains of session object. * */ this.auth.getSession(callback); }, /** * Set up user session token to current session and return it({@link https://docs.quickblox.com/docs/js-authentication#set-existing-session read more}). * @memberof QB * @param {String} token - A User Session Token. * @param {getSessionCallback} callback - The getSessionCallback function. * @callback getSessionCallback * @param {Object} error - The error object. * @param {Object} session - Contains of session object. * */ startSessionWithToken: function(token, callback) { if (token === undefined) throw new Error('Cannot start session with undefined token'); else if (token === '') throw new Error('Cannot start session with empty string token'); else if (token === null) throw new Error('Cannot start session with null value token'); else if (typeof callback !== 'function') throw new Error('Cannot start session without callback function'); else { try { this.service.setSession({token: token}); } catch (err) { callback(err, null); } if (typeof callback === 'function') { try{ this.auth.getSession(callback); // TODO: pay attention on it, if we decide to remove application_id from QB.init: // artan 06-09-2022 // should set value application_id from session model into config.creds.appId } catch(er){ callback(er, null); } } } }, /** * Create new session({@link https://docs.quickblox.com/docs/js-authentication#create-session read more}). * @memberof QB * @param {String} appIdOrToken Should be applecationID or QBtoken. * @param {createSessionCallback} callback * */ createSession: function(params, callback) { /** * This callback return session object. * @callback createSession * @param {Object} error - The error object. * @param {Object} session - Contains of session object. * */ this.auth.createSession(params, callback); }, /** * Destroy current session({@link https://docs.quickblox.com/docs/js-authentication#destroy-session-token read more}). * @memberof QB * @param {destroySessionCallback} callback - The destroySessionCallback function. * */ destroySession: function(callback) { /** * This callback returns error or empty string. * @callback destroySessionCallback * @param {Object | Null} error - The error object if got en error and null if success. * @param {Null | String} result - String (" ") if session was removed successfully. * */ this.auth.destroySession(callback); }, /** * Login to QuickBlox application({@link https://docs.quickblox.com/docs/js-authentication#log-in-user read more}). * @memberof QB * @param {Object} params - Params object for login into the session. * @param {loginCallback} callback - The loginCallback function. * */ login: function(params, callback) { /** * This callback return error or user Object. * @callback loginCallback * @param {Object | Null} error - The error object if got en error and null if success. * @param {Null | Object} result - User data object if everything goes well and null on error. * */ this.auth.login(params, callback); }, /** * Remove user from current session, but doesn't destroy it({@link https://docs.quickblox.com/docs/js-authentication#log-out-user read more}). * @memberof QB * @param {logoutCallback} callback - The logoutCallback function. * */ logout: function(callback) { /** * This callback return error or user Object. * @callback logoutCallback * @param {Object | Null} error - The error object if got en error and null if success. * @param {Null | String} result - String (" ") if session was removed successfully. * */ this.auth.logout(callback); } }; /** * @namespace */ var QB = new QuickBlox(); QB.QuickBlox = QuickBlox; module.exports = QB; },{"./modules/chat/qbChat":234,"./modules/chat/qbDialog":236,"./modules/chat/qbMessage":237,"./modules/qbAddressBook":238,"./modules/qbAuth":239,"./modules/qbContent":240,"./modules/qbData":241,"./modules/qbPushNotifications":242,"./modules/qbUsers":243,"./modules/webrtc/qbWebRTCClient":245,"./qbConfig":252,"./qbProxy":254,"./qbUtils":256,"webrtc-adapter":218}],254:[function(require,module,exports){ 'use strict'; var config = require('./qbConfig'); var Utils = require('./qbUtils'); /** * For server-side applications through using npm package 'quickblox' * you should include the following lines */ var qbFetch, qbFormData; if (Utils.getEnv().node) { qbFetch = require('node-fetch'); qbFormData = require('form-data'); } else { qbFetch = fetch; qbFormData = FormData; } function ServiceProxy() { this.qbInst = { config: config, session: null }; this.reqCount = 0; } ServiceProxy.prototype = { _fetchingSettings: false, _queue: [], setSession: function(session) { this.qbInst.session = session; }, getSession: function() { return this.qbInst.session; }, handleResponse: function(error, response, next, retry) { // can add middleware here... if (error) { const errorMsg = JSON.stringify(error.message).toLowerCase(); if (typeof config.on.sessionExpired === 'function' && error.code === 401 && errorMsg.indexOf('session does not exist') > -1) { config.on.sessionExpired(function() { next(error, response); }, retry); } else { next(error, null); } } else { if (config.addISOTime) { response = Utils.injectISOTimes(response); } next(null, response); } }, startLogger: function(params) { var clonedData; ++this.reqCount; if (params.data && params.data.file) { clonedData = JSON.parse(JSON.stringify(params.data)); clonedData.file = "..."; } else if (Utils.getEnv().nativescript) { clonedData = JSON.stringify(params.data); } else { clonedData = params.data; } Utils.QBLog('[Request][' + this.reqCount + ']', (params.type || 'GET') + ' ' + params.url, clonedData ? clonedData : ""); }, ajax: function(params, callback) { if (this._fetchingSettings) { this._queue.push([params, callback]); return; } this.startLogger(params); var self = this, isGetOrHeadType = !params.type || params.type === 'GET' || params.type === 'HEAD', qbSessionToken = self.qbInst && self.qbInst.session && self.qbInst.session.token, isQBRequest = params.url.indexOf('s3.amazonaws.com') === -1, isMultipartFormData = params.contentType === false, qbDataType = params.dataType || 'json', qbUrl = params.url, qbRequest = {}, qbRequestBody, qbResponse; qbRequest.method = params.type || 'GET'; if (params.data) { qbRequestBody = _getBodyRequest(); if (isGetOrHeadType) { qbUrl += '?' + qbRequestBody; } else { qbRequest.body = qbRequestBody; } } if (!isMultipartFormData) { qbRequest.headers = { 'Content-Type': params.contentType || 'application/x-www-form-urlencoded; charset=UTF-8' }; } if (isQBRequest) { if (!qbRequest.headers) { qbRequest.headers = {}; } qbRequest.headers['QB-OS'] = Utils.getOS(); qbRequest.headers['QB-SDK'] = 'JS ' + config.version + ' - Client'; if(qbSessionToken) { qbRequest.headers['QB-Token'] = qbSessionToken; } if (params.url.indexOf(config.urls.account) > -1) { qbRequest.headers['QB-Account-Key'] = config.creds.accountKey; this._fetchingSettings = true; } } if (config.timeout) { qbRequest.timeout = config.timeout; } qbFetch(qbUrl, qbRequest) .then(function(response) { qbResponse = response; if (qbRequest.method === 'GET' || qbRequest.method === 'POST'){ var qbTokenExpirationDate = qbResponse.headers.get('qb-token-expirationdate'); var headerHasToken = !(qbTokenExpirationDate === null || typeof qbTokenExpirationDate === 'undefined'); qbTokenExpirationDate = (headerHasToken) ? qbTokenExpirationDate : new Date(); self.qbInst.config.updateSessionExpirationDate(qbTokenExpirationDate, headerHasToken); Utils.QBLog('[Request][ajax]','header has token:',headerHasToken ); Utils.QBLog('[Request][ajax]','updateSessionExpirationDate ... Set value: ', self.qbInst.config.qbTokenExpirationDate ); } if (qbDataType === 'text') { return response.text(); } else { return response.json(); } }, function() { // Need to research this issue, response doesn't exist if server will return empty body (status 200) qbResponse = { status: 200 }; return ' '; }).then(function(body) { _requestCallback(null, qbResponse, body); }, function(error) { _requestCallback(error); }).catch((error) => { console.log('qbProxy fetch ... catch, error: ', error); _requestCallback(error); }); /* * Private functions * Only for ServiceProxy.ajax() method closure */ function _fixedEncodeURIComponent(str) { return encodeURIComponent(str).replace(/[#$&+,/:;=?@\[\]]/g, function(c) { return '%' + c.charCodeAt(0).toString(16); }); } function _getBodyRequest() { var data = params.data, qbData; if (isMultipartFormData) { qbData = new qbFormData(); Object.keys(data).forEach(function(item) { if (params.fileToCustomObject && (item === 'file')) { qbData.append(item, data[item].data, data[item].name); } else { qbData.append(item, params.data[item]); } }); } else if (params.isNeedStringify) { qbData = JSON.stringify(data); } else { qbData = Object.keys(data).map(function(k) { if (Utils.isObject(data[k])) { return Object.keys(data[k]).map(function(v) { return _fixedEncodeURIComponent(k) + '[' + (Utils.isArray(data[k]) ? '' : v) + ']=' + _fixedEncodeURIComponent(data[k][v]); }).sort().join('&'); } else { return _fixedEncodeURIComponent(k) + (Utils.isArray(data[k]) ? '[]' : '' ) + '=' + _fixedEncodeURIComponent(data[k]); } }).sort().join('&'); } return qbData; } function _requestCallback(error, response, body) { var statusCode = response && (response.status || response.statusCode), responseMessage, responseBody; if (error || (statusCode !== 200 && statusCode !== 201 && statusCode !== 202)) { var errorMsg; try { errorMsg = { code: response && statusCode || error && error.code, status: response && response.headers && response.headers.status || 'error', message: body || error && error.errno, detail: body && body.errors || error && error.syscall }; } catch(e) { errorMsg = error; } responseBody = body || error || body.errors; responseMessage = Utils.getEnv().nativescript ? JSON.stringify(responseBody) : responseBody; Utils.QBLog('[Response][' + self.reqCount + ']', 'error', statusCode, responseMessage); self.handleResponse(errorMsg, null, callback, retry); } else { responseBody = (body && body !== ' ') ? body : 'empty body'; responseMessage = Utils.getEnv().nativescript ? JSON.stringify(responseBody) : responseBody; Utils.QBLog('[Response][' + self.reqCount + ']', responseMessage); self.handleResponse(null, body, callback, retry); } if (self._fetchingSettings) { self._fetchingSettings = false; while (self._queue.length) { var args = self._queue.shift(); self.ajax.apply(self, args); } } } function retry(session) { if (!!session) { self.setSession(session); self.ajax(params, callback); } } } }; module.exports = ServiceProxy; },{"./qbConfig":252,"./qbUtils":256,"form-data":81,"node-fetch":105}],255:[function(require,module,exports){ 'use strict'; /** JSHint inline rules */ /* globals Strophe */ /** * QuickBlox JavaScript SDK * Strophe Connection Object */ require('strophe.js'); var config = require('./qbConfig'); var chatPRTCL = config.chatProtocol; var Utils = require('./qbUtils'); function Connection() { var protocol = chatPRTCL.active === 1 ? chatPRTCL.bosh : chatPRTCL.websocket; var conn = new Strophe.Connection(protocol); if (chatPRTCL.active === 1) { conn.xmlInput = function(data) { if (data.childNodes[0]) { for (var i = 0, len = data.childNodes.length; i < len; i++) { Utils.QBLog('[QBChat]', 'RECV:', data.childNodes[i]); } } }; conn.xmlOutput = function(data) { if (data.childNodes[0]) { for (var i = 0, len = data.childNodes.length; i < len; i++) { Utils.QBLog('[QBChat]', 'SENT:', data.childNodes[i]); } } }; } else { conn.xmlInput = function(data) { Utils.QBLog('[QBChat]', 'RECV:', data); }; conn.xmlOutput = function(data) { Utils.QBLog('[QBChat]', 'SENT:', data); }; } return conn; } module.exports = Connection; },{"./qbConfig":252,"./qbUtils":256,"strophe.js":208}],256:[function(require,module,exports){ (function (global){(function (){ /* eslint no-console: 2 */ 'use strict'; var config = require('./qbConfig'); var unsupported = "This function isn't supported outside of the browser (...yet)"; var isNativeScript = typeof global === 'object' && (global.hasOwnProperty('android') || global.hasOwnProperty('NSObject')), isNode = typeof window === 'undefined' && typeof exports === 'object' && !isNativeScript, isBrowser = typeof window !== 'undefined'; if (isNode) { var fs = require('fs'); var os = require('os'); } // The object for type MongoDB.Bson.ObjectId // http://docs.mongodb.org/manual/reference/object-id/ var ObjectId = { machine: Math.floor(Math.random() * 16777216).toString(16), pid: Math.floor(Math.random() * 32767).toString(16), increment: 0 }; var Utils = { /** * [getEnv get a name of an execution environment] * @return {object} return names of env. (node/browser) */ getEnv: function() { return { 'nativescript': isNativeScript, 'browser': isBrowser, 'node': isNode }; }, _getOSInfoFromNodeJS: function() { return os.platform(); }, _getOSInfoFromBrowser: function() { return window.navigator.userAgent; }, _getOSInfoFromNativeScript: function() { return (global && global.hasOwnProperty('android') ? 'Android' : 'iOS') + ' - NativeScript'; }, getOS: function() { var self = this; var osName = 'An unknown OS'; var OS_LIST = [ { osName:'Windows', codeNames:['Windows', 'win32'] }, { osName:'Linux', codeNames:['Linux', 'linux'] }, { osName:'macOS', codeNames:['Mac OS', 'darwin'] } ]; var platformInfo; if (self.getEnv().browser) { platformInfo = self._getOSInfoFromBrowser(); } else if (self.getEnv().node) { platformInfo = self._getOSInfoFromNodeJS(); } else if (self.getEnv().nativescript) { return self._getOSInfoFromNativeScript(); } OS_LIST.forEach(function(osInfo) { osInfo.codeNames.forEach(function(codeName) { var index = platformInfo.indexOf(codeName); if (index !== -1) { osName = osInfo.osName; } }); }); return osName; }, safeCallbackCall: function() { var listenerString = arguments[0].toString(), listenerName = listenerString.split('(')[0].split(' ')[1], argumentsCopy = [], listenerCall; for (var i = 0; i < arguments.length; i++) { argumentsCopy.push(arguments[i]); } listenerCall = argumentsCopy.shift(); try { listenerCall.apply(null, argumentsCopy); } catch (err) { if (listenerName === '') { console.error('Error: ' + err); }else{ console.error('Error in listener ' + listenerName + ': ' + err); } } }, randomNonce: function() { return Math.floor(Math.random() * 10000); }, unixTime: function() { return Math.floor(Date.now() / 1000); }, getUrl: function(base, id) { var resource = id ? '/' + id : ''; return 'https://' + config.endpoints.api + '/' + base + resource + config.urls.type; }, isArray: function(arg) { return Object.prototype.toString.call(arg) === '[object Array]'; }, isObject: function(arg) { return Object.prototype.toString.call(arg) === '[object Object]'; }, // Generating BSON ObjectId and converting it to a 24 character string representation // Changed from https://github.com/justaprogrammer/ObjectId.js/blob/master/src/main/javascript/Objectid.js getBsonObjectId: function() { var timestamp = this.unixTime().toString(16), increment = (ObjectId.increment++).toString(16); if (increment > 0xffffff) ObjectId.increment = 0; return '00000000'.substr(0, 8 - timestamp.length) + timestamp + '000000'.substr(0, 6 - ObjectId.machine.length) + ObjectId.machine + '0000'.substr(0, 4 - ObjectId.pid.length) + ObjectId.pid + '000000'.substr(0, 6 - increment.length) + increment; }, getCurrentTime: function() { return ((new Date()).toTimeString().split(' ')[0]); }, injectISOTimes: function(data) { if (data.created_at) { if (typeof data.created_at === 'number') data.iso_created_at = new Date(data.created_at * 1000).toISOString(); if (typeof data.updated_at === 'number') data.iso_updated_at = new Date(data.updated_at * 1000).toISOString(); } else if (data.items) { for (var i = 0, len = data.items.length; i < len; ++i) { if (typeof data.items[i].created_at === 'number') data.items[i].iso_created_at = new Date(data.items[i].created_at * 1000).toISOString(); if (typeof data.items[i].updated_at === 'number') data.items[i].iso_updated_at = new Date(data.items[i].updated_at * 1000).toISOString(); } } return data; }, QBLog: function(){ if (this.loggers) { for (var i=0; i