├── package.json ├── index.js ├── gruntfile.js ├── bin └── pub.js ├── test └── test-chat-websocket.js ├── README.md └── dist ├── pomelo-cocos2d-js.min.js └── pomelo-cocos2d-js.js /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pomelo-cocos2d-js", 3 | "version": "0.1.5", 4 | "description": "pomelo-cocos2d-js client", 5 | "main": "index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/NetEase/pomelo-cocos2d-js" 9 | }, 10 | "keywords": [ 11 | "pomelo", 12 | "cocos2d-x", 13 | "jsb" 14 | ], 15 | "dependencies": { 16 | "pomelo-protobuf": "0.4.0", 17 | "pomelo-protocol": "0.1.5", 18 | "pomelo-jsclient-websocket": "0.1.1" 19 | }, 20 | "author": "fantasyni", 21 | "license": "MIT", 22 | "devDependencies": { 23 | "grunt": "~0.4.2", 24 | "grunt-browserify": "3.x", 25 | "grunt-contrib-uglify": "~0.3.2" 26 | } 27 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var Util = require('util'); 2 | 3 | function checkCocos2dJsb() { 4 | if (typeof cc !== 'undefined' && cc && cc.sys && cc.sys.isNative) { 5 | return true; 6 | } 7 | 8 | return false; 9 | } 10 | 11 | var Root; 12 | (function() { 13 | Root = this; 14 | }()); 15 | 16 | if (checkCocos2dJsb()) { 17 | var console = cc; 18 | Root.console = console; 19 | cc.formatStr = Util.format; 20 | } 21 | 22 | var EventEmitter = require('events').EventEmitter; 23 | Root.EventEmitter = EventEmitter; 24 | var protobuf = require('pomelo-protobuf'); 25 | Root.protobuf = protobuf; 26 | var Protocol = require('pomelo-protocol'); 27 | Root.Protocol = Protocol; 28 | var pomelo = require('pomelo-jsclient-websocket'); 29 | Root.pomelo = pomelo; -------------------------------------------------------------------------------- /gruntfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function(grunt) { 4 | 5 | grunt.loadNpmTasks('grunt-browserify'); 6 | grunt.loadNpmTasks('grunt-contrib-uglify'); 7 | 8 | // Project configuration. 9 | grunt.initConfig({ 10 | pkg: grunt.file.readJSON('package.json'), 11 | browserify: { 12 | standalone: { 13 | src: ['index.js'], 14 | dest: './dist/pomelo-cocos2d-js.js' 15 | } 16 | }, 17 | uglify: { 18 | dist: { 19 | files: { 20 | './dist/pomelo-cocos2d-js.min.js': ['<%= browserify.standalone.dest %>'], 21 | } 22 | } 23 | }, 24 | }); 25 | 26 | // Default task. 27 | grunt.registerTask('default', ['browserify:standalone', 'uglify']); 28 | }; -------------------------------------------------------------------------------- /bin/pub.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var pomeloClientPath = require.resolve('../dist/pomelo-cocos2d-js.js'); 3 | var fileStr = fs.readFileSync(pomeloClientPath).toString(); 4 | fileStr = fileStr.replace(/typeof\s*require==\"function\"\&\&require/g, "function(str) {throw new Error('can not require ' + str)}"); 5 | 6 | fs.writeFileSync(pomeloClientPath, fileStr); 7 | 8 | var pomeloClientPath = require.resolve('../dist/pomelo-cocos2d-js.min.js'); 9 | var fileStr = fs.readFileSync(pomeloClientPath).toString(); 10 | fileStr = fileStr.replace(/typeof\s*require==\"function\"\&\&require/g, "function(str) {throw new Error('can not require ' + str)}"); 11 | 12 | fs.writeFileSync(pomeloClientPath, fileStr); 13 | 14 | console.log('replace require done...'); -------------------------------------------------------------------------------- /test/test-chat-websocket.js: -------------------------------------------------------------------------------- 1 | var pomelo = window.pomelo; 2 | 3 | var route = 'gate.gateHandler.queryEntry'; 4 | var uid = "uid"; 5 | var rid = "rid"; 6 | var username = "username"; 7 | 8 | pomelo.init({ 9 | host: "127.0.0.1", 10 | port: 3014, 11 | log: true 12 | }, function() { 13 | pomelo.request(route, { 14 | uid: uid 15 | }, function(data) { 16 | pomelo.disconnect(); 17 | pomelo.init({ 18 | host: data.host, 19 | port: data.port, 20 | log: true 21 | }, function() { 22 | var route = "connector.entryHandler.enter"; 23 | pomelo.request(route, { 24 | username: username, 25 | rid: rid 26 | }, function(data) { 27 | cc.log(JSON.stringify(data)); 28 | chatSend(); 29 | }); 30 | }); 31 | }); 32 | }); 33 | 34 | function chatSend() { 35 | var route = "chat.chatHandler.send"; 36 | var target = "*"; 37 | var msg = "msg" 38 | pomelo.request(route, { 39 | rid: rid, 40 | content: msg, 41 | from: username, 42 | target: target 43 | }, function(data) { 44 | cc.log(JSON.stringify(data)); 45 | }); 46 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #pomelo-cocos2d-js 2 | 3 | `pomelo-cocos2d-js` is a library for using in [pomelo](http://pomelo.netease.com/) with [cocos2dx-js](http://cocos2d-x.org/products#cocos2dx-js) 4 | 5 | #Usage 6 | using browserify to resolve library dependencies 7 | 8 | add this library to your project 9 | 10 | ``` 11 | npm install pomelo-cocos2d-js --save 12 | ``` 13 | 14 | add to your browserify main.js 15 | 16 | ``` 17 | require('pomelo-cocos2d-js'); 18 | ``` 19 | 20 | in cocos2d-js you can also add [dist/pomelo-cocos2d-js.js](https://github.com/NetEase/pomelo-cocos2d-js/tree/master/dist/pomelo-cocos2d-js.js) file into ***jsList*** 21 | 22 | then you can use `pomelo` object under the gloal `window` object the same as using in the browser 23 | 24 | simple chat test 25 | ``` 26 | var pomelo = window.pomelo; 27 | 28 | var route = 'gate.gateHandler.queryEntry'; 29 | var uid = "uid"; 30 | var rid = "rid"; 31 | var username = "username"; 32 | 33 | pomelo.init({ 34 | host: "127.0.0.1", 35 | port: 3014, 36 | log: true 37 | }, function() { 38 | pomelo.request(route, { 39 | uid: uid 40 | }, function(data) { 41 | pomelo.disconnect(); 42 | pomelo.init({ 43 | host: data.host, 44 | port: data.port, 45 | log: true 46 | }, function() { 47 | var route = "connector.entryHandler.enter"; 48 | pomelo.request(route, { 49 | username: username, 50 | rid: rid 51 | }, function(data) { 52 | cc.log(JSON.stringify(data)); 53 | chatSend(); 54 | }); 55 | }); 56 | }); 57 | }); 58 | 59 | function chatSend() { 60 | var route = "chat.chatHandler.send"; 61 | var target = "*"; 62 | var msg = "msg" 63 | pomelo.request(route, { 64 | rid: rid, 65 | content: msg, 66 | from: username, 67 | target: target 68 | }, function(data) { 69 | cc.log(JSON.stringify(data)); 70 | }); 71 | } 72 | ``` 73 | 74 | ## License 75 | 76 | (The MIT License) 77 | 78 | Copyright (c) 2012-2015 NetEase, Inc. and other contributors 79 | 80 | Permission is hereby granted, free of charge, to any person obtaining 81 | a copy of this software and associated documentation files (the 82 | 'Software'), to deal in the Software without restriction, including 83 | without limitation the rights to use, copy, modify, merge, publish, 84 | distribute, sublicense, and/or sell copies of the Software, and to 85 | permit persons to whom the Software is furnished to do so, subject to 86 | the following conditions: 87 | 88 | The above copyright notice and this permission notice shall be 89 | included in all copies or substantial portions of the Software. 90 | 91 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 92 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 93 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 94 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 95 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 96 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 97 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 98 | -------------------------------------------------------------------------------- /dist/pomelo-cocos2d-js.min.js: -------------------------------------------------------------------------------- 1 | !function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;gL)throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x"+L.toString(16)+" bytes");0>e?e=0:e>>>=0,d.TYPED_ARRAY_SUPPORT?c=d._augment(new Uint8Array(e)):(c.length=e,c._isBuffer=!0);var g;if(d.TYPED_ARRAY_SUPPORT&&"number"==typeof a.byteLength)c._set(a);else if(A(a))if(d.isBuffer(a))for(g=0;e>g;g++)c[g]=a.readUInt8(g);else for(g=0;e>g;g++)c[g]=(a[g]%256+256)%256;else if("string"===f)c.write(a,0,b);else if("number"===f&&!d.TYPED_ARRAY_SUPPORT)for(g=0;e>g;g++)c[g]=0;return e>0&&e<=d.poolSize&&(c.parent=M),c}function e(a,b){if(!(this instanceof e))return new e(a,b);var c=new d(a,b);return delete c.parent,c}function f(a,b,c,d){c=Number(c)||0;var e=a.length-c;d?(d=Number(d),d>e&&(d=e)):d=e;var f=b.length;if(f%2!==0)throw new Error("Invalid hex string");d>f/2&&(d=f/2);for(var g=0;d>g;g++){var h=parseInt(b.substr(2*g,2),16);if(isNaN(h))throw new Error("Invalid hex string");a[c+g]=h}return g}function g(a,b,c,d){var e=G(C(b,a.length-c),a,c,d);return e}function h(a,b,c,d){var e=G(D(b),a,c,d);return e}function i(a,b,c,d){return h(a,b,c,d)}function j(a,b,c,d){var e=G(F(b),a,c,d);return e}function k(a,b,c,d){var e=G(E(b,a.length-c),a,c,d);return e}function l(a,b,c){return I.fromByteArray(0===b&&c===a.length?a:a.slice(b,c))}function m(a,b,c){var d="",e="";c=Math.min(a.length,c);for(var f=b;c>f;f++)a[f]<=127?(d+=H(e)+String.fromCharCode(a[f]),e=""):e+="%"+a[f].toString(16);return d+H(e)}function n(a,b,c){var d="";c=Math.min(a.length,c);for(var e=b;c>e;e++)d+=String.fromCharCode(127&a[e]);return d}function o(a,b,c){var d="";c=Math.min(a.length,c);for(var e=b;c>e;e++)d+=String.fromCharCode(a[e]);return d}function p(a,b,c){var d=a.length;(!b||0>b)&&(b=0),(!c||0>c||c>d)&&(c=d);for(var e="",f=b;c>f;f++)e+=B(a[f]);return e}function q(a,b,c){for(var d=a.slice(b,c),e="",f=0;fa)throw new RangeError("offset is not uint");if(a+b>c)throw new RangeError("Trying to access beyond buffer length")}function s(a,b,c,e,f,g){if(!d.isBuffer(a))throw new TypeError("buffer must be a Buffer instance");if(b>f||g>b)throw new RangeError("value is out of bounds");if(c+e>a.length)throw new RangeError("index out of range")}function t(a,b,c,d){0>b&&(b=65535+b+1);for(var e=0,f=Math.min(a.length-c,2);f>e;e++)a[c+e]=(b&255<<8*(d?e:1-e))>>>8*(d?e:1-e)}function u(a,b,c,d){0>b&&(b=4294967295+b+1);for(var e=0,f=Math.min(a.length-c,4);f>e;e++)a[c+e]=b>>>8*(d?e:3-e)&255}function v(a,b,c,d,e,f){if(b>e||f>b)throw new RangeError("value is out of bounds");if(c+d>a.length)throw new RangeError("index out of range");if(0>c)throw new RangeError("index out of range")}function w(a,b,c,d,e){return e||v(a,b,c,4,3.4028234663852886e38,-3.4028234663852886e38),J.write(a,b,c,d,23,4),c+4}function x(a,b,c,d,e){return e||v(a,b,c,8,1.7976931348623157e308,-1.7976931348623157e308),J.write(a,b,c,d,52,8),c+8}function y(a){if(a=z(a).replace(O,""),a.length<2)return"";for(;a.length%4!==0;)a+="=";return a}function z(a){return a.trim?a.trim():a.replace(/^\s+|\s+$/g,"")}function A(a){return K(a)||d.isBuffer(a)||a&&"object"==typeof a&&"number"==typeof a.length}function B(a){return 16>a?"0"+a.toString(16):a.toString(16)}function C(a,b){b=b||1/0;for(var c,d=a.length,e=null,f=[],g=0;d>g;g++){if(c=a.charCodeAt(g),c>55295&&57344>c){if(!e){if(c>56319){(b-=3)>-1&&f.push(239,191,189);continue}if(g+1===d){(b-=3)>-1&&f.push(239,191,189);continue}e=c;continue}if(56320>c){(b-=3)>-1&&f.push(239,191,189),e=c;continue}c=e-55296<<10|c-56320|65536,e=null}else e&&((b-=3)>-1&&f.push(239,191,189),e=null);if(128>c){if((b-=1)<0)break;f.push(c)}else if(2048>c){if((b-=2)<0)break;f.push(c>>6|192,63&c|128)}else if(65536>c){if((b-=3)<0)break;f.push(c>>12|224,c>>6&63|128,63&c|128)}else{if(!(2097152>c))throw new Error("Invalid code point");if((b-=4)<0)break;f.push(c>>18|240,c>>12&63|128,c>>6&63|128,63&c|128)}}return f}function D(a){for(var b=[],c=0;c>8,e=c%256,f.push(e),f.push(d);return f}function F(a){return I.toByteArray(y(a))}function G(a,b,c,d){for(var e=0;d>e&&!(e+c>=b.length||e>=a.length);e++)b[e+c]=a[e];return e}function H(a){try{return decodeURIComponent(a)}catch(b){return String.fromCharCode(65533)}}var I=a("base64-js"),J=a("ieee754"),K=a("is-array");c.Buffer=d,c.SlowBuffer=e,c.INSPECT_MAX_BYTES=50,d.poolSize=8192;var L=1073741823,M={};d.TYPED_ARRAY_SUPPORT=function(){try{var a=new ArrayBuffer(0),b=new Uint8Array(a);return b.foo=function(){return 42},42===b.foo()&&"function"==typeof b.subarray&&0===new Uint8Array(1).subarray(1,1).byteLength}catch(c){return!1}}(),d.isBuffer=function(a){return!(null==a||!a._isBuffer)},d.compare=function(a,b){if(!d.isBuffer(a)||!d.isBuffer(b))throw new TypeError("Arguments must be Buffers");if(a===b)return 0;for(var c=a.length,e=b.length,f=0,g=Math.min(c,e);g>f&&a[f]===b[f];f++);return f!==g&&(c=a[f],e=b[f]),e>c?-1:c>e?1:0},d.isEncoding=function(a){switch(String(a).toLowerCase()){case"hex":case"utf8":case"utf-8":case"ascii":case"binary":case"base64":case"raw":case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return!0;default:return!1}},d.concat=function(a,b){if(!K(a))throw new TypeError("list argument must be an Array of Buffers.");if(0===a.length)return new d(0);if(1===a.length)return a[0];var c;if(void 0===b)for(b=0,c=0;c>>1;break;case"utf8":case"utf-8":c=C(a).length;break;case"base64":c=F(a).length;break;default:c=a.length}return c},d.prototype.length=void 0,d.prototype.parent=void 0,d.prototype.toString=function(a,b,c){var d=!1;if(b>>>=0,c=void 0===c||c===1/0?this.length:c>>>0,a||(a="utf8"),0>b&&(b=0),c>this.length&&(c=this.length),b>=c)return"";for(;;)switch(a){case"hex":return p(this,b,c);case"utf8":case"utf-8":return m(this,b,c);case"ascii":return n(this,b,c);case"binary":return o(this,b,c);case"base64":return l(this,b,c);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return q(this,b,c);default:if(d)throw new TypeError("Unknown encoding: "+a);a=(a+"").toLowerCase(),d=!0}},d.prototype.equals=function(a){if(!d.isBuffer(a))throw new TypeError("Argument must be a Buffer");return this===a?!0:0===d.compare(this,a)},d.prototype.inspect=function(){var a="",b=c.INSPECT_MAX_BYTES;return this.length>0&&(a=this.toString("hex",0,b).match(/.{2}/g).join(" "),this.length>b&&(a+=" ... ")),""},d.prototype.compare=function(a){if(!d.isBuffer(a))throw new TypeError("Argument must be a Buffer");return this===a?0:d.compare(this,a)},d.prototype.indexOf=function(a,b){function c(a,b,c){for(var d=-1,e=0;c+e2147483647?b=2147483647:-2147483648>b&&(b=-2147483648),b>>=0,0===this.length)return-1;if(b>=this.length)return-1;if(0>b&&(b=Math.max(this.length+b,0)),"string"==typeof a)return 0===a.length?-1:String.prototype.indexOf.call(this,a,b);if(d.isBuffer(a))return c(this,a,b);if("number"==typeof a)return d.TYPED_ARRAY_SUPPORT&&"function"===Uint8Array.prototype.indexOf?Uint8Array.prototype.indexOf.call(this,a,b):c(this,[a],b);throw new TypeError("val must be string, number or Buffer")},d.prototype.get=function(a){return console.log(".get() is deprecated. Access using array indexes instead."),this.readUInt8(a)},d.prototype.set=function(a,b){return console.log(".set() is deprecated. Access using array indexes instead."),this.writeUInt8(a,b)},d.prototype.write=function(a,b,c,d){if(isFinite(b))isFinite(c)||(d=c,c=void 0);else{var e=d;d=b,b=c,c=e}if(b=Number(b)||0,0>c||0>b||b>this.length)throw new RangeError("attempt to write outside buffer bounds");var l=this.length-b;c?(c=Number(c),c>l&&(c=l)):c=l,d=String(d||"utf8").toLowerCase();var m;switch(d){case"hex":m=f(this,a,b,c);break;case"utf8":case"utf-8":m=g(this,a,b,c);break;case"ascii":m=h(this,a,b,c);break;case"binary":m=i(this,a,b,c);break;case"base64":m=j(this,a,b,c);break;case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":m=k(this,a,b,c);break;default:throw new TypeError("Unknown encoding: "+d)}return m},d.prototype.toJSON=function(){return{type:"Buffer",data:Array.prototype.slice.call(this._arr||this,0)}},d.prototype.slice=function(a,b){var c=this.length;a=~~a,b=void 0===b?c:~~b,0>a?(a+=c,0>a&&(a=0)):a>c&&(a=c),0>b?(b+=c,0>b&&(b=0)):b>c&&(b=c),a>b&&(b=a);var e;if(d.TYPED_ARRAY_SUPPORT)e=d._augment(this.subarray(a,b));else{var f=b-a;e=new d(f,void 0);for(var g=0;f>g;g++)e[g]=this[g+a]}return e.length&&(e.parent=this.parent||this),e},d.prototype.readUIntLE=function(a,b,c){a>>>=0,b>>>=0,c||r(a,b,this.length);for(var d=this[a],e=1,f=0;++f>>=0,b>>>=0,c||r(a,b,this.length);for(var d=this[a+--b],e=1;b>0&&(e*=256);)d+=this[a+--b]*e;return d},d.prototype.readUInt8=function(a,b){return b||r(a,1,this.length),this[a]},d.prototype.readUInt16LE=function(a,b){return b||r(a,2,this.length),this[a]|this[a+1]<<8},d.prototype.readUInt16BE=function(a,b){return b||r(a,2,this.length),this[a]<<8|this[a+1]},d.prototype.readUInt32LE=function(a,b){return b||r(a,4,this.length),(this[a]|this[a+1]<<8|this[a+2]<<16)+16777216*this[a+3]},d.prototype.readUInt32BE=function(a,b){return b||r(a,4,this.length),16777216*this[a]+(this[a+1]<<16|this[a+2]<<8|this[a+3])},d.prototype.readIntLE=function(a,b,c){a>>>=0,b>>>=0,c||r(a,b,this.length);for(var d=this[a],e=1,f=0;++f=e&&(d-=Math.pow(2,8*b)),d},d.prototype.readIntBE=function(a,b,c){a>>>=0,b>>>=0,c||r(a,b,this.length);for(var d=b,e=1,f=this[a+--d];d>0&&(e*=256);)f+=this[a+--d]*e;return e*=128,f>=e&&(f-=Math.pow(2,8*b)),f},d.prototype.readInt8=function(a,b){return b||r(a,1,this.length),128&this[a]?-1*(255-this[a]+1):this[a]},d.prototype.readInt16LE=function(a,b){b||r(a,2,this.length);var c=this[a]|this[a+1]<<8;return 32768&c?4294901760|c:c},d.prototype.readInt16BE=function(a,b){b||r(a,2,this.length);var c=this[a+1]|this[a]<<8;return 32768&c?4294901760|c:c},d.prototype.readInt32LE=function(a,b){return b||r(a,4,this.length),this[a]|this[a+1]<<8|this[a+2]<<16|this[a+3]<<24},d.prototype.readInt32BE=function(a,b){return b||r(a,4,this.length),this[a]<<24|this[a+1]<<16|this[a+2]<<8|this[a+3]},d.prototype.readFloatLE=function(a,b){return b||r(a,4,this.length),J.read(this,a,!0,23,4)},d.prototype.readFloatBE=function(a,b){return b||r(a,4,this.length),J.read(this,a,!1,23,4)},d.prototype.readDoubleLE=function(a,b){return b||r(a,8,this.length),J.read(this,a,!0,52,8)},d.prototype.readDoubleBE=function(a,b){return b||r(a,8,this.length),J.read(this,a,!1,52,8)},d.prototype.writeUIntLE=function(a,b,c,d){a=+a,b>>>=0,c>>>=0,d||s(this,a,b,c,Math.pow(2,8*c),0);var e=1,f=0;for(this[b]=255&a;++f>>0&255;return b+c},d.prototype.writeUIntBE=function(a,b,c,d){a=+a,b>>>=0,c>>>=0,d||s(this,a,b,c,Math.pow(2,8*c),0);var e=c-1,f=1;for(this[b+e]=255&a;--e>=0&&(f*=256);)this[b+e]=a/f>>>0&255;return b+c},d.prototype.writeUInt8=function(a,b,c){return a=+a,b>>>=0,c||s(this,a,b,1,255,0),d.TYPED_ARRAY_SUPPORT||(a=Math.floor(a)),this[b]=a,b+1},d.prototype.writeUInt16LE=function(a,b,c){return a=+a,b>>>=0,c||s(this,a,b,2,65535,0),d.TYPED_ARRAY_SUPPORT?(this[b]=a,this[b+1]=a>>>8):t(this,a,b,!0),b+2},d.prototype.writeUInt16BE=function(a,b,c){return a=+a,b>>>=0,c||s(this,a,b,2,65535,0),d.TYPED_ARRAY_SUPPORT?(this[b]=a>>>8,this[b+1]=a):t(this,a,b,!1),b+2},d.prototype.writeUInt32LE=function(a,b,c){return a=+a,b>>>=0,c||s(this,a,b,4,4294967295,0),d.TYPED_ARRAY_SUPPORT?(this[b+3]=a>>>24,this[b+2]=a>>>16,this[b+1]=a>>>8,this[b]=a):u(this,a,b,!0),b+4},d.prototype.writeUInt32BE=function(a,b,c){return a=+a,b>>>=0,c||s(this,a,b,4,4294967295,0),d.TYPED_ARRAY_SUPPORT?(this[b]=a>>>24,this[b+1]=a>>>16,this[b+2]=a>>>8,this[b+3]=a):u(this,a,b,!1),b+4},d.prototype.writeIntLE=function(a,b,c,d){a=+a,b>>>=0,d||s(this,a,b,c,Math.pow(2,8*c-1)-1,-Math.pow(2,8*c-1));var e=0,f=1,g=0>a?1:0;for(this[b]=255&a;++e>0)-g&255;return b+c},d.prototype.writeIntBE=function(a,b,c,d){a=+a,b>>>=0,d||s(this,a,b,c,Math.pow(2,8*c-1)-1,-Math.pow(2,8*c-1));var e=c-1,f=1,g=0>a?1:0;for(this[b+e]=255&a;--e>=0&&(f*=256);)this[b+e]=(a/f>>0)-g&255;return b+c},d.prototype.writeInt8=function(a,b,c){return a=+a,b>>>=0,c||s(this,a,b,1,127,-128),d.TYPED_ARRAY_SUPPORT||(a=Math.floor(a)),0>a&&(a=255+a+1),this[b]=a,b+1},d.prototype.writeInt16LE=function(a,b,c){return a=+a,b>>>=0,c||s(this,a,b,2,32767,-32768),d.TYPED_ARRAY_SUPPORT?(this[b]=a,this[b+1]=a>>>8):t(this,a,b,!0),b+2},d.prototype.writeInt16BE=function(a,b,c){return a=+a,b>>>=0,c||s(this,a,b,2,32767,-32768),d.TYPED_ARRAY_SUPPORT?(this[b]=a>>>8,this[b+1]=a):t(this,a,b,!1),b+2},d.prototype.writeInt32LE=function(a,b,c){return a=+a,b>>>=0,c||s(this,a,b,4,2147483647,-2147483648),d.TYPED_ARRAY_SUPPORT?(this[b]=a,this[b+1]=a>>>8,this[b+2]=a>>>16,this[b+3]=a>>>24):u(this,a,b,!0),b+4},d.prototype.writeInt32BE=function(a,b,c){return a=+a,b>>>=0,c||s(this,a,b,4,2147483647,-2147483648),0>a&&(a=4294967295+a+1),d.TYPED_ARRAY_SUPPORT?(this[b]=a>>>24,this[b+1]=a>>>16,this[b+2]=a>>>8,this[b+3]=a):u(this,a,b,!1),b+4},d.prototype.writeFloatLE=function(a,b,c){return w(this,a,b,!0,c)},d.prototype.writeFloatBE=function(a,b,c){return w(this,a,b,!1,c)},d.prototype.writeDoubleLE=function(a,b,c){return x(this,a,b,!0,c)},d.prototype.writeDoubleBE=function(a,b,c){return x(this,a,b,!1,c)},d.prototype.copy=function(a,b,c,e){if(c||(c=0),e||0===e||(e=this.length),b>=a.length&&(b=a.length),b||(b=0),e>0&&c>e&&(e=c),e===c)return 0;if(0===a.length||0===this.length)return 0;if(0>b)throw new RangeError("targetStart out of bounds");if(0>c||c>=this.length)throw new RangeError("sourceStart out of bounds");if(0>e)throw new RangeError("sourceEnd out of bounds");e>this.length&&(e=this.length),a.length-bf||!d.TYPED_ARRAY_SUPPORT)for(var g=0;f>g;g++)a[g+b]=this[g+c];else a._set(this.subarray(c,c+f),b);return f},d.prototype.fill=function(a,b,c){if(a||(a=0),b||(b=0),c||(c=this.length),b>c)throw new RangeError("end < start");if(c!==b&&0!==this.length){if(0>b||b>=this.length)throw new RangeError("start out of bounds");if(0>c||c>this.length)throw new RangeError("end out of bounds");var d;if("number"==typeof a)for(d=b;c>d;d++)this[d]=a;else{var e=C(a.toString()),f=e.length;for(d=b;c>d;d++)this[d]=e[d%f]}return this}},d.prototype.toArrayBuffer=function(){if("undefined"!=typeof Uint8Array){if(d.TYPED_ARRAY_SUPPORT)return new d(this).buffer;for(var a=new Uint8Array(this.length),b=0,c=a.length;c>b;b+=1)a[b]=this[b];return a.buffer}throw new TypeError("Buffer.toArrayBuffer not supported in this browser")};var N=d.prototype;d._augment=function(a){return a.constructor=d,a._isBuffer=!0,a._set=a.set,a.get=N.get,a.set=N.set,a.write=N.write,a.toString=N.toString,a.toLocaleString=N.toString,a.toJSON=N.toJSON,a.equals=N.equals,a.compare=N.compare,a.indexOf=N.indexOf,a.copy=N.copy,a.slice=N.slice,a.readUIntLE=N.readUIntLE,a.readUIntBE=N.readUIntBE,a.readUInt8=N.readUInt8,a.readUInt16LE=N.readUInt16LE,a.readUInt16BE=N.readUInt16BE,a.readUInt32LE=N.readUInt32LE,a.readUInt32BE=N.readUInt32BE,a.readIntLE=N.readIntLE,a.readIntBE=N.readIntBE,a.readInt8=N.readInt8,a.readInt16LE=N.readInt16LE,a.readInt16BE=N.readInt16BE,a.readInt32LE=N.readInt32LE,a.readInt32BE=N.readInt32BE,a.readFloatLE=N.readFloatLE,a.readFloatBE=N.readFloatBE,a.readDoubleLE=N.readDoubleLE,a.readDoubleBE=N.readDoubleBE,a.writeUInt8=N.writeUInt8,a.writeUIntLE=N.writeUIntLE,a.writeUIntBE=N.writeUIntBE,a.writeUInt16LE=N.writeUInt16LE,a.writeUInt16BE=N.writeUInt16BE,a.writeUInt32LE=N.writeUInt32LE,a.writeUInt32BE=N.writeUInt32BE,a.writeIntLE=N.writeIntLE,a.writeIntBE=N.writeIntBE,a.writeInt8=N.writeInt8,a.writeInt16LE=N.writeInt16LE,a.writeInt16BE=N.writeInt16BE,a.writeInt32LE=N.writeInt32LE,a.writeInt32BE=N.writeInt32BE,a.writeFloatLE=N.writeFloatLE,a.writeFloatBE=N.writeFloatBE,a.writeDoubleLE=N.writeDoubleLE,a.writeDoubleBE=N.writeDoubleBE,a.fill=N.fill,a.inspect=N.inspect,a.toArrayBuffer=N.toArrayBuffer,a};var O=/[^+\/0-9A-z\-]/g},{"base64-js":3,ieee754:4,"is-array":5}],3:[function(a,b,c){var d="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";!function(a){"use strict";function b(a){var b=a.charCodeAt(0);return b===g||b===l?62:b===h||b===m?63:i>b?-1:i+10>b?b-i+26+26:k+26>b?b-k:j+26>b?b-j+26:void 0}function c(a){function c(a){j[l++]=a}var d,e,g,h,i,j;if(a.length%4>0)throw new Error("Invalid string. Length must be a multiple of 4");var k=a.length;i="="===a.charAt(k-2)?2:"="===a.charAt(k-1)?1:0,j=new f(3*a.length/4-i),g=i>0?a.length-4:a.length;var l=0;for(d=0,e=0;g>d;d+=4,e+=3)h=b(a.charAt(d))<<18|b(a.charAt(d+1))<<12|b(a.charAt(d+2))<<6|b(a.charAt(d+3)),c((16711680&h)>>16),c((65280&h)>>8),c(255&h);return 2===i?(h=b(a.charAt(d))<<2|b(a.charAt(d+1))>>4,c(255&h)):1===i&&(h=b(a.charAt(d))<<10|b(a.charAt(d+1))<<4|b(a.charAt(d+2))>>2,c(h>>8&255),c(255&h)),j}function e(a){function b(a){return d.charAt(a)}function c(a){return b(a>>18&63)+b(a>>12&63)+b(a>>6&63)+b(63&a)}var e,f,g,h=a.length%3,i="";for(e=0,g=a.length-h;g>e;e+=3)f=(a[e]<<16)+(a[e+1]<<8)+a[e+2],i+=c(f);switch(h){case 1:f=a[a.length-1],i+=b(f>>2),i+=b(f<<4&63),i+="==";break;case 2:f=(a[a.length-2]<<8)+a[a.length-1],i+=b(f>>10),i+=b(f>>4&63),i+=b(f<<2&63),i+="="}return i}var f="undefined"!=typeof Uint8Array?Uint8Array:Array,g="+".charCodeAt(0),h="/".charCodeAt(0),i="0".charCodeAt(0),j="a".charCodeAt(0),k="A".charCodeAt(0),l="-".charCodeAt(0),m="_".charCodeAt(0);a.toByteArray=c,a.fromByteArray=e}("undefined"==typeof c?this.base64js={}:c)},{}],4:[function(a,b,c){c.read=function(a,b,c,d,e){var f,g,h=8*e-d-1,i=(1<>1,k=-7,l=c?e-1:0,m=c?-1:1,n=a[b+l];for(l+=m,f=n&(1<<-k)-1,n>>=-k,k+=h;k>0;f=256*f+a[b+l],l+=m,k-=8);for(g=f&(1<<-k)-1,f>>=-k,k+=d;k>0;g=256*g+a[b+l],l+=m,k-=8);if(0===f)f=1-j;else{if(f===i)return g?0/0:(n?-1:1)*(1/0);g+=Math.pow(2,d),f-=j}return(n?-1:1)*g*Math.pow(2,f-d)},c.write=function(a,b,c,d,e,f){var g,h,i,j=8*f-e-1,k=(1<>1,m=23===e?Math.pow(2,-24)-Math.pow(2,-77):0,n=d?0:f-1,o=d?1:-1,p=0>b||0===b&&0>1/b?1:0;for(b=Math.abs(b),isNaN(b)||b===1/0?(h=isNaN(b)?1:0,g=k):(g=Math.floor(Math.log(b)/Math.LN2),b*(i=Math.pow(2,-g))<1&&(g--,i*=2),b+=g+l>=1?m/i:m*Math.pow(2,1-l),b*i>=2&&(g++,i/=2),g+l>=k?(h=0,g=k):g+l>=1?(h=(b*i-1)*Math.pow(2,e),g+=l):(h=b*Math.pow(2,l-1)*Math.pow(2,e),g=0));e>=8;a[c+n]=255&h,n+=o,h/=256,e-=8);for(g=g<0;a[c+n]=255&g,n+=o,g/=256,j-=8);a[c+n-o]|=128*p}},{}],5:[function(a,b){var c=Array.isArray,d=Object.prototype.toString;b.exports=c||function(a){return!!a&&"[object Array]"==d.call(a)}},{}],6:[function(a,b){function c(){this._events=this._events||{},this._maxListeners=this._maxListeners||void 0}function d(a){return"function"==typeof a}function e(a){return"number"==typeof a}function f(a){return"object"==typeof a&&null!==a}function g(a){return void 0===a}b.exports=c,c.EventEmitter=c,c.prototype._events=void 0,c.prototype._maxListeners=void 0,c.defaultMaxListeners=10,c.prototype.setMaxListeners=function(a){if(!e(a)||0>a||isNaN(a))throw TypeError("n must be a positive number");return this._maxListeners=a,this},c.prototype.emit=function(a){var b,c,e,h,i,j;if(this._events||(this._events={}),"error"===a&&(!this._events.error||f(this._events.error)&&!this._events.error.length)){if(b=arguments[1],b instanceof Error)throw b;throw TypeError('Uncaught, unspecified "error" event.')}if(c=this._events[a],g(c))return!1;if(d(c))switch(arguments.length){case 1:c.call(this);break;case 2:c.call(this,arguments[1]);break;case 3:c.call(this,arguments[1],arguments[2]);break;default:for(e=arguments.length,h=new Array(e-1),i=1;e>i;i++)h[i-1]=arguments[i];c.apply(this,h)}else if(f(c)){for(e=arguments.length,h=new Array(e-1),i=1;e>i;i++)h[i-1]=arguments[i];for(j=c.slice(),e=j.length,i=0;e>i;i++)j[i].apply(this,h)}return!0},c.prototype.addListener=function(a,b){var e;if(!d(b))throw TypeError("listener must be a function");if(this._events||(this._events={}),this._events.newListener&&this.emit("newListener",a,d(b.listener)?b.listener:b),this._events[a]?f(this._events[a])?this._events[a].push(b):this._events[a]=[this._events[a],b]:this._events[a]=b,f(this._events[a])&&!this._events[a].warned){var e;e=g(this._maxListeners)?c.defaultMaxListeners:this._maxListeners,e&&e>0&&this._events[a].length>e&&(this._events[a].warned=!0,console.error("(node) warning: possible EventEmitter memory leak detected. %d listeners added. Use emitter.setMaxListeners() to increase limit.",this._events[a].length),"function"==typeof console.trace&&console.trace())}return this},c.prototype.on=c.prototype.addListener,c.prototype.once=function(a,b){function c(){this.removeListener(a,c),e||(e=!0,b.apply(this,arguments))}if(!d(b))throw TypeError("listener must be a function");var e=!1;return c.listener=b,this.on(a,c),this},c.prototype.removeListener=function(a,b){var c,e,g,h;if(!d(b))throw TypeError("listener must be a function");if(!this._events||!this._events[a])return this;if(c=this._events[a],g=c.length,e=-1,c===b||d(c.listener)&&c.listener===b)delete this._events[a],this._events.removeListener&&this.emit("removeListener",a,b);else if(f(c)){for(h=g;h-->0;)if(c[h]===b||c[h].listener&&c[h].listener===b){e=h;break}if(0>e)return this;1===c.length?(c.length=0,delete this._events[a]):c.splice(e,1),this._events.removeListener&&this.emit("removeListener",a,b)}return this},c.prototype.removeAllListeners=function(a){var b,c;if(!this._events)return this;if(!this._events.removeListener)return 0===arguments.length?this._events={}:this._events[a]&&delete this._events[a],this;if(0===arguments.length){for(b in this._events)"removeListener"!==b&&this.removeAllListeners(b);return this.removeAllListeners("removeListener"),this._events={},this}if(c=this._events[a],d(c))this.removeListener(a,c);else for(;c.length;)this.removeListener(a,c[c.length-1]);return delete this._events[a],this},c.prototype.listeners=function(a){var b;return b=this._events&&this._events[a]?d(this._events[a])?[this._events[a]]:this._events[a].slice():[]},c.listenerCount=function(a,b){var c;return c=a._events&&a._events[b]?d(a._events[b])?1:a._events[b].length:0}},{}],7:[function(a,b){b.exports="function"==typeof Object.create?function(a,b){a.super_=b,a.prototype=Object.create(b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}})}:function(a,b){a.super_=b;var c=function(){};c.prototype=b.prototype,a.prototype=new c,a.prototype.constructor=a}},{}],8:[function(a,b){function c(){if(!g){g=!0;for(var a,b=f.length;b;){a=f,f=[];for(var c=-1;++c=3&&(d.depth=arguments[2]),arguments.length>=4&&(d.colors=arguments[3]),p(b)?d.showHidden=b:b&&c._extend(d,b),v(d.showHidden)&&(d.showHidden=!1),v(d.depth)&&(d.depth=2),v(d.colors)&&(d.colors=!1),v(d.customInspect)&&(d.customInspect=!0),d.colors&&(d.stylize=f),i(d,a,d.depth)}function f(a,b){var c=e.styles[b];return c?"["+e.colors[c][0]+"m"+a+"["+e.colors[c][1]+"m":a}function g(a){return a}function h(a){var b={};return a.forEach(function(a){b[a]=!0}),b}function i(a,b,d){if(a.customInspect&&b&&A(b.inspect)&&b.inspect!==c.inspect&&(!b.constructor||b.constructor.prototype!==b)){var e=b.inspect(d,a);return t(e)||(e=i(a,e,d)),e}var f=j(a,b);if(f)return f;var g=Object.keys(b),p=h(g);if(a.showHidden&&(g=Object.getOwnPropertyNames(b)),z(b)&&(g.indexOf("message")>=0||g.indexOf("description")>=0))return k(b);if(0===g.length){if(A(b)){var q=b.name?": "+b.name:"";return a.stylize("[Function"+q+"]","special")}if(w(b))return a.stylize(RegExp.prototype.toString.call(b),"regexp");if(y(b))return a.stylize(Date.prototype.toString.call(b),"date");if(z(b))return k(b)}var r="",s=!1,u=["{","}"];if(o(b)&&(s=!0,u=["[","]"]),A(b)){var v=b.name?": "+b.name:"";r=" [Function"+v+"]"}if(w(b)&&(r=" "+RegExp.prototype.toString.call(b)),y(b)&&(r=" "+Date.prototype.toUTCString.call(b)),z(b)&&(r=" "+k(b)),0===g.length&&(!s||0==b.length))return u[0]+r+u[1];if(0>d)return w(b)?a.stylize(RegExp.prototype.toString.call(b),"regexp"):a.stylize("[Object]","special");a.seen.push(b);var x;return x=s?l(a,b,d,p,g):g.map(function(c){return m(a,b,d,p,c,s)}),a.seen.pop(),n(x,r,u)}function j(a,b){if(v(b))return a.stylize("undefined","undefined");if(t(b)){var c="'"+JSON.stringify(b).replace(/^"|"$/g,"").replace(/'/g,"\\'").replace(/\\"/g,'"')+"'";return a.stylize(c,"string")}return s(b)?a.stylize(""+b,"number"):p(b)?a.stylize(""+b,"boolean"):q(b)?a.stylize("null","null"):void 0}function k(a){return"["+Error.prototype.toString.call(a)+"]"}function l(a,b,c,d,e){for(var f=[],g=0,h=b.length;h>g;++g)f.push(F(b,String(g))?m(a,b,c,d,String(g),!0):"");return e.forEach(function(e){e.match(/^\d+$/)||f.push(m(a,b,c,d,e,!0))}),f}function m(a,b,c,d,e,f){var g,h,j;if(j=Object.getOwnPropertyDescriptor(b,e)||{value:b[e]},j.get?h=j.set?a.stylize("[Getter/Setter]","special"):a.stylize("[Getter]","special"):j.set&&(h=a.stylize("[Setter]","special")),F(d,e)||(g="["+e+"]"),h||(a.seen.indexOf(j.value)<0?(h=q(c)?i(a,j.value,null):i(a,j.value,c-1),h.indexOf("\n")>-1&&(h=f?h.split("\n").map(function(a){return" "+a}).join("\n").substr(2):"\n"+h.split("\n").map(function(a){return" "+a}).join("\n"))):h=a.stylize("[Circular]","special")),v(g)){if(f&&e.match(/^\d+$/))return h;g=JSON.stringify(""+e),g.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)?(g=g.substr(1,g.length-2),g=a.stylize(g,"name")):(g=g.replace(/'/g,"\\'").replace(/\\"/g,'"').replace(/(^"|"$)/g,"'"),g=a.stylize(g,"string"))}return g+": "+h}function n(a,b,c){var d=0,e=a.reduce(function(a,b){return d++,b.indexOf("\n")>=0&&d++,a+b.replace(/\u001b\[\d\d?m/g,"").length+1},0);return e>60?c[0]+(""===b?"":b+"\n ")+" "+a.join(",\n ")+" "+c[1]:c[0]+b+" "+a.join(", ")+" "+c[1]}function o(a){return Array.isArray(a)}function p(a){return"boolean"==typeof a}function q(a){return null===a}function r(a){return null==a}function s(a){return"number"==typeof a}function t(a){return"string"==typeof a}function u(a){return"symbol"==typeof a}function v(a){return void 0===a}function w(a){return x(a)&&"[object RegExp]"===C(a)}function x(a){return"object"==typeof a&&null!==a}function y(a){return x(a)&&"[object Date]"===C(a)}function z(a){return x(a)&&("[object Error]"===C(a)||a instanceof Error)}function A(a){return"function"==typeof a}function B(a){return null===a||"boolean"==typeof a||"number"==typeof a||"string"==typeof a||"symbol"==typeof a||"undefined"==typeof a}function C(a){return Object.prototype.toString.call(a)}function D(a){return 10>a?"0"+a.toString(10):a.toString(10)}function E(){var a=new Date,b=[D(a.getHours()),D(a.getMinutes()),D(a.getSeconds())].join(":");return[a.getDate(),J[a.getMonth()],b].join(" ")}function F(a,b){return Object.prototype.hasOwnProperty.call(a,b)}var G=/%[sdj%]/g;c.format=function(a){if(!t(a)){for(var b=[],c=0;c=f)return a;switch(a){case"%s":return String(d[c++]);case"%d":return Number(d[c++]);case"%j":try{return JSON.stringify(d[c++])}catch(b){return"[Circular]"}default:return a}}),h=d[c];f>c;h=d[++c])g+=q(h)||!x(h)?" "+h:" "+e(h);return g},c.deprecate=function(a,e){function f(){if(!g){if(b.throwDeprecation)throw new Error(e);b.traceDeprecation?console.trace(e):console.error(e),g=!0}return a.apply(this,arguments)}if(v(d.process))return function(){return c.deprecate(a,e).apply(this,arguments)};if(b.noDeprecation===!0)return a;var g=!1;return f};var H,I={};c.debuglog=function(a){if(v(H)&&(H=b.env.NODE_DEBUG||""),a=a.toUpperCase(),!I[a])if(new RegExp("\\b"+a+"\\b","i").test(H)){var d=b.pid;I[a]=function(){var b=c.format.apply(c,arguments);console.error("%s %d: %s",a,d,b)}}else I[a]=function(){};return I[a]},c.inspect=e,e.colors={bold:[1,22],italic:[3,23],underline:[4,24],inverse:[7,27],white:[37,39],grey:[90,39],black:[30,39],blue:[34,39],cyan:[36,39],green:[32,39],magenta:[35,39],red:[31,39],yellow:[33,39]},e.styles={special:"cyan",number:"yellow","boolean":"yellow",undefined:"grey","null":"bold",string:"green",date:"magenta",regexp:"red"},c.isArray=o,c.isBoolean=p,c.isNull=q,c.isNullOrUndefined=r,c.isNumber=s,c.isString=t,c.isSymbol=u,c.isUndefined=v,c.isRegExp=w,c.isObject=x,c.isDate=y,c.isError=z,c.isFunction=A,c.isPrimitive=B,c.isBuffer=a("./support/isBuffer");var J=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];c.log=function(){console.log("%s - %s",E(),c.format.apply(c,arguments))},c.inherits=a("inherits"),c._extend=function(a,b){if(!b||!x(b))return a;for(var c=Object.keys(b),d=c.length;d--;)a[c[d]]=b[c[d]];return a}}).call(this,a("_process"),"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"./support/isBuffer":9,_process:8,inherits:7}],11:[function(a,b){!function(){var a="js-websocket",c="0.0.1",d=window.Protocol,e=window.protobuf,f=window.decodeIO_protobuf,g=null,h=null,i=d.Package,j=d.Message,k=window.EventEmitter,l=window.rsa;"undefined"!=typeof window&&"undefined"!=typeof sys&&sys.localStorage&&(window.localStorage=sys.localStorage);var m=200,n=501;"function"!=typeof Object.create&&(Object.create=function(a){function b(){}return b.prototype=a,new b});var o=window,p=Object.create(k.prototype);o.pomelo=p;var q,r=null,s=0,t={},u={},v={},w={},x={},y={},z={},A=0,B=0,C=0,D=0,E=100,F=null,G=null,H=null,I=null,J=null,K=!1,L=null,M=null,N=0,O=5e3,P=10,Q={sys:{type:a,version:c,rsa:{}},user:{}},R=null;p.init=function(a,b){R=b;var c=a.host,d=a.port;J=a.encode||T,I=a.decode||S;var e="ws://"+c;if(d&&(e+=":"+d),Q.user=a.user,a.encrypt){q=!0,l.generate(1024,"10001");var f={rsa_n:l.n.toString(16),rsa_e:l.e};Q.sys.rsa=f}H=a.handshakeCallback,U(a,e,b)};var S=p.decode=function(a){var b=j.decode(a);if(!(b.id>0)||(b.route=v[b.id],delete v[b.id], 2 | b.route))return b.body=da(b),b},T=p.encode=function(a,b,c){var f=a?j.TYPE_REQUEST:j.TYPE_NOTIFY;if(e&&z[b])c=e.encode(b,c);else if(g&&g.lookup(b)){var h=g.build(b);c=new h(c).encodeNB()}else c=d.strencode(JSON.stringify(c));var i=0;return w&&w[b]&&(b=w[b],i=1),j.encode(a,f,i,b,c)},U=function(a,b,c){console.log("connect to "+b);var a=a||{},j=a.maxReconnectAttempts||P;if(M=b,window.localStorage&&window.localStorage.getItem("protos")&&0===A){var k=JSON.parse(window.localStorage.getItem("protos"));A=k.version||0,y=k.server||{},z=k.client||{},e&&e.init({encoderProtos:z,decoderProtos:y}),f&&(g=f.loadJson(z),h=f.loadJson(y))}Q.sys.protoVersion=A;var l=function(){K&&p.emit("reconnect"),V();var a=i.encode(i.TYPE_HANDSHAKE,d.strencode(JSON.stringify(Q)));X(a)},m=function(a){ba(i.decode(a.data),c),C&&(D=Date.now()+C)},n=function(a){p.emit("io-error",a),console.error("socket error: ",a)},o=function(b){p.emit("close",b),p.emit("disconnect",b),console.error("socket close: ",b),a.reconnect&&j>N&&(K=!0,N++,L=setTimeout(function(){U(a,M,c)},O),O*=2)};r=new WebSocket(b),r.binaryType="arraybuffer",r.onopen=l,r.onmessage=m,r.onerror=n,r.onclose=o};p.disconnect=function(){r&&(r.disconnect&&r.disconnect(),r.close&&r.close(),console.log("disconnect"),r=null),F&&(clearTimeout(F),F=null),G&&(clearTimeout(G),G=null)};var V=function(){K=!1,O=5e3,N=0,clearTimeout(L)};p.request=function(a,b,c){2===arguments.length&&"function"==typeof b?(c=b,b={}):b=b||{},a=a||b.route,a&&(s++,W(s,a,b),t[s]=c,v[s]=a)},p.notify=function(a,b){b=b||{},W(0,a,b)};var W=function(a,b,c){if(q){c=JSON.stringify(c);var d=l.signString(c,"sha256");c=JSON.parse(c),c.__crypto__=d}J&&(c=J(a,b,c));var e=i.encode(i.TYPE_DATA,c);X(e)},X=function(a){r&&r.send(a.buffer)},Y=function(){if(B){var a=i.encode(i.TYPE_HEARTBEAT);G&&(clearTimeout(G),G=null),F||(F=setTimeout(function(){F=null,X(a),D=Date.now()+C,G=setTimeout(Z,C)},B))}},Z=function(){var a=D-Date.now();a>E?G=setTimeout(Z,a):(console.error("server heartbeat timeout"),p.emit("heartbeat timeout"),p.disconnect())},$=function(a){if(a=JSON.parse(d.strdecode(a)),a.code===n)return void p.emit("error","client version not fullfill");if(a.code!==m)return void p.emit("error","handshake fail");ea(a);var b=i.encode(i.TYPE_HANDSHAKE_ACK);X(b),R&&R(r)},_=function(a){var b=a;I&&(b=I(b)),ca(p,b)},aa=function(a){a=JSON.parse(d.strdecode(a)),p.emit("onKick",a)};u[i.TYPE_HANDSHAKE]=$,u[i.TYPE_HEARTBEAT]=Y,u[i.TYPE_DATA]=_,u[i.TYPE_KICK]=aa;var ba=function(a){if(Array.isArray(a))for(var b=0;bb)return console.log(b),null;var c=[];do{var d=b%128,e=Math.floor(b/128);0!==e&&(d+=128),c.push(d),b=e}while(0!==b);return c},c.encodeSInt32=function(a){var b=parseInt(a);return isNaN(b)?null:(b=0>b?2*Math.abs(b)-1:2*b,c.encodeUInt32(b))},c.decodeUInt32=function(a){for(var b=0,c=0;cd)return b}return b},c.decodeSInt32=function(a){var b=this.decodeUInt32(a),c=b%2===1?-1:1;return b=(b%2+b)/2*c}},{}],13:[function(a,b){b.exports={TYPES:{uInt32:0,sInt32:0,int32:0,"double":1,string:2,message:2,"float":5}}},{}],14:[function(a,b){function c(a,b,c){for(;c>l;){var g=d(),h=(g.type,g.tag),i=b.__tags[h];switch(b[i].option){case"optional":case"required":a[i]=e(b[i].type,b);break;case"repeated":a[i]||(a[i]=[]),f(a[i],b[i].type,b)}}return a}function d(){var a=i.decodeUInt32(g());return{type:7&a,tag:a>>3}}function e(a,b){switch(a){case"uInt32":return i.decodeUInt32(g());case"int32":case"sInt32":return i.decodeSInt32(g());case"float":var d=h.readFloatLE(l);return l+=4,d;case"double":var e=h.readDoubleLE(l);return l+=8,e;case"string":var f=i.decodeUInt32(g()),j=h.toString("utf8",l,l+f);return l+=f,j;default:var m=b&&(b.__messages[a]||k.protos["message "+a]);if(m){var f=i.decodeUInt32(g()),n={};return c(n,m,l+f),n}}}function f(a,b,c){if(j.isSimpleType(b))for(var d=i.decodeUInt32(g()),f=0;d>f;f++)a.push(e(b));else a.push(e(b,c))}function g(a){var b=[],c=l;a=a||!1;var d;do{var d=h.readUInt8(c);b.push(d),c++}while(d>=128);return a||(l=c),b}var h,i=a("./codec"),j=a("./util"),k=b.exports,l=0;k.init=function(a){this.protos=a||{}},k.setProtos=function(a){a&&(this.protos=a)},k.decode=function(a,b){var d=this.protos[a];return h=b,l=0,d?c({},d,h.length):null}},{"./codec":12,"./util":18}],15:[function(a,b){(function(c){function d(a,b){if(!b||!a)return console.warn("no protos or msg exist! msg : %j, protos : %j",a,b),!1;for(var c in b){var e=b[c];switch(e.option){case"required":if("undefined"==typeof a[c])return console.warn("no property exist for required! name: %j, proto: %j, msg: %j",c,e,a),!1;case"optional":if("undefined"!=typeof a[c]){var f=b.__messages[e.type]||m.protos["message "+e.type];if(f&&!d(a[c],f))return console.warn("inner proto error! name: %j, proto: %j, msg: %j",c,e,a),!1}break;case"repeated":var f=b.__messages[e.type]||m.protos["message "+e.type];if(a[c]&&f)for(var g=0;g0&&(b=g(d[e],j,b,a,c))}}return b}function f(a,b,d,f,g){var i=0;switch(b){case"uInt32":d=h(f,d,j.encodeUInt32(a));break;case"int32":case"sInt32":d=h(f,d,j.encodeSInt32(a));break;case"float":f.writeFloatLE(a,d),d+=4;break;case"double":f.writeDoubleLE(a,d),d+=8;break;case"string":i=c.byteLength(a),d=h(f,d,j.encodeUInt32(i)),f.write(a,d,i),d+=i;break;default:var k=g.__messages[b]||m.protos["message "+b];if(k){var l=new c(2*c.byteLength(JSON.stringify(a)));i=0,i=e(l,i,k,a),d=h(f,d,j.encodeUInt32(i)),l.copy(f,d,0,i),d+=i}}return d}function g(a,b,c,d,e){var g=0;if(l.isSimpleType(b.type))for(c=h(d,c,i(b.type,b.tag)),c=h(d,c,j.encodeUInt32(a.length)),g=0;g0)?h.slice(0,i):null}}).call(this,a("buffer").Buffer)},{"./codec":12,"./constant":13,"./util":18,buffer:2}],16:[function(a,b){function c(a){var b={},d={},e={};for(var f in a){var g=a[f],h=f.split(" ");switch(h[0]){case"message":if(2!==h.length)continue;d[h[1]]=c(g);continue;case"required":case"optional":case"repeated":if(3!==h.length||e[g])continue;b[h[2]]={option:h[0],type:h[1],tag:g},e[g]=h[2]}}return b.__messages=d,b.__tags=e,b}var d=b.exports;d.parse=function(a){var b={};for(var d in a)b[d]=c(a[d]);return b}},{}],17:[function(a,b){(function(c){var d=a("./encoder"),e=a("./decoder"),f=a("./parser"),g=b.exports;g.encode=function(a,b){return d.encode(a,b)},g.encode2Bytes=function(a,b){var c=this.encode(a,b);if(!c||!c.length)return console.warn("encode msg failed! key : %j, msg : %j",a,b),null;for(var d=new Uint8Array(c.length),e=0;e=g?[g]:2047>=g?[192|g>>6,128|63&g]:[224|g>>12,128|(4032&g)>>6,128|63&g];for(var i=0;if;)c[f]<128?(g=c[f],f+=1):c[f]<224?(g=((31&c[f])<<6)+(63&c[f+1]),f+=2):(g=((15&c[f])<<12)+((63&c[f+1])<<6)+(63&c[f+2]),f+=3),e.push(g);return String.fromCharCode.apply(null,e)},m.encode=function(a,b){var c=b?b.length:0,e=new d(f+c),g=0;return e[g++]=255&a,e[g++]=c>>16&255,e[g++]=c>>8&255,e[g++]=255&c,b&&o(e,g,b,0,c),e},m.decode=function(a){for(var b=0,c=new d(a),e=0,f=[];b>>0;var h=e?new d(e):null;h&&o(h,0,c,b,e),b+=e,f.push({type:g,body:h})}return 1===f.length?f[0]:f},n.encode=function(a,b,c,f,j){var k=p(b)?r(a):0,l=g+k;if(q(b))if(c){if("number"!=typeof f)throw new Error("error flag for number route!");l+=h}else if(l+=i,f){if(f=e.strencode(f),f.length>255)throw new Error("route maxlength is overflow");l+=f.length}j&&(l+=j.length);var m=new d(l),n=0;return n=s(b,c,m,n),p(b)&&(n=t(a,m,n)),q(b)&&(n=u(c,f,m,n)),j&&(n=v(j,m,n)),m},n.decode=function(a){var b=new d(a),c=b.length||b.byteLength,f=0,g=0,h=null,i=b[f++],j=i&k,m=i>>1&l;if(p(m)){var n=0,r=0;do n=parseInt(b[f]),g+=(127&n)<<7*r,f++,r++;while(n>=128)}if(q(m))if(j)h=b[f++]<<8|b[f++];else{var s=b[f++];s?(h=new d(s),o(h,0,b,f,s),h=e.strdecode(h)):h="",f+=s}var t=c-f,u=new d(t);return o(u,0,b,f,t),{id:g,type:m,compressRoute:j,route:h,body:u}};var o=function(a,b,c,d,e){if("function"==typeof c.copy)c.copy(a,b,d,d+e);else for(var f=0;e>f;f++)a[b++]=c[d++]},p=function(a){return a===n.TYPE_REQUEST||a===n.TYPE_RESPONSE},q=function(a){return a===n.TYPE_REQUEST||a===n.TYPE_NOTIFY||a===n.TYPE_PUSH},r=function(a){var b=0;do b+=1,a>>=7;while(a>0);return b},s=function(a,b,c,d){if(a!==n.TYPE_REQUEST&&a!==n.TYPE_NOTIFY&&a!==n.TYPE_RESPONSE&&a!==n.TYPE_PUSH)throw new Error("unkonw message type: "+a);return c[d]=a<<1|(b?1:0),d+g},t=function(a,b,c){do{var d=a%128,e=Math.floor(a/128);0!==e&&(d+=128),b[c++]=d,a=e}while(0!==a);return c},u=function(a,b,c,d){if(a){if(b>j)throw new Error("route number is overflow");c[d++]=b>>8&255,c[d++]=255&b}else b?(c[d++]=255&b.length,o(c,d,b,0,b.length),d+=b.length):c[d++]=0;return d},v=function(a,b,c){return o(b,c,a,0,a.length),c+a.length};b.exports=e,"undefined"!=typeof window&&(window.Protocol=e)}("undefined"==typeof window?b.exports:this.Protocol={},"undefined"==typeof window?a:Uint8Array,this)}).call(this,a("buffer").Buffer)},{buffer:2}]},{},[1]); -------------------------------------------------------------------------------- /dist/pomelo-cocos2d-js.js: -------------------------------------------------------------------------------- 1 | (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=function(str) {throw new Error('can not require ' + str)};if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=function(str) {throw new Error('can not require ' + str)};for(var o=0;o 36 | * @license MIT 37 | */ 38 | 39 | var base64 = require('base64-js') 40 | var ieee754 = require('ieee754') 41 | var isArray = require('is-array') 42 | 43 | exports.Buffer = Buffer 44 | exports.SlowBuffer = SlowBuffer 45 | exports.INSPECT_MAX_BYTES = 50 46 | Buffer.poolSize = 8192 // not used by this implementation 47 | 48 | var kMaxLength = 0x3fffffff 49 | var rootParent = {} 50 | 51 | /** 52 | * If `Buffer.TYPED_ARRAY_SUPPORT`: 53 | * === true Use Uint8Array implementation (fastest) 54 | * === false Use Object implementation (most compatible, even IE6) 55 | * 56 | * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+, 57 | * Opera 11.6+, iOS 4.2+. 58 | * 59 | * Note: 60 | * 61 | * - Implementation must support adding new properties to `Uint8Array` instances. 62 | * Firefox 4-29 lacked support, fixed in Firefox 30+. 63 | * See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438. 64 | * 65 | * - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function. 66 | * 67 | * - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of 68 | * incorrect length in some situations. 69 | * 70 | * We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they will 71 | * get the Object implementation, which is slower but will work correctly. 72 | */ 73 | Buffer.TYPED_ARRAY_SUPPORT = (function () { 74 | try { 75 | var buf = new ArrayBuffer(0) 76 | var arr = new Uint8Array(buf) 77 | arr.foo = function () { return 42 } 78 | return arr.foo() === 42 && // typed array instances can be augmented 79 | typeof arr.subarray === 'function' && // chrome 9-10 lack `subarray` 80 | new Uint8Array(1).subarray(1, 1).byteLength === 0 // ie10 has broken `subarray` 81 | } catch (e) { 82 | return false 83 | } 84 | })() 85 | 86 | /** 87 | * Class: Buffer 88 | * ============= 89 | * 90 | * The Buffer constructor returns instances of `Uint8Array` that are augmented 91 | * with function properties for all the node `Buffer` API functions. We use 92 | * `Uint8Array` so that square bracket notation works as expected -- it returns 93 | * a single octet. 94 | * 95 | * By augmenting the instances, we can avoid modifying the `Uint8Array` 96 | * prototype. 97 | */ 98 | function Buffer (subject, encoding) { 99 | var self = this 100 | if (!(self instanceof Buffer)) return new Buffer(subject, encoding) 101 | 102 | var type = typeof subject 103 | var length 104 | 105 | if (type === 'number') { 106 | length = +subject 107 | } else if (type === 'string') { 108 | length = Buffer.byteLength(subject, encoding) 109 | } else if (type === 'object' && subject !== null) { 110 | // assume object is array-like 111 | if (subject.type === 'Buffer' && isArray(subject.data)) subject = subject.data 112 | length = +subject.length 113 | } else { 114 | throw new TypeError('must start with number, buffer, array or string') 115 | } 116 | 117 | if (length > kMaxLength) { 118 | throw new RangeError('Attempt to allocate Buffer larger than maximum size: 0x' + 119 | kMaxLength.toString(16) + ' bytes') 120 | } 121 | 122 | if (length < 0) length = 0 123 | else length >>>= 0 // coerce to uint32 124 | 125 | if (Buffer.TYPED_ARRAY_SUPPORT) { 126 | // Preferred: Return an augmented `Uint8Array` instance for best performance 127 | self = Buffer._augment(new Uint8Array(length)) // eslint-disable-line consistent-this 128 | } else { 129 | // Fallback: Return THIS instance of Buffer (created by `new`) 130 | self.length = length 131 | self._isBuffer = true 132 | } 133 | 134 | var i 135 | if (Buffer.TYPED_ARRAY_SUPPORT && typeof subject.byteLength === 'number') { 136 | // Speed optimization -- use set if we're copying from a typed array 137 | self._set(subject) 138 | } else if (isArrayish(subject)) { 139 | // Treat array-ish objects as a byte array 140 | if (Buffer.isBuffer(subject)) { 141 | for (i = 0; i < length; i++) { 142 | self[i] = subject.readUInt8(i) 143 | } 144 | } else { 145 | for (i = 0; i < length; i++) { 146 | self[i] = ((subject[i] % 256) + 256) % 256 147 | } 148 | } 149 | } else if (type === 'string') { 150 | self.write(subject, 0, encoding) 151 | } else if (type === 'number' && !Buffer.TYPED_ARRAY_SUPPORT) { 152 | for (i = 0; i < length; i++) { 153 | self[i] = 0 154 | } 155 | } 156 | 157 | if (length > 0 && length <= Buffer.poolSize) self.parent = rootParent 158 | 159 | return self 160 | } 161 | 162 | function SlowBuffer (subject, encoding) { 163 | if (!(this instanceof SlowBuffer)) return new SlowBuffer(subject, encoding) 164 | 165 | var buf = new Buffer(subject, encoding) 166 | delete buf.parent 167 | return buf 168 | } 169 | 170 | Buffer.isBuffer = function isBuffer (b) { 171 | return !!(b != null && b._isBuffer) 172 | } 173 | 174 | Buffer.compare = function compare (a, b) { 175 | if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) { 176 | throw new TypeError('Arguments must be Buffers') 177 | } 178 | 179 | if (a === b) return 0 180 | 181 | var x = a.length 182 | var y = b.length 183 | for (var i = 0, len = Math.min(x, y); i < len && a[i] === b[i]; i++) {} 184 | if (i !== len) { 185 | x = a[i] 186 | y = b[i] 187 | } 188 | if (x < y) return -1 189 | if (y < x) return 1 190 | return 0 191 | } 192 | 193 | Buffer.isEncoding = function isEncoding (encoding) { 194 | switch (String(encoding).toLowerCase()) { 195 | case 'hex': 196 | case 'utf8': 197 | case 'utf-8': 198 | case 'ascii': 199 | case 'binary': 200 | case 'base64': 201 | case 'raw': 202 | case 'ucs2': 203 | case 'ucs-2': 204 | case 'utf16le': 205 | case 'utf-16le': 206 | return true 207 | default: 208 | return false 209 | } 210 | } 211 | 212 | Buffer.concat = function concat (list, totalLength) { 213 | if (!isArray(list)) throw new TypeError('list argument must be an Array of Buffers.') 214 | 215 | if (list.length === 0) { 216 | return new Buffer(0) 217 | } else if (list.length === 1) { 218 | return list[0] 219 | } 220 | 221 | var i 222 | if (totalLength === undefined) { 223 | totalLength = 0 224 | for (i = 0; i < list.length; i++) { 225 | totalLength += list[i].length 226 | } 227 | } 228 | 229 | var buf = new Buffer(totalLength) 230 | var pos = 0 231 | for (i = 0; i < list.length; i++) { 232 | var item = list[i] 233 | item.copy(buf, pos) 234 | pos += item.length 235 | } 236 | return buf 237 | } 238 | 239 | Buffer.byteLength = function byteLength (str, encoding) { 240 | var ret 241 | str = str + '' 242 | switch (encoding || 'utf8') { 243 | case 'ascii': 244 | case 'binary': 245 | case 'raw': 246 | ret = str.length 247 | break 248 | case 'ucs2': 249 | case 'ucs-2': 250 | case 'utf16le': 251 | case 'utf-16le': 252 | ret = str.length * 2 253 | break 254 | case 'hex': 255 | ret = str.length >>> 1 256 | break 257 | case 'utf8': 258 | case 'utf-8': 259 | ret = utf8ToBytes(str).length 260 | break 261 | case 'base64': 262 | ret = base64ToBytes(str).length 263 | break 264 | default: 265 | ret = str.length 266 | } 267 | return ret 268 | } 269 | 270 | // pre-set for values that may exist in the future 271 | Buffer.prototype.length = undefined 272 | Buffer.prototype.parent = undefined 273 | 274 | // toString(encoding, start=0, end=buffer.length) 275 | Buffer.prototype.toString = function toString (encoding, start, end) { 276 | var loweredCase = false 277 | 278 | start = start >>> 0 279 | end = end === undefined || end === Infinity ? this.length : end >>> 0 280 | 281 | if (!encoding) encoding = 'utf8' 282 | if (start < 0) start = 0 283 | if (end > this.length) end = this.length 284 | if (end <= start) return '' 285 | 286 | while (true) { 287 | switch (encoding) { 288 | case 'hex': 289 | return hexSlice(this, start, end) 290 | 291 | case 'utf8': 292 | case 'utf-8': 293 | return utf8Slice(this, start, end) 294 | 295 | case 'ascii': 296 | return asciiSlice(this, start, end) 297 | 298 | case 'binary': 299 | return binarySlice(this, start, end) 300 | 301 | case 'base64': 302 | return base64Slice(this, start, end) 303 | 304 | case 'ucs2': 305 | case 'ucs-2': 306 | case 'utf16le': 307 | case 'utf-16le': 308 | return utf16leSlice(this, start, end) 309 | 310 | default: 311 | if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) 312 | encoding = (encoding + '').toLowerCase() 313 | loweredCase = true 314 | } 315 | } 316 | } 317 | 318 | Buffer.prototype.equals = function equals (b) { 319 | if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') 320 | if (this === b) return true 321 | return Buffer.compare(this, b) === 0 322 | } 323 | 324 | Buffer.prototype.inspect = function inspect () { 325 | var str = '' 326 | var max = exports.INSPECT_MAX_BYTES 327 | if (this.length > 0) { 328 | str = this.toString('hex', 0, max).match(/.{2}/g).join(' ') 329 | if (this.length > max) str += ' ... ' 330 | } 331 | return '' 332 | } 333 | 334 | Buffer.prototype.compare = function compare (b) { 335 | if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') 336 | if (this === b) return 0 337 | return Buffer.compare(this, b) 338 | } 339 | 340 | Buffer.prototype.indexOf = function indexOf (val, byteOffset) { 341 | if (byteOffset > 0x7fffffff) byteOffset = 0x7fffffff 342 | else if (byteOffset < -0x80000000) byteOffset = -0x80000000 343 | byteOffset >>= 0 344 | 345 | if (this.length === 0) return -1 346 | if (byteOffset >= this.length) return -1 347 | 348 | // Negative offsets start from the end of the buffer 349 | if (byteOffset < 0) byteOffset = Math.max(this.length + byteOffset, 0) 350 | 351 | if (typeof val === 'string') { 352 | if (val.length === 0) return -1 // special case: looking for empty string always fails 353 | return String.prototype.indexOf.call(this, val, byteOffset) 354 | } 355 | if (Buffer.isBuffer(val)) { 356 | return arrayIndexOf(this, val, byteOffset) 357 | } 358 | if (typeof val === 'number') { 359 | if (Buffer.TYPED_ARRAY_SUPPORT && Uint8Array.prototype.indexOf === 'function') { 360 | return Uint8Array.prototype.indexOf.call(this, val, byteOffset) 361 | } 362 | return arrayIndexOf(this, [ val ], byteOffset) 363 | } 364 | 365 | function arrayIndexOf (arr, val, byteOffset) { 366 | var foundIndex = -1 367 | for (var i = 0; byteOffset + i < arr.length; i++) { 368 | if (arr[byteOffset + i] === val[foundIndex === -1 ? 0 : i - foundIndex]) { 369 | if (foundIndex === -1) foundIndex = i 370 | if (i - foundIndex + 1 === val.length) return byteOffset + foundIndex 371 | } else { 372 | foundIndex = -1 373 | } 374 | } 375 | return -1 376 | } 377 | 378 | throw new TypeError('val must be string, number or Buffer') 379 | } 380 | 381 | // `get` will be removed in Node 0.13+ 382 | Buffer.prototype.get = function get (offset) { 383 | console.log('.get() is deprecated. Access using array indexes instead.') 384 | return this.readUInt8(offset) 385 | } 386 | 387 | // `set` will be removed in Node 0.13+ 388 | Buffer.prototype.set = function set (v, offset) { 389 | console.log('.set() is deprecated. Access using array indexes instead.') 390 | return this.writeUInt8(v, offset) 391 | } 392 | 393 | function hexWrite (buf, string, offset, length) { 394 | offset = Number(offset) || 0 395 | var remaining = buf.length - offset 396 | if (!length) { 397 | length = remaining 398 | } else { 399 | length = Number(length) 400 | if (length > remaining) { 401 | length = remaining 402 | } 403 | } 404 | 405 | // must be an even number of digits 406 | var strLen = string.length 407 | if (strLen % 2 !== 0) throw new Error('Invalid hex string') 408 | 409 | if (length > strLen / 2) { 410 | length = strLen / 2 411 | } 412 | for (var i = 0; i < length; i++) { 413 | var parsed = parseInt(string.substr(i * 2, 2), 16) 414 | if (isNaN(parsed)) throw new Error('Invalid hex string') 415 | buf[offset + i] = parsed 416 | } 417 | return i 418 | } 419 | 420 | function utf8Write (buf, string, offset, length) { 421 | var charsWritten = blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length) 422 | return charsWritten 423 | } 424 | 425 | function asciiWrite (buf, string, offset, length) { 426 | var charsWritten = blitBuffer(asciiToBytes(string), buf, offset, length) 427 | return charsWritten 428 | } 429 | 430 | function binaryWrite (buf, string, offset, length) { 431 | return asciiWrite(buf, string, offset, length) 432 | } 433 | 434 | function base64Write (buf, string, offset, length) { 435 | var charsWritten = blitBuffer(base64ToBytes(string), buf, offset, length) 436 | return charsWritten 437 | } 438 | 439 | function utf16leWrite (buf, string, offset, length) { 440 | var charsWritten = blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length) 441 | return charsWritten 442 | } 443 | 444 | Buffer.prototype.write = function write (string, offset, length, encoding) { 445 | // Support both (string, offset, length, encoding) 446 | // and the legacy (string, encoding, offset, length) 447 | if (isFinite(offset)) { 448 | if (!isFinite(length)) { 449 | encoding = length 450 | length = undefined 451 | } 452 | } else { // legacy 453 | var swap = encoding 454 | encoding = offset 455 | offset = length 456 | length = swap 457 | } 458 | 459 | offset = Number(offset) || 0 460 | 461 | if (length < 0 || offset < 0 || offset > this.length) { 462 | throw new RangeError('attempt to write outside buffer bounds') 463 | } 464 | 465 | var remaining = this.length - offset 466 | if (!length) { 467 | length = remaining 468 | } else { 469 | length = Number(length) 470 | if (length > remaining) { 471 | length = remaining 472 | } 473 | } 474 | encoding = String(encoding || 'utf8').toLowerCase() 475 | 476 | var ret 477 | switch (encoding) { 478 | case 'hex': 479 | ret = hexWrite(this, string, offset, length) 480 | break 481 | case 'utf8': 482 | case 'utf-8': 483 | ret = utf8Write(this, string, offset, length) 484 | break 485 | case 'ascii': 486 | ret = asciiWrite(this, string, offset, length) 487 | break 488 | case 'binary': 489 | ret = binaryWrite(this, string, offset, length) 490 | break 491 | case 'base64': 492 | ret = base64Write(this, string, offset, length) 493 | break 494 | case 'ucs2': 495 | case 'ucs-2': 496 | case 'utf16le': 497 | case 'utf-16le': 498 | ret = utf16leWrite(this, string, offset, length) 499 | break 500 | default: 501 | throw new TypeError('Unknown encoding: ' + encoding) 502 | } 503 | return ret 504 | } 505 | 506 | Buffer.prototype.toJSON = function toJSON () { 507 | return { 508 | type: 'Buffer', 509 | data: Array.prototype.slice.call(this._arr || this, 0) 510 | } 511 | } 512 | 513 | function base64Slice (buf, start, end) { 514 | if (start === 0 && end === buf.length) { 515 | return base64.fromByteArray(buf) 516 | } else { 517 | return base64.fromByteArray(buf.slice(start, end)) 518 | } 519 | } 520 | 521 | function utf8Slice (buf, start, end) { 522 | var res = '' 523 | var tmp = '' 524 | end = Math.min(buf.length, end) 525 | 526 | for (var i = start; i < end; i++) { 527 | if (buf[i] <= 0x7F) { 528 | res += decodeUtf8Char(tmp) + String.fromCharCode(buf[i]) 529 | tmp = '' 530 | } else { 531 | tmp += '%' + buf[i].toString(16) 532 | } 533 | } 534 | 535 | return res + decodeUtf8Char(tmp) 536 | } 537 | 538 | function asciiSlice (buf, start, end) { 539 | var ret = '' 540 | end = Math.min(buf.length, end) 541 | 542 | for (var i = start; i < end; i++) { 543 | ret += String.fromCharCode(buf[i] & 0x7F) 544 | } 545 | return ret 546 | } 547 | 548 | function binarySlice (buf, start, end) { 549 | var ret = '' 550 | end = Math.min(buf.length, end) 551 | 552 | for (var i = start; i < end; i++) { 553 | ret += String.fromCharCode(buf[i]) 554 | } 555 | return ret 556 | } 557 | 558 | function hexSlice (buf, start, end) { 559 | var len = buf.length 560 | 561 | if (!start || start < 0) start = 0 562 | if (!end || end < 0 || end > len) end = len 563 | 564 | var out = '' 565 | for (var i = start; i < end; i++) { 566 | out += toHex(buf[i]) 567 | } 568 | return out 569 | } 570 | 571 | function utf16leSlice (buf, start, end) { 572 | var bytes = buf.slice(start, end) 573 | var res = '' 574 | for (var i = 0; i < bytes.length; i += 2) { 575 | res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256) 576 | } 577 | return res 578 | } 579 | 580 | Buffer.prototype.slice = function slice (start, end) { 581 | var len = this.length 582 | start = ~~start 583 | end = end === undefined ? len : ~~end 584 | 585 | if (start < 0) { 586 | start += len 587 | if (start < 0) start = 0 588 | } else if (start > len) { 589 | start = len 590 | } 591 | 592 | if (end < 0) { 593 | end += len 594 | if (end < 0) end = 0 595 | } else if (end > len) { 596 | end = len 597 | } 598 | 599 | if (end < start) end = start 600 | 601 | var newBuf 602 | if (Buffer.TYPED_ARRAY_SUPPORT) { 603 | newBuf = Buffer._augment(this.subarray(start, end)) 604 | } else { 605 | var sliceLen = end - start 606 | newBuf = new Buffer(sliceLen, undefined) 607 | for (var i = 0; i < sliceLen; i++) { 608 | newBuf[i] = this[i + start] 609 | } 610 | } 611 | 612 | if (newBuf.length) newBuf.parent = this.parent || this 613 | 614 | return newBuf 615 | } 616 | 617 | /* 618 | * Need to make sure that buffer isn't trying to write out of bounds. 619 | */ 620 | function checkOffset (offset, ext, length) { 621 | if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint') 622 | if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length') 623 | } 624 | 625 | Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) { 626 | offset = offset >>> 0 627 | byteLength = byteLength >>> 0 628 | if (!noAssert) checkOffset(offset, byteLength, this.length) 629 | 630 | var val = this[offset] 631 | var mul = 1 632 | var i = 0 633 | while (++i < byteLength && (mul *= 0x100)) { 634 | val += this[offset + i] * mul 635 | } 636 | 637 | return val 638 | } 639 | 640 | Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) { 641 | offset = offset >>> 0 642 | byteLength = byteLength >>> 0 643 | if (!noAssert) { 644 | checkOffset(offset, byteLength, this.length) 645 | } 646 | 647 | var val = this[offset + --byteLength] 648 | var mul = 1 649 | while (byteLength > 0 && (mul *= 0x100)) { 650 | val += this[offset + --byteLength] * mul 651 | } 652 | 653 | return val 654 | } 655 | 656 | Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) { 657 | if (!noAssert) checkOffset(offset, 1, this.length) 658 | return this[offset] 659 | } 660 | 661 | Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) { 662 | if (!noAssert) checkOffset(offset, 2, this.length) 663 | return this[offset] | (this[offset + 1] << 8) 664 | } 665 | 666 | Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) { 667 | if (!noAssert) checkOffset(offset, 2, this.length) 668 | return (this[offset] << 8) | this[offset + 1] 669 | } 670 | 671 | Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) { 672 | if (!noAssert) checkOffset(offset, 4, this.length) 673 | 674 | return ((this[offset]) | 675 | (this[offset + 1] << 8) | 676 | (this[offset + 2] << 16)) + 677 | (this[offset + 3] * 0x1000000) 678 | } 679 | 680 | Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) { 681 | if (!noAssert) checkOffset(offset, 4, this.length) 682 | 683 | return (this[offset] * 0x1000000) + 684 | ((this[offset + 1] << 16) | 685 | (this[offset + 2] << 8) | 686 | this[offset + 3]) 687 | } 688 | 689 | Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) { 690 | offset = offset >>> 0 691 | byteLength = byteLength >>> 0 692 | if (!noAssert) checkOffset(offset, byteLength, this.length) 693 | 694 | var val = this[offset] 695 | var mul = 1 696 | var i = 0 697 | while (++i < byteLength && (mul *= 0x100)) { 698 | val += this[offset + i] * mul 699 | } 700 | mul *= 0x80 701 | 702 | if (val >= mul) val -= Math.pow(2, 8 * byteLength) 703 | 704 | return val 705 | } 706 | 707 | Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) { 708 | offset = offset >>> 0 709 | byteLength = byteLength >>> 0 710 | if (!noAssert) checkOffset(offset, byteLength, this.length) 711 | 712 | var i = byteLength 713 | var mul = 1 714 | var val = this[offset + --i] 715 | while (i > 0 && (mul *= 0x100)) { 716 | val += this[offset + --i] * mul 717 | } 718 | mul *= 0x80 719 | 720 | if (val >= mul) val -= Math.pow(2, 8 * byteLength) 721 | 722 | return val 723 | } 724 | 725 | Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) { 726 | if (!noAssert) checkOffset(offset, 1, this.length) 727 | if (!(this[offset] & 0x80)) return (this[offset]) 728 | return ((0xff - this[offset] + 1) * -1) 729 | } 730 | 731 | Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) { 732 | if (!noAssert) checkOffset(offset, 2, this.length) 733 | var val = this[offset] | (this[offset + 1] << 8) 734 | return (val & 0x8000) ? val | 0xFFFF0000 : val 735 | } 736 | 737 | Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) { 738 | if (!noAssert) checkOffset(offset, 2, this.length) 739 | var val = this[offset + 1] | (this[offset] << 8) 740 | return (val & 0x8000) ? val | 0xFFFF0000 : val 741 | } 742 | 743 | Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) { 744 | if (!noAssert) checkOffset(offset, 4, this.length) 745 | 746 | return (this[offset]) | 747 | (this[offset + 1] << 8) | 748 | (this[offset + 2] << 16) | 749 | (this[offset + 3] << 24) 750 | } 751 | 752 | Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) { 753 | if (!noAssert) checkOffset(offset, 4, this.length) 754 | 755 | return (this[offset] << 24) | 756 | (this[offset + 1] << 16) | 757 | (this[offset + 2] << 8) | 758 | (this[offset + 3]) 759 | } 760 | 761 | Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) { 762 | if (!noAssert) checkOffset(offset, 4, this.length) 763 | return ieee754.read(this, offset, true, 23, 4) 764 | } 765 | 766 | Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) { 767 | if (!noAssert) checkOffset(offset, 4, this.length) 768 | return ieee754.read(this, offset, false, 23, 4) 769 | } 770 | 771 | Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) { 772 | if (!noAssert) checkOffset(offset, 8, this.length) 773 | return ieee754.read(this, offset, true, 52, 8) 774 | } 775 | 776 | Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) { 777 | if (!noAssert) checkOffset(offset, 8, this.length) 778 | return ieee754.read(this, offset, false, 52, 8) 779 | } 780 | 781 | function checkInt (buf, value, offset, ext, max, min) { 782 | if (!Buffer.isBuffer(buf)) throw new TypeError('buffer must be a Buffer instance') 783 | if (value > max || value < min) throw new RangeError('value is out of bounds') 784 | if (offset + ext > buf.length) throw new RangeError('index out of range') 785 | } 786 | 787 | Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) { 788 | value = +value 789 | offset = offset >>> 0 790 | byteLength = byteLength >>> 0 791 | if (!noAssert) checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0) 792 | 793 | var mul = 1 794 | var i = 0 795 | this[offset] = value & 0xFF 796 | while (++i < byteLength && (mul *= 0x100)) { 797 | this[offset + i] = (value / mul) >>> 0 & 0xFF 798 | } 799 | 800 | return offset + byteLength 801 | } 802 | 803 | Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) { 804 | value = +value 805 | offset = offset >>> 0 806 | byteLength = byteLength >>> 0 807 | if (!noAssert) checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0) 808 | 809 | var i = byteLength - 1 810 | var mul = 1 811 | this[offset + i] = value & 0xFF 812 | while (--i >= 0 && (mul *= 0x100)) { 813 | this[offset + i] = (value / mul) >>> 0 & 0xFF 814 | } 815 | 816 | return offset + byteLength 817 | } 818 | 819 | Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) { 820 | value = +value 821 | offset = offset >>> 0 822 | if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0) 823 | if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value) 824 | this[offset] = value 825 | return offset + 1 826 | } 827 | 828 | function objectWriteUInt16 (buf, value, offset, littleEndian) { 829 | if (value < 0) value = 0xffff + value + 1 830 | for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; i++) { 831 | buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>> 832 | (littleEndian ? i : 1 - i) * 8 833 | } 834 | } 835 | 836 | Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) { 837 | value = +value 838 | offset = offset >>> 0 839 | if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) 840 | if (Buffer.TYPED_ARRAY_SUPPORT) { 841 | this[offset] = value 842 | this[offset + 1] = (value >>> 8) 843 | } else { 844 | objectWriteUInt16(this, value, offset, true) 845 | } 846 | return offset + 2 847 | } 848 | 849 | Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) { 850 | value = +value 851 | offset = offset >>> 0 852 | if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) 853 | if (Buffer.TYPED_ARRAY_SUPPORT) { 854 | this[offset] = (value >>> 8) 855 | this[offset + 1] = value 856 | } else { 857 | objectWriteUInt16(this, value, offset, false) 858 | } 859 | return offset + 2 860 | } 861 | 862 | function objectWriteUInt32 (buf, value, offset, littleEndian) { 863 | if (value < 0) value = 0xffffffff + value + 1 864 | for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; i++) { 865 | buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff 866 | } 867 | } 868 | 869 | Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) { 870 | value = +value 871 | offset = offset >>> 0 872 | if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) 873 | if (Buffer.TYPED_ARRAY_SUPPORT) { 874 | this[offset + 3] = (value >>> 24) 875 | this[offset + 2] = (value >>> 16) 876 | this[offset + 1] = (value >>> 8) 877 | this[offset] = value 878 | } else { 879 | objectWriteUInt32(this, value, offset, true) 880 | } 881 | return offset + 4 882 | } 883 | 884 | Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) { 885 | value = +value 886 | offset = offset >>> 0 887 | if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) 888 | if (Buffer.TYPED_ARRAY_SUPPORT) { 889 | this[offset] = (value >>> 24) 890 | this[offset + 1] = (value >>> 16) 891 | this[offset + 2] = (value >>> 8) 892 | this[offset + 3] = value 893 | } else { 894 | objectWriteUInt32(this, value, offset, false) 895 | } 896 | return offset + 4 897 | } 898 | 899 | Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) { 900 | value = +value 901 | offset = offset >>> 0 902 | if (!noAssert) { 903 | checkInt( 904 | this, value, offset, byteLength, 905 | Math.pow(2, 8 * byteLength - 1) - 1, 906 | -Math.pow(2, 8 * byteLength - 1) 907 | ) 908 | } 909 | 910 | var i = 0 911 | var mul = 1 912 | var sub = value < 0 ? 1 : 0 913 | this[offset] = value & 0xFF 914 | while (++i < byteLength && (mul *= 0x100)) { 915 | this[offset + i] = ((value / mul) >> 0) - sub & 0xFF 916 | } 917 | 918 | return offset + byteLength 919 | } 920 | 921 | Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) { 922 | value = +value 923 | offset = offset >>> 0 924 | if (!noAssert) { 925 | checkInt( 926 | this, value, offset, byteLength, 927 | Math.pow(2, 8 * byteLength - 1) - 1, 928 | -Math.pow(2, 8 * byteLength - 1) 929 | ) 930 | } 931 | 932 | var i = byteLength - 1 933 | var mul = 1 934 | var sub = value < 0 ? 1 : 0 935 | this[offset + i] = value & 0xFF 936 | while (--i >= 0 && (mul *= 0x100)) { 937 | this[offset + i] = ((value / mul) >> 0) - sub & 0xFF 938 | } 939 | 940 | return offset + byteLength 941 | } 942 | 943 | Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) { 944 | value = +value 945 | offset = offset >>> 0 946 | if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80) 947 | if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value) 948 | if (value < 0) value = 0xff + value + 1 949 | this[offset] = value 950 | return offset + 1 951 | } 952 | 953 | Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) { 954 | value = +value 955 | offset = offset >>> 0 956 | if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) 957 | if (Buffer.TYPED_ARRAY_SUPPORT) { 958 | this[offset] = value 959 | this[offset + 1] = (value >>> 8) 960 | } else { 961 | objectWriteUInt16(this, value, offset, true) 962 | } 963 | return offset + 2 964 | } 965 | 966 | Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) { 967 | value = +value 968 | offset = offset >>> 0 969 | if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) 970 | if (Buffer.TYPED_ARRAY_SUPPORT) { 971 | this[offset] = (value >>> 8) 972 | this[offset + 1] = value 973 | } else { 974 | objectWriteUInt16(this, value, offset, false) 975 | } 976 | return offset + 2 977 | } 978 | 979 | Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) { 980 | value = +value 981 | offset = offset >>> 0 982 | if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) 983 | if (Buffer.TYPED_ARRAY_SUPPORT) { 984 | this[offset] = value 985 | this[offset + 1] = (value >>> 8) 986 | this[offset + 2] = (value >>> 16) 987 | this[offset + 3] = (value >>> 24) 988 | } else { 989 | objectWriteUInt32(this, value, offset, true) 990 | } 991 | return offset + 4 992 | } 993 | 994 | Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) { 995 | value = +value 996 | offset = offset >>> 0 997 | if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) 998 | if (value < 0) value = 0xffffffff + value + 1 999 | if (Buffer.TYPED_ARRAY_SUPPORT) { 1000 | this[offset] = (value >>> 24) 1001 | this[offset + 1] = (value >>> 16) 1002 | this[offset + 2] = (value >>> 8) 1003 | this[offset + 3] = value 1004 | } else { 1005 | objectWriteUInt32(this, value, offset, false) 1006 | } 1007 | return offset + 4 1008 | } 1009 | 1010 | function checkIEEE754 (buf, value, offset, ext, max, min) { 1011 | if (value > max || value < min) throw new RangeError('value is out of bounds') 1012 | if (offset + ext > buf.length) throw new RangeError('index out of range') 1013 | if (offset < 0) throw new RangeError('index out of range') 1014 | } 1015 | 1016 | function writeFloat (buf, value, offset, littleEndian, noAssert) { 1017 | if (!noAssert) { 1018 | checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38) 1019 | } 1020 | ieee754.write(buf, value, offset, littleEndian, 23, 4) 1021 | return offset + 4 1022 | } 1023 | 1024 | Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) { 1025 | return writeFloat(this, value, offset, true, noAssert) 1026 | } 1027 | 1028 | Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) { 1029 | return writeFloat(this, value, offset, false, noAssert) 1030 | } 1031 | 1032 | function writeDouble (buf, value, offset, littleEndian, noAssert) { 1033 | if (!noAssert) { 1034 | checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308) 1035 | } 1036 | ieee754.write(buf, value, offset, littleEndian, 52, 8) 1037 | return offset + 8 1038 | } 1039 | 1040 | Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) { 1041 | return writeDouble(this, value, offset, true, noAssert) 1042 | } 1043 | 1044 | Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) { 1045 | return writeDouble(this, value, offset, false, noAssert) 1046 | } 1047 | 1048 | // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) 1049 | Buffer.prototype.copy = function copy (target, target_start, start, end) { 1050 | if (!start) start = 0 1051 | if (!end && end !== 0) end = this.length 1052 | if (target_start >= target.length) target_start = target.length 1053 | if (!target_start) target_start = 0 1054 | if (end > 0 && end < start) end = start 1055 | 1056 | // Copy 0 bytes; we're done 1057 | if (end === start) return 0 1058 | if (target.length === 0 || this.length === 0) return 0 1059 | 1060 | // Fatal error conditions 1061 | if (target_start < 0) { 1062 | throw new RangeError('targetStart out of bounds') 1063 | } 1064 | if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds') 1065 | if (end < 0) throw new RangeError('sourceEnd out of bounds') 1066 | 1067 | // Are we oob? 1068 | if (end > this.length) end = this.length 1069 | if (target.length - target_start < end - start) { 1070 | end = target.length - target_start + start 1071 | } 1072 | 1073 | var len = end - start 1074 | 1075 | if (len < 1000 || !Buffer.TYPED_ARRAY_SUPPORT) { 1076 | for (var i = 0; i < len; i++) { 1077 | target[i + target_start] = this[i + start] 1078 | } 1079 | } else { 1080 | target._set(this.subarray(start, start + len), target_start) 1081 | } 1082 | 1083 | return len 1084 | } 1085 | 1086 | // fill(value, start=0, end=buffer.length) 1087 | Buffer.prototype.fill = function fill (value, start, end) { 1088 | if (!value) value = 0 1089 | if (!start) start = 0 1090 | if (!end) end = this.length 1091 | 1092 | if (end < start) throw new RangeError('end < start') 1093 | 1094 | // Fill 0 bytes; we're done 1095 | if (end === start) return 1096 | if (this.length === 0) return 1097 | 1098 | if (start < 0 || start >= this.length) throw new RangeError('start out of bounds') 1099 | if (end < 0 || end > this.length) throw new RangeError('end out of bounds') 1100 | 1101 | var i 1102 | if (typeof value === 'number') { 1103 | for (i = start; i < end; i++) { 1104 | this[i] = value 1105 | } 1106 | } else { 1107 | var bytes = utf8ToBytes(value.toString()) 1108 | var len = bytes.length 1109 | for (i = start; i < end; i++) { 1110 | this[i] = bytes[i % len] 1111 | } 1112 | } 1113 | 1114 | return this 1115 | } 1116 | 1117 | /** 1118 | * Creates a new `ArrayBuffer` with the *copied* memory of the buffer instance. 1119 | * Added in Node 0.12. Only available in browsers that support ArrayBuffer. 1120 | */ 1121 | Buffer.prototype.toArrayBuffer = function toArrayBuffer () { 1122 | if (typeof Uint8Array !== 'undefined') { 1123 | if (Buffer.TYPED_ARRAY_SUPPORT) { 1124 | return (new Buffer(this)).buffer 1125 | } else { 1126 | var buf = new Uint8Array(this.length) 1127 | for (var i = 0, len = buf.length; i < len; i += 1) { 1128 | buf[i] = this[i] 1129 | } 1130 | return buf.buffer 1131 | } 1132 | } else { 1133 | throw new TypeError('Buffer.toArrayBuffer not supported in this browser') 1134 | } 1135 | } 1136 | 1137 | // HELPER FUNCTIONS 1138 | // ================ 1139 | 1140 | var BP = Buffer.prototype 1141 | 1142 | /** 1143 | * Augment a Uint8Array *instance* (not the Uint8Array class!) with Buffer methods 1144 | */ 1145 | Buffer._augment = function _augment (arr) { 1146 | arr.constructor = Buffer 1147 | arr._isBuffer = true 1148 | 1149 | // save reference to original Uint8Array set method before overwriting 1150 | arr._set = arr.set 1151 | 1152 | // deprecated, will be removed in node 0.13+ 1153 | arr.get = BP.get 1154 | arr.set = BP.set 1155 | 1156 | arr.write = BP.write 1157 | arr.toString = BP.toString 1158 | arr.toLocaleString = BP.toString 1159 | arr.toJSON = BP.toJSON 1160 | arr.equals = BP.equals 1161 | arr.compare = BP.compare 1162 | arr.indexOf = BP.indexOf 1163 | arr.copy = BP.copy 1164 | arr.slice = BP.slice 1165 | arr.readUIntLE = BP.readUIntLE 1166 | arr.readUIntBE = BP.readUIntBE 1167 | arr.readUInt8 = BP.readUInt8 1168 | arr.readUInt16LE = BP.readUInt16LE 1169 | arr.readUInt16BE = BP.readUInt16BE 1170 | arr.readUInt32LE = BP.readUInt32LE 1171 | arr.readUInt32BE = BP.readUInt32BE 1172 | arr.readIntLE = BP.readIntLE 1173 | arr.readIntBE = BP.readIntBE 1174 | arr.readInt8 = BP.readInt8 1175 | arr.readInt16LE = BP.readInt16LE 1176 | arr.readInt16BE = BP.readInt16BE 1177 | arr.readInt32LE = BP.readInt32LE 1178 | arr.readInt32BE = BP.readInt32BE 1179 | arr.readFloatLE = BP.readFloatLE 1180 | arr.readFloatBE = BP.readFloatBE 1181 | arr.readDoubleLE = BP.readDoubleLE 1182 | arr.readDoubleBE = BP.readDoubleBE 1183 | arr.writeUInt8 = BP.writeUInt8 1184 | arr.writeUIntLE = BP.writeUIntLE 1185 | arr.writeUIntBE = BP.writeUIntBE 1186 | arr.writeUInt16LE = BP.writeUInt16LE 1187 | arr.writeUInt16BE = BP.writeUInt16BE 1188 | arr.writeUInt32LE = BP.writeUInt32LE 1189 | arr.writeUInt32BE = BP.writeUInt32BE 1190 | arr.writeIntLE = BP.writeIntLE 1191 | arr.writeIntBE = BP.writeIntBE 1192 | arr.writeInt8 = BP.writeInt8 1193 | arr.writeInt16LE = BP.writeInt16LE 1194 | arr.writeInt16BE = BP.writeInt16BE 1195 | arr.writeInt32LE = BP.writeInt32LE 1196 | arr.writeInt32BE = BP.writeInt32BE 1197 | arr.writeFloatLE = BP.writeFloatLE 1198 | arr.writeFloatBE = BP.writeFloatBE 1199 | arr.writeDoubleLE = BP.writeDoubleLE 1200 | arr.writeDoubleBE = BP.writeDoubleBE 1201 | arr.fill = BP.fill 1202 | arr.inspect = BP.inspect 1203 | arr.toArrayBuffer = BP.toArrayBuffer 1204 | 1205 | return arr 1206 | } 1207 | 1208 | var INVALID_BASE64_RE = /[^+\/0-9A-z\-]/g 1209 | 1210 | function base64clean (str) { 1211 | // Node strips out invalid characters like \n and \t from the string, base64-js does not 1212 | str = stringtrim(str).replace(INVALID_BASE64_RE, '') 1213 | // Node converts strings with length < 2 to '' 1214 | if (str.length < 2) return '' 1215 | // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not 1216 | while (str.length % 4 !== 0) { 1217 | str = str + '=' 1218 | } 1219 | return str 1220 | } 1221 | 1222 | function stringtrim (str) { 1223 | if (str.trim) return str.trim() 1224 | return str.replace(/^\s+|\s+$/g, '') 1225 | } 1226 | 1227 | function isArrayish (subject) { 1228 | return isArray(subject) || Buffer.isBuffer(subject) || 1229 | subject && typeof subject === 'object' && 1230 | typeof subject.length === 'number' 1231 | } 1232 | 1233 | function toHex (n) { 1234 | if (n < 16) return '0' + n.toString(16) 1235 | return n.toString(16) 1236 | } 1237 | 1238 | function utf8ToBytes (string, units) { 1239 | units = units || Infinity 1240 | var codePoint 1241 | var length = string.length 1242 | var leadSurrogate = null 1243 | var bytes = [] 1244 | var i = 0 1245 | 1246 | for (; i < length; i++) { 1247 | codePoint = string.charCodeAt(i) 1248 | 1249 | // is surrogate component 1250 | if (codePoint > 0xD7FF && codePoint < 0xE000) { 1251 | // last char was a lead 1252 | if (leadSurrogate) { 1253 | // 2 leads in a row 1254 | if (codePoint < 0xDC00) { 1255 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) 1256 | leadSurrogate = codePoint 1257 | continue 1258 | } else { 1259 | // valid surrogate pair 1260 | codePoint = leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00 | 0x10000 1261 | leadSurrogate = null 1262 | } 1263 | } else { 1264 | // no lead yet 1265 | 1266 | if (codePoint > 0xDBFF) { 1267 | // unexpected trail 1268 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) 1269 | continue 1270 | } else if (i + 1 === length) { 1271 | // unpaired lead 1272 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) 1273 | continue 1274 | } else { 1275 | // valid lead 1276 | leadSurrogate = codePoint 1277 | continue 1278 | } 1279 | } 1280 | } else if (leadSurrogate) { 1281 | // valid bmp char, but last char was a lead 1282 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) 1283 | leadSurrogate = null 1284 | } 1285 | 1286 | // encode utf8 1287 | if (codePoint < 0x80) { 1288 | if ((units -= 1) < 0) break 1289 | bytes.push(codePoint) 1290 | } else if (codePoint < 0x800) { 1291 | if ((units -= 2) < 0) break 1292 | bytes.push( 1293 | codePoint >> 0x6 | 0xC0, 1294 | codePoint & 0x3F | 0x80 1295 | ) 1296 | } else if (codePoint < 0x10000) { 1297 | if ((units -= 3) < 0) break 1298 | bytes.push( 1299 | codePoint >> 0xC | 0xE0, 1300 | codePoint >> 0x6 & 0x3F | 0x80, 1301 | codePoint & 0x3F | 0x80 1302 | ) 1303 | } else if (codePoint < 0x200000) { 1304 | if ((units -= 4) < 0) break 1305 | bytes.push( 1306 | codePoint >> 0x12 | 0xF0, 1307 | codePoint >> 0xC & 0x3F | 0x80, 1308 | codePoint >> 0x6 & 0x3F | 0x80, 1309 | codePoint & 0x3F | 0x80 1310 | ) 1311 | } else { 1312 | throw new Error('Invalid code point') 1313 | } 1314 | } 1315 | 1316 | return bytes 1317 | } 1318 | 1319 | function asciiToBytes (str) { 1320 | var byteArray = [] 1321 | for (var i = 0; i < str.length; i++) { 1322 | // Node's code seems to be doing this and not & 0x7F.. 1323 | byteArray.push(str.charCodeAt(i) & 0xFF) 1324 | } 1325 | return byteArray 1326 | } 1327 | 1328 | function utf16leToBytes (str, units) { 1329 | var c, hi, lo 1330 | var byteArray = [] 1331 | for (var i = 0; i < str.length; i++) { 1332 | if ((units -= 2) < 0) break 1333 | 1334 | c = str.charCodeAt(i) 1335 | hi = c >> 8 1336 | lo = c % 256 1337 | byteArray.push(lo) 1338 | byteArray.push(hi) 1339 | } 1340 | 1341 | return byteArray 1342 | } 1343 | 1344 | function base64ToBytes (str) { 1345 | return base64.toByteArray(base64clean(str)) 1346 | } 1347 | 1348 | function blitBuffer (src, dst, offset, length) { 1349 | for (var i = 0; i < length; i++) { 1350 | if ((i + offset >= dst.length) || (i >= src.length)) break 1351 | dst[i + offset] = src[i] 1352 | } 1353 | return i 1354 | } 1355 | 1356 | function decodeUtf8Char (str) { 1357 | try { 1358 | return decodeURIComponent(str) 1359 | } catch (err) { 1360 | return String.fromCharCode(0xFFFD) // UTF 8 invalid char 1361 | } 1362 | } 1363 | 1364 | },{"base64-js":3,"ieee754":4,"is-array":5}],3:[function(require,module,exports){ 1365 | var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; 1366 | 1367 | ;(function (exports) { 1368 | 'use strict'; 1369 | 1370 | var Arr = (typeof Uint8Array !== 'undefined') 1371 | ? Uint8Array 1372 | : Array 1373 | 1374 | var PLUS = '+'.charCodeAt(0) 1375 | var SLASH = '/'.charCodeAt(0) 1376 | var NUMBER = '0'.charCodeAt(0) 1377 | var LOWER = 'a'.charCodeAt(0) 1378 | var UPPER = 'A'.charCodeAt(0) 1379 | var PLUS_URL_SAFE = '-'.charCodeAt(0) 1380 | var SLASH_URL_SAFE = '_'.charCodeAt(0) 1381 | 1382 | function decode (elt) { 1383 | var code = elt.charCodeAt(0) 1384 | if (code === PLUS || 1385 | code === PLUS_URL_SAFE) 1386 | return 62 // '+' 1387 | if (code === SLASH || 1388 | code === SLASH_URL_SAFE) 1389 | return 63 // '/' 1390 | if (code < NUMBER) 1391 | return -1 //no match 1392 | if (code < NUMBER + 10) 1393 | return code - NUMBER + 26 + 26 1394 | if (code < UPPER + 26) 1395 | return code - UPPER 1396 | if (code < LOWER + 26) 1397 | return code - LOWER + 26 1398 | } 1399 | 1400 | function b64ToByteArray (b64) { 1401 | var i, j, l, tmp, placeHolders, arr 1402 | 1403 | if (b64.length % 4 > 0) { 1404 | throw new Error('Invalid string. Length must be a multiple of 4') 1405 | } 1406 | 1407 | // the number of equal signs (place holders) 1408 | // if there are two placeholders, than the two characters before it 1409 | // represent one byte 1410 | // if there is only one, then the three characters before it represent 2 bytes 1411 | // this is just a cheap hack to not do indexOf twice 1412 | var len = b64.length 1413 | placeHolders = '=' === b64.charAt(len - 2) ? 2 : '=' === b64.charAt(len - 1) ? 1 : 0 1414 | 1415 | // base64 is 4/3 + up to two characters of the original data 1416 | arr = new Arr(b64.length * 3 / 4 - placeHolders) 1417 | 1418 | // if there are placeholders, only get up to the last complete 4 chars 1419 | l = placeHolders > 0 ? b64.length - 4 : b64.length 1420 | 1421 | var L = 0 1422 | 1423 | function push (v) { 1424 | arr[L++] = v 1425 | } 1426 | 1427 | for (i = 0, j = 0; i < l; i += 4, j += 3) { 1428 | tmp = (decode(b64.charAt(i)) << 18) | (decode(b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3)) 1429 | push((tmp & 0xFF0000) >> 16) 1430 | push((tmp & 0xFF00) >> 8) 1431 | push(tmp & 0xFF) 1432 | } 1433 | 1434 | if (placeHolders === 2) { 1435 | tmp = (decode(b64.charAt(i)) << 2) | (decode(b64.charAt(i + 1)) >> 4) 1436 | push(tmp & 0xFF) 1437 | } else if (placeHolders === 1) { 1438 | tmp = (decode(b64.charAt(i)) << 10) | (decode(b64.charAt(i + 1)) << 4) | (decode(b64.charAt(i + 2)) >> 2) 1439 | push((tmp >> 8) & 0xFF) 1440 | push(tmp & 0xFF) 1441 | } 1442 | 1443 | return arr 1444 | } 1445 | 1446 | function uint8ToBase64 (uint8) { 1447 | var i, 1448 | extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes 1449 | output = "", 1450 | temp, length 1451 | 1452 | function encode (num) { 1453 | return lookup.charAt(num) 1454 | } 1455 | 1456 | function tripletToBase64 (num) { 1457 | return encode(num >> 18 & 0x3F) + encode(num >> 12 & 0x3F) + encode(num >> 6 & 0x3F) + encode(num & 0x3F) 1458 | } 1459 | 1460 | // go through the array every three bytes, we'll deal with trailing stuff later 1461 | for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) { 1462 | temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]) 1463 | output += tripletToBase64(temp) 1464 | } 1465 | 1466 | // pad the end with zeros, but make sure to not forget the extra bytes 1467 | switch (extraBytes) { 1468 | case 1: 1469 | temp = uint8[uint8.length - 1] 1470 | output += encode(temp >> 2) 1471 | output += encode((temp << 4) & 0x3F) 1472 | output += '==' 1473 | break 1474 | case 2: 1475 | temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1]) 1476 | output += encode(temp >> 10) 1477 | output += encode((temp >> 4) & 0x3F) 1478 | output += encode((temp << 2) & 0x3F) 1479 | output += '=' 1480 | break 1481 | } 1482 | 1483 | return output 1484 | } 1485 | 1486 | exports.toByteArray = b64ToByteArray 1487 | exports.fromByteArray = uint8ToBase64 1488 | }(typeof exports === 'undefined' ? (this.base64js = {}) : exports)) 1489 | 1490 | },{}],4:[function(require,module,exports){ 1491 | exports.read = function(buffer, offset, isLE, mLen, nBytes) { 1492 | var e, m, 1493 | eLen = nBytes * 8 - mLen - 1, 1494 | eMax = (1 << eLen) - 1, 1495 | eBias = eMax >> 1, 1496 | nBits = -7, 1497 | i = isLE ? (nBytes - 1) : 0, 1498 | d = isLE ? -1 : 1, 1499 | s = buffer[offset + i]; 1500 | 1501 | i += d; 1502 | 1503 | e = s & ((1 << (-nBits)) - 1); 1504 | s >>= (-nBits); 1505 | nBits += eLen; 1506 | for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8); 1507 | 1508 | m = e & ((1 << (-nBits)) - 1); 1509 | e >>= (-nBits); 1510 | nBits += mLen; 1511 | for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8); 1512 | 1513 | if (e === 0) { 1514 | e = 1 - eBias; 1515 | } else if (e === eMax) { 1516 | return m ? NaN : ((s ? -1 : 1) * Infinity); 1517 | } else { 1518 | m = m + Math.pow(2, mLen); 1519 | e = e - eBias; 1520 | } 1521 | return (s ? -1 : 1) * m * Math.pow(2, e - mLen); 1522 | }; 1523 | 1524 | exports.write = function(buffer, value, offset, isLE, mLen, nBytes) { 1525 | var e, m, c, 1526 | eLen = nBytes * 8 - mLen - 1, 1527 | eMax = (1 << eLen) - 1, 1528 | eBias = eMax >> 1, 1529 | rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0), 1530 | i = isLE ? 0 : (nBytes - 1), 1531 | d = isLE ? 1 : -1, 1532 | s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0; 1533 | 1534 | value = Math.abs(value); 1535 | 1536 | if (isNaN(value) || value === Infinity) { 1537 | m = isNaN(value) ? 1 : 0; 1538 | e = eMax; 1539 | } else { 1540 | e = Math.floor(Math.log(value) / Math.LN2); 1541 | if (value * (c = Math.pow(2, -e)) < 1) { 1542 | e--; 1543 | c *= 2; 1544 | } 1545 | if (e + eBias >= 1) { 1546 | value += rt / c; 1547 | } else { 1548 | value += rt * Math.pow(2, 1 - eBias); 1549 | } 1550 | if (value * c >= 2) { 1551 | e++; 1552 | c /= 2; 1553 | } 1554 | 1555 | if (e + eBias >= eMax) { 1556 | m = 0; 1557 | e = eMax; 1558 | } else if (e + eBias >= 1) { 1559 | m = (value * c - 1) * Math.pow(2, mLen); 1560 | e = e + eBias; 1561 | } else { 1562 | m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen); 1563 | e = 0; 1564 | } 1565 | } 1566 | 1567 | for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8); 1568 | 1569 | e = (e << mLen) | m; 1570 | eLen += mLen; 1571 | for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8); 1572 | 1573 | buffer[offset + i - d] |= s * 128; 1574 | }; 1575 | 1576 | },{}],5:[function(require,module,exports){ 1577 | 1578 | /** 1579 | * isArray 1580 | */ 1581 | 1582 | var isArray = Array.isArray; 1583 | 1584 | /** 1585 | * toString 1586 | */ 1587 | 1588 | var str = Object.prototype.toString; 1589 | 1590 | /** 1591 | * Whether or not the given `val` 1592 | * is an array. 1593 | * 1594 | * example: 1595 | * 1596 | * isArray([]); 1597 | * // > true 1598 | * isArray(arguments); 1599 | * // > false 1600 | * isArray(''); 1601 | * // > false 1602 | * 1603 | * @param {mixed} val 1604 | * @return {bool} 1605 | */ 1606 | 1607 | module.exports = isArray || function (val) { 1608 | return !! val && '[object Array]' == str.call(val); 1609 | }; 1610 | 1611 | },{}],6:[function(require,module,exports){ 1612 | // Copyright Joyent, Inc. and other Node contributors. 1613 | // 1614 | // Permission is hereby granted, free of charge, to any person obtaining a 1615 | // copy of this software and associated documentation files (the 1616 | // "Software"), to deal in the Software without restriction, including 1617 | // without limitation the rights to use, copy, modify, merge, publish, 1618 | // distribute, sublicense, and/or sell copies of the Software, and to permit 1619 | // persons to whom the Software is furnished to do so, subject to the 1620 | // following conditions: 1621 | // 1622 | // The above copyright notice and this permission notice shall be included 1623 | // in all copies or substantial portions of the Software. 1624 | // 1625 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 1626 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 1627 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 1628 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 1629 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 1630 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 1631 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 1632 | 1633 | function EventEmitter() { 1634 | this._events = this._events || {}; 1635 | this._maxListeners = this._maxListeners || undefined; 1636 | } 1637 | module.exports = EventEmitter; 1638 | 1639 | // Backwards-compat with node 0.10.x 1640 | EventEmitter.EventEmitter = EventEmitter; 1641 | 1642 | EventEmitter.prototype._events = undefined; 1643 | EventEmitter.prototype._maxListeners = undefined; 1644 | 1645 | // By default EventEmitters will print a warning if more than 10 listeners are 1646 | // added to it. This is a useful default which helps finding memory leaks. 1647 | EventEmitter.defaultMaxListeners = 10; 1648 | 1649 | // Obviously not all Emitters should be limited to 10. This function allows 1650 | // that to be increased. Set to zero for unlimited. 1651 | EventEmitter.prototype.setMaxListeners = function(n) { 1652 | if (!isNumber(n) || n < 0 || isNaN(n)) 1653 | throw TypeError('n must be a positive number'); 1654 | this._maxListeners = n; 1655 | return this; 1656 | }; 1657 | 1658 | EventEmitter.prototype.emit = function(type) { 1659 | var er, handler, len, args, i, listeners; 1660 | 1661 | if (!this._events) 1662 | this._events = {}; 1663 | 1664 | // If there is no 'error' event listener then throw. 1665 | if (type === 'error') { 1666 | if (!this._events.error || 1667 | (isObject(this._events.error) && !this._events.error.length)) { 1668 | er = arguments[1]; 1669 | if (er instanceof Error) { 1670 | throw er; // Unhandled 'error' event 1671 | } 1672 | throw TypeError('Uncaught, unspecified "error" event.'); 1673 | } 1674 | } 1675 | 1676 | handler = this._events[type]; 1677 | 1678 | if (isUndefined(handler)) 1679 | return false; 1680 | 1681 | if (isFunction(handler)) { 1682 | switch (arguments.length) { 1683 | // fast cases 1684 | case 1: 1685 | handler.call(this); 1686 | break; 1687 | case 2: 1688 | handler.call(this, arguments[1]); 1689 | break; 1690 | case 3: 1691 | handler.call(this, arguments[1], arguments[2]); 1692 | break; 1693 | // slower 1694 | default: 1695 | len = arguments.length; 1696 | args = new Array(len - 1); 1697 | for (i = 1; i < len; i++) 1698 | args[i - 1] = arguments[i]; 1699 | handler.apply(this, args); 1700 | } 1701 | } else if (isObject(handler)) { 1702 | len = arguments.length; 1703 | args = new Array(len - 1); 1704 | for (i = 1; i < len; i++) 1705 | args[i - 1] = arguments[i]; 1706 | 1707 | listeners = handler.slice(); 1708 | len = listeners.length; 1709 | for (i = 0; i < len; i++) 1710 | listeners[i].apply(this, args); 1711 | } 1712 | 1713 | return true; 1714 | }; 1715 | 1716 | EventEmitter.prototype.addListener = function(type, listener) { 1717 | var m; 1718 | 1719 | if (!isFunction(listener)) 1720 | throw TypeError('listener must be a function'); 1721 | 1722 | if (!this._events) 1723 | this._events = {}; 1724 | 1725 | // To avoid recursion in the case that type === "newListener"! Before 1726 | // adding it to the listeners, first emit "newListener". 1727 | if (this._events.newListener) 1728 | this.emit('newListener', type, 1729 | isFunction(listener.listener) ? 1730 | listener.listener : listener); 1731 | 1732 | if (!this._events[type]) 1733 | // Optimize the case of one listener. Don't need the extra array object. 1734 | this._events[type] = listener; 1735 | else if (isObject(this._events[type])) 1736 | // If we've already got an array, just append. 1737 | this._events[type].push(listener); 1738 | else 1739 | // Adding the second element, need to change to array. 1740 | this._events[type] = [this._events[type], listener]; 1741 | 1742 | // Check for listener leak 1743 | if (isObject(this._events[type]) && !this._events[type].warned) { 1744 | var m; 1745 | if (!isUndefined(this._maxListeners)) { 1746 | m = this._maxListeners; 1747 | } else { 1748 | m = EventEmitter.defaultMaxListeners; 1749 | } 1750 | 1751 | if (m && m > 0 && this._events[type].length > m) { 1752 | this._events[type].warned = true; 1753 | console.error('(node) warning: possible EventEmitter memory ' + 1754 | 'leak detected. %d listeners added. ' + 1755 | 'Use emitter.setMaxListeners() to increase limit.', 1756 | this._events[type].length); 1757 | if (typeof console.trace === 'function') { 1758 | // not supported in IE 10 1759 | console.trace(); 1760 | } 1761 | } 1762 | } 1763 | 1764 | return this; 1765 | }; 1766 | 1767 | EventEmitter.prototype.on = EventEmitter.prototype.addListener; 1768 | 1769 | EventEmitter.prototype.once = function(type, listener) { 1770 | if (!isFunction(listener)) 1771 | throw TypeError('listener must be a function'); 1772 | 1773 | var fired = false; 1774 | 1775 | function g() { 1776 | this.removeListener(type, g); 1777 | 1778 | if (!fired) { 1779 | fired = true; 1780 | listener.apply(this, arguments); 1781 | } 1782 | } 1783 | 1784 | g.listener = listener; 1785 | this.on(type, g); 1786 | 1787 | return this; 1788 | }; 1789 | 1790 | // emits a 'removeListener' event iff the listener was removed 1791 | EventEmitter.prototype.removeListener = function(type, listener) { 1792 | var list, position, length, i; 1793 | 1794 | if (!isFunction(listener)) 1795 | throw TypeError('listener must be a function'); 1796 | 1797 | if (!this._events || !this._events[type]) 1798 | return this; 1799 | 1800 | list = this._events[type]; 1801 | length = list.length; 1802 | position = -1; 1803 | 1804 | if (list === listener || 1805 | (isFunction(list.listener) && list.listener === listener)) { 1806 | delete this._events[type]; 1807 | if (this._events.removeListener) 1808 | this.emit('removeListener', type, listener); 1809 | 1810 | } else if (isObject(list)) { 1811 | for (i = length; i-- > 0;) { 1812 | if (list[i] === listener || 1813 | (list[i].listener && list[i].listener === listener)) { 1814 | position = i; 1815 | break; 1816 | } 1817 | } 1818 | 1819 | if (position < 0) 1820 | return this; 1821 | 1822 | if (list.length === 1) { 1823 | list.length = 0; 1824 | delete this._events[type]; 1825 | } else { 1826 | list.splice(position, 1); 1827 | } 1828 | 1829 | if (this._events.removeListener) 1830 | this.emit('removeListener', type, listener); 1831 | } 1832 | 1833 | return this; 1834 | }; 1835 | 1836 | EventEmitter.prototype.removeAllListeners = function(type) { 1837 | var key, listeners; 1838 | 1839 | if (!this._events) 1840 | return this; 1841 | 1842 | // not listening for removeListener, no need to emit 1843 | if (!this._events.removeListener) { 1844 | if (arguments.length === 0) 1845 | this._events = {}; 1846 | else if (this._events[type]) 1847 | delete this._events[type]; 1848 | return this; 1849 | } 1850 | 1851 | // emit removeListener for all listeners on all events 1852 | if (arguments.length === 0) { 1853 | for (key in this._events) { 1854 | if (key === 'removeListener') continue; 1855 | this.removeAllListeners(key); 1856 | } 1857 | this.removeAllListeners('removeListener'); 1858 | this._events = {}; 1859 | return this; 1860 | } 1861 | 1862 | listeners = this._events[type]; 1863 | 1864 | if (isFunction(listeners)) { 1865 | this.removeListener(type, listeners); 1866 | } else { 1867 | // LIFO order 1868 | while (listeners.length) 1869 | this.removeListener(type, listeners[listeners.length - 1]); 1870 | } 1871 | delete this._events[type]; 1872 | 1873 | return this; 1874 | }; 1875 | 1876 | EventEmitter.prototype.listeners = function(type) { 1877 | var ret; 1878 | if (!this._events || !this._events[type]) 1879 | ret = []; 1880 | else if (isFunction(this._events[type])) 1881 | ret = [this._events[type]]; 1882 | else 1883 | ret = this._events[type].slice(); 1884 | return ret; 1885 | }; 1886 | 1887 | EventEmitter.listenerCount = function(emitter, type) { 1888 | var ret; 1889 | if (!emitter._events || !emitter._events[type]) 1890 | ret = 0; 1891 | else if (isFunction(emitter._events[type])) 1892 | ret = 1; 1893 | else 1894 | ret = emitter._events[type].length; 1895 | return ret; 1896 | }; 1897 | 1898 | function isFunction(arg) { 1899 | return typeof arg === 'function'; 1900 | } 1901 | 1902 | function isNumber(arg) { 1903 | return typeof arg === 'number'; 1904 | } 1905 | 1906 | function isObject(arg) { 1907 | return typeof arg === 'object' && arg !== null; 1908 | } 1909 | 1910 | function isUndefined(arg) { 1911 | return arg === void 0; 1912 | } 1913 | 1914 | },{}],7:[function(require,module,exports){ 1915 | if (typeof Object.create === 'function') { 1916 | // implementation from standard node.js 'util' module 1917 | module.exports = function inherits(ctor, superCtor) { 1918 | ctor.super_ = superCtor 1919 | ctor.prototype = Object.create(superCtor.prototype, { 1920 | constructor: { 1921 | value: ctor, 1922 | enumerable: false, 1923 | writable: true, 1924 | configurable: true 1925 | } 1926 | }); 1927 | }; 1928 | } else { 1929 | // old school shim for old browsers 1930 | module.exports = function inherits(ctor, superCtor) { 1931 | ctor.super_ = superCtor 1932 | var TempCtor = function () {} 1933 | TempCtor.prototype = superCtor.prototype 1934 | ctor.prototype = new TempCtor() 1935 | ctor.prototype.constructor = ctor 1936 | } 1937 | } 1938 | 1939 | },{}],8:[function(require,module,exports){ 1940 | // shim for using process in browser 1941 | 1942 | var process = module.exports = {}; 1943 | var queue = []; 1944 | var draining = false; 1945 | 1946 | function drainQueue() { 1947 | if (draining) { 1948 | return; 1949 | } 1950 | draining = true; 1951 | var currentQueue; 1952 | var len = queue.length; 1953 | while(len) { 1954 | currentQueue = queue; 1955 | queue = []; 1956 | var i = -1; 1957 | while (++i < len) { 1958 | currentQueue[i](); 1959 | } 1960 | len = queue.length; 1961 | } 1962 | draining = false; 1963 | } 1964 | process.nextTick = function (fun) { 1965 | queue.push(fun); 1966 | if (!draining) { 1967 | setTimeout(drainQueue, 0); 1968 | } 1969 | }; 1970 | 1971 | process.title = 'browser'; 1972 | process.browser = true; 1973 | process.env = {}; 1974 | process.argv = []; 1975 | process.version = ''; // empty string to avoid regexp issues 1976 | process.versions = {}; 1977 | 1978 | function noop() {} 1979 | 1980 | process.on = noop; 1981 | process.addListener = noop; 1982 | process.once = noop; 1983 | process.off = noop; 1984 | process.removeListener = noop; 1985 | process.removeAllListeners = noop; 1986 | process.emit = noop; 1987 | 1988 | process.binding = function (name) { 1989 | throw new Error('process.binding is not supported'); 1990 | }; 1991 | 1992 | // TODO(shtylman) 1993 | process.cwd = function () { return '/' }; 1994 | process.chdir = function (dir) { 1995 | throw new Error('process.chdir is not supported'); 1996 | }; 1997 | process.umask = function() { return 0; }; 1998 | 1999 | },{}],9:[function(require,module,exports){ 2000 | module.exports = function isBuffer(arg) { 2001 | return arg && typeof arg === 'object' 2002 | && typeof arg.copy === 'function' 2003 | && typeof arg.fill === 'function' 2004 | && typeof arg.readUInt8 === 'function'; 2005 | } 2006 | },{}],10:[function(require,module,exports){ 2007 | (function (process,global){ 2008 | // Copyright Joyent, Inc. and other Node contributors. 2009 | // 2010 | // Permission is hereby granted, free of charge, to any person obtaining a 2011 | // copy of this software and associated documentation files (the 2012 | // "Software"), to deal in the Software without restriction, including 2013 | // without limitation the rights to use, copy, modify, merge, publish, 2014 | // distribute, sublicense, and/or sell copies of the Software, and to permit 2015 | // persons to whom the Software is furnished to do so, subject to the 2016 | // following conditions: 2017 | // 2018 | // The above copyright notice and this permission notice shall be included 2019 | // in all copies or substantial portions of the Software. 2020 | // 2021 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 2022 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 2023 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 2024 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 2025 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 2026 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 2027 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 2028 | 2029 | var formatRegExp = /%[sdj%]/g; 2030 | exports.format = function(f) { 2031 | if (!isString(f)) { 2032 | var objects = []; 2033 | for (var i = 0; i < arguments.length; i++) { 2034 | objects.push(inspect(arguments[i])); 2035 | } 2036 | return objects.join(' '); 2037 | } 2038 | 2039 | var i = 1; 2040 | var args = arguments; 2041 | var len = args.length; 2042 | var str = String(f).replace(formatRegExp, function(x) { 2043 | if (x === '%%') return '%'; 2044 | if (i >= len) return x; 2045 | switch (x) { 2046 | case '%s': return String(args[i++]); 2047 | case '%d': return Number(args[i++]); 2048 | case '%j': 2049 | try { 2050 | return JSON.stringify(args[i++]); 2051 | } catch (_) { 2052 | return '[Circular]'; 2053 | } 2054 | default: 2055 | return x; 2056 | } 2057 | }); 2058 | for (var x = args[i]; i < len; x = args[++i]) { 2059 | if (isNull(x) || !isObject(x)) { 2060 | str += ' ' + x; 2061 | } else { 2062 | str += ' ' + inspect(x); 2063 | } 2064 | } 2065 | return str; 2066 | }; 2067 | 2068 | 2069 | // Mark that a method should not be used. 2070 | // Returns a modified function which warns once by default. 2071 | // If --no-deprecation is set, then it is a no-op. 2072 | exports.deprecate = function(fn, msg) { 2073 | // Allow for deprecating things in the process of starting up. 2074 | if (isUndefined(global.process)) { 2075 | return function() { 2076 | return exports.deprecate(fn, msg).apply(this, arguments); 2077 | }; 2078 | } 2079 | 2080 | if (process.noDeprecation === true) { 2081 | return fn; 2082 | } 2083 | 2084 | var warned = false; 2085 | function deprecated() { 2086 | if (!warned) { 2087 | if (process.throwDeprecation) { 2088 | throw new Error(msg); 2089 | } else if (process.traceDeprecation) { 2090 | console.trace(msg); 2091 | } else { 2092 | console.error(msg); 2093 | } 2094 | warned = true; 2095 | } 2096 | return fn.apply(this, arguments); 2097 | } 2098 | 2099 | return deprecated; 2100 | }; 2101 | 2102 | 2103 | var debugs = {}; 2104 | var debugEnviron; 2105 | exports.debuglog = function(set) { 2106 | if (isUndefined(debugEnviron)) 2107 | debugEnviron = process.env.NODE_DEBUG || ''; 2108 | set = set.toUpperCase(); 2109 | if (!debugs[set]) { 2110 | if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) { 2111 | var pid = process.pid; 2112 | debugs[set] = function() { 2113 | var msg = exports.format.apply(exports, arguments); 2114 | console.error('%s %d: %s', set, pid, msg); 2115 | }; 2116 | } else { 2117 | debugs[set] = function() {}; 2118 | } 2119 | } 2120 | return debugs[set]; 2121 | }; 2122 | 2123 | 2124 | /** 2125 | * Echos the value of a value. Trys to print the value out 2126 | * in the best way possible given the different types. 2127 | * 2128 | * @param {Object} obj The object to print out. 2129 | * @param {Object} opts Optional options object that alters the output. 2130 | */ 2131 | /* legacy: obj, showHidden, depth, colors*/ 2132 | function inspect(obj, opts) { 2133 | // default options 2134 | var ctx = { 2135 | seen: [], 2136 | stylize: stylizeNoColor 2137 | }; 2138 | // legacy... 2139 | if (arguments.length >= 3) ctx.depth = arguments[2]; 2140 | if (arguments.length >= 4) ctx.colors = arguments[3]; 2141 | if (isBoolean(opts)) { 2142 | // legacy... 2143 | ctx.showHidden = opts; 2144 | } else if (opts) { 2145 | // got an "options" object 2146 | exports._extend(ctx, opts); 2147 | } 2148 | // set default options 2149 | if (isUndefined(ctx.showHidden)) ctx.showHidden = false; 2150 | if (isUndefined(ctx.depth)) ctx.depth = 2; 2151 | if (isUndefined(ctx.colors)) ctx.colors = false; 2152 | if (isUndefined(ctx.customInspect)) ctx.customInspect = true; 2153 | if (ctx.colors) ctx.stylize = stylizeWithColor; 2154 | return formatValue(ctx, obj, ctx.depth); 2155 | } 2156 | exports.inspect = inspect; 2157 | 2158 | 2159 | // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics 2160 | inspect.colors = { 2161 | 'bold' : [1, 22], 2162 | 'italic' : [3, 23], 2163 | 'underline' : [4, 24], 2164 | 'inverse' : [7, 27], 2165 | 'white' : [37, 39], 2166 | 'grey' : [90, 39], 2167 | 'black' : [30, 39], 2168 | 'blue' : [34, 39], 2169 | 'cyan' : [36, 39], 2170 | 'green' : [32, 39], 2171 | 'magenta' : [35, 39], 2172 | 'red' : [31, 39], 2173 | 'yellow' : [33, 39] 2174 | }; 2175 | 2176 | // Don't use 'blue' not visible on cmd.exe 2177 | inspect.styles = { 2178 | 'special': 'cyan', 2179 | 'number': 'yellow', 2180 | 'boolean': 'yellow', 2181 | 'undefined': 'grey', 2182 | 'null': 'bold', 2183 | 'string': 'green', 2184 | 'date': 'magenta', 2185 | // "name": intentionally not styling 2186 | 'regexp': 'red' 2187 | }; 2188 | 2189 | 2190 | function stylizeWithColor(str, styleType) { 2191 | var style = inspect.styles[styleType]; 2192 | 2193 | if (style) { 2194 | return '\u001b[' + inspect.colors[style][0] + 'm' + str + 2195 | '\u001b[' + inspect.colors[style][1] + 'm'; 2196 | } else { 2197 | return str; 2198 | } 2199 | } 2200 | 2201 | 2202 | function stylizeNoColor(str, styleType) { 2203 | return str; 2204 | } 2205 | 2206 | 2207 | function arrayToHash(array) { 2208 | var hash = {}; 2209 | 2210 | array.forEach(function(val, idx) { 2211 | hash[val] = true; 2212 | }); 2213 | 2214 | return hash; 2215 | } 2216 | 2217 | 2218 | function formatValue(ctx, value, recurseTimes) { 2219 | // Provide a hook for user-specified inspect functions. 2220 | // Check that value is an object with an inspect function on it 2221 | if (ctx.customInspect && 2222 | value && 2223 | isFunction(value.inspect) && 2224 | // Filter out the util module, it's inspect function is special 2225 | value.inspect !== exports.inspect && 2226 | // Also filter out any prototype objects using the circular check. 2227 | !(value.constructor && value.constructor.prototype === value)) { 2228 | var ret = value.inspect(recurseTimes, ctx); 2229 | if (!isString(ret)) { 2230 | ret = formatValue(ctx, ret, recurseTimes); 2231 | } 2232 | return ret; 2233 | } 2234 | 2235 | // Primitive types cannot have properties 2236 | var primitive = formatPrimitive(ctx, value); 2237 | if (primitive) { 2238 | return primitive; 2239 | } 2240 | 2241 | // Look up the keys of the object. 2242 | var keys = Object.keys(value); 2243 | var visibleKeys = arrayToHash(keys); 2244 | 2245 | if (ctx.showHidden) { 2246 | keys = Object.getOwnPropertyNames(value); 2247 | } 2248 | 2249 | // IE doesn't make error fields non-enumerable 2250 | // http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx 2251 | if (isError(value) 2252 | && (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) { 2253 | return formatError(value); 2254 | } 2255 | 2256 | // Some type of object without properties can be shortcutted. 2257 | if (keys.length === 0) { 2258 | if (isFunction(value)) { 2259 | var name = value.name ? ': ' + value.name : ''; 2260 | return ctx.stylize('[Function' + name + ']', 'special'); 2261 | } 2262 | if (isRegExp(value)) { 2263 | return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); 2264 | } 2265 | if (isDate(value)) { 2266 | return ctx.stylize(Date.prototype.toString.call(value), 'date'); 2267 | } 2268 | if (isError(value)) { 2269 | return formatError(value); 2270 | } 2271 | } 2272 | 2273 | var base = '', array = false, braces = ['{', '}']; 2274 | 2275 | // Make Array say that they are Array 2276 | if (isArray(value)) { 2277 | array = true; 2278 | braces = ['[', ']']; 2279 | } 2280 | 2281 | // Make functions say that they are functions 2282 | if (isFunction(value)) { 2283 | var n = value.name ? ': ' + value.name : ''; 2284 | base = ' [Function' + n + ']'; 2285 | } 2286 | 2287 | // Make RegExps say that they are RegExps 2288 | if (isRegExp(value)) { 2289 | base = ' ' + RegExp.prototype.toString.call(value); 2290 | } 2291 | 2292 | // Make dates with properties first say the date 2293 | if (isDate(value)) { 2294 | base = ' ' + Date.prototype.toUTCString.call(value); 2295 | } 2296 | 2297 | // Make error with message first say the error 2298 | if (isError(value)) { 2299 | base = ' ' + formatError(value); 2300 | } 2301 | 2302 | if (keys.length === 0 && (!array || value.length == 0)) { 2303 | return braces[0] + base + braces[1]; 2304 | } 2305 | 2306 | if (recurseTimes < 0) { 2307 | if (isRegExp(value)) { 2308 | return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); 2309 | } else { 2310 | return ctx.stylize('[Object]', 'special'); 2311 | } 2312 | } 2313 | 2314 | ctx.seen.push(value); 2315 | 2316 | var output; 2317 | if (array) { 2318 | output = formatArray(ctx, value, recurseTimes, visibleKeys, keys); 2319 | } else { 2320 | output = keys.map(function(key) { 2321 | return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array); 2322 | }); 2323 | } 2324 | 2325 | ctx.seen.pop(); 2326 | 2327 | return reduceToSingleString(output, base, braces); 2328 | } 2329 | 2330 | 2331 | function formatPrimitive(ctx, value) { 2332 | if (isUndefined(value)) 2333 | return ctx.stylize('undefined', 'undefined'); 2334 | if (isString(value)) { 2335 | var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '') 2336 | .replace(/'/g, "\\'") 2337 | .replace(/\\"/g, '"') + '\''; 2338 | return ctx.stylize(simple, 'string'); 2339 | } 2340 | if (isNumber(value)) 2341 | return ctx.stylize('' + value, 'number'); 2342 | if (isBoolean(value)) 2343 | return ctx.stylize('' + value, 'boolean'); 2344 | // For some reason typeof null is "object", so special case here. 2345 | if (isNull(value)) 2346 | return ctx.stylize('null', 'null'); 2347 | } 2348 | 2349 | 2350 | function formatError(value) { 2351 | return '[' + Error.prototype.toString.call(value) + ']'; 2352 | } 2353 | 2354 | 2355 | function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { 2356 | var output = []; 2357 | for (var i = 0, l = value.length; i < l; ++i) { 2358 | if (hasOwnProperty(value, String(i))) { 2359 | output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, 2360 | String(i), true)); 2361 | } else { 2362 | output.push(''); 2363 | } 2364 | } 2365 | keys.forEach(function(key) { 2366 | if (!key.match(/^\d+$/)) { 2367 | output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, 2368 | key, true)); 2369 | } 2370 | }); 2371 | return output; 2372 | } 2373 | 2374 | 2375 | function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { 2376 | var name, str, desc; 2377 | desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] }; 2378 | if (desc.get) { 2379 | if (desc.set) { 2380 | str = ctx.stylize('[Getter/Setter]', 'special'); 2381 | } else { 2382 | str = ctx.stylize('[Getter]', 'special'); 2383 | } 2384 | } else { 2385 | if (desc.set) { 2386 | str = ctx.stylize('[Setter]', 'special'); 2387 | } 2388 | } 2389 | if (!hasOwnProperty(visibleKeys, key)) { 2390 | name = '[' + key + ']'; 2391 | } 2392 | if (!str) { 2393 | if (ctx.seen.indexOf(desc.value) < 0) { 2394 | if (isNull(recurseTimes)) { 2395 | str = formatValue(ctx, desc.value, null); 2396 | } else { 2397 | str = formatValue(ctx, desc.value, recurseTimes - 1); 2398 | } 2399 | if (str.indexOf('\n') > -1) { 2400 | if (array) { 2401 | str = str.split('\n').map(function(line) { 2402 | return ' ' + line; 2403 | }).join('\n').substr(2); 2404 | } else { 2405 | str = '\n' + str.split('\n').map(function(line) { 2406 | return ' ' + line; 2407 | }).join('\n'); 2408 | } 2409 | } 2410 | } else { 2411 | str = ctx.stylize('[Circular]', 'special'); 2412 | } 2413 | } 2414 | if (isUndefined(name)) { 2415 | if (array && key.match(/^\d+$/)) { 2416 | return str; 2417 | } 2418 | name = JSON.stringify('' + key); 2419 | if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) { 2420 | name = name.substr(1, name.length - 2); 2421 | name = ctx.stylize(name, 'name'); 2422 | } else { 2423 | name = name.replace(/'/g, "\\'") 2424 | .replace(/\\"/g, '"') 2425 | .replace(/(^"|"$)/g, "'"); 2426 | name = ctx.stylize(name, 'string'); 2427 | } 2428 | } 2429 | 2430 | return name + ': ' + str; 2431 | } 2432 | 2433 | 2434 | function reduceToSingleString(output, base, braces) { 2435 | var numLinesEst = 0; 2436 | var length = output.reduce(function(prev, cur) { 2437 | numLinesEst++; 2438 | if (cur.indexOf('\n') >= 0) numLinesEst++; 2439 | return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1; 2440 | }, 0); 2441 | 2442 | if (length > 60) { 2443 | return braces[0] + 2444 | (base === '' ? '' : base + '\n ') + 2445 | ' ' + 2446 | output.join(',\n ') + 2447 | ' ' + 2448 | braces[1]; 2449 | } 2450 | 2451 | return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1]; 2452 | } 2453 | 2454 | 2455 | // NOTE: These type checking functions intentionally don't use `instanceof` 2456 | // because it is fragile and can be easily faked with `Object.create()`. 2457 | function isArray(ar) { 2458 | return Array.isArray(ar); 2459 | } 2460 | exports.isArray = isArray; 2461 | 2462 | function isBoolean(arg) { 2463 | return typeof arg === 'boolean'; 2464 | } 2465 | exports.isBoolean = isBoolean; 2466 | 2467 | function isNull(arg) { 2468 | return arg === null; 2469 | } 2470 | exports.isNull = isNull; 2471 | 2472 | function isNullOrUndefined(arg) { 2473 | return arg == null; 2474 | } 2475 | exports.isNullOrUndefined = isNullOrUndefined; 2476 | 2477 | function isNumber(arg) { 2478 | return typeof arg === 'number'; 2479 | } 2480 | exports.isNumber = isNumber; 2481 | 2482 | function isString(arg) { 2483 | return typeof arg === 'string'; 2484 | } 2485 | exports.isString = isString; 2486 | 2487 | function isSymbol(arg) { 2488 | return typeof arg === 'symbol'; 2489 | } 2490 | exports.isSymbol = isSymbol; 2491 | 2492 | function isUndefined(arg) { 2493 | return arg === void 0; 2494 | } 2495 | exports.isUndefined = isUndefined; 2496 | 2497 | function isRegExp(re) { 2498 | return isObject(re) && objectToString(re) === '[object RegExp]'; 2499 | } 2500 | exports.isRegExp = isRegExp; 2501 | 2502 | function isObject(arg) { 2503 | return typeof arg === 'object' && arg !== null; 2504 | } 2505 | exports.isObject = isObject; 2506 | 2507 | function isDate(d) { 2508 | return isObject(d) && objectToString(d) === '[object Date]'; 2509 | } 2510 | exports.isDate = isDate; 2511 | 2512 | function isError(e) { 2513 | return isObject(e) && 2514 | (objectToString(e) === '[object Error]' || e instanceof Error); 2515 | } 2516 | exports.isError = isError; 2517 | 2518 | function isFunction(arg) { 2519 | return typeof arg === 'function'; 2520 | } 2521 | exports.isFunction = isFunction; 2522 | 2523 | function isPrimitive(arg) { 2524 | return arg === null || 2525 | typeof arg === 'boolean' || 2526 | typeof arg === 'number' || 2527 | typeof arg === 'string' || 2528 | typeof arg === 'symbol' || // ES6 symbol 2529 | typeof arg === 'undefined'; 2530 | } 2531 | exports.isPrimitive = isPrimitive; 2532 | 2533 | exports.isBuffer = require('./support/isBuffer'); 2534 | 2535 | function objectToString(o) { 2536 | return Object.prototype.toString.call(o); 2537 | } 2538 | 2539 | 2540 | function pad(n) { 2541 | return n < 10 ? '0' + n.toString(10) : n.toString(10); 2542 | } 2543 | 2544 | 2545 | var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 2546 | 'Oct', 'Nov', 'Dec']; 2547 | 2548 | // 26 Feb 16:19:34 2549 | function timestamp() { 2550 | var d = new Date(); 2551 | var time = [pad(d.getHours()), 2552 | pad(d.getMinutes()), 2553 | pad(d.getSeconds())].join(':'); 2554 | return [d.getDate(), months[d.getMonth()], time].join(' '); 2555 | } 2556 | 2557 | 2558 | // log is just a thin wrapper to console.log that prepends a timestamp 2559 | exports.log = function() { 2560 | console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments)); 2561 | }; 2562 | 2563 | 2564 | /** 2565 | * Inherit the prototype methods from one constructor into another. 2566 | * 2567 | * The Function.prototype.inherits from lang.js rewritten as a standalone 2568 | * function (not on Function.prototype). NOTE: If this file is to be loaded 2569 | * during bootstrapping this function needs to be rewritten using some native 2570 | * functions as prototype setup using normal JavaScript does not work as 2571 | * expected during bootstrapping (see mirror.js in r114903). 2572 | * 2573 | * @param {function} ctor Constructor function which needs to inherit the 2574 | * prototype. 2575 | * @param {function} superCtor Constructor function to inherit prototype from. 2576 | */ 2577 | exports.inherits = require('inherits'); 2578 | 2579 | exports._extend = function(origin, add) { 2580 | // Don't do anything if add isn't an object 2581 | if (!add || !isObject(add)) return origin; 2582 | 2583 | var keys = Object.keys(add); 2584 | var i = keys.length; 2585 | while (i--) { 2586 | origin[keys[i]] = add[keys[i]]; 2587 | } 2588 | return origin; 2589 | }; 2590 | 2591 | function hasOwnProperty(obj, prop) { 2592 | return Object.prototype.hasOwnProperty.call(obj, prop); 2593 | } 2594 | 2595 | }).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) 2596 | },{"./support/isBuffer":9,"_process":8,"inherits":7}],11:[function(require,module,exports){ 2597 | (function() { 2598 | var JS_WS_CLIENT_TYPE = 'js-websocket'; 2599 | var JS_WS_CLIENT_VERSION = '0.0.1'; 2600 | 2601 | var Protocol = window.Protocol; 2602 | var protobuf = window.protobuf; 2603 | var decodeIO_protobuf = window.decodeIO_protobuf; 2604 | var decodeIO_encoder = null; 2605 | var decodeIO_decoder = null; 2606 | var Package = Protocol.Package; 2607 | var Message = Protocol.Message; 2608 | var EventEmitter = window.EventEmitter; 2609 | var rsa = window.rsa; 2610 | 2611 | if(typeof(window) != "undefined" && typeof(sys) != 'undefined' && sys.localStorage) { 2612 | window.localStorage = sys.localStorage; 2613 | } 2614 | 2615 | var RES_OK = 200; 2616 | var RES_FAIL = 500; 2617 | var RES_OLD_CLIENT = 501; 2618 | 2619 | if (typeof Object.create !== 'function') { 2620 | Object.create = function (o) { 2621 | function F() {} 2622 | F.prototype = o; 2623 | return new F(); 2624 | }; 2625 | } 2626 | 2627 | var root = window; 2628 | var pomelo = Object.create(EventEmitter.prototype); // object extend from object 2629 | root.pomelo = pomelo; 2630 | var socket = null; 2631 | var reqId = 0; 2632 | var callbacks = {}; 2633 | var handlers = {}; 2634 | //Map from request id to route 2635 | var routeMap = {}; 2636 | var dict = {}; // route string to code 2637 | var abbrs = {}; // code to route string 2638 | var serverProtos = {}; 2639 | var clientProtos = {}; 2640 | var protoVersion = 0; 2641 | 2642 | var heartbeatInterval = 0; 2643 | var heartbeatTimeout = 0; 2644 | var nextHeartbeatTimeout = 0; 2645 | var gapThreshold = 100; // heartbeat gap threashold 2646 | var heartbeatId = null; 2647 | var heartbeatTimeoutId = null; 2648 | var handshakeCallback = null; 2649 | 2650 | var decode = null; 2651 | var encode = null; 2652 | 2653 | var reconnect = false; 2654 | var reconncetTimer = null; 2655 | var reconnectUrl = null; 2656 | var reconnectAttempts = 0; 2657 | var reconnectionDelay = 5000; 2658 | var DEFAULT_MAX_RECONNECT_ATTEMPTS = 10; 2659 | 2660 | var useCrypto; 2661 | 2662 | var handshakeBuffer = { 2663 | 'sys': { 2664 | type: JS_WS_CLIENT_TYPE, 2665 | version: JS_WS_CLIENT_VERSION, 2666 | rsa: {} 2667 | }, 2668 | 'user': { 2669 | } 2670 | }; 2671 | 2672 | var initCallback = null; 2673 | 2674 | pomelo.init = function(params, cb) { 2675 | initCallback = cb; 2676 | var host = params.host; 2677 | var port = params.port; 2678 | 2679 | encode = params.encode || defaultEncode; 2680 | decode = params.decode || defaultDecode; 2681 | 2682 | var url = 'ws://' + host; 2683 | if(port) { 2684 | url += ':' + port; 2685 | } 2686 | 2687 | handshakeBuffer.user = params.user; 2688 | if(params.encrypt) { 2689 | useCrypto = true; 2690 | rsa.generate(1024, "10001"); 2691 | var data = { 2692 | rsa_n: rsa.n.toString(16), 2693 | rsa_e: rsa.e 2694 | } 2695 | handshakeBuffer.sys.rsa = data; 2696 | } 2697 | handshakeCallback = params.handshakeCallback; 2698 | connect(params, url, cb); 2699 | }; 2700 | 2701 | var defaultDecode = pomelo.decode = function(data) { 2702 | //probuff decode 2703 | var msg = Message.decode(data); 2704 | 2705 | if(msg.id > 0){ 2706 | msg.route = routeMap[msg.id]; 2707 | delete routeMap[msg.id]; 2708 | if(!msg.route){ 2709 | return; 2710 | } 2711 | } 2712 | 2713 | msg.body = deCompose(msg); 2714 | return msg; 2715 | }; 2716 | 2717 | var defaultEncode = pomelo.encode = function(reqId, route, msg) { 2718 | var type = reqId ? Message.TYPE_REQUEST : Message.TYPE_NOTIFY; 2719 | 2720 | //compress message by protobuf 2721 | if(protobuf && clientProtos[route]) { 2722 | msg = protobuf.encode(route, msg); 2723 | } else if(decodeIO_encoder && decodeIO_encoder.lookup(route)) { 2724 | var Builder = decodeIO_encoder.build(route); 2725 | msg = new Builder(msg).encodeNB(); 2726 | } else { 2727 | msg = Protocol.strencode(JSON.stringify(msg)); 2728 | } 2729 | 2730 | var compressRoute = 0; 2731 | if(dict && dict[route]) { 2732 | route = dict[route]; 2733 | compressRoute = 1; 2734 | } 2735 | 2736 | return Message.encode(reqId, type, compressRoute, route, msg); 2737 | }; 2738 | 2739 | var connect = function(params, url, cb) { 2740 | console.log('connect to ' + url); 2741 | 2742 | var params = params || {}; 2743 | var maxReconnectAttempts = params.maxReconnectAttempts || DEFAULT_MAX_RECONNECT_ATTEMPTS; 2744 | reconnectUrl = url; 2745 | //Add protobuf version 2746 | if(window.localStorage && window.localStorage.getItem('protos') && protoVersion === 0) { 2747 | var protos = JSON.parse(window.localStorage.getItem('protos')); 2748 | 2749 | protoVersion = protos.version || 0; 2750 | serverProtos = protos.server || {}; 2751 | clientProtos = protos.client || {}; 2752 | 2753 | if(!!protobuf) { 2754 | protobuf.init({encoderProtos: clientProtos, decoderProtos: serverProtos}); 2755 | } 2756 | if(!!decodeIO_protobuf) { 2757 | decodeIO_encoder = decodeIO_protobuf.loadJson(clientProtos); 2758 | decodeIO_decoder = decodeIO_protobuf.loadJson(serverProtos); 2759 | } 2760 | } 2761 | //Set protoversion 2762 | handshakeBuffer.sys.protoVersion = protoVersion; 2763 | 2764 | var onopen = function(event) { 2765 | if(!!reconnect) { 2766 | pomelo.emit('reconnect'); 2767 | } 2768 | reset(); 2769 | var obj = Package.encode(Package.TYPE_HANDSHAKE, Protocol.strencode(JSON.stringify(handshakeBuffer))); 2770 | send(obj); 2771 | }; 2772 | var onmessage = function(event) { 2773 | processPackage(Package.decode(event.data), cb); 2774 | // new package arrived, update the heartbeat timeout 2775 | if(heartbeatTimeout) { 2776 | nextHeartbeatTimeout = Date.now() + heartbeatTimeout; 2777 | } 2778 | }; 2779 | var onerror = function(event) { 2780 | pomelo.emit('io-error', event); 2781 | console.error('socket error: ', event); 2782 | }; 2783 | var onclose = function(event) { 2784 | pomelo.emit('close',event); 2785 | pomelo.emit('disconnect', event); 2786 | console.error('socket close: ', event); 2787 | if(!!params.reconnect && reconnectAttempts < maxReconnectAttempts) { 2788 | reconnect = true; 2789 | reconnectAttempts++; 2790 | reconncetTimer = setTimeout(function() { 2791 | connect(params, reconnectUrl, cb); 2792 | }, reconnectionDelay); 2793 | reconnectionDelay *= 2; 2794 | } 2795 | }; 2796 | socket = new WebSocket(url); 2797 | socket.binaryType = 'arraybuffer'; 2798 | socket.onopen = onopen; 2799 | socket.onmessage = onmessage; 2800 | socket.onerror = onerror; 2801 | socket.onclose = onclose; 2802 | }; 2803 | 2804 | pomelo.disconnect = function() { 2805 | if(socket) { 2806 | if(socket.disconnect) socket.disconnect(); 2807 | if(socket.close) socket.close(); 2808 | console.log('disconnect'); 2809 | socket = null; 2810 | } 2811 | 2812 | if(heartbeatId) { 2813 | clearTimeout(heartbeatId); 2814 | heartbeatId = null; 2815 | } 2816 | if(heartbeatTimeoutId) { 2817 | clearTimeout(heartbeatTimeoutId); 2818 | heartbeatTimeoutId = null; 2819 | } 2820 | }; 2821 | 2822 | var reset = function() { 2823 | reconnect = false; 2824 | reconnectionDelay = 1000 * 5; 2825 | reconnectAttempts = 0; 2826 | clearTimeout(reconncetTimer); 2827 | }; 2828 | 2829 | pomelo.request = function(route, msg, cb) { 2830 | if(arguments.length === 2 && typeof msg === 'function') { 2831 | cb = msg; 2832 | msg = {}; 2833 | } else { 2834 | msg = msg || {}; 2835 | } 2836 | route = route || msg.route; 2837 | if(!route) { 2838 | return; 2839 | } 2840 | 2841 | reqId++; 2842 | sendMessage(reqId, route, msg); 2843 | 2844 | callbacks[reqId] = cb; 2845 | routeMap[reqId] = route; 2846 | }; 2847 | 2848 | pomelo.notify = function(route, msg) { 2849 | msg = msg || {}; 2850 | sendMessage(0, route, msg); 2851 | }; 2852 | 2853 | var sendMessage = function(reqId, route, msg) { 2854 | if(useCrypto) { 2855 | msg = JSON.stringify(msg); 2856 | var sig = rsa.signString(msg, "sha256"); 2857 | msg = JSON.parse(msg); 2858 | msg['__crypto__'] = sig; 2859 | } 2860 | 2861 | if(encode) { 2862 | msg = encode(reqId, route, msg); 2863 | } 2864 | 2865 | var packet = Package.encode(Package.TYPE_DATA, msg); 2866 | send(packet); 2867 | }; 2868 | 2869 | var send = function(packet) { 2870 | if(socket) 2871 | socket.send(packet.buffer); 2872 | }; 2873 | 2874 | var handler = {}; 2875 | 2876 | var heartbeat = function(data) { 2877 | if(!heartbeatInterval) { 2878 | // no heartbeat 2879 | return; 2880 | } 2881 | 2882 | var obj = Package.encode(Package.TYPE_HEARTBEAT); 2883 | if(heartbeatTimeoutId) { 2884 | clearTimeout(heartbeatTimeoutId); 2885 | heartbeatTimeoutId = null; 2886 | } 2887 | 2888 | if(heartbeatId) { 2889 | // already in a heartbeat interval 2890 | return; 2891 | } 2892 | heartbeatId = setTimeout(function() { 2893 | heartbeatId = null; 2894 | send(obj); 2895 | 2896 | nextHeartbeatTimeout = Date.now() + heartbeatTimeout; 2897 | heartbeatTimeoutId = setTimeout(heartbeatTimeoutCb, heartbeatTimeout); 2898 | }, heartbeatInterval); 2899 | }; 2900 | 2901 | var heartbeatTimeoutCb = function() { 2902 | var gap = nextHeartbeatTimeout - Date.now(); 2903 | if(gap > gapThreshold) { 2904 | heartbeatTimeoutId = setTimeout(heartbeatTimeoutCb, gap); 2905 | } else { 2906 | console.error('server heartbeat timeout'); 2907 | pomelo.emit('heartbeat timeout'); 2908 | pomelo.disconnect(); 2909 | } 2910 | }; 2911 | 2912 | var handshake = function(data) { 2913 | data = JSON.parse(Protocol.strdecode(data)); 2914 | if(data.code === RES_OLD_CLIENT) { 2915 | pomelo.emit('error', 'client version not fullfill'); 2916 | return; 2917 | } 2918 | 2919 | if(data.code !== RES_OK) { 2920 | pomelo.emit('error', 'handshake fail'); 2921 | return; 2922 | } 2923 | 2924 | handshakeInit(data); 2925 | 2926 | var obj = Package.encode(Package.TYPE_HANDSHAKE_ACK); 2927 | send(obj); 2928 | if(initCallback) { 2929 | initCallback(socket); 2930 | } 2931 | }; 2932 | 2933 | var onData = function(data) { 2934 | var msg = data; 2935 | if(decode) { 2936 | msg = decode(msg); 2937 | } 2938 | processMessage(pomelo, msg); 2939 | }; 2940 | 2941 | var onKick = function(data) { 2942 | data = JSON.parse(Protocol.strdecode(data)); 2943 | pomelo.emit('onKick', data); 2944 | }; 2945 | 2946 | handlers[Package.TYPE_HANDSHAKE] = handshake; 2947 | handlers[Package.TYPE_HEARTBEAT] = heartbeat; 2948 | handlers[Package.TYPE_DATA] = onData; 2949 | handlers[Package.TYPE_KICK] = onKick; 2950 | 2951 | var processPackage = function(msgs) { 2952 | if(Array.isArray(msgs)) { 2953 | for(var i=0; i>3 3217 | }; 3218 | } 3219 | 3220 | /** 3221 | * Get tag head without move the offset 3222 | */ 3223 | function peekHead(){ 3224 | var tag = codec.decodeUInt32(peekBytes()); 3225 | 3226 | return { 3227 | type : tag&0x7, 3228 | tag : tag>>3 3229 | }; 3230 | } 3231 | 3232 | function decodeProp(type, protos){ 3233 | switch(type){ 3234 | case 'uInt32': 3235 | return codec.decodeUInt32(getBytes()); 3236 | case 'int32' : 3237 | case 'sInt32' : 3238 | return codec.decodeSInt32(getBytes()); 3239 | case 'float' : 3240 | var float = buffer.readFloatLE(offset); 3241 | offset += 4; 3242 | return float; 3243 | case 'double' : 3244 | var double = buffer.readDoubleLE(offset); 3245 | offset += 8; 3246 | return double; 3247 | case 'string' : 3248 | var length = codec.decodeUInt32(getBytes()); 3249 | 3250 | var str = buffer.toString('utf8', offset, offset+length); 3251 | offset += length; 3252 | 3253 | return str; 3254 | default : 3255 | var message = protos && (protos.__messages[type] || Decoder.protos['message ' + type]); 3256 | if(message){ 3257 | var length = codec.decodeUInt32(getBytes()); 3258 | var msg = {}; 3259 | decodeMsg(msg, message, offset+length); 3260 | return msg; 3261 | } 3262 | break; 3263 | } 3264 | } 3265 | 3266 | function decodeArray(array, type, protos){ 3267 | if(util.isSimpleType(type)){ 3268 | var length = codec.decodeUInt32(getBytes()); 3269 | 3270 | for(var i = 0; i < length; i++){ 3271 | array.push(decodeProp(type)); 3272 | } 3273 | }else{ 3274 | array.push(decodeProp(type, protos)); 3275 | } 3276 | } 3277 | 3278 | function getBytes(flag){ 3279 | var bytes = []; 3280 | var pos = offset; 3281 | flag = flag || false; 3282 | 3283 | var b; 3284 | do{ 3285 | var b = buffer.readUInt8(pos); 3286 | bytes.push(b); 3287 | pos++; 3288 | }while(b >= 128); 3289 | 3290 | if(!flag){ 3291 | offset = pos; 3292 | } 3293 | return bytes; 3294 | } 3295 | 3296 | function peekBytes(){ 3297 | return getBytes(true); 3298 | } 3299 | },{"./codec":12,"./util":18}],15:[function(require,module,exports){ 3300 | (function (Buffer){ 3301 | var codec = require('./codec'); 3302 | var constant = require('./constant'); 3303 | var util = require('./util'); 3304 | 3305 | var Encoder = module.exports; 3306 | 3307 | Encoder.init = function(protos){ 3308 | this.protos = protos || {}; 3309 | }; 3310 | 3311 | Encoder.encode = function(route, msg){ 3312 | if(!route || !msg){ 3313 | console.warn('Route or msg can not be null! route : %j, msg %j', route, msg); 3314 | return null; 3315 | } 3316 | 3317 | //Get protos from protos map use the route as key 3318 | var protos = this.protos[route]; 3319 | 3320 | //Check msg 3321 | if(!checkMsg(msg, protos)){ 3322 | console.warn('check msg failed! msg : %j, proto : %j', msg, protos); 3323 | return null; 3324 | } 3325 | 3326 | //Set the length of the buffer 2 times bigger to prevent overflow 3327 | var length = Buffer.byteLength(JSON.stringify(msg))*2; 3328 | 3329 | //Init buffer and offset 3330 | var buffer = new Buffer(length); 3331 | var offset = 0; 3332 | 3333 | if(!!protos){ 3334 | offset = encodeMsg(buffer, offset, protos, msg); 3335 | if(offset > 0){ 3336 | return buffer.slice(0, offset); 3337 | } 3338 | } 3339 | 3340 | return null; 3341 | }; 3342 | 3343 | /** 3344 | * Check if the msg follow the defination in the protos 3345 | */ 3346 | function checkMsg(msg, protos){ 3347 | if(!protos || !msg){ 3348 | console.warn('no protos or msg exist! msg : %j, protos : %j', msg, protos); 3349 | return false; 3350 | } 3351 | 3352 | for(var name in protos){ 3353 | var proto = protos[name]; 3354 | 3355 | //All required element must exist 3356 | switch(proto.option){ 3357 | case 'required' : 3358 | if(typeof(msg[name]) === 'undefined'){ 3359 | console.warn('no property exist for required! name: %j, proto: %j, msg: %j', name, proto, msg); 3360 | return false; 3361 | } 3362 | case 'optional' : 3363 | if(typeof(msg[name]) !== 'undefined'){ 3364 | var message = protos.__messages[proto.type] || Encoder.protos['message ' + proto.type]; 3365 | if(!!message && !checkMsg(msg[name], message)){ 3366 | console.warn('inner proto error! name: %j, proto: %j, msg: %j', name, proto, msg); 3367 | return false; 3368 | } 3369 | } 3370 | break; 3371 | case 'repeated' : 3372 | //Check nest message in repeated elements 3373 | var message = protos.__messages[proto.type] || Encoder.protos['message ' + proto.type]; 3374 | if(!!msg[name] && !!message){ 3375 | for(var i = 0; i < msg[name].length; i++){ 3376 | if(!checkMsg(msg[name][i], message)){ 3377 | return false; 3378 | } 3379 | } 3380 | } 3381 | break; 3382 | } 3383 | } 3384 | 3385 | return true; 3386 | } 3387 | 3388 | function encodeMsg(buffer, offset, protos, msg){ 3389 | for(var name in msg){ 3390 | if(!!protos[name]){ 3391 | var proto = protos[name]; 3392 | 3393 | switch(proto.option){ 3394 | case 'required' : 3395 | case 'optional' : 3396 | offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag)); 3397 | offset = encodeProp(msg[name], proto.type, offset, buffer, protos); 3398 | break; 3399 | case 'repeated' : 3400 | if(!!msg[name] && msg[name].length > 0){ 3401 | offset = encodeArray(msg[name], proto, offset, buffer, protos); 3402 | } 3403 | break; 3404 | } 3405 | } 3406 | } 3407 | 3408 | return offset; 3409 | } 3410 | 3411 | function encodeProp(value, type, offset, buffer, protos){ 3412 | var length = 0; 3413 | 3414 | switch(type){ 3415 | case 'uInt32': 3416 | offset = writeBytes(buffer, offset, codec.encodeUInt32(value)); 3417 | break; 3418 | case 'int32' : 3419 | case 'sInt32': 3420 | offset = writeBytes(buffer, offset, codec.encodeSInt32(value)); 3421 | break; 3422 | case 'float': 3423 | buffer.writeFloatLE(value, offset); 3424 | offset += 4; 3425 | break; 3426 | case 'double': 3427 | buffer.writeDoubleLE(value, offset); 3428 | offset += 8; 3429 | break; 3430 | case 'string': 3431 | length = Buffer.byteLength(value); 3432 | 3433 | //Encode length 3434 | offset = writeBytes(buffer, offset, codec.encodeUInt32(length)); 3435 | //write string 3436 | buffer.write(value, offset, length); 3437 | offset += length; 3438 | break; 3439 | default : 3440 | var message = protos.__messages[type] || Encoder.protos['message ' + type]; 3441 | if(!!message){ 3442 | //Use a tmp buffer to build an internal msg 3443 | var tmpBuffer = new Buffer(Buffer.byteLength(JSON.stringify(value))*2); 3444 | length = 0; 3445 | 3446 | length = encodeMsg(tmpBuffer, length, message, value); 3447 | //Encode length 3448 | offset = writeBytes(buffer, offset, codec.encodeUInt32(length)); 3449 | //contact the object 3450 | tmpBuffer.copy(buffer, offset, 0, length); 3451 | 3452 | offset += length; 3453 | } 3454 | break; 3455 | } 3456 | 3457 | return offset; 3458 | } 3459 | 3460 | /** 3461 | * Encode reapeated properties, simple msg and object are decode differented 3462 | */ 3463 | function encodeArray(array, proto, offset, buffer, protos){ 3464 | var i = 0; 3465 | if(util.isSimpleType(proto.type)){ 3466 | offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag)); 3467 | offset = writeBytes(buffer, offset, codec.encodeUInt32(array.length)); 3468 | for(i = 0; i < array.length; i++){ 3469 | offset = encodeProp(array[i], proto.type, offset, buffer); 3470 | } 3471 | }else{ 3472 | for(i = 0; i < array.length; i++){ 3473 | offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag)); 3474 | offset = encodeProp(array[i], proto.type, offset, buffer, protos); 3475 | } 3476 | } 3477 | 3478 | return offset; 3479 | } 3480 | 3481 | function writeBytes(buffer, offset, bytes){ 3482 | for(var i = 0; i < bytes.length; i++){ 3483 | buffer.writeUInt8(bytes[i], offset); 3484 | offset++; 3485 | } 3486 | 3487 | return offset; 3488 | } 3489 | 3490 | function encodeTag(type, tag){ 3491 | var value = constant.TYPES[type]; 3492 | 3493 | if(value === undefined) value = 2; 3494 | 3495 | return codec.encodeUInt32((tag<<3)|value); 3496 | } 3497 | 3498 | }).call(this,require("buffer").Buffer) 3499 | },{"./codec":12,"./constant":13,"./util":18,"buffer":2}],16:[function(require,module,exports){ 3500 | var Parser = module.exports; 3501 | 3502 | /** 3503 | * [parse the original protos, give the paresed result can be used by protobuf encode/decode.] 3504 | * @param {[Object]} protos Original protos, in a js map. 3505 | * @return {[Object]} The presed result, a js object represent all the meta data of the given protos. 3506 | */ 3507 | Parser.parse = function(protos){ 3508 | var maps = {}; 3509 | for(var key in protos){ 3510 | maps[key] = parseObject(protos[key]); 3511 | } 3512 | 3513 | return maps; 3514 | }; 3515 | 3516 | /** 3517 | * [parse a single protos, return a object represent the result. The method can be invocked recursively.] 3518 | * @param {[Object]} obj The origin proto need to parse. 3519 | * @return {[Object]} The parsed result, a js object. 3520 | */ 3521 | function parseObject(obj){ 3522 | var proto = {}; 3523 | var nestProtos = {}; 3524 | var tags = {}; 3525 | 3526 | for(var name in obj){ 3527 | var tag = obj[name]; 3528 | var params = name.split(' '); 3529 | 3530 | switch(params[0]){ 3531 | case 'message': 3532 | if(params.length !== 2){ 3533 | continue; 3534 | } 3535 | nestProtos[params[1]] = parseObject(tag); 3536 | continue; 3537 | case 'required': 3538 | case 'optional': 3539 | case 'repeated':{ 3540 | //params length should be 3 and tag can't be duplicated 3541 | if(params.length !== 3 || !!tags[tag]){ 3542 | continue; 3543 | } 3544 | proto[params[2]] = { 3545 | option : params[0], 3546 | type : params[1], 3547 | tag : tag 3548 | }; 3549 | tags[tag] = params[2]; 3550 | } 3551 | } 3552 | } 3553 | 3554 | proto.__messages = nestProtos; 3555 | proto.__tags = tags; 3556 | return proto; 3557 | } 3558 | },{}],17:[function(require,module,exports){ 3559 | (function (Buffer){ 3560 | var encoder = require('./encoder'); 3561 | var decoder = require('./decoder'); 3562 | var parser = require('./parser'); 3563 | 3564 | var Protobuf = module.exports; 3565 | 3566 | /** 3567 | * [encode the given message, return a Buffer represent the message encoded by protobuf] 3568 | * @param {[type]} key The key to identify the message type. 3569 | * @param {[type]} msg The message body, a js object. 3570 | * @return {[type]} The binary encode result in a Buffer. 3571 | */ 3572 | Protobuf.encode = function(key, msg){ 3573 | return encoder.encode(key, msg); 3574 | }; 3575 | 3576 | Protobuf.encode2Bytes = function(key, msg){ 3577 | var buffer = this.encode(key, msg); 3578 | if(!buffer || !buffer.length){ 3579 | console.warn('encode msg failed! key : %j, msg : %j', key, msg); 3580 | return null; 3581 | } 3582 | var bytes = new Uint8Array(buffer.length); 3583 | for(var offset = 0; offset < buffer.length; offset++){ 3584 | bytes[offset] = buffer.readUInt8(offset); 3585 | } 3586 | 3587 | return bytes; 3588 | }; 3589 | 3590 | Protobuf.encodeStr = function(key, msg, code){ 3591 | code = code || 'base64'; 3592 | var buffer = Protobuf.encode(key, msg); 3593 | return !!buffer?buffer.toString(code):buffer; 3594 | }; 3595 | 3596 | Protobuf.decode = function(key, msg){ 3597 | return decoder.decode(key, msg); 3598 | }; 3599 | 3600 | Protobuf.decodeStr = function(key, str, code){ 3601 | code = code || 'base64'; 3602 | var buffer = new Buffer(str, code); 3603 | 3604 | return !!buffer?Protobuf.decode(key, buffer):buffer; 3605 | }; 3606 | 3607 | Protobuf.parse = function(json){ 3608 | return parser.parse(json); 3609 | }; 3610 | 3611 | Protobuf.setEncoderProtos = function(protos){ 3612 | encoder.init(protos); 3613 | }; 3614 | 3615 | Protobuf.setDecoderProtos = function(protos){ 3616 | decoder.init(protos); 3617 | }; 3618 | 3619 | Protobuf.init = function(opts){ 3620 | //On the serverside, use serverProtos to encode messages send to client 3621 | encoder.init(opts.encoderProtos); 3622 | 3623 | //On the serverside, user clientProtos to decode messages receive from clients 3624 | decoder.init(opts.decoderProtos); 3625 | 3626 | }; 3627 | }).call(this,require("buffer").Buffer) 3628 | },{"./decoder":14,"./encoder":15,"./parser":16,"buffer":2}],18:[function(require,module,exports){ 3629 | var util = module.exports; 3630 | 3631 | util.isSimpleType = function(type){ 3632 | return ( type === 'uInt32' || 3633 | type === 'sInt32' || 3634 | type === 'int32' || 3635 | type === 'uInt64' || 3636 | type === 'sInt64' || 3637 | type === 'float' || 3638 | type === 'double'); 3639 | }; 3640 | 3641 | util.equal = function(obj0, obj1){ 3642 | for(var key in obj0){ 3643 | var m = obj0[key]; 3644 | var n = obj1[key]; 3645 | 3646 | if(typeof(m) === 'object'){ 3647 | if(!util.equal(m, n)){ 3648 | return false; 3649 | } 3650 | }else if(m !== n){ 3651 | return false; 3652 | } 3653 | } 3654 | 3655 | return true; 3656 | }; 3657 | },{}],19:[function(require,module,exports){ 3658 | module.exports = require('./lib/protocol'); 3659 | },{"./lib/protocol":20}],20:[function(require,module,exports){ 3660 | (function (Buffer){ 3661 | (function (exports, ByteArray, global) { 3662 | var Protocol = exports; 3663 | 3664 | var PKG_HEAD_BYTES = 4; 3665 | var MSG_FLAG_BYTES = 1; 3666 | var MSG_ROUTE_CODE_BYTES = 2; 3667 | var MSG_ID_MAX_BYTES = 5; 3668 | var MSG_ROUTE_LEN_BYTES = 1; 3669 | 3670 | var MSG_ROUTE_CODE_MAX = 0xffff; 3671 | 3672 | var MSG_COMPRESS_ROUTE_MASK = 0x1; 3673 | var MSG_TYPE_MASK = 0x7; 3674 | 3675 | var Package = Protocol.Package = {}; 3676 | var Message = Protocol.Message = {}; 3677 | 3678 | Package.TYPE_HANDSHAKE = 1; 3679 | Package.TYPE_HANDSHAKE_ACK = 2; 3680 | Package.TYPE_HEARTBEAT = 3; 3681 | Package.TYPE_DATA = 4; 3682 | Package.TYPE_KICK = 5; 3683 | 3684 | Message.TYPE_REQUEST = 0; 3685 | Message.TYPE_NOTIFY = 1; 3686 | Message.TYPE_RESPONSE = 2; 3687 | Message.TYPE_PUSH = 3; 3688 | 3689 | /** 3690 | * pomele client encode 3691 | * id message id; 3692 | * route message route 3693 | * msg message body 3694 | * socketio current support string 3695 | */ 3696 | Protocol.strencode = function(str) { 3697 | if(typeof Buffer !== "undefined" && ByteArray === Buffer) { 3698 | // encoding defaults to 'utf8' 3699 | return (new Buffer(str)); 3700 | } else { 3701 | var byteArray = new ByteArray(str.length * 3); 3702 | var offset = 0; 3703 | for(var i = 0; i < str.length; i++){ 3704 | var charCode = str.charCodeAt(i); 3705 | var codes = null; 3706 | if(charCode <= 0x7f){ 3707 | codes = [charCode]; 3708 | }else if(charCode <= 0x7ff){ 3709 | codes = [0xc0|(charCode>>6), 0x80|(charCode & 0x3f)]; 3710 | }else{ 3711 | codes = [0xe0|(charCode>>12), 0x80|((charCode & 0xfc0)>>6), 0x80|(charCode & 0x3f)]; 3712 | } 3713 | for(var j = 0; j < codes.length; j++){ 3714 | byteArray[offset] = codes[j]; 3715 | ++offset; 3716 | } 3717 | } 3718 | var _buffer = new ByteArray(offset); 3719 | copyArray(_buffer, 0, byteArray, 0, offset); 3720 | return _buffer; 3721 | } 3722 | }; 3723 | 3724 | /** 3725 | * client decode 3726 | * msg String data 3727 | * return Message Object 3728 | */ 3729 | Protocol.strdecode = function(buffer) { 3730 | if(typeof Buffer !== "undefined" && ByteArray === Buffer) { 3731 | // encoding defaults to 'utf8' 3732 | return buffer.toString(); 3733 | } else { 3734 | var bytes = new ByteArray(buffer); 3735 | var array = []; 3736 | var offset = 0; 3737 | var charCode = 0; 3738 | var end = bytes.length; 3739 | while(offset < end){ 3740 | if(bytes[offset] < 128){ 3741 | charCode = bytes[offset]; 3742 | offset += 1; 3743 | }else if(bytes[offset] < 224){ 3744 | charCode = ((bytes[offset] & 0x1f)<<6) + (bytes[offset+1] & 0x3f); 3745 | offset += 2; 3746 | }else{ 3747 | charCode = ((bytes[offset] & 0x0f)<<12) + ((bytes[offset+1] & 0x3f)<<6) + (bytes[offset+2] & 0x3f); 3748 | offset += 3; 3749 | } 3750 | array.push(charCode); 3751 | } 3752 | return String.fromCharCode.apply(null, array); 3753 | } 3754 | }; 3755 | 3756 | /** 3757 | * Package protocol encode. 3758 | * 3759 | * Pomelo package format: 3760 | * +------+-------------+------------------+ 3761 | * | type | body length | body | 3762 | * +------+-------------+------------------+ 3763 | * 3764 | * Head: 4bytes 3765 | * 0: package type, 3766 | * 1 - handshake, 3767 | * 2 - handshake ack, 3768 | * 3 - heartbeat, 3769 | * 4 - data 3770 | * 5 - kick 3771 | * 1 - 3: big-endian body length 3772 | * Body: body length bytes 3773 | * 3774 | * @param {Number} type package type 3775 | * @param {ByteArray} body body content in bytes 3776 | * @return {ByteArray} new byte array that contains encode result 3777 | */ 3778 | Package.encode = function(type, body){ 3779 | var length = body ? body.length : 0; 3780 | var buffer = new ByteArray(PKG_HEAD_BYTES + length); 3781 | var index = 0; 3782 | buffer[index++] = type & 0xff; 3783 | buffer[index++] = (length >> 16) & 0xff; 3784 | buffer[index++] = (length >> 8) & 0xff; 3785 | buffer[index++] = length & 0xff; 3786 | if(body) { 3787 | copyArray(buffer, index, body, 0, length); 3788 | } 3789 | return buffer; 3790 | }; 3791 | 3792 | /** 3793 | * Package protocol decode. 3794 | * See encode for package format. 3795 | * 3796 | * @param {ByteArray} buffer byte array containing package content 3797 | * @return {Object} {type: package type, buffer: body byte array} 3798 | */ 3799 | Package.decode = function(buffer){ 3800 | var offset = 0; 3801 | var bytes = new ByteArray(buffer); 3802 | var length = 0; 3803 | var rs = []; 3804 | while(offset < bytes.length) { 3805 | var type = bytes[offset++]; 3806 | length = ((bytes[offset++]) << 16 | (bytes[offset++]) << 8 | bytes[offset++]) >>> 0; 3807 | var body = length ? new ByteArray(length) : null; 3808 | if(body) { 3809 | copyArray(body, 0, bytes, offset, length); 3810 | } 3811 | offset += length; 3812 | rs.push({'type': type, 'body': body}); 3813 | } 3814 | return rs.length === 1 ? rs[0]: rs; 3815 | }; 3816 | 3817 | /** 3818 | * Message protocol encode. 3819 | * 3820 | * @param {Number} id message id 3821 | * @param {Number} type message type 3822 | * @param {Number} compressRoute whether compress route 3823 | * @param {Number|String} route route code or route string 3824 | * @param {Buffer} msg message body bytes 3825 | * @return {Buffer} encode result 3826 | */ 3827 | Message.encode = function(id, type, compressRoute, route, msg){ 3828 | // caculate message max length 3829 | var idBytes = msgHasId(type) ? caculateMsgIdBytes(id) : 0; 3830 | var msgLen = MSG_FLAG_BYTES + idBytes; 3831 | 3832 | if(msgHasRoute(type)) { 3833 | if(compressRoute) { 3834 | if(typeof route !== 'number'){ 3835 | throw new Error('error flag for number route!'); 3836 | } 3837 | msgLen += MSG_ROUTE_CODE_BYTES; 3838 | } else { 3839 | msgLen += MSG_ROUTE_LEN_BYTES; 3840 | if(route) { 3841 | route = Protocol.strencode(route); 3842 | if(route.length>255) { 3843 | throw new Error('route maxlength is overflow'); 3844 | } 3845 | msgLen += route.length; 3846 | } 3847 | } 3848 | } 3849 | 3850 | if(msg) { 3851 | msgLen += msg.length; 3852 | } 3853 | 3854 | var buffer = new ByteArray(msgLen); 3855 | var offset = 0; 3856 | 3857 | // add flag 3858 | offset = encodeMsgFlag(type, compressRoute, buffer, offset); 3859 | 3860 | // add message id 3861 | if(msgHasId(type)) { 3862 | offset = encodeMsgId(id, buffer, offset); 3863 | } 3864 | 3865 | // add route 3866 | if(msgHasRoute(type)) { 3867 | offset = encodeMsgRoute(compressRoute, route, buffer, offset); 3868 | } 3869 | 3870 | // add body 3871 | if(msg) { 3872 | offset = encodeMsgBody(msg, buffer, offset); 3873 | } 3874 | 3875 | return buffer; 3876 | }; 3877 | 3878 | /** 3879 | * Message protocol decode. 3880 | * 3881 | * @param {Buffer|Uint8Array} buffer message bytes 3882 | * @return {Object} message object 3883 | */ 3884 | Message.decode = function(buffer) { 3885 | var bytes = new ByteArray(buffer); 3886 | var bytesLen = bytes.length || bytes.byteLength; 3887 | var offset = 0; 3888 | var id = 0; 3889 | var route = null; 3890 | 3891 | // parse flag 3892 | var flag = bytes[offset++]; 3893 | var compressRoute = flag & MSG_COMPRESS_ROUTE_MASK; 3894 | var type = (flag >> 1) & MSG_TYPE_MASK; 3895 | 3896 | // parse id 3897 | if(msgHasId(type)) { 3898 | var m = 0; 3899 | var i = 0; 3900 | do{ 3901 | m = parseInt(bytes[offset]); 3902 | id += (m & 0x7f) << (7 * i); 3903 | offset++; 3904 | i++; 3905 | }while(m >= 128); 3906 | } 3907 | 3908 | // parse route 3909 | if(msgHasRoute(type)) { 3910 | if(compressRoute) { 3911 | route = (bytes[offset++]) << 8 | bytes[offset++]; 3912 | } else { 3913 | var routeLen = bytes[offset++]; 3914 | if(routeLen) { 3915 | route = new ByteArray(routeLen); 3916 | copyArray(route, 0, bytes, offset, routeLen); 3917 | route = Protocol.strdecode(route); 3918 | } else { 3919 | route = ''; 3920 | } 3921 | offset += routeLen; 3922 | } 3923 | } 3924 | 3925 | // parse body 3926 | var bodyLen = bytesLen - offset; 3927 | var body = new ByteArray(bodyLen); 3928 | 3929 | copyArray(body, 0, bytes, offset, bodyLen); 3930 | 3931 | return {'id': id, 'type': type, 'compressRoute': compressRoute, 3932 | 'route': route, 'body': body}; 3933 | }; 3934 | 3935 | var copyArray = function(dest, doffset, src, soffset, length) { 3936 | if('function' === typeof src.copy) { 3937 | // Buffer 3938 | src.copy(dest, doffset, soffset, soffset + length); 3939 | } else { 3940 | // Uint8Array 3941 | for(var index=0; index>= 7; 3961 | } while(id > 0); 3962 | return len; 3963 | }; 3964 | 3965 | var encodeMsgFlag = function(type, compressRoute, buffer, offset) { 3966 | if(type !== Message.TYPE_REQUEST && type !== Message.TYPE_NOTIFY && 3967 | type !== Message.TYPE_RESPONSE && type !== Message.TYPE_PUSH) { 3968 | throw new Error('unkonw message type: ' + type); 3969 | } 3970 | 3971 | buffer[offset] = (type << 1) | (compressRoute ? 1 : 0); 3972 | 3973 | return offset + MSG_FLAG_BYTES; 3974 | }; 3975 | 3976 | var encodeMsgId = function(id, buffer, offset) { 3977 | do{ 3978 | var tmp = id % 128; 3979 | var next = Math.floor(id/128); 3980 | 3981 | if(next !== 0){ 3982 | tmp = tmp + 128; 3983 | } 3984 | buffer[offset++] = tmp; 3985 | 3986 | id = next; 3987 | } while(id !== 0); 3988 | 3989 | return offset; 3990 | }; 3991 | 3992 | var encodeMsgRoute = function(compressRoute, route, buffer, offset) { 3993 | if (compressRoute) { 3994 | if(route > MSG_ROUTE_CODE_MAX){ 3995 | throw new Error('route number is overflow'); 3996 | } 3997 | 3998 | buffer[offset++] = (route >> 8) & 0xff; 3999 | buffer[offset++] = route & 0xff; 4000 | } else { 4001 | if(route) { 4002 | buffer[offset++] = route.length & 0xff; 4003 | copyArray(buffer, offset, route, 0, route.length); 4004 | offset += route.length; 4005 | } else { 4006 | buffer[offset++] = 0; 4007 | } 4008 | } 4009 | 4010 | return offset; 4011 | }; 4012 | 4013 | var encodeMsgBody = function(msg, buffer, offset) { 4014 | copyArray(buffer, offset, msg, 0, msg.length); 4015 | return offset + msg.length; 4016 | }; 4017 | 4018 | module.exports = Protocol; 4019 | if(typeof(window) != "undefined") { 4020 | window.Protocol = Protocol; 4021 | } 4022 | })(typeof(window)=="undefined" ? module.exports : (this.Protocol = {}),typeof(window)=="undefined" ? Buffer : Uint8Array, this); 4023 | 4024 | }).call(this,require("buffer").Buffer) 4025 | },{"buffer":2}]},{},[1]); 4026 | --------------------------------------------------------------------------------