UNPKG

phaser

Version:

A fast, free and fun HTML5 Game Framework for Desktop and Mobile web browsers.

1,579 lines (1,344 loc) 592 kB
/** * The MIT License (MIT) * * Copyright (c) 2013 p2.js authors * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ !function(e){"object"==typeof exports?module.exports=e():"function"==typeof define&&define.amd?define('p2', (function() { return this.p2 = e(); })()):"undefined"!=typeof window?window.p2=e():"undefined"!=typeof global?self.p2=e():"undefined"!=typeof self&&(self.p2=e())}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({"PcZj9L":[function(require,module,exports){ var TA = require('typedarray') var xDataView = typeof DataView === 'undefined' ? TA.DataView : DataView var xArrayBuffer = typeof ArrayBuffer === 'undefined' ? TA.ArrayBuffer : ArrayBuffer var xUint8Array = typeof Uint8Array === 'undefined' ? TA.Uint8Array : Uint8Array exports.Buffer = Buffer exports.SlowBuffer = Buffer exports.INSPECT_MAX_BYTES = 50 Buffer.poolSize = 8192 var browserSupport /** * Class: Buffer * ============= * * The Buffer constructor returns instances of `Uint8Array` that are augmented * with function properties for all the node `Buffer` API functions. We use * `Uint8Array` so that square bracket notation works as expected -- it returns * a single octet. * * By augmenting the instances, we can avoid modifying the `Uint8Array` * prototype. * * Firefox is a special case because it doesn't allow augmenting "native" object * instances. See `ProxyBuffer` below for more details. */ function Buffer (subject, encoding) { var type = typeof subject // Work-around: node's base64 implementation // allows for non-padded strings while base64-js // does not.. if (encoding === 'base64' && type === 'string') { subject = stringtrim(subject) while (subject.length % 4 !== 0) { subject = subject + '=' } } // Find the length var length if (type === 'number') length = coerce(subject) else if (type === 'string') length = Buffer.byteLength(subject, encoding) else if (type === 'object') length = coerce(subject.length) // Assume object is an array else throw new Error('First argument needs to be a number, array or string.') var buf = augment(new xUint8Array(length)) if (Buffer.isBuffer(subject)) { // Speed optimization -- use set if we're copying from a Uint8Array buf.set(subject) } else if (isArrayIsh(subject)) { // Treat array-ish objects as a byte array. for (var i = 0; i < length; i++) { if (Buffer.isBuffer(subject)) buf[i] = subject.readUInt8(i) else buf[i] = subject[i] } } else if (type === 'string') { buf.write(subject, 0, encoding) } return buf } // STATIC METHODS // ============== Buffer.isEncoding = function(encoding) { switch ((encoding + '').toLowerCase()) { case 'hex': case 'utf8': case 'utf-8': case 'ascii': case 'binary': case 'base64': case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': case 'raw': return true default: return false } } Buffer.isBuffer = function isBuffer (b) { return b && b._isBuffer } Buffer.byteLength = function (str, encoding) { switch (encoding || 'utf8') { case 'hex': return str.length / 2 case 'utf8': case 'utf-8': return utf8ToBytes(str).length case 'ascii': case 'binary': return str.length case 'base64': return base64ToBytes(str).length default: throw new Error('Unknown encoding') } } Buffer.concat = function (list, totalLength) { if (!Array.isArray(list)) { throw new Error('Usage: Buffer.concat(list, [totalLength])\n' + 'list should be an Array.') } var i var buf if (list.length === 0) { return new Buffer(0) } else if (list.length === 1) { return list[0] } if (typeof totalLength !== 'number') { totalLength = 0 for (i = 0; i < list.length; i++) { buf = list[i] totalLength += buf.length } } var buffer = new Buffer(totalLength) var pos = 0 for (i = 0; i < list.length; i++) { buf = list[i] buf.copy(buffer, pos) pos += buf.length } return buffer } // INSTANCE METHODS // ================ function _hexWrite (buf, string, offset, length) { offset = Number(offset) || 0 var remaining = buf.length - offset if (!length) { length = remaining } else { length = Number(length) if (length > remaining) { length = remaining } } // must be an even number of digits var strLen = string.length if (strLen % 2 !== 0) { throw new Error('Invalid hex string') } if (length > strLen / 2) { length = strLen / 2 } for (var i = 0; i < length; i++) { var byte = parseInt(string.substr(i * 2, 2), 16) if (isNaN(byte)) throw new Error('Invalid hex string') buf[offset + i] = byte } Buffer._charsWritten = i * 2 return i } function _utf8Write (buf, string, offset, length) { var bytes, pos return Buffer._charsWritten = blitBuffer(utf8ToBytes(string), buf, offset, length) } function _asciiWrite (buf, string, offset, length) { var bytes, pos return Buffer._charsWritten = blitBuffer(asciiToBytes(string), buf, offset, length) } function _binaryWrite (buf, string, offset, length) { return _asciiWrite(buf, string, offset, length) } function _base64Write (buf, string, offset, length) { var bytes, pos return Buffer._charsWritten = blitBuffer(base64ToBytes(string), buf, offset, length) } function BufferWrite (string, offset, length, encoding) { // Support both (string, offset, length, encoding) // and the legacy (string, encoding, offset, length) if (isFinite(offset)) { if (!isFinite(length)) { encoding = length length = undefined } } else { // legacy var swap = encoding encoding = offset offset = length length = swap } offset = Number(offset) || 0 var remaining = this.length - offset if (!length) { length = remaining } else { length = Number(length) if (length > remaining) { length = remaining } } encoding = String(encoding || 'utf8').toLowerCase() switch (encoding) { case 'hex': return _hexWrite(this, string, offset, length) case 'utf8': case 'utf-8': return _utf8Write(this, string, offset, length) case 'ascii': return _asciiWrite(this, string, offset, length) case 'binary': return _binaryWrite(this, string, offset, length) case 'base64': return _base64Write(this, string, offset, length) default: throw new Error('Unknown encoding') } } function BufferToString (encoding, start, end) { var self = (this instanceof ProxyBuffer) ? this._proxy : this encoding = String(encoding || 'utf8').toLowerCase() start = Number(start) || 0 end = (end !== undefined) ? Number(end) : end = self.length // Fastpath empty strings if (end === start) return '' switch (encoding) { case 'hex': return _hexSlice(self, start, end) case 'utf8': case 'utf-8': return _utf8Slice(self, start, end) case 'ascii': return _asciiSlice(self, start, end) case 'binary': return _binarySlice(self, start, end) case 'base64': return _base64Slice(self, start, end) default: throw new Error('Unknown encoding') } } function BufferToJSON () { return { type: 'Buffer', data: Array.prototype.slice.call(this, 0) } } // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) function BufferCopy (target, target_start, start, end) { var source = this if (!start) start = 0 if (!end && end !== 0) end = this.length if (!target_start) target_start = 0 // Copy 0 bytes; we're done if (end === start) return if (target.length === 0 || source.length === 0) return // Fatal error conditions if (end < start) throw new Error('sourceEnd < sourceStart') if (target_start < 0 || target_start >= target.length) throw new Error('targetStart out of bounds') if (start < 0 || start >= source.length) throw new Error('sourceStart out of bounds') if (end < 0 || end > source.length) throw new Error('sourceEnd out of bounds') // Are we oob? if (end > this.length) end = this.length if (target.length - target_start < end - start) end = target.length - target_start + start // copy! for (var i = 0; i < end - start; i++) target[i + target_start] = this[i + start] } function _base64Slice (buf, start, end) { var bytes = buf.slice(start, end) return require('base64-js').fromByteArray(bytes) } function _utf8Slice (buf, start, end) { var bytes = buf.slice(start, end) var res = '' var tmp = '' var i = 0 while (i < bytes.length) { if (bytes[i] <= 0x7F) { res += decodeUtf8Char(tmp) + String.fromCharCode(bytes[i]) tmp = '' } else { tmp += '%' + bytes[i].toString(16) } i++ } return res + decodeUtf8Char(tmp) } function _asciiSlice (buf, start, end) { var bytes = buf.slice(start, end) var ret = '' for (var i = 0; i < bytes.length; i++) ret += String.fromCharCode(bytes[i]) return ret } function _binarySlice (buf, start, end) { return _asciiSlice(buf, start, end) } function _hexSlice (buf, start, end) { var len = buf.length if (!start || start < 0) start = 0 if (!end || end < 0 || end > len) end = len var out = '' for (var i = start; i < end; i++) { out += toHex(buf[i]) } return out } // TODO: add test that modifying the new buffer slice will modify memory in the // original buffer! Use code from: // http://nodejs.org/api/buffer.html#buffer_buf_slice_start_end function BufferSlice (start, end) { var len = this.length start = clamp(start, len, 0) end = clamp(end, len, len) return augment(this.subarray(start, end)) // Uint8Array built-in method } function BufferReadUInt8 (offset, noAssert) { var buf = this if (!noAssert) { assert(offset !== undefined && offset !== null, 'missing offset') assert(offset < buf.length, 'Trying to read beyond buffer length') } if (offset >= buf.length) return return buf[offset] } function _readUInt16 (buf, offset, littleEndian, noAssert) { if (!noAssert) { assert(typeof (littleEndian) === 'boolean', 'missing or invalid endian') assert(offset !== undefined && offset !== null, 'missing offset') assert(offset + 1 < buf.length, 'Trying to read beyond buffer length') } var len = buf.length if (offset >= len) { return } else if (offset + 1 === len) { var dv = new xDataView(new xArrayBuffer(2)) dv.setUint8(0, buf[len - 1]) return dv.getUint16(0, littleEndian) } else { return buf._dataview.getUint16(offset, littleEndian) } } function BufferReadUInt16LE (offset, noAssert) { return _readUInt16(this, offset, true, noAssert) } function BufferReadUInt16BE (offset, noAssert) { return _readUInt16(this, offset, false, noAssert) } function _readUInt32 (buf, offset, littleEndian, noAssert) { if (!noAssert) { assert(typeof (littleEndian) === 'boolean', 'missing or invalid endian') assert(offset !== undefined && offset !== null, 'missing offset') assert(offset + 3 < buf.length, 'Trying to read beyond buffer length') } var len = buf.length if (offset >= len) { return } else if (offset + 3 >= len) { var dv = new xDataView(new xArrayBuffer(4)) for (var i = 0; i + offset < len; i++) { dv.setUint8(i, buf[i + offset]) } return dv.getUint32(0, littleEndian) } else { return buf._dataview.getUint32(offset, littleEndian) } } function BufferReadUInt32LE (offset, noAssert) { return _readUInt32(this, offset, true, noAssert) } function BufferReadUInt32BE (offset, noAssert) { return _readUInt32(this, offset, false, noAssert) } function BufferReadInt8 (offset, noAssert) { var buf = this if (!noAssert) { assert(offset !== undefined && offset !== null, 'missing offset') assert(offset < buf.length, 'Trying to read beyond buffer length') } if (offset >= buf.length) return return buf._dataview.getInt8(offset) } function _readInt16 (buf, offset, littleEndian, noAssert) { if (!noAssert) { assert(typeof (littleEndian) === 'boolean', 'missing or invalid endian') assert(offset !== undefined && offset !== null, 'missing offset') assert(offset + 1 < buf.length, 'Trying to read beyond buffer length') } var len = buf.length if (offset >= len) { return } else if (offset + 1 === len) { var dv = new xDataView(new xArrayBuffer(2)) dv.setUint8(0, buf[len - 1]) return dv.getInt16(0, littleEndian) } else { return buf._dataview.getInt16(offset, littleEndian) } } function BufferReadInt16LE (offset, noAssert) { return _readInt16(this, offset, true, noAssert) } function BufferReadInt16BE (offset, noAssert) { return _readInt16(this, offset, false, noAssert) } function _readInt32 (buf, offset, littleEndian, noAssert) { if (!noAssert) { assert(typeof (littleEndian) === 'boolean', 'missing or invalid endian') assert(offset !== undefined && offset !== null, 'missing offset') assert(offset + 3 < buf.length, 'Trying to read beyond buffer length') } var len = buf.length if (offset >= len) { return } else if (offset + 3 >= len) { var dv = new xDataView(new xArrayBuffer(4)) for (var i = 0; i + offset < len; i++) { dv.setUint8(i, buf[i + offset]) } return dv.getInt32(0, littleEndian) } else { return buf._dataview.getInt32(offset, littleEndian) } } function BufferReadInt32LE (offset, noAssert) { return _readInt32(this, offset, true, noAssert) } function BufferReadInt32BE (offset, noAssert) { return _readInt32(this, offset, false, noAssert) } function _readFloat (buf, offset, littleEndian, noAssert) { if (!noAssert) { assert(typeof (littleEndian) === 'boolean', 'missing or invalid endian') assert(offset + 3 < buf.length, 'Trying to read beyond buffer length') } return buf._dataview.getFloat32(offset, littleEndian) } function BufferReadFloatLE (offset, noAssert) { return _readFloat(this, offset, true, noAssert) } function BufferReadFloatBE (offset, noAssert) { return _readFloat(this, offset, false, noAssert) } function _readDouble (buf, offset, littleEndian, noAssert) { if (!noAssert) { assert(typeof (littleEndian) === 'boolean', 'missing or invalid endian') assert(offset + 7 < buf.length, 'Trying to read beyond buffer length') } return buf._dataview.getFloat64(offset, littleEndian) } function BufferReadDoubleLE (offset, noAssert) { return _readDouble(this, offset, true, noAssert) } function BufferReadDoubleBE (offset, noAssert) { return _readDouble(this, offset, false, noAssert) } function BufferWriteUInt8 (value, offset, noAssert) { var buf = this if (!noAssert) { assert(value !== undefined && value !== null, 'missing value') assert(offset !== undefined && offset !== null, 'missing offset') assert(offset < buf.length, 'trying to write beyond buffer length') verifuint(value, 0xff) } if (offset >= buf.length) return buf[offset] = value } function _writeUInt16 (buf, value, offset, littleEndian, noAssert) { if (!noAssert) { assert(value !== undefined && value !== null, 'missing value') assert(typeof (littleEndian) === 'boolean', 'missing or invalid endian') assert(offset !== undefined && offset !== null, 'missing offset') assert(offset + 1 < buf.length, 'trying to write beyond buffer length') verifuint(value, 0xffff) } var len = buf.length if (offset >= len) { return } else if (offset + 1 === len) { var dv = new xDataView(new xArrayBuffer(2)) dv.setUint16(0, value, littleEndian) buf[offset] = dv.getUint8(0) } else { buf._dataview.setUint16(offset, value, littleEndian) } } function BufferWriteUInt16LE (value, offset, noAssert) { _writeUInt16(this, value, offset, true, noAssert) } function BufferWriteUInt16BE (value, offset, noAssert) { _writeUInt16(this, value, offset, false, noAssert) } function _writeUInt32 (buf, value, offset, littleEndian, noAssert) { if (!noAssert) { assert(value !== undefined && value !== null, 'missing value') assert(typeof (littleEndian) === 'boolean', 'missing or invalid endian') assert(offset !== undefined && offset !== null, 'missing offset') assert(offset + 3 < buf.length, 'trying to write beyond buffer length') verifuint(value, 0xffffffff) } var len = buf.length if (offset >= len) { return } else if (offset + 3 >= len) { var dv = new xDataView(new xArrayBuffer(4)) dv.setUint32(0, value, littleEndian) for (var i = 0; i + offset < len; i++) { buf[i + offset] = dv.getUint8(i) } } else { buf._dataview.setUint32(offset, value, littleEndian) } } function BufferWriteUInt32LE (value, offset, noAssert) { _writeUInt32(this, value, offset, true, noAssert) } function BufferWriteUInt32BE (value, offset, noAssert) { _writeUInt32(this, value, offset, false, noAssert) } function BufferWriteInt8 (value, offset, noAssert) { var buf = this if (!noAssert) { assert(value !== undefined && value !== null, 'missing value') assert(offset !== undefined && offset !== null, 'missing offset') assert(offset < buf.length, 'Trying to write beyond buffer length') verifsint(value, 0x7f, -0x80) } if (offset >= buf.length) return buf._dataview.setInt8(offset, value) } function _writeInt16 (buf, value, offset, littleEndian, noAssert) { if (!noAssert) { assert(value !== undefined && value !== null, 'missing value') assert(typeof (littleEndian) === 'boolean', 'missing or invalid endian') assert(offset !== undefined && offset !== null, 'missing offset') assert(offset + 1 < buf.length, 'Trying to write beyond buffer length') verifsint(value, 0x7fff, -0x8000) } var len = buf.length if (offset >= len) { return } else if (offset + 1 === len) { var dv = new xDataView(new xArrayBuffer(2)) dv.setInt16(0, value, littleEndian) buf[offset] = dv.getUint8(0) } else { buf._dataview.setInt16(offset, value, littleEndian) } } function BufferWriteInt16LE (value, offset, noAssert) { _writeInt16(this, value, offset, true, noAssert) } function BufferWriteInt16BE (value, offset, noAssert) { _writeInt16(this, value, offset, false, noAssert) } function _writeInt32 (buf, value, offset, littleEndian, noAssert) { if (!noAssert) { assert(value !== undefined && value !== null, 'missing value') assert(typeof (littleEndian) === 'boolean', 'missing or invalid endian') assert(offset !== undefined && offset !== null, 'missing offset') assert(offset + 3 < buf.length, 'Trying to write beyond buffer length') verifsint(value, 0x7fffffff, -0x80000000) } var len = buf.length if (offset >= len) { return } else if (offset + 3 >= len) { var dv = new xDataView(new xArrayBuffer(4)) dv.setInt32(0, value, littleEndian) for (var i = 0; i + offset < len; i++) { buf[i + offset] = dv.getUint8(i) } } else { buf._dataview.setInt32(offset, value, littleEndian) } } function BufferWriteInt32LE (value, offset, noAssert) { _writeInt32(this, value, offset, true, noAssert) } function BufferWriteInt32BE (value, offset, noAssert) { _writeInt32(this, value, offset, false, noAssert) } function _writeFloat (buf, value, offset, littleEndian, noAssert) { if (!noAssert) { assert(value !== undefined && value !== null, 'missing value') assert(typeof (littleEndian) === 'boolean', 'missing or invalid endian') assert(offset !== undefined && offset !== null, 'missing offset') assert(offset + 3 < buf.length, 'Trying to write beyond buffer length') verifIEEE754(value, 3.4028234663852886e+38, -3.4028234663852886e+38) } var len = buf.length if (offset >= len) { return } else if (offset + 3 >= len) { var dv = new xDataView(new xArrayBuffer(4)) dv.setFloat32(0, value, littleEndian) for (var i = 0; i + offset < len; i++) { buf[i + offset] = dv.getUint8(i) } } else { buf._dataview.setFloat32(offset, value, littleEndian) } } function BufferWriteFloatLE (value, offset, noAssert) { _writeFloat(this, value, offset, true, noAssert) } function BufferWriteFloatBE (value, offset, noAssert) { _writeFloat(this, value, offset, false, noAssert) } function _writeDouble (buf, value, offset, littleEndian, noAssert) { if (!noAssert) { assert(value !== undefined && value !== null, 'missing value') assert(typeof (littleEndian) === 'boolean', 'missing or invalid endian') assert(offset !== undefined && offset !== null, 'missing offset') assert(offset + 7 < buf.length, 'Trying to write beyond buffer length') verifIEEE754(value, 1.7976931348623157E+308, -1.7976931348623157E+308) } var len = buf.length if (offset >= len) { return } else if (offset + 7 >= len) { var dv = new xDataView(new xArrayBuffer(8)) dv.setFloat64(0, value, littleEndian) for (var i = 0; i + offset < len; i++) { buf[i + offset] = dv.getUint8(i) } } else { buf._dataview.setFloat64(offset, value, littleEndian) } } function BufferWriteDoubleLE (value, offset, noAssert) { _writeDouble(this, value, offset, true, noAssert) } function BufferWriteDoubleBE (value, offset, noAssert) { _writeDouble(this, value, offset, false, noAssert) } // fill(value, start=0, end=buffer.length) function BufferFill (value, start, end) { if (!value) value = 0 if (!start) start = 0 if (!end) end = this.length if (typeof value === 'string') { value = value.charCodeAt(0) } if (typeof value !== 'number' || isNaN(value)) { throw new Error('value is not a number') } if (end < start) throw new Error('end < start') // Fill 0 bytes; we're done if (end === start) return if (this.length === 0) return if (start < 0 || start >= this.length) { throw new Error('start out of bounds') } if (end < 0 || end > this.length) { throw new Error('end out of bounds') } for (var i = start; i < end; i++) { this[i] = value } } function BufferInspect () { var out = [] var len = this.length for (var i = 0; i < len; i++) { out[i] = toHex(this[i]) if (i === exports.INSPECT_MAX_BYTES) { out[i + 1] = '...' break } } return '<Buffer ' + out.join(' ') + '>' } // Creates a new `ArrayBuffer` with the *copied* memory of the buffer instance. // Added in Node 0.12. function BufferToArrayBuffer () { return (new Buffer(this)).buffer } // HELPER FUNCTIONS // ================ function stringtrim (str) { if (str.trim) return str.trim() return str.replace(/^\s+|\s+$/g, '') } /** * Check to see if the browser supports augmenting a `Uint8Array` instance. * @return {boolean} */ function _browserSupport () { var arr = new xUint8Array(0) arr.foo = function () { return 42 } try { return (42 === arr.foo()) } catch (e) { return false } } /** * Class: ProxyBuffer * ================== * * Only used in Firefox, since Firefox does not allow augmenting "native" * objects (like Uint8Array instances) with new properties for some unknown * (probably silly) reason. So we'll use an ES6 Proxy (supported since * Firefox 18) to wrap the Uint8Array instance without actually adding any * properties to it. * * Instances of this "fake" Buffer class are the "target" of the * ES6 Proxy (see `augment` function). * * We couldn't just use the `Uint8Array` as the target of the `Proxy` because * Proxies have an important limitation on trapping the `toString` method. * `Object.prototype.toString.call(proxy)` gets called whenever something is * implicitly cast to a String. Unfortunately, with a `Proxy` this * unconditionally returns `Object.prototype.toString.call(target)` which would * always return "[object Uint8Array]" if we used the `Uint8Array` instance as * the target. And, remember, in Firefox we cannot redefine the `Uint8Array` * instance's `toString` method. * * So, we use this `ProxyBuffer` class as the proxy's "target". Since this class * has its own custom `toString` method, it will get called whenever `toString` * gets called, implicitly or explicitly, on the `Proxy` instance. * * We also have to define the Uint8Array methods `subarray` and `set` on * `ProxyBuffer` because if we didn't then `proxy.subarray(0)` would have its * `this` set to `proxy` (a `Proxy` instance) which throws an exception in * Firefox which expects it to be a `TypedArray` instance. */ function ProxyBuffer (arr) { this._arr = arr if (arr.byteLength !== 0) this._dataview = new xDataView(arr.buffer, arr.byteOffset, arr.byteLength) } ProxyBuffer.prototype.write = BufferWrite ProxyBuffer.prototype.toString = BufferToString ProxyBuffer.prototype.toLocaleString = BufferToString ProxyBuffer.prototype.toJSON = BufferToJSON ProxyBuffer.prototype.copy = BufferCopy ProxyBuffer.prototype.slice = BufferSlice ProxyBuffer.prototype.readUInt8 = BufferReadUInt8 ProxyBuffer.prototype.readUInt16LE = BufferReadUInt16LE ProxyBuffer.prototype.readUInt16BE = BufferReadUInt16BE ProxyBuffer.prototype.readUInt32LE = BufferReadUInt32LE ProxyBuffer.prototype.readUInt32BE = BufferReadUInt32BE ProxyBuffer.prototype.readInt8 = BufferReadInt8 ProxyBuffer.prototype.readInt16LE = BufferReadInt16LE ProxyBuffer.prototype.readInt16BE = BufferReadInt16BE ProxyBuffer.prototype.readInt32LE = BufferReadInt32LE ProxyBuffer.prototype.readInt32BE = BufferReadInt32BE ProxyBuffer.prototype.readFloatLE = BufferReadFloatLE ProxyBuffer.prototype.readFloatBE = BufferReadFloatBE ProxyBuffer.prototype.readDoubleLE = BufferReadDoubleLE ProxyBuffer.prototype.readDoubleBE = BufferReadDoubleBE ProxyBuffer.prototype.writeUInt8 = BufferWriteUInt8 ProxyBuffer.prototype.writeUInt16LE = BufferWriteUInt16LE ProxyBuffer.prototype.writeUInt16BE = BufferWriteUInt16BE ProxyBuffer.prototype.writeUInt32LE = BufferWriteUInt32LE ProxyBuffer.prototype.writeUInt32BE = BufferWriteUInt32BE ProxyBuffer.prototype.writeInt8 = BufferWriteInt8 ProxyBuffer.prototype.writeInt16LE = BufferWriteInt16LE ProxyBuffer.prototype.writeInt16BE = BufferWriteInt16BE ProxyBuffer.prototype.writeInt32LE = BufferWriteInt32LE ProxyBuffer.prototype.writeInt32BE = BufferWriteInt32BE ProxyBuffer.prototype.writeFloatLE = BufferWriteFloatLE ProxyBuffer.prototype.writeFloatBE = BufferWriteFloatBE ProxyBuffer.prototype.writeDoubleLE = BufferWriteDoubleLE ProxyBuffer.prototype.writeDoubleBE = BufferWriteDoubleBE ProxyBuffer.prototype.fill = BufferFill ProxyBuffer.prototype.inspect = BufferInspect ProxyBuffer.prototype.toArrayBuffer = BufferToArrayBuffer ProxyBuffer.prototype._isBuffer = true ProxyBuffer.prototype.subarray = function () { return this._arr.subarray.apply(this._arr, arguments) } ProxyBuffer.prototype.set = function () { return this._arr.set.apply(this._arr, arguments) } var ProxyHandler = { get: function (target, name) { if (name in target) return target[name] else return target._arr[name] }, set: function (target, name, value) { target._arr[name] = value } } function augment (arr) { if (browserSupport === undefined) { browserSupport = _browserSupport() } if (browserSupport) { // Augment the Uint8Array *instance* (not the class!) with Buffer methods arr.write = BufferWrite arr.toString = BufferToString arr.toLocaleString = BufferToString arr.toJSON = BufferToJSON arr.copy = BufferCopy arr.slice = BufferSlice arr.readUInt8 = BufferReadUInt8 arr.readUInt16LE = BufferReadUInt16LE arr.readUInt16BE = BufferReadUInt16BE arr.readUInt32LE = BufferReadUInt32LE arr.readUInt32BE = BufferReadUInt32BE arr.readInt8 = BufferReadInt8 arr.readInt16LE = BufferReadInt16LE arr.readInt16BE = BufferReadInt16BE arr.readInt32LE = BufferReadInt32LE arr.readInt32BE = BufferReadInt32BE arr.readFloatLE = BufferReadFloatLE arr.readFloatBE = BufferReadFloatBE arr.readDoubleLE = BufferReadDoubleLE arr.readDoubleBE = BufferReadDoubleBE arr.writeUInt8 = BufferWriteUInt8 arr.writeUInt16LE = BufferWriteUInt16LE arr.writeUInt16BE = BufferWriteUInt16BE arr.writeUInt32LE = BufferWriteUInt32LE arr.writeUInt32BE = BufferWriteUInt32BE arr.writeInt8 = BufferWriteInt8 arr.writeInt16LE = BufferWriteInt16LE arr.writeInt16BE = BufferWriteInt16BE arr.writeInt32LE = BufferWriteInt32LE arr.writeInt32BE = BufferWriteInt32BE arr.writeFloatLE = BufferWriteFloatLE arr.writeFloatBE = BufferWriteFloatBE arr.writeDoubleLE = BufferWriteDoubleLE arr.writeDoubleBE = BufferWriteDoubleBE arr.fill = BufferFill arr.inspect = BufferInspect arr.toArrayBuffer = BufferToArrayBuffer arr._isBuffer = true if (arr.byteLength !== 0) arr._dataview = new xDataView(arr.buffer, arr.byteOffset, arr.byteLength) return arr } else { // This is a browser that doesn't support augmenting the `Uint8Array` // instance (*ahem* Firefox) so use an ES6 `Proxy`. var proxyBuffer = new ProxyBuffer(arr) var proxy = new Proxy(proxyBuffer, ProxyHandler) proxyBuffer._proxy = proxy return proxy } } // slice(start, end) function clamp (index, len, defaultValue) { if (typeof index !== 'number') return defaultValue index = ~~index; // Coerce to integer. if (index >= len) return len if (index >= 0) return index index += len if (index >= 0) return index return 0 } function coerce (length) { // Coerce length to a number (possibly NaN), round up // in case it's fractional (e.g. 123.456) then do a // double negate to coerce a NaN to 0. Easy, right? length = ~~Math.ceil(+length) return length < 0 ? 0 : length } function isArrayIsh (subject) { return Array.isArray(subject) || Buffer.isBuffer(subject) || subject && typeof subject === 'object' && typeof subject.length === 'number' } function toHex (n) { if (n < 16) return '0' + n.toString(16) return n.toString(16) } function utf8ToBytes (str) { var byteArray = [] for (var i = 0; i < str.length; i++) if (str.charCodeAt(i) <= 0x7F) byteArray.push(str.charCodeAt(i)) else { var h = encodeURIComponent(str.charAt(i)).substr(1).split('%') for (var j = 0; j < h.length; j++) byteArray.push(parseInt(h[j], 16)) } return byteArray } function asciiToBytes (str) { var byteArray = [] for (var i = 0; i < str.length; i++) { // Node's code seems to be doing this and not & 0x7F.. byteArray.push(str.charCodeAt(i) & 0xFF) } return byteArray } function base64ToBytes (str) { return require('base64-js').toByteArray(str) } function blitBuffer (src, dst, offset, length) { var pos, i = 0 while (i < length) { if ((i + offset >= dst.length) || (i >= src.length)) break dst[i + offset] = src[i] i++ } return i } function decodeUtf8Char (str) { try { return decodeURIComponent(str) } catch (err) { return String.fromCharCode(0xFFFD) // UTF 8 invalid char } } /* * We have to make sure that the value is a valid integer. This means that it * is non-negative. It has no fractional component and that it does not * exceed the maximum allowed value. * * value The number to check for validity * * max The maximum value */ function verifuint (value, max) { assert(typeof (value) == 'number', 'cannot write a non-number as a number') assert(value >= 0, 'specified a negative value for writing an unsigned value') assert(value <= max, 'value is larger than maximum value for type') assert(Math.floor(value) === value, 'value has a fractional component') } /* * A series of checks to make sure we actually have a signed 32-bit number */ function verifsint(value, max, min) { assert(typeof (value) == 'number', 'cannot write a non-number as a number') assert(value <= max, 'value larger than maximum allowed value') assert(value >= min, 'value smaller than minimum allowed value') assert(Math.floor(value) === value, 'value has a fractional component') } function verifIEEE754(value, max, min) { assert(typeof (value) == 'number', 'cannot write a non-number as a number') assert(value <= max, 'value larger than maximum allowed value') assert(value >= min, 'value smaller than minimum allowed value') } function assert (test, message) { if (!test) throw new Error(message || 'Failed assertion') } },{"base64-js":3,"typedarray":4}],"native-buffer-browserify":[function(require,module,exports){ module.exports=require('PcZj9L'); },{}],3:[function(require,module,exports){ (function (exports) { 'use strict'; var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; function b64ToByteArray(b64) { var i, j, l, tmp, placeHolders, arr; if (b64.length % 4 > 0) { throw 'Invalid string. Length must be a multiple of 4'; } // the number of equal signs (place holders) // if there are two placeholders, than the two characters before it // represent one byte // if there is only one, then the three characters before it represent 2 bytes // this is just a cheap hack to not do indexOf twice placeHolders = b64.indexOf('='); placeHolders = placeHolders > 0 ? b64.length - placeHolders : 0; // base64 is 4/3 + up to two characters of the original data arr = [];//new Uint8Array(b64.length * 3 / 4 - placeHolders); // if there are placeholders, only get up to the last complete 4 chars l = placeHolders > 0 ? b64.length - 4 : b64.length; for (i = 0, j = 0; i < l; i += 4, j += 3) { tmp = (lookup.indexOf(b64[i]) << 18) | (lookup.indexOf(b64[i + 1]) << 12) | (lookup.indexOf(b64[i + 2]) << 6) | lookup.indexOf(b64[i + 3]); arr.push((tmp & 0xFF0000) >> 16); arr.push((tmp & 0xFF00) >> 8); arr.push(tmp & 0xFF); } if (placeHolders === 2) { tmp = (lookup.indexOf(b64[i]) << 2) | (lookup.indexOf(b64[i + 1]) >> 4); arr.push(tmp & 0xFF); } else if (placeHolders === 1) { tmp = (lookup.indexOf(b64[i]) << 10) | (lookup.indexOf(b64[i + 1]) << 4) | (lookup.indexOf(b64[i + 2]) >> 2); arr.push((tmp >> 8) & 0xFF); arr.push(tmp & 0xFF); } return arr; } function uint8ToBase64(uint8) { var i, extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes output = "", temp, length; function tripletToBase64 (num) { return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]; }; // go through the array every three bytes, we'll deal with trailing stuff later for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) { temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]); output += tripletToBase64(temp); } // pad the end with zeros, but make sure to not forget the extra bytes switch (extraBytes) { case 1: temp = uint8[uint8.length - 1]; output += lookup[temp >> 2]; output += lookup[(temp << 4) & 0x3F]; output += '=='; break; case 2: temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1]); output += lookup[temp >> 10]; output += lookup[(temp >> 4) & 0x3F]; output += lookup[(temp << 2) & 0x3F]; output += '='; break; } return output; } module.exports.toByteArray = b64ToByteArray; module.exports.fromByteArray = uint8ToBase64; }()); },{}],4:[function(require,module,exports){ var undefined = (void 0); // Paranoia // Beyond this value, index getters/setters (i.e. array[0], array[1]) are so slow to // create, and consume so much memory, that the browser appears frozen. var MAX_ARRAY_LENGTH = 1e5; // Approximations of internal ECMAScript conversion functions var ECMAScript = (function() { // Stash a copy in case other scripts modify these var opts = Object.prototype.toString, ophop = Object.prototype.hasOwnProperty; return { // Class returns internal [[Class]] property, used to avoid cross-frame instanceof issues: Class: function(v) { return opts.call(v).replace(/^\[object *|\]$/g, ''); }, HasProperty: function(o, p) { return p in o; }, HasOwnProperty: function(o, p) { return ophop.call(o, p); }, IsCallable: function(o) { return typeof o === 'function'; }, ToInt32: function(v) { return v >> 0; }, ToUint32: function(v) { return v >>> 0; } }; }()); // Snapshot intrinsics var LN2 = Math.LN2, abs = Math.abs, floor = Math.floor, log = Math.log, min = Math.min, pow = Math.pow, round = Math.round; // ES5: lock down object properties function configureProperties(obj) { if (getOwnPropertyNames && defineProperty) { var props = getOwnPropertyNames(obj), i; for (i = 0; i < props.length; i += 1) { defineProperty(obj, props[i], { value: obj[props[i]], writable: false, enumerable: false, configurable: false }); } } } // emulate ES5 getter/setter API using legacy APIs // http://blogs.msdn.com/b/ie/archive/2010/09/07/transitioning-existing-code-to-the-es5-getter-setter-apis.aspx // (second clause tests for Object.defineProperty() in IE<9 that only supports extending DOM prototypes, but // note that IE<9 does not support __defineGetter__ or __defineSetter__ so it just renders the method harmless) var defineProperty = Object.defineProperty || function(o, p, desc) { if (!o === Object(o)) throw new TypeError("Object.defineProperty called on non-object"); if (ECMAScript.HasProperty(desc, 'get') && Object.prototype.__defineGetter__) { Object.prototype.__defineGetter__.call(o, p, desc.get); } if (ECMAScript.HasProperty(desc, 'set') && Object.prototype.__defineSetter__) { Object.prototype.__defineSetter__.call(o, p, desc.set); } if (ECMAScript.HasProperty(desc, 'value')) { o[p] = desc.value; } return o; }; var getOwnPropertyNames = Object.getOwnPropertyNames || function getOwnPropertyNames(o) { if (o !== Object(o)) throw new TypeError("Object.getOwnPropertyNames called on non-object"); var props = [], p; for (p in o) { if (ECMAScript.HasOwnProperty(o, p)) { props.push(p); } } return props; }; // ES5: Make obj[index] an alias for obj._getter(index)/obj._setter(index, value) // for index in 0 ... obj.length function makeArrayAccessors(obj) { if (!defineProperty) { return; } if (obj.length > MAX_ARRAY_LENGTH) throw new RangeError("Array too large for polyfill"); function makeArrayAccessor(index) { defineProperty(obj, index, { 'get': function() { return obj._getter(index); }, 'set': function(v) { obj._setter(index, v); }, enumerable: true, configurable: false }); } var i; for (i = 0; i < obj.length; i += 1) { makeArrayAccessor(i); } } // Internal conversion functions: // pack<Type>() - take a number (interpreted as Type), output a byte array // unpack<Type>() - take a byte array, output a Type-like number function as_signed(value, bits) { var s = 32 - bits; return (value << s) >> s; } function as_unsigned(value, bits) { var s = 32 - bits; return (value << s) >>> s; } function packI8(n) { return [n & 0xff]; } function unpackI8(bytes) { return as_signed(bytes[0], 8); } function packU8(n) { return [n & 0xff]; } function unpackU8(bytes) { return as_unsigned(bytes[0], 8); } function packU8Clamped(n) { n = round(Number(n)); return [n < 0 ? 0 : n > 0xff ? 0xff : n & 0xff]; } function packI16(n) { return [(n >> 8) & 0xff, n & 0xff]; } function unpackI16(bytes) { return as_signed(bytes[0] << 8 | bytes[1], 16); } function packU16(n) { return [(n >> 8) & 0xff, n & 0xff]; } function unpackU16(bytes) { return as_unsigned(bytes[0] << 8 | bytes[1], 16); } function packI32(n) { return [(n >> 24) & 0xff, (n >> 16) & 0xff, (n >> 8) & 0xff, n & 0xff]; } function unpackI32(bytes) { return as_signed(bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3], 32); } function packU32(n) { return [(n >> 24) & 0xff, (n >> 16) & 0xff, (n >> 8) & 0xff, n & 0xff]; } function unpackU32(bytes) { return as_unsigned(bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3], 32); } function packIEEE754(v, ebits, fbits) { var bias = (1 << (ebits - 1)) - 1, s, e, f, ln, i, bits, str, bytes; function roundToEven(n) { var w = floor(n), f = n - w; if (f < 0.5) return w; if (f > 0.5) return w + 1; return w % 2 ? w + 1 : w; } // Compute sign, exponent, fraction if (v !== v) { // NaN // http://dev.w3.org/2006/webapi/WebIDL/#es-type-mapping e = (1 << ebits) - 1; f = pow(2, fbits - 1); s = 0; } else if (v === Infinity || v === -Infinity) { e = (1 << ebits) - 1; f = 0; s = (v < 0) ? 1 : 0; } else if (v === 0) { e = 0; f = 0; s = (1 / v === -Infinity) ? 1 : 0; } else { s = v < 0; v = abs(v); if (v >= pow(2, 1 - bias)) { e = min(floor(log(v) / LN2), 1023); f = roundToEven(v / pow(2, e) * pow(2, fbits)); if (f / pow(2, fbits) >= 2) { e = e + 1; f = 1; } if (e > bias) { // Overflow e = (1 << ebits) - 1; f = 0; } else { // Normalized e = e + bias; f = f - pow(2, fbits); } } else { // Denormalized e = 0; f = roundToEven(v / pow(2, 1 - bias - fbits)); } } // Pack sign, exponent, fraction bits = []; for (i = fbits; i; i -= 1) { bits.push(f % 2 ? 1 : 0); f = floor(f / 2); } for (i = ebits; i; i -= 1) { bits.push(e % 2 ? 1 : 0); e = floor(e / 2); } bits.push(s ? 1 : 0); bits.reverse(); str = bits.join(''); // Bits to bytes bytes = []; while (str.length) { bytes.push(parseInt(str.substring(0, 8), 2)); str = str.substring(8); } return bytes; } function unpackIEEE754(bytes, ebits, fbits) { // Bytes to bits var bits = [], i, j, b, str, bias, s, e, f; for (i = bytes.length; i; i -= 1) { b = bytes[i - 1]; for (j = 8; j; j -= 1) { bits.push(b % 2 ? 1 : 0); b = b >> 1; } } bits.reverse(); str = bits.join(''); // Unpack sign, exponent, fraction bias = (1 << (ebits - 1)) - 1; s = parseInt(str.substring(0, 1), 2) ? -1 : 1; e = parseInt(str.substring(1, 1 + ebits), 2); f = parseInt(str.substring(1 + ebits), 2); // Produce number if (e === (1 << ebits) - 1) { return f !== 0 ? NaN : s * Infinity; } else if (e > 0) { // Normalized return s * pow(2, e - bias) * (1 + f / pow(2, fbits)); } else if (f !== 0) { // Denormalized return s * pow(2, -(bias - 1)) * (f / pow(2, fbits)); } else { return s < 0 ? -0 : 0; } } function unpackF64(b) { return unpackIEEE754(b, 11, 52); } function packF64(v) { return packIEEE754(v, 11, 52); } function unpackF32(b) { return unpackIEEE754(b, 8, 23); } function packF32(v) { return packIEEE754(v, 8, 23); } // // 3 The ArrayBuffer Type // (function() { /** @constructor */ var ArrayBuffer = function ArrayBuffer(length) { length = ECMAScript.ToInt32(length); if (length < 0) throw new RangeError('ArrayBuffer size is not a small enough positive integer'); this.byteLength = length; this._bytes = []; this._bytes.length = length; var i; for (i = 0; i < this.byteLength; i += 1) { this._bytes[i] = 0; } configureProperties(this); }; exports.ArrayBuffer = exports.ArrayBuffer || ArrayBuffer; // // 4 The ArrayBufferView Type // // NOTE: this constructor is not exported /** @constructor */ var ArrayBufferView = function ArrayBufferView() { //this.buffer = null; //this.byteOffset = 0; //this.byteLength = 0; }; // // 5 The Typed Array View Types // function makeConstructor(bytesPerElement, pack, unpack) { // Each TypedArray type requires a distinct constructor instance with // identical logic, which this produces. var ctor; ctor = function(buffer, byteOffset, length) { var array, sequence, i, s; if (!arguments.length || typeof arguments[0] === 'number') { // Constructor(unsigned long length) this.length = ECMAScript.ToInt32(arguments[0]); if (length < 0) throw new RangeError('ArrayBufferView size is not a small enough positive integer'); this.byteLength = this.length * this.BYTES_PER_ELEMENT; this.buffer = new ArrayBuffer(this.byteLength); this.byteOffset = 0; } else if (typeof arguments[0] === 'object' && arguments[0].constructor === ctor) { // Constructor(TypedArray array) array = arguments[0]; this.length = array.length; this.byteLength = this.length * this.BYTES_PER_ELEMENT; this.buffer = new ArrayBuffer(this.byteLength); this.byteOffset = 0; for (i = 0; i < this.length; i += 1) { this._setter(i, array._getter(i)); } } else if (typeof arguments[0] === 'object' && !(arguments[0] instanceof ArrayBuffer || ECMAScript.Class(arguments[0]) === 'ArrayBuffer')) { // Constructor(sequence<type> array) sequence = arguments[0]; this.length = ECMAScript.ToUint32(sequence.length); this.byteLength = this.length * this.BYTES_PER_ELEMENT; this.buffer = new ArrayBuffer(this.byteLength); this.byteOffset = 0; for (i = 0; i < this.length; i += 1) { s = sequence[i]; this._setter(i, Number(s)); } } else if (typeof arguments[0] === 'object' && (arguments[0] instanceof ArrayBuffer || ECMAScript.Class(arguments[0]) === 'ArrayBuffer')) { // Constructor(ArrayBuffer buffer, // optional unsigned long byteOffset, optional unsigned long length) this.buffer = buffer; this.byteOffset = ECMAScript.ToUint32(byteOffset); if (this.byteOffset > this.buffer.byteLength) { throw new RangeError("byteOffset out of range"); } if (this.byteOffset % this.BYTES_PER_ELEMENT) { // The given byteOffset must be a multiple of the element // size of the specific type, otherwise an exception is raised. throw new RangeError("ArrayBuffer length minus the byteOffset is not a multiple of the element size."); } if (arguments.length < 3) { this.byteLength = this.buffer.byteLength - this.byteOffset; if (this.byteLength % this.BYTES_PER_ELEMENT) { throw new Ran